Skip to contentSkip to conversation

bpf: fail mmap without CONFIG_MMU

PATCH In review Andrii Nakryiko 4 participants 4 messages Nov 23, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: fail mmap without CONFIG_MMU

mmap() support for BPF array depends on vmalloc_user_node_flags, which is available only on CONFIG_MMU configurations. Fail mmap-able allocations if no CONFIG_MMU is set.

---
 kernel/bpf/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/syscall.c +4 -0
index bb002f15b32a..242a06fbdf18 100644
@@ -156,8 +156,12 @@ static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
 	}
 	if (mmapable) {
 		BUG_ON(!PAGE_ALIGNED(size));
+#ifndef CONFIG_MMU
+		return NULL;
+#else
 		return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
 					       __GFP_RETRY_MAYFAIL | flags);
+#endif
 	}
 	return __vmalloc_node_flags_caller(size, numa_node,
 					   GFP_KERNEL | __GFP_RETRY_MAYFAIL |
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 16 lines · Andrii Nakryiko

mmap() support for BPF array depends on vmalloc_user_node_flags, which is available only on CONFIG_MMU configurations. Fail mmap-able allocations if no CONFIG_MMU is set.

---
  kernel/bpf/syscall.c | 4 ++++
  1 file changed, 4 insertions(+)
kernel/bpf/syscall.c +0 -0
index bb002f15b32a..242a06fbdf18 100644

kernel/bpf/syscall.c static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) · line 163

Quoted 8 lines
kernel/bpf/syscall.c +3 -0
@@ -156,8 +156,12 @@ static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
  	}
  	if (mmapable) {
  		BUG_ON(!PAGE_ALIGNED(size));
+#ifndef CONFIG_MMU
+		return NULL;
+#else
  		return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
  					       __GFP_RETRY_MAYFAIL | flags);

Hmm, this should rather live in include/linux/vmalloc.h, otherwise every future user of vmalloc_user_node_flags() would need to add this ifdef? vmalloc.h has the below, so perhaps this could be added there instead:

[..]
#ifndef CONFIG_MMU
extern void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags); static inline void *__vmalloc_node_flags_caller(unsigned long size, int node,
gfp_t flags, void *caller)

{
        return __vmalloc_node_flags(size, node, flags);
}
#else
extern void *__vmalloc_node_flags_caller(unsigned long size,
                                         int node, gfp_t flags, void *caller);

#endif
[..]

Quoted 5 lines
kernel/bpf/syscall.c +1 -0
+#endif
  	}
  	return __vmalloc_node_flags_caller(size, numa_node,
  					   GFP_KERNEL | __GFP_RETRY_MAYFAIL |
#2 lore raw
Quoted 22 lines · Daniel Borkmann
Quoted 20 lines · Andrii Nakryiko

mmap() support for BPF array depends on vmalloc_user_node_flags, which is available only on CONFIG_MMU configurations. Fail mmap-able allocations if no CONFIG_MMU is set.

---
  kernel/bpf/syscall.c | 4 ++++
  1 file changed, 4 insertions(+)
kernel/bpf/syscall.c +1 -0
index bb002f15b32a..242a06fbdf18 100644
@@ -156,8 +156,12 @@ static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
      }
      if (mmapable) {
              BUG_ON(!PAGE_ALIGNED(size));
+#ifndef CONFIG_MMU
Quoted 8 lines

kernel/bpf/syscall.c static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) · line 163

Quoted 4 lines
kernel/bpf/syscall.c +2 -0
+             return NULL;
+#else
              return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
                                             __GFP_RETRY_MAYFAIL | flags);

Hmm, this should rather live in include/linux/vmalloc.h, otherwise every future user of vmalloc_user_node_flags() would need to add this ifdef? vmalloc.h has the below, so perhaps this could be added there instead:

Ok, I finally untangled this all. The real fix is to implement vmalloc_user_node_flags() in mm/no_mmu.c. Took me a bit to understand how all this stuff is supposed to compile. If you take a look at vmalloc.h, you'll see that most variants of vmalloc() are not conditionally defined like __vmalloc_node_flags_caller. I'll send a different patch with proper fix in few minutes.

Quoted 21 lines

[..]
#ifndef CONFIG_MMU
extern void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags); static inline void *__vmalloc_node_flags_caller(unsigned long size, int node,
gfp_t flags, void *caller)

{
         return __vmalloc_node_flags(size, node, flags);
}
#else
extern void *__vmalloc_node_flags_caller(unsigned long size,
                                          int node, gfp_t flags, void *caller);

#endif
[..]

Quoted 5 lines
kernel/bpf/syscall.c +1 -0
+#endif
      }
      return __vmalloc_node_flags_caller(size, numa_node,
                                         GFP_KERNEL | __GFP_RETRY_MAYFAIL |
#3 lore raw
In reply to Andrii Nakryiko
Quoted 16 lines · Andrii Nakryiko

mmap() support for BPF array depends on vmalloc_user_node_flags, which is available only on CONFIG_MMU configurations. Fail mmap-able allocations if no CONFIG_MMU is set.

---
 kernel/bpf/syscall.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/syscall.c +0 -0
index bb002f15b32a..242a06fbdf18 100644

kernel/bpf/syscall.c static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable) · line 164

Quoted 9 lines
kernel/bpf/syscall.c +4 -0
@@ -156,8 +156,12 @@ static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
 	}
 	if (mmapable) {
 		BUG_ON(!PAGE_ALIGNED(size));
+#ifndef CONFIG_MMU
+		return NULL;
+#else
 		return vmalloc_user_node_flags(size, numa_node, GFP_KERNEL |
 					       __GFP_RETRY_MAYFAIL | flags);
+#endif

Typically such ifdef goes into .h. There are several examples of such !CONFIG_MMU in vmalloc.h. May be move it there?