Skip to content

Commit 44bda4b

Browse files
Hari Vyasbjorn-helgaas
authored andcommitted
PCI: Fix is_added/is_busmaster race condition
When a PCI device is detected, pdev->is_added is set to 1 and proc and sysfs entries are created. When the device is removed, pdev->is_added is checked for one and then device is detached with clearing of proc and sys entries and at end, pdev->is_added is set to 0. is_added and is_busmaster are bit fields in pci_dev structure sharing same memory location. A strange issue was observed with multiple removal and rescan of a PCIe NVMe device using sysfs commands where is_added flag was observed as zero instead of one while removing device and proc,sys entries are not cleared. This causes issue in later device addition with warning message "proc_dir_entry" already registered. Debugging revealed a race condition between the PCI core setting the is_added bit in pci_bus_add_device() and the NVMe driver reset work-queue setting the is_busmaster bit in pci_set_master(). As these fields are not handled atomically, that clears the is_added bit. Move the is_added bit to a separate private flag variable and use atomic functions to set and retrieve the device addition state. This avoids the race because is_added no longer shares a memory location with is_busmaster. Link: https://bugzilla.kernel.org/show_bug.cgi?id=200283 Signed-off-by: Hari Vyas <hari.vyas@broadcom.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com> Reviewed-by: Lukas Wunner <lukas@wunner.de> Acked-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent a54e43f commit 44bda4b

9 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎arch/powerpc/kernel/pci-common.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include <asm/ppc-pci.h>
4343
#include <asm/eeh.h>
4444

45+
#include "../../../drivers/pci/pci.h"
46+
4547
/* hose_spinlock protects accesses to the the phb_bitmap. */
4648
static DEFINE_SPINLOCK(hose_spinlock);
4749
LIST_HEAD(hose_list);
@@ -1014,7 +1016,7 @@ void pcibios_setup_bus_devices(struct pci_bus *bus)
10141016
/* Cardbus can call us to add new devices to a bus, so ignore
10151017
* those who are already fully discovered
10161018
*/
1017-
if (dev->is_added)
1019+
if (pci_dev_is_added(dev))
10181020
continue;
10191021

10201022
pcibios_setup_device(dev);

‎arch/powerpc/platforms/powernv/pci-ioda.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
#include "powernv.h"
4848
#include "pci.h"
49+
#include "../../../../drivers/pci/pci.h"
4950

5051
#define PNV_IODA1_M64_NUM 16 /* Number of M64 BARs */
5152
#define PNV_IODA1_M64_SEGS 8 /* Segments per M64 BAR */
@@ -3138,7 +3139,7 @@ static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
31383139
struct pci_dn *pdn;
31393140
int mul, total_vfs;
31403141

3141-
if (!pdev->is_physfn || pdev->is_added)
3142+
if (!pdev->is_physfn || pci_dev_is_added(pdev))
31423143
return;
31433144

31443145
pdn = pci_get_pdn(pdev);

‎arch/powerpc/platforms/pseries/setup.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include <asm/security_features.h>
7272

7373
#include "pseries.h"
74+
#include "../../../../drivers/pci/pci.h"
7475

7576
int CMO_PrPSP = -1;
7677
int CMO_SecPSP = -1;
@@ -664,7 +665,7 @@ static void pseries_pci_fixup_iov_resources(struct pci_dev *pdev)
664665
const int *indexes;
665666
struct device_node *dn = pci_device_to_OF_node(pdev);
666667

667-
if (!pdev->is_physfn || pdev->is_added)
668+
if (!pdev->is_physfn || pci_dev_is_added(pdev))
668669
return;
669670
/*Firmware must support open sriov otherwise dont configure*/
670671
indexes = of_get_property(dn, "ibm,open-sriov-vf-bar-info", NULL);

‎drivers/pci/bus.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ void pci_bus_add_device(struct pci_dev *dev)
330330
return;
331331
}
332332

