AI Trajectory


Trajectory: (Expand/Collapse All)
Seq Timestamp Type Name Duration
0/0 2026/07/28 13:22 flow patch-triage
1m Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [
    "vfio_ap_stop_copy_read",
    "vfio_ap_resuming_write",
    "vfio_ap_set_state",
    "vfio_ap_get_state",
    "vfio_ap_get_data_size",
    "vfio_ap_set_new_guest_config"
  ],
  "Reasoning": "The patch implements VFIO migration support for the s390 AP (Adjunct Processor) matrix mediated device driver. It adds file operations for reading and writing migration state (vfio_ap_stop_copy_read, vfio_ap_resuming_write) and VFIO migration state transition callbacks (vfio_ap_set_state, vfio_ap_get_state). These are reachable from userspace via VFIO ioctls and migration file descriptors, and can be tested in an s390x QEMU environment with AP instructions enabled.",
  "WorthFuzzing": true
}

1/1 2026/07/28 13:22 action read-patch-diff
0m Args:
null

Results:
{
  "PatchDiff": "commit 82a64d57797ed3dc1e0c4219fa19484b162d14bf\nAuthor: syz-cluster \u003ctriage@syzkaller.com\u003e\nDate:   Tue Jul 28 13:22:51 2026 +0000\n\n    syz-cluster: applied patch under review\n\ndiff --git a/Documentation/arch/s390/vfio-ap.rst b/Documentation/arch/s390/vfio-ap.rst\nindex ac0c07f76ddd7b..02d7cbe5e47918 100644\n--- a/Documentation/arch/s390/vfio-ap.rst\n+++ b/Documentation/arch/s390/vfio-ap.rst\n@@ -1012,7 +1012,7 @@ guest_matrix dyn ap_config\n \n the following features are advertised:\n \n----------------+---------------------------------------------------------------+\n++--------------+---------------------------------------------------------------+\n | Flag         | Description                                                   |\n +==============+===============================================================+\n | guest_matrix | guest_matrix attribute exists. It reports the matrix of       |\n@@ -1021,105 +1021,523 @@ the following features are advertised:\n +--------------+---------------------------------------------------------------+\n | dyn          | Indicates hot plug/unplug of AP adapters, domains and control |\n |              | domains for a guest to which the mdev is attached.            |\n-+------------+-----------------------------------------------------------------+\n++--------------+---------------------------------------------------------------+\n | ap_config    | ap_config interface for one-shot modifications to mdev config |\n +--------------+---------------------------------------------------------------+\n+| migratable   | Indicates that live guest migration is supported for guests   |\n+|              | to which crypto devices are passed through                    |\n++--------------+---------------------------------------------------------------+\n \n-Limitations\n-===========\n-Live guest migration is not supported for guests using AP devices without\n-intervention by a system administrator. Before a KVM guest can be migrated,\n-the vfio_ap mediated device must be removed. Unfortunately, it can not be\n-removed manually (i.e., echo 1 \u003e /sys/devices/vfio_ap/matrix/$UUID/remove) while\n-the mdev is in use by a KVM guest. If the guest is being emulated by QEMU,\n-its mdev can be hot unplugged from the guest in one of two ways:\n-\n-1. If the KVM guest was started with libvirt, you can hot unplug the mdev via\n-   the following commands:\n-\n-      virsh detach-device \u003cguestname\u003e \u003cpath-to-device-xml\u003e\n-\n-      For example, to hot unplug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 from\n-      the guest named 'my-guest':\n-\n-         virsh detach-device my-guest ~/config/my-guest-hostdev.xml\n-\n-            The contents of my-guest-hostdev.xml:\n-\n-.. code-block:: xml\n-\n-            \u003chostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'\u003e\n-              \u003csource\u003e\n-                \u003caddress uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/\u003e\n-              \u003c/source\u003e\n-            \u003c/hostdev\u003e\n-\n-\n-      virsh qemu-monitor-command \u003cguest-name\u003e --hmp \"device-del \u003cdevice-id\u003e\"\n-\n-      For example, to hot unplug the vfio_ap mediated device identified on the\n-      qemu command line with 'id=hostdev0' from the guest named 'my-guest':\n-\n-.. code-block:: sh\n-\n-         virsh qemu-monitor-command my-guest --hmp \"device_del hostdev0\"\n-\n-2. A vfio_ap mediated device can be hot unplugged by attaching the qemu monitor\n-   to the guest and using the following qemu monitor command:\n-\n-      (QEMU) device-del id=\u003cdevice-id\u003e\n-\n-      For example, to hot unplug the vfio_ap mediated device that was specified\n-      on the qemu command line with 'id=hostdev0' when the guest was started:\n-\n-         (QEMU) device-del id=hostdev0\n-\n-After live migration of the KVM guest completes, an AP configuration can be\n-restored to the KVM guest by hot plugging a vfio_ap mediated device on the target\n-system into the guest in one of two ways:\n-\n-1. If the KVM guest was started with libvirt, you can hot plug a matrix mediated\n-   device into the guest via the following virsh commands:\n-\n-   virsh attach-device \u003cguestname\u003e \u003cpath-to-device-xml\u003e\n-\n-      For example, to hot plug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 into\n-      the guest named 'my-guest':\n-\n-         virsh attach-device my-guest ~/config/my-guest-hostdev.xml\n-\n-            The contents of my-guest-hostdev.xml:\n-\n-.. code-block:: xml\n-\n-            \u003chostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'\u003e\n-              \u003csource\u003e\n-                \u003caddress uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/\u003e\n-              \u003c/source\u003e\n-            \u003c/hostdev\u003e\n-\n-\n-   virsh qemu-monitor-command \u003cguest-name\u003e --hmp \\\n-   \"device_add vfio-ap,sysfsdev=\u003cpath-to-mdev\u003e,id=\u003cdevice-id\u003e\"\n-\n-      For example, to hot plug the vfio_ap mediated device\n-      62177883-f1bb-47f0-914d-32a22e3a8804 into the guest named 'my-guest' with\n-      device-id hostdev0:\n-\n-      virsh qemu-monitor-command my-guest --hmp \\\n-      \"device_add vfio-ap,\\\n-      sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\\\n-      id=hostdev0\"\n-\n-2. A vfio_ap mediated device can be hot plugged by attaching the qemu monitor\n-   to the guest and using the following qemu monitor command:\n-\n-      (qemu) device_add \"vfio-ap,sysfsdev=\u003cpath-to-mdev\u003e,id=\u003cdevice-id\u003e\"\n-\n-      For example, to plug the vfio_ap mediated device\n-      62177883-f1bb-47f0-914d-32a22e3a8804 into the guest with the device-id\n-      hostdev0:\n+Live Guest Migration\n+====================\n+The VFIO AP mediated device is not used to provide userspace with direct\n+access to device memory as is the case with other devices that use the VFIO\n+framework for device pass-through. Instead, it manages AP\n+configuration metadata identifying the adapters, domains, and control\n+domains to which a guest will be granted access. These AP resources are\n+configured by assigning them to a vfio-ap mediated device via its sysfs\n+assignment interfaces. When the guest is started, the vfio_ap device driver\n+sets the guest's AP configuration from the metadata stored with the mediated\n+device. The AP devices are not accessed directly through the vfio_ap driver,\n+so the driver does not migrate the state of the AP devices themselves.\n+Instead, it migrates the AP configuration metadata. The vfio_ap device\n+driver on the destination host will then ensure that the AP configuration\n+metadata migrated from the source host is compatible with the AP\n+configuration available on the destination host and if\n+so, will set the destination guest's AP configuration accordingly.\n \n-         (QEMU) device-add \"vfio-ap,\\\n-         sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\\\n-         id=hostdev0\"\n+To be considered compatible, the AP configuration for the destination host must\n+meet these requirements:\n+\n+* Each AP adapter, domain and control domain number assigned to the source\n+  guest's AP configuration must be in the destination host's AP\n+  configuration. This can be verified as follows:\n+\n+  * On the source host, display the AP configuration of the source guest\n+    with the following two commands:\n+\n+    .. code-block:: bash\n+\n+       cat /sys/devices/vfio_ap/matrix/$UUID/guest_matrix\n+       cat /sys/devices/vfio_ap/matrix/$UUID/control_domains\n+\n+    .. note::\n+       Replace ``$UUID`` with the actual UUID of your mediated device.\n+\n+       The ``guest_matrix`` lists the APQNs of the queue devices assigned\n+       to the mediated device; for example:\n+\n+       .. code-block:: bash\n+\n+          00.0033\n+          00.003a\n+\n+       The ``control_domains`` lists the domain numbers of the control\n+       domains; for example:\n+\n+       .. code-block:: bash\n+\n+          0033\n+          003a\n+\n+  * Display the AP configuration of the destination host with the following\n+    two commands:\n+\n+    .. code-block:: bash\n+\n+       lszcrypt -V\n+       cat /sys/bus/ap/ap_control_domain_mask\n+\n+    .. note::\n+       The ``lszcrypt -V`` command lists the AP cards (adapters) and queue\n+       devices on the host system; for example:\n+\n+       .. code-block:: bash\n+\n+          =========== ===== ============ ==========\n+          CARD.DOMAIN TYPE  MODE         DRIVER\n+          =========== ===== ============ ==========\n+          00          CEX5C CCA-Coproc   cex4card\n+          00.0033     CEX5C CCA-Coproc   vfio_ap\n+          00.003a     CEX5C CCA-Coproc   vfio_ap\n+          =========== ===== ============ ==========\n+\n+       Each bit in the ap_control_domain_mask corresponds to\n+       domain numbers 0-255. If a bit is set to one, then\n+       the control domain is available on the source host. For\n+       example:\n+\n+       .. code-block:: bash\n+\n+          0x0000000000001020000000000000000000000000000000000000000000000000\n+\n+       This mask indicates that AP control domains 51 (0x33) and 58 (0x3a) are\n+       in the AP configuration of the host system.\n+\n+  * Verify the configurations:\n+\n+    * Each APQN in the guest's sysfs ``guest_matrix`` file must match an\n+      APQN returned from the ``lszcrypt -V`` command on the destination host. It\n+      is important to note the driver to which the queue is bound which\n+      is displayed in the ``driver`` column. Queues not bound to the vfio_ap\n+      driver are not available to the destination guest. Even if the APQNs are\n+      available to the driver, that does not guarantee they can be used by the\n+      guest being migrated; for example, migration will fail if any of these APQNs\n+      are assigned to a mediated device other than the one being migrated.\n+\n+    * Each domain number from the source guest's sysfs\n+      ``control_domains`` attribute must correspond to a bit set to one in\n+      the destination host's ``ap_control_domain_mask``.\n+\n+\n+* Each APQN assigned to the source guest and destination guest must\n+  reference a queue device with compatible hardware capabilities:\n+\n+  +--------------+---------------------------------------------------------------+\n+  | Hardware     | Description                                                   |\n+  | Capabilities |                                                               |\n+  +==============+===============================================================+\n+  | [1]          | * AP special command facility (APSC)                          |\n+  | Facilities   | * AP 4096-bit ME PKU commands facility (AP4KM)                |\n+  |              | * AP 4096-bit CRT PKU commands (AP4KC)                        |\n+  +--------------+---------------------------------------------------------------+\n+  | [1] Mode     | * CCA-mode                                                    |\n+  |              | * Accelerator-mode                                            |\n+  |              | * XCP-mode (EP11)                                             |\n+  +--------------+---------------------------------------------------------------+\n+  | [1] AP       |   APXA installed                                              |\n+  | extended     |                                                               |\n+  | addressing   |                                                               |\n+  +--------------+---------------------------------------------------------------+\n+  | [1] Command  |   Command filtering available                                 |\n+  | filtering    |                                                               |\n+  +--------------+---------------------------------------------------------------+\n+  | [2]          | * Full native card function                                   |\n+  | Functional   | * Only stateless functions                                    |\n+  | capabilities |                                                               |\n+  +--------------+---------------------------------------------------------------+\n+  | [3] AP type  | * 10 (0x0a): CEX4                                             |\n+  |              | * 11 (0x0b): CEX5                                             |\n+  |              | * 12 (0x0c)  CEX6                                             |\n+  |              | * 13 (0x0d)  CEX7                                             |\n+  |              | * 14 (0x0e)  CEX8                                             |\n+  +--------------+---------------------------------------------------------------+\n+\n+  +-----------------------------------------------------------------------------+\n+  | Table Keys                                                                  |\n+  +=============================================================================+\n+  |                                                                             |\n+  | [1]        The hardware capability must be the same for both the source     |\n+  |            and destination queue device.                                    |\n+  |                                                                             |\n+  | [2]        The source and destination queues must either both have          |\n+  |            full native card function or both have stateless functions.      |\n+  |            If the functional capabilities don't match, then the source      |\n+  |            queue can have stateless capabilities since full native card     |\n+  |            function can handle stateless functions; otherwise, migration    |\n+  |            will fail.                                                       |\n+  |                                                                             |\n+  | [3]        The AP type on the source and destination guests can             |\n+  |            differ if the queue passed through to the target guest is a      |\n+  |            newer model (backwards compatible)                               |\n+  +-----------------------------------------------------------------------------+\n+\n+  * To verify the hardware capabilities are compatible:\n+\n+    * On both the source and destination hosts, display the hardware capabilities\n+      for each AP adapter in the AP configuration for that host:\n+\n+      .. code-block:: bash\n+\n+         cat /sys/bus/ap/devices/card$APID/ap_functions\n+\n+      .. note::\n+         The ``$APID`` is the two-character adapter number in hexadecimal format;\n+         for example, ``card02`` or ``card1f``. The ``/sys/bus/ap/devices`` directory\n+         also lists the APQNs of the queue devices installed in the respective\n+         host system with the first two characters being the APID; for example:\n+         ``02.0004`` or ``00.0033`` .\n+\n+    * Verify the hardware capabilities for each AP adapter device on the source\n+      and destination host are compatible. The ``ap_functions`` attribute is a\n+      bitmask. The bits in the mask read from left to right starting with bit 0.\n+      Each bit that is set to one indicates the corresponding hardware capability\n+      is installed:\n+\n+      * Bits 0-2 are the facilities bits. Each bit value must match for the AP\n+        devices on the source and destination systems. The values indicate the\n+        following:\n+\n+        * bit-0: APSC is installed\n+        * bit-1: AP4KM is installed\n+        * bit-2: AP4KC is installed\n+\n+      * Bits 3-5 specify the adapter Modes. Only one of these bits will be set to\n+        one and must be the same for both the source and destination adapter\n+        devices:\n+\n+        * bit-3: CCA-mode\n+        * bit-4: Accelerator-mode\n+        * bit-5: XCP-mode (EP11)\n+\n+      * Bit 6 specifies whether APXA is installed and must be the same for both\n+        the source and destination adapter devices.\n+\n+      * Bit 7 specifies whether command filtering is installed\n+        and must be the same for both the source and destination adapter devices.\n+\n+      * Bits 8-15 are the classification bits. There are only two relevant\n+        bits and only one of them will be set to one. For the source and destination\n+        adapter devices to be considered compatible:\n+\n+        * Bit 8 may be set to 1 on both hosts (full native card function)\n+        * Bit 9 may be set to 1 on both hosts (only stateless functions available)\n+        * Bit 9 may be set to 1 on the source host and bit 8 may be set to 1 on\n+          the destination host\n+\n+      * Bits 16-17 are the binding state bits which must be 00\n+\n+  * To verify the AP types are compatible:\n+\n+    * On both the source and destination hosts, display the hardware type\n+      for each AP adapter in the AP configuration for that host to ensure the\n+      source and destination adapter types are the same, or the source type is\n+      less than the destination type::\n+\n+       cat /sys/bus/ap/devices/card$APID/hwtype\n+\n+      .. note::\n+         The ``$APID`` is the two-character adapter number in hexadecimal format;\n+         for example, ``card02`` or ``card1f``.\n+\n+Setting up for live guest migration on the destination host\n+-----------------------------------------------------------\n+To set up a host as the destination for live guest migration, do the\n+following:\n+\n+1. Create a mediated device\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+Create a mediated device with the same name (UUID) as the mediated device\n+used to supply the AP configuration to the source guest. For example, if\n+the mediated device on the source guest is named\n+62177883-f1bb-47f0-914d-32a22e3a8804:\n+\n+.. code-block:: bash\n+\n+   echo 62177883-f1bb-47f0-914d-32a22e3a8804 \u003e \\\n+     /sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough/create\n+\n+2. Reserve adapters and domains for the vfio_ap device driver's use\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+Reserve the same adapters, and domains passed through to the source guest\n+for the vfio_ap device driver on the destination host by clearing the\n+adapter numbers from the ``/sys/bus/ap/apmask`` and/or the domain numbers\n+from the ``/sys/bus/ap/aqmask`` files.\n+\n+For example, suppose adapters 0, 20 (0x14) and 22 (0x16) as well as\n+domains 51 (0x33) and 58 (0x3a) are passed through to the source guest. To\n+ensure they can be passed through to the destination guest, they must be\n+made available to the vfio_ap device driver on the destination host. To\n+make them available, do one or both of the following on the destination host:\n+\n+.. code-block:: bash\n+\n+   echo -0,-0x14,-22 \u003e /sys/bus/ap/apmask\n+   echo -0,-0x33,-0x3a \u003e /sys/bus/ap/aqmask\n+\n+.. note::\n+\n+    * It is not necessary to reserve both the adapters and domains.\n+      Reserving an adapter implicitly allows the vfio_ap driver to assign\n+      each of the domains available on the system along with the reserved\n+      adapters to a guest's AP configuration and vice versa.\n+\n+    * The Cartesian product of the adapters and domains reserved for the\n+      vfio_ap device driver identifies the APQNs of the AP queue devices to be\n+      bound to the vfio_ap device driver. If an APQN does not reference a\n+      queue device installed in the system at the time of live guest migration,\n+      the migration will fail.\n+\n+    * It is not necessary to configure the destination mediated device with\n+      adapters, domains and control domains because the source guest's AP\n+      configuration will overlay the destination guest's during migration.\n+\n+    * To verify that the queue devices are reserved for the vfio_ap\n+      device driver, issue the ``lszcrypt -V`` command and you should\n+      see ``vfio_ap`` in the ``DRIVER`` column of the output.\n+\n+Live guest migration failures due to AP configuration errors\n+------------------------------------------------------------\n+This section describes the errors that may occur during live guest migration\n+when the AP configuration of the source guest and destination host are not\n+compatible and where to look for problem determination.\n+\n+The destination host is missing the mediated device\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+\n+This error occurs when a mediated device with the same name (UUID) as the\n+mediated device being migrated has not been created on the destination host.\n+\n+**Source host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | error: device not found: mediated device '$UUID' not           |\n+|              | found                                                          |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | initiating migration                                           |\n+|              | qemu-system-s390x: Sibling indicated error 1                   |\n++--------------+----------------------------------------------------------------+\n+\n+.. note::\n+   $UUID is the UUID of the mediated device being migrated from the source host.\n+\n+\n+One or more queues are not bound to the vfio_ap driver on the destination host\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+The queue devices passed through to the source guest are not installed in the\n+destination host or are not bound to its vfio_ap device driver.\n+\n+**Source host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | error: operation failed: migration failed. Message from the    |\n+|              | source host: operation failed: job 'migration out' failed:     |\n+|              | Sibling indicated error 1. Message from the destination host:  |\n+|              | operation failed: job 'migration in' failed: load of migration |\n+|              | failed: No such device: Failed to load vmstate version_id: 1,  |\n+|              | ret: -19                                                       |\n++--------------+----------------------------------------------------------------+\n+\n+**Destination host**\n+\n++--------------+----------------------------------------------------------------+\n+| Kernel log:  | vfio_ap_mdev $UUID: Queue $APID.$APQI not available to         |\n+|              | vfio_ap driver on target host                                  |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | shutting down, reason=failed                                   |\n+|              | qemu-system-s390x: terminating on signal $SIGNAL_NUM from      |\n+|              | pid $PID $DAEMON                                               |\n++--------------+----------------------------------------------------------------+\n+\n+\n+One or more control domains are not available on the destination host\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+The control domains passed through to the source guest are not installed on the\n+destination host.\n+\n+**Source host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | error: operation failed: migration failed. Message from the    |\n+|              | source host: operation failed: job 'migration out' failed:     |\n+|              | Sibling indicated error 1. Message from the destination host:  |\n+|              | operation failed: job 'migration in' failed: load of migration |\n+|              | failed: No such device: Failed to load vmstate version_id: 1,  |\n+|              | ret: -19                                                       |\n++--------------+----------------------------------------------------------------+\n+\n+**Destination host**\n+\n++--------------+----------------------------------------------------------------+\n+| Kernel log:  | vfio_ap_mdev $UUID: Control domain $DOMAIN_NUM not available   |\n+|              | on the destination host                                        |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | shutting down, reason=failed                                   |\n+|              | qemu-system-s390x: terminating on signal $SIGNAL_NUM from      |\n+|              | pid $PID $DAEMON                                               |\n++--------------+----------------------------------------------------------------+\n+\n+\n+.. note::\n+   * $UUID is the UUID of the mediated device being migrated from the source host.\n+   * $APID.$APQI is adapter number and queue index (domain number) comprising the\n+     $APQN (for example, 00.0033) of the queue device that is not available.\n+   * $SIGNAL_NUM is the signal number that caused the QEMU process to terminate.\n+   * $PID is the process ID of the QEMU process on the destination host.\n+   * $DAEMON is the name of the QEMU daemon process.\n+   * This message will be logged for each queue for which this error is detected.\n+\n+Failure to get hardware information for a queue device on destination host\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+The vfio_ap device driver on the destination host will execute an AP instruction\n+to get the hardware information for a queue device to perform queue compatibility\n+checks. The instruction can fail for a number of reasons in which case the\n+migration will be terminated.\n+\n+**Source host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | error: operation failed: migration failed. Message from the    |\n+|              | source host: operation failed: job 'migration out' failed:     |\n+|              | Sibling indicated error 1. Message from the destination host:  |\n+|              | operation failed: job 'migration in' failed: load of migration |\n+|              | failed: failed to get hardware information for queue device    |\n+|              | $APQN                                                          |\n++--------------+----------------------------------------------------------------+\n+\n+**Destination host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | vfio_ap_mdev $UUID: migration failed: Failed to get hwinfo for |\n+|              | queue $APQN on target host: rc=$RESPONSE-CODE                  |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | shutting down, reason=failed                                   |\n+|              | qemu-system-s390x: terminating on signal $SIGNAL-ID from pid   |\n+|              | $PID $DAEMON                                                   |\n++--------------+----------------------------------------------------------------+\n+\n+.. note::\n+   * $UUID is the UUID of the mediated device being migrated from the source host.\n+   * $APQN (for example, 00.0033) of the queue device that is not compatible.\n+   * $RESPONSE_CODE is the response code from the AP instruction.\n+   * $SIGNAL_NUM is the signal number of the signal that caused the QEMU process\n+     to terminate.\n+   * $PID is the process ID of the QEMU process on the destination host.\n+   * $DAEMON is the name of the QEMU daemon process.\n+   * This message will be logged for each queue for which this error is detected.\n+\n+The AP configurations of the source guest and destination host not compatible\n+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n+The functional capabilities or facilities available on one or more of the queues\n+passed through to the source guest are not compatible with the queue device with\n+the same APQN on the destination system (see the Hardware Capabilities table above)\n+\n+**Source host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | error: operation failed: migration failed. Message from the    |\n+|              | source host: operation failed: job 'migration out' failed:     |\n+|              | Sibling indicated error 1. Message from the destination host:  |\n+|              | operation failed: job 'migration in' failed: load of migration |\n+|              | failed: No such device: Failed to load vmstate version_id: 1,  |\n+|              | ret: -19                                                       |\n++--------------+----------------------------------------------------------------+\n+| Kernel log:  | N/A                                                            |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | *initiating migration*                                         |\n+|              |                                                                |\n+|              | *qemu-system-s390x: Sibling indicated error 1*                 |\n++--------------+----------------------------------------------------------------+\n+\n+**Destination host**\n+\n++--------------+----------------------------------------------------------------+\n+| Log          | Message                                                        |\n++==============+================================================================+\n+| Console log: | N/A                                                            |\n++--------------+----------------------------------------------------------------+\n+| Kernel log:  | *vfio_ap_mdev $UUID: Migration failed: Source and target       |\n+|              | queue ($APQN) not compatible*                                  |\n+|              |                                                                |\n+|              | The message above will be followed by one or more messages     |\n+|              | enumerating the incompatible features; for example:            |\n+|              |                                                                |\n+|              | ``vfio_ap_mdev $UUID: APSC facility installed in source queue  |\n+|              | $APQN``                                                        |\n+|              | ``vfio_ap_mdev $UUID: APSC facility not installed in target    |\n+|              | queue $APQN``                                                  |\n+|              |                                                                |\n+|              | ``AP type of source ($APTYPE1) not compatible with target      |\n+|              | ($APTYPE2)`` ...                                               |\n++--------------+----------------------------------------------------------------+\n+| QEMU log:    | *initiating migration*                                         |\n+|              |                                                                |\n+|              | *qemu-system-s390x: error while loading state section id ...*  |\n+|              |                                                                |\n+|              | *shutting down, reason=failed*                                 |\n+|              |                                                                |\n+|              | *terminating on signal $SIGNAL-ID from pid $PID $DAEMON*       |\n++--------------+----------------------------------------------------------------+\n+\n+.. note::\n+   * $UUID is the UUID of the mediated device being migrated from the source host.\n+   * $APQN (for example, 00.0033) of the queue device that is incompatible.\n+   * $APTYPE1 is the type of the source queue device.\n+   * $APTYPE2 is the type of the target queue device.\n+   * $SIGNAL-ID is the signal number of the signal that caused the QEMU process\n+     to terminate.\n+   * $PROCESS-ID is the process ID of the QEMU process on the source host.\n+   * $PID is the process ID of the QEMU process on the destination host.\n+   * $DAEMON is the name of the QEMU daemon process.\n+   * Each follow-on message logging the incompatibility will be logged for each\n+     error detected for the specified queue device.\n+\n+AP Configuration Management\n+---------------------------\n+The AP configurations of the source guest and destination host must be kept\n+in synchronization or live guest migration will likely fail due to potential\n+incompatibilities introduced by such changes. In particular, changing adapter\n+or domain reservations via the sysfs ``apmask`` or ``aqmask`` attributes on\n+either the source or destination host without coordinating such changes between\n+the two hosts -especially while a migration is taking place - can result in\n+migration failures.\n+\n+Configuration stability is an orchestration-layer or system administrator\n+responsibility, which is consistent with other VFIO device types. The vfio_ap\n+device driver will validate source and destination configurations and provide\n+clear diagnostics when incompatibilities are detected, enabling orchestration\n+tools to implement appropriate policies; for example, blocking migration when\n+such changes are to be made.\n+\n+Master Key administration\n+--------------------------\n+It is imperative that s390 Common Cryptographic Architecture (CCA) master key\n+administration be performed on both the source and destination AP\n+devices to synchronize the key values prior to allowing live guest\n+migration. If the master keys do not match, then crypto applications that\n+rely on secure keys wrapped by a CCA master key will fail when the guest on\n+which they are running is migrated to the destination host. Again, this is\n+an orchestration-layer or system administrator responsibility and migration must\n+be blocked while master key administration is being performed.\ndiff --git a/drivers/s390/crypto/Makefile b/drivers/s390/crypto/Makefile\nindex e83c6603c8587d..20f29184825a7b 100644\n--- a/drivers/s390/crypto/Makefile\n+++ b/drivers/s390/crypto/Makefile\n@@ -34,5 +34,5 @@ pkey-uv-objs := pkey_uv.o\n obj-$(CONFIG_PKEY_UV) += pkey-uv.o\n \n # adjunct processor matrix\n-vfio_ap-objs := vfio_ap_drv.o vfio_ap_ops.o\n+vfio_ap-objs := vfio_ap_drv.o vfio_ap_ops.o vfio_ap_migration.o\n obj-$(CONFIG_VFIO_AP) += vfio_ap.o\ndiff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c\nindex 8e69ed286bb9ca..4e973736522bc1 100644\n--- a/drivers/s390/crypto/vfio_ap_drv.c\n+++ b/drivers/s390/crypto/vfio_ap_drv.c\n@@ -26,9 +26,11 @@ MODULE_LICENSE(\"GPL v2\");\n struct ap_matrix_dev *matrix_dev;\n debug_info_t *vfio_ap_dbf_info;\n \n+#define FEATURES \"guest_matrix hotplug ap_config migratable\"\n+\n static ssize_t features_show(struct device *dev, struct device_attribute *attr, char *buf)\n {\n-\treturn sysfs_emit(buf, \"guest_matrix hotplug ap_config\\n\");\n+\treturn sysfs_emit(buf, \"%s\\n\", FEATURES);\n }\n static DEVICE_ATTR_RO(features);\n \ndiff --git a/drivers/s390/crypto/vfio_ap_migration.c b/drivers/s390/crypto/vfio_ap_migration.c\nnew file mode 100644\nindex 00000000000000..8379cf8bbce897\n--- /dev/null\n+++ b/drivers/s390/crypto/vfio_ap_migration.c\n@@ -0,0 +1,1530 @@\n+// SPDX-License-Identifier: GPL-2.0\n+/*\n+ * Drives vfio_ap mdev migration.\n+ *\n+ * Copyright IBM Corp. 2025\n+ */\n+#include \u003clinux/anon_inodes.h\u003e\n+#include \u003clinux/file.h\u003e\n+#include \"vfio_ap_private.h\"\n+\n+/*\n+ * Masks the fields of the queue information returned from the PQAP(TAPQ)\n+ * command. In order to migrate a guest, it's AP configuration must be\n+ * compatible with AP configuration assigned to the target guest's mdev.\n+ * This mask is used to verify that the queue information for each source and\n+ * target queue is compatible.\n+ *\n+ * The following bits must match for the source device and the corresponding\n+ * destination device:\n+ * -------------------------------------------------------------------------\n+ * S bit 0: APSC  facility installed\n+ * M bit 1: APQKM facility installed\n+ * C bit 2: AP4KC facility installed\n+ * Mode bits 3-5:\n+ *     D bit 3: CCA-mode facility\n+ *     A bit 4: accelerator-mode facility\n+ *     X bit 5: XCP-mode facility\n+ * N  bit 6: APXA facility installed\n+ * SL bit 7: SLCF facility installed\n+ *\n+ * Either bit 8 or bit 9 will be set. If bit 8 is set for the source device,\n+ * then it must also be set for the corresponding destination device:\n+ * -------------------------------------------------------------------------\n+ * Classification (functional capabilities) bits 8-16\n+ *     bit 8: Native card function\n+ *     bit 9: Only stateless functions\n+ *\n+ * The BS bits must be set to 0 for both the source and corresponding\n+ * destination device:\n+ * -------------------------------------------------------------------------\n+ * BS bits 16-17:\n+ *\n+ * The AP type of the source device must be less than or equal to that of\n+ * the corresponding destination device:\n+ * -------------------------------------------------------------------------\n+ * AP Type bits 32-40:\n+ */\n+#define QINFO_DATA_MASK\t\t0xffffc000ff000000\n+\n+/*\n+ * Masks the bit that indicates whether full native card function is available\n+ * from the 8 bits specifying the functional capabilities of a queue\n+ */\n+#define CLASSIFICATION_NATIVE_FCN_MASK\t\t0x80\n+\n+/* The maximum number of queues that can be installed in an s390 system */\n+#define MAX_AP_QUEUES\t\t\t\t(AP_DEVICES * AP_DOMAINS)\n+\n+/**\n+ * struct vfio_ap_migration_file\n+ *\n+ * This object is used for chunk processing of multiple reads and writes of\n+ * AP configuration information.\n+ *\n+ * @filp:\tfile stream used to read or write AP configuration data\n+ * @ap_config:\tobject used to store AP configuration data between read or write\n+ *\t\tcalls\n+ * @config_sz:\tthe size (in bytes) of @ap_config\n+ */\n+struct vfio_ap_migration_file {\n+\tstruct file\t\t*filp;\n+\tstruct vfio_ap_config\t*ap_config;\n+\tunsigned long\t\tconfig_sz;\n+};\n+\n+/**\n+ * struct vfio_ap_migration_data\n+ *\n+ * Manages the migration state for the VFIO device that maintains the AP\n+ * configuration of the guest being migrated.\n+ *\n+ * @mig_state:\t\tthe current migration state\n+ * @resuming_mig_file:\tthe object used to restore the state of the vfio-ap\n+ *\t\t\tdevice the destination host:\n+ * @stop_copy_mig_file: the object used to store the AP configuration of the\n+ *\t\t\tsource guest for transfer to the destination host.\n+ */\n+struct vfio_ap_migration_data {\n+\tenum vfio_device_mig_state\tmig_state;\n+\tstruct vfio_ap_migration_file\tresuming_mig_file;\n+\tstruct vfio_ap_migration_file\tstop_copy_mig_file;\n+};\n+\n+/**\n+ * struct vfio_ap_queue_info - the information for an AP queue\n+ *\n+ * @data: contains the queue information returned in GR2 from the PQAP(TAPQ)\n+ *\t  command\n+ * @apqn: the APQN of the queue\n+ * @reserved: padding to ensure consistent structure size across platforms\n+ */\n+struct vfio_ap_queue_info {\n+\tu64 data;\n+\tu16 apqn;\n+\tu8  reserved[6];\n+};\n+\n+/**\n+ * struct vfio_ap_config - the guest's AP configuration\n+ *\n+ * @num_queues:\tthe number of queues passed through to the guest\n+ * @reserved:\tpadding to ensure proper alignment of @adm\n+ * @adm:\tbitmap specifying the control domains in the AP configuration\n+ * @qinfo:\tan array of vfio_ap_queue_info objects, each specifying the\n+ *\t\tqueue information for a queue passed through to the guest\n+ */\n+struct vfio_ap_config {\n+\tu32\t\t\t\tnum_queues;\n+\tu8\t\t\t\treserved[4];\n+\tu64\t\t\t\tadm[DIV_ROUND_UP(AP_DOMAINS, 64)];\n+\tstruct vfio_ap_queue_info\tqinfo[] __counted_by(num_queues);\n+};\n+\n+static void\n+vfio_ap_release_stop_copy_file(struct vfio_ap_migration_data *mig_data)\n+{\n+\tkfree(mig_data-\u003estop_copy_mig_file.ap_config);\n+\tmig_data-\u003estop_copy_mig_file.ap_config = NULL;\n+\tmig_data-\u003estop_copy_mig_file.config_sz = 0;\n+\tmig_data-\u003estop_copy_mig_file.filp = NULL;\n+}\n+\n+static void\n+vfio_ap_release_resuming_file(struct vfio_ap_migration_data *mig_data)\n+{\n+\tkvfree(mig_data-\u003eresuming_mig_file.ap_config);\n+\tmig_data-\u003eresuming_mig_file.ap_config = NULL;\n+\tmig_data-\u003eresuming_mig_file.config_sz = 0;\n+\tmig_data-\u003eresuming_mig_file.filp = NULL;\n+}\n+\n+static int vfio_ap_release_mig_file(struct inode *file_inode, struct file *filp)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev = filp-\u003eprivate_data;\n+\tint ret = 0;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\t/*\n+\t * mig_data may be NULL if the device was closed (vfio_ap_mdev_close_device)\n+\t * before the migration FD was released by userspace. In that case the\n+\t * migration file state was already cleaned up; nothing to do here.\n+\t */\n+\tif (!matrix_mdev-\u003emig_data)\n+\t\tgoto done;\n+\n+\tif (filp == matrix_mdev-\u003emig_data-\u003estop_copy_mig_file.filp)\n+\t\tvfio_ap_release_stop_copy_file(matrix_mdev-\u003emig_data);\n+\telse if (filp == matrix_mdev-\u003emig_data-\u003eresuming_mig_file.filp)\n+\t\tvfio_ap_release_resuming_file(matrix_mdev-\u003emig_data);\n+\telse\n+\t\tret = -ENOENT;\n+\n+done:\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\tvfio_device_put_registration(\u0026matrix_mdev-\u003evdev);\n+\treturn ret;\n+}\n+\n+/**\n+ * validate_stop_copy_read_parms: Validate the input parameters to the\n+ *                                vfio_ap_stop_copy_read function\n+ *\n+ * @matrix_mdev: The object device containing the state to be read\n+ * @filp: Pointer to the file stream used to read the vfio-ap device state\n+ * @pos:  The file offset from which to start reading data\n+ * @len:  The length of the data to be read\n+ *\n+ * Verify the following:\n+ * - @filp private data is an ap_matrix_mdev instance\n+ * - @filp is the instance opened when state transitioned from STOP to STOP_COPY\n+ * - @pos + @len does not cause integer overflow\n+ *\n+ * Returns: 0 if the parameters pass validation; otherwise returns an error\n+ */\n+static int validate_stop_copy_read_parms(struct file *filp, loff_t *pos,\n+\t\t\t\t\t size_t len)\n+{\n+\tstruct vfio_ap_migration_data *mig_data;\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tloff_t total_len;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (check_add_overflow((loff_t)len, *pos, \u0026total_len))\n+\t\treturn -EIO;\n+\n+\t/*\n+\t * matrix_mdev is guaranteed live here: vfio_ap_open_file_stream() took\n+\t * a vfio_device registration reference that is held until\n+\t * vfio_ap_release_mig_file() runs, so the embedding matrix_mdev cannot\n+\t * be freed while this file descriptor is open.\n+\t */\n+\tmatrix_mdev = filp-\u003eprivate_data;\n+\n+\tif (!matrix_mdev-\u003emig_data)\n+\t\treturn -ENODEV;\n+\n+\tmig_data = matrix_mdev-\u003emig_data;\n+\n+\tif (mig_data-\u003estop_copy_mig_file.filp != filp)\n+\t\treturn -EINVAL;\n+\n+\treturn 0;\n+}\n+\n+static size_t vfio_ap_config_size(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t\t  int *num_queues)\n+{\n+\tsize_t qinfo_size;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\t*num_queues = vfio_ap_mdev_get_num_queues(\u0026matrix_mdev-\u003eshadow_apcb);\n+\tqinfo_size = *num_queues * sizeof(struct vfio_ap_queue_info);\n+\n+\treturn qinfo_size + sizeof(struct vfio_ap_config);\n+}\n+\n+static int get_hardware_info_for_queue(const char *mdev_name,\n+\t\t\t\t       struct ap_tapq_hwinfo *hwinfo,\n+\t\t\t\t       unsigned long apqn)\n+{\n+\tstruct ap_queue_status status;\n+\n+\tstatus = ap_tapq(apqn, hwinfo);\n+\n+\tswitch (status.response_code) {\n+\tcase AP_RESPONSE_NORMAL:\n+\tcase AP_RESPONSE_RESET_IN_PROGRESS:\n+\tcase AP_RESPONSE_DECONFIGURED:\n+\tcase AP_RESPONSE_CHECKSTOPPED:\n+\tcase AP_RESPONSE_BUSY:\n+\t\t/* For all these RCs the tapq info should be available */\n+\t\treturn 0;\n+\tcase AP_RESPONSE_Q_NOT_AVAIL:\n+\t\tpr_err(\"vfio_ap_mdev %s: Failed to get hwinfo for queue %02lx.%04lx: TAPQ rc=%d\",\n+\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),\n+\t\t       status.response_code);\n+\t\treturn -ENODEV;\n+\tdefault:\n+\t\t/*\n+\t\t * Without a pending async error, the tapq info should be\n+\t\t * available\n+\t\t */\n+\t\tif (status.async)\n+\t\t\treturn 0;\n+\n+\t\tpr_err(\"vfio_ap_mdev %s:Failed to get hwinfo for queue %02lx.%04lx: TAPQ rc=%d\",\n+\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),\n+\t\t       status.response_code);\n+\t\treturn -EIO;\n+\t}\n+}\n+\n+static int vfio_ap_store_queue_info(const char *mdev_name,\n+\t\t\t\t    struct vfio_ap_config *ap_config)\n+{\n+\tstruct ap_tapq_hwinfo source_hwinfo;\n+\tunsigned long num_queues;\n+\tint ret;\n+\n+\t/*\n+\t * ap_tapq() is a hardware instruction that may take time to complete.\n+\t * It must be called without mdevs_lock held to avoid blocking other\n+\t * mdevs. The apqn list was already snapshotted into ap_config-\u003eqinfo[]\n+\t * by the caller under the lock.\n+\t */\n+\tfor (num_queues = 0; num_queues \u003c ap_config-\u003enum_queues; num_queues++) {\n+\t\tret = get_hardware_info_for_queue(mdev_name, \u0026source_hwinfo,\n+\t\t\t\t\t\t  ap_config-\u003eqinfo[num_queues].apqn);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\n+\t\tap_config-\u003eqinfo[num_queues].data = source_hwinfo.value;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int vfio_ap_get_config(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tunsigned long *apm, *aqm, apid, apqi;\n+\tstruct vfio_ap_config *ap_configuration;\n+\tconst char *mdev_name;\n+\tsize_t ap_config_size;\n+\tint num_queues;\n+\tint ret;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tap_config_size = vfio_ap_config_size(matrix_mdev, \u0026num_queues);\n+\n+\tap_configuration = kzalloc(ap_config_size, GFP_KERNEL_ACCOUNT);\n+\tif (!ap_configuration)\n+\t\treturn -ENOMEM;\n+\n+\t/*\n+\t * num_queues must be set before writing qinfo[] elements; the\n+\t * __counted_by(num_queues) annotation on qinfo[] causes the compiler to\n+\t * insert bounds checks that evaluate against ap_configuration-\u003enum_queues.\n+\t * Writing through qinfo[i] with num_queues still 0 would trap.\n+\t */\n+\tap_configuration-\u003enum_queues = num_queues;\n+\n+\tapm = matrix_mdev-\u003eshadow_apcb.apm;\n+\taqm = matrix_mdev-\u003eshadow_apcb.aqm;\n+\tnum_queues = 0;\n+\tfor_each_set_bit_inv(apid, apm, AP_DEVICES) {\n+\t\tfor_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {\n+\t\t\tap_configuration-\u003eqinfo[num_queues].apqn =\n+\t\t\t\tAP_MKQID(apid, apqi);\n+\t\t\tnum_queues += 1;\n+\t\t}\n+\t}\n+\tmemcpy(ap_configuration-\u003eadm, matrix_mdev-\u003eshadow_apcb.adm,\n+\t       sizeof(ap_configuration-\u003eadm));\n+\tmdev_name = dev_name(matrix_mdev-\u003evdev.dev);\n+\n+\tret = vfio_ap_store_queue_info(mdev_name, ap_configuration);\n+\tif (ret) {\n+\t\tkfree(ap_configuration);\n+\t\treturn ret;\n+\t}\n+\n+\tmatrix_mdev-\u003emig_data-\u003estop_copy_mig_file.ap_config = ap_configuration;\n+\tmatrix_mdev-\u003emig_data-\u003estop_copy_mig_file.config_sz = ap_config_size;\n+\n+\treturn 0;\n+}\n+\n+static ssize_t vfio_ap_stop_copy_read(struct file *filp, char __user *buf,\n+\t\t\t\t      size_t len, loff_t *pos)\n+{\n+\tstruct vfio_ap_migration_file *mig_file;\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tloff_t read_pos;\n+\tssize_t ret;\n+\n+\t/*\n+\t * When userspace calls read() with an explicit offset (pread), pos is\n+\t * non-NULL and the function rejects it with -ESPIPE (illegal seek). For\n+\t * normal read() calls, pos is NULL, so we'll use the file's internal\n+\t * position filp-\u003ef_pos\n+\t */\n+\tif (pos)\n+\t\treturn -ESPIPE;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tpos = \u0026filp-\u003ef_pos;\n+\n+\tret = validate_stop_copy_read_parms(filp, pos, len);\n+\tif (ret) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn ret;\n+\t}\n+\n+\tmatrix_mdev = filp-\u003eprivate_data;\n+\tmig_file = \u0026matrix_mdev-\u003emig_data-\u003estop_copy_mig_file;\n+\n+\tif (!mig_file-\u003eap_config) {\n+\t\tret = vfio_ap_get_config(matrix_mdev);\n+\t\tif (ret) {\n+\t\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\t\treturn ret;\n+\t\t}\n+\t}\n+\n+\t/*\n+\t * Compute the offset and clamped length fully under the lock so that\n+\t * concurrent read()s on this stream file each see a consistent view of\n+\t * the current position.  *pos is advanced here while we still hold the\n+\t * lock; copy_to_user() then uses the snapshot read_pos.  This prevents\n+\t * two threads from calculating the same offset and both copying the\n+\t * same region (or one reading past the end of the buffer).\n+\t */\n+\tif (*pos \u003e= mig_file-\u003econfig_sz) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn 0;\n+\t}\n+\n+\tlen = min_t(size_t, mig_file-\u003econfig_sz - *pos, len);\n+\tif (len == 0) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn 0;\n+\t}\n+\n+\tread_pos = *pos;\n+\t*pos += len;\n+\n+\t/*\n+\t * Drop the lock only for the copy_to_user().  The ap_config buffer is\n+\t * stable: it is allocated once in vfio_ap_get_config() and freed only\n+\t * in vfio_ap_release_mig_files() / vfio_ap_release_stop_copy_file(),\n+\t * both of which require mdevs_lock.  Since we already advanced *pos\n+\t * above, no other thread will compute an overlapping region.\n+\t */\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (copy_to_user(buf, (char *)mig_file-\u003eap_config + read_pos, len))\n+\t\treturn -EFAULT;\n+\n+\treturn len;\n+}\n+\n+static const struct file_operations vfio_ap_stop_copy_fops = {\n+\t.owner = THIS_MODULE,\n+\t.read = vfio_ap_stop_copy_read,\n+\t.compat_ioctl = compat_ptr_ioctl,\n+\t.release = vfio_ap_release_mig_file,\n+};\n+\n+static struct file *vfio_ap_open_file_stream(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t\t\t     const struct file_operations *fops,\n+\t\t\t\t\t     int flags)\n+{\n+\tstruct file *filp;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\t/*\n+\t * Pin the vfio_device registration so that matrix_mdev cannot be freed\n+\t * while the migration FD is still open. The matching put is in\n+\t * vfio_ap_release_mig_file().\n+\t */\n+\tif (!vfio_device_try_get_registration(\u0026matrix_mdev-\u003evdev))\n+\t\treturn ERR_PTR(-ENODEV);\n+\n+\tfilp = anon_inode_getfile(\"vfio_ap_mig_file\", fops, matrix_mdev, flags);\n+\tif (IS_ERR(filp)) {\n+\t\tvfio_device_put_registration(\u0026matrix_mdev-\u003evdev);\n+\t\treturn filp;\n+\t}\n+\n+\tstream_open(filp-\u003ef_inode, filp);\n+\n+\t/*\n+\t * Take a second reference on the file so the driver holds its own\n+\t * reference independent of the one consumed when the VFIO core\n+\t * installs the FD into the userspace file table. Without this,\n+\t * the driver's saved filp could be the only reference; an fput()\n+\t * during a device reset would prematurely destroy the file while\n+\t * the userspace FD still points to it.\n+\t */\n+\tget_file(filp);\n+\n+\treturn filp;\n+}\n+\n+static int validate_resuming_write_parms(struct file *filp,\n+\t\t\t\t\t size_t len, loff_t *pos)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tloff_t total_len;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (!len || *pos \u003c 0)\n+\t\treturn -EINVAL;\n+\n+\tif (check_add_overflow((loff_t)len, *pos, \u0026total_len))\n+\t\treturn -ERANGE;\n+\n+\tmatrix_mdev = filp-\u003eprivate_data;\n+\tif (!matrix_mdev || !matrix_mdev-\u003emig_data)\n+\t\treturn -ENODEV;\n+\n+\tif (filp != matrix_mdev-\u003emig_data-\u003eresuming_mig_file.filp)\n+\t\treturn -ENXIO;\n+\n+\t/*\n+\t * If the ap_config has not yet been allocated and the file position\n+\t * indicates this is not the first write, or the ap_config has been allocated\n+\t * but the file position indicates this is the first write, then this is an\n+\t * error condition.\n+\t */\n+\tif ((!matrix_mdev-\u003emig_data-\u003eresuming_mig_file.ap_config \u0026\u0026 *pos != 0) ||\n+\t    (matrix_mdev-\u003emig_data-\u003eresuming_mig_file.ap_config \u0026\u0026 *pos == 0))\n+\t\treturn -EFAULT;\n+\n+\t/*\n+\t * The first write must cover at least num_queues (the first field of\n+\t * struct vfio_ap_config) so that allocate_ap_config() can derive the\n+\t * correct allocation size.  A shorter first write would cause\n+\t * cfg_sz to be set to len, the completion check\n+\t * (write_pos + len == cfg_sz) would fire immediately, and\n+\t * do_post_copy_validation() would read qinfo[] from a buffer that\n+\t * is too small to contain it.\n+\t */\n+\tif (*pos == 0 \u0026\u0026 len \u003c offsetofend(struct vfio_ap_config, num_queues))\n+\t\treturn -EINVAL;\n+\n+\treturn 0;\n+}\n+\n+static ssize_t calculate_ap_config_size(unsigned int num_queues)\n+{\n+\tsize_t qinfo_size;\n+\n+\tif (num_queues \u003e MAX_AP_QUEUES)\n+\t\treturn -EINVAL;\n+\n+\tqinfo_size = num_queues * sizeof(struct vfio_ap_queue_info);\n+\treturn qinfo_size + sizeof(struct vfio_ap_config);\n+}\n+\n+/**\n+ * allocate_ap_config:\n+ *\n+ * Allocate storage for the source guest's AP configuration data sent from\n+ * userspace.\n+ *\n+ * @ap_config:\tThe location in which to store the pointer to the storage\n+ *\t\tallocated for the AP configuration data.\n+ * @buf:\tThe userspace buffer containing some or all of the source\n+ *\t\tguest's AP configuration data\n+ * @len:\tThe number of bytes of data to copy from @buf\n+ *\n+ * Returns:\tThe number of bytes of storage allocated for the config data or\n+ *\t\tan error:\n+ *\n+ *\t\t-EINVAL: len is 0, or num_queues exceeds the maximum (only checked\n+ *\t\t\t if @len covers the full vfio_ap_config header)\n+ *\t\t-EIO: failed to copy data from @buf\n+ *\t\t-ENOMEM: the allocation of storage failed\n+ */\n+static ssize_t allocate_ap_config(struct vfio_ap_config **ap_config,\n+\t\t\t\t  const char __user *buf, size_t len)\n+{\n+\tstruct vfio_ap_config tmp_ap_config;\n+\tssize_t config_size;\n+\n+\t/*\n+\t * validate_resuming_write_parms() guarantees the first write covers at\n+\t * least num_queues, so we can always derive the final allocation size\n+\t * here.\n+\t */\n+\tif (copy_from_user(\u0026tmp_ap_config, buf, min(len, sizeof(tmp_ap_config))))\n+\t\treturn -EIO;\n+\n+\tconfig_size = calculate_ap_config_size(tmp_ap_config.num_queues);\n+\tif (config_size \u003c 0)\n+\t\treturn config_size;\n+\n+\t/*\n+\t * Use kvzalloc so that large configurations can fall back to vmalloc\n+\t * rather than failing a high-order contiguous physical allocation.\n+\t */\n+\t*ap_config = kvzalloc(config_size, GFP_KERNEL_ACCOUNT);\n+\tif (!*ap_config)\n+\t\treturn -ENOMEM;\n+\n+\treturn config_size;\n+}\n+\n+/**\n+ * qdev_is_bound_to_vfio_ap:\n+ *\n+ * Query to determine whether a queue with the specified APQN is available on\n+ * the host system and bound to the vfio_ap device driver.\n+ *\n+ * @apqn: The APQN of the queue device being queried\n+ *\n+ * Returns: True if there is a queue device with the specified @apqn installed\n+ *\t    in the system and is bound to the vfio_ap device driver; otherwise,\n+ *\t    returns false.\n+ */\n+static bool qdev_is_bound_to_vfio_ap(unsigned int apqn)\n+{\n+\tstruct ap_queue *queue;\n+\tbool is_bound = true;\n+\n+\tqueue = ap_get_qdev(apqn);\n+\tif (!queue)\n+\t\treturn false;\n+\n+\tif (queue-\u003eap_dev.device.driver != \u0026matrix_dev-\u003evfio_ap_drv-\u003edriver)\n+\t\tis_bound = false;\n+\n+\tput_device(\u0026queue-\u003eap_dev.device);\n+\n+\treturn is_bound;\n+}\n+\n+/**\n+ * queues_available:\n+ *\n+ * Query whether each queue from the source guest's AP configuration is\n+ * available and bound to the vfio_ap device driver; if not, log an error\n+ * message.\n+ *\n+ * @mdev_name:\t   The mdev name to use in error messages\n+ * @source_config: The object specifying the source guest's AP configuration\n+ *\n+ * Returns: true if each queue identified in @source_config is available and\n+ *\t    bound to the vfio_ap device driver; otherwise, returns false.\n+ */\n+static bool queues_available(const char *mdev_name,\n+\t\t\t     struct vfio_ap_config *source_config)\n+{\n+\tunsigned long apqn;\n+\tbool ret = true;\n+\n+\tfor (int i = 0; i \u003c source_config-\u003enum_queues; i++) {\n+\t\tapqn = source_config-\u003eqinfo[i].apqn;\n+\n+\t\t/*\n+\t\t * Find the queue device bound to the vfio_ap device driver. If it is\n+\t\t * not found, log an error and continue so users see all problems\n+\t\t * at once, not one-at-a-time through retries of the migration.\n+\t\t */\n+\t\tif (!qdev_is_bound_to_vfio_ap(apqn)) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Queue %02lx.%04lx not available to vfio_ap driver on target host\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t\tret = false;\n+\t\t}\n+\t}\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * control_domains_available\n+ *\n+ * Query whether each control domain specified in the source guest's AP\n+ * configuration is installed in the host system.\n+ *\n+ * @mdev_name:\t\tThe name of the mdev to use when logging messages\n+ * @source_config:\tThe object specifying the source guest's AP config\n+ *\n+ * Returns:\tTrue if each control domain is installed; otherwise, logs an\n+ *\t\terror message for each unavailable control domain and returns\n+ *\t\tfalse.\n+ */\n+static bool control_domains_available(const char *mdev_name,\n+\t\t\t\t      struct vfio_ap_config *source_config)\n+{\n+\tunsigned long domain_num;\n+\tbool available = true;\n+\n+\tfor_each_set_bit_inv(domain_num, (unsigned long *)source_config-\u003eadm,\n+\t\t\t     AP_DOMAINS) {\n+\t\tif (!ap_test_config_ctrl_domain(domain_num)) {\n+\t\t\tpr_err(\"vfio_ap_mdev: %s: Control domain %04lx not available on the destination host\",\n+\t\t\t       mdev_name, domain_num);\n+\t\t\tavailable = false;\n+\t\t}\n+\t}\n+\n+\treturn available;\n+}\n+\n+static void report_facilities_compatibility(const char *mdev_name,\n+\t\t\t\t\t    unsigned long apqn,\n+\t\t\t\t\t    struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t\t    struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (src_hwinfo-\u003eapsc != target_hwinfo-\u003eapsc) {\n+\t\tif (src_hwinfo-\u003eapsc) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: APSC facility installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: APSC facility not installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: APSC facility not installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s APSC facility installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+\n+\tif (src_hwinfo-\u003emex4k != target_hwinfo-\u003emex4k) {\n+\t\tif (src_hwinfo-\u003emex4k) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: mex4k facility installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: mex4k facility not installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: mex4k facility not installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: mex4k facility installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+\n+\tif (src_hwinfo-\u003ecrt4k != target_hwinfo-\u003ecrt4k) {\n+\t\tif (src_hwinfo-\u003ecrt4k) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: crt4k facility installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: crt4k facility not installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: crt4k facility not installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: crt4k facility installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+}\n+\n+static void report_mode_compatibility(const char *mdev_name,\n+\t\t\t\t      unsigned long apqn,\n+\t\t\t\t      struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t      struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (src_hwinfo-\u003ecca != target_hwinfo-\u003ecca) {\n+\t\tif (src_hwinfo-\u003ecca) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Coprocessor-mode facility installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Coprocessor-mode  facility not installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Coprocessor-mode facility not installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Coprocessor-mode  facility installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+\n+\tif (src_hwinfo-\u003eaccel != target_hwinfo-\u003eaccel) {\n+\t\tif (src_hwinfo-\u003eaccel) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Accelerator-mode facility installed source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Accelerator-mode facility not installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Accelerator-mode facility not installed source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Accelerator-mode facility installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+\n+\tif (src_hwinfo-\u003eep11 != target_hwinfo-\u003eep11) {\n+\t\tif (src_hwinfo-\u003eep11) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: XCP-mode facility installed source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: XCP-mode facility not installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: XCP-mode facility not installed source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: XCP-mode facility installed target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+}\n+\n+static void report_apxa_compatibility(const char *mdev_name,\n+\t\t\t\t      unsigned long apqn,\n+\t\t\t\t      struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t      struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (src_hwinfo-\u003eapxa != target_hwinfo-\u003eapxa) {\n+\t\tif (src_hwinfo-\u003eapxa) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: AP-extended-addressing (APXA) facility installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: AP-extended-addressing (APXA) facility not installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: AP-extended-addressing (APXA) facility not installed in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: AP-extended-addressing (APXA) facility installed in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+}\n+\n+static void report_slcf_compatibility(const char *mdev_name,\n+\t\t\t\t      unsigned long apqn,\n+\t\t\t\t      struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t      struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (src_hwinfo-\u003eslcf != target_hwinfo-\u003eslcf) {\n+\t\tif (src_hwinfo-\u003eslcf) {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Stateless-command-filtering (SLCF) available in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Stateless-command-filtering (SLCF) not available in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t} else {\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Stateless-command-filtering (SLCF) not available in source queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\t\tpr_err(\"vfio_ap_mdev %s: Stateless-command-filtering (SLCF) available in target queue %02lx.%04lx\\n\",\n+\t\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t\t}\n+\t}\n+}\n+\n+static void report_bs_compatibility(const char *mdev_name,\n+\t\t\t\t    unsigned long apqn,\n+\t\t\t\t    struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t    struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\t/*\n+\t * The BS field on both the source and destination must be 0, so if one\n+\t * of them is not, then report an error.\n+\t */\n+\tif (src_hwinfo-\u003ebs || target_hwinfo-\u003ebs) {\n+\t\tpr_err(\"vfio_ap_mdev %s: Bind/associate state for source (%01x) and target (%01x) queue %02lx.%04lx must be 0\\n\",\n+\t\t       mdev_name, src_hwinfo-\u003ebs, target_hwinfo-\u003ebs,\n+\t\t       AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t}\n+}\n+\n+static void report_aptype_compatibility(const char *mdev_name,\n+\t\t\t\t\tunsigned long apqn,\n+\t\t\t\t\tstruct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t\tstruct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (src_hwinfo-\u003eat \u003e target_hwinfo-\u003eat) {\n+\t\tpr_err(\"vfio_ap_mdev %s: AP type of source (%02x) not compatible with target (%02x)\\n\",\n+\t\t       mdev_name, src_hwinfo-\u003eat, target_hwinfo-\u003eat);\n+\t}\n+}\n+\n+static bool classes_compatible(struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t       struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tunsigned long src_native, target_native;\n+\n+\tsrc_native = src_hwinfo-\u003eclass \u0026 CLASSIFICATION_NATIVE_FCN_MASK;\n+\ttarget_native = target_hwinfo-\u003eclass \u0026 CLASSIFICATION_NATIVE_FCN_MASK;\n+\n+\t/*\n+\t * If the source queue has full native card function and the\n+\t * target queue has only stateless functions available, then\n+\t * there may be instructions that will not execute on the\n+\t * target queue. This shall be reported as an error.\n+\t *\n+\t * If the source queue has only stateless card functions and the\n+\t * target queue has full native card function available, then\n+\t * we are okay because the target queue can run all stateless card\n+\t * functions.\n+\t */\n+\treturn (src_native != target_native) ? !src_native : true;\n+}\n+\n+static void report_class_compatibility(const char *mdev_name,\n+\t\t\t\t       unsigned long apqn,\n+\t\t\t\t       struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t       struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tif (!classes_compatible(src_hwinfo, target_hwinfo)) {\n+\t\tpr_err(\"vfio_ap_mdev %s: Full native card function available on source queue %02lx.%04lx\\n\",\n+\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\t\tpr_err(\"vfio_ap_mdev %s: Only stateless functions available on target queue %02lx.%04lx\\n\",\n+\t\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\t}\n+}\n+\n+/*\n+ * Log a device error reporting that migration failed due to queue\n+ * incompatibilities followed by a device error for each incompatible feature.\n+ */\n+static void report_qinfo_incompatibilities(const char *mdev_name,\n+\t\t\t\t\t   unsigned long apqn,\n+\t\t\t\t\t   struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t\t   struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tpr_err(\"vfio_ap_mdev %s: Migration failed: Source and target queue (%02lx.%04lx) not compatible\\n\",\n+\t       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));\n+\n+\treport_facilities_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_mode_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_apxa_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_slcf_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_aptype_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_bs_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\treport_class_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+}\n+\n+/**\n+ * queue_hardware_info_is_compatible:\n+ *\n+ * Verify whether the hardware information for a source queue is compatible with\n+ * the hardware info for the corresponding queue on this system.\n+ *\n+ * In order to be compatible, the hardware information for each queue must\n+ * meet the following requirements:\n+ *\n+ * 1. The hardware facilities bits much match\n+ * 2. The AP type of the source queue must be the same as or older than that\n+ *    of the target queue (target is backwards compatible)\n+ * 3. The classification bits must indicate:\n+ *    - Both queues have full native card function or both have stateless\n+ *      functions available\n+ *    - If the classification bits don't match, then the only acceptable\n+ *      configuration is stateless functions for the source queue and\n+ *      full native function for the target queue\n+ * 4. The BS bits for both queues must be 0 (Queue usable for all messages\n+ *    supported by the adapter)\n+ *\n+ * @mdev_name:\tThe mdev name to use in error messages\n+ * @apqn:\tThe APQN for the queues\n+ * @src_hwinfo: The hardware info for the source queue\n+ * @target_hwinfo: The hardware info for the corresponding queue on this system\n+ *\n+ * Returns: true if the hardware info for the two queues is compatible;\n+ *          otherwise, returns false.\n+ */\n+static bool queue_hardware_info_is_compatible(const char *mdev_name,\n+\t\t\t\t\t      unsigned long apqn,\n+\t\t\t\t\t      struct ap_tapq_hwinfo *src_hwinfo,\n+\t\t\t\t\t      struct ap_tapq_hwinfo *target_hwinfo)\n+{\n+\tunsigned long src_bits, target_bits;\n+\n+\tsrc_bits = src_hwinfo-\u003evalue \u0026 QINFO_DATA_MASK;\n+\ttarget_bits = target_hwinfo-\u003evalue \u0026 QINFO_DATA_MASK;\n+\n+\t/* If all bits match the queues are compatible */\n+\tif (src_bits == target_bits \u0026\u0026\n+\t    (src_hwinfo-\u003ebs == 0 \u0026\u0026 target_hwinfo-\u003ebs == 0))\n+\t\treturn true;\n+\n+\tif (src_hwinfo-\u003eapsc  == target_hwinfo-\u003eapsc     \u0026\u0026\n+\t    src_hwinfo-\u003emex4k == target_hwinfo-\u003emex4k    \u0026\u0026\n+\t    src_hwinfo-\u003ecrt4k == target_hwinfo-\u003ecrt4k    \u0026\u0026\n+\t    src_hwinfo-\u003ecca   == target_hwinfo-\u003ecca      \u0026\u0026\n+\t    src_hwinfo-\u003eaccel == target_hwinfo-\u003eaccel    \u0026\u0026\n+\t    src_hwinfo-\u003eep11  == target_hwinfo-\u003eep11     \u0026\u0026\n+\t    src_hwinfo-\u003eslcf  == target_hwinfo-\u003eslcf     \u0026\u0026\n+\t    src_hwinfo-\u003eapxa  == target_hwinfo-\u003eapxa     \u0026\u0026\n+\t    src_hwinfo-\u003eat    \u003c= target_hwinfo-\u003eat       \u0026\u0026\n+\t    classes_compatible(src_hwinfo, target_hwinfo) \u0026\u0026\n+\t    (src_hwinfo-\u003ebs == 0 \u0026\u0026 target_hwinfo-\u003ebs == 0))\n+\t\treturn true;\n+\n+\treport_qinfo_incompatibilities(mdev_name, apqn, src_hwinfo, target_hwinfo);\n+\n+\treturn false;\n+}\n+\n+/**\n+ * verify_ap_configs_are_compatible:\n+ *\n+ * Verifies that the queues in the source guest's AP configuration are\n+ * compatible with the corresponding queues on this system.\n+ *\n+ * @mdev_name:\t   The mdev name to use in error messages\n+ * @source_config: The object specifying the source guest's AP configuration\n+ *\n+ * Returns: an error indicating either a failure to retrieve a queue's\n+ *\t\t\thardware information or one or more source queues are not\n+ *\t\t\tcompatible with the corresponding queue on this system; otherwise,\n+ *\t\t\treturns zero to indicate compatibility.\n+ */\n+static int verify_ap_configs_are_compatible(const char *mdev_name,\n+\t\t\t\t\t    struct vfio_ap_config *source_config)\n+{\n+\tstruct ap_tapq_hwinfo src_hwinfo, dest_hwinfo;\n+\tunsigned long apqn;\n+\tint ret = 0, rc;\n+\n+\tfor (int i = 0; i \u003c source_config-\u003enum_queues; i++) {\n+\t\tapqn = source_config-\u003eqinfo[i].apqn;\n+\n+\t\t/*\n+\t\t * If we can't get the hardware info for a particular queue, then let's\n+\t\t * capture the function return code and continue so we can log all\n+\t\t * errors to aid in debugging of migration.\n+\t\t */\n+\t\trc = get_hardware_info_for_queue(mdev_name, \u0026dest_hwinfo, apqn);\n+\t\tif (rc) {\n+\t\t\tret = rc;\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tsrc_hwinfo.value =  source_config-\u003eqinfo[i].data;\n+\n+\t\tif (!queue_hardware_info_is_compatible(mdev_name, apqn,\n+\t\t\t\t\t\t       \u0026src_hwinfo,\n+\t\t\t\t\t\t       \u0026dest_hwinfo))\n+\t\t\tret = -EINVAL;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+static int do_post_copy_validation(const char *mdev_name,\n+\t\t\t\t   struct vfio_ap_config *source_config)\n+{\n+\tif (!queues_available(mdev_name, source_config))\n+\t\treturn -ENODEV;\n+\n+\tif (!control_domains_available(mdev_name, source_config))\n+\t\treturn -ENODEV;\n+\n+\treturn verify_ap_configs_are_compatible(mdev_name, source_config);\n+}\n+\n+/**\n+ * setup_ap_matrix_from_ap_config:\n+ *\n+ * Set the bits corresponding to the adapters, domains and control domains\n+ * from the source guest's AP configuration into an ap_matrix object to be\n+ * used to update the destination guest to run on this host.\n+ *\n+ * @ap_config:\t\tThe source guest's AP configuration\n+ * @guest_matrix:\tThe object to be used to update the destination guest's\n+ *\t\t\tAP configuration\n+ */\n+static void setup_ap_matrix_from_ap_config(struct vfio_ap_config *ap_config,\n+\t\t\t\t\t   struct ap_matrix *guest_matrix)\n+{\n+\tstruct ap_config_info host_config_info = { 0 };\n+\tunsigned long apid, apqi, *guest_adm;\n+\tstruct vfio_ap_queue_info qinfo;\n+\n+\tap_qci(\u0026host_config_info);\n+\t/*\n+\t * Zero the bitmaps before calling vfio_ap_matrix_init(), which only\n+\t * sets the apm_max/aqm_max/adm_max scalar fields and leaves the bitmap\n+\t * arrays untouched.  Without this, stack garbage in guest_matrix-\u003eapm,\n+\t * -\u003eaqm, and -\u003eadm would grant the destination guest access to\n+\t * arbitrary unassigned queues and control domains.\n+\t */\n+\tmemset(guest_matrix-\u003eapm, 0, sizeof(guest_matrix-\u003eapm));\n+\tmemset(guest_matrix-\u003eaqm, 0, sizeof(guest_matrix-\u003eaqm));\n+\tmemset(guest_matrix-\u003eadm, 0, sizeof(guest_matrix-\u003eadm));\n+\tvfio_ap_matrix_init(\u0026host_config_info, guest_matrix);\n+\n+\tfor (int i = 0; i \u003c ap_config-\u003enum_queues; i++) {\n+\t\tqinfo = ap_config-\u003eqinfo[i];\n+\t\tapid = AP_QID_CARD(qinfo.apqn);\n+\t\tapqi = AP_QID_QUEUE(qinfo.apqn);\n+\n+\t\tif (!test_bit_inv(apid, guest_matrix-\u003eapm))\n+\t\t\tset_bit_inv(apid, guest_matrix-\u003eapm);\n+\t\tif (!test_bit_inv(apqi, guest_matrix-\u003eaqm))\n+\t\t\tset_bit_inv(apqi, guest_matrix-\u003eaqm);\n+\t}\n+\n+\tguest_adm = (unsigned long *)ap_config-\u003eadm;\n+\tfor_each_set_bit_inv(apqi, guest_adm, AP_DOMAINS) {\n+\t\tif (!test_bit_inv(apqi, guest_matrix-\u003eadm))\n+\t\t\tset_bit_inv(apqi, guest_matrix-\u003eadm);\n+\t}\n+}\n+\n+static ssize_t vfio_ap_resuming_write(struct file *filp, const char __user *buf,\n+\t\t\t\t      size_t len, loff_t *pos)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tstruct vfio_ap_config *ap_config;\n+\tstruct ap_matrix guest_matrix;\n+\tbool new_allocation = false;\n+\tloff_t write_pos;\n+\tssize_t ret = 0, cfg_sz;\n+\tconst char *mdev_name;\n+\n+\t/*\n+\t * When userspace calls write() with an explicit offset (pwrite), pos is\n+\t * non-NULL and the function rejects it with -ESPIPE (illegal seek). For\n+\t * normal write() calls, pos is NULL, so we'll use the file's internal\n+\t * position filp-\u003ef_pos\n+\t */\n+\tif (pos)\n+\t\treturn -ESPIPE;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tpos = \u0026filp-\u003ef_pos;\n+\n+\tret = validate_resuming_write_parms(filp, len, pos);\n+\tif (ret) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn ret;\n+\t}\n+\n+\tmatrix_mdev = filp-\u003eprivate_data;\n+\tmdev_name = dev_name(matrix_mdev-\u003evdev.dev);\n+\n+\t/*\n+\t * If this is the first write operation, allocate storage for the AP\n+\t * configuration sized to fit the full payload (validate_resuming_write_parms\n+\t * guarantees num_queues is present in buf).  For subsequent writes the\n+\t * buffer is already correctly sized; just reuse it.\n+\t */\n+\tif (*pos == 0) {\n+\t\tret = allocate_ap_config(\u0026ap_config, buf, len);\n+\t\tif (ret \u003c 0) {\n+\t\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\t\treturn ret;\n+\t\t}\n+\n+\t\tcfg_sz = ret;\n+\t\tnew_allocation = true;\n+\t} else {\n+\t\tap_config = matrix_mdev-\u003emig_data-\u003eresuming_mig_file.ap_config;\n+\t\tcfg_sz = matrix_mdev-\u003emig_data-\u003eresuming_mig_file.config_sz;\n+\t}\n+\n+\tif (*pos + len \u003e cfg_sz) {\n+\t\tif (new_allocation)\n+\t\t\tkvfree(ap_config);\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn -EIO;\n+\t}\n+\n+\t/*\n+\t * Snapshot and advance *pos under the lock before dropping it for\n+\t * copy_from_user().  This prevents concurrent write()s on the same\n+\t * stream file from computing the same destination offset and clobbering\n+\t * each other's data or racing to reassign mig_data-\u003eresuming_mig_file.\n+\t */\n+\twrite_pos = *pos;\n+\t*pos += len;\n+\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (copy_from_user((char *)ap_config + write_pos, buf, len)) {\n+\t\tif (new_allocation)\n+\t\t\tkvfree(ap_config);\n+\t\treturn -EIO;\n+\t}\n+\n+\t/* Check if we've completed writing the entire configuration */\n+\tif (write_pos + len == cfg_sz) {\n+\t\t/*\n+\t\t * do_post_copy_validation() calls ap_tapq() which is a slow\n+\t\t * hardware instruction.  Run it before acquiring the update\n+\t\t * locks to avoid holding guests_lock, kvm-\u003elock, and\n+\t\t * mdevs_lock across the hardware calls.\n+\t\t */\n+\t\tret = do_post_copy_validation(mdev_name, ap_config);\n+\t\tif (ret \u003c 0) {\n+\t\t\tif (new_allocation)\n+\t\t\t\tkvfree(ap_config);\n+\t\t\treturn ret;\n+\t\t}\n+\n+\t\tsetup_ap_matrix_from_ap_config(ap_config, \u0026guest_matrix);\n+\n+\t\tmutex_lock(\u0026ap_attr_mutex);\n+\t\tget_update_locks_for_mdev(matrix_mdev);\n+\n+\t\t/*\n+\t\t * Verify the device wasn't closed while mdevs_lock was dropped\n+\t\t * for the copy_from_user and do_post_copy_validation above.\n+\t\t * get_update_locks_for_mdev() reacquires mdevs_lock.\n+\t\t */\n+\t\tif (!matrix_mdev-\u003emig_data) {\n+\t\t\trelease_update_locks_for_mdev(matrix_mdev);\n+\t\t\tmutex_unlock(\u0026ap_attr_mutex);\n+\t\t\tif (new_allocation)\n+\t\t\t\tkvfree(ap_config);\n+\t\t\treturn -ENODEV;\n+\t\t}\n+\n+\t\tret = vfio_ap_set_new_guest_config(matrix_mdev, \u0026guest_matrix);\n+\n+\t\trelease_update_locks_for_mdev(matrix_mdev);\n+\t\tmutex_unlock(\u0026ap_attr_mutex);\n+\n+\t\tif (ret) {\n+\t\t\tif (new_allocation)\n+\t\t\t\tkvfree(ap_config);\n+\t\t\treturn ret;\n+\t\t}\n+\t}\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\t/*\n+\t * Re-read mig_data under the lock; the device could have been closed\n+\t * concurrently while the lock was dropped for copy_from_user().\n+\t */\n+\tif (!matrix_mdev-\u003emig_data) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\tif (new_allocation)\n+\t\t\tkvfree(ap_config);\n+\t\treturn -ENODEV;\n+\t}\n+\tif (new_allocation)\n+\t\tkvfree(matrix_mdev-\u003emig_data-\u003eresuming_mig_file.ap_config);\n+\tmatrix_mdev-\u003emig_data-\u003eresuming_mig_file.ap_config = ap_config;\n+\tmatrix_mdev-\u003emig_data-\u003eresuming_mig_file.config_sz = cfg_sz;\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn len;\n+}\n+\n+static const struct file_operations vfio_ap_resume_fops = {\n+\t.owner = THIS_MODULE,\n+\t.write = vfio_ap_resuming_write,\n+\t.release = vfio_ap_release_mig_file,\n+};\n+\n+static struct file *vfio_ap_resuming_init(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn vfio_ap_open_file_stream(matrix_mdev, \u0026vfio_ap_resume_fops, O_WRONLY);\n+}\n+\n+static struct file *\n+vfio_ap_transition_to_state(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t    enum vfio_device_mig_state new_state)\n+{\n+\tstruct vfio_ap_migration_data *mig_data;\n+\tenum vfio_device_mig_state cur_state;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\tmig_data = matrix_mdev-\u003emig_data;\n+\tcur_state = mig_data-\u003emig_state;\n+\tdev_dbg(matrix_mdev-\u003evdev.dev, \"%s: %d -\u003e %d\\n\", __func__, cur_state,\n+\t\tnew_state);\n+\n+\t/*\n+\t * Begins the process of saving the vfio device state by creating and\n+\t * returning a streaming data_fd to be used to read out the internal\n+\t * state of the vfio-ap device on the source host.\n+\t */\n+\tif (cur_state == VFIO_DEVICE_STATE_STOP \u0026\u0026\n+\t    new_state == VFIO_DEVICE_STATE_STOP_COPY) {\n+\t\tstruct file *filp = vfio_ap_open_file_stream(matrix_mdev,\n+\t\t\t\t\t\t\t     \u0026vfio_ap_stop_copy_fops,\n+\t\t\t\t\t\t\t     O_RDONLY);\n+\t\tif (IS_ERR(filp))\n+\t\t\treturn ERR_CAST(filp);\n+\n+\t\tmig_data-\u003estop_copy_mig_file.filp = filp;\n+\n+\t\treturn filp;\n+\t}\n+\n+\t/*\n+\t * Begins the process of restoring the vfio device state by creating and\n+\t * returning a streaming data_fd to be used to read in the internal\n+\t * state of the vfio-ap device on the destination host.\n+\t */\n+\tif (cur_state == VFIO_DEVICE_STATE_STOP \u0026\u0026\n+\t    new_state == VFIO_DEVICE_STATE_RESUMING) {\n+\t\tstruct file *filp = vfio_ap_resuming_init(matrix_mdev);\n+\n+\t\tif (IS_ERR(filp))\n+\t\t\treturn ERR_CAST(filp);\n+\n+\t\tmig_data-\u003eresuming_mig_file.filp = filp;\n+\t\treturn filp;\n+\t}\n+\n+\t/*\n+\t * Terminates the data transfer session of the vfio-ap device state\n+\t * between the source and target hosts. Since the vfio-ap device does\n+\t * not virtualize a DMA device, there is no internal device state to\n+\t * incorporate into the vfio-ap device on the target.\n+\t */\n+\tif ((cur_state == VFIO_DEVICE_STATE_RESUMING \u0026\u0026\n+\t     new_state == VFIO_DEVICE_STATE_STOP) ||\n+\t    (cur_state == VFIO_DEVICE_STATE_STOP_COPY \u0026\u0026\n+\t     new_state == VFIO_DEVICE_STATE_STOP)) {\n+\t\treturn NULL;\n+\t}\n+\n+\t/*\n+\t * These states indicate migration has either not been initiated or\n+\t * has completed and the vfio-ap device is operating normally. Since the\n+\t * vfio-ap device does not virtualize a DMA device, there is no internal\n+\t * device state to incorporate into the vfio-ap device on the target.\n+\t */\n+\tif ((cur_state == VFIO_DEVICE_STATE_STOP \u0026\u0026\n+\t     new_state == VFIO_DEVICE_STATE_RUNNING) ||\n+\t    (cur_state == VFIO_DEVICE_STATE_RUNNING \u0026\u0026\n+\t     new_state == VFIO_DEVICE_STATE_STOP)) {\n+\t\treturn NULL;\n+\t}\n+\n+\t/* vfio_mig_get_next_state() does not use arcs other than the above */\n+\tWARN_ON(true);\n+\n+\treturn ERR_PTR(-EINVAL);\n+}\n+\n+static struct file *vfio_ap_set_state(struct vfio_device *vdev,\n+\t\t\t\t      enum vfio_device_mig_state  new_state)\n+{\n+\tint ret;\n+\tstruct file *filp = NULL;\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tenum vfio_device_mig_state next_state;\n+\tstruct vfio_ap_migration_data *mig_data;\n+\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tif (ap_is_se_guest()) {\n+\t\tdev_err_once(matrix_mdev-\u003evdev.dev,\n+\t\t\t     \"Migration not allowed from or to a Secure Execution guest\\n\");\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\treturn ERR_PTR(-EPERM);\n+\t}\n+\n+\tmig_data = matrix_mdev-\u003emig_data;\n+\n+\t/*\n+\t * The mig_data pointer is set in the vfio_ap_init_migration_data\n+\t * function which is called when the vfio-ap device fd is opened.\n+\t * Since the implicit pre-open state is RUNNING, a request to set\n+\t * RUNNING is a no-op. Any other state transition is invalid before\n+\t * open_device.\n+\t */\n+\tif (!mig_data) {\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\tif (new_state == VFIO_DEVICE_STATE_RUNNING)\n+\t\t\treturn NULL;\n+\t\treturn ERR_PTR(-ENODEV);\n+\t}\n+\n+\tdev_dbg(vdev-\u003edev, \"%s -\u003e %d\\n\", __func__, new_state);\n+\n+\twhile (mig_data-\u003emig_state != VFIO_DEVICE_STATE_ERROR \u0026\u0026\n+\t       mig_data-\u003emig_state != new_state) {\n+\t\tret = vfio_mig_get_next_state(vdev, mig_data-\u003emig_state,\n+\t\t\t\t\t      new_state, \u0026next_state);\n+\t\tif (ret) {\n+\t\t\tfilp = ERR_PTR(ret);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tfilp = vfio_ap_transition_to_state(matrix_mdev, next_state);\n+\t\tif (IS_ERR(filp))\n+\t\t\tbreak;\n+\n+\t\tmig_data-\u003emig_state = next_state;\n+\n+\t\tif (WARN_ON(filp \u0026\u0026 new_state != next_state)) {\n+\t\t\tfput(filp);\n+\t\t\tfilp = ERR_PTR(-EINVAL);\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn filp;\n+}\n+\n+static int vfio_ap_get_state(struct vfio_device *vdev,\n+\t\t\t     enum vfio_device_mig_state  *current_state)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tstruct vfio_ap_migration_data *mig_data;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\tmig_data =  matrix_mdev-\u003emig_data;\n+\n+\t/*\n+\t * The mig_data pointer is set in the vfio_ap_init_migration_data\n+\t * function which is called when the vfio-ap device fd is opened.\n+\t * If mig_data is NULL, report RUNNING as the implicit pre-open state\n+\t * so userspace doesn't need to perform any state transition before the\n+\t * device becomes active.\n+\t */\n+\t*current_state = (mig_data) ? mig_data-\u003emig_state :\n+\t\t\t\t      VFIO_DEVICE_STATE_RUNNING;\n+\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn 0;\n+}\n+\n+static int vfio_ap_get_data_size(struct vfio_device *vdev,\n+\t\t\t\t unsigned long *stop_copy_length)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tsize_t qinfo_sz;\n+\tint num_queues;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\tnum_queues = vfio_ap_mdev_get_num_queues(\u0026matrix_mdev-\u003eshadow_apcb);\n+\tqinfo_sz = num_queues * sizeof(struct vfio_ap_queue_info);\n+\t*stop_copy_length = qinfo_sz + sizeof(struct vfio_ap_config);\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn 0;\n+}\n+\n+static const struct vfio_migration_ops vfio_ap_migration_ops = {\n+\t.migration_set_state = vfio_ap_set_state,\n+\t.migration_get_state = vfio_ap_get_state,\n+\t.migration_get_data_size = vfio_ap_get_data_size,\n+};\n+\n+/**\n+ * vfio_ap_init_migrations_capabilities - initialize migration capabilities\n+ *\n+ * @matrix_mdev: pointer to object containing the mdev state\n+ */\n+void vfio_ap_init_migration_capabilities(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\t/* Live guest migration is not supported for SE guests */\n+\tif (ap_is_se_guest())\n+\t\treturn;\n+\n+\tmatrix_mdev-\u003evdev.migration_flags = VFIO_MIGRATION_STOP_COPY;\n+\tmatrix_mdev-\u003evdev.mig_ops = \u0026vfio_ap_migration_ops;\n+}\n+\n+/**\n+ * vfio_ap_init_migration_data - initialize migration data and functions\n+ *\n+ * @matrix_mdev: pointer to object containing the mdev state\n+ *\n+ * Return: zero if initialization is successful; otherwise, returns a error.\n+ */\n+int vfio_ap_init_migration_data(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tstruct vfio_ap_migration_data *mig_data;\n+\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tmig_data = kzalloc_obj(struct vfio_ap_migration_data, GFP_KERNEL);\n+\tif (!mig_data)\n+\t\treturn -ENOMEM;\n+\n+\tmig_data-\u003emig_state = VFIO_DEVICE_STATE_RUNNING;\n+\tmatrix_mdev-\u003emig_data = mig_data;\n+\n+\treturn 0;\n+}\n+\n+static void vfio_ap_release_mig_files(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tstruct vfio_ap_migration_data *mig_data;\n+\n+\t/*\n+\t * The fput call does not call .release synchronously while the\n+\t * mdevs_lock mutex is held, so there is no problem with incurring a\n+\t * deadlock situation if fput is executed in this function.\n+\t */\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tmig_data = matrix_mdev-\u003emig_data;\n+\tif (!mig_data)\n+\t\treturn;\n+\n+\tif (mig_data-\u003estop_copy_mig_file.filp) {\n+\t\tfput(mig_data-\u003estop_copy_mig_file.filp);\n+\t\tmig_data-\u003estop_copy_mig_file.filp = NULL;\n+\t}\n+\n+\tkfree(mig_data-\u003estop_copy_mig_file.ap_config);\n+\tmig_data-\u003estop_copy_mig_file.ap_config = NULL;\n+\tmig_data-\u003estop_copy_mig_file.config_sz = 0;\n+\n+\tif (mig_data-\u003eresuming_mig_file.filp) {\n+\t\tfput(mig_data-\u003eresuming_mig_file.filp);\n+\t\tmig_data-\u003eresuming_mig_file.filp = NULL;\n+\t}\n+\n+\tkvfree(mig_data-\u003eresuming_mig_file.ap_config);\n+\tmig_data-\u003eresuming_mig_file.ap_config = NULL;\n+\tmig_data-\u003eresuming_mig_file.config_sz = 0;\n+}\n+\n+/**\n+ * vfio_ap_release_migration_data: reclaim private migration data\n+ *\n+ * @vdev: pointer to the mdev\n+ */\n+void vfio_ap_release_migration_data(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (!matrix_mdev-\u003emig_data)\n+\t\treturn;\n+\n+\t/*\n+\t * Drop the driver's get_file() references on any open migration FDs\n+\t * and free the associated ap_config buffers before freeing mig_data.\n+\t * This ensures that if the device is closed while a migration FD is\n+\t * still held by userspace, vfio_ap_release_mig_file() will see\n+\t * mig_data == NULL and skip the cleanup (the fput() here will\n+\t * eventually trigger .release, but mig_data is gone by then).\n+\t */\n+\tvfio_ap_release_mig_files(matrix_mdev);\n+\tkfree(matrix_mdev-\u003emig_data);\n+\tmatrix_mdev-\u003emig_data = NULL;\n+}\n+\n+/**\n+ * vfio_ap_reset_migration_state - Reset the vfio-ap migration state\n+ *\n+ * @matrix_mdev: pointer to the object maintaining the vfio-ap device state\n+ *\n+ * Called during VFIO_DEVICE_RESET to clean up any active migration\n+ * state and reset the device to RUNNING state as required by the VFIO\n+ * migration specification.\n+ */\n+void vfio_ap_reset_migration_state(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (!matrix_mdev-\u003emig_data)\n+\t\treturn;\n+\n+\tvfio_ap_release_mig_files(matrix_mdev);\n+\tmatrix_mdev-\u003emig_data-\u003emig_state = VFIO_DEVICE_STATE_RUNNING;\n+}\ndiff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c\nindex 44b3a1dcc1b365..0b49be22d5d8b6 100644\n--- a/drivers/s390/crypto/vfio_ap_ops.c\n+++ b/drivers/s390/crypto/vfio_ap_ops.c\n@@ -81,53 +81,6 @@ static inline void release_update_locks_for_kvm(struct kvm *kvm)\n \tmutex_unlock(\u0026matrix_dev-\u003eguests_lock);\n }\n \n-/**\n- * get_update_locks_for_mdev: Acquire the locks required to dynamically update a\n- *\t\t\t      KVM guest's APCB in the proper order.\n- *\n- * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP\n- *\t\t configuration data to use to update a KVM guest's APCB.\n- *\n- * The proper locking order is:\n- * 1. matrix_dev-\u003eguests_lock: required to use the KVM pointer to update a KVM\n- *\t\t\t       guest's APCB.\n- * 2. matrix_mdev-\u003ekvm-\u003elock:  required to update a guest's APCB\n- * 3. matrix_dev-\u003emdevs_lock:  required to access data stored in a matrix_mdev\n- *\n- * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM\n- *\t lock will not be taken.\n- */\n-static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)\n-{\n-\tmutex_lock(\u0026matrix_dev-\u003eguests_lock);\n-\tif (matrix_mdev \u0026\u0026 matrix_mdev-\u003ekvm)\n-\t\tmutex_lock(\u0026matrix_mdev-\u003ekvm-\u003elock);\n-\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n-}\n-\n-/**\n- * release_update_locks_for_mdev: Release the locks used to dynamically update a\n- *\t\t\t\t  KVM guest's APCB in the proper order.\n- *\n- * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP\n- *\t\t configuration data to use to update a KVM guest's APCB.\n- *\n- * The proper unlocking order is:\n- * 1. matrix_dev-\u003emdevs_lock\n- * 2. matrix_mdev-\u003ekvm-\u003elock\n- * 3. matrix_dev-\u003eguests_lock\n- *\n- * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM\n- *\t lock will not be released.\n- */\n-static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)\n-{\n-\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n-\tif (matrix_mdev \u0026\u0026 matrix_mdev-\u003ekvm)\n-\t\tmutex_unlock(\u0026matrix_mdev-\u003ekvm-\u003elock);\n-\tmutex_unlock(\u0026matrix_dev-\u003eguests_lock);\n-}\n-\n /**\n  * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and\n  *\t\t\t     acquire the locks required to update the APCB of\n@@ -642,8 +595,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)\n \treturn 0;\n }\n \n-static void vfio_ap_matrix_init(struct ap_config_info *info,\n-\t\t\t\tstruct ap_matrix *matrix)\n+void vfio_ap_matrix_init(struct ap_config_info *info, struct ap_matrix *matrix)\n {\n \tmatrix-\u003eapm_max = info-\u003eapxa ? info-\u003ena : 63;\n \tmatrix-\u003eaqm_max = info-\u003eapxa ? info-\u003end : 15;\n@@ -775,18 +727,30 @@ static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,\n \n static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)\n {\n-\tstruct ap_matrix_mdev *matrix_mdev =\n-\t\tcontainer_of(vdev, struct ap_matrix_mdev, vdev);\n+\tstruct ap_matrix_mdev *matrix_mdev;\n \n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n \tmatrix_mdev-\u003emdev = to_mdev_device(vdev-\u003edev);\n \tvfio_ap_matrix_init(\u0026matrix_dev-\u003einfo, \u0026matrix_mdev-\u003ematrix);\n \tmatrix_mdev-\u003epqap_hook = handle_pqap;\n \tvfio_ap_matrix_init(\u0026matrix_dev-\u003einfo, \u0026matrix_mdev-\u003eshadow_apcb);\n \thash_init(matrix_mdev-\u003eqtable.queues);\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n \n \treturn 0;\n }\n \n+static void vfio_ap_mdev_release_dev(struct vfio_device *vdev)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\tvfio_ap_release_migration_data(matrix_mdev);\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+}\n+\n static int vfio_ap_mdev_probe(struct mdev_device *mdev)\n {\n \tstruct ap_matrix_mdev *matrix_mdev;\n@@ -797,13 +761,28 @@ static int vfio_ap_mdev_probe(struct mdev_device *mdev)\n \tif (IS_ERR(matrix_mdev))\n \t\treturn PTR_ERR(matrix_mdev);\n \n+\t/*\n+\t * Migration capabilities must be initialized before calling\n+\t * vfio_register_emulated_iommu_dev; otherwise, the VFIO core\n+\t * will see mig_ops as NULL during the registration. This could\n+\t * prevent the VFIO core from properly setting up migration\n+\t * infrastructure like debugfs entries.\n+\t *\n+\t * This must be done before acquiring mdevs_lock to avoid an ABBA\n+\t * deadlock: vfio_register_emulated_iommu_dev() acquires dev_set-\u003elock\n+\t * internally, while vfio_ap_mdev_open_device() is called by the VFIO\n+\t * core with dev_set-\u003elock already held and then acquires mdevs_lock.\n+\t */\n+\tvfio_ap_init_migration_capabilities(matrix_mdev);\n+\n \tret = vfio_register_emulated_iommu_dev(\u0026matrix_mdev-\u003evdev);\n \tif (ret)\n \t\tgoto err_put_vdev;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n \tmatrix_mdev-\u003ereq_trigger = NULL;\n \tmatrix_mdev-\u003ecfg_chg_trigger = NULL;\n \tdev_set_drvdata(\u0026mdev-\u003edev, matrix_mdev);\n-\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n \tlist_add(\u0026matrix_mdev-\u003enode, \u0026matrix_dev-\u003emdev_list);\n \tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n \treturn 0;\n@@ -991,19 +970,20 @@ static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,\n \tunsigned long apqi;\n \n \tfor_each_set_bit_inv(apqi, matrix_mdev-\u003ematrix.aqm, AP_DOMAINS)\n-\t\tvfio_ap_mdev_link_apqn(matrix_mdev,\n-\t\t\t\t       AP_MKQID(apid, apqi));\n+\t\tvfio_ap_mdev_link_apqn(matrix_mdev, AP_MKQID(apid, apqi));\n }\n \n-static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,\n-\t\t\t\t    unsigned long apid,\n-\t\t\t\t    struct list_head *qlist)\n+static void collect_queues_by_apid(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t\t   unsigned long apid,\n+\t\t\t\t   struct list_head *qlist)\n {\n \tstruct vfio_ap_queue *q;\n \tunsigned long  apqi;\n \n \tfor_each_set_bit_inv(apqi, matrix_mdev-\u003eshadow_apcb.aqm, AP_DOMAINS) {\n-\t\tq = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));\n+\t\tq = matrix_mdev ?\n+\t\t\t\tvfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi)) :\n+\t\t\t\tvfio_ap_find_queue(AP_MKQID(apid, apqi));\n \t\tif (q)\n \t\t\tlist_add_tail(\u0026q-\u003ereset_qnode, qlist);\n \t}\n@@ -1015,7 +995,7 @@ static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,\n \tstruct list_head qlist;\n \n \tINIT_LIST_HEAD(\u0026qlist);\n-\tcollect_queues_to_reset(matrix_mdev, apid, \u0026qlist);\n+\tcollect_queues_by_apid(matrix_mdev, apid, \u0026qlist);\n \tvfio_ap_mdev_reset_qlist(\u0026qlist);\n }\n \n@@ -1031,7 +1011,7 @@ static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,\n \tINIT_LIST_HEAD(\u0026qlist);\n \n \tfor_each_set_bit_inv(apid, apm_reset, AP_DEVICES)\n-\t\tcollect_queues_to_reset(matrix_mdev, apid, \u0026qlist);\n+\t\tcollect_queues_by_apid(matrix_mdev, apid, \u0026qlist);\n \n \treturn vfio_ap_mdev_reset_qlist(\u0026qlist);\n }\n@@ -1702,54 +1682,100 @@ static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)\n \tbitmap_copy(dst-\u003eadm, src-\u003eadm, AP_DOMAINS);\n }\n \n-static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,\n-\t\t\t       const char *buf, size_t count)\n+static void get_removed_matrixes(struct ap_matrix *m_removed,\n+\t\t\t\t struct ap_matrix *m_old,\n+\t\t\t\t struct ap_matrix *m_new)\n {\n-\tstruct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);\n-\tstruct ap_matrix m_new, m_old, m_added, m_removed;\n+\tbitmap_andnot(m_removed-\u003eapm, m_old-\u003eapm, m_new-\u003eapm, AP_DEVICES);\n+\tbitmap_andnot(m_removed-\u003eaqm, m_old-\u003eaqm, m_new-\u003eaqm, AP_DOMAINS);\n+\tbitmap_andnot(m_removed-\u003eadm, m_old-\u003eadm, m_new-\u003eadm, AP_DOMAINS);\n+}\n+\n+static void get_added_matrixes(struct ap_matrix *m_added,\n+\t\t\t       struct ap_matrix *m_old,\n+\t\t\t       struct ap_matrix *m_new)\n+{\n+\tbitmap_andnot(m_added-\u003eapm, m_new-\u003eapm, m_old-\u003eapm, AP_DEVICES);\n+\tbitmap_andnot(m_added-\u003eaqm, m_new-\u003eaqm, m_old-\u003eaqm, AP_DOMAINS);\n+\tbitmap_andnot(m_added-\u003eadm, m_new-\u003eadm, m_old-\u003eadm, AP_DOMAINS);\n+}\n+\n+static int validate_new_state(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tint rc;\n+\n+\t/* Ensure new state is valid, else undo new state */\n+\trc = vfio_ap_mdev_validate_masks(matrix_mdev);\n+\tif (rc)\n+\t\treturn rc;\n+\n+\trc = ap_matrix_overflow_check(matrix_mdev);\n+\tif (rc)\n+\t\treturn rc;\n+\n+\treturn 0;\n+}\n+\n+static void link_new_queues(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t    struct ap_matrix *m_added)\n+{\n+\tunsigned long apid, apqi;\n+\n+\tfor_each_set_bit_inv(apid, m_added-\u003eapm, AP_DEVICES)\n+\t\tvfio_ap_mdev_link_adapter(matrix_mdev, apid);\n+\n+\tfor_each_set_bit_inv(apqi, m_added-\u003eaqm, AP_DOMAINS)\n+\t\tvfio_ap_mdev_link_domain(matrix_mdev, apqi);\n+}\n+\n+/**\n+ * vfio_ap_set_new_guest_config:\n+ *\n+ * Set a new AP configuration for a guest.\n+ *\n+ * @matrix_mdev: Object used to maintain the AP configuration for a guest\n+ * @m_new:\t\t Object used to set the new AP configuration\n+ *\n+ * Returns: zero (0) if the new AP configuration is successfully set; otherwise,\n+ *\t    returns an error:\n+ *\n+ *\t    ~ EADDRNOTAVAIL One or more APQNs are reserved for host use\n+ *\t    ~ EADDRINUSE    One or more APQNs are assigned to another mdev\n+ *\t    ~ ENODEV\t    An adapter, domain or control domain in the new\n+ *\t\t\t    AP configuration exceeds the max architected value\n+ */\n+int vfio_ap_set_new_guest_config(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t\t struct ap_matrix *m_new)\n+{\n+\tstruct ap_matrix m_old, m_old_shadow, m_added, m_removed;\n \tDECLARE_BITMAP(apm_filtered, AP_DEVICES);\n-\tunsigned long newbit;\n-\tchar *newbuf, *rest;\n-\tint rc = count;\n \tbool do_update;\n+\tint rc;\n \n-\tnewbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);\n-\tif (!newbuf)\n-\t\treturn -ENOMEM;\n-\trest = newbuf;\n-\n-\tmutex_lock(\u0026ap_attr_mutex);\n-\tget_update_locks_for_mdev(matrix_mdev);\n+\tlockdep_assert_held(\u0026ap_attr_mutex);\n+\tassert_has_update_locks_for_mdev(matrix_mdev);\n \n-\t/* Save old state */\n+\t/* Save the old state */\n \tap_matrix_copy(\u0026m_old, \u0026matrix_mdev-\u003ematrix);\n-\tif (parse_bitmap(\u0026rest, m_new.apm, AP_DEVICES) ||\n-\t    parse_bitmap(\u0026rest, m_new.aqm, AP_DOMAINS) ||\n-\t    parse_bitmap(\u0026rest, m_new.adm, AP_DOMAINS)) {\n-\t\trc = -EINVAL;\n-\t\tgoto out;\n-\t}\n+\tap_matrix_copy(\u0026m_old_shadow, \u0026matrix_mdev-\u003eshadow_apcb);\n \n-\tbitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);\n-\tbitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);\n-\tbitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);\n-\tbitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);\n+\t/*\n+\t * Get the adapters, domains and control domains added and/or removed\n+\t * from the existing configuration\n+\t */\n+\tget_removed_matrixes(\u0026m_removed, \u0026m_old, m_new);\n+\tget_added_matrixes(\u0026m_added, \u0026m_old, m_new);\n \n \t/* Need new bitmaps in matrix_mdev for validation */\n-\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, \u0026m_new);\n+\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, m_new);\n \n \t/* Ensure new state is valid, else undo new state */\n-\trc = vfio_ap_mdev_validate_masks(matrix_mdev);\n+\trc = validate_new_state(matrix_mdev);\n \tif (rc) {\n \t\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, \u0026m_old);\n-\t\tgoto out;\n+\t\tap_matrix_copy(\u0026matrix_mdev-\u003eshadow_apcb, \u0026m_old_shadow);\n+\t\treturn rc;\n \t}\n-\trc = ap_matrix_overflow_check(matrix_mdev);\n-\tif (rc) {\n-\t\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, \u0026m_old);\n-\t\tgoto out;\n-\t}\n-\trc = count;\n \n \t/* Need old bitmaps in matrix_mdev for unplug/unlink */\n \tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, \u0026m_old);\n@@ -1759,14 +1785,10 @@ static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr\n \tvfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);\n \n \t/* Need new bitmaps in matrix_mdev for linking new adapters/domains */\n-\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, \u0026m_new);\n-\n-\t/* Link newly added adapters */\n-\tfor_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)\n-\t\tvfio_ap_mdev_link_adapter(matrix_mdev, newbit);\n+\tap_matrix_copy(\u0026matrix_mdev-\u003ematrix, m_new);\n \n-\tfor_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)\n-\t\tvfio_ap_mdev_link_domain(matrix_mdev, newbit);\n+\t/* Link queues associated with the newly added adapters and domains */\n+\tlink_new_queues(matrix_mdev, \u0026m_added);\n \n \t/* filter resources not bound to vfio-ap */\n \tdo_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);\n@@ -1777,7 +1799,39 @@ static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr\n \t\tvfio_ap_mdev_update_guest_apcb(matrix_mdev);\n \t\treset_queues_for_apids(matrix_mdev, apm_filtered);\n \t}\n-out:\n+\n+\treturn 0;\n+}\n+\n+static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,\n+\t\t\t       const char *buf, size_t count)\n+{\n+\tstruct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);\n+\tstruct ap_matrix m_new;\n+\tchar *newbuf, *rest;\n+\tssize_t rc;\n+\n+\tnewbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);\n+\tif (!newbuf)\n+\t\treturn -ENOMEM;\n+\trest = newbuf;\n+\n+\tmutex_lock(\u0026ap_attr_mutex);\n+\tget_update_locks_for_mdev(matrix_mdev);\n+\n+\tif (parse_bitmap(\u0026rest, m_new.apm, AP_DEVICES) ||\n+\t    parse_bitmap(\u0026rest, m_new.aqm, AP_DOMAINS) ||\n+\t    parse_bitmap(\u0026rest, m_new.adm, AP_DOMAINS)) {\n+\t\tkfree(newbuf);\n+\t\trelease_update_locks_for_mdev(matrix_mdev);\n+\t\tmutex_unlock(\u0026ap_attr_mutex);\n+\t\treturn -EINVAL;\n+\t}\n+\n+\trc = vfio_ap_set_new_guest_config(matrix_mdev, \u0026m_new);\n+\tif (!rc)\n+\t\trc = count;\n+\n \trelease_update_locks_for_mdev(matrix_mdev);\n \tmutex_unlock(\u0026ap_attr_mutex);\n \tkfree(newbuf);\n@@ -2052,19 +2106,39 @@ static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)\n \n static int vfio_ap_mdev_open_device(struct vfio_device *vdev)\n {\n-\tstruct ap_matrix_mdev *matrix_mdev =\n-\t\tcontainer_of(vdev, struct ap_matrix_mdev, vdev);\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\tint ret;\n \n \tif (!vdev-\u003ekvm)\n \t\treturn -EINVAL;\n \n-\treturn vfio_ap_mdev_set_kvm(matrix_mdev, vdev-\u003ekvm);\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\tret = vfio_ap_init_migration_data(matrix_mdev);\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\tif (ret)\n+\t\treturn ret;\n+\n+\tret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev-\u003ekvm);\n+\tif (ret) {\n+\t\t/* Clean up migration data on failure */\n+\t\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\t\tvfio_ap_release_migration_data(matrix_mdev);\n+\t\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\t}\n+\n+\treturn ret;\n }\n \n static void vfio_ap_mdev_close_device(struct vfio_device *vdev)\n {\n-\tstruct ap_matrix_mdev *matrix_mdev =\n-\t\tcontainer_of(vdev, struct ap_matrix_mdev, vdev);\n+\tstruct ap_matrix_mdev *matrix_mdev;\n+\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\n+\tvfio_ap_release_migration_data(matrix_mdev);\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n \n \tvfio_ap_mdev_unset_kvm(matrix_mdev);\n }\n@@ -2098,6 +2172,14 @@ static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)\n \trelease_update_locks_for_mdev(matrix_mdev);\n }\n \n+int vfio_ap_mdev_get_num_queues(struct ap_matrix *ap_matrix)\n+{\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+\n+\treturn bitmap_weight(ap_matrix-\u003eapm, AP_DEVICES) *\n+\t       bitmap_weight(ap_matrix-\u003eaqm, AP_DOMAINS);\n+}\n+\n static int vfio_ap_mdev_get_device_info(unsigned long arg)\n {\n \tunsigned long minsz;\n@@ -2275,6 +2357,10 @@ static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,\n \t\tbreak;\n \tcase VFIO_DEVICE_RESET:\n \t\tret = vfio_ap_mdev_reset_queues(matrix_mdev);\n+\t\tif (ret == 0) {\n+\t\t\t/* Reset migration state per VFIO migration spec */\n+\t\t\tvfio_ap_reset_migration_state(matrix_mdev);\n+\t\t}\n \t\tbreak;\n \tcase VFIO_DEVICE_GET_IRQ_INFO:\n \t\tret = vfio_ap_get_irq_info(arg);\n@@ -2360,6 +2446,7 @@ static const struct attribute_group vfio_queue_attr_group = {\n \n static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {\n \t.init = vfio_ap_mdev_init_dev,\n+\t.release = vfio_ap_mdev_release_dev,\n \t.open_device = vfio_ap_mdev_open_device,\n \t.close_device = vfio_ap_mdev_close_device,\n \t.ioctl = vfio_ap_mdev_ioctl,\ndiff --git a/drivers/s390/crypto/vfio_ap_private.h b/drivers/s390/crypto/vfio_ap_private.h\nindex 9bff666b0b35d3..150dfce8a674b2 100644\n--- a/drivers/s390/crypto/vfio_ap_private.h\n+++ b/drivers/s390/crypto/vfio_ap_private.h\n@@ -91,6 +91,9 @@ struct ap_queue_table {\n \tDECLARE_HASHTABLE(queues, 8);\n };\n \n+/* Forward declaration for migration data structure */\n+struct vfio_ap_migration_data;\n+\n /**\n  * struct ap_matrix_mdev - Contains the data associated with a matrix mediated\n  *\t\t\t   device.\n@@ -110,6 +113,7 @@ struct ap_queue_table {\n  * @aqm_add:\tbitmap of APQIs added to the host's AP configuration\n  * @adm_add:\tbitmap of control domain numbers added to the host's AP\n  *\t\tconfiguration\n+ * @mig_data:  vfio device migration data\n  */\n struct ap_matrix_mdev {\n \tstruct vfio_device vdev;\n@@ -125,6 +129,7 @@ struct ap_matrix_mdev {\n \tDECLARE_BITMAP(apm_add, AP_DEVICES);\n \tDECLARE_BITMAP(aqm_add, AP_DOMAINS);\n \tDECLARE_BITMAP(adm_add, AP_DOMAINS);\n+\tstruct vfio_ap_migration_data *mig_data;\n };\n \n /**\n@@ -152,6 +157,64 @@ struct vfio_ap_queue {\n \tstruct work_struct reset_work;\n };\n \n+/**\n+ * get_update_locks_for_mdev: Acquire the locks required to dynamically update a\n+ *\t\t\t      KVM guest's APCB in the proper order.\n+ *\n+ * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP\n+ *\t\t configuration data to use to update a KVM guest's APCB.\n+ *\n+ * The proper locking order is:\n+ * 1. matrix_dev-\u003eguests_lock: required to use the KVM pointer to update a KVM\n+ *\t\t\t       guest's APCB.\n+ * 2. matrix_mdev-\u003ekvm-\u003elock:  required to update a guest's APCB\n+ * 3. matrix_dev-\u003emdevs_lock:  required to access data stored in a matrix_mdev\n+ *\n+ * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM\n+ *\t lock will not be taken.\n+ */\n+static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tmutex_lock(\u0026matrix_dev-\u003eguests_lock);\n+\tif (matrix_mdev \u0026\u0026 matrix_mdev-\u003ekvm)\n+\t\tmutex_lock(\u0026matrix_mdev-\u003ekvm-\u003elock);\n+\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\n+}\n+\n+/**\n+ * release_update_locks_for_mdev: Release the locks used to dynamically update a\n+ *\t\t\t\t  KVM guest's APCB in the proper order.\n+ *\n+ * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP\n+ *\t\t configuration data to use to update a KVM guest's APCB.\n+ *\n+ * The proper unlocking order is:\n+ * 1. matrix_dev-\u003emdevs_lock\n+ * 2. matrix_mdev-\u003ekvm-\u003elock\n+ * 3. matrix_dev-\u003eguests_lock\n+ *\n+ * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM\n+ *\t lock will not be released.\n+ */\n+static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n+\tif (matrix_mdev \u0026\u0026 matrix_mdev-\u003ekvm)\n+\t\tmutex_unlock(\u0026matrix_mdev-\u003ekvm-\u003elock);\n+\tmutex_unlock(\u0026matrix_dev-\u003eguests_lock);\n+}\n+\n+static inline void\n+assert_has_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)\n+{\n+\tlockdep_assert_held(\u0026matrix_dev-\u003eguests_lock);\n+\tif (matrix_mdev \u0026\u0026 matrix_mdev-\u003ekvm)\n+\t\tlockdep_assert_held(\u0026matrix_mdev-\u003ekvm-\u003elock);\n+\tlockdep_assert_held(\u0026matrix_dev-\u003emdevs_lock);\n+}\n+\n+int vfio_ap_mdev_get_num_queues(struct ap_matrix *ap_matrix);\n+\n int vfio_ap_mdev_register(void);\n void vfio_ap_mdev_unregister(void);\n \n@@ -165,4 +228,14 @@ void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info,\n void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,\n \t\t\t      struct ap_config_info *old_config_info);\n \n+void vfio_ap_matrix_init(struct ap_config_info *info, struct ap_matrix *matrix);\n+\n+void vfio_ap_init_migration_capabilities(struct ap_matrix_mdev *matrix_mdev);\n+int vfio_ap_init_migration_data(struct ap_matrix_mdev *matrix_mdev);\n+void vfio_ap_release_migration_data(struct ap_matrix_mdev *matrix_mdev);\n+void vfio_ap_reset_migration_state(struct ap_matrix_mdev *matrix_mdev);\n+\n+int vfio_ap_set_new_guest_config(struct ap_matrix_mdev *matrix_mdev,\n+\t\t\t\t struct ap_matrix *m_new);\n+\n #endif /* _VFIO_AP_PRIVATE_H_ */\n"
}

2/1 2026/07/28 13:22 agent patch-evaluator
1m Model:
best-expensive

Args:
null

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [
    "vfio_ap_stop_copy_read",
    "vfio_ap_resuming_write",
    "vfio_ap_set_state",
    "vfio_ap_get_state",
    "vfio_ap_get_data_size",
    "vfio_ap_set_new_guest_config"
  ],
  "Reasoning": "The patch implements VFIO migration support for the s390 AP (Adjunct Processor) matrix mediated device driver. It adds file operations for reading and writing migration state (vfio_ap_stop_copy_read, vfio_ap_resuming_write) and VFIO migration state transition callbacks (vfio_ap_set_state, vfio_ap_get_state). These are reachable from userspace via VFIO ioctls and migration file descriptors, and can be tested in an s390x QEMU environment with AP instructions enabled.",
  "WorthFuzzing": true
}

Instruction:
You are an expert Linux kernel maintainer.
Your job is to review a provided patch series and determine
if it makes functional changes to the kernel that should be fuzzed.

IMPORTANT: The changes have ALREADY been applied and committed as the HEAD commit in
your workspace. Do NOT rely on your internal knowledge of the kernel. You must actively
use your code access tools to examine the actual source code and confirm any assumptions.

Return WorthFuzzing=false if the patch only contains:
- Modifications to Documentation/, Kconfig files, or code comments.
- Purely decorative changes, such as logging (e.g., pr_err, printk) or tracepoints.
- Changes to numeric constants or macros that do not functionally alter execution flow.
- Code paths that are impossible to reach in virtualized environments like GCE or QEMU,
  even when utilizing software-emulated hardware (e.g., usb gadget, mac80211_hwsim).
