Skip to contentSkip to conversation

mm/slab: mark alloc tags empty for sheafs allocated with __GFP_NO_OBJ_EXT

PATCH Superseded Suren Baghdasaryan 3 participants 6 messages Feb 24 – Feb 25

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mm/slab: mark alloc tags empty for sheafs allocated with __GFP_NO_OBJ_EXT

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using __GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---
 mm/slab.h |  4 ++--
 mm/slub.c | 23 +++++++++++++++--------
 2 files changed, 17 insertions(+), 10 deletions(-)
mm/slab.h +2 -2
index 71c7261bf822..f6ef862b60ef 100644
@@ -290,14 +290,14 @@ static inline void *nearest_obj(struct kmem_cache *cache,
 
 /* Determine object index from a given position */
 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
-					  void *addr, void *obj)
+					  void *addr, const void *obj)
 {
 	return reciprocal_divide(kasan_reset_tag(obj) - addr,
 				 cache->reciprocal_size);
 }
 
 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
-					const struct slab *slab, void *obj)
+					const struct slab *slab, const void *obj)
 {
 	if (is_kfence_address(obj))
 		return 0;
mm/slub.c +15 -8
index 862642c165ed..5c76e55eee7d 100644
@@ -2041,18 +2041,18 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
+static inline void mark_objexts_empty(const void *obj)
 {
-	struct slab *obj_exts_slab;
+	struct slab *obj_slab;
 	unsigned long slab_exts;
 
-	obj_exts_slab = virt_to_slab(obj_exts);
-	slab_exts = slab_obj_exts(obj_exts_slab);
+	obj_slab = virt_to_slab(obj);
+	slab_exts = slab_obj_exts(obj_slab);
 	if (slab_exts) {
 		get_slab_obj_exts(slab_exts);
-		unsigned int offs = obj_to_index(obj_exts_slab->slab_cache,
-						 obj_exts_slab, obj_exts);
-		struct slabobj_ext *ext = slab_obj_ext(obj_exts_slab,
+		unsigned int offs = obj_to_index(obj_slab->slab_cache,
+						 obj_slab, obj);
+		struct slabobj_ext *ext = slab_obj_ext(obj_slab,
 						       slab_exts, offs);
 
 		if (unlikely(is_codetag_empty(&ext->ref))) {
@@ -2090,7 +2090,7 @@ static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
 
 #else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) {}
+static inline void mark_objexts_empty(const void *obj) {}
 static inline bool mark_failed_objexts_alloc(struct slab *slab) { return false; }
 static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
 			struct slabobj_ext *vec, unsigned int objects) {}
@@ -2312,6 +2312,10 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab)
 
 #else /* CONFIG_SLAB_OBJ_EXT */
 
+static inline void mark_objexts_empty(const void *obj)
+{
+}
+
 static inline void init_slab_obj_exts(struct slab *slab)
 {
 }
@@ -2783,6 +2787,9 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 
 static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 {
+	if (s->flags & SLAB_KMALLOC)
+		mark_objexts_empty(sheaf);
+
 	kfree(sheaf);
 
 	stat(s, SHEAF_FREE);

base-commit: 7dff99b354601dd01829e1511711846e04340a69

Signature · 1 line
2.53.0.414.gf7e9f6c205-goog
Tested-by Reviewed-by on v2 1/1
#1 lore raw
Quoted 1 line · Suren Baghdasaryan

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using

nit: the plural form of sheaf is "sheaves" ;)

Quoted 10 lines

__GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---

This resolves the issue on my machine.
and the fix looks good to me, so:

and

By the way, perhaps it's worth adding a comment on __GFP_NO_OBJ_EXT saying "you need to call mark_objexts_empty() before freeing the object".

Signature · 2 lines
Cheers,
Harry / Hyeonggon
#2 lore raw
Quoted 5 lines · Harry Yoo
Quoted 1 line · Suren Baghdasaryan

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using

nit: the plural form of sheaf is "sheaves" ;)

Ack.

Quoted 17 lines
Quoted 11 lines

__GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---

This resolves the issue on my machine.
and the fix looks good to me, so:

Quoted 7 lines

and

By the way, perhaps it's worth adding a comment on __GFP_NO_OBJ_EXT saying "you need to call mark_objexts_empty() before freeing the object".

Good idea. I'll post v3 with that comment and spelling fix. Thanks!

Quoted 4 lines

--
Cheers,
Harry / Hyeonggon

#3 lore raw
Quoted 29 lines · Suren Baghdasaryan
Quoted 5 lines · Harry Yoo
Quoted 1 line · Suren Baghdasaryan

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using

nit: the plural form of sheaf is "sheaves" ;)

Ack.

Quoted 19 lines
Quoted 11 lines

__GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---

This resolves the issue on my machine.
and the fix looks good to me, so:

and

Quoted 7 lines
Quoted 5 lines

By the way, perhaps it's worth adding a comment on __GFP_NO_OBJ_EXT saying "you need to call mark_objexts_empty() before freeing the object".

Good idea. I'll post v3 with that comment and spelling fix.

I added comments and also renamed mark_objexts_empty() into mark_obj_codetag_empty() to better reflect the functionality since it's now used not only for objexts.
v3 is posted here:
https://lore.kernel.org/all/[email protected]/

Quoted 6 lines

Thanks!

Quoted 4 lines

--
Cheers,
Harry / Hyeonggon

Tested-by on v2 1/1
#4 lore raw
In reply to Suren Baghdasaryan
Quoted 97 lines · Suren Baghdasaryan

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using __GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---
mm/slab.h | 4 ++--
mm/slub.c | 23 +++++++++++++++--------

2 files changed, 17 insertions(+), 10 deletions(-)
mm/slab.h +2 -2
index 71c7261bf822..f6ef862b60ef 100644
@@ -290,14 +290,14 @@ static inline void *nearest_obj(struct kmem_cache *cache,
 
 /* Determine object index from a given position */
 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
-					  void *addr, void *obj)
+					  void *addr, const void *obj)
 {
 	return reciprocal_divide(kasan_reset_tag(obj) - addr,
 				 cache->reciprocal_size);
 }
 
 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
