buffer: fix kmemleak false positive in submit_bh_wbc
Subsystems (2)
PATCH 1/1 buffer: fix kmemleak false positive in submit_bh_wbc
Bios allocated in submit_bh_wbc are properly freed via their end_io handler. Since commit 48f22f80938d, bio_put() caches them in a per-CPU bio cache for reuse rather than freeing them back to the mempool. While cached bios are reachable by kmemleak via the per-CPU cache pointers, once recycled for new I/O they are only referenced by block layer internals that kmemleak does not scan, causing false positive leak reports.
Mark the bio allocation with kmemleak_not_leak() to suppress the false positive.
--- fs/buffer.c | 2 ++ 1 file changed, 2 insertions(+)
fs/buffer.c +2 -0
@@ -49,6 +49,7 @@#include <linux/sched/mm.h> #include <trace/events/block.h> #include <linux/fscrypt.h> +#include <linux/kmemleak.h> #include <linux/fsverity.h> #include <linux/sched/isolation.h>
@@ -2799,6 +2800,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, opf |=REQ_PRIO ; bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); +kmemleak_not_leak (bio);fscrypt_set_bio_crypt_ctx_bh (bio, bh,GFP_NOIO );
Signature · 1 line
2.51.0
Jens Axboe
maintainerQuoted 31 lines · Sasha Levin
Bios allocated in
submit_bh_wbcare properly freed via theirend_iohandler. Since commit48f22f80938d,bio_put()caches them in a per-CPU bio cache for reuse rather than freeing them back to the mempool. While cached bios are reachable by kmemleak via the per-CPU cache pointers, once recycled for new I/O they are only referenced by block layer internals that kmemleak does not scan, causing false positive leak reports.Mark the bio allocation with
kmemleak_not_leak()to suppress the false positive.Fixes 48f22f80938d ("block: enable per-cpu bio cache by default")Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>--- fs/buffer.c | 2 ++ 1 file changed, 2 insertions(+)fs/buffer.c +1
-0@@ -49,6 +49,7 @@#include <linux/sched/mm.h> #include <trace/events/block.h> #include <linux/fscrypt.h> +#include <linux/kmemleak.h> #include <linux/fsverity.h> #include <linux/sched/isolation.h>
fs/buffer.c static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, · line 2805
Quoted 6 lines
fs/buffer.c +1
-0@@ -2799,6 +2800,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, opf |=REQ_PRIO ; bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); +kmemleak_not_leak (bio);fscrypt_set_bio_crypt_ctx_bh (bio, bh,GFP_NOIO );
What if they do end up getting leaked? This seems like an odd work-around, would be better to ensure the caching side marks them as in-use when grabbed and freed when put.
Signature · 1 line
Jens Axboe
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Sasha Levin
Bios allocated in
submit_bh_wbcare properly freed via theirend_iohandler. Since commit48f22f80938d,bio_put()caches them in a per-CPU bio cache for reuse rather than freeing them back to the mempool. While cached bios are reachable by kmemleak via the per-CPU cache pointers, once recycled for new I/O they are only referenced by block layer internals that kmemleak does not scan, causing false positive leak reports.Mark the bio allocation with
kmemleak_not_leak()to suppress the false positive.Fixes 48f22f80938d ("block: enable per-cpu bio cache by default")Assisted-by Claude:claude-opus-4-6Signed-off-by Sasha Levin <[email protected]>--- fs/buffer.c | 2 ++ 1 file changed, 2 insertions(+)fs/buffer.c +1
-0@@ -49,6 +49,7 @@#include <linux/sched/mm.h> #include <trace/events/block.h> #include <linux/fscrypt.h> +#include <linux/kmemleak.h> #include <linux/fsverity.h> #include <linux/sched/isolation.h> @@ -2799,6 +2800,7 @@ static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, opf |=REQ_PRIO ;
Quoted 6 lines
fs/buffer.c static void submit_bh_wbc(blk_opf_t opf, struct buffer_head *bh, · line 2805
Quoted 4 lines
fs/buffer.c +1
-0bio =bio_alloc (bh->b_bdev,1 , opf,GFP_NOIO ); +kmemleak_not_leak (bio);fscrypt_set_bio_crypt_ctx_bh (bio, bh,GFP_NOIO );What if they do end up getting leaked? This seems like an odd
I was under the impression that kmemleak doesn't really track much under the hood of the block layer to begin with, but looking at the code I'm probably wrong.
Quoted 2 lines
work-around, would be better to ensure the caching side marks them as in-use when grabbed and freed when put.
Something like:?
block/bio.c +9 -0
@@ -17,6 +17,7 @@#include <linux/cgroup.h> #include <linux/highmem.h> #include <linux/blk-crypto.h> +#include <linux/kmemleak.h> #include <linux/xarray.h> #include <trace/events/block.h>
@@ -504,6 +505,9 @@ static struct bio *bio_alloc_percpu_cache(struct block_device *bdev, cache->nr--;put_cpu (); +kmemleak_alloc ((void *)bio - bs->front_pad, +kmem_cache_size (bs->bio_slab),1 , gfp); +if (nr_vecs)bio_init_inline (bio, bdev, nr_vecs, opf);else
@@ -765,6 +769,9 @@ static int __bio_alloc_cache_prune(struct bio_alloc_cache *cache,while ((bio = cache->free_list) !=NULL ) { cache->free_list = bio->bi_next; cache->nr--; +kmemleak_alloc ((void *)bio - bio->bi_pool->front_pad, +kmem_cache_size (bio->bi_pool->bio_slab), +1 ,GFP_NOWAIT );bio_free (bio);if (++i == nr)break ;
@@ -823,6 +830,7 @@ static inline void bio_put_percpu_cache(struct bio *bio)if (in_task ()) {bio_uninit (bio); +kmemleak_free ((void *)bio - bio->bi_pool->front_pad); bio->bi_next = cache->free_list;/* Not necessary but helps not to iopoll already freed bios */ bio->bi_bdev =NULL ;
@@ -832,6 +840,7 @@ static inline void bio_put_percpu_cache(struct bio *bio)lockdep_assert_irqs_disabled ();bio_uninit (bio); +kmemleak_free ((void *)bio - bio->bi_pool->front_pad); bio->bi_next = cache->free_list_irq; cache->free_list_irq = bio; cache->nr_irq++;
Signature · 2 lines
Thanks, Sasha