Skip to content

Commit a539e2a

Browse files
lmbanakryiko
authored andcommitted
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 <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Tested-by: Alan Maguire <[email protected]> Reviewed-by: Shakeel Butt <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 8259eb0 commit a539e2a

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

include/asm-generic/vmlinux.lds.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,11 @@ defined(CONFIG_AUTOFDO_CLANG) || defined(CONFIG_PROPELLER_CLANG)
667667
*/
668668
#ifdef CONFIG_DEBUG_INFO_BTF
669669
#define BTF \
670+
. = ALIGN(PAGE_SIZE); \
670671
.BTF : AT(ADDR(.BTF) - LOAD_OFFSET) { \
671672
BOUNDED_SECTION_BY(.BTF, _BTF) \
672673
} \
673-
. = ALIGN(4); \
674+
. = ALIGN(PAGE_SIZE); \
674675
.BTF_ids : AT(ADDR(.BTF_ids) - LOAD_OFFSET) { \
675676
*(.BTF_ids) \
676677
}

kernel/bpf/sysfs_btf.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,46 @@
77
#include <linux/kobject.h>
88
#include <linux/init.h>
99
#include <linux/sysfs.h>
10+
#include <linux/mm.h>
11+
#include <linux/io.h>
12+
#include <linux/btf.h>
1013

1114
/* See scripts/link-vmlinux.sh, gen_btf() func for details */
1215
extern char __start_BTF[];
1316
extern char __stop_BTF[];
1417

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+
1546
static struct bin_attribute bin_attr_btf_vmlinux __ro_after_init = {
1647
.attr = { .name = "vmlinux", .mode = 0444, },
1748
.read_new = sysfs_bin_attr_simple_read,
49+
.mmap = btf_sysfs_vmlinux_mmap,
1850
};
1951

2052
struct kobject *btf_kobj;

0 commit comments

Comments
 (0)