Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btf: Allow mmap of vmlinux btf

User space needs access to kernel BTF for many modern features of BPF.
Right now each process needs to read the BTF blob either in pieces or
as a whole. Allow mmaping the sysfs file so that processes can directly
access the memory allocated for it in the kernel.

remap_pfn_range is used instead of vm_insert_page due to aarch64
compatibility issues.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Tested-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Link: https://lore.kernel.org/bpf/20250520-vmlinux-mmap-v5-1-e8c941acc414@isovalent.com

authored by

Lorenz Bauer and committed by
Andrii Nakryiko
a539e2a6 8259eb0e

+34 -1
+2 -1
include/asm-generic/vmlinux.lds.h
··· 667 667 */ 668 668 #ifdef CONFIG_DEBUG_INFO_BTF 669 669 #define BTF \ 670 + . = ALIGN(PAGE_SIZE); \ 670 671 .BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \ 671 672 BOUNDED_SECTION_BY(.BTF, _BTF) \ 672 673 } \ 673 - . = ALIGN(4); \ 674 + . = ALIGN(PAGE_SIZE); \ 674 675 .BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) { \ 675 676 *(.BTF_ids) \ 676 677 }
+32
kernel/bpf/sysfs_btf.c
··· 7 7 #include <linux/kobject.h> 8 8 #include <linux/init.h> 9 9 #include <linux/sysfs.h> 10 + #include <linux/mm.h> 11 + #include <linux/io.h> 12 + #include <linux/btf.h> 10 13 11 14 /* See scripts/link-vmlinux.sh, gen_btf() func for details */ 12 15 extern char __start_BTF[]; 13 16 extern char __stop_BTF[]; 14 17 18 + static int btf_sysfs_vmlinux_mmap(struct file *filp, struct kobject *kobj, 19 + const struct bin_attribute *attr, 20 + struct vm_area_struct *vma) 21 + { 22 + unsigned long pages = PAGE_ALIGN(attr->size) >> PAGE_SHIFT; 23 + size_t vm_size = vma->vm_end - vma->vm_start; 24 + phys_addr_t addr = virt_to_phys(__start_BTF); 25 + unsigned long pfn = addr >> PAGE_SHIFT; 26 + 27 + if (attr->private != __start_BTF || !PAGE_ALIGNED(addr)) 28 + return -EINVAL; 29 + 30 + if (vma->vm_pgoff) 31 + return -EINVAL; 32 + 33 + if (vma->vm_flags & (VM_WRITE | VM_EXEC | VM_MAYSHARE)) 34 + return -EACCES; 35 + 36 + if (pfn + pages < pfn) 37 + return -EINVAL; 38 + 39 + if ((vm_size >> PAGE_SHIFT) > pages) 40 + return -EINVAL; 41 + 42 + vm_flags_mod(vma, VM_DONTDUMP, VM_MAYEXEC | VM_MAYWRITE); 43 + return remap_pfn_range(vma, vma->vm_start, pfn, vm_size, vma->vm_page_prot); 44 + } 45 + 15 46 static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = { 16 47 .attr = { .name = "vmlinux", .mode = 0444, }, 17 48 .read_new = sysfs_bin_attr_simple_read, 49 + .mmap = btf_sysfs_vmlinux_mmap, 18 50 }; 19 51 20 52 struct kobject *btf_kobj;