]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
bpf: Refactor cleanup of bpf_prog_test_run_skb
authorPaul Chaignon <paul.chaignon@gmail.com>
Thu, 9 Oct 2025 20:11:07 +0000 (22:11 +0200)
committerMartin KaFai Lau <martin.lau@kernel.org>
Fri, 10 Oct 2025 17:43:03 +0000 (10:43 -0700)
This bit of refactoring aims to simplify how we free memory in
bpf_prog_test_run_skb to avoid code duplication.

Signed-off-by: Paul Chaignon <paul.chaignon@gmail.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Tested-by: syzbot@syzkaller.appspotmail.com
Link: https://patch.msgid.link/8971e01ae87b84f5af6b8b40defd3c310faf1c0f.1760037899.git.paul.chaignon@gmail.com
net/bpf/test_run.c

index dfb03ee0bb62a537476f4349d00300dac9859edf..a39b26739a1ef9a0c6b73b6332678d7ce53077ec 100644 (file)
@@ -990,10 +990,10 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
        u32 size = kattr->test.data_size_in;
        u32 repeat = kattr->test.repeat;
        struct __sk_buff *ctx = NULL;
+       struct sk_buff *skb = NULL;
+       struct sock *sk = NULL;
        u32 retval, duration;
        int hh_len = ETH_HLEN;
-       struct sk_buff *skb;
-       struct sock *sk;
        void *data;
        int ret;
 
@@ -1012,8 +1012,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 
        ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
        if (IS_ERR(ctx)) {
-               kfree(data);
-               return PTR_ERR(ctx);
+               ret = PTR_ERR(ctx);
+               ctx = NULL;
+               goto out;
        }
 
        switch (prog->type) {
@@ -1033,21 +1034,20 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
 
        sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
        if (!sk) {
-               kfree(data);
-               kfree(ctx);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto out;
        }
        sock_init_data(NULL, sk);
 
        skb = slab_build_skb(data);
        if (!skb) {
-               kfree(data);
-               kfree(ctx);
-               sk_free(sk);
-               return -ENOMEM;
+               ret = -ENOMEM;
+               goto out;
        }
        skb->sk = sk;
 
+       data = NULL; /* data released via kfree_skb */
+
        skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
        __skb_put(skb, size);
 
@@ -1142,7 +1142,9 @@ out:
        if (dev && dev != net->loopback_dev)
                dev_put(dev);
        kfree_skb(skb);
-       sk_free(sk);
+       kfree(data);
+       if (sk)
+               sk_free(sk);
        kfree(ctx);
        return ret;
 }