Skip to content

Commit 5913320

Browse files
kawasakijwrdegoede
authored andcommitted
platform/x86: p2sb: Allow p2sb_bar() calls during PCI device probe
p2sb_bar() unhides P2SB device to get resources from the device. It guards the operation by locking pci_rescan_remove_lock so that parallel rescans do not find the P2SB device. However, this lock causes deadlock when PCI bus rescan is triggered by /sys/bus/pci/rescan. The rescan locks pci_rescan_remove_lock and probes PCI devices. When PCI devices call p2sb_bar() during probe, it locks pci_rescan_remove_lock again. Hence the deadlock. To avoid the deadlock, do not lock pci_rescan_remove_lock in p2sb_bar(). Instead, do the lock at fs_initcall. Introduce p2sb_cache_resources() for fs_initcall which gets and caches the P2SB resources. At p2sb_bar(), refer the cache and return to the caller. Before operating the device at P2SB DEVFN for resource cache, check that its device class is PCI_CLASS_MEMORY_OTHER 0x0580 that PCH specifications define. This avoids unexpected operation to other devices at the same DEVFN. Link: https://lore.kernel.org/linux-pci/6xb24fjmptxxn5js2fjrrddjae6twex5bjaftwqsuawuqqqydx@7cl3uik5ef6j/ Fixes: 9745fb0 ("platform/x86/intel: Add Primary to Sideband (P2SB) bridge support") Cc: [email protected] Suggested-by: Andy Shevchenko <[email protected]> Signed-off-by: Shin'ichiro Kawasaki <[email protected]> Link: https://lore.kernel.org/r/[email protected] Reviewed-by: Andy Shevchenko <[email protected]> Reviewed-by: Ilpo Järvinen <[email protected]> Tested-by Klara Modin <[email protected]> Reviewed-by: Hans de Goede <[email protected]> Signed-off-by: Hans de Goede <[email protected]>
1 parent 416de02 commit 5913320

File tree

1 file changed

+139
-41
lines changed

1 file changed

+139
-41
lines changed

drivers/platform/x86/p2sb.c

Lines changed: 139 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,21 @@ static const struct x86_cpu_id p2sb_cpu_ids[] = {
2626
{}
2727
};
2828

29+
/*
30+
* Cache BAR0 of P2SB device functions 0 to 7.
31+
* TODO: The constant 8 is the number of functions that PCI specification
32+
* defines. Same definitions exist tree-wide. Unify this definition and
33+
* the other definitions then move to include/uapi/linux/pci.h.
34+
*/
35+
#define NR_P2SB_RES_CACHE 8
36+
37+
struct p2sb_res_cache {
38+
u32 bus_dev_id;
39+
struct resource res;
40+
};
41+
42+
static struct p2sb_res_cache p2sb_resources[NR_P2SB_RES_CACHE];
43+
2944
static int p2sb_get_devfn(unsigned int *devfn)
3045
{
3146
unsigned int fn = P2SB_DEVFN_DEFAULT;
@@ -39,8 +54,16 @@ static int p2sb_get_devfn(unsigned int *devfn)
3954
return 0;
4055
}
4156

57+
static bool p2sb_valid_resource(struct resource *res)
58+
{
59+
if (res->flags)
60+
return true;
61+
62+
return false;
63+
}
64+
4265
/* Copy resource from the first BAR of the device in question */
43-
static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
66+
static void p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
4467
{
4568
struct resource *bar0 = &pdev->resource[0];
4669

@@ -56,89 +79,164 @@ static int p2sb_read_bar0(struct pci_dev *pdev, struct resource *mem)
5679
mem->end = bar0->end;
5780
mem->flags = bar0->flags;
5881
mem->desc = bar0->desc;
59-
60-
return 0;
6182
}
6283

