Skip to contentSkip to conversation

s390/vfio-ap: Fix leaks of pinned NIB and registered GISC

PATCH New Anthony Krowiak 1 participant 4 messages Oct 12 1 message not in the archive

Subsystems (4)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Anthony Krowiak

author maintainer
#1 lore raw

PATCH v10 1/6 s390/vfio-ap: Fix leaks of pinned NIB and registered GISC

Several code paths in the vfio_ap driver failed to free the AQIC resources — the pinned guest NIB page and the registered guest ISC used to enable interrupts for a queue — when a queue became unavailable or when unexpected response codes were returned. This could cause memory exhaustion and depletion of KVM interrupt subclass registrations over time with repeated dynamic AP reconfiguration. On the other hand, there are situations whereby these resources must be intentionally leaked. If the page were unpinned and returned to the allocator, a subsequent wild DMA-write to that physical address would corrupt memory belonging to the new owner and could crash or compromise the host kernel.

vfio_ap_mdev_reset_queue()
~~~~~~~~~~~~~~~~~~~~~~~~~~
AP_RESPONSE_BUSY is not a valid response code for a PQAP(ZAPQ) instruction, so it is removed. This contradicts what is stated in the following:

commit 411b0109daa52 ("s390/vfio-ap: wait for response code 05 to clear on queue reset")

The AP architecture limits PQAP(ZAPQ) response codes to 01, 02, 03, 04, and 0x0A; 05 is not valid for ZAPQ. As a consequence, AP_RESPONSE_BUSY can never appear in q->reset_status (which holds the ZAPQ result), so the corresponding check in apq_reset_check() is also removed as dead code.

apq_status_check() / apq_reset_check()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The handling of PQAP(TAPQ) response codes in apq_status_check() and the ZAPQ re-issue logic in apq_reset_check() are reworked to correctly distinguish between cases that require waiting and cases that require re-issuing the ZAPQ.

AP_RESPONSE_BUSY (05) is valid for TAPQ. TAPQ returning BUSY says only that the queue is busy — it says nothing about whether our ZAPQ was accepted or is currently zeroizing. Re-issuing the ZAPQ on TAPQ BUSY could interrupt a zeroization already in progress. AP_RESPONSE_BUSY in apq_status_check() is therefore changed from -EAGAIN (which triggered a ZAPQ re-issue) to -EBUSY (keep waiting).

AP_RESPONSE_RESET_IN_PROGRESS (02) from TAPQ means a reset is in progress. It may be our ZAPQ or a prior reset (e.g. a RAPQ from the AP bus or guest). In either case, keep waiting: if it is our ZAPQ it will zeroize the queue; if it is a prior reset, q->reset_status will hold RESET_IN_PROGRESS (our ZAPQ was rejected) and the re-issue condition below will fire a new ZAPQ once TAPQ reports clean.

The re-issue condition in apq_reset_check() covers three cases where a new ZAPQ is genuinely required:
- q->reset_status holds AP_RESPONSE_RESET_IN_PROGRESS: our initial ZAPQ was rejected because a prior reset was already running. That reset does not zeroize the queue; re-issue once it clears.
- q->reset_status holds AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
our initial ZAPQ was rejected because the queue was mid-transition; re-issue once TAPQ reports clean.
- apq_status_check() returns -EAGAIN: TAPQ returned an ASSOC error code (0x35 or 0x36). These asynchronous codes are returned for every AP instruction until the queue is explicitly reset via ZAPQ; re-issuing is necessary to terminate the loop.

vfio_ap_mdev_remove_queue()
~~~~~~~~~~~~~~~~~~~~~~~~~~~
When the AP bus scan detects a queue is no longer in the host AP configuration, vfio_ap_mdev_remove_queue() skips the ZAPQ since issuing it would fail anyway. A comment is added to the existing else branch to document that vfio_ap_free_aqic_resources() is safe to call here because the hardware is gone and can no longer DMA-write to the NIB.

apq_reset_check()
~~~~~~~~~~~~~~~~~
When apq_reset_check() returns from the -EIO path (TAPQ returned an invalid response code), the TAPQ status is now copied to q->reset_status. This ensures the queue is marked not-passable — _queue_passable() checks for AP_RESPONSE_NORMAL — so the queue is not passed through to a guest.

vfio_ap_irq_disable()
~~~~~~~~~~~~~~~~~~~~~
The valid response codes for PQAP(AQIC) include AP_RESPONSE_STATE_CHANGE_IN_PROGRESS (0x0a), AP_RESPONSE_INVALID_GISA (0x08), AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE (0x35), and AP_RESPONSE_ASSOC_FAILED (0x36), none of which were explicitly handled. All four fell through to the default case which freed the AQIC resources. These each should be handled differently:

* AP_RESPONSE_STATE_CHANGE_IN_PROGRESS indicates a transient condition, analogous to AP_RESPONSE_RESET_IN_PROGRESS and AP_RESPONSE_BUSY. It is added to the same case as RESET_IN_PROGRESS and RESPONSE_BUSY whereby the process sleeps for 20ms and the AQIC gets re-executed.

* AP_RESPONSE_INVALID_GISA indicates the AQIC instruction was rejected due to an invalid GISA address. The disable did not take effect and the hardware still holds the NIB page address. The AQIC resources must therefore be intentionally leaked.

* AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE and AP_RESPONSE_ASSOC_FAILED are asynchronous response codes from a previously executed association instruction. All subsequent AQIC calls will end with the asynchronous response code until the queue is reset; therefore the AQIC disable was not executed and the hardware still holds the NIB address. The AQIC resources must therefore be intentionally leaked.

vfio_ap_wait_for_irqclear() is called after AQIC returns NORMAL or OTHERWISE_CHANGED to confirm that the asynchronous disablement has completed (IR=0) before the AQIC resources are freed. The function returns one of:
* 0: IR=0 confirmed; safe to free AQIC resources
* -ENODEV: queue is non-operational; safe to free
* -ETIMEDOUT: IR=0 could not be confirmed within the polling window, or TAPQ returned an async association response code (0x35/0x36) which also prevents confirmation
* -EIO: TAPQ returned an unexpected response code

When IR=0 cannot be confirmed, the AQIC resources are intentionally retained to prevent a wild DMA-write to a freed NIB page. The original AQIC status (NORMAL or OTHERWISE_CHANGED) is returned to the guest unchanged; the host's responsibility is to execute the AQIC instruction on the guest's behalf and return the hardware status. The guest can act on that status as it sees fit.

vfio_ap_free_aqic_resources()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The function used q->matrix_mdev->kvm and &q->matrix_mdev->vdev when releasing the registered guest ISC and pinned NIB page respectively. This is correct on the normal teardown path, but becomes wrong when a queue reset fails and AQIC resources are intentionally retained across a guest lifecycle. If the mdev is subsequently opened by a new guest (new KVM, new IOMMU container), a later call to vfio_ap_free_aqic_resources() would release the old resources against the new guest's KVM and vfio_device. This produces two bugs:

* vfio_unpin_pages(new_vdev, old_iova) unpins from the wrong IOMMU container. The iommufd backend hits WARN_ON(!access); the type1 backend may silently unpin the wrong page or warn. The original pin in the old container is never dropped, causing vfio to warn about a non-empty pfn_list at container teardown.

