From: "Jason J. Herne" <jjherne@linux.ibm.com>
To: linux-s390@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, akrowiak@linux.ibm.com,
borntraeger@linux.ibm.com, agordeev@linux.ibm.com,
gor@linux.ibm.com, hca@linux.ibm.com, mjrosato@linux.ibm.com,
frankja@linux.ibm.com, imbrenda@linux.ibm.com,
jrossi@linux.ibm.com, stable@vger.kernel.org
Subject: [PATCH 3/5] s390/vfio-ap: Hold a private NIB page reference when a reset is unconfirmed
Date: Thu, 3 Dec 2026 16:45:29 -0500 [thread overview]
Message-ID: <20261203214531.1395796-4-jjherne@linux.ibm.com> (raw)
In-Reply-To: <20261203214531.1395796-1-jjherne@linux.ibm.com>
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")
Cc: stable@vger.kernel.org # 6.6.x
Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
---
drivers/s390/crypto/vfio_ap_ops.c | 45 +++++++++++++++++++++++----
drivers/s390/crypto/vfio_ap_private.h | 2 ++
2 files changed, 41 insertions(+), 6 deletions(-)
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index fba6f256ce77..d460337e365f 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -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);
}
}
diff --git a/drivers/s390/crypto/vfio_ap_private.h b/drivers/s390/crypto/vfio_ap_private.h
index 9bff666b0b35..cd8fc1fba151 100644
--- a/drivers/s390/crypto/vfio_ap_private.h
+++ b/drivers/s390/crypto/vfio_ap_private.h
@@ -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;
--
2.56.1
next prev parent reply other threads:[~2026-12-03 21:45 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 21:45 [PATCH 0/5] s390/vfio-ap: Fixes for queue reset and AQIC handling Jason J. Herne
2026-12-03 21:45 ` [PATCH 1/5] s390/vfio-ap: Prevent passing unreset queues Jason J. Herne
2026-12-03 21:45 ` [PATCH 2/5] s390/vfio-ap: bound the wait for queue reset verification Jason J. Herne
2026-12-03 21:45 ` Jason J. Herne [this message]
2026-12-03 21:45 ` [PATCH 4/5] s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign Jason J. Herne
2026-12-03 21:45 ` [PATCH 5/5] s390/vfio-ap: Fix resource handling for rejected AQIC enable requests Jason J. Herne
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203214531.1395796-4-jjherne@linux.ibm.com \
--to=jjherne@linux.ibm.com \
--cc=agordeev@linux.ibm.com \
--cc=akrowiak@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=frankja@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=imbrenda@linux.ibm.com \
--cc=jrossi@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mjrosato@linux.ibm.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox