From: Anthony Krowiak <akrowiak@linux.ibm.com>
To: sashiko-reviews@lists.linux.dev
Cc: Vasily Gorbik <gor@linux.ibm.com>,
kvm@vger.kernel.org, Heiko Carstens <hca@linux.ibm.com>,
Christian Borntraeger <borntraeger@linux.ibm.com>,
linux-s390@vger.kernel.org,
Alexander Gordeev <agordeev@linux.ibm.com>,
Jason Herne <jjherne@linux.ibm.com>,
Matthew Rosato <mjrosato@linux.ibm.com>,
Halil Pasic <pasic@linux.ibm.com>
Subject: Re: [PATCH v3 3/4] s390/vfio-ap: Fix unbounded loop in apq_reset_check()
Date: Thu, 27 Aug 2026 16:04:45 -0400 [thread overview]
Message-ID: <b8449e09-945c-4e43-bf6c-abb6d87c63b4@linux.ibm.com> (raw)
In-Reply-To: <20260827134318.D34871F000E9@smtp.kernel.org>
On 8/27/26 9:43 AM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
> - [High] The timeout condition is evaluated before checking for a successful reset, causing the driver to falsely report success while skipping required resource cleanup if success occurs exactly at the timeout boundary.
> - [High] The timeout path fails to clear `q->saved_isc`, leading to Cross-VM KVM interrupt routing corruption when the queue is reassigned to a new guest.
> --
>
> commit bdd9b4236348c839f4b1d25d5fe39d6e047b2068
> Author: Anthony Krowiak <akrowiak@linux.ibm.com>
>
> s390/vfio-ap: Fix unbounded loop in apq_reset_check()
>
> This commit introduces a 2000ms maximum wait time for AP queue resets in
> apq_reset_check() to prevent indefinite spinning. If the reset times out,
> the function breaks out of the loop and leaves the AQIC resources pinned to
> avoid use-after-free issues.
>
>> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
>> index 4c32fd6eaaa63..a9a33f4949a0e 100644
>> --- a/drivers/s390/crypto/vfio_ap_ops.c
>> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> [ ... ]
>> @@ -2016,8 +2017,32 @@ static void apq_reset_check(struct work_struct *reset_work)
>> elapsed += AP_RESET_INTERVAL;
>> status = ap_tapq(q->apqn, NULL);
>> ret = apq_status_check(q->apqn, &status);
>> - if (ret == -EIO)
>> + if (ret == -EIO) {
>> + memcpy(&q->reset_status, &status, sizeof(status));
>> return;
>> + }
>> + if (elapsed >= AP_RESET_MAX_WAIT) {
>> + /*
>> + * Timed out waiting for reset to complete.
>> + *
>> + * 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.
>> + */
>> + memcpy(&q->reset_status, &status, sizeof(status));
>> + return;
> [Severity: High]
> Does this code falsely report success while skipping required resource cleanup
> if the reset succeeds exactly at the timeout boundary?
>
> Because this timeout condition is evaluated before checking if the response is
> successful, if ap_tapq() returns AP_RESPONSE_NORMAL on the exact iteration
> where elapsed reaches AP_RESET_MAX_WAIT (2000ms), it is treated as a timeout.
>
> The loop exits without calling vfio_ap_free_aqic_resources(), but copies the
> success status into q->reset_status, meaning the caller thinks the reset
> fully succeeded.
Ironically I was just reviewing this code and drew that same conclusion.
We need to check the return code from the call to apq_reset_check()
and if it 0, bail out of the wait loop.
Regarding copying the status into q->reset_status, that should only be
done prior to entering the loop and on ret == -EAGAIN after the
AQIC instruction is executed.
>
> [Severity: High]
> Does this timeout path fail to clear q->saved_isc, leading to cross-vm kvm
> interrupt routing corruption when the queue is reassigned to a new guest?
>
> When apq_reset_check() times out, it skips vfio_ap_free_aqic_resources() to
> leak the memory, but leaves q->saved_isc populated since the vfio_ap_queue
> struct persists across mdev assignments.
>
> When a new guest later enables AP interrupts, vfio_ap_free_aqic_resources()
> uses the stale q->saved_isc to call:
>
> kvm_s390_gisc_unregister(new_kvm, q->saved_isc);
>
> This unregisters the stale ISC against the new guest's KVM object,
> decrementing the new guest's ISC reference count and wrongly disabling alerts
> for that ISC.
If the return code from apq_status_check() returns 0, the
AQIC resources need to be freed.
>
>> + }
>> if (ret == -EBUSY) {
next prev parent reply other threads:[~2026-08-27 20:04 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-27 13:24 [PATCH v3 0/4] s390/vfio-ap: Fix pre-existing bugs in vfio_ap device driver Anthony Krowiak
2026-08-27 13:24 ` [PATCH v3 1/4] s390/vfio-ap: Fix leak of pinned NIB and registered NISC in vfio_ap_irq_enable/disable() Anthony Krowiak
2026-08-27 13:39 ` sashiko-bot
2026-08-27 13:24 ` [PATCH v3 2/4] s390/vfio-ap: Fix failure to release IRQ notification eventfd contexts Anthony Krowiak
2026-08-27 13:32 ` sashiko-bot
2026-08-27 13:24 ` [PATCH v3 3/4] s390/vfio-ap: Fix unbounded loop in apq_reset_check() Anthony Krowiak
2026-08-27 13:43 ` sashiko-bot
2026-08-27 20:04 ` Anthony Krowiak [this message]
2026-08-27 13:24 ` [PATCH v3 4/4] s390/vfio-ap: Use AP_DOMAINS for adm_add bitmap size in vfio_ap_mdev_cfg_add() Anthony Krowiak
2026-08-27 13:30 ` sashiko-bot
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=b8449e09-945c-4e43-bf6c-abb6d87c63b4@linux.ibm.com \
--to=akrowiak@linux.ibm.com \
--cc=agordeev@linux.ibm.com \
--cc=borntraeger@linux.ibm.com \
--cc=gor@linux.ibm.com \
--cc=hca@linux.ibm.com \
--cc=jjherne@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=linux-s390@vger.kernel.org \
--cc=mjrosato@linux.ibm.com \
--cc=pasic@linux.ibm.com \
--cc=sashiko-reviews@lists.linux.dev \
/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