Skip to contentSkip to conversation

s390/vfio-ap: Fixes for queue reset and AQIC handling

PATCH New Jason J. Herne 2 participants 11 messages Dec 3

Subsystems (4)
lore
Share & tools
lore

Jason J. Herne

author maintainer
#0 lore raw

cover s390/vfio-ap: Fixes for queue reset and AQIC handling

This is a bugfix series to address problems found during ai lead code review. Its focus is primarily reset & data security related.

Summary of changes:
- Patch 1: Prevent passing unreset, checkstopped, or deconfigured queues through to guests so we don't risk leaking sensitive leftover data.
- Patch 2: Bound the queue reset verification loop (2s timeout) to avoid infinite hangs if resets fail to complete (rare or impossible).
- Patch 3: Hold a private page reference on unconfirmed resets to satisfy VFIO DMA unmapping while preventing page reuse/corruption by hardware.
- Patch 4: Clear dangling matrix_mdev references and clean up AQIC resources for filtered queues during adapter/domain unassignment.
- Patch 5: Fix resource handling and state tracking for rejected or non-NORMAL AQIC enable requests.

Jason J. Herne (5):
s390/vfio-ap: Prevent passing unreset queues
s390/vfio-ap: bound the wait for queue reset verification s390/vfio-ap: Hold a private NIB page reference when a reset is
unconfirmed
s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign s390/vfio-ap: Fix resource handling for rejected AQIC enable requests

 drivers/s390/crypto/vfio_ap_ops.c     | 140 +++++++++++++++++++++-----
 drivers/s390/crypto/vfio_ap_private.h |   2 +
 2 files changed, 119 insertions(+), 23 deletions(-)
Signature · 1 line
2.56.1
author maintainer
#1 lore raw

PATCH 1/5 s390/vfio-ap: Prevent passing unreset queues

vfio_ap_mdev_reset_queues() and _queue_passable() use q->reset_status to decide whether a queue was reset and may be passed to a guest. apq_reset_check() can leave the status from the initial ZAPQ (usually AP_RESPONSE_NORMAL) in place when the reset is not confirmed, so an unreset queue looks reset. Fix the cases where this happens:

- TAPQ returns an invalid response code (-EIO), or AP_RESPONSE_Q_NOT_AVAIL (-ENODEV, e.g. ZAPQ rc 00 then TAPQ rc 01):
the worker returns without saving the TAPQ status. Save it. In the -EIO case the AQIC resources are still intentionally leaked, since it is unknown whether the queue can still write to the NIB. Not saving the status on -EIO predates the DECONFIGURED and CHECKSTOPPED handling below.

- AP_RESPONSE_DECONFIGURED and AP_RESPONSE_CHECKSTOPPED are treated as success by apq_status_check(), but neither confirms a zeroize:
all status bits are zero for these codes and a checkstop resets the queue rather than zeroizing it. Return -ENODEV for them so the worker frees the AQIC resources (the queue can no longer write to the NIB) and saves the status.

Make _queue_passable() accept only AP_RESPONSE_NORMAL, so deconfigured and checkstopped queues are no longer passed to a guest. This drops the earlier policy of passing such queues through to avoid possible data leaks.

Fixes c51f8c6bb5c8 ("s390/vfio-ap: allow deconfigured queue to be passed through to a guest")
Fixes dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete")
Fixes a681226c675c ("s390/vfio-ap: handle hardware checkstop state on queue reset operation")
---
 drivers/s390/crypto/vfio_ap_ops.c | 27 +++++++++++++++++++++++----
 1 file changed, 23 insertions(+), 4 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +23 -4