63-
static int p2sb_scan_and_read(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
84+
static void p2sb_scan_and_cache_devfn(struct pci_bus *bus, unsigned int devfn)
6485
{
86+
struct p2sb_res_cache *cache = &p2sb_resources[PCI_FUNC(devfn)];
6587
struct pci_dev *pdev;
66-
int ret;
6788

6889
pdev = pci_scan_single_device(bus, devfn);
6990
if (!pdev)
70-
return -ENODEV;
91+
return;
7192

72-
ret = p2sb_read_bar0(pdev, mem);
93+
p2sb_read_bar0(pdev, &cache->res);
94+
cache->bus_dev_id = bus->dev.id;
7395

7496
pci_stop_and_remove_bus_device(pdev);
75-
return ret;
7697
}
7798

78-
/**
79-
* p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
80-
* @bus: PCI bus to communicate with
81-
* @devfn: PCI slot and function to communicate with
82-
* @mem: memory resource to be filled in
83-
*
84-
* The BIOS prevents the P2SB device from being enumerated by the PCI
85-
* subsystem, so we need to unhide and hide it back to lookup the BAR.
86-
*
87-
* if @bus is NULL, the bus 0 in domain 0 will be used.
88-
* If @devfn is 0, it will be replaced by devfn of the P2SB device.
89-
*
90-
* Caller must provide a valid pointer to @mem.
91-
*
92-
* Locking is handled by pci_rescan_remove_lock mutex.
93-
*
94-
* Return:
95-
* 0 on success or appropriate errno value on error.
96-
*/
97-
int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
99+
static int p2sb_scan_and_cache(struct pci_bus *bus, unsigned int devfn)
100+
{
101+
unsigned int slot, fn;
102+
103+
if (PCI_FUNC(devfn) == 0) {
104+
/*
105+
* When function number of the P2SB device is zero, scan it and
106+
* other function numbers, and if devices are available, cache
107+
* their BAR0s.
108+
*/
109+
slot = PCI_SLOT(devfn);
110+
for (fn = 0; fn < NR_P2SB_RES_CACHE; fn++)
111+
p2sb_scan_and_cache_devfn(bus, PCI_DEVFN(slot, fn));
112+
} else {
113+
/* Scan the P2SB device and cache its BAR0 */
114+
p2sb_scan_and_cache_devfn(bus, devfn);
115+
}
116+
117+
if (!p2sb_valid_resource(&p2sb_resources[PCI_FUNC(devfn)].res))
118+
return -ENOENT;
119+
120+
return 0;
121+
}
122+
123+
static struct pci_bus *p2sb_get_bus(struct pci_bus *bus)
124+
{
125+
static struct pci_bus *p2sb_bus;
126+
127+
bus = bus ?: p2sb_bus;
128+
if (bus)
129+
return bus;
130+
131+
/* Assume P2SB is on the bus 0 in domain 0 */
132+
p2sb_bus = pci_find_bus(0, 0);
133+
return p2sb_bus;
134+
}
135+
136+
static int p2sb_cache_resources(void)
98137
{
99-
struct pci_dev *pdev_p2sb;
100138
unsigned int devfn_p2sb;
101139
u32 value = P2SBC_HIDE;
140+
struct pci_bus *bus;
141+
u16 class;
102142
int ret;
103143

104144
/* Get devfn for P2SB device itself */
105145
ret = p2sb_get_devfn(&devfn_p2sb);
106146
if (ret)
107147
return ret;
108148

109-
/* if @bus is NULL, use bus 0 in domain 0 */
110-
bus = bus ?: pci_find_bus(0, 0);
149+
bus = p2sb_get_bus(NULL);
150+
if (!bus)
151+
return -ENODEV;
152+
153+
/*
154+
* When a device with same devfn exists and its device class is not
155+
* PCI_CLASS_MEMORY_OTHER for P2SB, do not touch it.
156+
*/
157+
pci_bus_read_config_word(bus, devfn_p2sb, PCI_CLASS_DEVICE, &class);
158+
if (!PCI_POSSIBLE_ERROR(class) && class != PCI_CLASS_MEMORY_OTHER)
159+
return -ENODEV;
111160

112161
/*
113162
* Prevent concurrent PCI bus scan from seeing the P2SB device and
114163
* removing via sysfs while it is temporarily exposed.
115164
*/
116165
pci_lock_rescan_remove();
117166

118-
/* Unhide the P2SB device, if needed */
167+
/*
168+
* The BIOS prevents the P2SB device from being enumerated by the PCI
169+
* subsystem, so we need to unhide and hide it back to lookup the BAR.
170+
* Unhide the P2SB device here, if needed.
171+
*/
119172
pci_bus_read_config_dword(bus, devfn_p2sb, P2SBC, &value);
120173
if (value & P2SBC_HIDE)
121174
pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, 0);
122175

123-
pdev_p2sb = pci_scan_single_device(bus, devfn_p2sb);
124-
if (devfn)
125-
ret = p2sb_scan_and_read(bus, devfn, mem);
126-
else
127-
ret = p2sb_read_bar0(pdev_p2sb, mem);
128-
pci_stop_and_remove_bus_device(pdev_p2sb);
176+
ret = p2sb_scan_and_cache(bus, devfn_p2sb);
129177

130178
/* Hide the P2SB device, if it was hidden */
131179
if (value & P2SBC_HIDE)
132180
pci_bus_write_config_dword(bus, devfn_p2sb, P2SBC, P2SBC_HIDE);
133181

134182
pci_unlock_rescan_remove();
135183

136-
if (ret)
137-
return ret;
184+
return ret;
185+
}
186+
187+
/**
188+
* p2sb_bar - Get Primary to Sideband (P2SB) bridge device BAR
189+
* @bus: PCI bus to communicate with
190+
* @devfn: PCI slot and function to communicate with
191+
* @mem: memory resource to be filled in
192+
*
193+
* If @bus is NULL, the bus 0 in domain 0 will be used.
194+
* If @devfn is 0, it will be replaced by devfn of the P2SB device.
195+
*
196+
* Caller must provide a valid pointer to @mem.
197+
*
198+
* Return:
199+
* 0 on success or appropriate errno value on error.
200+
*/
201+
int p2sb_bar(struct pci_bus *bus, unsigned int devfn, struct resource *mem)
202+
{
203+
struct p2sb_res_cache *cache;
204+
int ret;
205+
206+
bus = p2sb_get_bus(bus);
207+
if (!bus)
208+
return -ENODEV;
209+
210+
if (!devfn) {
211+
ret = p2sb_get_devfn(&devfn);
212+
if (ret)
213+
return ret;
214+
}
138215

139-
if (mem->flags == 0)
216+
cache = &p2sb_resources[PCI_FUNC(devfn)];
217+
if (cache->bus_dev_id != bus->dev.id)
140218
return -ENODEV;
141219

220+
if (!p2sb_valid_resource(&cache->res))
221+
return -ENOENT;
222+
223+
memcpy(mem, &cache->res, sizeof(*mem));
142224
return 0;
143225
}
144226
EXPORT_SYMBOL_GPL(p2sb_bar);
227+
228+
static int __init p2sb_fs_init(void)
229+
{
230+
p2sb_cache_resources();
231+
return 0;
232+
}
233+
234+
/*
235+
* pci_rescan_remove_lock to avoid access to unhidden P2SB devices can
236+
* not be locked in sysfs pci bus rescan path because of deadlock. To
237+
* avoid the deadlock, access to P2SB devices with the lock at an early
238+
* step in kernel initialization and cache required resources. This
239+
* should happen after subsys_initcall which initializes PCI subsystem
240+
* and before device_initcall which requires P2SB resources.
241+
*/
242+
fs_initcall(p2sb_fs_init);

0 commit comments

Comments
 (0)