Skip to content

Commit d9865de

Browse files
pchaignoKernel Patches Daemon
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. Tested-by: [email protected] Signed-off-by: Paul Chaignon <[email protected]>
1 parent ce3f403 commit d9865de

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

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

10131013
ctx = bpf_ctx_init(kattr, sizeof(struct __sk_buff));
10141014
if (IS_ERR(ctx)) {
1015-
kfree(data);
1016-
return PTR_ERR(ctx);
1015+
ret = PTR_ERR(ctx);
1016+
ctx = NULL;
1017+
goto out;
10171018
}
10181019

10191020
switch (prog->type) {
@@ -1033,24 +1034,23 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
10331034

10341035
sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
10351036
if (!sk) {
1036-
kfree(data);
1037-
kfree(ctx);
1038-
return -ENOMEM;
1037+
ret = -ENOMEM;
1038+
goto out;
10391039
}
10401040
sock_init_data(NULL, sk);
10411041

10421042
skb = slab_build_skb(data);
10431043
if (!skb) {
1044-
kfree(data);
1045-
kfree(ctx);
1046-
sk_free(sk);
1047-
return -ENOMEM;
1044+
ret = -ENOMEM;
1045+
goto out;
10481046
}
10491047
skb->sk = sk;
10501048

10511049
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
10521050
__skb_put(skb, size);
10531051

1052+
data = NULL; /* data released via kfree_skb */
1053+
10541054
if (ctx && ctx->ifindex > 1) {
10551055
dev = dev_get_by_index(net, ctx->ifindex);
10561056
if (!dev) {
@@ -1142,7 +1142,9 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
11421142
if (dev && dev != net->loopback_dev)
11431143
dev_put(dev);
11441144
kfree_skb(skb);
1145-
sk_free(sk);
1145+
kfree(data);
1146+
if (sk)
1147+
sk_free(sk);
11461148
kfree(ctx);
11471149
return ret;
11481150
}

0 commit comments

Comments
 (0)