diff mbox

[v2,4/5] vfio: implement APIs to set/put kvm to/from vfio group

Message ID 1477771515-18015-5-git-send-email-jike.song@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jike Song Oct. 29, 2016, 8:05 p.m. UTC
A vfio_group may be or may not be attached to a KVM instance,
but if it is, the user of vfio_group might also want to know
which KVM instance it is attached to, to perform some operations
exclusively provided by KVM. In VFIO there are already external
APIs for KVM to get/put the vfio_group, by extending these APIs
KVM can set or clear itself to/from the vfio_group, for external
users to get/put.

Signed-off-by: Jike Song <jike.song@intel.com>
---
 drivers/vfio/vfio.c  | 30 ++++++++++++++++++++++++++++++
 include/linux/vfio.h |  4 ++++
 2 files changed, 34 insertions(+)

Comments

kernel test robot Oct. 29, 2016, 10:15 p.m. UTC | #1
Hi Jike,

[auto build test ERROR on vfio/next]
[also build test ERROR on v4.9-rc3]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
[Suggest to use git(>=2.9.0) format-patch --base=<commit> (or --base=auto for convenience) to record what (public, well-known) commit your patch series was built on]
[Check https://git-scm.com/docs/git-format-patch for more information]

url:    https://github.com/0day-ci/linux/commits/Jike-Song/plumb-kvm-vfio-to-allow-getting-kvm-from-vfio_group/20161030-041353
base:   https://github.com/awilliam/linux-vfio.git next
config: ia64-allmodconfig (attached as .config)
compiler: ia64-linux-gcc (GCC) 6.2.0
reproduce:
        wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=ia64 

All errors (new ones prefixed by >>):

   In file included from include/linux/kvm_host.h:31:0,
                    from drivers/vfio/vfio.c:37:
>> include/uapi/linux/kvm.h:13:21: fatal error: asm/kvm.h: No such file or directory
    #include <asm/kvm.h>
                        ^
   compilation terminated.

vim +13 include/uapi/linux/kvm.h

6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   1  #ifndef __LINUX_KVM_H
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   2  #define __LINUX_KVM_H
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   3  
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   4  /*
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   5   * Userspace interface for /dev/kvm - kernel based virtual machine
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   6   *
dea8caee include/linux/kvm.h Rusty Russell          2007-07-17   7   * Note: you must update KVM_API_VERSION if you change this interface.
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   8   */
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10   9  
00bfddaf include/linux/kvm.h Jaswinder Singh Rajput 2009-01-15  10  #include <linux/types.h>
97646202 include/linux/kvm.h Christian Borntraeger  2008-03-12  11  #include <linux/compiler.h>
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10  12  #include <linux/ioctl.h>
f6a40e3b include/linux/kvm.h Jerone Young           2007-11-19 @13  #include <asm/kvm.h>
6aa8b732 include/linux/kvm.h Avi Kivity             2006-12-10  14  
2ff81f70 include/linux/kvm.h Avi Kivity             2007-04-29  15  #define KVM_API_VERSION 12
0b76e20b include/linux/kvm.h Avi Kivity             2006-12-22  16  
c54d2aba include/linux/kvm.h Jan Kiszka             2009-11-02  17  /* *** Deprecated interfaces *** */
c54d2aba include/linux/kvm.h Jan Kiszka             2009-11-02  18  
c54d2aba include/linux/kvm.h Jan Kiszka             2009-11-02  19  #define KVM_TRC_SHIFT           16
c54d2aba include/linux/kvm.h Jan Kiszka             2009-11-02  20  
c54d2aba include/linux/kvm.h Jan Kiszka             2009-11-02  21  #define KVM_TRC_ENTRYEXIT       (1 << KVM_TRC_SHIFT)

:::::: The code at line 13 was first introduced by commit
:::::: f6a40e3bdf5fe0a7d7d7f2dbc5b10158fbdad968 KVM: Portability: Move kvm_memory_alias to asm/kvm.h

:::::: TO: Jerone Young <jyoung5@us.ibm.com>
:::::: CC: Avi Kivity <avi@qumranet.com>

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation
Paolo Bonzini Oct. 30, 2016, 8:52 a.m. UTC | #2
On 29/10/2016 22:05, Jike Song wrote:
> +	mutex_lock(&group->udata.lock);
> +
> +	if (!group->udata.kvm)
> +		goto out;
> +
> +	kvm_get_kvm(group->udata.kvm);
> +	mutex_unlock(&group->udata.lock);
> +	return group->udata.kvm;

There is still a very small race window, so you need to assign
group->udata.kvm to a local variable inside the mutex, and then return.

Paolo

> +out:
> +	mutex_unlock(&group->udata.lock);
> +	return NULL;
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Jike Song Oct. 31, 2016, 2:12 a.m. UTC | #3
On 10/30/2016 04:52 PM, Paolo Bonzini wrote:
> On 29/10/2016 22:05, Jike Song wrote:
>> +	mutex_lock(&group->udata.lock);
>> +
>> +	if (!group->udata.kvm)
>> +		goto out;
>> +
>> +	kvm_get_kvm(group->udata.kvm);
>> +	mutex_unlock(&group->udata.lock);
>> +	return group->udata.kvm;
> 
> There is still a very small race window, so you need to assign
> group->udata.kvm to a local variable inside the mutex, and then return.

Ok, will do that, thanks :)

