From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512365; b=x+8oAOjPv+cHAG3qIu6p7OE7BL7/4hj2wJVj7hv1nHZ8mAx/q0enL5/aCDU/cE5pPgXf qKR3Y4lWV8FftA4hgG7qXpLzvHv9pH6UZIQFtC6zdSyXd01U763d1S0mQpmnjRJLf+VWu 8s0W/pT52xT5FsV8fH6wqOnRAViGFJTbX9+gBP7klQ6ck3U7jCW0y0L8hF0c84np9C5mX x+xq0rwLPjr64yhMAcyQGfD9ggF8p9lFGMW4W7K0CQRo12uJsw8f8/5zMpPnsWvdFYfkC FD56PPm8sp3k3ehS37Ers9HcWL7cIkW5ccNpvJyC6Uv6UQSRyCxHLoYxljh6n2ZR0ug== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512365; 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=Vybt+JWCVjipUDqRxsGbV19pW7YGWWgPfDI78C5Y7J8=; b=2xpn9sxYBJxVF6XqxlHx7Frs55WO0PPxrVq6VrZ/Bll7x4NrOgWrsNKqS6KvZ0uO93ax BRyfKLT5LGuF9Px9jw0zmhT0s1hPU5b5/7taL6XWeO7+WYpm4ncglE9KQWA7ba59+XoMW DxCgDTkQvniRopq3sePXT7yBg7+TQuReUTTH/CEvl/lteN6sxPdABu1k+wyxvr92JUTwS 0ZawrJgb+foAC0TBjhZSLGfdvw/0r7PpWkmRk7pxUoAc+E78KktQ2Jj2nLpB3hGWAcMui U4BqQ+2pMdVc43s7vLrFkR3e6vclatWOidaUEL5wkaMwd7rFGpjMcATXOHWYMbd23Zw== 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=O6xXnCwx; arc=none smtp.remote-ip=209.85.210.41 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 E33C2606A32F for ; Sat, 5 Dec 2026 23:12:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E33C2606A32F 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=O6xXnCwx Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-8629c7cc36eso1334780a34.3 for ; Sat, 05 Dec 2026 15:12:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512364; x=1797117164; 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=Vybt+JWCVjipUDqRxsGbV19pW7YGWWgPfDI78C5Y7J8=; b=O6xXnCwx9Pmzk5sWILi0OoDu2LR8nAWAQBOs26ZJakOZWDPVVbESY4rxw4zhsShHV0 Yc1fU2uMKJY7Q1OYS4MQJQr4C6nbAdd1jIkMhaslsbGYMFx3+IvxV5A7y+5fHfRB/car Bv25f/9TgA3oXMxeJqmHeLGJYuTLMGRv7M7FMwqkBFQt34IR+FeiAAvhXr84GgrfA12D kIV1B3U0brl7n7SnPuyOoHRTUnk4eYTAnOSlU5bVP4wJfz3Lq39FB2myVi1+tWaoQwQX XxJp5E16xlBL9c02YqIAk/L3pPbXpEr6ZSjt5imqLLS1ZS1NqWzAobndNQRvCnMDrzcM Ezog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512364; x=1797117164; 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=Vybt+JWCVjipUDqRxsGbV19pW7YGWWgPfDI78C5Y7J8=; b=DEo1LaJn25P5t2iHWwyT9+KVIZ0IvtGXchgo3V8mdvoxvNP5A26Z7NOCyYKipqiDyv wxedw1+/N2I4/JVAhsA1JeLD3UW/Po17UGqYNdsFAiFO1qamNp77IC7nBagbyInOUCC0 5Dsz9WjJFUTtjkwDgvIcPym3r7bp8L6bh8oJAWA62HpqadfGd3/cUDWWFKgQvTa17zZb ZJu/pfO/GgZhdrKXTSCj2X2r9DebMJ3Wgq33HpD1PaRfBgToCt8WAHoGnLnupL0w640a WiBx45T/u5ps+kz1/LDxPz2nQCEjGZ2lpcs25c1Kc/VPlex5AP8gGe2l9xwOR6k+Yvv4 KvTw== X-Gm-Message-State: APuU4Z8p4NUEXmh2gP+RQX3m9Xm4vN5be9aEdQHFi06l4HLA0qwj84xo 6AGpOVTUcZUQgX4794uDSwsNsx9mrQdXWcbkHBuRK/hbAykAT0m/zb60YfFnIwQO72IvBhYn/2X dIoxH5+Y= X-Gm-Gg: AZFdTZeAHCyNjkGNSSjY/l92kn4xjL3m3rij5WMoe4ht3Am1BR+Y8jaDQT0MqgziY1P F+DrbifQYwOOqMhJkmZk8BytI4q0vg/7HCtBP2mmhDH7acUdWqnicJ6eAStLRSVdZxwyAJys239 oKie5Qy/hK6fA3SLc+Ine0a39njdpy0NFw7b02zJkdW38kgx0pH51zdY9ihFJS7K5Un5cH0fE1y saCwbuxbXWlb4oqwC38CGsKH/qahJFjg9l86TGX6XZQqDNB/Q9BcT11rk0cfnnrl9ipOEtxFKKl qfVCvIrAOlqZGrtKctFSdaJIc3WXSyUKfVIcWiVXfXWUVW/4huc9CESVtzD4i26FCgQ+EqEXb7T Vj2rVfEBWq9ckAWbm8dzFg+Eqzon+GUXDA6bvhXWvm6KJcNvBiTY62NeRyZvjT/9h/MoB+ekAIL ExIid6FZV+7TYhSmQLORHn76m184e7uQ2oditgGziUow7YL37IuqqfXYvhdv/plQ/Ozht7KeUNK OBfTk/jqO3e7G0JYW0mvgtqoEhpmMLWKkgcnH3TA1W8PDYT0yLJ4AXAIqAYFONFsKwiZ9AhyQJX au7yZzwayL4Yeqc0qdsFeXtM5DVQeoGFL9LL+HE0FpBq5aa2sEVRE/4EuBTFqe5YaEugpLKDj6D 5hQwFBIna53CmLkCfjylZWuRIMgckzUKAfn91 X-Received: by 2002:a05:6830:6412:b0:862:a901:21fe with SMTP id 46e09a7af769-863f8a47d16mr6771313a34.29.1796512364002; Sat, 05 Dec 2026 15:12:44 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f7dd42a8sm6279788a34.0.2026.12.05.15.12.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:12:43 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.6.y 05/13] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:12:25 +0000 Message-ID: <20261205231233.4140988-6-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231233.4140988-1-axboe@kernel.dk> References: <20261205231233.4140988-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 302552d8def1..5ba3737c4725 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -364,6 +364,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 056c8a27df14..78b336e75061 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3155,7 +3155,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