From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.49 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512283; b=B3wLR23Y5IBVA5npMt58jf/omXxlmifQ2qhUdRppinPbAmAwQnf/CLReZztDwUPkVY5X NJLbhcOw33QBjMJ6f5q21kSGRzFzKbVyCY8VwYQ/AVkPdbbnVyrVgzQ08FgDJA2SFi0RF RHQCkYPZARCgVqrYu7dowu+J4H/zl67+U4+Ekxy6Iu97iamiuj0VxBRQ+7kAHjW+L5uA8 vD+ty4ZEaLYzw4JpY1jIbhbWmgIluBjdTyEmb+39an30lJ6sy19gYj/LF5WywQfYyqMU0 MFwTWn829KvZQWB7IcMg7mXUTwTo6ElmqUhWHk5xAM19XI1l8V+cSwlyfuTpX/JiV4A== 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=YSXebWju2YM9j/y0LYoQHKcjtNMQKOUoLktwiH7u1mmZxLaI5pF1zlsp5cu5K8I3KWBe QL2FKLCR4BFP3uPk0Y91rx2UxtUHJFh/osNjJjlDTAS6F/fjh/CtpaKj0L0QseR5NFMpV klzrmUkk2qHJ9KDuGv7mkMHZkd8FgYMt36Yk7VYoYuw/VrdAQ9qQrmliR88AT4XMewgW7 3qJkKT4d1KPTykmwBrbLYIxxQ5C+l2kx4womooqMBDd74wUxL/gl1U0/5CZQFh0jKsL2Y U/ZRoSQLXdxMu2DURVa0RnSOe59NQNf1KU7OLuMhkqpsZUtlifKIHYpFCI1bPQWs0LQ== 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.210.49 Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (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 C29CA606A322 for ; Sat, 5 Dec 2026 23:11:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C29CA606A322 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-ot1-f49.google.com with SMTP id 46e09a7af769-8470d7d430fso1088928a34.3 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=iE2FykGgxYo/QnlQPorJs5wKQGBAPg1mH6NSaGgbh60lYkRI8u3p+QNkFigl40fxCB uVVE2szeAMHOYBvpi/6VWypw5cQ2ROx7JZUs72I8dB81HPsgA1w64t0f7yIww3ccXBKC Ikaly89LTzYJX7CHPIpOnUCXKumTkVMqU+hm6ICOg64QEgTqHUXLysAsjjbW1r1bkXi0 nSaInYrXlF0cJNEaZ+o//yTZFHre9AKIvzlEHsqBxLqFeBktK+TlE8ezotSBQSwa+g1C 8Re/2WGxbXVmHxcsPH+27kyLg3QhZo20JAJW5R+eCZ4nDoi6ngCu8gJO/De3O6NKQz82 mUZg== X-Gm-Message-State: APuU4Z96JBSFxzzVps6f71gClN1tJWpW+5fQFouCwprTYGGLVhJtfwiD iWPVS9hmowlUZp4Js13/62t4N6UuzXRHrjVg/ob7el7FKKlYXpR33kQb+XDAf4yaxQoPcD+nnHN 4Ye0L6OE= X-Gm-Gg: AZFdTZcCa79Utmv6F2tV5V5ALqd/ZwthIZ5LdddtY1El8f/P4Xd59DasEgY00+lGyZY qsbTa2ppw2cRdscfjsOK2QxtFzG40i0DrtnhksZFbcqcV828eXxWXYoIT6kzMCz9UTbTMzXcLsS jvAZe/VSX5MAfH7i5rnWHxPJ0/hUuuUu9gU3nYCV0KMiZNH29xE1ks101ZV+xE2X/BRceWMJvEV eQQEUSQodgofkIdvu25p68n6taIEJZS/2kjQzPsakcepN4ZWUATum7/jvqp2gOOTuMCTTAZB4b5 yZ4EaN4PNsAIfZGIfouhKvE48Si2rc+lLq3uFSz0h/Zuxdy/ELvFY4KeMylJ0+S7hGz+ae3z0cY jpKszr2AaBDt+c47DXRYbjoFQWVAJB3QvNfjLrdBYlEUVJHVQ9OpDl0msJIwtmxs+sxQREtVRo3 oRHvlTj3Ugofoj4WgQH/avFNtafaCfm/FqsWH5VNoxrmNPBmLRQgdDcKxhV9EjWjcOkbU+7W2aJ L99pjJeidfN9EDQFshneMBLXQ6fqcLgQJOwaHralRT3PiG7MeMameDdT/OQpATZPY8wnPuc8wkx DtECuRtP9CpOsn6ZXULWkjlyPxqPnwLzQ55GBYAcDIxjBmkVo9arHZA0TtwrzBuPxrlCve97QXd TTZjZ5Ohvizsc+pLkQ59THFlriVvIXstl5RI7 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: stable@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