index 373ce574c431..10430b9446db 100644
@@ -694,8 +694,6 @@ static bool _queue_passable(struct vfio_ap_queue *q)
 
 	switch (q->reset_status.response_code) {
 	case AP_RESPONSE_NORMAL:
-	case AP_RESPONSE_DECONFIGURED:
-	case AP_RESPONSE_CHECKSTOPPED:
 		return true;
 	default:
 		return false;
@@ -1927,9 +1925,20 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
 {
 	switch (status->response_code) {
 	case AP_RESPONSE_NORMAL:
+		/* TAPQ => AP_RESPONSE_NORMAL means reset/zero complete. */
+		return 0;
 	case AP_RESPONSE_DECONFIGURED:
 	case AP_RESPONSE_CHECKSTOPPED:
-		return 0;
+		/*
+		 * The queue is non-operational: interrupts are not possible so
+		 * AQIC resources can be safely freed. However, zeroization
+		 * cannot be confirmed because all status bits are zeroed when
+		 * these response codes are returned. There is no way to
+		 * distinguish a zeroized queue from one that has not been
+		 * zeroized. Return -ENODEV to signal that AQIC resources should
+		 * be freed but that zeroization has not been confirmed.
+		 */
+		return -ENODEV;
 	case AP_RESPONSE_RESET_IN_PROGRESS:
 	case AP_RESPONSE_BUSY:
 		return -EBUSY;
@@ -1969,9 +1978,19 @@ static void apq_reset_check(struct work_struct *reset_work)
 		elapsed += AP_RESET_INTERVAL;
 		status = ap_tapq(q->apqn, NULL);
 		ret = apq_status_check(q->apqn, &status);
-		if (ret == -EIO)
+		if (ret == -EIO) {
+			/*
+			 * Invalid TAPQ response code. It is unknown whether the
+			 * queue can still write to the NIB, so leak the AQIC
+			 * resources rather than let the NIB page be reused.
+			 * Record the status so the queue is not passable.
+			 */
+			memcpy(&q->reset_status, &status, sizeof(status));
 			return;
+		}
 		if (ret == -ENODEV) {
+			/* Record the status so the queue is not passable. */
+			memcpy(&q->reset_status, &status, sizeof(status));
 			vfio_ap_free_aqic_resources(q);
 			return;
 		}
Signature · 1 line
2.56.1
author maintainer
#3 lore raw

PATCH 2/5 s390/vfio-ap: bound the wait for queue reset verification

In reply to Jason J. Herne

apq_reset_check() TAPQ polls the queue until reset/zeroize is confirmed. Commit dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete") removed the time limit, because the architecture defines no upper bound for a RAPQ/ZAPQ and giving up early and freeing the AQIC resources could let a still-active queue write to a reused NIB page.

That left the loop with no exit if the reset never completes. This can happen when TAPQ keeps returning a busy/reset-in-progress response, or when the ZAPQ is repeatedly rejected with reset-in-progress or state-change-in-progress (or the queue keeps reporting an asynchronous association failure) and is reissued on every pass. The work item then spins forever, and anything that flushes it, such as vfio_ap_mdev_reset_queues() and vfio_ap_mdev_reset_qlist(), waits forever with it.

Give up after 2 seconds. A timeout does not confirm the queue was reset, so do not free the AQIC resources, which leaks the pinned NIB page rather than risk a write to memory that has been reused. Record the last TAPQ status in q->reset_status, substituting AP_RESPONSE_BUSY if it was AP_RESPONSE_NORMAL, so the queue is not considered passable and the reset callers report -EIO.

This keeps the original safety property (never free resources for a queue whose reset is unconfirmed) while removing the hang.

Fixes dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete")
---
 drivers/s390/crypto/vfio_ap_ops.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
drivers/s390/crypto/vfio_ap_ops.c +15 -0
index 10430b9446db..fba6f256ce77 100644
@@ -31,6 +31,7 @@
 #define AP_QUEUE_IN_USE "in use"
 
 #define AP_RESET_INTERVAL		20	/* Reset sleep interval (20ms)		*/
+#define VFIOAP_RESET_TIMEOUT		2000	/* Max wait for reset to complete (2s)	*/
 
 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
@@ -1974,6 +1975,20 @@ static void apq_reset_check(struct work_struct *reset_work)
 	q = container_of(reset_work, struct vfio_ap_queue, reset_work);
 	memcpy(&status, &q->reset_status, sizeof(status));
 	while (true) {
+		if (elapsed >= VFIOAP_RESET_TIMEOUT) {
+			/*
+			 * Reset timed out. The queue could still be active so
+			 * leak AQIC resources rather than risk wild DMA write.
+			 */
+			pr_warn_ratelimited("Reset timed out for APQN %02x.%04x: leaking NIB page to prevent host crash\n",
+					    AP_QID_CARD(q->apqn),
+					    AP_QID_QUEUE(q->apqn));
+			memcpy(&q->reset_status, &status, sizeof(status));
+			if (!q->reset_status.response_code)
+				/* Record BUSY status so queue is not passable. */
+				q->reset_status.response_code = AP_RESPONSE_BUSY;
+			return;
+		}
 		msleep(AP_RESET_INTERVAL);
 		elapsed += AP_RESET_INTERVAL;
 		status = ap_tapq(q->apqn, NULL);
Signature · 1 line
2.56.1
author maintainer
#5 lore raw

PATCH 3/5 s390/vfio-ap: Hold a private NIB page reference when a reset is unconfirmed

In reply to Jason J. Herne

When a queue reset times out or returns an unrecognized TAPQ or ZAPQ response, the hardware may still write to the NIB. Previous behavior left the NIB pinned in VFIO to protect the page, but VFIO requires drivers to unpin on a DMA unmap callback. The pin is also orphaned when the queue is freed. A later VFIO_IOMMU_UNMAP_DMA of that range then makes type1 loop until BUG_ON(++retries > 10), which panics the host if panic_on_oops is set or hangs the container otherwise.

Take a reference of our own on the NIB page before unpinning it from VFIO, and never drop it. VFIO gets its unpin, and the page cannot be freed and reused while the hardware might still write to it. The cost is a leaked page (the whole folio if the page is part of a large one) per distinct NIB on an unconfirmed reset.

Use this in apq_reset_check() for the timeout and invalid-TAPQ-response cases that previously skipped the free. Also use it in vfio_ap_mdev_reset_queue() for an invalid ZAPQ response. That case queues no reset work and previously left the NIB pinned, so the pin was orphaned when vfio_ap_mdev_remove_queue() freed the queue.

Also use it in vfio_ap_irq_disable() when AQIC still returns BUSY or RESET_IN_PROGRESS after the last retry. That path used to free the resources, including the unpin, even though interruption may still be enabled.

Fixes ec89b55e3bce ("s390: ap: implement PAPQ AQIC interception in kernel")
---
 drivers/s390/crypto/vfio_ap_ops.c     | 45 +++++++++++++++++++++++----
 drivers/s390/crypto/vfio_ap_private.h |  2 ++
 2 files changed, 41 insertions(+), 6 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +39 -6
index fba6f256ce77..d460337e365f 100644
@@ -272,7 +272,7 @@ static void vfio_ap_wait_for_irqclear(int apqn)
  *
  * Unregisters the ISC in the GIB when the saved ISC not invalid.
  * Unpins the guest's page holding the NIB when it exists.
- * Resets the saved_iova and saved_isc to invalid values.
+ * Resets saved_iova, saved_isc, saved_page.
  */
 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
 {
@@ -286,9 +286,32 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
 	if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
 		vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
 		q->saved_iova = 0;
+		q->saved_page = NULL;
 	}
 }
 
+/**
+ * vfio_ap_leak_aqic_resources - release the AQIC resources of a queue that may
+ *				 still be able to store to the NIB
+ * @q: The vfio_ap_queue
+ *
+ * VFIO requires the driver to unpin pages when it is told to invalidate a DMA
+ * mapping, so the NIB page cannot stay pinned by VFIO. Take a reference of our
+ * own on the page before it is unpinned and never drop it. The page can then
+ * not be freed and reused for something else while the queue may still write
+ * to it. This leaks the page (the whole folio if it is a large one).
+ */
+static void vfio_ap_leak_aqic_resources(struct vfio_ap_queue *q)
+{
+	if (q->saved_page) {
+		pr_warn_ratelimited("vfio_ap: APQN %02x.%04x: leaking NIB page to prevent reuse while the queue may still write to it\n",
+				    AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn));
+		get_page(q->saved_page);
+		q->saved_page = NULL;
+	}
+	vfio_ap_free_aqic_resources(q);
+}
+
 /**
  * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
  * @q: The vfio_ap_queue
@@ -300,7 +323,9 @@ static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
  * with the AP interrupt handling.
  *
  * In the case the AP is busy, or a reset is in progress,
- * retries after 20ms, up to 5 times.
+ * retries after 20ms, up to 5 times. If the queue is still busy or resetting
+ * after the last retry, the interrupt may still be enabled, so the NIB page is
+ * leaked by vfio_ap_leak_aqic_resources() instead of being released.
  *
  * Returns if ap_aqic function failed with invalid, deconfigured or
  * checkstopped AP.
@@ -338,6 +363,8 @@ static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
 
 	WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
 		  status.response_code);
+	vfio_ap_leak_aqic_resources(q);
+	return status;
 end_free:
 	vfio_ap_free_aqic_resources(q);
 	return status;
@@ -493,6 +520,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
 		/* See if we did clear older IRQ configuration */
 		vfio_ap_free_aqic_resources(q);
 		q->saved_iova = nib;
+		q->saved_page = h_page;
 		q->saved_isc = isc;
 		break;
 	case AP_RESPONSE_OTHERWISE_CHANGED:
@@ -1978,15 +2006,17 @@ static void apq_reset_check(struct work_struct *reset_work)
 		if (elapsed >= VFIOAP_RESET_TIMEOUT) {
 			/*
 			 * Reset timed out. The queue could still be active so
-			 * leak AQIC resources rather than risk wild DMA write.
+			 * leak the NIB page rather than risk a write to memory
+			 * that has been reused.
 			 */
-			pr_warn_ratelimited("Reset timed out for APQN %02x.%04x: leaking NIB page to prevent host crash\n",
+			pr_warn_ratelimited("Reset timed out for APQN %02x.%04x\n",
 					    AP_QID_CARD(q->apqn),
 					    AP_QID_QUEUE(q->apqn));
 			memcpy(&q->reset_status, &status, sizeof(status));
 			if (!q->reset_status.response_code)
 				/* Record BUSY status so queue is not passable. */
 				q->reset_status.response_code = AP_RESPONSE_BUSY;
+			vfio_ap_leak_aqic_resources(q);
 			return;
 		}
 		msleep(AP_RESET_INTERVAL);
@@ -1996,11 +2026,12 @@ static void apq_reset_check(struct work_struct *reset_work)
 		if (ret == -EIO) {
 			/*
 			 * Invalid TAPQ response code. It is unknown whether the
-			 * queue can still write to the NIB, so leak the AQIC
-			 * resources rather than let the NIB page be reused.
+			 * queue can still write to the NIB, so leak the NIB page
+			 * rather than let it be reused.
 			 * Record the status so the queue is not passable.
 			 */
 			memcpy(&q->reset_status, &status, sizeof(status));
+			vfio_ap_leak_aqic_resources(q);
 			return;
 		}
 		if (ret == -ENODEV) {
@@ -2060,6 +2091,8 @@ static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
 		     "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
 		     AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
 		     status.response_code);
+		/* Unknown whether queue was reset, it may still write the NIB */
+		vfio_ap_leak_aqic_resources(q);
 	}
 }
 
drivers/s390/crypto/vfio_ap_private.h +2 -0
index 9bff666b0b35..cd8fc1fba151 100644
@@ -132,6 +132,7 @@ struct ap_matrix_mdev {
  *			  vfio_ap device driver
  * @matrix_mdev: the matrix mediated device
  * @saved_iova: the notification indicator byte (nib) address
+ * @saved_page: the host page holding the nib while it is pinned by VFIO
  * @apqn: the APQN of the AP queue device
  * @saved_isc: the guest ISC registered with the GIB interface
  * @mdev_qnode: allows the vfio_ap_queue struct to be added to a hashtable
@@ -143,6 +144,7 @@ struct ap_matrix_mdev {
 struct vfio_ap_queue {
 	struct ap_matrix_mdev *matrix_mdev;
 	dma_addr_t saved_iova;
+	struct page *saved_page;
 	int	apqn;
 #define VFIO_AP_ISC_INVALID 0xff
 	unsigned char saved_isc;
Signature · 1 line
2.56.1
author maintainer
#7 lore raw

PATCH 4/5 s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign

In reply to Jason J. Herne

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")
---
 drivers/s390/crypto/vfio_ap_ops.c | 40 +++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 10 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +30 -10
index d460337e365f..836551e9f65b 100644
@@ -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);
 	}
 }
 
Signature · 1 line
2.56.1
author maintainer
#9 lore raw

PATCH 5/5 s390/vfio-ap: Fix resource handling for rejected AQIC enable requests

In reply to Jason J. Herne

When the guest enables interrupts, vfio_ap_irq_enable() pins the new NIB page and registers the guest ISC, then issues PQAP(AQIC). The resources are kept only if the hardware accepts the new NIB. Two cases are handled wrongly:

* For any response other than NORMAL or OTHERWISE_CHANGED, the code warned and called vfio_ap_irq_disable(). That issued PQAP(AQIC) with ir=0 to the hardware, switching off the guest's working interrupts even though only the new enable request had failed, and then freed the previously saved NIB page and ISC. It also never released the newly pinned page and registered ISC, so they leak.

* OTHERWISE_CHANGED (interrupts already enabled, or a prior asynchronous request still completing) is not a failure, but was logged as one.

Handle every response other than NORMAL the same way: release the newly pinned NIB page and the newly registered ISC, and leave the previously saved resources untouched. Do not disable interrupts in the hardware. Log a failure only for responses other than NORMAL and OTHERWISE_CHANGED. The status from PQAP(AQIC) is still returned to the guest as is.

Also update the function and case comments to describe this.

Fixes ec89b55e3bce ("s390: ap: implement PAPQ AQIC interception in kernel")
---
 drivers/s390/crypto/vfio_ap_ops.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +14 -7
index 836551e9f65b..3a09a93c09eb 100644
@@ -440,6 +440,10 @@ static int ensure_nib_shared(unsigned long addr)
  * Register the guest ISC to GIB interface and retrieve the
  * host ISC to issue the host side PQAP/AQIC
  *
+ * The newly pinned NIB and registered ISC are saved in *q only if ap_aqic()
+ * returns AP_RESPONSE_NORMAL. Otherwise they are released and the previously
+ * saved NIB and ISC are left untouched.
+ *
  * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
  * vfio_pin_pages or kvm_s390_gisc_register failed.
  *
@@ -524,21 +528,24 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
 		q->saved_isc = isc;
 		break;
 	case AP_RESPONSE_OTHERWISE_CHANGED:
-		/* We could not modify IRQ settings: clear new configuration */
+	default:
+		/*
+		 * We could not modify IRQ settings: release the new configuration.
+		 * Keep the previously saved configuration: for most responses the
+		 * queue is still operational and the hardware may still be using it.
+		 * If the queue is not operational the saved resources are released
+		 * by the reset/unassign/remove paths.
+		 */
 		ret = kvm_s390_gisc_unregister(kvm, isc);
 		if (ret)
 			VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
 					 __func__, ret, isc, q->apqn);
 		vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
 		break;
