From patchwork Wed Aug 2 23:14:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 130242 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp849799vqx; Wed, 2 Aug 2023 18:56:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlFhFg0sasInQp7S/iHa4YlxsrsJ5R/wQL6o6AIRLm1HQVuBbsZUefQ73fK2rQDmVxpKr8Rg X-Received: by 2002:a17:902:d2d1:b0:1b8:ac61:ffcd with SMTP id n17-20020a170902d2d100b001b8ac61ffcdmr18500648plc.3.1691027810557; Wed, 02 Aug 2023 18:56:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691027810; cv=none; d=google.com; s=arc-20160816; b=pXC/lM9sAt/7JrvlS+5bkrd4qlB98gupUL7JB5PKDZHVU+DEIFM4NPUvdlCsYaz+Qe eEwJOVN6X4SMMa0M8vFVhVYpwAXVIkUwDtrjuRqDqCl0poMhVDi01iso0WMl9N1UemlY RT3EJ6BRdkSInfst9Eeh0B2yJlHPa7rTCRN/A08ewnKM9atB+aa/YVDUwfex49bIP5uV 9yrAXRy5fx4tWCYgF0EYEvFdtghIC7QaEs+r69Ds/rHBYzu1rF8GTl9ClrUrw6Rw2V+B 7VuxdhU4ctNtYDXuU3/ZGoypu+u/uveywHU07t6U7sijx90RkRMz6VC0Qv546pKm1vpp HaGA== 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=ZVMrEkXrlbn/ItTJ4HiS7H6RgOlSwZhVqf1SA1QexX2ObQfQklWRwIRtDyYmP6srE8 YBk/NSx14EWUUcXcgYKv3LTM79ekZoowZUmcrKaFRq0N1yFrxnxhVT7IdBZsn4IKHmMO 3KQU80TQHdrepxWcOmN2bTQ1LAQii0r/RcLBGxU9Ng225lhrw6d+hQ35ulWyNbZmeWCU OXQaq5Z8UKdG5QmxG78GwxzAk7//7qZ5EONOWlv5KAwmW+90Wa9zk3nvWMwSCPUGjkGl Tjg0XRGED6TyMFNLmlEo4UBBHAYCaJAAz01jrAae9Y3Qt5HLONBjlDnE5/WMU/hT3oQU QYhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=VvLqsRm2; 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 l17-20020a170903245100b001b8a2774d60si12122909pls.486.2023.08.02.18.56.37; Wed, 02 Aug 2023 18:56:50 -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=VvLqsRm2; 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 S232452AbjHBXPO (ORCPT + 99 others); Wed, 2 Aug 2023 19:15:14 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232454AbjHBXPK (ORCPT ); Wed, 2 Aug 2023 19:15:10 -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 6A66E2D73 for ; Wed, 2 Aug 2023 16:14:49 -0700 (PDT) Received: by mail-pf1-x431.google.com with SMTP id d2e1a72fcca58-686f74a8992so59277b3a.1 for ; Wed, 02 Aug 2023 16:14:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691018089; x=1691622889; 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=VvLqsRm2OJJU+D1M0rQbLPXxUftobv7dOQuACj7+lEUWYSbCpZrRdum9arEJRpP6ZV foTPkMFYT2P2s+xHw4MltELp2HXcKx/0V3ic1d23NgGVFndvLNP9oY87foFS2DBiLedT oeChmSpVCBUhO/fCH0jp5bVYp2cMM2tVlEomNUshrTP3pSwQS2A1zyhseuKV+d3+A183 njGjTCTc7Sw6rGyupEfH/N2kT02i3JaGSKpzP2PMWbFzbaU5Wj+mOVGAlegiNhTjpuN8 eh+WJhobV2o8TeVm+5GNvWY/IZceK7XasS5NiIamNk/fe/HrS8d+ddNgDB4p8nM6OoNF YnEg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691018089; x=1691622889; 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=XcIAzS4TWu33tf9X9OludSR7gcHhlnR9HEWqie+fDxWwi4wZxfbP0/etEpY+jOJ51u keObqwNLcsSiwuraMCFJ0QAX5JfCNE11jC8JJUjTNzA1Xu1qe8BuI73xgCn1xWJV96NO eMF3X/wn29RX3u5W3TtpxfUWvykrN3di2YR//rD5OvTiaK4NRpV2hwYQLyrjq/Hsc7OG Cq8vTFrmUJHLlSD+tZwDmHnayVJuZQlzHMLYlDpznWAH9AEh7t+YYJEu/xglVVddngrr p1hgSwt8YgsUNzYL4iB/JwO6ifxsfn2cXNq3qpuZUZd+V1gOmcGGZHcEj/iSnXIvp85Z eHxw== X-Gm-Message-State: ABy/qLZ5Nc+r93HDyUUoD8EC7q334nfpWlltRhg4eLuCxxkmXrX8t6Ra NcbfBivC8dWLLDr7hqbMN6IRTfPMy+sDQtIlxwo= X-Received: by 2002:a05:6a00:419a:b0:67d:308b:97ef with SMTP id ca26-20020a056a00419a00b0067d308b97efmr16742452pfb.2.1691018088830; Wed, 02 Aug 2023 16:14:48 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s6-20020aa78d46000000b006871859d9a1sm8588086pfe.7.2023.08.02.16.14.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:14:48 -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: Wed, 2 Aug 2023 17:14:38 -0600 Message-Id: <20230802231442.275558-2-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802231442.275558-1-axboe@kernel.dk> References: <20230802231442.275558-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, T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: 1773171177719527943 X-GMAIL-MSGID: 1773171177719527943 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 Wed Aug 2 23:14:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 130241 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp848828vqx; Wed, 2 Aug 2023 18:53:48 -0700 (PDT) X-Google-Smtp-Source: APBJJlF+CwTnM/ItjoUuTlyHCbstxIQugkqDbeHR+WNsiWNGkewA2WpDEndoOIk/fTYCGdBUviTZ X-Received: by 2002:a05:6a00:419a:b0:67d:308b:97ef with SMTP id ca26-20020a056a00419a00b0067d308b97efmr17055762pfb.2.1691027628610; Wed, 02 Aug 2023 18:53:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691027628; cv=none; d=google.com; s=arc-20160816; b=I0u2Grm/EArXsVjHvuEEudPQ/w7PIWHQMHoO/SpxWu/S9fffzqGeWsc1bhXK1KzbaI S6V7U35u8PPBEXL4sulKAixuYxizRFOBgfZh4z6OcFoW7tMHed8s7FhL1Yo79gTOZLea EmpaUcXQT6r5ONuXxjgsl95D7EUduM8U9iRQ+OI7a81IfTrgRFHWe+58newBWS8Qg34O /wh9vyr9HPYflLgtm7vxNNHN5uwFVzEL/k+uin/RpP50/U6vDHbAD520Beq0m2nlE9DD 5mB7Ztug27W1nRUsznxg223M1K5Aqvq4DuX8AqE9ZOPOnOMLZ0uNQcUE2DHT50vbDtXd 6BkQ== 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=f+mLzQiRjim1MaNN23WQQV9xVXMWWDimwJxzp5ox54nOwGhqZzE9vqvxG69pzxhBq6 A409kaQu3rnPOpJAKMF32AvCfdU+35wE6n39CHiODakGHmVIjtcuKppGZ63CcbzXewSq lDGgS6Mbk/+r6UnNT9kAgG0NNV8ek1YoQC3fYAJEjkkRAhrx19NclwmcUnhDmCcazyq4 flnL/Ov+umjQVMqa5L4bDhtMgyR/deT5y5nNmtwIJhsQBIW/Tm8jZRQipD/Wyh7e56i8 I6L+ymBPL0zt80QiOKEBny6UwxzVEz3XiOn509Ib9bIG+reJMbuh2SzT2RqkfIwaJZ31 NT/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b="fW/ZRQer"; 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 h11-20020a056a00170b00b006797c2c00besi9129801pfc.144.2023.08.02.18.53.35; Wed, 02 Aug 2023 18:53:48 -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="fW/ZRQer"; 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 S232697AbjHBXPR (ORCPT + 99 others); Wed, 2 Aug 2023 19:15:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56766 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232491AbjHBXPK (ORCPT ); Wed, 2 Aug 2023 19:15:10 -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 8FC0830C3 for ; Wed, 2 Aug 2023 16:14:50 -0700 (PDT) Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-68781a69befso62594b3a.0 for ; Wed, 02 Aug 2023 16:14:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691018090; x=1691622890; 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=fW/ZRQer1agPI61N0YIVyKetq1QNjGBCi7YCG1B4WYW19AOZNDS01zZ/Gskbn10aQv zPL0Gm9sWpm/+j9NGMFN9h0w454eLFh0MroSEEZFdj/piNjbc+BVIIq/SGXmFihPWFDD IolSyAxo0zr/0bHyo5nOgMNrX9dj/i+tZ9Ri5p3emb24cxEjrptIIM/88boLDwSglNux x9CeZr4wOQ/1DPRO3IwpLWxrFw7mfVV1C/+m9RduIA8VF4yWJUL2lXfxEQLxvSNXcy8N bsZ9AwBf9lAaRobv/k0nOZ9H9rDxbF/4B1fsJfRUxzwvFyI0AgxZkYKDFNMdIrT5yHsC NWJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691018090; x=1691622890; 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=WjNFw7zrB/otOshp+tK0bMox+8e3TadBqhVTlTLxlHSq0suBKqiNchp07BoQPR/3SG daJFiMYQrJhoqbci3jJEeKFGa2vN3vWdNmvwOuTuJNqvTeY6L1D9unphRdicIimVUx2w MeFCvMC4z3o9kmW2nIhzhhO8ZyqlPGTZS50HCLTCqtLjx13DUi6xWRWJM2/jB95IcL4T LL+vE1bKBHTV0Uqg9UM9h6CIGId5k2SELs1iZ8eET46GhdPb4AJSg5BjPjoJBP4k2a1m MpX3DUFucQ7JR5JI2fqVm1wmM7rG8J2t25g7iGorJ0nsXXvXASrZubPVpt8q+PJ2XPQA t5gA== X-Gm-Message-State: ABy/qLYSLrKc0fDg9B1Qd2atEqUZrHvfQ5qaFZlf9ccwA0tKX7hZHlHC ihptHX6mO56n0VEdqzXUX4Jh4w== X-Received: by 2002:a05:6a21:9989:b0:111:a0e5:d2b7 with SMTP id ve9-20020a056a21998900b00111a0e5d2b7mr18562254pzb.4.1691018089962; Wed, 02 Aug 2023 16:14:49 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s6-20020aa78d46000000b006871859d9a1sm8588086pfe.7.2023.08.02.16.14.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:14:49 -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: Wed, 2 Aug 2023 17:14:39 -0600 Message-Id: <20230802231442.275558-3-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802231442.275558-1-axboe@kernel.dk> References: <20230802231442.275558-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, T_SCC_BODY_TEXT_LINE 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: 1773170986932928484 X-GMAIL-MSGID: 1773170986932928484 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 Wed Aug 2 23:14:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 130233 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp808673vqx; Wed, 2 Aug 2023 17:10:43 -0700 (PDT) X-Google-Smtp-Source: APBJJlFOqdGgLOA6h6ZOMV82b9o5iWcwG8bn88whB/rYSFNTNg/z5A9KhMrn+mp0J+boV1nUtVzD X-Received: by 2002:a05:6a20:4312:b0:136:f3ef:4d2 with SMTP id h18-20020a056a20431200b00136f3ef04d2mr16264705pzk.3.1691021442964; Wed, 02 Aug 2023 17:10:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691021442; cv=none; d=google.com; s=arc-20160816; b=C0VJbyoKW25oz9bx3/m47BHReDJfd26wv+I4EWHMql2t5I+zYWC0O4mxAyPLxymvy0 MRBiWzds9u7aCO62WK7V4x9E0G+ELDVkCS2N+VKPpzNbCtIK7qQx9+vDQpU9wLaj248e 5W2zjuj4rwSNCue5VOr9pcrBHC1QJUepjY01hI2ZtrZyW+WDIsDDsQHjs8Xp1YXmaP9b DG79st8zStLAhvjJRUpd/Hg10fQYzft1AcXyE8/8kHgvV3Q/aOoNb6Mwec0Vpu13AJ8p 2eJC37CapHqIsOQU4jkgboApqOdMkzEG3F98cm6vy4RU3CvdXIlQIYG9VLh2gcCov4+z r8EQ== 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=WYDIyeoAajuf9ZWE7uQTqbgmYwdh8IUMjMetvGRNy4k=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=pNK0Oov6Rb6Dhc6BLSgA7hbf7dVj+TVo5Z9BYSjQyU3Ljj8H6wH/dWJsxVZuVkbQkZ a77Wy3X5XYmZFYPpD6qztm+Dh8Eb232F/BhWUtWT6krcLgKSr+GP8Wj6S4nct9EbXH2P 9zp3M9KYLSLbX4uNhw8Tk93nv/zqtbYvI09AOo1L1DDDPUpYsZ7mVBhScF3ZMCYHRnBn GqPS1NbuYa3gSBdG6288oCYCuJdbLkKmUexQE6uxzbRjjiAPwXDiO6bASlh9aKJJAZlT D0Y6osPNRxbD/hNHF908wIakGfD9KN2lmKPxuq9LsOO9jP9SnygpYBz2DMewPMD9T0d6 raUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b="j/TOQbbG"; 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 b26-20020a6567da000000b0056428dcea78si8401142pgs.17.2023.08.02.17.10.29; Wed, 02 Aug 2023 17:10:42 -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="j/TOQbbG"; 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 S232659AbjHBXPV (ORCPT + 99 others); Wed, 2 Aug 2023 19:15:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232418AbjHBXPM (ORCPT ); Wed, 2 Aug 2023 19:15:12 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6D06030CD for ; Wed, 2 Aug 2023 16:14:51 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-5648fca18b9so52931a12.0 for ; Wed, 02 Aug 2023 16:14:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691018091; x=1691622891; 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=j/TOQbbGU9Y515OgPvP1VwT9KdHRrb9ZDbEFT0J9+QwQPRL96cGPPaAJbKHcolCg/v itATAWdxTMiqX3Zkqg1wadUnLchhbL6hXdOtbXCGVe2v3V4z8Nt0T/y81lotBAxPNuFS 3jQhHAaOLb63IEUn9WrcCVk4CiP6j51lN5RIN8jWDUznmFcpRC0hHBxbV/CvEYlaB9t9 VsKqVb2kgmdSxFvDCGyvQXAfDtKM6QuKUyBID07WNA6SlVltqxVQSndTDhhEBd1K3IG5 VmMZiaqMTLTyG+f4FleKE8FQKsdnp/uKHb4rblU9yxmqwjG9IEtCcxXKW9yhHclmyFLw 7tVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691018091; x=1691622891; 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=O3CE0PRmOJu3ENSTEtFJAAggm+6dzOoahba9hh1XkPs8InhIe7PxA3x3Qb5IOc6YOZ Sv0I1mE3K7tUU9IUoF9UkRGiNEcF+pTB0nP5fIa+llNmbBOmsdvcJcy3wPYkPMDT55Os KU97t7po1gQW6SKvXi6K8y0KXcJc42xYlriCuetVeodjeI8m4XevlbevPv3idKZZtFIB vCrrMuLOcGwE21gJEbVOgvkdccNstWfsHxdtbbOSH1T13tx0ghZd7q2lxdwG3kdtbSRU O/i8tqQFKyRyza4aXqhWNRYH+i4bGAwY6PtxQ2CFsXz4ucCKv/9j38g7/xmOU6fG1pF1 bXhQ== X-Gm-Message-State: ABy/qLaKl33sc/IEyzsazshtGGxHC66fpn02r+0wW6pj95/bh16m44iJ +oHl2/T/dr3LQP4A8BIsm+ZZcMKOivvS4MvLZvA= X-Received: by 2002:a05:6a21:33a6:b0:134:1671:6191 with SMTP id yy38-20020a056a2133a600b0013416716191mr17934352pzb.0.1691018090915; Wed, 02 Aug 2023 16:14:50 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s6-20020aa78d46000000b006871859d9a1sm8588086pfe.7.2023.08.02.16.14.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:14:50 -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: Wed, 2 Aug 2023 17:14:40 -0600 Message-Id: <20230802231442.275558-4-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802231442.275558-1-axboe@kernel.dk> References: <20230802231442.275558-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, T_SCC_BODY_TEXT_LINE 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: 1773164500341687321 X-GMAIL-MSGID: 1773164500341687321 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 Wed Aug 2 23:14:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 130216 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp798420vqx; Wed, 2 Aug 2023 16:46:24 -0700 (PDT) X-Google-Smtp-Source: APBJJlFkP1LW2WzItT67B0j7MkZFTQmTBSUgNbjKw4ndLCtGrlG0n+zxaZh1LuHBIe7NFal2xIqj X-Received: by 2002:a17:906:2112:b0:99b:b3a1:437b with SMTP id 18-20020a170906211200b0099bb3a1437bmr5966188ejt.40.1691019984659; Wed, 02 Aug 2023 16:46:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691019984; cv=none; d=google.com; s=arc-20160816; b=Jh73lk+v3waWVZV5KXGgBNEoeQgK/woVCEV1pm1HYIq0atYZts2EZQA2mriSPPH7fo GGNPFkWTv/4hjIY6QJbB1eFjn7BQNYjeseQTjvzglTppG9TlxEnD5vUfhzFO7j6zY/eO j/nIIz5jW6TVnyjqbkwuXoY7Xva16QyKYYTBu6+BGT7gQwDDoNm0rD1CFz2YK+3oOQbU zMavDBv6VLtXYQzFS2tgqUP/fG6WQMvqKL3AXnxN/GqbjQQttyB9b0MfXKw8SD4/0wxJ pqFf396IFeBcftV56arBbdVQqRwFtqTVBPlrMUZ43pr2Kh5zhEt8n8bDqzjN1Vkt5ic+ ZEsg== 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=C9zc53z+/V9/ECV3tKzh/T4s2rLLXca0QnmcU/Zn8AM=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=lIzRrvzkVO2ZU8TgbwVc5TJybjdcyUfyvFs9pjgGor3otCIVO5RVWEHGQ32rz3fJru O9jUtavRaPuX9M0hI39ACDpzf688cqUjScFlpp49XMQ6KOMuE39qrkboKFT0/l4xq/6T PLZc2KJ9vscurk4QCtfzkdvAoCxeYz8QWzsppjUKog0307mp0j8jQZbxRsR/qQZXvbuJ uHq4eYH+H0ytumsBERaBB1BH26cWfJHASRotilIfzq62NFQXC1vOGXpHWpHKh4NS+XzH qP1QnxITgsMAeDKrZZVCN3oTiVGAWdp0dJafbKzIJpE+5gQYBB+BTHn+8nnO6dlwZSSC bm3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=ev+WS3qa; 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 f19-20020a170906495300b00992b63c9c16si416619ejt.129.2023.08.02.16.46.01; Wed, 02 Aug 2023 16:46:24 -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=ev+WS3qa; 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 S232710AbjHBXPZ (ORCPT + 99 others); Wed, 2 Aug 2023 19:15:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56836 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232633AbjHBXPR (ORCPT ); Wed, 2 Aug 2023 19:15:17 -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 BC1F530DF for ; Wed, 2 Aug 2023 16:14:52 -0700 (PDT) Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6872c60b572so72547b3a.1 for ; Wed, 02 Aug 2023 16:14:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691018092; x=1691622892; 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=ev+WS3qaydOlkCo+QeWAPNPNHOvDvLz5rDIt+FcjMulT+GD53Eu9fsndQPFqHbDgXJ mw283KDuVisSntJFefGF8SIPSxPd9f442R6XxDH0n6hi23KGuugxfAfcEvTbF/HIC+Vu Sn5dWrcPzQ4zKVjPfWEOtZEfB48Fdg+a1WihE30oh/hjsk5vtGYpTIvBwXeqY8X8qquI NmD183MSlVg2qxSIgViiM76fbjawy/rg+taIIGQiTF6zpLCcZQIQQhxTJDzVXBnNAxmv wyE928xlSO/4VQ51BL5c5b99xLg3hcgEfOJ6ZOtOnvVTX7CoRTSbqCjJSfiksRbnuIzG lX+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691018092; x=1691622892; 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=mHIHxQA4yOrraajc5VrSXXCU7y5bv8HlOOgvdkGh1gjV7OzaAzoNNqghD+6VteuRW6 CA/fHHYxvQd92+P5lRRRjKguFg9Qk4fuRrTetKXJ0+YHVgjMoPQ519uncWxYtpoly3i5 jQLcmoj9JKVjNexd1dRB/fStY+1cEixb6Yyt/nWCfFSuJ/CrLnX9qHbnGs/kwBaXoZ6m 2cBOkq89h5bhaF4/VDOKBc0gXXs88RajQxJA1weM8o4+HwTpp+A5NAjHhqZKkPZVQI4c uB+6SMZl7DlMgSIkfJwo7VDFAx0wiWfKJ4Oi2y1vHzz0z9m5UpIXarj2JOG0q5NGeGvV 3VMQ== X-Gm-Message-State: ABy/qLZB3KpFGNLBCZhm9Kk5CT+CjqNzZ0kc+XOMzNNmO2QfQUNlpOiI J39n43Sa+0ADagMT2xcz2VAenA== X-Received: by 2002:a05:6a00:32c8:b0:67f:7403:1fe8 with SMTP id cl8-20020a056a0032c800b0067f74031fe8mr14821176pfb.3.1691018092005; Wed, 02 Aug 2023 16:14:52 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s6-20020aa78d46000000b006871859d9a1sm8588086pfe.7.2023.08.02.16.14.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:14:51 -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: Wed, 2 Aug 2023 17:14:41 -0600 Message-Id: <20230802231442.275558-5-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802231442.275558-1-axboe@kernel.dk> References: <20230802231442.275558-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, T_SCC_BODY_TEXT_LINE 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: 1773162971578703480 X-GMAIL-MSGID: 1773162971578703480 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 Wed Aug 2 23:14:42 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 130215 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp797605vqx; Wed, 2 Aug 2023 16:44:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHe06SVDsMjcy6syjkjMpFgSxn9PuTKNC/RqrXPzmYY7ewvxRnouyYo8v6I88yHESe0qaIT X-Received: by 2002:a17:906:209b:b0:99c:55ee:1798 with SMTP id 27-20020a170906209b00b0099c55ee1798mr1105550ejq.4.1691019851514; Wed, 02 Aug 2023 16:44:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691019851; cv=none; d=google.com; s=arc-20160816; b=aFoQ5Ehb3Gy1iu88bJwmnnq/zEl114lTNvu523pIE8LgX69k2WL03cdCTeF4SnijMN P7RcoEPVDn9rVrz8BfAtlFBxUox1GVtMGJwNNdswH7a7B82iY1uG1nMhcyx1yI2+ePAL I6vKubO9+rAy9kSEXKQdxCkhhEFdufpx1Yg+M4oCryQnCK3BDWijOzmm1KgeJIQUyDI0 9IbeNR3+yKdEup3z2StaH3NPDsqesZI9AgGKU5yqFx2pGI7YOTPxUmaTyDLckKzbLPgn ZrlHb4BfZCHw7PXUyB/coTjXR5Wcydj+YY5mFFDMwEsn0uDUoaIIMscxQf8BEfPf9/SF XbPg== 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=/RwNb6060GvxMf1WFfDWU7B06woCuybXAwaM3ADMoJI=; fh=p+wOSmeLusTVqUKR8aQc56XBO9QP4vSQ6AXDIQF3284=; b=H+bWQQpEY2FqGK/99KnSX1x+PSI7c0dbuvoHg6EcQsnUutXqRRl1rFmzWqam34zFjn OQcbTsNlRStAgWrCfO0iyZvM4rcmZiK4ta1FFNcxxRqs8F8MoUITE5M+K83b1d1vL3vw r6L6HpEOrIy5jsQ115EcxqKUTDK+lxbTiUzJncgKUv1JnPNkgkedHHdd56grRTVCBVdf 8Hk99j+XnGToMHZQTOwzoOiDFvqBlrqjxko36rVZImYz6LeoiR1jfZHNw0XOEyh50aZU cs5MEsXoIlysU6t8n0sr6sRgCAsOm47EPvxJHPIngeH1Dc7BX2pjkM0ILapx5zMNdCh8 qOuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20221208.gappssmtp.com header.s=20221208 header.b=G3YFu3VB; 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 lw20-20020a170906bcd400b00992bfd1ffc2si10773015ejb.981.2023.08.02.16.43.47; Wed, 02 Aug 2023 16:44:11 -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=G3YFu3VB; 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 S232782AbjHBXP2 (ORCPT + 99 others); Wed, 2 Aug 2023 19:15:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232705AbjHBXPT (ORCPT ); Wed, 2 Aug 2023 19:15:19 -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 7613730E2 for ; Wed, 2 Aug 2023 16:14:53 -0700 (PDT) Received: by mail-pf1-x42c.google.com with SMTP id d2e1a72fcca58-682b1768a0bso77706b3a.0 for ; Wed, 02 Aug 2023 16:14:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20221208.gappssmtp.com; s=20221208; t=1691018093; x=1691622893; 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=/RwNb6060GvxMf1WFfDWU7B06woCuybXAwaM3ADMoJI=; b=G3YFu3VBfV2kew819iJkt8pzws146UX3tdSxLcGhgNeeHElhwaUp2/iqKMD3dyKssM 8kOcqn6yRtEsGxNSKg39400WNb3Ak7Ff+7zZI/Tpo2REpkTtZ31gZ6TAS9hNTHezCfCn el84qfM7+46aErq/UlyKoNARzk5oqaOl2vlJBjPClg9z1+T+39U/YY5ehiw9qC3/Vc4L i5ZCTwm2+zkivaZ+eCx8NZCAyZs+oze05qt4Q5wvpalCCxjX1IcZanhH4DhhIOawfbJW 2D1Qphh1gYRluMxQybEi/Aexr8dM9TTKg6BiRSaS3lSlFHc/XyKrglfSl0Ubyf3yuikR q+7Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691018093; x=1691622893; 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=/RwNb6060GvxMf1WFfDWU7B06woCuybXAwaM3ADMoJI=; b=SZzPyp8plHDVsxOGZwG9GwZd+Npxs+dy4XDS2h5wlW4cT9oPwaR1uXwYzcbyVFADzS JQpy+9UICCgaE+Ux/ATFjrT3s3nGNeZD5byAnaaiIfCkD2SNWrf0IHw6MR+CwXaweVqu Mjh1b2PDo87Jfzzf8V+jwJBSbcBdmpCxqrFviEJlLmZzjRM6Q1Jbw/cJPt1Nl54I4U0s f0lt71lsmDrjts2LqjnQIMBhd52vNnE5j0HG9C/5+LPWjR3dQCvwr0XUeEjAY2qNoeB0 Nqk1eU1+tOLoBUUHRiZnRVDw/YgawUosAJLRY5BH6ooJKtw/x7lfuGv29o9Y2qXq2eZ6 gdog== X-Gm-Message-State: ABy/qLYXfHUlherDURMmDRQtRFCJ6YlRTfGvhpbS55QsKN216bhF8at2 v78o0dJyfOYxJGBA2nicahdabg== X-Received: by 2002:a05:6a21:3385:b0:13e:1d49:7249 with SMTP id yy5-20020a056a21338500b0013e1d497249mr11382247pzb.2.1691018092917; Wed, 02 Aug 2023 16:14:52 -0700 (PDT) Received: from localhost.localdomain ([198.8.77.157]) by smtp.gmail.com with ESMTPSA id s6-20020aa78d46000000b006871859d9a1sm8588086pfe.7.2023.08.02.16.14.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Aug 2023 16:14:52 -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: Wed, 2 Aug 2023 17:14:42 -0600 Message-Id: <20230802231442.275558-6-axboe@kernel.dk> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230802231442.275558-1-axboe@kernel.dk> References: <20230802231442.275558-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, T_SCC_BODY_TEXT_LINE 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: 1773162831602974708 X-GMAIL-MSGID: 1773162831602974708 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 | 313 +++++++++++++++++++++++++++++++++ io_uring/waitid.h | 15 ++ 8 files changed, 350 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 e52cbdcb29b8..94147e0835bf 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" @@ -352,6 +353,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; @@ -3286,6 +3288,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..14ffa07e161a --- /dev/null +++ b/io_uring/waitid.c @@ -0,0 +1,313 @@ +// 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; + 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);