Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp2085874pxb; Mon, 8 Mar 2021 13:51:46 -0800 (PST) X-Google-Smtp-Source: ABdhPJyB59u3J5j8j+UOZ0qwkn+NouDn4jwqET56qfq++mRQQKSud+t9KhmV11WpYcdip2vIhehS X-Received: by 2002:a17:906:874f:: with SMTP id hj15mr16851850ejb.404.1615240306473; Mon, 08 Mar 2021 13:51:46 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615240306; cv=none; d=google.com; s=arc-20160816; b=ZWsSY43Kr0nXiVZQ+yhRCGJYyG9MiC91/XRhTcJZiEW3GZbtM+9ZftqcDETRzfAWUo QRmilLq38X+tZqQ+eyJfBl6dqwavEA4Ixs86MnC+ZtrvAjMOBUBnYWWweYeYgEcBj/tU wNzbxZGKP27/8g++Ztl7DB6AMg9SolvJibgFL74uKMgyLxf7x7AB4Zw55rdsuAB9jLOX RBtM477axWn2uocHi01xDtjnXp41pe2XofA9THLbZBjkXdG19Va0U85Pkqi7N2zYF5+X P/PRY93pGQARZz4eJQ+nc/ciUMJ6n+RhJnRD0CrCiyKhp+JQS7W7xX0I7BoAL4Je4EF6 OuMg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:cc:to:from :subject:dkim-signature; bh=mnL7xLU8VAUfu1s0cNPH4G0MS7CPcOoMJVc60snGgA0=; b=c5EtDAV09G99pV2LjnywnPZd40SDoOYYAE/QkES6XaAWDYvGIwnW1a7/184CnUJvxs QXl0DkxlZMOixSl7cb3knhtuEPjmlLnYgLWNmBN1O7v/EETi8mkU6DGyTQ4nugY7fC/r oG2GP97n6/AsdqhxYhthIdi2kwzdUQO10KTOUVErVp0+sEijOa2wGporqgkbuvrpWjda IuZVYeoVlo70JZJbfMLwAXnIl8Pd23VS+PbY1+vfC6y0KPj1/lZaqP3xgUENDP66hmrS wiq0BbW/Hdt7lNeK9M4vyofbMWZKB1TW2r6Jy4IoZCQA7ThnbExobGetrS+JNSxksBkO RQ0w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aAbbJsjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m3si3792695ejl.510.2021.03.08.13.51.24; Mon, 08 Mar 2021 13:51:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aAbbJsjS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231542AbhCHVss (ORCPT + 99 others); Mon, 8 Mar 2021 16:48:48 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:53062 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231812AbhCHVsa (ORCPT ); Mon, 8 Mar 2021 16:48:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615240109; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mnL7xLU8VAUfu1s0cNPH4G0MS7CPcOoMJVc60snGgA0=; b=aAbbJsjSdBU5Ma+oFqM+OuxhUd6W7JK/B/IQPZ1MMeBDnypk+40O3qxQKsHTyNkGU1f+VH 5tsxX69tFq0ZkKocD5yawPTN1qmI8zZ7BjT48h2N+ffMfrUnBlX/hHR/k01d5MDT9ji4Lp qKJQYZi+bFER0SSQJGNjqrcYYHXs+lw= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-357-I2zFBCQvPoKp9ZvePbcsRg-1; Mon, 08 Mar 2021 16:48:26 -0500 X-MC-Unique: I2zFBCQvPoKp9ZvePbcsRg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E2F251084D68; Mon, 8 Mar 2021 21:48:24 +0000 (UTC) Received: from gimli.home (ovpn-112-255.phx2.redhat.com [10.3.112.255]) by smtp.corp.redhat.com (Postfix) with ESMTP id D09ED3A47; Mon, 8 Mar 2021 21:48:16 +0000 (UTC) Subject: [PATCH v1 06/14] vfio: Add vma to pfn callback From: Alex Williamson To: alex.williamson@redhat.com Cc: cohuck@redhat.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, jgg@nvidia.com, peterx@redhat.com Date: Mon, 08 Mar 2021 14:48:16 -0700 Message-ID: <161524009646.3480.6519905534709638083.stgit@gimli.home> In-Reply-To: <161523878883.3480.12103845207889888280.stgit@gimli.home> References: <161523878883.3480.12103845207889888280.stgit@gimli.home> User-Agent: StGit/0.21-2-g8ef5 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a new vfio_device_ops callback to allow the bus driver to translate a vma mapping of a vfio device fd to a pfn. Plumb through vfio-core. Implemented for vfio-pci. Suggested-by: Jason Gunthorpe Signed-off-by: Alex Williamson --- drivers/vfio/pci/vfio_pci.c | 1 + drivers/vfio/vfio.c | 16 ++++++++++++++++ include/linux/vfio.h | 3 +++ 3 files changed, 20 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 415b5109da9b..585895970e9c 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -1756,6 +1756,7 @@ static const struct vfio_device_ops vfio_pci_ops = { .mmap = vfio_pci_mmap, .request = vfio_pci_request, .match = vfio_pci_match, + .vma_to_pfn = vfio_pci_bar_vma_to_pfn, }; static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev); diff --git a/drivers/vfio/vfio.c b/drivers/vfio/vfio.c index 3a3e85a0dc3e..c47895539a1a 100644 --- a/drivers/vfio/vfio.c +++ b/drivers/vfio/vfio.c @@ -944,6 +944,22 @@ struct vfio_device *vfio_device_get_from_vma(struct vm_area_struct *vma) } EXPORT_SYMBOL_GPL(vfio_device_get_from_vma); +int vfio_vma_to_pfn(struct vm_area_struct *vma, unsigned long *pfn) +{ + struct vfio_device *device; + + if (!vma->vm_file || vma->vm_file->f_op != &vfio_device_fops) + return -EINVAL; + + device = vma->vm_file->private_data; + + if (unlikely(!device->ops->vma_to_pfn)) + return -EINVAL; + + return device->ops->vma_to_pfn(vma, pfn); +} +EXPORT_SYMBOL_GPL(vfio_vma_to_pfn); + static struct vfio_device *vfio_device_get_from_name(struct vfio_group *group, char *buf) { diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 660b8adf90a6..dbd90d0ba713 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -29,6 +29,7 @@ * @match: Optional device name match callback (return: 0 for no-match, >0 for * match, -errno for abort (ex. match with insufficient or incorrect * additional args) + * @vma_to_pfn: Optional pfn from vma lookup against vma mapping device fd */ struct vfio_device_ops { char *name; @@ -43,6 +44,7 @@ struct vfio_device_ops { int (*mmap)(void *device_data, struct vm_area_struct *vma); void (*request)(void *device_data, unsigned int count); int (*match)(void *device_data, char *buf); + int (*vma_to_pfn)(struct vm_area_struct *vma, unsigned long *pfn); }; extern struct iommu_group *vfio_iommu_group_get(struct device *dev); @@ -59,6 +61,7 @@ extern void *vfio_device_data(struct vfio_device *device); extern void vfio_device_unmap_mapping_range(struct vfio_device *device, loff_t start, loff_t len); extern struct vfio_device *vfio_device_get_from_vma(struct vm_area_struct *vma); +extern int vfio_vma_to_pfn(struct vm_area_struct *vma, unsigned long *pfn); /* events for the backend driver notify callback */ enum vfio_iommu_notify_type {