- Code in vendor-specific PCIe switch, SmartNIC, or GPU drivers (e.g., mlxsw, pds_core, qed,
  ionic, amdgpu) that require physical PCIe hardware cards not emulated in standard QEMU.
- Driver .remove, .shutdown, or pci_unregister_driver teardown callbacks (e.g., igb_remove)
  that are executed only during PCI hot-unplug or sysfs driver unbind operations.

If it modifies reachable core kernel logic, drivers, or architectures, use your code search
tools to verify the code can be executed, then return WorthFuzzing=true.

When returning WorthFuzzing=true, you MUST ALSO:
1. Extract any specific kernel functions that should be heavily fuzzed into FocusSymbols.
   Avoid listing generic hot-path functions to prevent skewed test distributions.
   Prefer non-static, non-inlined API entrypoint functions over internal static helper functions
   (which are inlined by the compiler and do not have distinct symbol addresses).
2. Identify any specific CONFIG_ options required to properly test this new/modified feature.
   Go and look into the Kconfig files and check for ifdefs around the code, do not make assumptions.
   Also check "depends on" lines in Kconfig to include any non-standard parent subsystem configs
   needed for Kbuild to compile the code statically into vmlinux. List them in the EnableConfigs
   output array, and DO NOT add a 'CONFIG_' prefix (e.g., return "NET_IPV4" instead of "CONFIG_NET_IPV4").
