mm/slub: don't let kmemleak report rcu sheaves
Subsystems (3)
PATCH 1/1 mm/slub: don't let kmemleak report rcu sheaves
kmemleak reports full rcu sheaves as leaked:
unreferenced object 0xffff88810005f600 (size 512):
hex dump (first 32 bytes):
e0 79 f0 2b 81 88 ff ff 20 00 b4 81 ff ff ff ff
00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00
backtrace:
__alloc_empty_sheaf mm/slub.c:2854 [inline]
alloc_empty_sheaf mm/slub.c:2874 [inline]
__pcs_replace_empty_main+0x260/0x2e0 mm/slub.c:4806The second word is rcu_free_sheaf and the size equals the capacity: the sheaf was handed to call_rcu() by __kfree_rcu_sheaf() and is waiting for a grace period. At that point the only reference to it is the ->next pointer of the previous callback in the rcu callback list.
That previous callback may live in memory kmemleak never scans, e.g. the rcu_head of a struct slab queued by rcu_free_slab() for a SLAB_TYPESAFE_BY_RCU cache. Slab pages are frozen (page_count() == 0), so kmemleak skips their struct page. The chain from rcu_data is cut and the sheaf, typically allocated long ago and recycled, looks unreferenced. Since the sheaf is then not scanned either, the objects it is about to free can be reported as well.
Sheaves are allocator metadata and are always owned by a pcs, a barn or an rcu callback. Mark them with kmemleak_not_leak(): they are never reported but still scanned, so objects queued in them stay referenced. Allocations that can't spin are not tracked by kmemleak, so skip them, as alloc_slab_obj_exts() does.
---
Reproduced in QEMU (syzbot config, 2 vCPUs, numa=fake=2) by stalling a grace period with a preempted reader on CPU0 while CPU1 interleaves rcu_free_slab() (filp slabs) and rcu_free_sheaf() (maple nodes freed by kfree_rcu()) callbacks, then running two kmemleak scans:
before: 16-18 sheaves reported per run, rcu_head.func == rcu_free_sheaf,
plus maple nodes held by them
after: no sheaf or maple node reports, no new warnings
mm/slub.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/slub.c +8 -0
@@ -2856,6 +2856,14 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,if (unlikely (!sheaf))return NULL ; +/* +* A queued rcu sheaf may only be referenced by an earlier rcu callback +* in memory kmemleak doesn't scan (e.g. struct slab). Never report +* sheaves, but keep scanning the objects they hold. +*/ +if (alloc_flags_allow_spinning (alloc_flags)) +kmemleak_not_leak (sheaf); + sheaf->cache = s;stat (s,SHEAF_ALLOC );
Signature · 1 line
2.43.0
Vlastimil Babka (SUSE)
maintainer+CC Breno
Quoted 16 lines · Nguyen Ngoc Thang
kmemleak reports full rcu sheaves as leaked:
unreferenced object
0xffff88810005f600(size 512):hex dump (first 32 bytes): e0 79 f0 2b 81 88 ff ff 20 00 b4 81 ff ff ff ff 00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00 backtrace: __alloc_empty_sheaf mm/slub.c:2854 [inline] alloc_empty_sheaf mm/slub.c:2874 [inline] __pcs_replace_empty_main+0x260/0x2e0 mm/slub.c:4806The second word is
rcu_free_sheafand the size equals the capacity: the sheaf was handed tocall_rcu()by__kfree_rcu_sheaf()and is waiting for a grace period. At that point the only reference to it is the ->next pointer of the previous callback in the rcu callback list.
Quoted 7 lines
That previous callback may live in memory kmemleak never scans, e.g. the
rcu_headof a struct slab queued byrcu_free_slab()for aSLAB_TYPESAFE_BY_RCUcache. Slab pages are frozen (page_count()== 0), so kmemleak skips their struct page. The chain fromrcu_datais cut and the sheaf, typically allocated long ago and recycled, looks unreferenced. Since the sheaf is then not scanned either, the objects it is about to free can be reported as well.
I don't get this part. The previous object can't be that much gone, RCU still needs to get to this sheaf at some point, so there has to be a live pointer somewhere?
Quoted 10 lines
Sheaves are allocator metadata and are always owned by a pcs, a barn or an rcu callback. Mark them with
kmemleak_not_leak(): they are never reported but still scanned, so objects queued in them stay referenced. Allocations that can't spin are not tracked by kmemleak, so skip them, asalloc_slab_obj_exts()does.Fixes ec66e0d59952 ("slab: add sheaf support for batching kfree_rcu() operations")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>
I wonder if we should rather do this at the moment the sheaf is handed over to rcu.
Or if it's a generic problem that should rather be solved by kmemleak having visibility to the rcu queues.
Quoted 32 lines
---
Reproduced in QEMU (syzbot config, 2 vCPUs, numa=fake=2) by stalling a grace period with a preempted reader on CPU0 while CPU1 interleavesrcu_free_slab()(filp slabs) andrcu_free_sheaf()(maple nodes freed bykfree_rcu()) callbacks, then running two kmemleak scans:before: 16-18 sheaves reported per run, rcu_head.func ==
rcu_free_sheaf,
plus maple nodes held by them
after: no sheaf or maple node reports, no new warningsmm/slub.c | 8 ++++++++ 1 file changed, 8 insertions(+)mm/slub.c +8
-0@@ -2856,6 +2856,14 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,if (unlikely (!sheaf))return NULL ; +/* +* A queued rcu sheaf may only be referenced by an earlier rcu callback +* in memory kmemleak doesn't scan (e.g. struct slab). Never report +* sheaves, but keep scanning the objects they hold. +*/ +if (alloc_flags_allow_spinning (alloc_flags)) +kmemleak_not_leak (sheaf); + sheaf->cache = s;stat (s,SHEAF_ALLOC );
Catalin Marinas
maintainerQuoted 27 lines · Vlastimil Babka (SUSE)
+CC Breno
Quoted 16 lines · Nguyen Ngoc Thang
kmemleak reports full rcu sheaves as leaked:
unreferenced object
0xffff88810005f600(size 512):hex dump (first 32 bytes): e0 79 f0 2b 81 88 ff ff 20 00 b4 81 ff ff ff ff 00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00 backtrace: __alloc_empty_sheaf mm/slub.c:2854 [inline] alloc_empty_sheaf mm/slub.c:2874 [inline] __pcs_replace_empty_main+0x260/0x2e0 mm/slub.c:4806The second word is
rcu_free_sheafand the size equals the capacity: the sheaf was handed tocall_rcu()by__kfree_rcu_sheaf()and is waiting for a grace period. At that point the only reference to it is the ->next pointer of the previous callback in the rcu callback list.Quoted 7 lines
That previous callback may live in memory kmemleak never scans, e.g. the
rcu_headof a struct slab queued byrcu_free_slab()for aSLAB_TYPESAFE_BY_RCUcache. Slab pages are frozen (page_count()== 0), so kmemleak skips their struct page. The chain fromrcu_datais cut and the sheaf, typically allocated long ago and recycled, looks unreferenced. Since the sheaf is then not scanned either, the objects it is about to free can be reported as well.
Quoted 3 lines
I don't get this part. The previous object can't be that much gone, RCU still needs to get to this sheaf at some point, so there has to be a live pointer somewhere?
After some back and forth with claude, I think there are a couple of places where kmemleak fails to track. Since commit 9aec2fb0fd5e ("slab:
allocate frozen pages"), kmemleak no longer scans the slab structures in scan_zone_pages(). We could do something like below:
mm/kmemleak.c +6 -2
@@ -1875,9 +1875,13 @@ static int scan_zone_pages(struct zone *zone)if (!(pfn &63 ))cond_resched_tasks_rcu_qs (); -/* only scan in-use pages belonging to this zone */ +/* +* Only scan in-use pages belonging to this zone. Slab pages +* are frozen (zero refcount) but struct slab holds pointers +* such as the rcu_head of a slab pending rcu_free_slab(). +*/ if (page && (page_zone (page) != zone || -page_count (page) ==0 )) + (page_count (page) ==0 && !PageSlab (page)))) page =NULL ; if (page && first && page == last + 1 &&
I don't think this is specific to sheaves, SLAB_TYPESAFE_BY_RCU may have a similar problem, though I don't remember to have seen such reports yet. It should be covered by the above.
However, this only fixes the links through struct slab. The rcu list can also go through a sheaf allocated with kmalloc_nolock(), which kmemleak doesn't track. Any callback queued after it is missed (together with the objects queued in that sheaf that may come from ordinary kfree_rcu()). When kmemleak is enabled, could we simply avoid allocating sheaves with kmalloc_nolock(), i.e.:
mm/slub.c +8 -0
@@ -6278,6 +6278,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags)local_unlock (&s->cpu_sheaves->lock); +/* +* A sheaf allocated with kmalloc_nolock() is not tracked by +* kmemleak, so neither its objects nor the rcu callback queued +* after it would be scanned. Fall back to defer_kfree_rcu(). +*/ +if (IS_ENABLED (CONFIG_DEBUG_KMEMLEAK ) && !allow_spin) + goto fail; +/* Don't wake up kswapd, it will cause deadlock under pi_lock */ empty =alloc_empty_sheaf (s, __GFP_NOWARN, alloc_flags);
Not tested and it's possible I misunderstood how all this works. But if it works, I don't think the kmemleak_not_leak() annotation is needed.
Signature · 1 line
Catalin
Catalin Marinas
maintainerQuoted 27 lines · Catalin Marinas
After some back and forth with claude, I think there are a couple of places where kmemleak fails to track. Since commit
9aec2fb0fd5e("slab:
allocate frozen pages"), kmemleak no longer scans the slab structures inscan_zone_pages(). We could do something like below:mm/kmemleak.c +6
-2@@ -1875,9 +1875,13 @@ static int scan_zone_pages(struct zone *zone)if (!(pfn &63 ))cond_resched_tasks_rcu_qs (); -/* only scan in-use pages belonging to this zone */ +/* +* Only scan in-use pages belonging to this zone. Slab pages +* are frozen (zero refcount) but struct slab holds pointers +* such as the rcu_head of a slab pending rcu_free_slab(). +*/ if (page && (page_zone (page) != zone || -page_count (page) ==0 )) + (page_count (page) ==0 && !PageSlab (page)))) page =NULL ; if (page && first && page == last + 1 &&
Quoted 3 lines
I don't think this is specific to sheaves,
SLAB_TYPESAFE_BY_RCUmay have a similar problem, though I don't remember to have seen such reports yet. It should be covered by the above.
This still leaves a hole. Scanning the slab descriptor recovers the link through slab->rcu_head.next, but the preceding callback can also be free_object_rcu() whose rcu_head is embedded in a kmemleak metadata object allocated from a SLAB_NOLEAKTRACE cache. This would break the chain.
So I think we should keep the original kmemleak_not_leak() patch at allocation.
Quoted 16 lines
However, this only fixes the links through struct slab. The rcu list can also go through a sheaf allocated with
kmalloc_nolock(), which kmemleak doesn't track. Any callback queued after it is missed (together with the objects queued in that sheaf that may come from ordinarykfree_rcu()). When kmemleak is enabled, could we simply avoid allocating sheaves withkmalloc_nolock(), i.e.:mm/slub.c +1
-0@@ -6278,6 +6278,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags)local_unlock (&s->cpu_sheaves->lock); +/*
mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags) · line 6291
Quoted 10 lines
mm/slub.c +7
-0+* A sheaf allocated with kmalloc_nolock() is not tracked by +* kmemleak, so neither its objects nor the rcu callback queued +* after it would be scanned. Fall back to defer_kfree_rcu(). +*/ +if (IS_ENABLED (CONFIG_DEBUG_KMEMLEAK ) && !allow_spin) + goto fail; +/* Don't wake up kswapd, it will cause deadlock under pi_lock */ empty =alloc_empty_sheaf (s, __GFP_NOWARN, alloc_flags);
For the objects queued in a sheaf, rather than the !allow_spin fallback above, it's simpler to move the kmemleak_ignore() call in kvfree_call_rcu() before kfree_rcu_sheaf(), so they are treated the same as on the non-sheaf path. kfree_rcu_nolock() currently only has a kunit user, freeing kmalloc_nolock() objects that kmemleak doesn't track anyway.
So this second hunk isn't needed either.
To Vlastimil's earlier point, kmemleak could follow these rcu chains instead of relying on kmemleak_not_leak(). However, with chain breaks at any _nolock or SLAB_NOLEAKTRACE object (including kmemleak's own metadata) and walking the rcu callback lists directly means digging into rcu internals. The annotation is easier to maintain.
(got lost in all the slub code, it was much simpler 17 years ago when we merged kmemleak ;))
Signature · 1 line
Catalin
Harry Yoo
maintainerQuoted 9 lines · Catalin Marinas
Quoted 8 lines · Catalin Marinas
mm/slub.c +0
-0@@ -6278,6 +6278,14 @@ bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags)local_unlock (&s->cpu_sheaves->lock);
Quoted 16 lines
mm/slub.c bool __kfree_rcu_sheaf(struct kmem_cache *s, void *obj, unsigned int free_flags) · line 6291
Quoted 11 lines
mm/slub.c +8
-0+/* +* A sheaf allocated with kmalloc_nolock() is not tracked by +* kmemleak, so neither its objects nor the rcu callback queued +* after it would be scanned. Fall back to defer_kfree_rcu(). +*/ +if (IS_ENABLED (CONFIG_DEBUG_KMEMLEAK ) && !allow_spin) + goto fail; +/* Don't wake up kswapd, it will cause deadlock under pi_lock */ empty =alloc_empty_sheaf (s, __GFP_NOWARN, alloc_flags);For the objects queued in a sheaf, rather than the !allow_spin fallback above, it's simpler to move the
kmemleak_ignore()call inkvfree_call_rcu()beforekfree_rcu_sheaf(), so they are treated the same as on the non-sheaf path.
No strong opinion on this but kmemleak_not_leak() would let kmemleak track objects queued in a sheaf?
Wondering it'd make sense to mark bnodes with kmemleak_not_leak() as well instead of calling kmemleak_ignore() on all objects.
Quoted 5 lines
To Vlastimil's earlier point, kmemleak could follow these rcu chains instead of relying on
kmemleak_not_leak(). However, with chain breaks at any_nolockorSLAB_NOLEAKTRACEobject (including kmemleak's own metadata) and walking the rcu callback lists directly means digging into rcu internals. The annotation is easier to maintain.
Fair enough.
Quoted 2 lines
(got lost in all the slub code, it was much simpler 17 years ago when we merged kmemleak ;))
Good old days :')
Signature · 2 lines
Cheers, Harry / Hyeonggon
Quoted 56 lines · Nguyen Ngoc Thang
kmemleak reports full rcu sheaves as leaked:
unreferenced object
0xffff88810005f600(size 512):hex dump (first 32 bytes): e0 79 f0 2b 81 88 ff ff 20 00 b4 81 ff ff ff ff 00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00 backtrace: __alloc_empty_sheaf mm/slub.c:2854 [inline] alloc_empty_sheaf mm/slub.c:2874 [inline] __pcs_replace_empty_main+0x260/0x2e0 mm/slub.c:4806The second word is
rcu_free_sheafand the size equals the capacity: the sheaf was handed tocall_rcu()by__kfree_rcu_sheaf()and is waiting for a grace period. At that point the only reference to it is the ->next pointer of the previous callback in the rcu callback list.That previous callback may live in memory kmemleak never scans, e.g. the
rcu_headof a struct slab queued byrcu_free_slab()for aSLAB_TYPESAFE_BY_RCUcache. Slab pages are frozen (page_count()== 0), so kmemleak skips their struct page. The chain fromrcu_datais cut and the sheaf, typically allocated long ago and recycled, looks unreferenced. Since the sheaf is then not scanned either, the objects it is about to free can be reported as well.Sheaves are allocator metadata and are always owned by a pcs, a barn or an rcu callback. Mark them with
kmemleak_not_leak(): they are never reported but still scanned, so objects queued in them stay referenced. Allocations that can't spin are not tracked by kmemleak, so skip them, asalloc_slab_obj_exts()does.Fixes ec66e0d59952 ("slab: add sheaf support for batching kfree_rcu() operations")Reported-by [email protected]Signed-off-by Nguyen Ngoc Thang <[email protected]>---
Reproduced in QEMU (syzbot config, 2 vCPUs, numa=fake=2) by stalling a grace period with a preempted reader on CPU0 while CPU1 interleavesrcu_free_slab()(filp slabs) andrcu_free_sheaf()(maple nodes freed bykfree_rcu()) callbacks, then running two kmemleak scans:before: 16-18 sheaves reported per run, rcu_head.func ==
rcu_free_sheaf,
plus maple nodes held by them
after: no sheaf or maple node reports, no new warningsmm/slub.c | 8 ++++++++ 1 file changed, 8 insertions(+)mm/slub.c +1
-0@@ -2856,6 +2856,14 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,if (unlikely (!sheaf))return NULL ; +/*
mm/slub.c static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp, · line 2869
Quoted 10 lines
mm/slub.c +7
-0+* A queued rcu sheaf may only be referenced by an earlier rcu callback +* in memory kmemleak doesn't scan (e.g. struct slab). Never report +* sheaves, but keep scanning the objects they hold. +*/ +if (alloc_flags_allow_spinning (alloc_flags)) +kmemleak_not_leak (sheaf); + sheaf->cache = s;stat (s,SHEAF_ALLOC );
PATCH v2 1/1 mm/slub: don't let kmemleak report rcu sheaves
In reply to Nguyen Ngoc Thangkmemleak reports sheaves as leaked:
unreferenced object 0xffff8881009d4e00 (size 512):
hex dump (first 32 bytes):
b0 7e 81 2d 81 88 ff ff 60 bd b3 81 ff ff ff ff
00 12 04 00 81 88 ff ff 3c 00 00 00 00 00 00 00
backtrace:
__alloc_empty_sheaf mm/slub.c:2854 [inline]
alloc_empty_sheaf mm/slub.c:2874 [inline]
__pcs_replace_full_main+0xf7/0x330 mm/slub.c:6094
free_to_pcs mm/slub.c:6147 [inline]
kfree+0x353/0x390 mm/slub.c:6923The same happens for sheaves allocated by __pcs_replace_empty_main(), __kfree_rcu_sheaf() and kmem_cache_prefill_sheaf().
The second word is rcu_free_sheaf: the sheaf was handed to call_rcu() by __kfree_rcu_sheaf() and is waiting for a grace period. Sheaves are recycled between the pcs, the barn and rcu, so any sheaf can end up there, no matter where it was allocated. While queued, the only reference to it is the ->next pointer of the previous callback.
That callback often lives in memory kmemleak never scans. kmemleak itself frees every kmemleak_object with call_rcu(free_object_rcu), and those come from the SLAB_NOLEAKTRACE object_cache. struct slab of a SLAB_TYPESAFE_BY_RCU cache pending rcu_free_slab() is another case, as slab pages are frozen and skipped by scan_zone_pages(). The chain from rcu_data is cut, and the sheaf, typically old, looks unreferenced. Since it is then not scanned either, the objects it is about to free can be reported as well.
Making kmemleak follow rcu callback lists would mean digging into rcu internals and would still break at any untracked callback. Sheaves are allocator metadata, always owned by a pcs, a barn or an rcu callback, so mark them with kmemleak_not_leak() instead: they are never reported but still scanned, so objects queued in them stay referenced. Allocations that can't spin are not tracked by kmemleak, so skip them, as alloc_slab_obj_exts() does.
---
v2:
- Rewrite the explanation. v1 blamed struct slab, but in my reproducer most reported sheaves are queued right behind kmemleak's own free_object_rcu() callback, whose kmemleak_object comes from the SLAB_NOLEAKTRACE object_cache and is never scanned. struct slab remains another possible break, as Catalin pointed out.
- Comment: "e.g. struct slab" -> "e.g. its own metadata".
- Also fixes syzbot 4d078516add5f4f12cea (sheaves allocated by __pcs_replace_full_main()), same root cause.
- Rebased on v7.4-rc4; applies to next-20261120 as well. There, sheaves of SLAB_NOLEAKTRACE caches are not tracked, and kmemleak_not_leak() on them is a harmless no-op (paint_ptr() ignores unknown objects).
- Code unchanged otherwise, so I kept Catalin's Reviewed-by.
v1: https://lore.kernel.org/all/[email protected]/
Tested in QEMU (syzbot config, 2 vCPUs), stalling a grace period with a preempted reader on CPU0 while CPU1 queues rcu_free_slab() and rcu_free_sheaf() callbacks, then running kmemleak scans:
before (v7.4-rc3): 176 reports, 82 of them sheaves (allocated by
__kfree_rcu_sheaf, kmem_cache_prefill_sheaf, __pcs_replace_full_main, __pcs_replace_empty_main), the rest maple nodes held by them
after (v7.4-rc4 + v2, 4 runs): no sheaf or maple node reports.
5-9 reports per run remain, all from a single exited task (task_struct, pid, creds) waiting for its RCU callback. Same kmemleak blind spot, not sheaf related.
mm/slub.c | 8 ++++++++ 1 file changed, 8 insertions(+)
mm/slub.c +8 -0
@@ -2856,6 +2856,14 @@ static struct slab_sheaf *__alloc_empty_sheaf(struct kmem_cache *s, gfp_t gfp,if (unlikely (!sheaf))return NULL ; +/* +* A queued rcu sheaf may only be referenced by an earlier rcu callback +* in memory kmemleak doesn't scan (e.g. its own metadata). Never +* report sheaves, but keep scanning the objects they hold. +*/ +if (alloc_flags_allow_spinning (alloc_flags)) +kmemleak_not_leak (sheaf); + sheaf->cache = s;stat (s,SHEAF_ALLOC );
Signature · 1 line
2.43.0