]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
media: cobalt: fix race condition in setting HPD
authorHans Verkuil <hverkuil-cisco@xs4all.nl>
Fri, 23 Apr 2021 08:00:49 +0000 (10:00 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 20 Jul 2021 14:15:43 +0000 (16:15 +0200)
[ Upstream commit 3d37ef41bed0854805ab9af22c422267510e1344 ]

The cobalt_s_bit_sysctrl reads the old register value over PCI,
then changes a bit and sets writes the new value to the register.

This is used among other things for setting the HPD output pin.

But if the HPD is changed for multiple inputs at the same time,
then this causes a race condition where a stale value is read.

Serialize this function with a mutex.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/media/pci/cobalt/cobalt-driver.c
drivers/media/pci/cobalt/cobalt-driver.h

index 4885e833c05227999cfcb00266c88917c765472b..f422558e63920070615de2d6b709817688f9fdeb 100644 (file)
@@ -675,6 +675,7 @@ static int cobalt_probe(struct pci_dev *pci_dev,
                return -ENOMEM;
        cobalt->pci_dev = pci_dev;
        cobalt->instance = i;
+       mutex_init(&cobalt->pci_lock);
 
        retval = v4l2_device_register(&pci_dev->dev, &cobalt->v4l2_dev);
        if (retval) {
index 429bee4ef79c593e0c5a7d2e446154d35090b475..883093e5adea9d3239af732accd18b4fe187eec5 100644 (file)
@@ -250,6 +250,8 @@ struct cobalt {
        int instance;
        struct pci_dev *pci_dev;
        struct v4l2_device v4l2_dev;
+       /* serialize PCI access in cobalt_s_bit_sysctrl() */
+       struct mutex pci_lock;
 
        void __iomem *bar0, *bar1;
 
@@ -319,10 +321,13 @@ static inline u32 cobalt_g_sysctrl(struct cobalt *cobalt)
 static inline void cobalt_s_bit_sysctrl(struct cobalt *cobalt,
                                        int bit, int val)
 {
-       u32 ctrl = cobalt_read_bar1(cobalt, COBALT_SYS_CTRL_BASE);
+       u32 ctrl;
 
+       mutex_lock(&cobalt->pci_lock);
+       ctrl = cobalt_read_bar1(cobalt, COBALT_SYS_CTRL_BASE);
        cobalt_write_bar1(cobalt, COBALT_SYS_CTRL_BASE,
                        (ctrl & ~(1UL << bit)) | (val << bit));
+       mutex_unlock(&cobalt->pci_lock);
 }
 
 static inline u32 cobalt_g_sysstat(struct cobalt *cobalt)