-	default:
-		pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
-			status.response_code);
-		vfio_ap_irq_disable(q);
-		break;
 	}
 
-	if (status.response_code != AP_RESPONSE_NORMAL) {
+	if (status.response_code != AP_RESPONSE_NORMAL &&
+	    status.response_code != AP_RESPONSE_OTHERWISE_CHANGED) {
 		VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
 				 "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
 				 "gisa=%#x, isc=%#x, apqn=%#04x\n",
Signature · 1 line
2.56.1
#8 lore raw

Re: [PATCH 4/5] s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign

In reply to Jason J. Herne
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] s390/vfio-ap: unreset unlink clears matrix_mdev with live AQIC state --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] s390/vfio-ap: unreset unlink clears matrix_mdev with live AQIC state
--

commit 53e525cdd277ac2c49c6360db65bfb03dbc20712
Author: Jason J. Herne <[email protected]>

s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign

This commit clears q->matrix_mdev when unassigning an adapter or domain for queues not in shadow_apcb, and releases their AQIC resources first if they are no longer in matrix_dev->info.

Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +5 -0
index d460337e365f2..836551e9f65be 100644
@@ -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.
+ */

drivers/s390/crypto/vfio_ap_ops.c static struct vfio_ap_queue · line 1190

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +10 -0
+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);
+}