* kvm_s390_gisc_unregister() could fire against the wrong KVM if the old struct kvm is freed and a new one allocated at the same address (struct kvm is kzalloc'd and the slab allocator can reuse addresses), silently dropping an ISC registration the new guest still needs.

Fix this by introducing struct vfio_ap_aqic_resources, which groups the ISC, NIB IOVA, and the owning vfio_device and KVM together, replacing the standalone saved_isc and saved_iova fields in struct vfio_ap_queue. vfio_ap_irq_enable() snapshots the owning vdev and kvm at acquire time, taking a KVM reference via kvm_get_kvm() to guarantee the pointer remains valid. vfio_ap_free_aqic_resources() then always releases each resource against its original owner, dropping the KVM reference with kvm_put_kvm() after unregistering the ISC.

vfio_ap_mdev_unset_kvm() and vfio_ap_unlink_mdev_fr_queue() clear vdev, kvm (dropping the reference), and isc from aqic_resources when the mdev-queue association is severed, since the IOMMU container and KVM of the closing session are no longer valid targets for resource release. The iova is intentionally kept: the physical page remains pinned to prevent a wild DMA-write to a reallocated page.

However, close_device() must not return with any page still pinned. After close_device() returns, vfio core removes the device from iommu->device_list; a subsequent VFIO_IOMMU_UNMAP_DMA will find the pin in dma->pfn_list, receive no response from the empty device list, retry 10 times, and hit BUG_ON(++retries > 10). To prevent this, vfio_ap_mdev_unset_kvm() force-unpins any NIB pages that survive the close-time reset, logging pr_crit. This carries the same theoretical wild-DMA risk as the force-unpin in unmap_iova(), and is chosen for the same reason: a guaranteed host panic is worse than a theoretical risk from already-misbehaving hardware.

The same force-unpin loop also covers the case where userspace closes the container fd without calling VFIO_IOMMU_UNMAP_DMA first. vfio_remove_dma() runs during container teardown and hits WARN_ON(!RB_EMPTY_ROOT(&dma->pfn_list)) for any pin that was not released. Because the force-unpin loop runs after flush_work() has drained all reset workers, iova is zero for every queue before close_device() returns, so pfn_list is already empty when vfio_remove_dma() runs.

Note: it is not known whether hardware also writes to the GISA on interrupt delivery (in addition to the NIB). If it does, keeping only the NIB pinned is insufficient since the GISA is freed with the VM in kvm_arch_destroy_vm() regardless of vfio_ap state. The current code assumes the NIB is the sole DMA target.

unmap_iova()
~~~~~~~~~~~~
Calls vfio_ap_irq_disable() to disable interrupts for the queue, but does not check the result. If the IRQ disable failed or could not be confirmed, vfio_ap_irq_disable() leaks the NIB to prevent a wild DMA-write; however, the vfio core requires that the NIB page be unpinned before dma_unmap returns, or it will BUG_ON after 10 re-notification rounds.

The fix is to fall back to a bounded queue reset via ZAPQ, which zeroizes the NIB pointer in the hardware, eliminating the DMA risk that justified the leak. If the reset completes, the worker frees the AQIC resources. If the reset times out or fails, the NIB is force-unpinned with pr_crit — the same trade-off accepted in the close_device() path above.

Fixes ec89b55e3bce7 ("s390: ap: implement PAPQ AQIC interception in kernel")
---
 drivers/s390/crypto/vfio_ap_ops.c     | 506 ++++++++++++++++++++++----
 drivers/s390/crypto/vfio_ap_private.h |  51 ++-
 2 files changed, 463 insertions(+), 94 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +427 -79
index 4db878c18f41..084a687762eb 100644
@@ -31,6 +31,7 @@
 #define AP_QUEUE_IN_USE "in use"
 
 #define AP_RESET_INTERVAL		20	/* Reset sleep interval (20ms)		*/
+#define AP_RESET_MAX_WAIT		2000	/* Maximum wait for reset (2000ms)	*/
 
 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
 static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
@@ -226,27 +227,48 @@ static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
 }
 
 /**
- * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
- * @apqn: The AP Queue number
- *
- * Checks the IRQ bit for the status of this APQN using ap_tapq.
- * Returns if the ap_tapq function succeeded and the bit is clear.
- * Returns if ap_tapq function failed with invalid, deconfigured or
- * checkstopped AP.
- * Otherwise retries up to 5 times after waiting 20ms.
+ * vfio_ap_wait_for_irqclear - wait for the IR bit to clear after a disable
+ *
+ * @q:			the vfio_ap_queue
+ * @tapq_status:	used to return the TAPQ status to the caller
+ *
+ * Repeatedly polls the AP queue status via PQAP(TAPQ) every 20ms until the IR
+ * bit is clear, the queue becomes non-operational, or 6 retries are exhausted.
+ *
+ * Because PQAP(AQIC) disable initiates an asynchronous process, a
+ * condition-code 0 completion does not guarantee the IR bit has been cleared.
+ * The host must confirm IR=0 before unpinning the NIB page to avoid a wild
+ * DMA write to a freed page.
+ *
+ * Return:
+ * 0		if the IR bit is clear (i.e., interrupts are disabled).
+ *
+ * -ENODEV	if the PQAP-TAPQ response code indicates the queue is not available,
+ *		is deconfigured, or is checkstopped (i.e., not operational).
+ *
+ * -ETIMEDOUT	the function timed out before the IR bit was cleared, or TAPQ
+ *		returned AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE or
+ *		AP_RESPONSE_ASSOC_FAILED, which mean the instruction was not
+ *		executed and will continue to be returned for all subsequent
+ *		instructions except ZAPQ until the queue is reset. Since IR=0
+ *		cannot be confirmed, the NIB must be treated as a potential DMA
+ *		target and leaked rather than freed.
+ *
+ * -EIO		PQAP-TAPQ returned an invalid response code
  */
-static void vfio_ap_wait_for_irqclear(int apqn)
+static int vfio_ap_wait_for_irqclear(int apqn, struct ap_queue_status *tapq_status)
 {
 	struct ap_queue_status status;
 	int retry = 5;
 
 	do {
 		status = ap_tapq(apqn, NULL);
+		memcpy(tapq_status, &status, sizeof(status));
 		switch (status.response_code) {
 		case AP_RESPONSE_NORMAL:
 		case AP_RESPONSE_RESET_IN_PROGRESS:
 			if (!status.irq_enabled)
-				return;
+				return 0;
 			fallthrough;
 		case AP_RESPONSE_BUSY:
 			msleep(20);
@@ -254,90 +276,213 @@ static void vfio_ap_wait_for_irqclear(int apqn)
 		case AP_RESPONSE_Q_NOT_AVAIL:
 		case AP_RESPONSE_DECONFIGURED:
 		case AP_RESPONSE_CHECKSTOPPED:
+			WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
+				  status.response_code, apqn);
+			return -ENODEV;
+		case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
+		case AP_RESPONSE_ASSOC_FAILED:
+			/*
+			 * The TAPQ instruction was not executed. Executing TAPQ
+			 * again will result in the same error until the queue
+			 * is reset. We shouldn't, however, reset the queue in
+			 * this context, so log a warning and return -ETIMEDOUT
+			 * since that would happen anyway if we continued to
+			 * execute the TAPQ.
+			 */
+			WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
+				  status.response_code, apqn);
+			return -ETIMEDOUT;
 		default:
 			WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
 				  status.response_code, apqn);
-			return;
+			return -EIO;
 		}
 	} while (--retry);
 