333-
dev->is_added = 1;
333+
pci_dev_assign_added(dev, true);
334334
}
335335
EXPORT_SYMBOL_GPL(pci_bus_add_device);
336336

@@ -347,14 +347,14 @@ void pci_bus_add_devices(const struct pci_bus *bus)
347347

348348
list_for_each_entry(dev, &bus->devices, bus_list) {
349349
/* Skip already-added devices */
350-
if (dev->is_added)
350+
if (pci_dev_is_added(dev))
351351
continue;
352352
pci_bus_add_device(dev);
353353
}
354354

355355
list_for_each_entry(dev, &bus->devices, bus_list) {
356356
/* Skip if device attach failed */
357-
if (!dev->is_added)
357+
if (!pci_dev_is_added(dev))
358358
continue;
359359
child = dev->subordinate;
360360
if (child)

‎drivers/pci/hotplug/acpiphp_glue.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static void enable_slot(struct acpiphp_slot *slot)
509509

510510
list_for_each_entry(dev, &bus->devices, bus_list) {
511511
/* Assume that newly added devices are powered on already. */
512-
if (!dev->is_added)
512+
if (!pci_dev_is_added(dev))
513513
dev->current_state = PCI_D0;
514514
}
515515

‎drivers/pci/pci.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ struct pci_sriov {
288288

289289
/* pci_dev priv_flags */
290290
#define PCI_DEV_DISCONNECTED 0
291+
#define PCI_DEV_ADDED 1
291292

292293
static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
293294
{
@@ -300,6 +301,16 @@ static inline bool pci_dev_is_disconnected(const struct pci_dev *dev)
300301
return test_bit(PCI_DEV_DISCONNECTED, &dev->priv_flags);
301302
}
302303

304+
static inline void pci_dev_assign_added(struct pci_dev *dev, bool added)
305+
{
306+
assign_bit(PCI_DEV_ADDED, &dev->priv_flags, added);
307+
}
308+
309+
static inline bool pci_dev_is_added(const struct pci_dev *dev)
310+
{
311+
return test_bit(PCI_DEV_ADDED, &dev->priv_flags);
312+
}
313+
303314
#ifdef CONFIG_PCI_ATS
304315
void pci_restore_ats_state(struct pci_dev *dev);
305316
#else

‎drivers/pci/probe.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,13 +2433,13 @@ int pci_scan_slot(struct pci_bus *bus, int devfn)
24332433
dev = pci_scan_single_device(bus, devfn);
24342434
if (!dev)
24352435
return 0;
2436-
if (!dev->is_added)
2436+
if (!pci_dev_is_added(dev))
24372437
nr++;
24382438

24392439
for (fn = next_fn(bus, dev, 0); fn > 0; fn = next_fn(bus, dev, fn)) {
24402440
dev = pci_scan_single_device(bus, devfn + fn);
24412441
if (dev) {
2442-
if (!dev->is_added)
2442+
if (!pci_dev_is_added(dev))
24432443
nr++;
24442444
dev->multifunction = 1;
24452445
}

‎drivers/pci/remove.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ static void pci_stop_dev(struct pci_dev *dev)
1919
{
2020
pci_pme_active(dev, false);
2121

22-
if (dev->is_added) {
22+
if (pci_dev_is_added(dev)) {
2323
device_release_driver(&dev->dev);
2424
pci_proc_detach_device(dev);
2525
pci_remove_sysfs_dev_files(dev);
26-
dev->is_added = 0;
26+
27+
pci_dev_assign_added(dev, false);
2728
}
2829

2930
if (dev->bus->self)

‎include/linux/pci.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ struct pci_dev {
368368
unsigned int transparent:1; /* Subtractive decode bridge */
369369
unsigned int multifunction:1; /* Multi-function device */
370370

371-
unsigned int is_added:1;
372371
unsigned int is_busmaster:1; /* Is busmaster */
373372
unsigned int no_msi:1; /* May not use MSI */
374373
unsigned int no_64bit_msi:1; /* May only use 32-bit MSIs */

0 commit comments

Comments
 (0)