-					const struct slab *slab, void *obj)
+					const struct slab *slab, const void *obj)
 {
 	if (is_kfence_address(obj))
 		return 0;
mm/slub.c +15 -8
index 862642c165ed..5c76e55eee7d 100644
@@ -2041,18 +2041,18 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
+static inline void mark_objexts_empty(const void *obj)
 {
-	struct slab *obj_exts_slab;
+	struct slab *obj_slab;
 	unsigned long slab_exts;
 
-	obj_exts_slab = virt_to_slab(obj_exts);
-	slab_exts = slab_obj_exts(obj_exts_slab);
+	obj_slab = virt_to_slab(obj);
+	slab_exts = slab_obj_exts(obj_slab);
 	if (slab_exts) {
 		get_slab_obj_exts(slab_exts);
-		unsigned int offs = obj_to_index(obj_exts_slab->slab_cache,
-						 obj_exts_slab, obj_exts);
-		struct slabobj_ext *ext = slab_obj_ext(obj_exts_slab,
+		unsigned int offs = obj_to_index(obj_slab->slab_cache,
+						 obj_slab, obj);
+		struct slabobj_ext *ext = slab_obj_ext(obj_slab,
 						       slab_exts, offs);
 
 		if (unlikely(is_codetag_empty(&ext->ref))) {
@@ -2090,7 +2090,7 @@ static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
 
 #else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) {}
+static inline void mark_objexts_empty(const void *obj) {}
 static inline bool mark_failed_objexts_alloc(struct slab *slab) { return false; }
 static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
 			struct slabobj_ext *vec, unsigned int objects) {}
@@ -2312,6 +2312,10 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab)
 
 #else /* CONFIG_SLAB_OBJ_EXT */
 
+static inline void mark_objexts_empty(const void *obj)
+{
+}
+
 static inline void init_slab_obj_exts(struct slab *slab)
 {
 }
@@ -2783,6 +2787,9 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 
 static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 {
+	if (s->flags & SLAB_KMALLOC)
+		mark_objexts_empty(sheaf);
+
 	kfree(sheaf);
 
 	stat(s, SHEAF_FREE);
Quoted 3 lines

base-commit: 7dff99b354601dd01829e1511711846e04340a69

Signature · 1 line
2.53.0.414.gf7e9f6c205-goog

David

#5 lore raw
Quoted 97 lines · David Wang
Quoted 95 lines · Suren Baghdasaryan

alloc_empty_sheaf() allocates sheafs from SLAB_KMALLOC caches using __GFP_NO_OBJ_EXT to avoid recursion, however it does not mark their allocation tags empty before freeing, which results in a warning when CONFIG_MEM_ALLOC_PROFILING_DEBUG is set. Fix this by marking allocation tags for such allocations as empty.

---
mm/slab.h | 4 ++--
mm/slub.c | 23 +++++++++++++++--------

2 files changed, 17 insertions(+), 10 deletions(-)
mm/slab.h +2 -2
index 71c7261bf822..f6ef862b60ef 100644
@@ -290,14 +290,14 @@ static inline void *nearest_obj(struct kmem_cache *cache,
 
 /* Determine object index from a given position */
 static inline unsigned int __obj_to_index(const struct kmem_cache *cache,
-                                        void *addr, void *obj)
+                                        void *addr, const void *obj)
 {
      return reciprocal_divide(kasan_reset_tag(obj) - addr,
                               cache->reciprocal_size);
 }
 
 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
-                                      const struct slab *slab, void *obj)
+                                      const struct slab *slab, const void *obj)
 {
      if (is_kfence_address(obj))
              return 0;
mm/slub.c +15 -8
index 862642c165ed..5c76e55eee7d 100644
@@ -2041,18 +2041,18 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
 
 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts)
+static inline void mark_objexts_empty(const void *obj)
 {
-      struct slab *obj_exts_slab;
+      struct slab *obj_slab;
      unsigned long slab_exts;
 
-      obj_exts_slab = virt_to_slab(obj_exts);
-      slab_exts = slab_obj_exts(obj_exts_slab);
+      obj_slab = virt_to_slab(obj);
+      slab_exts = slab_obj_exts(obj_slab);
      if (slab_exts) {
              get_slab_obj_exts(slab_exts);
-              unsigned int offs = obj_to_index(obj_exts_slab->slab_cache,
-                                               obj_exts_slab, obj_exts);
-              struct slabobj_ext *ext = slab_obj_ext(obj_exts_slab,
+              unsigned int offs = obj_to_index(obj_slab->slab_cache,
+                                               obj_slab, obj);
+              struct slabobj_ext *ext = slab_obj_ext(obj_slab,
                                                     slab_exts, offs);
 
              if (unlikely(is_codetag_empty(&ext->ref))) {
@@ -2090,7 +2090,7 @@ static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
 
 #else /* CONFIG_MEM_ALLOC_PROFILING_DEBUG */
 
-static inline void mark_objexts_empty(struct slabobj_ext *obj_exts) {}
+static inline void mark_objexts_empty(const void *obj) {}
 static inline bool mark_failed_objexts_alloc(struct slab *slab) { return false; }
 static inline void handle_failed_objexts_alloc(unsigned long obj_exts,
                      struct slabobj_ext *vec, unsigned int objects) {}
@@ -2312,6 +2312,10 @@ static void alloc_slab_obj_exts_early(struct kmem_cache *s, struct slab *slab)
 
 #else /* CONFIG_SLAB_OBJ_EXT */
 
+static inline void mark_objexts_empty(const void *obj)
+{
+}
+
 static inline void init_slab_obj_exts(struct slab *slab)
 {
 }
@@ -2783,6 +2787,9 @@ static inline struct slab_sheaf *alloc_empty_sheaf(struct kmem_cache *s,
 
 static void free_empty_sheaf(struct kmem_cache *s, struct slab_sheaf *sheaf)
 {
+      if (s->flags & SLAB_KMALLOC)
+              mark_objexts_empty(sheaf);
+
      kfree(sheaf);
 
Quoted 7 lines
Quoted 5 lines
mm/slub.c +0 -0
      stat(s, SHEAF_FREE);

base-commit: 7dff99b354601dd01829e1511711846e04340a69 --
2.53.0.414.gf7e9f6c205-goog

Thanks!

Quoted 3 lines

David