From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 4B7093491E1 for ; Wed, 13 May 2026 16:33:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778689997; cv=none; b=S6yTBJUeEXQj8pPpAlXvyA2+nyHI3r54LvasiSd0xnEUGv+6ieV39zsreobKSNVazvAprhyGojlzNf0kZluKfYpAnGOwciSXPPriG9zw+f34ihHOttUHlprEqh1GqbK5Y/qeDal9diLrkhAEpfAGsTm0a/QYh25a6+XmHyKj8JY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1778689997; c=relaxed/simple; bh=0kJxebrqY7yM4eqi8zKHfa6rDAJwC7y3A7o0yXl91WA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eUqwok16pK/ST3NxbTaNZ47Nr0JTL+F3mrcq7hb6GqJnARsF36O4A8lpOUrmDIYwSBPk6peJSd7L77eRHLm/uqHc+KQTVZN17OMcegX1XPCxtXdU7YJAcol1/z/DFZ5JRD/+17p+kwi5jupjQeNeKySiMApnqiFH1ESSSHk9Cr4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AFkFkEQY; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AFkFkEQY" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CD9AC19425; Wed, 13 May 2026 16:33:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1778689997; bh=0kJxebrqY7yM4eqi8zKHfa6rDAJwC7y3A7o0yXl91WA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AFkFkEQYlsJ1rYVTrBobSK0VHOuX2Oru0L0L8UMsO9UnJtDGtjpsYcrD5E0TUm8PY dtzeIpCoMTDsQQBnLWDhURXnE+njld6IwmXfuVYEDmEYIxy7iBfU9brec21YsEQ5Vl x4Y79oix0Co7Q2VzQObTi18A0j9WWqHLW0ZsJWJ/v0iuo/TbiBk/K9n7R4ftOOY4Ww gh/Z0YEoHQJFpK1Ju0DdjrhHDj6PWbfvJ/sDS/9ECxsgjM11szXxhYC60ssEfHTC41 VQQpUf4O5zGqbXlVt5yoHuWXMrxek62jctwUMaC4V3VPoFmCHHIrgnK3wmVzVOHkBf 0i48Q2IIXlCLQ== From: Sasha Levin To: stable@vger.kernel.org Cc: Petr Malat , Tejun Heo , Sasha Levin Subject: [PATCH 7.0.y 1/2] cgroup: Increment nr_dying_subsys_* from rmdir context Date: Wed, 13 May 2026 12:33:13 -0400 Message-ID: <20260513163314.3807064-1-sashal@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: <2026051228-thrift-subside-ee8f@gregkh> References: <2026051228-thrift-subside-ee8f@gregkh> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Petr Malat [ Upstream commit 13e786b64bd3fd81c7eb22aa32bf8305c32f2ccf ] Incrementing nr_dying_subsys_* in offline_css(), which is executed by cgroup_offline_wq worker, leads to a race where user can see the value to be 0 if he reads cgroup.stat after calling rmdir and before the worker executes. This makes the user wrongly expect resources released by the removed cgroup to be available for a new assignment. Increment nr_dying_subsys_* from kill_css(), which is called from the cgroup_rmdir() context. Fixes: ab0312526867 ("cgroup: Show # of subsystem CSSes in cgroup.stat") Signed-off-by: Petr Malat Signed-off-by: Tejun Heo Stable-dep-of: 93618edf7538 ("cgroup: Defer css percpu_ref kill on rmdir until cgroup is depopulated") Signed-off-by: Sasha Levin --- kernel/cgroup/cgroup.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 4ca3cb993da29..ef517f0e929af 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5768,16 +5768,6 @@ static void offline_css(struct cgroup_subsys_state *css) RCU_INIT_POINTER(css->cgroup->subsys[ss->id], NULL); wake_up_all(&css->cgroup->offline_waitq); - - css->cgroup->nr_dying_subsys[ss->id]++; - /* - * Parent css and cgroup cannot be freed until after the freeing - * of child css, see css_free_rwork_fn(). - */ - while ((css = css->parent)) { - css->nr_descendants--; - css->cgroup->nr_dying_subsys[ss->id]++; - } } /** @@ -6089,6 +6079,8 @@ static void css_killed_ref_fn(struct percpu_ref *ref) */ static void kill_css(struct cgroup_subsys_state *css) { + struct cgroup_subsys *ss = css->ss; + lockdep_assert_held(&cgroup_mutex); if (css->flags & CSS_DYING) @@ -6125,6 +6117,16 @@ static void kill_css(struct cgroup_subsys_state *css) * css is confirmed to be seen as killed on all CPUs. */ percpu_ref_kill_and_confirm(&css->refcnt, css_killed_ref_fn); + + css->cgroup->nr_dying_subsys[ss->id]++; + /* + * Parent css and cgroup cannot be freed until after the freeing + * of child css, see css_free_rwork_fn(). + */ + while ((css = css->parent)) { + css->nr_descendants--; + css->cgroup->nr_dying_subsys[ss->id]++; + } } /** -- 2.53.0