All of lore.kernel.org
 help / color / mirror / Atom feed
From: Anthony Krowiak <akrowiak@linux.ibm.com>
To: sashiko-reviews@lists.linux.dev
Cc: linux-s390@vger.kernel.org,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	kvm@vger.kernel.org, Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>
Subject: Re: [PATCH v7 03/15] s390/vfio-ap: Functions to initialize/release vfio device migration data
Date: Wed, 7 Oct 2026 17:49:56 -0400	[thread overview]
Message-ID: <14a9fc54-d1be-47dc-8f60-5e99c44f8ddf@linux.ibm.com> (raw)
In-Reply-To: <20260807223840.4FCA41F000E9@smtp.kernel.org>



On 8/7/26 6:38 PM, sashiko-bot@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review fosund 4 potential issue(s) to consider:
>
> New issues:
> - [High] The patch exposes the VFIO_MIGRATION_STOP_COPY capability to userspace before the actual migration data transfer logic is implemented, causing silent migration failures.
> - [High] An open migration FD can access a newly allocated migration session if the VFIO device is closed and reopened, bypassing the intended `NULL` check.
>
> Pre-existing issues:
> - [High] Unconditional NULL assignments to `req_trigger` and `cfg_chg_trigger` in `probe` can overwrite valid eventfd contexts set by concurrent ioctls, causing a memory leak and breaking interrupts.
> - [High] The mediated device is exposed to userspace before it is fully initialized and added to the driver's tracking lists, allowing multiple mdevs to attach to the same KVM guest.
> --
>
> --- Patch [3]: [PATCH v7 03/15] s390/vfio-ap: Functions to initialize/release vfio device migration data ---
> commit 04c1b466fec62cf9e53c2d6610cde9ecbf5fefad
> Author: Anthony Krowiak <akrowiak@linux.ibm.com>
>
> s390/vfio-ap: Functions to initialize/release vfio device migration data
>
> This commit provides the initialization and release functions for the data
> structures used during live guest migration of vfio-ap devices. It introduces
> stubs for the migration operations that will be fully implemented in
> subsequent patches.
>
>> diff --git a/drivers/s390/crypto/vfio_ap_migration.c b/drivers/s390/crypto/vfio_ap_migration.c
>> index 374d3a67cb214..cf303d228a0a5 100644s
>> --- a/drivers/s390/crypto/vfio_ap_migration.c
>> +++ b/drivers/s390/crypto/vfio_ap_migration.c
> [ ... ]
>> +static struct file *vfio_ap_set_state(struct vfio_device *vdev,
>> +				      enum vfio_device_mig_state  new_state)
>> +{
>> +	return NULL;
>> +}
> [ ... ]
>> +void vfio_ap_init_migration_capabilities(struct ap_matrix_mdev *matrix_mdev)
>> +{
>> +	if (ap_is_se_guest())
>> +		return;
>> +
>> +	matrix_mdev->vdev.migration_flags = VFIO_MIGRATION_STOP_COPY;
>> +	matrix_mdev->vdev.mig_ops = &vfio_ap_migration_ops;
>> +}
> [Severity: High]
> Will advertising the VFIO_MIGRATION_STOP_COPY capability to userspace before
> the actual migration data transfer logic is implemented cause silent failures?
>
> If vfio_ap_set_state() returns NULL for the VFIO_DEVICE_STATE_STOP_COPY
> transition, won't the VFIO core interpret this as a successful transition
> that does not require a data stream? This could cause QEMU to silently
> succeed on the source side without transferring any device state, leaving
> the AP subsystem broken for the guest on the target side.

This is just one of a series of 15 patches that implement VFIO migration of
vfio-ap devices. The rest of the series fills in the blanks.

