[PATCH] perf/core: fix memdup_user.cocci warnings

From: kernel test robot
Date: Wed Apr 14 2021 - 01:16:31 EST


From: kernel test robot <lkp@xxxxxxxxx>

kernel/events/core.c:5925:13-20: WARNING opportunity for memdup_user

Use memdup_user rather than duplicating its implementation
This is a little bit restricted to reduce false positives

Generated by: scripts/coccinelle/api/memdup_user.cocci

CC: Namhyung Kim <namhyung@xxxxxxxxxx>
Reported-by: kernel test robot <lkp@xxxxxxxxx>
Signed-off-by: kernel test robot <lkp@xxxxxxxxx>
---

url: https://github.com/0day-ci/linux/commits/Namhyung-Kim/perf-core-Sharing-events-with-multiple-cgroups/20210413-124251
base: https://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git cface0326a6c2ae5c8f47bd466f07624b3e348a7

core.c | 12 +++---------
1 file changed, 3 insertions(+), 9 deletions(-)

--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -5922,15 +5922,9 @@ static long _perf_ioctl(struct perf_even

cgrp_bufsz = nr_cgrps * sizeof(*cgrp_buf);

- cgrp_buf = kmalloc(cgrp_bufsz, GFP_KERNEL);
- if (cgrp_buf == NULL)
- return -ENOMEM;
-
- if (copy_from_user(cgrp_buf, (u64 __user *)(arg + 8),
- cgrp_bufsz)) {
- kfree(cgrp_buf);
- return -EFAULT;
- }
+ cgrp_buf = memdup_user((u64 __user *)(arg + 8), cgrp_bufsz);
+ if (IS_ERR(cgrp_buf))
+ return PTR_ERR(cgrp_buf);

ret = perf_event_attach_cgroup_node(event, nr_cgrps, cgrp_buf);