[Severity: High]
Can this clear q->matrix_mdev while the queue still holds live AQIC resources and armed hardware interrupts?

A queue filtered out of shadow_apcb (for example, when a sibling queue on the same adapter is unbound) stays in qtable.queues and matrix_dev->info. Because handle_pqap() does not check shadow_apcb, a guest can still run PQAP(AQIC) on it to pin a NIB page, register a GISC, and arm interrupts.

Unassigning the adapter or domain then drops the queue from qtable.queues and clears q->matrix_mdev without resetting it. Hardware interrupts stay armed against the guest's NIB and GISA, and the orphaned NIB pin causes vfio_dma_do_unmap() to hit BUG_ON(++retries > 10) on DMA unmap.

On the other branch where matrix_dev->info is clear, if the host re-adds the adapter before ap_scan_bus() removes the queue and the guest enables AQIC via handle_pqap(), vfio_ap_free_aqic_resources() unpins the NIB page without disabling hardware interrupts.

Resetting any queue with active AQIC state before clearing q->matrix_mdev, or rejecting queues outside shadow_apcb in handle_pqap(), would avoid leaving armed interrupts behind.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#2 lore raw

Re: [PATCH 1/5] s390/vfio-ap: Prevent passing unreset queues

In reply to Jason J. Herne
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] s390/vfio-ap: _queue_passable() permanently blocks recovered adapters - [Low] s390/vfio-ap: missing…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] s390/vfio-ap: _queue_passable() permanently blocks recovered adapters
- [Low] s390/vfio-ap: missing Fixes tag for commit 65e05ec252a9 --

