[PATCH 4/5] s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign
From: Jason J. Herne
Date: Thu Dec 03 2026 - 16:45:52 EST
When an adapter or domain is unassigned from a matrix mdev,
vfio_ap_mdev_unlink_adapter() and vfio_ap_mdev_unlink_domain() remove
each affected queue from the mdev's hash table. Only queues that are in
the guest's APCB are added to the reset list. A queue that was already
filtered from the APCB is unlinked but never added to that list, so
vfio_ap_unlink_mdev_fr_queue() is never called for it. Its
q->matrix_mdev keeps pointing at the mdev.
The mdev's matrix.apm/aqm no longer include the adapter or domain at
this point, so vfio_ap_mdev_unlink_fr_queues() called from
vfio_ap_mdev_remove() will not visit the queue either. When the mdev is
freed, q->matrix_mdev is left dangling. Later paths that dereference it,
such as vfio_ap_mdev_remove_queue(), can then access freed memory.
Clear the link for these queues in an else branch. Queues that are added
to the reset list keep their link until the reset completes, as before.
Clearing the link has a side effect. vfio_ap_free_aqic_resources()
needs q->matrix_mdev to unpin the NIB page and unregister the ISC, and
vfio_ap_mdev_remove_queue() calls it for queues that are no longer in
the host's AP configuration. Such a queue can still hold AQIC resources.
This happens when the adapter or domain is removed from the host
configuration while a guest is using AP interrupts. That path only
clears the shadow APCB bits and does not reset the queue. If the
adapter or domain is then unassigned before the AP bus removes the
queue device, the link is cleared first. The later cleanup hits
WARN_ON(!q->matrix_mdev) and leaks the pinned page and the ISC
registration.
A queue outside the host configuration is deconfigured, so it cannot
write to the NIB. Release its AQIC resources before clearing the link.
Queues still in the host configuration that were filtered from the APCB
have already been reset, or had their resources released or leaked by
the reset, so they have nothing left to free here.
Also drop the "qlist" NULL check. Both helpers have a single caller, and
that caller always passes a freshly initialized list.
Fixes: 70aeefe574cb ("s390/vfio-ap: reset queues after adapter/domain unassignment")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Jason J. Herne <jjherne@xxxxxxxxxxxxx>
---
drivers/s390/crypto/vfio_ap_ops.c | 40 +++++++++++++++++++++++--------
1 file changed, 30 insertions(+), 10 deletions(-)
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index d460337e365f..836551e9f65b 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -1173,6 +1173,22 @@ static struct vfio_ap_queue
return q;
}
+/*
+ * Unlink a queue that is not being reset. If it is no longer in the host's AP
+ * configuration it is gone, but vfio_ap_mdev_remove_queue() can't release its
+ * AQIC resources once the mdev link is cleared, so release them here.
+ */
+static void vfio_ap_unlink_mdev_fr_unreset_queue(struct vfio_ap_queue *q)
+{
+ if (!test_bit_inv(AP_QID_CARD(q->apqn),
+ (unsigned long *)matrix_dev->info.apm) ||
+ !test_bit_inv(AP_QID_QUEUE(q->apqn),
+ (unsigned long *)matrix_dev->info.aqm))
+ vfio_ap_free_aqic_resources(q);
+
+ vfio_ap_unlink_mdev_fr_queue(q);
+}
+
/**
* vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
* adapter from the matrix mdev to which the
@@ -1191,12 +1207,14 @@ static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
+ if (!q)
+ continue;
- if (q && qlist) {
- if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
- test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
- list_add_tail(&q->reset_qnode, qlist);
- }
+ if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
+ test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
+ list_add_tail(&q->reset_qnode, qlist);
+ else
+ vfio_ap_unlink_mdev_fr_unreset_queue(q);
}
}
@@ -1392,12 +1410,14 @@ static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
+ if (!q)
+ continue;
- if (q && qlist) {
- if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
- test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
- list_add_tail(&q->reset_qnode, qlist);
- }
+ if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
+ test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
+ list_add_tail(&q->reset_qnode, qlist);
+ else
+ vfio_ap_unlink_mdev_fr_unreset_queue(q);
}
}
--
2.56.1