-	WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
-		  __func__, status.response_code, apqn);
+	WARN_ONCE(1, "%s: tapq rc %02x: timed out waiting for interrupts disabled for %02x.%04x\n",
+		  __func__, status.response_code,
+		  AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+	return -ETIMEDOUT;
 }
 
 /**
- * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
+ * vfio_ap_free_aqic_resources - free vfio_ap_queue AQIC resources
  * @q: The vfio_ap_queue
  *
- * 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.
+ * Unregisters the ISC from the GIB and unpins the NIB page, using the KVM
+ * instance and vfio_device snapshotted at enable time in q->aqic_resources.
+ *
+ * For the ISC: always call kvm_s390_gisc_unregister() on the KVM that was
+ * passed to kvm_s390_gisc_register() at enable time (q->aqic_resources.kvm).
+ * A reference is held on that KVM (via kvm_get_kvm() at enable time) so the
+ * pointer is always valid here. The reference is dropped after unregistering.
+ *
+ * For the NIB: always use q->aqic_resources.vdev — the vfio_device that owns
+ * the IOMMU container in which the page was pinned — regardless of what
+ * q->matrix_mdev currently points to.
  */
 static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
 {
 	if (!q)
 		return;
-	if (q->saved_isc != VFIO_AP_ISC_INVALID) {
-		if (!WARN_ON(!q->matrix_mdev) && q->matrix_mdev->kvm)
-			kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
-		q->saved_isc = VFIO_AP_ISC_INVALID;
+
+	if (q->aqic_resources.isc != VFIO_AP_ISC_INVALID) {
+		if (q->aqic_resources.kvm) {
+			kvm_s390_gisc_unregister(q->aqic_resources.kvm, q->aqic_resources.isc);
+			kvm_put_kvm(q->aqic_resources.kvm);
+		}
+		q->aqic_resources.isc = VFIO_AP_ISC_INVALID;
+		q->aqic_resources.kvm = NULL;
 	}
-	if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
-		vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
-		q->saved_iova = 0;
+
+	if (q->aqic_resources.iova) {
+		if (q->aqic_resources.vdev)
+			vfio_unpin_pages(q->aqic_resources.vdev, q->aqic_resources.iova, 1);
+		else
+			pr_warn_ratelimited("APQN %02x.%04x: Failed to unpin NIB page at %pad\n",
+					    AP_QID_CARD(q->apqn),
+					    AP_QID_QUEUE(q->apqn),
+					    &q->aqic_resources.iova);
+		q->aqic_resources.iova = 0;
+		q->aqic_resources.vdev = NULL;
 	}
 }
 
 /**
- * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
- * @q: The vfio_ap_queue
+ * vfio_ap_irq_disable - disable interrupts for an AP queue
+ * @q: the vfio_ap_queue
+ *
+ * Issues PQAP(AQIC) to disable interrupts for the AP queue. On success
+ * (AP_RESPONSE_NORMAL or AP_RESPONSE_OTHERWISE_CHANGED), polls via
+ * vfio_ap_wait_for_irqclear() until the IR bit is confirmed clear before
+ * freeing the pinned NIB page and unregistering the guest ISC. This wait is
+ * necessary because AQIC disable is asynchronous: freeing the NIB before IR=0
+ * is confirmed risks a wild DMA write to a freed host page.
  *
- * Uses ap_aqic to disable the interruption and in case of success, reset
- * in progress or IRQ disable command already proceeded: calls
- * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
- * and calls vfio_ap_free_aqic_resources() to free the resources associated
- * with the AP interrupt handling.
+ * Retries up to 5 times (with 20ms sleep) if the queue is busy or a reset is
+ * in progress.
  *
- * In the case the AP is busy, or a reset is in progress,
- * retries after 20ms, up to 5 times.
+ * If the IR bit cannot be confirmed clear (timeout), the NIB page and guest
+ * ISC are intentionally leaked. If the page were unpinned and returned to the
+ * allocator, a subsequent hardware DMA write to that physical address would
+ * corrupt memory belonging to a new owner — a wild DMA write that could crash
+ * or compromise the host kernel.
  *
- * Returns if ap_aqic function failed with invalid, deconfigured or
- * checkstopped AP.
+ * If the queue is non-operational (deconfigured, checkstopped, not available),
+ * resources are freed immediately since the hardware can no longer write to
+ * the NIB.
  *
  * Return: &struct ap_queue_status
  */
 static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
 {
 	union ap_qirq_ctrl aqic_gisa = { .value = 0 };
-	struct ap_queue_status status;
-	int retries = 5;
+	struct ap_queue_status status, tapq_status;
+	int retries = 5, ret;
 
 	do {
 		status = ap_aqic(q->apqn, aqic_gisa, 0);
 		switch (status.response_code) {
 		case AP_RESPONSE_OTHERWISE_CHANGED:
 		case AP_RESPONSE_NORMAL:
-			vfio_ap_wait_for_irqclear(q->apqn);
-			goto end_free;
+			/*
+			 * AQIC disable was accepted (NORMAL), or the queue was
+			 * already disabled or a prior async request is still
+			 * completing (OTHERWISE_CHANGED).  In both cases, we must
+			 * wait until interrupt processing has been disabled
+			 * before proceeding.
+			 */
+			ret = vfio_ap_wait_for_irqclear(q->apqn, &tapq_status);
+			if (ret == 0 || ret == -ENODEV)
+				goto end_free;
+
+			if (ret == -EIO) {
+				/*
+				 * An unknown TAPQ response code was returned which
+				 * indicates a bug or some type of hardware I/O issue.
+				 * Since we don't know whether queue interrupts were
+				 * disabled or not, the AQIC resources must be leaked.
+				 */
+				memcpy(&status, &tapq_status, sizeof(status));
+				goto end_fail;
+			}
+
+			/* Timed out waiting to confirm interrupts are disabled */
+			if (tapq_status.response_code == AP_RESPONSE_NORMAL ||
+			    tapq_status.response_code == AP_RESPONSE_BUSY) {
+				/*
+				 * If AQIC returned NORMAL, the guest would incorrectly
+				 * interpret that as a successful disable and may free or
+				 * reuse the NIB while hardware can still write to it.
+				 * AP_RESPONSE_BUSY is not valid for PQAP-AQIC.
+				 *
+				 * Return OTHERWISE_CHANGED to signal to the guest that
+				 * the disable interrupts operation did not complete.
+				 */
+				memset(&status, 0, sizeof(status));
+				status.response_code = AP_RESPONSE_OTHERWISE_CHANGED;
+			} else {
+				/*
+				 * For all other TAPQ response codes,
+				 * return the TAPQ status directly since those
+				 * codes are also valid for AQIC.
+				 */
+				memcpy(&status, &tapq_status, sizeof(status));
+			}
+			goto end_fail;
 		case AP_RESPONSE_RESET_IN_PROGRESS:
 		case AP_RESPONSE_BUSY:
+		case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
 			msleep(20);
 			break;
 		case AP_RESPONSE_Q_NOT_AVAIL:
 		case AP_RESPONSE_DECONFIGURED:
 		case AP_RESPONSE_CHECKSTOPPED:
+			/* AP not operational; no further interrupts possible */
+			WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
+				  status.response_code);
+			goto end_free;
 		case AP_RESPONSE_INVALID_ADDRESS:
+		case AP_RESPONSE_INVALID_GISA:
+		case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
+		case AP_RESPONSE_ASSOC_FAILED:
 		default:
-			/* All cases in default means AP not operational */
+			/*
+			 * The AQIC disable was rejected; IRQ is still enabled
+			 * and the hardware still holds the NIB address. Do not
+			 * free resources.
+			 */
 			WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
 				  status.response_code);
-			goto end_free;
+			goto end_fail;
 		}
 	} while (retries--);
 
 	WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
 		  status.response_code);
+
+end_fail:
+	/*
+	 * We are here either because the AQIC instruction failed to disable
+	 * interrupts, or because IR=0 could not be confirmed. In either case
+	 * the NIB page and guest ISC cannot be freed: hardware may still write
+	 * to the NIB, and unpinning the page would allow it to be reallocated
+	 * to a new owner. A subsequent hardware DMA write to that physical
+	 * address would corrupt the new owner's memory — a wild DMA write that
+	 * could crash or compromise the host kernel. The resources are
+	 * therefore intentionally leaked.
+	 */
+	return status;
+
 end_free:
+	/*
+	 * This label is reached because the queue was successfully disabled,
+	 * or because the queue is not operational or not available, in which case
+	 * interrupts can not be processed, so free the AQIC resources - the pinned NIB
+	 * page and the registered guest ISC - used to enable interrupts so they will
+	 * not be leaked.
+	 */
 	vfio_ap_free_aqic_resources(q);
 	return status;
 }
