From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796512321; b=q0lTykuDkJwrOMaVccYipK2N2/BfRZBj2/ZaMRT2BUgq2Jxb+mUkClx2ze/Yf0OWjAZe qaM72Zc4HtZnETsXNmr8AgVTNu/E+flH31Ht+ZpmVZOV93p0z/PdY0P48SYeLTPG47bOE XSONnOgtuVkEGbPrDhFE/p8c/wvKxbVfzf9wwJhI3yVWPWgipnirrCttj7aGaHwflqOal 5dhGLaFLiKUsYT7aQ8v3S9D5n0w//cjU7cWzSM50AdKP8Xr7ookPM141s7t9EGBOhAaiX iD73wA2v5K2kKlM9GZhmkSyn04OwFTbuwvra5rmdOSO8h4W9nuAkZowBS0ZMvotiRBg== 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=uygLLRrfKE7idGVTM4IxVpChIRu/N1tXaV20e3OHlWGOKRQLijigQxwt5uECOUF2asFe iy7md1nTWok3PiGOf7lf8QfGdckSJojN9xPFGa6cisfrRDYzARxlgWZRxlaW52X8qQ45Y 1zEa2X2HGsG+rlhEVjTgG+GLTrT5dcEVWRBgH47AcYLkE4RQjNH3cAq+FuvhvCpuXbbXz IqudvlAaiy11Z7OAhpR+u6FpWX0Vb+fjbZ0w0DGwH1hKkpB1O2Qkz9vVcvYw2qg+mVZ9f k/zKIMaL7Mq9jLvhICAITM+V62wq/Fs5mlav5z0dD/caVTmwOvn55BpZmZWWX4vUdMA== 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.167.174 Received: from mail-oi1-f174.google.com (mail-oi1-f174.google.com [209.85.167.174]) (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 EF000606A32F for ; Sat, 5 Dec 2026 23:12:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF000606A32F 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-oi1-f174.google.com with SMTP id 5614622812f47-57e4bd7fb1eso770830b6e.0 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=fJXTh3OeBeFHwYdeI1S5SubIzlz7/yhnZv6t+rCl+om/zZc0xrobv4IVCDaY+m+TMU 7HsdcQxJLvSREsTjoT9VMavBw0KzukTQmF91+fHfF3Bc0c96aUX660KnsZYRbTLMniRG JsnAGrpV/gMKzM67ysNhauvdEEP3vx783mIxyugF32qPWZ858H6bMzGf0Co9usbBdDUr tM4QO4nhzg9RIAjS/iQ1ivd0BMmqyGsMv91Ajp56EcutAU5XuP79TutSW6eSEN3Nh9O4 zAX3vnBW1bLBpfsveMbivQ6epCjzbHJoieVXK08fDHgT5zKr0YAj/XjJwEesKRS4Y0E9 NGew== X-Gm-Message-State: APuU4Z/R22u3V8rcdDdqq3mJSI7WX26omrAbpkMq625hxuwHNQ8Rmiah NMzgqEjH8i392JZm9j6kEZQGjgYX9AoWsKWJn9xUZp8+3tooitX47MEF+ABp09yox50yWMkQF+i cYCUAMkk= X-Gm-Gg: AZFdTZet+Bf+eyYuq5aip79XSIqe4WjNo7z3duclqWPOAd11zkeUbdK7rV556mZohZ6 qC+xyTP0bbhgJXO+TVA/O7bHEYR3Qnh5o0jFeM2msqREyh7J6KtwFPKmNKM9PyvOnV6hobt72kD hi7Cdbl2d4J2++AKfclAPm4YcqVCQ9YAubZVvb6gx8GT95njwERdE1TpJHfgp0ikFbTd1dO9Fr1 jVWDpmuCr/AbLDSs3iJTRi/5+6vJmCAeHiQTzxkOpEHhQu0uZ9Xkgi2lUbd+TP+xqdHr31Blgn6 d3D8IKoOu+xDakZQEaIXUVx5ZTiVnwT0vK974RLy31AC/lgR4vhYBD/05bbYTpQ8oPlyOJNEPny qLcNd+FVVgFWwGVJzrEdB6Y0zEAEBWKilmAbKMASepwM+CZ45gOcmkM46KEkZMUEcu5OhGL6jVa u5U+CpvZ4tHIBIPV3KTMpkAJWMYrC4VqX6N+2Vyjsfrf52wUrKJFgu9SLaHOf1T86VvWZibBNUB dWZ2jBsetesiFRc2/MjRmenVgDlzuk5ZwcmyW4zd2LbG772gUqKyVCNm7HuYjdSWYdVROFO4j0A diWc94mqjs85M+c6GNqpJW+Lg9noNcNPOMXEkbV7VjTG6Scr4VPaU0MZp2Yy/4s3pGvFmfsgEJP rJp3ad/gGEIE2J0ebCHCEn++Ve/cyAlW4ca5P 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: 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.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