From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.51 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512365; b=tDJ1bkakOJY6P9ZcfXR/PBVYQj6ZgY3OSz1OExx0MY5ZyWfaSiierY27VsyoQg/HhVeO CvDQJNN2Xc/9aMQhSmnmyVDyOWhrXxas45uohLzSg5jz0P8mb/pV6TgthrQP5LjdQZaTc OoOLnvCpJWO6N1ZYOR5X0NO4RM003Wv/DfaWJmHvnFNNP60c4NijhDvfb/d02xan6/8DC Et58XF/QXovlsdtSyK227dSsfJ4d9VGmsxLtIxE6Zh9IyvfxWt1KwYwbQhS/IPcrW76qO +l2KfGEcS/T7Rjj0D6NyHhCjQy9QMR9GQKZEF18W1vkLyiUjZyk1ZdvDStKjRSrjBIw== 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=W7VmLEIC+oOizOTlvZfCbFophSA499k24PVoxs2+ZjOTVGpIusmAkQr+2N9hhnCGPe51 qK8q97giEAybRzaGVYqpZ3lR9nAlCk/KQSDhdq+lre1GoHus5ZRFgktdoJI4VahL588aE yeiKJ0EGhy+X9fpmszcEpxPyG5Z18gONOOOvLq266101LJg+HW5yEBMmUKj3Ymwl1YiQ7 doknLj/uF0+AVWG2y5iu72EQZ652uG4IHWXG5Bahxm7QRrPIlDXosfoevF58X9VAXO0zE 4DcDLQ7fW6iIJQbJsrP3RmNrmHV0SMkgCfOUI4GunCAbyCmRtqOb1P72OSOWB2vV5Gw== 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.51 Received: from mail-ot1-f51.google.com (mail-ot1-f51.google.com [209.85.210.51]) (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 F3287606A33F for ; Sat, 5 Dec 2026 23:12:44 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org F3287606A33F 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-f51.google.com with SMTP id 46e09a7af769-863bc454a85so1360307a34.1 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=RSZxY2yvADJpyxNvC2748z9K1Gm/LOdq3EojUnlxND09UrERxsGH1g7OibiRxAAFap PnfVVdwOsfpapHjxSY2MD5oyxLtj3eCRchQGVaGn+snwRxlylRpIL7x5BfYc+5vLveVb Vpc1vhNzad+8h02WYcefSa7MicMsZ4ZoOa//TsICYCuOUV0fkHmCpOVxHn9B7gVfGD3N O8F1163kcewZcfKjJLonHFoS4C1WLaViy1rxgPRoorWYIs064SWKR0SSP+ypUucCuurZ QhO+gM2uFzBMsPOWOZ+AkEGRchimbQ3Fh/vcUavP/pkr6GSDGXW0URFiOFK2heCHLChC Zfgg== X-Gm-Message-State: APuU4Z9AhRHY6ZO52CHnxZACtgIJcjSSLYOUMWvb0WE0noFPLNuW1Ecd wKn+UrrIJqQq1DfNpp9Xl01EK2luJhiNQeoAqSKMOiUTfmYuU1tDGOcJ7rQ8izmZDxvqCpAnAUu hE2rIbSM= X-Gm-Gg: AZFdTZdezeg9uquchUQDoxdFGJrjJt/WQGxETRu3Em8lfhu83lDMiiX45RWK8yG/ct3 7WzwWB6bkiMhva0b9dMeVeDM8nDAZIhYsCOIbLgwxTIhBdStuCF6iUVD0WNHz4ZL91P4auR17xU ck4tHxf6EQQJVoQD/0EKFsmq1CAXTmzp1rZJDDlK8364DEgmP65jlqvzZ9U/3pteOsBswwS4eeZ BBpMptoU/LZVQDI1XLYk0nqg5uuPmYq7mmv2ZTcS4GcC6/yA76kAFtYtWonDOl0OnU8sMucxed5 B9odzlZ3T2abhAg76UVif+okI5cstQ1EuSdVnWgnCqtttlc6PBfbHxoNRCxvVTj9MibHb+CD1kt NvnY1gJ9natn/fVivw8lwVP9h3r8BlWhIsiMkGKrIa8DdXnvYvXVxZ7cjxtl3ySJLH5wRolk2Bu 7t27Sh/wOZujRFE5myM8+6ZDYfZ6/8XHSit5dGhHQCMk85hVrP26dQhWyzHxrIrTkTUvefeXX0Q Q663KN7nUSYN82I7BaVRg0Wb6mZlveCHXmb1B1qAXpNf8uk8CmK2S5NSI3yylqlvxtIdPYbk15t erfvwu/KOQEk0BTEAIHfviGM/+lxAdGcoDecbgsgX15i181lmLxLyFTomXjUQP4Ezk6yP9CsHp8 uqceVvtQTYX09w4SgHmKaswiPme2jydKakhe9 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: 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 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