media: Fix warnings for smatch and sparse
Subsystems (7)
cover media: Fix warnings for smatch and sparse
---
Ricardo Ribalda (6):
media: usb: siano: Fix allocation of urbs
media: cxd2880: Replaze kmalloc with kzalloc
media: platform: sti: hva: clk_unprepare unconditionally
media: v4l2-ctrls-core.c: Do not use iterator outside loop
media: adv7180: Only request valids IRQs
media: touchscreen: sur40: convert le16 to cpu before use
drivers/input/touchscreen/sur40.c | 2 +- drivers/media/i2c/adv7180.c | 2 +- drivers/media/platform/st/sti/hva/hva-hw.c | 3 +-- drivers/media/spi/cxd2880-spi.c | 2 +- drivers/media/usb/siano/smsusb.c | 28 +++++++++++++++++-------- drivers/media/v4l2-core/v4l2-ctrls-api.c | 33 +++++++++++++++++------------- 6 files changed, 43 insertions(+), 27 deletions(-)
---
base-commit: 34d7bf1c8e59f5fbf438ee32c96389ebe41ca2e8
change-id: 20240410-smatch-8f235d50753d
Best regards,
Signature · 1 line
Ricardo Ribalda <[email protected]>
PATCH 1/6 media: usb: siano: Fix allocation of urbs
USB urbs must be allocated with usb_alloc_urb. Quoting the manual
Only use this function (usb_init_urb) if you _really_ understand what you are doing.
Fix the following smatch error:
drivers/media/usb/siano/smsusb.c:53:38: warning: array of flexible structures
--- drivers/media/usb/siano/smsusb.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)
drivers/media/usb/siano/smsusb.c +20 -8
@@ -40,7 +40,7 @@ struct smsusb_urb_t {struct smscore_buffer_t *cb;struct smsusb_device_t *dev; -struct urb urb; +struct urb *urb;/* For the bottom half */ struct work_struct wq;
@@ -160,7 +160,7 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, } usb_fill_bulk_urb( - &surb->urb, + surb->urb, dev->udev, usb_rcvbulkpipe(dev->udev, dev->in_ep), surb->cb->p,
@@ -168,9 +168,9 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, smsusb_onresponse, surb ); - surb->urb.transfer_flags |=URB_FREE_BUFFER ; + surb->urb->transfer_flags |=URB_FREE_BUFFER ; -return usb_submit_urb (&surb->urb,GFP_ATOMIC ); +return usb_submit_urb (surb->urb,GFP_ATOMIC ); }static void smsusb_stop_streaming (struct smsusb_device_t *dev)
@@ -178,7 +178,7 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)int i;for (i =0 ; i <MAX_URBS ; i++) { -usb_kill_urb (&dev->surbs[i].urb); +usb_kill_urb (dev->surbs[i].urb);if (dev->surbs[i].wq.func)cancel_work_sync (&dev->surbs[i].wq);
@@ -338,6 +338,8 @@ static void smsusb_term_device(struct usb_interface *intf)struct smsusb_device_t *dev =usb_get_intfdata (intf);if (dev) { +int i; + dev->state =SMSUSB_DISCONNECTED ;smsusb_stop_streaming (dev);
@@ -346,6 +348,9 @@ static void smsusb_term_device(struct usb_interface *intf)if (dev->coredev)smscore_unregister_device (dev->coredev); +for (i =0 ; i <MAX_URBS ; i++) +usb_free_urb (dev->surbs[i].urb); +pr_debug ("device 0x%p destroyed\n" , dev);kfree (dev); }
@@ -390,6 +395,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)void *mdev;int i, rc;int align =0 ; +int n_urb =0 ;/* create device object */ dev =kzalloc (sizeof (struct smsusb_device_t ),GFP_KERNEL );
@@ -461,9 +467,11 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id) dev->coredev->is_usb_device = true;/* initialize urbs */ -for (i =0 ; i <MAX_URBS ; i++) { - dev->surbs[i].dev = dev; -usb_init_urb (&dev->surbs[i].urb); +for (n_urb =0 ; n_urb <MAX_URBS ; n_urb++) { + dev->surbs[n_urb].dev = dev; + dev->surbs[n_urb].urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!dev->surbs[n_urb].urb) + goto free_urbs; }pr_debug ("smsusb_start_streaming(...).\n" );
@@ -485,6 +493,10 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)return rc; +free_urbs: +for (i =0 ; i < n_urb; i++) +usb_free_urb (dev->surbs[n_urb].urb); + err_unregister_device:smsusb_term_device (intf);#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Signature · 1 line
2.44.0.478.gd926399ef9-goog
Re: [PATCH 1/6] media: usb: siano: Fix allocation of urbs
Quoted 87 lines · Ricardo Ribalda
USB urbs must be allocated with
usb_alloc_urb. Quoting the manualOnly use this function (
usb_init_urb) if you _really_ understand what you are doing.Fix the following smatch error:
drivers/media/usb/siano/smsusb.c:53:38: warning: array of flexible structuresSigned-off-by Ricardo Ribalda <[email protected]>--- drivers/media/usb/siano/smsusb.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)drivers/media/usb/siano/smsusb.c +11
-5@@ -40,7 +40,7 @@ struct smsusb_urb_t {struct smscore_buffer_t *cb;struct smsusb_device_t *dev; -struct urb urb; +struct urb *urb;/* For the bottom half */ struct work_struct wq;@@ -160,7 +160,7 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, } usb_fill_bulk_urb( - &surb->urb, + surb->urb, dev->udev, usb_rcvbulkpipe(dev->udev, dev->in_ep), surb->cb->p,@@ -168,9 +168,9 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, smsusb_onresponse, surb ); - surb->urb.transfer_flags |=URB_FREE_BUFFER ; + surb->urb->transfer_flags |=URB_FREE_BUFFER ; -return usb_submit_urb (&surb->urb,GFP_ATOMIC ); +return usb_submit_urb (surb->urb,GFP_ATOMIC ); }static void smsusb_stop_streaming (struct smsusb_device_t *dev)@@ -178,7 +178,7 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)int i;for (i =0 ; i <MAX_URBS ; i++) { -usb_kill_urb (&dev->surbs[i].urb); +usb_kill_urb (dev->surbs[i].urb);if (dev->surbs[i].wq.func)cancel_work_sync (&dev->surbs[i].wq);@@ -338,6 +338,8 @@ static void smsusb_term_device(struct usb_interface *intf)struct smsusb_device_t *dev =usb_get_intfdata (intf);if (dev) { +int i; + dev->state =SMSUSB_DISCONNECTED ;smsusb_stop_streaming (dev);@@ -346,6 +348,9 @@ static void smsusb_term_device(struct usb_interface *intf)if (dev->coredev)smscore_unregister_device (dev->coredev); +for (i =0 ; i <MAX_URBS ; i++) +usb_free_urb (dev->surbs[i].urb); +pr_debug ("device 0x%p destroyed\n" , dev);kfree (dev); }@@ -390,6 +395,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)void *mdev;int i, rc;int align =0 ; +int n_urb =0 ;/* create device object */ dev =kzalloc (sizeof (struct smsusb_device_t ),GFP_KERNEL );@@ -461,9 +467,11 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id) dev->coredev->is_usb_device = true;
drivers/media/usb/siano/smsusb.c static int smsusb_init_device(struct usb_interface *intf, int board_id) · line 475
Quoted 10 lines
drivers/media/usb/siano/smsusb.c +5
-3/* initialize urbs */ -for (i =0 ; i <MAX_URBS ; i++) { - dev->surbs[i].dev = dev; -usb_init_urb (&dev->surbs[i].urb); +for (n_urb =0 ; n_urb <MAX_URBS ; n_urb++) { + dev->surbs[n_urb].dev = dev; + dev->surbs[n_urb].urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!dev->surbs[n_urb].urb) + goto free_urbs; }
After allocating the URBs there are a few more error paths that do 'goto err_unregister_device;' instead of 'goto free_urbs;'. From what I can see, those need to go through 'free_urbs' as well.
drivers/media/usb/siano/smsusb.c static int smsusb_init_device(struct usb_interface *intf, int board_id) · line 498
Quoted 9 lines
drivers/media/usb/siano/smsusb.c +3
-0pr_debug ("smsusb_start_streaming(...).\n" );@@ -485,6 +493,10 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)return rc; +free_urbs: +for (i =0 ; i < n_urb; i++) +usb_free_urb (dev->surbs[n_urb].urb);
Would it be better to also assign NULL to dev->surbs[n_urb].urb? That way there are no invalid pointers that can mess up things.
Regards,
Hans
Quoted 5 lines
drivers/media/usb/siano/smsusb.c +1
-0+ err_unregister_device:smsusb_term_device (intf);#ifdef CONFIG_MEDIA_CONTROLLER_DVB
Re: [PATCH 1/6] media: usb: siano: Fix allocation of urbs
Quoted 107 lines · Hans Verkuil
Quoted 87 lines · Ricardo Ribalda
USB urbs must be allocated with
usb_alloc_urb. Quoting the manualOnly use this function (
usb_init_urb) if you _really_ understand what you are doing.Fix the following smatch error:
drivers/media/usb/siano/smsusb.c:53:38: warning: array of flexible structuresSigned-off-by Ricardo Ribalda <[email protected]>--- drivers/media/usb/siano/smsusb.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-)drivers/media/usb/siano/smsusb.c +11
-5@@ -40,7 +40,7 @@ struct smsusb_urb_t {struct smscore_buffer_t *cb;struct smsusb_device_t *dev; -struct urb urb; +struct urb *urb;/* For the bottom half */ struct work_struct wq;@@ -160,7 +160,7 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, } usb_fill_bulk_urb( - &surb->urb, + surb->urb, dev->udev, usb_rcvbulkpipe(dev->udev, dev->in_ep), surb->cb->p,@@ -168,9 +168,9 @@ static int smsusb_submit_urb(struct smsusb_device_t *dev, smsusb_onresponse, surb ); - surb->urb.transfer_flags |=URB_FREE_BUFFER ; + surb->urb->transfer_flags |=URB_FREE_BUFFER ; -return usb_submit_urb (&surb->urb,GFP_ATOMIC ); +return usb_submit_urb (surb->urb,GFP_ATOMIC ); }static void smsusb_stop_streaming (struct smsusb_device_t *dev)@@ -178,7 +178,7 @@ static void smsusb_stop_streaming(struct smsusb_device_t *dev)int i;for (i =0 ; i <MAX_URBS ; i++) { -usb_kill_urb (&dev->surbs[i].urb); +usb_kill_urb (dev->surbs[i].urb);if (dev->surbs[i].wq.func)cancel_work_sync (&dev->surbs[i].wq);@@ -338,6 +338,8 @@ static void smsusb_term_device(struct usb_interface *intf)struct smsusb_device_t *dev =usb_get_intfdata (intf);if (dev) { +int i; + dev->state =SMSUSB_DISCONNECTED ;smsusb_stop_streaming (dev);@@ -346,6 +348,9 @@ static void smsusb_term_device(struct usb_interface *intf)if (dev->coredev)smscore_unregister_device (dev->coredev); +for (i =0 ; i <MAX_URBS ; i++) +usb_free_urb (dev->surbs[i].urb); +pr_debug ("device 0x%p destroyed\n" , dev);kfree (dev); }@@ -390,6 +395,7 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)void *mdev;int i, rc;int align =0 ; +int n_urb =0 ;/* create device object */ dev =kzalloc (sizeof (struct smsusb_device_t ),GFP_KERNEL );@@ -461,9 +467,11 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id) dev->coredev->is_usb_device = true;drivers/media/usb/siano/smsusb.c static int smsusb_init_device(struct usb_interface *intf, int board_id) · line 475
Quoted 10 lines
drivers/media/usb/siano/smsusb.c +5
-3/* initialize urbs */ -for (i =0 ; i <MAX_URBS ; i++) { - dev->surbs[i].dev = dev; -usb_init_urb (&dev->surbs[i].urb); +for (n_urb =0 ; n_urb <MAX_URBS ; n_urb++) { + dev->surbs[n_urb].dev = dev; + dev->surbs[n_urb].urb =usb_alloc_urb (0 ,GFP_KERNEL ); +if (!dev->surbs[n_urb].urb) + goto free_urbs; }After allocating the URBs there are a few more error paths that do 'goto
err_unregister_device;' instead of 'gotofree_urbs;'. From what I can see, those need to go through 'free_urbs' as well.Quoted 4 lines
drivers/media/usb/siano/smsusb.c +0
-0pr_debug ("smsusb_start_streaming(...).\n" );@@ -485,6 +493,10 @@ static int smsusb_init_device(struct usb_interface *intf, int board_id)
Quoted 8 lines
drivers/media/usb/siano/smsusb.c static int smsusb_init_device(struct usb_interface *intf, int board_id) · line 498
Quoted 5 lines
drivers/media/usb/siano/smsusb.c +3
-0return rc; +free_urbs: +for (i =0 ; i < n_urb; i++) +usb_free_urb (dev->surbs[n_urb].urb);Would it be better to also assign NULL to dev->surbs[n_urb].urb? That way there are no invalid pointers that can mess up things.
Ricardo, just post a v2 of this patch. I posted a PR for the other patches. Nice work, BTW!
Regards,
Hans
Quoted 12 lines
Regards,
Hans
Quoted 5 lines
drivers/media/usb/siano/smsusb.c +1
-0+ err_unregister_device:smsusb_term_device (intf);#ifdef CONFIG_MEDIA_CONTROLLER_DVB
PATCH 2/6 media: cxd2880: Replaze kmalloc with kzalloc
In reply to Ricardo RibaldaFix smatch error:drivers/media/spi/cxd2880-spi.c:391 cxd2880_start_feed() warn: Please consider using kzalloc instead of kmalloc
--- drivers/media/spi/cxd2880-spi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/spi/cxd2880-spi.c +1 -1
@@ -388,7 +388,7 @@ static int cxd2880_start_feed(struct dvb_demux_feed *feed)if (dvb_spi->feed_count ==0 ) { dvb_spi->ts_buf = -kmalloc (MAX_TRANS_PKT *188 , +kzalloc (MAX_TRANS_PKT *188 ,GFP_KERNEL |GFP_DMA );if (!dvb_spi->ts_buf) {pr_err ("ts buffer allocate failed\n" );
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 3/6 media: platform: sti: hva: clk_unprepare unconditionally
In reply to Ricardo Ribaldahva->clk cannot be NULL at this point. Simplify the code and make smatch happy:
drivers/media/platform/st/sti/hva/hva-hw.c:412 hva_hw_probe() warn: 'hva->clk' from clk_prepare() not released on lines: 412
--- drivers/media/platform/st/sti/hva/hva-hw.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/media/platform/st/sti/hva/hva-hw.c +1 -2
@@ -406,8 +406,7 @@ int hva_hw_probe(struct platform_device *pdev, struct hva_dev *hva) err_disable:pm_runtime_disable (dev); err_clk: -if (hva->clk) -clk_unprepare (hva->clk); +clk_unprepare (hva->clk);return ret; }
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 4/6 media: v4l2-ctrls-core.c: Do not use iterator outside loop
In reply to Ricardo RibaldaSimplify a bit the code introducing a new variable for iterating through the control list.
It also makes smatch happy:
drivers/media/v4l2-core/v4l2-ctrls-api.c:1091 v4l2_query_ext_ctrl() warn: iterator used outside loop: 'ref'
--- drivers/media/v4l2-core/v4l2-ctrls-api.c | 33 ++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-)
drivers/media/v4l2-core/v4l2-ctrls-api.c +19 -14
@@ -1052,35 +1052,40 @@ int v4l2_query_ext_ctrl(struct v4l2_ctrl_handler *hdl, struct v4l2_query_ext_ctrif (id >=node2id (hdl->ctrl_refs.prev)) { ref =NULL ;/* Yes, so there is no next control */ }else if (ref) { +struct v4l2_ctrl_ref *pos = ref; +/* * We found a control with the given ID, so just get * the next valid one in the list. */ -list_for_each_entry_continue (ref, &hdl->ctrl_refs, node) { - is_compound = ref->ctrl->is_array || - ref->ctrl->type >=V4L2_CTRL_COMPOUND_TYPES ; -if (id < ref->ctrl->id && - (is_compound & mask) == match) + ref =NULL ; +list_for_each_entry_continue (pos, &hdl->ctrl_refs, node) { + is_compound = pos->ctrl->is_array || + pos->ctrl->type >=V4L2_CTRL_COMPOUND_TYPES ; +if (id < pos->ctrl->id && + (is_compound & mask) == match) { + ref = pos;break ; + } } -if (&ref->node == &hdl->ctrl_refs) - ref =NULL ; }else { +struct v4l2_ctrl_ref *pos; +/* * No control with the given ID exists, so start * searching for the next largest ID. We know there * is one, otherwise the first 'if' above would have * been true. */ -list_for_each_entry (ref, &hdl->ctrl_refs, node) { - is_compound = ref->ctrl->is_array || - ref->ctrl->type >=V4L2_CTRL_COMPOUND_TYPES ; -if (id < ref->ctrl->id && - (is_compound & mask) == match) +list_for_each_entry (pos, &hdl->ctrl_refs, node) { + is_compound = pos->ctrl->is_array || + pos->ctrl->type >=V4L2_CTRL_COMPOUND_TYPES ; +if (id < pos->ctrl->id && + (is_compound & mask) == match) { + ref = pos;break ; + } } -if (&ref->node == &hdl->ctrl_refs) - ref =NULL ; } }mutex_unlock (hdl->lock);
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 5/6 media: adv7180: Only request valids IRQs
In reply to Ricardo Ribaldai2c_device_probe(), seems to assume that irq = 0 means that there is no irq to request.
The driver also believes that on the clean path. So lets be consistent here.
Also make smatch happy.
Fix:drivers/media/i2c/adv7180.c:1526 adv7180_probe() warn: 'client->irq' from request_threaded_irq() not released on lines: 1526
--- drivers/media/i2c/adv7180.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/media/i2c/adv7180.c +1 -1
@@ -1486,7 +1486,7 @@ static int adv7180_probe(struct i2c_client *client)if (ret) goto err_media_entity_cleanup; -if (state->irq) { +if (state->irq >0 ) { ret =request_threaded_irq (client->irq,NULL , adv7180_irq,IRQF_ONESHOT |IRQF_TRIGGER_FALLING ,KBUILD_MODNAME , state);
Signature · 1 line
2.44.0.478.gd926399ef9-goog
PATCH 6/6 media: touchscreen: sur40: convert le16 to cpu before use
In reply to Ricardo RibaldaSmatch found this issue:drivers/input/touchscreen/sur40.c:424:55: warning: incorrect type in argument 2 (different base types)drivers/input/touchscreen/sur40.c:424:55: expected int keydrivers/input/touchscreen/sur40.c:424:55: got restricted __le16 [usertype] blob_id
--- drivers/input/touchscreen/sur40.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/input/touchscreen/sur40.c +1 -1
@@ -421,7 +421,7 @@ static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)if (blob->type !=SUR40_TOUCH )return ; - slotnum =input_mt_get_slot_by_key (input, blob->blob_id); + slotnum =input_mt_get_slot_by_key (input,le16_to_cpu (blob->blob_id));if (slotnum <0 || slotnum >=MAX_CONTACTS )return ;
Signature · 1 line
2.44.0.478.gd926399ef9-goog
Re: [PATCH 6/6] media: touchscreen: sur40: convert le16 to cpu before use
Quoted 15 lines · Ricardo Ribalda
Smatch found this issue:
drivers/input/touchscreen/sur40.c:424:55: warning: incorrect type in argument 2 (different base types)drivers/input/touchscreen/sur40.c:424:55: expected int keydrivers/input/touchscreen/sur40.c:424:55: got restricted__le16[usertype]blob_idSigned-off-by Ricardo Ribalda <[email protected]>--- drivers/input/touchscreen/sur40.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/input/touchscreen/sur40.c +0
-0
drivers/input/touchscreen/sur40.c static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input) · line 424
Quoted 5 lines
drivers/input/touchscreen/sur40.c +1
-1@@ -421,7 +421,7 @@ static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input)if (blob->type !=SUR40_TOUCH )return ; - slotnum =input_mt_get_slot_by_key (input, blob->blob_id); + slotnum =input_mt_get_slot_by_key (input,le16_to_cpu (blob->blob_id));
We are not really using the real value of the ID for computations but only as an opaque "key", so doing the conversion is not strictly necessary, but it is cheap so we may as well do it.
drivers/input/touchscreen/sur40.c static void sur40_report_blob(struct sur40_blob *blob, struct input_dev *input) · line 426
Quoted 2 lines
drivers/input/touchscreen/sur40.c +0
-0if (slotnum <0 || slotnum >=MAX_CONTACTS )return ;
Applied, thank you.
Signature · 1 line
Dmitry