From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512323; b=4HoO6WZQzA7OjB4pne2hqMsH9Q98jw5uQavPGh46nJwpkGQO4CjT9VbWeOZdbP4Di+N/ eZ64ug61q/PYCuDbME7wDtvgWdXXzhy6v7zddMsLh+u+yowBfvjVNXoIo7PwW9LUoKdA4 WqLH6N4BROKOXcA08wnUGeZK9BB2vhK3aaMA2o56OunUQRZbhnhrhQ245l45zyNFnQUq8 9BpTdh9gyeleeZPnpPkbEUa78FXcSJNLx5yB+bynszLLksCln0OD41Mk3xPdjgXcL751b X11YmvdeiEcAeY2dk/uXHJtqPATP15KTH+VvCl8HBYxwo12UDXTvDMiGoUGXffeyz3w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512323; 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=5OtSloEHKnAsl1tpRTSlUHZXhmWx8McIAp5E4Ekhymo=; b=Jpk+WgE9Eynugqef6kbfhceV7syhLrwgiAwmGnyrjfSBj0swi6axFtj5TiYjzUxURcj9 ZV6CmYrLrlvrlXkaUN75Z8KH7S3gSoq0dfuxeBO7pDwa7e7wBM9cgI8ZwjUVjtZxdf+Sm +I0B08RQaYnOLGO6b5cIxRnb/ix6+pzRlRJp211N3d5NFKbuUtdAA92qdXZ0qcWqy3AHc oloM1gPNDmZOZBhmbkdJrl7tB5IZ6ucH4xJuW8sIHqVv5jLBeSIinxaIShWjC1/NoMIfY SeLfAPDPIh4bNBVVd5qKrzSapAKebThbOt5EvvOpZ4lX3XTQFFqt8HtTaEkWbXjLvqg== 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="fwa/wSWe"; arc=none smtp.remote-ip=209.85.160.54 Received: from mail-oa1-f54.google.com (mail-oa1-f54.google.com [209.85.160.54]) (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 29F77606A322 for ; Sat, 5 Dec 2026 23:12:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 29F77606A322 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=fwa/wSWe Received: by mail-oa1-f54.google.com with SMTP id 586e51a60fabf-43bf9548df4so2096968fac.0 for ; Sat, 05 Dec 2026 15:12:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512322; x=1797117122; 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=5OtSloEHKnAsl1tpRTSlUHZXhmWx8McIAp5E4Ekhymo=; b=fwa/wSWerqN45rD1+0lMJ7pZErH15+ZeLZ2WjES2Q/fbLt9IowecKEVPaGwnEEc8Fm y3FNisU3AAuEdD2km1nO/gwd0omNmXWe0v5wiXYkJnfQL5BQ+KoO3XzqgHRRKFeCavIZ OAtBCMQi9bdMUpvAYWxFrN5eWUB3pmPcootcxz4lMmQmtipy6CR6rSIx0gnMn3n8jHuv 0wvmL0ttX80uvzx6Dzh2kdcWDIxLrEjWoX2mO/tS/rWx7xdshpKHrj3ZbqXAMafkMU2T H2I8Xt9w1boPctV2N4Ecmt/1agKy3SNSnAdHBgN1rErXc2uIyQST6NaAY5PzeSKXM9OT KCCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512322; x=1797117122; 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=5OtSloEHKnAsl1tpRTSlUHZXhmWx8McIAp5E4Ekhymo=; b=XpN7B0mjxkr7Bv0w5xfEBqheyd/2Y1UHxv0n5DqIo3V6EKhy4FlQgC69zp2xjZqSK+ LC/xbgfALc4JxkvpmKaTKQkMPqiwZ4LfVfbib6r3SRBd7XkMLTxDoUspnnASvtbjXWSZ HrIVZk0v6uPMDhp2gEJgwXiMCY0PC5CPigofEqeMqu8jivWUhjue6f0b/YOBoEhxCdC5 ipmSST8r3bQF9SSWE9OJdTrW6+/GhK+IdWfYgufOX/Zz4b5qYBNuj9BteBTgwZ909Cwf CPQzgRSMKGb2pleplEFTmFQra927AS07ZwxUa9V07zNGx2+2Lr2We5aVLlHyonmqkB8p ZznA== X-Gm-Message-State: APuU4Z9WIVXU+k1fgAeDD8W6e3kuhnGugELQQXHNWyaqLrfa6Mnda+i/ QbfhWh48hqNDgTulZ1ry8xw/eVCfazJoMLn77U7yHHo9twvIQtlfIrlHRc2mAWh9k1Q= X-Gm-Gg: AZFdTZc3DwR71Vy3dN/T1KovqRtb9ew4T5uYHcDt8wIShoptP0qobohkw8XNxolcRLi BRbEGOQLj4FFwCPOv3Man0zAn84RFHMR1WRPiTpMEe4edcNtSJQ4iBH7YG+CAdRWKXuY8K7Z8rN 8chAExKUUQ5S4/s8L6AyAB0i5N5IaWHOmrledkSEgBzlkWc+fbd7uuhkkf8Mm45P/Dhu/1RiLJ5 iZrBInr42vJooikGQ52U/QjF+wKqr3SxAIell+I4i2LP2i6d5I2ZnseMX0Rv6sQ20W4X5LXEibx 1QTDjctgpNFXsRA0pMsZb5JbF1u0f/XOWRhKpJIzGya2qfGfWFPEJNwTk+00eIOMRW5K1I8eI2f 24PEnmMdEERwM1aH+d7nJjzGYhiHn6RBsOrIxRGqJ0JPSwGbPkT1FThImk4Pt/UxUOXf9BJgD8b Yd8Jg3HUIHtvZqXY9z79ABNczwQ5ZjWS4+84GlV8+XxCsvfNumkn/K3dULT5yV0wECgLnDhhGcP 8R7UGP2dfNdhqIvrp+Sdy2DhU9JPXFWss4vMzbO5ucVqLp3H3CeGmI+gxi2VqZ74Ba/hbOLKt7v bLPnPITRW3Uk6RcxXwULANOor0PdDuhmmcJwsol6qeFJwE8cy6aqnmyyPbii4XiVJqkdfE5ptgr z6owNZxgtuYxU5NY4b6Fc5lVKZfNUmQLrso42Mw== X-Received: by 2002:a05:6808:1207:b0:4f5:eaac:517a with SMTP id 5614622812f47-57f203ed4cbmr5450958b6e.1.1796512322107; Sat, 05 Dec 2026 15:12:02 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57f1f6fe2ecsm4086916b6e.14.2026.12.05.15.12.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:12:00 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.18.y 06/14] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:11:39 +0000 Message-ID: <20261205231147.4095031-7-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231147.4095031-1-axboe@kernel.dk> References: <20261205231147.4095031-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 bdb0dc928c6e..a866cbb92620 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -447,6 +447,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 fb4528bc3aac..d300026c74bf 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -3068,7 +3068,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) kvfree(ctx->cancel_table.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 c7b08c9a1657..602b5af32053 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -186,7 +186,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