]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
bpf: Make variables in bpf_prog_test_run_xdp less confusing
authorAmery Hung <ameryhung@gmail.com>
Mon, 22 Sep 2025 23:33:53 +0000 (16:33 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 17 Jan 2026 15:31:27 +0000 (16:31 +0100)
[ Upstream commit 7eb83bff02ad5e82e8c456c58717ef181c220870 ]

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>
net/bpf/test_run.c

index 6418846d6bc65e91c243595548b4516da2464654..c8b8ac6ecbc20763306178a7ab42e006930cfcfe 100644 (file)
@@ -1200,9 +1200,9 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 {
        bool do_live = (kattr->test.flags & BPF_F_TEST_XDP_LIVE_FRAMES);
        u32 tailroom = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
+       u32 retval = 0, duration, max_linear_sz, size;
+       u32 linear_sz = kattr->test.data_size_in;
        u32 batch_size = kattr->test.batch_size;
-       u32 retval = 0, duration, max_data_sz;
-       u32 size = kattr->test.data_size_in;
        u32 headroom = XDP_PACKET_HEADROOM;
        u32 repeat = kattr->test.repeat;
        struct netdev_rx_queue *rxqueue;
@@ -1239,7 +1239,7 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
 
        if (ctx) {
                /* There can't be user provided data before the meta data */
-               if (ctx->data_meta || ctx->data_end != size ||
+               if (ctx->data_meta || ctx->data_end != kattr->test.data_size_in ||
                    ctx->data > ctx->data_end ||
                    unlikely(xdp_metalen_invalid(ctx->data)) ||
                    (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,
                headroom -= ctx->data;
        }
 
-       max_data_sz = PAGE_SIZE - headroom - tailroom;
-       if (size > max_data_sz) {
-               /* disallow live data mode for jumbo frames */
-               if (do_live)
-                       goto free_ctx;
-               size = max_data_sz;
-       }
+       max_linear_sz = PAGE_SIZE - headroom - tailroom;
+       linear_sz = min_t(u32, linear_sz, max_linear_sz);
+
+       /* disallow live data mode for jumbo frames */
+       if (do_live && kattr->test.data_size_in > linear_sz)
+               goto free_ctx;
 
-       data = bpf_test_init(kattr, size, max_data_sz, headroom, tailroom);
+       data = bpf_test_init(kattr, linear_sz, max_linear_sz, headroom, tailroom);
        if (IS_ERR(data)) {
                ret = PTR_ERR(data);
                goto free_ctx;
        }
 
        rxqueue = __netif_get_rx_queue(current->nsproxy->net_ns->loopback_dev, 0);
-       rxqueue->xdp_rxq.frag_size = headroom + max_data_sz + tailroom;
+       rxqueue->xdp_rxq.frag_size = PAGE_SIZE;
        xdp_init_buff(&xdp, rxqueue->xdp_rxq.frag_size, &rxqueue->xdp_rxq);
-       xdp_prepare_buff(&xdp, data, headroom, size, true);
+       xdp_prepare_buff(&xdp, data, headroom, linear_sz, true);
        sinfo = xdp_get_shared_info_from_buff(&xdp);
 
        ret = xdp_convert_md_to_buff(ctx, &xdp);
        if (ret)
                goto free_data;
 
+       size = linear_sz;
        if (unlikely(kattr->test.data_size_in > size)) {
                void __user *data_in = u64_to_user_ptr(kattr->test.data_in);