[PATCH] drm/imagination: Use memdup_user() rather than duplicating its implementation

From: Markus Elfring
Date: Sun Jan 28 2024 - 15:06:47 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 28 Jan 2024 20:50:36 +0100

* Reuse existing functionality from memdup_user() instead of keeping
duplicate source code.

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

* Delete labels and statements which became unnecessary
with this refactoring.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/gpu/drm/imagination/pvr_context.c | 21 +++------------------
drivers/gpu/drm/imagination/pvr_job.c | 15 +++------------
2 files changed, 6 insertions(+), 30 deletions(-)

diff --git a/drivers/gpu/drm/imagination/pvr_context.c b/drivers/gpu/drm/imagination/pvr_context.c
index eded5e955cc0..27814ae8a8f8 100644
--- a/drivers/gpu/drm/imagination/pvr_context.c
+++ b/drivers/gpu/drm/imagination/pvr_context.c
@@ -66,29 +66,14 @@ static int
process_static_context_state(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs *cmd_defs,
u64 stream_user_ptr, u32 stream_size, void *dest)
{
- void *stream;
int err;
+ void *stream = memdup_user(u64_to_user_ptr(stream_user_ptr), stream_size);

- stream = kzalloc(stream_size, GFP_KERNEL);
- if (!stream)
- return -ENOMEM;
-
- if (copy_from_user(stream, u64_to_user_ptr(stream_user_ptr), stream_size)) {
- err = -EFAULT;
- goto err_free;
- }
+ if (IS_ERR(stream))
+ return PTR_ERR(stream);

err = pvr_stream_process(pvr_dev, cmd_defs, stream, stream_size, dest);
- if (err)
- goto err_free;
-
kfree(stream);
-
- return 0;
-
-err_free:
- kfree(stream);
-
return err;
}

diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c
index 78c2f3c6dce0..e17d53b93b1f 100644
--- a/drivers/gpu/drm/imagination/pvr_job.c
+++ b/drivers/gpu/drm/imagination/pvr_job.c
@@ -87,23 +87,14 @@ static int pvr_fw_cmd_init(struct pvr_device *pvr_dev, struct pvr_job *job,
const struct pvr_stream_cmd_defs *stream_def,
u64 stream_userptr, u32 stream_len)
{
- void *stream;
int err;
+ void *stream = memdup_user(u64_to_user_ptr(stream_userptr), stream_len);

- stream = kzalloc(stream_len, GFP_KERNEL);
- if (!stream)
- return -ENOMEM;
-
- if (copy_from_user(stream, u64_to_user_ptr(stream_userptr), stream_len)) {
- err = -EFAULT;
- goto err_free_stream;
- }
+ if (IS_ERR(stream))
+ return PTR_ERR(stream);

err = pvr_job_process_stream(pvr_dev, stream_def, stream, stream_len, job);
-
-err_free_stream:
kfree(stream);
-
return err;
}

--
2.43.0