Skip to content

Commit e7a594a

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-next/master' into bpf-next/for-next
Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 8ccd382 + 1a437d3 commit e7a594a

18 files changed

+899
-142
lines changed

include/linux/btf.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,7 @@ bool btf_is_prog_ctx_type(struct bpf_verifier_log *log, const struct btf *btf,
580580
int get_kern_ctx_btf_id(struct bpf_verifier_log *log, enum bpf_prog_type prog_type);
581581
bool btf_types_are_same(const struct btf *btf1, u32 id1,
582582
const struct btf *btf2, u32 id2);
583+
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx);
583584
#else
584585
static inline const struct btf_type *btf_type_by_id(const struct btf *btf,
585586
u32 type_id)
@@ -654,6 +655,10 @@ static inline bool btf_types_are_same(const struct btf *btf1, u32 id1,
654655
{
655656
return false;
656657
}
658+
static inline int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
659+
{
660+
return -EOPNOTSUPP;
661+
}
657662
#endif
658663

659664
static inline bool btf_type_is_struct_ptr(struct btf *btf, const struct btf_type *t)

kernel/bpf/btf.c

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8042,15 +8042,44 @@ BTF_ID_LIST_GLOBAL(btf_tracing_ids, MAX_BTF_TRACING_TYPE)
80428042
BTF_TRACING_TYPE_xxx
80438043
#undef BTF_TRACING_TYPE
80448044

8045+
/* Validate well-formedness of iter argument type.
8046+
* On success, return positive BTF ID of iter state's STRUCT type.
8047+
* On error, negative error is returned.
8048+
*/
8049+
int btf_check_iter_arg(struct btf *btf, const struct btf_type *func, int arg_idx)
8050+
{
8051+
const struct btf_param *arg;
8052+
const struct btf_type *t;
8053+
const char *name;
8054+
int btf_id;
8055+
8056+
if (btf_type_vlen(func) <= arg_idx)
8057+
return -EINVAL;
8058+
8059+
arg = &btf_params(func)[arg_idx];
8060+
t = btf_type_skip_modifiers(btf, arg->type, NULL);
8061+
if (!t || !btf_type_is_ptr(t))
8062+
return -EINVAL;
8063+
t = btf_type_skip_modifiers(btf, t->type, &btf_id);
8064+
if (!t || !__btf_type_is_struct(t))
8065+
return -EINVAL;
8066+
8067+
name = btf_name_by_offset(btf, t->name_off);
8068+
if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
8069+
return -EINVAL;
8070+
8071+
return btf_id;
8072+
}
8073+
80458074
static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
80468075
const struct btf_type *func, u32 func_flags)
80478076
{
80488077
u32 flags = func_flags & (KF_ITER_NEW | KF_ITER_NEXT | KF_ITER_DESTROY);
8049-
const char *name, *sfx, *iter_name;
8050-
const struct btf_param *arg;
8078+
const char *sfx, *iter_name;
80518079
const struct btf_type *t;
80528080
char exp_name[128];
80538081
u32 nr_args;
8082+
int btf_id;
80548083

80558084
/* exactly one of KF_ITER_{NEW,NEXT,DESTROY} can be set */
80568085
if (!flags || (flags & (flags - 1)))
@@ -8061,28 +8090,21 @@ static int btf_check_iter_kfuncs(struct btf *btf, const char *func_name,
80618090
if (nr_args < 1)
80628091
return -EINVAL;
80638092

8064-
arg = &btf_params(func)[0];
8065-
t = btf_type_skip_modifiers(btf, arg->type, NULL);
8066-
if (!t || !btf_type_is_ptr(t))
8067-
return -EINVAL;
8068-
t = btf_type_skip_modifiers(btf, t->type, NULL);
8069-
if (!t || !__btf_type_is_struct(t))
8070-
return -EINVAL;
8071-
8072-
name = btf_name_by_offset(btf, t->name_off);
8073-
if (!name || strncmp(name, ITER_PREFIX, sizeof(ITER_PREFIX) - 1))
8074-
return -EINVAL;
8093+
btf_id = btf_check_iter_arg(btf, func, 0);
8094+
if (btf_id < 0)
8095+
return btf_id;
80758096

80768097
/* sizeof(struct bpf_iter_<type>) should be a multiple of 8 to
80778098
* fit nicely in stack slots
80788099
*/
8100+
t = btf_type_by_id(btf, btf_id);
80798101
if (t->size == 0 || (t->size % 8))
80808102
return -EINVAL;
80818103

80828104
/* validate bpf_iter_<type>_{new,next,destroy}(struct bpf_iter_<type> *)
80838105
* naming pattern
80848106
*/
8085-
iter_name = name + sizeof(ITER_PREFIX) - 1;
8107+
iter_name = btf_name_by_offset(btf, t->name_off) + sizeof(ITER_PREFIX) - 1;
80868108
if (flags & KF_ITER_NEW)
80878109
sfx = "new";
80888110
else if (flags & KF_ITER_NEXT)

kernel/bpf/verifier.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7970,27 +7970,38 @@ static bool is_iter_destroy_kfunc(struct bpf_kfunc_call_arg_meta *meta)
79707970
return meta->kfunc_flags & KF_ITER_DESTROY;
79717971
}
79727972

7973-
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg)
7973+
static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg_idx,
7974+
const struct btf_param *arg)
79747975
{
79757976
/* btf_check_iter_kfuncs() guarantees that first argument of any iter
79767977
* kfunc is iter state pointer
79777978
*/
7978-
return arg == 0 && is_iter_kfunc(meta);
7979+
if (is_iter_kfunc(meta))
7980+
return arg_idx == 0;
7981+
7982+
/* iter passed as an argument to a generic kfunc */
7983+
return btf_param_match_suffix(meta->btf, arg, "__iter");
79797984
}
79807985

