Skip to contentSkip to conversation

s390/hypfs_sprp: Replace kzalloc() + copy_from_user() with memdup_user()

PATCH In review Thorsten Blum 2 participants 7 messages Sep 11, 2025 – Sep 12, 2025 s390

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 s390/hypfs_sprp: Replace kzalloc() + copy_from_user() with memdup_user()

Replace kzalloc() followed by copy_from_user() with memdup_user() to improve and simplify __hypfs_sprp_ioctl().

Return early if an error occurs instead of trying to allocate memory for 'diag304' when memory allocation for 'data' already failed.

No functional changes intended.

---
 arch/s390/hypfs/hypfs_sprp.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/s390/hypfs/hypfs_sprp.c +7 -6
index 9fc3f0dae8f0..be11634bc7db 100644
@@ -73,15 +73,16 @@ static int __hypfs_sprp_ioctl(void __user *user_area)
 	void *data;
 	int rc;
 
-	rc = -ENOMEM;
 	data = (void *)get_zeroed_page(GFP_KERNEL);
-	diag304 = kzalloc(sizeof(*diag304), GFP_KERNEL);
-	if (!data || !diag304)
-		goto out;
+	if (!data)
+		return -ENOMEM;
 
-	rc = -EFAULT;
-	if (copy_from_user(diag304, user_area, sizeof(*diag304)))
+	diag304 = memdup_user(user_area, sizeof(*diag304));
+	if (IS_ERR(diag304)) {
+		rc = PTR_ERR(diag304);
 		goto out;
+	}
+
 	rc = -EINVAL;
 	if ((diag304->args[0] >> 8) != 0 || diag304->args[1] > DIAG304_CMD_MAX)
 		goto out;
Signature · 1 line
2.51.0
maintainer
#1 lore raw
Quoted 26 lines · Thorsten Blum

Replace kzalloc() followed by copy_from_user() with memdup_user() to improve and simplify __hypfs_sprp_ioctl().

Return early if an error occurs instead of trying to allocate memory for 'diag304' when memory allocation for 'data' already failed.

No functional changes intended.

---
 arch/s390/hypfs/hypfs_sprp.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
arch/s390/hypfs/hypfs_sprp.c +0 -4
index 9fc3f0dae8f0..be11634bc7db 100644
@@ -73,15 +73,16 @@ static int __hypfs_sprp_ioctl(void __user *user_area)
 	void *data;
 	int rc;
 
-	rc = -ENOMEM;
 	data = (void *)get_zeroed_page(GFP_KERNEL);
-	diag304 = kzalloc(sizeof(*diag304), GFP_KERNEL);
-	if (!data || !diag304)
-		goto out;

arch/s390/hypfs/hypfs_sprp.c static int __hypfs_sprp_ioctl(void __user *user_area) · line 84

Quoted 10 lines
arch/s390/hypfs/hypfs_sprp.c +6 -2
+	if (!data)
+		return -ENOMEM;
 
-	rc = -EFAULT;
-	if (copy_from_user(diag304, user_area, sizeof(*diag304)))
+	diag304 = memdup_user(user_area, sizeof(*diag304));
+	if (IS_ERR(diag304)) {
+		rc = PTR_ERR(diag304);
 		goto out;
+	}

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer. Please don't send patches like this which aren't a real improvement. This is just pointless code churn, and a waste of time.

#2 lore raw
Quoted 2 lines · Heiko Carstens

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer.

Unless I'm missing something, kfree() works just fine with error pointers. See linux/slab.h:

DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))

#3 lore raw
Quoted 8 lines · Thorsten Blum
Quoted 2 lines · Heiko Carstens

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer.

Unless I'm missing something, kfree() works just fine with error pointers. See linux/slab.h:

DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))

And unless kzalloc() is required here, memdup_user() is an improvement, since it uses kmalloc() internally and avoids unnecessarily zeroing the memory before overwriting it with copy_from_user().

Thanks,
Thorsten

maintainer
#4 lore raw
Quoted 5 lines · Thorsten Blum
Quoted 4 lines · Thorsten Blum
Quoted 2 lines · Heiko Carstens

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer.

Quoted 8 lines
Quoted 4 lines

Unless I'm missing something, kfree() works just fine with error pointers. See linux/slab.h:

DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))

And unless kzalloc() is required here, memdup_user() is an improvement, since it uses kmalloc() internally and avoids unnecessarily zeroing the memory before overwriting it with copy_from_user().

Again, there are gazillions of similar trivial cleanup patches possible. The additional zeroing in this case doesn't matter at all, since this code is executed once a year.

Reviewing and integrating such patches could keep us busy all day long. So, please don't send such patches.

Patches which fix (potential) bugs are welcome however.

maintainer
#5 lore raw
In reply to Thorsten Blum
Quoted 8 lines · Thorsten Blum
Quoted 2 lines · Heiko Carstens

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer.

Unless I'm missing something, kfree() works just fine with error pointers. See linux/slab.h:

DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))

This is for automatically freed memory, but not for the generic case. See cd7eb8f83fcf ("mm/slab: make __free(kfree) accept error pointers").

#6 lore raw
Quoted 5 lines · Heiko Carstens
Quoted 4 lines · Thorsten Blum
Quoted 2 lines · Heiko Carstens

This is not an improvement and also incorrect, since kfree() may now be called with an error pointer.

Quoted 7 lines
Quoted 4 lines

Unless I'm missing something, kfree() works just fine with error pointers. See linux/slab.h:

DEFINE_FREE(kfree, void *, if (!IS_ERR_OR_NULL(_T)) kfree(_T))

This is for automatically freed memory, but not for the generic case. See cd7eb8f83fcf ("mm/slab: make __free(kfree) accept error pointers").

Interesting, I didn't know this doesn't work for the generic kfree().

Thanks,
Thorsten