History

bpf: Fix percpu address space issues

In arraymap.c: In bpf_array_map_seq_start() and bpf_array_map_seq_next() cast return values from the __percpu address space to the generic address space via uintptr_t [1]. Correct the declaration of pptr pointer in __bpf_array_map_seq_show() to void __percpu * and cast the value from the generic address space to the __percpu address space via uintptr_t [1]. In hashtab.c: Assign the return value from bpf_mem_cache_alloc() to void pointer and cast the value to void __percpu ** (void pointer to percpu void pointer) before dereferencing. In memalloc.c: Explicitly declare __percpu variables. Cast obj to void __percpu **. In helpers.c: Cast ptr in BPF_CALL_1 and BPF_CALL_2 from generic address space to __percpu address space via const uintptr_t [1]. Found by GCC's named address space checks. There were no changes in the resulting object files. [1] https://sparse.docs.kernel.org/en/latest/annotations.html#address-space-name

Uros Bizjak committed 2 years ago

Commit 6d641ca1 parent 5148f194 files · +17 −16Browse files

Showing the changes to kernel/bpf/arraymap.c (1 of 4 files). Show all

Unified · Split

kernel/bpf/arraymap.c+4−4
@@ -600,7 +600,7 @@ static void *bpf_array_map_seq_start(struct seq_file *seq, loff_t *pos) · 599 unmodified lines
600 array = container_of(map, struct bpf_array, map);600 array = container_of(map, struct bpf_array, map);
601 index = info->index & array->index_mask;601 index = info->index & array->index_mask;
602 if (info->percpu_value_buf)602 if (info->percpu_value_buf)
603 return array->pptrs[index];603 return (void *)(uintptr_t)array->pptrs[index];
604 return array_map_elem_ptr(array, index);604 return array_map_elem_ptr(array, index);
605}605}
606 606
@@ -619,7 +619,7 @@ static void *bpf_array_map_seq_next(struct seq_file *seq, void *v, loff_t *pos) · 12 unmodified lines
619 array = container_of(map, struct bpf_array, map);619 array = container_of(map, struct bpf_array, map);
620 index = info->index & array->index_mask;620 index = info->index & array->index_mask;
621 if (info->percpu_value_buf)621 if (info->percpu_value_buf)
622 return array->pptrs[index];622 return (void *)(uintptr_t)array->pptrs[index];
623 return array_map_elem_ptr(array, index);623 return array_map_elem_ptr(array, index);
624}624}
625 625
@@ -632,7 +632,7 @@ static int __bpf_array_map_seq_show(struct seq_file *seq, void *v) · 6 unmodified lines
632 struct bpf_iter_meta meta;632 struct bpf_iter_meta meta;
633 struct bpf_prog *prog;633 struct bpf_prog *prog;
634 int off = 0, cpu = 0;634 int off = 0, cpu = 0;
635 void __percpu **pptr;635 void __percpu *pptr;
636 u32 size;636 u32 size;
637 637
638 meta.seq = seq;638 meta.seq = seq;
@@ -648,7 +648,7 @@ static int __bpf_array_map_seq_show(struct seq_file *seq, void *v) · 9 unmodified lines
648 if (!info->percpu_value_buf) {648 if (!info->percpu_value_buf) {
649 ctx.value = v;649 ctx.value = v;
650 } else {650 } else {
651 pptr = v;651 pptr = (void __percpu *)(uintptr_t)v;
652 size = array->elem_size;652 size = array->elem_size;
653 for_each_possible_cpu(cpu) {653 for_each_possible_cpu(cpu) {
654 copy_map_value_long(map, info->percpu_value_buf + off,654 copy_map_value_long(map, info->percpu_value_buf + off,