commit cac07ffe34091f7a1854821a6b6958aa7fdc8799
Author: Jason J. Herne <[email protected]>

Quoted 28 lines

s390/vfio-ap: Prevent passing unreset queues

vfio_ap_mdev_reset_queues() and _queue_passable() use q->reset_status to decide whether a queue was reset and may be passed to a guest. apq_reset_check() can leave the status from the initial ZAPQ (usually AP_RESPONSE_NORMAL) in place when the reset is not confirmed, so an unreset queue looks reset. Fix the cases where this happens:

- TAPQ returns an invalid response code (-EIO), or AP_RESPONSE_Q_NOT_AVAIL (-ENODEV, e.g. ZAPQ rc 00 then TAPQ rc 01):
the worker returns without saving the TAPQ status. Save it. In the -EIO case the AQIC resources are still intentionally leaked, since it is unknown whether the queue can still write to the NIB. Not saving the status on -EIO predates the DECONFIGURED and CHECKSTOPPED handling below.

- AP_RESPONSE_DECONFIGURED and AP_RESPONSE_CHECKSTOPPED are treated as success by apq_status_check(), but neither confirms a zeroize:
all status bits are zero for these codes and a checkstop resets the queue rather than zeroizing it. Return -ENODEV for them so the worker frees the AQIC resources (the queue can no longer write to the NIB) and saves the status.

