staging: most: video: Protect pending_mbos and mute with list_lock
Subsystems (2)
PATCH v3 1/2 staging: most: video: Protect pending_mbos and mute with list_lock
data_ready() and get_top_mbo() read mdev->pending_mbos without holding mdev->list_lock, racing with comp_rx_data() which modifies the list under mdev->list_lock. Also, comp_vdev_close() clears mdev->mute without holding mdev->list_lock.
Acquire mdev->list_lock in data_ready(), get_top_mbo(), and when clearing mdev->mute in comp_vdev_close(). Use list_first_entry_or_null() in get_top_mbo() so comp_vdev_read() can drop the redundant data_ready() check inside the read loop.
---
v3:
- New patch added after inspecting list_lock usage per Dan Carpenter's review of v2.
drivers/staging/most/video/video.c | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-)
drivers/staging/most/video/video.c +27 -7
@@ -60,14 +60,27 @@ static inline struct comp_fh *to_comp_fh(struct file *filp)static LIST_HEAD (video_devices);static DEFINE_SPINLOCK (list_lock); -static inline bool data_ready (struct most_video_dev *mdev) +static bool data_ready (struct most_video_dev *mdev) { -return !list_empty (&mdev->pending_mbos); +unsigned long flags; +bool ready; + +spin_lock_irqsave (&mdev->list_lock, flags); + ready = !list_empty (&mdev->pending_mbos); +spin_unlock_irqrestore (&mdev->list_lock, flags); + +return ready; } -static inline struct mbo *get_top_mbo (struct most_video_dev *mdev) +static struct mbo *get_top_mbo (struct most_video_dev *mdev) { - return list_first_entry(&mdev->pending_mbos, struct mbo, list); +struct mbo *mbo; + +spin_lock_irq (&mdev->list_lock); + mbo = list_first_entry_or_null(&mdev->pending_mbos, struct mbo, list); +spin_unlock_irq (&mdev->list_lock); + +return mbo; }static int comp_vdev_open (struct file *filp)
@@ -143,7 +156,9 @@ static int comp_vdev_close(struct file *filp) }most_stop_channel (mdev->iface, mdev->ch_idx, &comp); +spin_lock_irq (&mdev->list_lock); mdev->mute = false; +spin_unlock_irq (&mdev->list_lock);v4l2_fh_del (&fh->fh, filp);v4l2_fh_exit (&fh->fh);
@@ -175,10 +190,15 @@ static ssize_t comp_vdev_read(struct file *filp, char __user *buf,if (!data_ready (mdev))return -EAGAIN ; -while (count >0 &&data_ready (mdev)) { +while (count >0 ) {struct mbo *const mbo =get_top_mbo (mdev); -int const rem = mbo->processed_length - fh->offs; -int const cnt = rem < count ? rem : count; +int rem, cnt; + +if (!mbo) +break ; + + rem = mbo->processed_length - fh->offs; + cnt = rem < count ? rem : count;if (copy_to_user (buf, mbo->virt_address + fh->offs, cnt)) {v4l2_err (&mdev->v4l2_dev,"read: copy_to_user failed\n" );
Signature · 1 line
2.50.1 (Apple Git-155)
PATCH v3 2/2 staging: most: video: Document locks in struct most_video_dev
checkpatch.pl reports that list_lock and lock in struct most_video_dev are defined without a comment.
list_lock protects pending_mbos and mute across all callers. lock is assigned to vdev->lock and is used by the V4L2 core to serialize ioctls.
Add short comments describing what each lock protects.
---
v3:
- Reword list_lock comment to "Protects @pending_mbos and @mute" instead of singling out the rx completion path (Dan Carpenter)
- Split out locking fixes into patch 1/2
v2:
- Add a commit message and Signed-off-by (Andy Shevchenko)
- Use the "staging: most: video:" subject prefix
- Make the comments shorter and accurate
- Drop the outreachy list (Julia Lawall)
drivers/staging/most/video/video.c | 2 ++ 1 file changed, 2 insertions(+)
drivers/staging/most/video/video.c +2 -0
@@ -33,6 +33,7 @@ struct most_video_dev {bool mute;struct list_head pending_mbos; +/* Protects @pending_mbos and @mute */ spinlock_t list_lock;struct v4l2_device v4l2_dev;
@@ -40,6 +41,7 @@ struct most_video_dev {struct video_device *vdev;unsigned int ctrl_input; +/* Serializes V4L2 ioctls, used as the video_device lock */ struct mutex lock;wait_queue_head_t wait_data;
Signature · 1 line
2.50.1 (Apple Git-155)
Quoted 10 lines · Shivani Patel
data_ready()andget_top_mbo()readmdev->pending_mboswithout holdingmdev->list_lock, racing withcomp_rx_data()which modifies the list undermdev->list_lock. Also,comp_vdev_close()clearsmdev->mutewithout holdingmdev->list_lock.Acquire
mdev->list_lockindata_ready(),get_top_mbo(), and when clearingmdev->muteincomp_vdev_close(). Uselist_first_entry_or_null()inget_top_mbo()socomp_vdev_read()can drop the redundantdata_ready()check inside the read loop.
Quoted 5 lines
Signed-off-by Shivani Patel <[email protected]>---
v3:
- New patch added after inspectinglist_lockusage per Dan Carpenter's review of v2.
Huh... This is interesting.
Quoted 14 lines
drivers/staging/most/video/video.c | 34 ++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 7 deletions(-)drivers/staging/most/video/video.c +1
-1@@ -60,14 +60,27 @@ static inline struct comp_fh *to_comp_fh(struct file *filp)static LIST_HEAD (video_devices);static DEFINE_SPINLOCK (list_lock); -static inline bool data_ready (struct most_video_dev *mdev) +static bool data_ready (struct most_video_dev *mdev)
drivers/staging/most/video/video.c static inline struct comp_fh *to_comp_fh(struct file *filp) · line 72
Quoted 10 lines
drivers/staging/most/video/video.c +8
-1{ -return !list_empty (&mdev->pending_mbos); +unsigned long flags; +bool ready; + +spin_lock_irqsave (&mdev->list_lock, flags); + ready = !list_empty (&mdev->pending_mbos); +spin_unlock_irqrestore (&mdev->list_lock, flags); + +return ready;
It doesn't really matter if we take the lock where since we drop it right away. The list_empty() doesn't anything that requires locking so far as I can see.
Quoted 17 lines
drivers/staging/most/video/video.c +8
-2} -static inline struct mbo *get_top_mbo (struct most_video_dev *mdev) +static struct mbo *get_top_mbo (struct most_video_dev *mdev) { - return list_first_entry(&mdev->pending_mbos, struct mbo, list); +struct mbo *mbo; + +spin_lock_irq (&mdev->list_lock); + mbo = list_first_entry_or_null(&mdev->pending_mbos, struct mbo, list); +spin_unlock_irq (&mdev->list_lock); + +return mbo; }static int comp_vdev_open (struct file *filp)
drivers/staging/most/video/video.c static int comp_vdev_close(struct file *filp) · line 161
Quoted 6 lines
drivers/staging/most/video/video.c +2
-0@@ -143,7 +156,9 @@ static int comp_vdev_close(struct file *filp) }most_stop_channel (mdev->iface, mdev->ch_idx, &comp); +spin_lock_irq (&mdev->list_lock); mdev->mute = false; +spin_unlock_irq (&mdev->list_lock);
I don't see the point of protecting ->mute.
Quoted 9 lines
drivers/staging/most/video/video.c +1
-1v4l2_fh_del (&fh->fh, filp);v4l2_fh_exit (&fh->fh);@@ -175,10 +190,15 @@ static ssize_t comp_vdev_read(struct file *filp, char __user *buf,if (!data_ready (mdev))return -EAGAIN ; -while (count >0 &&data_ready (mdev)) { +while (count >0 ) {
drivers/staging/most/video/video.c static ssize_t comp_vdev_read(struct file *filp, char __user *buf, · line 201
Quoted 10 lines
drivers/staging/most/video/video.c +7
-2struct mbo *const mbo =get_top_mbo (mdev); -int const rem = mbo->processed_length - fh->offs; -int const cnt = rem < count ? rem : count; +int rem, cnt; + +if (!mbo) +break ; + + rem = mbo->processed_length - fh->offs; + cnt = rem < count ? rem : count;
The worry here is that we could have more than one reader open, right? I don't think this is a complete fix, because they could both take the same mbo and could have cnt >= rem and the other has a use after free.
I'm not super familiar with this code. I assume it's only supposed to have one reader at a time. Maybe it's serialized at a different layer?
regards,
dan carpenter
Quoted 10 lines · Dan Carpenter
Quoted 9 lines · Shivani Patel
data_ready()andget_top_mbo()readmdev->pending_mboswithout holdingmdev->list_lock, racing withcomp_rx_data()which modifies the list undermdev->list_lock. Also,comp_vdev_close()clearsmdev->mutewithout holdingmdev->list_lock.Acquire
mdev->list_lockindata_ready(),get_top_mbo(), and when clearingmdev->muteincomp_vdev_close(). Uselist_first_entry_or_null()inget_top_mbo()socomp_vdev_read()can drop the redundantdata_ready()check inside the read loop.
...
Quoted 8 lines
Quoted 8 lines
drivers/staging/most/video/video.c +5
-2-static inline bool data_ready (struct most_video_dev *mdev) +static bool data_ready (struct most_video_dev *mdev) { -return !list_empty (&mdev->pending_mbos); +unsigned long flags; +bool ready; + +spin_lock_irqsave (&mdev->list_lock, flags);
Quoted 8 lines
drivers/staging/most/video/video.c static inline struct comp_fh *to_comp_fh(struct file *filp) · line 72
Quoted 4 lines
drivers/staging/most/video/video.c +4
-0+ ready = !list_empty (&mdev->pending_mbos); +spin_unlock_irqrestore (&mdev->list_lock, flags); + +return ready;It doesn't really matter if we take the lock where since we drop it right away. The
list_empty()doesn't anything that requires locking so far as I can see.
Right, but the problem is that we possibly might compare valid pointer (list head itself) with the stale pointer (head->next and/or head->prev). Also don't forget that for 8-byte pointers on some architectures it might be the cross-boundary split (most likely won't happen with struct list_head as it should be at least 8-byte aligned, but still).
Quoted 1 line
Quoted 1 line
}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 31 lines · Andy Shevchenko
Quoted 10 lines · Dan Carpenter
Quoted 9 lines · Shivani Patel
data_ready()andget_top_mbo()readmdev->pending_mboswithout holdingmdev->list_lock, racing withcomp_rx_data()which modifies the list undermdev->list_lock. Also,comp_vdev_close()clearsmdev->mutewithout holdingmdev->list_lock.Acquire
mdev->list_lockindata_ready(),get_top_mbo(), and when clearingmdev->muteincomp_vdev_close(). Uselist_first_entry_or_null()inget_top_mbo()socomp_vdev_read()can drop the redundantdata_ready()check inside the read loop....
Quoted 8 lines
Quoted 8 lines
(quoted diff) +5
-2-static inline bool data_ready(struct most_video_dev *mdev) +static bool data_ready(struct most_video_dev *mdev) { - return !list_empty(&mdev->pending_mbos); + unsigned long flags; + bool ready; + + spin_lock_irqsave(&mdev->list_lock, flags);Quoted 8 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+ ready = !list_empty(&mdev->pending_mbos); + spin_unlock_irqrestore(&mdev->list_lock, flags); + + return ready;It doesn't really matter if we take the lock where since we drop it right away. The
list_empty()doesn't anything that requires locking so far as I can see.
Quoted 2 lines
Right, but the problem is that we possibly might compare valid pointer (list head itself) with the stale pointer (
head->nextand/orhead->prev).
It's fine. If you strip away the macros what the code is doing is.
drivers/staging/most/video/video.c +3 -1
@@ -62,7 +62,9 @@ static DEFINE_SPINLOCK(list_lock);static inline bool data_ready (struct most_video_dev *mdev) { -return !list_empty (&mdev->pending_mbos); +if (mdev->pending_mbos.next == mdev->pending_mbos.prev) +return false; +return true; }static inline struct mbo *get_top_mbo (struct most_video_dev *mdev)
The lock is unecessary, but also harmless. Why I don't like adding a lock is because we drop the lock before we return so the return value could be wrong by the time it gets to the caller. Nothing really relies on the return being correct. It's useful because we set the error code based on it, but if it's wrong the system isn't going to crash.
It's pretty common to do this sort of thing where we check in a racy way if we can return early, then we take a lock and check again to ensure that something is still true when it matters.
regards,
dan carpenter