Prefer calling several tools at the same time to save round-trips.


Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.

Prompt:
For your convenience, here is the diff of the changes:
commit 82a64d57797ed3dc1e0c4219fa19484b162d14bf
Author: syz-cluster <triage@syzkaller.com>
Date:   Tue Jul 28 13:22:51 2026 +0000

    syz-cluster: applied patch under review

diff --git a/Documentation/arch/s390/vfio-ap.rst b/Documentation/arch/s390/vfio-ap.rst
index ac0c07f76ddd7b..02d7cbe5e47918 100644
--- a/Documentation/arch/s390/vfio-ap.rst
+++ b/Documentation/arch/s390/vfio-ap.rst
@@ -1012,7 +1012,7 @@ guest_matrix dyn ap_config
 
 the following features are advertised:
 
----------------+---------------------------------------------------------------+
++--------------+---------------------------------------------------------------+
 | Flag         | Description                                                   |
 +==============+===============================================================+
 | guest_matrix | guest_matrix attribute exists. It reports the matrix of       |
@@ -1021,105 +1021,523 @@ the following features are advertised:
 +--------------+---------------------------------------------------------------+
 | dyn          | Indicates hot plug/unplug of AP adapters, domains and control |
 |              | domains for a guest to which the mdev is attached.            |
-+------------+-----------------------------------------------------------------+
++--------------+---------------------------------------------------------------+
 | ap_config    | ap_config interface for one-shot modifications to mdev config |
 +--------------+---------------------------------------------------------------+
+| migratable   | Indicates that live guest migration is supported for guests   |
+|              | to which crypto devices are passed through                    |
++--------------+---------------------------------------------------------------+
 
-Limitations
-===========
-Live guest migration is not supported for guests using AP devices without
-intervention by a system administrator. Before a KVM guest can be migrated,
-the vfio_ap mediated device must be removed. Unfortunately, it can not be
-removed manually (i.e., echo 1 > /sys/devices/vfio_ap/matrix/$UUID/remove) while
-the mdev is in use by a KVM guest. If the guest is being emulated by QEMU,
-its mdev can be hot unplugged from the guest in one of two ways:
-
-1. If the KVM guest was started with libvirt, you can hot unplug the mdev via
-   the following commands:
-
-      virsh detach-device <guestname> <path-to-device-xml>
-
-      For example, to hot unplug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 from
-      the guest named 'my-guest':
-
-         virsh detach-device my-guest ~/config/my-guest-hostdev.xml
-
-            The contents of my-guest-hostdev.xml:
-
-.. code-block:: xml
-
-            <hostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'>
-              <source>
-                <address uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/>
-              </source>
-            </hostdev>
-
-
-      virsh qemu-monitor-command <guest-name> --hmp "device-del <device-id>"
-
-      For example, to hot unplug the vfio_ap mediated device identified on the
-      qemu command line with 'id=hostdev0' from the guest named 'my-guest':
-
-.. code-block:: sh
-
-         virsh qemu-monitor-command my-guest --hmp "device_del hostdev0"
-
-2. A vfio_ap mediated device can be hot unplugged by attaching the qemu monitor
-   to the guest and using the following qemu monitor command:
-
-      (QEMU) device-del id=<device-id>
-
-      For example, to hot unplug the vfio_ap mediated device that was specified
-      on the qemu command line with 'id=hostdev0' when the guest was started:
-
-         (QEMU) device-del id=hostdev0
-
-After live migration of the KVM guest completes, an AP configuration can be
-restored to the KVM guest by hot plugging a vfio_ap mediated device on the target
-system into the guest in one of two ways:
-
-1. If the KVM guest was started with libvirt, you can hot plug a matrix mediated
-   device into the guest via the following virsh commands:
-
-   virsh attach-device <guestname> <path-to-device-xml>
-
-      For example, to hot plug mdev 62177883-f1bb-47f0-914d-32a22e3a8804 into
-      the guest named 'my-guest':
-
-         virsh attach-device my-guest ~/config/my-guest-hostdev.xml
-
-            The contents of my-guest-hostdev.xml:
-
-.. code-block:: xml
-
-            <hostdev mode='subsystem' type='mdev' managed='no' model='vfio-ap'>
-              <source>
-                <address uuid='62177883-f1bb-47f0-914d-32a22e3a8804'/>
-              </source>
-            </hostdev>
-
-
-   virsh qemu-monitor-command <guest-name> --hmp \
-   "device_add vfio-ap,sysfsdev=<path-to-mdev>,id=<device-id>"
-
-      For example, to hot plug the vfio_ap mediated device
-      62177883-f1bb-47f0-914d-32a22e3a8804 into the guest named 'my-guest' with
-      device-id hostdev0:
-
-      virsh qemu-monitor-command my-guest --hmp \
-      "device_add vfio-ap,\
-      sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\
-      id=hostdev0"
-
-2. A vfio_ap mediated device can be hot plugged by attaching the qemu monitor
-   to the guest and using the following qemu monitor command:
-
-      (qemu) device_add "vfio-ap,sysfsdev=<path-to-mdev>,id=<device-id>"
-
-      For example, to plug the vfio_ap mediated device
-      62177883-f1bb-47f0-914d-32a22e3a8804 into the guest with the device-id
-      hostdev0:
+Live Guest Migration
+====================
+The VFIO AP mediated device is not used to provide userspace with direct
+access to device memory as is the case with other devices that use the VFIO
+framework for device pass-through. Instead, it manages AP
+configuration metadata identifying the adapters, domains, and control
+domains to which a guest will be granted access. These AP resources are
+configured by assigning them to a vfio-ap mediated device via its sysfs
+assignment interfaces. When the guest is started, the vfio_ap device driver
+sets the guest's AP configuration from the metadata stored with the mediated
+device. The AP devices are not accessed directly through the vfio_ap driver,
+so the driver does not migrate the state of the AP devices themselves.
+Instead, it migrates the AP configuration metadata. The vfio_ap device
+driver on the destination host will then ensure that the AP configuration
+metadata migrated from the source host is compatible with the AP
+configuration available on the destination host and if
+so, will set the destination guest's AP configuration accordingly.
 
-         (QEMU) device-add "vfio-ap,\
-         sysfsdev=/sys/devices/vfio_ap/matrix/62177883-f1bb-47f0-914d-32a22e3a8804,\
-         id=hostdev0"
+To be considered compatible, the AP configuration for the destination host must
+meet these requirements:
+
+* Each AP adapter, domain and control domain number assigned to the source
+  guest's AP configuration must be in the destination host's AP
+  configuration. This can be verified as follows:
+
+  * On the source host, display the AP configuration of the source guest
+    with the following two commands:
+
+    .. code-block:: bash
+
+       cat /sys/devices/vfio_ap/matrix/$UUID/guest_matrix
+       cat /sys/devices/vfio_ap/matrix/$UUID/control_domains
+
+    .. note::
+       Replace ``$UUID`` with the actual UUID of your mediated device.
+
+       The ``guest_matrix`` lists the APQNs of the queue devices assigned
+       to the mediated device; for example:
+
+       .. code-block:: bash
+
+          00.0033
+          00.003a
+
+       The ``control_domains`` lists the domain numbers of the control
+       domains; for example:
+
+       .. code-block:: bash
+
+          0033
+          003a
+
+  * Display the AP configuration of the destination host with the following
+    two commands:
+
+    .. code-block:: bash
+
+       lszcrypt -V
+       cat /sys/bus/ap/ap_control_domain_mask
+
+    .. note::
+       The ``lszcrypt -V`` command lists the AP cards (adapters) and queue
+       devices on the host system; for example:
+
+       .. code-block:: bash
+
+          =========== ===== ============ ==========
+          CARD.DOMAIN TYPE  MODE         DRIVER
+          =========== ===== ============ ==========
+          00          CEX5C CCA-Coproc   cex4card
+          00.0033     CEX5C CCA-Coproc   vfio_ap
+          00.003a     CEX5C CCA-Coproc   vfio_ap
+          =========== ===== ============ ==========
+
+       Each bit in the ap_control_domain_mask corresponds to
+       domain numbers 0-255. If a bit is set to one, then
+       the control domain is available on the source host. For
+       example:
+
+       .. code-block:: bash
+
+          0x0000000000001020000000000000000000000000000000000000000000000000
+
+       This mask indicates that AP control domains 51 (0x33) and 58 (0x3a) are
+       in the AP configuration of the host system.
+
+  * Verify the configurations:
+
+    * Each APQN in the guest's sysfs ``guest_matrix`` file must match an
+      APQN returned from the ``lszcrypt -V`` command on the destination host. It
+      is important to note the driver to which the queue is bound which
+      is displayed in the ``driver`` column. Queues not bound to the vfio_ap
+      driver are not available to the destination guest. Even if the APQNs are
+      available to the driver, that does not guarantee they can be used by the
+      guest being migrated; for example, migration will fail if any of these APQNs
+      are assigned to a mediated device other than the one being migrated.
+
+    * Each domain number from the source guest's sysfs
+      ``control_domains`` attribute must correspond to a bit set to one in
+      the destination host's ``ap_control_domain_mask``.
+
+
+* Each APQN assigned to the source guest and destination guest must
+  reference a queue device with compatible hardware capabilities:
+
+  +--------------+---------------------------------------------------------------+
+  | Hardware     | Description                                                   |
+  | Capabilities |                                                               |
+  +==============+===============================================================+
+  | [1]          | * AP special command facility (APSC)                          |
+  | Facilities   | * AP 4096-bit ME PKU commands facility (AP4KM)                |
+  |              | * AP 4096-bit CRT PKU commands (AP4KC)                        |
+  +--------------+---------------------------------------------------------------+
+  | [1] Mode     | * CCA-mode                                                    |
+  |              | * Accelerator-mode                                            |
+  |              | * XCP-mode (EP11)                                             |
+  +--------------+---------------------------------------------------------------+
+  | [1] AP       |   APXA installed                                              |
+  | extended     |                                                               |
+  | addressing   |                                                               |
+  +--------------+---------------------------------------------------------------+
+  | [1] Command  |   Command filtering available                                 |
+  | filtering    |                                                               |
+  +--------------+---------------------------------------------------------------+
+  | [2]          | * Full native card function                                   |
+  | Functional   | * Only stateless functions                                    |
+  | capabilities |                                                               |
+  +--------------+---------------------------------------------------------------+
+  | [3] AP type  | * 10 (0x0a): CEX4                                             |
+  |              | * 11 (0x0b): CEX5                                             |
+  |              | * 12 (0x0c)  CEX6                                             |
+  |              | * 13 (0x0d)  CEX7                                             |
+  |              | * 14 (0x0e)  CEX8                                             |
+  +--------------+---------------------------------------------------------------+
+
+  +-----------------------------------------------------------------------------+
+  | Table Keys                                                                  |
+  +=============================================================================+
+  |                                                                             |
+  | [1]        The hardware capability must be the same for both the source     |
+  |            and destination queue device.                                    |
+  |                                                                             |
+  | [2]        The source and destination queues must either both have          |
+  |            full native card function or both have stateless functions.      |
+  |            If the functional capabilities don't match, then the source      |
+  |            queue can have stateless capabilities since full native card     |
+  |            function can handle stateless functions; otherwise, migration    |
+  |            will fail.                                                       |
+  |                                                                             |
+  | [3]        The AP type on the source and destination guests can             |
+  |            differ if the queue passed through to the target guest is a      |
+  |            newer model (backwards compatible)                               |
+  +-----------------------------------------------------------------------------+
+
+  * To verify the hardware capabilities are compatible:
+
+    * On both the source and destination hosts, display the hardware capabilities
+      for each AP adapter in the AP configuration for that host:
+
+      .. code-block:: bash
+
+         cat /sys/bus/ap/devices/card$APID/ap_functions
+
+      .. note::
+         The ``$APID`` is the two-character adapter number in hexadecimal format;
+         for example, ``card02`` or ``card1f``. The ``/sys/bus/ap/devices`` directory
+         also lists the APQNs of the queue devices installed in the respective
+         host system with the first two characters being the APID; for example:
+         ``02.0004`` or ``00.0033`` .
+
+    * Verify the hardware capabilities for each AP adapter device on the source
+      and destination host are compatible. The ``ap_functions`` attribute is a
+      bitmask. The bits in the mask read from left to right starting with bit 0.
+      Each bit that is set to one indicates the corresponding hardware capability
+      is installed:
+
+      * Bits 0-2 are the facilities bits. Each bit value must match for the AP
+        devices on the source and destination systems. The values indicate the
+        following:
+
+        * bit-0: APSC is installed
+        * bit-1: AP4KM is installed
+        * bit-2: AP4KC is installed
+
+      * Bits 3-5 specify the adapter Modes. Only one of these bits will be set to
+        one and must be the same for both the source and destination adapter
+        devices:
+
+        * bit-3: CCA-mode
+        * bit-4: Accelerator-mode
+        * bit-5: XCP-mode (EP11)
+
+      * Bit 6 specifies whether APXA is installed and must be the same for both
+        the source and destination adapter devices.
+
+      * Bit 7 specifies whether command filtering is installed
+        and must be the same for both the source and destination adapter devices.
+
+      * Bits 8-15 are the classification bits. There are only two relevant
+        bits and only one of them will be set to one. For the source and destination
+        adapter devices to be considered compatible:
+
+        * Bit 8 may be set to 1 on both hosts (full native card function)
+        * Bit 9 may be set to 1 on both hosts (only stateless functions available)
+        * Bit 9 may be set to 1 on the source host and bit 8 may be set to 1 on
+          the destination host
+
+      * Bits 16-17 are the binding state bits which must be 00
+
+  * To verify the AP types are compatible:
+
+    * On both the source and destination hosts, display the hardware type
+      for each AP adapter in the AP configuration for that host to ensure the
+      source and destination adapter types are the same, or the source type is
+      less than the destination type::
+
+       cat /sys/bus/ap/devices/card$APID/hwtype
+
+      .. note::
+         The ``$APID`` is the two-character adapter number in hexadecimal format;
+         for example, ``card02`` or ``card1f``.
+
+Setting up for live guest migration on the destination host
+-----------------------------------------------------------
+To set up a host as the destination for live guest migration, do the
+following:
+
+1. Create a mediated device
+~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Create a mediated device with the same name (UUID) as the mediated device
+used to supply the AP configuration to the source guest. For example, if
+the mediated device on the source guest is named
+62177883-f1bb-47f0-914d-32a22e3a8804:
+
+.. code-block:: bash
+
+   echo 62177883-f1bb-47f0-914d-32a22e3a8804 > \
+     /sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough/create
+
+2. Reserve adapters and domains for the vfio_ap device driver's use
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Reserve the same adapters, and domains passed through to the source guest
+for the vfio_ap device driver on the destination host by clearing the
+adapter numbers from the ``/sys/bus/ap/apmask`` and/or the domain numbers
+from the ``/sys/bus/ap/aqmask`` files.
+
+For example, suppose adapters 0, 20 (0x14) and 22 (0x16) as well as
+domains 51 (0x33) and 58 (0x3a) are passed through to the source guest. To
+ensure they can be passed through to the destination guest, they must be
+made available to the vfio_ap device driver on the destination host. To
+make them available, do one or both of the following on the destination host:
+
+.. code-block:: bash
+
+   echo -0,-0x14,-22 > /sys/bus/ap/apmask
+   echo -0,-0x33,-0x3a > /sys/bus/ap/aqmask
+
+.. note::
+
+    * It is not necessary to reserve both the adapters and domains.
+      Reserving an adapter implicitly allows the vfio_ap driver to assign
+      each of the domains available on the system along with the reserved
+      adapters to a guest's AP configuration and vice versa.
+
+    * The Cartesian product of the adapters and domains reserved for the
+      vfio_ap device driver identifies the APQNs of the AP queue devices to be
+      bound to the vfio_ap device driver. If an APQN does not reference a
+      queue device installed in the system at the time of live guest migration,
+      the migration will fail.
+
+    * It is not necessary to configure the destination mediated device with
+      adapters, domains and control domains because the source guest's AP
+      configuration will overlay the destination guest's during migration.
+
+    * To verify that the queue devices are reserved for the vfio_ap
+      device driver, issue the ``lszcrypt -V`` command and you should
+      see ``vfio_ap`` in the ``DRIVER`` column of the output.
+
+Live guest migration failures due to AP configuration errors
+------------------------------------------------------------
+This section describes the errors that may occur during live guest migration
+when the AP configuration of the source guest and destination host are not
+compatible and where to look for problem determination.
+
+The destination host is missing the mediated device
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This error occurs when a mediated device with the same name (UUID) as the
+mediated device being migrated has not been created on the destination host.
+
+**Source host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | error: device not found: mediated device '$UUID' not           |
+|              | found                                                          |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | initiating migration                                           |
+|              | qemu-system-s390x: Sibling indicated error 1                   |
++--------------+----------------------------------------------------------------+
+
+.. note::
+   $UUID is the UUID of the mediated device being migrated from the source host.
+
+
+One or more queues are not bound to the vfio_ap driver on the destination host
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The queue devices passed through to the source guest are not installed in the
+destination host or are not bound to its vfio_ap device driver.
+
+**Source host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | error: operation failed: migration failed. Message from the    |
+|              | source host: operation failed: job 'migration out' failed:     |
+|              | Sibling indicated error 1. Message from the destination host:  |
+|              | operation failed: job 'migration in' failed: load of migration |
+|              | failed: No such device: Failed to load vmstate version_id: 1,  |
+|              | ret: -19                                                       |
++--------------+----------------------------------------------------------------+
+
+**Destination host**
+
++--------------+----------------------------------------------------------------+
+| Kernel log:  | vfio_ap_mdev $UUID: Queue $APID.$APQI not available to         |
+|              | vfio_ap driver on target host                                  |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | shutting down, reason=failed                                   |
+|              | qemu-system-s390x: terminating on signal $SIGNAL_NUM from      |
+|              | pid $PID $DAEMON                                               |
++--------------+----------------------------------------------------------------+
+
+
+One or more control domains are not available on the destination host
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The control domains passed through to the source guest are not installed on the
+destination host.
+
+**Source host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | error: operation failed: migration failed. Message from the    |
+|              | source host: operation failed: job 'migration out' failed:     |
+|              | Sibling indicated error 1. Message from the destination host:  |
+|              | operation failed: job 'migration in' failed: load of migration |
+|              | failed: No such device: Failed to load vmstate version_id: 1,  |
+|              | ret: -19                                                       |
++--------------+----------------------------------------------------------------+
+
+**Destination host**
+
++--------------+----------------------------------------------------------------+
+| Kernel log:  | vfio_ap_mdev $UUID: Control domain $DOMAIN_NUM not available   |
+|              | on the destination host                                        |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | shutting down, reason=failed                                   |
+|              | qemu-system-s390x: terminating on signal $SIGNAL_NUM from      |
+|              | pid $PID $DAEMON                                               |
++--------------+----------------------------------------------------------------+
+
+
+.. note::
+   * $UUID is the UUID of the mediated device being migrated from the source host.
+   * $APID.$APQI is adapter number and queue index (domain number) comprising the
+     $APQN (for example, 00.0033) of the queue device that is not available.
+   * $SIGNAL_NUM is the signal number that caused the QEMU process to terminate.
+   * $PID is the process ID of the QEMU process on the destination host.
+   * $DAEMON is the name of the QEMU daemon process.
+   * This message will be logged for each queue for which this error is detected.
+
+Failure to get hardware information for a queue device on destination host
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The vfio_ap device driver on the destination host will execute an AP instruction
+to get the hardware information for a queue device to perform queue compatibility
+checks. The instruction can fail for a number of reasons in which case the
+migration will be terminated.
+
+**Source host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | error: operation failed: migration failed. Message from the    |
+|              | source host: operation failed: job 'migration out' failed:     |
+|              | Sibling indicated error 1. Message from the destination host:  |
+|              | operation failed: job 'migration in' failed: load of migration |
+|              | failed: failed to get hardware information for queue device    |
+|              | $APQN                                                          |
++--------------+----------------------------------------------------------------+
+
+**Destination host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | vfio_ap_mdev $UUID: migration failed: Failed to get hwinfo for |
+|              | queue $APQN on target host: rc=$RESPONSE-CODE                  |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | shutting down, reason=failed                                   |
+|              | qemu-system-s390x: terminating on signal $SIGNAL-ID from pid   |
+|              | $PID $DAEMON                                                   |
++--------------+----------------------------------------------------------------+
+
+.. note::
+   * $UUID is the UUID of the mediated device being migrated from the source host.
+   * $APQN (for example, 00.0033) of the queue device that is not compatible.
+   * $RESPONSE_CODE is the response code from the AP instruction.
+   * $SIGNAL_NUM is the signal number of the signal that caused the QEMU process
+     to terminate.
+   * $PID is the process ID of the QEMU process on the destination host.
+   * $DAEMON is the name of the QEMU daemon process.
+   * This message will be logged for each queue for which this error is detected.
+
+The AP configurations of the source guest and destination host not compatible
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+The functional capabilities or facilities available on one or more of the queues
+passed through to the source guest are not compatible with the queue device with
+the same APQN on the destination system (see the Hardware Capabilities table above)
+
+**Source host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | error: operation failed: migration failed. Message from the    |
+|              | source host: operation failed: job 'migration out' failed:     |
+|              | Sibling indicated error 1. Message from the destination host:  |
+|              | operation failed: job 'migration in' failed: load of migration |
+|              | failed: No such device: Failed to load vmstate version_id: 1,  |
+|              | ret: -19                                                       |
++--------------+----------------------------------------------------------------+
+| Kernel log:  | N/A                                                            |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | *initiating migration*                                         |
+|              |                                                                |
+|              | *qemu-system-s390x: Sibling indicated error 1*                 |
++--------------+----------------------------------------------------------------+
+
+**Destination host**
+
++--------------+----------------------------------------------------------------+
+| Log          | Message                                                        |
++==============+================================================================+
+| Console log: | N/A                                                            |
++--------------+----------------------------------------------------------------+
+| Kernel log:  | *vfio_ap_mdev $UUID: Migration failed: Source and target       |
+|              | queue ($APQN) not compatible*                                  |
+|              |                                                                |
+|              | The message above will be followed by one or more messages     |
+|              | enumerating the incompatible features; for example:            |
+|              |                                                                |
+|              | ``vfio_ap_mdev $UUID: APSC facility installed in source queue  |
+|              | $APQN``                                                        |
+|              | ``vfio_ap_mdev $UUID: APSC facility not installed in target    |
+|              | queue $APQN``                                                  |
+|              |                                                                |
+|              | ``AP type of source ($APTYPE1) not compatible with target      |
+|              | ($APTYPE2)`` ...                                               |
++--------------+----------------------------------------------------------------+
+| QEMU log:    | *initiating migration*                                         |
+|              |                                                                |
+|              | *qemu-system-s390x: error while loading state section id ...*  |
+|              |                                                                |
+|              | *shutting down, reason=failed*                                 |
+|              |                                                                |
+|              | *terminating on signal $SIGNAL-ID from pid $PID $DAEMON*       |
++--------------+----------------------------------------------------------------+
+
+.. note::
+   * $UUID is the UUID of the mediated device being migrated from the source host.
+   * $APQN (for example, 00.0033) of the queue device that is incompatible.
+   * $APTYPE1 is the type of the source queue device.
+   * $APTYPE2 is the type of the target queue device.
+   * $SIGNAL-ID is the signal number of the signal that caused the QEMU process
+     to terminate.
+   * $PROCESS-ID is the process ID of the QEMU process on the source host.
+   * $PID is the process ID of the QEMU process on the destination host.
+   * $DAEMON is the name of the QEMU daemon process.
+   * Each follow-on message logging the incompatibility will be logged for each
+     error detected for the specified queue device.
+
+AP Configuration Management
+---------------------------
+The AP configurations of the source guest and destination host must be kept
+in synchronization or live guest migration will likely fail due to potential
+incompatibilities introduced by such changes. In particular, changing adapter
+or domain reservations via the sysfs ``apmask`` or ``aqmask`` attributes on
+either the source or destination host without coordinating such changes between
+the two hosts -especially while a migration is taking place - can result in
+migration failures.
+
+Configuration stability is an orchestration-layer or system administrator
+responsibility, which is consistent with other VFIO device types. The vfio_ap
+device driver will validate source and destination configurations and provide
+clear diagnostics when incompatibilities are detected, enabling orchestration
+tools to implement appropriate policies; for example, blocking migration when
+such changes are to be made.
+
+Master Key administration
+--------------------------
+It is imperative that s390 Common Cryptographic Architecture (CCA) master key
+administration be performed on both the source and destination AP
+devices to synchronize the key values prior to allowing live guest
+migration. If the master keys do not match, then crypto applications that
+rely on secure keys wrapped by a CCA master key will fail when the guest on
+which they are running is migrated to the destination host. Again, this is
+an orchestration-layer or system administrator responsibility and migration must
+be blocked while master key administration is being performed.
diff --git a/drivers/s390/crypto/Makefile b/drivers/s390/crypto/Makefile
index e83c6603c8587d..20f29184825a7b 100644
--- a/drivers/s390/crypto/Makefile
+++ b/drivers/s390/crypto/Makefile
@@ -34,5 +34,5 @@ pkey-uv-objs := pkey_uv.o
 obj-$(CONFIG_PKEY_UV) += pkey-uv.o
 
 # adjunct processor matrix
-vfio_ap-objs := vfio_ap_drv.o vfio_ap_ops.o
+vfio_ap-objs := vfio_ap_drv.o vfio_ap_ops.o vfio_ap_migration.o
 obj-$(CONFIG_VFIO_AP) += vfio_ap.o
diff --git a/drivers/s390/crypto/vfio_ap_drv.c b/drivers/s390/crypto/vfio_ap_drv.c
index 8e69ed286bb9ca..4e973736522bc1 100644
--- a/drivers/s390/crypto/vfio_ap_drv.c
+++ b/drivers/s390/crypto/vfio_ap_drv.c
@@ -26,9 +26,11 @@ MODULE_LICENSE("GPL v2");
 struct ap_matrix_dev *matrix_dev;
 debug_info_t *vfio_ap_dbf_info;
 
+#define FEATURES "guest_matrix hotplug ap_config migratable"
+
 static ssize_t features_show(struct device *dev, struct device_attribute *attr, char *buf)
 {
-	return sysfs_emit(buf, "guest_matrix hotplug ap_config\n");
+	return sysfs_emit(buf, "%s\n", FEATURES);
 }
 static DEVICE_ATTR_RO(features);
 
