From patchwork Thu Jun 11 10:51:46 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Wu, Feng" X-Patchwork-Id: 49752 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f197.google.com (mail-wi0-f197.google.com [209.85.212.197]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id A10232460E for ; Thu, 11 Jun 2015 11:05:47 +0000 (UTC) Received: by wizw5 with SMTP id w5sf1912910wiz.2 for ; Thu, 11 Jun 2015 04:05:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=vQLs4fenT1Zr/xHixUsrzM5WMMIh6PqF/oxSYf4OS3Y=; b=A0DMLAfS8au8cyZj1yxE3oEKkPLkaFTyMJ0rzfWOXt8GgLwUDyPoo7KI+HfFQpdymf 0pMOQWPuaP6JHBjRdUGRm8FtG5NLaY9KV9up0yJC5iCmPRx7doB6qPPpc6C5SzzwmZ8w ncOLUh1uyHb/X/Fq+vvlSxBP3e723gCNX0RS4qRKmmzghLpVoxqE/a+vouSlmuQtSB+3 1CY1TVqtnrzklEwn8TEOASGcZMMrgX8Gqmctx4ilZF0zbrZwtUg/qapIQr87ZtN7ZBZw sDEYFmvC8sj0t23zT4qalE36BeV85MnRXpSP8lSIU4LVtLli63WxYkiq8TDXFBCgn85U Lr0g== X-Gm-Message-State: ALoCoQkI35LIKdTaiRRkZwdALFzqN77e6IijdjYwUEXQL/VEpp8HhP2DoypVFjNKAriLnSWQuxyq X-Received: by 10.112.142.170 with SMTP id rx10mr7935225lbb.12.1434020746981; Thu, 11 Jun 2015 04:05:46 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.8.51 with SMTP id o19ls364967laa.95.gmail; Thu, 11 Jun 2015 04:05:46 -0700 (PDT) X-Received: by 10.153.7.104 with SMTP id db8mr9420442lad.124.1434020746811; Thu, 11 Jun 2015 04:05:46 -0700 (PDT) Received: from mail-la0-f52.google.com (mail-la0-f52.google.com. [209.85.215.52]) by mx.google.com with ESMTPS id ll10si299622lac.26.2015.06.11.04.05.46 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Jun 2015 04:05:46 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) client-ip=209.85.215.52; Received: by laew7 with SMTP id w7so2603984lae.1 for ; Thu, 11 Jun 2015 04:05:46 -0700 (PDT) X-Received: by 10.112.198.74 with SMTP id ja10mr9249481lbc.19.1434020746516; Thu, 11 Jun 2015 04:05:46 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.108.230 with SMTP id hn6csp3959989lbb; Thu, 11 Jun 2015 04:05:45 -0700 (PDT) X-Received: by 10.70.125.129 with SMTP id mq1mr14123520pdb.19.1434020744707; Thu, 11 Jun 2015 04:05:44 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id dh15si483473pac.23.2015.06.11.04.05.43; Thu, 11 Jun 2015 04:05:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754552AbbFKLFg (ORCPT + 28 others); Thu, 11 Jun 2015 07:05:36 -0400 Received: from mga11.intel.com ([192.55.52.93]:24448 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752823AbbFKLBq (ORCPT ); Thu, 11 Jun 2015 07:01:46 -0400 Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by fmsmga102.fm.intel.com with ESMTP; 11 Jun 2015 04:01:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.13,594,1427785200"; d="scan'208";a="741394236" Received: from feng-bdw-de.bj.intel.com ([10.238.154.75]) by fmsmga002.fm.intel.com with ESMTP; 11 Jun 2015 04:01:42 -0700 From: Feng Wu To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: pbonzini@redhat.com, mtosatti@redhat.com, alex.williamson@redhat.com, eric.auger@linaro.org, feng.wu@intel.com Subject: [v4 10/16] KVM: kvm-vfio: wrappers to VFIO external API device helpers Date: Thu, 11 Jun 2015 18:51:46 +0800 Message-Id: <1434019912-15423-11-git-send-email-feng.wu@intel.com> X-Mailer: git-send-email 2.1.0 In-Reply-To: <1434019912-15423-1-git-send-email-feng.wu@intel.com> References: <1434019912-15423-1-git-send-email-feng.wu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: feng.wu@intel.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.52 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Eric Auger Provide wrapper functions that allow KVM-VFIO device code to interact with a vfio device: - kvm_vfio_device_get_external_user gets a handle to a struct vfio_device from the vfio device file descriptor and increments its reference counter, - kvm_vfio_device_put_external_user decrements the reference counter to a vfio device, - kvm_vfio_external_base_device returns a handle to the struct device of the vfio device. Also kvm_vfio_get_vfio_device and kvm_vfio_put_vfio_device helpers are introduced. Signed-off-by: Eric Auger --- virt/kvm/vfio.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/virt/kvm/vfio.c b/virt/kvm/vfio.c index 620e37f..80a45e4 100644 --- a/virt/kvm/vfio.c +++ b/virt/kvm/vfio.c @@ -60,6 +60,80 @@ static void kvm_vfio_group_put_external_user(struct vfio_group *vfio_group) symbol_put(vfio_group_put_external_user); } +static struct vfio_device *kvm_vfio_device_get_external_user(struct file *filep) +{ + struct vfio_device *vdev; + struct vfio_device *(*fn)(struct file *); + + fn = symbol_get(vfio_device_get_external_user); + if (!fn) + return ERR_PTR(-EINVAL); + + vdev = fn(filep); + + symbol_put(vfio_device_get_external_user); + + return vdev; +} + +static void kvm_vfio_device_put_external_user(struct vfio_device *vdev) +{ + void (*fn)(struct vfio_device *); + + fn = symbol_get(vfio_device_put_external_user); + if (!fn) + return; + + fn(vdev); + + symbol_put(vfio_device_put_external_user); +} + +static struct device *kvm_vfio_external_base_device(struct vfio_device *vdev) +{ + struct device *(*fn)(struct vfio_device *); + struct device *dev; + + fn = symbol_get(vfio_external_base_device); + if (!fn) + return NULL; + + dev = fn(vdev); + + symbol_put(vfio_external_base_device); + + return dev; +} + +/** + * kvm_vfio_get_vfio_device - Returns a handle to a vfio-device + * + * Checks it is a valid vfio device and increments its reference counter + * @fd: file descriptor of the vfio platform device + */ +static struct vfio_device *kvm_vfio_get_vfio_device(int fd) +{ + struct fd f = fdget(fd); + struct vfio_device *vdev; + + if (!f.file) + return ERR_PTR(-EINVAL); + vdev = kvm_vfio_device_get_external_user(f.file); + fdput(f); + return vdev; +} + +/** + * kvm_vfio_put_vfio_device: decrements the reference counter of the + * vfio platform * device + * + * @vdev: vfio_device handle to release + */ +static void kvm_vfio_put_vfio_device(struct vfio_device *vdev) +{ + kvm_vfio_device_put_external_user(vdev); +} + static bool kvm_vfio_group_is_coherent(struct vfio_group *vfio_group) { long (*fn)(struct vfio_group *, unsigned long);