@@ -401,22 +546,29 @@ static int ensure_nib_shared(unsigned long addr)
 }
 
 /**
- * vfio_ap_irq_enable - Enable Interruption for a APQN
+ * vfio_ap_irq_enable - enable interrupts for an AP queue on behalf of a guest
  *
- * @q:	 the vfio_ap_queue holding AQIC parameters
+ * @q:	 the vfio_ap_queue for which interrupts are to be enabled
  * @isc: the guest ISC to register with the GIB interface
- * @vcpu: the vcpu object containing the registers specifying the parameters
- *	  passed to the PQAP(AQIC) instruction.
+ * @vcpu: the vcpu whose registers contain the PQAP(AQIC) parameters
+ *
+ * Pins the guest NIB page, registers the guest ISC with the GIB to obtain a
+ * host ISC, and reissues PQAP(AQIC) with the translated host-absolute NIB
+ * address and host ISC on behalf of the guest.
  *
- * Pin the NIB saved in *q
- * Register the guest ISC to GIB interface and retrieve the
- * host ISC to issue the host side PQAP/AQIC
+ * The condition code and AP-queue status word returned by PQAP(AQIC) are
+ * reflected back to the guest as-is. IRQ state verification (polling until
+ * IR=1) is the responsibility of the guest AP bus, not the host.
  *
- * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
- * vfio_pin_pages or kvm_s390_gisc_register failed.
+ * Resource management is based solely on whether hardware accepted the new NIB:
+ * - AP_RESPONSE_NORMAL (CC=0): hardware accepted the new NIB; the old pinned
+ *   NIB page and registered guest ISC are freed and the new ones saved.
+ * - All other responses: hardware did not accept the new NIB; the newly pinned
+ *   page and registered ISC are freed and the previously saved resources are
+ *   left intact.
  *
- * Otherwise return the ap_queue_status returned by the ap_aqic(),
- * all retry handling will be done by the guest.
+ * AP_RESPONSE_INVALID_ADDRESS is returned if vfio_pin_pages() or
+ * kvm_s390_gisc_register() fails before the AQIC instruction is issued.
  *
  * Return: &struct ap_queue_status
  */
@@ -428,11 +580,10 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
 	struct ap_queue_status status = {};
 	struct kvm_s390_gisa *gisa;
 	struct page *h_page;
-	int nisc;
+	int nisc, ret;
 	struct kvm *kvm;
 	phys_addr_t h_nib;
 	dma_addr_t nib;
-	int ret;
 
 	/* Verify that the notification indicator byte address is valid */
 	if (vfio_ap_validate_nib(vcpu, &nib)) {
@@ -489,27 +640,49 @@ static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
 	status = ap_aqic(q->apqn, aqic_gisa, h_nib);
 	switch (status.response_code) {
 	case AP_RESPONSE_NORMAL:
-		/* See if we did clear older IRQ configuration */
+		/*
+		 * Hardware accepted the new NIB address (CC=0). The old NIB and
+		 * guest ISC are no longer used by hardware and can be freed.
+		 * The new resources are saved for tracking and future teardown.
+		 *
+		 * IRQ state verification (polling until IR=1) is the
+		 * responsibility of the guest AP bus, not the host. The
+		 * condition code and status word are reflected back to the
+		 * guest to respond to the PQAP-AQIC instruction.
+		 */
 		vfio_ap_free_aqic_resources(q);
-		q->saved_iova = nib;
-		q->saved_isc = isc;
+		q->aqic_resources.iova = nib;
+		q->aqic_resources.isc  = isc;
+		q->aqic_resources.vdev = &q->matrix_mdev->vdev;
+		kvm_get_kvm(q->matrix_mdev->kvm);
+		q->aqic_resources.kvm  = q->matrix_mdev->kvm;
 		break;
 	case AP_RESPONSE_OTHERWISE_CHANGED:
-		/* We could not modify IRQ settings: clear new configuration */
+		/*
+		 * Interrupts are already enabled or a prior async request is still
+		 * completing. Either way, the hardware's current NIB is the one
+		 * saved in q->aqic_res — not the newly prepared resources.
+		 * Release the newly pinned page and registered ISC;
+		 * leave aqic_res intact.
+		 */
+		fallthrough;
+	default:
+		/*
+		 * Hardware did not accept the new NIB (CC=3 or error). The
+		 * previously saved NIB and guest ISC remain active and must
+		 * not be freed. Release the newly pinned page and registered
+		 * ISC that were prepared for this (rejected) request.
+		 */
 		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",
@@ -635,7 +808,6 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	}
 
 	status = vcpu->run->s.regs.gprs[1];
-
 	/* If IR bit(16) is set we enable the interrupt */
 	if ((status >> (63 - 16)) & 0x01)
 		qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
@@ -851,6 +1023,12 @@ static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
 {
 	q->matrix_mdev = NULL;
+	q->aqic_resources.vdev = NULL;
+	if (q->aqic_resources.kvm) {
+		kvm_put_kvm(q->aqic_resources.kvm);
+		q->aqic_resources.kvm = NULL;
+	}
+	q->aqic_resources.isc = VFIO_AP_ISC_INVALID;
 }
 
 static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
@@ -864,7 +1042,7 @@ static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
 			q = vfio_ap_mdev_get_queue(matrix_mdev,
 						   AP_MKQID(apid, apqi));
 			if (q)
-				q->matrix_mdev = NULL;
+				vfio_ap_unlink_mdev_fr_queue(q);
 		}
 	}
 }
@@ -1857,8 +2035,46 @@ static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
 	int loop_cursor;
 
 	hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
-		if (q->saved_iova >= iova && q->saved_iova < iova + length)
+		if (q->aqic_resources.iova >= iova && q->aqic_resources.iova < iova + length) {
 			vfio_ap_irq_disable(q);
+			/*
+			 * If AQIC disable was unable to confirm that interrupts were
+			 * disabled (IR=0), q->aqic_resources.iova remains non-zero.
+			 * VFIO core requires the mapped pages to be unpinned during
+			 * DMA unmap notifications (the type1 IOMMU backend will
+			 * BUG_ON after 10 re-notification retries if the pin is not
+			 * released).
+			 *
+			 * To satisfy VFIO core safely, fall back to a queue reset
+			 * via ZAPQ. ZAPQ wipes the queue state and clears the
+			 * hardware's internal NIB address register, neutralizing
+			 * pending DMA. Flush the reset worker so that we know
+			 * whether the NIB was released before returning.
+			 */
+			if (q->aqic_resources.iova) {
+				vfio_ap_mdev_reset_queue(q);
+				flush_work(&q->reset_work);
+			}
+
+			/*
+			 * If the NIB is still pinned after the reset (reset failed
+			 * or timed out), force-unpin it anyway. A host kernel panic
+			 * from the VFIO BUG_ON is worse than the theoretical risk of
+			 * a wild DMA write from already-misbehaving hardware, and a
+			 * pr_crit message records that the forced unpin occurred.
+			 */
+			if (q->aqic_resources.iova) {
+				pr_crit("APQN %02x.%04x: NIB page at %pad could not be safely unpinned after queue reset failure; force-unpinning to prevent kernel panic\n",
+					AP_QID_CARD(q->apqn),
+					AP_QID_QUEUE(q->apqn),
+					&q->aqic_resources.iova);
+				if (q->aqic_resources.vdev)
+					vfio_unpin_pages(q->aqic_resources.vdev,
+							 q->aqic_resources.iova, 1);
+				q->aqic_resources.iova = 0;
+				q->aqic_resources.vdev = NULL;
+			}
+		}
 	}
 }
 
