From nobody Sat Feb 7 19:08:15 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 33C73EB64DD for ; Tue, 11 Jul 2023 20:44:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbjGKUoD (ORCPT ); Tue, 11 Jul 2023 16:44:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47798 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbjGKUoB (ORCPT ); Tue, 11 Jul 2023 16:44:01 -0400 Received: from mail-pf1-x431.google.com (mail-pf1-x431.google.com [IPv6:2607:f8b0:4864:20::431]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 87DB7195 for ; Tue, 11 Jul 2023 13:44:00 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-682eef7d752so842153b3a.0 for ; Tue, 11 Jul 2023 13:44:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1689108240; x=1689713040; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=RhnrJF2dPLoBWUU12VjDOt5vBH0HKwmnbELH9GE6Oiw=; b=3aCbceAwkgWqnj5/9VsREWCy67MDdUmA90tFIDvfByUxgd6lufiCgfQow5eLzCwAOr OLtIvB7uwtRtb1XARNCwQorqqwefVLZdlTcfKN4W86BdzonS5yVEStw9AymGna+7pgvL TW5QaH09WQy3ojKNx1oXs/4Tj++BorYbtHoiKFrbgHToLx4dBRJmLxmu5IfK8DhLkKws pGjaec3mdL5+FLZmFB+eXR80DAWnROaRF3etC8mAR080NZ+HMp0JC6RU9y0Du2/33ouz 3/DF3L9z0Xd6uO6G3gnuOKchAW5UcCnwcSByavl1FZ6JGnOqI2jahJ8OTMPQSutH2mXP IZDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689108240; x=1689713040; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=RhnrJF2dPLoBWUU12VjDOt5vBH0HKwmnbELH9GE6Oiw=; b=NjkxesJFyCy08RCo6Y8Uv1R5cFAGDg9sG6PMq0CaTNiBJhPk6kpZJMpUIWqwtUyPM4 M5ULTIHg1WIZfpPSSjQB/LLszUfeo71HJWv5RUekmNTQsa5Gg5sGEKknmku8B5VuQMnO J4MdA/E1silDA2+hqnnWBf93KOplMJRr1mf8MKOJ0u0SLUgC3bwMf7fmGU052uof7r+G NEq+6g/c+kX/XZ0JiB2BNO+DRNxontiiAxou+6Uk4HTnffZTqdq7hoZFnnYKzYa4enWq ++4Qf/wlDGzlRyhVOljurUQCoz+wgxwJiDBhKnbBLofXb0aqS8ngebtBF+5c9Tj6VrSp V7lA== X-Gm-Message-State: ABy/qLaNLhjnb/FQYsfKH+0Wvu9P6Jh0yOBozu4jRnUbaFKjdnUn0KKP Pw73wSN0EHrA8/LGd4FxRDooyA== X-Google-Smtp-Source: APBJJlH1WI4NxORv1Syze1cWnO86O3fT/I1N2IycCPN6Q6rYEkYelyb+zzWE/YFlXO5xjXQ/JgdcGA== X-Received: by 2002:a05:6a00:39a1:b0:67f:8ef5:2643 with SMTP id fi33-20020a056a0039a100b0067f8ef52643mr18904466pfb.2.1689108240070; Tue, 11 Jul 2023 13:44:00 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f7-20020aa78b07000000b00640ddad2e0dsm2124461pfd.47.2023.07.11.13.43.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 13:43:59 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: brauner@kernel.org, arnd@arndb.de, Jens Axboe Subject: [PATCH 1/5] exit: abtract out should_wake helper for child_wait_callback() Date: Tue, 11 Jul 2023 14:43:48 -0600 Message-Id: <20230711204352.214086-2-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230711204352.214086-1-axboe@kernel.dk> References: <20230711204352.214086-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Abstract out the helper that decides if we should wake up following a wake_up() callback on our internal waitqueue. No functional changes intended in this patch. Signed-off-by: Jens Axboe Acked-by: Christian Brauner --- kernel/exit.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index edb50b4c9972..2809dad69492 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1520,6 +1520,17 @@ static int ptrace_do_wait(struct wait_opts *wo, stru= ct task_struct *tsk) return 0; } =20 +static bool pid_child_should_wake(struct wait_opts *wo, struct task_struct= *p) +{ + if (!eligible_pid(wo, p)) + return false; + + if ((wo->wo_flags & __WNOTHREAD) && wo->child_wait.private !=3D p->parent) + return false; + + return true; +} + static int child_wait_callback(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) { @@ -1527,13 +1538,10 @@ static int child_wait_callback(wait_queue_entry_t *= wait, unsigned mode, child_wait); struct task_struct *p =3D key; =20 - if (!eligible_pid(wo, p)) - return 0; + if (pid_child_should_wake(wo, p)) + return default_wake_function(wait, mode, sync, key); =20 - if ((wo->wo_flags & __WNOTHREAD) && wait->private !=3D p->parent) - return 0; - - return default_wake_function(wait, mode, sync, key); + return 0; } =20 void __wake_up_parent(struct task_struct *p, struct task_struct *parent) --=20 2.40.1 From nobody Sat Feb 7 19:08:15 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8079CC001DC for ; Tue, 11 Jul 2023 20:44:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229538AbjGKUoJ (ORCPT ); Tue, 11 Jul 2023 16:44:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47814 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229964AbjGKUoD (ORCPT ); Tue, 11 Jul 2023 16:44:03 -0400 Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 24847195 for ; Tue, 11 Jul 2023 13:44:02 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-682ae5d4184so1334063b3a.1 for ; Tue, 11 Jul 2023 13:44:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1689108241; x=1691700241; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KZsZ8f6snXSrxb5rxjwdGIt7zdNvfSU6bnPqicjdHws=; b=3N0SJijDU79cAiD9z1CvyfDS/0x4aovQkXowXuC+Qe1dNPjhu6fJB7n6LWQMnowsAe gKjPDzKnsWBD/lwJgTSNQfJjXo2iY3sW1mTttU10WMNXtDzZA5helUetsfpokXL0VIeR qTBoIj6i6NslICeWocfhD5cpKS6rzMp1yweglA3MTbo83yNW33FIAEGIjHXn/SoQ10R4 0c/62G9XNASoHReTCnZT5a8jQggpIPw0IrSNebcBBMmqmZz7Kd4c97bpfncwLb3pvQHf XqIU1WdKzQ6bf48mwEaIVRV4aVQharmr6OAiPZ2Cbz6Rzaluuo4ZPzmSvUnzWVUfmKsQ GYhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689108241; x=1691700241; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KZsZ8f6snXSrxb5rxjwdGIt7zdNvfSU6bnPqicjdHws=; b=kJfk7nyzezqncYn3LZtvd2FeIzNB95KSpJmNWRVuucGhWFfQJpvq8WlKOEsVW14x3a IrEfLwUVgzOlk91ucnYDRNNYxafWz400yhuaLBnZ4W6NcOfMFYGxW6WrOiadN2Qs/BjY 1wVNFEmbmlExu+apJnNpMh/0IYQo1lfrCLX0Sb5YCYJl9Hx/AuSTIFGGpZ0h42ZOd31c dT110eOpC3jbF1kxEuxGMgLwPHMnk8gfomfHxsWGBvgIoetargLX83osBLAQkES+AnHL eDrHNFims2ioAsQlJUd5sMdgoHt9EobaEBkoK1Zvr2uBpBnLXDXAL/F4WRAhdsMWIKET kU7Q== X-Gm-Message-State: ABy/qLa5iJM/4Pax5OnPTEjzfWsjFahYgLoY55aRXSTgbxZF2EkjQT9H 1JaaZBPg9eNZNY3re27191/H0g== X-Google-Smtp-Source: APBJJlFUmKm5liBmfPCLe8ejx6eVIvyRfxMcXMOo2baI8F8tmJRbdIQrgiMCuzIJcf+oMF+SPGqA2A== X-Received: by 2002:a05:6a20:3d0d:b0:123:149b:a34f with SMTP id y13-20020a056a203d0d00b00123149ba34fmr22664283pzi.1.1689108241461; Tue, 11 Jul 2023 13:44:01 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f7-20020aa78b07000000b00640ddad2e0dsm2124461pfd.47.2023.07.11.13.44.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 13:44:00 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: brauner@kernel.org, arnd@arndb.de, Jens Axboe Subject: [PATCH 2/5] exit: move core of do_wait() into helper Date: Tue, 11 Jul 2023 14:43:49 -0600 Message-Id: <20230711204352.214086-3-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230711204352.214086-1-axboe@kernel.dk> References: <20230711204352.214086-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Rather than have a maze of gotos, put the actual logic in __do_wait() and have do_wait() loop deal with waitqueue setup/teardown and whether to call __do_wait() again. No functional changes intended in this patch. Signed-off-by: Jens Axboe --- kernel/exit.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index 2809dad69492..d8fb124cc038 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1590,16 +1590,10 @@ static int do_wait_pid(struct wait_opts *wo) return 0; } =20 -static long do_wait(struct wait_opts *wo) +static long __do_wait(struct wait_opts *wo) { - int retval; - - trace_sched_process_wait(wo->wo_pid); + long retval; =20 - init_waitqueue_func_entry(&wo->child_wait, child_wait_callback); - wo->child_wait.private =3D current; - add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); -repeat: /* * If there is nothing that can match our criteria, just get out. * We will clear ->notask_error to zero if we see any child that @@ -1617,18 +1611,18 @@ static long do_wait(struct wait_opts *wo) if (wo->wo_type =3D=3D PIDTYPE_PID) { retval =3D do_wait_pid(wo); if (retval) - goto end; + return retval; } else { struct task_struct *tsk =3D current; =20 do { retval =3D do_wait_thread(wo, tsk); if (retval) - goto end; + return retval; =20 retval =3D ptrace_do_wait(wo, tsk); if (retval) - goto end; + return retval; =20 if (wo->wo_flags & __WNOTHREAD) break; @@ -1638,14 +1632,31 @@ static long do_wait(struct wait_opts *wo) =20 notask: retval =3D wo->notask_error; - if (!retval && !(wo->wo_flags & WNOHANG)) { - retval =3D -ERESTARTSYS; - if (!signal_pending(current)) { - schedule(); - goto repeat; - } - } -end: + if (!retval && !(wo->wo_flags & WNOHANG)) + return -ERESTARTSYS; + + return retval; +} + +static long do_wait(struct wait_opts *wo) +{ + int retval; + + trace_sched_process_wait(wo->wo_pid); + + init_waitqueue_func_entry(&wo->child_wait, child_wait_callback); + wo->child_wait.private =3D current; + add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); + + do { + retval =3D __do_wait(wo); + if (retval !=3D -ERESTARTSYS) + break; + if (signal_pending(current)) + break; + schedule(); + } while (1); + __set_current_state(TASK_RUNNING); remove_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); return retval; --=20 2.40.1 From nobody Sat Feb 7 19:08:15 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id BBCECEB64DC for ; Tue, 11 Jul 2023 20:44:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230408AbjGKUoM (ORCPT ); Tue, 11 Jul 2023 16:44:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47828 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbjGKUoE (ORCPT ); Tue, 11 Jul 2023 16:44:04 -0400 Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 173F810E3 for ; Tue, 11 Jul 2023 13:44:03 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-682ae5d4184so1334067b3a.1 for ; Tue, 11 Jul 2023 13:44:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1689108242; x=1691700242; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=WYDIyeoAajuf9ZWE7uQTqbgmYwdh8IUMjMetvGRNy4k=; b=Kw7OJtlIsWYqkTdCePYJ6j0sZ/wo/9SVbH1Crr+PV4T3hXRLT6ogGGfyIuciZw49Gj cI5oawLufTg1VdoXxqFJTj96IZ9LHiViG8qs9gqHj9oPq4Y+91EB96UEMZsm7r77wu8P Yn5JRCM0rusYPLf30cag4PsOv2r3sDrVrBlFj8C8ONp/aX3iV8zqm3noaS84jfGYo3ha sfPT2yzhH/vVULPlCdqjag8UkzdvJZMrmv6e93tXoG7wSen83LRi+nKcGRZYT5lteNac JU7032+kC7Es/v+DINgPt+3jH99AVZJHNmV2oujw+iOFWZ8fWulhdp34w6aNdBogxLb2 x++g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689108242; x=1691700242; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WYDIyeoAajuf9ZWE7uQTqbgmYwdh8IUMjMetvGRNy4k=; b=KEj+NlNZSKDbESphy3bXJ8sum1Xq1l8vQz8iZ5yxHjICxUC2sFn/WZY4UNKPa0nrOe 1E3ZiPVlihJ38zPUiEo9udcRlXttyw7aXcisDFHHnpjyzxLgjP7bZM4cML7RwMnqitT4 ssahtHjScQEkcVw4TlTfvM7Ozc9MD1p+Nt0frrauIXZ29c7W/YYqL4Re1IhpaVP1Xlrs 8DvqVUHLhwBlt2EIsQALkjoFsFUNhA01qYELLk9Gqov+M31g9lIyRQmRq9CcRFJ477JP /nNjyWtAizVHo3zeQiBEflwXYa+YitcrVZIBlXBoTa+SfHtvWiql2u/RggXf/I9dcD0j KEnQ== X-Gm-Message-State: ABy/qLbi32jY6jV9rx2dcZuJhqkFboJzr2BITTM2aXhLPVVWDO8SLvD6 IrXrECxPE5tv5ZjYgS5m4zkBew== X-Google-Smtp-Source: APBJJlGrBzyDodI3jjldRm2AfTKQR8y4sjoRYFlulavDgrAkLjnAdFzCa2anegPvzfZpkAxtJcCLjQ== X-Received: by 2002:a05:6a20:a128:b0:132:8daa:23eb with SMTP id q40-20020a056a20a12800b001328daa23ebmr2053712pzk.4.1689108242624; Tue, 11 Jul 2023 13:44:02 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f7-20020aa78b07000000b00640ddad2e0dsm2124461pfd.47.2023.07.11.13.44.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 13:44:02 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: brauner@kernel.org, arnd@arndb.de, Jens Axboe Subject: [PATCH 3/5] exit: add kernel_waitid_prepare() helper Date: Tue, 11 Jul 2023 14:43:50 -0600 Message-Id: <20230711204352.214086-4-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230711204352.214086-1-axboe@kernel.dk> References: <20230711204352.214086-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move the setup logic out of kernel_waitid(), and into a separate helper. No functional changes intended in this patch. Signed-off-by: Jens Axboe --- kernel/exit.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index d8fb124cc038..8934c91a9fe1 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1662,14 +1662,12 @@ static long do_wait(struct wait_opts *wo) return retval; } =20 -static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, - int options, struct rusage *ru) +static int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t up= id, + struct waitid_info *infop, int options, + struct rusage *ru, unsigned int *f_flags) { - struct wait_opts wo; struct pid *pid =3D NULL; enum pid_type type; - long ret; - unsigned int f_flags =3D 0; =20 if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED| __WNOTHREAD|__WCLONE|__WALL)) @@ -1703,7 +1701,7 @@ static long kernel_waitid(int which, pid_t upid, stru= ct waitid_info *infop, if (upid < 0) return -EINVAL; =20 - pid =3D pidfd_get_pid(upid, &f_flags); + pid =3D pidfd_get_pid(upid, f_flags); if (IS_ERR(pid)) return PTR_ERR(pid); =20 @@ -1712,19 +1710,34 @@ static long kernel_waitid(int which, pid_t upid, st= ruct waitid_info *infop, return -EINVAL; } =20 - wo.wo_type =3D type; - wo.wo_pid =3D pid; - wo.wo_flags =3D options; - wo.wo_info =3D infop; - wo.wo_rusage =3D ru; - if (f_flags & O_NONBLOCK) - wo.wo_flags |=3D WNOHANG; + wo->wo_type =3D type; + wo->wo_pid =3D pid; + wo->wo_flags =3D options; + wo->wo_info =3D infop; + wo->wo_rusage =3D ru; + if (*f_flags & O_NONBLOCK) + wo->wo_flags |=3D WNOHANG; + + return 0; +} + +static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, + int options, struct rusage *ru) +{ + struct wait_opts wo; + long ret; + unsigned int f_flags =3D 0; + + ret =3D kernel_waitid_prepare(&wo, which, upid, infop, options, ru, + &f_flags); + if (ret) + return ret; =20 ret =3D do_wait(&wo); if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK)) ret =3D -EAGAIN; =20 - put_pid(pid); + put_pid(wo.wo_pid); return ret; } =20 --=20 2.40.1 From nobody Sat Feb 7 19:08:15 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B48BEB64DC for ; Tue, 11 Jul 2023 20:44:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230464AbjGKUoP (ORCPT ); Tue, 11 Jul 2023 16:44:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230059AbjGKUoH (ORCPT ); Tue, 11 Jul 2023 16:44:07 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F0C81710 for ; Tue, 11 Jul 2023 13:44:04 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6748a616e17so1602713b3a.1 for ; Tue, 11 Jul 2023 13:44:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1689108244; x=1689713044; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=C9zc53z+/V9/ECV3tKzh/T4s2rLLXca0QnmcU/Zn8AM=; b=4stpGtuZn7oMLOlDScS37tR4LbmPlFNAC023tmFK1/0CFyIBw0w4A5dau++EKDbUcT ujMsJ7PJofZSUz6kakzpmFt4T3MOOWrdKDgumuKIa0qo29UaJCdMBPCqUKMjBbnuPbMy 1kUratjfGG5pFsUfdUNIUKYG0s/yyYkSr1sIOWjZ0usCzVaBaf4/jXnyrtb0n7dxfUU2 LdnpD/cMhpPlEsp23nUsqMDSNEkkHz5GFPnKGCvwaZFOT31WMZaXOykDMf5keT6EtMF6 FYA59OuZmBp8EN/8e6xd0w747SZ4CksKsINtcvUbHcnrAG7m2vKKPcWbhPBOVhFbxq09 e3zA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689108244; x=1689713044; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=C9zc53z+/V9/ECV3tKzh/T4s2rLLXca0QnmcU/Zn8AM=; b=bSpBybGUT/r2NP25qTDp+fRHN8zUrBOtPBO2Dbg/XRSZRXbB6W4p1VDeFo0e+/aXUI 06tf1y0Rl3PYD3BW3MpNLml+gpVTCoNckydI7xSAgQFTULvnlGwZjh7mwn9DxrIR1Gys Yvi8rY57MIZ6J4jIIWXH0aJA48Turx6HpaErsCo0Oz0p/SmQDDhlaPPGde72Cpg5qWEk 9Q6+yf1NZ6wvJUlVBeqNRXH6EWnqeDbtStnEe2cRb5YmeERFqRfxQv23r8MXxcghFDWL 9lXAfXkA/JFeC7wsg0rKh60/mOeKXQs152nx90Apig9ZcQGydk0d15reKbS+wTwdsXgZ 8BPg== X-Gm-Message-State: ABy/qLbJ4pqLFZXxo0JEQIn4GITBqungwNH6K2Pk8QFMExa/szDnwXPY 29OHBhgwpUL4aD5q9tPoKUhUSg== X-Google-Smtp-Source: APBJJlG5T4xTBJELUr9n8GVlsKOccKtebMyEWZXZh35g0q8hofWjgCQqVZ49aKNw+R/H+/Vi+AwiLA== X-Received: by 2002:aa7:91c9:0:b0:668:834d:4bd with SMTP id z9-20020aa791c9000000b00668834d04bdmr17735349pfa.0.1689108243811; Tue, 11 Jul 2023 13:44:03 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f7-20020aa78b07000000b00640ddad2e0dsm2124461pfd.47.2023.07.11.13.44.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 13:44:03 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: brauner@kernel.org, arnd@arndb.de, Jens Axboe Subject: [PATCH 4/5] exit: add internal include file with helpers Date: Tue, 11 Jul 2023 14:43:51 -0600 Message-Id: <20230711204352.214086-5-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230711204352.214086-1-axboe@kernel.dk> References: <20230711204352.214086-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Move struct wait_opts and waitid_info into kernel/exit.h, and include function declarations for the recently added helpers. Make them non-static as well. This is in preparation for adding a waitid operation through io_uring. With the abtracted helpers, this is now possible. Signed-off-by: Jens Axboe --- kernel/exit.c | 32 +++++++------------------------- kernel/exit.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 25 deletions(-) create mode 100644 kernel/exit.h diff --git a/kernel/exit.c b/kernel/exit.c index 8934c91a9fe1..1c9d1cbadcd0 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -74,6 +74,8 @@ #include #include =20 +#include "exit.h" + /* * The default value should be high enough to not crash a system that rand= omly * crashes its kernel from time to time, but low enough to at least not pe= rmit @@ -1037,26 +1039,6 @@ SYSCALL_DEFINE1(exit_group, int, error_code) return 0; } =20 -struct waitid_info { - pid_t pid; - uid_t uid; - int status; - int cause; -}; - -struct wait_opts { - enum pid_type wo_type; - int wo_flags; - struct pid *wo_pid; - - struct waitid_info *wo_info; - int wo_stat; - struct rusage *wo_rusage; - - wait_queue_entry_t child_wait; - int notask_error; -}; - static int eligible_pid(struct wait_opts *wo, struct task_struct *p) { return wo->wo_type =3D=3D PIDTYPE_MAX || @@ -1520,7 +1502,7 @@ static int ptrace_do_wait(struct wait_opts *wo, struc= t task_struct *tsk) return 0; } =20 -static bool pid_child_should_wake(struct wait_opts *wo, struct task_struct= *p) +bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p) { if (!eligible_pid(wo, p)) return false; @@ -1590,7 +1572,7 @@ static int do_wait_pid(struct wait_opts *wo) return 0; } =20 -static long __do_wait(struct wait_opts *wo) +long __do_wait(struct wait_opts *wo) { long retval; =20 @@ -1662,9 +1644,9 @@ static long do_wait(struct wait_opts *wo) return retval; } =20 -static int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t up= id, - struct waitid_info *infop, int options, - struct rusage *ru, unsigned int *f_flags) +int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid, + struct waitid_info *infop, int options, + struct rusage *ru, unsigned int *f_flags) { struct pid *pid =3D NULL; enum pid_type type; diff --git a/kernel/exit.h b/kernel/exit.h new file mode 100644 index 000000000000..f10207ba1341 --- /dev/null +++ b/kernel/exit.h @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-only +#ifndef LINUX_WAITID_H +#define LINUX_WAITID_H + +struct waitid_info { + pid_t pid; + uid_t uid; + int status; + int cause; +}; + +struct wait_opts { + enum pid_type wo_type; + int wo_flags; + struct pid *wo_pid; + + struct waitid_info *wo_info; + int wo_stat; + struct rusage *wo_rusage; + + wait_queue_entry_t child_wait; + int notask_error; +}; + +bool pid_child_should_wake(struct wait_opts *wo, struct task_struct *p); +long __do_wait(struct wait_opts *wo); +int kernel_waitid_prepare(struct wait_opts *wo, int which, pid_t upid, + struct waitid_info *infop, int options, + struct rusage *ru, unsigned int *f_flags); +#endif --=20 2.40.1 From nobody Sat Feb 7 19:08:15 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CB1AEB64DD for ; Tue, 11 Jul 2023 20:44:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230439AbjGKUoS (ORCPT ); Tue, 11 Jul 2023 16:44:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230038AbjGKUoI (ORCPT ); Tue, 11 Jul 2023 16:44:08 -0400 Received: from mail-pf1-x433.google.com (mail-pf1-x433.google.com [IPv6:2607:f8b0:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F60D171E for ; Tue, 11 Jul 2023 13:44:06 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-66d6a9851f3so980765b3a.0 for ; Tue, 11 Jul 2023 13:44:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1689108245; x=1691700245; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=4n2XJYve3gudseqRv7072PVt+VBZ9vLHlAyLPSwsN/I=; b=hoqlSJMXDZRjzpvMprCZFtEPTuYFxntpTMkogVqVpJuYzu8Z7VT4qsPYmgexTPgYf1 91L7Ks/8w2OrPNdC+VctUI+jIEDikGki82svmKpbv0WvfqCR40J5LPHMc0hYj3FhTEbb 0WbF5XbluoHDZ6RqEFwHA1YMcxM8rbapIIi5iHBiGXT3oDIQ6T7UiftevGB9Z2TpRc8J WsAjaSeSY1+pF0GoZ3YtIy75LmKD+WnbbObBSPPXWaFNUnTbe8uZRapkkvgsagApRp6l m67C1BAzGlrmly7Vbjjw8uKBLGPdCoLxB9Gufo+KD4ODCfLxziMEo+K+pVMsFaLScI7/ /kUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689108245; x=1691700245; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4n2XJYve3gudseqRv7072PVt+VBZ9vLHlAyLPSwsN/I=; b=RBA1sIlEeyridH+lKp/dj4jxDM0z6tlm2K8VzfJCeXAzH+YuwniGn4PfaKX/VpbNQ3 yp8OtWiuPppca4cmKWVpidfcaiFEzETFj8SCyNV9NMR4VNh7fFmU+Xp9GGV57aCTf41R IKr2dcdOjC4Cu7EpBRKi2NFQOEqJLRHumVxl43v7dqCLg7VDHKgoXeMWSWY+KfKheHTv pG35MUmLyAOxJBkxQrRksp+Hus8dRBR3lHlIKsvuZPTo6d4Z3SUwxeIDg38Dvs5PJWWc 3TcWkHAtIoSYs0EUyCcJnsjxpqF/Bk5ApWPFLJ68fPVC0pWQCeWoFijhhmdCkBtCMWxu cWUg== X-Gm-Message-State: ABy/qLY9qWHfUHnDNy6s1AN8nvTgNDx5c1BPNqFakJ/oUKcCYoXF3JJa Aa8zCmB8X4AGL0WwwN5JE5AGxg== X-Google-Smtp-Source: APBJJlE4LNVpMmixgzJr9gZpxGsYoLsbC6xiug0URkD++caxIcUtwzXVNjRxNFEYXRn/HKO4NsRg/A== X-Received: by 2002:a05:6a00:308f:b0:677:bb4c:c321 with SMTP id bh15-20020a056a00308f00b00677bb4cc321mr16851831pfb.0.1689108245363; Tue, 11 Jul 2023 13:44:05 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id f7-20020aa78b07000000b00640ddad2e0dsm2124461pfd.47.2023.07.11.13.44.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 13:44:04 -0700 (PDT) From: Jens Axboe To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: brauner@kernel.org, arnd@arndb.de, Jens Axboe Subject: [PATCH 5/5] io_uring: add IORING_OP_WAITID support Date: Tue, 11 Jul 2023 14:43:52 -0600 Message-Id: <20230711204352.214086-6-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230711204352.214086-1-axboe@kernel.dk> References: <20230711204352.214086-1-axboe@kernel.dk> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This adds support for an async version of waitid(2), in a fully async version. If an event isn't immediately available, wait for a callback to trigger a retry. The format of the sqe is as follows: sqe->len The 'which', the idtype being queried/waited for. sqe->fd The 'pid' (or id) being waited for. sqe->file_index The 'options' being set. sqe->addr2 A pointer to siginfo_t, if any, being filled in. buf_index, add3, and waitid_flags are reserved/unused for now. waitid_flags will be used for options for this request type. One interesting use case may be to add multi-shot support, so that the request stays armed and posts a notification every time a monitored process state change occurs. Note that this does not support rusage, on Arnd's recommendation. See the waitid(2) man page for details on the arguments. Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 2 + include/uapi/linux/io_uring.h | 2 + io_uring/Makefile | 2 +- io_uring/cancel.c | 5 + io_uring/io_uring.c | 3 + io_uring/opdef.c | 9 ++ io_uring/waitid.c | 271 +++++++++++++++++++++++++++++++++ io_uring/waitid.h | 15 ++ 8 files changed, 308 insertions(+), 1 deletion(-) create mode 100644 io_uring/waitid.c create mode 100644 io_uring/waitid.h diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index a7f03d8d879f..598553877fc2 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -276,6 +276,8 @@ struct io_ring_ctx { struct hlist_head futex_list; struct io_alloc_cache futex_cache; =20 + struct hlist_head waitid_list; + const struct cred *sq_creds; /* cred used for __io_sq_thread() */ struct io_sq_data *sq_data; /* if using sq thread polling */ =20 diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 420f38675769..8fca2cffc343 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -66,6 +66,7 @@ struct io_uring_sqe { __u32 msg_ring_flags; __u32 uring_cmd_flags; __u32 futex_flags; + __u32 waitid_flags; }; __u64 user_data; /* data to be passed back at completion time */ /* pack this to avoid bogus arm OABI complaints */ @@ -239,6 +240,7 @@ enum io_uring_op { IORING_OP_FUTEX_WAIT, IORING_OP_FUTEX_WAKE, IORING_OP_FUTEX_WAITV, + IORING_OP_WAITID, =20 /* this goes last, obviously */ IORING_OP_LAST, diff --git a/io_uring/Makefile b/io_uring/Makefile index 2e4779bc550c..e5be47e4fc3b 100644 --- a/io_uring/Makefile +++ b/io_uring/Makefile @@ -8,6 +8,6 @@ obj-$(CONFIG_IO_URING) +=3D io_uring.o xattr.o nop.o fs.o = splice.o \ statx.o net.o msg_ring.o timeout.o \ sqpoll.o fdinfo.o tctx.o poll.o \ cancel.o kbuf.o rsrc.o rw.o opdef.o \ - notif.o + notif.o waitid.o obj-$(CONFIG_IO_WQ) +=3D io-wq.o obj-$(CONFIG_FUTEX) +=3D futex.o diff --git a/io_uring/cancel.c b/io_uring/cancel.c index 3dba8ccb1cd8..a01f3f41012b 100644 --- a/io_uring/cancel.c +++ b/io_uring/cancel.c @@ -16,6 +16,7 @@ #include "poll.h" #include "timeout.h" #include "futex.h" +#include "waitid.h" #include "cancel.h" =20 struct io_cancel { @@ -124,6 +125,10 @@ int io_try_cancel(struct io_uring_task *tctx, struct i= o_cancel_data *cd, if (ret !=3D -ENOENT) return ret; =20 + ret =3D io_waitid_cancel(ctx, cd, issue_flags); + if (ret !=3D -ENOENT) + return ret; + spin_lock(&ctx->completion_lock); if (!(cd->flags & IORING_ASYNC_CANCEL_FD)) ret =3D io_timeout_cancel(ctx, cd); diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 67ff148bc394..6d99d51b84e6 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -93,6 +93,7 @@ #include "net.h" #include "notif.h" #include "futex.h" +#include "waitid.h" =20 #include "timeout.h" #include "poll.h" @@ -336,6 +337,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(str= uct io_uring_params *p) ctx->submit_state.free_list.next =3D NULL; INIT_WQ_LIST(&ctx->locked_free_list); INIT_HLIST_HEAD(&ctx->futex_list); + INIT_HLIST_HEAD(&ctx->waitid_list); INIT_DELAYED_WORK(&ctx->fallback_work, io_fallback_req_func); INIT_WQ_LIST(&ctx->submit_state.compl_reqs); return ctx; @@ -3259,6 +3261,7 @@ static __cold bool io_uring_try_cancel_requests(struc= t io_ring_ctx *ctx, mutex_lock(&ctx->uring_lock); ret |=3D io_poll_remove_all(ctx, task, cancel_all); ret |=3D io_futex_remove_all(ctx, task, cancel_all); + ret |=3D io_waitid_remove_all(ctx, task, cancel_all); mutex_unlock(&ctx->uring_lock); ret |=3D io_kill_timeouts(ctx, task, cancel_all); if (task) diff --git a/io_uring/opdef.c b/io_uring/opdef.c index 2034acfe10d0..2fbdf6a6c24a 100644 --- a/io_uring/opdef.c +++ b/io_uring/opdef.c @@ -34,6 +34,7 @@ #include "cancel.h" #include "rw.h" #include "futex.h" +#include "waitid.h" =20 static int io_no_issue(struct io_kiocb *req, unsigned int issue_flags) { @@ -453,6 +454,10 @@ const struct io_issue_def io_issue_defs[] =3D { .prep =3D io_eopnotsupp_prep, #endif }, + [IORING_OP_WAITID] =3D { + .prep =3D io_waitid_prep, + .issue =3D io_waitid, + }, }; =20 const struct io_cold_def io_cold_defs[] =3D { @@ -681,6 +686,10 @@ const struct io_cold_def io_cold_defs[] =3D { [IORING_OP_FUTEX_WAITV] =3D { .name =3D "FUTEX_WAITV", }, + [IORING_OP_WAITID] =3D { + .name =3D "WAITID", + .async_size =3D sizeof(struct io_waitid_async), + }, }; =20 const char *io_uring_get_opcode(u8 opcode) diff --git a/io_uring/waitid.c b/io_uring/waitid.c new file mode 100644 index 000000000000..8d6ac22113dd --- /dev/null +++ b/io_uring/waitid.c @@ -0,0 +1,271 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Support for async notification of waitid + */ +#include +#include +#include +#include +#include + +#include + +#include "io_uring.h" +#include "cancel.h" +#include "waitid.h" +#include "../kernel/exit.h" + +struct io_waitid { + struct file *file; + int which; + pid_t upid; + int options; + struct wait_queue_head *head; + struct siginfo __user *infop; + struct waitid_info info; +}; + +static void io_waitid_free(struct io_kiocb *req) +{ + struct io_waitid_async *iwa =3D req->async_data; + + put_pid(iwa->wo.wo_pid); + kfree(req->async_data); + req->async_data =3D NULL; + req->flags &=3D ~REQ_F_ASYNC_DATA; +} + +static int io_waitid_finish(struct io_kiocb *req, int ret) +{ + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + int signo =3D 0; + + if (ret > 0) { + signo =3D SIGCHLD; + ret =3D 0; + } + if (!iw->infop) + goto done; + + if (!user_write_access_begin(iw->infop, sizeof(*iw->infop))) { + ret =3D -EFAULT; + goto done; + } + + unsafe_put_user(signo, &iw->infop->si_signo, Efault); + unsafe_put_user(0, &iw->infop->si_errno, Efault); + unsafe_put_user(iw->info.cause, &iw->infop->si_code, Efault); + unsafe_put_user(iw->info.pid, &iw->infop->si_pid, Efault); + unsafe_put_user(iw->info.uid, &iw->infop->si_uid, Efault); + unsafe_put_user(iw->info.status, &iw->infop->si_status, Efault); +done: + user_write_access_end(); + io_waitid_free(req); + return ret; +Efault: + ret =3D -EFAULT; + goto done; +} + +static void io_waitid_complete(struct io_kiocb *req, int ret) +{ + struct io_tw_state ts =3D { .locked =3D true }; + + lockdep_assert_held(&req->ctx->uring_lock); + + /* + * Did cancel find it meanwhile? + */ + if (hlist_unhashed(&req->hash_node)) + return; + + hlist_del_init(&req->hash_node); + + ret =3D io_waitid_finish(req, ret); + if (ret < 0) + req_set_fail(req); + io_req_set_res(req, ret, 0); + io_req_task_complete(req, &ts); +} + +static bool __io_waitid_cancel(struct io_ring_ctx *ctx, struct io_kiocb *r= eq) +{ + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + struct wait_queue_head *head; + + head =3D READ_ONCE(iw->head); + if (head) { + struct io_waitid_async *iwa =3D req->async_data; + + spin_lock_irq(&head->lock); + list_del_init(&iwa->wo.child_wait.entry); + iw->head =3D NULL; + spin_unlock_irq(&head->lock); + io_waitid_complete(req, -ECANCELED); + return true; + } + + return false; +} + +int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, + unsigned int issue_flags) +{ + struct hlist_node *tmp; + struct io_kiocb *req; + int nr =3D 0; + + if (cd->flags & (IORING_ASYNC_CANCEL_FD|IORING_ASYNC_CANCEL_FD_FIXED)) + return -ENOENT; + + io_ring_submit_lock(ctx, issue_flags); + hlist_for_each_entry_safe(req, tmp, &ctx->waitid_list, hash_node) { + if (req->cqe.user_data !=3D cd->data && + !(cd->flags & IORING_ASYNC_CANCEL_ANY)) + continue; + if (__io_waitid_cancel(ctx, req)) + nr++; + if (!(cd->flags & IORING_ASYNC_CANCEL_ALL)) + break; + } + io_ring_submit_unlock(ctx, issue_flags); + + if (nr) + return nr; + + return -ENOENT; +} + +bool io_waitid_remove_all(struct io_ring_ctx *ctx, struct task_struct *tas= k, + bool cancel_all) +{ + struct hlist_node *tmp; + struct io_kiocb *req; + bool found =3D false; + + lockdep_assert_held(&ctx->uring_lock); + + hlist_for_each_entry_safe(req, tmp, &ctx->waitid_list, hash_node) { + if (!io_match_task_safe(req, task, cancel_all)) + continue; + __io_waitid_cancel(ctx, req); + found =3D true; + } + + return found; +} + +static void io_waitid_cb(struct io_kiocb *req, struct io_tw_state *ts) +{ + struct io_waitid_async *iwa =3D req->async_data; + struct io_ring_ctx *ctx =3D req->ctx; + int ret; + + /* + * If we get -ERESTARTSYS here, we need to re-arm and check again + * to ensure we get another callback. If the retry works, then we can + * just remove ourselves from the waitqueue again and finish the + * request. + */ + ret =3D __do_wait(&iwa->wo); + if (unlikely(ret =3D=3D -ERESTARTSYS)) { + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + + io_tw_lock(ctx, ts); + iw->head =3D ¤t->signal->wait_chldexit; + add_wait_queue(iw->head, &iwa->wo.child_wait); + ret =3D __do_wait(&iwa->wo); + if (ret =3D=3D -ERESTARTSYS) + return; + + remove_wait_queue(iw->head, &iwa->wo.child_wait); + iw->head =3D NULL; + } + + io_tw_lock(ctx, ts); + io_waitid_complete(req, ret); +} + +static int io_waitid_wait(struct wait_queue_entry *wait, unsigned mode, + int sync, void *key) +{ + struct wait_opts *wo =3D container_of(wait, struct wait_opts, child_wait); + struct io_waitid_async *iwa =3D container_of(wo, struct io_waitid_async, = wo); + struct io_kiocb *req =3D iwa->req; + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + struct task_struct *p =3D key; + + if (!pid_child_should_wake(wo, p)) + return 0; + + req->io_task_work.func =3D io_waitid_cb; + io_req_task_work_add(req); + iw->head =3D NULL; + list_del_init(&wait->entry); + return 1; +} + +int io_waitid_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + + if (sqe->addr || sqe->buf_index || sqe->addr3 || sqe->waitid_flags) + return -EINVAL; + + iw->which =3D READ_ONCE(sqe->len); + iw->options =3D READ_ONCE(sqe->file_index); + iw->upid =3D READ_ONCE(sqe->fd); + iw->infop =3D u64_to_user_ptr(READ_ONCE(sqe->addr2)); + iw->head =3D NULL; + return 0; +} + +int io_waitid(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_waitid *iw =3D io_kiocb_to_cmd(req, struct io_waitid); + struct io_ring_ctx *ctx =3D req->ctx; + struct io_waitid_async *iwa; + unsigned int f_flags =3D 0; + int ret; + + if (io_alloc_async_data(req)) + return -ENOMEM; + + iwa =3D req->async_data; + iwa->req =3D req; + + ret =3D kernel_waitid_prepare(&iwa->wo, iw->which, iw->upid, &iw->info, + iw->options, NULL, &f_flags); + if (ret) + goto done; + + /* + * Arm our callback and add us to the waitqueue, in case no events + * are available. + */ + init_waitqueue_func_entry(&iwa->wo.child_wait, io_waitid_wait); + iwa->wo.child_wait.private =3D req->task; + iw->head =3D ¤t->signal->wait_chldexit; + add_wait_queue(iw->head, &iwa->wo.child_wait); + + io_ring_submit_lock(ctx, issue_flags); + hlist_add_head(&req->hash_node, &ctx->waitid_list); + + ret =3D __do_wait(&iwa->wo); + if (ret =3D=3D -ERESTARTSYS) { + io_ring_submit_unlock(ctx, issue_flags); + return IOU_ISSUE_SKIP_COMPLETE; + } + + hlist_del_init(&req->hash_node); + remove_wait_queue(iw->head, &iwa->wo.child_wait); + iw->head =3D NULL; + ret =3D io_waitid_finish(req, ret); + + io_ring_submit_unlock(ctx, issue_flags); +done: + if (ret < 0) + req_set_fail(req); + io_req_set_res(req, ret, 0); + return IOU_OK; +} diff --git a/io_uring/waitid.h b/io_uring/waitid.h new file mode 100644 index 000000000000..956a8adafe8c --- /dev/null +++ b/io_uring/waitid.h @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "../kernel/exit.h" + +struct io_waitid_async { + struct io_kiocb *req; + struct wait_opts wo; +}; + +int io_waitid_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); +int io_waitid(struct io_kiocb *req, unsigned int issue_flags); +int io_waitid_cancel(struct io_ring_ctx *ctx, struct io_cancel_data *cd, + unsigned int issue_flags); +bool io_waitid_remove_all(struct io_ring_ctx *ctx, struct task_struct *tas= k, + bool cancel_all); --=20 2.40.1