From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.45 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512321; b=FgwL2HqfEOXnqMs7uJzgtK0Qxl5rn6O+n6lEBTDaL0ZBZTq4r001uauS7cBWRetLOp5T 1qmKMp4K4YNvPpHx9YbgICGPpqRU1c/2Aczqv+1mgNGcPPVvYereVyaZXgDXnBfqp384l r76ivWwA4yngHZx+U2F9StGsza70czQfIs862BsQX80w0hodAHhogvMn0ncb9xpApcW6h pWzYHwRgFTFsmYjv8rBup6yZEL1+2ObXXElUVPLxZ8UZZXK9lMdlNDJc7s1goSoh/ph/5 4w0P5Tyjxs5DB50yOvvRTiG5BDpO8irdN/dmyPBUTxMAJoc7u/qFeaOVPPglVcWH57w== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512321; 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=g1SitgzdYhJssDRa4su3I5iZThJzjTsqW6Zj+dWtQi4=; b=1ATnzgiQT04DXgOYBlrlR+AamWvvUBgop2aHir1nDntHHuO0ZyknkVcFCJMXULh6CLC8 EDq+iz+nVkGFZ2zFFPBRGVRCbd50YjD7Pgt3m7GPXqXm4ObemeuRK5qt6m+v7HCctx2Jq 34NvkCTmczEPDXpnejKp+5XuMapOTvzeDkcL7e4Sr9w9v1bsUGOYfl0BZx0ySN46Tuwbs Lg9bs898FL7d9Z3L23K9haIhsyJbcN/VaesCOgmjgHzLufzQiGlalTiqkrI6vE8b0RhBL D4GWy5d88iIDdvcNfbsJJhYTmTlSaFgU6k8tQgg1xCAsqzDkQloH2uG4p+c7lLTEOLg== 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=Npulqv9z; arc=none smtp.remote-ip=209.85.210.45 Received: from mail-ot1-f45.google.com (mail-ot1-f45.google.com [209.85.210.45]) (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 CCD35606A31F for ; Sat, 5 Dec 2026 23:12:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org CCD35606A31F 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=Npulqv9z Received: by mail-ot1-f45.google.com with SMTP id 46e09a7af769-862bb0a189aso848506a34.1 for ; Sat, 05 Dec 2026 15:12:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512320; x=1797117120; 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=g1SitgzdYhJssDRa4su3I5iZThJzjTsqW6Zj+dWtQi4=; b=Npulqv9zBdkYJ+X8bj+QIaMfgm5Zd6mxVVsNGhMXpO2RqW28QDI2IpS0/OPcheUh5l 3Lv+8qFrW7DB2BYEo4ueSARF1eNA0vWDZ1yQJ8j8q29vFhp7hY/HFBZITz4GSP8ICIB+ HVsRFaBC2xuQqasF+47m37OTLMET+2OTgz39PrA1zH8KB5d3/+QFo1krACRAir0EslRF lZqdtFb5NqMSoVRHEi1B8Oe0Dqbo4osCr3AQujV0Bzq4Mj5xRtVJGGw9anZks12GRyP8 WscSNQOioqD3nl8q4h58zVWblX/odKT3haBTSykF5pjKNb2LzbS/VH86jSGHqkSQlz/m /Rug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512320; x=1797117120; 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=g1SitgzdYhJssDRa4su3I5iZThJzjTsqW6Zj+dWtQi4=; b=lXx9loXIR3S60vZK0GJp8NRjRNCIVePRILltWWjfFkw+zgvVTiy0YukZyfeQFW0PE6 EUm6kfnnZLHrZa4kNtIlv0c42Nor/K4xWQ7ASVeU5qFg7OzW6guIiImzTyDqTbGiaLve 0cGGcJmfZQSldeH4b6NjJqbXEEatF96U3IzgsQol2u2BAebMFlwtCB6g6om75rnLegQi te8Icp2t5xdrm1nPJ/4C4/pfO1j5ir6+t+kitrFJpe3/yPxk75H+Lrl7JwrL9BEJjAN8 0HeDCzEP/zW9AUPDgDLcso5cBztbNRfmRwHHgLcXp9aqLR3hWCNBV9SR5C0L+pjetxN5 /00g== X-Gm-Message-State: APuU4Z/LPam0nySbJjpruAmQ+FjYPCQKDoJrf03j98Afc7u9scmW0zeq 1bwUu74Wzo4NFZSLeolRvCj93gIBMH5V7r9e/6PXQQZUjY+m6HF4o/67FVUu0Yx7tFgGg2tt5du WNQiIH9M= X-Gm-Gg: AZFdTZd70zv3D2PN3n7ayYRMTeUjXfR4bT5vxAMFpIVisvQ4hSQCyetKNgcpo7ricFM 70bZ61hx0BU4sOjuVTe23iwuJtJV5qbNB9xHfnAGwDQx2BK747e8b6WawdjfJvWPgZvFMErgSV7 b9NHFmpfIQUMPzitn2SH2gL8bMxnTlIf5j/V6eYTpk+Eg6YGVwaIuHbyUZc+CF0QnAt+NLqyRlb zJYlSDD1DR7pzGXXbZWDRGNqp6B706M4vk2x3qm9gKTKzZoI6rHGo8jaTsSDXTSgR/9uNbHt858 oIJNEiEQIj/hi7d6vOMhGAbHlOJQ0EAuaV9LANS+M+FhkrdzDP71CtTyCWoSKg6qkXcJnPvm6qt /dGUgJEatnMwrLnZknoONIx/YuOR/zDN5LeBlOuMaLLWog3M/aCccPCN4yU4D4dBuWd/Exd75R+ /+VOMNS8R1gYdelfdzFepG6GXtzJBTItixl+Svo6kj9jPuiJFDe2U+C3f6ya3z4ewVVAd1yWQHc mdR3gdhtkSSHapVolmmcUV4AHs+H7ptsnBsBUrtlaVN8jDA9KfxYkHDedDIViy0LOaneMhansAb MphyVijrohUWgLc5HEUzshJOEFwxD6KjH/nLodrffrDTB25PiP6g8gk8SzW5GKCa2O2P7RClUqT O8D1ZG4jNvWugjW1Hu5TYyNitZGZ8kCmrccHE X-Received: by 2002:a05:6808:309d:b0:4d6:90f3:74b4 with SMTP id 5614622812f47-57f2080a85bmr5566634b6e.52.1796512319888; Sat, 05 Dec 2026 15:11:59 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57f1f6fe2ecsm4086916b6e.14.2026.12.05.15.11.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:11:59 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.18.y 05/14] io_uring: free the task context after a grace period Date: Sat, 5 Dec 2026 23:11:38 +0000 Message-ID: <20261205231147.4095031-6-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 9f5315d3f4368590c9fe06d4313b7247470bcf15 upstream. A task other than the owner can free a request, io_ring_exit_work() running the local task_work of a DEFER_TASKRUN ring for example, or io_do_iopoll() reaping completions. io_put_task() then takes the else branch: percpu_counter_sub(&tctx->inflight, 1); if (unlikely(atomic_read(&tctx->in_cancel))) wake_up(&tctx->wait); put_task_struct(tctx->task); The first line is what lets the owner leave io_uring_cancel_generic(), and on exec it frees the tctx right after. Nothing holds that off for the remaining three lines, the task reference only covers a tctx freed along with the task_struct. It used to work out because io_uring_del_tctx_node() took the uring_lock that the freeing side holds across io_put_task(), until that moved to tctx_lock. Free the tctx with kfree_rcu(), and enter an RCU read section in io_put_task() before the count is dropped. Fixes: 5623eb1ed035 ("io_uring/tctx: add separate lock for list of tctx's in ctx") Signed-off-by: Chris Mason Signed-off-by: Jens Axboe [axboe: 6.18 has no io_uring_free_tctx(), the tctx is freed in __io_uring_free()] --- include/linux/io_uring_types.h | 3 +++ io_uring/io_uring.c | 2 ++ io_uring/tctx.c | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 97f836654425..bdb0dc928c6e 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -130,6 +130,9 @@ struct io_uring_task { atomic_t inflight_tracked; struct percpu_counter inflight; + /* the tctx is freed after a grace period, see io_put_task() */ + struct rcu_head rcu; + struct { /* task_work */ struct llist_head task_list; struct callback_head task_work; diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index 75dc5659a8da..fb4528bc3aac 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -724,6 +724,8 @@ static inline void io_put_task(struct io_kiocb *req) if (likely(tctx->task == current)) { tctx->cached_refs++; } else { + guard(rcu)(); + percpu_counter_sub(&tctx->inflight, 1); if (unlikely(atomic_read(&tctx->in_cancel))) wake_up(&tctx->wait); diff --git a/io_uring/tctx.c b/io_uring/tctx.c index b4a44675093e..c7b08c9a1657 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -62,7 +62,7 @@ void __io_uring_free(struct task_struct *tsk) WARN_ON_ONCE(tctx->cached_refs); percpu_counter_destroy(&tctx->inflight); - kfree(tctx); + kfree_rcu(tctx, rcu); tsk->io_uring = NULL; } -- 2.55.0