From patchwork Wed Feb 11 08:32:17 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Auger X-Patchwork-Id: 5811111 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id C8D02BF440 for ; Wed, 11 Feb 2015 08:42:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0048F20218 for ; Wed, 11 Feb 2015 08:42:22 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1438A20211 for ; Wed, 11 Feb 2015 08:42:21 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1YLSpV-0006vM-H6; Wed, 11 Feb 2015 08:39:29 +0000 Received: from mail-ie0-f180.google.com ([209.85.223.180]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1YLSlq-0001qN-5S for linux-arm-kernel@lists.infradead.org; Wed, 11 Feb 2015 08:35:43 +0000 Received: by iecrp18 with SMTP id rp18so2350734iec.7 for ; Wed, 11 Feb 2015 00:35:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=d/8jJNZWLBQ3oETU2AwUMA0oxGEsIorL5zSB+t1Tb3A=; b=Khjt4qiI2zwtLnqwzgKdJMEiPmMMX/2nrV75yK1UMOKZZXOqcvizqijZCmCmThs4wS 3MlU+nTW7zwBfRuV6fW7sA+5Ws83Hxc+AGKq7St6ptZhMzhbFFe/GvQQORYF5OuhF85G QHNZ/Z6c26HrR9EVThpnyb3fSl1W7UXXov+hYTW+tH9H5U+RAg/uHWn5d85S0st50Kgi 9+UHnv2Rf/cwIHoFGfP6psoWyUwagFUM6xcSYNPWqXpX3Qx6OZ11g7duVfbRZ6DeJ/Je fVts2xDC/7IkWSxjvUotTexSLsviGCy9HJVewjLwvZUWBts1cfvseaeyWqkuhrYXi9q7 0seA== X-Gm-Message-State: ALoCoQnpBG6mCS+JCjNrgfGnqB+5EJIMywsYegHiDxTlAugLqWNiV4GmI9JZtRuEtECkmKnFTLX/ X-Received: by 10.50.79.230 with SMTP id m6mr2198546igx.33.1423643722153; Wed, 11 Feb 2015 00:35:22 -0800 (PST) Received: from gnx2579.solutionip.com ([113.28.134.59]) by mx.google.com with ESMTPSA id m38sm62189ioi.39.2015.02.11.00.35.17 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Wed, 11 Feb 2015 00:35:21 -0800 (PST) From: Eric Auger To: eric.auger@st.com, eric.auger@linaro.org, christoffer.dall@linaro.org, marc.zyngier@arm.com, linux-arm-kernel@lists.infradead.org, kvmarm@lists.cs.columbia.edu, kvm@vger.kernel.org, alex.williamson@redhat.com, feng.wu@intel.com Subject: [RFC v4 07/13] KVM: kvm-vfio: wrappers to VFIO external API device helpers Date: Wed, 11 Feb 2015 09:32:17 +0100 Message-Id: <1423643543-24409-8-git-send-email-eric.auger@linaro.org> X-Mailer: git-send-email 1.9.1 In-Reply-To: <1423643543-24409-1-git-send-email-eric.auger@linaro.org> References: <1423643543-24409-1-git-send-email-eric.auger@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150211_003542_302349_C7DB4BE5 X-CRM114-Status: GOOD ( 13.06 ) X-Spam-Score: -0.7 (/) Cc: b.reynal@virtualopensystems.com, patches@linaro.org, gleb@kernel.org, andre.przywara@arm.com, a.rigo@virtualopensystems.com, pbonzini@redhat.com, a.motakis@virtualopensystems.com X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- v3 -> v4: - wrappers are no more exposed in kvm_host and become kvm/vfio.c static functions - added kvm_vfio_get_vfio_device/kvm_vfio_put_vfio_device in that patch file v2 -> v3: - reword the commit message and title v1 -> v2: - kvm_vfio_external_get_base_device renamed into kvm_vfio_external_base_device - kvm_vfio_external_get_type removed --- 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);