Skip to content

Commit 031b421

Browse files
pchaignoguidosarducci
authored andcommitted
bpf: Refactor cleanup of bpf_prog_test_run_skb
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 <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Tested-by: [email protected] Link: https://patch.msgid.link/8971e01ae87b84f5af6b8b40defd3c310faf1c0f.1760037899.git.paul.chaignon@gmail.com
1 parent 4f3dce5 commit 031b421

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

net/bpf/test_run.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -979,10 +979,10 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
979979
u32 size = kattr->test.data_size_in;
980980
u32 repeat = kattr->test.repeat;
981981
struct __sk_buff *ctx = NULL;
982+
struct sk_buff *skb = NULL;
983+
struct sock *sk = NULL;
982984
u32 retval, duration;
983985
int hh_len = ETH_HLEN;
984-
struct sk_buff *skb;
985-
struct sock *sk;
986986
void *data;
987987
int ret;
988988

@@ -1001,8 +1001,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10011001

10021002
ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
10031003
if (IS_ERR(ctx)) {
1004-
kfree(data);
1005-
return PTR_ERR(ctx);
1004+
ret = PTR_ERR(ctx);
1005+
ctx = NULL;
1006+
goto out;
10061007
}
10071008

10081009
switch (prog->type) {
@@ -1022,21 +1023,20 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10221023

10231024
sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
10241025
if (!sk) {
1025-
kfree(data);
1026-
kfree(ctx);
1027-
return -ENOMEM;
1026+
ret = -ENOMEM;
1027+
goto out;
10281028
}
10291029
sock_init_data(NULL, sk);
10301030

10311031
skb = slab_build_skb(data);
10321032
if (!skb) {
1033-
kfree(data);
1034-
kfree(ctx);
1035-
sk_free(sk);
1036-
return -ENOMEM;
1033+
ret = -ENOMEM;
1034+
goto out;
10371035
}
10381036
skb->sk = sk;
10391037

1038+
data = NULL; /* data released via kfree_skb */
1039+
10401040
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
10411041
__skb_put(skb, size);
10421042

@@ -1131,7 +1131,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
11311131
if (dev && dev != net->loopback_dev)
11321132
dev_put(dev);
11331133
kfree_skb(skb);
1134-
sk_free(sk);
1134+
kfree(data);
1135+
if (sk)
1136+
sk_free(sk);
11351137
kfree(ctx);
11361138
return ret;
11371139
}

0 commit comments

Comments
 (0)