Skip to content

Commit

Permalink
hv_netvsc: Fix XDP refcnt for synthetic and VF NICs
Browse files Browse the repository at this point in the history
The caller of XDP_SETUP_PROG has already incremented refcnt in
__bpf_prog_get(), so drivers should only increment refcnt by
num_queues - 1.

To fix the issue, update netvsc_xdp_set() to add the correct number
to refcnt.

Hold a refcnt in netvsc_xdp_set()’s other caller, netvsc_attach().

And, do the same in netvsc_vf_setxdp(). Otherwise, every time when VF is
removed and added from the host side, the refcnt will be decreased by one,
which may cause page fault when unloading xdp program.

Fixes: 351e158 ("hv_netvsc: Add XDP support")
Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Haiyang Zhang authored and David S. Miller committed Feb 7, 2020
1 parent 6910fe9 commit 184367d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 11 additions & 2 deletions drivers/net/hyperv/netvsc_bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ int netvsc_xdp_set(struct net_device *dev, struct bpf_prog *prog,
}

if (prog)
bpf_prog_add(prog, nvdev->num_chn);
bpf_prog_add(prog, nvdev->num_chn - 1);

for (i = 0; i < nvdev->num_chn; i++)
rcu_assign_pointer(nvdev->chan_table[i].bpf_prog, prog);
Expand All @@ -136,6 +136,7 @@ int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog)
{
struct netdev_bpf xdp;
bpf_op_t ndo_bpf;
int ret;

ASSERT_RTNL();

Expand All @@ -148,10 +149,18 @@ int netvsc_vf_setxdp(struct net_device *vf_netdev, struct bpf_prog *prog)

memset(&xdp, 0, sizeof(xdp));

if (prog)
bpf_prog_inc(prog);

xdp.command = XDP_SETUP_PROG;
xdp.prog = prog;

return ndo_bpf(vf_netdev, &xdp);
ret = ndo_bpf(vf_netdev, &xdp);

if (ret && prog)
bpf_prog_put(prog);

return ret;
}

static u32 netvsc_xdp_query(struct netvsc_device *nvdev)
Expand Down
5 changes: 4 additions & 1 deletion drivers/net/hyperv/netvsc_drv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1059,9 +1059,12 @@ static int netvsc_attach(struct net_device *ndev,

prog = dev_info->bprog;
if (prog) {
bpf_prog_inc(prog);
ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
if (ret)
if (ret) {
bpf_prog_put(prog);
goto err1;
}
}

/* In any case device is now ready */
Expand Down

0 comments on commit 184367d

Please sign in to comment.