Skip to content

Commit 86bbf01

Browse files
committed
Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Alexei Starovoitov says: ==================== pull-request: bpf 2020-11-06 1) Pre-allocated per-cpu hashmap needs to zero-fill reused element, from David. 2) Tighten bpf_lsm function check, from KP. 3) Fix bpftool attaching to flow dissector, from Lorenz. 4) Use -fno-gcse for the whole kernel/bpf/core.c instead of function attribute, from Ard. * git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: bpf: Update verification logic for LSM programs bpf: Zero-fill re-used per-cpu map element bpf: BPF_PRELOAD depends on BPF_SYSCALL tools/bpftool: Fix attaching flow dissector libbpf: Fix possible use after free in xsk_socket__delete libbpf: Fix null dereference in xsk_socket__delete libbpf, hashmap: Fix undefined behavior in hash_bits bpf: Don't rely on GCC __attribute__((optimize)) to disable GCSE tools, bpftool: Remove two unused variables. tools, bpftool: Avoid array index warnings. xsk: Fix possible memory leak at socket close bpf: Add struct bpf_redir_neigh forward declaration to BPF helper defs samples/bpf: Set rlimit for memlock to infinity in all samples bpf: Fix -Wshadow warnings selftest/bpf: Fix profiler test using CO-RE relocation for enums ==================== Link: https://lore.kernel.org/r/20201106221759.24143-1-alexei.starovoitov@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
2 parents bf3e762 + 6f64e47 commit 86bbf01

25 files changed

Lines changed: 346 additions & 49 deletions

File tree

include/linux/compiler-gcc.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@
175175
#else
176176
#define __diag_GCC_8(s)
177177
#endif
178-
179-
#define __no_fgcse __attribute__((optimize("-fno-gcse")))

include/linux/compiler_types.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,6 @@ struct ftrace_likely_data {
247247
#define asm_inline asm
248248
#endif
249249

250-
#ifndef __no_fgcse
251-
# define __no_fgcse
252-
#endif
253-
254250
/* Are two types/vars the same type (ignoring qualifiers)? */
255251
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
256252

include/linux/filter.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -558,21 +558,21 @@ struct sk_filter {
558558
DECLARE_STATIC_KEY_FALSE(bpf_stats_enabled_key);
559559

560560
#define __BPF_PROG_RUN(prog, ctx, dfunc) ({ \
561-
u32 ret; \
561+
u32 __ret; \
562562
cant_migrate(); \
563563
if (static_branch_unlikely(&bpf_stats_enabled_key)) { \
564-
struct bpf_prog_stats *stats; \
565-
u64 start = sched_clock(); \
566-
ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \
567-
stats = this_cpu_ptr(prog->aux->stats); \
568-
u64_stats_update_begin(&stats->syncp); \
569-
stats->cnt++; \
570-
stats->nsecs += sched_clock() - start; \
571-
u64_stats_update_end(&stats->syncp); \
564+
struct bpf_prog_stats *__stats; \
565+
u64 __start = sched_clock(); \
566+
__ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \
567+
__stats = this_cpu_ptr(prog->aux->stats); \
568+
u64_stats_update_begin(&__stats->syncp); \
569+
__stats->cnt++; \
570+
__stats->nsecs += sched_clock() - __start; \
571+
u64_stats_update_end(&__stats->syncp); \
572572
} else { \
573-
ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \
573+
__ret = dfunc(ctx, (prog)->insnsi, (prog)->bpf_func); \
574574
} \
575-
ret; })
575+
__ret; })
576576

577577
#define BPF_PROG_RUN(prog, ctx) \
578578
__BPF_PROG_RUN(prog, ctx, bpf_dispatcher_nop_func)

include/net/xsk_buff_pool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
8686
void xp_destroy(struct xsk_buff_pool *pool);
8787
void xp_release(struct xdp_buff_xsk *xskb);
8888
void xp_get_pool(struct xsk_buff_pool *pool);
89-
void xp_put_pool(struct xsk_buff_pool *pool);
89+
bool xp_put_pool(struct xsk_buff_pool *pool);
9090
void xp_clear_dev(struct xsk_buff_pool *pool);
9191
void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
9292
void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);

kernel/bpf/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# SPDX-License-Identifier: GPL-2.0
22
obj-y := core.o
3-
CFLAGS_core.o += $(call cc-disable-warning, override-init)
3+
ifneq ($(CONFIG_BPF_JIT_ALWAYS_ON),y)
4+
# ___bpf_prog_run() needs GCSE disabled on x86; see 3193c0836f203 for details
5+
cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse
6+
endif
7+
CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy)
48

59
obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o
610
obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o

kernel/bpf/bpf_lsm.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <linux/bpf_verifier.h>
1414
#include <net/bpf_sk_storage.h>
1515
#include <linux/bpf_local_storage.h>
16+
#include <linux/btf_ids.h>
1617

