Re: [PATCH 2/5] s390/vfio-ap: bound the wait for queue reset verification
From: Anthony Krowiak
Date: Fri Dec 04 2026 - 13:12:54 EST
On 12/3/26 4:45 PM, Jason J. Herne wrote:
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")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Jason J. Herne <jjherne@xxxxxxxxxxxxx>
---
drivers/s390/crypto/vfio_ap_ops.c | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 10430b9446db..fba6f256ce77 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -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));
I don't think this is necessary. In the initial call to the
function as well as all succeeding calls, the status is obtained
from the TAPQ call below.
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));
Since this block will be entered only after a call to TAPQ followed by a
call to apq_status_check(), we need to check the return code (ret)
from apq_status_check() to see if it returned 0 which means the ZAPQ
succeeded. If that is the case, then the AQIC resources can be freed and
the status response code can be set to or left as 0.
+ memcpy(&q->reset_status, &status, sizeof(status));
!q->reset_status.response_code indicates the ZAPQ completed
successfully. Of course, in patch 1/5, no verification was
done to determine whether the ZAPQ completed; a response code of
0 only indicates that the reset was initiated. It's an asychronous
instruction, so the following needs to be verified before we know
that ZAPQ completed:
if (status->queue_empty && !status->replies_waiting &&
!status->irq_enabled && !status->async)
return 0;
Note also that you would have to check if the return code from
TAPQ is -ENODEV in which case you can free AQIC resources.
By putting this timeout check before the TAPQ and
apq_status_check() calls, you are going to have to duplicate
some of the succeeding checks in side this block.
+ 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);