public inbox for cgroups@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cgroup/dmem: Don't clobber pool in dmem_cgroup_calculate_protection
@ 2025-01-14 15:39 Friedrich Vock
  2025-01-14 15:58 ` Michal Koutný
  2025-01-27 15:27 ` [PATCH v2] cgroup/dmem: Don't open-code css_for_each_descendant_pre Friedrich Vock
  0 siblings, 2 replies; 14+ messages in thread
From: Friedrich Vock @ 2025-01-14 15:39 UTC (permalink / raw)
  To: Tejun Heo, Johannes Weiner, Michal Koutný, Simona Vetter,
	David Airlie
  Cc: Maarten Lankhorst, Maxime Ripard, dri-devel, cgroups

If the current css doesn't contain any pool that is a descendant of
the "pool" (i.e. when found_descendant == false), then "pool" will
point to some unrelated pool. If the current css has a child, we'll
overwrite parent_pool with this unrelated pool on the next iteration.

Fix this by overwriting "pool" only if it actually is a descendant of
parent_pool, and setting it to NULL otherwise. Also, skip traversing
subtrees if pool == NULL to avoid overwriting parent_pool (and because
it's pointless).

Fixes: b168ed458 ("kernel/cgroup: Add "dmem" memory accounting cgroup")
Signed-off-by: Friedrich Vock <friedrich.vock@gmx.de>
---
 kernel/cgroup/dmem.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/kernel/cgroup/dmem.c b/kernel/cgroup/dmem.c
index 52736ef0ccf25..10d37df5d50f6 100644
--- a/kernel/cgroup/dmem.c
+++ b/kernel/cgroup/dmem.c
@@ -222,8 +222,7 @@ dmem_cgroup_calculate_protection(struct dmem_cgroup_pool_state *limit_pool,
 	struct page_counter *climit;
 	struct cgroup_subsys_state *css, *next_css;
 	struct dmemcg_state *dmemcg_iter;
-	struct dmem_cgroup_pool_state *pool, *parent_pool;
-	bool found_descendant;
+	struct dmem_cgroup_pool_state *pool, *candidate_pool, *parent_pool;

 	climit = &limit_pool->cnt;

@@ -241,7 +240,13 @@ dmem_cgroup_calculate_protection(struct dmem_cgroup_pool_state *limit_pool,
 	 */
 	while (pool != test_pool) {
 		next_css = css_next_child(NULL, css);
-		if (next_css) {
+		/*
+		 * pool is NULL when the current css does not contain a
+		 * pool of the type we're interested in. In that case, it's
+		 * impossible that any child css contains a relevant pool, so
+		 * skip the subtree entirely and move on to the next sibling.
+		 */
+		if (next_css && pool) {
 			parent_pool = pool;
 		} else {
 			while (css != &limit_pool->cs->css) {
@@ -260,16 +265,16 @@ dmem_cgroup_calculate_protection(struct dmem_cgroup_pool_state *limit_pool,
 		}
 		css = next_css;

-		found_descendant = false;
 		dmemcg_iter = container_of(css, struct dmemcg_state, css);

-		list_for_each_entry_rcu(pool, &dmemcg_iter->pools, css_node) {
-			if (pool_parent(pool) == parent_pool) {
-				found_descendant = true;
+		pool = NULL;
+		list_for_each_entry_rcu(candidate_pool, &dmemcg_iter->pools, css_node) {
+			if (pool_parent(candidate_pool) == parent_pool) {
+				pool = candidate_pool;
 				break;
 			}
 		}
-		if (!found_descendant)
+		if (!pool)
 			continue;

 		page_counter_calculate_protection(
--
2.48.0


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2025-02-19 13:27 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-14 15:39 [PATCH] cgroup/dmem: Don't clobber pool in dmem_cgroup_calculate_protection Friedrich Vock
2025-01-14 15:58 ` Michal Koutný
2025-01-16  8:20   ` Friedrich Vock
2025-01-17 17:29     ` Michal Koutný
2025-01-17 19:02       ` Friedrich Vock
2025-01-24  9:56         ` Michal Koutný
2025-01-26 16:38           ` Friedrich Vock
2025-01-27 15:27 ` [PATCH v2] cgroup/dmem: Don't open-code css_for_each_descendant_pre Friedrich Vock
2025-02-12  8:40   ` Maarten Lankhorst
2025-02-18 14:55   ` Maarten Lankhorst
2025-02-18 18:39     ` Tejun Heo
2025-02-19  8:53       ` Maarten Lankhorst
2025-02-19  9:08       ` Maxime Ripard
2025-02-19 13:26         ` Simona Vetter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox