Skip to contentSkip to conversation

add debugfs to migration driver

PATCH Superseded Longfang Liu 3 participants 12 messages Mar 20, 2023 – Mar 25, 2023

Subsystems (4)

v9 of 19 Superseded Latest is v19 →

lore
Share & tools
lore

Longfang Liu

author maintainer
#0 lore raw

v9 cover add debugfs to migration driver

Add a debugfs function to the migration driver in VFIO to provide a step-by-step test function for the migration driver.

When the execution of live migration fails, the user can view the status and data during the migration process separately from the source and the destination, which is convenient for users to analyze and locate problems.

Changes v8 -> v9
Update the debugfs directory structure of vfio.

Changes v7 -> v8
Add support for platform devices.

Changes v6 -> v7
Fix some code style issues.

Changes v5 -> v6
Control the creation of debugfs through the CONFIG_DEBUG_FS.

Changes v4 -> v5
Remove the newly added vfio_migration_ops and use seq_printf to optimize the implementation of debugfs.

Changes v3 -> v4
Change the migration_debug_operate interface to debug_root file.

Changes v2 -> v3
Extend the debugfs function from hisilicon device to vfio.

Changes v1 -> v2
Change the registration method of root_debugfs to register with module initialization.

Longfang Liu (5):
vfio/migration: Add debugfs to live migration driver
hisi_acc_vfio_pci: extract public functions for container_of
hisi_acc_vfio_pci: register debugfs for hisilicon migration driver
Documentation: add debugfs description for vfio
vfio: update live migration device status

 .../ABI/testing/debugfs-hisi-migration        |  28 ++
 Documentation/ABI/testing/debugfs-vfio        |  25 ++
 drivers/vfio/Makefile                         |   2 +-
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 314 +++++++++++++++++-
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.h    |  11 +
 drivers/vfio/pci/mlx5/main.c                  |   3 +
 drivers/vfio/vfio.h                           |  14 +
 drivers/vfio/vfio_debugfs.c                   |  80 +++++
 drivers/vfio/vfio_main.c                      |   5 +
 include/linux/vfio.h                          |   8 +
 10 files changed, 479 insertions(+), 11 deletions(-)
 create mode 100644 Documentation/ABI/testing/debugfs-hisi-migration
 create mode 100644 Documentation/ABI/testing/debugfs-vfio
 create mode 100644 drivers/vfio/vfio_debugfs.c
Signature · 1 line
2.24.0
author maintainer
#1 lore raw

PATCH v9 1/5 vfio/migration: Add debugfs to live migration driver

There are multiple devices, software and operational steps involved in the process of live migration. An error occurred on any node may cause the live migration operation to fail.
This complex process makes it very difficult to locate and analyze the cause when the function fails.

In order to quickly locate the cause of the problem when the live migration fails, I added a set of debugfs to the vfio live migration driver.

    +-------------------------------------------+
    |                                           |
    |                                           |
    |                  QEMU                     |
    |                                           |
    |                                           |
    +---+----------------------------+----------+
        |      ^                     |      ^
        |      |                     |      |
        |      |                     |      |
        v      |                     v      |
     +---------+--+               +---------+--+
     |src vfio_dev|               |dst vfio_dev|
     +--+---------+               +--+---------+
        |      ^                     |      ^
        |      |                     |      |
        v      |                     |      |
   +-----------+----+           +-----------+----+
   |src dev debugfs |           |dst dev debugfs |
   +----------------+           +----------------+

The entire debugfs directory will be based on the definition of the CONFIG_DEBUG_FS macro. If this macro is not enabled, the interfaces in vfio.h will be empty definitions, and the creation and initialization of the debugfs directory will not be executed.

vfio

    |
    +---<dev_name1>
    |    +---migration
    |        +--state
    |        +--hisi_acc
    |            +--attr
    |            +--data
    |            +--debug
    |
    +---<dev_name2>
         +---migration
             +--state
             +--hisi_acc
                 +--attr
                 +--data
                 +--debug

debugfs will create a public root directory "vfio" file. then create a dev_name() file for each live migration device. First, create a unified state acquisition file of "migration" in this device directory.
Then, create a public live migration state lookup file "state" Finally, create a directory file based on the device type, and then create the device's own debugging files under this directory file.

Here, HiSilicon accelerator creates three debug files:
attr: used to obtain the attribute parameters of the current live migration device.
data: used to get the live migration data of the current live migration device.
debug: Used to debug the current live migration device through commands.

The live migration function of the current device can be tested by operating the debug files, and the functional status of the equipment and software at each stage can be tested step by step without performing the complete live migration function. And after the live migration is performed, the migration device data of the live migration can be obtained through the debug files.

---
 drivers/vfio/Makefile       |  2 +-
 drivers/vfio/vfio.h         | 14 +++++++
 drivers/vfio/vfio_debugfs.c | 80 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_main.c    |  5 +++
 include/linux/vfio.h        |  8 ++++
 5 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/vfio_debugfs.c
drivers/vfio/Makefile +1 -1
index 70e7dcb302ef..1debcff31d30 100644
@@ -7,7 +7,7 @@ vfio-y += vfio_main.o \
 vfio-$(CONFIG_IOMMUFD) += iommufd.o
 vfio-$(CONFIG_VFIO_CONTAINER) += container.o
 vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
-
+vfio-$(CONFIG_DEBUG_FS) += vfio_debugfs.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_PCI) += pci/
drivers/vfio/vfio.h +14 -0
index e9721d8424bc..8e5cafa6aa3a 100644
@@ -258,4 +258,18 @@ static inline void vfio_device_put_kvm(struct vfio_device *device)
 }
 #endif
 
+#ifdef CONFIG_DEBUG_FS
+void vfio_debugfs_create_root(void);
+void vfio_debugfs_remove_root(void);
+
+void vfio_device_debugfs_init(struct vfio_device *vdev);
+void vfio_device_debugfs_exit(struct vfio_device *vdev);
+#else
+static inline void vfio_debugfs_create_root(void) { }
+static inline void vfio_debugfs_remove_root(void) { }
+
+static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }
+static inline void vfio_device_debugfs_exit(struct vfio_device *vdev) { }
+#endif /* CONFIG_DEBUG_FS */
+
 #endif
drivers/vfio/vfio_debugfs.c new file +80 -0

80 lines not on this page · raw message

drivers/vfio/vfio_main.c +5 -0
index 3a597e799918..09772fd3a396 100644
@@ -1408,6 +1408,9 @@ static int __init vfio_init(void)
 	}
 
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	vfio_debugfs_create_root();
+
 	return 0;
 
 err_dev_class:
@@ -1425,6 +1428,8 @@ static void __exit vfio_cleanup(void)
 	vfio_virqfd_exit();
 	vfio_group_cleanup();
 	xa_destroy(&vfio_device_set_xa);
+
+	vfio_debugfs_remove_root();
 }
 
 module_init(vfio_init);
include/linux/vfio.h +8 -0
index 93134b023968..fa6b898ebb58 100644
@@ -63,6 +63,14 @@ struct vfio_device {
 	struct iommufd_ctx *iommufd_ictx;
 	bool iommufd_attached;
 #endif
+
+#ifdef CONFIG_DEBUG_FS
+	/*
+	 * debug_root is a static property of the vfio_device
+	 * which must be set prior to registering the vfio_device.
+	 */
+	struct dentry *debug_root;
+#endif
 };
 
 /**
Signature · 1 line
2.24.0
#2 lore raw

Re: [PATCH v9 1/5] vfio/migration: Add debugfs to live migration driver

Quoted 177 lines · On Mon, 20 Mar 2023 17:23:34 +0800 Longfang Liu <[email protected]> wrote:

There are multiple devices, software and operational steps involved in the process of live migration. An error occurred on any node may cause the live migration operation to fail.
This complex process makes it very difficult to locate and analyze the cause when the function fails.

In order to quickly locate the cause of the problem when the live migration fails, I added a set of debugfs to the vfio live migration driver.

    +-------------------------------------------+
    |                                           |
    |                                           |
    |                  QEMU                     |
    |                                           |
    |                                           |
    +---+----------------------------+----------+
        |      ^                     |      ^
        |      |                     |      |
        |      |                     |      |
        v      |                     v      |
     +---------+--+               +---------+--+
     |src vfio_dev|               |dst vfio_dev|
     +--+---------+               +--+---------+
        |      ^                     |      ^
        |      |                     |      |
        v      |                     |      |
   +-----------+----+           +-----------+----+
   |src dev debugfs |           |dst dev debugfs |
   +----------------+           +----------------+

The entire debugfs directory will be based on the definition of the CONFIG_DEBUG_FS macro. If this macro is not enabled, the interfaces in vfio.h will be empty definitions, and the creation and initialization of the debugfs directory will not be executed.

   vfio
    |
    +---<dev_name1>
    |    +---migration
    |        +--state
    |        +--hisi_acc
    |            +--attr
    |            +--data
    |            +--debug
    |
    +---<dev_name2>
         +---migration
             +--state
             +--hisi_acc
                 +--attr
                 +--data
                 +--debug

debugfs will create a public root directory "vfio" file. then create a dev_name() file for each live migration device. First, create a unified state acquisition file of "migration" in this device directory.
Then, create a public live migration state lookup file "state" Finally, create a directory file based on the device type, and then create the device's own debugging files under this directory file.

Here, HiSilicon accelerator creates three debug files:
attr: used to obtain the attribute parameters of the current live migration device.
data: used to get the live migration data of the current live migration device.
debug: Used to debug the current live migration device through commands.

The live migration function of the current device can be tested by operating the debug files, and the functional status of the equipment and software at each stage can be tested step by step without performing the complete live migration function. And after the live migration is performed, the migration device data of the live migration can be obtained through the debug files.

---
 drivers/vfio/Makefile       |  2 +-
 drivers/vfio/vfio.h         | 14 +++++++
 drivers/vfio/vfio_debugfs.c | 80 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_main.c    |  5 +++
 include/linux/vfio.h        |  8 ++++
 5 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/vfio_debugfs.c
drivers/vfio/Makefile +1 -1
index 70e7dcb302ef..1debcff31d30 100644
@@ -7,7 +7,7 @@ vfio-y += vfio_main.o \
 vfio-$(CONFIG_IOMMUFD) += iommufd.o
 vfio-$(CONFIG_VFIO_CONTAINER) += container.o
 vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
-
+vfio-$(CONFIG_DEBUG_FS) += vfio_debugfs.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_PCI) += pci/
drivers/vfio/vfio.h +14 -0
index e9721d8424bc..8e5cafa6aa3a 100644
@@ -258,4 +258,18 @@ static inline void vfio_device_put_kvm(struct vfio_device *device)
 }
 #endif
 
+#ifdef CONFIG_DEBUG_FS
+void vfio_debugfs_create_root(void);
+void vfio_debugfs_remove_root(void);
+
+void vfio_device_debugfs_init(struct vfio_device *vdev);
+void vfio_device_debugfs_exit(struct vfio_device *vdev);
+#else
+static inline void vfio_debugfs_create_root(void) { }
+static inline void vfio_debugfs_remove_root(void) { }
+
+static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }
+static inline void vfio_device_debugfs_exit(struct vfio_device *vdev) { }
+#endif /* CONFIG_DEBUG_FS */
+
 #endif
