media: staging/intel-ipu3: Implement lock for stream on/off operations
Subsystems (3)
PATCH 1/1 media: staging/intel-ipu3: Implement lock for stream on/off operations
Currently concurrent stream off operations on ImgU nodes are not synchronized, leading to use-after-free bugs (as reported by KASAN).
[ 250.090724] BUG: KASAN: use-after-free in ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090726] Read of size 8 at addr ffff888127b29bc0 by task yavta/18836 [ 250.090731] Hardware name: HP Soraka/Soraka, BIOS Google_Soraka.10431.17.0 03/22/2018 [ 250.090732] Call Trace: [ 250.090735] dump_stack+0x6a/0xb1 [ 250.090739] print_address_description+0x8e/0x279 [ 250.090743] ? ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090746] kasan_report+0x260/0x28a [ 250.090750] ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090754] ipu3_css_pool_cleanup+0x24/0x37 [ipu3_imgu] [ 250.090759] ipu3_css_pipeline_cleanup+0x61/0xb9 [ipu3_imgu] [ 250.090763] ipu3_css_stop_streaming+0x1f2/0x321 [ipu3_imgu] [ 250.090768] imgu_s_stream+0x94/0x443 [ipu3_imgu] [ 250.090772] ? ipu3_vb2_buf_queue+0x280/0x280 [ipu3_imgu] [ 250.090775] ? vb2_dma_sg_unmap_dmabuf+0x16/0x6f [videobuf2_dma_sg] [ 250.090778] ? vb2_buffer_in_use+0x36/0x58 [videobuf2_common] [ 250.090782] ipu3_vb2_stop_streaming+0xf9/0x135 [ipu3_imgu]
Implemented a lock to synchronize imgu stream on / off operations and the modification of streaming flag (in struct imgu_device), to prevent these issues.
--- drivers/staging/media/ipu3/ipu3-v4l2.c | 6 ++++++ drivers/staging/media/ipu3/ipu3.c | 3 +++ drivers/staging/media/ipu3/ipu3.h | 4 ++++ 3 files changed, 13 insertions(+)
drivers/staging/media/ipu3/ipu3-v4l2.c +6 -0
@@ -507,12 +507,15 @@ static int ipu3_vb2_start_streaming(struct vb2_queue *vq, unsigned int count) goto fail_stop_pipeline; } +mutex_lock (&imgu->streaming_lock); +/* Start streaming of the whole pipeline now */ dev_dbg (dev,"IMGU streaming is ready to start" ); r =imgu_s_stream (imgu, true);if (!r) imgu->streaming = true; +mutex_unlock (&imgu->streaming_lock);return 0 ; fail_stop_pipeline:
@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) {/* Yes, really stop streaming now */
@@ -552,6 +557,7 @@ static void ipu3_vb2_stop_streaming(struct vb2_queue *vq) imgu->streaming = false; } +mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );media_pipeline_stop (&node->vdev.entity); }
drivers/staging/media/ipu3/ipu3.c +3 -0
@@ -635,6 +635,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,return r;mutex_init (&imgu->lock); +mutex_init (&imgu->streaming_lock);atomic_set (&imgu->qbuf_barrier,0 );init_waitqueue_head (&imgu->buf_drain_wq);
@@ -699,6 +700,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,ipu3_css_set_powerdown (&pci_dev->dev, imgu->base); out_mutex_destroy:mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock);return r; }
@@ -716,6 +718,7 @@ static void imgu_pci_remove(struct pci_dev *pci_dev)ipu3_dmamap_exit (imgu);ipu3_mmu_exit (imgu->mmu);mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock); }static int __maybe_unused imgu_suspend (struct device *dev)
drivers/staging/media/ipu3/ipu3.h +4 -0
@@ -146,6 +146,10 @@ struct imgu_device {* vid_buf.list and css->queue */ struct mutex lock; + +/* Lock to protect writes to streaming flag in this struct */ +struct mutex streaming_lock; +/* Forbit streaming and buffer queuing during system suspend. */ atomic_t qbuf_barrier;/* Indicate if system suspend take place while imgu is streaming. */
Signature · 1 line
2.19.1
Sakari Ailus
maintainerHi Rajmohan,
Quoted 40 lines · Rajmohan Mani
Currently concurrent stream off operations on ImgU nodes are not synchronized, leading to use-after-free bugs (as reported by KASAN).
[ 250.090724] BUG: KASAN: use-after-free in ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090726] Read of size 8 at addr ffff888127b29bc0 by task yavta/18836 [ 250.090731] Hardware name: HP Soraka/Soraka, BIOS Google_Soraka.10431.17.0 03/22/2018 [ 250.090732] Call Trace: [ 250.090735] dump_stack+0x6a/0xb1 [ 250.090739] print_address_description+0x8e/0x279 [ 250.090743] ? ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090746] kasan_report+0x260/0x28a [ 250.090750] ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090754] ipu3_css_pool_cleanup+0x24/0x37 [ipu3_imgu] [ 250.090759] ipu3_css_pipeline_cleanup+0x61/0xb9 [ipu3_imgu] [ 250.090763] ipu3_css_stop_streaming+0x1f2/0x321 [ipu3_imgu] [ 250.090768] imgu_s_stream+0x94/0x443 [ipu3_imgu] [ 250.090772] ? ipu3_vb2_buf_queue+0x280/0x280 [ipu3_imgu] [ 250.090775] ? vb2_dma_sg_unmap_dmabuf+0x16/0x6f [videobuf2_dma_sg] [ 250.090778] ? vb2_buffer_in_use+0x36/0x58 [videobuf2_common] [ 250.090782] ipu3_vb2_stop_streaming+0xf9/0x135 [ipu3_imgu]Implemented a lock to synchronize imgu stream on / off operations and the modification of streaming flag (in struct
imgu_device), to prevent these issues.Reported-by Laurent Pinchart <[email protected]>Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Rajmohan Mani <[email protected]>--- drivers/staging/media/ipu3/ipu3-v4l2.c | 6 ++++++ drivers/staging/media/ipu3/ipu3.c | 3 +++ drivers/staging/media/ipu3/ipu3.h | 4 ++++ 3 files changed, 13 insertions(+)drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct vb2_queue *vq, unsigned int count) · line 511
Quoted 5 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0@@ -507,12 +507,15 @@ static int ipu3_vb2_start_streaming(struct vb2_queue *vq, unsigned int count) goto fail_stop_pipeline; } +mutex_lock (&imgu->streaming_lock); +
You appear to be using imgu_device.lock (while searching buffers to queue to the device) as well as imgu_video_device.lock (qbuf, dqbuf) to serialise access to imgu_video_device.buffers list. The two locks may be acquired at the same time but each by different processes. That needs to be addressed, but probably not in this patch.
I wonder if it'd be more simple to use imgu->lock here instead of adding a new one.
Quoted 71 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +4
-0/* Start streaming of the whole pipeline now */ dev_dbg (dev,"IMGU streaming is ready to start" ); r =imgu_s_stream (imgu, true);if (!r) imgu->streaming = true; +mutex_unlock (&imgu->streaming_lock);return 0 ; fail_stop_pipeline:@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) {/* Yes, really stop streaming now */ @@ -552,6 +557,7 @@ static void ipu3_vb2_stop_streaming(struct vb2_queue *vq) imgu->streaming = false; } +mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );media_pipeline_stop (&node->vdev.entity);}
drivers/staging/media/ipu3/ipu3.c +3
-0@@ -635,6 +635,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,return r;mutex_init (&imgu->lock); +mutex_init (&imgu->streaming_lock);atomic_set (&imgu->qbuf_barrier,0 );init_waitqueue_head (&imgu->buf_drain_wq);@@ -699,6 +700,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,ipu3_css_set_powerdown (&pci_dev->dev, imgu->base); out_mutex_destroy:mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock);return r; }@@ -716,6 +718,7 @@ static void imgu_pci_remove(struct pci_dev *pci_dev)ipu3_dmamap_exit (imgu);ipu3_mmu_exit (imgu->mmu);mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock); }static int __maybe_unused imgu_suspend (struct device *dev)drivers/staging/media/ipu3/ipu3.h +4
-0@@ -146,6 +146,10 @@ struct imgu_device {* vid_buf.list and css->queue */ struct mutex lock; + +/* Lock to protect writes to streaming flag in this struct */ +struct mutex streaming_lock; +/* Forbit streaming and buffer queuing during system suspend. */ atomic_t qbuf_barrier;/* Indicate if system suspend take place while imgu is streaming. */
Signature · 4 lines
Regards, Sakari Ailus [email protected]
Hi Sakari,
Quoted 64 lines
-----Original Message-----
From: Sakari Ailus [mailto:[email protected]]
Sent: Wednesday, January 30, 2019 12:59 AM
To: Mani, Rajmohan <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]; [email protected]; [email protected]; Laurent Pinchart <[email protected]>; Jacopo Mondi <[email protected]>; Qiu, Tian Shu <[email protected]>; Cao, Bingbu <[email protected]>; [email protected]; Zhi, Yong <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] media: staging/intel-ipu3: Implement lock for stream on/off operationsHi Rajmohan,
Quoted 7 lines · Rajmohan Mani
Currently concurrent stream off operations on ImgU nodes are not synchronized, leading to use-after-free bugs (as reported by KASAN).
[ 250.090724] BUG: KASAN: use-after-free in
ipu3_dmamap_free+0xc5/0x116[ipu3_imgu] [ 250.090726] Read of size 8 at addrffff888127b29bc0by task yavta/18836 [ 250.090731] Hardware
name: HP Soraka/Soraka, BIOS Google_Soraka.10431.17.0 03/22/2018 [250.090732] Call Trace:
Quoted 11 lines
[ 250.090735] dump_stack+0x6a/0xb1 [ 250.090739] print_address_description+0x8e/0x279 [ 250.090743] ? ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090746] kasan_report+0x260/0x28a [ 250.090750] ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090754] ipu3_css_pool_cleanup+0x24/0x37 [ipu3_imgu] [ 250.090759] ipu3_css_pipeline_cleanup+0x61/0xb9 [ipu3_imgu] [ 250.090763] ipu3_css_stop_streaming+0x1f2/0x321 [ipu3_imgu] [ 250.090768] imgu_s_stream+0x94/0x443 [ipu3_imgu] [ 250.090772] ? ipu3_vb2_buf_queue+0x280/0x280 [ipu3_imgu] [ 250.090775] ? vb2_dma_sg_unmap_dmabuf+0x16/0x6f [videobuf2_dma_sg] [ 250.090778]?
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct
Quoted 23 lines
vb2_buffer_in_use+0x36/0x58 [videobuf2_common] [ 250.090782] ipu3_vb2_stop_streaming+0xf9/0x135 [ipu3_imgu]Implemented a lock to synchronize imgu stream on / off operations and the modification of streaming flag (in struct
imgu_device), to prevent these issues.Reported-by Laurent Pinchart <[email protected]>Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Rajmohan Mani <[email protected]>--- drivers/staging/media/ipu3/ipu3-v4l2.c | 6 ++++++ drivers/staging/media/ipu3/ipu3.c | 3 +++ drivers/staging/media/ipu3/ipu3.h | 4 ++++ 3 files changed, 13 insertions(+)drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0@@ -507,12 +507,15 @@ static int ipu3_vb2_start_streaming(struct
vb2_queue*vq, unsigned int count)Quoted 3 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0goto fail_stop_pipeline; }
Quoted 6 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct · line 511
Quoted 2 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0+mutex_lock (&imgu->streaming_lock); +You appear to be using imgu_device.lock (while searching buffers to queue to the device) as well as imgu_video_device.lock (qbuf, dqbuf) to serialise access to imgu_video_device.buffers list.
Ack
Quoted 4 lines
The two locks may be acquired at the same
time but each by different processes. That needs to be addressed, but probably not in this patch.
The node specific locks will be used by different processes and all of these processes will be competing commonly (and successfully) for the imgu_device lock. I will look into this more.
Quoted 3 lines
I wonder if it'd be more simple to use
imgu->lockhere instead of adding a new one.
Extending imgu->lock here, does not work in this case, as imgu_queue_buffers() will be stuck acquiring imgu->lock, which was already acquired by imgu_s_stream() through ipu3_vb2_start_streaming().
Quoted 82 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct
Quoted 11 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0/* Start streaming of the whole pipeline now */ dev_dbg (dev,"IMGU streaming is ready to start" ); r =imgu_s_stream (imgu, true);if (!r) imgu->streaming = true; +mutex_unlock (&imgu->streaming_lock);return 0 ; fail_stop_pipeline:@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct
vb2_queue*vq)drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)
Quoted 8 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) { /* Yes, really stop streaming now */ @@ -552,6 +557,7 @@static
drivers/staging/media/ipu3/ipu3.c static void imgu_pci_remove(struct pci_dev
Quoted 31 lines
void
ipu3_vb2_stop_streaming(struct vb2_queue *vq)drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0imgu->streaming = false; } +mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );media_pipeline_stop (&node->vdev.entity); }drivers/staging/media/ipu3/ipu3.c +2
-0@@ -635,6 +635,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,return r;mutex_init (&imgu->lock); +mutex_init (&imgu->streaming_lock);atomic_set (&imgu->qbuf_barrier,0 );init_waitqueue_head (&imgu->buf_drain_wq);@@ -699,6 +700,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,ipu3_css_set_powerdown (&pci_dev->dev, imgu->base); out_mutex_destroy:mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock);return r; }@@ -716,6 +718,7 @@ static void imgu_pci_remove(struct pci_dev
*pci_dev)Quoted 13 lines
drivers/staging/media/ipu3/ipu3.c +1
-0ipu3_dmamap_exit (imgu);ipu3_mmu_exit (imgu->mmu);mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock); } static int __maybe_unused imgu_suspend(struct device *dev) diff --git
a/drivers/staging/media/ipu3/ipu3.hb/drivers/staging/media/ipu3/ipu3.h
index04fc99f47ebb..f732315f0701100644drivers/staging/media/ipu3/ipu3.h struct imgu_device { · line 155
Quoted 10 lines
drivers/staging/media/ipu3/ipu3.h +4
-0@@ -146,6 +146,10 @@ struct imgu_device {* vid_buf.list and css->queue */ struct mutex lock; + +/* Lock to protect writes to streaming flag in this struct */ +struct mutex streaming_lock; +/* Forbit streaming and buffer queuing during system suspend. */ atomic_t qbuf_barrier;/* Indicate if system suspend take place while imgu is streaming. */ --
Regards,Sakari Ailus
[email protected]
Sakari Ailus
maintainerHi Raj,
Quoted 75 lines · Rajmohan
Hi Sakari,
Quoted 64 lines
-----Original Message-----
From: Sakari Ailus [mailto:[email protected]]
Sent: Wednesday, January 30, 2019 12:59 AM
To: Mani, Rajmohan <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]; [email protected]; [email protected]; Laurent Pinchart <[email protected]>; Jacopo Mondi <[email protected]>; Qiu, Tian Shu <[email protected]>; Cao, Bingbu <[email protected]>; [email protected]; Zhi, Yong <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] media: staging/intel-ipu3: Implement lock for stream on/off operationsHi Rajmohan,
Quoted 7 lines · Rajmohan Mani
Currently concurrent stream off operations on ImgU nodes are not synchronized, leading to use-after-free bugs (as reported by KASAN).
[ 250.090724] BUG: KASAN: use-after-free in
ipu3_dmamap_free+0xc5/0x116[ipu3_imgu] [ 250.090726] Read of size 8 at addrffff888127b29bc0by task yavta/18836 [ 250.090731] Hardware
name: HP Soraka/Soraka, BIOS Google_Soraka.10431.17.0 03/22/2018 [250.090732] Call Trace:
Quoted 11 lines
[ 250.090735] dump_stack+0x6a/0xb1 [ 250.090739] print_address_description+0x8e/0x279 [ 250.090743] ? ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090746] kasan_report+0x260/0x28a [ 250.090750] ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090754] ipu3_css_pool_cleanup+0x24/0x37 [ipu3_imgu] [ 250.090759] ipu3_css_pipeline_cleanup+0x61/0xb9 [ipu3_imgu] [ 250.090763] ipu3_css_stop_streaming+0x1f2/0x321 [ipu3_imgu] [ 250.090768] imgu_s_stream+0x94/0x443 [ipu3_imgu] [ 250.090772] ? ipu3_vb2_buf_queue+0x280/0x280 [ipu3_imgu] [ 250.090775] ? vb2_dma_sg_unmap_dmabuf+0x16/0x6f [videobuf2_dma_sg] [ 250.090778]?
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct
Quoted 23 lines
vb2_buffer_in_use+0x36/0x58 [videobuf2_common] [ 250.090782] ipu3_vb2_stop_streaming+0xf9/0x135 [ipu3_imgu]Implemented a lock to synchronize imgu stream on / off operations and the modification of streaming flag (in struct
imgu_device), to prevent these issues.Reported-by Laurent Pinchart <[email protected]>Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Rajmohan Mani <[email protected]>--- drivers/staging/media/ipu3/ipu3-v4l2.c | 6 ++++++ drivers/staging/media/ipu3/ipu3.c | 3 +++ drivers/staging/media/ipu3/ipu3.h | 4 ++++ 3 files changed, 13 insertions(+)drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0@@ -507,12 +507,15 @@ static int ipu3_vb2_start_streaming(struct
vb2_queue*vq, unsigned int count)Quoted 3 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0goto fail_stop_pipeline; }Quoted 6 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct · line 511
Quoted 2 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0+mutex_lock (&imgu->streaming_lock); +You appear to be using imgu_device.lock (while searching buffers to queue to the device) as well as imgu_video_device.lock (qbuf, dqbuf) to serialise access to imgu_video_device.buffers list.
Ack
Quoted 8 lines
Quoted 4 lines
The two locks may be acquired at the same
time but each by different processes. That needs to be addressed, but probably not in this patch.The node specific locks will be used by different processes and all of these processes will be competing commonly (and successfully) for the
imgu_devicelock. I will look into this more.
Oops. I think I must have misread something; looking at the code again, indeed imgu_device.lock is acquired for all accesses. Please ignore this comment.
Quoted 91 lines
Quoted 4 lines
I wonder if it'd be more simple to use
imgu->lockhere instead of adding a new one.Extending
imgu->lockhere, does not work in this case, asimgu_queue_buffers()will be stuck acquiringimgu->lock, which was already acquired byimgu_s_stream()throughipu3_vb2_start_streaming().Quoted 82 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct
Quoted 11 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0/* Start streaming of the whole pipeline now */ dev_dbg (dev,"IMGU streaming is ready to start" ); r =imgu_s_stream (imgu, true);if (!r) imgu->streaming = true; +mutex_unlock (&imgu->streaming_lock);return 0 ; fail_stop_pipeline:@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct
vb2_queue*vq)drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)
Quoted 8 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) { /* Yes, really stop streaming now */ @@ -552,6 +557,7 @@static
drivers/staging/media/ipu3/ipu3.c static void imgu_pci_remove(struct pci_dev
Quoted 31 lines
void
ipu3_vb2_stop_streaming(struct vb2_queue *vq)drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0imgu->streaming = false; } +mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );media_pipeline_stop (&node->vdev.entity); }drivers/staging/media/ipu3/ipu3.c +2
-0@@ -635,6 +635,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,return r;mutex_init (&imgu->lock); +mutex_init (&imgu->streaming_lock);atomic_set (&imgu->qbuf_barrier,0 );init_waitqueue_head (&imgu->buf_drain_wq);@@ -699,6 +700,7 @@ static int imgu_pci_probe(struct pci_dev *pci_dev,ipu3_css_set_powerdown (&pci_dev->dev, imgu->base); out_mutex_destroy:mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock);return r; }@@ -716,6 +718,7 @@ static void imgu_pci_remove(struct pci_dev
*pci_dev)Quoted 13 lines
drivers/staging/media/ipu3/ipu3.c +1
-0ipu3_dmamap_exit (imgu);ipu3_mmu_exit (imgu->mmu);mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock); } static int __maybe_unused imgu_suspend(struct device *dev) diff --git
a/drivers/staging/media/ipu3/ipu3.hb/drivers/staging/media/ipu3/ipu3.h
index04fc99f47ebb..f732315f0701100644drivers/staging/media/ipu3/ipu3.h struct imgu_device { · line 155
Quoted 10 lines
drivers/staging/media/ipu3/ipu3.h +4
-0@@ -146,6 +146,10 @@ struct imgu_device {* vid_buf.list and css->queue */ struct mutex lock; + +/* Lock to protect writes to streaming flag in this struct */ +struct mutex streaming_lock; +/* Forbit streaming and buffer queuing during system suspend. */ atomic_t qbuf_barrier;/* Indicate if system suspend take place while imgu is streaming. */ --
Regards,Sakari Ailus
[email protected]
Signature · 2 lines
Sakari Ailus [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi Sakari,
Quoted 97 lines
Subject: Re: [PATCH] media: staging/intel-ipu3: Implement lock for stream on/off operations
Hi Raj,
Quoted 77 lines · Rajmohan
Hi Sakari,
Quoted 66 lines
-----Original Message-----
From: Sakari Ailus [mailto:[email protected]]
Sent: Wednesday, January 30, 2019 12:59 AM
To: Mani, Rajmohan <[email protected]>
Cc: Mauro Carvalho Chehab <[email protected]>; Greg Kroah-Hartman <[email protected]>; [email protected]; [email protected]; [email protected]; Laurent Pinchart <[email protected]>; Jacopo Mondi <[email protected]>; Qiu, Tian Shu <[email protected]>; Cao, Bingbu <[email protected]>; [email protected]; Zhi, Yong <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH] media: staging/intel-ipu3: Implement lock for stream on/off operationsHi Rajmohan,
Quoted 8 lines · Rajmohan Mani
Currently concurrent stream off operations on ImgU nodes are not synchronized, leading to use-after-free bugs (as reported by KASAN).
[ 250.090724] BUG: KASAN: use-after-free in
ipu3_dmamap_free+0xc5/0x116[ipu3_imgu] [ 250.090726] Read of size 8 at addrffff888127b29bc0by task yavta/18836 [ 250.090731] Hardware
name: HP Soraka/Soraka, BIOS Google_Soraka.10431.17.0 03/22/2018 [250.090732] Call Trace:
Quoted 11 lines
[ 250.090735] dump_stack+0x6a/0xb1 [ 250.090739] print_address_description+0x8e/0x279 [ 250.090743] ? ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090746] kasan_report+0x260/0x28a [ 250.090750] ipu3_dmamap_free+0xc5/0x116 [ipu3_imgu] [ 250.090754] ipu3_css_pool_cleanup+0x24/0x37 [ipu3_imgu] [ 250.090759] ipu3_css_pipeline_cleanup+0x61/0xb9 [ipu3_imgu] [ 250.090763] ipu3_css_stop_streaming+0x1f2/0x321 [ipu3_imgu] [ 250.090768] imgu_s_stream+0x94/0x443 [ipu3_imgu] [ 250.090772] ? ipu3_vb2_buf_queue+0x280/0x280 [ipu3_imgu] [ 250.090775] ? vb2_dma_sg_unmap_dmabuf+0x16/0x6f [videobuf2_dma_sg] [Quoted 1 line
250.090778]
?
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct
Quoted 23 lines
vb2_buffer_in_use+0x36/0x58 [videobuf2_common] [ 250.090782] ipu3_vb2_stop_streaming+0xf9/0x135 [ipu3_imgu]Implemented a lock to synchronize imgu stream on / off operations and the modification of streaming flag (in struct
imgu_device), to prevent these issues.Reported-by Laurent Pinchart <[email protected]>Suggested-by Laurent Pinchart <[email protected]>Signed-off-by Rajmohan Mani <[email protected]>--- drivers/staging/media/ipu3/ipu3-v4l2.c | 6 ++++++ drivers/staging/media/ipu3/ipu3.c | 3 +++ drivers/staging/media/ipu3/ipu3.h | 4 ++++ 3 files changed, 13 insertions(+)drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0@@ -507,12 +507,15 @@ static int ipu3_vb2_start_streaming(struct
vb2_queue*vq, unsigned int count)Quoted 3 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0goto fail_stop_pipeline; }Quoted 6 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static int ipu3_vb2_start_streaming(struct · line 511
Quoted 2 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0+mutex_lock (&imgu->streaming_lock); +You appear to be using imgu_device.lock (while searching buffers to queue to the device) as well as imgu_video_device.lock (qbuf, dqbuf) to serialise access to imgu_video_device.buffers list.
Ack
Quoted 7 lines
Quoted 4 lines
The two locks may be acquired at the same time but each by different processes. That needs to be addressed, but probably not in this patch.
The node specific locks will be used by different processes and all of these processes will be competing commonly (and successfully) for the
imgu_devicelock.Quoted 6 lines
I will look into this more.
Quoted 3 lines
I wonder if it'd be more simple to use
imgu->lockhere instead of adding a new one.
Quoted 9 lines
Quoted 3 lines
Extending
imgu->lockhere, does not work in this case, asimgu_queue_buffers()will be stuck acquiringimgu->lock, which was already acquired byimgu_s_stream()throughipu3_vb2_start_streaming().You could move acquiring the lock out of these functions. It would also seem that there is device-wide streaming state etc. information to which the access should also be serialised. Currently it's relying on the node-specific lock only which does not help.
Ack. Let me look into this more.
Quoted 2 lines
Can you grab the lock right after
dev_dbg()line in the function?
In order to reduce the amount of code that's run with the lock held, I placed the lock here.
Do you see issues in calls to imgu_sd subdevs and media_pipeline_start(), without the lock being held?
Quoted 3 lines
The lock should be also acquired before testing
imgu->streaminginipu3_vb2_buf_queue()to make sure it won't change in the meantime.
I thought about this and decided against this since the odds of multiple instances of applications, each of which using a single node simultaneously are less, in a typical camera use case.
Please confirm if you see significant issues without the lock here, so we can add the lock here as well.
Quoted 90 lines
Quoted 22 lines
Quoted 22 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct
Quoted 12 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0/* Start streaming of the whole pipeline now */ dev_dbg (dev,"IMGU streaming is ready to start" ); r =imgu_s_stream (imgu, true);if (!r) imgu->streaming = true; +mutex_unlock (&imgu->streaming_lock);return 0 ; fail_stop_pipeline:@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct
vb2_queue*vq)drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)
Quoted 8 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) { /* Yes, really stop streaming now */ @@ -552,6 +557,7 @@static
Quoted 6 lines
Quoted 6 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq) · line 561
Quoted 6 lines
void
ipu3_vb2_stop_streaming(struct vb2_queue *vq)drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0imgu->streaming = false; } +mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );I'd also call
ipu3_return_all_buffers()before releasing the lock: in principle the user may have queued new buffers on the devices before the driver marks the buffers as faulty.Quoted 9 lines
Quoted 9 lines
drivers/staging/media/ipu3/ipu3.c static int imgu_pci_probe(struct pci_dev
Quoted 9 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0media_pipeline_stop (&node->vdev.entity); }drivers/staging/media/ipu3/ipu3.c +0
-0@@ -635,6 +635,7 @@ static int imgu_pci_probe(struct pci_dev
*pci_dev,Quoted 8 lines
Quoted 8 lines
drivers/staging/media/ipu3/ipu3.c static int imgu_pci_probe(struct pci_dev
Quoted 8 lines
drivers/staging/media/ipu3/ipu3.c +1
-0return r;mutex_init (&imgu->lock); +mutex_init (&imgu->streaming_lock);atomic_set (&imgu->qbuf_barrier,0 );init_waitqueue_head (&imgu->buf_drain_wq);@@ -699,6 +700,7 @@ static int imgu_pci_probe(struct pci_dev
*pci_dev,Quoted 26 lines
Quoted 26 lines
drivers/staging/media/ipu3/ipu3.c static void imgu_pci_remove(struct pci_dev
Quoted 8 lines
drivers/staging/media/ipu3/ipu3.c +1
-0ipu3_css_set_powerdown (&pci_dev->dev, imgu->base); out_mutex_destroy:mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock);return r; }@@ -716,6 +718,7 @@ static void imgu_pci_remove(struct pci_dev
*pci_dev)Quoted 17 lines
drivers/staging/media/ipu3/ipu3.c +1
-1ipu3_dmamap_exit (imgu);ipu3_mmu_exit (imgu->mmu);mutex_destroy (&imgu->lock); +mutex_destroy (&imgu->streaming_lock); } static int __maybe_unused imgu_suspend(struct device *dev) diff --git a/drivers/staging/media/ipu3/ipu3.h
b/drivers/staging/media/ipu3/ipu3.h
index04fc99f47ebb..f732315f0701100644drivers/staging/media/ipu3/ipu3.h +1
-0@@ -146,6 +146,10 @@ struct imgu_device {* vid_buf.list and css->queue */ struct mutex lock; +Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/staging/media/ipu3/ipu3.h +3
-0+/* Lock to protect writes to streaming flag in this struct */ +struct mutex streaming_lock; +/* Forbit streaming and buffer queuing during system suspend. */ atomic_t qbuf_barrier;/* Indicate if system suspend take place while imgu is streaming. */
--
Sakari Ailus
[email protected]
Hi Sakari,
[snip]
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
Review static void ipu3_vb2_stop_streaming(struct
Quoted 2 lines
fail_stop_pipeline:(quoted diff) +0
-0@@ -543,6 +546,8 @@ static void ipu3_vb2_stop_streaming(struct
vb2_queue*vq)drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq)
Quoted 8 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +2
-0dev_err (&imgu->pci_dev->dev,"failed to stop subdev streaming\n" ); +mutex_lock (&imgu->streaming_lock); +/* Was this the first node with streaming disabled? */ if (imgu->streaming &&ipu3_all_nodes_streaming (imgu, node)) { /* Yes, really stop streaming now */ @@ -552,6 +557,7 @@static
Quoted 4 lines
void
ipu3_vb2_stop_streaming(struct vb2_queue *vq)drivers/staging/media/ipu3/ipu3-v4l2.c +0
-0imgu->streaming = false; }
Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
drivers/staging/media/ipu3/ipu3-v4l2.c static void ipu3_vb2_stop_streaming(struct vb2_queue *vq) · line 561
Quoted 2 lines
drivers/staging/media/ipu3/ipu3-v4l2.c +1
-0+mutex_unlock (&imgu->streaming_lock);ipu3_return_all_buffers (imgu, node,VB2_BUF_STATE_ERROR );I'd also call
ipu3_return_all_buffers()before releasing the lock: in principle the user may have queued new buffers on the devices before the driver marks the buffers as faulty.
Ack
(Somehow I missed this comment.)
[snip]