Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

virtio-pci: alloc only resources actually used.

Move resource allocation from common code to legacy and modern code.
Only request resources actually used, i.e. bar0 in legacy mode and
the bar(s) specified by capabilities in modern mode.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>

authored by

Gerd Hoffmann and committed by
Michael S. Tsirkin
59a5b0f7 8b8e658b

+32 -14
-7
drivers/virtio/virtio_pci_common.c
··· 509 509 if (rc) 510 510 goto err_enable_device; 511 511 512 - rc = pci_request_regions(pci_dev, "virtio-pci"); 513 - if (rc) 514 - goto err_request_regions; 515 - 516 512 if (force_legacy) { 517 513 rc = virtio_pci_legacy_probe(vp_dev); 518 514 /* Also try modern mode if we can't map BAR0 (no IO space). */ ··· 538 542 else 539 543 virtio_pci_modern_remove(vp_dev); 540 544 err_probe: 541 - pci_release_regions(pci_dev); 542 - err_request_regions: 543 545 pci_disable_device(pci_dev); 544 546 err_enable_device: 545 547 kfree(vp_dev); ··· 555 561 else 556 562 virtio_pci_modern_remove(vp_dev); 557 563 558 - pci_release_regions(pci_dev); 559 564 pci_disable_device(pci_dev); 560 565 } 561 566
+2
drivers/virtio/virtio_pci_common.h
··· 75 75 /* Multiply queue_notify_off by this value. (non-legacy mode). */ 76 76 u32 notify_offset_multiplier; 77 77 78 + int modern_bars; 79 + 78 80 /* Legacy only field */ 79 81 /* the IO mapping for the PCI config space */ 80 82 void __iomem *ioaddr;
+12 -1
drivers/virtio/virtio_pci_legacy.c
··· 215 215 int virtio_pci_legacy_probe(struct virtio_pci_device *vp_dev) 216 216 { 217 217 struct pci_dev *pci_dev = vp_dev->pci_dev; 218 + int rc; 218 219 219 220 /* We only own devices >= 0x1000 and <= 0x103f: leave the rest. */ 220 221 if (pci_dev->device < 0x1000 || pci_dev->device > 0x103f) ··· 227 226 return -ENODEV; 228 227 } 229 228 229 + rc = pci_request_region(pci_dev, 0, "virtio-pci-legacy"); 230 + if (rc) 231 + return rc; 232 + 233 + rc = -ENOMEM; 230 234 vp_dev->ioaddr = pci_iomap(pci_dev, 0, 0); 231 235 if (!vp_dev->ioaddr) 232 - return -ENOMEM; 236 + goto err_iomap; 233 237 234 238 vp_dev->isr = vp_dev->ioaddr + VIRTIO_PCI_ISR; 235 239 ··· 252 246 vp_dev->del_vq = del_vq; 253 247 254 248 return 0; 249 + 250 + err_iomap: 251 + pci_release_region(pci_dev, 0); 252 + return rc; 255 253 } 256 254 257 255 void virtio_pci_legacy_remove(struct virtio_pci_device *vp_dev) ··· 263 253 struct pci_dev *pci_dev = vp_dev->pci_dev; 264 254 265 255 pci_iounmap(pci_dev, vp_dev->ioaddr); 256 + pci_release_region(pci_dev, 0); 266 257 }
+18 -6
drivers/virtio/virtio_pci_modern.c
··· 499 499 * Returns offset of the capability, or 0. 500 500 */ 501 501 static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type, 502 - u32 ioresource_types) 502 + u32 ioresource_types, int *bars) 503 503 { 504 504 int pos; 505 505 ··· 520 520 521 521 if (type == cfg_type) { 522 522 if (pci_resource_len(dev, bar) && 523 - pci_resource_flags(dev, bar) & ioresource_types) 523 + pci_resource_flags(dev, bar) & ioresource_types) { 524 + *bars |= (1 << bar); 524 525 return pos; 526 + } 525 527 } 526 528 } 527 529 return 0; ··· 619 617 620 618 /* check for a common config: if not, use legacy mode (bar 0). */ 621 619 common = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_COMMON_CFG, 622 - IORESOURCE_IO | IORESOURCE_MEM); 620 + IORESOURCE_IO | IORESOURCE_MEM, 621 + &vp_dev->modern_bars); 623 622 if (!common) { 624 623 dev_info(&pci_dev->dev, 625 624 "virtio_pci: leaving for legacy driver\n"); ··· 629 626 630 627 /* If common is there, these should be too... */ 631 628 isr = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_ISR_CFG, 632 - IORESOURCE_IO | IORESOURCE_MEM); 629 + IORESOURCE_IO | IORESOURCE_MEM, 630 + &vp_dev->modern_bars); 633 631 notify = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_NOTIFY_CFG, 634 - IORESOURCE_IO | IORESOURCE_MEM); 632 + IORESOURCE_IO | IORESOURCE_MEM, 633 + &vp_dev->modern_bars); 635 634 if (!isr || !notify) { 636 635 dev_err(&pci_dev->dev, 637 636 "virtio_pci: missing capabilities %i/%i/%i\n", ··· 645 640 * device-specific configuration. 646 641 */ 647 642 device = virtio_pci_find_capability(pci_dev, VIRTIO_PCI_CAP_DEVICE_CFG, 648 - IORESOURCE_IO | IORESOURCE_MEM); 643 + IORESOURCE_IO | IORESOURCE_MEM, 644 + &vp_dev->modern_bars); 645 + 646 + err = pci_request_selected_regions(pci_dev, vp_dev->modern_bars, 647 + "virtio-pci-modern"); 648 + if (err) 649 + return err; 649 650 650 651 err = -EINVAL; 651 652 vp_dev->common = map_capability(pci_dev, common, ··· 738 727 pci_iounmap(pci_dev, vp_dev->notify_base); 739 728 pci_iounmap(pci_dev, vp_dev->isr); 740 729 pci_iounmap(pci_dev, vp_dev->common); 730 + pci_release_selected_regions(pci_dev, vp_dev->modern_bars); 741 731 }