Skip to content

Commit 0eb6e9d

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: e558cca ("bpf, test_run: Subtract size of xdp_frame from allowed metadata size") Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 202c5b9 commit 0eb6e9d

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
@@ -1200,9 +1200,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12001200
{
12011201
bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
12021202
u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
1203+
u32 retval = 0, duration, max_linear_sz, size;
1204+
u32 linear_sz = kattr->test.data_size_in;
12031205
u32 batch_size = kattr->test.batch_size;
1204-
u32 retval = 0, duration, max_data_sz;
1205-
u32 size = kattr->test.data_size_in;
12061206
u32 headroom = XDP_PACKET_HEADROOM;
12071207
u32 repeat = kattr->test.repeat;
12081208
struct netdev_rx_queue *rxqueue;
@@ -1239,7 +1239,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12391239

12401240
if (ctx) {
12411241
/* There can't be user provided data before the meta data */
1242-
if (ctx->data_meta || ctx->data_end != size ||
1242+
if (ctx->data_meta || ctx->data_end != kattr->test.data_size_in ||
12431243
ctx->data > ctx->data_end ||
12441244
unlikely(xdp_metalen_invalid(ctx->data)) ||
12451245
(do_live && (kattr->test.data_out || kattr->test.ctx_out)))
@@ -1248,30 +1248,30 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
12481248
headroom -= ctx->data;
12491249
}
12501250

1251-
max_data_sz = PAGE_SIZE - headroom - tailroom;
1252-
if (size > max_data_sz) {
1253-
/* disallow live data mode for jumbo frames */
1254-
if (do_live)
1255-
goto free_ctx;
1256-
size = max_data_sz;
1257-
}
1251+
max_linear_sz = PAGE_SIZE - headroom - tailroom;
1252+
linear_sz = min_t(u32, linear_sz, max_linear_sz);
1253+
1254+
/* disallow live data mode for jumbo frames */
1255+
if (do_live && kattr->test.data_size_in > linear_sz)
1256+
goto free_ctx;
12581257

1259-
data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
1258+
data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
12601259
if (IS_ERR(data)) {
12611260
ret = PTR_ERR(data);
12621261
goto free_ctx;
12631262
}
12641263

12651264
rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
1266-
rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
1265+
rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
12671266
xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
1268-
xdp_prepare_buff(&xdp, data, headroom, size, true);
1267+
xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
12691268
sinfo = xdp_get_shared_info_from_buff(&xdp);
12701269

12711270
ret = xdp_convert_md_to_buff(ctx, &xdp);
12721271
if (ret)
12731272
goto free_data;
12741273

1274+
size = linear_sz;
12751275
if (unlikely(kattr->test.data_size_in > size)) {
12761276
void __user *data_in = u64_to_user_ptr(kattr->test.data_in);
12771277

0 commit comments

Comments
 (0)