From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512283; b=FArBba1nN1UzifscPJaAvddTEf7x34ejE4rZaVzBJUd+U6VcgwQUOOYE2IwtpJOaQmB1 lOCYcVsx+xpeVexGYzBLh1La7MBHhFDRJzY/zPX8FkzkrtfslOyBmbbPUxWIlyhi3p0DL cnipmZbo+zvoyHIrzVTVMLwxwr0xaW9jfaOuGUbZvxzkPXG2ra+6a0+sAY/+xZGryBNrL G23JSb8xKcqH06cAPwYgFm4Py5WNOQrvOcqOafHWuNh6wXQHQveSabNiszXdTXqyMYzzl 2GZjhERS+qMG9t1Bgvo0Prx0bxBDostLRKEocEnnVNoNhfwD0649ryBuCHuYBmuI/4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512283; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=2MYKoEomWOY40zDairFOAT3bHQcxkBLLi4VK5M4xjuE=; b=bv3C6L5G50t3f1D08CzBfZ/SFdsjkT4/UkZmS+S+EXh1vKROokCX4rxgGJ+A15hPkij7 hOAfxXVolrc5r7aEzt14diGGzSopOTn/7r6TLJceRvu5NZ+ED6T24xotrUG8FkJr2q2PM a5x5QuqGh0CROii3/XrFkSnnTRmQO3IV+otHhP0ILRTi7lj2xmga4WX24rgXNtXRTM0MZ 03uiRRvYVFQ+jvisWECidY6oip64fWyVvYNiFMBICa7fo3YQCpYgmPte97mUUofh4BbWA YuBXwoX7dVx5c2sviymt7XO91Q+aiIKep+9QpkzIj/ehBM+8KkdUTmpQEgbfW0mfAhg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=kernel.dk; spf=pass smtp.mailfrom=kernel.dk; dkim=pass header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=fTSBoNFB; arc=none smtp.remote-ip=209.85.167.179 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7D71606A31F for ; Sat, 5 Dec 2026 23:11:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B7D71606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=kernel.dk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=kernel.dk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.dk header.i=@kernel.dk header.a=rsa-sha256 header.s=google header.b=fTSBoNFB Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-57e4bd7fb1eso770793b6e.0 for ; Sat, 05 Dec 2026 15:11:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512282; x=1797117082; darn=vger.kernel.org; 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:content-type; bh=2MYKoEomWOY40zDairFOAT3bHQcxkBLLi4VK5M4xjuE=; b=fTSBoNFBtZS8Dtsu5r7Azo9hQdcmo6Hlt1FoqVxGltHVHabkcUZMn8rAFATwQnd0oH 0hxLjEe+6JC+BkAQB39d6uPcvfcJLugjeiid+p07A+6HZC3Gwj49y4ycZobGozTsQJr5 eqiYHzi2Xoi6obBj2lFmgABpOP82xiN1fzMGEZwwSrUz5vuDjhHKmovJmn2eUI4oWpp4 Wd+Qf6expM3nwbNj91Ev3LqUXwQ8ZnPmzzuxitEpPjUjXbn95XEzwAdXsOVBYYCIk0Rv 4MzXIIxPutdP+Sk80Cu++cWySNHOpoe1+ebWiH3rhIZNlamtKKCgPDqRK0mHDwaBTLMF Z4ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512282; x=1797117082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=2MYKoEomWOY40zDairFOAT3bHQcxkBLLi4VK5M4xjuE=; b=zI+5GZyP77e6jZWPHkbzM+vV6q7WoI3DWPS71aQI+qljXapD+EcmuTJFf0ZnvuoEi5 Rc0FweFS+jEMUgFZ2h20J1+02PYRK31H5M22hLDzL4Z4vT1CTRZ5+/5yS0X68+BpETFY FDZDy5FGN+uaEaUjpJ2Qjo72kmkNpDKibfrgleyRmwcI2/JDJDtZBVee24FdYsnNWBlR dO2ZA564AG/WscjqfokO0Or24nuRlGZTwoynvifL9aWIuUndPBsgawSUe+xgITRyx9fN 3W5fpE1NJEWq/VC70ah4VyK2Lkwq7x5u00hkbK8Yua/yrcK1n0MwNB2xr2P+FXFTAMAD 43+Q== X-Gm-Message-State: APuU4Z8gyi4Eg2f9vA8xqy/3kp+5RVUQOeJX+jzB71QsEHlFJuZvviTU Hoopj6uQomBQMBdyUfg2SBgXUcw7zu5X7qF/Ww6nadznMewd41TqgCQIuq7bnShCR0o= X-Gm-Gg: AZFdTZdx97FQn/I7Px2kJSjxo5g+CZ0AwWCPQFarJLgl0FxCtTXV59Y04CEZaP1MXv8 kvN+YjjbTd8P/FLqsGpIaMp43jBcivGRnlZI/7qB5BSQOvia+VZSps1d3I8qfpDhiMG+4A6h5XW DnFaE6F7pG3NUmZJTPb9W4oKhuXz6fddKQTqY0w6nSg5EkqmpJwYBRYuIaPyto/KHof/Guxv30P c56b+JPix1AuBv+N3MgukSK+KpeAskrQWL3G6PtCVgZpvXQSriOTVkg/WGUhKzRCQyscg2fpYY9 lcjG+u0U/5kwcoYoSZG4wHgfw72Mvuk2eXo8dexXJpx/xMjYircHKb/MHshujed6DzY1upITzAz hn6bIR58uY0M7TSmRNCRu8LmBkdr4iA94ameN1Ulq/zgA1FOYXjnydERmlhAHDUvzW8EWyY06gi diZbeIryDE8ZbJLBCOlKglBQwKO+I12mUCX+ANYJY5O/x0QBi3BKch+JC31/A7XYYtySRdGslvE 0KSLu/KaIpSjld8rpmWvYCXgQRgrxZqsurlAh6t9kZwPOravhTnbMbh1+v1AaQH/yTl7mntvUBz 2YgGbDTdAAaFhJswul+Gx+G5+38Kfw47iU9jkMF3pB2VCB8NnkMr+qz/XkBNOYW/AbaGALzBVs7 ewVAAXadhzpEVkTqgpx6jNT3vazv5sJ18/HMu X-Received: by 2002:a05:6808:138a:b0:573:2541:180e with SMTP id 5614622812f47-57f205d2b6emr6400369b6e.10.1796512281674; Sat, 05 Dec 2026 15:11:21 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57f1f65febbsm4163964b6e.6.2026.12.05.15.11.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:11:20 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.12.y 06/15] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:10:58 +0000 Message-ID: <20261205231107.4023506-7-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231107.4023506-1-axboe@kernel.dk> References: <20261205231107.4023506-1-axboe@kernel.dk> X-Mailing-List: io-uring@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chris Mason Commit 0e433c705aff3846356f402e8b054d6e4ebbdbe0 upstream. On !PREEMPT_RT, a task that removes its tctx node on exit or exec can race with the exit work of a closed ring and cause a use-after-free of the io_ring_ctx. A mutex has to stay allocated until mutex_unlock() has returned. But io_uring_del_tctx_node() holds no reference on the ring, only its node on ->tctx_list keeps it, and io_ring_exit_work() frees the ring once it has tctx_lock and finds the list empty. If the exit work has queued as a waiter, it can get there while the unlock still needs the mutex: io_uring_del_tctx_node() io_ring_exit_work() ======================== =================== mutex_lock(tctx_lock) list_del() mutex_lock(tctx_lock) lock wait_lock set MUTEX_FLAG_WAITERS mutex_unlock(tctx_lock) clear the owner __mutex_trylock() succeeds unlock wait_lock list_empty() is true mutex_unlock(tctx_lock) io_ring_ctx_free() kfree(ctx) lock wait_lock waiter = lock->first_waiter Each node used to hold a file reference that was dropped after the unlock. Commit eebd2e37e662 ("io_uring: don't take task ring-file notes") removed it, and relies on the exit work to wait for the nodes. The fix used here is to do the unlock inside an RCU read section, and to free the io_ring_ctx only after a grace period. Fixes: eebd2e37e662 ("io_uring: don't take task ring-file notes") Signed-off-by: Chris Mason Signed-off-by: Jens Axboe --- include/linux/io_uring_types.h | 2 ++ io_uring/io_uring.c | 2 +- io_uring/tctx.c | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 3fa179dd10b4..a06b75630222 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -406,6 +406,8 @@ struct io_ring_ctx { struct llist_head fallback_llist; struct delayed_work fallback_work; struct work_struct exit_work; + /* the ctx is freed after a grace period, see io_uring_del_tctx_node() */ + struct rcu_head rcu; struct completion ref_comp; /* io-wq management, e.g. thread count */ diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 3e5201e19bb4..e61d1a124a27 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2865,7 +2865,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) kfree(ctx->cancel_table_locked.hbs); xa_destroy(&ctx->io_bl_xa); xa_destroy(&ctx->personalities); - kfree(ctx); + kfree_rcu(ctx, rcu); } static __cold void io_activate_pollwq_cb(struct callback_head *cb) diff --git a/io_uring/tctx.c b/io_uring/tctx.c index 63c97eec3038..9845bbd7551b 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -180,7 +180,10 @@ __cold void io_uring_del_tctx_node(unsigned long index) mutex_lock(&node->ctx->tctx_lock); list_del(&node->ctx_node); + /* keeps the ctx until the unlock is done, see io_ring_ctx_free() */ + rcu_read_lock(); mutex_unlock(&node->ctx->tctx_lock); + rcu_read_unlock(); if (tctx->last == node->ctx) tctx->last = NULL; -- 2.55.0