Linux s390 Architecture development
 help / color / mirror / Atom feed
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 4/5] s390/vfio-ap: clear matrix_mdev link for queues not reset on unassign
Date: Thu,  3 Dec 2026 16:45:30 -0500	[thread overview]
Message-ID: <20261203214531.1395796-5-jjherne@linux.ibm.com> (raw)
In-Reply-To: <20261203214531.1395796-1-jjherne@linux.ibm.com>

When an adapter or domain is unassigned from a matrix mdev,
vfio_ap_mdev_unlink_adapter() and vfio_ap_mdev_unlink_domain() remove
each affected queue from the mdev's hash table. Only queues that are in
the guest's APCB are added to the reset list. A queue that was already
filtered from the APCB is unlinked but never added to that list, so
vfio_ap_unlink_mdev_fr_queue() is never called for it. Its
q->matrix_mdev keeps pointing at the mdev.

The mdev's matrix.apm/aqm no longer include the adapter or domain at
this point, so vfio_ap_mdev_unlink_fr_queues() called from
vfio_ap_mdev_remove() will not visit the queue either. When the mdev is
freed, q->matrix_mdev is left dangling. Later paths that dereference it,
such as vfio_ap_mdev_remove_queue(), can then access freed memory.

Clear the link for these queues in an else branch. Queues that are added
to the reset list keep their link until the reset completes, as before.

Clearing the link has a side effect. vfio_ap_free_aqic_resources()
needs q->matrix_mdev to unpin the NIB page and unregister the ISC, and
vfio_ap_mdev_remove_queue() calls it for queues that are no longer in
the host's AP configuration. Such a queue can still hold AQIC resources.
This happens when the adapter or domain is removed from the host
configuration while a guest is using AP interrupts. That path only
clears the shadow APCB bits and does not reset the queue. If the
adapter or domain is then unassigned before the AP bus removes the
queue device, the link is cleared first. The later cleanup hits
WARN_ON(!q->matrix_mdev) and leaks the pinned page and the ISC
registration.

A queue outside the host configuration is deconfigured, so it cannot
write to the NIB. Release its AQIC resources before clearing the link.
Queues still in the host configuration that were filtered from the APCB
have already been reset, or had their resources released or leaked by
the reset, so they have nothing left to free here.

Also drop the "qlist" NULL check. Both helpers have a single caller, and
that caller always passes a freshly initialized list.

Fixes: 70aeefe574cb ("s390/vfio-ap: reset queues after adapter/domain unassignment")
Cc: stable@vger.kernel.org
Signed-off-by: Jason J. Herne <jjherne@linux.ibm.com>
---
 drivers/s390/crypto/vfio_ap_ops.c | 40 +++++++++++++++++++++++--------
 1 file changed, 30 insertions(+), 10 deletions(-)

diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index d460337e365f..836551e9f65b 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -1173,6 +1173,22 @@ static struct vfio_ap_queue
 	return q;
 }
 
+/*
+ * Unlink a queue that is not being reset. If it is no longer in the host's AP
+ * configuration it is gone, but vfio_ap_mdev_remove_queue() can't release its
+ * AQIC resources once the mdev link is cleared, so release them here.
+ */
+static void vfio_ap_unlink_mdev_fr_unreset_queue(struct vfio_ap_queue *q)
+{
+	if (!test_bit_inv(AP_QID_CARD(q->apqn),
+			  (unsigned long *)matrix_dev->info.apm) ||
+	    !test_bit_inv(AP_QID_QUEUE(q->apqn),
+			  (unsigned long *)matrix_dev->info.aqm))
+		vfio_ap_free_aqic_resources(q);
+
+	vfio_ap_unlink_mdev_fr_queue(q);
+}
+
 /**
  * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
  *				 adapter from the matrix mdev to which the
@@ -1191,12 +1207,14 @@ static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
 
 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
+		if (!q)
+			continue;
 
-		if (q && qlist) {
-			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
-			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
-				list_add_tail(&q->reset_qnode, qlist);
-		}
+		if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
+		    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
+			list_add_tail(&q->reset_qnode, qlist);
+		else
+			vfio_ap_unlink_mdev_fr_unreset_queue(q);
 	}
 }
 
@@ -1392,12 +1410,14 @@ static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
 
 	for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
 		q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
+		if (!q)
+			continue;
 
-		if (q && qlist) {
-			if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
-			    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
-				list_add_tail(&q->reset_qnode, qlist);
-		}
+		if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
+		    test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
+			list_add_tail(&q->reset_qnode, qlist);
+		else
+			vfio_ap_unlink_mdev_fr_unreset_queue(q);
 	}
 }
 
-- 
2.56.1


  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 ` [PATCH 3/5] s390/vfio-ap: Hold a private NIB page reference when a reset is unconfirmed Jason J. Herne
2026-12-03 21:45 ` Jason J. Herne [this message]
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-5-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