Skip to content

Commit

Permalink
PCI: Lock upstream bridge for pci_reset_function()
Browse files Browse the repository at this point in the history
Fix a long-standing locking gap for missing pci_cfg_access_lock() while
manipulating bridge reset registers and configuration during
pci_reset_bus_function().

If there is an upstream bridge, lock it before locking the device itself.
pci_dev_lock() calls pci_cfg_access_lock(), which blocks the writing of PCI
config space by user space.

Add lockdep assertion via pci_dev->cfg_access_lock to verify
pci_dev->block_cfg_access is set.

Co-developed-by: Dan Williams <dan.j.williams@intel.com>
Link: https://lore.kernel.org/r/20240502165851.1948523-3-dave.jiang@intel.com
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
Signed-off-by: Dave Jiang <dave.jiang@intel.com>
[bhelgaas: commit log]
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
  • Loading branch information
davejiang authored and bjorn-helgaas committed May 8, 2024
1 parent 962f1e7 commit 7e89efc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drivers/pci/access.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ void pci_cfg_access_lock(struct pci_dev *dev)
{
might_sleep();

lock_map_acquire(&dev->cfg_access_lock);

raw_spin_lock_irq(&pci_lock);
if (dev->block_cfg_access)
pci_wait_cfg(dev);
Expand Down Expand Up @@ -329,6 +331,8 @@ void pci_cfg_access_unlock(struct pci_dev *dev)
raw_spin_unlock_irqrestore(&pci_lock, flags);

wake_up_all(&pci_cfg_wait);

lock_map_release(&dev->cfg_access_lock);
}
EXPORT_SYMBOL_GPL(pci_cfg_access_unlock);

Expand Down
13 changes: 13 additions & 0 deletions drivers/pci/pci.c
Original file line number Diff line number Diff line change
Expand Up @@ -4879,6 +4879,7 @@ void __weak pcibios_reset_secondary_bus(struct pci_dev *dev)
*/
int pci_bridge_secondary_bus_reset(struct pci_dev *dev)
{
lock_map_assert_held(&dev->cfg_access_lock);
pcibios_reset_secondary_bus(dev);

return pci_bridge_wait_for_secondary_bus(dev, "bus reset");
Expand Down Expand Up @@ -5245,11 +5246,20 @@ void pci_init_reset_methods(struct pci_dev *dev)
*/
int pci_reset_function(struct pci_dev *dev)
{
struct pci_dev *bridge;
int rc;

if (!pci_reset_supported(dev))
return -ENOTTY;

/*
* If there's no upstream bridge, no locking is needed since there is
* no upstream bridge configuration to hold consistent.
*/
bridge = pci_upstream_bridge(dev);
if (bridge)
pci_dev_lock(bridge);

pci_dev_lock(dev);
pci_dev_save_and_disable(dev);

Expand All @@ -5258,6 +5268,9 @@ int pci_reset_function(struct pci_dev *dev)
pci_dev_restore(dev);
pci_dev_unlock(dev);

if (bridge)
pci_dev_unlock(bridge);

return rc;
}
EXPORT_SYMBOL_GPL(pci_reset_function);
Expand Down
3 changes: 3 additions & 0 deletions drivers/pci/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,9 @@ void pci_device_add(struct pci_dev *dev, struct pci_bus *bus)
dev->dev.dma_mask = &dev->dma_mask;
dev->dev.dma_parms = &dev->dma_parms;
dev->dev.coherent_dma_mask = 0xffffffffull;
lockdep_register_key(&dev->cfg_access_key);
lockdep_init_map(&dev->cfg_access_lock, dev_name(&dev->dev),
&dev->cfg_access_key, 0);

dma_set_max_seg_size(&dev->dev, 65536);
dma_set_seg_boundary(&dev->dev, 0xffffffff);
Expand Down
5 changes: 5 additions & 0 deletions include/linux/lockdep.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ extern void lock_unpin_lock(struct lockdep_map *lock, struct pin_cookie);
.wait_type_inner = _wait_type, \
.lock_type = LD_LOCK_WAIT_OVERRIDE, }

#define lock_map_assert_held(l) \
lockdep_assert(lock_is_held(l) != LOCK_STATE_NOT_HELD)

#else /* !CONFIG_LOCKDEP */

static inline void lockdep_init_task(struct task_struct *task)
Expand Down Expand Up @@ -388,6 +391,8 @@ extern int lockdep_is_held(const void *);
#define DEFINE_WAIT_OVERRIDE_MAP(_name, _wait_type) \
struct lockdep_map __maybe_unused _name = {}

#define lock_map_assert_held(l) do { (void)(l); } while (0)

#endif /* !LOCKDEP */

#ifdef CONFIG_PROVE_LOCKING
Expand Down
2 changes: 2 additions & 0 deletions include/linux/pci.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ struct pci_dev {
struct resource driver_exclusive_resource; /* driver exclusive resource ranges */

bool match_driver; /* Skip attaching driver */
struct lock_class_key cfg_access_key;
struct lockdep_map cfg_access_lock;

unsigned int transparent:1; /* Subtractive decode bridge */
unsigned int io_window:1; /* Bridge has I/O window */
Expand Down

0 comments on commit 7e89efc

Please sign in to comment.