@@ -1884,6 +2100,8 @@ static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
 static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 {
 	struct kvm *kvm = matrix_mdev->kvm;
+	unsigned long apid, apqi;
+	struct vfio_ap_queue *q;
 
 	if (kvm && kvm->arch.crypto.crycbd) {
 		get_update_locks_for_kvm(kvm);
@@ -1896,6 +2114,50 @@ static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
 		release_update_locks_for_kvm(kvm);
 		kvm_put_kvm(kvm);
 	}
+
+	/*
+	 * The reset above may not have been able to confirm that all queues
+	 * were zeroized (e.g. worker -EIO, unexpected ZAPQ rc). Any queue
+	 * that still has iova set holds a pin in the IOMMU container that is
+	 * about to go away. The vfio contract requires every pinned page to
+	 * be released before close_device() returns: vfio core removes the
+	 * device from iommu->device_list after close_device() returns, after
+	 * which vfio_notify_dma_unmap() becomes a no-op and a later
+	 * VFIO_IOMMU_UNMAP_DMA will hit BUG_ON(++retries > 10) in type1.
+	 *
+	 * Force-unpin any surviving NIB pages now, while vdev is still valid.
+	 * This carries the same theoretical wild-DMA risk as the force-unpin
+	 * in unmap_iova(), and is chosen for the same reason: a guaranteed
+	 * host panic is worse than a theoretical risk on already-misbehaving
+	 * hardware.
+	 */
+	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
+		for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
+			q = vfio_ap_mdev_get_queue(matrix_mdev,
+						   AP_MKQID(apid, apqi));
+			if (!q)
+				continue;
+
+			if (q->aqic_resources.iova) {
+				pr_crit("APQN %02x.%04x: NIB page at %pad could not be safely unpinned after queue reset failure; force-unpinning to prevent kernel panic\n",
+					AP_QID_CARD(q->apqn),
+					AP_QID_QUEUE(q->apqn),
+					&q->aqic_resources.iova);
+				if (q->aqic_resources.vdev)
+					vfio_unpin_pages(q->aqic_resources.vdev,
+							 q->aqic_resources.iova,
+							 1);
+				q->aqic_resources.iova = 0;
+				q->aqic_resources.vdev = NULL;
+			}
+
+			if (q->aqic_resources.kvm) {
+				kvm_put_kvm(q->aqic_resources.kvm);
+				q->aqic_resources.kvm = NULL;
+			}
+			q->aqic_resources.isc = VFIO_AP_ISC_INVALID;
+		}
+	}
 }
 
 static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
@@ -1919,20 +2181,59 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
 {
 	switch (status->response_code) {
 	case AP_RESPONSE_NORMAL:
+		/*
+		 * This response code only indicates that the PQAP(ZAPQ) has
+		 * been initiated. The following bit settings in the status
+		 * returned from TAPQ must be verified to confirm that the
+		 * asynchronous portion of the queue zeroization has completed.
+		 */
+		if (status->queue_empty && !status->replies_waiting &&
+		    !status->irq_enabled && !status->async)
+			return 0;
+
+		/* Async zeroization still in progress; keep waiting */
+		return -EBUSY;
+
 	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:
+		/*
+		 * A reset is in progress. It may be our ZAPQ or a prior reset
+		 * (e.g. a RAPQ from the AP bus or guest). Keep waiting: if it
+		 * is our ZAPQ it will zeroize the queue; if it is a prior reset,
+		 * q->reset_status holds RESET_IN_PROGRESS (our ZAPQ was
+		 * rejected) and apq_reset_check() will re-issue the ZAPQ once
+		 * this reset clears.
+		 */
+		return -EBUSY;
+
 	case AP_RESPONSE_BUSY:
+		/*
+		 * TAPQ returned BUSY (rc 05). This says the queue is busy but
+		 * tells us nothing about whether our ZAPQ was accepted: the
+		 * zeroization may already be in progress. Treat it the same as
+		 * RESET_IN_PROGRESS — keep waiting without re-issuing the ZAPQ.
+		 */
 		return -EBUSY;
+
 	case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
 	case AP_RESPONSE_ASSOC_FAILED:
 		/*
 		 * These asynchronous response codes indicate a PQAP(AAPQ)
 		 * instruction to associate a secret with the guest failed. All
 		 * subsequent AP instructions will end with the asynchronous
-		 * response code until the AP queue is reset; so, let's return
-		 * a value indicating a reset needs to be performed again.
+		 * response code until the AP queue is reset. Re-issue the ZAPQ.
 		 */
 		return -EAGAIN;
 	case AP_RESPONSE_Q_NOT_AVAIL:
@@ -1961,10 +2262,23 @@ 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)
-			return;
-		if (ret == -ENODEV) {
-			vfio_ap_free_aqic_resources(q);
+		if (ret == -EIO) {
+			/*
+			 * TAPQ returned an invalid response code indicating a
+			 * hardware or firmware bug. Since we cannot determine
+			 * whether the queue can still DMA-write to the NIB, the
+			 * AQIC resources are intentionally leaked lest the NIB
+			 * page is reallocated to a new owner. A subsequent
+			 * wild DMA-write to that physical address would corrupt
+			 * the new owner's memory which could crash or compromise
+			 * the host kernel.
+			 *
+			 * Record the TAPQ status so the queue is marked
+			 * not-passable - _queue_passable() checks
+			 * reset_status.response_code == AP_RESPONSE_NORMAL -
+			 * and the queue is not passed through to a guest.
+			 */
+			memcpy(&q->reset_status, &status, sizeof(status));
 			return;
 		}
 		if (ret == -EBUSY) {
@@ -1976,15 +2290,33 @@ static void apq_reset_check(struct work_struct *reset_work)
 					      status.irq_enabled);
 		} else {
 			if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
-			    q->reset_status.response_code == AP_RESPONSE_BUSY ||
 			    q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
 			    ret == -EAGAIN) {
+				/*
+				 * RESET_IN_PROGRESS in reset_status means the
+				 * initial ZAPQ was rejected because a prior reset
+				 * was already running. That reset does not zeroize
+				 * the queue; re-issue the ZAPQ once it clears.
+				 *
+				 * STATE_CHANGE_IN_PROGRESS in reset_status means
+				 * the initial ZAPQ was rejected because the queue
+				 * was mid-transition; re-issue once it clears.
+				 *
+				 * -EAGAIN means TAPQ returned an ASSOC error code
+				 * (0x35/0x36): the queue is stuck until reset.
+				 * Re-issue the ZAPQ to recover.
+				 */
 				status = ap_zapq(q->apqn, 0);
 				memcpy(&q->reset_status, &status, sizeof(status));
 				continue;
 			}
-			if (q->saved_isc != VFIO_AP_ISC_INVALID)
-				vfio_ap_free_aqic_resources(q);
+			/*
+			 * We end up here when the reset has completed (TAPQ
+			 * returned NORMAL with all status bits clear, or
+			 * DECONFIGURED/CHECKSTOPPED). The queue is zeroized or
+			 * non-operational; free the AQIC resources.
+			 */
+			vfio_ap_free_aqic_resources(q);
 			break;
 		}
 	}
@@ -2001,7 +2333,6 @@ static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
 	switch (status.response_code) {
 	case AP_RESPONSE_NORMAL:
 	case AP_RESPONSE_RESET_IN_PROGRESS:
-	case AP_RESPONSE_BUSY:
 	case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
 		/*
 		 * Let's verify whether the ZAPQ completed successfully on a work queue.
@@ -2014,6 +2345,15 @@ static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
 		vfio_ap_free_aqic_resources(q);
 		break;
 	default:
+		/*
+		 * An invalid response code indicates a hardware or firmware bug.
+		 * Since we cannot determine whether the queue can still
+		 * DMA-write to the NIB, the AQIC resources are intentionally
+		 * leaked lest the NIB page is reallocated to a new owner. A
+		 * subsequent hardware wild DMA-write to that physical address
+		 * would corrupt the new owner's memory which could crash or
+		 * compromise the host kernel.
+		 */
 		WARN(true,
 		     "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
 		     AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
@@ -2463,7 +2803,8 @@ int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
 	}
 
 	q->apqn = apqn;
-	q->saved_isc = VFIO_AP_ISC_INVALID;
+	q->aqic_resources.isc = VFIO_AP_ISC_INVALID;
+	/* aqic_res.iova, .vdev, .kvm all zero-initialised by kzalloc_obj */
 	memset(&q->reset_status, 0, sizeof(q->reset_status));
 	INIT_WORK(&q->reset_work, apq_reset_check);
 
@@ -2543,6 +2884,13 @@ void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
 		vfio_ap_mdev_reset_queue(q);
 		flush_work(&q->reset_work);
 	} else {
+		/*
+		 * The queue is no longer in the host's AP configuration.
+		 * The hardware cannot DMA-write to the NIB, so it is safe
+		 * to free the AQIC resources directly without issuing a
+		 * ZAPQ. If the queue is not assigned to an mdev,
+		 * vfio_ap_free_aqic_resources() is a no-op.
+		 */
 		vfio_ap_free_aqic_resources(q);
 	}
 
