Skip to contentSkip to conversation

mm, slub: emit the "free" trace report before freeing memory in kmem_cache_free()

PATCH Superseded Yunfeng Ye 4 participants 6 messages Nov 2, 2021 – Nov 3, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yunfeng Ye

author
#0 lore raw

PATCH v2 1/1 mm, slub: emit the "free" trace report before freeing memory in kmem_cache_free()

After the memory is freed, it can be immediately allocated by other CPUs, before the "free" trace report has been emitted. This causes inaccurate traces.

For example, if the following sequence of events occurs:

CPU 0 CPU 1

(1) alloc xxxxxx
(2) free xxxxxx
(3) alloc xxxxxx
(4) free xxxxxx

Then they will be inaccurately reported via tracing, so that they appear to have happened in this order:

CPU 0 CPU 1

(1) alloc xxxxxx
(2) alloc xxxxxx
(3) free xxxxxx
(4) free xxxxxx

This makes it look like CPU 1 somehow managed to allocate mmemory that CPU 0 still had allocated for itself.

In order to avoid this, emit the "free xxxxxx" tracing report just before the actual call to free the memory, instead of just after it.

---
v1 -> v2:
- Modify the description
- Add "Reviewed-by"

 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index 432145d7b4ec..427e62034c3f 100644
@@ -3526,8 +3526,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 	s = cache_from_obj(s, x);
 	if (!s)
 		return;
-	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 	trace_kmem_cache_free(_RET_IP_, x, s->name);
+	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 }
 EXPORT_SYMBOL(kmem_cache_free);
 
Signature · 1 line
2.27.0
#1 lore raw
Quoted 44 lines · Yunfeng Ye

After the memory is freed, it can be immediately allocated by other CPUs, before the "free" trace report has been emitted. This causes inaccurate traces.

For example, if the following sequence of events occurs:

CPU 0 CPU 1

(1) alloc xxxxxx
(2) free xxxxxx
(3) alloc xxxxxx
(4) free xxxxxx

Then they will be inaccurately reported via tracing, so that they appear to have happened in this order:

CPU 0 CPU 1

(1) alloc xxxxxx
(2) alloc xxxxxx
(3) free xxxxxx
(4) free xxxxxx

This makes it look like CPU 1 somehow managed to allocate mmemory that CPU 0 still had allocated for itself.

In order to avoid this, emit the "free xxxxxx" tracing report just before the actual call to free the memory, instead of just after it.

---
v1 -> v2:
- Modify the description
- Add "Reviewed-by"

 mm/slub.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +0 -0
index 432145d7b4ec..427e62034c3f 100644

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 3531

Quoted 7 lines
mm/slub.c +1 -1
@@ -3526,8 +3526,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 	s = cache_from_obj(s, x);
 	if (!s)
 		return;
-	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 	trace_kmem_cache_free(_RET_IP_, x, s->name);
+	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 }

It seems that kmem_cache_free() in mm/slab.c has the same problem. We can fix it. Thanks.

Quoted 2 lines
mm/slub.c +0 -0
 EXPORT_SYMBOL(kmem_cache_free);
 
#2 lore raw
Quoted 14 lines · Tang Yizhou

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 3531

Quoted 10 lines · Yunfeng Ye
mm/slub.c +1 -1
@@ -3526,8 +3526,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 	s = cache_from_obj(s, x);
 	if (!s)
 		return;
-	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 	trace_kmem_cache_free(_RET_IP_, x, s->name);
+	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 }

It seems that kmem_cache_free() in mm/slab.c has the same problem. We can fix it. Thanks.

Doh, true. Should go best before the local_irq_save() there. And also kmem_cache_free() in mm/slob.c.

Interestingly kfree() is already OK in all 3 implementations.

Quoted 3 lines
Quoted 2 lines
mm/slub.c +0 -0
 EXPORT_SYMBOL(kmem_cache_free);
 
author
#3 lore raw
Quoted 16 lines · Vlastimil Babka
Quoted 14 lines · Tang Yizhou

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 3531

Quoted 10 lines · Yunfeng Ye
mm/slub.c +1 -1
@@ -3526,8 +3526,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
 	s = cache_from_obj(s, x);
 	if (!s)
 		return;
-	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 	trace_kmem_cache_free(_RET_IP_, x, s->name);
+	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
 }

It seems that kmem_cache_free() in mm/slab.c has the same problem. We can fix it. Thanks.

Quoted 3 lines

Doh, true. Should go best before the local_irq_save() there. And also kmem_cache_free() in mm/slob.c.

Yes, I will fix the same problem together in the v3 patch.

Thanks.

Quoted 8 lines

Interestingly kfree() is already OK in all 3 implementations.

Quoted 3 lines

mm/slub.c void kmem_cache_free(struct kmem_cache *s, void *x) · line 3533

Quoted 2 lines
mm/slub.c +0 -0
 EXPORT_SYMBOL(kmem_cache_free);
 

.

Reviewed-by on v2 1/1
#4 lore raw
In reply to Yunfeng Ye
Quoted 16 lines · Yunfeng Ye

After the memory is freed, it can be immediately allocated by other CPUs, before the "free" trace report has been emitted. This causes inaccurate traces.

For example, if the following sequence of events occurs:

CPU 0 CPU 1

(1) alloc xxxxxx
(2) free xxxxxx
(3) alloc xxxxxx
(4) free xxxxxx

Then they will be inaccurately reported via tracing, so that they appear to have happened in this order:

Quoted 8 lines

CPU 0 CPU 1

(1) alloc xxxxxx
(2) alloc xxxxxx
(3) free xxxxxx
(4) free xxxxxx

This makes it look like CPU 1 somehow managed to allocate mmemory that

I see I created a typo for you, sorry about that: s/mmemory/memory/

But anyway, the wording looks good now. Please feel free to add:

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
Quoted 29 lines

CPU 0 still had allocated for itself.

In order to avoid this, emit the "free xxxxxx" tracing report just before the actual call to free the memory, instead of just after it.

---
v1 -> v2:
- Modify the description
- Add "Reviewed-by"

  mm/slub.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/slub.c +1 -1
index 432145d7b4ec..427e62034c3f 100644
@@ -3526,8 +3526,8 @@ void kmem_cache_free(struct kmem_cache *s, void *x)
  	s = cache_from_obj(s, x);
  	if (!s)
  		return;
-	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
  	trace_kmem_cache_free(_RET_IP_, x, s->name);
+	slab_free(s, virt_to_head_page(x), x, NULL, 1, _RET_IP_);
  }
  EXPORT_SYMBOL(kmem_cache_free);
 
author
#5 lore raw
Quoted 24 lines · John Hubbard
Quoted 23 lines · Yunfeng Ye

After the memory is freed, it can be immediately allocated by other CPUs, before the "free" trace report has been emitted. This causes inaccurate traces.

For example, if the following sequence of events occurs:

     CPU 0                 CPU 1

   (1) alloc xxxxxx
   (2) free  xxxxxx
                          (3) alloc xxxxxx
                          (4) free  xxxxxx

Then they will be inaccurately reported via tracing, so that they appear to have happened in this order:

     CPU 0                 CPU 1

   (1) alloc xxxxxx
                          (2) alloc xxxxxx
   (3) free  xxxxxx
                          (4) free  xxxxxx

Quoted 9 lines
Quoted 1 line

This makes it look like CPU 1 somehow managed to allocate mmemory that

I see I created a typo for you, sorry about that: s/mmemory/memory/

But anyway, the wording looks good now. Please feel free to add:

Ok, I will fix the typo in the v3 patch.

Thanks.

Quoted 2 lines

thanks,