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=1796512222; b=bkNwtlRU68948ouZlk1c8RDnERrqf2KCqRWQuhhg/WUFKm87i7iPI8MBkO6mrpZfI70r oJaLYDnFowdaLCxCRB3q0N1qdh4YQxhwbJx4of44WDzq3Qw97x/jWeOl4had7lGUQMFQV i0v23iPlLTySjCC0W3XwrBcZLd7w1AP5M+avAWGNdLrIDEF4mBRz4IbVxv4XsNdb3RRbT B2XuR/YjrVIIfSmi3dZsXy6xPzar9I86U4oMCdIWMK9gUO9Ddh+g8pjsNUmYnL5Sa+4hP hsoK819cXOi3WPsiwfMW544d0HASTJIhfNGlBYpPTxFVtBMswM+OP1KaJJKIuYpbIJQ== 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=RzqErhfpc225VPiB7c5GYR2spRZNE34QBw4gm1Kj1iaVsVQqh3KUpzofsaxLpXuzv4BQ cpZ6u0IsIc70/qXUgm6rR7VNaAc5Mg15YZdi5kQaBT0HkKmQ0JnNrEmNlCbgsgTqTwI5d qXKwNdD16kMkrPBBkuwa602mv1ufWx2CX2lj+N+d0IPjUd3qlxmIbKLGZufO6RxXnBkvn b5hmzixN/il+WRj5xKHwUiU94Cl518hwfheRRWGWjMQ69YD+q0ay5LOVXHbWQiqOhgNDp 5MC54qQ9Vef24/dsq57n54dLeKvkHZ+874XewR1Om1CSJ/tR6xn//1kTTERKlW1OfMg== 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.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 4D532606A335 for ; Sat, 5 Dec 2026 23:10:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 4D532606A335 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-f54.google.com with SMTP id 46e09a7af769-86288992de5so1769762a34.1 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=i2bjEufX7EjUNa6GKRhTm7fEanoUy1v7Av8y4xUmXXyA4BTBa+/WlYy5GVPfvanUha aFXHnaHrKgeqjV64FOeLedCp2MIHsJQAfOrGlEskAL49DVjo/oqg6RF0wjehi4Kq3BNl SBW1AInPbC96U4V6hvjvQwkNuVbrP1myLxS5fDCZvX3s00Qc/dX3UO2u+GQ483o7ygT6 yQvTKLVIYEZF45t7v5tsUZ770lDTaxQCbUP1vHNSfp2CwkDpaHepjK/FDtzZvNHa7Zvj PwwQbyIrS+hmGoWpOUGi6nmYCgv8ZYf/xEl8dLmaba5UrXO7k3LvufErXU0F5ZF6HrCb 6Q+Q== X-Gm-Message-State: APuU4Z9w+s/ADutyKEV6PAdKcws8t7ZDM8ICocPa3VAKSQsZ6RhOiZAU PENPHsdfk3O/cuB4CgpuFbG4HwdTqKS5sC1ZCqwRcr7KJQrasld+YnmBjCdvzn3Xou+u+TeJWcZ BG3ffuoo= X-Gm-Gg: AZFdTZfk4RwuRPrlgCDtHiFb2VO7gtdELEoNmFFB/sGsTkiRpBfYdmQaM+RhvHUecbz 2uln6GTn0NH5yEVnTuCrGEURY+jrC67Zrfflm8vZY64VVz0Jy9luSNX1I6DOLwSJDgUheIC9ZnK lSlk77Nn5XyeFQmDOxF+CGmWQRWSSvUCemiql/nLT7aUcKoQh6bREgd+YIBZnMf9om6LcV7F/QA tMe5+CM6+hTAa5NE5EbG60t7o8bUCQiKfHom1wCgOGzA0Jm3zIReOPSkSKH80ZW2QJfiaO8L70u U/cPjK33mqnMk1gyzn3IBJzhOGvQoNqwOwFQyu3NV2HwBWCDmOAxTE7JzVsA3J7ZPV0LjORaOfL L+IWU4hBEil2iDXHrpd2iECDkx3f99UzvmuxZEcKgXkXcfziPIYe6Ho2hW0NkIEHpRL+2etFLPy tAvtqm6YLAkjDxSeJzmLEgrGSgqHf9V7ZDiYnaEys+mYDsm1Y8Y5eOi47ELGwU6n4vSBl6OXvDo lia3FB9aR+CReZO2D/ixnukQxf8jVHZwPZTdwJSgurxzz0msdn3J7iumy/Habz4koSHpgpqy+Ke SJ1jLbz4KOJoGa49ImMjcWq1c5mTWz7cFeYkPzEYucpOdWeoP+oDkXVdWxVJOBzjkCzMzEQzaHo 6yd8flVUJZ6YvEYunxJOwSao9fD04St40wDiR 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: 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.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