From patchwork Tue Jul 11 20:43:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13309370 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 99905C001DC for ; Tue, 11 Jul 2023 20:44:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229888AbjGKUoC (ORCPT ); Tue, 11 Jul 2023 16:44:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229911AbjGKUoB (ORCPT ); Tue, 11 Jul 2023 16:44:01 -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 0BB7F1AE for ; Tue, 11 Jul 2023 13:44:01 -0700 (PDT) Received: by mail-pf1-x433.google.com with SMTP id d2e1a72fcca58-676cc97ca74so1392635b3a.1 for ; Tue, 11 Jul 2023 13:44:01 -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=Hd8IFZa4XxbjRMSVGhIcVVcl1ZQ67b9bR0C8n91MOfm8R2CmcvGE4eRE6XI/6pZqi5 zhbuLn6xa7275oGqpx6sERmz0UqHTt2PM7TQFoyhcZugiQ+4g9AaPF2kAxRAOoHs2OKC FXURuzYhu3QYetAyhZWiZGxagrnhdAgunsJYGYRULmKjY5+mdJhr1CXy2c0X25C+bM0d sqWo8q7ggjjKzzFlbK5AwCPmhQlb4vdybkI9fW4vpsRq3A1HMXFI4no3u/ez2aWK9JLx CThrSLLSA/JcHQbc65Rf4EZPyOoI2mMMS8KB5eiYsDWqOgzwVyw5XfLhOTZ3bnK4Wq/B 0KyA== X-Gm-Message-State: ABy/qLZUIKxBwpD8jvtV4JYzD3PTUcnVA3IRG6kp4qOJOFKA0UBqkRDb 8wm0xXPDBjdxwTlO8BI3wfu7Tz8DMYqxvUJPNUk= 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 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org 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, struct task_struct *tsk) return 0; } +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 != 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 = key; - if (!eligible_pid(wo, p)) - return 0; + if (pid_child_should_wake(wo, p)) + return default_wake_function(wait, mode, sync, key); - if ((wo->wo_flags & __WNOTHREAD) && wait->private != p->parent) - return 0; - - return default_wake_function(wait, mode, sync, key); + return 0; } void __wake_up_parent(struct task_struct *p, struct task_struct *parent) From patchwork Tue Jul 11 20:43:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13309372 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 C1B2EEB64DC for ; Tue, 11 Jul 2023 20:44:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230215AbjGKUoH (ORCPT ); Tue, 11 Jul 2023 16:44:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47816 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229959AbjGKUoD (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 874411AE for ; Tue, 11 Jul 2023 13:44:02 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-682b1768a0bso1337193b3a.0 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=ijVI2oZbe6VHQhowR8B4lBRjbdmt5TVCnedDd4STRLZ/ZwojwHEfM/n3Fe7maBgvjx gKPGon1IZsUyxai3D1ozypIIx45zKqjYgjYUmCwVW10Re95uxCjgK3LQJiXrHpgEbamL tKazla/nFyVuZxfymrk8lgatZvThIs7QbILYrEfIkr5/sN7qZFcK3gWgXzliFFghRaZk 2eHyXHE5WpytBnMt4oQeSvdbgN89rsXvjzOO4eY+gwIHqeoscGT0QRBfSCdv1zjtdeNs mq6wZw6MdNEZNkWavNOiZVh+8wI10jxzmQPWebc+uYP1it8kKNjThPKuoy5+/+nUYamL d3cA== X-Gm-Message-State: ABy/qLYYSTPfV86gmqZcj02xnksABqqnYA4sVx7/N2kbGMt3qsCYBh/+ puv87E593bBJMSf4080KcwetoseRK7EbgVZUqS4= 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 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org 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 Acked-by: Christian Brauner --- 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; } -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; - init_waitqueue_func_entry(&wo->child_wait, child_wait_callback); - wo->child_wait.private = 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 == PIDTYPE_PID) { retval = do_wait_pid(wo); if (retval) - goto end; + return retval; } else { struct task_struct *tsk = current; do { retval = do_wait_thread(wo, tsk); if (retval) - goto end; + return retval; retval = ptrace_do_wait(wo, tsk); if (retval) - goto end; + return retval; if (wo->wo_flags & __WNOTHREAD) break; @@ -1638,14 +1632,31 @@ static long do_wait(struct wait_opts *wo) notask: retval = wo->notask_error; - if (!retval && !(wo->wo_flags & WNOHANG)) { - retval = -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 = current; + add_wait_queue(¤t->signal->wait_chldexit, &wo->child_wait); + + do { + retval = __do_wait(wo); + if (retval != -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; From patchwork Tue Jul 11 20:43:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13309371 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 35311EB64DD for ; Tue, 11 Jul 2023 20:44:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230058AbjGKUoI (ORCPT ); Tue, 11 Jul 2023 16:44:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47830 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229903AbjGKUoE (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 8B76C10FA for ; Tue, 11 Jul 2023 13:44:03 -0700 (PDT) Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-682b1768a0bso1337197b3a.0 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=jyvyudN18M/NObseCEk5+uSm1z3RBppTz8o0LKTTzlc/28Kj2CkQUNaoa+bmbbf17k RB9hJGVqJL/t2RZCmri476jAx++BoKqNnaI6sUQFaGH46Ku2ArBdTJ3eq8RcreN0KEhy nknGORvBAdSwTVAexTWFY/+c9t+p2KR5hOZPp9SVs8YW+aMdU0Wuxq/RxRhs67U1yiLS qCiuXd6bs74dJziHjKLK1QuSmiJg9CPGV+Wlcs/EKXkjGrjo0CKkjqoiB0TVRhCEx+Xl +YezegsVUDKrGPEMOPl8p9gMhcf+nRyJsC3D7h0W5vgSv25lSlVWAj3hKbTTQVTGtq7z pP5w== X-Gm-Message-State: ABy/qLZGv9eqC2LJR7OpOHkzC4Ij79GuqXIgYXDgSP2TK05KPGNB03oi +A3WnGkuGM8M4+Twehg6UMRpf5ZeRskh4CsGOsQ= 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 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org 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; } -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 upid, + struct waitid_info *infop, int options, + struct rusage *ru, unsigned int *f_flags) { - struct wait_opts wo; struct pid *pid = NULL; enum pid_type type; - long ret; - unsigned int f_flags = 0; if (options & ~(WNOHANG|WNOWAIT|WEXITED|WSTOPPED|WCONTINUED| __WNOTHREAD|__WCLONE|__WALL)) @@ -1703,7 +1701,7 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, if (upid < 0) return -EINVAL; - pid = pidfd_get_pid(upid, &f_flags); + pid = pidfd_get_pid(upid, f_flags); if (IS_ERR(pid)) return PTR_ERR(pid); @@ -1712,19 +1710,34 @@ static long kernel_waitid(int which, pid_t upid, struct waitid_info *infop, return -EINVAL; } - wo.wo_type = type; - wo.wo_pid = pid; - wo.wo_flags = options; - wo.wo_info = infop; - wo.wo_rusage = ru; - if (f_flags & O_NONBLOCK) - wo.wo_flags |= WNOHANG; + wo->wo_type = type; + wo->wo_pid = pid; + wo->wo_flags = options; + wo->wo_info = infop; + wo->wo_rusage = ru; + if (*f_flags & O_NONBLOCK) + wo->wo_flags |= 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 = 0; + + ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru, + &f_flags); + if (ret) + return ret; ret = do_wait(&wo); if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK)) ret = -EAGAIN; - put_pid(pid); + put_pid(wo.wo_pid); return ret; } From patchwork Tue Jul 11 20:43:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13309373 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 11B1DC0015E for ; Tue, 11 Jul 2023 20:44:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230264AbjGKUoJ (ORCPT ); Tue, 11 Jul 2023 16:44:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230150AbjGKUoH (ORCPT ); Tue, 11 Jul 2023 16:44:07 -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 ECBBE1717 for ; Tue, 11 Jul 2023 13:44:04 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-6831a5caf75so127113b3a.0 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=Z/NNZMRuShDZTdofOWum9+xC6LhOWT9SuyxxVWrJ2J2FQqFF0m3GdYEx9VlqQ+XSBR tQTo276O8KO59LL4RdVWTi1J4pzUE1TwEFgCJ+5kV0FE0p3eYYMipxqCdiXQ3Npa/L6d dUNzPvAUvvHp6Qt43gQ0U761zJdo/lMVYyohX52S7p7zgUJn5h0dBUnpxLCki9YBO8YZ eEc58lswHZ2ymFveS/OvnhAFtQohs45nWFxRjpTWbTfJ5cqLQA7fFYvD8ZIdvqAoq6Xl Ckzbw6fC6coSSLpb8axGfiTyJjCBuxf02x4iAUE/Fi05fyd8qDbdX9aWMpTuMRs/HgG0 hVew== X-Gm-Message-State: ABy/qLbPTnAaVYsQPgaQos/Qu8xPfz46neJb/mt7+nTf/yZ4i5RrcGg+ 05rPyPZETZErCMUf/e4Pwd7cqAwZx9Flwl8SkEw= 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 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org 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 +#include "exit.h" + /* * The default value should be high enough to not crash a system that randomly * crashes its kernel from time to time, but low enough to at least not permit @@ -1037,26 +1039,6 @@ SYSCALL_DEFINE1(exit_group, int, error_code) return 0; } -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 == PIDTYPE_MAX || @@ -1520,7 +1502,7 @@ static int ptrace_do_wait(struct wait_opts *wo, struct task_struct *tsk) return 0; } -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; } -static long __do_wait(struct wait_opts *wo) +long __do_wait(struct wait_opts *wo) { long retval; @@ -1662,9 +1644,9 @@ static long do_wait(struct wait_opts *wo) return retval; } -static 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) +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 = 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 From patchwork Tue Jul 11 20:43:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 13309374 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 AED34C001DE for ; Tue, 11 Jul 2023 20:44:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230295AbjGKUoK (ORCPT ); Tue, 11 Jul 2023 16:44:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47906 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230305AbjGKUoJ (ORCPT ); Tue, 11 Jul 2023 16:44:09 -0400 Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D4A971734 for ; Tue, 11 Jul 2023 13:44:06 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-66d6a9851f3so980767b3a.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=UMqdvvxN16Q3aIXrdteAGWpgr30qN/E9Gumnz9imDmHoKW3XFlqcxGMsdlZL2Nb4ql Axbs+WS3XJuB5VaZWn9fOncwXXFimovhWOiwd0P2r6XwyGNO2yccfkZvL82u7T8cZXEM AN+f0Vf84ro6BB8SZfdS0pWojmP/JA/6F65CsU5SsVkRFWQ0Zv2NvQ++B8t2Rq+vIObk iSaqhZRmwS3Fiyaab0lg9+8tiarw9/Y8mYRWVbLdaq3QdM6xkcoT6GzY8LtMI7Klscq6 b6N0twBE7S1cYLqn5Eo94U01zkmqpeo/OKuVJqYHo1UVlcdlly1fMA1Ql38c6VRaqPVY +sJg== X-Gm-Message-State: ABy/qLZ9/hqcNQXzftwYZHyq0OxQYmiBq880ucgJh28vuGPZsBemGqqW GwdGOzF6408f1vOuti6M8MZyWtU/mJTSAC0r+CM= 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 Precedence: bulk List-ID: X-Mailing-List: io-uring@vger.kernel.org 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; + 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 */ 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, /* 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) += 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) += io-wq.o obj-$(CONFIG_FUTEX) += 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" struct io_cancel { @@ -124,6 +125,10 @@ int io_try_cancel(struct io_uring_task *tctx, struct io_cancel_data *cd, if (ret != -ENOENT) return ret; + ret = io_waitid_cancel(ctx, cd, issue_flags); + if (ret != -ENOENT) + return ret; + spin_lock(&ctx->completion_lock); if (!(cd->flags & IORING_ASYNC_CANCEL_FD)) ret = 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" #include "timeout.h" #include "poll.h" @@ -336,6 +337,7 @@ static __cold struct io_ring_ctx *io_ring_ctx_alloc(struct io_uring_params *p) ctx->submit_state.free_list.next = 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(struct io_ring_ctx *ctx, mutex_lock(&ctx->uring_lock); ret |= io_poll_remove_all(ctx, task, cancel_all); ret |= io_futex_remove_all(ctx, task, cancel_all); + ret |= io_waitid_remove_all(ctx, task, cancel_all); mutex_unlock(&ctx->uring_lock); ret |= 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" 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[] = { .prep = io_eopnotsupp_prep, #endif }, + [IORING_OP_WAITID] = { + .prep = io_waitid_prep, + .issue = io_waitid, + }, }; const struct io_cold_def io_cold_defs[] = { @@ -681,6 +686,10 @@ const struct io_cold_def io_cold_defs[] = { [IORING_OP_FUTEX_WAITV] = { .name = "FUTEX_WAITV", }, + [IORING_OP_WAITID] = { + .name = "WAITID", + .async_size = sizeof(struct io_waitid_async), + }, }; 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 = req->async_data; + + put_pid(iwa->wo.wo_pid); + kfree(req->async_data); + req->async_data = NULL; + req->flags &= ~REQ_F_ASYNC_DATA; +} + +static int io_waitid_finish(struct io_kiocb *req, int ret) +{ + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + int signo = 0; + + if (ret > 0) { + signo = SIGCHLD; + ret = 0; + } + if (!iw->infop) + goto done; + + if (!user_write_access_begin(iw->infop, sizeof(*iw->infop))) { + ret = -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 = -EFAULT; + goto done; +} + +static void io_waitid_complete(struct io_kiocb *req, int ret) +{ + struct io_tw_state ts = { .locked = 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 = 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 *req) +{ + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + struct wait_queue_head *head; + + head = READ_ONCE(iw->head); + if (head) { + struct io_waitid_async *iwa = req->async_data; + + spin_lock_irq(&head->lock); + list_del_init(&iwa->wo.child_wait.entry); + iw->head = 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 = 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 != 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 *task, + bool cancel_all) +{ + struct hlist_node *tmp; + struct io_kiocb *req; + bool found = 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 = true; + } + + return found; +} + +static void io_waitid_cb(struct io_kiocb *req, struct io_tw_state *ts) +{ + struct io_waitid_async *iwa = req->async_data; + struct io_ring_ctx *ctx = 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 = __do_wait(&iwa->wo); + if (unlikely(ret == -ERESTARTSYS)) { + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + + io_tw_lock(ctx, ts); + iw->head = ¤t->signal->wait_chldexit; + add_wait_queue(iw->head, &iwa->wo.child_wait); + ret = __do_wait(&iwa->wo); + if (ret == -ERESTARTSYS) + return; + + remove_wait_queue(iw->head, &iwa->wo.child_wait); + iw->head = 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 = container_of(wait, struct wait_opts, child_wait); + struct io_waitid_async *iwa = container_of(wo, struct io_waitid_async, wo); + struct io_kiocb *req = iwa->req; + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + struct task_struct *p = key; + + if (!pid_child_should_wake(wo, p)) + return 0; + + req->io_task_work.func = io_waitid_cb; + io_req_task_work_add(req); + iw->head = 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 = io_kiocb_to_cmd(req, struct io_waitid); + + if (sqe->addr || sqe->buf_index || sqe->addr3 || sqe->waitid_flags) + return -EINVAL; + + iw->which = READ_ONCE(sqe->len); + iw->options = READ_ONCE(sqe->file_index); + iw->upid = READ_ONCE(sqe->fd); + iw->infop = u64_to_user_ptr(READ_ONCE(sqe->addr2)); + iw->head = NULL; + return 0; +} + +int io_waitid(struct io_kiocb *req, unsigned int issue_flags) +{ + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + struct io_ring_ctx *ctx = req->ctx; + struct io_waitid_async *iwa; + unsigned int f_flags = 0; + int ret; + + if (io_alloc_async_data(req)) + return -ENOMEM; + + iwa = req->async_data; + iwa->req = req; + + ret = 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 = req->task; + iw->head = ¤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 = __do_wait(&iwa->wo); + if (ret == -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 = NULL; + ret = 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 *task, + bool cancel_all);