| @@ -37,6 +37,10 @@ module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR); · 36 unmodified lines |
| 37 | 37 | MODULE_PARM_DESC(nointxmask, |
| 38 | 38 | "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag."); |
| 39 | 39 | |
| 40 | +static DEFINE_MUTEX(driver_lock); |
| 41 | + |
| 42 | +static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev); |
| 43 | + |
| 40 | 44 | static int vfio_pci_enable(struct vfio_pci_device *vdev) |
| 41 | 45 | { |
| 42 | 46 | struct pci_dev *pdev = vdev->pdev; |
| @@ -44,6 +48,9 @@ static int vfio_pci_enable(struct vfio_pci_device *vdev) · 1 unmodified line |
| 44 | 48 | u16 cmd; |
| 45 | 49 | u8 msix_pos; |
| 46 | 50 | |
| 51 | + /* Don't allow our initial saved state to include busmaster */ |
| 52 | + pci_clear_master(pdev); |
| 53 | + |
| 47 | 54 | ret = pci_enable_device(pdev); |
| 48 | 55 | if (ret) |
| 49 | 56 | return ret; |
| @@ -99,7 +106,8 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) · 49 unmodified lines |
| 99 | 106 | struct pci_dev *pdev = vdev->pdev; |
| 100 | 107 | int bar; |
| 101 | 108 | |
| 102 | | − pci_disable_device(pdev); |
| 109 | + /* Stop the device from further DMA */ |
| 110 | + pci_clear_master(pdev); |
| 103 | 111 | |
| 104 | 112 | vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | |
| 105 | 113 | VFIO_IRQ_SET_ACTION_TRIGGER, |
| @@ -117,6 +125,8 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) · 11 unmodified lines |
| 117 | 125 | vdev->barmap[bar] = NULL; |
| 118 | 126 | } |
| 119 | 127 | |
| 128 | + vdev->needs_reset = true; |
| 129 | + |
| 120 | 130 | /* |
| 121 | 131 | * If we have saved state, restore it. If we can reset the device, |
| 122 | 132 | * even better. Resetting with current state seems better than |
| @@ -128,7 +138,7 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) · 5 unmodified lines |
| 128 | 138 | __func__, dev_name(&pdev->dev)); |
| 129 | 139 | |
| 130 | 140 | if (!vdev->reset_works) |
| 131 | | − return; |
| 141 | + goto out; |
| 132 | 142 | |
| 133 | 143 | pci_save_state(pdev); |
| 134 | 144 | } |
| @@ -148,46 +158,55 @@ static void vfio_pci_disable(struct vfio_pci_device *vdev) · 13 unmodified lines |
| 148 | 158 | if (ret) |
| 149 | 159 | pr_warn("%s: Failed to reset device %s (%d)\n", |
| 150 | 160 | __func__, dev_name(&pdev->dev), ret); |
| 161 | + else |
| 162 | + vdev->needs_reset = false; |
| 151 | 163 | } |
| 152 | 164 | |
| 153 | 165 | pci_restore_state(pdev); |
| 166 | +out: |
| 167 | + pci_disable_device(pdev); |
| 168 | + |
| 169 | + vfio_pci_try_bus_reset(vdev); |
| 154 | 170 | } |
| 155 | 171 | |
| 156 | 172 | static void vfio_pci_release(void *device_data) |
| 157 | 173 | { |
| 158 | 174 | struct vfio_pci_device *vdev = device_data; |
| 159 | 175 | |
| 160 | | − if (atomic_dec_and_test(&vdev->refcnt)) { |
| 176 | + mutex_lock(&driver_lock); |
| 177 | + |
| 178 | + if (!(--vdev->refcnt)) { |
| 161 | 179 | vfio_spapr_pci_eeh_release(vdev->pdev); |
| 162 | 180 | vfio_pci_disable(vdev); |
| 163 | 181 | } |
| 164 | 182 | |
| 183 | + mutex_unlock(&driver_lock); |
| 184 | + |
| 165 | 185 | module_put(THIS_MODULE); |
| 166 | 186 | } |
| 167 | 187 | |
| 168 | 188 | static int vfio_pci_open(void *device_data) |
| 169 | 189 | { |
| 170 | 190 | struct vfio_pci_device *vdev = device_data; |
| 171 | | − int ret; |
| 191 | + int ret = 0; |
| 172 | 192 | |
| 173 | 193 | if (!try_module_get(THIS_MODULE)) |
| 174 | 194 | return -ENODEV; |
| 175 | 195 | |
| 176 | | − if (atomic_inc_return(&vdev->refcnt) == 1) { |
| 196 | + mutex_lock(&driver_lock); |
| 197 | + |
| 198 | + if (!vdev->refcnt) { |
| 177 | 199 | ret = vfio_pci_enable(vdev); |
| 178 | 200 | if (ret) |
| 179 | 201 | goto error; |
| 180 | 202 | |
| 181 | | − ret = vfio_spapr_pci_eeh_open(vdev->pdev); |
| 182 | | − if (ret) { |
| 183 | | − vfio_pci_disable(vdev); |
| 184 | | − goto error; |
| 185 | | − } |
| 203 | + vfio_spapr_pci_eeh_open(vdev->pdev); |
| 186 | 204 | } |
| 187 | | − |
| 188 | | − return 0; |
| 205 | + vdev->refcnt++; |
| 189 | 206 | error: |
| 190 | | − module_put(THIS_MODULE); |
| 207 | + mutex_unlock(&driver_lock); |
| 208 | + if (ret) |
| 209 | + module_put(THIS_MODULE); |
| 191 | 210 | return ret; |
| 192 | 211 | } |
| 193 | 212 | |
| @@ -843,7 +862,6 @@ static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) · 649 unmodified lines |
| 843 | 862 | vdev->irq_type = VFIO_PCI_NUM_IRQS; |
| 844 | 863 | mutex_init(&vdev->igate); |
| 845 | 864 | spin_lock_init(&vdev->irqlock); |
| 846 | | − atomic_set(&vdev->refcnt, 0); |
| 847 | 865 | |
| 848 | 866 | ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev); |
| 849 | 867 | if (ret) { |
| @@ -858,12 +876,15 @@ static void vfio_pci_remove(struct pci_dev *pdev) · 8 unmodified lines |
| 858 | 876 | { |
| 859 | 877 | struct vfio_pci_device *vdev; |
| 860 | 878 | |
| 879 | + mutex_lock(&driver_lock); |
| 880 | + |
| 861 | 881 | vdev = vfio_del_group_dev(&pdev->dev); |
| 862 | | − if (!vdev) |
| 863 | | − return; |
| 882 | + if (vdev) { |
| 883 | + iommu_group_put(pdev->dev.iommu_group); |
| 884 | + kfree(vdev); |
| 885 | + } |
| 864 | 886 | |
| 865 | | − iommu_group_put(pdev->dev.iommu_group); |
| 866 | | − kfree(vdev); |
| 887 | + mutex_unlock(&driver_lock); |
| 867 | 888 | } |
| 868 | 889 | |
| 869 | 890 | static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev, |
| @@ -906,6 +927,110 @@ static struct pci_driver vfio_pci_driver = { · 36 unmodified lines |
| 906 | 927 | .err_handler = &vfio_err_handlers, |
| 907 | 928 | }; |
| 908 | 929 | |
| 930 | +/* |
| 931 | + * Test whether a reset is necessary and possible. We mark devices as |
| 932 | + * needs_reset when they are released, but don't have a function-local reset |
| 933 | + * available. If any of these exist in the affected devices, we want to do |
| 934 | + * a bus/slot reset. We also need all of the affected devices to be unused, |
| 935 | + * so we abort if any device has a non-zero refcnt. driver_lock prevents a |
| 936 | + * device from being opened during the scan or unbound from vfio-pci. |
| 937 | + */ |
| 938 | +static int vfio_pci_test_bus_reset(struct pci_dev *pdev, void *data) |
| 939 | +{ |
| 940 | + bool *needs_reset = data; |
| 941 | + struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver); |
| 942 | + int ret = -EBUSY; |
| 943 | + |
| 944 | + if (pci_drv == &vfio_pci_driver) { |
| 945 | + struct vfio_device *device; |
| 946 | + struct vfio_pci_device *vdev; |
| 947 | + |
| 948 | + device = vfio_device_get_from_dev(&pdev->dev); |
| 949 | + if (!device) |
| 950 | + return ret; |
| 951 | + |
| 952 | + vdev = vfio_device_data(device); |
| 953 | + if (vdev) { |
| 954 | + if (vdev->needs_reset) |
| 955 | + *needs_reset = true; |
| 956 | + |
| 957 | + if (!vdev->refcnt) |
| 958 | + ret = 0; |
| 959 | + } |
| 960 | + |
| 961 | + vfio_device_put(device); |
| 962 | + } |
| 963 | + |
| 964 | + /* |
| 965 | + * TODO: vfio-core considers groups to be viable even if some devices |
| 966 | + * are attached to known drivers, like pci-stub or pcieport. We can't |
| 967 | + * freeze devices from being unbound to those drivers like we can |
| 968 | + * here though, so it would be racy to test for them. We also can't |
| 969 | + * use device_lock() to prevent changes as that would interfere with |
| 970 | + * PCI-core taking device_lock during bus reset. For now, we require |
| 971 | + * devices to be bound to vfio-pci to get a bus/slot reset on release. |
| 972 | + */ |
| 973 | + |
| 974 | + return ret; |
| 975 | +} |
| 976 | + |
| 977 | +/* Clear needs_reset on all affected devices after successful bus/slot reset */ |
| 978 | +static int vfio_pci_clear_needs_reset(struct pci_dev *pdev, void *data) |
| 979 | +{ |
| 980 | + struct pci_driver *pci_drv = ACCESS_ONCE(pdev->driver); |
| 981 | + |
| 982 | + if (pci_drv == &vfio_pci_driver) { |
| 983 | + struct vfio_device *device; |
| 984 | + struct vfio_pci_device *vdev; |
| 985 | + |
| 986 | + device = vfio_device_get_from_dev(&pdev->dev); |
| 987 | + if (!device) |
| 988 | + return 0; |
| 989 | + |
| 990 | + vdev = vfio_device_data(device); |
| 991 | + if (vdev) |
| 992 | + vdev->needs_reset = false; |
| 993 | + |
| 994 | + vfio_device_put(device); |
| 995 | + } |
| 996 | + |
| 997 | + return 0; |
| 998 | +} |
| 999 | + |
| 1000 | +/* |
| 1001 | + * Attempt to do a bus/slot reset if there are devices affected by a reset for |
| 1002 | + * this device that are needs_reset and all of the affected devices are unused |
| 1003 | + * (!refcnt). Callers of this function are required to hold driver_lock such |
| 1004 | + * that devices can not be unbound from vfio-pci or opened by a user while we |
| 1005 | + * test for and perform a bus/slot reset. |
| 1006 | + */ |
| 1007 | +static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev) |
| 1008 | +{ |
| 1009 | + bool needs_reset = false, slot = false; |
| 1010 | + int ret; |
| 1011 | + |
| 1012 | + if (!pci_probe_reset_slot(vdev->pdev->slot)) |
| 1013 | + slot = true; |
| 1014 | + else if (pci_probe_reset_bus(vdev->pdev->bus)) |
| 1015 | + return; |
| 1016 | + |
| 1017 | + if (vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1018 | + vfio_pci_test_bus_reset, |
| 1019 | + &needs_reset, slot) || !needs_reset) |
| 1020 | + return; |
| 1021 | + |
| 1022 | + if (slot) |
| 1023 | + ret = pci_try_reset_slot(vdev->pdev->slot); |
| 1024 | + else |
| 1025 | + ret = pci_try_reset_bus(vdev->pdev->bus); |
| 1026 | + |
| 1027 | + if (ret) |
| 1028 | + return; |
| 1029 | + |
| 1030 | + vfio_pci_for_each_slot_or_bus(vdev->pdev, |
| 1031 | + vfio_pci_clear_needs_reset, NULL, slot); |
| 1032 | +} |
| 1033 | + |
| 909 | 1034 | static void __exit vfio_pci_cleanup(void) |
| 910 | 1035 | { |
| 911 | 1036 | pci_unregister_driver(&vfio_pci_driver); |