[PATCH] bpf: use kmemdup() to replace kmalloc + memcpy

Subsystems: bpf [general] (safe dynamic programs and tools), the rest

STALE1764d LANDED

Landed in mainline as 4674f21071b9 on 2021-12-12.

2 messages, 2 authors, 2021-12-12 · open the first message on its own page

[PATCH] bpf: use kmemdup() to replace kmalloc + memcpy

From: Jiapeng Chong <hidden>
Date: 2021-12-09 06:21:45

Eliminate the follow coccicheck warning:

./kernel/bpf/btf.c:6537:13-20: WARNING opportunity for kmemdup.

Reported-by: Abaci Robot <redacted>
Signed-off-by: Jiapeng Chong <redacted>
---
 kernel/bpf/btf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index 10e7a65..94f0342 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -6534,12 +6534,11 @@ static struct bpf_cand_cache *populate_cand_cache(struct bpf_cand_cache *cands,
 		bpf_free_cands_from_cache(*cc);
 		*cc = NULL;
 	}
-	new_cands = kmalloc(sizeof_cands(cands->cnt), GFP_KERNEL);
+	new_cands = kmemdup(cands, sizeof_cands(cands->cnt), GFP_KERNEL);
 	if (!new_cands) {
 		bpf_free_cands(cands);
 		return ERR_PTR(-ENOMEM);
 	}
-	memcpy(new_cands, cands, sizeof_cands(cands->cnt));
 	/* strdup the name, since it will stay in cache.
 	 * the cands->name points to strings in prog's BTF and the prog can be unloaded.
 	 */
-- 
1.8.3.1

Re: [PATCH] bpf: use kmemdup() to replace kmalloc + memcpy

From: Alexei Starovoitov <hidden>
Date: 2021-12-12 02:16:20

On Wed, Dec 8, 2021 at 10:22 PM Jiapeng Chong
[off-list ref] wrote:
Eliminate the follow coccicheck warning:

./kernel/bpf/btf.c:6537:13-20: WARNING opportunity for kmemdup.

Reported-by: Abaci Robot <redacted>
Signed-off-by: Jiapeng Chong <redacted>
Applied. Thanks
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help