drivers/vfio/vfio_debugfs.c new file +47 -0
new file mode 100644
index 000000000000..f5b6bf2a1d48
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, HiSilicon Ltd.
+ */
+
+#include <linux/device.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+#include <linux/vfio.h>
+#include "vfio.h"
+
+static struct dentry *vfio_debugfs_root;
+
+static int vfio_device_state_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_device *vdev = container_of(vf_dev, struct vfio_device, device);
+	enum vfio_device_mig_state state;
+	int ret;
+
+	ret = vdev->mig_ops->migration_get_state(vdev, &state);
+	if (ret)
+		return -EINVAL;
+
+	switch (state) {
+	case VFIO_DEVICE_STATE_RUNNING:
+		seq_printf(seq, "%s\n", "RUNNING");
+		break;
+	case VFIO_DEVICE_STATE_STOP_COPY:
+		seq_printf(seq, "%s\n", "STOP_COPY");
+		break;
+	case VFIO_DEVICE_STATE_STOP:
+		seq_printf(seq, "%s\n", "STOP");
+		break;
+	case VFIO_DEVICE_STATE_RESUMING:
+		seq_printf(seq, "%s\n", "RESUMING");
+		break;
+	case VFIO_DEVICE_STATE_RUNNING_P2P:
+		seq_printf(seq, "%s\n", "RESUMING_P2P");
+		break;
+	case VFIO_DEVICE_STATE_ERROR:
+		seq_printf(seq, "%s\n", "ERROR");
+		break;
+	default:
+		seq_printf(seq, "%s\n", "Invalid");
+	}
+

drivers/vfio/vfio_debugfs.c line 57

Quoted 10 lines
drivers/vfio/vfio_debugfs.c new file +10 -0
+	return 0;
+}
+
+void vfio_device_debugfs_init(struct vfio_device *vdev)
+{
+	struct dentry *vfio_dev_migration = NULL;
+	struct device *dev = &vdev->device;
+
+	vdev->debug_root = debugfs_create_dir(dev_name(vdev->dev), vfio_debugfs_root);
+	vfio_dev_migration = debugfs_create_dir("migration", vdev->debug_root);

Am I understanding correctly that there seems to be a standard with debugfs helpers to accept and test for bad inputs such that typical callers don't bother to test for ERR_PTRs from cases like above?

drivers/vfio/vfio_debugfs.c line 62

Quoted 5 lines
drivers/vfio/vfio_debugfs.c new file +5 -0
+
+	debugfs_create_devm_seqfile(dev, "state", vfio_dev_migration,
+				  vfio_device_state_read);
+}
+EXPORT_SYMBOL_GPL(vfio_device_debugfs_init);

I think these should be more integrated into vfio core. As it is, this series doesn't enable the base migration/state entry support for drivers other than hisi_acc, where this proposal requires explicit setup and teardown by the driver probe and remove callbacks.

Instead the init and exit functions here should be called directly from __vfio_register_dev() and vfio_unregister_group_dev() respectively. The setup of the migration directory and state file should be conditional on the device providing mig_ops, while the base dev_name directory is setup for all devices for future use cases.

This would allow that mlx5 and future drivers automatically get this with no driver code, and in fact all devices would get debugfs dev_name directories for better test coverage. hisi_acc would only need to add driver specific entries after registering the device and free any related data after unregistering the device. In this way, none of the entry points here would need to be exported for drivers, which is perhaps the direction Jason was thinking with his prior comments.

Quoted 23 lines
drivers/vfio/vfio_debugfs.c new file +18 -0
+
+void vfio_device_debugfs_exit(struct vfio_device *vdev)
+{
+	debugfs_remove_recursive(vdev->debug_root);
+}
+EXPORT_SYMBOL_GPL(vfio_device_debugfs_exit);
+
+void vfio_debugfs_create_root(void)
+{
+	vfio_debugfs_root = debugfs_create_dir("vfio", NULL);
+}
+
+void vfio_debugfs_remove_root(void)
+{
+	debugfs_remove_recursive(vfio_debugfs_root);
+	vfio_debugfs_root = NULL;
+}
+
drivers/vfio/vfio_main.c +0 -0
index 3a597e799918..09772fd3a396 100644

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1413

Quoted 6 lines
drivers/vfio/vfio_main.c +3 -0
@@ -1408,6 +1408,9 @@ static int __init vfio_init(void)
 	}
 
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	vfio_debugfs_create_root();
+

The pr_info is intended to be the last call in the init path.

drivers/vfio/vfio_main.c static void __exit vfio_cleanup(void) · line 1432

Quoted 9 lines
drivers/vfio/vfio_main.c +2 -0
 	return 0;
 
 err_dev_class:
@@ -1425,6 +1428,8 @@ static void __exit vfio_cleanup(void)
 	vfio_virqfd_exit();
 	vfio_group_cleanup();
 	xa_destroy(&vfio_device_set_xa);
+
+	vfio_debugfs_remove_root();

The added blank line is unnecessary, there's no conceptual reason to split this from the remaining teardown. I suspect this should also be first in the teardown since it was setup last on init and I don't see any other dependencies.

Quoted 8 lines
drivers/vfio/vfio_main.c +0 -0
 }
 
 module_init(vfio_init);
include/linux/vfio.h +0 -0
index 93134b023968..fa6b898ebb58 100644

