Skip to content

Commit 7ea7e98

Browse files
Matthew Wilcoxgregkh
authored andcommitted
PCI: Block on access to temporarily unavailable pci device
The existing implementation of pci_block_user_cfg_access() was recently criticised for providing out of date information and for returning errors on write, which applications won't be expecting. This reimplementation uses a global wait queue and a bit per device. I've open-coded prepare_to_wait() / finish_wait() as I could optimise it significantly by knowing that the pci_lock protected us at all points. It looked a bit funny to be doing a spin_unlock_irqsave(); schedule(), so I used spin_lock_irq() for the _user versions of pci_read_config and pci_write_config. Not carrying a flags pointer around made the code much less nasty. Attempts to block an already blocked device hit a BUG() and attempts to unblock an already unblocked device hit a WARN(). If we need to block access to a device from userspace, it's because it's unsafe for even another bit of the kernel to access the device. An attempt to block a device for a second time means we're about to access the device to perform some other operation, which could provoke undefined behaviour from the device. Signed-off-by: Matthew Wilcox <matthew@wil.cx> Acked-by: Adam Belay <abelay@novell.com> Acked-by: Alan Cox <alan@redhat.com> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
1 parent 50bf14b commit 7ea7e98

1 file changed

Lines changed: 47 additions & 28 deletions

File tree

‎drivers/pci/access.c‎

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <linux/pci.h>
22
#include <linux/module.h>
33
#include <linux/ioport.h>
4+
#include <linux/wait.h>
45

56
#include "pci.h"
67

@@ -63,30 +64,42 @@ EXPORT_SYMBOL(pci_bus_write_config_byte);
6364
EXPORT_SYMBOL(pci_bus_write_config_word);
6465
EXPORT_SYMBOL(pci_bus_write_config_dword);
6566

66-
static u32 pci_user_cached_config(struct pci_dev *dev, int pos)
67-
{
68-
u32 data;
67+
/*
68+
* The following routines are to prevent the user from accessing PCI config
69+
* space when it's unsafe to do so. Some devices require this during BIST and
70+
* we're required to prevent it during D-state transitions.
71+
*
72+
* We have a bit per device to indicate it's blocked and a global wait queue
73+
* for callers to sleep on until devices are unblocked.
74+
*/
75+
static DECLARE_WAIT_QUEUE_HEAD(pci_ucfg_wait);
6976

70-
data = dev->saved_config_space[pos/sizeof(dev->saved_config_space[0])];
71-
data >>= (pos % sizeof(dev->saved_config_space[0])) * 8;
72-
return data;
77+
static noinline void pci_wait_ucfg(struct pci_dev *dev)
78+
{
79+
DECLARE_WAITQUEUE(wait, current);
80+
81+
__add_wait_queue(&pci_ucfg_wait, &wait);
82+
do {
83+
set_current_state(TASK_UNINTERRUPTIBLE);
84+
spin_unlock_irq(&pci_lock);
85+
schedule();
86+
spin_lock_irq(&pci_lock);
87+
} while (dev->block_ucfg_access);
88+
__remove_wait_queue(&pci_ucfg_wait, &wait);
7389
}
7490

7591
#define PCI_USER_READ_CONFIG(size,type) \
7692
int pci_user_read_config_##size \
7793
(struct pci_dev *dev, int pos, type *val) \
7894
{ \
79-
unsigned long flags; \
8095
int ret = 0; \
8196
u32 data = -1; \
8297
if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
83-
spin_lock_irqsave(&pci_lock, flags); \
84-
if (likely(!dev->block_ucfg_access)) \
85-
ret = dev->bus->ops->read(dev->bus, dev->devfn, \
98+
spin_lock_irq(&pci_lock); \
99+
if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
100+
ret = dev->bus->ops->read(dev->bus, dev->devfn, \
86101
pos, sizeof(type), &data); \
87-
else if (pos < sizeof(dev->saved_config_space)) \
88-
data = pci_user_cached_config(dev, pos); \
89-
spin_unlock_irqrestore(&pci_lock, flags); \
102+
spin_unlock_irq(&pci_lock); \
90103
*val = (type)data; \
91104
return ret; \
92105
}
@@ -95,14 +108,13 @@ int pci_user_read_config_##size \
95108
int pci_user_write_config_##size \
96109
(struct pci_dev *dev, int pos, type val) \
97110
{ \
98-
unsigned long flags; \
99111
int ret = -EIO; \
100112
if (PCI_##size##_BAD) return PCIBIOS_BAD_REGISTER_NUMBER; \
101-
spin_lock_irqsave(&pci_lock, flags); \
102-
if (likely(!dev->block_ucfg_access)) \
103-
ret = dev->bus->ops->write(dev->bus, dev->devfn, \
113+
spin_lock_irq(&pci_lock); \
114+
if (unlikely(dev->block_ucfg_access)) pci_wait_ucfg(dev); \
115+
ret = dev->bus->ops->write(dev->bus, dev->devfn, \
104116
pos, sizeof(type), val); \
105-
spin_unlock_irqrestore(&pci_lock, flags); \
117+
spin_unlock_irq(&pci_lock); \
106118
return ret; \
107119
}
108120

@@ -117,21 +129,23 @@ PCI_USER_WRITE_CONFIG(dword, u32)
117129
* pci_block_user_cfg_access - Block userspace PCI config reads/writes
118130
* @dev: pci device struct
119131
*
120-
* This function blocks any userspace PCI config accesses from occurring.
121-
* When blocked, any writes will be bit bucketed and reads will return the
122-
* data saved using pci_save_state for the first 64 bytes of config
123-
* space and return 0xff for all other config reads.
124-
**/
132+
* When user access is blocked, any reads or writes to config space will
133+
* sleep until access is unblocked again. We don't allow nesting of
134+
* block/unblock calls.
135+
*/
125136
void pci_block_user_cfg_access(struct pci_dev *dev)
126137
{
127138
unsigned long flags;
139+
int was_blocked;
128140

129-
pci_save_state(dev);
130-
131-
/* spinlock to synchronize with anyone reading config space now */
132141
spin_lock_irqsave(&pci_lock, flags);
142+
was_blocked = dev->block_ucfg_access;
133143
dev->block_ucfg_access = 1;
134144
spin_unlock_irqrestore(&pci_lock, flags);
145+
146+
/* If we BUG() inside the pci_lock, we're guaranteed to hose
147+
* the machine */
148+
BUG_ON(was_blocked);
135149
}
136150
EXPORT_SYMBOL_GPL(pci_block_user_cfg_access);
137151

@@ -140,14 +154,19 @@ EXPORT_SYMBOL_GPL(pci_block_user_cfg_access);
140154
* @dev: pci device struct
141155
*
142156
* This function allows userspace PCI config accesses to resume.
143-
**/
157+
*/
144158
void pci_unblock_user_cfg_access(struct pci_dev *dev)
145159
{
146160
unsigned long flags;
147161

148-
/* spinlock to synchronize with anyone reading saved config space */
149162
spin_lock_irqsave(&pci_lock, flags);
163+
164+
/* This indicates a problem in the caller, but we don't need
165+
* to kill them, unlike a double-block above. */
166+
WARN_ON(!dev->block_ucfg_access);
167+
150168
dev->block_ucfg_access = 0;
169+
wake_up_all(&pci_ucfg_wait);
151170
spin_unlock_irqrestore(&pci_lock, flags);
152171
}
153172
EXPORT_SYMBOL_GPL(pci_unblock_user_cfg_access);

0 commit comments

Comments
 (0)