From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEFE8C4167D for ; Fri, 28 Jan 2022 00:26:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ED86F6B007E; Thu, 27 Jan 2022 19:26:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D1A556B007D; Thu, 27 Jan 2022 19:26:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A85F66B007E; Thu, 27 Jan 2022 19:26:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 77A296B0073 for ; Thu, 27 Jan 2022 19:26:30 -0500 (EST) Received: from smtpin10.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 1771E95AE5 for ; Fri, 28 Jan 2022 00:26:30 +0000 (UTC) X-FDA: 79077804540.10.F77FD9D Received: from ale.deltatee.com (ale.deltatee.com [204.191.154.188]) by imf12.hostedemail.com (Postfix) with ESMTP id C4CDA40003 for ; Fri, 28 Jan 2022 00:26:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=deltatee.com; s=20200525; h=Subject:MIME-Version:References:In-Reply-To: Message-Id:Date:Cc:To:From:content-disposition; bh=64vXVneAT9A14nJ5Wy4DU2Ru5X/AwQFCZoUVhV2cz8s=; b=ZnQOa3fNCsoceXpkfTOvkkxhQ3 YPnv8R6b2Pia3DQGSm/S40Iw4dBsQ5v3Ro2prXcn89tVI0MzOTjtMLCPtkjEysFf36McWDRAuOuxo ZIg7pNMDR3disto8RqevSoa5xVhahTIzavvy5qG++ccgQhBj4GG4J2IwOZXx3/P9xrXDeih/JvVcK uPgAqYGjZgMaZG9WePCJZWo7eMidSu1Yain3cpxonh4FHpfclfmfJsj2uv/5fCZNs0tMHmuR5Q0Db EF3FYDhp3FzdmOhXgut3tml0gqzsP6+8/6TBEGj1FP8uQakf6HyYBWeoW1hFJSf3CO5UxpfraTn2c 3fUhej/g==; Received: from cgy1-donard.priv.deltatee.com ([172.16.1.31]) by ale.deltatee.com with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nDF5q-005Oca-PY; Thu, 27 Jan 2022 17:26:23 -0700 Received: from gunthorp by cgy1-donard.priv.deltatee.com with local (Exim 4.94.2) (envelope-from ) id 1nDF5q-0001db-As; Thu, 27 Jan 2022 17:26:22 -0700 From: Logan Gunthorpe To: linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Stephen Bates , Christoph Hellwig , Dan Williams , Jason Gunthorpe , =?UTF-8?q?Christian=20K=C3=B6nig?= , John Hubbard , Don Dutile , Matthew Wilcox , Daniel Vetter , Jakowski Andrzej , Minturn Dave B , Jason Ekstrand , Dave Hansen , Xiong Jianxin , Bjorn Helgaas , Ira Weiny , Robin Murphy , Martin Oliveira , Chaitanya Kulkarni , Ralph Campbell , Logan Gunthorpe Date: Thu, 27 Jan 2022 17:26:14 -0700 Message-Id: <20220128002614.6136-25-logang@deltatee.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220128002614.6136-1-logang@deltatee.com> References: <20220128002614.6136-1-logang@deltatee.com> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 172.16.1.31 X-SA-Exim-Rcpt-To: linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org, linux-block@vger.kernel.org, linux-pci@vger.kernel.org, linux-mm@kvack.org, iommu@lists.linux-foundation.org, sbates@raithlin.com, hch@lst.de, jgg@ziepe.ca, christian.koenig@amd.com, ddutile@redhat.com, willy@infradead.org, daniel.vetter@ffwll.ch, jason@jlekstrand.net, dave.hansen@linux.intel.com, helgaas@kernel.org, dan.j.williams@intel.com, andrzej.jakowski@intel.com, dave.b.minturn@intel.com, jianxin.xiong@intel.com, ira.weiny@intel.com, robin.murphy@arm.com, martin.oliveira@eideticom.com, ckulkarnilinux@gmail.com, jhubbard@nvidia.com, rcampbell@nvidia.com, logang@deltatee.com X-SA-Exim-Mail-From: gunthorp@deltatee.com Subject: [PATCH v5 24/24] nvme-pci: allow mmaping the CMB in userspace X-SA-Exim-Version: 4.2.1 (built Sat, 13 Feb 2021 17:57:42 +0000) X-SA-Exim-Scanned: Yes (on ale.deltatee.com) X-Rspamd-Queue-Id: C4CDA40003 X-Rspam-User: nil Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=deltatee.com header.s=20200525 header.b=ZnQOa3fN; dmarc=pass (policy=none) header.from=deltatee.com; spf=pass (imf12.hostedemail.com: domain of gunthorp@deltatee.com designates 204.191.154.188 as permitted sender) smtp.mailfrom=gunthorp@deltatee.com X-Stat-Signature: t6d4ggp8bexdw95yre4imrn5fsfttxuz X-Rspamd-Server: rspam08 X-HE-Tag: 1643329589-917119 Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Allow userspace to obtain CMB memory by mmaping the controller's char device. The mmap call allocates and returns a hunk of CMB memory, (the offset is ignored) so userspace does not have control over the address within the CMB. A VMA allocated in this way will only be usable by drivers that set FOLL_PCI_P2PDMA when calling GUP. And inter-device support will be checked the first time the pages are mapped for DMA. Currently this is only supported by O_DIRECT to an PCI NVMe device or through the NVMe passthrough IOCTL. Signed-off-by: Logan Gunthorpe --- drivers/nvme/host/core.c | 15 +++++++++++++++ drivers/nvme/host/nvme.h | 2 ++ drivers/nvme/host/pci.c | 17 +++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index ecb01984b55d..54a1c350098e 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -3142,6 +3142,10 @@ static int nvme_dev_open(struct inode *inode, stru= ct file *file) } =20 file->private_data =3D ctrl; + + if (ctrl->ops->cdev_file_open) + ctrl->ops->cdev_file_open(ctrl, file); + return 0; } =20 @@ -3155,12 +3159,23 @@ static int nvme_dev_release(struct inode *inode, = struct file *file) return 0; } =20 +static int nvme_dev_mmap(struct file *file, struct vm_area_struct *vma) +{ + struct nvme_ctrl *ctrl =3D file->private_data; + + if (!ctrl->ops->mmap_cmb) + return -ENODEV; + + return ctrl->ops->mmap_cmb(ctrl, vma); +} + static const struct file_operations nvme_dev_fops =3D { .owner =3D THIS_MODULE, .open =3D nvme_dev_open, .release =3D nvme_dev_release, .unlocked_ioctl =3D nvme_dev_ioctl, .compat_ioctl =3D compat_ptr_ioctl, + .mmap =3D nvme_dev_mmap, }; =20 static ssize_t nvme_sysfs_reset(struct device *dev, diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index 97efc6d0b146..6b1649d50bdf 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -494,6 +494,8 @@ struct nvme_ctrl_ops { void (*delete_ctrl)(struct nvme_ctrl *ctrl); int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size); bool (*supports_pci_p2pdma)(struct nvme_ctrl *ctrl); + void (*cdev_file_open)(struct nvme_ctrl *ctrl, struct file *file); + int (*mmap_cmb)(struct nvme_ctrl *ctrl, struct vm_area_struct *vma); }; =20 /* diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index 330515886dfc..47e47ffc12bf 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -2961,6 +2961,21 @@ static bool nvme_pci_supports_pci_p2pdma(struct nv= me_ctrl *ctrl) return dma_pci_p2pdma_supported(dev->dev); } =20 +static void nvme_pci_cdev_file_open(struct nvme_ctrl *ctrl, struct file = *file) +{ + struct pci_dev *pdev =3D to_pci_dev(to_nvme_dev(ctrl)->dev); + + pci_p2pdma_file_open(pdev, file); +} + +static int nvme_pci_mmap_cmb(struct nvme_ctrl *ctrl, + struct vm_area_struct *vma) +{ + struct pci_dev *pdev =3D to_pci_dev(to_nvme_dev(ctrl)->dev); + + return pci_mmap_p2pmem(pdev, vma); +} + static const struct nvme_ctrl_ops nvme_pci_ctrl_ops =3D { .name =3D "pcie", .module =3D THIS_MODULE, @@ -2972,6 +2987,8 @@ static const struct nvme_ctrl_ops nvme_pci_ctrl_ops= =3D { .submit_async_event =3D nvme_pci_submit_async_event, .get_address =3D nvme_pci_get_address, .supports_pci_p2pdma =3D nvme_pci_supports_pci_p2pdma, + .cdev_file_open =3D nvme_pci_cdev_file_open, + .mmap_cmb =3D nvme_pci_mmap_cmb, }; =20 static int nvme_dev_map(struct nvme_dev *dev) --=20 2.30.2