Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/linux/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,13 @@ int map_check_no_btf(const struct bpf_map *map,
bool bpf_map_meta_equal(const struct bpf_map *meta0,
const struct bpf_map *meta1);

static inline bool bpf_map_has_internal_structs(struct bpf_map *map)
{
return btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK);
}

void bpf_map_free_internal_structs(struct bpf_map *map, void *obj);

extern const struct bpf_map_ops bpf_map_offload_ops;

/* bpf_type_flag contains a set of flags that are applicable to the values of
Expand Down
19 changes: 6 additions & 13 deletions kernel/bpf/arraymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,19 +448,12 @@ static void array_map_free_internal_structs(struct bpf_map *map)
struct bpf_array *array = container_of(map, struct bpf_array, map);
int i;

/* We don't reset or free fields other than timer and workqueue
* on uref dropping to zero.
*/
if (btf_record_has_field(map->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
for (i = 0; i < array->map.max_entries; i++) {
if (btf_record_has_field(map->record, BPF_TIMER))
bpf_obj_free_timer(map->record, array_map_elem_ptr(array, i));
if (btf_record_has_field(map->record, BPF_WORKQUEUE))
bpf_obj_free_workqueue(map->record, array_map_elem_ptr(array, i));
if (btf_record_has_field(map->record, BPF_TASK_WORK))
bpf_obj_free_task_work(map->record, array_map_elem_ptr(array, i));
}
}
/* We only free internal structs on uref dropping to zero */
if (!bpf_map_has_internal_structs(map))
return;

for (i = 0; i < array->map.max_entries; i++)
bpf_map_free_internal_structs(map, array_map_elem_ptr(array, i));
}

/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
Expand Down
36 changes: 13 additions & 23 deletions kernel/bpf/hashtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,6 @@ static bool htab_has_extra_elems(struct bpf_htab *htab)
return !htab_is_percpu(htab) && !htab_is_lru(htab) && !is_fd_htab(htab);
}

static void htab_free_internal_structs(struct bpf_htab *htab, struct htab_elem *elem)
{
if (btf_record_has_field(htab->map.record, BPF_TIMER))
bpf_obj_free_timer(htab->map.record,
htab_elem_value(elem, htab->map.key_size));
if (btf_record_has_field(htab->map.record, BPF_WORKQUEUE))
bpf_obj_free_workqueue(htab->map.record,
htab_elem_value(elem, htab->map.key_size));
if (btf_record_has_field(htab->map.record, BPF_TASK_WORK))
bpf_obj_free_task_work(htab->map.record,
htab_elem_value(elem, htab->map.key_size));
}

static void htab_free_prealloced_internal_structs(struct bpf_htab *htab)
{
u32 num_entries = htab->map.max_entries;
Expand All @@ -240,7 +227,8 @@ static void htab_free_prealloced_internal_structs(struct bpf_htab *htab)
struct htab_elem *elem;

elem = get_htab_elem(htab, i);
htab_free_internal_structs(htab, elem);
bpf_map_free_internal_structs(&htab->map,
htab_elem_value(elem, htab->map.key_size));
cond_resched();
}
}
Expand Down Expand Up @@ -1509,8 +1497,9 @@ static void htab_free_malloced_internal_structs(struct bpf_htab *htab)
struct htab_elem *l;

hlist_nulls_for_each_entry(l, n, head, hash_node) {
/* We only free timer on uref dropping to zero */
htab_free_internal_structs(htab, l);
/* We only free internal structs on uref dropping to zero */
bpf_map_free_internal_structs(&htab->map,
htab_elem_value(l, htab->map.key_size));
}
cond_resched_rcu();
}
Expand All @@ -1521,13 +1510,14 @@ static void htab_map_free_internal_structs(struct bpf_map *map)
{
struct bpf_htab *htab = container_of(map, struct bpf_htab, map);

/* We only free timer and workqueue on uref dropping to zero */
if (btf_record_has_field(htab->map.record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
if (!htab_is_prealloc(htab))
htab_free_malloced_internal_structs(htab);
else
htab_free_prealloced_internal_structs(htab);
}
/* We only free internal structs on uref dropping to zero */
if (!bpf_map_has_internal_structs(map))
return;

if (htab_is_prealloc(htab))
htab_free_prealloced_internal_structs(htab);
else
htab_free_malloced_internal_structs(htab);
}

/* Called when map->refcnt goes to zero, either from workqueue or from syscall */
Expand Down
10 changes: 10 additions & 0 deletions kernel/bpf/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4431,3 +4431,13 @@ void *__bpf_dynptr_data_rw(const struct bpf_dynptr_kern *ptr, u32 len)
return NULL;
return (void *)__bpf_dynptr_data(ptr, len);
}