include/linux/vfio.h struct vfio_device { · line 71

Quoted 9 lines
include/linux/vfio.h +6 -0
@@ -63,6 +63,14 @@ struct vfio_device {
 	struct iommufd_ctx *iommufd_ictx;
 	bool iommufd_attached;
 #endif
+
+#ifdef CONFIG_DEBUG_FS
+	/*
+	 * debug_root is a static property of the vfio_device
+	 * which must be set prior to registering the vfio_device.
+	 */

What imposed this ordering requirement? In fact the code flow here makes the debugfs entries available event before the device is registered with vfio core, which seems problematic. This goes away if it's setup as described above. Thanks,

Alex

Quoted 5 lines
include/linux/vfio.h +2 -0
+	struct dentry *debug_root;
+#endif
 };
 
 /**
#3 lore raw

Re: [PATCH v9 1/5] vfio/migration: Add debugfs to live migration driver

Quoted 7 lines · Alex Williamson

This would allow that mlx5 and future drivers automatically get this with no driver code, and in fact all devices would get debugfs dev_name directories for better test coverage. hisi_acc would only need to add driver specific entries after registering the device and free any related data after unregistering the device. In this way, none of the entry points here would need to be exported for drivers, which is perhaps the direction Jason was thinking with his prior comments.

Yes, my point was more not to add more ops specifically to wrapper debugfs stuff. If drivers want a special debugfs file then there is lots of infrastructure to just create that off a common point

Jason

author maintainer
#4 lore raw

Re: [PATCH v9 1/5] vfio/migration: Add debugfs to live migration driver

Quoted 10 lines · Jason Gunthorpe
Quoted 7 lines · Alex Williamson

This would allow that mlx5 and future drivers automatically get this with no driver code, and in fact all devices would get debugfs dev_name directories for better test coverage. hisi_acc would only need to add driver specific entries after registering the device and free any related data after unregistering the device. In this way, none of the entry points here would need to be exported for drivers, which is perhaps the direction Jason was thinking with his prior comments.

Quoted 4 lines

Yes, my point was more not to add more ops specifically to wrapper debugfs stuff. If drivers want a special debugfs file then there is lots of infrastructure to just create that off a common point

OK! I understand what you and Alex are asking for. I will re-debug a version based on your request.

Thanks.
Longfang.

Quoted 3 lines

Jason
.

author maintainer
#5 lore raw

Re: [PATCH v9 1/5] vfio/migration: Add debugfs to live migration driver

In reply to Alex Williamson
Quoted 186 lines · Alex Williamson
Quoted 183 lines · On Mon, 20 Mar 2023 17:23:34 +0800 Longfang Liu <[email protected]> wrote:

There are multiple devices, software and operational steps involved in the process of live migration. An error occurred on any node may cause the live migration operation to fail.
This complex process makes it very difficult to locate and analyze the cause when the function fails.

In order to quickly locate the cause of the problem when the live migration fails, I added a set of debugfs to the vfio live migration driver.

    +-------------------------------------------+
    |                                           |
    |                                           |
    |                  QEMU                     |
    |                                           |
    |                                           |
    +---+----------------------------+----------+
        |      ^                     |      ^
        |      |                     |      |
        |      |                     |      |
        v      |                     v      |
     +---------+--+               +---------+--+
     |src vfio_dev|               |dst vfio_dev|
     +--+---------+               +--+---------+
        |      ^                     |      ^
        |      |                     |      |
        v      |                     |      |
   +-----------+----+           +-----------+----+
   |src dev debugfs |           |dst dev debugfs |
   +----------------+           +----------------+

The entire debugfs directory will be based on the definition of the CONFIG_DEBUG_FS macro. If this macro is not enabled, the interfaces in vfio.h will be empty definitions, and the creation and initialization of the debugfs directory will not be executed.

   vfio
    |
    +---<dev_name1>
    |    +---migration
    |        +--state
    |        +--hisi_acc
    |            +--attr
    |            +--data
    |            +--debug
    |
    +---<dev_name2>
         +---migration
             +--state
             +--hisi_acc
                 +--attr
                 +--data
                 +--debug

debugfs will create a public root directory "vfio" file. then create a dev_name() file for each live migration device. First, create a unified state acquisition file of "migration" in this device directory.
Then, create a public live migration state lookup file "state" Finally, create a directory file based on the device type, and then create the device's own debugging files under this directory file.

Here, HiSilicon accelerator creates three debug files:
attr: used to obtain the attribute parameters of the current live migration device.
data: used to get the live migration data of the current live migration device.
debug: Used to debug the current live migration device through commands.

The live migration function of the current device can be tested by operating the debug files, and the functional status of the equipment and software at each stage can be tested step by step without performing the complete live migration function. And after the live migration is performed, the migration device data of the live migration can be obtained through the debug files.

---
 drivers/vfio/Makefile       |  2 +-
 drivers/vfio/vfio.h         | 14 +++++++
 drivers/vfio/vfio_debugfs.c | 80 +++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_main.c    |  5 +++
 include/linux/vfio.h        |  8 ++++
 5 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 drivers/vfio/vfio_debugfs.c
drivers/vfio/Makefile +1 -1
index 70e7dcb302ef..1debcff31d30 100644
@@ -7,7 +7,7 @@ vfio-y += vfio_main.o \
 vfio-$(CONFIG_IOMMUFD) += iommufd.o
 vfio-$(CONFIG_VFIO_CONTAINER) += container.o
 vfio-$(CONFIG_VFIO_VIRQFD) += virqfd.o
-
+vfio-$(CONFIG_DEBUG_FS) += vfio_debugfs.o
 obj-$(CONFIG_VFIO_IOMMU_TYPE1) += vfio_iommu_type1.o
 obj-$(CONFIG_VFIO_IOMMU_SPAPR_TCE) += vfio_iommu_spapr_tce.o
 obj-$(CONFIG_VFIO_PCI) += pci/
drivers/vfio/vfio.h +14 -0
index e9721d8424bc..8e5cafa6aa3a 100644
@@ -258,4 +258,18 @@ static inline void vfio_device_put_kvm(struct vfio_device *device)
 }
 #endif
 
+#ifdef CONFIG_DEBUG_FS
+void vfio_debugfs_create_root(void);
+void vfio_debugfs_remove_root(void);
+
+void vfio_device_debugfs_init(struct vfio_device *vdev);
+void vfio_device_debugfs_exit(struct vfio_device *vdev);
+#else
+static inline void vfio_debugfs_create_root(void) { }
+static inline void vfio_debugfs_remove_root(void) { }
+
+static inline void vfio_device_debugfs_init(struct vfio_device *vdev) { }
+static inline void vfio_device_debugfs_exit(struct vfio_device *vdev) { }
+#endif /* CONFIG_DEBUG_FS */
+
 #endif
drivers/vfio/vfio_debugfs.c new file +53 -0
new file mode 100644
index 000000000000..f5b6bf2a1d48
@@ -0,0 +1,80 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2023, HiSilicon Ltd.
+ */
+
+#include <linux/device.h>
+#include <linux/debugfs.h>
+#include <linux/seq_file.h>
+#include <linux/vfio.h>
+#include "vfio.h"
+
+static struct dentry *vfio_debugfs_root;
+
+static int vfio_device_state_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_device *vdev = container_of(vf_dev, struct vfio_device, device);
+	enum vfio_device_mig_state state;
+	int ret;
+
+	ret = vdev->mig_ops->migration_get_state(vdev, &state);
+	if (ret)
+		return -EINVAL;
+
+	switch (state) {
+	case VFIO_DEVICE_STATE_RUNNING:
+		seq_printf(seq, "%s\n", "RUNNING");
+		break;
+	case VFIO_DEVICE_STATE_STOP_COPY:
+		seq_printf(seq, "%s\n", "STOP_COPY");
+		break;
+	case VFIO_DEVICE_STATE_STOP:
+		seq_printf(seq, "%s\n", "STOP");
+		break;
+	case VFIO_DEVICE_STATE_RESUMING:
+		seq_printf(seq, "%s\n", "RESUMING");
+		break;
+	case VFIO_DEVICE_STATE_RUNNING_P2P:
+		seq_printf(seq, "%s\n", "RESUMING_P2P");
+		break;
+	case VFIO_DEVICE_STATE_ERROR:
+		seq_printf(seq, "%s\n", "ERROR");
+		break;
+	default:
+		seq_printf(seq, "%s\n", "Invalid");
+	}
+
+	return 0;
+}
+
+void vfio_device_debugfs_init(struct vfio_device *vdev)
+{
+	struct dentry *vfio_dev_migration = NULL;
Quoted 9 lines

drivers/vfio/vfio_debugfs.c line 57

Quoted 4 lines
drivers/vfio/vfio_debugfs.c new file +4 -0
+	struct device *dev = &vdev->device;
+
+	vdev->debug_root = debugfs_create_dir(dev_name(vdev->dev), vfio_debugfs_root);
+	vfio_dev_migration = debugfs_create_dir("migration", vdev->debug_root);

Am I understanding correctly that there seems to be a standard with debugfs helpers to accept and test for bad inputs such that typical callers don't bother to test for ERR_PTRs from cases like above?

It is stated in the debugfs_create_dir interface that an error will be returned only when CONFIG_DEBUG_FS is not enabled.

Quoted 17 lines

drivers/vfio/vfio_debugfs.c line 62

Quoted 5 lines
drivers/vfio/vfio_debugfs.c new file +5 -0
+
+	debugfs_create_devm_seqfile(dev, "state", vfio_dev_migration,
+				  vfio_device_state_read);
+}
+EXPORT_SYMBOL_GPL(vfio_device_debugfs_init);

I think these should be more integrated into vfio core. As it is, this series doesn't enable the base migration/state entry support for drivers other than hisi_acc, where this proposal requires explicit setup and teardown by the driver probe and remove callbacks.

Instead the init and exit functions here should be called directly from __vfio_register_dev() and vfio_unregister_group_dev() respectively. The setup of the migration directory and state file should be conditional on the device providing mig_ops, while the base dev_name directory is setup for all devices for future use cases.

Quoted 8 lines

This would allow that mlx5 and future drivers automatically get this with no driver code, and in fact all devices would get debugfs dev_name directories for better test coverage. hisi_acc would only need to add driver specific entries after registering the device and free any related data after unregistering the device. In this way, none of the entry points here would need to be exported for drivers, which is perhaps the direction Jason was thinking with his prior comments.

I understand your plan.
I will fix it in the next version.

Quoted 25 lines
Quoted 25 lines
drivers/vfio/vfio_debugfs.c new file +18 -0
+
+void vfio_device_debugfs_exit(struct vfio_device *vdev)
+{
+	debugfs_remove_recursive(vdev->debug_root);
+}
+EXPORT_SYMBOL_GPL(vfio_device_debugfs_exit);
+
+void vfio_debugfs_create_root(void)
+{
+	vfio_debugfs_root = debugfs_create_dir("vfio", NULL);
+}
+
+void vfio_debugfs_remove_root(void)
+{
+	debugfs_remove_recursive(vfio_debugfs_root);
+	vfio_debugfs_root = NULL;
+}
+
drivers/vfio/vfio_main.c +0 -0
index 3a597e799918..09772fd3a396 100644
@@ -1408,6 +1408,9 @@ static int __init vfio_init(void)
 	}
 
