bpf: fix false positive kmemleak report in bpf_ringbuf_area_alloc()
Subsystems (4)
PATCH 1/1 bpf: fix false positive kmemleak report in bpf_ringbuf_area_alloc()
kmemleak scans struct page, but it does not scan the page content. if we allocate some memory with kmalloc(), then allocate page with alloc_page(), and if we put kmalloc pointer somewhere inside that page, kmemleak will report kmalloc pointer as a false positive.
we can instruct kmemleak to scan the memory area by calling kmemleak_alloc()/kmemleak_free(), but part of struct bpf_ringbuf is mmaped to user space, and if struct bpf_ringbuf changes we would have to revisit and review size argument in kmemleak_alloc(), because we do not want kmemleak to scan the user space memory.
let's simplify things and use kmemleak_not_leak() here.
--- kernel/bpf/ringbuf.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/bpf/ringbuf.c +2 -0
@@ -8,6 +8,7 @@#include <linux/vmalloc.h> #include <linux/wait.h> #include <linux/poll.h> +#include <linux/kmemleak.h> #include <uapi/linux/btf.h> #define RINGBUF_CREATE_FLAG_MASK (BPF_F_NUMA_NODE)
@@ -105,6 +106,7 @@ static struct bpf_ringbuf *bpf_ringbuf_area_alloc(size_t data_sz, int numa_node) rb =vmap (pages, nr_meta_pages +2 * nr_data_pages,VM_ALLOC |VM_USERMAP ,PAGE_KERNEL );if (rb) { +kmemleak_not_leak (pages); rb->pages = pages; rb->nr_pages = nr_pages;return rb;
Signature · 1 line
2.30.2
Quoted 12 lines · Rustam Kovhaev
kmemleak scans struct page, but it does not scan the page content. if we allocate some memory with
kmalloc(), then allocate page withalloc_page(), and if we put kmalloc pointer somewhere inside that page, kmemleak will report kmalloc pointer as a false positive.we can instruct kmemleak to scan the memory area by calling kmemleak_alloc()/kmemleak_free(), but part of struct
bpf_ringbufis mmaped to user space, and if structbpf_ringbufchanges we would have to revisit and review size argument inkmemleak_alloc(), because we do not want kmemleak to scan the user space memory.
let's simplify things and usekmemleak_not_leak()here.
Quoted 5 lines
Applied, thanks! (Also included Andrii's prior analysis as well to the commit log so there's a bit more context if we need to revisit in future [0].)