Skip to contentSkip to conversation

staging: greybus: camera: Switch to memdup_user_nul()

PATCH Superseded Yang Yingliang 3 participants 7 messages Apr 1, 2021

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 staging: greybus: camera: Switch to memdup_user_nul()

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
 drivers/staging/greybus/camera.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/greybus/camera.c +3 -10
index b570e13394ac..2ecdc1bc5092 100644
@@ -1120,16 +1120,9 @@ static ssize_t gb_camera_debugfs_write(struct file *file,
 	if (len > 1024)
 		return -EINVAL;
 
-	kbuf = kmalloc(len + 1, GFP_KERNEL);
-	if (!kbuf)
-		return -ENOMEM;
-
-	if (copy_from_user(kbuf, buf, len)) {
-		ret = -EFAULT;
-		goto done;
-	}
-
-	kbuf[len] = '\0';
+	kbuf = memdup_user_nul(buf, len);
+	if (IS_ERR(kbuf))
+		return PTR_ERR(kbuf);;
 
 	ret = op->execute(gcam, kbuf, len);
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 8 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
 drivers/staging/greybus/camera.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

What changed from v1?

Always put that below the --- line like the documentation asks you to. Please fix up and send a v3.

thanks,

greg k-h

#2 lore raw
Quoted 9 lines · Greg KH
Quoted 8 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
  drivers/staging/greybus/camera.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
Quoted 4 lines

What changed from v1?

Always put that below the --- line like the documentation asks you to. Please fix up and send a v3.

OK, I will add the changelog and send a v3 later

Thanks,

Yang

Quoted 5 lines

thanks,

greg k-h
.

#3 lore raw
In reply to Yang Yingliang
Quoted 20 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
 drivers/staging/greybus/camera.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/greybus/camera.c +0 -3
index b570e13394ac..2ecdc1bc5092 100644
@@ -1120,16 +1120,9 @@ static ssize_t gb_camera_debugfs_write(struct file *file,
 	if (len > 1024)
 		return -EINVAL;
 
-	kbuf = kmalloc(len + 1, GFP_KERNEL);
-	if (!kbuf)
-		return -ENOMEM;

drivers/staging/greybus/camera.c static ssize_t gb_camera_debugfs_write(struct file *file, · line 1125

Quoted 10 lines
drivers/staging/greybus/camera.c +3 -7
-
-	if (copy_from_user(kbuf, buf, len)) {
-		ret = -EFAULT;
-		goto done;
-	}
-
-	kbuf[len] = '\0';
+	kbuf = memdup_user_nul(buf, len);
+	if (IS_ERR(kbuf))
+		return PTR_ERR(kbuf);;
                                    ^^

There is an extra semi-colon here. Checkpatch actually catches this sort of typo.

regards,
dan carpenter

#4 lore raw
Quoted 32 lines · Dan Carpenter
Quoted 20 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
 drivers/staging/greybus/camera.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/greybus/camera.c +0 -3
index b570e13394ac..2ecdc1bc5092 100644
@@ -1120,16 +1120,9 @@ static ssize_t gb_camera_debugfs_write(struct file *file,
 	if (len > 1024)
 		return -EINVAL;
 
-	kbuf = kmalloc(len + 1, GFP_KERNEL);
-	if (!kbuf)
-		return -ENOMEM;

drivers/staging/greybus/camera.c static ssize_t gb_camera_debugfs_write(struct file *file, · line 1125

Quoted 10 lines
drivers/staging/greybus/camera.c +3 -7
-
-	if (copy_from_user(kbuf, buf, len)) {
-		ret = -EFAULT;
-		goto done;
-	}
-
-	kbuf[len] = '\0';
+	kbuf = memdup_user_nul(buf, len);
+	if (IS_ERR(kbuf))
+		return PTR_ERR(kbuf);;
                                    ^^
Quoted 2 lines

There is an extra semi-colon here. Checkpatch actually catches this sort of typo.

So when someone makes a typo like this, my response is:

1) Let's add this to checkpatch (turns out it was already done)
2) Let's grep the kernel and fix the other instances. The command would be something like: git grep ';;$' | grep '\.c:'

regards,
dan carpenter

#5 lore raw
Quoted 35 lines · Dan Carpenter
Quoted 32 lines · Dan Carpenter
Quoted 20 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
  drivers/staging/greybus/camera.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/greybus/camera.c +0 -3
index b570e13394ac..2ecdc1bc5092 100644
@@ -1120,16 +1120,9 @@ static ssize_t gb_camera_debugfs_write(struct file *file,
  	if (len > 1024)
  		return -EINVAL;
 
-	kbuf = kmalloc(len + 1, GFP_KERNEL);
-	if (!kbuf)
-		return -ENOMEM;

drivers/staging/greybus/camera.c static ssize_t gb_camera_debugfs_write(struct file *file, · line 1125

Quoted 10 lines
drivers/staging/greybus/camera.c +3 -7
-
-	if (copy_from_user(kbuf, buf, len)) {
-		ret = -EFAULT;
-		goto done;
-	}
-
-	kbuf[len] = '\0';
+	kbuf = memdup_user_nul(buf, len);
+	if (IS_ERR(kbuf))
+		return PTR_ERR(kbuf);;
                                     ^^
Quoted 2 lines

There is an extra semi-colon here. Checkpatch actually catches this sort of typo.

Quoted 5 lines

So when someone makes a typo like this, my response is:

1) Let's add this to checkpatch (turns out it was already done)
2) Let's grep the kernel and fix the other instances. The command would be something like: git grep ';;$' | grep '\.c:'

I search it in kernel and find some other instances like this, I can send some patches to fix these.

Thanks,
Yang

Quoted 5 lines

regards,
dan carpenter

.

#6 lore raw
In reply to Dan Carpenter
Quoted 32 lines · Dan Carpenter
Quoted 20 lines · Yang Yingliang

Use memdup_user_nul() helper instead of open-coding to simplify the code.

---
  drivers/staging/greybus/camera.c | 13 +++----------
  1 file changed, 3 insertions(+), 10 deletions(-)
drivers/staging/greybus/camera.c +0 -3
index b570e13394ac..2ecdc1bc5092 100644
@@ -1120,16 +1120,9 @@ static ssize_t gb_camera_debugfs_write(struct file *file,
  	if (len > 1024)
  		return -EINVAL;
 
-	kbuf = kmalloc(len + 1, GFP_KERNEL);
-	if (!kbuf)
-		return -ENOMEM;

drivers/staging/greybus/camera.c static ssize_t gb_camera_debugfs_write(struct file *file, · line 1125

Quoted 10 lines
drivers/staging/greybus/camera.c +3 -7
-
-	if (copy_from_user(kbuf, buf, len)) {
-		ret = -EFAULT;
-		goto done;
-	}
-
-	kbuf[len] = '\0';
+	kbuf = memdup_user_nul(buf, len);
+	if (IS_ERR(kbuf))
+		return PTR_ERR(kbuf);;
                                     ^^
Quoted 2 lines

There is an extra semi-colon here. Checkpatch actually catches this sort of typo.

I will remove it, and send a v3 later.

Thanks,

Yang

Quoted 5 lines

regards,
dan carpenter

.