Quoted 7 lines

drivers/vfio/vfio_main.c static int __init vfio_init(void) · line 1413

Quoted 4 lines
drivers/vfio/vfio_main.c +3 -0
 	pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
+
+	vfio_debugfs_create_root();
+

The pr_info is intended to be the last call in the init path.

OK.

Quoted 30 lines

drivers/vfio/vfio_main.c static void __exit vfio_cleanup(void) · line 1432

Quoted 9 lines
drivers/vfio/vfio_main.c +2 -0
 	return 0;
 
 err_dev_class:
@@ -1425,6 +1428,8 @@ static void __exit vfio_cleanup(void)
 	vfio_virqfd_exit();
 	vfio_group_cleanup();
 	xa_destroy(&vfio_device_set_xa);
+
+	vfio_debugfs_remove_root();

The added blank line is unnecessary, there's no conceptual reason to split this from the remaining teardown. I suspect this should also be first in the teardown since it was setup last on init and I don't see any other dependencies.
OK.

Quoted 14 lines
drivers/vfio/vfio_main.c +0 -0
 }
 
 module_init(vfio_init);
include/linux/vfio.h +3 -0
index 93134b023968..fa6b898ebb58 100644
@@ -63,6 +63,14 @@ struct vfio_device {
 	struct iommufd_ctx *iommufd_ictx;
 	bool iommufd_attached;
 #endif
+
+#ifdef CONFIG_DEBUG_FS
+	/*
Quoted 9 lines

include/linux/vfio.h struct vfio_device { · line 71

Quoted 3 lines
include/linux/vfio.h +3 -0
+	 * debug_root is a static property of the vfio_device
+	 * which must be set prior to registering the vfio_device.
+	 */

What imposed this ordering requirement? In fact the code flow here makes the debugfs entries available event before the device is registered with vfio core, which seems problematic. This goes away if it's setup as described above. Thanks,

OK.
I will modify this comment.

Thanks.
Longfang.

Quoted 10 lines

Alex

include/linux/vfio.h struct vfio_device { · line 76

Quoted 5 lines
include/linux/vfio.h +2 -0
+	struct dentry *debug_root;
+#endif
 };
 
 /**

.

author maintainer
#6 lore raw

PATCH v9 2/5 hisi_acc_vfio_pci: extract public functions for container_of

In reply to Longfang Liu

In the current driver, vdev is obtained from struct hisi_acc_vf_core_device through the container_of function. This method is used in many places in the driver. In order to reduce this repetitive operation, I extracted a public function to replace it.

---
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 21 ++++++++++---------
 1 file changed, 11 insertions(+), 10 deletions(-)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +11 -10
index a117eaf21c14..a1589947e721 100644
@@ -630,6 +630,12 @@ static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vde
 	}
 }
 
+static struct hisi_acc_vf_core_device *hisi_acc_get_vf_dev(struct vfio_device *vdev)
+{
+	return container_of(vdev, struct hisi_acc_vf_core_device,
+			    core_device.vdev);
+}
+
 /*
  * This function is called in all state_mutex unlock cases to
  * handle a 'deferred_reset' if exists.
@@ -1042,8 +1048,7 @@ static struct file *
 hisi_acc_vfio_pci_set_device_state(struct vfio_device *vdev,
 				   enum vfio_device_mig_state new_state)
 {
-	struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(vdev,
-			struct hisi_acc_vf_core_device, core_device.vdev);
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
 	enum vfio_device_mig_state next_state;
 	struct file *res = NULL;
 	int ret;
@@ -1084,8 +1089,7 @@ static int
 hisi_acc_vfio_pci_get_device_state(struct vfio_device *vdev,
 				   enum vfio_device_mig_state *curr_state)
 {
-	struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(vdev,
-			struct hisi_acc_vf_core_device, core_device.vdev);
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
 
 	mutex_lock(&hisi_acc_vdev->state_mutex);
 	*curr_state = hisi_acc_vdev->mig_state;
@@ -1301,8 +1305,7 @@ static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int
 
 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
 {
-	struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
-			struct hisi_acc_vf_core_device, core_device.vdev);
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
 	struct vfio_pci_core_device *vdev = &hisi_acc_vdev->core_device;
 	int ret;
 
@@ -1325,8 +1328,7 @@ static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
 
 static void hisi_acc_vfio_pci_close_device(struct vfio_device *core_vdev)
 {
-	struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
-			struct hisi_acc_vf_core_device, core_device.vdev);
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
 	struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
 
 	iounmap(vf_qm->io_base);
@@ -1341,8 +1343,7 @@ static const struct vfio_migration_ops hisi_acc_vfio_pci_migrn_state_ops = {
 
 static int hisi_acc_vfio_pci_migrn_init_dev(struct vfio_device *core_vdev)
 {
-	struct hisi_acc_vf_core_device *hisi_acc_vdev = container_of(core_vdev,
-			struct hisi_acc_vf_core_device, core_device.vdev);
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
 	struct pci_dev *pdev = to_pci_dev(core_vdev->dev);
 	struct hisi_qm *pf_qm = hisi_acc_get_pf_qm(pdev);
 
Signature · 1 line
2.24.0
author maintainer
#7 lore raw

PATCH v9 3/5 hisi_acc_vfio_pci: register debugfs for hisilicon migration driver

In reply to Longfang Liu

On the debugfs framework of VFIO, if the CONFIG_DEBUG_FS macro is enabled, the debug function is registered for the live migration driver of the HiSilicon accelerator device.

After registering the HiSilicon accelerator device on the debugfs framework of live migration of vfio, a directory file "hisi_acc" of debugfs is created, and then three debug function files are created in this directory:

data file: used to get the migration data of the live migration device attr file: used to get device attributes of the live migration device debug file: used to test for acquiring and writing device state data for VF device.

---
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 290 ++++++++++++++++++
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.h    |  11 +
 2 files changed, 301 insertions(+)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +290 -0
index a1589947e721..22ebe0e8f90f 100644
@@ -15,6 +15,7 @@
 #include <linux/anon_inodes.h>
 
 #include "hisi_acc_vfio_pci.h"
+#include "../../vfio.h"
 
 /* Return 0 on VM acc device ready, -ETIMEDOUT hardware timeout */
 static int qm_wait_dev_not_ready(struct hisi_qm *qm)
@@ -606,6 +607,18 @@ hisi_acc_check_int_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 	}
 }
 
+static void hisi_acc_vf_migf_save(struct hisi_acc_vf_migration_file *src_migf,
+	struct hisi_acc_vf_migration_file *dst_migf)
+{
+	if (!dst_migf)
+		return;
+
+	dst_migf->disabled = false;
+	dst_migf->total_length = src_migf->total_length;
+	memcpy(&dst_migf->vf_data, &src_migf->vf_data,
+		    sizeof(struct acc_vf_data));
+}
+
 static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 {
 	mutex_lock(&migf->lock);
@@ -618,12 +631,16 @@ static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 {
 	if (hisi_acc_vdev->resuming_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->resuming_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->resuming_migf);
 		fput(hisi_acc_vdev->resuming_migf->filp);
 		hisi_acc_vdev->resuming_migf = NULL;
 	}
 
 	if (hisi_acc_vdev->saving_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->saving_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->saving_migf);
 		fput(hisi_acc_vdev->saving_migf->filp);
 		hisi_acc_vdev->saving_migf = NULL;
@@ -1303,6 +1320,273 @@ static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int
 	return vfio_pci_core_ioctl(core_vdev, cmd, arg);
 }
 
