From patchwork Mon Oct 31 06:35:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jike Song X-Patchwork-Id: 9405075 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D5C1B6022E for ; Mon, 31 Oct 2016 06:40:51 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C55CC2912B for ; Mon, 31 Oct 2016 06:40:51 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B571429133; Mon, 31 Oct 2016 06:40:51 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00 autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 04E252912B for ; Mon, 31 Oct 2016 06:40:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1761280AbcJaGki (ORCPT ); Mon, 31 Oct 2016 02:40:38 -0400 Received: from mga09.intel.com ([134.134.136.24]:46459 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1761178AbcJaGkf (ORCPT ); Mon, 31 Oct 2016 02:40:35 -0400 Received: from orsmga004.jf.intel.com ([10.7.209.38]) by orsmga102.jf.intel.com with ESMTP; 30 Oct 2016 23:40:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.31,573,1473145200"; d="scan'208";a="25931471" Received: from kvmgt1.bj.intel.com ([10.238.154.158]) by orsmga004.jf.intel.com with ESMTP; 30 Oct 2016 23:40:33 -0700 From: Jike Song To: pbonzini@redhat.com, alex.williamson@redhat.com, guangrong.xiao@linux.intel.com Cc: kwankhede@nvidia.com, cjia@nvidia.com, kevin.tian@intel.com, jike.song@intel.com, kvm@vger.kernel.org Subject: [v3 5/5] KVM: set/clear kvm to/from vfio group during add/delete Date: Mon, 31 Oct 2016 14:35:06 +0800 Message-Id: <1477895706-22824-6-git-send-email-jike.song@intel.com> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1477895706-22824-1-git-send-email-jike.song@intel.com> References: <1477895706-22824-1-git-send-email-jike.song@intel.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP With VFIO being capable of carrying usrdata, we can extend the existing interfaces between KVM and VFIO, whenever a group is added to or deleted from KVM, set/clear KVM instance to/from this group. That enables 3rd-party users of vfio_group to know which KVM it attached to, if there is one. Signed-off-by: Jike Song Acked-by: Paolo Bonzini --- virt/kvm/vfio.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 1dd087d..62da226 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -60,6 +60,34 @@ static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group) symbol_put(vfio_group_put_external_user); } +static void kvm_vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm) +{ + void (*fn)(struct vfio_group *, struct kvm *); + + fn = symbol_get(vfio_group_set_kvm); + if (!fn) + return; + + fn(group, kvm); + kvm_get_kvm(kvm); + + symbol_put(vfio_group_set_kvm); +} + +static void kvm_vfio_group_clear_kvm(struct vfio_group *group, struct kvm *kvm) +{ + void (*fn)(struct vfio_group *, struct kvm *); + + fn = symbol_get(vfio_group_set_kvm); + if (!fn) + return; + + fn(group, NULL); + kvm_put_kvm(kvm); + + symbol_put(vfio_group_set_kvm); +} + static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group) { long (*fn)(struct vfio_group *, unsigned long); @@ -155,6 +183,8 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg) list_add_tail(&kvg->node, &kv->group_list); kvg->vfio_group = vfio_group; + kvm_vfio_group_set_kvm(vfio_group, dev->kvm); + kvm_arch_start_assignment(dev->kvm); mutex_unlock(&kv->lock); @@ -196,6 +226,8 @@ static int kvm_vfio_set_group(struct kvm_device *dev, long attr, u64 arg) mutex_unlock(&kv->lock); + kvm_vfio_group_clear_kvm(vfio_group, dev->kvm); + kvm_vfio_group_put_external_user(vfio_group); kvm_vfio_update_coherency(dev); @@ -240,6 +272,7 @@ static void kvm_vfio_destroy(struct kvm_device *dev) struct kvm_vfio_group *kvg, *tmp; list_for_each_entry_safe(kvg, tmp, &kv->group_list, node) { + kvm_vfio_group_clear_kvm(kvg->vfio_group, dev->kvm); kvm_vfio_group_put_external_user(kvg->vfio_group); list_del(&kvg->node); kfree(kvg);