>
>> +/**
>> + * vfio_ap_release_mig_files:
>> + *
>> + * Free the ap_config buffers for any open migration FDs. Although a
>> + * migration FD may still be held open by userspace, it is safe to free
>> + * mig_data here because:
>> + *
>> + *   1. matrix_mdev remains valid for the lifetime of any open migration
>> + *      FD via the vfio_device registration reference taken in
>> + *      vfio_ap_open_file_stream() and dropped in
>> + *      vfio_ap_release_mig_file().
>> + *
>> + *   2. mig_data is only accessed by the migration file ops
>> + *      (vfio_ap_stop_copy_read, vfio_ap_resuming_write) under
>> + *      mdevs_lock. Once mig_data is set to NULL by the caller, those
>> + *      paths will see NULL and return -ENODEV before dereferencing it.
>> + *
> [Severity: High]
> Does this lifecycle design handle the case where the VFIO device is closed
> and reopened while a migration FD is kept open?
>
> If userspace keeps the migration FD open, closes the VFIO device, and then
> reopens it, vfio_ap_mdev_open_device() will allocate a new mig_data. When the
> old migration FD subsequently performs read or write operations, won't it
> bypass the NULL check and incorrectly access the mig_data belonging to the
> new session?

This patch (03/15) introduces the initialization and teardown functions
for the migration lifecycle, while subsequent patches in the series
implement the full data streaming operations and validation checks:

- Patch 07/15 ("s390/vfio-ap: File ops called to save the vfio device
   state") implements validate_stop_copy_read_parms().
- Patch 10/15 ("s390/vfio-ap: File ops called to resume the vfio device
   state") implements validate_resuming_write_parms().

In those handlers, both read and write operations verify under mdevs_lock
that the passed struct file * matches the active migration stream
pointer for the current session (mig_data->stop_copy_mig_file.filp or
mig_data->resuming_mig_file.filp).

When the device is closed and reopened, open_device allocates a fresh
mig_data with NULL file pointers. If a stale migration FD from a
previous session attempts to perform read or write operations, the
equality check against the current session's filp fails (-EINVAL or
-ENXIO), safely preventing access to the new session's mig_data.

>
>> diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
>> index 36786d70a88f5..90b0fce0123b5 100644
>> --- a/drivers/s390/crypto/vfio_ap_ops.c
>> +++ b/drivers/s390/crypto/vfio_ap_ops.c
> [ ... ]
>> @@ -797,13 +809,28 @@ static int vfio_ap_mdev_probe(struct mdev_device *mdev)
>>   	if (IS_ERR(matrix_mdev))
>>   		return PTR_ERR(matrix_mdev);
>>   
>> +	/*
>> +	 * Migration capabilities must be initialized before calling
>> +	 * vfio_register_emulated_iommu_dev; otherwise, the VFIO core
>> +	 * will see mig_ops as NULL during the registration. This could
>> +	 * prevent the VFIO core from properly setting up migration
>> +	 * infrastructure like debugfs entries.
>> +	 *
>> +	 * This must be done before acquiring mdevs_lock to avoid an ABBA
>> +	 * deadlock: vfio_register_emulated_iommu_dev() acquires dev_set->lock
>> +	 * internally, while vfio_ap_mdev_open_device() is called by the VFIO
>> +	 * core with dev_set->lock already held and then acquires mdevs_lock.
>> +	 */
>> +	vfio_ap_init_migration_capabilities(matrix_mdev);
>> +
>>   	ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
>>   	if (ret)
>>   		goto err_put_vdev;
>> +
>> +	mutex_lock(&matrix_dev->mdevs_lock);
>>   	matrix_mdev->req_trigger = NULL;
>>   	matrix_mdev->cfg_chg_trigger = NULL;
> [Severity: High]
> This is a pre-existing issue, but does exposing the device to userspace
> before setting these triggers to NULL introduce a race condition?
>
> If userspace immediately opens the device after
> vfio_register_emulated_iommu_dev() and configures interrupts, it will
> allocate and set valid eventfd contexts for req_trigger and cfg_chg_trigger.
> When the probe thread continues and overwrites these with NULL, won't it leak
> the eventfd contexts and break guest interrupts?

This was fixed with commit 7fa61c29850d in response to this same comment
in another review. 7fa61c29850d

>
>>   	dev_set_drvdata(&mdev->dev, matrix_mdev);
>> -	mutex_lock(&matrix_dev->mdevs_lock);
>>   	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
> [Severity: High]
> This is also a pre-existing issue, but does calling
> vfio_register_emulated_iommu_dev() before list_add() allow multiple mediated
> devices to attach to the same KVM guest?
>
> If two mdevs are probed and opened concurrently before they are added to
> mdev_list, won't they bypass the conflict detection logic in
> vfio_ap_mdev_set_kvm() and violate KVM uniqueness invariants?

This will be fixed with a standalone patch since it is not related to this
series.

>


  reply	other threads:[~2026-10-07 21:50 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07 22:18 [PATCH v7 00/15] s390/vfio-ap: Add live guest migration support Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 01/15] s390/vfio-ap: Provide function to get the number of queues assigned to mdev Anthony Krowiak
