Skip to content

Commit f2557d7

Browse files
ameryhunggregkh
authored andcommitted
net/mlx5e: RX, Fix generating skb from non-linear xdp_buff for striding RQ
[ Upstream commit 87bcef1 ] XDP programs can change the layout of an xdp_buff through bpf_xdp_adjust_tail() and bpf_xdp_adjust_head(). Therefore, the driver cannot assume the size of the linear data area nor fragments. Fix the bug in mlx5 by generating skb according to xdp_buff after XDP programs run. Currently, when handling multi-buf XDP, the mlx5 driver assumes the layout of an xdp_buff to be unchanged. That is, the linear data area continues to be empty and fragments remain the same. This may cause the driver to generate erroneous skb or triggering a kernel warning. When an XDP program added linear data through bpf_xdp_adjust_head(), the linear data will be ignored as mlx5e_build_linear_skb() builds an skb without linear data and then pull data from fragments to fill the linear data area. When an XDP program has shrunk the non-linear data through bpf_xdp_adjust_tail(), the delta passed to __pskb_pull_tail() may exceed the actual nonlinear data size and trigger the BUG_ON in it. To fix the issue, first record the original number of fragments. If the number of fragments changes after the XDP program runs, rewind the end fragment pointer by the difference and recalculate the truesize. Then, build the skb with the linear data area matching the xdp_buff. Finally, only pull data in if there is non-linear data and fill the linear part up to 256 bytes. Fixes: f52ac70 ("net/mlx5e: RX, Add XDP multi-buffer support in Striding RQ") Signed-off-by: Amery Hung <[email protected]> Signed-off-by: Tariq Toukan <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent d969645 commit f2557d7

File tree

1 file changed

+23
-3
lines changed
  • drivers/net/ethernet/mellanox/mlx5/core

1 file changed

+23
-3
lines changed

drivers/net/ethernet/mellanox/mlx5/core/en_rx.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
20172017
u32 byte_cnt = cqe_bcnt;
20182018
struct skb_shared_info *sinfo;
20192019
unsigned int truesize = 0;
2020+
u32 pg_consumed_bytes;
20202021
struct bpf_prog *prog;
20212022
struct sk_buff *skb;
20222023
u32 linear_frame_sz;
@@ -2070,7 +2071,8 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
20702071

20712072
while (byte_cnt) {
20722073
/* Non-linear mode, hence non-XSK, which always uses PAGE_SIZE. */
2073-
u32 pg_consumed_bytes = min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
2074+
pg_consumed_bytes =
2075+
min_t(u32, PAGE_SIZE - frag_offset, byte_cnt);
20742076

20752077
if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state))
20762078
truesize += pg_consumed_bytes;
@@ -2086,10 +2088,15 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
20862088
}
20872089

20882090
if (prog) {
2091+
u8 nr_frags_free, old_nr_frags = sinfo->nr_frags;
2092+
u32 len;
2093+
20892094
if (mlx5e_xdp_handle(rq, prog, mxbuf)) {
20902095
if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags)) {
20912096
struct mlx5e_frag_page *pfp;
20922097

2098+
frag_page -= old_nr_frags - sinfo->nr_frags;
2099+
20932100
for (pfp = head_page; pfp < frag_page; pfp++)
20942101
pfp->frags++;
20952102

@@ -2100,9 +2107,19 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
21002107
return NULL; /* page/packet was consumed by XDP */
21012108
}
21022109

2110+
nr_frags_free = old_nr_frags - sinfo->nr_frags;
2111+
if (unlikely(nr_frags_free)) {
2112+
frag_page -= nr_frags_free;
2113+
truesize -= (nr_frags_free - 1) * PAGE_SIZE +
2114+
ALIGN(pg_consumed_bytes,
2115+
BIT(rq->mpwqe.log_stride_sz));
2116+
}
2117+
2118+
len = mxbuf->xdp.data_end - mxbuf->xdp.data;
2119+
21032120
skb = mlx5e_build_linear_skb(
21042121
rq, mxbuf->xdp.data_hard_start, linear_frame_sz,
2105-
mxbuf->xdp.data - mxbuf->xdp.data_hard_start, 0,
2122+
mxbuf->xdp.data - mxbuf->xdp.data_hard_start, len,
21062123
mxbuf->xdp.data - mxbuf->xdp.data_meta);
21072124
if (unlikely(!skb)) {
21082125
mlx5e_page_release_fragmented(rq->page_pool,
@@ -2127,8 +2144,11 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
21272144
do
21282145
pagep->frags++;
21292146
while (++pagep < frag_page);
2147+
2148+
headlen = min_t(u16, MLX5E_RX_MAX_HEAD - len,
2149+
skb->data_len);
2150+
__pskb_pull_tail(skb, headlen);
21302151
}
2131-
__pskb_pull_tail(skb, headlen);
21322152
} else {
21332153
dma_addr_t addr;
21342154

0 commit comments

Comments
 (0)