hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/pci/pci-sysfs.c
....@@ -156,7 +156,8 @@
156156 {
157157 struct pci_dev *pdev = to_pci_dev(dev);
158158
159
- return sprintf(buf, "%s\n", PCIE_SPEED2STR(pcie_get_speed_cap(pdev)));
159
+ return sprintf(buf, "%s\n",
160
+ pci_speed_string(pcie_get_speed_cap(pdev)));
160161 }
161162 static DEVICE_ATTR_RO(max_link_speed);
162163
....@@ -175,30 +176,15 @@
175176 struct pci_dev *pci_dev = to_pci_dev(dev);
176177 u16 linkstat;
177178 int err;
178
- const char *speed;
179
+ enum pci_bus_speed speed;
179180
180181 err = pcie_capability_read_word(pci_dev, PCI_EXP_LNKSTA, &linkstat);
181182 if (err)
182183 return -EINVAL;
183184
184
- switch (linkstat & PCI_EXP_LNKSTA_CLS) {
185
- case PCI_EXP_LNKSTA_CLS_16_0GB:
186
- speed = "16 GT/s";
187
- break;
188
- case PCI_EXP_LNKSTA_CLS_8_0GB:
189
- speed = "8 GT/s";
190
- break;
191
- case PCI_EXP_LNKSTA_CLS_5_0GB:
192
- speed = "5 GT/s";
193
- break;
194
- case PCI_EXP_LNKSTA_CLS_2_5GB:
195
- speed = "2.5 GT/s";
196
- break;
197
- default:
198
- speed = "Unknown speed";
199
- }
185
+ speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
200186
201
- return sprintf(buf, "%s\n", speed);
187
+ return sprintf(buf, "%s\n", pci_speed_string(speed));
202188 }
203189 static DEVICE_ATTR_RO(current_link_speed);
204190
....@@ -412,8 +398,7 @@
412398 }
413399 static DEVICE_ATTR_RW(msi_bus);
414400
415
-static ssize_t bus_rescan_store(struct bus_type *bus, const char *buf,
416
- size_t count)
401
+static ssize_t rescan_store(struct bus_type *bus, const char *buf, size_t count)
417402 {
418403 unsigned long val;
419404 struct pci_bus *b = NULL;
....@@ -429,7 +414,7 @@
429414 }
430415 return count;
431416 }
432
-static BUS_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, bus_rescan_store);
417
+static BUS_ATTR_WO(rescan);
433418
434419 static struct attribute *pci_bus_attrs[] = {
435420 &bus_attr_rescan.attr,
....@@ -462,9 +447,8 @@
462447 }
463448 return count;
464449 }
465
-static struct device_attribute dev_rescan_attr = __ATTR(rescan,
466
- (S_IWUSR|S_IWGRP),
467
- NULL, dev_rescan_store);
450
+static struct device_attribute dev_attr_dev_rescan = __ATTR(rescan, 0200, NULL,
451
+ dev_rescan_store);
468452
469453 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
470454 const char *buf, size_t count)
....@@ -478,13 +462,12 @@
478462 pci_stop_and_remove_bus_device_locked(to_pci_dev(dev));
479463 return count;
480464 }
481
-static struct device_attribute dev_remove_attr = __ATTR_IGNORE_LOCKDEP(remove,
482
- (S_IWUSR|S_IWGRP),
483
- NULL, remove_store);
465
+static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0220, NULL,
466
+ remove_store);
484467
485
-static ssize_t dev_bus_rescan_store(struct device *dev,
486
- struct device_attribute *attr,
487
- const char *buf, size_t count)
468
+static ssize_t bus_rescan_store(struct device *dev,
469
+ struct device_attribute *attr,
470
+ const char *buf, size_t count)
488471 {
489472 unsigned long val;
490473 struct pci_bus *bus = to_pci_bus(dev);
....@@ -502,7 +485,8 @@
502485 }
503486 return count;
504487 }
505
-static DEVICE_ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_bus_rescan_store);
488
+static struct device_attribute dev_attr_bus_rescan = __ATTR(rescan, 0200, NULL,
489
+ bus_rescan_store);
506490
507491 #if defined(CONFIG_PM) && defined(CONFIG_ACPI)
508492 static ssize_t d3cold_allowed_store(struct device *dev,
....@@ -549,154 +533,6 @@
549533 static DEVICE_ATTR_RO(devspec);
550534 #endif
551535
552
-#ifdef CONFIG_PCI_IOV
553
-static ssize_t sriov_totalvfs_show(struct device *dev,
554
- struct device_attribute *attr,
555
- char *buf)
556
-{
557
- struct pci_dev *pdev = to_pci_dev(dev);
558
-
559
- return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev));
560
-}
561
-
562
-
563
-static ssize_t sriov_numvfs_show(struct device *dev,
564
- struct device_attribute *attr,
565
- char *buf)
566
-{
567
- struct pci_dev *pdev = to_pci_dev(dev);
568
-
569
- return sprintf(buf, "%u\n", pdev->sriov->num_VFs);
570
-}
571
-
572
-/*
573
- * num_vfs > 0; number of VFs to enable
574
- * num_vfs = 0; disable all VFs
575
- *
576
- * Note: SRIOV spec doesn't allow partial VF
577
- * disable, so it's all or none.
578
- */
579
-static ssize_t sriov_numvfs_store(struct device *dev,
580
- struct device_attribute *attr,
581
- const char *buf, size_t count)
582
-{
583
- struct pci_dev *pdev = to_pci_dev(dev);
584
- int ret;
585
- u16 num_vfs;
586
-
587
- ret = kstrtou16(buf, 0, &num_vfs);
588
- if (ret < 0)
589
- return ret;
590
-
591
- if (num_vfs > pci_sriov_get_totalvfs(pdev))
592
- return -ERANGE;
593
-
594
- device_lock(&pdev->dev);
595
-
596
- if (num_vfs == pdev->sriov->num_VFs)
597
- goto exit;
598
-
599
- /* is PF driver loaded w/callback */
600
- if (!pdev->driver || !pdev->driver->sriov_configure) {
601
- pci_info(pdev, "Driver doesn't support SRIOV configuration via sysfs\n");
602
- ret = -ENOENT;
603
- goto exit;
604
- }
605
-
606
- if (num_vfs == 0) {
607
- /* disable VFs */
608
- ret = pdev->driver->sriov_configure(pdev, 0);
609
- goto exit;
610
- }
611
-
612
- /* enable VFs */
613
- if (pdev->sriov->num_VFs) {
614
- pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n",
615
- pdev->sriov->num_VFs, num_vfs);
616
- ret = -EBUSY;
617
- goto exit;
618
- }
619
-
620
- ret = pdev->driver->sriov_configure(pdev, num_vfs);
621
- if (ret < 0)
622
- goto exit;
623
-
624
- if (ret != num_vfs)
625
- pci_warn(pdev, "%d VFs requested; only %d enabled\n",
626
- num_vfs, ret);
627
-
628
-exit:
629
- device_unlock(&pdev->dev);
630
-
631
- if (ret < 0)
632
- return ret;
633
-
634
- return count;
635
-}
636
-
637
-static ssize_t sriov_offset_show(struct device *dev,
638
- struct device_attribute *attr,
639
- char *buf)
640
-{
641
- struct pci_dev *pdev = to_pci_dev(dev);
642
-
643
- return sprintf(buf, "%u\n", pdev->sriov->offset);
644
-}
645
-
646
-static ssize_t sriov_stride_show(struct device *dev,
647
- struct device_attribute *attr,
648
- char *buf)
649
-{
650
- struct pci_dev *pdev = to_pci_dev(dev);
651
-
652
- return sprintf(buf, "%u\n", pdev->sriov->stride);
653
-}
654
-
655
-static ssize_t sriov_vf_device_show(struct device *dev,
656
- struct device_attribute *attr,
657
- char *buf)
658
-{
659
- struct pci_dev *pdev = to_pci_dev(dev);
660
-
661
- return sprintf(buf, "%x\n", pdev->sriov->vf_device);
662
-}
663
-
664
-static ssize_t sriov_drivers_autoprobe_show(struct device *dev,
665
- struct device_attribute *attr,
666
- char *buf)
667
-{
668
- struct pci_dev *pdev = to_pci_dev(dev);
669
-
670
- return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe);
671
-}
672
-
673
-static ssize_t sriov_drivers_autoprobe_store(struct device *dev,
674
- struct device_attribute *attr,
675
- const char *buf, size_t count)
676
-{
677
- struct pci_dev *pdev = to_pci_dev(dev);
678
- bool drivers_autoprobe;
679
-
680
- if (kstrtobool(buf, &drivers_autoprobe) < 0)
681
- return -EINVAL;
682
-
683
- pdev->sriov->drivers_autoprobe = drivers_autoprobe;
684
-
685
- return count;
686
-}
687
-
688
-static struct device_attribute sriov_totalvfs_attr = __ATTR_RO(sriov_totalvfs);
689
-static struct device_attribute sriov_numvfs_attr =
690
- __ATTR(sriov_numvfs, (S_IRUGO|S_IWUSR|S_IWGRP),
691
- sriov_numvfs_show, sriov_numvfs_store);
692
-static struct device_attribute sriov_offset_attr = __ATTR_RO(sriov_offset);
693
-static struct device_attribute sriov_stride_attr = __ATTR_RO(sriov_stride);
694
-static struct device_attribute sriov_vf_device_attr = __ATTR_RO(sriov_vf_device);
695
-static struct device_attribute sriov_drivers_autoprobe_attr =
696
- __ATTR(sriov_drivers_autoprobe, (S_IRUGO|S_IWUSR|S_IWGRP),
697
- sriov_drivers_autoprobe_show, sriov_drivers_autoprobe_store);
698
-#endif /* CONFIG_PCI_IOV */
699
-
700536 static ssize_t driver_override_store(struct device *dev,
701537 struct device_attribute *attr,
702538 const char *buf, size_t count)
....@@ -738,7 +574,7 @@
738574 ssize_t len;
739575
740576 device_lock(dev);
741
- len = snprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
577
+ len = scnprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override);
742578 device_unlock(dev);
743579 return len;
744580 }
....@@ -790,7 +626,7 @@
790626 };
791627
792628 static struct attribute *pcibus_attrs[] = {
793
- &dev_attr_rescan.attr,
629
+ &dev_attr_bus_rescan.attr,
794630 &dev_attr_cpuaffinity.attr,
795631 &dev_attr_cpulistaffinity.attr,
796632 NULL,
....@@ -818,7 +654,7 @@
818654 !!(pdev->resource[PCI_ROM_RESOURCE].flags &
819655 IORESOURCE_ROM_SHADOW));
820656 }
821
-static struct device_attribute vga_attr = __ATTR_RO(boot_vga);
657
+static DEVICE_ATTR_RO(boot_vga);
822658
823659 static ssize_t pci_read_config(struct file *filp, struct kobject *kobj,
824660 struct bin_attribute *bin_attr, char *buf,
....@@ -872,6 +708,7 @@
872708 data[off - init_off + 3] = (val >> 24) & 0xff;
873709 off += 4;
874710 size -= 4;
711
+ cond_resched();
875712 }
876713
877714 if (size >= 2) {
....@@ -904,6 +741,11 @@
904741 unsigned int size = count;
905742 loff_t init_off = off;
906743 u8 *data = (u8 *) buf;
744
+ int ret;
745
+
746
+ ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
747
+ if (ret)
748
+ return ret;
907749
908750 if (off > dev->cfg_size)
909751 return 0;
....@@ -1083,7 +925,7 @@
1083925 sysfs_bin_attr_init(b->legacy_io);
1084926 b->legacy_io->attr.name = "legacy_io";
1085927 b->legacy_io->size = 0xffff;
1086
- b->legacy_io->attr.mode = S_IRUSR | S_IWUSR;
928
+ b->legacy_io->attr.mode = 0600;
1087929 b->legacy_io->read = pci_read_legacy_io;
1088930 b->legacy_io->write = pci_write_legacy_io;
1089931 b->legacy_io->mmap = pci_mmap_legacy_io;
....@@ -1097,7 +939,7 @@
1097939 sysfs_bin_attr_init(b->legacy_mem);
1098940 b->legacy_mem->attr.name = "legacy_mem";
1099941 b->legacy_mem->size = 1024*1024;
1100
- b->legacy_mem->attr.mode = S_IRUSR | S_IWUSR;
942
+ b->legacy_mem->attr.mode = 0600;
1101943 b->legacy_mem->mmap = pci_mmap_legacy_mem;
1102944 pci_adjust_legacy_attr(b, pci_mmap_mem);
1103945 error = device_create_bin_file(&b->dev, b->legacy_mem);
....@@ -1112,8 +954,7 @@
1112954 kfree(b->legacy_io);
1113955 b->legacy_io = NULL;
1114956 kzalloc_err:
1115
- printk(KERN_WARNING "pci: warning: could not create legacy I/O port and ISA memory resources to sysfs\n");
1116
- return;
957
+ dev_warn(&b->dev, "could not create legacy I/O port and ISA memory resources in sysfs\n");
1117958 }
1118959
1119960 void pci_remove_legacy_files(struct pci_bus *b)
....@@ -1166,6 +1007,11 @@
11661007 int bar = (unsigned long)attr->private;
11671008 enum pci_mmap_state mmap_type;
11681009 struct resource *res = &pdev->resource[bar];
1010
+ int ret;
1011
+
1012
+ ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1013
+ if (ret)
1014
+ return ret;
11691015
11701016 if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
11711017 return -EINVAL;
....@@ -1242,6 +1088,12 @@
12421088 struct bin_attribute *attr, char *buf,
12431089 loff_t off, size_t count)
12441090 {
1091
+ int ret;
1092
+
1093
+ ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
1094
+ if (ret)
1095
+ return ret;
1096
+
12451097 return pci_resource_io(filp, kobj, attr, buf, off, count, true);
12461098 }
12471099
....@@ -1256,7 +1108,7 @@
12561108 {
12571109 int i;
12581110
1259
- for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1111
+ for (i = 0; i < PCI_STD_NUM_BARS; i++) {
12601112 struct bin_attribute *res_attr;
12611113
12621114 res_attr = pdev->res_attr[i];
....@@ -1305,7 +1157,7 @@
13051157 }
13061158 }
13071159 res_attr->attr.name = res_attr_name;
1308
- res_attr->attr.mode = S_IRUSR | S_IWUSR;
1160
+ res_attr->attr.mode = 0600;
13091161 res_attr->size = pci_resource_len(pdev, num);
13101162 res_attr->private = (void *)(unsigned long)num;
13111163 retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
....@@ -1327,7 +1179,7 @@
13271179 int retval;
13281180
13291181 /* Expose the PCI resources from this device as files */
1330
- for (i = 0; i < PCI_ROM_RESOURCE; i++) {
1182
+ for (i = 0; i < PCI_STD_NUM_BARS; i++) {
13311183
13321184 /* skip empty resources */
13331185 if (!pci_resource_len(pdev, i))
....@@ -1345,10 +1197,10 @@
13451197 }
13461198 return 0;
13471199 }
1348
-#else /* !HAVE_PCI_MMAP */
1200
+#else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
13491201 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
13501202 void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
1351
-#endif /* HAVE_PCI_MMAP */
1203
+#endif
13521204
13531205 /**
13541206 * pci_write_rom - used to enable access to the PCI ROM display
....@@ -1418,7 +1270,7 @@
14181270 static const struct bin_attribute pci_config_attr = {
14191271 .attr = {
14201272 .name = "config",
1421
- .mode = S_IRUGO | S_IWUSR,
1273
+ .mode = 0644,
14221274 },
14231275 .size = PCI_CFG_SPACE_SIZE,
14241276 .read = pci_read_config,
....@@ -1428,7 +1280,7 @@
14281280 static const struct bin_attribute pcie_config_attr = {
14291281 .attr = {
14301282 .name = "config",
1431
- .mode = S_IRUGO | S_IWUSR,
1283
+ .mode = 0644,
14321284 },
14331285 .size = PCI_CFG_SPACE_EXP_SIZE,
14341286 .read = pci_read_config,
....@@ -1457,24 +1309,22 @@
14571309 return count;
14581310 }
14591311
1460
-static struct device_attribute reset_attr = __ATTR(reset, 0200, NULL, reset_store);
1312
+static DEVICE_ATTR(reset, 0200, NULL, reset_store);
14611313
14621314 static int pci_create_capabilities_sysfs(struct pci_dev *dev)
14631315 {
14641316 int retval;
14651317
14661318 pcie_vpd_create_sysfs_dev_files(dev);
1467
- pcie_aspm_create_sysfs_dev_files(dev);
14681319
14691320 if (dev->reset_fn) {
1470
- retval = device_create_file(&dev->dev, &reset_attr);
1321
+ retval = device_create_file(&dev->dev, &dev_attr_reset);
14711322 if (retval)
14721323 goto error;
14731324 }
14741325 return 0;
14751326
14761327 error:
1477
- pcie_aspm_remove_sysfs_dev_files(dev);
14781328 pcie_vpd_remove_sysfs_dev_files(dev);
14791329 return retval;
14801330 }
....@@ -1487,6 +1337,11 @@
14871337
14881338 if (!sysfs_initialized)
14891339 return -EACCES;
1340
+
1341
+#ifdef CONFIG_NO_GKI
1342
+ if (atomic_cmpxchg(&pdev->sysfs_init_cnt, 0, 1) == 1)
1343
+ return 0; /* already added */
1344
+#endif
14901345
14911346 if (pdev->cfg_size > PCI_CFG_SPACE_SIZE)
14921347 retval = sysfs_create_bin_file(&pdev->dev.kobj, &pcie_config_attr);
....@@ -1510,7 +1365,7 @@
15101365 sysfs_bin_attr_init(attr);
15111366 attr->size = rom_size;
15121367 attr->attr.name = "rom";
1513
- attr->attr.mode = S_IRUSR | S_IWUSR;
1368
+ attr->attr.mode = 0600;
15141369 attr->read = pci_read_rom;
15151370 attr->write = pci_write_rom;
15161371 retval = sysfs_create_bin_file(&pdev->dev.kobj, attr);
....@@ -1550,9 +1405,8 @@
15501405 static void pci_remove_capabilities_sysfs(struct pci_dev *dev)
15511406 {
15521407 pcie_vpd_remove_sysfs_dev_files(dev);
1553
- pcie_aspm_remove_sysfs_dev_files(dev);
15541408 if (dev->reset_fn) {
1555
- device_remove_file(&dev->dev, &reset_attr);
1409
+ device_remove_file(&dev->dev, &dev_attr_reset);
15561410 dev->reset_fn = 0;
15571411 }
15581412 }
....@@ -1567,6 +1421,11 @@
15671421 {
15681422 if (!sysfs_initialized)
15691423 return;
1424
+
1425
+#ifdef CONFIG_NO_GKI
1426
+ if (atomic_cmpxchg(&pdev->sysfs_init_cnt, 1, 0) == 0)
1427
+ return; /* already removed */
1428
+#endif
15701429
15711430 pci_remove_capabilities_sysfs(pdev);
15721431
....@@ -1605,7 +1464,7 @@
16051464 late_initcall(pci_sysfs_init);
16061465
16071466 static struct attribute *pci_dev_dev_attrs[] = {
1608
- &vga_attr.attr,
1467
+ &dev_attr_boot_vga.attr,
16091468 NULL,
16101469 };
16111470
....@@ -1615,7 +1474,7 @@
16151474 struct device *dev = kobj_to_dev(kobj);
16161475 struct pci_dev *pdev = to_pci_dev(dev);
16171476
1618
- if (a == &vga_attr.attr)
1477
+ if (a == &dev_attr_boot_vga.attr)
16191478 if ((pdev->class >> 8) != PCI_CLASS_DISPLAY_VGA)
16201479 return 0;
16211480
....@@ -1623,8 +1482,8 @@
16231482 }
16241483
16251484 static struct attribute *pci_dev_hp_attrs[] = {
1626
- &dev_remove_attr.attr,
1627
- &dev_rescan_attr.attr,
1485
+ &dev_attr_remove.attr,
1486
+ &dev_attr_dev_rescan.attr,
16281487 NULL,
16291488 };
16301489
....@@ -1673,56 +1532,10 @@
16731532 NULL,
16741533 };
16751534
1676
-static const struct attribute_group pci_bridge_group = {
1677
- .attrs = pci_bridge_attrs,
1678
-};
1679
-
1680
-const struct attribute_group *pci_bridge_groups[] = {
1681
- &pci_bridge_group,
1682
- NULL,
1683
-};
1684
-
1685
-static const struct attribute_group pcie_dev_group = {
1686
- .attrs = pcie_dev_attrs,
1687
-};
1688
-
1689
-const struct attribute_group *pcie_dev_groups[] = {
1690
- &pcie_dev_group,
1691
- NULL,
1692
-};
1693
-
16941535 static const struct attribute_group pci_dev_hp_attr_group = {
16951536 .attrs = pci_dev_hp_attrs,
16961537 .is_visible = pci_dev_hp_attrs_are_visible,
16971538 };
1698
-
1699
-#ifdef CONFIG_PCI_IOV
1700
-static struct attribute *sriov_dev_attrs[] = {
1701
- &sriov_totalvfs_attr.attr,
1702
- &sriov_numvfs_attr.attr,
1703
- &sriov_offset_attr.attr,
1704
- &sriov_stride_attr.attr,
1705
- &sriov_vf_device_attr.attr,
1706
- &sriov_drivers_autoprobe_attr.attr,
1707
- NULL,
1708
-};
1709
-
1710
-static umode_t sriov_attrs_are_visible(struct kobject *kobj,
1711
- struct attribute *a, int n)
1712
-{
1713
- struct device *dev = kobj_to_dev(kobj);
1714
-
1715
- if (!dev_is_pf(dev))
1716
- return 0;
1717
-
1718
- return a->mode;
1719
-}
1720
-
1721
-static const struct attribute_group sriov_dev_attr_group = {
1722
- .attrs = sriov_dev_attrs,
1723
- .is_visible = sriov_attrs_are_visible,
1724
-};
1725
-#endif /* CONFIG_PCI_IOV */
17261539
17271540 static const struct attribute_group pci_dev_attr_group = {
17281541 .attrs = pci_dev_dev_attrs,
....@@ -1750,6 +1563,9 @@
17501563 #ifdef CONFIG_PCIEAER
17511564 &aer_stats_attr_group,
17521565 #endif
1566
+#ifdef CONFIG_PCIEASPM
1567
+ &aspm_ctrl_attr_group,
1568
+#endif
17531569 NULL,
17541570 };
17551571