History

bpf: Switch bpf_map_{area_alloc,area_mmapable_alloc}() to u64 size

Given we recently extended the original bpf_map_area_alloc() helper in commit fc9702273e2e ("bpf: Add mmap() support for BPF_MAP_TYPE_ARRAY"), we need to apply the same logic as in ff1c08e1f74b ("bpf: Change size to u64 for bpf_map_{area_alloc, charge_init}()"). To avoid conflicts, extend it for bpf-next.

Daniel Borkmann committed 6 years ago

Commit 196e8ca1 parent 91e60152 files · +10 −7Browse files

Unified · Split

2 files
  1. include/linux/bpf.h +3 −3
  2. kernel/bpf/syscall.c +7 −4
include/linux/bpf.h+3−3
@@ -794,12 +794,12 @@ void bpf_map_put_with_uref(struct bpf_map *map); · 793 unmodified lines
794void bpf_map_put(struct bpf_map *map);794void bpf_map_put(struct bpf_map *map);
795int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);795int bpf_map_charge_memlock(struct bpf_map *map, u32 pages);
796void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);796void bpf_map_uncharge_memlock(struct bpf_map *map, u32 pages);
797int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size);797int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size);
798void bpf_map_charge_finish(struct bpf_map_memory *mem);798void bpf_map_charge_finish(struct bpf_map_memory *mem);
799void bpf_map_charge_move(struct bpf_map_memory *dst,799void bpf_map_charge_move(struct bpf_map_memory *dst,
800 struct bpf_map_memory *src);800 struct bpf_map_memory *src);
801void *bpf_map_area_alloc(size_t size, int numa_node);801void *bpf_map_area_alloc(u64 size, int numa_node);
802void *bpf_map_area_mmapable_alloc(size_t size, int numa_node);802void *bpf_map_area_mmapable_alloc(u64 size, int numa_node);
803void bpf_map_area_free(void *base);803void bpf_map_area_free(void *base);
804void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);804void bpf_map_init_from_attr(struct bpf_map *map, union bpf_attr *attr);
805 805
kernel/bpf/syscall.c+7−4
@@ -128,7 +128,7 @@ static struct bpf_map *find_and_alloc_map(union bpf_attr *attr) · 127 unmodified lines
128 return map;128 return map;
129}129}
130 130
131static void *__bpf_map_area_alloc(size_t size, int numa_node, bool mmapable)131static void *__bpf_map_area_alloc(u64 size, int numa_node, bool mmapable)
132{132{
133 /* We really just want to fail instead of triggering OOM killer133 /* We really just want to fail instead of triggering OOM killer
134 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,134 * under memory pressure, therefore we set __GFP_NORETRY to kmalloc,
@@ -143,6 +143,9 @@ static void *__bpf_map_area_alloc(size_t size, int numa_node, bool mmapable) · 8 unmodified lines
143 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;143 const gfp_t flags = __GFP_NOWARN | __GFP_ZERO;
144 void *area;144 void *area;
145 145
146 if (size >= SIZE_MAX)
147 return NULL;
148
146 /* kmalloc()'ed memory can't be mmap()'ed */149 /* kmalloc()'ed memory can't be mmap()'ed */
147 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {150 if (!mmapable && size <= (PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER)) {
148 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,151 area = kmalloc_node(size, GFP_USER | __GFP_NORETRY | flags,
@@ -160,12 +163,12 @@ static void *__bpf_map_area_alloc(size_t size, int numa_node, bool mmapable) · 11 unmodified lines
160 flags, __builtin_return_address(0));163 flags, __builtin_return_address(0));
161}164}
162 165
163void *bpf_map_area_alloc(size_t size, int numa_node)166void *bpf_map_area_alloc(u64 size, int numa_node)
164{167{
165 return __bpf_map_area_alloc(size, numa_node, false);168 return __bpf_map_area_alloc(size, numa_node, false);
166}169}
167 170
168void *bpf_map_area_mmapable_alloc(size_t size, int numa_node)171void *bpf_map_area_mmapable_alloc(u64 size, int numa_node)
169{172{
170 return __bpf_map_area_alloc(size, numa_node, true);173 return __bpf_map_area_alloc(size, numa_node, true);
171}174}
@@ -214,7 +217,7 @@ static void bpf_uncharge_memlock(struct user_struct *user, u32 pages) · 42 unmodified lines
214 atomic_long_sub(pages, &user->locked_vm);217 atomic_long_sub(pages, &user->locked_vm);
215}218}
216 219
217int bpf_map_charge_init(struct bpf_map_memory *mem, size_t size)220int bpf_map_charge_init(struct bpf_map_memory *mem, u64 size)
218{221{
219 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;222 u32 pages = round_up(size, PAGE_SIZE) >> PAGE_SHIFT;
220 struct user_struct *user;223 struct user_struct *user;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t