[Devel] [PATCH vz9] mm: fix mem_cgroup_migrate's warn_on itself

Alexander Atanasov alexander.atanasov at virtuozzo.com
Wed Feb 15 11:57:56 MSK 2023


The original check only expected to have Anon,Swap and Cached pages.
Which is not a valid assumption any more since folios have more
folio types. To fix this update the condition to handle more
folio types and change its logic to be in form of
flag && (any other flag) rather than flag != (!any other flag)
- do this to avoid warn_on if a new flags are added.

Ressurect and rewrite a comment about what does the check do
also  put the explanation why the check is in mem_cgroup_migrate and
not when updating the flags.

https://jira.sw.ru/browse/PSBM-144609
Signed-off-by: Alexander Atanasov <alexander.atanasov at virtuozzo.com>
---
 mm/memcontrol.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 30a9c52f5b7f..0ec77725002e 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -7686,8 +7686,26 @@ void mem_cgroup_migrate(struct folio *old, struct folio *new)
 			page_counter_charge(&memcg->memsw, nr_pages);
 	}
 
-	WARN_ON((!PageAnon(&new->page) && !PageSwapBacked(&new->page)) !=
-		folio_memcg_cache(new));
+	/*
+	 * finist explained the idea behind adding a WARN_ON() here:
+	 * - we do not want to check flags correctness on each flag change
+	 *   because of performance
+	 * - we do want to have a warning in case we somehow messed-up and
+	 *   have got a folio with wrong bits set
+	 * - we do not insist to catch every first buggy page with wrong
+	 *   bits
+	 *
+	 * But we still want to get a warning about the problem sooner or
+	 * later if the problem with flags exists
+	 *
+	 * To achieve this check if a folio that is marked as cache does
+	 * not have any other incompatible flags set.
+	 */
+	WARN_ON(folio_memcg_cache(new) &&
+		(folio_test_slab(new) || folio_test_anon(new)
+		|| folio_test_swapbacked(new)
+		|| folio_test_swapcache(new) || folio_test_mappedtodisk(new)
+		|| folio_test_ksm(new)));
 
 	if (folio_memcg_cache(new))
 		page_counter_charge(&memcg->cache, nr_pages);
-- 
2.31.1



More information about the Devel mailing list