From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512222; b=10S7KPk7kb3enUxg9zsveSGwDLEhlY1NII+CzyB8EemjDUa7BGpmpvRTdg0qiSFxeKrZ sC6YqOoAorPARD3sznJhSCcCPJGoC0to5HAmeWvcUG3Rr0SLMvGfiw1KlokabiTst4uF8 rE0DeAZ+nXJGAKqh4frufixCdJxc6jgXAkzhWBlBpYRu/mWThbl54qoaVDIUGv35b+Oto GH5C/dDuZt5iMu+qvxsXIY7jfaeTy9xs1V/MCQ6ZB5c4kaJWC1vJXH7vToE/pDi74MLYh 9mpRW9032UzDaYhfrriiiBvYyoZpCDYNrP7Kf1JsFfOM0TgoadQ9bL0UAX7uKy8RwMg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796512222; 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=TJwuGerzNw4+NarwYx5JaqceK0u1dLd6G9ryIHPMesM=; b=te2B/4NWOsB+iB2z/yCyryvHasJRj77SWLaFZ6brDj/0uew7yu2X8as8Vw47VV7v31tL 2bEsrQJNhwEl66e3aFQxwlWIjHBDBw/LZRg7gdfxW1/4c8+saD6unCdouIvOZN3uBA8qR YhyrikzXa6GHb0n9RUZZOFWvcO2EKsG5AHpKf9IpjAvYkRnbXCYwDSSX4LzpA0MVFHbAw mCoQ+uFvOsCzhJrQlA+ag+c6YM+cwYstXV5qalCHb2Q2ZVg9DKdjs5W+4cXiUBmyUhL+R L1BatEdWZ17568vUKZ0PupYyxoJSvUVBqLpAWQGmznBA/2Zp7kEjrn2vmVGDOrIsS8g== 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=r5DQ8p54; arc=none smtp.remote-ip=209.85.210.53 Received: from mail-ot1-f53.google.com (mail-ot1-f53.google.com [209.85.210.53]) (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 4C864606A32F for ; Sat, 5 Dec 2026 23:10:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4C864606A32F 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=r5DQ8p54 Received: by mail-ot1-f53.google.com with SMTP id 46e09a7af769-86408192288so531659a34.0 for ; Sat, 05 Dec 2026 15:10:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.dk; s=google; t=1796512221; x=1797117021; 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=TJwuGerzNw4+NarwYx5JaqceK0u1dLd6G9ryIHPMesM=; b=r5DQ8p549ri4s3pdlTePCxJsXpyDWECIA1S7H0Q9skxbAfY7jJFOJ1g8uexSVlD0vr GRHXjqbo8WhiYyDxFCu2DAtHfiS77xAk+haQdDC2mMgmrn62Xp0bIAo3nc94PKhsAo9s F+JyGnSEQxCH1FtkcCj/PX09SUUTnmhISgrlL2Miw68N5mwEarARVQcWqm0zXpCmJb2c baeOlRoX/y/4F1pZU8cQ9t9W6HZnG14HRxtabnVptku875bjQ/UjlKZi4Z8gHRHK0qQX ioQwHn5k4dzIWYP4TywX7iKcbISvW/sVqhppTGYT8t4RtA06XZ6d9Hq46uw6jEWBdSXH vA1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796512221; x=1797117021; 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=TJwuGerzNw4+NarwYx5JaqceK0u1dLd6G9ryIHPMesM=; b=06E8A+BitAW4feIYz2NwF8GTlUBO+eBWesu3S97exg50GUbZBEiPNr6HKl3PfROnMS rbsjkLK0k42D86240i+4W2448CrntwjsGwI1P1C5tbvV4fADx2/KG9uSsR3AntYKuAIL NVOj8R/Y71nQNyW4WwkT+1ei+iSwDXc8ggt57U1tPvkBY22nMInFv+qxi8oa8RuHaceY 26p1SPW/OFgfc69Qirzzrg8nKQnxQBc2ytE+DYmPikDGGOQADHtA7oQer3aqC4yOMgMJ kUSgJZ/2uQVEzcEBR/3lER9y5zXIX0Q1zUxGxu80fzYFSfjeuTB3qI2T2yQwvfKVZAMI fkVQ== X-Gm-Message-State: APuU4Z/JNfbY41JyCuP9ZOZ8XYvDT7dcteW289SEt05lvfRH2NQUOnpM fuYyxhQhQ89nNNXyU2uv/fMQJ6EWeKbVCGVqNBb1sZdmme0WkjSgedrZuvznE7nS/JnDh2OLyTY JJTnFp4k= X-Gm-Gg: AZFdTZdjbzQA7pV1TtQK5iimwoJU9pNZc8zkgHIryfSTbEPG9nUCE5/JwvI115j6+ZB EatHL+TktQ63LvDnPVXusKmvdSqPmOHwXBszWix0cj+uNNtuTPiy2eYjBa+xmli/lHFL1va92YN LP5zvnhRi7ZLA59USskupMDa7o8J1HmUMmrjKK3sGM22X35HNYow82YuojumyniHlO+hZa/3+L5 CXGUAiSaP4XZrX3/ut+GBsI7EKdxCFqqnoyhdLSLKwBa8kHf9gZpqC0ycnXK319675vd6+ohVu0 391FxwV9d89LLsac8pneJk0eeXKQ5w7Lsd2n5OzK9r2icmOk/D1lQwhco9g725ibAOMZvzprbGO 55c9YYbDMGT/7sZ3UBpQ5LRPNhax10CqnqKZbu7khluLVFSJcIUZNyKo7cbvO0Ne4xhlbuFmhh5 FBqgxVn7r/aP+Pqcy/hl1X5wx/2itHQBtPWcikuT+4xKu380b0Jy2jpHhcrHMBzpn+GQtCyE0TJ gMFcMy1fc8K41taQrAT7LQTYtNWc1dYfe/0OhYQP0Sc++HhOtUFr7939/V2Fz8CaHBSPRuc2oif vxZ4JJOFlk7DMP4WHRnb+viuEFK4hoIplEVx9BTZ8eH1IFz1ZullU5/okx2OoFj6nBqe5HRH1Sr zlotaWP/2RN5TA2xjnXwkJaUMiXRo88LRtGz0 X-Received: by 2002:a05:6830:3113:b0:862:8871:5bd3 with SMTP id 46e09a7af769-863ec26f6f6mr8796289a34.37.1796512221217; Sat, 05 Dec 2026 15:10:21 -0800 (PST) Received: from r7625 ([96.43.243.2]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-863f8313082sm6309624a34.16.2026.12.05.15.10.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 15:10:20 -0800 (PST) From: Jens Axboe To: stable@vger.kernel.org Cc: io-uring@vger.kernel.org, gregkh@linuxfoundation.org Subject: [PATCH 6.1.y 3/9] io_uring: free the task context after a grace period Date: Sat, 5 Dec 2026 23:10:06 +0000 Message-ID: <20261205231012.3977993-4-axboe@kernel.dk> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261205231012.3977993-1-axboe@kernel.dk> References: <20261205231012.3977993-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 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.1 drops the count in __io_put_task() and has no guard(), use rcu_read_lock() and rcu_read_unlock() there] --- 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 13def71c956f..93d9e8130b5a 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -62,6 +62,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 1434e3a3d658..75e92f581f61 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -692,10 +692,12 @@ void __io_put_task(struct task_struct *task, int nr) { struct io_uring_task *tctx = task->io_uring; + rcu_read_lock(); percpu_counter_sub(&tctx->inflight, nr); if (unlikely(atomic_read(&tctx->in_idle))) wake_up(&tctx->wait); put_task_struct_many(task, nr); + rcu_read_unlock(); } void io_task_refs_refill(struct io_uring_task *tctx) diff --git a/io_uring/tctx.c b/io_uring/tctx.c index eb4f4ec2f7a3..daba0a2c4ec2 100644 --- a/io_uring/tctx.c +++ b/io_uring/tctx.c @@ -65,7 +65,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