79817986
static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idx,
79827987
struct bpf_kfunc_call_arg_meta *meta)
79837988
{
79847989
struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
79857990
const struct btf_type *t;
7986-
const struct btf_param *arg;
7987-
int spi, err, i, nr_slots;
7988-
u32 btf_id;
7991+
int spi, err, i, nr_slots, btf_id;
79897992

7990-
/* btf_check_iter_kfuncs() ensures we don't need to validate anything here */
7991-
arg = &btf_params(meta->func_proto)[0];
7992-
t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */
7993-
t = btf_type_skip_modifiers(meta->btf, t->type, &btf_id); /* STRUCT */
7993+
/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs()
7994+
* ensures struct convention, so we wouldn't need to do any BTF
7995+
* validation here. But given iter state can be passed as a parameter
7996+
* to any kfunc, if arg has "__iter" suffix, we need to be a bit more
7997+
* conservative here.
7998+
*/
7999+
btf_id = btf_check_iter_arg(meta->btf, meta->func_proto, regno - 1);
8000+
if (btf_id < 0) {
8001+
verbose(env, "expected valid iter pointer as arg #%d\n", regno);
8002+
return -EINVAL;
8003+
}
8004+
t = btf_type_by_id(meta->btf, btf_id);
79948005
nr_slots = t->size / BPF_REG_SIZE;
79958006

79968007
if (is_iter_new_kfunc(meta)) {
@@ -8012,7 +8023,9 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
80128023
if (err)
80138024
return err;
80148025
} else {
8015-
/* iter_next() or iter_destroy() expect initialized iter state*/
8026+
/* iter_next() or iter_destroy(), as well as any kfunc
8027+
* accepting iter argument, expect initialized iter state
8028+
*/
80168029
err = is_iter_reg_valid_init(env, reg, meta->btf, btf_id, nr_slots);
80178030
switch (err) {
80188031
case 0:
@@ -11382,7 +11395,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,
1138211395
if (is_kfunc_arg_dynptr(meta->btf, &args[argno]))
1138311396
return KF_ARG_PTR_TO_DYNPTR;
1138411397

11385-
if (is_kfunc_arg_iter(meta, argno))
11398+
if (is_kfunc_arg_iter(meta, argno, &args[argno]))
1138611399
return KF_ARG_PTR_TO_ITER;
1138711400

1138811401
if (is_kfunc_arg_list_head(meta->btf, &args[argno]))

tools/testing/selftests/bpf/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ test_lru_map
88
test_lpm_map
99
test_tag
1010
FEATURE-DUMP.libbpf
11+
FEATURE-DUMP.selftests
1112
fixdep
1213
/test_progs
1314
/test_progs-no_alu32

tools/testing/selftests/bpf/Makefile

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ OPT_FLAGS ?= $(if $(RELEASE),-O2,-O0)
3333
LIBELF_CFLAGS := $(shell $(PKG_CONFIG) libelf --cflags 2>/dev/null)
3434
LIBELF_LIBS := $(shell $(PKG_CONFIG) libelf --libs 2>/dev/null || echo -lelf)
3535

36+
ifeq ($(srctree),)
37+
srctree := $(patsubst %/,%,$(dir $(CURDIR)))
38+
srctree := $(patsubst %/,%,$(dir $(srctree)))
39+
srctree := $(patsubst %/,%,$(dir $(srctree)))
40+
srctree := $(patsubst %/,%,$(dir $(srctree)))
41+
endif
42+
3643
CFLAGS += -g $(OPT_FLAGS) -rdynamic \
3744
-Wall -Werror -fno-omit-frame-pointer \
3845
$(GENFLAGS) $(SAN_CFLAGS) $(LIBELF_CFLAGS) \
@@ -60,6 +67,9 @@ progs/timer_crash.c-CFLAGS := -fno-strict-aliasing
6067
progs/test_global_func9.c-CFLAGS := -fno-strict-aliasing
6168
progs/verifier_nocsr.c-CFLAGS := -fno-strict-aliasing
6269

70+
# Some utility functions use LLVM libraries
71+
jit_disasm_helpers.c-CFLAGS = $(LLVM_CFLAGS)
72+
6373
ifneq ($(LLVM),)
6474
# Silence some warnings when compiled with clang
6575
CFLAGS += -Wno-unused-command-line-argument
@@ -168,6 +178,31 @@ endef
168178

169179
include ../lib.mk
170180

181+
NON_CHECK_FEAT_TARGETS := clean docs-clean
182+
CHECK_FEAT := $(filter-out $(NON_CHECK_FEAT_TARGETS),$(or $(MAKECMDGOALS), "none"))
183+
ifneq ($(CHECK_FEAT),)
184+
FEATURE_USER := .selftests
185+
FEATURE_TESTS := llvm
186+
FEATURE_DISPLAY := $(FEATURE_TESTS)
187+
188+
# Makefile.feature expects OUTPUT to end with a slash
189+
$(let OUTPUT,$(OUTPUT)/,\
190+
$(eval include ../../../build/Makefile.feature))
191+
endif
192+
193+
ifeq ($(feature-llvm),1)
194+
LLVM_CFLAGS += -DHAVE_LLVM_SUPPORT
195+
LLVM_CONFIG_LIB_COMPONENTS := mcdisassembler all-targets
196+
# both llvm-config and lib.mk add -D_GNU_SOURCE, which ends up as conflict
197+
LLVM_CFLAGS += $(filter-out -D_GNU_SOURCE,$(shell $(LLVM_CONFIG) --cflags))
198+
LLVM_LDLIBS += $(shell $(LLVM_CONFIG) --libs $(LLVM_CONFIG_LIB_COMPONENTS))
199+
ifeq ($(shell $(LLVM_CONFIG) --shared-mode),static)
200+
LLVM_LDLIBS += $(shell $(LLVM_CONFIG) --system-libs $(LLVM_CONFIG_LIB_COMPONENTS))
201+
LLVM_LDLIBS += -lstdc++
202+
endif
203+
LLVM_LDFLAGS += $(shell $(LLVM_CONFIG) --ldflags)
204+
endif
205+
171206
SCRATCH_DIR := $(OUTPUT)/tools
172207
BUILD_DIR := $(SCRATCH_DIR)/build
173208
INCLUDE_DIR := $(SCRATCH_DIR)/include
@@ -612,6 +647,10 @@ ifeq ($(filter clean docs-clean,$(MAKECMDGOALS)),)
612647
include $(wildcard $(TRUNNER_TEST_OBJS:.o=.d))
613648
endif
614649

650+
# add per extra obj CFGLAGS definitions
651+
$(foreach N,$(patsubst $(TRUNNER_OUTPUT)/%.o,%,$(TRUNNER_EXTRA_OBJS)), \
652+
$(eval $(TRUNNER_OUTPUT)/$(N).o: CFLAGS += $($(N).c-CFLAGS)))
653+
615654
$(TRUNNER_EXTRA_OBJS): $(TRUNNER_OUTPUT)/%.o: \
616655
%.c \
617656
$(TRUNNER_EXTRA_HDRS) \
@@ -628,6 +667,9 @@ ifneq ($2:$(OUTPUT),:$(shell pwd))
628667
$(Q)rsync -aq $$^ $(TRUNNER_OUTPUT)/
629668
endif
630669

670+
$(OUTPUT)/$(TRUNNER_BINARY): LDLIBS += $$(LLVM_LDLIBS)
671+
$(OUTPUT)/$(TRUNNER_BINARY): LDFLAGS += $$(LLVM_LDFLAGS)
672+
631673
# some X.test.o files have runtime dependencies on Y.bpf.o files
632674
$(OUTPUT)/$(TRUNNER_BINARY): | $(TRUNNER_BPF_OBJS)
633675

@@ -637,7 +679,7 @@ $(OUTPUT)/$(TRUNNER_BINARY): $(TRUNNER_TEST_OBJS) \
637679
$(TRUNNER_BPFTOOL) \
638680
| $(TRUNNER_BINARY)-extras
639681
$$(call msg,BINARY,,$$@)
640-
$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) -o $$@
682+
$(Q)$$(CC) $$(CFLAGS) $$(filter %.a %.o,$$^) $$(LDLIBS) $$(LDFLAGS) -o $$@
641683
$(Q)$(RESOLVE_BTFIDS) --btf $(TRUNNER_OUTPUT)/btf_data.bpf.o $$@
642684
$(Q)ln -sf $(if $2,..,.)/tools/build/bpftool/$(USE_BOOTSTRAP)bpftool \
643685
$(OUTPUT)/$(if $2,$2/)bpftool
@@ -656,6 +698,7 @@ TRUNNER_EXTRA_SOURCES := test_progs.c \
656698
cap_helpers.c \
657699
unpriv_helpers.c \
658700
netlink_helpers.c \
701+
jit_disasm_helpers.c \
659702
test_loader.c \
660703
xsk.c \
661704
disasm.c \
@@ -798,7 +841,8 @@ EXTRA_CLEAN := $(SCRATCH_DIR) $(HOST_SCRATCH_DIR) \
798841
$(addprefix $(OUTPUT)/,*.o *.d *.skel.h *.lskel.h *.subskel.h \
799842
no_alu32 cpuv4 bpf_gcc bpf_testmod.ko \
800843
bpf_test_no_cfi.ko \
801-
liburandom_read.so)
844+
liburandom_read.so) \
845+
$(OUTPUT)/FEATURE-DUMP.selftests
802846

803847
.PHONY: docs docs-clean
804848

tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,12 @@ bpf_testmod_test_mod_kfunc(int i)
141141

142142
__bpf_kfunc int bpf_iter_testmod_seq_new(struct bpf_iter_testmod_seq *it, s64 value, int cnt)
143143
{
144-
if (cnt < 0) {
145-
it->cnt = 0;
144+
it->cnt = cnt;
145+
146+
if (cnt < 0)
146147
return -EINVAL;
147-
}
148148

149149
it->value = value;
150-
it->cnt = cnt;
151150

152151
return 0;
153152
}
@@ -162,6 +161,14 @@ __bpf_kfunc s64 *bpf_iter_testmod_seq_next(struct bpf_iter_testmod_seq* it)
162161
return &it->value;
163162
}
164163

164+
__bpf_kfunc s64 bpf_iter_testmod_seq_value(int val, struct bpf_iter_testmod_seq* it__iter)
165+
{
166+
if (it__iter->cnt < 0)
167+
return 0;
168+
169+
return val + it__iter->value;
170+
}
171+
165172
__bpf_kfunc void bpf_iter_testmod_seq_destroy(struct bpf_iter_testmod_seq *it)
166173
{
167174
it->cnt = 0;
@@ -531,6 +538,7 @@ BTF_KFUNCS_START(bpf_testmod_common_kfunc_ids)
531538
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW)
532539
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL)
533540
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY)
541+
BTF_ID_FLAGS(func, bpf_iter_testmod_seq_value)
534542
BTF_ID_FLAGS(func, bpf_kfunc_common_test)
535543
BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)
536544
BTF_ID_FLAGS(func, bpf_testmod_ctx_create, KF_ACQUIRE | KF_RET_NULL)

0 commit comments

Comments
 (0)