+static int hisi_acc_vf_debug_io(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
+	struct device *dev = vdev->dev;
+	u64 data;
+	int ret;
+
+	ret = qm_wait_dev_not_ready(vf_qm);
+	if (ret)
+		dev_err(dev, "VF device not ready!\n");
+
+	data = readl(vf_qm->io_base + QM_MB_CMD_SEND_BASE);
+	dev_info(dev, "debug mailbox val: 0x%llx\n", data);
+
+	return ret;
+}
+
+static int hisi_acc_vf_debug_resume(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_check_match(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to match the VF!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_load_data(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to recover the VF!\n");
+		return -EINVAL;
+	}
+
+	vf_qm_fun_reset(&hisi_acc_vdev->vf_qm);
+	dev_info(dev, "successful to resume device data!\n");
+
+	return 0;
+}
+
+static int hisi_acc_vf_debug_save(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+	dev_info(dev, "successful to save device data!\n");
+
+	return 0;
+}
+
+static int
+hisi_acc_vf_debug_operate(struct vfio_device *vdev, unsigned int cmd)
+{
+	int ret;
+
+	switch (cmd) {
+	case STATE_SAVE:
+		ret = hisi_acc_vf_debug_save(vdev);
+		if (ret)
+			return ret;
+		break;
+	case STATE_RESUME:
+		ret = hisi_acc_vf_debug_resume(vdev);
+		if (ret)
+			return ret;
+		break;
+	case RW_IO_TEST:
+		ret = hisi_acc_vf_debug_io(vdev);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int hisi_acc_vf_debug_check(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	enum vfio_device_mig_state state;
+
+	if (!vdev->mig_ops || !migf) {
+		dev_err(vdev->dev, "device not support debugfs!\n");
+		return -EINVAL;
+	}
+
+	/* If device not opened, the debugfs operation will trigger calltrace */
+	(void)vdev->mig_ops->migration_get_state(vdev, &state);
+	if (state == VFIO_DEVICE_STATE_ERROR ||
+	    state == VFIO_DEVICE_STATE_STOP) {
+		dev_err(vdev->dev, "device not opened!\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static ssize_t hisi_acc_vf_debug_write(struct file *filp, const char __user *buffer,
+			    size_t count, loff_t *pos)
+{
+	struct vfio_device	*vdev = filp->private_data;
+	char tbuf[VFIO_DEV_DBG_LEN];
+	unsigned long cmd;
+	int len, ret;
+
+	if (*pos)
+		return 0;
+
+	if (count >= VFIO_DEV_DBG_LEN)
+		return -ENOSPC;
+
+	ret = hisi_acc_vf_debug_check(vdev);
+	if (ret)
+		return -EINVAL;
+
+	len = simple_write_to_buffer(tbuf, VFIO_DEV_DBG_LEN - 1,
+					pos, buffer, count);
+	if (len < 0 || len > VFIO_DEV_DBG_LEN - 1)
+		return -EINVAL;
+	tbuf[len] = '\0';
+	if (kstrtoul(tbuf, 0, &cmd))
+		return -EFAULT;
+
+	ret = hisi_acc_vf_debug_operate(vdev, cmd);
+	if (ret) {
+		dev_err(vdev->dev, "device debug test failed!\n");
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static const struct file_operations hisi_acc_vf_debug_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = hisi_acc_vf_debug_write,
+};
+
+static void seq_print_hex_data(struct seq_file *seq, const void *buf, size_t len)
+{
+#define HEX_LINE_SIZE		131
+#define HEX_ROW_SIZE		16
+	unsigned char linebuf[HEX_LINE_SIZE];
+	int i, linelen, remaining = len;
+	const u8 *ptr = buf;
+
+	for (i = 0; i < len; i += HEX_ROW_SIZE) {
+		linelen = min(remaining, HEX_ROW_SIZE);
+		remaining -= HEX_ROW_SIZE;
+
+		hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE, 1,
+					linebuf, sizeof(linebuf), false);
+
+		seq_printf(seq, "%s%.8x: %s\n", "Mig Data:", i, linebuf);
+	}
+}
+
+static int hisi_acc_vf_data_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+	size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);
+
+	if (debug_migf && debug_migf->total_length)
+		seq_print_hex_data(seq, (unsigned char *)&debug_migf->vf_data,
+				vf_data_sz);
+	else
+		seq_printf(seq, "%s\n", "device not migrated!");
+
+	return 0;
+}
+
+static int hisi_acc_vf_attr_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+
+	if (debug_migf && debug_migf->total_length) {
+		seq_printf(seq,
+			 "acc device:\n"
+			 "device  state: %d\n"
+			 "device  ready: %u\n"
+			 "data    valid: %d\n"
+			 "data     size: %lu\n",
+			 hisi_acc_vdev->mig_state,
+			 hisi_acc_vdev->vf_qm_state,
+			 debug_migf->disabled,
+			 debug_migf->total_length);
+	} else {
+		seq_printf(seq, "%s\n", "device not migrated!");
+	}
+
+	return 0;
+}
+
+static int hisi_acc_vfio_debug_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+	struct dentry *vfio_dev_migration = NULL;
+	struct dentry *vfio_hisi_acc = NULL;
+	struct device *dev = vdev->dev;
+	void *migf = NULL;
+
+	if (!debugfs_initialized())
+		return 0;
+
+	vfio_device_debugfs_init(vdev);
+
+	migf = kzalloc(sizeof(struct hisi_acc_vf_migration_file), GFP_KERNEL);
+	if (!migf)
+		return -ENOMEM;
+
+	hisi_acc_vdev->debug_migf = migf;
+
+	vfio_dev_migration = debugfs_lookup("migration", vdev->debug_root);
+	if (!vfio_dev_migration) {
+		dev_err(dev, "failed to lookup migration debugfs file!\n");
+		return -ENODEV;
+	}
+
+	vfio_hisi_acc = debugfs_create_dir("hisi_acc", vfio_dev_migration);
+	debugfs_create_devm_seqfile(dev, "data", vfio_hisi_acc,
+				  hisi_acc_vf_data_read);
+	debugfs_create_devm_seqfile(dev, "attr", vfio_hisi_acc,
+				  hisi_acc_vf_attr_read);
+	debugfs_create_file("debug", 0200, vfio_hisi_acc,
+				  vdev, &hisi_acc_vf_debug_fops);
+
+	return 0;
+}
+
+static void hisi_acc_vf_debugfs_exit(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+
+	if (!debugfs_initialized())
+		return;
+
+	vfio_device_debugfs_exit(vdev);
+	kfree(hisi_acc_vdev->debug_migf);
+}
+
 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
@@ -1416,6 +1700,9 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	if (IS_ERR(hisi_acc_vdev))
 		return PTR_ERR(hisi_acc_vdev);
 
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vfio_debug_init(hisi_acc_vdev);
+
 	dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
 	ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
 	if (ret)
@@ -1423,6 +1710,8 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	return 0;
 
 out_put_vdev:
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 	return ret;
 }
@@ -1431,6 +1720,7 @@ static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
 
+	hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 }
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h +11 -0
index dcabfeec6ca1..ef50b12f018d 100644
@@ -49,6 +49,14 @@
 #define QM_EQC_DW0		0X8000
 #define QM_AEQC_DW0		0X8020
 
+#define VFIO_DEV_DBG_LEN		256
+
+enum mig_debug_cmd {
+	STATE_SAVE,
+	STATE_RESUME,
+	RW_IO_TEST,
+};
+
 struct acc_vf_data {
 #define QM_MATCH_SIZE offsetofend(struct acc_vf_data, qm_rsv_state)
 	/* QM match information */
@@ -113,5 +121,8 @@ struct hisi_acc_vf_core_device {
 	spinlock_t reset_lock;
 	struct hisi_acc_vf_migration_file *resuming_migf;
 	struct hisi_acc_vf_migration_file *saving_migf;
+
+	/* For debugfs */
+	struct hisi_acc_vf_migration_file *debug_migf;
 };
 #endif /* HISI_ACC_VFIO_PCI_H */
Signature · 1 line
2.24.0
#8 lore raw

Re: [PATCH v9 3/5] hisi_acc_vfio_pci: register debugfs for hisilicon migration driver

Quoted 127 lines · On Mon, 20 Mar 2023 17:23:36 +0800 Longfang Liu <[email protected]> wrote:

On the debugfs framework of VFIO, if the CONFIG_DEBUG_FS macro is enabled, the debug function is registered for the live migration driver of the HiSilicon accelerator device.

After registering the HiSilicon accelerator device on the debugfs framework of live migration of vfio, a directory file "hisi_acc" of debugfs is created, and then three debug function files are created in this directory:

data file: used to get the migration data of the live migration device attr file: used to get device attributes of the live migration device debug file: used to test for acquiring and writing device state data for VF device.

---
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 290 ++++++++++++++++++
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.h    |  11 +
 2 files changed, 301 insertions(+)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +72 -0
index a1589947e721..22ebe0e8f90f 100644
@@ -15,6 +15,7 @@
 #include <linux/anon_inodes.h>
 
 #include "hisi_acc_vfio_pci.h"
+#include "../../vfio.h"
 
 /* Return 0 on VM acc device ready, -ETIMEDOUT hardware timeout */
 static int qm_wait_dev_not_ready(struct hisi_qm *qm)
@@ -606,6 +607,18 @@ hisi_acc_check_int_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 	}
 }
 
+static void hisi_acc_vf_migf_save(struct hisi_acc_vf_migration_file *src_migf,
+	struct hisi_acc_vf_migration_file *dst_migf)
+{
+	if (!dst_migf)
+		return;
+
+	dst_migf->disabled = false;
+	dst_migf->total_length = src_migf->total_length;
+	memcpy(&dst_migf->vf_data, &src_migf->vf_data,
+		    sizeof(struct acc_vf_data));
+}
+
 static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 {
 	mutex_lock(&migf->lock);
@@ -618,12 +631,16 @@ static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 {
 	if (hisi_acc_vdev->resuming_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->resuming_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->resuming_migf);
 		fput(hisi_acc_vdev->resuming_migf->filp);
 		hisi_acc_vdev->resuming_migf = NULL;
 	}
 
 	if (hisi_acc_vdev->saving_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->saving_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->saving_migf);
 		fput(hisi_acc_vdev->saving_migf->filp);
 		hisi_acc_vdev->saving_migf = NULL;
@@ -1303,6 +1320,273 @@ static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int
 	return vfio_pci_core_ioctl(core_vdev, cmd, arg);
 }
 
