From patchwork Fri Aug 11 14:16:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 134559 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1174302vqi; Fri, 11 Aug 2023 08:29:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH15ffZ+KslU7Wvwsfdn2MTB+hDS/HQ0aPE+yjsQmIx7iI+6l4tEiZdnNVGimlGniymEwYx X-Received: by 2002:a17:906:11:b0:99c:dc3e:f8e3 with SMTP id 17-20020a170906001100b0099cdc3ef8e3mr2076977eja.28.1691767740911; Fri, 11 Aug 2023 08:29:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691767740; cv=none; d=google.com; s=arc-20160816; b=DLRf/xwjfV4IaTAdiCkM396hVviaNSmNwvNfDv9btgMlbydAAL29Mgbfr0x08iJRLp zUgukNTUAVoWyic+QDfFFgRWa9j4XTCz6XxT90IJyEixyolvxtQtuEOXsjw5MPKYEwZD +Wjog6TijpcM0UHY8YqzraE82ReJV/0kZGd1Qo947WAcEuHyqqCJz3DvFQhZTTuQ4SBS D+IayjcGuHO9dcEhP5zlSTBDpxpvPWKnTrlIlSSs+9DILisPsVPQAhd/WJ33zbDmdkFQ 05d8HwQEiVkJ39eRfY8YEWjHCs8nFdV+ay/XN16ZBQv04PH98gcwuyv6cH99tsLFRK1a yCHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0W4hkYYGlPKafnFtIfHw3u5JfmMzy8zkW1KZ07xxWOU=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=DY50tqcs+Xdbbfpq6Ji3yJuM2ZmG+IWYQCLs2LJ/x0bCzgWpo44GFCKR8evSf51OXZ aF1U0WeXLkA30XyHjR1nWeNvPwGrLGzUKFlvKt+puagEU1z/1tmNbfjYwUNWIg/YbP9Z MOxaFNHvxuSv6DsOy0qjvDvuc0Bwa+6ilFFaPTbsTvhN3tCQESBphb3CMlrqPKgwIGQA 6wHAFwSpmcZrHSj8axc8Ja8eUUiKDZyB9JCk1NZJ3aJFIzPem7sKsnuKe24I8kZmBBcg fgUZ5iYPRSMXNRP+suRw5BUYrq6ROVgaZpmgpm3e69pgJCdL5mw8FaCt27Yt3TAAfPuV AgrQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=KzQycaVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a24-20020a170906245800b0099bd0683c1esi3277971ejb.949.2023.08.11.08.28.36; Fri, 11 Aug 2023 08:29:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=KzQycaVb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235446AbjHKOQc (ORCPT + 99 others); Fri, 11 Aug 2023 10:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235244AbjHKOQa (ORCPT ); Fri, 11 Aug 2023 10:16:30 -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 379FBFA for ; Fri, 11 Aug 2023 07:16:30 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-68781a69befso348187b3a.0 for ; Fri, 11 Aug 2023 07:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691763390; x=1692368190; 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=0W4hkYYGlPKafnFtIfHw3u5JfmMzy8zkW1KZ07xxWOU=; b=KzQycaVbeQFqn6gTdLfSsgzYGwAkhqL6P3muxw2AlRiyuWbnNPG8OuNJ/N0p6RPBUc fuIkGlIMxfVAsZ505aZKk+mLHx4KhxGZQ5AdYJqQ2MnuQL3oJZ6ILW3TRl+uUH6vhtOJ wPcvXOp/Bi5hwk6simuX6VR0kBwYBAt6Cqos2/VeKV6N4OXymfd3kHtP5QcP003+Pwcs Z6oGoKjL4NQZqXadRpaTOsnO0dZfcAH58YrOSWdIUEqY+Ba5yRH1Q2BsUAIu1CV+l2W+ QMOcOzTC7K9nGnxDbB3LtBDOznBzg6+wrSYlZMZDEpi3qmKsu1C4ts3UI8nGvkNM61Bp FASA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691763390; x=1692368190; 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=0W4hkYYGlPKafnFtIfHw3u5JfmMzy8zkW1KZ07xxWOU=; b=F3s8u6P0VAKpAEl9BxlXfdL4eVufB446K2Bd4Nsqig9M2PPUhjzq+6h1+O0pYny0k9 XZw6Sd14zVwHcN57i1SetluyJEdF38+patgIe0Z229+uhvcBaAps14rpYf04jo7NNO+X 64YUYuVF4h8uL8Ow+FmOUKQudTooK5t2X9ULCEMhYyuGBMB07SQF0v5u+6Gwju9bAMxm 3Uni1F0XcAf2JNRYfhPNL01Mn509dsO5H2ZT81GeEtNo5QRUxnJLrc1wMjFVolGow/cG WqBQOLGdgZRN3WFzIqS0PnFVQGM/PJ+gbTKRTehsfYmrWljF005K4xSSjR33Vic1lGXO k7qA== X-Gm-Message-State: AOJu0YwrDEGh0VtVTmVE47rNvM8ddWkOBss3RdZQ3xcIaHTaRwjlsey8 P7LE4hRjr9+/x88gLjSCSkRLOOMfSHwLKMuwU7M= X-Received: by 2002:a05:6a00:4986:b0:67d:308b:97ef with SMTP id dn6-20020a056a00498600b0067d308b97efmr2489718pfb.2.1691763389670; Fri, 11 Aug 2023 07:16:29 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s21-20020a639255000000b00564ca424f79sm3422311pgn.48.2023.08.11.07.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:16:29 -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: abstract out should_wake helper for child_wait_callback() Date: Fri, 11 Aug 2023 08:16:22 -0600 Message-Id: <20230811141626.161210-2-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811141626.161210-1-axboe@kernel.dk> References: <20230811141626.161210-1-axboe@kernel.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773947050323495661 X-GMAIL-MSGID: 1773947050323495661 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. Acked-by: Christian Brauner Signed-off-by: Jens Axboe --- 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 Fri Aug 11 14:16:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 134551 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1161849vqi; Fri, 11 Aug 2023 08:08:27 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEyx0T9yNTLlKexGHjzXpNbnQ7tFj9FiZzk6XawVdXM6R5zSSL6vqx3xZC+xzIAeUuttrim X-Received: by 2002:a17:902:ecce:b0:1af:e302:123 with SMTP id a14-20020a170902ecce00b001afe3020123mr2675763plh.3.1691766507145; Fri, 11 Aug 2023 08:08:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691766507; cv=none; d=google.com; s=arc-20160816; b=Nkdqv88YXmuGBfd5KTX6ECYsIKQea2zPFfvrmyTb0T1Qmx1/OJEbBsUQ8ubtBMsRey bSymbL8iqJanCzfFwz99SFcCptFlPxTFaejGsi1MOjWbOjRR8nkGPqZ8AAmvGOplKq3w XT92UKJjUWjCUqoBLA8jtPgAE5/kqxAfNrd8+NBm0gx5lK9dmS0WTcv+p7K/1C+e1YNQ 52QzDdiZ93nk1iWD0MShFNdS26xMvcx6wbUwvkChcToJ9u97hAOX9kMx55S3ObxFBFTg JpJ+VnqgZPN4tq22v25LqNp41y3faBRnrsgxgmEkIWKh89pbw/mMqaHOQlK6JaGdAWXY l+yw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qOKoMn5bZK+SN77CCIJEeYWpwJKTharQ/mIzkTPuUOw=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=fTs7LKB0qTKdcyS09q80cvaMFmtQsXupx9hCfzbMZwil/o+sHO4wOLmwb3mzy8URHk mdg6u/vh3dDPtj1QwdDq1LSjVM/Vs8b2X+MS4VRGUQLtkqPBft4m9deBUL5innDz4A2D 16d4WyuDHyXCIWl+WKt6NXzYFgHavUC6VX6zC+2TAoMUxmNoj9i5sd/ZXbz9p0gWYqAo tp/03I3GpnQUKMUrKouJv4gGor7bBLO5RY3z3gIZ+Tz4nMolfSQbIueHxRm6xB+XLs1N lLDg9Zfjz4MhEUWWC7X6kOKBbUqEfhI8EsJmy2AGlliu4f+C9d7J9OvkwK+Ts+KgS30F xUUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=Fr5qDzmr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h17-20020a170902f7d100b001bba53994acsi3476571plw.243.2023.08.11.08.08.12; Fri, 11 Aug 2023 08:08:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=Fr5qDzmr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235606AbjHKOQe (ORCPT + 99 others); Fri, 11 Aug 2023 10:16:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60636 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233485AbjHKOQc (ORCPT ); Fri, 11 Aug 2023 10:16:32 -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 751E0FC for ; Fri, 11 Aug 2023 07:16:31 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6872c60b572so414111b3a.1 for ; Fri, 11 Aug 2023 07:16:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691763391; x=1692368191; 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=qOKoMn5bZK+SN77CCIJEeYWpwJKTharQ/mIzkTPuUOw=; b=Fr5qDzmrAy1xDvekdT2iNkrexabX/hs5iQLUXxadawHbBbzk+RSE46f718kVopldKR MImJ/7himOqEyn8S1veUmdA7+ou5itO5weo/m20hLMFIRnwxvfaAmOzRY0zB9vs/5sD8 kwRe6HJJPhmMTVvj+xMytfw/99DO4JJq82w02ZzCfr9ZvnB57t/IH5nO+nJ3BUFOUvUV sJ32FRfSuafhtEu8GTvH0wTm5B2uLXEFHneJht4Thm6LOo4PwW+55ylkELretTuQ+JY9 D63zYdzkqz9n4OYQonmnzjLHdNxfFsEoPiiJhUfMfHTa7ApGtKueR+CBBe63bdgIo0wj fFag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691763391; x=1692368191; 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=qOKoMn5bZK+SN77CCIJEeYWpwJKTharQ/mIzkTPuUOw=; b=jKG9CGOTrnY3xhgR7rNEGMdPAgSPLRwJ1tSc48/kdP2cdr5IyDU8WeoObIsgpy+tTZ asH/Tm2nuksJIcyHIQc0vZmQnWjD6i0QkCa5kvBWN7vaEkVRHRe9QB2mo1LihiS5R64R Act+gWjzfPnaR8x9DgZWUOzJOzqyHrt2Hacae50rJhrEthjGwakwu+HU6spLs32j+cRZ TbriYmuJFY8fVVn4JQzfa5MZviMxpYO6+p2l4FamTVDbI3sNc/04gLMCo9LJslh72iq+ 89afXe/zp6majlg2mRGRhovpbz42qiTshqxVJLRxb0PWTMazyYmtasihI4+5+l7H19cD 8Mdg== X-Gm-Message-State: AOJu0Yyd1A4+FEpzx3PSO8yxuSpj0USjkOgbekYUxj+z9yMe/CAO8ruv mNBt2f5Y4NUk2VfY2BfGmMAAOw== X-Received: by 2002:a05:6a21:6d92:b0:13e:1d49:7249 with SMTP id wl18-20020a056a216d9200b0013e1d497249mr3188783pzb.2.1691763390888; Fri, 11 Aug 2023 07:16:30 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s21-20020a639255000000b00564ca424f79sm3422311pgn.48.2023.08.11.07.16.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:16:30 -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: Fri, 11 Aug 2023 08:16:23 -0600 Message-Id: <20230811141626.161210-3-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811141626.161210-1-axboe@kernel.dk> References: <20230811141626.161210-1-axboe@kernel.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773945757093776077 X-GMAIL-MSGID: 1773945757093776077 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. Acked-by: Christian Brauner 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; } -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 Fri Aug 11 14:16:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 134541 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1150414vqi; Fri, 11 Aug 2023 07:51:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFzd4ZeRDNq0ISCwwy7jwvt5fUamo6Y/rC3eDNYz6UqtUTJ7vQn1WicLktoNt44Rqvg5IE0 X-Received: by 2002:a05:6a20:160b:b0:140:d536:d434 with SMTP id l11-20020a056a20160b00b00140d536d434mr2870428pzj.10.1691765498758; Fri, 11 Aug 2023 07:51:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691765498; cv=none; d=google.com; s=arc-20160816; b=geppowNP1AT9kJHMA8JzkXyyAMEaox+MjJpH6OqCm/vcHDkB1aiHg/XvfuQEbJBn0N PAS3NVU56xw/MjUYIOPmAPTBkKzrJ0yl2P8AdCsYlKDS7PdN0bFXPt6FrafcLIoWL479 Q0I2KAz4lrRoua41tamN64b7bESxLqu9o/SmLYa2B1TtoEnov1dVq4pH5tYOArdXFWoq NIkHEoyOLZVDOrlMTvvgDSbY0LdSZbKyWAEC7PcrJEd08dgIf6QwdomlWjiJsJvffmm5 HxClaDDJjDqRF4g6PQua4t4UqYRPyjLYNbTkf09I4DdJbM0YHxXs40vJWl34JD9j7PRX aTTw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=7SdvGakOC76UVVqy+Q7KBbtdiqly+wN0IeNy1TtcFHw=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=PHBNQ/mipKaznuWu/BfDrNrzXpEjzUBWcO+G3p7d/nBJfMQIBQh/Eaiyo+wwndjrhV nL98feZuS3KeRBLMt5laqg8ejzTasBsiw9buEDhznTsqiKYrCuN29wbC8qTKfhkDbLIP G+aF4jgzmavCOBxRnVad2bAHjZhaG1sB23MaDQFrZIWj85QJhD2bRYOuEe2ESCtUk2ev iPkp+gWXms60pmfz7v1YSO089IoFy+yY4M0x5AVT161//EzC+/fBPd41nd/J7piDOmY5 gTwfelgXeAFq9gKUgPTnfv3cfwGK+U8w1yuz5MfOD2UKlC4oqfa6mN/nDuxV08fC+S19 bm+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=yZ0jizWL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a7-20020a634d07000000b00563e0e86f17si3603349pgb.573.2023.08.11.07.51.24; Fri, 11 Aug 2023 07:51:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=yZ0jizWL; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235902AbjHKOQs (ORCPT + 99 others); Fri, 11 Aug 2023 10:16:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235584AbjHKOQd (ORCPT ); Fri, 11 Aug 2023 10:16:33 -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 C4CC8FC for ; Fri, 11 Aug 2023 07:16:32 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6874a386ec7so412809b3a.1 for ; Fri, 11 Aug 2023 07:16:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691763392; x=1692368192; 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=7SdvGakOC76UVVqy+Q7KBbtdiqly+wN0IeNy1TtcFHw=; b=yZ0jizWLa+0++mgdS1Vx1V5O0p2BLQ4scMOevPUBIl4x8laXJA/aIp0EdjmJz/h/Ua GSQkF52hwg8GF5x28SG7aAqxb+2dp3XK+JF4ZJ/eJfSn2XqBLt6lb1poWLcWV/Fkur4y LCQ+UN1nP2ogkioPaom+xeJrhN+zoxR+/nCe9ur80IwhWo+bczxcjDdDaFEIZ2XrtLV0 0Oo9ummWzCQiDvPP2Ov2Lbz6dSrhtbUL6s5fezYc3uXXNPqbAo4YTCHpBjeQ+gDTeQmr mTSaOGrin7EPkIfPsdViCmwW3wArMNbxEPkVGyqNBMryUCAPet4Qj6V3vv/D0oo+2Q4r 6p9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691763392; x=1692368192; 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=7SdvGakOC76UVVqy+Q7KBbtdiqly+wN0IeNy1TtcFHw=; b=DJ/PanbC+b5adx4lNv8ruXzoeARz/epGE8WsU+Gz3r9i2wUxuYVHChQu21EH2/VApt 9oNKfhW2omN0d/Izg99ILvpIYpeqD0SKzl0UCZlIvEOFT8rHoM0AMLkNI5a8j2JVToyv Uk/j2HPnDkB6WM+/B2iLssibMuBXBMHuPVDq+XCRGYNU7L/yFOQXqZIUZRBGHLNM0wY2 Mnfn28G3Cgx1biJPhUsts2wYL11E472q24N1vJZy9+9vqxKkO9B4YSMYB3LCWGSNm7g3 X+gl+LmjM9zWgxrfEIOD1W0fmO1+ynWQKAbWCCGPKdcC4R9BZ3CWXXlWBCwcXgDTxf1L 74tQ== X-Gm-Message-State: AOJu0YxguQz0PnERAQ+ff2pzz6Eb5XIdZOP11IEXm53JxIhG3Bnah5Y+ ZqBVFHG+5j8UrEHQWHawZmUbtw== X-Received: by 2002:a05:6a21:6d88:b0:13f:9233:58d with SMTP id wl8-20020a056a216d8800b0013f9233058dmr3197361pzb.2.1691763392274; Fri, 11 Aug 2023 07:16:32 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s21-20020a639255000000b00564ca424f79sm3422311pgn.48.2023.08.11.07.16.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:16:31 -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: Fri, 11 Aug 2023 08:16:24 -0600 Message-Id: <20230811141626.161210-4-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811141626.161210-1-axboe@kernel.dk> References: <20230811141626.161210-1-axboe@kernel.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773944699451926335 X-GMAIL-MSGID: 1773944699451926335 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 | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/kernel/exit.c b/kernel/exit.c index d8fb124cc038..5c4cd1769641 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1662,14 +1662,13 @@ 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) { - struct wait_opts wo; + unsigned int f_flags = 0; 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)) @@ -1712,19 +1711,32 @@ 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; + 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_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; + + ret = kernel_waitid_prepare(&wo, which, upid, infop, options, ru); + if (ret) + return ret; ret = do_wait(&wo); - if (!ret && !(options & WNOHANG) && (f_flags & O_NONBLOCK)) + if (!ret && !(options & WNOHANG) && (wo.wo_flags & WNOHANG)) ret = -EAGAIN; - put_pid(pid); + put_pid(wo.wo_pid); return ret; } From patchwork Fri Aug 11 14:16:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 134537 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1145566vqi; Fri, 11 Aug 2023 07:43:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEFNyutob4r4ilFtbgLCZ9SMWU8UvHZxsmYBp8+bcCsiGm7v1/3HS2K+E67vIjXn8C8qJcx X-Received: by 2002:a17:906:1d1:b0:99c:55c5:1c6e with SMTP id 17-20020a17090601d100b0099c55c51c6emr2667540ejj.8.1691764983041; Fri, 11 Aug 2023 07:43:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691764983; cv=none; d=google.com; s=arc-20160816; b=SZgNZBvcFUtP6DcihCVVXBOwii9LeJRifW719L9KULu8ZbNHxaqQCtrFVM/gCYVgSG B2WDe3vpiPo4C6zimYCidFiy5lRIZkUPepJcDsGI3a/AfsuJGkVaiyhmNRp8j2V1Ob4L DeLaioO8w9J3iwxB0+dp2YVUfE4H8OHhovZHfayRIYKmatTDR+J32mVGTxE5dC578FHE tIQvP8u7hKBseIYneiq4RW0d3nekhcf0d8oKl1kRV77ogyncrcEiLJ9m6m60AF1wrvlW Gn/cLcyk670DkLi8oHyXSaTGsCZRuEdMMtRD4387Y7QnR2rnJidne2SP6eozdPgVBvn1 2ZHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=V8yRFdlpefOfywdA2XDRJ2uYVWVe52WNVP5jsRYSyGo=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=e8tRrNWi8Ykx0Aa93iFKpQs2uGqLdKaHCy8ADOZ09JeVyB9lfUE/2SGbx2swqgSoXx gi3Js14YJsvXVyuFZwJbeuVbXPQqZvmTmniZqEWfGoS0g9K6kyHXNGwppKTjFBBMChL1 5NHBGhaU8D+R3KRsyOx+i7ILa/yGGCIQsEmEpza7ayBgo/Yjq9aZi5f8Lpp+B5hEdoUr TeqPyDMP868cTI0UMEE+22zTDnIidLeLrU/VqHFs1agdK1QTRfcgWYpBeG7siX0qsICZ owTHl9O8b7X1bTcP8y3hfI/M6seO4nkva3cUBoot9z1kzVsCrPVTrXsjN5w6k1XgrvQ8 Sdow== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=ZDotbKvY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id d23-20020a170906641700b0098df03ffa69si3371167ejm.421.2023.08.11.07.42.38; Fri, 11 Aug 2023 07:43:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=ZDotbKvY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235737AbjHKOQm (ORCPT + 99 others); Fri, 11 Aug 2023 10:16:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235664AbjHKOQj (ORCPT ); Fri, 11 Aug 2023 10:16:39 -0400 Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFCF92696 for ; Fri, 11 Aug 2023 07:16:33 -0700 (PDT) Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1bda3d0f0f1so2272535ad.0 for ; Fri, 11 Aug 2023 07:16:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691763393; x=1692368193; 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=V8yRFdlpefOfywdA2XDRJ2uYVWVe52WNVP5jsRYSyGo=; b=ZDotbKvYQpcsRvEvVcLhD9yzgNFRHqGHIk/JGuRkq9Uz0HhmBUEb+HOJhsR25jxre+ FU2Hn0XtaQxsP9c08ZJuE2L51GhiD2m38JIowgriCNuZI3MX3IZo1F9OqF1rGp/s+QGp Pwxs4cCSTf0fywZR3wgbgxvNdO/2D3h4Ua6t8YcBxG68VPzr6vLYHkBZMRY7GE7QnQKw 4RPcD3/lT6xvQLHagl9FqPvwa9P0r8gOC0N07CuZhWyo/po5NCzs+Q4pK0RWjIJi8GTt pvlmAVmo1soP9YPkqgP2YXur88r9Ue613rtG2g7bTPl0Cxjo4K9vRpLJlyLG0qh6LAsI Qgog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691763393; x=1692368193; 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=V8yRFdlpefOfywdA2XDRJ2uYVWVe52WNVP5jsRYSyGo=; b=Bje6b9yxg7dk4bGgh6GKbxUz8YJn1KSLaDlwsyipdeSiKCXb+wvssYCDFH9cpiCCLM xO6e8qAPvv9ohqyzEHuW5YNLAVq5D8jvhY2IWji7J0VN9OyqPN19qGTQpjgvOAJmnQ2T eLelKuqRKTbkED6alHG/HwluDZMEwuj4JWSEmM8z/Ak5n4oQrVLhLXQCj53nLWMgh8bT iVtSOk3ZZXuZ2kyq0KIP1eEdGnyv40Tk64vaTJLIZ3l7uwPjOF/5AKcVmqW8QM1vrR/d XNS/OUFeRtTxOqDiaBUy8uPmvyueP377nVRp7oIanqVn9d+YeRO3YtmrlLLiCIU3cVEL iqMQ== X-Gm-Message-State: AOJu0YwWjgRfJAEEExxhuGLIsTZeC+Pmnp7ei7hGIOsXsPlZb+J8Hy9+ 65AOBN6trVQXtVLec1EVos/3xw== X-Received: by 2002:a17:902:e745:b0:1bb:ac37:384b with SMTP id p5-20020a170902e74500b001bbac37384bmr2247979plf.6.1691763393370; Fri, 11 Aug 2023 07:16:33 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s21-20020a639255000000b00564ca424f79sm3422311pgn.48.2023.08.11.07.16.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:16:33 -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: Fri, 11 Aug 2023 08:16:25 -0600 Message-Id: <20230811141626.161210-5-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811141626.161210-1-axboe@kernel.dk> References: <20230811141626.161210-1-axboe@kernel.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773944158946611617 X-GMAIL-MSGID: 1773944158946611617 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 5c4cd1769641..2e39468823e7 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) +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 = 0; struct pid *pid = NULL; diff --git a/kernel/exit.h b/kernel/exit.h new file mode 100644 index 000000000000..278faa26a653 --- /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); +#endif From patchwork Fri Aug 11 14:16:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 134539 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1149282vqi; Fri, 11 Aug 2023 07:49:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHcjAIuRbgDVs5BH5ZzdUuvOUo/wxOpr8DfwRXF04PbaZG+CKDPysUc3Ge0IgsLoHqAhLC5 X-Received: by 2002:a17:902:7405:b0:1b8:525a:f685 with SMTP id g5-20020a170902740500b001b8525af685mr1766434pll.37.1691765365938; Fri, 11 Aug 2023 07:49:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691765365; cv=none; d=google.com; s=arc-20160816; b=xqQfKHV6rupWQqo3S00LV1Xc/ju+6+hClzBSIzgUzHixlcobuK9ZkD/Zbfs9GkYaQB bTgLAL80yioYPFk22f8+rscOmMPddS5fg7+jpbe9eWFkjLsC+dBPfecFObJYiY2Y758f t2lR8eLJQusST1k+rOygNxTySeUnoOoT8dllA+fsbAzOvNZod0ellkeoY1b/JoN1GKiY zkUKII3L0YEFDC7FzpqyyhVPeRJH0gVORFHnTyyFq8deuHQhsTYQE/E9nhS8E947s0Nh qGljCwPL9AXOVTp33QPCdqmkltM62ZT4AGnd4DiN2HQD0AtQWLg+yA12s7BhHKjHH9BT Ti0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qPHV/tYxuc80KyQiAeK8qRpB7upCrh2nbslvvtFqoD8=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=FFP2WxmJE0f8/+M9vanmCB3MY7P66J5TOBv5d7RjWM3OHBGTi1/6AGNdXqzjVTE3s5 MIeRXB6nPqbf85fgqEhO7K3D/Wv0KxUTQkjPEWDpYakPxHp9N5ZDAt1vP6tzwGKx+PJG CfoZbVaqNcR9Y7IgdnN3shLrcCkJ4aN0fzMC/uqOzPiuGvk2rK/MAhvrJi4f3jRb8x2q Yungn+uM019LRtxl63dnNDTaxvmUMQxYS7ZIMOpnBIy0qPeEQPPYqZjQCcFoY7bJof0W hlz6k2pfuWUTH8ivDWe2QzFm4lXyRiSYaK+byre7sR47ymAiQYuiPI43U7s3j2XdGqX6 O8YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b="gUK/MDUg"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p16-20020a170902ebd000b001bc5169be55si3273785plg.314.2023.08.11.07.49.12; Fri, 11 Aug 2023 07:49:25 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b="gUK/MDUg"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235932AbjHKOQv (ORCPT + 99 others); Fri, 11 Aug 2023 10:16:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60704 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235392AbjHKOQj (ORCPT ); Fri, 11 Aug 2023 10:16:39 -0400 Received: from mail-pf1-x42c.google.com (mail-pf1-x42c.google.com [IPv6:2607:f8b0:4864:20::42c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1C6612D72 for ; Fri, 11 Aug 2023 07:16:35 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-6879986a436so418578b3a.0 for ; Fri, 11 Aug 2023 07:16:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691763394; x=1692368194; 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=qPHV/tYxuc80KyQiAeK8qRpB7upCrh2nbslvvtFqoD8=; b=gUK/MDUgHgtnPWtbIm1EWwkA8/BiJksh9TfxCLJ0+tqLn0ZdnEqvvHFHwgSGulYfVA v7o9Jljxq4HN5JKznnajV3CNYaWivrbreJutEJ3E3zkBakCnPl0uuFuLRjBMQ3lsyF4f 6C4dK8eREDl1T8RW2AE2fTt0IQO+R0zQTTkmf+bADhKT0v/cFNEpZGDdgHjC/meSNB1d y2yRQpVyb+yJ7ykM4s5gzwLfIKoKJ5Xe7rh5A2v//8hbGMfpGc0oQAyeqXAumfxHj7VF Ap1qnXBlhmzbI0ZafHoSZC6poN/1ZAPFVFt1hKSgKIPrZtLzalHZeKtQLi5r+aXD8571 Ry4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691763394; x=1692368194; 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=qPHV/tYxuc80KyQiAeK8qRpB7upCrh2nbslvvtFqoD8=; b=jaF3ETwvJHFOV/txRJ8Hd2ZPuOGwoGe8qtWx74CQh/zKV/Li3Qklu6NG9bbFxpdl9h rNaaSBCqLO8958WuoR8Ix61Xau6OtXkLNaNKWf0Ir4eUgjIb3K2Qt2hIj0iWADrQpRIX AGXLUprChI8KNiospCWk9HLNuctaHecidMoGy0fZZs3eNKtmxv0jyKX58bJxSGLpdCyo tFCSMJCV0beLpCvrlAFY+DRWKm8KSZdNaFCwo0MiMT4QHWtS6448NuFCWwfBgVtOGPYS R0jJvK9acnVkEEJwMYHzQglbv2LNH5pC+5YEJFov+8swKZ3F0YJJHKQRUsRT5jz05WaH Ji2A== X-Gm-Message-State: AOJu0YyUs7oM7y5QmKrJ++URcQXOi6tC9PEU7A8m1uFGw7++EvEt/c3Y Dg83d7+ubHeU2VaMnaIELWdcmE40WlLLzhBKhAQ= X-Received: by 2002:a05:6a20:8e17:b0:13d:fff1:c672 with SMTP id y23-20020a056a208e1700b0013dfff1c672mr3324704pzj.4.1691763394530; Fri, 11 Aug 2023 07:16:34 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s21-20020a639255000000b00564ca424f79sm3422311pgn.48.2023.08.11.07.16.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:16:33 -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: Fri, 11 Aug 2023 08:16:26 -0600 Message-Id: <20230811141626.161210-6-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230811141626.161210-1-axboe@kernel.dk> References: <20230811141626.161210-1-axboe@kernel.dk> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773944560216177792 X-GMAIL-MSGID: 1773944560216177792 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 | 312 +++++++++++++++++++++++++++++++++ io_uring/waitid.h | 15 ++ 8 files changed, 349 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 3dd3d7557531..ef42a8203bdd 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 f1dbbe51f867..d33d9d91f94c 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" @@ -335,6 +336,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; @@ -3277,6 +3279,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 b9e1e12cac9c..1c5cfa9d7b31 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..29c8467e3242 --- /dev/null +++ b/io_uring/waitid.c @@ -0,0 +1,312 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Support for async notification of waitid + */ +#include +#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; +} + +#ifdef CONFIG_COMPAT +static bool io_waitid_compat_copy_si(struct io_waitid *iw, int signo) +{ + struct compat_siginfo __user *infop; + bool ret; + + infop = (struct compat_siginfo __user *) iw->infop; + + if (!user_write_access_begin(infop, sizeof(*infop))) + return false; + + unsafe_put_user(signo, &infop->si_signo, Efault); + unsafe_put_user(0, &infop->si_errno, Efault); + unsafe_put_user(iw->info.cause, &infop->si_code, Efault); + unsafe_put_user(iw->info.pid, &infop->si_pid, Efault); + unsafe_put_user(iw->info.uid, &infop->si_uid, Efault); + unsafe_put_user(iw->info.status, &infop->si_status, Efault); + ret = true; +done: + user_write_access_end(); + return ret; +Efault: + ret = false; + goto done; +} +#endif + +static bool io_waitid_copy_si(struct io_kiocb *req, int signo) +{ + struct io_waitid *iw = io_kiocb_to_cmd(req, struct io_waitid); + bool ret; + + if (!iw->infop) + return true; + +#ifdef CONFIG_COMPAT + if (req->ctx->compat) + return io_waitid_compat_copy_si(iw, signo); +#endif + + if (!user_write_access_begin(iw->infop, sizeof(*iw->infop))) + return false; + + 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); + ret = true; +done: + user_write_access_end(); + return ret; +Efault: + ret = false; + goto done; +} + +static int io_waitid_finish(struct io_kiocb *req, int ret) +{ + int signo = 0; + + if (ret > 0) { + signo = SIGCHLD; + ret = 0; + } + + if (!io_waitid_copy_si(req, signo)) + ret = -EFAULT; + io_waitid_free(req); + return ret; +} + +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; + 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); + 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);