Skip to contentSkip to conversation

mm/slab: don't set SLAB_MAY_ACCOUNT for KMALLOC_PARTITION_CACHES

PATCH Applied Hao Li 3 participants 5 messages Nov 11 – Nov 23

Subsystems (2)
lore
Share & tools
lore

Hao Li

author reviewer
#0 lore raw

PATCH 1/1 mm/slab: don't set SLAB_MAY_ACCOUNT for KMALLOC_PARTITION_CACHES

An allocation with __GFP_ACCOUNT set (without __GFP_DMA or __GFP_RECLAIMABLE) is redirected to KMALLOC_CGROUP and will never hit KMALLOC_NORMAL or KMALLOC_PARTITION_CACHES. Consequently, neither of these caches requires objcg slots in obj_exts.

While KMALLOC_NORMAL already skips setting SLAB_MAY_ACCOUNT, KMALLOC_PARTITION_CACHES still carries it unnecessarily.

Extend the check to skip KMALLOC_PARTITION_CACHES as well, saving unnecessary objcg slots in obj_exts.

---
This doesn't seem like a real bug to me, just a small waste of memory, so I didn't add a Fixes tag.

---
 mm/slab_common.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/slab_common.c +6 -4
index 2befd24b4d09..f0ea1fa178e7 100644
@@ -1000,16 +1000,18 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
 #endif
 
 	/*
-	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL cache and not
-	 * aliased with any other type, make sure it's never merged with any other
-	 * cache.
+	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL or
+	 * KMALLOC_PARTITION_CACHES cache and not aliased with any other type,
+	 * make sure it's never merged with any other cache.
 	 *
 	 * In other cases the kmalloc cache may end up being used for a
 	 * __GFP_ACCOUNT allocation so mark it as such. The exception is a
 	 * KMALLOC_NO_OBJ_EXT cache.
 	 */
 	if (!mem_cgroup_kmem_disabled()) {
-		if (type == KMALLOC_NORMAL && KMALLOC_RECLAIM != KMALLOC_NORMAL)
+		if (type >= KMALLOC_PARTITION_START &&
+		    type <= KMALLOC_PARTITION_END &&
+		    KMALLOC_RECLAIM != KMALLOC_NORMAL)
 			flags |= SLAB_NO_MERGE;
 		else if (!(flags & SLAB_NO_OBJ_EXT))
 			flags |= SLAB_MAY_ACCOUNT;
Signature · 1 line
2.55.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Hao Li

An allocation with __GFP_ACCOUNT set (without __GFP_DMA or __GFP_RECLAIMABLE) is redirected to KMALLOC_CGROUP and will never hit KMALLOC_NORMAL or KMALLOC_PARTITION_CACHES. Consequently, neither of these caches requires objcg slots in obj_exts.

Quoted 7 lines

While KMALLOC_NORMAL already skips setting SLAB_MAY_ACCOUNT, KMALLOC_PARTITION_CACHES still carries it unnecessarily.

Extend the check to skip KMALLOC_PARTITION_CACHES as well, saving unnecessary objcg slots in obj_exts.

Quoted 3 lines

---
This doesn't seem like a real bug to me, just a small waste of memory, so I didn't add a Fixes tag.

Hmm I'd add

Fixes 7def2e8549e5 ("mm/slab: add cache_ and slab_needs_objcg() helpers")

The question is whether cc stable as well. 7.3 LTS will be used for Android and IIRC they have memory allocation profiling enabled. Not sure about kmalloc partitions. Cc'ing Suren.

Quoted 31 lines
---
 mm/slab_common.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/slab_common.c +6 -4
index 2befd24b4d09..f0ea1fa178e7 100644
@@ -1000,16 +1000,18 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
 #endif
 
 	/*
-	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL cache and not
-	 * aliased with any other type, make sure it's never merged with any other
-	 * cache.
+	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL or
+	 * KMALLOC_PARTITION_CACHES cache and not aliased with any other type,
+	 * make sure it's never merged with any other cache.
 	 *
 	 * In other cases the kmalloc cache may end up being used for a
 	 * __GFP_ACCOUNT allocation so mark it as such. The exception is a
 	 * KMALLOC_NO_OBJ_EXT cache.
 	 */
 	if (!mem_cgroup_kmem_disabled()) {
-		if (type == KMALLOC_NORMAL && KMALLOC_RECLAIM != KMALLOC_NORMAL)
+		if (type >= KMALLOC_PARTITION_START &&
+		    type <= KMALLOC_PARTITION_END &&
+		    KMALLOC_RECLAIM != KMALLOC_NORMAL)
 			flags |= SLAB_NO_MERGE;
 		else if (!(flags & SLAB_NO_OBJ_EXT))
 			flags |= SLAB_MAY_ACCOUNT;
author reviewer
#2 lore raw
Quoted 9 lines · Vlastimil Babka (SUSE)
Quoted 8 lines · Hao Li

An allocation with __GFP_ACCOUNT set (without __GFP_DMA or __GFP_RECLAIMABLE) is redirected to KMALLOC_CGROUP and will never hit KMALLOC_NORMAL or KMALLOC_PARTITION_CACHES. Consequently, neither of these caches requires objcg slots in obj_exts.

While KMALLOC_NORMAL already skips setting SLAB_MAY_ACCOUNT, KMALLOC_PARTITION_CACHES still carries it unnecessarily.

Quoted 6 lines
Quoted 4 lines

Extend the check to skip KMALLOC_PARTITION_CACHES as well, saving unnecessary objcg slots in obj_exts.

Thanks!

Quoted 7 lines
Quoted 4 lines

---
This doesn't seem like a real bug to me, just a small waste of memory, so I didn't add a Fixes tag.

Hmm I'd add

Fixes 7def2e8549e5 ("mm/slab: add cache_ and slab_needs_objcg() helpers")

Ah right, I mixed up Fixes with cc stable. Thanks for pointing it out!

Quoted 4 lines

The question is whether cc stable as well. 7.3 LTS will be used for Android and IIRC they have memory allocation profiling enabled. Not sure about kmalloc partitions. Cc'ing Suren.

Makes sense. If Android uses partitioned caches, then it looks like we should cc stable.

Quoted 33 lines
Quoted 32 lines
---
 mm/slab_common.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
mm/slab_common.c +6 -4
index 2befd24b4d09..f0ea1fa178e7 100644
@@ -1000,16 +1000,18 @@ new_kmalloc_cache(int idx, enum kmalloc_cache_type type)
 #endif
 
 	/*
-	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL cache and not
-	 * aliased with any other type, make sure it's never merged with any other
-	 * cache.
+	 * If memcg_kmem is enabled and this is a KMALLOC_NORMAL or
+	 * KMALLOC_PARTITION_CACHES cache and not aliased with any other type,
+	 * make sure it's never merged with any other cache.
 	 *
 	 * In other cases the kmalloc cache may end up being used for a
 	 * __GFP_ACCOUNT allocation so mark it as such. The exception is a
 	 * KMALLOC_NO_OBJ_EXT cache.
 	 */
 	if (!mem_cgroup_kmem_disabled()) {
-		if (type == KMALLOC_NORMAL && KMALLOC_RECLAIM != KMALLOC_NORMAL)
+		if (type >= KMALLOC_PARTITION_START &&
+		    type <= KMALLOC_PARTITION_END &&
+		    KMALLOC_RECLAIM != KMALLOC_NORMAL)
 			flags |= SLAB_NO_MERGE;
 		else if (!(flags & SLAB_NO_OBJ_EXT))
 			flags |= SLAB_MAY_ACCOUNT;
maintainer Applied
#3 lore raw
In reply to Hao Li
Quoted 9 lines · Hao Li

An allocation with __GFP_ACCOUNT set (without __GFP_DMA or __GFP_RECLAIMABLE) is redirected to KMALLOC_CGROUP and will never hit KMALLOC_NORMAL or KMALLOC_PARTITION_CACHES. Consequently, neither of these caches requires objcg slots in obj_exts.

While KMALLOC_NORMAL already skips setting SLAB_MAY_ACCOUNT, KMALLOC_PARTITION_CACHES still carries it unnecessarily.

[...]

Applied to mm/slab.git for-next, thanks!

[1/1] mm/slab: don't set SLAB_MAY_ACCOUNT for KMALLOC_PARTITION_CACHES

      commit: ec81fa8f7dbcf89d9b4112544f76f47c8eb5c725
Signature · 2 lines
Cheers,
Harry / Hyeonggon
maintainer
#4 lore raw
Quoted 9 lines · Harry Yoo (Meta)
Quoted 8 lines · Hao Li

An allocation with __GFP_ACCOUNT set (without __GFP_DMA or __GFP_RECLAIMABLE) is redirected to KMALLOC_CGROUP and will never hit KMALLOC_NORMAL or KMALLOC_PARTITION_CACHES. Consequently, neither of these caches requires objcg slots in obj_exts.

While KMALLOC_NORMAL already skips setting SLAB_MAY_ACCOUNT, KMALLOC_PARTITION_CACHES still carries it unnecessarily.

Quoted 6 lines
Quoted 1 line

[...]

Applied to mm/slab.git for-next, thanks!

[1/1] mm/slab: don't set SLAB_MAY_ACCOUNT for KMALLOC_PARTITION_CACHES

      commit: ec81fa8f7dbcf89d9b4112544f76f47c8eb5c725

Just for the record I added Fixes: tag per Vlastimil's suggestion but didn't add Cc: stable (dunno if Android would need it (due to random kmalloc caches), but it can be backported later if needed).

Signature · 2 lines
Cheers,
Harry / Hyeonggon