diff --git a/drivers/s390/crypto/vfio_ap_migration.c b/drivers/s390/crypto/vfio_ap_migration.c
new file mode 100644
index 00000000000000..8379cf8bbce897
--- /dev/null
+++ b/drivers/s390/crypto/vfio_ap_migration.c
@@ -0,0 +1,1530 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Drives vfio_ap mdev migration.
+ *
+ * Copyright IBM Corp. 2025
+ */
+#include <linux/anon_inodes.h>
+#include <linux/file.h>
+#include "vfio_ap_private.h"
+
+/*
+ * Masks the fields of the queue information returned from the PQAP(TAPQ)
+ * command. In order to migrate a guest, it's AP configuration must be
+ * compatible with AP configuration assigned to the target guest's mdev.
+ * This mask is used to verify that the queue information for each source and
+ * target queue is compatible.
+ *
+ * The following bits must match for the source device and the corresponding
+ * destination device:
+ * -------------------------------------------------------------------------
+ * S bit 0: APSC  facility installed
+ * M bit 1: APQKM facility installed
+ * C bit 2: AP4KC facility installed
+ * Mode bits 3-5:
+ *     D bit 3: CCA-mode facility
+ *     A bit 4: accelerator-mode facility
+ *     X bit 5: XCP-mode facility
+ * N  bit 6: APXA facility installed
+ * SL bit 7: SLCF facility installed
+ *
+ * Either bit 8 or bit 9 will be set. If bit 8 is set for the source device,
+ * then it must also be set for the corresponding destination device:
+ * -------------------------------------------------------------------------
+ * Classification (functional capabilities) bits 8-16
+ *     bit 8: Native card function
+ *     bit 9: Only stateless functions
+ *
+ * The BS bits must be set to 0 for both the source and corresponding
+ * destination device:
+ * -------------------------------------------------------------------------
+ * BS bits 16-17:
+ *
+ * The AP type of the source device must be less than or equal to that of
+ * the corresponding destination device:
+ * -------------------------------------------------------------------------
+ * AP Type bits 32-40:
+ */
+#define QINFO_DATA_MASK		0xffffc000ff000000
+
+/*
+ * Masks the bit that indicates whether full native card function is available
+ * from the 8 bits specifying the functional capabilities of a queue
+ */
+#define CLASSIFICATION_NATIVE_FCN_MASK		0x80
+
+/* The maximum number of queues that can be installed in an s390 system */
+#define MAX_AP_QUEUES				(AP_DEVICES * AP_DOMAINS)
+
+/**
+ * struct vfio_ap_migration_file
+ *
+ * This object is used for chunk processing of multiple reads and writes of
+ * AP configuration information.
+ *
+ * @filp:	file stream used to read or write AP configuration data
+ * @ap_config:	object used to store AP configuration data between read or write
+ *		calls
+ * @config_sz:	the size (in bytes) of @ap_config
+ */
+struct vfio_ap_migration_file {
+	struct file		*filp;
+	struct vfio_ap_config	*ap_config;
+	unsigned long		config_sz;
+};
+
+/**
+ * struct vfio_ap_migration_data
+ *
+ * Manages the migration state for the VFIO device that maintains the AP
+ * configuration of the guest being migrated.
+ *
+ * @mig_state:		the current migration state
+ * @resuming_mig_file:	the object used to restore the state of the vfio-ap
+ *			device the destination host:
+ * @stop_copy_mig_file: the object used to store the AP configuration of the
+ *			source guest for transfer to the destination host.
+ */
+struct vfio_ap_migration_data {
+	enum vfio_device_mig_state	mig_state;
+	struct vfio_ap_migration_file	resuming_mig_file;
+	struct vfio_ap_migration_file	stop_copy_mig_file;
+};
+
+/**
+ * struct vfio_ap_queue_info - the information for an AP queue
+ *
+ * @data: contains the queue information returned in GR2 from the PQAP(TAPQ)
+ *	  command
+ * @apqn: the APQN of the queue
+ * @reserved: padding to ensure consistent structure size across platforms
+ */
+struct vfio_ap_queue_info {
+	u64 data;
+	u16 apqn;
+	u8  reserved[6];
+};
+
+/**
+ * struct vfio_ap_config - the guest's AP configuration
+ *
+ * @num_queues:	the number of queues passed through to the guest
+ * @reserved:	padding to ensure proper alignment of @adm
+ * @adm:	bitmap specifying the control domains in the AP configuration
+ * @qinfo:	an array of vfio_ap_queue_info objects, each specifying the
+ *		queue information for a queue passed through to the guest
+ */
+struct vfio_ap_config {
+	u32				num_queues;
+	u8				reserved[4];
+	u64				adm[DIV_ROUND_UP(AP_DOMAINS, 64)];
+	struct vfio_ap_queue_info	qinfo[] __counted_by(num_queues);
+};
+
+static void
+vfio_ap_release_stop_copy_file(struct vfio_ap_migration_data *mig_data)
+{
+	kfree(mig_data->stop_copy_mig_file.ap_config);
+	mig_data->stop_copy_mig_file.ap_config = NULL;
+	mig_data->stop_copy_mig_file.config_sz = 0;
+	mig_data->stop_copy_mig_file.filp = NULL;
+}
+
+static void
+vfio_ap_release_resuming_file(struct vfio_ap_migration_data *mig_data)
+{
+	kvfree(mig_data->resuming_mig_file.ap_config);
+	mig_data->resuming_mig_file.ap_config = NULL;
+	mig_data->resuming_mig_file.config_sz = 0;
+	mig_data->resuming_mig_file.filp = NULL;
+}
+
+static int vfio_ap_release_mig_file(struct inode *file_inode, struct file *filp)
+{
+	struct ap_matrix_mdev *matrix_mdev = filp->private_data;
+	int ret = 0;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+
+	/*
+	 * mig_data may be NULL if the device was closed (vfio_ap_mdev_close_device)
+	 * before the migration FD was released by userspace. In that case the
+	 * migration file state was already cleaned up; nothing to do here.
+	 */
+	if (!matrix_mdev->mig_data)
+		goto done;
+
+	if (filp == matrix_mdev->mig_data->stop_copy_mig_file.filp)
+		vfio_ap_release_stop_copy_file(matrix_mdev->mig_data);
+	else if (filp == matrix_mdev->mig_data->resuming_mig_file.filp)
+		vfio_ap_release_resuming_file(matrix_mdev->mig_data);
+	else
+		ret = -ENOENT;
+
+done:
+	mutex_unlock(&matrix_dev->mdevs_lock);
+	vfio_device_put_registration(&matrix_mdev->vdev);
+	return ret;
+}
+
+/**
+ * validate_stop_copy_read_parms: Validate the input parameters to the
+ *                                vfio_ap_stop_copy_read function
+ *
+ * @matrix_mdev: The object device containing the state to be read
+ * @filp: Pointer to the file stream used to read the vfio-ap device state
+ * @pos:  The file offset from which to start reading data
+ * @len:  The length of the data to be read
+ *
+ * Verify the following:
+ * - @filp private data is an ap_matrix_mdev instance
+ * - @filp is the instance opened when state transitioned from STOP to STOP_COPY
+ * - @pos + @len does not cause integer overflow
+ *
+ * Returns: 0 if the parameters pass validation; otherwise returns an error
+ */
+static int validate_stop_copy_read_parms(struct file *filp, loff_t *pos,
+					 size_t len)
+{
+	struct vfio_ap_migration_data *mig_data;
+	struct ap_matrix_mdev *matrix_mdev;
+	loff_t total_len;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	if (check_add_overflow((loff_t)len, *pos, &total_len))
+		return -EIO;
+
+	/*
+	 * matrix_mdev is guaranteed live here: vfio_ap_open_file_stream() took
+	 * a vfio_device registration reference that is held until
+	 * vfio_ap_release_mig_file() runs, so the embedding matrix_mdev cannot
+	 * be freed while this file descriptor is open.
+	 */
+	matrix_mdev = filp->private_data;
+
+	if (!matrix_mdev->mig_data)
+		return -ENODEV;
+
+	mig_data = matrix_mdev->mig_data;
+
+	if (mig_data->stop_copy_mig_file.filp != filp)
+		return -EINVAL;
+
+	return 0;
+}
+
+static size_t vfio_ap_config_size(struct ap_matrix_mdev *matrix_mdev,
+				  int *num_queues)
+{
+	size_t qinfo_size;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	*num_queues = vfio_ap_mdev_get_num_queues(&matrix_mdev->shadow_apcb);
+	qinfo_size = *num_queues * sizeof(struct vfio_ap_queue_info);
+
+	return qinfo_size + sizeof(struct vfio_ap_config);
+}
+
+static int get_hardware_info_for_queue(const char *mdev_name,
+				       struct ap_tapq_hwinfo *hwinfo,
+				       unsigned long apqn)
+{
+	struct ap_queue_status status;
+
+	status = ap_tapq(apqn, hwinfo);
+
+	switch (status.response_code) {
+	case AP_RESPONSE_NORMAL:
+	case AP_RESPONSE_RESET_IN_PROGRESS:
+	case AP_RESPONSE_DECONFIGURED:
+	case AP_RESPONSE_CHECKSTOPPED:
+	case AP_RESPONSE_BUSY:
+		/* For all these RCs the tapq info should be available */
+		return 0;
+	case AP_RESPONSE_Q_NOT_AVAIL:
+		pr_err("vfio_ap_mdev %s: Failed to get hwinfo for queue %02lx.%04lx: TAPQ rc=%d",
+		       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
+		       status.response_code);
+		return -ENODEV;
+	default:
+		/*
+		 * Without a pending async error, the tapq info should be
+		 * available
+		 */
+		if (status.async)
+			return 0;
+
+		pr_err("vfio_ap_mdev %s:Failed to get hwinfo for queue %02lx.%04lx: TAPQ rc=%d",
+		       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
+		       status.response_code);
+		return -EIO;
+	}
+}
+
+static int vfio_ap_store_queue_info(const char *mdev_name,
+				    struct vfio_ap_config *ap_config)
+{
+	struct ap_tapq_hwinfo source_hwinfo;
+	unsigned long num_queues;
+	int ret;
+
+	/*
+	 * ap_tapq() is a hardware instruction that may take time to complete.
+	 * It must be called without mdevs_lock held to avoid blocking other
+	 * mdevs. The apqn list was already snapshotted into ap_config->qinfo[]
+	 * by the caller under the lock.
+	 */
+	for (num_queues = 0; num_queues < ap_config->num_queues; num_queues++) {
+		ret = get_hardware_info_for_queue(mdev_name, &source_hwinfo,
+						  ap_config->qinfo[num_queues].apqn);
+		if (ret)
+			return ret;
+
+		ap_config->qinfo[num_queues].data = source_hwinfo.value;
+	}
+
+	return 0;
+}
+
+static int vfio_ap_get_config(struct ap_matrix_mdev *matrix_mdev)
+{
+	unsigned long *apm, *aqm, apid, apqi;
+	struct vfio_ap_config *ap_configuration;
+	const char *mdev_name;
+	size_t ap_config_size;
+	int num_queues;
+	int ret;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	ap_config_size = vfio_ap_config_size(matrix_mdev, &num_queues);
+
+	ap_configuration = kzalloc(ap_config_size, GFP_KERNEL_ACCOUNT);
+	if (!ap_configuration)
+		return -ENOMEM;
+
+	/*
+	 * num_queues must be set before writing qinfo[] elements; the
+	 * __counted_by(num_queues) annotation on qinfo[] causes the compiler to
+	 * insert bounds checks that evaluate against ap_configuration->num_queues.
+	 * Writing through qinfo[i] with num_queues still 0 would trap.
+	 */
+	ap_configuration->num_queues = num_queues;
+
+	apm = matrix_mdev->shadow_apcb.apm;
+	aqm = matrix_mdev->shadow_apcb.aqm;
+	num_queues = 0;
+	for_each_set_bit_inv(apid, apm, AP_DEVICES) {
+		for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
+			ap_configuration->qinfo[num_queues].apqn =
+				AP_MKQID(apid, apqi);
+			num_queues += 1;
+		}
+	}
+	memcpy(ap_configuration->adm, matrix_mdev->shadow_apcb.adm,
+	       sizeof(ap_configuration->adm));
+	mdev_name = dev_name(matrix_mdev->vdev.dev);
+
+	ret = vfio_ap_store_queue_info(mdev_name, ap_configuration);
+	if (ret) {
+		kfree(ap_configuration);
+		return ret;
+	}
+
+	matrix_mdev->mig_data->stop_copy_mig_file.ap_config = ap_configuration;
+	matrix_mdev->mig_data->stop_copy_mig_file.config_sz = ap_config_size;
+
+	return 0;
+}
+
+static ssize_t vfio_ap_stop_copy_read(struct file *filp, char __user *buf,
+				      size_t len, loff_t *pos)
+{
+	struct vfio_ap_migration_file *mig_file;
+	struct ap_matrix_mdev *matrix_mdev;
+	loff_t read_pos;
+	ssize_t ret;
+
+	/*
+	 * When userspace calls read() with an explicit offset (pread), pos is
+	 * non-NULL and the function rejects it with -ESPIPE (illegal seek). For
+	 * normal read() calls, pos is NULL, so we'll use the file's internal
+	 * position filp->f_pos
+	 */
+	if (pos)
+		return -ESPIPE;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+
+	pos = &filp->f_pos;
+
+	ret = validate_stop_copy_read_parms(filp, pos, len);
+	if (ret) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return ret;
+	}
+
+	matrix_mdev = filp->private_data;
+	mig_file = &matrix_mdev->mig_data->stop_copy_mig_file;
+
+	if (!mig_file->ap_config) {
+		ret = vfio_ap_get_config(matrix_mdev);
+		if (ret) {
+			mutex_unlock(&matrix_dev->mdevs_lock);
+			return ret;
+		}
+	}
+
+	/*
+	 * Compute the offset and clamped length fully under the lock so that
+	 * concurrent read()s on this stream file each see a consistent view of
+	 * the current position.  *pos is advanced here while we still hold the
+	 * lock; copy_to_user() then uses the snapshot read_pos.  This prevents
+	 * two threads from calculating the same offset and both copying the
+	 * same region (or one reading past the end of the buffer).
+	 */
+	if (*pos >= mig_file->config_sz) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return 0;
+	}
+
+	len = min_t(size_t, mig_file->config_sz - *pos, len);
+	if (len == 0) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return 0;
+	}
+
+	read_pos = *pos;
+	*pos += len;
+
+	/*
+	 * Drop the lock only for the copy_to_user().  The ap_config buffer is
+	 * stable: it is allocated once in vfio_ap_get_config() and freed only
+	 * in vfio_ap_release_mig_files() / vfio_ap_release_stop_copy_file(),
+	 * both of which require mdevs_lock.  Since we already advanced *pos
+	 * above, no other thread will compute an overlapping region.
+	 */
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	if (copy_to_user(buf, (char *)mig_file->ap_config + read_pos, len))
+		return -EFAULT;
+
+	return len;
+}
+
+static const struct file_operations vfio_ap_stop_copy_fops = {
+	.owner = THIS_MODULE,
+	.read = vfio_ap_stop_copy_read,
+	.compat_ioctl = compat_ptr_ioctl,
+	.release = vfio_ap_release_mig_file,
+};
+
+static struct file *vfio_ap_open_file_stream(struct ap_matrix_mdev *matrix_mdev,
+					     const struct file_operations *fops,
+					     int flags)
+{
+	struct file *filp;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	/*
+	 * Pin the vfio_device registration so that matrix_mdev cannot be freed
+	 * while the migration FD is still open. The matching put is in
+	 * vfio_ap_release_mig_file().
+	 */
+	if (!vfio_device_try_get_registration(&matrix_mdev->vdev))
+		return ERR_PTR(-ENODEV);
+
+	filp = anon_inode_getfile("vfio_ap_mig_file", fops, matrix_mdev, flags);
+	if (IS_ERR(filp)) {
+		vfio_device_put_registration(&matrix_mdev->vdev);
+		return filp;
+	}
+
+	stream_open(filp->f_inode, filp);
+
+	/*
+	 * Take a second reference on the file so the driver holds its own
+	 * reference independent of the one consumed when the VFIO core
+	 * installs the FD into the userspace file table. Without this,
+	 * the driver's saved filp could be the only reference; an fput()
+	 * during a device reset would prematurely destroy the file while
+	 * the userspace FD still points to it.
+	 */
+	get_file(filp);
+
+	return filp;
+}
+
+static int validate_resuming_write_parms(struct file *filp,
+					 size_t len, loff_t *pos)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+	loff_t total_len;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	if (!len || *pos < 0)
+		return -EINVAL;
+
+	if (check_add_overflow((loff_t)len, *pos, &total_len))
+		return -ERANGE;
+
+	matrix_mdev = filp->private_data;
+	if (!matrix_mdev || !matrix_mdev->mig_data)
+		return -ENODEV;
+
+	if (filp != matrix_mdev->mig_data->resuming_mig_file.filp)
+		return -ENXIO;
+
+	/*
+	 * If the ap_config has not yet been allocated and the file position
+	 * indicates this is not the first write, or the ap_config has been allocated
+	 * but the file position indicates this is the first write, then this is an
+	 * error condition.
+	 */
+	if ((!matrix_mdev->mig_data->resuming_mig_file.ap_config && *pos != 0) ||
+	    (matrix_mdev->mig_data->resuming_mig_file.ap_config && *pos == 0))
+		return -EFAULT;
+
+	/*
+	 * The first write must cover at least num_queues (the first field of
+	 * struct vfio_ap_config) so that allocate_ap_config() can derive the
+	 * correct allocation size.  A shorter first write would cause
+	 * cfg_sz to be set to len, the completion check
+	 * (write_pos + len == cfg_sz) would fire immediately, and
+	 * do_post_copy_validation() would read qinfo[] from a buffer that
+	 * is too small to contain it.
+	 */
+	if (*pos == 0 && len < offsetofend(struct vfio_ap_config, num_queues))
+		return -EINVAL;
+
+	return 0;
+}
+
+static ssize_t calculate_ap_config_size(unsigned int num_queues)
+{
+	size_t qinfo_size;
+
+	if (num_queues > MAX_AP_QUEUES)
+		return -EINVAL;
+
+	qinfo_size = num_queues * sizeof(struct vfio_ap_queue_info);
+	return qinfo_size + sizeof(struct vfio_ap_config);
+}
+
+/**
+ * allocate_ap_config:
+ *
+ * Allocate storage for the source guest's AP configuration data sent from
+ * userspace.
+ *
+ * @ap_config:	The location in which to store the pointer to the storage
+ *		allocated for the AP configuration data.
+ * @buf:	The userspace buffer containing some or all of the source
+ *		guest's AP configuration data
+ * @len:	The number of bytes of data to copy from @buf
+ *
+ * Returns:	The number of bytes of storage allocated for the config data or
+ *		an error:
+ *
+ *		-EINVAL: len is 0, or num_queues exceeds the maximum (only checked
+ *			 if @len covers the full vfio_ap_config header)
+ *		-EIO: failed to copy data from @buf
+ *		-ENOMEM: the allocation of storage failed
+ */
+static ssize_t allocate_ap_config(struct vfio_ap_config **ap_config,
+				  const char __user *buf, size_t len)
+{
+	struct vfio_ap_config tmp_ap_config;
+	ssize_t config_size;
+
+	/*
+	 * validate_resuming_write_parms() guarantees the first write covers at
+	 * least num_queues, so we can always derive the final allocation size
+	 * here.
+	 */
+	if (copy_from_user(&tmp_ap_config, buf, min(len, sizeof(tmp_ap_config))))
+		return -EIO;
+
+	config_size = calculate_ap_config_size(tmp_ap_config.num_queues);
+	if (config_size < 0)
+		return config_size;
+
+	/*
+	 * Use kvzalloc so that large configurations can fall back to vmalloc
+	 * rather than failing a high-order contiguous physical allocation.
+	 */
+	*ap_config = kvzalloc(config_size, GFP_KERNEL_ACCOUNT);
+	if (!*ap_config)
+		return -ENOMEM;
+
+	return config_size;
+}
+
+/**
+ * qdev_is_bound_to_vfio_ap:
+ *
+ * Query to determine whether a queue with the specified APQN is available on
+ * the host system and bound to the vfio_ap device driver.
+ *
+ * @apqn: The APQN of the queue device being queried
+ *
+ * Returns: True if there is a queue device with the specified @apqn installed
+ *	    in the system and is bound to the vfio_ap device driver; otherwise,
+ *	    returns false.
+ */
+static bool qdev_is_bound_to_vfio_ap(unsigned int apqn)
+{
+	struct ap_queue *queue;
+	bool is_bound = true;
+
+	queue = ap_get_qdev(apqn);
+	if (!queue)
+		return false;
+
+	if (queue->ap_dev.device.driver != &matrix_dev->vfio_ap_drv->driver)
+		is_bound = false;
+
+	put_device(&queue->ap_dev.device);
+
+	return is_bound;
+}
+
+/**
+ * queues_available:
+ *
+ * Query whether each queue from the source guest's AP configuration is
+ * available and bound to the vfio_ap device driver; if not, log an error
+ * message.
+ *
+ * @mdev_name:	   The mdev name to use in error messages
+ * @source_config: The object specifying the source guest's AP configuration
+ *
+ * Returns: true if each queue identified in @source_config is available and
+ *	    bound to the vfio_ap device driver; otherwise, returns false.
+ */
+static bool queues_available(const char *mdev_name,
+			     struct vfio_ap_config *source_config)
+{
+	unsigned long apqn;
+	bool ret = true;
+
+	for (int i = 0; i < source_config->num_queues; i++) {
+		apqn = source_config->qinfo[i].apqn;
+
+		/*
+		 * Find the queue device bound to the vfio_ap device driver. If it is
+		 * not found, log an error and continue so users see all problems
+		 * at once, not one-at-a-time through retries of the migration.
+		 */
+		if (!qdev_is_bound_to_vfio_ap(apqn)) {
+			pr_err("vfio_ap_mdev %s: Queue %02lx.%04lx not available to vfio_ap driver on target host\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+			ret = false;
+		}
+	}
+
+	return ret;
+}
+
+/**
+ * control_domains_available
+ *
+ * Query whether each control domain specified in the source guest's AP
+ * configuration is installed in the host system.
+ *
+ * @mdev_name:		The name of the mdev to use when logging messages
+ * @source_config:	The object specifying the source guest's AP config
+ *
+ * Returns:	True if each control domain is installed; otherwise, logs an
+ *		error message for each unavailable control domain and returns
+ *		false.
+ */
+static bool control_domains_available(const char *mdev_name,
+				      struct vfio_ap_config *source_config)
+{
+	unsigned long domain_num;
+	bool available = true;
+
+	for_each_set_bit_inv(domain_num, (unsigned long *)source_config->adm,
+			     AP_DOMAINS) {
+		if (!ap_test_config_ctrl_domain(domain_num)) {
+			pr_err("vfio_ap_mdev: %s: Control domain %04lx not available on the destination host",
+			       mdev_name, domain_num);
+			available = false;
+		}
+	}
+
+	return available;
+}
+
+static void report_facilities_compatibility(const char *mdev_name,
+					    unsigned long apqn,
+					    struct ap_tapq_hwinfo *src_hwinfo,
+					    struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (src_hwinfo->apsc != target_hwinfo->apsc) {
+		if (src_hwinfo->apsc) {
+			pr_err("vfio_ap_mdev %s: APSC facility installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: APSC facility not installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: APSC facility not installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s APSC facility installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+
+	if (src_hwinfo->mex4k != target_hwinfo->mex4k) {
+		if (src_hwinfo->mex4k) {
+			pr_err("vfio_ap_mdev %s: mex4k facility installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: mex4k facility not installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: mex4k facility not installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: mex4k facility installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+
+	if (src_hwinfo->crt4k != target_hwinfo->crt4k) {
+		if (src_hwinfo->crt4k) {
+			pr_err("vfio_ap_mdev %s: crt4k facility installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: crt4k facility not installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: crt4k facility not installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: crt4k facility installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+}
+
+static void report_mode_compatibility(const char *mdev_name,
+				      unsigned long apqn,
+				      struct ap_tapq_hwinfo *src_hwinfo,
+				      struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (src_hwinfo->cca != target_hwinfo->cca) {
+		if (src_hwinfo->cca) {
+			pr_err("vfio_ap_mdev %s: Coprocessor-mode facility installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Coprocessor-mode  facility not installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: Coprocessor-mode facility not installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Coprocessor-mode  facility installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+
+	if (src_hwinfo->accel != target_hwinfo->accel) {
+		if (src_hwinfo->accel) {
+			pr_err("vfio_ap_mdev %s: Accelerator-mode facility installed source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Accelerator-mode facility not installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: Accelerator-mode facility not installed source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Accelerator-mode facility installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+
+	if (src_hwinfo->ep11 != target_hwinfo->ep11) {
+		if (src_hwinfo->ep11) {
+			pr_err("vfio_ap_mdev %s: XCP-mode facility installed source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: XCP-mode facility not installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: XCP-mode facility not installed source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: XCP-mode facility installed target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+}
+
+static void report_apxa_compatibility(const char *mdev_name,
+				      unsigned long apqn,
+				      struct ap_tapq_hwinfo *src_hwinfo,
+				      struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (src_hwinfo->apxa != target_hwinfo->apxa) {
+		if (src_hwinfo->apxa) {
+			pr_err("vfio_ap_mdev %s: AP-extended-addressing (APXA) facility installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: AP-extended-addressing (APXA) facility not installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: AP-extended-addressing (APXA) facility not installed in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: AP-extended-addressing (APXA) facility installed in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+}
+
+static void report_slcf_compatibility(const char *mdev_name,
+				      unsigned long apqn,
+				      struct ap_tapq_hwinfo *src_hwinfo,
+				      struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (src_hwinfo->slcf != target_hwinfo->slcf) {
+		if (src_hwinfo->slcf) {
+			pr_err("vfio_ap_mdev %s: Stateless-command-filtering (SLCF) available in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Stateless-command-filtering (SLCF) not available in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		} else {
+			pr_err("vfio_ap_mdev %s: Stateless-command-filtering (SLCF) not available in source queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+			pr_err("vfio_ap_mdev %s: Stateless-command-filtering (SLCF) available in target queue %02lx.%04lx\n",
+			       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+		}
+	}
+}
+
+static void report_bs_compatibility(const char *mdev_name,
+				    unsigned long apqn,
+				    struct ap_tapq_hwinfo *src_hwinfo,
+				    struct ap_tapq_hwinfo *target_hwinfo)
+{
+	/*
+	 * The BS field on both the source and destination must be 0, so if one
+	 * of them is not, then report an error.
+	 */
+	if (src_hwinfo->bs || target_hwinfo->bs) {
+		pr_err("vfio_ap_mdev %s: Bind/associate state for source (%01x) and target (%01x) queue %02lx.%04lx must be 0\n",
+		       mdev_name, src_hwinfo->bs, target_hwinfo->bs,
+		       AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+	}
+}
+
+static void report_aptype_compatibility(const char *mdev_name,
+					unsigned long apqn,
+					struct ap_tapq_hwinfo *src_hwinfo,
+					struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (src_hwinfo->at > target_hwinfo->at) {
+		pr_err("vfio_ap_mdev %s: AP type of source (%02x) not compatible with target (%02x)\n",
+		       mdev_name, src_hwinfo->at, target_hwinfo->at);
+	}
+}
+
+static bool classes_compatible(struct ap_tapq_hwinfo *src_hwinfo,
+			       struct ap_tapq_hwinfo *target_hwinfo)
+{
+	unsigned long src_native, target_native;
+
+	src_native = src_hwinfo->class & CLASSIFICATION_NATIVE_FCN_MASK;
+	target_native = target_hwinfo->class & CLASSIFICATION_NATIVE_FCN_MASK;
+
+	/*
+	 * If the source queue has full native card function and the
+	 * target queue has only stateless functions available, then
+	 * there may be instructions that will not execute on the
+	 * target queue. This shall be reported as an error.
+	 *
+	 * If the source queue has only stateless card functions and the
+	 * target queue has full native card function available, then
+	 * we are okay because the target queue can run all stateless card
+	 * functions.
+	 */
+	return (src_native != target_native) ? !src_native : true;
+}
+
+static void report_class_compatibility(const char *mdev_name,
+				       unsigned long apqn,
+				       struct ap_tapq_hwinfo *src_hwinfo,
+				       struct ap_tapq_hwinfo *target_hwinfo)
+{
+	if (!classes_compatible(src_hwinfo, target_hwinfo)) {
+		pr_err("vfio_ap_mdev %s: Full native card function available on source queue %02lx.%04lx\n",
+		       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+		pr_err("vfio_ap_mdev %s: Only stateless functions available on target queue %02lx.%04lx\n",
+		       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+	}
+}
+
+/*
+ * Log a device error reporting that migration failed due to queue
+ * incompatibilities followed by a device error for each incompatible feature.
+ */
+static void report_qinfo_incompatibilities(const char *mdev_name,
+					   unsigned long apqn,
+					   struct ap_tapq_hwinfo *src_hwinfo,
+					   struct ap_tapq_hwinfo *target_hwinfo)
+{
+	pr_err("vfio_ap_mdev %s: Migration failed: Source and target queue (%02lx.%04lx) not compatible\n",
+	       mdev_name, AP_QID_CARD(apqn), AP_QID_QUEUE(apqn));
+
+	report_facilities_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_mode_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_apxa_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_slcf_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_aptype_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_bs_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+	report_class_compatibility(mdev_name, apqn, src_hwinfo, target_hwinfo);
+}
+
+/**
+ * queue_hardware_info_is_compatible:
+ *
+ * Verify whether the hardware information for a source queue is compatible with
+ * the hardware info for the corresponding queue on this system.
+ *
+ * In order to be compatible, the hardware information for each queue must
+ * meet the following requirements:
+ *
+ * 1. The hardware facilities bits much match
+ * 2. The AP type of the source queue must be the same as or older than that
+ *    of the target queue (target is backwards compatible)
+ * 3. The classification bits must indicate:
+ *    - Both queues have full native card function or both have stateless
+ *      functions available
+ *    - If the classification bits don't match, then the only acceptable
+ *      configuration is stateless functions for the source queue and
+ *      full native function for the target queue
+ * 4. The BS bits for both queues must be 0 (Queue usable for all messages
+ *    supported by the adapter)
+ *
+ * @mdev_name:	The mdev name to use in error messages
+ * @apqn:	The APQN for the queues
+ * @src_hwinfo: The hardware info for the source queue
+ * @target_hwinfo: The hardware info for the corresponding queue on this system
+ *
+ * Returns: true if the hardware info for the two queues is compatible;
+ *          otherwise, returns false.
+ */
+static bool queue_hardware_info_is_compatible(const char *mdev_name,
+					      unsigned long apqn,
+					      struct ap_tapq_hwinfo *src_hwinfo,
+					      struct ap_tapq_hwinfo *target_hwinfo)
+{
+	unsigned long src_bits, target_bits;
+
+	src_bits = src_hwinfo->value & QINFO_DATA_MASK;
+	target_bits = target_hwinfo->value & QINFO_DATA_MASK;
+
+	/* If all bits match the queues are compatible */
+	if (src_bits == target_bits &&
+	    (src_hwinfo->bs == 0 && target_hwinfo->bs == 0))
+		return true;
+
+	if (src_hwinfo->apsc  == target_hwinfo->apsc     &&
+	    src_hwinfo->mex4k == target_hwinfo->mex4k    &&
+	    src_hwinfo->crt4k == target_hwinfo->crt4k    &&
+	    src_hwinfo->cca   == target_hwinfo->cca      &&
+	    src_hwinfo->accel == target_hwinfo->accel    &&
+	    src_hwinfo->ep11  == target_hwinfo->ep11     &&
+	    src_hwinfo->slcf  == target_hwinfo->slcf     &&
+	    src_hwinfo->apxa  == target_hwinfo->apxa     &&
+	    src_hwinfo->at    <= target_hwinfo->at       &&
+	    classes_compatible(src_hwinfo, target_hwinfo) &&
+	    (src_hwinfo->bs == 0 && target_hwinfo->bs == 0))
+		return true;
+
+	report_qinfo_incompatibilities(mdev_name, apqn, src_hwinfo, target_hwinfo);
+
+	return false;
+}
+
+/**
+ * verify_ap_configs_are_compatible:
+ *
+ * Verifies that the queues in the source guest's AP configuration are
+ * compatible with the corresponding queues on this system.
+ *
+ * @mdev_name:	   The mdev name to use in error messages
+ * @source_config: The object specifying the source guest's AP configuration
+ *
+ * Returns: an error indicating either a failure to retrieve a queue's
+ *			hardware information or one or more source queues are not
+ *			compatible with the corresponding queue on this system; otherwise,
+ *			returns zero to indicate compatibility.
+ */
+static int verify_ap_configs_are_compatible(const char *mdev_name,
+					    struct vfio_ap_config *source_config)
+{
+	struct ap_tapq_hwinfo src_hwinfo, dest_hwinfo;
+	unsigned long apqn;
+	int ret = 0, rc;
+
+	for (int i = 0; i < source_config->num_queues; i++) {
+		apqn = source_config->qinfo[i].apqn;
+
+		/*
+		 * If we can't get the hardware info for a particular queue, then let's
+		 * capture the function return code and continue so we can log all
+		 * errors to aid in debugging of migration.
+		 */
+		rc = get_hardware_info_for_queue(mdev_name, &dest_hwinfo, apqn);
+		if (rc) {
+			ret = rc;
+			continue;
+		}
+
+		src_hwinfo.value =  source_config->qinfo[i].data;
+
+		if (!queue_hardware_info_is_compatible(mdev_name, apqn,
+						       &src_hwinfo,
+						       &dest_hwinfo))
+			ret = -EINVAL;
+	}
+
+	return ret;
+}
+
+static int do_post_copy_validation(const char *mdev_name,
+				   struct vfio_ap_config *source_config)
+{
+	if (!queues_available(mdev_name, source_config))
+		return -ENODEV;
+
+	if (!control_domains_available(mdev_name, source_config))
+		return -ENODEV;
+
+	return verify_ap_configs_are_compatible(mdev_name, source_config);
+}
+
+/**
+ * setup_ap_matrix_from_ap_config:
+ *
+ * Set the bits corresponding to the adapters, domains and control domains
+ * from the source guest's AP configuration into an ap_matrix object to be
+ * used to update the destination guest to run on this host.
+ *
+ * @ap_config:		The source guest's AP configuration
+ * @guest_matrix:	The object to be used to update the destination guest's
+ *			AP configuration
+ */
+static void setup_ap_matrix_from_ap_config(struct vfio_ap_config *ap_config,
+					   struct ap_matrix *guest_matrix)
+{
+	struct ap_config_info host_config_info = { 0 };
+	unsigned long apid, apqi, *guest_adm;
+	struct vfio_ap_queue_info qinfo;
+
+	ap_qci(&host_config_info);
+	/*
+	 * Zero the bitmaps before calling vfio_ap_matrix_init(), which only
+	 * sets the apm_max/aqm_max/adm_max scalar fields and leaves the bitmap
+	 * arrays untouched.  Without this, stack garbage in guest_matrix->apm,
+	 * ->aqm, and ->adm would grant the destination guest access to
+	 * arbitrary unassigned queues and control domains.
+	 */
+	memset(guest_matrix->apm, 0, sizeof(guest_matrix->apm));
+	memset(guest_matrix->aqm, 0, sizeof(guest_matrix->aqm));
+	memset(guest_matrix->adm, 0, sizeof(guest_matrix->adm));
+	vfio_ap_matrix_init(&host_config_info, guest_matrix);
+
+	for (int i = 0; i < ap_config->num_queues; i++) {
+		qinfo = ap_config->qinfo[i];
+		apid = AP_QID_CARD(qinfo.apqn);
+		apqi = AP_QID_QUEUE(qinfo.apqn);
+
+		if (!test_bit_inv(apid, guest_matrix->apm))
+			set_bit_inv(apid, guest_matrix->apm);
+		if (!test_bit_inv(apqi, guest_matrix->aqm))
+			set_bit_inv(apqi, guest_matrix->aqm);
+	}
+
+	guest_adm = (unsigned long *)ap_config->adm;
+	for_each_set_bit_inv(apqi, guest_adm, AP_DOMAINS) {
+		if (!test_bit_inv(apqi, guest_matrix->adm))
+			set_bit_inv(apqi, guest_matrix->adm);
+	}
+}
+
+static ssize_t vfio_ap_resuming_write(struct file *filp, const char __user *buf,
+				      size_t len, loff_t *pos)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+	struct vfio_ap_config *ap_config;
+	struct ap_matrix guest_matrix;
+	bool new_allocation = false;
+	loff_t write_pos;
+	ssize_t ret = 0, cfg_sz;
+	const char *mdev_name;
+
+	/*
+	 * When userspace calls write() with an explicit offset (pwrite), pos is
+	 * non-NULL and the function rejects it with -ESPIPE (illegal seek). For
+	 * normal write() calls, pos is NULL, so we'll use the file's internal
+	 * position filp->f_pos
+	 */
+	if (pos)
+		return -ESPIPE;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	pos = &filp->f_pos;
+
+	ret = validate_resuming_write_parms(filp, len, pos);
+	if (ret) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return ret;
+	}
+
+	matrix_mdev = filp->private_data;
+	mdev_name = dev_name(matrix_mdev->vdev.dev);
+
+	/*
+	 * If this is the first write operation, allocate storage for the AP
+	 * configuration sized to fit the full payload (validate_resuming_write_parms
+	 * guarantees num_queues is present in buf).  For subsequent writes the
+	 * buffer is already correctly sized; just reuse it.
+	 */
+	if (*pos == 0) {
+		ret = allocate_ap_config(&ap_config, buf, len);
+		if (ret < 0) {
+			mutex_unlock(&matrix_dev->mdevs_lock);
+			return ret;
+		}
+
+		cfg_sz = ret;
+		new_allocation = true;
+	} else {
+		ap_config = matrix_mdev->mig_data->resuming_mig_file.ap_config;
+		cfg_sz = matrix_mdev->mig_data->resuming_mig_file.config_sz;
+	}
+
+	if (*pos + len > cfg_sz) {
+		if (new_allocation)
+			kvfree(ap_config);
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return -EIO;
+	}
+
+	/*
+	 * Snapshot and advance *pos under the lock before dropping it for
+	 * copy_from_user().  This prevents concurrent write()s on the same
+	 * stream file from computing the same destination offset and clobbering
+	 * each other's data or racing to reassign mig_data->resuming_mig_file.
+	 */
+	write_pos = *pos;
+	*pos += len;
+
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	if (copy_from_user((char *)ap_config + write_pos, buf, len)) {
+		if (new_allocation)
+			kvfree(ap_config);
+		return -EIO;
+	}
+
+	/* Check if we've completed writing the entire configuration */
+	if (write_pos + len == cfg_sz) {
+		/*
+		 * do_post_copy_validation() calls ap_tapq() which is a slow
+		 * hardware instruction.  Run it before acquiring the update
+		 * locks to avoid holding guests_lock, kvm->lock, and
+		 * mdevs_lock across the hardware calls.
+		 */
+		ret = do_post_copy_validation(mdev_name, ap_config);
+		if (ret < 0) {
+			if (new_allocation)
+				kvfree(ap_config);
+			return ret;
+		}
+
+		setup_ap_matrix_from_ap_config(ap_config, &guest_matrix);
+
+		mutex_lock(&ap_attr_mutex);
+		get_update_locks_for_mdev(matrix_mdev);
+
+		/*
+		 * Verify the device wasn't closed while mdevs_lock was dropped
+		 * for the copy_from_user and do_post_copy_validation above.
+		 * get_update_locks_for_mdev() reacquires mdevs_lock.
+		 */
+		if (!matrix_mdev->mig_data) {
+			release_update_locks_for_mdev(matrix_mdev);
+			mutex_unlock(&ap_attr_mutex);
+			if (new_allocation)
+				kvfree(ap_config);
+			return -ENODEV;
+		}
+
+		ret = vfio_ap_set_new_guest_config(matrix_mdev, &guest_matrix);
+
+		release_update_locks_for_mdev(matrix_mdev);
+		mutex_unlock(&ap_attr_mutex);
+
+		if (ret) {
+			if (new_allocation)
+				kvfree(ap_config);
+			return ret;
+		}
+	}
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	/*
+	 * Re-read mig_data under the lock; the device could have been closed
+	 * concurrently while the lock was dropped for copy_from_user().
+	 */
+	if (!matrix_mdev->mig_data) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		if (new_allocation)
+			kvfree(ap_config);
+		return -ENODEV;
+	}
+	if (new_allocation)
+		kvfree(matrix_mdev->mig_data->resuming_mig_file.ap_config);
+	matrix_mdev->mig_data->resuming_mig_file.ap_config = ap_config;
+	matrix_mdev->mig_data->resuming_mig_file.config_sz = cfg_sz;
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	return len;
+}
+
+static const struct file_operations vfio_ap_resume_fops = {
+	.owner = THIS_MODULE,
+	.write = vfio_ap_resuming_write,
+	.release = vfio_ap_release_mig_file,
+};
+
+static struct file *vfio_ap_resuming_init(struct ap_matrix_mdev *matrix_mdev)
+{
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	return vfio_ap_open_file_stream(matrix_mdev, &vfio_ap_resume_fops, O_WRONLY);
+}
+
+static struct file *
+vfio_ap_transition_to_state(struct ap_matrix_mdev *matrix_mdev,
+			    enum vfio_device_mig_state new_state)
+{
+	struct vfio_ap_migration_data *mig_data;
+	enum vfio_device_mig_state cur_state;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+	mig_data = matrix_mdev->mig_data;
+	cur_state = mig_data->mig_state;
+	dev_dbg(matrix_mdev->vdev.dev, "%s: %d -> %d\n", __func__, cur_state,
+		new_state);
+
+	/*
+	 * Begins the process of saving the vfio device state by creating and
+	 * returning a streaming data_fd to be used to read out the internal
+	 * state of the vfio-ap device on the source host.
+	 */
+	if (cur_state == VFIO_DEVICE_STATE_STOP &&
+	    new_state == VFIO_DEVICE_STATE_STOP_COPY) {
+		struct file *filp = vfio_ap_open_file_stream(matrix_mdev,
+							     &vfio_ap_stop_copy_fops,
+							     O_RDONLY);
+		if (IS_ERR(filp))
+			return ERR_CAST(filp);
+
+		mig_data->stop_copy_mig_file.filp = filp;
+
+		return filp;
+	}
+
+	/*
+	 * Begins the process of restoring the vfio device state by creating and
+	 * returning a streaming data_fd to be used to read in the internal
+	 * state of the vfio-ap device on the destination host.
+	 */
+	if (cur_state == VFIO_DEVICE_STATE_STOP &&
+	    new_state == VFIO_DEVICE_STATE_RESUMING) {
+		struct file *filp = vfio_ap_resuming_init(matrix_mdev);
+
+		if (IS_ERR(filp))
+			return ERR_CAST(filp);
+
+		mig_data->resuming_mig_file.filp = filp;
+		return filp;
+	}
+
+	/*
+	 * Terminates the data transfer session of the vfio-ap device state
+	 * between the source and target hosts. Since the vfio-ap device does
+	 * not virtualize a DMA device, there is no internal device state to
+	 * incorporate into the vfio-ap device on the target.
+	 */
+	if ((cur_state == VFIO_DEVICE_STATE_RESUMING &&
+	     new_state == VFIO_DEVICE_STATE_STOP) ||
+	    (cur_state == VFIO_DEVICE_STATE_STOP_COPY &&
+	     new_state == VFIO_DEVICE_STATE_STOP)) {
+		return NULL;
+	}
+
+	/*
+	 * These states indicate migration has either not been initiated or
+	 * has completed and the vfio-ap device is operating normally. Since the
+	 * vfio-ap device does not virtualize a DMA device, there is no internal
+	 * device state to incorporate into the vfio-ap device on the target.
+	 */
+	if ((cur_state == VFIO_DEVICE_STATE_STOP &&
+	     new_state == VFIO_DEVICE_STATE_RUNNING) ||
+	    (cur_state == VFIO_DEVICE_STATE_RUNNING &&
+	     new_state == VFIO_DEVICE_STATE_STOP)) {
+		return NULL;
+	}
+
+	/* vfio_mig_get_next_state() does not use arcs other than the above */
+	WARN_ON(true);
+
+	return ERR_PTR(-EINVAL);
+}
+
+static struct file *vfio_ap_set_state(struct vfio_device *vdev,
+				      enum vfio_device_mig_state  new_state)
+{
+	int ret;
+	struct file *filp = NULL;
+	struct ap_matrix_mdev *matrix_mdev;
+	enum vfio_device_mig_state next_state;
+	struct vfio_ap_migration_data *mig_data;
+
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	if (ap_is_se_guest()) {
+		dev_err_once(matrix_mdev->vdev.dev,
+			     "Migration not allowed from or to a Secure Execution guest\n");
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		return ERR_PTR(-EPERM);
+	}
+
+	mig_data = matrix_mdev->mig_data;
+
+	/*
+	 * The mig_data pointer is set in the vfio_ap_init_migration_data
+	 * function which is called when the vfio-ap device fd is opened.
+	 * Since the implicit pre-open state is RUNNING, a request to set
+	 * RUNNING is a no-op. Any other state transition is invalid before
+	 * open_device.
+	 */
+	if (!mig_data) {
+		mutex_unlock(&matrix_dev->mdevs_lock);
+		if (new_state == VFIO_DEVICE_STATE_RUNNING)
+			return NULL;
+		return ERR_PTR(-ENODEV);
+	}
+
+	dev_dbg(vdev->dev, "%s -> %d\n", __func__, new_state);
+
+	while (mig_data->mig_state != VFIO_DEVICE_STATE_ERROR &&
+	       mig_data->mig_state != new_state) {
+		ret = vfio_mig_get_next_state(vdev, mig_data->mig_state,
+					      new_state, &next_state);
+		if (ret) {
+			filp = ERR_PTR(ret);
+			break;
+		}
+
+		filp = vfio_ap_transition_to_state(matrix_mdev, next_state);
+		if (IS_ERR(filp))
+			break;
+
+		mig_data->mig_state = next_state;
+
+		if (WARN_ON(filp && new_state != next_state)) {
+			fput(filp);
+			filp = ERR_PTR(-EINVAL);
+			break;
+		}
+	}
+
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	return filp;
+}
+
+static int vfio_ap_get_state(struct vfio_device *vdev,
+			     enum vfio_device_mig_state  *current_state)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+	struct vfio_ap_migration_data *mig_data;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+	mig_data =  matrix_mdev->mig_data;
+
+	/*
+	 * The mig_data pointer is set in the vfio_ap_init_migration_data
+	 * function which is called when the vfio-ap device fd is opened.
+	 * If mig_data is NULL, report RUNNING as the implicit pre-open state
+	 * so userspace doesn't need to perform any state transition before the
+	 * device becomes active.
+	 */
+	*current_state = (mig_data) ? mig_data->mig_state :
+				      VFIO_DEVICE_STATE_RUNNING;
+
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	return 0;
+}
+
+static int vfio_ap_get_data_size(struct vfio_device *vdev,
+				 unsigned long *stop_copy_length)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+	size_t qinfo_sz;
+	int num_queues;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+	num_queues = vfio_ap_mdev_get_num_queues(&matrix_mdev->shadow_apcb);
+	qinfo_sz = num_queues * sizeof(struct vfio_ap_queue_info);
+	*stop_copy_length = qinfo_sz + sizeof(struct vfio_ap_config);
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	return 0;
+}
+
+static const struct vfio_migration_ops vfio_ap_migration_ops = {
+	.migration_set_state = vfio_ap_set_state,
+	.migration_get_state = vfio_ap_get_state,
+	.migration_get_data_size = vfio_ap_get_data_size,
+};
+
+/**
+ * vfio_ap_init_migrations_capabilities - initialize migration capabilities
+ *
+ * @matrix_mdev: pointer to object containing the mdev state
+ */
+void vfio_ap_init_migration_capabilities(struct ap_matrix_mdev *matrix_mdev)
+{
+	/* Live guest migration is not supported for SE guests */
+	if (ap_is_se_guest())
+		return;
+
+	matrix_mdev->vdev.migration_flags = VFIO_MIGRATION_STOP_COPY;
+	matrix_mdev->vdev.mig_ops = &vfio_ap_migration_ops;
+}
+
+/**
+ * vfio_ap_init_migration_data - initialize migration data and functions
+ *
+ * @matrix_mdev: pointer to object containing the mdev state
+ *
+ * Return: zero if initialization is successful; otherwise, returns a error.
+ */
+int vfio_ap_init_migration_data(struct ap_matrix_mdev *matrix_mdev)
+{
+	struct vfio_ap_migration_data *mig_data;
+
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	mig_data = kzalloc_obj(struct vfio_ap_migration_data, GFP_KERNEL);
+	if (!mig_data)
+		return -ENOMEM;
+
+	mig_data->mig_state = VFIO_DEVICE_STATE_RUNNING;
+	matrix_mdev->mig_data = mig_data;
+
+	return 0;
+}
+
+static void vfio_ap_release_mig_files(struct ap_matrix_mdev *matrix_mdev)
+{
+	struct vfio_ap_migration_data *mig_data;
+
+	/*
+	 * The fput call does not call .release synchronously while the
+	 * mdevs_lock mutex is held, so there is no problem with incurring a
+	 * deadlock situation if fput is executed in this function.
+	 */
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	mig_data = matrix_mdev->mig_data;
+	if (!mig_data)
+		return;
+
+	if (mig_data->stop_copy_mig_file.filp) {
+		fput(mig_data->stop_copy_mig_file.filp);
+		mig_data->stop_copy_mig_file.filp = NULL;
+	}
+
+	kfree(mig_data->stop_copy_mig_file.ap_config);
+	mig_data->stop_copy_mig_file.ap_config = NULL;
+	mig_data->stop_copy_mig_file.config_sz = 0;
+
+	if (mig_data->resuming_mig_file.filp) {
+		fput(mig_data->resuming_mig_file.filp);
+		mig_data->resuming_mig_file.filp = NULL;
+	}
+
+	kvfree(mig_data->resuming_mig_file.ap_config);
+	mig_data->resuming_mig_file.ap_config = NULL;
+	mig_data->resuming_mig_file.config_sz = 0;
+}
+
+/**
+ * vfio_ap_release_migration_data: reclaim private migration data
+ *
+ * @vdev: pointer to the mdev
+ */
+void vfio_ap_release_migration_data(struct ap_matrix_mdev *matrix_mdev)
+{
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	if (!matrix_mdev->mig_data)
+		return;
+
+	/*
+	 * Drop the driver's get_file() references on any open migration FDs
+	 * and free the associated ap_config buffers before freeing mig_data.
+	 * This ensures that if the device is closed while a migration FD is
+	 * still held by userspace, vfio_ap_release_mig_file() will see
+	 * mig_data == NULL and skip the cleanup (the fput() here will
+	 * eventually trigger .release, but mig_data is gone by then).
+	 */
+	vfio_ap_release_mig_files(matrix_mdev);
+	kfree(matrix_mdev->mig_data);
+	matrix_mdev->mig_data = NULL;
+}
+
+/**
+ * vfio_ap_reset_migration_state - Reset the vfio-ap migration state
+ *
+ * @matrix_mdev: pointer to the object maintaining the vfio-ap device state
+ *
+ * Called during VFIO_DEVICE_RESET to clean up any active migration
+ * state and reset the device to RUNNING state as required by the VFIO
+ * migration specification.
+ */
+void vfio_ap_reset_migration_state(struct ap_matrix_mdev *matrix_mdev)
+{
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	if (!matrix_mdev->mig_data)
+		return;
+
+	vfio_ap_release_mig_files(matrix_mdev);
+	matrix_mdev->mig_data->mig_state = VFIO_DEVICE_STATE_RUNNING;
+}
diff --git a/drivers/s390/crypto/vfio_ap_ops.c b/drivers/s390/crypto/vfio_ap_ops.c
index 44b3a1dcc1b365..0b49be22d5d8b6 100644
--- a/drivers/s390/crypto/vfio_ap_ops.c
+++ b/drivers/s390/crypto/vfio_ap_ops.c
@@ -81,53 +81,6 @@ static inline void release_update_locks_for_kvm(struct kvm *kvm)
 	mutex_unlock(&matrix_dev->guests_lock);
 }
 
-/**
- * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
- *			      KVM guest's APCB in the proper order.
- *
- * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
- *		 configuration data to use to update a KVM guest's APCB.
- *
- * The proper locking order is:
- * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
- *			       guest's APCB.
- * 2. matrix_mdev->kvm->lock:  required to update a guest's APCB
- * 3. matrix_dev->mdevs_lock:  required to access data stored in a matrix_mdev
- *
- * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
- *	 lock will not be taken.
- */
-static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
-{
-	mutex_lock(&matrix_dev->guests_lock);
-	if (matrix_mdev && matrix_mdev->kvm)
-		mutex_lock(&matrix_mdev->kvm->lock);
-	mutex_lock(&matrix_dev->mdevs_lock);
-}
-
-/**
- * release_update_locks_for_mdev: Release the locks used to dynamically update a
- *				  KVM guest's APCB in the proper order.
- *
- * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
- *		 configuration data to use to update a KVM guest's APCB.
- *
- * The proper unlocking order is:
- * 1. matrix_dev->mdevs_lock
- * 2. matrix_mdev->kvm->lock
- * 3. matrix_dev->guests_lock
- *
- * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
- *	 lock will not be released.
- */
-static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
-{
-	mutex_unlock(&matrix_dev->mdevs_lock);
-	if (matrix_mdev && matrix_mdev->kvm)
-		mutex_unlock(&matrix_mdev->kvm->lock);
-	mutex_unlock(&matrix_dev->guests_lock);
-}
-
 /**
  * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
  *			     acquire the locks required to update the APCB of
@@ -642,8 +595,7 @@ static int handle_pqap(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
-static void vfio_ap_matrix_init(struct ap_config_info *info,
-				struct ap_matrix *matrix)
+void vfio_ap_matrix_init(struct ap_config_info *info, struct ap_matrix *matrix)
 {
 	matrix->apm_max = info->apxa ? info->na : 63;
 	matrix->aqm_max = info->apxa ? info->nd : 15;
@@ -775,18 +727,30 @@ static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
 
 static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
 {
-	struct ap_matrix_mdev *matrix_mdev =
-		container_of(vdev, struct ap_matrix_mdev, vdev);
+	struct ap_matrix_mdev *matrix_mdev;
 
+	mutex_lock(&matrix_dev->mdevs_lock);
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
 	matrix_mdev->mdev = to_mdev_device(vdev->dev);
 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
 	matrix_mdev->pqap_hook = handle_pqap;
 	vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
 	hash_init(matrix_mdev->qtable.queues);
+	mutex_unlock(&matrix_dev->mdevs_lock);
 
 	return 0;
 }
 
+static void vfio_ap_mdev_release_dev(struct vfio_device *vdev)
+{
+	struct ap_matrix_mdev *matrix_mdev;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+	vfio_ap_release_migration_data(matrix_mdev);
+	mutex_unlock(&matrix_dev->mdevs_lock);
+}
+
 static int vfio_ap_mdev_probe(struct mdev_device *mdev)
 {
 	struct ap_matrix_mdev *matrix_mdev;
@@ -797,13 +761,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;
 	dev_set_drvdata(&mdev->dev, matrix_mdev);
-	mutex_lock(&matrix_dev->mdevs_lock);
 	list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
 	mutex_unlock(&matrix_dev->mdevs_lock);
 	return 0;
@@ -991,19 +970,20 @@ static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
 	unsigned long apqi;
 
 	for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
-		vfio_ap_mdev_link_apqn(matrix_mdev,
-				       AP_MKQID(apid, apqi));
+		vfio_ap_mdev_link_apqn(matrix_mdev, AP_MKQID(apid, apqi));
 }
 
-static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
-				    unsigned long apid,
-				    struct list_head *qlist)
+static void collect_queues_by_apid(struct ap_matrix_mdev *matrix_mdev,
+				   unsigned long apid,
+				   struct list_head *qlist)
 {
 	struct vfio_ap_queue *q;
 	unsigned long  apqi;
 
 	for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
-		q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
+		q = matrix_mdev ?
+				vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi)) :
+				vfio_ap_find_queue(AP_MKQID(apid, apqi));
 		if (q)
 			list_add_tail(&q->reset_qnode, qlist);
 	}
@@ -1015,7 +995,7 @@ static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
 	struct list_head qlist;
 
 	INIT_LIST_HEAD(&qlist);
-	collect_queues_to_reset(matrix_mdev, apid, &qlist);
+	collect_queues_by_apid(matrix_mdev, apid, &qlist);
 	vfio_ap_mdev_reset_qlist(&qlist);
 }
 
@@ -1031,7 +1011,7 @@ static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
 	INIT_LIST_HEAD(&qlist);
 
 	for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
-		collect_queues_to_reset(matrix_mdev, apid, &qlist);
+		collect_queues_by_apid(matrix_mdev, apid, &qlist);
 
 	return vfio_ap_mdev_reset_qlist(&qlist);
 }
@@ -1702,54 +1682,100 @@ static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
 	bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
 }
 
-static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
-			       const char *buf, size_t count)
+static void get_removed_matrixes(struct ap_matrix *m_removed,
+				 struct ap_matrix *m_old,
+				 struct ap_matrix *m_new)
 {
-	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
-	struct ap_matrix m_new, m_old, m_added, m_removed;
+	bitmap_andnot(m_removed->apm, m_old->apm, m_new->apm, AP_DEVICES);
+	bitmap_andnot(m_removed->aqm, m_old->aqm, m_new->aqm, AP_DOMAINS);
+	bitmap_andnot(m_removed->adm, m_old->adm, m_new->adm, AP_DOMAINS);
+}
+
+static void get_added_matrixes(struct ap_matrix *m_added,
+			       struct ap_matrix *m_old,
+			       struct ap_matrix *m_new)
+{
+	bitmap_andnot(m_added->apm, m_new->apm, m_old->apm, AP_DEVICES);
+	bitmap_andnot(m_added->aqm, m_new->aqm, m_old->aqm, AP_DOMAINS);
+	bitmap_andnot(m_added->adm, m_new->adm, m_old->adm, AP_DOMAINS);
+}
+
+static int validate_new_state(struct ap_matrix_mdev *matrix_mdev)
+{
+	int rc;
+
+	/* Ensure new state is valid, else undo new state */
+	rc = vfio_ap_mdev_validate_masks(matrix_mdev);
+	if (rc)
+		return rc;
+
+	rc = ap_matrix_overflow_check(matrix_mdev);
+	if (rc)
+		return rc;
+
+	return 0;
+}
+
+static void link_new_queues(struct ap_matrix_mdev *matrix_mdev,
+			    struct ap_matrix *m_added)
+{
+	unsigned long apid, apqi;
+
+	for_each_set_bit_inv(apid, m_added->apm, AP_DEVICES)
+		vfio_ap_mdev_link_adapter(matrix_mdev, apid);
+
+	for_each_set_bit_inv(apqi, m_added->aqm, AP_DOMAINS)
+		vfio_ap_mdev_link_domain(matrix_mdev, apqi);
+}
+
+/**
+ * vfio_ap_set_new_guest_config:
+ *
+ * Set a new AP configuration for a guest.
+ *
+ * @matrix_mdev: Object used to maintain the AP configuration for a guest
+ * @m_new:		 Object used to set the new AP configuration
+ *
+ * Returns: zero (0) if the new AP configuration is successfully set; otherwise,
+ *	    returns an error:
+ *
+ *	    ~ EADDRNOTAVAIL One or more APQNs are reserved for host use
+ *	    ~ EADDRINUSE    One or more APQNs are assigned to another mdev
+ *	    ~ ENODEV	    An adapter, domain or control domain in the new
+ *			    AP configuration exceeds the max architected value
+ */
+int vfio_ap_set_new_guest_config(struct ap_matrix_mdev *matrix_mdev,
+				 struct ap_matrix *m_new)
+{
+	struct ap_matrix m_old, m_old_shadow, m_added, m_removed;
 	DECLARE_BITMAP(apm_filtered, AP_DEVICES);
-	unsigned long newbit;
-	char *newbuf, *rest;
-	int rc = count;
 	bool do_update;
+	int rc;
 
-	newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
-	if (!newbuf)
-		return -ENOMEM;
-	rest = newbuf;
-
-	mutex_lock(&ap_attr_mutex);
-	get_update_locks_for_mdev(matrix_mdev);
+	lockdep_assert_held(&ap_attr_mutex);
+	assert_has_update_locks_for_mdev(matrix_mdev);
 
-	/* Save old state */
+	/* Save the old state */
 	ap_matrix_copy(&m_old, &matrix_mdev->matrix);
-	if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
-	    parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
-	    parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
-		rc = -EINVAL;
-		goto out;
-	}
+	ap_matrix_copy(&m_old_shadow, &matrix_mdev->shadow_apcb);
 
-	bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
-	bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
-	bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
-	bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
+	/*
+	 * Get the adapters, domains and control domains added and/or removed
+	 * from the existing configuration
+	 */
+	get_removed_matrixes(&m_removed, &m_old, m_new);
+	get_added_matrixes(&m_added, &m_old, m_new);
 
 	/* Need new bitmaps in matrix_mdev for validation */
-	ap_matrix_copy(&matrix_mdev->matrix, &m_new);
+	ap_matrix_copy(&matrix_mdev->matrix, m_new);
 
 	/* Ensure new state is valid, else undo new state */
-	rc = vfio_ap_mdev_validate_masks(matrix_mdev);
+	rc = validate_new_state(matrix_mdev);
 	if (rc) {
 		ap_matrix_copy(&matrix_mdev->matrix, &m_old);
-		goto out;
+		ap_matrix_copy(&matrix_mdev->shadow_apcb, &m_old_shadow);
+		return rc;
 	}
-	rc = ap_matrix_overflow_check(matrix_mdev);
-	if (rc) {
-		ap_matrix_copy(&matrix_mdev->matrix, &m_old);
-		goto out;
-	}
-	rc = count;
 
 	/* Need old bitmaps in matrix_mdev for unplug/unlink */
 	ap_matrix_copy(&matrix_mdev->matrix, &m_old);
@@ -1759,14 +1785,10 @@ static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr
 	vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
 
 	/* Need new bitmaps in matrix_mdev for linking new adapters/domains */
-	ap_matrix_copy(&matrix_mdev->matrix, &m_new);
-
-	/* Link newly added adapters */
-	for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
-		vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
+	ap_matrix_copy(&matrix_mdev->matrix, m_new);
 
-	for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
-		vfio_ap_mdev_link_domain(matrix_mdev, newbit);
+	/* Link queues associated with the newly added adapters and domains */
+	link_new_queues(matrix_mdev, &m_added);
 
 	/* filter resources not bound to vfio-ap */
 	do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
@@ -1777,7 +1799,39 @@ static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr
 		vfio_ap_mdev_update_guest_apcb(matrix_mdev);
 		reset_queues_for_apids(matrix_mdev, apm_filtered);
 	}
-out:
+
+	return 0;
+}
+
+static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
+			       const char *buf, size_t count)
+{
+	struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
+	struct ap_matrix m_new;
+	char *newbuf, *rest;
+	ssize_t rc;
+
+	newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
+	if (!newbuf)
+		return -ENOMEM;
+	rest = newbuf;
+
+	mutex_lock(&ap_attr_mutex);
+	get_update_locks_for_mdev(matrix_mdev);
+
+	if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
+	    parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
+	    parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
+		kfree(newbuf);
+		release_update_locks_for_mdev(matrix_mdev);
+		mutex_unlock(&ap_attr_mutex);
+		return -EINVAL;
+	}
+
+	rc = vfio_ap_set_new_guest_config(matrix_mdev, &m_new);
+	if (!rc)
+		rc = count;
+
 	release_update_locks_for_mdev(matrix_mdev);
 	mutex_unlock(&ap_attr_mutex);
 	kfree(newbuf);
@@ -2052,19 +2106,39 @@ static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
 
 static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
 {
-	struct ap_matrix_mdev *matrix_mdev =
-		container_of(vdev, struct ap_matrix_mdev, vdev);
+	struct ap_matrix_mdev *matrix_mdev;
+	int ret;
 
 	if (!vdev->kvm)
 		return -EINVAL;
 
-	return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
+	mutex_lock(&matrix_dev->mdevs_lock);
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+	ret = vfio_ap_init_migration_data(matrix_mdev);
+	mutex_unlock(&matrix_dev->mdevs_lock);
+
+	if (ret)
+		return ret;
+
+	ret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
+	if (ret) {
+		/* Clean up migration data on failure */
+		mutex_lock(&matrix_dev->mdevs_lock);
+		vfio_ap_release_migration_data(matrix_mdev);
+		mutex_unlock(&matrix_dev->mdevs_lock);
+	}
+
+	return ret;
 }
 
 static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
 {
-	struct ap_matrix_mdev *matrix_mdev =
-		container_of(vdev, struct ap_matrix_mdev, vdev);
+	struct ap_matrix_mdev *matrix_mdev;
+
+	mutex_lock(&matrix_dev->mdevs_lock);
+	matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
+	vfio_ap_release_migration_data(matrix_mdev);
+	mutex_unlock(&matrix_dev->mdevs_lock);
 
 	vfio_ap_mdev_unset_kvm(matrix_mdev);
 }
@@ -2098,6 +2172,14 @@ static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
 	release_update_locks_for_mdev(matrix_mdev);
 }
 
+int vfio_ap_mdev_get_num_queues(struct ap_matrix *ap_matrix)
+{
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+
+	return bitmap_weight(ap_matrix->apm, AP_DEVICES) *
+	       bitmap_weight(ap_matrix->aqm, AP_DOMAINS);
+}
+
 static int vfio_ap_mdev_get_device_info(unsigned long arg)
 {
 	unsigned long minsz;
@@ -2275,6 +2357,10 @@ static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
 		break;
 	case VFIO_DEVICE_RESET:
 		ret = vfio_ap_mdev_reset_queues(matrix_mdev);
+		if (ret == 0) {
+			/* Reset migration state per VFIO migration spec */
+			vfio_ap_reset_migration_state(matrix_mdev);
+		}
 		break;
 	case VFIO_DEVICE_GET_IRQ_INFO:
 		ret = vfio_ap_get_irq_info(arg);
@@ -2360,6 +2446,7 @@ static const struct attribute_group vfio_queue_attr_group = {
 
 static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
 	.init = vfio_ap_mdev_init_dev,
+	.release = vfio_ap_mdev_release_dev,
 	.open_device = vfio_ap_mdev_open_device,
 	.close_device = vfio_ap_mdev_close_device,
 	.ioctl = vfio_ap_mdev_ioctl,
diff --git a/drivers/s390/crypto/vfio_ap_private.h b/drivers/s390/crypto/vfio_ap_private.h
index 9bff666b0b35d3..150dfce8a674b2 100644
--- a/drivers/s390/crypto/vfio_ap_private.h
+++ b/drivers/s390/crypto/vfio_ap_private.h
@@ -91,6 +91,9 @@ struct ap_queue_table {
 	DECLARE_HASHTABLE(queues, 8);
 };
 
+/* Forward declaration for migration data structure */
+struct vfio_ap_migration_data;
+
 /**
  * struct ap_matrix_mdev - Contains the data associated with a matrix mediated
  *			   device.
@@ -110,6 +113,7 @@ struct ap_queue_table {
  * @aqm_add:	bitmap of APQIs added to the host's AP configuration
  * @adm_add:	bitmap of control domain numbers added to the host's AP
  *		configuration
+ * @mig_data:  vfio device migration data
  */
 struct ap_matrix_mdev {
 	struct vfio_device vdev;
@@ -125,6 +129,7 @@ struct ap_matrix_mdev {
 	DECLARE_BITMAP(apm_add, AP_DEVICES);
 	DECLARE_BITMAP(aqm_add, AP_DOMAINS);
 	DECLARE_BITMAP(adm_add, AP_DOMAINS);
+	struct vfio_ap_migration_data *mig_data;
 };
 
 /**
@@ -152,6 +157,64 @@ struct vfio_ap_queue {
 	struct work_struct reset_work;
 };
 
+/**
+ * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
+ *			      KVM guest's APCB in the proper order.
+ *
+ * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
+ *		 configuration data to use to update a KVM guest's APCB.
+ *
+ * The proper locking order is:
+ * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
+ *			       guest's APCB.
+ * 2. matrix_mdev->kvm->lock:  required to update a guest's APCB
+ * 3. matrix_dev->mdevs_lock:  required to access data stored in a matrix_mdev
+ *
+ * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
+ *	 lock will not be taken.
+ */
+static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
+{
+	mutex_lock(&matrix_dev->guests_lock);
+	if (matrix_mdev && matrix_mdev->kvm)
+		mutex_lock(&matrix_mdev->kvm->lock);
+	mutex_lock(&matrix_dev->mdevs_lock);
+}
+
+/**
+ * release_update_locks_for_mdev: Release the locks used to dynamically update a
+ *				  KVM guest's APCB in the proper order.
+ *
+ * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
+ *		 configuration data to use to update a KVM guest's APCB.
+ *
+ * The proper unlocking order is:
+ * 1. matrix_dev->mdevs_lock
+ * 2. matrix_mdev->kvm->lock
+ * 3. matrix_dev->guests_lock
+ *
+ * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
+ *	 lock will not be released.
+ */
+static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
+{
+	mutex_unlock(&matrix_dev->mdevs_lock);
+	if (matrix_mdev && matrix_mdev->kvm)
+		mutex_unlock(&matrix_mdev->kvm->lock);
+	mutex_unlock(&matrix_dev->guests_lock);
+}
+
+static inline void
+assert_has_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
+{
+	lockdep_assert_held(&matrix_dev->guests_lock);
+	if (matrix_mdev && matrix_mdev->kvm)
+		lockdep_assert_held(&matrix_mdev->kvm->lock);
+	lockdep_assert_held(&matrix_dev->mdevs_lock);
+}
+
+int vfio_ap_mdev_get_num_queues(struct ap_matrix *ap_matrix);
+
 int vfio_ap_mdev_register(void);
 void vfio_ap_mdev_unregister(void);
 
@@ -165,4 +228,14 @@ void vfio_ap_on_cfg_changed(struct ap_config_info *new_config_info,
 void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
 			      struct ap_config_info *old_config_info);
 
+void vfio_ap_matrix_init(struct ap_config_info *info, struct ap_matrix *matrix);
+
+void vfio_ap_init_migration_capabilities(struct ap_matrix_mdev *matrix_mdev);
+int vfio_ap_init_migration_data(struct ap_matrix_mdev *matrix_mdev);
+void vfio_ap_release_migration_data(struct ap_matrix_mdev *matrix_mdev);
+void vfio_ap_reset_migration_state(struct ap_matrix_mdev *matrix_mdev);
+
+int vfio_ap_set_new_guest_config(struct ap_matrix_mdev *matrix_mdev,
+				 struct ap_matrix *m_new);
+
 #endif /* _VFIO_AP_PRIVATE_H_ */

3/2 2026/07/28 13:22 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 37573
							output: 37
							thoughts: 454
						

Thoughts:
**Analyzing the VFIO AP Migration Patch**

Okay, so I'm looking at this patch that introduces migration support for `vfio_ap`. My initial thought is to understand the scope and how it's implemented. I see the changes are focused on s390's AP (Adjunct Processor) matrix devices. Specifically, this patch touches `vfio_ap_drv.c`, `vfio_ap_ops.c`, `vfio_ap_private.h`, and adds `vfio_ap_migration.c`. It sounds like it’s implementing the necessary VFIO migration callbacks: `migration_set_state`, `migration_get_state`, and `migration_get_data_size`, handling the reading and writing of migration state via a file descriptor.

Now, the important question arises: Is this code actually *reachable* in a typical QEMU setup? VFIO AP is pretty specialized, tied to the s390x architecture. It relies on the AP hardware or a decent emulation of it. The next logical step is to see if we can fuzz this functionality. Because it's VFIO, it *should* be accessible from userspace through VFIO ioctls and sysfs, right? I'd love to see if someone can exploit this.

But here's a wrinkle: To create a `vfio_ap` mdev, we need the `vfio_ap` module, which inherently depends on the s390 AP bus. So, I have to make sure the s390 AP bus is present and active in the QEMU environment. Checking my memory... yes, QEMU has *some* support for s390 AP, but the real question is whether it *requires* actual physical AP hardware (or very accurate emulation) to create an mdev?

To clarify that, I'll need to dig into the driver code, specifically `drivers/s390/crypto/vfio_ap_drv.c` or perhaps `drivers/s390/crypto/ap_bus.c`. The `vfio_ap` driver registers an mdev driver, so to create an mdev, we would write to something like `/sys/devices/vfio_ap/matrix/mdev_supported_types/vfio_ap-passthrough/create`. The issue is does `/sys/devices/vfio_ap/matrix` get created unconditionally when the module is loaded? I should probably check `drivers/s390/crypto/vfio_ap_drv.c` to be sure.



4/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "vfio_ap_matrix_dev",
  "PathPrefix": "drivers/s390/crypto/"
}

Results:
{
  "Output": "drivers/s390/crypto/vfio_ap_drv.c=60=static struct ap_driver vfio_ap_drv = {\n--\ndrivers/s390/crypto/vfio_ap_drv.c-68-\ndrivers/s390/crypto/vfio_ap_drv.c:69:static void vfio_ap_matrix_dev_release(struct device *dev)\ndrivers/s390/crypto/vfio_ap_drv.c-70-{\n--\ndrivers/s390/crypto/vfio_ap_drv.c=81=static struct device_driver matrix_driver = {\n--\ndrivers/s390/crypto/vfio_ap_drv.c-87-\ndrivers/s390/crypto/vfio_ap_drv.c:88:static int vfio_ap_matrix_dev_create(void)\ndrivers/s390/crypto/vfio_ap_drv.c-89-{\n--\ndrivers/s390/crypto/vfio_ap_drv.c-120-\tmatrix_dev-\u003edevice.bus = \u0026matrix_bus;\ndrivers/s390/crypto/vfio_ap_drv.c:121:\tmatrix_dev-\u003edevice.release = vfio_ap_matrix_dev_release;\ndrivers/s390/crypto/vfio_ap_drv.c-122-\tmatrix_dev-\u003evfio_ap_drv = \u0026vfio_ap_drv;\n--\ndrivers/s390/crypto/vfio_ap_drv.c-144-\ndrivers/s390/crypto/vfio_ap_drv.c:145:static void vfio_ap_matrix_dev_destroy(void)\ndrivers/s390/crypto/vfio_ap_drv.c-146-{\n--\ndrivers/s390/crypto/vfio_ap_drv.c=169=static int __init vfio_ap_init(void)\n--\ndrivers/s390/crypto/vfio_ap_drv.c-180-\ndrivers/s390/crypto/vfio_ap_drv.c:181:\tret = vfio_ap_matrix_dev_create();\ndrivers/s390/crypto/vfio_ap_drv.c-182-\tif (ret)\n--\ndrivers/s390/crypto/vfio_ap_drv.c-186-\tif (ret) {\ndrivers/s390/crypto/vfio_ap_drv.c:187:\t\tvfio_ap_matrix_dev_destroy();\ndrivers/s390/crypto/vfio_ap_drv.c-188-\t\treturn ret;\n--\ndrivers/s390/crypto/vfio_ap_drv.c-193-\t\tap_driver_unregister(\u0026vfio_ap_drv);\ndrivers/s390/crypto/vfio_ap_drv.c:194:\t\tvfio_ap_matrix_dev_destroy();\ndrivers/s390/crypto/vfio_ap_drv.c-195-\n--\ndrivers/s390/crypto/vfio_ap_drv.c=202=static void __exit vfio_ap_exit(void)\n--\ndrivers/s390/crypto/vfio_ap_drv.c-205-\tap_driver_unregister(\u0026vfio_ap_drv);\ndrivers/s390/crypto/vfio_ap_drv.c:206:\tvfio_ap_matrix_dev_destroy();\ndrivers/s390/crypto/vfio_ap_drv.c-207-\tdebug_unregister(vfio_ap_dbf_info);\n--\ndrivers/s390/crypto/vfio_ap_ops.c=37=static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);\ndrivers/s390/crypto/vfio_ap_ops.c:38:static const struct vfio_device_ops vfio_ap_matrix_dev_ops;\ndrivers/s390/crypto/vfio_ap_ops.c-39-static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);\n--\ndrivers/s390/crypto/vfio_ap_ops.c=754=static int vfio_ap_mdev_probe(struct mdev_device *mdev)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-759-\tmatrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, \u0026mdev-\u003edev,\ndrivers/s390/crypto/vfio_ap_ops.c:760:\t\t\t\t\t\u0026vfio_ap_matrix_dev_ops);\ndrivers/s390/crypto/vfio_ap_ops.c-761-\tif (IS_ERR(matrix_mdev))\n--\ndrivers/s390/crypto/vfio_ap_ops.c=2443=static const struct attribute_group vfio_queue_attr_group = {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2446-\ndrivers/s390/crypto/vfio_ap_ops.c:2447:static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {\ndrivers/s390/crypto/vfio_ap_ops.c-2448-\t.init = vfio_ap_mdev_init_dev,\n"
}

5/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 40002
							output: 39
							thoughts: 42
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience:

**Investigating the vfio_ap Matrix Device Creation**

Alright, so `vfio_ap_matrix_dev_create` is responsible for conjuring up the `/sys/devices/vfio_ap/matrix` device. My immediate thought is, "What's the *real* dependency here?" Is it truly a simple creation, or does it hinge on some underlying Advanced Processor (AP) hardware actually being present and visible to the system? I need to dig deeper. I'm already formulating questions: Is there a check for AP availability before this function gets called? Does it attempt to probe the AP hardware in any way? This is key because that device's existence or non-existence is a big clue to whether the vfio-ap driver is behaving as it should in a given system configuration. I'm going to follow the code path and see where that dependency truly lies. This could be nothing, it could be a simple device registration... or it could be a more intricate dance with AP hardware features. Time to get to the details.



6/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "vfio_ap_matrix_dev_create",
  "PathPrefix": "drivers/s390/crypto/"
}

Results:
{
  "Output": "drivers/s390/crypto/vfio_ap_drv.c=81=static struct device_driver matrix_driver = {\n--\ndrivers/s390/crypto/vfio_ap_drv.c-87-\ndrivers/s390/crypto/vfio_ap_drv.c:88:static int vfio_ap_matrix_dev_create(void)\ndrivers/s390/crypto/vfio_ap_drv.c-89-{\n--\ndrivers/s390/crypto/vfio_ap_drv.c=169=static int __init vfio_ap_init(void)\n--\ndrivers/s390/crypto/vfio_ap_drv.c-180-\ndrivers/s390/crypto/vfio_ap_drv.c:181:\tret = vfio_ap_matrix_dev_create();\ndrivers/s390/crypto/vfio_ap_drv.c-182-\tif (ret)\n"
}

7/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 40575
							output: 44
							thoughts: 18
						

Thoughts:
Here's my attempt at summarizing the thought process, tailored for an expert audience:

**Diving into `vfio_ap_matrix_dev_create`**

Okay, time to get my hands dirty. I'm going to take a closer look at the `vfio_ap_matrix_dev_create` implementation. I need to understand exactly how this function handles the creation of these AP (Addressable Protection) matrix devices within the VFIO framework. I want to dissect the low-level interactions and understand the nuances in the code. I'll focus on the data structures it manipulates, any error checking it performs, and how it integrates with the overall VFIO device model. I'm particularly interested in how it handles resource allocation, device initialization, and the mapping of physical memory. Understanding these details is crucial for debugging and optimization. Let's see what's really happening under the hood.



8/2 2026/07/28 13:23 tool read-file
0m Args:
{
  "File": "drivers/s390/crypto/vfio_ap_drv.c",
  "FirstLine": 88,
  "LineCount": 50
}

Results:
{
  "Contents": "  88:\tstatic int vfio_ap_matrix_dev_create(void)\n  89:\t{\n  90:\t\tint ret;\n  91:\t\tstruct device *root_device;\n  92:\t\n  93:\t\troot_device = root_device_register(VFIO_AP_ROOT_NAME);\n  94:\t\tif (IS_ERR(root_device))\n  95:\t\t\treturn PTR_ERR(root_device);\n  96:\t\n  97:\t\tret = bus_register(\u0026matrix_bus);\n  98:\t\tif (ret)\n  99:\t\t\tgoto bus_register_err;\n 100:\t\n 101:\t\tmatrix_dev = kzalloc_obj(*matrix_dev);\n 102:\t\tif (!matrix_dev) {\n 103:\t\t\tret = -ENOMEM;\n 104:\t\t\tgoto matrix_alloc_err;\n 105:\t\t}\n 106:\t\n 107:\t\t/* Fill in config info via PQAP(QCI), if available */\n 108:\t\tif (test_facility(12)) {\n 109:\t\t\tret = ap_qci(\u0026matrix_dev-\u003einfo);\n 110:\t\t\tif (ret)\n 111:\t\t\t\tgoto matrix_alloc_err;\n 112:\t\t}\n 113:\t\n 114:\t\tmutex_init(\u0026matrix_dev-\u003emdevs_lock);\n 115:\t\tINIT_LIST_HEAD(\u0026matrix_dev-\u003emdev_list);\n 116:\t\tmutex_init(\u0026matrix_dev-\u003eguests_lock);\n 117:\t\n 118:\t\tdev_set_name(\u0026matrix_dev-\u003edevice, \"%s\", VFIO_AP_DEV_NAME);\n 119:\t\tmatrix_dev-\u003edevice.parent = root_device;\n 120:\t\tmatrix_dev-\u003edevice.bus = \u0026matrix_bus;\n 121:\t\tmatrix_dev-\u003edevice.release = vfio_ap_matrix_dev_release;\n 122:\t\tmatrix_dev-\u003evfio_ap_drv = \u0026vfio_ap_drv;\n 123:\t\n 124:\t\tret = device_register(\u0026matrix_dev-\u003edevice);\n 125:\t\tif (ret)\n 126:\t\t\tgoto matrix_reg_err;\n 127:\t\n 128:\t\tret = driver_register(\u0026matrix_driver);\n 129:\t\tif (ret)\n 130:\t\t\tgoto matrix_drv_err;\n 131:\t\n 132:\t\treturn 0;\n 133:\t\n 134:\tmatrix_drv_err:\n 135:\t\tdevice_del(\u0026matrix_dev-\u003edevice);\n 136:\tmatrix_reg_err:\n 137:\t\tput_device(\u0026matrix_dev-\u003edevice);\n"
}

9/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 41556
							output: 45
							thoughts: 84
						

Thoughts:
Okay, here's my interpretation of this process, considering my expertise in this area:

**Initial Device Discovery and API Check**

Alright, so the first thing I see is that it's unconditionally registering the root device and bus. That's a fundamental step, likely setting the stage for interaction with the hardware. It's essentially laying the groundwork for device communication before any specific functionality is enabled. My instincts are telling me this part is fairly straightforward - a basic device enumeration process.

Now, here's where it gets interesting. It's checking for facility 12, which I immediately recognize as the indicator for Crypto Express Coprocessor (CEX) or an equivalent. *If* that facility is available, it's calling `ap_qci`. That makes perfect sense. `ap_qci` is the API for Query Crypto Information. That API retrieves information about the cryptographic coprocessor. If that call *fails*, it immediately returns an error. This likely indicates a problem with either the coprocessor itself or the setup. Essentially, if this crucial query fails, there's no point in continuing.

On the other hand, *if* facility 12 isn't available, or the API call succeeds, it *proceeds* to the next section. It's important to understand the branching logic here. It is an *if* *else* sequence. The existence of CEXes on the system affects the entire process, either for good, by calling the API, or, for bad, by erroring out.

And wait a second... now I'm seeing `vfio_ap_init` in `drivers/s390/crypto/vfio_ap_drv.c`. Okay, *that* changes things. VFIO (Virtual Function I/O) means this is likely related to virtualized access to cryptographic hardware. So, the whole process is now starting to unfold. It’s registering the root device, then checking the existence of a crypto facility, then moving on to VFIO.



10/2 2026/07/28 13:23 tool read-file
0m Args:
{
  "File": "drivers/s390/crypto/vfio_ap_drv.c",
  "FirstLine": 169,
  "LineCount": 50
}

Results:
{
  "Contents": " 169:\tstatic int __init vfio_ap_init(void)\n 170:\t{\n 171:\t\tint ret;\n 172:\t\n 173:\t\tret = vfio_ap_dbf_info_init();\n 174:\t\tif (ret)\n 175:\t\t\treturn ret;\n 176:\t\n 177:\t\t/* If there are no AP instructions, there is nothing to pass through. */\n 178:\t\tif (!ap_instructions_available())\n 179:\t\t\treturn -ENODEV;\n 180:\t\n 181:\t\tret = vfio_ap_matrix_dev_create();\n 182:\t\tif (ret)\n 183:\t\t\treturn ret;\n 184:\t\n 185:\t\tret = ap_driver_register(\u0026vfio_ap_drv, THIS_MODULE, VFIO_AP_DRV_NAME);\n 186:\t\tif (ret) {\n 187:\t\t\tvfio_ap_matrix_dev_destroy();\n 188:\t\t\treturn ret;\n 189:\t\t}\n 190:\t\n 191:\t\tret = vfio_ap_mdev_register();\n 192:\t\tif (ret) {\n 193:\t\t\tap_driver_unregister(\u0026vfio_ap_drv);\n 194:\t\t\tvfio_ap_matrix_dev_destroy();\n 195:\t\n 196:\t\t\treturn ret;\n 197:\t\t}\n 198:\t\n 199:\t\treturn 0;\n 200:\t}\n 201:\t\n 202:\tstatic void __exit vfio_ap_exit(void)\n 203:\t{\n 204:\t\tvfio_ap_mdev_unregister();\n 205:\t\tap_driver_unregister(\u0026vfio_ap_drv);\n 206:\t\tvfio_ap_matrix_dev_destroy();\n 207:\t\tdebug_unregister(vfio_ap_dbf_info);\n 208:\t}\n 209:\t\n 210:\tmodule_init(vfio_ap_init);\n 211:\tmodule_exit(vfio_ap_exit);\n"
}

11/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 42696
							output: 47
							thoughts: 251
						

Thoughts:
**Fuzzing AP Instructions with Syzkaller**

Okay, so the initial check is whether AP instructions are available; if not, we get a `-ENODEV` error. But hang on, QEMU *does* emulate AP instructions, right? The `ap` CPU feature is there. That's the spark! If QEMU handles them, can we fuzz this? Absolutely, syzkaller *can* run on s390x with QEMU.

But... my brain is churning... usually, syzkaller s390x instances are run with KVM or TCG. Does TCG support AP instructions, though? Doesn't matter, if this is reachable in KVM, it's worth the fuzzing effort. And, wait, yes, syzkaller *does* use KVM for s390x.

The next question: Is `CONFIG_VFIO_AP` enabled by default in syzkaller? We need to proactively add `CONFIG_VFIO_AP` to the `EnableConfigs`. The patch adds `vfio_ap_migration.c` and modifies `vfio_ap_ops.c`. Now, my main focus should be going over the functions in `vfio_ap_migration.c` and adding those to `FocusSymbols`. That's where the interesting fuzzing targets likely reside.



12/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "^static.* vfio_ap_.*\\(",
  "PathPrefix": "drivers/s390/crypto/vfio_ap_migration.c"
}

Results:
{
  "Output": "drivers/s390/crypto/vfio_ap_migration.c=134=vfio_ap_release_resuming_file(struct vfio_ap_migration_data *mig_data)\n--\ndrivers/s390/crypto/vfio_ap_migration.c-141-\ndrivers/s390/crypto/vfio_ap_migration.c:142:static int vfio_ap_release_mig_file(struct inode *file_inode, struct file *filp)\ndrivers/s390/crypto/vfio_ap_migration.c-143-{\n--\ndrivers/s390/crypto/vfio_ap_migration.c=186=static int validate_stop_copy_read_parms(struct file *filp, loff_t *pos,\n--\ndrivers/s390/crypto/vfio_ap_migration.c-216-\ndrivers/s390/crypto/vfio_ap_migration.c:217:static size_t vfio_ap_config_size(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_migration.c-218-\t\t\t\t  int *num_queues)\n--\ndrivers/s390/crypto/vfio_ap_migration.c=230=static int get_hardware_info_for_queue(const char *mdev_name,\n--\ndrivers/s390/crypto/vfio_ap_migration.c-265-\ndrivers/s390/crypto/vfio_ap_migration.c:266:static int vfio_ap_store_queue_info(const char *mdev_name,\ndrivers/s390/crypto/vfio_ap_migration.c-267-\t\t\t\t    struct vfio_ap_config *ap_config)\n--\ndrivers/s390/crypto/vfio_ap_migration.c-290-\ndrivers/s390/crypto/vfio_ap_migration.c:291:static int vfio_ap_get_config(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_migration.c-292-{\n--\ndrivers/s390/crypto/vfio_ap_migration.c-341-\ndrivers/s390/crypto/vfio_ap_migration.c:342:static ssize_t vfio_ap_stop_copy_read(struct file *filp, char __user *buf,\ndrivers/s390/crypto/vfio_ap_migration.c-343-\t\t\t\t      size_t len, loff_t *pos)\n--\ndrivers/s390/crypto/vfio_ap_migration.c=1031=static void setup_ap_matrix_from_ap_config(struct vfio_ap_config *ap_config,\n--\ndrivers/s390/crypto/vfio_ap_migration.c-1068-\ndrivers/s390/crypto/vfio_ap_migration.c:1069:static ssize_t vfio_ap_resuming_write(struct file *filp, const char __user *buf,\ndrivers/s390/crypto/vfio_ap_migration.c-1070-\t\t\t\t      size_t len, loff_t *pos)\n--\ndrivers/s390/crypto/vfio_ap_migration.c=1302=static struct file *vfio_ap_set_state(struct vfio_device *vdev,\n--\ndrivers/s390/crypto/vfio_ap_migration.c-1365-\ndrivers/s390/crypto/vfio_ap_migration.c:1366:static int vfio_ap_get_state(struct vfio_device *vdev,\ndrivers/s390/crypto/vfio_ap_migration.c-1367-\t\t\t     enum vfio_device_mig_state  *current_state)\n--\ndrivers/s390/crypto/vfio_ap_migration.c-1391-\ndrivers/s390/crypto/vfio_ap_migration.c:1392:static int vfio_ap_get_data_size(struct vfio_device *vdev,\ndrivers/s390/crypto/vfio_ap_migration.c-1393-\t\t\t\t unsigned long *stop_copy_length)\n--\ndrivers/s390/crypto/vfio_ap_migration.c=1437=int vfio_ap_init_migration_data(struct ap_matrix_mdev *matrix_mdev)\n--\ndrivers/s390/crypto/vfio_ap_migration.c-1452-\ndrivers/s390/crypto/vfio_ap_migration.c:1453:static void vfio_ap_release_mig_files(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_migration.c-1454-{\n"
}

13/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 44541
							output: 46
							thoughts: 18
						

Thoughts:
Here's a summary of my thought process:

**VFIO AP Operations: Investigating the Latest Developments**

Okay, time to dive in and see what's new. My immediate instinct is to check `vfio_ap_ops.c`. This file is the likely spot to find any recent additions or modifications related to the Accelerated Processing (AP) operations within VFIO. I need to get a handle on what the development team has been working on in this area. New functions, changes to existing ones... I want to quickly identify any key updates that might impact performance, compatibility, or overall functionality of VFIO with AP devices. Let's see what we can dig up.



14/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "^.* vfio_ap_.*\\(",
  "PathPrefix": "drivers/s390/crypto/vfio_ap_ops.c"
}

Results:
{
  "Output": "drivers/s390/crypto/vfio_ap_ops.c-34-\ndrivers/s390/crypto/vfio_ap_ops.c:35:static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c:36:static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);\ndrivers/s390/crypto/vfio_ap_ops.c:37:static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);\ndrivers/s390/crypto/vfio_ap_ops.c-38-static const struct vfio_device_ops vfio_ap_matrix_dev_ops;\ndrivers/s390/crypto/vfio_ap_ops.c:39:static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);\ndrivers/s390/crypto/vfio_ap_ops.c-40-\n--\ndrivers/s390/crypto/vfio_ap_ops.c=142=static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-158- */\ndrivers/s390/crypto/vfio_ap_ops.c:159:static struct vfio_ap_queue *vfio_ap_mdev_get_queue(\ndrivers/s390/crypto/vfio_ap_ops.c-160-\t\t\t\t\tstruct ap_matrix_mdev *matrix_mdev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-183- */\ndrivers/s390/crypto/vfio_ap_ops.c:184:static void vfio_ap_wait_for_irqclear(int apqn)\ndrivers/s390/crypto/vfio_ap_ops.c-185-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-221- */\ndrivers/s390/crypto/vfio_ap_ops.c:222:static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-223-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-242- * in progress or IRQ disable command already proceeded: calls\ndrivers/s390/crypto/vfio_ap_ops.c:243: * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear\ndrivers/s390/crypto/vfio_ap_ops.c:244: * and calls vfio_ap_free_aqic_resources() to free the resources associated\ndrivers/s390/crypto/vfio_ap_ops.c-245- * with the AP interrupt handling.\n--\ndrivers/s390/crypto/vfio_ap_ops.c-254- */\ndrivers/s390/crypto/vfio_ap_ops.c:255:static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-256-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-291-/**\ndrivers/s390/crypto/vfio_ap_ops.c:292: * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.\ndrivers/s390/crypto/vfio_ap_ops.c-293- *\n--\ndrivers/s390/crypto/vfio_ap_ops.c-303- */\ndrivers/s390/crypto/vfio_ap_ops.c:304:static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)\ndrivers/s390/crypto/vfio_ap_ops.c-305-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c=334=static int ensure_nib_shared(unsigned long addr)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-368- */\ndrivers/s390/crypto/vfio_ap_ops.c:369:static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,\ndrivers/s390/crypto/vfio_ap_ops.c-370-\t\t\t\t\t\t int isc,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-497- */\ndrivers/s390/crypto/vfio_ap_ops.c:498:static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)\ndrivers/s390/crypto/vfio_ap_ops.c-499-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-526- * - AP_RESPONSE_NORMAL (0) : in case of success\ndrivers/s390/crypto/vfio_ap_ops.c:527: *   Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.\ndrivers/s390/crypto/vfio_ap_ops.c-528- * We take the matrix_dev lock to ensure serialization on queues and\n--\ndrivers/s390/crypto/vfio_ap_ops.c=534=static int handle_pqap(struct kvm_vcpu *vcpu)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-574-\ndrivers/s390/crypto/vfio_ap_ops.c:575:\tq = vfio_ap_mdev_get_queue(matrix_mdev, apqn);\ndrivers/s390/crypto/vfio_ap_ops.c-576-\tif (!q) {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-586-\tif ((status \u003e\u003e (63 - 16)) \u0026 0x01)\ndrivers/s390/crypto/vfio_ap_ops.c:587:\t\tqstatus = vfio_ap_irq_enable(q, status \u0026 0x07, vcpu);\ndrivers/s390/crypto/vfio_ap_ops.c-588-\telse\ndrivers/s390/crypto/vfio_ap_ops.c:589:\t\tqstatus = vfio_ap_irq_disable(q);\ndrivers/s390/crypto/vfio_ap_ops.c-590-\n--\ndrivers/s390/crypto/vfio_ap_ops.c-597-\ndrivers/s390/crypto/vfio_ap_ops.c:598:void vfio_ap_matrix_init(struct ap_config_info *info, struct ap_matrix *matrix)\ndrivers/s390/crypto/vfio_ap_ops.c-599-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c=605=static void signal_guest_ap_cfg_changed(struct ap_matrix_mdev *matrix_mdev)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-610-\ndrivers/s390/crypto/vfio_ap_ops.c:611:static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-612-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-622-\ndrivers/s390/crypto/vfio_ap_ops.c:623:static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-624-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c=635=static bool _queue_passable(struct vfio_ap_queue *q)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-671- */\ndrivers/s390/crypto/vfio_ap_ops.c:672:static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-673-\t\t\t\t       unsigned long *apm_filtered)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-727-\ndrivers/s390/crypto/vfio_ap_ops.c:728:static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)\ndrivers/s390/crypto/vfio_ap_ops.c-729-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-743-\ndrivers/s390/crypto/vfio_ap_ops.c:744:static void vfio_ap_mdev_release_dev(struct vfio_device *vdev)\ndrivers/s390/crypto/vfio_ap_ops.c-745-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-753-\ndrivers/s390/crypto/vfio_ap_ops.c:754:static int vfio_ap_mdev_probe(struct mdev_device *mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-755-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-772-\t * deadlock: vfio_register_emulated_iommu_dev() acquires dev_set-\u003elock\ndrivers/s390/crypto/vfio_ap_ops.c:773:\t * internally, while vfio_ap_mdev_open_device() is called by the VFIO\ndrivers/s390/crypto/vfio_ap_ops.c-774-\t * core with dev_set-\u003elock already held and then acquires mdevs_lock.\n--\ndrivers/s390/crypto/vfio_ap_ops.c-794-\ndrivers/s390/crypto/vfio_ap_ops.c:795:static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-796-\t\t\t\t    struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-797-{\ndrivers/s390/crypto/vfio_ap_ops.c:798:\tif (!q || vfio_ap_mdev_get_queue(matrix_mdev, q-\u003eapqn))\ndrivers/s390/crypto/vfio_ap_ops.c-799-\t\treturn;\n--\ndrivers/s390/crypto/vfio_ap_ops.c-804-\ndrivers/s390/crypto/vfio_ap_ops.c:805:static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)\ndrivers/s390/crypto/vfio_ap_ops.c-806-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-808-\ndrivers/s390/crypto/vfio_ap_ops.c:809:\tq = vfio_ap_find_queue(apqn);\ndrivers/s390/crypto/vfio_ap_ops.c-810-\tvfio_ap_mdev_link_queue(matrix_mdev, q);\n--\ndrivers/s390/crypto/vfio_ap_ops.c-812-\ndrivers/s390/crypto/vfio_ap_ops.c:813:static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-814-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-817-\ndrivers/s390/crypto/vfio_ap_ops.c:818:static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-819-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-822-\ndrivers/s390/crypto/vfio_ap_ops.c:823:static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-824-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-830-\t\t\t\t     AP_DOMAINS) {\ndrivers/s390/crypto/vfio_ap_ops.c:831:\t\t\tq = vfio_ap_mdev_get_queue(matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-832-\t\t\t\t\t\t   AP_MKQID(apid, apqi));\n--\ndrivers/s390/crypto/vfio_ap_ops.c-838-\ndrivers/s390/crypto/vfio_ap_ops.c:839:static void vfio_ap_mdev_remove(struct mdev_device *mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-840-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-858-\ndrivers/s390/crypto/vfio_ap_ops.c:859:static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *assignee,\ndrivers/s390/crypto/vfio_ap_ops.c-860-\t\t\t\t\t struct ap_matrix_mdev *assigned_to,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-872-\ndrivers/s390/crypto/vfio_ap_ops.c:873:static void vfio_ap_mdev_log_in_use_err(struct ap_matrix_mdev *assignee,\ndrivers/s390/crypto/vfio_ap_ops.c-874-\t\t\t\t\tunsigned long *apm, unsigned long *aqm)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-900- */\ndrivers/s390/crypto/vfio_ap_ops.c:901:static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *assignee,\ndrivers/s390/crypto/vfio_ap_ops.c-902-\t\t\t\t\t  unsigned long *mdev_apm,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-955- */\ndrivers/s390/crypto/vfio_ap_ops.c:956:static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-957-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-961-\ndrivers/s390/crypto/vfio_ap_ops.c:962:\treturn vfio_ap_mdev_verify_no_sharing(matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-963-\t\t\t\t\t      matrix_mdev-\u003ematrix.apm,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-966-\ndrivers/s390/crypto/vfio_ap_ops.c:967:static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-968-\t\t\t\t      unsigned long apid)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1002=static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1015-\ndrivers/s390/crypto/vfio_ap_ops.c:1016:\treturn vfio_ap_mdev_reset_qlist(\u0026qlist);\ndrivers/s390/crypto/vfio_ap_ops.c-1017-}\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1053=static ssize_t assign_adapter_store(struct device *dev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1080-\ndrivers/s390/crypto/vfio_ap_ops.c:1081:\tret = vfio_ap_mdev_validate_masks(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-1082-\tif (ret) {\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1103=static struct vfio_ap_queue\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1108-\ndrivers/s390/crypto/vfio_ap_ops.c:1109:\tq = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));\ndrivers/s390/crypto/vfio_ap_ops.c-1110-\t/* If the queue is assigned to the matrix mdev, unlink it. */\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1125- */\ndrivers/s390/crypto/vfio_ap_ops.c:1126:static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1127-\t\t\t\t\tunsigned long apid,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1133-\tfor_each_set_bit_inv(apqi, matrix_mdev-\u003ematrix.aqm, AP_DOMAINS) {\ndrivers/s390/crypto/vfio_ap_ops.c:1134:\t\tq = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);\ndrivers/s390/crypto/vfio_ap_ops.c-1135-\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1143-\ndrivers/s390/crypto/vfio_ap_ops.c:1144:static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1145-\t\t\t\t\t     unsigned long *apids)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1174-\ndrivers/s390/crypto/vfio_ap_ops.c:1175:static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1176-\t\t\t\t\t    unsigned long apid)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1231=static DEVICE_ATTR_WO(unassign_adapter);\ndrivers/s390/crypto/vfio_ap_ops.c-1232-\ndrivers/s390/crypto/vfio_ap_ops.c:1233:static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1234-\t\t\t\t     unsigned long apqi)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1277=static ssize_t assign_domain_store(struct device *dev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1304-\ndrivers/s390/crypto/vfio_ap_ops.c:1305:\tret = vfio_ap_mdev_validate_masks(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-1306-\tif (ret) {\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1325=static DEVICE_ATTR_WO(assign_domain);\ndrivers/s390/crypto/vfio_ap_ops.c-1326-\ndrivers/s390/crypto/vfio_ap_ops.c:1327:static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1328-\t\t\t\t       unsigned long apqi,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1334-\tfor_each_set_bit_inv(apid, matrix_mdev-\u003ematrix.apm, AP_DEVICES) {\ndrivers/s390/crypto/vfio_ap_ops.c:1335:\t\tq = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);\ndrivers/s390/crypto/vfio_ap_ops.c-1336-\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1344-\ndrivers/s390/crypto/vfio_ap_ops.c:1345:static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1346-\t\t\t\t\t    unsigned long *apqis)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1375-\ndrivers/s390/crypto/vfio_ap_ops.c:1376:static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1377-\t\t\t\t\t   unsigned long apqi)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1557=static DEVICE_ATTR_RO(control_domains);\ndrivers/s390/crypto/vfio_ap_ops.c-1558-\ndrivers/s390/crypto/vfio_ap_ops.c:1559:static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)\ndrivers/s390/crypto/vfio_ap_ops.c-1560-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1588=static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1594-\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\ndrivers/s390/crypto/vfio_ap_ops.c:1595:\tnchars = vfio_ap_mdev_matrix_show(\u0026matrix_mdev-\u003ematrix, buf);\ndrivers/s390/crypto/vfio_ap_ops.c-1596-\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1602=static ssize_t guest_matrix_show(struct device *dev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1608-\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\ndrivers/s390/crypto/vfio_ap_ops.c:1609:\tnchars = vfio_ap_mdev_matrix_show(\u0026matrix_mdev-\u003eshadow_apcb, buf);\ndrivers/s390/crypto/vfio_ap_ops.c-1610-\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1703=static int validate_new_state(struct ap_matrix_mdev *matrix_mdev)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1707-\t/* Ensure new state is valid, else undo new state */\ndrivers/s390/crypto/vfio_ap_ops.c:1708:\trc = vfio_ap_mdev_validate_masks(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-1709-\tif (rc)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1719=static void link_new_queues(struct ap_matrix_mdev *matrix_mdev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1746- */\ndrivers/s390/crypto/vfio_ap_ops.c:1747:int vfio_ap_set_new_guest_config(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1748-\t\t\t\t struct ap_matrix *m_new)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1793-\t/* filter resources not bound to vfio-ap */\ndrivers/s390/crypto/vfio_ap_ops.c:1794:\tdo_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);\ndrivers/s390/crypto/vfio_ap_ops.c:1795:\tdo_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-1796-\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1806=static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1830-\ndrivers/s390/crypto/vfio_ap_ops.c:1831:\trc = vfio_ap_set_new_guest_config(matrix_mdev, \u0026m_new);\ndrivers/s390/crypto/vfio_ap_ops.c-1832-\tif (!rc)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1860=static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1874- */\ndrivers/s390/crypto/vfio_ap_ops.c:1875:static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-1876-\t\t\t\tstruct kvm *kvm)\n--\ndrivers/s390/crypto/vfio_ap_ops.c=1904=static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1915-\ndrivers/s390/crypto/vfio_ap_ops.c:1916:static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,\ndrivers/s390/crypto/vfio_ap_ops.c-1917-\t\t\t\t   u64 length)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1934- */\ndrivers/s390/crypto/vfio_ap_ops.c:1935:static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-1936-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-1954-\ndrivers/s390/crypto/vfio_ap_ops.c:1955:static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)\ndrivers/s390/crypto/vfio_ap_ops.c-1956-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c=2003=static void apq_reset_check(struct work_struct *reset_work)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2040-\ndrivers/s390/crypto/vfio_ap_ops.c:2041:static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)\ndrivers/s390/crypto/vfio_ap_ops.c-2042-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2070-\ndrivers/s390/crypto/vfio_ap_ops.c:2071:static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2072-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2088-\ndrivers/s390/crypto/vfio_ap_ops.c:2089:static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)\ndrivers/s390/crypto/vfio_ap_ops.c-2090-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2106-\ndrivers/s390/crypto/vfio_ap_ops.c:2107:static int vfio_ap_mdev_open_device(struct vfio_device *vdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2108-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2116-\tmatrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);\ndrivers/s390/crypto/vfio_ap_ops.c:2117:\tret = vfio_ap_init_migration_data(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-2118-\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2122-\ndrivers/s390/crypto/vfio_ap_ops.c:2123:\tret = vfio_ap_mdev_set_kvm(matrix_mdev, vdev-\u003ekvm);\ndrivers/s390/crypto/vfio_ap_ops.c-2124-\tif (ret) {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2133-\ndrivers/s390/crypto/vfio_ap_ops.c:2134:static void vfio_ap_mdev_close_device(struct vfio_device *vdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2135-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2145-\ndrivers/s390/crypto/vfio_ap_ops.c:2146:static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)\ndrivers/s390/crypto/vfio_ap_ops.c-2147-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2174-\ndrivers/s390/crypto/vfio_ap_ops.c:2175:int vfio_ap_mdev_get_num_queues(struct ap_matrix *ap_matrix)\ndrivers/s390/crypto/vfio_ap_ops.c-2176-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2182-\ndrivers/s390/crypto/vfio_ap_ops.c:2183:static int vfio_ap_mdev_get_device_info(unsigned long arg)\ndrivers/s390/crypto/vfio_ap_ops.c-2184-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2202-\ndrivers/s390/crypto/vfio_ap_ops.c:2203:static ssize_t vfio_ap_get_irq_info(unsigned long arg)\ndrivers/s390/crypto/vfio_ap_ops.c-2204-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2231-\ndrivers/s390/crypto/vfio_ap_ops.c:2232:static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)\ndrivers/s390/crypto/vfio_ap_ops.c-2233-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2253-\ndrivers/s390/crypto/vfio_ap_ops.c:2254:static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-2255-\t\t\t\t   unsigned long arg)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2287-\ndrivers/s390/crypto/vfio_ap_ops.c:2288:static int vfio_ap_set_cfg_change_irq(struct ap_matrix_mdev *matrix_mdev, unsigned long arg)\ndrivers/s390/crypto/vfio_ap_ops.c-2289-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2320-\ndrivers/s390/crypto/vfio_ap_ops.c:2321:static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-2322-\t\t\t    unsigned long arg)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2326-\ndrivers/s390/crypto/vfio_ap_ops.c:2327:\tret = vfio_ap_irq_set_init(\u0026irq_set, arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2328-\tif (ret)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2334-\t\tcase VFIO_AP_REQ_IRQ_INDEX:\ndrivers/s390/crypto/vfio_ap_ops.c:2335:\t\t\treturn vfio_ap_set_request_irq(matrix_mdev, arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2336-\t\tcase VFIO_AP_CFG_CHG_IRQ_INDEX:\ndrivers/s390/crypto/vfio_ap_ops.c:2337:\t\t\treturn vfio_ap_set_cfg_change_irq(matrix_mdev, arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2338-\t\tdefault:\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2345-\ndrivers/s390/crypto/vfio_ap_ops.c:2346:static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,\ndrivers/s390/crypto/vfio_ap_ops.c-2347-\t\t\t\t    unsigned int cmd, unsigned long arg)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2355-\tcase VFIO_DEVICE_GET_INFO:\ndrivers/s390/crypto/vfio_ap_ops.c:2356:\t\tret = vfio_ap_mdev_get_device_info(arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2357-\t\tbreak;\ndrivers/s390/crypto/vfio_ap_ops.c-2358-\tcase VFIO_DEVICE_RESET:\ndrivers/s390/crypto/vfio_ap_ops.c:2359:\t\tret = vfio_ap_mdev_reset_queues(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-2360-\t\tif (ret == 0) {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2365-\tcase VFIO_DEVICE_GET_IRQ_INFO:\ndrivers/s390/crypto/vfio_ap_ops.c:2366:\t\tret = vfio_ap_get_irq_info(arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2367-\t\tbreak;\ndrivers/s390/crypto/vfio_ap_ops.c-2368-\tcase VFIO_DEVICE_SET_IRQS:\ndrivers/s390/crypto/vfio_ap_ops.c:2369:\t\tret = vfio_ap_set_irqs(matrix_mdev, arg);\ndrivers/s390/crypto/vfio_ap_ops.c-2370-\t\tbreak;\n--\ndrivers/s390/crypto/vfio_ap_ops.c=2395=static ssize_t status_show(struct device *dev,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2406-\tq = dev_get_drvdata(\u0026apdev-\u003edevice);\ndrivers/s390/crypto/vfio_ap_ops.c:2407:\tmatrix_mdev = vfio_ap_mdev_for_queue(q);\ndrivers/s390/crypto/vfio_ap_ops.c-2408-\n--\ndrivers/s390/crypto/vfio_ap_ops.c=2461=static struct mdev_driver vfio_ap_matrix_driver = {\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2473-\ndrivers/s390/crypto/vfio_ap_ops.c:2474:int vfio_ap_mdev_register(void)\ndrivers/s390/crypto/vfio_ap_ops.c-2475-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2496-\ndrivers/s390/crypto/vfio_ap_ops.c:2497:void vfio_ap_mdev_unregister(void)\ndrivers/s390/crypto/vfio_ap_ops.c-2498-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2502-\ndrivers/s390/crypto/vfio_ap_ops.c:2503:int vfio_ap_mdev_probe_queue(struct ap_device *apdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2504-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2556-\ndrivers/s390/crypto/vfio_ap_ops.c:2557:void vfio_ap_mdev_remove_queue(struct ap_device *apdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2558-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2619- */\ndrivers/s390/crypto/vfio_ap_ops.c:2620:int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)\ndrivers/s390/crypto/vfio_ap_ops.c-2621-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2625-\tmutex_lock(\u0026matrix_dev-\u003emdevs_lock);\ndrivers/s390/crypto/vfio_ap_ops.c:2626:\tret = vfio_ap_mdev_verify_no_sharing(NULL, apm, aqm);\ndrivers/s390/crypto/vfio_ap_ops.c-2627-\tmutex_unlock(\u0026matrix_dev-\u003emdevs_lock);\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2643- */\ndrivers/s390/crypto/vfio_ap_ops.c:2644:static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,\ndrivers/s390/crypto/vfio_ap_ops.c-2645-\t\t\t\t\tunsigned long *aprem,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2684- */\ndrivers/s390/crypto/vfio_ap_ops.c:2685:static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,\ndrivers/s390/crypto/vfio_ap_ops.c-2686-\t\t\t\t    unsigned long *aq_remove,\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2725- */\ndrivers/s390/crypto/vfio_ap_ops.c:2726:static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,\ndrivers/s390/crypto/vfio_ap_ops.c-2727-\t\t\t\t       struct ap_config_info *prev_config_info)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2756- */\ndrivers/s390/crypto/vfio_ap_ops.c:2757:static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)\ndrivers/s390/crypto/vfio_ap_ops.c-2758-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2827- */\ndrivers/s390/crypto/vfio_ap_ops.c:2828:static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,\ndrivers/s390/crypto/vfio_ap_ops.c-2829-\t\t\t\t unsigned long *adm_add)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2858- */\ndrivers/s390/crypto/vfio_ap_ops.c:2859:static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,\ndrivers/s390/crypto/vfio_ap_ops.c-2860-\t\t\t\t    struct ap_config_info *prev_config_info)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2890- */\ndrivers/s390/crypto/vfio_ap_ops.c:2891:void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,\ndrivers/s390/crypto/vfio_ap_ops.c-2892-\t\t\t    struct ap_config_info *prev_cfg_info)\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2905-\ndrivers/s390/crypto/vfio_ap_ops.c:2906:static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)\ndrivers/s390/crypto/vfio_ap_ops.c-2907-{\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2921-\tif (filter_adapters || filter_domains)\ndrivers/s390/crypto/vfio_ap_ops.c:2922:\t\tdo_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);\ndrivers/s390/crypto/vfio_ap_ops.c-2923-\ndrivers/s390/crypto/vfio_ap_ops.c-2924-\tif (filter_cdoms)\ndrivers/s390/crypto/vfio_ap_ops.c:2925:\t\tdo_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);\ndrivers/s390/crypto/vfio_ap_ops.c-2926-\n--\ndrivers/s390/crypto/vfio_ap_ops.c-2935-\ndrivers/s390/crypto/vfio_ap_ops.c:2936:void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,\ndrivers/s390/crypto/vfio_ap_ops.c-2937-\t\t\t      struct ap_config_info *old_config_info)\n"
}

15/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 56748
							output: 37
							thoughts: 278
						

Thoughts:
**Analyzing the VFIO-AP Migration Patch**

Okay, so this patch is introducing a few key elements related to VFIO-AP migration, specifically a new `vfio_ap_migration.c` file and, importantly, some new migration operations within `vfio_ap_matrix_dev_ops`. My initial focus needs to be on these new operations. Let's list them out to ensure I have a good grasp:

*   `vfio_ap_set_state`
*   `vfio_ap_get_state`
*   `vfio_ap_get_data_size`

And then, crucially, the file operations for the migration file descriptor:

*   `vfio_ap_stop_copy_read`
*   `vfio_ap_resuming_write`

Hmm, I also need to make sure I add `vfio_ap_set_new_guest_config` to that list. I will want to see how this function is used.

I need to confirm the configuration dependencies here. This patch obviously leverages `CONFIG_VFIO_AP`, but I want to make sure there are no other configurations that might silently introduce dependencies or conflicts. Is `CONFIG_VFIO_AP` the sole, essential configuration option? Yes, it appears to be the main one. Wait... could `CONFIG_VFIO_MDEV` come into play? Ah, no, it looks like `VFIO_AP` either selects or depends on `VFIO_MDEV`, so that makes sense. Just to be completely thorough, though, I'll quickly glance at `drivers/s390/crypto/Kconfig` to look for anything that might be relevant. It's always good to be extra careful, especially with such a low-level and crucial part of the system.



16/2 2026/07/28 13:23 tool read-file
0m Error:
the file does not exist

Args:
{
  "File": "drivers/s390/crypto/Kconfig",
  "FirstLine": 1,
  "LineCount": 50
}

Results:
{
  "error": "the file does not exist"
}

17/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 57470
							output: 25
							thoughts: 55
						

Thoughts:
**Navigating the Kconfig Labyrinth**

Okay, so I'm trying to track down this Kconfig file, and it seems like it's pointing me toward `drivers/s390/crypto/Kconfig`. But that feels a bit...specific. Before I get too deep down that rabbit hole, let's just make sure I haven't missed a more general entry. My first thought is to broaden the search. I should probably check `drivers/s390/Kconfig` directly – maybe it's nested in there somewhere, providing a more high-level overview. Or, since we're dealing with crypto, perhaps it's tucked away in `drivers/crypto/Kconfig`. That would make a lot of sense, keeping the crypto-specific configuration centralized. Finally, just to be thorough and consider the architectural level, I should scan `arch/s390/Kconfig`. You know how these things can sometimes be organized – it's always good to cover all the bases before making any assumptions about the most direct path. Gotta be efficient here.



18/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "config VFIO_AP",
  "PathPrefix": ""
}

Results:
{
  "Output": "arch/s390/Kconfig=823=config VFIO_CCW\n--\narch/s390/Kconfig-833-\narch/s390/Kconfig:834:config VFIO_AP\narch/s390/Kconfig-835-\tdef_tristate n\n"
}

19/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 57870
							output: 37
							thoughts: 22
						

Thoughts:
Here's my take on the thought process, as requested:

**Diving into the s390 Kernel Configuration**

Okay, time to get my hands dirty with the s390 kernel configuration. I need to understand this specific section better. Let's start by navigating to `arch/s390/Kconfig`, and I'll jump right to around line 834. That should be a good starting point to contextualize what I'm looking for. Let's see what gems we can find there. This is where the real work happens.



20/2 2026/07/28 13:23 tool read-file
0m Args:
{
  "File": "arch/s390/Kconfig",
  "FirstLine": 820,
  "LineCount": 30
}

Results:
{
  "Contents": " 820:\t\n 821:\t\t  If unsure, say N.\n 822:\t\n 823:\tconfig VFIO_CCW\n 824:\t\tdef_tristate n\n 825:\t\tprompt \"Support for VFIO-CCW subchannels\"\n 826:\t\tdepends on VFIO\n 827:\t\tselect VFIO_MDEV\n 828:\t\thelp\n 829:\t\t  This driver allows usage of I/O subchannels via VFIO-CCW.\n 830:\t\n 831:\t\t  To compile this driver as a module, choose M here: the\n 832:\t\t  module will be called vfio_ccw.\n 833:\t\n 834:\tconfig VFIO_AP\n 835:\t\tdef_tristate n\n 836:\t\tprompt \"VFIO support for AP devices\"\n 837:\t\tdepends on KVM\n 838:\t\tdepends on VFIO\n 839:\t\tdepends on AP\n 840:\t\tselect VFIO_MDEV\n 841:\t\thelp\n 842:\t\t  This driver grants access to Adjunct Processor (AP) devices\n 843:\t\t  via the VFIO mediated device interface.\n 844:\t\n 845:\t\t  To compile this driver as a module, choose M here: the module\n 846:\t\t  will be called vfio_ap.\n 847:\t\n 848:\tendmenu\n 849:\t\n"
}

21/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 58445
							output: 22
							thoughts: 47
						

Thoughts:
Okay, here's my take on those thoughts, tailored for an expert audience and presented in the first person:

**Decoding the `VFIO_AP` Dependency Chain**

Right, let's unpack this. The core concern here is `VFIO_AP`, and the immediate dependencies jump out: `KVM`, `VFIO`, and `AP`. It's a classic layered approach. KVM, of course, is the bedrock hypervisor. VFIO provides the IOMMU-based device assignment, and then we have `AP`. The critical piece, and the one that needs immediate clarification, is that `AP`. My instinct says this is either referring to something like `ZCRYPT` – the IBM hardware crypto accelerator – or perhaps the broader "AP bus" infrastructure. Time to dive into the code and see exactly where `AP` is defined within this context. That's the key to understanding the full picture here. We need to identify that component to fully grasp the control flow and resource allocation.



22/2 2026/07/28 13:23 tool grepper
0m Args:
{
  "Expression": "config AP",
  "PathPrefix": ""
}

Results:
{
  "Output": "Documentation/admin-guide/bootconfig.rst=287=to initrd image, user can notice it before boot.\n--\nDocumentation/admin-guide/bootconfig.rst-289-\nDocumentation/admin-guide/bootconfig.rst:290:Bootconfig APIs\nDocumentation/admin-guide/bootconfig.rst-291-===============\n--\narch/m68k/Kconfig.machine=29=config MAC\n--\narch/m68k/Kconfig.machine-40-\narch/m68k/Kconfig.machine:41:config APOLLO\narch/m68k/Kconfig.machine-42-\tbool \"Apollo support\"\n--\narch/powerpc/platforms/44x/Kconfig=291=config 476FPE\n--\narch/powerpc/platforms/44x/Kconfig-294-\narch/powerpc/platforms/44x/Kconfig:295:config APM821xx\narch/powerpc/platforms/44x/Kconfig-296-\tbool\n--\narch/s390/Kconfig=785=config EADM_SCH\n--\narch/s390/Kconfig-795-\narch/s390/Kconfig:796:config AP\narch/s390/Kconfig-797-\tdef_tristate y\n--\narch/s390/Kconfig-808-\narch/s390/Kconfig:809:config AP_DEBUG\narch/s390/Kconfig-810-\tdef_bool n\n--\narch/s390/Kconfig=885=config CMM_IUCV\n--\narch/s390/Kconfig-892-\narch/s390/Kconfig:893:config APPLDATA_BASE\narch/s390/Kconfig-894-\tdef_bool n\n--\narch/s390/Kconfig-908-\narch/s390/Kconfig:909:config APPLDATA_MEM\narch/s390/Kconfig-910-\tdef_tristate m\n--\narch/s390/Kconfig-925-\narch/s390/Kconfig:926:config APPLDATA_OS\narch/s390/Kconfig-927-\tdef_tristate m\n--\narch/s390/Kconfig-940-\narch/s390/Kconfig:941:config APPLDATA_NET_SUM\narch/s390/Kconfig-942-\tdef_tristate m\n--\narch/x86/Kconfig=2697=config X86_APM_BOOT\n--\narch/x86/Kconfig-2700-\narch/x86/Kconfig:2701:menuconfig APM\narch/x86/Kconfig-2702-\ttristate \"APM (Advanced Power Management) BIOS support\"\n--\narch/x86/Kconfig=2753=if APM\narch/x86/Kconfig-2754-\narch/x86/Kconfig:2755:config APM_IGNORE_USER_SUSPEND\narch/x86/Kconfig-2756-\tbool \"Ignore USER SUSPEND\"\n--\narch/x86/Kconfig-2761-\narch/x86/Kconfig:2762:config APM_DO_ENABLE\narch/x86/Kconfig-2763-\tbool \"Enable PM at boot time\"\n--\narch/x86/Kconfig-2778-\narch/x86/Kconfig:2779:config APM_CPU_IDLE\narch/x86/Kconfig-2780-\tdepends on CPU_IDLE\n--\narch/x86/Kconfig-2790-\narch/x86/Kconfig:2791:config APM_DISPLAY_BLANK\narch/x86/Kconfig-2792-\tbool \"Enable console blanking using APM\"\n--\narch/x86/Kconfig-2803-\narch/x86/Kconfig:2804:config APM_ALLOW_INTS\narch/x86/Kconfig-2805-\tbool \"Allow interrupts during APM BIOS calls\"\n--\ndrivers/clk/qcom/Kconfig=330=config CLK_HAWI_TCSRCC\n--\ndrivers/clk/qcom/Kconfig-336-\ndrivers/clk/qcom/Kconfig:337:config APQ_GCC_8084\ndrivers/clk/qcom/Kconfig-338-\ttristate \"APQ8084 Global Clock Controller\"\n--\ndrivers/clk/qcom/Kconfig-345-\ndrivers/clk/qcom/Kconfig:346:config APQ_MMCC_8084\ndrivers/clk/qcom/Kconfig-347-\ttristate \"APQ8084 Multimedia Clock Controller\"\n--\ndrivers/dma/Kconfig=78=config AMCC_PPC440SPE_ADMA\n--\ndrivers/dma/Kconfig-87-\ndrivers/dma/Kconfig:88:config APPLE_ADMAC\ndrivers/dma/Kconfig-89-\ttristate \"Apple ADMAC support\"\n--\ndrivers/firmware/efi/Kconfig=154=config EFI_DEV_PATH_PARSER\n--\ndrivers/firmware/efi/Kconfig-156-\ndrivers/firmware/efi/Kconfig:157:config APPLE_PROPERTIES\ndrivers/firmware/efi/Kconfig-158-\tbool \"Apple Device Properties\"\n--\ndrivers/iio/light/Kconfig=68=config AL3320A\n--\ndrivers/iio/light/Kconfig-78-\ndrivers/iio/light/Kconfig:79:config APDS9160\ndrivers/iio/light/Kconfig-80-\ttristate \"APDS9160 combined als and proximity sensor\"\n--\ndrivers/iio/light/Kconfig-89-\ndrivers/iio/light/Kconfig:90:config APDS9300\ndrivers/iio/light/Kconfig-91-\ttristate \"APDS9300 ambient light sensor\"\n--\ndrivers/iio/light/Kconfig-99-\ndrivers/iio/light/Kconfig:100:config APDS9306\ndrivers/iio/light/Kconfig-101-\ttristate \"Avago APDS9306 Ambient Light Sensor\"\n--\ndrivers/iio/light/Kconfig-111-\ndrivers/iio/light/Kconfig:112:config APDS9960\ndrivers/iio/light/Kconfig-113-\ttristate \"Avago APDS9960 gesture/RGB/ALS/proximity sensor\"\n--\ndrivers/iio/light/Kconfig-124-\ndrivers/iio/light/Kconfig:125:config APDS9999\ndrivers/iio/light/Kconfig-126-\ttristate \"Broadcom APDS9999 ALS, RGB and proximity sensor\"\n--\ndrivers/iommu/Kconfig=296=config SPAPR_TCE_IOMMU\n--\ndrivers/iommu/Kconfig-303-\ndrivers/iommu/Kconfig:304:config APPLE_DART\ndrivers/iommu/Kconfig-305-\ttristate \"Apple DART IOMMU Support\"\n--\ndrivers/irqchip/Kconfig=805=config IRQ_IDT3243X\n--\ndrivers/irqchip/Kconfig-809-\ndrivers/irqchip/Kconfig:810:config APPLE_AIC\ndrivers/irqchip/Kconfig-811-\tbool \"Apple Interrupt Controller (AIC)\"\n--\ndrivers/misc/Kconfig=327=config SGI_GRU_DEBUG\n--\ndrivers/misc/Kconfig-333-\ndrivers/misc/Kconfig:334:config APDS9802ALS\ndrivers/misc/Kconfig-335-\ttristate \"Medfield Avago APDS9802 ALS Sensor module\"\n--\ndrivers/net/ethernet/8390/Kconfig=105=config NE2K_PCI\n--\ndrivers/net/ethernet/8390/Kconfig-122-\ndrivers/net/ethernet/8390/Kconfig:123:config APNE\ndrivers/net/ethernet/8390/Kconfig-124-\ttristate \"PCMCIA NE2000 support\"\n--\ndrivers/net/wireless/realtek/rtw89/pci.c=3334=static int rtw89_pci_cfg_dac(struct rtw89_dev *rtwdev, bool force)\n--\ndrivers/net/wireless/realtek/rtw89/pci.c-3346-\ndrivers/net/wireless/realtek/rtw89/pci.c:3347:\t/* Configure DAC only via PCI config API, not DBI interfaces */\ndrivers/net/wireless/realtek/rtw89/pci.c-3348-\tret = pci_read_config_byte(pdev, RTW89_PCIE_L1_CTRL, \u0026val);\n--\ndrivers/perf/Kconfig=253=config MARVELL_CN10K_TAD_PMU\n--\ndrivers/perf/Kconfig-259-\ndrivers/perf/Kconfig:260:config APPLE_M1_CPU_PMU\ndrivers/perf/Kconfig-261-\tbool \"Apple M1 CPU PMU support\"\n--\ndrivers/platform/x86/Kconfig=194=config ADV_SWBUTTON\n--\ndrivers/platform/x86/Kconfig-204-\ndrivers/platform/x86/Kconfig:205:config APPLE_GMUX\ndrivers/platform/x86/Kconfig-206-\ttristate \"Apple Gmux Driver\"\n--\ndrivers/pmdomain/apple/Kconfig=3=if ARCH_APPLE || COMPILE_TEST\ndrivers/pmdomain/apple/Kconfig-4-\ndrivers/pmdomain/apple/Kconfig:5:config APPLE_PMGR_PWRSTATE\ndrivers/pmdomain/apple/Kconfig-6-\tbool \"Apple SoC PMGR power state control\"\n--\ndrivers/power/supply/Kconfig=18=config POWER_SUPPLY_HWMON\n--\ndrivers/power/supply/Kconfig-30-\ndrivers/power/supply/Kconfig:31:config APM_POWER\ndrivers/power/supply/Kconfig-32-\ttristate \"APM emulation for class batteries\"\n--\ndrivers/soc/apple/Kconfig=5=menu \"Apple SoC drivers\"\ndrivers/soc/apple/Kconfig-6-\ndrivers/soc/apple/Kconfig:7:config APPLE_MAILBOX\ndrivers/soc/apple/Kconfig-8-\ttristate \"Apple SoC mailboxes\"\n--\ndrivers/soc/apple/Kconfig-18-\ndrivers/soc/apple/Kconfig:19:config APPLE_RTKIT\ndrivers/soc/apple/Kconfig-20-\ttristate \"Apple RTKit co-processor IPC protocol\"\n--\ndrivers/soc/apple/Kconfig-30-\ndrivers/soc/apple/Kconfig:31:config APPLE_SART\ndrivers/soc/apple/Kconfig-32-\ttristate \"Apple SART DMA address filter\"\n--\ndrivers/soc/apple/Kconfig-40-\ndrivers/soc/apple/Kconfig:41:config APPLE_TUNABLE\ndrivers/soc/apple/Kconfig-42-\ttristate\n--\ndrivers/usb/misc/Kconfig=147=config USB_QCOM_EUD\n--\ndrivers/usb/misc/Kconfig-158-\ndrivers/usb/misc/Kconfig:159:config APPLE_MFI_FASTCHARGE\ndrivers/usb/misc/Kconfig-160-\ttristate \"Fast charge control for iOS devices\"\n--\ndrivers/video/Kconfig=6=menu \"Graphics support\"\ndrivers/video/Kconfig-7-\ndrivers/video/Kconfig:8:config APERTURE_HELPERS\ndrivers/video/Kconfig-9-\tbool\n--\ndrivers/watchdog/Kconfig=1111=config MSC313E_WATCHDOG\n--\ndrivers/watchdog/Kconfig-1122-\ndrivers/watchdog/Kconfig:1123:config APPLE_WATCHDOG\ndrivers/watchdog/Kconfig-1124-\ttristate \"Apple SoC watchdog\"\n--\nfs/xfs/xfs_super.c=1655=xfs_fs_fill_super(\n--\nfs/xfs/xfs_super.c-1667-\t * is guaranteed to have been completed by either the old mount API or\nfs/xfs/xfs_super.c:1668:\t * the newer fsopen/fsconfig API.\nfs/xfs/xfs_super.c-1669-\t */\n--\nkernel/power/Kconfig=311=config PM_TRACE_RTC\n--\nkernel/power/Kconfig-328-\nkernel/power/Kconfig:329:config APM_EMULATION\nkernel/power/Kconfig-330-\ttristate \"Advanced Power Management Emulation\"\n"
}

23/2 2026/07/28 13:23 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 62074
							output: 230
							thoughts: 2783
						

Thoughts:
**VFIO AP Migration Implementation Review**

Okay, let's break this down. First, I need to figure out the right configuration options. `AP` in the `Kconfig` files corresponds to `config AP`, and since we're dealing with VFIO, that likely means `EnableConfigs` should be `VFIO_AP`.  I'll add that. Need to ensure `KVM` and `VFIO` are also enabled, but let's assume `VFIO_AP` pulls in those dependencies.

Next, I need to identify reachable symbols. The patch introduces a bunch of functions. First up, there are these functions that are clearly related to migration: `vfio_ap_stop_copy_read`, `vfio_ap_resuming_write`, `vfio_ap_set_new_guest_config`, `vfio_ap_get_state`, `vfio_ap_set_state`, `vfio_ap_get_data_size`, `vfio_ap_init_migration_data`, `vfio_ap_release_migration_data`, `vfio_ap_reset_migration_state`, and `vfio_ap_init_migration_capabilities`.

`vfio_ap_stop_copy_read` is called from the `.read` callback for `vfio_ap_stop_copy_fops`. `vfio_ap_resuming_write` is called from the `.write` callback for `vfio_ap_resume_fops`.  `vfio_ap_set_state` and `vfio_ap_get_state` are the migration callbacks. `vfio_ap_get_data_size` is also a migration callback. `vfio_ap_set_new_guest_config` is called from `ap_config_store` which is a sysfs attribute store.  I also need to verify that `ap_config` exists and is the sysfs store. The function `vfio_ap_set_new_guest_config` is new code handling it.

Let's check if the syzkaller tool needs to target those symbols by name. The tool wants non-static and non-inlined API entrypoint functions. I need to make sure these symbols are actually reachable.  I see that `vfio_ap_set_new_guest_config` is *not* static, so I'll add that one to the list. I've also verified that  `vfio_ap_stop_copy_read` and `vfio_ap_resuming_write` are static, however, the fact that they're function pointers to `file_operations` means they're unlikely to be inlined and the compiler should emit their addresses.  Also `vfio_ap_set_state` and `vfio_ap_get_state` are static, but this is fine. I'll include:
- `vfio_ap_stop_copy_read`
- `vfio_ap_resuming_write`
- `vfio_ap_set_state`
- `vfio_ap_get_state`
- `vfio_ap_get_data_size`
- `vfio_ap_set_new_guest_config`

Is this actually testable in a virtualized environment like QEMU? Yes, QEMU has s390x emulation, and syzkaller has s390x support.  Does it emulate AP matrix devices? Yes, since it's a VFIO device, QEMU will pass it through if it supports the underlying hardware, which it does.  QEMU provides the AP bus to the guest if the feature is enabled. `ap_instructions_available()` will return true in QEMU. Syzkaller can create the mdev.

Now, let's double-check the definitions. I will check the definitions of the functions in order to verify that these functions are implemented correctly. I will also check:
- `vfio_ap_stop_copy_read` in `drivers/s390/crypto/vfio_ap_migration.c`.
- `vfio_ap_resuming_write` in `drivers/s390/crypto/vfio_ap_migration.c`.
- `vfio_ap_set_state` in `drivers/s390/crypto/vfio_ap_migration.c`.
- `vfio_ap_get_state` in `drivers/s390/crypto/vfio_ap_migration.c`.
- `vfio_ap_get_data_size` in `drivers/s390/crypto/vfio_


24/2 2026/07/28 13:24 tool set-results
0m Error:
the following configs do not exist in the kernel tree: VFIO_AP

Args:
{
  "EnableConfigs": [
    "VFIO_AP"
  ],
  "FocusSymbols": [
    "vfio_ap_stop_copy_read",
    "vfio_ap_resuming_write",
    "vfio_ap_set_state",
    "vfio_ap_get_state",
    "vfio_ap_get_data_size",
    "vfio_ap_set_new_guest_config"
  ],
  "Reasoning": "The patch implements VFIO migration support for the s390 AP (Adjunct Processor) matrix mediated device driver. It adds file operations for reading and writing migration state (vfio_ap_stop_copy_read, vfio_ap_resuming_write) and VFIO migration state transition callbacks (vfio_ap_set_state, vfio_ap_get_state). These are reachable from userspace via VFIO ioctls and migration file descriptors, and can be tested in an s390x QEMU environment with AP instructions enabled.",
  "WorthFuzzing": true
}

Results:
{
  "error": "the following configs do not exist in the kernel tree: VFIO_AP"
}

25/2 2026/07/28 13:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 66140
							output: 25
							thoughts: 29
						

Thoughts:
Okay, here's my interpretation of those thoughts, tailored for an expert audience, and delivered as a first-person summary:

**VFIO_AP Conundrum**

Alright, hang on a second. Something doesn't feel right. My internal checks are saying `VFIO_AP` isn't a recognized symbol. That's... unexpected, to say the least. I could have sworn I've come across it before in the context of VFIO, especially when considering advanced pass-through configurations.

Best to trust my instincts here and perform a quick double-check. Time to fire up the Kconfig search and verify its presence (or absence) once more. I need to confirm whether `VFIO_AP` actually exists in the current configuration or kernel tree. This could be a typo on my part, or maybe a symbol that's been deprecated or renamed across kernel versions. Whatever the case, a fresh search is the first step. Gotta be methodical about this. The implications depend on the outcome. A missing symbol means that a section of code may require further inspection, or an adjustment of configuration.



26/2 2026/07/28 13:24 tool grepper
0m Args:
{
  "Expression": "config VFIO_AP",
  "PathPrefix": ""
}

Results:
{
  "Output": "arch/s390/Kconfig=823=config VFIO_CCW\n--\narch/s390/Kconfig-833-\narch/s390/Kconfig:834:config VFIO_AP\narch/s390/Kconfig-835-\tdef_tristate n\n"
}

27/2 2026/07/28 13:24 llm patch-evaluator
0m Model:
gemini-3.1-pro-preview

Args:
null

Results:
null

Tokens:
							input: 66518
							output: 224
							thoughts: 293
						

Thoughts:
**Investigating a Kernel Configuration Discrepancy**

Okay, so *that* config option, `VFIO_AP`, actually *does* exist! I just verified it in `arch/s390/Kconfig`. This is puzzling because `set-results` just reported that it *doesn't* exist. Hmm, I wonder if the tool is, by default, checking the configuration against an `x86_64` baseline. That would make sense, since I suspect this is an architecture-specific option. Perhaps the tool is only designed to validate against `x86_64` configs.

Wait, is that it? If this is indeed an s390-specific setting, I could just ignore the error, or maybe the tool is rejecting it. The tool *did* return an error, didn't it? If so, I need to either call it again without the `VFIO_AP` option, or just remove it from my config list if it's not present in the tool's known configuration set.

Right, the tool specifically says "the following configs do not exist in the kernel tree: VFIO_AP". But I *just* grepped for it, and it's there in `arch/s390/Kconfig`. Okay, let me make sure I didn't misspell it. Nope, it's definitely `VFIO_AP`.

So, the simplest thing to do is just remove `VFIO_AP` from my `EnableConfigs` list, since the tool is clearly rejecting it. Maybe the tool's validation is just flawed for non-x86 configurations, or maybe it doesn't support the configuration. I could also provide an empty array for `EnableConfigs` to satisfy the tool, assuming it's expecting some input, since this is an s390x-specific configuration. That might be the workaround, since the tool is possibly expecting `x86_64` specific configurations.



28/2 2026/07/28 13:24 tool set-results
0m Args:
{
  "EnableConfigs": [],
  "FocusSymbols": [
    "vfio_ap_stop_copy_read",
    "vfio_ap_resuming_write",
    "vfio_ap_set_state",
    "vfio_ap_get_state",
    "vfio_ap_get_data_size",
    "vfio_ap_set_new_guest_config"
  ],
  "Reasoning": "The patch implements VFIO migration support for the s390 AP (Adjunct Processor) matrix mediated device driver. It adds file operations for reading and writing migration state (vfio_ap_stop_copy_read, vfio_ap_resuming_write) and VFIO migration state transition callbacks (vfio_ap_set_state, vfio_ap_get_state). These are reachable from userspace via VFIO ioctls and migration file descriptors, and can be tested in an s390x QEMU environment with AP instructions enabled.",
  "WorthFuzzing": true
}

Results:
{
  "EnableConfigs": null,
  "FocusSymbols": [
    "vfio_ap_stop_copy_read",
    "vfio_ap_resuming_write",
    "vfio_ap_set_state",
    "vfio_ap_get_state",
    "vfio_ap_get_data_size",
    "vfio_ap_set_new_guest_config"
  ],
  "Reasoning": "The patch implements VFIO migration support for the s390 AP (Adjunct Processor) matrix mediated device driver. It adds file operations for reading and writing migration state (vfio_ap_stop_copy_read, vfio_ap_resuming_write) and VFIO migration state transition callbacks (vfio_ap_set_state, vfio_ap_get_state). These are reachable from userspace via VFIO ioctls and migration file descriptors, and can be tested in an s390x QEMU environment with AP instructions enabled.",
  "WorthFuzzing": true
}

LLM Calls Summary:
Total Calls Total Tokens Avg Tokens Total Duration (Seconds) Avg Duration (Seconds)
Tool Calls Summary:
Total Calls Total Duration (Seconds) Avg Duration (Seconds)