Skip to content

Commit 875d7cb

Browse files
ameryhunggregkh
authored andcommitted
bpf: Make variables in bpf_prog_test_run_xdp less confusing
[ Upstream commit 7eb83bf ] Change the variable naming in bpf_prog_test_run_xdp() to make the overall logic less confusing. As different modes were added to the function over the time, some variables got overloaded, making it hard to understand and changing the code becomes error-prone. Replace "size" with "linear_sz" where it refers to the size of metadata and data. If "size" refers to input data size, use test.data_size_in directly. Replace "max_data_sz" with "max_linear_sz" to better reflect the fact that it is the maximum size of metadata and data (i.e., linear_sz). Also, xdp_rxq.frags_size is always PAGE_SIZE, so just set it directly instead of subtracting headroom and tailroom and adding them back. Signed-off-by: Amery Hung <ameryhung@gmail.com> Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org> Link: https://patch.msgid.link/20250922233356.3356453-6-ameryhung@gmail.com Stable-dep-of: e558cca21779 ("bpf, test_run: Subtract size of xdp_frame from allowed metadata size") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent a60c827 commit 875d7cb

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

net/bpf/test_run.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11441144
{
11451145
bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
11461146
u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1147+
u32 retval = 0, duration, max_linear_sz, size;
1148+
u32 linear_sz = kattr->test.data_size_in;
11471149
u32 batch_size = kattr->test.batch_size;
1148-
u32 retval = 0, duration, max_data_sz;
1149-
u32 size = kattr->test.data_size_in;
11501150
u32 headroom = XDP_PACKET_HEADROOM;
11511151
u32 repeat = kattr->test.repeat;
11521152
struct netdev_rx_queue *rxqueue;
@@ -1183,7 +1183,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11831183

11841184
if (ctx) {
11851185
/* There can't be user provided data before the meta data */
1186-
if (ctx->data_meta || ctx->data_end != size ||
1186+
if (ctx->data_meta || ctx->data_end != kattr->test.data_size_in ||
11871187
ctx->data > ctx->data_end ||
11881188
unlikely(xdp_metalen_invalid(ctx->data)) ||
11891189
(do_live && (kattr->test.data_out || kattr->test.ctx_out)))
@@ -1192,30 +1192,30 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11921192
headroom -= ctx->data;
11931193
}
11941194

1195-
max_data_sz = PAGE_SIZE - headroom - tailroom;
1196-
if (size > max_data_sz) {
1197-
/* disallow live data mode for jumbo frames */
1198-
if (do_live)
1199-
goto free_ctx;
1200-
size = max_data_sz;
1201-
}
1195+
max_linear_sz = PAGE_SIZE - headroom - tailroom;
1196+
linear_sz = min_t(u32, linear_sz, max_linear_sz);
1197+
1198+
/* disallow live data mode for jumbo frames */
1199+
if (do_live && kattr->test.data_size_in > linear_sz)
1200+
goto free_ctx;
12021201

1203-
data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
1202+
data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
12041203
if (IS_ERR(data)) {
12051204
ret = PTR_ERR(data);
12061205
goto free_ctx;
12071206
}
12081207

12091208
rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
1210-
rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
1209+
rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
12111210
xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1212-
xdp_prepare_buff(&xdp, data, headroom, size, true);
1211+
xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
12131212
sinfo = xdp_get_shared_info_from_buff(&xdp);
12141213

12151214
ret = xdp_convert_md_to_buff(ctx, &xdp);
12161215
if (ret)
12171216
goto free_data;
12181217

1218+
size = linear_sz;
12191219
if (unlikely(kattr->test.data_size_in > size)) {
12201220
void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
12211221

0 commit comments

Comments
 (0)