2026-08-07 22:27   ` sashiko-bot
2026-08-10 13:14   ` Jason J. Herne
2026-08-24 15:31     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 02/15] s390/vfio-ap: Data structures for facilitating vfio device migration Anthony Krowiak
2026-08-07 22:31   ` sashiko-bot
2026-08-10 13:45   ` Jason J. Herne
2026-10-05 15:18     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 03/15] s390/vfio-ap: Functions to initialize/release vfio device migration data Anthony Krowiak
2026-08-07 22:38   ` sashiko-bot
2026-10-07 21:49     ` Anthony Krowiak [this message]
2026-08-11 13:57   ` Jason J. Herne
2026-10-05 15:24     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 04/15] s390/vfio-ap: Reset migration state in VFIO_DEVICE_RESET ioctl handler Anthony Krowiak
2026-08-07 22:54   ` sashiko-bot
2026-08-11 17:05   ` Jason J. Herne
2026-10-05 18:52     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 05/15] s390/vfio-ap: Callback to get/set vfio device mig state during guest migration Anthony Krowiak
2026-08-07 22:45   ` sashiko-bot
2026-10-08 18:44     ` Anthony Krowiak
2026-08-12 16:04   ` Jason J. Herne
2026-10-05 20:42     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 06/15] s390/vfio-ap: Transition guest migration state from STOP to STOP_COPY Anthony Krowiak
2026-08-07 22:43   ` sashiko-bot
2026-08-07 22:18 ` [PATCH v7 07/15] s390/vfio-ap: File ops called to save the vfio device migration state Anthony Krowiak
2026-08-07 22:37   ` sashiko-bot
2026-08-12 12:35     ` Anthony Krowiak
2026-10-07 20:18     ` Anthony Krowiak
2026-08-13 15:08   ` Jason J. Herne
2026-10-05 21:55     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 08/15] s390/vfio-ap: Transition device migration state from STOP to RESUMING Anthony Krowiak
2026-08-07 22:41   ` sashiko-bot
2026-10-08 13:08     ` Anthony Krowiak
2026-08-18 14:47   ` Jason J. Herne
2026-10-06 14:43     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 09/15] s390/vfio-ap: Add method to set a new guest AP configuration Anthony Krowiak
2026-08-07 22:40   ` sashiko-bot
2026-08-19 13:25   ` Jason J. Herne
2026-10-07 13:46     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 10/15] s390/vfio-ap: File ops called to resume the vfio device migration Anthony Krowiak
2026-08-07 22:30   ` sashiko-bot
2026-08-11 18:29     ` Anthony Krowiak
2026-10-07 20:08     ` Anthony Krowiak
2026-08-19 17:49   ` Jason J. Herne
2026-10-06 14:54     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 11/15] s390/vfio-ap: Transition device migration state to STOP Anthony Krowiak
2026-08-07 22:54   ` sashiko-bot
2026-10-08 19:08     ` Anthony Krowiak
2026-08-20 12:43   ` Jason J. Herne
2026-10-07 19:42     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 12/15] s390/vfio-ap: Transition device migration state from STOP to RUNNING and vice versa Anthony Krowiak
2026-08-07 22:43   ` sashiko-bot
2026-10-08 18:41     ` Anthony Krowiak
2026-08-20 12:45   ` Jason J. Herne
2026-10-07 19:28     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 13/15] s390/vfio-ap: Callback to get the size of data to be migrated during guest migration Anthony Krowiak
2026-08-07 22:37   ` sashiko-bot
2026-08-20 12:52   ` Jason J. Herne
2026-08-07 22:18 ` [PATCH v7 14/15] s390/vfio-ap: Add 'migratable' feature to sysfs 'features' attribute Anthony Krowiak
2026-08-07 22:36   ` sashiko-bot
2026-08-20 13:01   ` Jason J. Herne
2026-10-06 21:16     ` Anthony Krowiak
2026-10-07 12:06     ` Anthony Krowiak
2026-08-07 22:18 ` [PATCH v7 15/15] s390/vfio-ap: Add live guest migration chapter to vfio-ap.rst Anthony Krowiak
2026-08-07 22:32   ` 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=14a9fc54-d1be-47dc-8f60-5e99c44f8ddf@linux.ibm.com \
    --to=akrowiak@linux.ibm.com \
    --cc=agordeev@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=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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.