Skip to content

Commit a6e38bb

Browse files
pchaignoKernel Patches Daemon
authored andcommitted
bpf: Refactor cleanup of bpf_prog_test_run_skb
This bit of refactoring aims to simplify the next patch in this series, in which freeing 'data' is a bit less straightforward. Tested-by: [email protected] Signed-off-by: Paul Chaignon <[email protected]>
1 parent cd9a59e commit a6e38bb

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
@@ -990,10 +990,10 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
990990
u32 size = kattr->test.data_size_in;
991991
u32 repeat = kattr->test.repeat;
992992
struct __sk_buff *ctx = NULL;
993+
struct sk_buff *skb = NULL;
994+
struct sock *sk = NULL;
993995
u32 retval, duration;
994996
int hh_len = ETH_HLEN;
995-
struct sk_buff *skb;
996-
struct sock *sk;
997997
void *data;
998998
int ret;
999999

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

10101010
ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
10111011
if (IS_ERR(ctx)) {
1012-
kfree(data);
1013-
return PTR_ERR(ctx);
1012+
ret = PTR_ERR(ctx);
1013+
ctx = NULL;
1014+
goto out;
10141015
}
10151016

10161017
switch (prog->type) {
@@ -1030,24 +1031,23 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10301031

10311032
sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
10321033
if (!sk) {
1033-
kfree(data);
1034-
kfree(ctx);
1035-
return -ENOMEM;
1034+
ret = -ENOMEM;
1035+
goto out;
10361036
}
10371037
sock_init_data(NULL, sk);
10381038

10391039
skb = slab_build_skb(data);
10401040
if (!skb) {
1041-
kfree(data);
1042-
kfree(ctx);
1043-
sk_free(sk);
1044-
return -ENOMEM;
1041+
ret = -ENOMEM;
1042+
goto out;
10451043
}
10461044
skb->sk = sk;
10471045

10481046
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
10491047
__skb_put(skb, size);
10501048

1049+
data = NULL; /* data released via kfree_skb */
1050+
10511051
if (ctx && ctx->ifindex > 1) {
10521052
dev = dev_get_by_index(net, ctx->ifindex);
10531053
if (!dev) {
@@ -1139,7 +1139,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
11391139
if (dev && dev != net->loopback_dev)
11401140
dev_put(dev);
11411141
kfree_skb(skb);
1142-
sk_free(sk);
1142+
kfree(data);
1143+
if (sk)
1144+
sk_free(sk);
11431145
kfree(ctx);
11441146
return ret;
11451147
}

0 commit comments

Comments
 (0)