From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BF3641DEF1; Wed, 15 Jul 2026 13:24:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784121883; cv=none; b=D6UQ3J1FV/m0/8omp2Bpprk9TvlpwSnCboJ7thuIrEgekWeqenLatIJMbOpYFylHGWBtlDlsdj4M6OUyKR35pwBPvlRznCd05F60zeKuwygbvdPUIKWV9Wf027hIXwlkFptFHJrkO+G1N9twBI//uS068U8mo+UhNOQm0TJ+xNk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784121883; c=relaxed/simple; bh=oNvvz7J2Hu5fu36ODaZxmH1lq4ffgOu6G6kz95Tperw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version:Content-Type; b=CqQXR5luFV2gN9/dzP8GMmmj7nhBPI0ZBX2Xway+KJS0UAIq1daRrfFJTCRjDM1GDhPmzFrVhij8HpelrgPVwF3ICTFEOqMaiGkfRPyvnJkgGeG0rtUCBOiW3Ysa7zBB6kz8mb5nBNBc6zojNIPh3EaD5Yn05Afl3wW10HocWBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=SF9I7/lZ; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="SF9I7/lZ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1784121879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=VVC8BP6HSrZ601d9eEJEZtY6cb1rMz2d7T5MCvA0R3k=; b=SF9I7/lZMX+u/QqdYTqudSyhNKcIiUMpMxiK0v4P92tLTHBZr0JAVBuZVQgLUBugXdndUb NRjdMTi7nv/nXXSk25IlyokymrrBT4e80pYGjh6LilWP6/pOQzSyrLjh8wKm3OOH8fbg5c am1Dxrsi3P3HEU4b6FHH0StNZIDB544= From: Tao Cui To: tj@kernel.org, axboe@kernel.dk, josef@toxicpanda.com Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, cui.tao@linux.dev, Tao Cui Subject: [PATCH v4] blk-cgroup: fix leaks and online flag on radix_tree_insert failure Date: Wed, 15 Jul 2026 21:24:07 +0800 Message-ID: <20260715132407.1469777-1-cui.tao@linux.dev> Precedence: bulk X-Mailing-List: cgroups@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT From: Tao Cui When radix_tree_insert() fails in blkg_create(), the error path has two issues: 1. blkg->online is set to true unconditionally, even when the blkg was never fully inserted. Move the assignment inside the success block. 2. The error path calls blkg_put() without first calling percpu_ref_kill(). Because the refcount is still in percpu mode, percpu_ref_put() only does this_cpu_sub() without checking for zero, so blkg_release() is never triggered. This permanently leaks the blkg memory, its percpu iostat, policy data, the parent blkg reference, and the cgroup css reference — the latter preventing the cgroup from ever being destroyed. Fix by replacing blkg_put() with percpu_ref_kill(), matching the pattern used in blkg_destroy(). Acked-by: Tejun Heo Signed-off-by: Tao Cui --- v4: - Resend. No functional change. Folded in Acked-by from Tejun Heo and rebased onto current mainline (context-only: the err_put_css label was renamed to err_free_blkg upstream). v3: - Remove the redundant blkg_put() after percpu_ref_kill() to avoid a double-put that causes the refcount to go negative and bypass blkg_release(), as pointed out by the sashiko AI review. v3: https://lore.kernel.org/all/20260507061229.57466-1-cuitao@kylinos.cn/ v2: - Also fix the percpu_ref leak on the radix_tree_insert() error path by adding percpu_ref_kill() before blkg_put(), as pointed out by the sashiko AI review. v1: https://lore.kernel.org/all/20260506131124.16755-1-cuitao@kylinos.cn/ --- block/blk-cgroup.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index d2a1f5903f24..d9676126c5b5 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -434,15 +434,15 @@ static struct blkcg_gq *blkg_create(struct blkcg *blkcg, struct gendisk *disk, blkg->pd[i]->online = true; } } + blkg->online = true; } - blkg->online = true; spin_unlock(&blkcg->lock); if (!ret) return blkg; /* @blkg failed fully initialized, use the usual release path */ - blkg_put(blkg); + percpu_ref_kill(&blkg->refcnt); return ERR_PTR(ret); err_free_blkg: -- 2.43.0