Make _queue_passable() accept only AP_RESPONSE_NORMAL, so deconfigured and checkstopped queues are no longer passed to a guest. This drops the earlier policy of passing such queues through to avoid possible data leaks.

Quoted 5 lines
Fixes c51f8c6bb5c8 ("s390/vfio-ap: allow deconfigured queue to be passed through to a guest")
Fixes dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete")
Fixes a681226c675c ("s390/vfio-ap: handle hardware checkstop state on queue reset operation")

[Severity: Low]
Since this also fixes the -ENODEV path in apq_reset_check() returning without saving the TAPQ status for AP_RESPONSE_Q_NOT_AVAIL, should this include:

Fixes 65e05ec252a9 ("s390/vfio-ap: fix KVM GISC and page leak when queue removed from host config")

drivers/s390/crypto/vfio_ap_ops.c static bool _queue_passable(struct vfio_ap_queue *q) · line 699

Quoted 13 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -2
index 373ce574c4319..10430b9446dbb 100644
@@ -694,8 +694,6 @@ static bool _queue_passable(struct vfio_ap_queue *q)
 
 	switch (q->reset_status.response_code) {
 	case AP_RESPONSE_NORMAL:
-	case AP_RESPONSE_DECONFIGURED:
-	case AP_RESPONSE_CHECKSTOPPED:
 		return true;
 	default:
 		return false;

[Severity: High]
Can rejecting AP_RESPONSE_DECONFIGURED and AP_RESPONSE_CHECKSTOPPED here permanently block a recovered adapter from being passed to a guest?

If a queue reset runs while an adapter is deconfigured (for example in vfio_ap_mdev_unset_kvm()), q->reset_status records AP_RESPONSE_DECONFIGURED. When the adapter comes back online, ap_scan_domains() updates the ap_queue in place without re-probing it or triggering vfio_ap_on_cfg_changed().

Later assigning the adapter via assign_adapter_store() links the existing vfio_ap_queue without retrying a reset, so vfio_ap_mdev_filter_matrix() sees the stale status and clears the APID from shadow_apcb.apm. Unassigning also skips the reset because the APID is already clear in shadow_apcb.apm.

The same happens after checkstop recovery, leaving a healthy adapter unusable for guests until the queue is manually unbound and rebound from vfio_ap. Should vfio_ap retry resetting queues with a non-normal reset_status when linking or filtering them?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw

Re: [PATCH 2/5] s390/vfio-ap: bound the wait for queue reset verification

In reply to Jason J. Herne
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] s390/vfio-ap: timed-out unreset queue still passed to next guest - [High] s390/vfio-ap: sie_page2…

Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] s390/vfio-ap: timed-out unreset queue still passed to next guest
- [High] s390/vfio-ap: sie_page2 GISA freed while unreset queue still references it
- [Low] s390/vfio-ap: final ap_zapq() retry leaks NIB on non-operational status
--

