Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1718413rdh; Tue, 26 Sep 2023 01:01:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHTyynZD9/42ZbC/xXNTwgzFnb++jPERF35JXLXsGoUd+lKKKTRp7NrWizJ/4E7UvhI6Sw4 X-Received: by 2002:a05:6358:88f:b0:143:723:8f89 with SMTP id m15-20020a056358088f00b0014307238f89mr9460402rwj.4.1695715316225; Tue, 26 Sep 2023 01:01:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695715316; cv=none; d=google.com; s=arc-20160816; b=XyECkaHJfy17ocVZBMwhPatqTEBqfUfWDj0fBujl3b3+A9o6xQyh7jqOvlxhMEJRaR dgMt0N0JJryk4hyV+kUjQiIIO1jYdx/lbXy8+ljM9fVDwtr1+3j2DShQH30jrItIPLP8 ZdEafRBQDNbm/OuTR97/+2tcm8ZQvu7ZQ3gYLgSn2YAJGminz1qAQVM5vHksrPTF0Twh hnvWtNZJvbioms2iHzmDClz62sxcKRNuPv03y4ucDVHNnrv1trua3/ozPqNl2nAp6qEl 3DcmlAWZ94P7XbyUL1xh+lw8Q76KGInmPwn/qL6JbKGOUswJqfEgTaODBN4RJcn5m4Er NtqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature; bh=ffVsp3XgCa3HFUWUSmx44KcvJBm4g8vYR0LbPrRFOWA=; fh=VIOmVuSpt03ioSSTsYO+bWowKS38BlXXR4tRnDijCyU=; b=WhHCPre+MzK6qLlpCK0jWliD4ArwqeG0Wb9hleuNWLv5ocsl0tHKy+hl0aDNxHrb/+ QbIgOn++TwH6khgX7CctQfWvYTA5rNCmbaPgsLROnWaWeG3f7ukw5o2klZcwuCK8UGtP X2qQ3XOJlKmUS4R6L6wXNiLp5mbX8A5hc0qPyMj/tsodyD5lrHAHdHt0xy9IWx73QK29 hoKFpzMgSjjs25mVmIC49UL/sVSfxSegBntpJiqIhZquJIV0RMcaRvkkIytHsDlk2J8g oMNY/WluF8TdTCFRSEvCa/FQlPDwnrWb3wWlE/rwkns98AG5CUIv7HzFzClgezTSZEcJ VUcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=Pey6gtEW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id fb30-20020a056a002d9e00b00690d8405fe2si12808355pfb.60.2023.09.26.01.01.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 01:01:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel-dk.20230601.gappssmtp.com header.s=20230601 header.b=Pey6gtEW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 493A0807FCF4; Mon, 25 Sep 2023 23:41:42 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233749AbjIZGlg (ORCPT + 99 others); Tue, 26 Sep 2023 02:41:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53690 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233655AbjIZGle (ORCPT ); Tue, 26 Sep 2023 02:41:34 -0400 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3A573E6 for ; Mon, 25 Sep 2023 23:41:27 -0700 (PDT) Received: by mail-wr1-x432.google.com with SMTP id ffacd0b85a97d-32163c3ece5so936194f8f.1 for ; Mon, 25 Sep 2023 23:41:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20230601.gappssmtp.com; s=20230601; t=1695710485; x=1696315285; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=ffVsp3XgCa3HFUWUSmx44KcvJBm4g8vYR0LbPrRFOWA=; b=Pey6gtEWMIegUm5jPk53P85QRcWuFCtHLijxt6VipS6QtMmHnlDJHHi8rFVKSxlTMQ N6vRhYdpSFcwzUuuHrMcZojBTsnfVzX3U8epyfnfosEcgsQQE9bRHDOBnXGh4R4t4ieu ZUSrhhv8PUnUMAjuVnCU+CIVP2v10iQMtxqK8L6Y6QOVs+ELIkHXAIipeCatCk9fCLmE MyywKsJsLjC/pUTkUrHqBUBe8z1swgAtvJLKmTFz4UBtXKTBpxGJDrAEbZhM8lz9IVST 6/5+2A4nMPOxUEobm/rApNepva2lE9yT4fOSDhC95L6WSsLy1LOvXQ3+/35g5DCojSB8 12qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695710485; x=1696315285; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ffVsp3XgCa3HFUWUSmx44KcvJBm4g8vYR0LbPrRFOWA=; b=RJdeCgZu0NQpD5kWywHUe0fIdiiOmQR3hMnLlMk5IGYAw0SqxX9GWkUorbCa9Gdfos dWgsjHuB0RFHAWug6oSm78jmAheehdwx48pgcnJtnKuO8UDEQlNKhDtu/i3fOYK8IfeI sXaBQtSSqXtpieBlXfeAzCwNnw1SSjHTBOdHX3Dsn0wRX3aZ5gZZAmpvK+b8Q0y8M+su OV6myuOUroVkK/iX0VLrc6Wlg28ebykKCCv/CjTwUlzrSef3wlxkJk1j5JZheWXzSvIZ CYjMa3myL1B4xnNCULMzVFe++zzcn6M3hKlHbIVHpslWh1xEXV3sVhMSCsVMekdYXUoo YQFg== X-Gm-Message-State: AOJu0YxQNw4FOrDC6zAxnypcJAJ217GPDuqy/jMjMOhUMUKPWMDv5r9D Ewrtgy2GTVRNv5NRg0oJhOm/y+pgzCa5VREKQawTEdiu X-Received: by 2002:adf:e7c7:0:b0:317:5f08:329f with SMTP id e7-20020adfe7c7000000b003175f08329fmr7964722wrn.1.1695710485575; Mon, 25 Sep 2023 23:41:25 -0700 (PDT) Received: from [172.20.13.88] ([45.147.210.162]) by smtp.gmail.com with ESMTPSA id ay18-20020a5d6f12000000b003143cb109d5sm4624976wrb.14.2023.09.25.23.41.24 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Sep 2023 23:41:24 -0700 (PDT) Message-ID: <9eb0fa5e-5f8d-4a55-940c-5e1ec22bbfd9@kernel.dk> Date: Tue, 26 Sep 2023 00:41:24 -0600 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 8/8] io_uring: add support for vectored futex waits Content-Language: en-US To: io-uring@vger.kernel.org, linux-kernel@vger.kernel.org Cc: peterz@infradead.org, andres@anarazel.de, tglx@linutronix.de References: <20230921182908.160080-1-axboe@kernel.dk> <20230921182908.160080-9-axboe@kernel.dk> From: Jens Axboe In-Reply-To: <20230921182908.160080-9-axboe@kernel.dk> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 25 Sep 2023 23:41:42 -0700 (PDT) After discussing this one with Thomas yesterday at KR2023 I had this nagging feeling that something was still amiss. Took a closer look at it, and there is an issue with the odd case of futex_wait_multiple_setup() returning 1. It does so if a wakeup was triggered during setup. Which is fine, except then it also unqueues ALL the futexes at that point, which is unlike the normal wakeup path on the io_uring side. It'd be nice to unify those and leave the cleanup to the caller, but since we also re-loop in that setup handler if nobody was woken AND we use the futex_unqueue_multiple() to see if we were woken to begin with, I think it's cleaner to just note this fact in io_uring and deal with it. I'm folding in the below incremental for now. Has a few cleanups in there too that I spotted while doing that, the important bit is the ->futexv_unqueued part. diff --git a/io_uring/futex.c b/io_uring/futex.c index 0c07df8668aa..8a2b0a260d5b 100644 --- a/io_uring/futex.c +++ b/io_uring/futex.c @@ -23,6 +23,7 @@ struct io_futex { unsigned long futexv_owned; u32 futex_flags; unsigned int futex_nr; + bool futexv_unqueued; }; struct io_futex_data { @@ -71,25 +72,21 @@ static void io_futexv_complete(struct io_kiocb *req, struct io_tw_state *ts) { struct io_futex *iof = io_kiocb_to_cmd(req, struct io_futex); struct futex_vector *futexv = req->async_data; - struct io_ring_ctx *ctx = req->ctx; - int res = 0; - io_tw_lock(ctx, ts); + io_tw_lock(req->ctx, ts); + + if (!iof->futexv_unqueued) { + int res = futex_unqueue_multiple(futexv, iof->futex_nr); - res = futex_unqueue_multiple(futexv, iof->futex_nr); - if (res != -1) - io_req_set_res(req, res, 0); + if (res != -1) + io_req_set_res(req, res, 0); + } kfree(req->async_data); req->flags &= ~REQ_F_ASYNC_DATA; __io_futex_complete(req, ts); } -static bool io_futexv_claimed(struct io_futex *iof) -{ - return test_bit(0, &iof->futexv_owned); -} - static bool io_futexv_claim(struct io_futex *iof) { if (test_bit(0, &iof->futexv_owned) || @@ -238,6 +235,7 @@ int io_futexv_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) } iof->futexv_owned = 0; + iof->futexv_unqueued = 0; req->flags |= REQ_F_ASYNC_DATA; req->async_data = futexv; return 0; @@ -278,6 +276,18 @@ int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags) ret = futex_wait_multiple_setup(futexv, iof->futex_nr, &woken); + /* + * We got woken while setting up, let that side do the completion. + * Note that futex_wait_multiple_setup() will have unqueued all + * the futexes in this case. Mark us as having done that already, + * since this is different from normal wakeup. + */ + if (ret == 1) { + iof->futexv_unqueued = 1; + io_req_set_res(req, woken, 0); + goto skip; + } + /* * The above call leaves us potentially non-running. This is fine * for the sync syscall as it'll be blocking unless we already got @@ -287,29 +297,23 @@ int io_futexv_wait(struct io_kiocb *req, unsigned int issue_flags) __set_current_state(TASK_RUNNING); /* - * We got woken while setting up, let that side do the completion + * 0 return means that we successfully setup the waiters, and that + * nobody triggered a wakeup while we were doing so. If the wakeup + * happened post setup, the task_work will be run post this issue + * and under the submission lock. */ - if (io_futexv_claimed(iof)) { + if (!ret) { + hlist_add_head(&req->hash_node, &ctx->futex_list); skip: io_ring_submit_unlock(ctx, issue_flags); return IOU_ISSUE_SKIP_COMPLETE; } /* - * 0 return means that we successfully setup the waiters, and that - * nobody triggered a wakeup while we were doing so. < 0 or 1 return - * is either an error or we got a wakeup while setting up. + * Error case, ret is < 0. Mark the request as failed. */ - if (!ret) { - hlist_add_head(&req->hash_node, &ctx->futex_list); - goto skip; - } - io_ring_submit_unlock(ctx, issue_flags); - if (ret < 0) - req_set_fail(req); - else if (woken != -1) - ret = woken; + req_set_fail(req); io_req_set_res(req, ret, 0); kfree(futexv); req->flags &= ~REQ_F_ASYNC_DATA; -- Jens Axboe