media: stkwebcam: move stk_camera_read_reg() scratch buffer to struct stk_camera
PATCH 1/1 media: stkwebcam: move stk_camera_read_reg() scratch buffer to struct stk_camera
From: Tom Rix <[email protected]>
In stk_camera_read_reg() a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the struct stk_camera where it will be alloc-ed and freed once.
--- drivers/media/usb/stkwebcam/stk-webcam.c | 11 ++--------- drivers/media/usb/stkwebcam/stk-webcam.h | 2 ++ 2 files changed, 4 insertions(+), 9 deletions(-)
drivers/media/usb/stkwebcam/stk-webcam.c +2 -9
@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)int stk_camera_read_reg (struct stk_camera *dev,u16 index,u8 *value) {struct usb_device *udev = dev->udev; -unsigned char *buf;int ret; - buf =kmalloc (sizeof (u8),GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - ret =usb_control_msg (udev,usb_rcvctrlpipe (udev,0 ),0x00 ,USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_DEVICE ,0x00 , index, - buf, + &dev->read_reg_scratch,sizeof (u8),500 );if (ret >=0 ) - *value = *buf; - -kfree (buf); + *value = dev->read_reg_scratch;if (ret <0 )return ret;
drivers/media/usb/stkwebcam/stk-webcam.h +2 -0
@@ -105,6 +105,8 @@ struct stk_camera {struct list_head sio_avail;struct list_head sio_full;unsigned sequence; + +u8 read_reg_scratch; };#define vdev_to_camera (d) container_of(d, struct stk_camera, vdev)
Signature · 1 line
2.26.3
Hi Trix,
Quoted 27 lines · On 3/12/22 20:30, [email protected] wrote:
From: Tom Rix <[email protected]>
In
stk_camera_read_reg()a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the structstk_camerawhere it will be alloc-ed and freed once.Signed-off-by Tom Rix <[email protected]>--- drivers/media/usb/stkwebcam/stk-webcam.c | 11 ++--------- drivers/media/usb/stkwebcam/stk-webcam.h | 2 ++ 2 files changed, 4 insertions(+), 9 deletions(-)drivers/media/usb/stkwebcam/stk-webcam.c +0
-4@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)int stk_camera_read_reg (struct stk_camera *dev,u16 index,u8 *value) {struct usb_device *udev = dev->udev; -unsigned char *buf;int ret; - buf =kmalloc (sizeof (u8),GFP_KERNEL ); -if (!buf) -return -ENOMEM ;
drivers/media/usb/stkwebcam/stk-webcam.c int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value) · line 162
Quoted 10 lines
drivers/media/usb/stkwebcam/stk-webcam.c +1
-2- ret =usb_control_msg (udev,usb_rcvctrlpipe (udev,0 ),0x00 ,USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_DEVICE ,0x00 , index, - buf, + &dev->read_reg_scratch,sizeof (u8),500 );
Wouldn't it be better to move to modern USB API like usb_control_msg_recv()?
This API does not require buffer to be allocated via kmalloc(), so you will be able to use value directly.
With regards,
Pavel Skripkin
Quoted 18 lines · On 3/12/22 20:30, [email protected] wrote:
From: Tom Rix <[email protected]>
In
stk_camera_read_reg()a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the structstk_camerawhere it will be alloc-ed and freed once.Signed-off-by Tom Rix <[email protected]>--- drivers/media/usb/stkwebcam/stk-webcam.c | 11 ++--------- drivers/media/usb/stkwebcam/stk-webcam.h | 2 ++ 2 files changed, 4 insertions(+), 9 deletions(-)drivers/media/usb/stkwebcam/stk-webcam.c +0
-0
drivers/media/usb/stkwebcam/stk-webcam.c int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value) · line 150
Quoted 1 line
drivers/media/usb/stkwebcam/stk-webcam.c +0
-0@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)int stk_camera_read_reg (struct stk_camera *dev,u16 index,u8 *value)
And just random note: there are 4 possible uninit value bugs.
stk_start_stream() calls stk_camera_read_reg 4 times, but ignores return values.
stk_camera_read_reg() should have __must_check annotation and return value should be checked on each call.
If you have time you can take care of it :) Or I will fix it one day...
With regards,
Pavel Skripkin
Quoted 24 lines · Pavel Skripkin
Quoted 18 lines · On 3/12/22 20:30, [email protected] wrote:
From: Tom Rix <[email protected]>
In
stk_camera_read_reg()a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the structstk_camerawhere it will be alloc-ed and freed once.Signed-off-by Tom Rix <[email protected]>---
drivers/media/usb/stkwebcam/stk-webcam.c| 11 ++---------drivers/media/usb/stkwebcam/stk-webcam.h| 2 ++2 files changed, 4 insertions(+), 9 deletions(-)drivers/media/usb/stkwebcam/stk-webcam.c +0
-0drivers/media/usb/stkwebcam/stk-webcam.c int stk_camera_write_reg(struct stk_camera · line 151
Quoted 2 lines
drivers/media/usb/stkwebcam/stk-webcam.c +0
-0@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value) int stk_camera_read_reg(struct stk_camera *dev, u16 index, u8 *value)And just random note: there are 4 possible uninit value bugs.
Quoted 6 lines
stk_start_stream()callsstk_camera_read_reg4 times, but ignores return values.
stk_camera_read_reg()should have__must_checkannotation and return value should be checked on each call.If you have time you can take care of it :) Or I will fix it one day...
These do show up in my usual static analysis and it why I was looking at this file.
And was sidetracked by the short malloc.
Unfortunately I looked and there are many other similar instances treewide ~100
These aren't caught in checkpatch, so working on that..
Tom
Quoted 8 lines
With regards,
Pavel Skripkin
Hi Tom,
Quoted 8 lines · Tom Rix
These do show up in my usual static analysis and it why I was looking at this file.
And was sidetracked by the short malloc.
Unfortunately I looked and there are many other similar instances treewide ~100
Most of them are in very old drivers and I don't think they ever be fixed. I've looked into one bug reported by syzkaller and there was like 30 calls w/o proper error handling in one driver. Redoing whole driver logic without access to hw seems dangerous :))
Quoted 2 lines
These aren't caught in checkpatch, so working on that..
I think, it's not checkpath responsibility. Maybe it worth adding such check to smatch. I tried to implement such checker, but never finished it :(
With regards,
Pavel Skripkin
Quoted 6 lines · Pavel Skripkin
Hi Tom,
Quoted 3 lines · Tom Rix
These do show up in my usual static analysis and it why I was looking at this file.
Quoted 7 lines
Quoted 5 lines
And was sidetracked by the short malloc.
Unfortunately I looked and there are many other similar instances treewide ~100
Most of them are in very old drivers and I don't think they ever be fixed. I've looked into one bug reported by syzkaller and there was like 30 calls w/o proper error handling in one driver. Redoing whole driver logic without access to hw seems dangerous :))
From the checkpatch change below, there are about 150 dinky allocs treewide
Here is a refactoring
https://lore.kernel.org/lkml/[email protected]/
Quoted 8 lines
Quoted 4 lines
These aren't caught in checkpatch, so working on that..
I think, it's not checkpath responsibility. Maybe it worth adding such check to smatch. I tried to implement such checker, but never finished it :(
Poking new development to not do dinky allocs I think is worth it, here is my checkpatch patch
https://lore.kernel.org/lkml/[email protected]/
steal the regex for smatch.
Tom
Quoted 4 lines
With regards,
Pavel Skripkin
Hi Tom,
Quoted 6 lines · On 12/03/2022 18:30, [email protected] wrote:
From: Tom Rix <[email protected]>
In
stk_camera_read_reg()a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the structstk_camerawhere it will be alloc-ed and freed once.
I read the replies to this patch, but I am not certain if you still want this patch to be merged, or will make a v2. I have no problem applying this patch as-is, but I just want to have confirmation that there won't be a v2.
Regards,
Hans
Quoted 52 lines
Signed-off-by Tom Rix <[email protected]>--- drivers/media/usb/stkwebcam/stk-webcam.c | 11 ++--------- drivers/media/usb/stkwebcam/stk-webcam.h | 2 ++ 2 files changed, 4 insertions(+), 9 deletions(-)drivers/media/usb/stkwebcam/stk-webcam.c +2
-9@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)int stk_camera_read_reg (struct stk_camera *dev,u16 index,u8 *value) {struct usb_device *udev = dev->udev; -unsigned char *buf;int ret; - buf =kmalloc (sizeof (u8),GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - ret =usb_control_msg (udev,usb_rcvctrlpipe (udev,0 ),0x00 ,USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_DEVICE ,0x00 , index, - buf, + &dev->read_reg_scratch,sizeof (u8),500 );if (ret >=0 ) - *value = *buf; - -kfree (buf); + *value = dev->read_reg_scratch;if (ret <0 )return ret;drivers/media/usb/stkwebcam/stk-webcam.h +2
-0@@ -105,6 +105,8 @@ struct stk_camera {struct list_head sio_avail;struct list_head sio_full;unsigned sequence; + +u8 read_reg_scratch; };#define vdev_to_camera (d) container_of(d, struct stk_camera, vdev)
Quoted 5 lines · Hans Verkuil
Hi Tom,
Quoted 2 lines · On 12/03/2022 18:30, [email protected] wrote:
From: Tom Rix <[email protected]>
Quoted 7 lines
Quoted 4 lines
In
stk_camera_read_reg()a single byte buffer is alloc-ed and freed on every function call. Since the size is known, move the buffer to the structstk_camerawhere it will be alloc-ed and freed once.I read the replies to this patch, but I am not certain if you still want this patch to be merged, or will make a v2. I have no problem applying this patch as-is, but I just want to have confirmation that there won't be a v2.
Merged as-is is fine.
Thanks
Tom
Quoted 56 lines
Regards,
Hans
Quoted 51 lines
Signed-off-by Tom Rix <[email protected]>--- drivers/media/usb/stkwebcam/stk-webcam.c | 11 ++--------- drivers/media/usb/stkwebcam/stk-webcam.h | 2 ++ 2 files changed, 4 insertions(+), 9 deletions(-)drivers/media/usb/stkwebcam/stk-webcam.c +2
-9@@ -150,25 +150,18 @@ int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)int stk_camera_read_reg (struct stk_camera *dev,u16 index,u8 *value) {struct usb_device *udev = dev->udev; -unsigned char *buf;int ret; - buf =kmalloc (sizeof (u8),GFP_KERNEL ); -if (!buf) -return -ENOMEM ; - ret =usb_control_msg (udev,usb_rcvctrlpipe (udev,0 ),0x00 ,USB_DIR_IN |USB_TYPE_VENDOR |USB_RECIP_DEVICE ,0x00 , index, - buf, + &dev->read_reg_scratch,sizeof (u8),500 );if (ret >=0 ) - *value = *buf; - -kfree (buf); + *value = dev->read_reg_scratch;if (ret <0 )return ret;drivers/media/usb/stkwebcam/stk-webcam.h +2
-0@@ -105,6 +105,8 @@ struct stk_camera {struct list_head sio_avail;struct list_head sio_full;unsigned sequence; + +u8 read_reg_scratch; };#define vdev_to_camera (d) container_of(d, struct stk_camera, vdev)