Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796334338; b=JOUzLcI2XnGZjxPDKP88yAX32UH6E/yrnnXj/MS790FOiy0yK+IZzj8g1pPvP1Q++z3Z UXyv665axCfum3idGa18yp/e76JbCv4Ic7O3jCObQxXq0/TwZ8f3+edibZLub1mIwpX+g h4SLJmApTUfVGWWaY3/H6z04Uz7kV8Oox6xWv0h4sU3edb18+PoFvrxL9mbj+TaoF0Lke VtmIECVF+uyvJUm+xTW4Lxq2jSd509Kbc3FC3MswkfN7PXvJPxxZK0tez/pWEriA9O2FO z/3nMArW852Mxoo/8EeG9jKqksY0YnYOcdJG8X+j84p05tpe3dAMt0mklDKiZ7+dS7A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796334338; h=DMARC-Filter:Received:DKIM-Signature:Received:Received:Received: Received:Received:Received:Received:From:To:Cc:Subject:Date: Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-TM-AS-GCONF:X-Proofpoint-Spam-Details-Enc: X-Proofpoint-GUID:X-Proofpoint-Spam-Info:X-Proofpoint-ORIG-GUID: X-Authority-Analysis:X-Proofpoint-Virus-Version: X-Proofpoint-Spam-Details; bh=zKNaAFtoalv8n7eO/jMZwH2gjaMxIt4AFbM/qaT0MDM=; b=NapRg2o+a1qvS5xg1kMLhaCqw4hZ+6XrNEntulVPD+OzjJkExmQ5MrYN+ps8wrQFdeA5 vefySnu1jg8dJfMMk78cl2sYuieWcjyoB8qwq7eYeIMTqhE/qxhDHslmp0qtnhS/++dsj AwrK+UudXjq5jWB1eYso0jpx9CEUearhEmZnpjP2gXBwcyY0bfaURGJvao9kMx+IsLc3L snTvghjrXkg/MyWWbjsegg+vQL7MRbRqpqEFcyGHZrNcvnpE7TsHiW+/1QBu56by5MSH5 NIH9XYgx4qmtipWp/hadj07aIGGYPtDyd8w3FlDnKIPcjXdNzfDYuxGCVIqzKp5Wh9w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=KsyplMBD; arc=none smtp.remote-ip=148.163.156.1 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60EC3606A682; Thu, 3 Dec 2026 21:45:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 60EC3606A682 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.ibm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.a=rsa-sha256 header.s=pp1 header.b=KsyplMBD Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B3LZHMU342184; Thu, 3 Dec 2026 21:45:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=pp1; bh=zKNaAFtoalv8n7eO/ jMZwH2gjaMxIt4AFbM/qaT0MDM=; b=KsyplMBDUJIkOBqdmb4mqXkgiK9senxcE QQy/627v7ZkPh47Mv/PsDsVqepXTQVAdaka6T9VGQo3+ell8g36eAaaVq91YhqrL rB2tKiK2cwO7jckciDhOB6UbOKm7FFINvXICoAOuIcyg+njM67bD5HR9Bj+Cc1sD U5OBfcfK89+9JeaSprwTpa0/ZisTxkp7l0gjuWnYs7urq+sHduOqwXoNq7pHSw31 gO/jvLZSMx5GS0hzwKTIYCqhOrmJbb4jhvHhBZ4wofdBQgpOfSIsa1iazIxfqMuM WwfWJFpfQPqty8rAYCbsTgPyw9ZFYL1gCmHBqrzOxwuHwvDgcj1QA== Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4jbgfd00uj-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Thu, 03 Dec 2026 21:45:37 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B3IKj53944741; Thu, 3 Dec 2026 21:45:36 GMT Received: from smtprelay05.wdc07v.mail.ibm.com ([172.16.1.72]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4jb7h9a557-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 03 Dec 2026 21:45:36 +0000 (GMT) Received: from smtpav05.dal12v.mail.ibm.com (smtpav05.dal12v.mail.ibm.com [10.241.53.104]) by smtprelay05.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 6B3LjZKk23921158 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 3 Dec 2026 21:45:35 GMT Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id F0AD858052; Thu, 3 Dec 2026 21:45:34 +0000 (GMT) Received: from smtpav05.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2A50A58056; Thu, 3 Dec 2026 21:45:34 +0000 (GMT) Received: from li-2227244c-2a68-11b2-a85c-fc811bb24bca.ibm.com (unknown [9.61.13.210]) by smtpav05.dal12v.mail.ibm.com (Postfix) with ESMTP; Thu, 3 Dec 2026 21:45:34 +0000 (GMT) From: "Jason J. Herne" 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 Message-ID: <20261203214531.1395796-4-jjherne@linux.ibm.com> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261203214531.1395796-1-jjherne@linux.ibm.com> References: <20261203214531.1395796-1-jjherne@linux.ibm.com> X-Mailing-List: linux-s390@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjAzMDA4NiBTYWx0ZWRfX1Rmx/HWHJ16T vF7YDr7Tx6tg/+M4khDZ/3EMokdh3pcJ+pAozMpipiXuNs/PsdVuC0Dvbvn2jGbi4FSS5lH7XKx o3lM7ELZ+iCLsiU1v2mS287s1p9UtFskb6HSlhn9Rqu3hgtSHNL8eAyZQ/3mYznglwmrblfom8/ rP1Tz063H0h3cwi59SZjMUxD5iGY56DESez+TYJNvR3HX51OMNcMV9cS6jBkpIoUaysBCKnwNJc rLnEdHjkOjExIdRBXnqCoCuxJXPJzYxOjLrfYgImxfJ2eMSDEzmBiCogeDPuYqpfmX7BEa5AojC Cingh7izKtA6/pYjA7Hjt48WhK4w0E3kE5KNxgf3frA/q3/0VLnw8py8Nc6zeGUM7yTO6S+XZFN nmHZCfZqSxu8ONXoVJNRWEkQuQFDDW+uPoa5aduFCywhzNldMBkfPzrSbg0EniwuSfbeO3cNOqt qGzZCRWNLC/H6BXaxCA== X-Proofpoint-GUID: 6li4ZuhufHsZa4bpxsl5jF57YDmC-cIV X-Proofpoint-Spam-Info: AW1haW4tMjYxMjAzMDA4NiBTYWx0ZWRfXzNT9ubdClPwI CnHlIEBKkcBUEmQ/YLdaHQvyxMJFrJln8z0OZ9LxHyYlKuY3SIg2DWBNw0YtCbyubi9M8D0CnlL nJakFmRLoDCaB1x7ofLai8myjXc+lzU= X-Proofpoint-ORIG-GUID: 6li4ZuhufHsZa4bpxsl5jF57YDmC-cIV X-Authority-Analysis: v=2.4 cv=I9zw19gg c=1 sm=1 tr=0 ts=6b11e301 cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=dzqxYXk-TiCgdiES:21 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=FtoKnDa-xLH6STuaVtYA:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-03_05,2026-12-03_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 spamscore=0 clxscore=1015 malwarescore=0 lowpriorityscore=0 priorityscore=1501 adultscore=0 bulkscore=0 impostorscore=0 phishscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2612010000 definitions=main-2612030086 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 --- 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