Skip to content

Commit c3cb470

Browse files
jbeulichkonradwilk
authored andcommitted
xen-pciback: support wild cards in slot specifications
Particularly for hiding sets of SR-IOV devices, specifying them all individually is rather cumbersome. Therefore, allow function and slot numbers to be replaced by a wildcard character ('*'). Unfortunately this gets complicated by the in-kernel sscanf() implementation not being really standard conformant - matching of plain text tails cannot be checked by the caller (a patch to overcome this will be sent shortly, and a follow-up patch for simplifying the code is planned to be sent when that fixed went upstream). Signed-off-by: Jan Beulich <jbeulich@suse.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
1 parent ecc635f commit c3cb470

1 file changed

Lines changed: 82 additions & 7 deletions

File tree

‎drivers/xen/xen-pciback/pci_stub.c‎

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,41 @@ static inline int str_to_slot(const char *buf, int *domain, int *bus,
897897
int *slot, int *func)
898898
{
899899
int err;
900+
char wc = '*';
900901

901902
err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
902-
if (err == 4)
903+
switch (err) {
904+
case 3:
905+
*func = -1;
906+
err = sscanf(buf, " %x:%x:%x.%c", domain, bus, slot, &wc);
907+
break;
908+
case 2:
909+
*slot = *func = -1;
910+
err = sscanf(buf, " %x:%x:*.%c", domain, bus, &wc);
911+
if (err >= 2)
912+
++err;
913+
break;
914+
}
915+
if (err == 4 && wc == '*')
903916
return 0;
904917
else if (err < 0)
905918
return -EINVAL;
906919

907920
/* try again without domain */
908921
*domain = 0;
922+
wc = '*';
909923
err = sscanf(buf, " %x:%x.%x", bus, slot, func);
910-
if (err == 3)
924+
switch (err) {
925+
case 2:
926+
*func = -1;
927+
err = sscanf(buf, " %x:%x.%c", bus, slot, &wc);
928+
break;
929+
case 1:
930+
*slot = *func = -1;
931+
err = sscanf(buf, " %x:*.%c", bus, &wc) + 1;
932+
break;
933+
}
934+
if (err == 3 && wc == '*')
911935
return 0;
912936

913937
return -EINVAL;
@@ -930,6 +954,19 @@ static int pcistub_device_id_add(int domain, int bus, int slot, int func)
930954
{
931955
struct pcistub_device_id *pci_dev_id;
932956
unsigned long flags;
957+
int rc = 0;
958+
959+
if (slot < 0) {
960+
for (slot = 0; !rc && slot < 32; ++slot)
961+
rc = pcistub_device_id_add(domain, bus, slot, func);
962+
return rc;
963+
}
964+
965+
if (func < 0) {
966+
for (func = 0; !rc && func < 8; ++func)
967+
rc = pcistub_device_id_add(domain, bus, slot, func);
968+
return rc;
969+
}
933970

934971
pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
935972
if (!pci_dev_id)
@@ -952,15 +989,15 @@ static int pcistub_device_id_add(int domain, int bus, int slot, int func)
952989
static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
953990
{
954991
struct pcistub_device_id *pci_dev_id, *t;
955-
int devfn = PCI_DEVFN(slot, func);
956992
int err = -ENOENT;
957993
unsigned long flags;
958994

959995
spin_lock_irqsave(&device_ids_lock, flags);
960996
list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
961997
slot_list) {
962-
if (pci_dev_id->domain == domain
963-
&& pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
998+
if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
999+
&& (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
1000+
&& (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
9641001
/* Don't break; here because it's possible the same
9651002
* slot could be in the list more than once
9661003
*/
@@ -1216,6 +1253,10 @@ static ssize_t permissive_add(struct device_driver *drv, const char *buf,
12161253
err = str_to_slot(buf, &domain, &bus, &slot, &func);
12171254
if (err)
12181255
goto out;
1256+
if (slot < 0 || func < 0) {
1257+
err = -EINVAL;
1258+
goto out;
1259+
}
12191260
psdev = pcistub_device_find(domain, bus, slot, func);
12201261
if (!psdev) {
12211262
err = -ENODEV;
@@ -1297,17 +1338,51 @@ static int __init pcistub_init(void)
12971338

12981339
if (pci_devs_to_hide && *pci_devs_to_hide) {
12991340
do {
1341+
char wc = '*';
1342+
13001343
parsed = 0;
13011344

13021345
err = sscanf(pci_devs_to_hide + pos,
13031346
" (%x:%x:%x.%x) %n",
13041347
&domain, &bus, &slot, &func, &parsed);
1305-
if (err != 4) {
1348+
switch (err) {
1349+
case 3:
1350+
func = -1;
1351+
err = sscanf(pci_devs_to_hide + pos,
1352+
" (%x:%x:%x.%c) %n",
1353+
&domain, &bus, &slot, &wc,
1354+
&parsed);
1355+
break;
1356+
case 2:
1357+
slot = func = -1;
1358+
err = sscanf(pci_devs_to_hide + pos,
1359+
" (%x:%x:*.%c) %n",
1360+
&domain, &bus, &wc, &parsed) + 1;
1361+
break;
1362+
}
1363+
1364+
if (err != 4 || wc != '*') {
13061365
domain = 0;
1366+
wc = '*';
13071367
err = sscanf(pci_devs_to_hide + pos,
13081368
" (%x:%x.%x) %n",
13091369
&bus, &slot, &func, &parsed);
1310-
if (err != 3)
1370+
switch (err) {
1371+
case 2:
1372+
func = -1;
1373+
err = sscanf(pci_devs_to_hide + pos,
1374+
" (%x:%x.%c) %n",
1375+
&bus, &slot, &wc,
1376+
&parsed);
1377+
break;
1378+
case 1:
1379+
slot = func = -1;
1380+
err = sscanf(pci_devs_to_hide + pos,
1381+
" (%x:*.%c) %n",
1382+
&bus, &wc, &parsed) + 1;
1383+
break;
1384+
}
1385+
if (err != 3 || wc != '*')
13111386
goto parse_error;
13121387
}
13131388

0 commit comments

Comments
 (0)