Skip to content

Commit 5bc2d55

Browse files
Jakub Kicinskiborkmann
authored andcommitted
bpf: offload: factor out netdev checking at allocation time
Add a helper to check if netdev could be found and whether it has .ndo_bpf callback. There is no need to check the callback every time it's invoked, ndos can't reasonably be swapped for a set without .ndp_bpf while program is loaded. bpf_dev_offload_check() will also be used by map offload. Signed-off-by: Jakub Kicinski <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 0a9c199 commit 5bc2d55

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

kernel/bpf/offload.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,19 @@
3030
static DECLARE_RWSEM(bpf_devs_lock);
3131
static LIST_HEAD(bpf_prog_offload_devs);
3232

33+
static int bpf_dev_offload_check(struct net_device *netdev)
34+
{
35+
if (!netdev)
36+
return -EINVAL;
37+
if (!netdev->netdev_ops->ndo_bpf)
38+
return -EOPNOTSUPP;
39+
return 0;
40+
}
41+
3342
int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
3443
{
3544
struct bpf_prog_offload *offload;
45+
int err;
3646

3747
if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS &&
3848
attr->prog_type != BPF_PROG_TYPE_XDP)
@@ -49,12 +59,15 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
4959

5060
offload->netdev = dev_get_by_index(current->nsproxy->net_ns,
5161
attr->prog_ifindex);
52-
if (!offload->netdev)
53-
goto err_free;
62+
err = bpf_dev_offload_check(offload->netdev);
63+
if (err)
64+
goto err_maybe_put;
5465

5566
down_write(&bpf_devs_lock);
56-
if (offload->netdev->reg_state != NETREG_REGISTERED)
67+
if (offload->netdev->reg_state != NETREG_REGISTERED) {
68+
err = -EINVAL;
5769
goto err_unlock;
70+
}
5871
prog->aux->offload = offload;
5972
list_add_tail(&offload->offloads, &bpf_prog_offload_devs);
6073
dev_put(offload->netdev);
@@ -63,10 +76,11 @@ int bpf_prog_offload_init(struct bpf_prog *prog, union bpf_attr *attr)
6376
return 0;
6477
err_unlock:
6578
up_write(&bpf_devs_lock);
66-
dev_put(offload->netdev);
67-
err_free:
79+
err_maybe_put:
80+
if (offload->netdev)
81+
dev_put(offload->netdev);
6882
kfree(offload);
69-
return -EINVAL;
83+
return err;
7084
}
7185

7286
static int __bpf_offload_ndo(struct bpf_prog *prog, enum bpf_netdev_command cmd,
@@ -80,8 +94,6 @@ static int __bpf_offload_ndo(struct bpf_prog *prog, enum bpf_netdev_command cmd,
8094
if (!offload)
8195
return -ENODEV;
8296
netdev = offload->netdev;
83-
if (!netdev->netdev_ops->ndo_bpf)
84-
return -EOPNOTSUPP;
8597

8698
data->command = cmd;
8799

0 commit comments

Comments
 (0)