Linux s390 Architecture development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Omar Elghoul" <oelghoul@linux.ibm.com>,
	linux-s390@vger.kernel.org, kvm@vger.kernel.org
Cc: "Heiko Carstens" <hca@linux.ibm.com>,
	"Vasily Gorbik" <gor@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	"Christian Borntraeger" <borntraeger@linux.ibm.com>
Subject: Re: [PATCH v10 5/5] vfio-pci/zdev: Add VFIO FMB device features
Date: Tue, 20 Oct 2026 22:46:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-179575@kernel.org> (raw)
In-Reply-To: <20261020212427.2561-6-oelghoul@linux.ibm.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] vfio-pci/zdev: race with firmware FMB update on nested enable in zpci_fmb_enable()
--

commit 9b8c0a84d66e0a84f5421ca2e3d89abbb0e740d4
Author: Omar Elghoul <oelghoul@linux.ibm.com>

vfio-pci/zdev: Add VFIO FMB device features

This commit introduces the VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE and
VFIO_DEVICE_FEATURE_ZPCI_FMB_READ device features to allow userspace to
enable, disable, and read the Function Measurement Block (FMB) on s390 zPCI
devices.

> diff --git a/drivers/vfio/pci/vfio_pci_zdev.c b/drivers/vfio/pci/vfio_pci_zdev.c
> index f47f36314a1cc..c2afe60111d73 100644
> --- a/drivers/vfio/pci/vfio_pci_zdev.c
> +++ b/drivers/vfio/pci/vfio_pci_zdev.c
> @@ -219,3 +219,63 @@ void vfio_pci_zdev_close_device(struct vfio_pci_core_device *vdev)
>  	if (zpci_kvm_hook.kvm_unregister)
>  		zpci_kvm_hook.kvm_unregister(zdev);
>  }
> +
> +int vfio_pci_zdev_feature_fmb_enable(struct vfio_pci_core_device *vdev, u32 flags,
> +				     void __user *arg, size_t argsz)
> +{
> +	struct zpci_dev *zdev;
> +	struct vfio_device_feature_zpci_fmb_enable fmb_enable;
> +	int ret;
> +
> +	ret = vfio_check_feature(flags, argsz, VFIO_DEVICE_FEATURE_SET, sizeof(fmb_enable));
> +	if (ret != 1)
> +		return ret;
> +
> +	zdev = to_zpci(vdev->pdev);
> +	if (!zdev)
> +		return -ENODEV;
> +
> +	if (copy_from_user(&fmb_enable, arg, sizeof(fmb_enable)))
> +		return -EFAULT;
> +
> +	guard(mutex)(&zdev->fmb_lock);
> +
> +	if (fmb_enable.enabled) {
> +		zdev->statistics_on = true;
> +		return zpci_fmb_enable(zdev);
> +	}

[Severity: Medium]
When VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE is invoked with enabled != 0 while
FMB measurement is already active in firmware, can
vfio_pci_zdev_feature_fmb_enable() -> zpci_fmb_enable() race with a periodic
firmware FMB DMA update and leave stale pre-reset or torn counters in
zdev->fmb?

In arch/s390/pci/pci.c, existing callers such as zpci_fmb_request_on() only
call zpci_fmb_enable() when FMB measurement is not already active:

arch/s390/pci/pci.c:zpci_fmb_request_on() {
	if (zdev->statistics_on && zdev->fmb)
		return -EINVAL;
	...
	zdev->statistics_on = true;
	return zpci_fmb_enable(zdev);
}

Here, vfio_pci_zdev_feature_fmb_enable() in drivers/vfio/pci/vfio_pci_zdev.c
calls zpci_fmb_enable() directly on a nested enable so that FMB counters are
reset to zero as documented for VFIO_DEVICE_FEATURE_ZPCI_FMB_ENABLE in
include/uapi/linux/vfio.h. However, zpci_fmb_enable() zeroes the live DMA
buffer before issuing the ZPCI_MOD_FC_SET_MEASURE command via zpci_mod_fc():

arch/s390/pci/pci.c:zpci_fmb_enable() {
	...
	if (!zdev->fmb) {
		zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
		if (!zdev->fmb)
			return -ENOMEM;
	} else {
		/* reuse the same FMB buffer for as long as the zdev lives */
		memset(zdev->fmb, 0, sizeof(*zdev->fmb));
	}
	...
	fib.fmb_addr = virt_to_phys(zdev->fmb);
	fib.gd = zdev->gisa;
	cc = zpci_mod_fc(req, &fib, &status);
}

Because virt_to_phys(zdev->fmb) is still registered with firmware during the
memset() and up until zpci_mod_fc() completes, a periodic firmware FMB update
firing in that window can overwrite zdev->fmb with pre-reset counters and a
non-zero timestamp. Since firmware only writes to zdev->fmb at fmb_update
intervals, subsequent VFIO_DEVICE_FEATURE_ZPCI_FMB_READ calls would then
return those stale values instead of zeroed counters.

Should FMB measurement be disabled in firmware before zeroing zdev->fmb, or
zdev->fmb cleared again after stopping measurement?

[ ... ]

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

      reply	other threads:[~2026-10-20 22:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20 21:24 [PATCH v10 0/5] vfio-pci/zdev: Improved zPCI Function Measurement Support Omar Elghoul
2026-10-20 21:24 ` [PATCH v10 1/5] s390/pci: Hold fmb_lock when enabling or disabling PCI devices Omar Elghoul
2026-10-20 22:35   ` sashiko-bot
2026-10-20 21:24 ` [PATCH v10 2/5] s390/pci: Reuse FMB buffer across enable and disable cycles Omar Elghoul
2026-10-20 22:35   ` sashiko-bot
2026-10-20 21:24 ` [PATCH v10 3/5] s390/pci: Improve function measurement state management Omar Elghoul
2026-10-20 22:53   ` sashiko-bot
2026-10-20 21:24 ` [PATCH v10 4/5] s390/pci: Fence FMB enable/disable via debugfs for passthrough devices Omar Elghoul
2026-10-20 22:33   ` sashiko-bot
2026-10-20 21:24 ` [PATCH v10 5/5] vfio-pci/zdev: Add VFIO FMB device features Omar Elghoul
2026-10-20 22:46   ` sashiko-bot [this message]

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=sashiko-outbox-179575@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=oelghoul@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