1718
/* For every LSM hook that allows attachment of BPF programs, declare a nop
1819
* function where a BPF program can be attached.
@@ -26,7 +27,11 @@ noinline RET bpf_lsm_##NAME(__VA_ARGS__) \
2627
#include <linux/lsm_hook_defs.h>
2728
#undef LSM_HOOK
2829

29-
#define BPF_LSM_SYM_PREFX "bpf_lsm_"
30+
#define LSM_HOOK(RET, DEFAULT, NAME, ...) BTF_ID(func, bpf_lsm_##NAME)
31+
BTF_SET_START(bpf_lsm_hooks)
32+
#include <linux/lsm_hook_defs.h>
33+
#undef LSM_HOOK
34+
BTF_SET_END(bpf_lsm_hooks)
3035

3136
int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
3237
const struct bpf_prog *prog)
@@ -37,8 +42,7 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
3742
return -EINVAL;
3843
}
3944

40-
if (strncmp(BPF_LSM_SYM_PREFX, prog->aux->attach_func_name,
41-
sizeof(BPF_LSM_SYM_PREFX) - 1)) {
45+
if (!btf_id_set_contains(&bpf_lsm_hooks, prog->aux->attach_btf_id)) {
4246
bpf_log(vlog, "attach_btf_id %u points to wrong type name %s\n",
4347
prog->aux->attach_btf_id, prog->aux->attach_func_name);
4448
return -EINVAL;

kernel/bpf/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1369,7 +1369,7 @@ u64 __weak bpf_probe_read_kernel(void *dst, u32 size, const void *unsafe_ptr)
13691369
*
13701370
* Decode and execute eBPF instructions.
13711371
*/
1372-
static u64 __no_fgcse ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
1372+
static u64 ___bpf_prog_run(u64 *regs, const struct bpf_insn *insn, u64 *stack)
13731373
{
13741374
#define BPF_INSN_2_LBL(x, y) [BPF_##x | BPF_##y] = &&x##_##y
13751375
#define BPF_INSN_3_LBL(x, y, z) [BPF_##x | BPF_##y | BPF_##z] = &&x##_##y##_##z

kernel/bpf/hashtab.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,32 @@ static void pcpu_copy_value(struct bpf_htab *htab, void __percpu *pptr,
821821
}
822822
}
823823

824+
static void pcpu_init_value(struct bpf_htab *htab, void __percpu *pptr,
825+
void *value, bool onallcpus)
826+
{
827+
/* When using prealloc and not setting the initial value on all cpus,
828+
* zero-fill element values for other cpus (just as what happens when
829+
* not using prealloc). Otherwise, bpf program has no way to ensure
830+
* known initial values for cpus other than current one
831+
* (onallcpus=false always when coming from bpf prog).
832+
*/
833+
if (htab_is_prealloc(htab) && !onallcpus) {
834+
u32 size = round_up(htab->map.value_size, 8);
835+
int current_cpu = raw_smp_processor_id();
836+
int cpu;
837+
838+
for_each_possible_cpu(cpu) {
839+
if (cpu == current_cpu)
840+
bpf_long_memcpy(per_cpu_ptr(pptr, cpu), value,
841+
size);
842+
else
843+
memset(per_cpu_ptr(pptr, cpu), 0, size);
844+
}
845+
} else {
846+
pcpu_copy_value(htab, pptr, value, onallcpus);
847+
}
848+
}
849+
824850
static bool fd_htab_map_needs_adjust(const struct bpf_htab *htab)
825851
{
826852
return htab->map.map_type == BPF_MAP_TYPE_HASH_OF_MAPS &&
@@ -891,7 +917,7 @@ static struct htab_elem *alloc_htab_elem(struct bpf_htab *htab, void *key,
891917
}
892918
}
893919

894-
pcpu_copy_value(htab, pptr, value, onallcpus);
920+
pcpu_init_value(htab, pptr, value, onallcpus);
895921

896922
if (!prealloc)
897923
htab_elem_set_ptr(l_new, key_size, pptr);
@@ -1183,7 +1209,7 @@ static int __htab_lru_percpu_map_update_elem(struct bpf_map *map, void *key,
11831209
pcpu_copy_value(htab, htab_elem_get_ptr(l_old, key_size),
11841210
value, onallcpus);
11851211
} else {
1186-
pcpu_copy_value(htab, htab_elem_get_ptr(l_new, key_size),
1212+
pcpu_init_value(htab, htab_elem_get_ptr(l_new, key_size),
11871213
value, onallcpus);
11881214
hlist_nulls_add_head_rcu(&l_new->hash_node, head);
11891215
l_new = NULL;

kernel/bpf/preload/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ config USERMODE_DRIVER
66
menuconfig BPF_PRELOAD
77
bool "Preload BPF file system with kernel specific program and map iterators"
88
depends on BPF
9+
depends on BPF_SYSCALL
910
# The dependency on !COMPILE_TEST prevents it from being enabled
1011
# in allmodconfig or allyesconfig configurations
1112
depends on !COMPILE_TEST

net/xdp/xsk.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,8 @@ static void xsk_destruct(struct sock *sk)
11461146
if (!sock_flag(sk, SOCK_DEAD))
11471147
return;
11481148

1149-
xp_put_pool(xs->pool);
1149+
if (!xp_put_pool(xs->pool))
1150+
xdp_put_umem(xs->umem);
11501151

11511152
sk_refcnt_debug_dec(sk);
11521153
}

0 commit comments

Comments
 (0)