From nobody Sat Sep 26 15:35:17 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6637046984E; Mon, 31 Aug 2026 17:14:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196497; cv=none; b=PaqR3QsbehCjbfzTDNDu6W7MXK1wvLskpa5jKSKOBVt1p+lzpTNNo0nb22JsOjsPdgTF/uS1VXAInkbRnXOygT3JMbDVCsotVMZvlBjD9rlpepz01XxNZau00O8WHAoq7ttWCcRq22q5Lq7Wyj9hCGXf+DUtAJ4WXTYi/lDw8Qw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196497; c=relaxed/simple; bh=UtE/4n1qiFPm7xyLR+d5HjV4rEDtuaT0rENZL7pkNZ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=J9jbyCnIdViScjZ3g3+siE12qknUioHg0Cwy+5XQNCgALtGVea+yg1jCkcOf1FZF6cdLzCo7s8yxmZ2xzJOW+xm3smtRcdU8EHC0cHBGso+yP9tOyrI74q5umxlHqAv8j/M0QxsqWcpQg9l7FO8Ax1RwCNxgUDajCpJbBLEGvdA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=jz7+YDZt; arc=none smtp.client-ip=148.163.158.5 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.b="jz7+YDZt" Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67VG1mpP2801042; Mon, 31 Aug 2026 17:14:50 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=7CX8xtKid2KGN08xm hMEtD8qbqmCWjuVQwE06cOUqo0=; b=jz7+YDZtz76bh6Kk+zFpc3mOQGKOXyFFR MPGoGPU4FM4ojbTIlrq1Rb+gNA8MmP33WS0J5ury7kBfoPL8i2IPvtyff+0BB0vP cXwRkfXR2iBSD65//rzTA63viYeaBW7VDlwj8NuS2dIGHo8DTzJwWjReuTqgNRCm 7ML16bEKO3NCf1N/SAzjFT1HAeIAFKXETEIeIVF1WTj2/IgyXf2rbXCkTySi+/fz WXV7ZMi4xaKhmJa8Z/N5FTXyLdObTOhxI/4qKEEl5myqi9xC+K4YFKxcQdjB6dBQ adheyhzLP8rUR2+3gqNn4DIpBZT9tTBAAn3pnYs5REAYMZTg/PJFg== Received: from ppma12.dal12v.mail.ibm.com (dc.9e.1632.ip4.static.sl-reverse.com [50.22.158.220]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4gbq2t2k0f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:49 +0000 (GMT) Received: from pps.filterd (ppma12.dal12v.mail.ibm.com [127.0.0.1]) by ppma12.dal12v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 67VHBG5T016310; Mon, 31 Aug 2026 17:14:48 GMT Received: from smtprelay07.wdc07v.mail.ibm.com ([172.16.1.74]) by ppma12.dal12v.mail.ibm.com (PPS) with ESMTPS id 4gc9rq7a59-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:48 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay07.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 67VHEljE16122554 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Aug 2026 17:14:47 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 494495805C; Mon, 31 Aug 2026 17:14:47 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A08AC5805A; Mon, 31 Aug 2026 17:14:45 +0000 (GMT) Received: from li-4c4c4544-004d-4810-8043-b7c04f423534.ibm.com.com (unknown [9.61.29.117]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 31 Aug 2026 17:14:45 +0000 (GMT) From: Anthony Krowiak To: linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jjherne@linux.ibm.com, borntraeger@de.ibm.com, mjrosato@linux.ibm.com, pasic@linux.ibm.com, alex@shazbot.org, kwankhede@nvidia.com, fiuczy@linux.ibm.com, pbonzini@redhat.com, frankja@linux.ibm.com, imbrenda@linux.ibm.com, agordeev@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, stable@vger.kernel.org Subject: [PATCH v5 1/4] s390/vfio-ap: Fix leak of pinned NIB and registered NISC in vfio_ap_irq_enable/disable() Date: Mon, 31 Aug 2026 13:14:40 -0400 Message-ID: <20260831171443.222225-2-akrowiak@linux.ibm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260831171443.222225-1-akrowiak@linux.ibm.com> References: <20260831171443.222225-1-akrowiak@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Authority-Analysis: v=2.4 cv=bc1bluPB c=1 sm=1 tr=0 ts=6a95b689 cx=c_pps a=bLidbwmWQ0KltjZqbj+ezA==:117 a=bLidbwmWQ0KltjZqbj+ezA==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=RzCfie-kr_QcCd8fBx8p:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=mhE2La7kI1ux2J5OmX4A:9 X-Proofpoint-Spam-Info: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX3Ev0SaT4R9QU /zjCEAbGrCmh66rfaMAM1GUdKlVljbH6dk8I6CBDos23cTMAGZIrDtv+0wcM0/ogEWCPjwRZhLF AVOwR2PNAowS6ixWhkIgCuBaArKH3jQ= X-Proofpoint-ORIG-GUID: 94GSqBMlMg5wCRJtLwntVJhUQeZro7Aa X-Proofpoint-GUID: 94GSqBMlMg5wCRJtLwntVJhUQeZro7Aa X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX8Ajn4vSrmQkl XJaNriEvn4S263Z7wN+wAN2pGrdnUBGFJ0ensw/L+iSEBR44/SUN3AHhOLSTXqrhta+0Lfkg3rW 76khu+hcMnpDeFQWDDTT0Pe6ddfB1F55V3SpGaU84x6O32kL8vyVJpfZonNBDhJWIX5ClkxVYSG PHrfFj13e9Uxd7pQQLiYbBwpb7wSkg/d4PExqE1Co619j6zspOzTDrJXSgCjw0xZ4B8ZQAB5Nco mG8NjbkoHK9glZkhHFh7RdyhmQD/N29sxScKcLtjejVLnAAe10J/GmWg3zvmpGuMwgrVXKu9EF+ saZAxhr9XQEFmoX8/HYCk4C+cWQZYiMG6g4bs0iVBxlV7YYHQrKpHriKaYerVtP3QnabpZcosmQ pRGxdJQEnaJVOL1BN+doQ/NEi6uTrRz6plFMPgUfR8G0i6KIQCLvEVxIaHEkZ5mz//SVWxwMg3c T4OLFSdb390MJUFTMZA== X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-31_05,2026-08-31_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 adultscore=0 malwarescore=0 spamscore=0 lowpriorityscore=0 phishscore=0 clxscore=1015 priorityscore=1501 impostorscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608310147 Content-Type: text/plain; charset="utf-8" The vfio_ap_irq_enable() and vfio_ap_disable() functions execute the PQAP(AQIC) instructions to enable/disable interrupts for an AP queue. A switch statement is used to examine the status response code returned from the instruction to determine whether it succeeded or failed and react accordingly. vfio_ap_irq_enable() ~~~~~~~~~~~~~~~~~~~~ For the default case, the vfio_ap_irq_disable function is invoked to disable interrupts for the queue and clean up the AQIC resources (i.e., unpin the NIB and unregister the NISC) that are stored with the vfio_ap_queue object. There are a number of problems with this: 1. Neither the q->saved_iova nor q->saved_isc has been set for the current AQIC call, so the AQIC resources - assuming those values have been previously set - will be the NIB and NISC resources from a prior call; the NIB and NISC from the current call are therefore leaked. 2. Interrupts may never have been enabled. Sending a disable instruction to a queue that the hardware just told you is in a bad state (CHECKSTOPPED, DECONFIGURED, Q_NOT_AVAIL) is at best wasted work and at worst generates a further WARN_ONCE from inside vfio_ap_irq_disable's own default. 3. The hardware just rejected the new ap_aqic() enable attempt with an unexpected status. Disabling a previously-working IRQ config - assuming that is even possible - as a reaction to a failed enable attempt does not make sense; it is actively destructive, tearing down something that was working for no valid reason. The fix is to unregister the NISC and an unpin the NIB used in the AQIC call in the default case of the switch statement and leave the AQIC resources stored with the vfio_ap_queue object alone. Another problem with vfio_ap_irq_enable() is that the old NIB stored in q->saved_iova from a previous AQIC are immediately freed - assuming they are stored - as soon as the AQIC response status returns AP_RESPONSE_NORMAL. The problem with this is, AQIC is an asynchronous operation; the only way to tell if it has completed is to check the I-bit (7) in the status returned from AQIC. This bit indicates whether interrupts are enabled (1) or disabled (0). The fix for this is to wait a specified period of time until the I-bit is set, similar to vfio_ap_wait_for_irqclear() - waits for I-bit=3D=3D0 - which is called from vfio_ap_irq_disable() to verify the operation has completed. If verification of I-bit =3D=3D 1 occurs within a specified period of time, freeing the old NIB (q->saved_iova) because once the I-bit is set, the new NIB is made available for queue interrupts and any old NIB will no longer be used. If the verification times out, then the new NIB will be freed and the old NIB will be allowed to leak which is preferable because the queue might still have in-flight DMA writes directed to the old NIB which would result in a use-after-free kernel crash. vfio_ap_irq_disable() ~~~~~~~~~~~~~~~~~~~~~ There are two problems with the way this function handles the response code returned from the PQAP(AQIC) instruction: 1. For response codes AP_RESPONSE_NORMAL or AP_RESPONSE_OTHERWISE_CHANGED, a call is made to vfio_ap_wait_for_irqclear() which waits for the I-bit (7) - indicates whether interrupts are enabled (1) or disabled (0) - to be cleared. That function does not return anything, so there is no way to determine whether it succeeded or not. The vfio_ap_irq_disable() function then frees the AQIC resources. This is a problem because the hardware may still write to the NIB resulting in a use-after-free kernel crash. The fix for this is to add a boolean return code from vfio_ap_wait_for_irqclear(). This will be checked in vfio_ap_irq_disable() and if clearing of the IR bit could not be verified, the AQIC resources will be allowed to leak. This is preferable to a kernel crash. 2. For response code AP_RESPONSE_INVALID_ADDRESS - indicates the NIB address passed to PQAP(AQIC) is not valid - as well as the default case, the vfio_ap_irq_disable() frees the AQIC resources. Since the AQIC disable was rejected, the IRQ is still enabled and the hardware still holds the NIB address, so freeing the NIB could result in a use-after-free kernel crash. The fix for this is to allow the AQIC resources to be leaked. This is preferable to a kernel crash. Fixes: ec89b55e3bce7 ("s390: ap: implement PAPQ AQIC interception in kernel= ") Cc: stable@vger.kernel.org Signed-off-by: Anthony Krowiak --- drivers/s390/crypto/vfio_ap_ops.c | 152 +++++++++++++++++++++++++----- 1 file changed, 129 insertions(+), 23 deletions(-) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_a= p_ops.c index 940c0ff668be..8fb0476e3d39 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -30,6 +30,9 @@ #define AP_QUEUE_UNASSIGNED "unassigned" #define AP_QUEUE_IN_USE "in use" =20 +#define AP_IRQ_DISABLED 0 +#define AP_IRQ_ENABLED 1 + #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */ =20 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev); @@ -226,16 +229,27 @@ static struct vfio_ap_queue *vfio_ap_mdev_get_queue( } =20 /** - * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries - * @apqn: The AP Queue number + * vfio_ap_wait_for_irqstate - wait for the IR bit to reach the requested = state * - * 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. + * @apqn: the APQN of the queue + * @ir: the expected state of the IR bit: AP_IRQ_DISABLED or AP_IRQ_ENABLED + * + * Repeatedly polls the AP queue status via PQAP(TAPQ) every 20ms until th= e IR + * bit matches @ir, the queue becomes non-operational, or 5 retries are + * exhausted. + * + * Because PQAP(AQIC) initiates an asynchronous process, a condition-code 0 + * completion does not guarantee the IR bit has reached the requested stat= e. + * The caller must use this function to confirm the state before proceedin= g. + * + * Return: + * - true if the IR bit matches @ir, or the AP is non-operational (in which + * case no further interrupts can be generated) + * + * - false if the IR bit still does not match @ir after all retries are + * exhausted */ -static void vfio_ap_wait_for_irqclear(int apqn) +static bool vfio_ap_wait_for_irqstate(int apqn, int ir) { struct ap_queue_status status; int retry =3D 5; @@ -245,8 +259,8 @@ static void vfio_ap_wait_for_irqclear(int apqn) switch (status.response_code) { case AP_RESPONSE_NORMAL: case AP_RESPONSE_RESET_IN_PROGRESS: - if (!status.irq_enabled) - return; + if (status.irq_enabled =3D=3D ir) + return true; fallthrough; case AP_RESPONSE_BUSY: msleep(20); @@ -257,12 +271,15 @@ static void vfio_ap_wait_for_irqclear(int apqn) default: WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__, status.response_code, apqn); - return; + return true; } } while (--retry); =20 - 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 %s for %= 02x.%04x\n", + __func__, status.response_code, + ir ? "enabled" : "disabled", + AP_QID_CARD(apqn), AP_QID_QUEUE(apqn)); + return false; } =20 /** @@ -317,8 +334,30 @@ static struct ap_queue_status vfio_ap_irq_disable(stru= ct vfio_ap_queue *q) 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. + */ + if (vfio_ap_wait_for_irqstate(q->apqn, AP_IRQ_DISABLED)) + goto end_free; + /* + * Timed out waiting to confirm interrupts are disabled. + * If ap_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. + * Zero the status word and set OTHERWISE_CHANGED to mimic + * what the hardware does for that response code. This + * signals to the guest that the reset operation did not + * complete. + */ + if (status.response_code =3D=3D AP_RESPONSE_NORMAL) { + memset(&status, 0, sizeof(status)); + status.response_code =3D AP_RESPONSE_OTHERWISE_CHANGED; + } + goto end_fail; case AP_RESPONSE_RESET_IN_PROGRESS: case AP_RESPONSE_BUSY: msleep(20); @@ -326,18 +365,46 @@ static struct ap_queue_status vfio_ap_irq_disable(str= uct vfio_ap_queue *q) 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: 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--); =20 WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__, status.response_code); + +end_fail: + /* + * We are here either because of a failure to verify that + * interrupts have been disabled, or because the AQIC instruction + * failed to disable them. The AQIC resources - the pinned NIB page + * and the registered guest ISC - cannot be freed here. The hardware + * may still write to the NIB; freeing the pinned page would result + * in a use-after-free kernel crash. The resources will therefore be + * leaked. This is preferable to a use-after-free. + */ + return status; + end_free: + /* + * This label is reached because the queue was successfully disabled, + * or because the queue is not operational, 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; } @@ -432,6 +499,7 @@ static struct ap_queue_status vfio_ap_irq_enable(struct= vfio_ap_queue *q, struct kvm *kvm; phys_addr_t h_nib; dma_addr_t nib; + char *msg; int ret; =20 /* Verify that the notification indicator byte address is valid */ @@ -489,13 +557,49 @@ static struct ap_queue_status vfio_ap_irq_enable(stru= ct vfio_ap_queue *q, status =3D ap_aqic(q->apqn, aqic_gisa, h_nib); switch (status.response_code) { case AP_RESPONSE_NORMAL: - /* See if we did clear older IRQ configuration */ + /* + * AQIC initiates an asynchronous process; however, AP_RESPONSE_NORMAL + * does not guarantee interrupts are enabled yet (i.e., IR bit (7) + * is set). Wait to confirm before committing the new NIB and + * freeing the old resources. + */ + if (!vfio_ap_wait_for_irqstate(q->apqn, AP_IRQ_ENABLED)) { + /* + * Timed out waiting to verify IRQs are enabled. If the + * hardware is merely stalled, it might eventually complete + * and write interrupt status bytes to the new NIB. + * + * If the NIB page is unpinned and freed here, this delayed + * hardware write would result in a host use-after-free/wild + * DMA write and a host kernel crash. + * + * To prevent this, we must leak the new resources (leave the + * NIB page pinned and Guest ISC registered) and return + * AP_RESPONSE_OTHERWISE_CHANGED to signal the guest to retry. + */ + msg =3D "%s: Timed out waiting to verify IRQs enabled for apqn=3D%#04x\= n"; + VFIO_AP_DBF_WARN(msg, __func__, q->apqn); + memset(&status, 0, sizeof(status)); + status.response_code =3D AP_RESPONSE_OTHERWISE_CHANGED; + break; + } + /* + * Now that IR=3D1 is confirmed (IRQs enabled), the + * new NIB is in use for this queue, so no interrupts can be made + * pending via any previously-registered NIB and the old + * resources can be safely freed. + */ vfio_ap_free_aqic_resources(q); q->saved_iova =3D nib; q->saved_isc =3D isc; break; case AP_RESPONSE_OTHERWISE_CHANGED: - /* We could not modify IRQ settings: clear new configuration */ + /* + * IRQ control is already set as requested or a prior async + * request has not yet completed; in either case, this response + * comes with CC=3D3 indicating the new NIB and ISC were not accepted by + * the hardware, so clean them up. + */ ret =3D kvm_s390_gisc_unregister(kvm, isc); if (ret) VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=3D%d isc=3D%d, apqn= =3D%#04x\n", @@ -503,9 +607,12 @@ static struct ap_queue_status vfio_ap_irq_enable(struc= t vfio_ap_queue *q, 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); + /* We could not modify IRQ settings: clear new configuration */ + ret =3D kvm_s390_gisc_unregister(kvm, isc); + if (ret) + VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=3D%d isc=3D%d, apqn= =3D%#04x\n", + __func__, ret, isc, q->apqn); + vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1); break; } =20 @@ -635,7 +742,6 @@ static int handle_pqap(struct kvm_vcpu *vcpu) } =20 status =3D vcpu->run->s.regs.gprs[1]; - /* If IR bit(16) is set we enable the interrupt */ if ((status >> (63 - 16)) & 0x01) qstatus =3D vfio_ap_irq_enable(q, status & 0x07, vcpu); --=20 2.53.0 From nobody Sat Sep 26 15:35:17 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BC3B94A5C34; Mon, 31 Aug 2026 17:14:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196500; cv=none; b=qPhOmSUrEHShmDr8RIpxZTdF6VoMmdCamJKmGdezTsT+RyF75glrQ76HuX0YbMO8yHQzTd3/LFa31w2Rz8ZcpIkrdz4sCri8qRjIJH3nZdNXUFWgt+XuNeFRx91lcersie58fcMPOgN0WpXzMhCK+4+uFYQLVXAPnkAtLO+cBFw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196500; c=relaxed/simple; bh=CiHUV6znCSVFxtuAf32UAWE35DZ8YBHWUWVeFklCWL0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=PTfwmKd8IB1huBgb/YLmLyHpu746k8VTBgeRnjc+pFW/egr1sKhv/tQphanVZQ5I1UBSTmGmKPSLgbZ+5xuumrw5KuOflcA2NsCrL7AfV/lJqQN5BtfzE1sFqvcDGKbMtwt6sm3Jl42hyCE1oC/x2gSWyZKaqg/MBErTQv4nmVA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=PZ4hCJkA; arc=none smtp.client-ip=148.163.156.1 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.b="PZ4hCJkA" 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 67VG1VA42743470; Mon, 31 Aug 2026 17:14:52 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=x2j/kVZpHF39/YVj4 EMWXSGYZDF1LuuVNeNL23Xg+dc=; b=PZ4hCJkAGSmXws6X1ITJyEnTf/wBnNA0W +u3dZBg3/xpgjcf1tuBLkOki4YnxNS2lduQJDh0zFJqY98VTf2TFjmXRILkWbzu+ D8HRcR78G6nq+q6jHd8DrVxQfHUzkevgxnUW8B+NQiDeiqIUkOL9qaNX2904mW9k GghXHeyzddjBrai9ayiFZxfEjglflMoVbmeyTK3/VjzGRmYz04DhlEEqejmWJMTt 68FYfrAIJ8eGsWkwMUuUQJl8eV8aQIgP+3EB2EyPhyp3ypGuIHRgFxZPSClNoTY/ coP853f6AvHImoRFFKjJaRWvjJN/STcQo9/ylXui85vRLh4WcoDfA== Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4gbq54jsdq-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:51 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 67VHBL14017267; Mon, 31 Aug 2026 17:14:50 GMT Received: from smtprelay03.dal12v.mail.ibm.com ([172.16.1.5]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4gca4vy73q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:50 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay03.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 67VHEn4e19137140 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Aug 2026 17:14:49 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 32AB158054; Mon, 31 Aug 2026 17:14:49 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 725D55805F; Mon, 31 Aug 2026 17:14:47 +0000 (GMT) Received: from li-4c4c4544-004d-4810-8043-b7c04f423534.ibm.com.com (unknown [9.61.29.117]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 31 Aug 2026 17:14:47 +0000 (GMT) From: Anthony Krowiak To: linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jjherne@linux.ibm.com, borntraeger@de.ibm.com, mjrosato@linux.ibm.com, pasic@linux.ibm.com, alex@shazbot.org, kwankhede@nvidia.com, fiuczy@linux.ibm.com, pbonzini@redhat.com, frankja@linux.ibm.com, imbrenda@linux.ibm.com, agordeev@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, stable@vger.kernel.org Subject: [PATCH v5 2/4] s390/vfio-ap: Fix failure to release IRQ notification eventfd contexts Date: Mon, 31 Aug 2026 13:14:41 -0400 Message-ID: <20260831171443.222225-3-akrowiak@linux.ibm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260831171443.222225-1-akrowiak@linux.ibm.com> References: <20260831171443.222225-1-akrowiak@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX0uE6PEA86K// /CdbIqO972ODhYIPsvDUfIqEETvc/9EnncYeHX+bH6n4QQLf/NcQqVuI5NGakQV40W6Dp6CIz34 SYWYucXbM0FVHukgq1J1y1SVxbmOduzDEZ/sxSh0oAljmsGXqxDnxu9mjbvkk9zf5gZte99q0v9 vKDZvAwkopOw/FeERecyguj2PIjQs2IPwvu2tha21VO66oKAFy+W2WX/yecmFYVzqULmpUHW8VI eW+w0Pt8axBRTnr9Aj0//5UuZJLixoRuIyTEdaWf6dMm6+xi/9BOYgWOIRWv09R96L3snPUmN8o QP/SPQxeGWyPY+ICPn+vmXERCO4UXMniXsZhSTouh50vHExAD2jHpa2zACvgkkY7YEDVGzVaaPM VPJ4yZb5y34cxSIZJ7sg5Qf8rXdkLSQzSg+jDf9doypC/70dP7qSFj8Cu0BEs9YiCaOhV67qISr viiG1fsxtcnzSW3Q5tA== X-Proofpoint-ORIG-GUID: HfRZvZuv49WdR9tN1LmmUdLnMMSfk81g X-Authority-Analysis: v=2.4 cv=CNgamxrD c=1 sm=1 tr=0 ts=6a95b68b cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=fxeDLeT5AZ2qCvnLJ4gA:9 X-Proofpoint-GUID: HfRZvZuv49WdR9tN1LmmUdLnMMSfk81g X-Proofpoint-Spam-Info: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX7ZAsMfI4N0y7 r+n9FtwX2MphpdNS+wG6oLFux7rafgju6h/JfcfcTEo0nDJMqAEX1VwgkDv23ISMKrgBIeUcMxF EZ2IKMai0hrXJil2SYHa39Q5qSs+c0E= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-31_05,2026-08-31_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 suspectscore=0 phishscore=0 lowpriorityscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608310147 Content-Type: text/plain; charset="utf-8" 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= ") Cc: stable@vger.kernel.org Signed-off-by: Anthony Krowiak Reviewed-by: Matthew Rosato --- drivers/s390/crypto/vfio_ap_ops.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_a= p_ops.c index 8fb0476e3d39..a4980d993b68 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -2167,12 +2167,28 @@ static int vfio_ap_mdev_open_device(struct vfio_dev= ice *vdev) return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm); } =20 +static void vfio_ap_mdev_release_eventfds(struct ap_matrix_mdev *matrix_md= ev) +{ + if (matrix_mdev->req_trigger) { + eventfd_ctx_put(matrix_mdev->req_trigger); + matrix_mdev->req_trigger =3D NULL; + } + if (matrix_mdev->cfg_chg_trigger) { + eventfd_ctx_put(matrix_mdev->cfg_chg_trigger); + matrix_mdev->cfg_chg_trigger =3D NULL; + } +} + static void vfio_ap_mdev_close_device(struct vfio_device *vdev) { struct ap_matrix_mdev *matrix_mdev =3D container_of(vdev, struct ap_matrix_mdev, vdev); =20 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); } =20 static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int co= unt) --=20 2.53.0 From nobody Sat Sep 26 15:35:17 2026 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EADAC46A5ED; Mon, 31 Aug 2026 17:14:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196501; cv=none; b=A1dI/MFzOLZxagS7v8ZeaQ/sx9CQg/Plj/xVKwsmlLz6/oWHzkcNNTChPktOf33HQsLmHHwbG2tFs/DI68e7TSRalIy+EJrfGYko/kWMxcjanxRD/+D2eoCen6vIY+pLWRREIqSXode48nF3XMvBi5h20gHlMGCQKAFcRm32Oo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196501; c=relaxed/simple; bh=oM08gdJq3lTMby6bywff8KS4I4DaDsNi4i3+BPr2/4Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=kwrFG6Liez3sLjFQqwAoYerBBbU57F6HXNPlpUma9+pipxcN5+uhuUdjyenw1f18ApxcCxMvYddn8Qcxgr+l3GfbShHYB/Lu4PY1FZ+KSvlCJkVZOkhxUBGcToO3yWa205Yo/la/cCqhEgkIBoCi7Y/kqTn6ShsclZSpi7X+AzY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=NqDZGrY5; arc=none smtp.client-ip=148.163.158.5 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.b="NqDZGrY5" Received: from pps.filterd (m0353725.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67VG1nWm1615981; Mon, 31 Aug 2026 17:14:54 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=UEDpNqqw0A5yeqsLX aFVLJvnjHyytvF0RzwLizSkGSs=; b=NqDZGrY5Poxdb3Z25b4nSg23rFIT+/JLT X1u+3fH+gcy66LL7a2XYO6FBIUQIpVjPzoqGGqvzbHrLruSfHJZZ+ZICtA93I1yC jINUqvKEH0ROdXXYv4g7qteEqdkpzH9s0DUNBEngho5zI7RfoB2l3ZKhbzxnAltx LT0pI0nEvzbIWRmGRwQn9aZVnoWnantYsKv2HqU77gbPYmlndNrG8DX0LZTJamGP KrW/BX9k7LklnD0PemFpVR9W/oH7L5R2ORGTMrP86tDT4UTRPSYup61muaso5ozV lqCucXNXe8HS+tbbQ120YU5t0HlnCagwOQCzJqM5Syg54z8T7pf0A== 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 4gbnudjs2e-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:53 +0000 (GMT) Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 67VHBGOs031356; Mon, 31 Aug 2026 17:14:52 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([172.16.1.7]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 4gccexxwqy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:52 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 67VHEpM521561986 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Aug 2026 17:14:51 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 0AD8B58054; Mon, 31 Aug 2026 17:14:51 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5B7405805A; Mon, 31 Aug 2026 17:14:49 +0000 (GMT) Received: from li-4c4c4544-004d-4810-8043-b7c04f423534.ibm.com.com (unknown [9.61.29.117]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 31 Aug 2026 17:14:49 +0000 (GMT) From: Anthony Krowiak To: linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jjherne@linux.ibm.com, borntraeger@de.ibm.com, mjrosato@linux.ibm.com, pasic@linux.ibm.com, alex@shazbot.org, kwankhede@nvidia.com, fiuczy@linux.ibm.com, pbonzini@redhat.com, frankja@linux.ibm.com, imbrenda@linux.ibm.com, agordeev@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com, stable@vger.kernel.org Subject: [PATCH v5 3/4] s390/vfio-ap: Fix unbounded loop in apq_reset_check() Date: Mon, 31 Aug 2026 13:14:42 -0400 Message-ID: <20260831171443.222225-4-akrowiak@linux.ibm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260831171443.222225-1-akrowiak@linux.ibm.com> References: <20260831171443.222225-1-akrowiak@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-GUID: pS08_G_wPH5-GhRuWPTMUWtQIv9C-JwX X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX6kpNW/Au6Hwl 1aT9tdAInJQtkbiwiT6yi1xUzuCNFGF0mbR5EDXjtO6ZaYH97pOE1EHe4S3Sw1EyEe4F8ySScKh kAMTSUdM4oIuwQsODx50GMck3y5SnaGpzwHpOqxny7AJ9mzYEQghELC62pvUPLHJVJm9+xdJ36v 9sB4Hrog/FoZfhWRaq9JsRSTFTjSvmc3S33Wsy8IrDSnPQcQlS/8mx8bLM2Vp1qXNvRFJsOfgwd ylVlKz/hwcYTtBISlAwoe5SM2efEgYz4qOYiIfhx1hY0KID67ihtDj1C98Hew1dYU4dwejVneCV omBXlCrJ16j/r0s6FsL8Cb0L/dpfU4kCyfE8VkYnk489OpQo8XOJZXgr74iBK0oHNP6JgqLVFIX ROKJ6VBn3jwsDRyAgbrFPGmWl/4Xi+3I1Qw0I+X0NjJX7ZmVWSfduEARy47P27maKQtJ/Ff6ton R3TaDnAfHbbBAeEtMHQ== X-Proofpoint-ORIG-GUID: pS08_G_wPH5-GhRuWPTMUWtQIv9C-JwX X-Proofpoint-Spam-Info: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX8LqniC87B3tt PPUO+5nXTO7dk/+tViSOMN0IHdXM1Qdhek2bqO/VBqCtDGWLJsu39ng0qDcRrSArGulojm5Zo34 cOtSgG2gSejpURKQkLVfm9DSOnOGbeY= X-Authority-Analysis: v=2.4 cv=B92JFutM c=1 sm=1 tr=0 ts=6a95b68d cx=c_pps a=aDMHemPKRhS1OARIsFnwRA==:117 a=aDMHemPKRhS1OARIsFnwRA==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=V8glGbnc2Ofi9Qvn3v5h:22 a=VwQbUJbxAAAA:8 a=VnNF1IyMAAAA:8 a=xrMLxwH7pWkX3jLdfMEA:9 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-31_05,2026-08-31_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 spamscore=0 clxscore=1015 suspectscore=0 phishscore=0 lowpriorityscore=0 bulkscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608310147 Content-Type: text/plain; charset="utf-8" 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_MAX_WAIT (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_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 the apq_reset_check() did not verify completion of the reset, the AQIC resources associated with this queue cannot be freed because the NIB is the active DMA target for AP interrupt delivery until the reset completes; freeing the pinned page while the hardware may still write to it would result in a use-after-free kernel crash. If the reset eventually completes, interrupts will be terminated, but the pinned NIB page and ISC registration will be leaked. This is preferable to either a use-after-free kernel crash or waiting indefinitely and blocking access to all mdevs, hanging the guests to which they are attached. There is another bug in this code that is fixed via this patch. A response code AP_RESPONSE_NORMAL (0) does not indicate that the queue was zeroized; it only indicates the PQAP-ZAPQ was accepted. The zeroizing of the queue is done asynchronously. To verify completion, the following bits in the status word returned from PQAP-ZAPQ must be verified: status->irq_enabled =3D=3D 0 status->queue_empty =3D=3D 1 status->replies_waiting =3D=3D 0 status->async =3D=3D 0 Note that on timeout, q->reset_status will hold the status from the most recent reset operation so that callers inspecting q->reset_status.response_code after flush_work() will see the value and can return an appropriate return code. Fixes: dd174833e44e ("s390/vfio-ap: remove upper limit on wait for queue re= set to complete") Cc: stable@vger.kernel.org Signed-off-by: Anthony Krowiak --- drivers/s390/crypto/vfio_ap_ops.c | 97 ++++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 3 deletions(-) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_a= p_ops.c index a4980d993b68..6c315d7a0a08 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -34,6 +34,7 @@ #define AP_IRQ_ENABLED 1 =20 #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */ +#define AP_RESET_MAX_WAIT 2000 /* Maximum wait for reset (2000ms) */ =20 static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev); static int vfio_ap_mdev_reset_qlist(struct list_head *qlist); @@ -2025,12 +2026,31 @@ static int apq_status_check(int apqn, struct ap_que= ue_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 ZAPQ must be verified to indicate that the + * queue has been zeroized. + */ + if (status->queue_empty && !status->replies_waiting && + !status->irq_enabled && !status->async) + return 0; + + /* Still transitioning; keep waiting */ + return -EBUSY; + case AP_RESPONSE_DECONFIGURED: case AP_RESPONSE_CHECKSTOPPED: + /* + * If the queue is non-operational, interrupts are not possible + * and AQIC resources can be safely freed. + */ return 0; + case AP_RESPONSE_RESET_IN_PROGRESS: case AP_RESPONSE_BUSY: return -EBUSY; + case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE: case AP_RESPONSE_ASSOC_FAILED: /* @@ -2041,6 +2061,7 @@ static int apq_status_check(int apqn, struct ap_queue= _status *status) * a value indicating a reset needs to be performed again. */ return -EAGAIN; + default: WARN(true, "failed to verify reset of queue %02x.%04x: TAPQ rc=3D%u\n", @@ -2050,6 +2071,33 @@ static int apq_status_check(int apqn, struct ap_queu= e_status *status) } } =20 +static void report_aqic_resource_leak(struct vfio_ap_queue *q) +{ + if (q->saved_isc !=3D VFIO_AP_ISC_INVALID || q->saved_iova) { + if (q->matrix_mdev) { + dev_warn_ratelimited(mdev_dev(q->matrix_mdev->mdev), + "Reset timed out for APQN %02x.%04x: leaking AQIC resources (NIB= page & GISC) 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 AQIC r= esources (NIB page & GISC) 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)" =20 static void apq_reset_check(struct work_struct *reset_work) @@ -2067,6 +2115,47 @@ static void apq_reset_check(struct work_struct *rese= t_work) ret =3D apq_status_check(q->apqn, &status); if (ret =3D=3D -EIO) return; + if (elapsed >=3D AP_RESET_MAX_WAIT) { + /* + * If the status check determined that the reset completed + * successfully or the queue is not operational, clean up + * the AQIC resources because queue reset disables + * interrupts and interrupts are not possible on a + * non-operational queue. + */ + if (!ret) + goto done; + /* + * Timed out without being able to verify reset completed. + * + * The AQIC resources associated with this queue - the pinned page + * containing the NIB and the registered guest ISC - cannot be freed + * here. The NIB is the active DMA target for AP interrupt delivery + * until the reset completes; freeing the pinned page while the + * hardware may still write to it would result in a use-after-free + * kernel crash. + * + * If the reset eventually completes, interrupts will be terminated + * and the pinned NIB page and ISC registration will be leaked. This + * is preferable to either a use-after-free or waiting indefinitely: + * the caller of apq_reset_check() holds mdevs_lock while flush_work() + * blocks holds the matrix_dev->mdevs_lock mutex, which + * serializes access to all mdev objects system-wide, so blocking + * here would stall all other guests using AP queues. + */ + report_aqic_resource_leak(q); + /* + * Report the actual non-zero hardware response code, or synthesize + * AP_RESPONSE_RESET_IN_PROGRESS if TAPQ completed normally but + * the status bits failed to transition to their post-reset states. + */ + if (status.response_code =3D=3D AP_RESPONSE_NORMAL) + q->reset_status.response_code =3D AP_RESPONSE_RESET_IN_PROGRESS; + else + q->reset_status.response_code =3D status.response_code; + + return; + } if (ret =3D=3D -EBUSY) { pr_notice_ratelimited(WAIT_MSG, elapsed, AP_QID_CARD(q->apqn), @@ -2083,11 +2172,13 @@ static void apq_reset_check(struct work_struct *res= et_work) memcpy(&q->reset_status, &status, sizeof(status)); continue; } - if (q->saved_isc !=3D VFIO_AP_ISC_INVALID) - vfio_ap_free_aqic_resources(q); - break; + goto done; } } + +done: + if (q->saved_isc !=3D VFIO_AP_ISC_INVALID) + vfio_ap_free_aqic_resources(q); } =20 static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q) --=20 2.53.0 From nobody Sat Sep 26 15:35:17 2026 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC3774A5EA4; Mon, 31 Aug 2026 17:15:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196503; cv=none; b=BI37udWSvO2GvXdCBnNz718uuohwDy/g82b0Tlbwrf8Nxm29RLyNPYmzoA43JJvqIbqfsbfdvySWcXfLe/CHfr+a7RXYFfHlRANUJ5eKpA5PXBP5Zn97tYRhMRJxfyr+2teNVMoQI29iNTrH98t5Lj6E2+yTLVNolLGgyiTt4vE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788196503; c=relaxed/simple; bh=NKBvcvXQkm6OPik4Bkk4UOBR8sbHP9sYXzt8QIPxrtw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OWCsP7yGZzUIhs1jbsDColKOzjGJyp/aVrUSJLTCuJ2OqHr32RUHv+lrMY8Qe52FfINLleU/itc4DkjK7/2he9zCYJW/tghpZPt0PAZX+CJyBmTcNLINC0u0aIHjngkpKJzqmYAHsBQUsi4yZFEv5WdHsD2cfYbl37Chbm5Wxac= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.ibm.com; spf=pass smtp.mailfrom=linux.ibm.com; dkim=pass (2048-bit key) header.d=ibm.com header.i=@ibm.com header.b=I1RR+Jxz; arc=none smtp.client-ip=148.163.156.1 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.b="I1RR+Jxz" 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 67VG1YGn2743498; Mon, 31 Aug 2026 17:14:55 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=4Rk+90ci341HS9i8U pd5NPgb04DKhBUD61HAxQjDrsw=; b=I1RR+JxzgWmsPzpSViRcO9Ic7XxyxNtjE rTXiCxdbG4I+wb1iIBzXM/ToOqtUSLftoZhaaxspJCJzc4cQqOXKUxNkjNb1rScC naulMtf7o0Q0IEEUz8ocSGhtELv7mqa5gyMCGa5uv5Fi0kKf5qaThNtERgnIslHn 0KbO6678XLyI7hdGJHwwDeERDSLJ5mhb/DIcDHaHuM7Ph6eCr/nA8Q9oVjbYACJU jMaBRTp+ctjYfF044IPW3Pz6V9an6t9v2RWpOYGs5WcqabrKioj0yyvUCoHu6pZU AahcxrowwrWg42IZA7FF80SIa7OfabkYOEGiEYeAMtC6uYwgEcD2g== Received: from ppma22.wdc07v.mail.ibm.com (5c.69.3da9.ip4.static.sl-reverse.com [169.61.105.92]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 4gbq54jsdx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:55 +0000 (GMT) Received: from pps.filterd (ppma22.wdc07v.mail.ibm.com [127.0.0.1]) by ppma22.wdc07v.mail.ibm.com (8.18.1.7/8.18.1.7) with ESMTP id 67VHBPP0017293; Mon, 31 Aug 2026 17:14:54 GMT Received: from smtprelay06.dal12v.mail.ibm.com ([172.16.1.8]) by ppma22.wdc07v.mail.ibm.com (PPS) with ESMTPS id 4gca4vy73x-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 31 Aug 2026 17:14:54 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay06.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 67VHEqL812714520 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 31 Aug 2026 17:14:53 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id BD12C58054; Mon, 31 Aug 2026 17:14:52 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3436C5805D; Mon, 31 Aug 2026 17:14:51 +0000 (GMT) Received: from li-4c4c4544-004d-4810-8043-b7c04f423534.ibm.com.com (unknown [9.61.29.117]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTP; Mon, 31 Aug 2026 17:14:51 +0000 (GMT) From: Anthony Krowiak To: linux-s390@vger.kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: jjherne@linux.ibm.com, borntraeger@de.ibm.com, mjrosato@linux.ibm.com, pasic@linux.ibm.com, alex@shazbot.org, kwankhede@nvidia.com, fiuczy@linux.ibm.com, pbonzini@redhat.com, frankja@linux.ibm.com, imbrenda@linux.ibm.com, agordeev@linux.ibm.com, hca@linux.ibm.com, gor@linux.ibm.com Subject: [PATCH v5 4/4] s390/vfio-ap: Use AP_DOMAINS for adm_add bitmap size in vfio_ap_mdev_cfg_add() Date: Mon, 31 Aug 2026 13:14:43 -0400 Message-ID: <20260831171443.222225-5-akrowiak@linux.ibm.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260831171443.222225-1-akrowiak@linux.ibm.com> References: <20260831171443.222225-1-akrowiak@linux.ibm.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-TM-AS-GCONF: 00 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfX5iFG8mdGb5XO SLXMshjmV7vjSQSiJf+5qBRF4Kq02qhoQRX19uPBmo9rj/sQVNT5hetEbIyBHJRkSwE2NBWqqbq ccc9RPqoCGL31kIVdcPe/DB3H8ApUpwQvbthbQ94punioaZNiRchE03MWUmQ2HTMMDT+SP4XC+T 0jFM9n+aNQu1P0exor1sE88xzenUO7Yjg8nN28A0AKuPq09HivTOAgAti/+mcfbbwVRoeLg7PKt eQUruXz8YeU3K/oqsv+rqnhA4qkBU3q2+pPKN5ii0DSFJgoriwlvar+24eKxLAHCBgRA2Gc7fRs sBw9BKsqpp+nIC7GBgC0+UUyc5pevot+YgPazgKFzUj4zUtfrS/6BnrQDWkMgA/2VyuP7ovTqHP B3UipgHgaLF0goy55/46ahhPKHQFq5Yu5U6aT+7ymBWHaDQt4NIiT/Lm2+T1qAanAUU628HpYM6 D3BpWKG0LQ3bvN1lpDg== X-Proofpoint-ORIG-GUID: kjA1Cr7d59fGaTd10XswaE4vmV7J8WbC X-Authority-Analysis: v=2.4 cv=CNgamxrD c=1 sm=1 tr=0 ts=6a95b68f cx=c_pps a=5BHTudwdYE3Te8bg5FgnPg==:117 a=5BHTudwdYE3Te8bg5FgnPg==:17 a=Sv0fKeRqtYgA:10 a=VkNPw1HP01LnGYTKEx00:22 a=RnoormkPH1_aCDwRdu11:22 a=U7nrCbtTmkRpXpFmAIza:22 a=VnNF1IyMAAAA:8 a=gReD5cTbzgwmqpVxveEA:9 X-Proofpoint-GUID: kjA1Cr7d59fGaTd10XswaE4vmV7J8WbC X-Proofpoint-Spam-Info: AW1haW4tMjYwODMxMDE0NyBTYWx0ZWRfXyrFdoZQEwNtx fsCL6Mw0TYGPW4C8lw2n3/j34OHoI4RN856/gy3kgZiybjwxZMTAiitkL8dTE8qHf7qiDGtenu1 cPZnHctr5V6f7avBlr8J/Kr1sYb5qZk= X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-31_05,2026-08-31_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 bulkscore=0 suspectscore=0 phishscore=0 lowpriorityscore=0 priorityscore=1501 clxscore=1015 impostorscore=0 adultscore=0 malwarescore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608310147 Content-Type: text/plain; charset="utf-8" Domain and control domain bitmaps are sized by the AP_DOMAINS constant, not AP_DEVICES. The two constants are both 256 today so there is no functional impact, but using the wrong constant is inconsistent with every operation on aqm/adm bitmaps. Use AP_DOMAINS to keep the code consistent and correct in case the two constants ever diverge. Note: This patch was submitted in response to a sashiko review comment pointing out there are other functions besides vfio_ap_mdev_cfg_add(), so there are fixes included here for those also. The subject line was kept the same since this is in v2 of this patch. Signed-off-by: Anthony Krowiak Reviewed-by: Matthew Rosato --- drivers/s390/crypto/vfio_ap_ops.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_a= p_ops.c index 6c315d7a0a08..70c179e9cb19 100644 --- a/drivers/s390/crypto/vfio_ap_ops.c +++ b/drivers/s390/crypto/vfio_ap_ops.c @@ -1517,7 +1517,7 @@ static void vfio_ap_mdev_hot_unplug_domain(struct ap_= matrix_mdev *matrix_mdev, { DECLARE_BITMAP(apqis, AP_DOMAINS); =20 - bitmap_zero(apqis, AP_DEVICES); + bitmap_zero(apqis, AP_DOMAINS); set_bit_inv(apqi, apqis); vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis); } @@ -2910,11 +2910,11 @@ static void vfio_ap_mdev_on_cfg_remove(struct ap_co= nfig_info *cur_config_info, do_remove |=3D bitmap_andnot(aqrem, (unsigned long *)prev_config_info->aqm, (unsigned long *)cur_config_info->aqm, - AP_DEVICES); + AP_DOMAINS); do_remove |=3D bitmap_andnot(cdrem, (unsigned long *)prev_config_info->adm, (unsigned long *)cur_config_info->adm, - AP_DEVICES); + AP_DOMAINS); =20 if (do_remove) vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem); @@ -3025,7 +3025,7 @@ static void vfio_ap_mdev_cfg_add(unsigned long *apm_a= dd, unsigned long *aqm_add, bitmap_and(matrix_mdev->aqm_add, matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS); bitmap_and(matrix_mdev->adm_add, - matrix_mdev->matrix.adm, adm_add, AP_DEVICES); + matrix_mdev->matrix.adm, adm_add, AP_DOMAINS); =20 mutex_unlock(&matrix_dev->mdevs_lock); } --=20 2.53.0