+static int hisi_acc_vf_debug_io(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
+	struct device *dev = vdev->dev;
+	u64 data;
+	int ret;
+
+	ret = qm_wait_dev_not_ready(vf_qm);
+	if (ret)
+		dev_err(dev, "VF device not ready!\n");
+
+	data = readl(vf_qm->io_base + QM_MB_CMD_SEND_BASE);
+	dev_info(dev, "debug mailbox val: 0x%llx\n", data);
+
+	return ret;
+}
+
+static int hisi_acc_vf_debug_resume(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_check_match(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to match the VF!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_load_data(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to recover the VF!\n");
+		return -EINVAL;
+	}
+
+	vf_qm_fun_reset(&hisi_acc_vdev->vf_qm);
+	dev_info(dev, "successful to resume device data!\n");
+
+	return 0;
+}
+
+static int hisi_acc_vf_debug_save(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1387

Quoted 10 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +10 -0
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+	dev_info(dev, "successful to save device data!\n");
+
+	return 0;
+}

It seems backwards that we've defined the 'debug' file to be writable only in order to select which operation to perform, where the user then needs to look in dmesg for information related to an errno. Wouldn't it make more sense to create separate save, restore, and io_test files that perform the operation when read and return a buffer indicating the error condition or success? What's the benefit of multiplexing these through writes to a single debug file?

Quoted 18 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +18 -0
+
+static int
+hisi_acc_vf_debug_operate(struct vfio_device *vdev, unsigned int cmd)
+{
+	int ret;
+
+	switch (cmd) {
+	case STATE_SAVE:
+		ret = hisi_acc_vf_debug_save(vdev);
+		if (ret)
+			return ret;
+		break;
+	case STATE_RESUME:
+		ret = hisi_acc_vf_debug_resume(vdev);
+		if (ret)
+			return ret;
+		break;
+	case RW_IO_TEST:

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1415

Quoted 10 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +10 -0
+		ret = hisi_acc_vf_debug_io(vdev);
+		if (ret)
+			return ret;
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}

Initialize ret = -EINVAL, 'return ret' outside the case, and drop the per-case returns.

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1426

Quoted 11 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +11 -0
+
+static int hisi_acc_vf_debug_check(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	enum vfio_device_mig_state state;
+
+	if (!vdev->mig_ops || !migf) {
+		dev_err(vdev->dev, "device not support debugfs!\n");
+		return -EINVAL;
+	}

Why did we create the file then?

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1434

Quoted 8 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +8 -0
+
+	/* If device not opened, the debugfs operation will trigger calltrace */
+	(void)vdev->mig_ops->migration_get_state(vdev, &state);
+	if (state == VFIO_DEVICE_STATE_ERROR ||
+	    state == VFIO_DEVICE_STATE_STOP) {
+		dev_err(vdev->dev, "device not opened!\n");
+		return -EINVAL;
+	}

And we can't use something like open_count? What protects this from racing against the device being released?

Quoted 53 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +53 -0
+
+	return 0;
+}
+
+static ssize_t hisi_acc_vf_debug_write(struct file *filp, const char __user *buffer,
+			    size_t count, loff_t *pos)
+{
+	struct vfio_device	*vdev = filp->private_data;
+	char tbuf[VFIO_DEV_DBG_LEN];
+	unsigned long cmd;
+	int len, ret;
+
+	if (*pos)
+		return 0;
+
+	if (count >= VFIO_DEV_DBG_LEN)
+		return -ENOSPC;
+
+	ret = hisi_acc_vf_debug_check(vdev);
+	if (ret)
+		return -EINVAL;
+
+	len = simple_write_to_buffer(tbuf, VFIO_DEV_DBG_LEN - 1,
+					pos, buffer, count);
+	if (len < 0 || len > VFIO_DEV_DBG_LEN - 1)
+		return -EINVAL;
+	tbuf[len] = '\0';
+	if (kstrtoul(tbuf, 0, &cmd))
+		return -EFAULT;
+
+	ret = hisi_acc_vf_debug_operate(vdev, cmd);
+	if (ret) {
+		dev_err(vdev->dev, "device debug test failed!\n");
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static const struct file_operations hisi_acc_vf_debug_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = hisi_acc_vf_debug_write,
+};
+
+static void seq_print_hex_data(struct seq_file *seq, const void *buf, size_t len)
+{
+#define HEX_LINE_SIZE		131
+#define HEX_ROW_SIZE		16
+	unsigned char linebuf[HEX_LINE_SIZE];
+	int i, linelen, remaining = len;
+	const u8 *ptr = buf;
+

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1497

Quoted 10 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +10 -0
+	for (i = 0; i < len; i += HEX_ROW_SIZE) {
+		linelen = min(remaining, HEX_ROW_SIZE);
+		remaining -= HEX_ROW_SIZE;
+
+		hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE, 1,
+					linebuf, sizeof(linebuf), false);
+
+		seq_printf(seq, "%s%.8x: %s\n", "Mig Data:", i, linebuf);
+	}
+}

Can't this use seq_hex_dump()? If there's a _good_ reason why not, it should be noted in comments and the function should be renamed not to pollute the seq_ namespace. Thanks,

Alex

Quoted 150 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +98 -0
+
+static int hisi_acc_vf_data_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+	size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);
+
+	if (debug_migf && debug_migf->total_length)
+		seq_print_hex_data(seq, (unsigned char *)&debug_migf->vf_data,
+				vf_data_sz);
+	else
+		seq_printf(seq, "%s\n", "device not migrated!");
+
+	return 0;
+}
+
+static int hisi_acc_vf_attr_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+
+	if (debug_migf && debug_migf->total_length) {
+		seq_printf(seq,
+			 "acc device:\n"
+			 "device  state: %d\n"
+			 "device  ready: %u\n"
+			 "data    valid: %d\n"
+			 "data     size: %lu\n",
+			 hisi_acc_vdev->mig_state,
+			 hisi_acc_vdev->vf_qm_state,
+			 debug_migf->disabled,
+			 debug_migf->total_length);
+	} else {
+		seq_printf(seq, "%s\n", "device not migrated!");
+	}
+
+	return 0;
+}
+
+static int hisi_acc_vfio_debug_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+	struct dentry *vfio_dev_migration = NULL;
+	struct dentry *vfio_hisi_acc = NULL;
+	struct device *dev = vdev->dev;
+	void *migf = NULL;
+
+	if (!debugfs_initialized())
+		return 0;
+
+	vfio_device_debugfs_init(vdev);
+
+	migf = kzalloc(sizeof(struct hisi_acc_vf_migration_file), GFP_KERNEL);
+	if (!migf)
+		return -ENOMEM;
+
+	hisi_acc_vdev->debug_migf = migf;
+
+	vfio_dev_migration = debugfs_lookup("migration", vdev->debug_root);
+	if (!vfio_dev_migration) {
+		dev_err(dev, "failed to lookup migration debugfs file!\n");
+		return -ENODEV;
+	}
+
+	vfio_hisi_acc = debugfs_create_dir("hisi_acc", vfio_dev_migration);
+	debugfs_create_devm_seqfile(dev, "data", vfio_hisi_acc,
+				  hisi_acc_vf_data_read);
+	debugfs_create_devm_seqfile(dev, "attr", vfio_hisi_acc,
+				  hisi_acc_vf_attr_read);
+	debugfs_create_file("debug", 0200, vfio_hisi_acc,
+				  vdev, &hisi_acc_vf_debug_fops);
+
+	return 0;
+}
+
+static void hisi_acc_vf_debugfs_exit(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+
+	if (!debugfs_initialized())
+		return;
+
+	vfio_device_debugfs_exit(vdev);
+	kfree(hisi_acc_vdev->debug_migf);
+}
+
 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
@@ -1416,6 +1700,9 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	if (IS_ERR(hisi_acc_vdev))
 		return PTR_ERR(hisi_acc_vdev);
 
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vfio_debug_init(hisi_acc_vdev);
+
 	dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
 	ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
 	if (ret)
@@ -1423,6 +1710,8 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	return 0;
 
 out_put_vdev:
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 	return ret;
 }
@@ -1431,6 +1720,7 @@ static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
 
+	hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 }
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h +11 -0
index dcabfeec6ca1..ef50b12f018d 100644
@@ -49,6 +49,14 @@
 #define QM_EQC_DW0		0X8000
 #define QM_AEQC_DW0		0X8020
 
+#define VFIO_DEV_DBG_LEN		256
+
+enum mig_debug_cmd {
+	STATE_SAVE,
+	STATE_RESUME,
+	RW_IO_TEST,
+};
+
 struct acc_vf_data {
 #define QM_MATCH_SIZE offsetofend(struct acc_vf_data, qm_rsv_state)
 	/* QM match information */
@@ -113,5 +121,8 @@ struct hisi_acc_vf_core_device {
 	spinlock_t reset_lock;
 	struct hisi_acc_vf_migration_file *resuming_migf;
 	struct hisi_acc_vf_migration_file *saving_migf;
+
+	/* For debugfs */
+	struct hisi_acc_vf_migration_file *debug_migf;
 };
 #endif /* HISI_ACC_VFIO_PCI_H */
author maintainer
#9 lore raw

Re: [PATCH v9 3/5] hisi_acc_vfio_pci: register debugfs for hisilicon migration driver

Quoted 141 lines · Alex Williamson
Quoted 127 lines · On Mon, 20 Mar 2023 17:23:36 +0800 Longfang Liu <[email protected]> wrote:

On the debugfs framework of VFIO, if the CONFIG_DEBUG_FS macro is enabled, the debug function is registered for the live migration driver of the HiSilicon accelerator device.

After registering the HiSilicon accelerator device on the debugfs framework of live migration of vfio, a directory file "hisi_acc" of debugfs is created, and then three debug function files are created in this directory:

data file: used to get the migration data of the live migration device attr file: used to get device attributes of the live migration device debug file: used to test for acquiring and writing device state data for VF device.

---
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 290 ++++++++++++++++++
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.h    |  11 +
 2 files changed, 301 insertions(+)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +72 -0
index a1589947e721..22ebe0e8f90f 100644
@@ -15,6 +15,7 @@
 #include <linux/anon_inodes.h>
 
 #include "hisi_acc_vfio_pci.h"
+#include "../../vfio.h"
 
 /* Return 0 on VM acc device ready, -ETIMEDOUT hardware timeout */
 static int qm_wait_dev_not_ready(struct hisi_qm *qm)
@@ -606,6 +607,18 @@ hisi_acc_check_int_state(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 	}
 }
 
