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=1796512396; b=VfK0FIbbrPbR5UXXyiCk7N335927Qhqgj8QBEEe0EQsdCPeZQMKcTNCHuy9bwNJSw4MY LidfGGNQ8s3LCNue5txxRRKsDd+1FXKSHhwga2OOLOEQgvjb1oQXzYLquXMPI1lW33uus UsWCKrfb3uHkwNhEuYGEA9eBIb2xY1U4CGb2kTMxy+/JrQPyariJ2pXCv4wSsXRpJB5Du eeO8eo3nVLlRf1wXD7NpAOXOniLafT10mqQ6P/fTv/h27TlBI0TwgxxW/eXyAEUukFB/l exaPR39yPat85q4f4YNH59mSsx6s3Ud05bBHCddILbUrojOXJcXbzq4tFTt++U9d9yA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512396; 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=AJFo8cFFA4fFw7yIDFsute5mXWa74kMHHLB1lmc8A14=; b=Ci2J7ZUnlZqI1FDtbW+pE55dEBBVqF5BlvBMwBuYbcCVEaIxPqMrpfCFm0VYJmPSKDto aTaj0RkEBmbYdA+DAGD7jvSiPTgcSCxI2JLsVbsfGU0qDyeSzO7cG/uVCvkxqBwusZbeR UyvDqvYC3u3BarX98e/YgjTkFO1COp+XAtfCSnW/EJdybcaE/p5iqIE/h9yZyLWW+0OOn qQFpRQ4wuCmt1Xnh3AJizxq4ftziUmrXqMjeBLYdg+qxFc10aALqG05UsvvN7KzahLty7 JOjkKAOO4S/VQABidnPi7TxlyDCld3N6HPGbUvfg1ZuBI1rNY3wAdPZEIwrYe921b3w== 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="b9IY/n13"; 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 75BC5606A330 for ; Sat, 5 Dec 2026 23:13:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 75BC5606A330 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=b9IY/n13 Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-8479f46ecd8so979181a34.0 for ; Sat, 05 Dec 2026 15:13:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512395; x=1797117195; 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=AJFo8cFFA4fFw7yIDFsute5mXWa74kMHHLB1lmc8A14=; b=b9IY/n13e9ffXlEav3uSUfobJc7J7LEwEQpaoIZQhYdYhk6BsflMoR3IrlGzDrv3sZ FgT6Lz5oLIohU4YU78CREnhEkqco7axZj9I44/q7JRvEFQIhn5QFl24kf+c+jDsk2oyj 06+rSOExHz5QsOe/o/9LaNSxZyEOdneIkTZCapCPuzUAlL8Oy65mvJRhuXE1VOfbOsQE 9evkcLofzmGPAVC6Y23TiLB+bjduTUI7YmDfIT9rm4wt50ZkUW+vWMDkDX4sNMqEzCNE fpm3/FLAvuUYTaZ6IWHhJR3MUujRicFyuOE3R9T1qJkw62Z4SqbBcOXvGU/vR/yvu1nK hkEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512395; x=1797117195; 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=AJFo8cFFA4fFw7yIDFsute5mXWa74kMHHLB1lmc8A14=; b=0NO90USUqFfV8jB3NbsltzhxQRTATBWoRBNagr6eErKWMSv4g6bzfjoNKNj+9LvOsK oEKuxiQjEmUJRupfPPo6hA5w7jH/jS0RV8aq5efuxs1PC97UIRt3sbNok/+gGKy7ECfa I25XBcaDMwqp/d7OndTvs+CR6pj30j2GVExg4jh4AOQrUDJHeQ0JKOEIWiVTpQFMW8d7 aYr3UIAdHgKb4PFp8UH0tyyf8GVsTNc/qv76LZtS9jjtqjvxoqGhMSboFwIcfvdwsmQi utbzulkns/CWxRx/XBR2kp6EJtHDAJHSfQJXcxa+lSJ665HoWKxYsNeUp5UgJ5gLPDt2 UOCw== X-Gm-Message-State: APuU4Z9IF+daaGiu6n+BwUHOsPEERHOHasq17fZRTFdTu1i6sXHS1JXD MOkc7pQlzx6zFePziaD9MD2PlF2hC7YnV8Z3WHUHMeAZ7yYKbRKz11SD6AtUKTcVvUs= X-Gm-Gg: AZFdTZdPan7ReNAIADB+YhnVB8B2SjCOjGdm2Yb/P8FObSB2P3iTblnKD80Qif/YKbz n/MdBP0gKGKkpdiohNcFB0xWZM9nEA3zow2S6eTp89HeuuuSZYAbTxxsXkdic2qXPKW1U52AK2J bz8cu+QwEPnp4+dqDRHFQMCVB6sn7cm739KGV+IhyBMO9LEMZMZrDKuTuA9jMacy9nwDCKOjPgr eACTdnAIImi48GhrUInFAaLyn2t8KDCUYRqpeaQj6S6ZPqZB9RQTn+HWwLcKIga4gSmiLAn2lNL +CCs/aB7Yh+OjNxpBO8KcgQ5fni23w5N5aTtQG6k9bAMMxjGbL8Wb4V2HC8bfkb3BjGT9dSQqmZ quwxeLGn5b/TPFl8fH0bq//KFCrtgxfcIy2jTGp6vvQGwDeQ9gGErlCzSj6vIiju2k84CH3Z0+e pQqSqP36GUfe2+LhHxYYZYpn+/cCIYBTTg3ymwPZis5W5/WIEBlreYuxApFQ5fAgxJAZFMHDsPN VpeLNt9gEJDCUDH9I5EhYWKpa+OxB41XbFlRG0D9+aH+10a+TgxSexoLV4nuPBTMmP3e82CsyV6 LMTDk//Oc+j/oGYYpaB2vo1TOxIDTYAdpM62m94/Iqx1q1KezZD/2S3peCb9AbQ6tCD4B6Xnh/7 lb+8UCUWaki/qoAns0rJG+Vtwiv6usdWawSib X-Received: by 2002:a05:6830:2b07:b0:862:9ff6:53c2 with SMTP id 46e09a7af769-863f89c5b20mr6032203a34.10.1796512395415; Sat, 05 Dec 2026 15:13:15 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8331ff6sm6269737a34.22.2026.12.05.15.13.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:13:14 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 7.3.y 04/15] io_uring: free the ring after a grace period Date: Sat, 5 Dec 2026 23:12:54 +0000 Message-ID: <20261205231305.4158103-5-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231305.4158103-1-axboe@kernel.dk> References: <20261205231305.4158103-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 1c85ce0e471d..61d4a9e3549e 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -516,6 +516,8 @@ struct io_ring_ctx { /* ctx exit and cancelation */ 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 c6bd17cca45f..f10035424a78 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -2323,7 +2323,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx) xa_destroy(&ctx->io_bl_xa); xa_destroy(&ctx->hpage_acct); 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 b4fecea3ea47..c6b7f5647f79 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -231,7 +231,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