--
Thanks,
Jike
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c
index e3e58e3..a5c90d2 100644
--- a/drivers/vfio/vfio.c
+++ b/drivers/vfio/vfio.c
@@ -34,6 +34,7 @@ 
 #include <linux/uaccess.h>
 #include <linux/vfio.h>
 #include <linux/wait.h>
+#include <linux/kvm_host.h>
 
 #define DRIVER_VERSION	"0.3"
 #define DRIVER_AUTHOR	"Alex Williamson <alex.williamson@redhat.com>"
@@ -86,6 +87,10 @@  struct vfio_group {
 	struct mutex			unbound_lock;
 	atomic_t			opened;
 	bool				noiommu;
+	struct {
+		struct kvm *kvm;
+		struct mutex lock;
+	} udata;
 };
 
 struct vfio_device {
@@ -333,6 +338,7 @@  static struct vfio_group *vfio_create_group(struct iommu_group *iommu_group)
 	mutex_init(&group->device_lock);
 	INIT_LIST_HEAD(&group->unbound_list);
 	mutex_init(&group->unbound_lock);
+	mutex_init(&group->udata.lock);
 	atomic_set(&group->container_users, 0);
 	atomic_set(&group->opened, 0);
 	group->iommu_group = iommu_group;
@@ -1739,6 +1745,30 @@  long vfio_external_check_extension(struct vfio_group *group, unsigned long arg)
 }
 EXPORT_SYMBOL_GPL(vfio_external_check_extension);
 
+void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm)
+{
+	mutex_lock(&group->udata.lock);
+	group->udata.kvm = kvm;
+	mutex_unlock(&group->udata.lock);
+}
+EXPORT_SYMBOL_GPL(vfio_group_set_kvm);
+
+struct kvm *vfio_group_get_kvm(struct vfio_group *group)
+{
+	mutex_lock(&group->udata.lock);
+
+	if (!group->udata.kvm)
+		goto out;
+
+	kvm_get_kvm(group->udata.kvm);
+	mutex_unlock(&group->udata.lock);
+	return group->udata.kvm;
+out:
+	mutex_unlock(&group->udata.lock);
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(vfio_group_get_kvm);
+
 /**
  * Sub-module support
  */
diff --git a/include/linux/vfio.h b/include/linux/vfio.h
index ad9b857..3abd690 100644
--- a/include/linux/vfio.h
+++ b/include/linux/vfio.h
@@ -95,6 +95,10 @@  extern long vfio_external_check_extension(struct vfio_group *group,
 extern struct vfio_group *vfio_group_get_from_dev(struct device *dev);
 extern void vfio_group_put(struct vfio_group *group);
 
+struct kvm;
+extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
+extern struct kvm *vfio_group_get_kvm(struct vfio_group *group);
+
 /*
  * Sub-module helpers
  */