drivers/s390/crypto/vfio_ap_private.h +36 -15

65 lines not on this page · raw message

Signature · 1 line
2.53.0
author maintainer
#2 lore raw

PATCH v10 2/6 s390/vfio-ap: Fix failure to release IRQ notification eventfd contexts

In reply to a message not in the archive

When userspace registers IRQ notification eventfds via the VFIO_DEVICE_SET_IRQS ioctl, vfio_ap_set_request_irq() and vfio_ap_set_cfg_change_irq() each call eventfd_ctx_fdget(), which takes a reference on the eventfd_ctx and stores it in matrix_mdev->req_trigger and matrix_mdev->cfg_chg_trigger respectively.

These references are dropped only when userspace explicitly replaces or clears them via a subsequent SET_IRQS call. If the device is closed without that explicit teardown - because the guest exits, the VM process crashes, or the device file is simply closed - neither vfio_ap_mdev_close_device() nor the remove path releases these references. The eventfd_ctx backing objects and their associated file references therefore leak for the lifetime of the kernel.

Fix this by introducing vfio_ap_mdev_release_eventfds() and calling it from vfio_ap_mdev_close_device() after vfio_ap_mdev_unset_kvm(). The VFIO core guarantees that close_device is called before vfio_unregister_group_dev() returns in the remove path, so fixing close_device is sufficient to cover both teardown paths.

Note:
~~~~
The matrix_dev->mdevs lock must be held during the call to vfio_ap_mdev_release_eventfds(). There is a small window between the calls to vfio_ap_mdev_unset_kvm() which gets and releases the update locks and the acquisition of the matrix_dev->mdevs_lock mutex during which it is possible - although highly unlikely during normal operation - whereby a concurrent SET_IRQS call can get in.

Taking matrix_dev->mdevs_lock around vfio_ap_mdev_release_eventfds() is sufficient to make this race-free. The SET_IRQS ioctl path writes req_trigger and cfg_chg_trigger only from vfio_ap_mdev_ioctl(), which holds mdevs_lock for its entire duration and always calls eventfd_ctx_put() on the previous value before storing the new one.

Any number of concurrent SET_IRQS calls during the window between vfio_ap_mdev_unset_kvm() and the acquisition of mdevs_lock are therefore safe: each ioctl invocation puts the reference it found and installs a new one, leaving exactly one live reference in the field when it releases the lock. When release_eventfds subsequently acquires mdevs_lock it finds that single surviving reference and puts it. Conversely, a SET_IRQS call that loses the race and blocks on mdevs_lock will find the field NULL after release_eventfds finishes, take ownership of the reference it just created, and install it into a field that will never be read again - a transient leak. To close that final case, callers must ensure no new SET_IRQS ioctls can be issued after close_device() is called, which the VFIO core guarantees by releasing the device file before invoking close_device().

Fixes bf48961f6f48e ("s390/vfio-ap: realize the VFIO_DEVICE_SET_IRQS ioctl")
---
 drivers/s390/crypto/vfio_ap_ops.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
drivers/s390/crypto/vfio_ap_ops.c +16 -0
index 084a687762eb..394c478a4922 100644
@@ -2408,12 +2408,28 @@ static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
 	return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
 }
 
+static void vfio_ap_mdev_release_eventfds(struct ap_matrix_mdev *matrix_mdev)
+{
+	if (matrix_mdev->req_trigger) {
+		eventfd_ctx_put(matrix_mdev->req_trigger);
+		matrix_mdev->req_trigger = NULL;
+	}
+	if (matrix_mdev->cfg_chg_trigger) {
+		eventfd_ctx_put(matrix_mdev->cfg_chg_trigger);
+		matrix_mdev->cfg_chg_trigger = NULL;
+	}
+}
+
 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
 {
 	struct ap_matrix_mdev *matrix_mdev =
 		container_of(vdev, struct ap_matrix_mdev, vdev);
 
 	vfio_ap_mdev_unset_kvm(matrix_mdev);
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	vfio_ap_mdev_release_eventfds(matrix_mdev);
+	mutex_unlock(&matrix_dev->mdevs_lock);
 }
 
 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
Signature · 1 line
2.53.0
author maintainer
#3 lore raw

PATCH v10 3/6 s390/vfio-ap: Fix unbounded loop in apq_reset_check()

In reply to a message not in the archive

The apq_reset_check() worker polls ap_tapq() in a while(true) loop waiting for a queue reset to complete. When ap_tapq() returns AP_RESPONSE_BUSY or AP_RESPONSE_RESET_IN_PROGRESS, apq_status_check() returns -EBUSY and the loop continues after sleeping AP_RESET_INTERVAL (20ms). There is no upper bound on how many times the loop iterates, so if the hardware continuously returns a busy response the worker runs indefinitely.

This is particularly harmful because several callers of vfio_ap_mdev_reset_queue() - such as vfio_ap_mdev_reset_queues(), vfio_ap_mdev_reset_qlist() and vfio_ap_mdev_remove_queue() - call flush_work() on the queue's reset_work while holding one or more of the global matrix_dev locks (guests_lock, mdevs_lock) or the KVM lock. An indefinitely spinning worker permanently blocks access to all ap_matrix_mdev objects, which could hang other guests that are using them.

Fix this by introducing AP_RESET_MAX_WAIT (2000ms) and breaking out of the poll loop when elapsed time reaches that threshold.

If apq_reset_check() times out before verifying completion of the reset, the NIB page cannot be freed. The NIB is the active DMA target for AP interrupt delivery until the reset completes; freeing the pinned page would allow it to be reallocated to a new owner. A subsequent hardware wild DMA-write to that physical address would corrupt the new owner's memory and could crash or compromise the host kernel.

The ISC registration is not a DMA target and can be freed immediately at timeout. report_apq_reset_check_timeout() unregisters the ISC on the KVM that was used at enable time (held via kvm_get_kvm() since vfio_ap_irq_enable()), preventing kvm_s390_gisa_destroy() from WARNing at VM teardown for every queue whose reset timed out.

If the reset eventually completes, interrupts will be terminated, but the pinned NIB page will be leaked. This is preferable to a compromised kernel or kernel crash, or waiting indefinitely and blocking access to all mdevs, hanging the guests to which they are attached.

To prevent a use-after-free when the leaked NIB outlives the matrix_mdev, vfio_ap_unlink_mdev_fr_queue() clears the embedded vdev pointer on unlinking so subsequent queue cleanup will not dereference freed memory.

This patch also fixes a bug whereby AP_RESPONSE_NORMAL (0) returned from PQAP(ZAPQ) was incorrectly treated as confirmation that the queue was zeroized. AP_RESPONSE_NORMAL only indicates that the ZAPQ was accepted; zeroization is performed asynchronously. To confirm completion, the following bits in the status word returned from PQAP(TAPQ) must all be verified:

status->irq_enabled == 0
status->queue_empty == 1
status->replies_waiting == 0
status->async == 0

This patch also fixes a bug in the top-of-loop ZAPQ re-issue branch of apq_reset_check(). When q->reset_status holds AP_RESPONSE_STATE_CHANGE_IN_PROGRESS from the initial ZAPQ, the worker re-issues the ZAPQ but did not copy the result back to q->reset_status. On the next iteration the stale response code caused the ZAPQ to be re-issued again, repeating every 20ms until the 2s timeout regardless of what the re-issued ZAPQ returned. The memcpy is added to update q->reset_status so the loop transitions correctly to the TAPQ polling path once the re-issued ZAPQ is accepted.

Fixes dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete")
---
 drivers/s390/crypto/vfio_ap_ops.c | 188 ++++++++++++++++++++++--------
 1 file changed, 137 insertions(+), 51 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +137 -51
index 394c478a4922..627f8ed0a960 100644
@@ -1020,6 +1020,15 @@ static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
 	hash_del(&q->mdev_qnode);
 }
 
