Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Anthony Krowiak" <akrowiak@linux.ibm.com>
Cc: kvm@vger.kernel.org, "Vasily Gorbik" <gor@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.vom>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Heiko Carstens" <hca@linux.ibm.com>,
	linux-s390@vger.kernel.org
Subject: Re: [PATCH v4 1/9] s390/vfio-ap: Fix stale do_remove flag across iterations in vfio_ap_mdev_cfg_remove
Date: Wed, 12 Aug 2026 17:22:34 +0000	[thread overview]
Message-ID: <20260812172235.07A501F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260812170945.738351-2-akrowiak@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

Pre-existing issues:
- [High] Unchecked dereference of `matrix_mdev->kvm` in `vfio_ap_mdev_cfg_remove` and `vfio_ap_mdev_hot_plug_cfg` leading to a kernel panic for mediated devices not attached to a KVM guest.
- [High] Control domain hot unplug in `vfio_ap_mdev_cfg_remove()` is completely broken due to incorrect use of `bitmap_andnot` instead of `bitmap_and`.
- [High] Unsafe iteration over `matrix_dev->mdev_list` in `vfio_ap_mdev_cfg_remove` without holding `matrix_dev->mdevs_lock`, risking a data race.
- [High] Unsupported older AP adapters (pre-CEX4) can bypass filtering in `vfio_ap_mdev_cfg_add` if no new domains are added simultaneously.
--

--- Patch [1]: [PATCH v4 1/9] s390/vfio-ap: Fix stale do_remove flag across iterations in vfio_ap_mdev_cfg_remove ---
commit 842cc8fca6dcd2512ea1bac051db5a235987ced0
Author: Anthony Krowiak <akrowiak@linux.ibm.com>

s390/vfio-ap: Fix stale do_remove flag across iterations in vfio_ap_mdev_cfg_remove

This commit fixes the initialization and accumulation of the do_remove flag
in vfio_ap_mdev_cfg_remove(). Previously, the flag was never reset per
iteration, causing a positive result to carry over to subsequent matrix mdevs.
The fix sets the flag using bitmap_and() for the first check rather than
accumulating it.

> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
> index 44b3a1dcc1b36..845c86ba8bc30 100644
> --- a/drivers/s390/crypto/vfio_ap_ops.c
> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> @@ -2603,15 +2603,15 @@ static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
>  	DECLARE_BITMAP(aprem, AP_DEVICES);
>  	DECLARE_BITMAP(aqrem, AP_DOMAINS);
>  	DECLARE_BITMAP(cdrem, AP_DOMAINS);
> -	int do_remove = 0;
> +	int do_remove;
>  
>  	list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {

[Severity: High]
This isn't a bug introduced by this patch, but is it safe to iterate over
matrix_dev->mdev_list here without holding matrix_dev->mdevs_lock?

The caller vfio_ap_on_cfg_changed() holds matrix_dev->guests_lock, but not
matrix_dev->mdevs_lock. Since mdevs_lock is acquired inside the loop and
released before the next iteration, concurrent modifications might corrupt
the list traversal if new mdevs are added in vfio_ap_mdev_probe() holding
only mdevs_lock.

>  		mutex_lock(&matrix_mdev->kvm->lock);

[Severity: High]
This is a pre-existing issue, but does this code unconditionally dereference
matrix_mdev->kvm without checking for null?

If a mediated device is created but not yet attached to a kvm guest,
matrix_mdev->kvm remains initialized to null. If a host ap configuration
change occurs in this state, wouldn't this cause a kernel panic?

The same unconditional dereference appears to exist in
vfio_ap_mdev_hot_plug_cfg() when called from vfio_ap_on_cfg_changed().

>  		mutex_lock(&matrix_dev->mdevs_lock);
>  
> -		do_remove |= bitmap_and(aprem, ap_remove,
> -					  matrix_mdev->matrix.apm,
> -					  AP_DEVICES);
> +		do_remove = bitmap_and(aprem, ap_remove,
> +				       matrix_mdev->matrix.apm,
> +				       AP_DEVICES);
>  		do_remove |= bitmap_and(aqrem, aq_remove,
>  					  matrix_mdev->matrix.aqm,
>  					  AP_DOMAINS);

[Severity: High]
This isn't a bug introduced by this patch, but looking at the next lines in
vfio_ap_mdev_cfg_remove():

drivers/s390/crypto/vfio_ap_ops.c:vfio_ap_mdev_cfg_remove() {
    ...
    do_remove |= bitmap_andnot(cdrem, cd_remove,
                               matrix_mdev->matrix.adm,
                               AP_DOMAINS);
    ...
}

Is it correct to use bitmap_andnot() here instead of bitmap_and()?

This computes cd_remove & ~matrix_mdev->matrix.adm, yielding domains that
were removed from the host but are explicitly not assigned to the mdev.
Later, vfio_ap_mdev_hot_unplug_cfg() checks for intersection between the
guest's active domains and cdrem, which will always fail. Should this be
bitmap_and() instead?

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for unsupported
older ap adapters to bypass filtering?

In vfio_ap_mdev_cfg_add(), the code calls:

drivers/s390/crypto/vfio_ap_ops.c:vfio_ap_mdev_cfg_add() {
    ...
    vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
    ...
}

If an older adapter is added to the host configuration without any
new domains being added simultaneously (because the domains were already
present), aqm_add will be empty.

This causes the inner loop in vfio_ap_filter_apid_by_qtype() to be skipped:

drivers/s390/crypto/vfio_ap_ops.c:vfio_ap_filter_apid_by_qtype() {
    ...
    for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
        status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
    ...
}

Because aqm is empty, the loop evaluating adapter type does not execute,
meaning the adapter's type is never verified and it remains in apm_add.
Should the active domains be passed to the filter instead of the delta?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260812170945.738351-1-akrowiak@linux.ibm.com?part=1

  reply	other threads:[~2026-08-12 17:22 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-12 17:09 [PATCH v4 0/9] s390/vfio-ap: Fix bugs in vfio_ap device driver callback functions Anthony Krowiak
2026-08-12 17:09 ` [PATCH v4 1/9] s390/vfio-ap: Fix stale do_remove flag across iterations in vfio_ap_mdev_cfg_remove Anthony Krowiak
2026-08-12 17:22   ` sashiko-bot [this message]
2026-08-12 17:09 ` [PATCH v4 2/9] s390/vfio-ap: Fix dereference matrix_mdev->kvm without checking for NULL Anthony Krowiak
2026-08-12 17:28   ` sashiko-bot
2026-08-12 17:09 ` [PATCH v4 3/9] s390/vfio-ap: Fix missing lock required to access list of ap_matrix_mdev objects Anthony Krowiak
2026-08-12 17:28   ` sashiko-bot
2026-08-12 18:25     ` Anthony Krowiak
2026-08-12 17:09 ` [PATCH v4 4/9] s390/vfio-ap: Fix required lock not held during update of ap_matrix_mdev object Anthony Krowiak
2026-08-12 17:24   ` sashiko-bot
2026-08-12 17:09 ` [PATCH v4 5/9] s390/vfio-ap: Fix control domain removal in vfio_ap_mdev_cfg_remove Anthony Krowiak
2026-08-12 17:19   ` sashiko-bot
2026-08-12 17:09 ` [PATCH v4 6/9] s390/vfio-ap: fix potential use of uninitialized apm_filtered bitmap Anthony Krowiak
2026-08-12 17:17   ` sashiko-bot
2026-08-12 17:09 ` [PATCH v4 7/9] s390/vfio-ap: Fix hot-unplug skipped when last AP adapter or domain removed Anthony Krowiak
2026-08-12 17:24   ` sashiko-bot
2026-08-12 17:09 ` [PATCH v4 8/9] s390/vfio-ap: Fix NULL deref in status_show() during queue probe Anthony Krowiak
2026-08-12 17:27   ` sashiko-bot
2026-08-12 17:58   ` Matthew Rosato
2026-08-12 18:16     ` Anthony Krowiak
2026-08-12 18:56       ` Matthew Rosato
2026-08-12 17:09 ` [PATCH v4 9/9] s390/vfio-ap: Fix memory leak when queue removed from host AP config Anthony Krowiak
2026-08-12 17:38   ` 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=20260812172235.07A501F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=akrowiak@linux.ibm.com \
    --cc=borntraeger@linux.ibm.vom \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --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