From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.160.41 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512324; b=NS7WkgT8AKzkbAbO4aSJCQiff/ANZF/ioNvKhqCt26P6wP9er9JLe9SEFXpzI3tElsXy FXlgqQr+Q1aCSQG6aHntHjx0Ztft+VvgtAs9Z8xS9TaZZNZTA1e21rQ4PzCFt4l7X1TIz WQWFGssOvsm56adq52t2dABZ21E5hOZahgWktT6oEaBEvAdViXuJzRCWTSCggmLiWqXQu ry0CGUCgQZrrXRko6+sGGak8xtG420dmG3rqFD2t8wlvPsb3S/us5oqFmJI2EOeEaUMXk oY6vpMYRHGnPM+ZfRNMLhNcQrM4/UU2P5+3pXE9SjoI24mQNgaVsnTGVbykuEOfE75g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512324; 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=VFdTvjr9yIKSDxnCAdD35C6z1NxF+gl4du0rULbpgwBqVlbqZB/iPAp51kYcmxftcv1r DdlDXE4+csrtrbOB4JcDK6UYCdtyDVwVm/8fVqrOYVuYBXWG1yE3CRCNS03mzJAIV9uYQ Jp6lRTtGXwmmVOOmsrLXzDYI8EDh69xmms7yVA+OvC7ZV4LDWLJRgGmMClBKjUO1slY5o AmgFKM3sNsQEuIJq0wokIlr76VscH2urlZrs232SiPNhL9A8gD7hxIfxvO729GSKVPepT VTqtcw/j/q2PKTzie0fFO+fnE3w0LcbVQU8Fr/tKIXkkVRNxL9MsYY1TVRPXqw1SBgg== 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.41 Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) (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 221FE606A31F for ; Sat, 5 Dec 2026 23:12:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 221FE606A31F 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-f41.google.com with SMTP id 586e51a60fabf-43bf9548df4so2096969fac.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=LXiSKFKTPAhhb6BLKH39ef1/z6g+rlAKLq996HaY2FB1mRM7dZJrfZqRFjVmRC4//w X6Q7yUaEXmjZu0Ksxf9DdTtdFeKP2ESLgIRvbJcBdFI76Q3c+ahjCi0MVeI0VqX+Xz1I p+EQfNTp6DL/SX0vmdlolU+s6ha28Y+F91u6GWybnP450WW17qvXTn2gH8oFw7QizFRY eOVsmKQfb+bNxpBnSH4oRSxmS6O42mQlAo0oK9ijW9olFA5v/eD0ueJl2tQF+qdDHzWZ y9Ws7m72Es1VI5O559m03fBvfBUI7uHuUkqAAMLT3iNfzbxHo+yhCAqZwK6kkEBpCAEk Ljfw== X-Gm-Message-State: APuU4Z/HtVxn+DsKjD5ZbVVvkv8PO9My8OTG6Qy0sWISSMD9KoXsIQAe AkIIlpvNzlZkctT27a5fKzQCGkNJPOdqxCNapraiEXuXkdcL5j5R/5vYfuEAuuzm0omm5Dmc6YC 4nNb+esA= X-Gm-Gg: AZFdTZfCs5T3lku2GT5dn/md0RJGnqcy15WVMEpTgoK1P0IW6O4KiK5iY745/HCqY9M BY0giWD0EpYdtFIIWRG5j53XAKkTu81IVjS53rIF2tKVyAzlFoaGfQPPlHu0SX9/V0DUHnrKE5j g+nTz9vOD6YbdNwd/0dljK5Fx7zXS9aZZKWBTrgVqgcmB90Id+JeYYxHrgvZYSDvak6B8xsRxgr vQHEgm9/hpzOblajUEyHrybYyLxh+9HyIKtdlV1lpWWQWPtL0iPt2xgY5h56fCrX4TcFWJOecAw b3Gg23J3lFxDHVksqWVg3zr8pSLF/BGWqqHGkpDKAPQ4BTZUo93sLNcriXrdw85swsLo5fr4uVD iy/XQnaLKwd1Z3IMHssPeSdlEz6UOlABH4a8d9bmyjei/1J34aigmetXsBKr8Kqtl1ED0FQXN6W exp4I4DpEfK8RWCsA4NOIqRfQKOR5oCIgcmCMn6+0kSFpZcMJX0JAZN0pv6lueDw/ByYI/+UVn0 PK9OYSoKrt6/7xfD4talMuogG5oB3q3EtfV2WH0jr8oq55NmCrmbvo107xNqa1oKX96CmZrLJlD 01rIt70hw8T8SNUHkKGU1e3ax0UBnKrED0kPb2VAmFpWxuwinxDv+eEs02vB126KWWroKRGhVtc Ezb47B80mC779u5EekSi4p1BEX22gpT5nH1ehtQ== 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: 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 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