@@ -626,6 +626,27 @@ static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vde
626626 }
627627}
628628
629+ /*
630+ * This function is called in all state_mutex unlock cases to
631+ * handle a 'deferred_reset' if exists.
632+ */
633+ static void
634+ hisi_acc_vf_state_mutex_unlock (struct hisi_acc_vf_core_device * hisi_acc_vdev )
635+ {
636+ again :
637+ spin_lock (& hisi_acc_vdev -> reset_lock );
638+ if (hisi_acc_vdev -> deferred_reset ) {
639+ hisi_acc_vdev -> deferred_reset = false;
640+ spin_unlock (& hisi_acc_vdev -> reset_lock );
641+ hisi_acc_vdev -> vf_qm_state = QM_NOT_READY ;
642+ hisi_acc_vdev -> mig_state = VFIO_DEVICE_STATE_RUNNING ;
643+ hisi_acc_vf_disable_fds (hisi_acc_vdev );
644+ goto again ;
645+ }
646+ mutex_unlock (& hisi_acc_vdev -> state_mutex );
647+ spin_unlock (& hisi_acc_vdev -> reset_lock );
648+ }
649+
629650static void hisi_acc_vf_start_device (struct hisi_acc_vf_core_device * hisi_acc_vdev )
630651{
631652 struct hisi_qm * vf_qm = & hisi_acc_vdev -> vf_qm ;
@@ -922,7 +943,7 @@ hisi_acc_vfio_pci_set_device_state(struct vfio_device *vdev,
922943 break ;
923944 }
924945 }
925- mutex_unlock ( & hisi_acc_vdev -> state_mutex );
946+ hisi_acc_vf_state_mutex_unlock ( hisi_acc_vdev );
926947 return res ;
927948}
928949
@@ -935,10 +956,35 @@ hisi_acc_vfio_pci_get_device_state(struct vfio_device *vdev,
935956
936957 mutex_lock (& hisi_acc_vdev -> state_mutex );
937958 * curr_state = hisi_acc_vdev -> mig_state ;
938- mutex_unlock ( & hisi_acc_vdev -> state_mutex );
959+ hisi_acc_vf_state_mutex_unlock ( hisi_acc_vdev );
939960 return 0 ;
940961}
941962
963+ static void hisi_acc_vf_pci_aer_reset_done (struct pci_dev * pdev )
964+ {
965+ struct hisi_acc_vf_core_device * hisi_acc_vdev = dev_get_drvdata (& pdev -> dev );
966+
967+ if (hisi_acc_vdev -> core_device .vdev .migration_flags !=
968+ VFIO_MIGRATION_STOP_COPY )
969+ return ;
970+
971+ /*
972+ * As the higher VFIO layers are holding locks across reset and using
973+ * those same locks with the mm_lock we need to prevent ABBA deadlock
974+ * with the state_mutex and mm_lock.
975+ * In case the state_mutex was taken already we defer the cleanup work
976+ * to the unlock flow of the other running context.
977+ */
978+ spin_lock (& hisi_acc_vdev -> reset_lock );
979+ hisi_acc_vdev -> deferred_reset = true;
980+ if (!mutex_trylock (& hisi_acc_vdev -> state_mutex )) {
981+ spin_unlock (& hisi_acc_vdev -> reset_lock );
982+ return ;
983+ }
984+ spin_unlock (& hisi_acc_vdev -> reset_lock );
985+ hisi_acc_vf_state_mutex_unlock (hisi_acc_vdev );
986+ }
987+
942988static int hisi_acc_vf_qm_init (struct hisi_acc_vf_core_device * hisi_acc_vdev )
943989{
944990 struct vfio_pci_core_device * vdev = & hisi_acc_vdev -> core_device ;
@@ -1259,12 +1305,17 @@ static const struct pci_device_id hisi_acc_vfio_pci_table[] = {
12591305
12601306MODULE_DEVICE_TABLE (pci , hisi_acc_vfio_pci_table );
12611307
1308+ static const struct pci_error_handlers hisi_acc_vf_err_handlers = {
1309+ .reset_done = hisi_acc_vf_pci_aer_reset_done ,
1310+ .error_detected = vfio_pci_core_aer_err_detected ,
1311+ };
1312+
12621313static struct pci_driver hisi_acc_vfio_pci_driver = {
12631314 .name = KBUILD_MODNAME ,
12641315 .id_table = hisi_acc_vfio_pci_table ,
12651316 .probe = hisi_acc_vfio_pci_probe ,
12661317 .remove = hisi_acc_vfio_pci_remove ,
1267- .err_handler = & vfio_pci_core_err_handlers ,
1318+ .err_handler = & hisi_acc_vf_err_handlers ,
12681319};
12691320
12701321module_pci_driver (hisi_acc_vfio_pci_driver );
0 commit comments