[PATCH] s390/cio: Use memdup_user() rather than duplicating its implementation

From: Markus Elfring
Date: Mon Jan 29 2024 - 09:21:16 EST


From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
Date: Mon, 29 Jan 2024 15:00:36 +0100

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

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

* Use another label in six function implementations.

Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
---
drivers/s390/cio/chsc_sch.c | 90 +++++++++++++++++--------------------
1 file changed, 42 insertions(+), 48 deletions(-)

diff --git a/drivers/s390/cio/chsc_sch.c b/drivers/s390/cio/chsc_sch.c
index 902237d0baef..2ad4264589d9 100644
--- a/drivers/s390/cio/chsc_sch.c
+++ b/drivers/s390/cio/chsc_sch.c
@@ -442,15 +442,13 @@ static int chsc_ioctl_info_channel_path(void __user *user_cd)
scpcd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!scpcd_area)
return -ENOMEM;
- cd = kzalloc(sizeof(*cd), GFP_KERNEL);
- if (!cd) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(cd, user_cd, sizeof(*cd))) {
- ret = -EFAULT;
- goto out_free;
+
+ cd = memdup_user(user_cd, sizeof(*cd));
+ if (IS_ERR(cd)) {
+ ret = PTR_ERR(cd);
+ goto out_free_page;
}
+
scpcd_area->request.length = 0x0010;
scpcd_area->request.code = 0x0028;
scpcd_area->m = cd->m;
@@ -477,6 +475,7 @@ static int chsc_ioctl_info_channel_path(void __user *user_cd)
ret = 0;
out_free:
kfree(cd);
+out_free_page:
free_page((unsigned long)scpcd_area);
return ret;
}
@@ -504,15 +503,13 @@ static int chsc_ioctl_info_cu(void __user *user_cd)
scucd_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!scucd_area)
return -ENOMEM;
- cd = kzalloc(sizeof(*cd), GFP_KERNEL);
- if (!cd) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(cd, user_cd, sizeof(*cd))) {
- ret = -EFAULT;
- goto out_free;
+
+ cd = memdup_user(user_cd, sizeof(*cd));
+ if (IS_ERR(cd)) {
+ ret = PTR_ERR(cd);
+ goto out_free_page;
}
+
scucd_area->request.length = 0x0010;
scucd_area->request.code = 0x0026;
scucd_area->m = cd->m;
@@ -539,6 +536,7 @@ static int chsc_ioctl_info_cu(void __user *user_cd)
ret = 0;
out_free:
kfree(cd);
+out_free_page:
free_page((unsigned long)scucd_area);
return ret;
}
@@ -567,15 +565,13 @@ static int chsc_ioctl_info_sch_cu(void __user *user_cud)
sscud_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!sscud_area)
return -ENOMEM;
- cud = kzalloc(sizeof(*cud), GFP_KERNEL);
- if (!cud) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(cud, user_cud, sizeof(*cud))) {
- ret = -EFAULT;
- goto out_free;
+
+ cud = memdup_user(user_cud, sizeof(*cud));
+ if (IS_ERR(cud)) {
+ ret = PTR_ERR(cud);
+ goto out_free_page;
}
+
sscud_area->request.length = 0x0010;
sscud_area->request.code = 0x0006;
sscud_area->m = cud->schid.m;
@@ -603,6 +599,7 @@ static int chsc_ioctl_info_sch_cu(void __user *user_cud)
ret = 0;
out_free:
kfree(cud);
+out_free_page:
free_page((unsigned long)sscud_area);
return ret;
}
@@ -629,15 +626,13 @@ static int chsc_ioctl_conf_info(void __user *user_ci)
sci_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!sci_area)
return -ENOMEM;
- ci = kzalloc(sizeof(*ci), GFP_KERNEL);
- if (!ci) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(ci, user_ci, sizeof(*ci))) {
- ret = -EFAULT;
- goto out_free;
+
+ ci = memdup_user(user_ci, sizeof(*ci));
+ if (IS_ERR(ci)) {
+ ret = PTR_ERR(ci);
+ goto out_free_page;
}
+
sci_area->request.length = 0x0010;
sci_area->request.code = 0x0012;
sci_area->m = ci->id.m;
@@ -663,6 +658,7 @@ static int chsc_ioctl_conf_info(void __user *user_ci)
ret = 0;
out_free:
kfree(ci);
+out_free_page:
free_page((unsigned long)sci_area);
return ret;
}
@@ -700,15 +696,13 @@ static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
sccl_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!sccl_area)
return -ENOMEM;
- ccl = kzalloc(sizeof(*ccl), GFP_KERNEL);
- if (!ccl) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(ccl, user_ccl, sizeof(*ccl))) {
- ret = -EFAULT;
- goto out_free;
+
+ ccl = memdup_user(user_ccl, sizeof(*ccl));
+ if (IS_ERR(ccl)) {
+ ret = PTR_ERR(ccl);
+ goto out_free_page;
}
+
sccl_area->request.length = 0x0020;
sccl_area->request.code = 0x0030;
sccl_area->fmt = ccl->req.fmt;
@@ -746,6 +740,7 @@ static int chsc_ioctl_conf_comp_list(void __user *user_ccl)
ret = 0;
out_free:
kfree(ccl);
+out_free_page:
free_page((unsigned long)sccl_area);
return ret;
}
@@ -800,15 +795,13 @@ static int chsc_ioctl_dcal(void __user *user_dcal)
sdcal_area = (void *)get_zeroed_page(GFP_KERNEL | GFP_DMA);
if (!sdcal_area)
return -ENOMEM;
- dcal = kzalloc(sizeof(*dcal), GFP_KERNEL);
- if (!dcal) {
- ret = -ENOMEM;
- goto out_free;
- }
- if (copy_from_user(dcal, user_dcal, sizeof(*dcal))) {
- ret = -EFAULT;
- goto out_free;
+
+ dcal = memdup_user(user_dcal, sizeof(*dcal));
+ if (IS_ERR(dcal)) {
+ ret = PTR_ERR(dcal);
+ goto out_free_page;
}
+
sdcal_area->request.length = 0x0020;
sdcal_area->request.code = 0x0034;
sdcal_area->atype = dcal->req.atype;
@@ -835,6 +828,7 @@ static int chsc_ioctl_dcal(void __user *user_dcal)
ret = 0;
out_free:
kfree(dcal);
+out_free_page:
free_page((unsigned long)sdcal_area);
return ret;
}
--
2.43.0