commit b0dd58c5d0782ca7241d08c5695985f4c6e563d9
Author: Jason J. Herne <[email protected]>

s390/vfio-ap: bound the wait for queue reset verification

This commit adds a 2-second timeout to apq_reset_check() so that verification of an AP queue reset does not loop indefinitely, leaking AQIC resources and recording a non-zero reset_status on timeout.

Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -0
index 10430b9446dbb..fba6f256ce77a 100644

[ ... ]

drivers/s390/crypto/vfio_ap_ops.c static void apq_reset_check(struct work_struct *reset_work) · line 1978

Quoted 5 lines
drivers/s390/crypto/vfio_ap_ops.c +1 -0
@@ -1974,6 +1975,20 @@ static void apq_reset_check(struct work_struct *reset_work)
 	q = container_of(reset_work, struct vfio_ap_queue, reset_work);
 	memcpy(&status, &q->reset_status, sizeof(status));
 	while (true) {
+		if (elapsed >= VFIOAP_RESET_TIMEOUT) {

[Severity: Low]
Can this timeout check leak the NIB page if the final ap_zapq() retry returns a non-operational status?

When elapsed reaches VFIOAP_RESET_TIMEOUT, the ap_zapq() retry path continues straight into this check without inspecting status.response_code.

If that ap_zapq() returned AP_RESPONSE_DECONFIGURED, AP_RESPONSE_CHECKSTOPPED, or AP_RESPONSE_Q_NOT_AVAIL, this logs a spurious timeout warning and leaks the NIB page instead of freeing AQIC resources via the -ENODEV path.

drivers/s390/crypto/vfio_ap_ops.c static void apq_reset_check(struct work_struct *reset_work) · line 1985

Quoted 7 lines
drivers/s390/crypto/vfio_ap_ops.c +7 -0
+			/*
+			 * Reset timed out. The queue could still be active so
+			 * leak AQIC resources rather than risk wild DMA write.
+			 */
+			pr_warn_ratelimited("Reset timed out for APQN %02x.%04x: leaking NIB page to prevent host crash\n",
+					    AP_QID_CARD(q->apqn),
+					    AP_QID_QUEUE(q->apqn));

[Severity: High]
What prevents the unreset AP queue from writing to a freed GISA page after VM teardown?

vfio_ap_irq_enable() programs the queue hardware with both h_nib and the physical address of kvm->arch.sie_page2->gisa via ap_aqic(). When this timeout lets vfio_ap_mdev_unset_kvm() return, KVM proceeds to kvm_arch_destroy_vm() and calls kfree(kvm->arch.sie_page2).

If the still-active queue later raises an interrupt, hardware can write to the freed and reused host kernel page, causing host memory corruption. (Because saved_isc is not unregistered here, this also triggers WARN(gi->alert.mask != 0x00) in kvm_s390_gisa_destroy().)

drivers/s390/crypto/vfio_ap_ops.c static void apq_reset_check(struct work_struct *reset_work) · line 1991

Quoted 6 lines
drivers/s390/crypto/vfio_ap_ops.c +6 -0
+			memcpy(&q->reset_status, &status, sizeof(status));
+			if (!q->reset_status.response_code)
+				/* Record BUSY status so queue is not passable. */
+				q->reset_status.response_code = AP_RESPONSE_BUSY;
+			return;
+		}

[Severity: High]
Does recording a non-zero reset_status actually prevent the unreset queue from being passed to the next guest?

_queue_passable() is only checked inside vfio_ap_mdev_filter_matrix(), which is not called when detaching or attaching a guest. During VM teardown, vfio_ap_mdev_unset_kvm() ignores the -EIO return from vfio_ap_mdev_reset_queues() and leaves matrix_mdev->shadow_apcb intact.

When another guest later opens the same mdev, vfio_ap_mdev_set_kvm() calls vfio_ap_mdev_update_guest_apcb() directly with that stale shadow_apcb. The new guest then gets access to the unreset, unzeroized queue, breaking guest-to-guest isolation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#6 lore raw

Re: [PATCH 3/5] s390/vfio-ap: Hold a private NIB page reference when a reset is unconfirmed

In reply to Jason J. Herne
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] s390/vfio-ap: guest-triggerable permanent folio leak in vfio_ap_irq_disable() - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] s390/vfio-ap: guest-triggerable permanent folio leak in vfio_ap_irq_disable()
- [Medium] s390/vfio-ap: get_page() in vfio_ap_leak_aqic_resources() drops FOLL_PIN
--

