From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.46 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512224; b=ihNVqTlYLGw4oH1IP+szJzUEbnGsAmzHJvNdh/TT25xg092Ui8nFsMN5ex8DBfSqn9TM F5g6XO5hpCLSVDx2ikpUG2QeoNRA9VOQ8SXO3vpfthiiPEWHzx8DIwROYXNudiHtg0MwJ Fgiiy2090837k7wfTK5nIQFvEWRe4e/ykBh3f7uCVU1cb6yhADWReZn+wdXLiW5Bjabq2 lA+Pm7X5Zaek+zAhQK2U1Mw0QAVj+PQaq3mHaaLezLsrTdycRCX9mCVr/SeO50UZDNsKy UqE5P4aj8YIBzfidGzeShVWe00pO5HF7MZ+tF0J2GBHCTEBqolOEzmIswz1tEzWM9fw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512224; 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=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=F2dBU523tFgtCXFOIroIYiyRPlWkKZtubSaL2mdTno/HQqFQDq4vOLXYjiGdRe9oNClI Oklv6+ujjS9I+7Sy3um8ArIR3hE3oswpHIgduUBVJTU88MwZPikM5zZ7T1kpi9KrZyTDP f9rGh1aDgacuRhh28cERF7CJ/nR9wcbMkuHqSru4ADKPvGfuRYuaewCBnQ+l1ghQYl4ir AJNhxIIZMmtpFwESPIUuQDnlmG6SjKPqoXnbmrcQodawoDGUKVkJvFzQKKDjkKow8n/8w X33bl+r9//WJCbZtlNS/ifpwKwLaW8vALsHFxAa/5Od+XqKaKIk5/F+BFVTLUyndO+g== 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=Bnhiy1ig; arc=none smtp.remote-ip=209.85.210.46 Received: from mail-ot1-f46.google.com (mail-ot1-f46.google.com [209.85.210.46]) (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 D2331606A31F for ; Sat, 5 Dec 2026 23:10:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org D2331606A31F 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=Bnhiy1ig Received: by mail-ot1-f46.google.com with SMTP id 46e09a7af769-86408192288so531677a34.0 for ; Sat, 05 Dec 2026 15:10:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512223; x=1797117023; 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=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=Bnhiy1igrzjgKqIYGTSkitV/Ksy3bZrnBIiHUUYNqOu2iEk59TQij0mynzT5L46UFc vu9OBpmL/Qyvwg7Hzv1+AGJxkHcsF4khmgh80Ozl+wk/cKjaTlb1kM2nvLDouFt+IFqI /AmIggvsq54gH6t6BjD3m4fZnSmUQlzNW97PDb4KnaMDzNIEl4xmlYNtOrRZUIMCzyJx 9I4EHN5m6st+zuK25fBM2VWH3HoAuSGeSoeSqHWBJjeQu5BZRMqetmqQh20ubKDo7Q17 kOi+WhpBh/iKEdN3nNXI0ZWnqmrP+qzaEO4orZTm4TseUNILST54LjVddHtqHkP+pk/p mgQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512223; x=1797117023; 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=gmd42HYQ8tWbIT7rNjwfI5TeRS5oMc/SPS4bUqBJp6A=; b=AUqpaNDHPetnVUO4e1gHpv3HPGHf4UbRH2lZP80OPtBWrx0EAJfpCOe8qigtJguThz fmHY/UpJF5rS95znk+/LzIfInvF3am7Qp9nLXW1C5h7+UXbAN1jomKEPm5eciI2yk44t EmqA/UWlffyHb/4Y5PwLUMvxSpAD+lsrmd96CIOpZcauhRMKNuuLC4Syz6MEuMd3D7LX d44GxGeyqcjFCX4cK2pfHbw5vbXvVbeXIqWNHI/RJafT37v6FExsVGRCAfP/VuO+clO9 E24/qK0GF7SjT2yNuJiwy76ALdjuHQpq06K7ra5W1H1PtBrHsvppSFo0H05I9D0/kOp3 mrlg== X-Gm-Message-State: APuU4Z/Zy0y7/hOuJ0pu3QnbTjQzeB19UzDRySQ9q9v61VL4FIqLxsst egNpO3rn1GzJ7UekVutMOs33AyNQOpJ0Nzs85XKlRsmw8l0VPYTTvH1DE6Hha3jkqzc= X-Gm-Gg: AZFdTZcqdBUbxvX+XVNUiTmVhibOj+ZbZpP+gs6ZA9Ghln+Cm3R+ECYgWGuAYdFDh2F dO4mDyeyhwvXD8JtpQUWTvUgxB8P0pcJstMkrZ46SlSVYnqNYfDzCx1gRG/p1LB8lz8za8H0ZLW QKlSR9pyrhb5yaO+zFVLnRayPiUxbREcuMNGQWQsDGNv3J3DPgd8BHWNrj9TrE/FNq+7teLG/zl NZsEnx6HTKBiWpJbF5jFYuDDRE3OVIJEeSkbnAhkubs3ghQ/3yhMJX3w0Ls67NBIZ7UPHNg3J5q HtPuXEV5chJZ6E0/bKgWd2UFMqYH/DnzZM5qtzLbGGrVs485ItgiSrPUmK4ZEDh3bofCyTwdnQp DwzCkdE0UXYrrJxcMy78nA4Ev1yNJT0pYNa+t02gpU4k3j1hwOfjqkIbpmZiWXt4G7Hsru5RizJ Rg9dffwsvJ6objaWfEFicMQlJ22iOyih2RDirSTCYRX8YtAKmY0Ui7uStegSfsvKq9wAQ07G0dS G6oXFkGQWiooksLWSfbu3qZfLaelGlKqZT7DLA1/rq1x7SXfby4D8vhLK38uZVaUf9cYOrCQ3M4 /jwihfiDEVIn5fHizoB0x6Nmm1DktK49iqAVXcUPVvk2f8+Wdsi7EYi3UgJRZZ0O2YIvYzK3pB5 JigcoFmqiCtxyIu3Q/2UKaPavE+FzzzFWYXgGyQ6NtpE1ZmknIXT7EpJWNA== X-Received: by 2002:a05:6830:2b24:b0:85f:6f:6c39 with SMTP id 46e09a7af769-863f89c5df7mr4780558a34.13.1796512222850; Sat, 05 Dec 2026 15:10:22 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8313082sm6309624a34.16.2026.12.05.15.10.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:10:22 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.1.y 4/9] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:10:07 +0000 Message-ID: <20261205231012.3977993-5-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231012.3977993-1-axboe@kernel.dk> References: <20261205231012.3977993-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 93d9e8130b5a..8bee77acb384 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -351,6 +351,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 75e92f581f61..dc2cac09cbe6 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2805,7 +2805,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) kfree(ctx->io_bl); xa_destroy(&ctx->io_bl_xa); xa_destroy(&ctx->personalities); - kfree(ctx); + kfree_rcu(ctx, rcu); } static __poll_t io_uring_poll(struct file *file, poll_table *wait) diff --git a/io_uring/tctx.c b/io_uring/tctx.c index daba0a2c4ec2..85d670a52c51 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