Skip to contentSkip to conversation

slab: Add check for memcg_data's upper bits in folio_memcg_kmem

PATCH Superseded Hao Ge 1 participant 1 message Oct 14, 2025

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Hao Ge

author
#0 lore raw

PATCH v2 1/1 slab: Add check for memcg_data's upper bits in folio_memcg_kmem

From: Hao Ge <[email protected]>

This is because OBJEXTS_ALLOC_FAIL and OBJEXTS_ALLOC_FAIL currently share the same bit position. Therefore, we cannot simply determine whether memcg_data still points to the slabobj_ext vector by checking folio->memcg_data & MEMCG_DATA_OBJEXTS.

We can distinguish between these two cases by checking whether the upper bits set:

1) MEMCG_DATA_OBJEXTS is set, but upper bits are not set, so it should mean obj_exts allocation failed (OBJEXTS_ALLOC_FAIL), thus do not report error, or

2) MEMCG_DATA_OBJEXTS is set, and upper bits are also set, so someone did not clear a valid folio->memcg_data before freeing the folio (report error).

So let's add check for memcg_data's upper bits in folio_memcg_kmem.

Fixes 7612833192d5 ("slab: Reuse first bit for OBJEXTS_ALLOC_FAIL")

---
v2: Per Vlastimil and Harry's suggestion, instead of introducing a new bit,
implement this by checking if the highest bit is set.
Many thanks to Vlastimil and Harry.

---
 include/linux/memcontrol.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
include/linux/memcontrol.h +3 -1
index 873e510d6f8d..f9f7ba14be04 100644
@@ -534,7 +534,9 @@ static inline struct mem_cgroup *get_mem_cgroup_from_objcg(struct obj_cgroup *ob
 static inline bool folio_memcg_kmem(struct folio *folio)
 {
 	VM_BUG_ON_PGFLAGS(PageTail(&folio->page), &folio->page);
-	VM_BUG_ON_FOLIO(folio->memcg_data & MEMCG_DATA_OBJEXTS, folio);
+	VM_BUG_ON_FOLIO((folio->memcg_data & MEMCG_DATA_OBJEXTS) &&
+			(folio->memcg_data & ~(ULONG_MAX >> 1)),
+			folio);
 	return folio->memcg_data & MEMCG_DATA_KMEM;
 }
 
Signature · 1 line
2.25.1