commit 3b9e364f526db43b7ae435971ce6e07ced29e979
Author: Jason J. Herne <[email protected]>

s390/vfio-ap: Hold a private NIB page reference when a reset is unconfirmed

This commit saves the pinned NIB struct page in vfio_ap_queue and adds vfio_ap_leak_aqic_resources() to take an extra page reference before unpinning from VFIO when a queue reset or IRQ disable cannot be confirmed.

Quoted 4 lines
drivers/s390/crypto/vfio_ap_ops.c +0 -0
index fba6f256ce77a..d460337e365f2 100644

[ ... ]

drivers/s390/crypto/vfio_ap_ops.c static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q) · line 313

Quoted 10 lines
drivers/s390/crypto/vfio_ap_ops.c +10 -0
+static void vfio_ap_leak_aqic_resources(struct vfio_ap_queue *q)
+{
+	if (q->saved_page) {
+		pr_warn_ratelimited("vfio_ap: APQN %02x.%04x: leaking NIB page to prevent reuse while the queue may still write to it\n",
+				    AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn));
+		get_page(q->saved_page);
+		q->saved_page = NULL;
+	}
+	vfio_ap_free_aqic_resources(q);
+}

[Severity: Medium]
Does get_page() lose FOLL_PIN DMA protection once vfio_unpin_pages() runs?

get_page() only takes a plain refcount, so once vfio_unpin_pages() drops VFIO's pin, folio_maybe_dma_pinned() becomes false while hardware may still write to the NIB. Paths gated on it, such as shrink_folio_list() on file-backed guest memory, then lose DMA protection.

Also, if VFIO type1 maps a VM_PFNMAP reserved page without GUP pinning, get_page() on a zero-refcount page can hit VM_WARN_ON_FOLIO(). Using folio_add_pins(page_folio(q->saved_page), 1) when folio_maybe_dma_pinned() is true would preserve FOLL_PIN semantics and skip non-GUP reserved pages.

[ ... ]

drivers/s390/crypto/vfio_ap_ops.c static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q) · line 370

Quoted 9 lines
drivers/s390/crypto/vfio_ap_ops.c +2 -0
@@ -338,6 +363,8 @@ static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
 
 	WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
 		  status.response_code);
+	vfio_ap_leak_aqic_resources(q);
+	return status;
 end_free:
 	vfio_ap_free_aqic_resources(q);
 	return status;

[Severity: High]
Can an untrusted guest use this path to permanently leak host folios?

handle_pqap() calls vfio_ap_irq_disable() on guest PQAP(AQIC) with IR clear, while PQAP(RAPQ) and PQAP(ZAPQ) run in SIE without interception. A guest can enable IRQs on a page and keep the queue busy or resetting from another vCPU across the 100ms retry loop to hit vfio_ap_leak_aqic_resources().

That drops the VFIO pin (releasing RLIMIT_MEMLOCK) while taking a permanent folio reference and leaving the queue assigned. The guest can repeat this on new folios to exhaust host RAM, and the leak survives VM teardown.

Unlike unmap_iova(), handle_pqap() does not need to unpin the NIB when ap_aqic() is busy or resetting; keeping the VFIO pin and returning the status lets the guest retry without leaking memory.

Even on the unmap_iova() path, polling for only 100ms (compared to VFIOAP_RESET_TIMEOUT of 2000ms) risks leaking a folio if a normal queue reset coincides with a DMA unmap.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#10 lore raw
In reply to Jason J. Herne
Sashiko has finished reviewing this 5-patch series:

Sashiko has finished reviewing this 5-patch series:

- Patch 5: no issues found.
- Patch 1 ("s390/vfio-ap: Prevent passing unreset queues"): 2 potential issues (1 High, 1 Low)
- Patch 2 ("s390/vfio-ap: bound the wait for queue reset verific..."): 3 potential issues (2 High, 1 Low)
- Patch 3 ("s390/vfio-ap: Hold a private NIB page reference when..."): 2 potential issues (1 High, 1 Medium)
- Patch 4 ("s390/vfio-ap: clear matrix_mdev link for queues not..."): 1 potential issue (1 High)

Detailed reviews have been sent as separate replies to patches 1-4.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]