Skip to content

Commit c93c124

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-cpumap-improve-error-propagation-in-cpu_map_update_elem'
Kohei Enju says: ==================== bpf: cpumap: improve error propagation in cpu_map_update_elem() This series improves error propagation in cpumap and adds selftests that cover the failure cases. Currently, failures returned from __cpu_map_entry_alloc() are ignored and always converted to -ENOMEM by cpu_map_update_elem(). This series ensures the correct error propagation and adds selftests. Changes: v2: - send to bpf-next, not to bpf - drop Fixes: tag v1: https://lore.kernel.org/bpf/[email protected]/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 5d9fb42 + 18352f8 commit c93c124

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

kernel/bpf/cpumap.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ static struct bpf_cpu_map_entry *
430430
__cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
431431
u32 cpu)
432432
{
433-
int numa, err, i, fd = value->bpf_prog.fd;
433+
int numa, err = -ENOMEM, i, fd = value->bpf_prog.fd;
434434
gfp_t gfp = GFP_KERNEL | __GFP_NOWARN;
435435
struct bpf_cpu_map_entry *rcpu;
436436
struct xdp_bulk_queue *bq;
@@ -440,7 +440,7 @@ __cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
440440

441441
rcpu = bpf_map_kmalloc_node(map, sizeof(*rcpu), gfp | __GFP_ZERO, numa);
442442
if (!rcpu)
443-
return NULL;
443+
return ERR_PTR(err);
444444

445445
/* Alloc percpu bulkq */
446446
rcpu->bulkq = bpf_map_alloc_percpu(map, sizeof(*rcpu->bulkq),
@@ -468,16 +468,21 @@ __cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
468468
rcpu->value.qsize = value->qsize;
469469
gro_init(&rcpu->gro);
470470

471-
if (fd > 0 && __cpu_map_load_bpf_program(rcpu, map, fd))
472-
goto free_ptr_ring;
471+
if (fd > 0) {
472+
err = __cpu_map_load_bpf_program(rcpu, map, fd);
473+
if (err)
474+
goto free_ptr_ring;
475+
}
473476

474477
/* Setup kthread */
475478
init_completion(&rcpu->kthread_running);
476479
rcpu->kthread = kthread_create_on_node(cpu_map_kthread_run, rcpu, numa,
477480
"cpumap/%d/map:%d", cpu,
478481
map->id);
479-
if (IS_ERR(rcpu->kthread))
482+
if (IS_ERR(rcpu->kthread)) {
483+
err = PTR_ERR(rcpu->kthread);
480484
goto free_prog;
485+
}
481486

482487
/* Make sure kthread runs on a single CPU */
483488
kthread_bind(rcpu->kthread, cpu);
@@ -503,7 +508,7 @@ __cpu_map_entry_alloc(struct bpf_map *map, struct bpf_cpumap_val *value,
503508
free_percpu(rcpu->bulkq);
504509
free_rcu:
505510
kfree(rcpu);
506-
return NULL;
511+
return ERR_PTR(err);
507512
}
508513

509514
static void __cpu_map_entry_free(struct work_struct *work)
@@ -596,8 +601,8 @@ static long cpu_map_update_elem(struct bpf_map *map, void *key, void *value,
596601
} else {
597602
/* Updating qsize cause re-allocation of bpf_cpu_map_entry */
598603
rcpu = __cpu_map_entry_alloc(map, &cpumap_value, key_cpu);
599-
if (!rcpu)
600-
return -ENOMEM;
604+
if (IS_ERR(rcpu))
605+
return PTR_ERR(rcpu);
601606
}
602607
rcu_read_lock();
603608
__cpu_map_entry_replace(cmap, key_cpu, rcpu);

tools/testing/selftests/bpf/prog_tests/xdp_cpumap_attach.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ static void test_xdp_with_cpumap_helpers(void)
1818
struct bpf_cpumap_val val = {
1919
.qsize = 192,
2020
};
21-
int err, prog_fd, prog_redir_fd, map_fd;
21+
int err, prog_fd, prog_redir_fd, map_fd, bad_fd;
2222
struct nstoken *nstoken = NULL;
2323
__u32 idx = 0;
2424

@@ -79,7 +79,22 @@ static void test_xdp_with_cpumap_helpers(void)
7979
val.qsize = 192;
8080
val.bpf_prog.fd = bpf_program__fd(skel->progs.xdp_dummy_prog);
8181
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
82-
ASSERT_NEQ(err, 0, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
82+
ASSERT_EQ(err, -EINVAL, "Add non-BPF_XDP_CPUMAP program to cpumap entry");
83+
84+
/* Try to attach non-BPF file descriptor */
85+
bad_fd = open("/dev/null", O_RDONLY);
86+
ASSERT_GE(bad_fd, 0, "Open /dev/null for non-BPF fd");
87+
88+
val.bpf_prog.fd = bad_fd;
89+
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
90+
ASSERT_EQ(err, -EINVAL, "Add non-BPF fd to cpumap entry");
91+
92+
/* Try to attach nonexistent file descriptor */
93+
err = close(bad_fd);
94+
ASSERT_EQ(err, 0, "Close non-BPF fd for nonexistent fd");
95+
96+
err = bpf_map_update_elem(map_fd, &idx, &val, 0);
97+
ASSERT_EQ(err, -EBADF, "Add nonexistent fd to cpumap entry");
8398

8499
/* Try to attach BPF_XDP program with frags to cpumap when we have
85100
* already loaded a BPF_XDP program on the map

0 commit comments

Comments
 (0)