+/**
+ * vfio_ap_unlink_mdev_fr_queue - dissociate a queue from its matrix_mdev
+ * @q: the vfio_ap_queue
+ *
+ * Dissociates @q from its matrix_mdev and clears any references to the
+ * matrix_mdev's embedded vdev and kvm. This prevents a use-after-free
+ * if a reset timed out (which leaks AQIC resources) and the mdev is
+ * subsequently freed before the queue is destroyed.
+ */
 static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
 {
 	q->matrix_mdev = NULL;
@@ -2232,7 +2241,7 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
 		/*
 		 * These asynchronous response codes indicate a PQAP(AAPQ)
 		 * instruction to associate a secret with the guest failed. All
-		 * subsequent AP instructions will end with the asynchronous
+		 * subsequent PQAP-TAPQ invocations will end with the asynchronous
 		 * response code until the AP queue is reset. Re-issue the ZAPQ.
 		 */
 		return -EAGAIN;
@@ -2247,8 +2256,77 @@ static int apq_status_check(int apqn, struct ap_queue_status *status)
 	}
 }
 
+static void report_apq_reset_check_timeout(struct vfio_ap_queue *q)
+{
+	/*
+	 * The ISC registration updates the refcount in the GISA alert mask.
+	 * Unregister it now on the KVM that was used at enable time; the
+	 * reference kept since vfio_ap_irq_enable() guarantees the pointer
+	 * is valid here.  Leaving the ISC registered would prevent the
+	 * refcount from ever reaching zero, causing kvm_s390_gisa_destroy()
+	 * to WARN at VM teardown for every queue whose reset timed out.
+	 */
+	if (q->aqic_resources.isc != VFIO_AP_ISC_INVALID) {
+		if (q->aqic_resources.kvm) {
+			kvm_s390_gisc_unregister(q->aqic_resources.kvm,
+						 q->aqic_resources.isc);
+			kvm_put_kvm(q->aqic_resources.kvm);
+			q->aqic_resources.kvm = NULL;
+		}
+		q->aqic_resources.isc = VFIO_AP_ISC_INVALID;
+	}
+
+	if (q->aqic_resources.iova) {
+		if (q->matrix_mdev) {
+			dev_warn_ratelimited(mdev_dev(q->matrix_mdev->mdev),
+					     "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));
+		} else {
+			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));
+		}
+	} else {
+		if (q->matrix_mdev) {
+			dev_warn_ratelimited(mdev_dev(q->matrix_mdev->mdev),
+					     "Reset timed out for APQN %02x.%04x\n",
+					     AP_QID_CARD(q->apqn),
+					     AP_QID_QUEUE(q->apqn));
+		} else {
+			pr_warn_ratelimited("Reset timed out for APQN %02x.%04x\n",
+					    AP_QID_CARD(q->apqn),
+					    AP_QID_QUEUE(q->apqn));
+		}
+	}
+}
+
 #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
 
+/**
+ * apq_reset_finalize - store final TAPQ status and free AQIC resources.
+ * @q:      the vfio_ap_queue
+ * @status: the final AP queue status returned by PQAP(TAPQ)
+ * @ret:    the return value from apq_status_check()
+ *
+ * Copies the full TAPQ status word to q->reset_status so that all status
+ * bits reflect the confirmed end state of the queue. If ret == 0,
+ * zeroization was confirmed and the response code is overridden with
+ * AP_RESPONSE_NORMAL so that _queue_passable() returns true. For
+ * ret == -ENODEV (DECONFIGURED or CHECKSTOPPED), the non-zero response
+ * code is left intact so _queue_passable() correctly returns false.
+ * AQIC resources are then freed.
+ */
+static void apq_reset_finalize(struct vfio_ap_queue *q,
+			       struct ap_queue_status *status, int ret)
+{
+	memcpy(&q->reset_status, status, sizeof(*status));
+	if (!ret)
+		q->reset_status.response_code = AP_RESPONSE_NORMAL;
+
+	vfio_ap_free_aqic_resources(q);
+}
+
 static void apq_reset_check(struct work_struct *reset_work)
 {
 	int ret = -EBUSY, elapsed = 0;
@@ -2260,65 +2338,73 @@ static void apq_reset_check(struct work_struct *reset_work)
 	while (true) {
 		msleep(AP_RESET_INTERVAL);
 		elapsed += AP_RESET_INTERVAL;
-		status = ap_tapq(q->apqn, NULL);
-		ret = apq_status_check(q->apqn, &status);
-		if (ret == -EIO) {
-			/*
-			 * TAPQ returned an invalid response code indicating a
-			 * hardware or firmware bug. Since we cannot determine
-			 * whether the queue can still DMA-write to the NIB, the
-			 * AQIC resources are intentionally leaked lest the NIB
-			 * page is reallocated to a new owner. A subsequent
-			 * wild DMA-write to that physical address would corrupt
-			 * the new owner's memory which could crash or compromise
-			 * the host kernel.
-			 *
-			 * Record the TAPQ status so the queue is marked
-			 * not-passable - _queue_passable() checks
-			 * reset_status.response_code == AP_RESPONSE_NORMAL -
-			 * and the queue is not passed through to a guest.
-			 */
+
+		if (q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS) {
+			status = ap_zapq(q->apqn, 0);
 			memcpy(&q->reset_status, &status, sizeof(status));
-			return;
-		}
-		if (ret == -EBUSY) {
-			pr_notice_ratelimited(WAIT_MSG, elapsed,
-					      AP_QID_CARD(q->apqn),
-					      AP_QID_QUEUE(q->apqn),
-					      status.response_code,
-					      status.queue_empty,
-					      status.irq_enabled);
 		} else {
-			if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
-			    q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
-			    ret == -EAGAIN) {
+			status = ap_tapq(q->apqn, NULL);
+			ret = apq_status_check(q->apqn, &status);
+			if (ret == -EIO) {
 				/*
-				 * RESET_IN_PROGRESS in reset_status means the
-				 * initial ZAPQ was rejected because a prior reset
-				 * was already running. That reset does not zeroize
-				 * the queue; re-issue the ZAPQ once it clears.
-				 *
-				 * STATE_CHANGE_IN_PROGRESS in reset_status means
-				 * the initial ZAPQ was rejected because the queue
-				 * was mid-transition; re-issue once it clears.
+				 * TAPQ returned an invalid response code indicating a
+				 * hardware or firmware bug. Since we cannot determine
+				 * whether the queue can still DMA-write to the NIB, the
+				 * AQIC resources are intentionally leaked lest the NIB
+				 * page is reallocated to a new owner. A subsequent
+				 * wild DMA-write to that physical address would corrupt
+				 * the new owner's memory which could crash or compromise
+				 * the host kernel.
 				 *
-				 * -EAGAIN means TAPQ returned an ASSOC error code
-				 * (0x35/0x36): the queue is stuck until reset.
-				 * Re-issue the ZAPQ to recover.
+				 * Record the TAPQ status so the queue is marked
+				 * not-passable - _queue_passable() checks
+				 * reset_status.response_code == AP_RESPONSE_NORMAL -
+				 * and the queue is not passed through to a guest.
 				 */
+				memcpy(&q->reset_status, &status, sizeof(status));
+				return;
+			} else if (ret == -EAGAIN) {
 				status = ap_zapq(q->apqn, 0);
 				memcpy(&q->reset_status, &status, sizeof(status));
-				continue;
+			} else if (!ret || ret == -ENODEV) {
+				/*
+				 * We are here for one of two reasons:
+				 *
+				 * Zeroization confirmed (ret == 0): the TAPQ status bits
+				 * indicate the async portion of the ZAPQ completed
+				 * successfully.
+				 *
+				 * Queue non-operational (ret == -ENODEV): the queue is
+				 * deconfigured or checkstopped; interrupts are not
+				 * possible. Zeroization cannot be confirmed in this state,
+				 * but the queue cannot generate interrupts, so the
+				 * NIB page is no longer a DMA target.
+				 *
+				 * In either case, it is safe to free up the AQIC
+				 * resources.
+				 */
+				apq_reset_finalize(q, &status, ret);
+				return;
 			}
-			/*
-			 * We end up here when the reset has completed (TAPQ
-			 * returned NORMAL with all status bits clear, or
-			 * DECONFIGURED/CHECKSTOPPED). The queue is zeroized or
-			 * non-operational; free the AQIC resources.
-			 */
-			vfio_ap_free_aqic_resources(q);
-			break;
 		}
+
+		/*
+		 * If we haven't returned above, the reset is still in progress
+		 * or being retried. Check for timeout before continuing.
+		 */
+		if (elapsed >= AP_RESET_MAX_WAIT) {
+			report_apq_reset_check_timeout(q);
+			return;
+		}
+
+		if (ret == -EBUSY)
+			pr_notice_ratelimited(WAIT_MSG, elapsed,
+					      AP_QID_CARD(q->apqn),
+					      AP_QID_QUEUE(q->apqn),
+					      status.response_code,
+					      status.queue_empty,
+					      status.irq_enabled);
+
 	}
 }
 
Signature · 1 line
2.53.0
author maintainer
#4 lore raw

PATCH v10 4/6 s390/vfio-ap: fix queue state leakage to guest and host

In reply to a message not in the archive

_queue_passable() accepted AP_RESPONSE_DECONFIGURED and AP_RESPONSE_CHECKSTOPPED as passable states in addition to AP_RESPONSE_NORMAL. Neither DECONFIGURED nor CHECKSTOPPED confirms that the queue was zeroized; only AP_RESPONSE_NORMAL
(0) does. A queue not confirmed zeroized must not be passed through to a guest, as it may contain key material from a previous guest or host operation.

_queue_passable() is therefore changed to a two-step check. First, reset_status.response_code is verified to be AP_RESPONSE_NORMAL; if not, the queue is not passable regardless of its current hardware state. Second, a live PQAP(TAPQ) is issued to detect state changes that occurred after the last reset — such as a subsequent deconfigure or checkstop — without requiring a callback from the AP bus. Only the response code from TAPQ is checked; queue_empty and irq_enabled are not examined because reset_status == AP_RESPONSE_NORMAL already confirms the queue was zeroized and interrupts were disabled at reset time. If TAPQ returns AP_RESPONSE_BUSY, the queue state cannot be determined and the queue is treated as not passable; since reset_status was NORMAL this is expected to be transient and the queue will be re-evaluated on the next filter_matrix call.

vfio_ap_mdev_probe_queue() calls vfio_ap_mdev_reset_queue() and flush_work() while holding the update locks to guarantee a clean queue before it can be admitted to a guest. This adds at least AP_RESET_INTERVAL (20 ms) of lock-hold time per probed queue, since apq_reset_check() sleeps before its first TAPQ. In theory this is O(N) serialized under the global mdevs_lock; in practice, AP hardware constraints and typical deployment sizes bound the number of queues allocated for guest use in a single LPAR to a modest number, so the total probe cost is not necessarily a real-world concern.

Deferring the reset outside the lock-held section is not
safe: apq_reset_check() accesses q->reset_status and q->aqic_resources without holding any lock, and vfio_ap_mdev_remove_queue() can run concurrently and call kfree(q) after releasing the locks, leaving the worker with a dangling pointer. flush_work() inside the lock-held section serializes against remove_queue and prevents this.

The fundamental remedy would be to replace the global mdevs_lock with a per-mdev lock, scoping the blocking to the single mdev whose queue is being probed and allowing resets across different mdevs to proceed in parallel. That is a non-trivial redesign of the locking architecture and is deferred to a separate effort.

vfio_ap_mdev_set_kvm() is updated to call filter_matrix before updating the guest APCB at open_device time. This ensures that a queue which became operational (or non-operational) after the shadow_apcb was last updated — but before the guest was started — is correctly admitted or excluded when the guest boots.

Fixes dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue reset to complete")
---
 drivers/s390/crypto/vfio_ap_ops.c | 55 ++++++++++++++++++++++++-------
 1 file changed, 44 insertions(+), 11 deletions(-)
drivers/s390/crypto/vfio_ap_ops.c +44 -11
index 627f8ed0a960..a9438b3c5a70 100644
@@ -859,19 +859,38 @@ static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
 			     AP_DOMAINS);
 }
 