+static void hisi_acc_vf_migf_save(struct hisi_acc_vf_migration_file *src_migf,
+	struct hisi_acc_vf_migration_file *dst_migf)
+{
+	if (!dst_migf)
+		return;
+
+	dst_migf->disabled = false;
+	dst_migf->total_length = src_migf->total_length;
+	memcpy(&dst_migf->vf_data, &src_migf->vf_data,
+		    sizeof(struct acc_vf_data));
+}
+
 static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 {
 	mutex_lock(&migf->lock);
@@ -618,12 +631,16 @@ static void hisi_acc_vf_disable_fd(struct hisi_acc_vf_migration_file *migf)
 static void hisi_acc_vf_disable_fds(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 {
 	if (hisi_acc_vdev->resuming_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->resuming_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->resuming_migf);
 		fput(hisi_acc_vdev->resuming_migf->filp);
 		hisi_acc_vdev->resuming_migf = NULL;
 	}
 
 	if (hisi_acc_vdev->saving_migf) {
+		hisi_acc_vf_migf_save(hisi_acc_vdev->saving_migf,
+						hisi_acc_vdev->debug_migf);
 		hisi_acc_vf_disable_fd(hisi_acc_vdev->saving_migf);
 		fput(hisi_acc_vdev->saving_migf->filp);
 		hisi_acc_vdev->saving_migf = NULL;
@@ -1303,6 +1320,273 @@ static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int
 	return vfio_pci_core_ioctl(core_vdev, cmd, arg);
 }
 
+static int hisi_acc_vf_debug_io(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
+	struct device *dev = vdev->dev;
+	u64 data;
+	int ret;
+
+	ret = qm_wait_dev_not_ready(vf_qm);
+	if (ret)
+		dev_err(dev, "VF device not ready!\n");
+
+	data = readl(vf_qm->io_base + QM_MB_CMD_SEND_BASE);
+	dev_info(dev, "debug mailbox val: 0x%llx\n", data);
+
+	return ret;
+}
+
+static int hisi_acc_vf_debug_resume(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_check_match(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to match the VF!\n");
+		return -EINVAL;
+	}
+
+	ret = vf_qm_load_data(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to recover the VF!\n");
+		return -EINVAL;
+	}
+
+	vf_qm_fun_reset(&hisi_acc_vdev->vf_qm);
+	dev_info(dev, "successful to resume device data!\n");
+
+	return 0;
+}
+
+static int hisi_acc_vf_debug_save(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
+	struct device *dev = vdev->dev;
+	int ret;

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1387

Quoted 10 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +10 -0
+
+	ret = vf_qm_state_save(hisi_acc_vdev, migf);
+	if (ret) {
+		dev_err(dev, "failed to save device data!\n");
+		return -EINVAL;
+	}
+	dev_info(dev, "successful to save device data!\n");
+
+	return 0;
+}
Quoted 8 lines

It seems backwards that we've defined the 'debug' file to be writable only in order to select which operation to perform, where the user then needs to look in dmesg for information related to an errno. Wouldn't it make more sense to create separate save, restore, and io_test files that perform the operation when read and return a buffer indicating the error condition or success? What's the benefit of multiplexing these through writes to a single debug file?

Although you have added new debug files. But the user's operation is simplified. Users only need a simple cat operation to trigger the corresponding operation. And it does not require the user to know the corresponding command. This option is acceptable

Quoted 23 lines
Quoted 23 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +23 -0
+
+static int
+hisi_acc_vf_debug_operate(struct vfio_device *vdev, unsigned int cmd)
+{
+	int ret;
+
+	switch (cmd) {
+	case STATE_SAVE:
+		ret = hisi_acc_vf_debug_save(vdev);
+		if (ret)
+			return ret;
+		break;
+	case STATE_RESUME:
+		ret = hisi_acc_vf_debug_resume(vdev);
+		if (ret)
+			return ret;
+		break;
+	case RW_IO_TEST:
+		ret = hisi_acc_vf_debug_io(vdev);
+		if (ret)
+			return ret;
+		break;
+	default:
Quoted 9 lines

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1370

Quoted 5 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +5 -0
+		return -EINVAL;
+	}
+
+	return 0;
+}

Initialize ret = -EINVAL, 'return ret' outside the case, and drop the per-case returns.

After the above debug file is modified, it will be deleted here.

Quoted 5 lines
Quoted 5 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +5 -0
+
+static int hisi_acc_vf_debug_check(struct vfio_device *vdev)
+{
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *migf = hisi_acc_vdev->debug_migf;
Quoted 9 lines

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1426

Quoted 6 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +6 -0
+	enum vfio_device_mig_state state;
+
+	if (!vdev->mig_ops || !migf) {
+		dev_err(vdev->dev, "device not support debugfs!\n");
+		return -EINVAL;
+	}

Why did we create the file then?

After the above debug file is modified, it will be deleted here.

Quoted 12 lines

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1434

Quoted 8 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +8 -0
+
+	/* If device not opened, the debugfs operation will trigger calltrace */
+	(void)vdev->mig_ops->migration_get_state(vdev, &state);
+	if (state == VFIO_DEVICE_STATE_ERROR ||
+	    state == VFIO_DEVICE_STATE_STOP) {
+		dev_err(vdev->dev, "device not opened!\n");
+		return -EINVAL;
+	}

And we can't use something like open_count? What protects this from racing against the device being released?

There is indeed a need for a more atomic detection method to ensure that the device can be operated.

Quoted 59 lines
Quoted 59 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +59 -0
+
+	return 0;
+}
+
+static ssize_t hisi_acc_vf_debug_write(struct file *filp, const char __user *buffer,
+			    size_t count, loff_t *pos)
+{
+	struct vfio_device	*vdev = filp->private_data;
+	char tbuf[VFIO_DEV_DBG_LEN];
+	unsigned long cmd;
+	int len, ret;
+
+	if (*pos)
+		return 0;
+
+	if (count >= VFIO_DEV_DBG_LEN)
+		return -ENOSPC;
+
+	ret = hisi_acc_vf_debug_check(vdev);
+	if (ret)
+		return -EINVAL;
+
+	len = simple_write_to_buffer(tbuf, VFIO_DEV_DBG_LEN - 1,
+					pos, buffer, count);
+	if (len < 0 || len > VFIO_DEV_DBG_LEN - 1)
+		return -EINVAL;
+	tbuf[len] = '\0';
+	if (kstrtoul(tbuf, 0, &cmd))
+		return -EFAULT;
+
+	ret = hisi_acc_vf_debug_operate(vdev, cmd);
+	if (ret) {
+		dev_err(vdev->dev, "device debug test failed!\n");
+		return -EINVAL;
+	}
+
+	return count;
+}
+
+static const struct file_operations hisi_acc_vf_debug_fops = {
+	.owner = THIS_MODULE,
+	.open = simple_open,
+	.write = hisi_acc_vf_debug_write,
+};
+
+static void seq_print_hex_data(struct seq_file *seq, const void *buf, size_t len)
+{
+#define HEX_LINE_SIZE		131
+#define HEX_ROW_SIZE		16
+	unsigned char linebuf[HEX_LINE_SIZE];
+	int i, linelen, remaining = len;
+	const u8 *ptr = buf;
+
+	for (i = 0; i < len; i += HEX_ROW_SIZE) {
+		linelen = min(remaining, HEX_ROW_SIZE);
+		remaining -= HEX_ROW_SIZE;
+
+		hex_dump_to_buffer(ptr + i, linelen, HEX_ROW_SIZE, 1,
+					linebuf, sizeof(linebuf), false);
Quoted 9 lines

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static long hisi_acc_vfio_pci_ioctl(struct vfio_device *core_vdev, unsigned int · line 1497

Quoted 4 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +4 -0
+
+		seq_printf(seq, "%s%.8x: %s\n", "Mig Data:", i, linebuf);
+	}
+}

Can't this use seq_hex_dump()? If there's a _good_ reason why not, it should be noted in comments and the function should be renamed not to pollute the seq_ namespace. Thanks,

I'll debug it with seq_hex_dump().

Thanks,
Longfang.

Quoted 155 lines

Alex

Quoted 142 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +98 -0
+
+static int hisi_acc_vf_data_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+	size_t vf_data_sz = offsetofend(struct acc_vf_data, padding);
+
+	if (debug_migf && debug_migf->total_length)
+		seq_print_hex_data(seq, (unsigned char *)&debug_migf->vf_data,
+				vf_data_sz);
+	else
+		seq_printf(seq, "%s\n", "device not migrated!");
+
+	return 0;
+}
+
+static int hisi_acc_vf_attr_read(struct seq_file *seq, void *data)
+{
+	struct device *vf_dev = seq->private;
+	struct vfio_pci_core_device *core_device = dev_get_drvdata(vf_dev);
+	struct vfio_device	*vdev = &core_device->vdev;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(vdev);
+	struct hisi_acc_vf_migration_file *debug_migf = hisi_acc_vdev->debug_migf;
+
+	if (debug_migf && debug_migf->total_length) {
+		seq_printf(seq,
+			 "acc device:\n"
+			 "device  state: %d\n"
+			 "device  ready: %u\n"
+			 "data    valid: %d\n"
+			 "data     size: %lu\n",
+			 hisi_acc_vdev->mig_state,
+			 hisi_acc_vdev->vf_qm_state,
+			 debug_migf->disabled,
+			 debug_migf->total_length);
+	} else {
+		seq_printf(seq, "%s\n", "device not migrated!");
+	}
+
+	return 0;
+}
+
+static int hisi_acc_vfio_debug_init(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+	struct dentry *vfio_dev_migration = NULL;
+	struct dentry *vfio_hisi_acc = NULL;
+	struct device *dev = vdev->dev;
+	void *migf = NULL;
+
+	if (!debugfs_initialized())
+		return 0;
+
+	vfio_device_debugfs_init(vdev);
+
+	migf = kzalloc(sizeof(struct hisi_acc_vf_migration_file), GFP_KERNEL);
+	if (!migf)
+		return -ENOMEM;
+
+	hisi_acc_vdev->debug_migf = migf;
+
+	vfio_dev_migration = debugfs_lookup("migration", vdev->debug_root);
+	if (!vfio_dev_migration) {
+		dev_err(dev, "failed to lookup migration debugfs file!\n");
+		return -ENODEV;
+	}
+
+	vfio_hisi_acc = debugfs_create_dir("hisi_acc", vfio_dev_migration);
+	debugfs_create_devm_seqfile(dev, "data", vfio_hisi_acc,
+				  hisi_acc_vf_data_read);
+	debugfs_create_devm_seqfile(dev, "attr", vfio_hisi_acc,
+				  hisi_acc_vf_attr_read);
+	debugfs_create_file("debug", 0200, vfio_hisi_acc,
+				  vdev, &hisi_acc_vf_debug_fops);
+
+	return 0;
+}
+
+static void hisi_acc_vf_debugfs_exit(struct hisi_acc_vf_core_device *hisi_acc_vdev)
+{
+	struct vfio_device *vdev = &hisi_acc_vdev->core_device.vdev;
+
+	if (!debugfs_initialized())
+		return;
+
+	vfio_device_debugfs_exit(vdev);
+	kfree(hisi_acc_vdev->debug_migf);
+}
+
 static int hisi_acc_vfio_pci_open_device(struct vfio_device *core_vdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
@@ -1416,6 +1700,9 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	if (IS_ERR(hisi_acc_vdev))
 		return PTR_ERR(hisi_acc_vdev);
 
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vfio_debug_init(hisi_acc_vdev);
+
 	dev_set_drvdata(&pdev->dev, &hisi_acc_vdev->core_device);
 	ret = vfio_pci_core_register_device(&hisi_acc_vdev->core_device);
 	if (ret)
@@ -1423,6 +1710,8 @@ static int hisi_acc_vfio_pci_probe(struct pci_dev *pdev, const struct pci_device
 	return 0;
 
 out_put_vdev:
+	if (ops == &hisi_acc_vfio_pci_migrn_ops)
+		hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 	return ret;
 }
@@ -1431,6 +1720,7 @@ static void hisi_acc_vfio_pci_remove(struct pci_dev *pdev)
 {
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_drvdata(pdev);
 
+	hisi_acc_vf_debugfs_exit(hisi_acc_vdev);
 	vfio_pci_core_unregister_device(&hisi_acc_vdev->core_device);
 	vfio_put_device(&hisi_acc_vdev->core_device.vdev);
 }
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h +8 -0
index dcabfeec6ca1..ef50b12f018d 100644
@@ -49,6 +49,14 @@
 #define QM_EQC_DW0		0X8000
 #define QM_AEQC_DW0		0X8020
 
+#define VFIO_DEV_DBG_LEN		256
+
+enum mig_debug_cmd {
+	STATE_SAVE,
+	STATE_RESUME,
+	RW_IO_TEST,
+};
+
 struct acc_vf_data {
 #define QM_MATCH_SIZE offsetofend(struct acc_vf_data, qm_rsv_state)
 	/* QM match information */

drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h struct hisi_acc_vf_core_device { · line 128

Quoted 8 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h +3 -0
@@ -113,5 +121,8 @@ struct hisi_acc_vf_core_device {
 	spinlock_t reset_lock;
 	struct hisi_acc_vf_migration_file *resuming_migf;
 	struct hisi_acc_vf_migration_file *saving_migf;
+
+	/* For debugfs */
+	struct hisi_acc_vf_migration_file *debug_migf;
 };
 #endif /* HISI_ACC_VFIO_PCI_H */

.

author maintainer
#10 lore raw

PATCH v9 4/5 Documentation: add debugfs description for vfio

In reply to Longfang Liu

Add two debugfs document description file to help users understand how to use the accelerator live migration driver's debugfs.

---
 .../ABI/testing/debugfs-hisi-migration        | 28 +++++++++++++++++++
 Documentation/ABI/testing/debugfs-vfio        | 25 +++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 Documentation/ABI/testing/debugfs-hisi-migration
 create mode 100644 Documentation/ABI/testing/debugfs-vfio
Documentation/ABI/testing/debugfs-hisi-migration new file +28 -0
new file mode 100644
index 000000000000..ffcf4b5ecd51
@@ -0,0 +1,28 @@
+What:		/sys/kernel/debug/vfio/<device>/migration/hisi_acc/data
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	Read the live migration data of the vfio device.
+		The output format of the data is defined by the live
+		migration driver.
+
+What:		/sys/kernel/debug/vfio/<device>/migration/hisi_acc/attr
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	Read the live migration attributes of the vfio device.
+		The output format of the attributes is defined by the live
+		migration driver.
+
+What:		/sys/kernel/debug/vfio/<device>/migration/hisi_acc/debug
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	This debug file supports "echo" command	write operations.
+		The command is actually represented by an integer number, and the
+		specific meaning is defined by the corresponding device driver.
+		For example, three commands are defined in the HiSilicon accelerator
+		live migration driver:
+		0: State save
+		1: State resume
+		2: IO test
Documentation/ABI/testing/debugfs-vfio new file +25 -0
new file mode 100644
index 000000000000..81a66cf60553
@@ -0,0 +1,25 @@
+What:		/sys/kernel/debug/vfio
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	This debugfs file directory is used for debugging
+		of vfio devices.
+		Each device can create a device subdirectory under this
+		directory by referencing the public registration interface.
+
+What:		/sys/kernel/debug/vfio/<device>/migration
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	This debugfs file directory is used for debugging
+		of vfio devices that support live migration.
+		The debugfs of each vfio device that supports live migration
+		could be created under this directory.
+
+What:		/sys/kernel/debug/vfio/<device>/migration/state
+Date:		Mar 2023
+KernelVersion:  6.2
+Contact:	Longfang Liu <[email protected]>
+Description:	Read the live migration status of the vfio device.
+		The status of these live migrations includes:
+		ERROR, RUNNING, STOP, STOP_COPY, RESUMING.
Signature · 1 line
2.24.0
author maintainer
#11 lore raw

PATCH v9 5/5 vfio: update live migration device status

In reply to Longfang Liu

migration debugfs needs to perform debug operations based on the status of the current device. If the device is not loaded or has stopped, debugfs does not allow operations.

so, after the live migration function is executed and the device is turned off, the device no longer needs to be accessed. At this time, the status of the device needs to be set to stop.

---
 drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c | 3 +++
 drivers/vfio/pci/mlx5/main.c                   | 3 +++
 2 files changed, 6 insertions(+)
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +3 -0
index 22ebe0e8f90f..54573b4f7f6d 100644
@@ -1615,6 +1615,9 @@ static void hisi_acc_vfio_pci_close_device(struct vfio_device *core_vdev)
 	struct hisi_acc_vf_core_device *hisi_acc_vdev = hisi_acc_get_vf_dev(core_vdev);
 	struct hisi_qm *vf_qm = &hisi_acc_vdev->vf_qm;
 
+	if (core_vdev->mig_ops)
+		hisi_acc_vdev->mig_state = VFIO_DEVICE_STATE_STOP;
+
 	iounmap(vf_qm->io_base);
 	vfio_pci_core_close_device(core_vdev);
 }
drivers/vfio/pci/mlx5/main.c +3 -0
index e897537a9e8a..dc3564436946 100644
@@ -1269,6 +1269,9 @@ static void mlx5vf_pci_close_device(struct vfio_device *core_vdev)
 	struct mlx5vf_pci_core_device *mvdev = container_of(
 		core_vdev, struct mlx5vf_pci_core_device, core_device.vdev);
 
+	if (mvdev->migrate_cap)
+		mvdev->mig_state = VFIO_DEVICE_STATE_STOP;
+
 	mlx5vf_cmd_close_migratable(mvdev);
 	vfio_pci_core_close_device(core_vdev);
 }
Signature · 1 line
2.24.0