From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512396; b=Ei/kHPOqSTN5qXdmK+KEtCYwks4M86mxSC/l4i8/4vJn1KvETh4T32mkQViplOentvPq PPofue99ZyFUlWPQ83vjSW0Bs9O7Z6DJSrOGZH5Yz4ntUDCJZColE41u4ZXlCvL8MJocf q8VNW0vbe9mIqvBB0XXqhqPqDlZ2c8mfF7h39jH3bpXFBt0m6H1lC5zSOCUVq+az2YLY6 ZEXkOOwEDUsIg6RwGBVkAUAur38HJhQOQnB0kFaPQ3QdqeppxrG4R056xnuidtLgaD5Ef YLwlhs4eni6Xk61GnRlV8EjShdLZGy7QS0N2uT/NYTyvioG2v+Nsvt/VqFSfrDi36XA== 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=bIjWio4GsaMaowX9rKh2iOh5Mr7kT+6/DZR/4rEu5ap01lKzyhnUufUgtYNOxQHbNw0b w0B3c8NSlv6XV4x7juzzGBCNKBZ59VAYGZu+KsQjy0rIPhZhXkk6JoACUnr0vlPy6kkZ8 U0CEevtMJWh071NiuMXABWtIhEa6OV6+sqQiaKcnihDImMa4mhBhMjKlduaM4dWdL0GYw NtlxANShEDNz/k/drsdEXLtQ2bX5eGx4jUDeFzzVqA8vBSG56A6eCUnYX53GobrE4GEZH /L3O1OZogdyRkQ8WqK3VtO4k9KSYiztLZtNmRgl6/VkEaKntWsKiXQQCLTXwIeURQDw== 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.54 Received: from mail-ot1-f54.google.com (mail-ot1-f54.google.com [209.85.210.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 5D42C606A32F for ; Sat, 5 Dec 2026 23:13:16 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5D42C606A32F 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-f54.google.com with SMTP id 46e09a7af769-8470d7d430fso1089567a34.3 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=soliZDdHGusfZvS1Xwd1qJO4rpwj+Nx8wJ2TJQ5xhriFkHDaQcjYbbHk0kUcILTQmj FylPwoYC1kcxLY2P4Hrx+VKSpAVINU4m5E4Zou/MSWqWZwICAkc8c+StR7vWXrU1skDr inYszovnIr3tSqDExiUgTCp3w25MeD3tDGHciGO66R5WecQzURzV4Au+TNNk5SSH04Qk ugctQKf2tSy0KDnUaGRsePghvOdNZBtJYdJLPgoF1ric3DABzQPJcXKK/o6vAeDW7xqy VeasBcIfUMvlU+3YNiYRazWC7a6wCl40ZOOMFeUPVDOMbELCCSZfUUloGpEWRwkC6rLR 9MZw== X-Gm-Message-State: APuU4Z8NzxVQ5EtjkMwklAL22Kr/nE5GbhfK/0SITunb9mLlgMt+8yr4 4JENYJ90kfycVnNKzoeyZnyL3alByQWjt2bPd4iLPAd+aGX6OSD7ejgY+mDigJqZE7oi1flsrwh hAll9DgY= X-Gm-Gg: AZFdTZeqyIrFoxDm20V6Fbz5+VhsL+jQ1jwz5zUzYBoiK0LFowQUbqT6341VLny/GHD BXOar7dvyxnwAlY8Ap0j8o2hzaT9KK8BwkznFQXiZf6uoq7/NiPI2hTu5i9X4SJgfrEBnvyXtlE 2uGz+THYQTQuf7++9WzM2P90duo/E+ioYt4UE5vEoUtQxxm1INsf7UH1HljhgNq0roKvqSmZfCA 6R+e2ebMk4A5tu920B3n2l0DNXDpxvt8I29viBYfSq56QqQigjrVcUVBfdAGxYH6KwxT7nVk9jP YoIkng5PdC1lxQJXi1PQa+QZRMp2yWKqNlL009yjrg5WjzGLZIpSceBESfAxsTQECOUxO3f1HwJ H8H/Ol7QL5CN5KBD0OKqg8MNP5YzQEWaYGfk34Oh3rR3hdYdHLklU1p+dgqKKifFUEARcLHCOGm LCXpNq6+tOWGi7xBRMSVVo/ZRExE+OodfpJ1uRrNOelUu/PsKz1yL4HEaYGu//qOcMh7FhHcWaN 2XabZBFpKQZrHe9kMbyRPe9AkKyaIvdNGE33ub4hehmIxfR6DUNgUP2kyC3Z/OT+VUge91uUb3h ZGvB21ByhjaGIN8TRe3PdX4JbLEUpESpWN4Tmy3MvwHsdkYzyDSPlolEiR9uuZFdJVoxJzP+YTv Sn5YL4K8S9ipY4WQ0DuCT0Auy2HqadfbvGXjQ 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: 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 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