+/**
+ * _queue_passable:
+ *
+ * In order to prevent leaking of sensitive data to a guest, a queue must have
+ * been reset before passing it through. If the status stored for the most
+ * recent reset operation is anything other than AP_RESPONSE_NORMAL, then the
+ * queue can not be passed through. On the other hand, the status of the queue
+ * could have changed since the last reset; for example, the queue could have been
+ * deconfigured via the HMC/SE or the sysfs config interface for a crypto card,
+ * or something could have occurred - e.g., a hardware failure - that moved the
+ * queue status to check stop state. To verify the queue is still passable,
+ * the PQAP-TAPQ instruction will be executed to determine the current state
+ * of the queue.
+ *
+ * @q:	the object containing the data associated with a queue bound to the
+ *	vfio_ap device driver
+ *
+ * Returns:	true if the queue can be safely passed through to a guest;
+ *		otherwise,returns false
+ */
 static bool _queue_passable(struct vfio_ap_queue *q)
 {
+	struct ap_queue_status status;
+
 	if (!q)
 		return false;
 
-	switch (q->reset_status.response_code) {
-	case AP_RESPONSE_NORMAL:
-	case AP_RESPONSE_DECONFIGURED:
-	case AP_RESPONSE_CHECKSTOPPED:
-		return true;
-	default:
+	if (q->reset_status.response_code != AP_RESPONSE_NORMAL)
 		return false;
-	}
+
+	status = ap_tapq(q->apqn, NULL);
+	return status.response_code == AP_RESPONSE_NORMAL;
 }
 
 /*
@@ -2020,6 +2039,8 @@ static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
 static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 				struct kvm *kvm)
 {
+	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
+
 	if (kvm->arch.crypto.crycbd) {
 		get_update_locks_for_kvm(kvm);
 		if (kvm->arch.crypto.pqap_hook) {
@@ -2030,6 +2051,15 @@ static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
 
 		kvm_get_kvm(kvm);
 		matrix_mdev->kvm = kvm;
+		/*
+		 * Filter the matrix to ensure the shadow_apcb reflects the
+		 * current operational state of all assigned queues. A queue
+		 * may have become operational (or non-operational) since the
+		 * shadow_apcb was last updated. Any adapters newly filtered
+		 * out are reset to prevent stale state from reaching the guest.
+		 */
+		if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered))
+			reset_queues_for_apids(matrix_mdev, apm_filtered);
 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
 		release_update_locks_for_kvm(kvm);
 	}
@@ -2357,9 +2387,9 @@ static void apq_reset_check(struct work_struct *reset_work)
 				 * the host kernel.
 				 *
 				 * Record the TAPQ status so the queue is marked
-				 * not-passable - _queue_passable() checks
-				 * reset_status.response_code == AP_RESPONSE_NORMAL -
-				 * and the queue is not passed through to a guest.
+				 * not-passable: _queue_passable() first checks
+				 * reset_status.response_code == AP_RESPONSE_NORMAL,
+				 * and the queue will not be passed through to a guest.
 				 */
 				memcpy(&q->reset_status, &status, sizeof(status));
 				return;
@@ -2912,6 +2942,8 @@ int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
 
 	if (matrix_mdev) {
 		vfio_ap_mdev_link_queue(matrix_mdev, q);
+		vfio_ap_mdev_reset_queue(q);
+		flush_work(&q->reset_work);
 
 		/*
 		 * If we're in the process of handling the adding of adapters or
@@ -3001,8 +3033,8 @@ void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
 		vfio_ap_unlink_queue_fr_mdev(q);
 
 	dev_set_drvdata(&apdev->device, NULL);
-	kfree(q);
 	release_update_locks_for_mdev(matrix_mdev);
+	kfree(q);
 }
 
 /**
@@ -3408,3 +3440,4 @@ void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
 
 	mutex_unlock(&matrix_dev->guests_lock);
 }
+
Signature · 1 line
2.53.0