[1/4] hisi_acc_vfio_pci: Add support for precopy IOCTL

Message ID 20221123113236.896-2-shameerali.kolothum.thodi@huawei.com
State New
Headers
Series hisi_acc_vfio_pci: Add PRE_COPY migration feature support |

Commit Message

Shameerali Kolothum Thodi Nov. 23, 2022, 11:32 a.m. UTC
  PRECOPY IOCTL in the case of HiSiIicon ACC driver can be used to
perform the device compatibility check earlier during migration.

Signed-off-by: Shameer Kolothum <shameerali.kolothum.thodi@huawei.com>
---
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.c    | 52 +++++++++++++++++++
 .../vfio/pci/hisilicon/hisi_acc_vfio_pci.h    |  1 +
 2 files changed, 53 insertions(+)
  

Comments

kernel test robot Dec. 1, 2022, 1:29 p.m. UTC | #1
Hi Shameer,

I love your patch! Yet something to improve:

[auto build test ERROR on awilliam-vfio/for-linus]
[also build test ERROR on linus/master v6.1-rc7 next-20221201]
[cannot apply to awilliam-vfio/next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Shameer-Kolothum/hisi_acc_vfio_pci-Add-PRE_COPY-migration-feature-support/20221123-193542
base:   https://github.com/awilliam/linux-vfio.git for-linus
patch link:    https://lore.kernel.org/r/20221123113236.896-2-shameerali.kolothum.thodi%40huawei.com
patch subject: [PATCH 1/4] hisi_acc_vfio_pci: Add support for precopy IOCTL
config: ia64-allmodconfig
compiler: ia64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/7ac2dded048e6633c89d0e330dad983a81705eb2
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Shameer-Kolothum/hisi_acc_vfio_pci-Add-PRE_COPY-migration-feature-support/20221123-193542
        git checkout 7ac2dded048e6633c89d0e330dad983a81705eb2
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=ia64 SHELL=/bin/bash drivers/vfio/pci/hisilicon/

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c: In function 'hisi_acc_vf_precopy_ioctl':
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:777:34: error: storage size of 'info' isn't known
     777 |         struct vfio_precopy_info info;
         |                                  ^~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:781:20: error: 'VFIO_MIG_GET_PRECOPY_INFO' undeclared (first use in this function)
     781 |         if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:781:20: note: each undeclared identifier is reported only once for each function it appears in
   In file included from include/uapi/linux/posix_types.h:5,
                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kasan-checks.h:5,
                    from include/asm-generic/rwonce.h:26,
                    from ./arch/ia64/include/generated/asm/rwonce.h:1,
                    from include/linux/compiler.h:246,
                    from include/linux/dev_printk.h:14,
                    from include/linux/device.h:15,
                    from drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:6:
>> include/linux/stddef.h:16:33: error: invalid use of undefined type 'struct vfio_precopy_info'
      16 | #define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
         |                                 ^~~~~~~~~~~~~~~~~~
   include/linux/stddef.h:33:10: note: in expansion of macro 'offsetof'
      33 |         (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
         |          ^~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:784:17: note: in expansion of macro 'offsetofend'
     784 |         minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
         |                 ^~~~~~~~~~~
   include/linux/stddef.h:24:55: error: invalid use of undefined type 'struct vfio_precopy_info'
      24 | #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
         |                                                       ^~
   include/linux/stddef.h:33:35: note: in expansion of macro 'sizeof_field'
      33 |         (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
         |                                   ^~~~~~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:784:17: note: in expansion of macro 'offsetofend'
     784 |         minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
         |                 ^~~~~~~~~~~
>> drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:792:41: error: 'VFIO_DEVICE_STATE_PRE_COPY' undeclared (first use in this function); did you mean 'VFIO_DEVICE_STATE_STOP_COPY'?
     792 |         if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
         |                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                         VFIO_DEVICE_STATE_STOP_COPY
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:777:34: warning: unused variable 'info' [-Wunused-variable]
     777 |         struct vfio_precopy_info info;
         |                                  ^~~~


vim +16 include/linux/stddef.h

6e218287432472 Richard Knutsson 2006-09-30  14  
^1da177e4c3f41 Linus Torvalds   2005-04-16  15  #undef offsetof
14e83077d55ff4 Rasmus Villemoes 2022-03-23 @16  #define offsetof(TYPE, MEMBER)	__builtin_offsetof(TYPE, MEMBER)
3876488444e712 Denys Vlasenko   2015-03-09  17
  
kernel test robot Dec. 2, 2022, 9:01 a.m. UTC | #2
Hi Shameer,

I love your patch! Yet something to improve:

[auto build test ERROR on awilliam-vfio/for-linus]
[also build test ERROR on linus/master v6.1-rc7 next-20221202]
[cannot apply to awilliam-vfio/next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Shameer-Kolothum/hisi_acc_vfio_pci-Add-PRE_COPY-migration-feature-support/20221123-193542
base:   https://github.com/awilliam/linux-vfio.git for-linus
patch link:    https://lore.kernel.org/r/20221123113236.896-2-shameerali.kolothum.thodi%40huawei.com
patch subject: [PATCH 1/4] hisi_acc_vfio_pci: Add support for precopy IOCTL
config: arm64-allyesconfig
compiler: aarch64-linux-gcc (GCC) 12.1.0
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # https://github.com/intel-lab-lkp/linux/commit/7ac2dded048e6633c89d0e330dad983a81705eb2
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Shameer-Kolothum/hisi_acc_vfio_pci-Add-PRE_COPY-migration-feature-support/20221123-193542
        git checkout 7ac2dded048e6633c89d0e330dad983a81705eb2
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm64 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>

All errors (new ones prefixed by >>):

   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c: In function 'hisi_acc_vf_precopy_ioctl':
>> drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:777:34: error: storage size of 'info' isn't known
     777 |         struct vfio_precopy_info info;
         |                                  ^~~~
>> drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:781:20: error: 'VFIO_MIG_GET_PRECOPY_INFO' undeclared (first use in this function)
     781 |         if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
         |                    ^~~~~~~~~~~~~~~~~~~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:781:20: note: each undeclared identifier is reported only once for each function it appears in
   In file included from include/uapi/linux/posix_types.h:5,
                    from include/uapi/linux/types.h:14,
                    from include/linux/types.h:6,
                    from include/linux/kasan-checks.h:5,
                    from include/asm-generic/rwonce.h:26,
                    from arch/arm64/include/asm/rwonce.h:71,
                    from include/linux/compiler.h:246,
                    from include/linux/dev_printk.h:14,
                    from include/linux/device.h:15,
                    from drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:6:
   include/linux/stddef.h:16:33: error: invalid use of undefined type 'struct vfio_precopy_info'
      16 | #define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
         |                                 ^~~~~~~~~~~~~~~~~~
   include/linux/stddef.h:33:10: note: in expansion of macro 'offsetof'
      33 |         (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
         |          ^~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:784:17: note: in expansion of macro 'offsetofend'
     784 |         minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
         |                 ^~~~~~~~~~~
   include/linux/stddef.h:24:55: error: invalid use of undefined type 'struct vfio_precopy_info'
      24 | #define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
         |                                                       ^~
   include/linux/stddef.h:33:35: note: in expansion of macro 'sizeof_field'
      33 |         (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
         |                                   ^~~~~~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:784:17: note: in expansion of macro 'offsetofend'
     784 |         minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
         |                 ^~~~~~~~~~~
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:792:41: error: 'VFIO_DEVICE_STATE_PRE_COPY' undeclared (first use in this function); did you mean 'VFIO_DEVICE_STATE_STOP_COPY'?
     792 |         if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
         |                                         ^~~~~~~~~~~~~~~~~~~~~~~~~~
         |                                         VFIO_DEVICE_STATE_STOP_COPY
   drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c:777:34: warning: unused variable 'info' [-Wunused-variable]
     777 |         struct vfio_precopy_info info;
         |                                  ^~~~


vim +777 drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c

   770	
   771	static long hisi_acc_vf_precopy_ioctl(struct file *filp,
   772					      unsigned int cmd, unsigned long arg)
   773	{
   774		struct hisi_acc_vf_migration_file *migf = filp->private_data;
   775		struct hisi_acc_vf_core_device *hisi_acc_vdev = migf->hisi_acc_vdev;
   776		loff_t *pos = &filp->f_pos;
 > 777		struct vfio_precopy_info info;
   778		unsigned long minsz;
   779		int ret;
   780	
 > 781		if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
   782			return -ENOTTY;
   783	
   784		minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
   785	
   786		if (copy_from_user(&info, (void __user *)arg, minsz))
   787			return -EFAULT;
   788		if (info.argsz < minsz)
   789			return -EINVAL;
   790	
   791		mutex_lock(&hisi_acc_vdev->state_mutex);
   792		if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
   793			mutex_unlock(&hisi_acc_vdev->state_mutex);
   794			return -EINVAL;
   795		}
   796	
   797		mutex_lock(&migf->lock);
   798	
   799		if (migf->disabled) {
   800			ret = -ENODEV;
   801			goto out;
   802		}
   803	
   804		if (*pos > migf->total_length) {
   805			ret = -EINVAL;
   806			goto out;
   807		}
   808	
   809		info.dirty_bytes = 0;
   810		info.initial_bytes = migf->total_length - *pos;
   811	
   812		ret = copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
   813	out:
   814		mutex_unlock(&migf->lock);
   815		mutex_unlock(&hisi_acc_vdev->state_mutex);
   816		return ret;
   817	}
   818
  

Patch

diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
index 0c0c0c7f0521..f3b74a06edb6 100644
--- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
+++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c
@@ -764,9 +764,58 @@  hisi_acc_vf_pci_resume(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 
 	stream_open(migf->filp->f_inode, migf->filp);
 	mutex_init(&migf->lock);
+	migf->hisi_acc_vdev = hisi_acc_vdev;
 	return migf;
 }
 
+static long hisi_acc_vf_precopy_ioctl(struct file *filp,
+				      unsigned int cmd, unsigned long arg)
+{
+	struct hisi_acc_vf_migration_file *migf = filp->private_data;
+	struct hisi_acc_vf_core_device *hisi_acc_vdev = migf->hisi_acc_vdev;
+	loff_t *pos = &filp->f_pos;
+	struct vfio_precopy_info info;
+	unsigned long minsz;
+	int ret;
+
+	if (cmd != VFIO_MIG_GET_PRECOPY_INFO)
+		return -ENOTTY;
+
+	minsz = offsetofend(struct vfio_precopy_info, dirty_bytes);
+
+	if (copy_from_user(&info, (void __user *)arg, minsz))
+		return -EFAULT;
+	if (info.argsz < minsz)
+		return -EINVAL;
+
+	mutex_lock(&hisi_acc_vdev->state_mutex);
+	if (hisi_acc_vdev->mig_state != VFIO_DEVICE_STATE_PRE_COPY) {
+		mutex_unlock(&hisi_acc_vdev->state_mutex);
+		return -EINVAL;
+	}
+
+	mutex_lock(&migf->lock);
+
+	if (migf->disabled) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	if (*pos > migf->total_length) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	info.dirty_bytes = 0;
+	info.initial_bytes = migf->total_length - *pos;
+
+	ret = copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
+out:
+	mutex_unlock(&migf->lock);
+	mutex_unlock(&hisi_acc_vdev->state_mutex);
+	return ret;
+}
+
 static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t len,
 				     loff_t *pos)
 {
@@ -807,6 +856,8 @@  static ssize_t hisi_acc_vf_save_read(struct file *filp, char __user *buf, size_t
 static const struct file_operations hisi_acc_vf_save_fops = {
 	.owner = THIS_MODULE,
 	.read = hisi_acc_vf_save_read,
+	.unlocked_ioctl = hisi_acc_vf_precopy_ioctl,
+	.compat_ioctl = compat_ptr_ioctl,
 	.release = hisi_acc_vf_release_file,
 	.llseek = no_llseek,
 };
@@ -832,6 +883,7 @@  hisi_acc_vf_stop_copy(struct hisi_acc_vf_core_device *hisi_acc_vdev)
 
 	stream_open(migf->filp->f_inode, migf->filp);
 	mutex_init(&migf->lock);
+	migf->hisi_acc_vdev = hisi_acc_vdev;
 
 	ret = vf_qm_state_save(hisi_acc_vdev, migf);
 	if (ret) {
diff --git a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h
index 67343325b320..11d51345f5b5 100644
--- a/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h
+++ b/drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.h
@@ -91,6 +91,7 @@  struct hisi_acc_vf_migration_file {
 	struct mutex lock;
 	bool disabled;
 
+	struct hisi_acc_vf_core_device *hisi_acc_vdev;
 	struct acc_vf_data vf_data;
 	size_t total_length;
 };