Skip to content

Commit 184367d

Browse files
haiyangzdavem330
authored andcommitted
hv_netvsc: Fix XDP refcnt for synthetic and VF NICs
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 6910fe9 commit 184367d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

drivers/net/hyperv/netvsc_bpf.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ int netvsc_xdp_set(struct net_device *dev, struct bpf_prog *prog,
120120
}
121121

122122
if (prog)
123-
bpf_prog_add(prog, nvdev->num_chn);
123+
bpf_prog_add(prog, nvdev->num_chn - 1);
124124

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

140141
ASSERT_RTNL();
141142

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

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

152+
if (prog)
153+
bpf_prog_inc(prog);
154+
151155
xdp.command = XDP_SETUP_PROG;
152156
xdp.prog = prog;
153157

154-
return ndo_bpf(vf_netdev, &xdp);
158+
ret = ndo_bpf(vf_netdev, &xdp);
159+
160+
if (ret && prog)
161+
bpf_prog_put(prog);
162+
163+
return ret;
155164
}
156165

157166
static u32 netvsc_xdp_query(struct netvsc_device *nvdev)

drivers/net/hyperv/netvsc_drv.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,9 +1059,12 @@ static int netvsc_attach(struct net_device *ndev,
10591059

10601060
prog = dev_info->bprog;
10611061
if (prog) {
1062+
bpf_prog_inc(prog);
10621063
ret = netvsc_xdp_set(ndev, prog, NULL, nvdev);
1063-
if (ret)
1064+
if (ret) {
1065+
bpf_prog_put(prog);
10641066
goto err1;
1067+
}
10651068
}
10661069

10671070
/* In any case device is now ready */

0 commit comments

Comments
 (0)