void bpf_map_free_internal_structs(struct bpf_map *map, void *val)
{
if (btf_record_has_field(map->record, BPF_TIMER))
bpf_obj_free_timer(map->record, val);
if (btf_record_has_field(map->record, BPF_WORKQUEUE))
bpf_obj_free_workqueue(map->record, val);
if (btf_record_has_field(map->record, BPF_TASK_WORK))
bpf_obj_free_task_work(map->record, val);
}
17 changes: 12 additions & 5 deletions kernel/bpf/verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -8464,6 +8464,9 @@ static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno,
case BPF_TASK_WORK:
field_off = map->record->task_work_off;
break;
case BPF_WORKQUEUE:
field_off = map->record->wq_off;
break;
default:
verifier_bug(env, "unsupported BTF field type: %s\n", struct_name);
return -EINVAL;
Expand Down Expand Up @@ -8505,13 +8508,17 @@ static int process_wq_func(struct bpf_verifier_env *env, int regno,
{
struct bpf_reg_state *regs = cur_regs(env), *reg = &regs[regno];
struct bpf_map *map = reg->map_ptr;
u64 val = reg->var_off.value;
int err;

if (map->record->wq_off != val + reg->off) {
verbose(env, "off %lld doesn't point to 'struct bpf_wq' that is at %d\n",
val + reg->off, map->record->wq_off);
return -EINVAL;
err = check_map_field_pointer(env, regno, BPF_WORKQUEUE);
if (err)
return err;

if (meta->map.ptr) {
verifier_bug(env, "Two map pointers in a bpf_wq helper");
return -EFAULT;
}

meta->map.uid = reg->map_uid;
meta->map.ptr = map;
return 0;
Expand Down
56 changes: 56 additions & 0 deletions tools/testing/selftests/bpf/prog_tests/wq.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,59 @@ void serial_test_failures_wq(void)
{
RUN_TESTS(wq_failures);
}

static void test_failure_map_no_btf(void)
{
struct wq *skel = NULL;
char log[8192];
const struct bpf_insn *insns;
size_t insn_cnt;
int ret, err, map_fd;
LIBBPF_OPTS(bpf_prog_load_opts, opts, .log_size = sizeof(log), .log_buf = log,
.log_level = 2);

skel = wq__open();
if (!ASSERT_OK_PTR(skel, "skel_open"))
return;

err = bpf_object__prepare(skel->obj);
if (!ASSERT_OK(err, "skel__prepare"))
goto out;

map_fd = bpf_map_create(BPF_MAP_TYPE_ARRAY, "map_no_btf", sizeof(__u32), sizeof(__u64), 100,
NULL);
if (!ASSERT_GT(map_fd, -1, "map create"))
goto out;

err = bpf_map__reuse_fd(skel->maps.array, map_fd);
if (!ASSERT_OK(err, "map reuse fd")) {
close(map_fd);
goto out;
}

insns = bpf_program__insns(skel->progs.test_map_no_btf);
if (!ASSERT_OK_PTR(insns, "insns ptr"))
goto out;

insn_cnt = bpf_program__insn_cnt(skel->progs.test_map_no_btf);
if (!ASSERT_GT(insn_cnt, 0u, "insn cnt"))
goto out;

ret = bpf_prog_load(BPF_PROG_TYPE_TRACEPOINT, NULL, "GPL", insns, insn_cnt, &opts);
if (!ASSERT_LT(ret, 0, "prog load failed")) {
if (ret > 0)
close(ret);
goto out;
}

ASSERT_HAS_SUBSTR(log, "map 'map_no_btf' has to have BTF in order to use bpf_wq",
"log complains no map BTF");
out:
wq__destroy(skel);
}

void test_wq_custom(void)
{
if (test__start_subtest("test_failure_map_no_btf"))
test_failure_map_no_btf();
}
17 changes: 17 additions & 0 deletions tools/testing/selftests/bpf/progs/wq.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,20 @@ long test_call_lru_sleepable(void *ctx)

return test_elem_callback(&lru, &key, wq_callback);
}

SEC("tc")
long test_map_no_btf(void *ctx)
{
struct elem *val;
struct bpf_wq *wq;
int key = 42;

val = bpf_map_lookup_elem(&array, &key);
if (!val)
return -2;

wq = &val->w;
if (bpf_wq_init(wq, &array, 0) != 0)
return -3;
return 0;
}
23 changes: 23 additions & 0 deletions tools/testing/selftests/bpf/progs/wq_failures.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,26 @@ long test_wrong_wq_pointer_offset(void *ctx)

return -22;
}

SEC("tc")
__log_level(2)
__failure
__msg(": (85) call bpf_wq_init#")
__msg("R1 doesn't have constant offset. bpf_wq has to be at the constant offset")
long test_bad_wq_off(void *ctx)
{
struct elem *val;
struct bpf_wq *wq;
int key = 42;
u64 unknown;

val = bpf_map_lookup_elem(&array, &key);
if (!val)
return -2;

unknown = bpf_get_prandom_u32();
wq = &val->w + unknown;
if (bpf_wq_init(wq, &array, 0) != 0)
return -3;
return 0;
}
Loading