From patchwork Mon Aug 5 09:32:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13753331 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 6CA17C3DA7F for ; Mon, 5 Aug 2024 09:34:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A58B6B0095; Mon, 5 Aug 2024 05:34:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 92D5C6B0096; Mon, 5 Aug 2024 05:34:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7815B6B0098; Mon, 5 Aug 2024 05:34:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 5C4E16B0095 for ; Mon, 5 Aug 2024 05:34:33 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 05556A8AF8 for ; Mon, 5 Aug 2024 09:34:33 +0000 (UTC) X-FDA: 82417681626.25.41B07FF Received: from smtp-fw-52002.amazon.com (smtp-fw-52002.amazon.com [52.119.213.150]) by imf26.hostedemail.com (Postfix) with ESMTP id EE24214000D for ; Mon, 5 Aug 2024 09:34:30 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=TKnw5uIk; spf=pass (imf26.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722850440; h=from:from:sender: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:dkim-signature; bh=QLzX8kQ4Me7VkWkGGUOEVumIPTgCaBccV0Vp9jQZvU0=; b=5wB+/wr7M6hFDd2GF86t7QauSWxGvUBbn9hrumWWO4v3XzEe+rykTZc6K/s67WGl3+HofB MGZUbAnmmcI5yR6JQP/egvHCM9sQe9Q4/QEdJRWpkpfPr0Dgyzx0yhGFWnMlcMxbbMBnSK h5W4bDxpc85QEaU2MFmwbLfyrw1+FGc= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=TKnw5uIk; spf=pass (imf26.hostedemail.com: domain of "prvs=940e15008=jgowans@amazon.com" designates 52.119.213.150 as permitted sender) smtp.mailfrom="prvs=940e15008=jgowans@amazon.com"; dmarc=pass (policy=quarantine) header.from=amazon.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722850440; a=rsa-sha256; cv=none; b=Z51LudRc2uCzzFcQmpz/UXQ4yU3GmFHawmKICb8C/RCHLgzaX3l9Tov8sWdaHcV64kl7q8 BudX5rHno96v1YN5xS3j1YOONilpQXAYJNiLCCOaeYQxkD8wByupZbU1JcA3o3KKuOS5o/ owqcOf84x5dxXF+WNN+EkyVNyaJGjLg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1722850471; x=1754386471; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=QLzX8kQ4Me7VkWkGGUOEVumIPTgCaBccV0Vp9jQZvU0=; b=TKnw5uIkWkfndoeMCMsx5vw6hULdccDEnvHuP0BOnvIEVFcjdZySe+hD PXjDoQ/MhHZuPzGGfG1LZx5GaZllA4FXABFsNdDaYFyhYVLzjGOWYTj+k 8OC/XrAWvYpAhgis8PccOnrl2txeb4C5y0FMnqsQdL48f5K0V+1EkJi/a c=; X-IronPort-AV: E=Sophos;i="6.09,264,1716249600"; d="scan'208";a="650673665" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-east-1.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52002.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Aug 2024 09:34:29 +0000 Received: from EX19MTAEUB002.ant.amazon.com [10.0.17.79:60629] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.35.73:2525] with esmtp (Farcaster) id d3e48c8d-1f8d-494a-8e05-dd641a46ffbf; Mon, 5 Aug 2024 09:34:28 +0000 (UTC) X-Farcaster-Flow-ID: d3e48c8d-1f8d-494a-8e05-dd641a46ffbf Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:34:27 +0000 Received: from u5d18b891348c5b.ant.amazon.com (10.146.13.113) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Mon, 5 Aug 2024 09:34:18 +0000 From: James Gowans To: CC: James Gowans , Sean Christopherson , Paolo Bonzini , Alexander Viro , Steve Sistare , Christian Brauner , Jan Kara , "Anthony Yznaga" , Mike Rapoport , "Andrew Morton" , , Jason Gunthorpe , , Usama Arif , , Alexander Graf , David Woodhouse , Paul Durrant , Nicolas Saenz Julienne Subject: [PATCH 05/10] guestmemfs: add file mmap callback Date: Mon, 5 Aug 2024 11:32:40 +0200 Message-ID: <20240805093245.889357-6-jgowans@amazon.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240805093245.889357-1-jgowans@amazon.com> References: <20240805093245.889357-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [10.146.13.113] X-ClientProxiedBy: EX19D044UWB004.ant.amazon.com (10.13.139.134) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: EE24214000D X-Stat-Signature: r9cbzraz8ycy6ci935z7pq3rp9a3etkk X-HE-Tag: 1722850470-95087 X-HE-Meta: U2FsdGVkX185wP6oU4k9bBadHsrKtP/BLDFsy8oaocQb7lZpJ/UUDunwpGbr+QrL8T/XGPRpbrA32dCV+6Q+JwdFXcNwg4pGwxA+1ZHgbbCs/7fIFrLTPRALiLdpWUJOllkjROXh/ygrQS5vmJHWL3OoZu/CJksxOQ9Tp3/Opm+LRU35i/xxmeKzpq8Y/pVefHh5IohSYjxMOSPbbJupv6DP1HgudZvkYs/x0EBsdNGTfCwRTXtJH75EfD46JiseNxAzQImVWBPQipBfObYfZRR0OfRbu5isQhIHzWSHhe+aNzf7u5DGWhcl05ehgJEllohq/pXXb3fabQ7zyMKoLDevLLiwLZmZgLWCNeMzayvg7gN7gSkmG4wpfKMSdt1IVjWNdp9KmKU94XMkbTQAWJ7wsgZCa8f/inHH6zW+7a7+My1zkrM3vi0XAFgZH3GixQcPnbRdapj2OKxrKGJpWmu0JXJHMcYL0ZR0pUFiwoYhdyHK7fi7/qIx5OZH1uzsSDqyolHb99aG2kh3vn8IPbN0HB5U6zBLqO0D8VCEPhDvrQk6Mdgxp+iCPs89v5wUD7PkG2XHxAG98r/ZZMFpjbnqD7su82VfOwk4Kkafp9HkERLTmfGyMEuwCxvZ0hlgVe080VREamFc4UenRIymh/xo5zY/WgZU4zott4vCnLZie7QD698NRsYjNTHqPpuq4Lqz/ySufzRNnNETbkQ1puwpOo229qQ21w6HmzBGhvFES3YZ2Kt5Fz0ny0yiHYMa21vps4SwnsQal3MV/zXFWp6I3gkJC97dI69cuLEZru9PpmFoDFytJCjWBqIuDASsImAQtwb2UmQ6f3ryRT2eM9O8Vch5MuFGwSttAj4RZVWq9w7VDOG+Hq2JmHWMCY4cyGrsNwyOj9BpTDltVpzNDY5eAZhc5X74wUVQnpHIpGmT/jp+T7KtPK7uIlX4ccyiaXfEKBcpCeH9gQRRVx2 2Fwme9aA tiZMKEXTX82tbp33rMuF5zxJhtLBTZZBE629CJX6KjrlmRFUdWZwO1WDHELt11cA17Aqh31OZgAK1f98gBfhenDILdr98eN43jHfJJWt/+Du319yblvgfNk2zsCyKWXjE6LpkjbXve03pHSamORYCX+XjV0lGxWqYQb/QOEiirWsXr4nE1QLnoobE//wfrx/sUwRhU3pEt9VB5yhAOUO/BJEYo6DLGCVpEAP8QYBKkcksXw0WGjwtvPb9L56J3BMDFff0WGrJuwuSPG36Oeo+3s4wVFzLJ6OkYoMqB7op1K2RxAVdy8CgRYrU0QGOUIxbDSEASFWcETE5X3D9DuhK7Fjv6raLjtq3zvIpMikBmRwd0ah+3Z2yfbNsIbu/bnobGP+P0dWFabbx3avjZS9hq+oiwh1YR9hn4WSjdYRzzQvFdAuB0g7NYd/BWd2YRt3+WxJVfz47V+aglB756sXwohpN/Vk2USNNKWKD8ZNO1D64WzYDEfANwp53CcSklYWn9X5rDaj69233fFbY/e/1pNJp+9RFDE6HLeUnEd7QxF6/7MidgEIvHFUBznzoMJBXZ1xf8rOyiiUs/SE3/kbgV8eZLMux9uDhzz+9OxxrNaRo5q8= 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: List-Subscribe: List-Unsubscribe: Make the file data usable to userspace by adding mmap. That's all that QEMU needs for guest RAM, so that's all be bother implementing for now. When mmaping the file the VMA is marked as PFNMAP to indicate that there are no struct pages for the memory in this VMA. Remap_pfn_range() is used to actually populate the page tables. All PTEs are pre-faulted into the pgtables at mmap time so that the pgtables are usable when this virtual address range is given to VFIO's MAP_DMA. Signed-off-by: James Gowans --- fs/guestmemfs/file.c | 43 +++++++++++++++++++++++++++++++++++++- fs/guestmemfs/guestmemfs.c | 2 +- fs/guestmemfs/guestmemfs.h | 3 +++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/fs/guestmemfs/file.c b/fs/guestmemfs/file.c index 618c93b12196..b1a52abcde65 100644 --- a/fs/guestmemfs/file.c +++ b/fs/guestmemfs/file.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only #include "guestmemfs.h" +#include static int truncate(struct inode *inode, loff_t newsize) { @@ -41,6 +42,46 @@ static int inode_setattr(struct mnt_idmap *idmap, struct dentry *dentry, struct return 0; } +/* + * To be able to use PFNMAP VMAs for VFIO DMA mapping we need the page tables + * populated with mappings. Pre-fault everything. + */ +static int mmap(struct file *filp, struct vm_area_struct *vma) +{ + int rc; + unsigned long *mappings_block; + struct guestmemfs_inode *guestmemfs_inode; + + guestmemfs_inode = guestmemfs_get_persisted_inode(filp->f_inode->i_sb, + filp->f_inode->i_ino); + + mappings_block = guestmemfs_inode->mappings; + + /* Remap-pfn-range will mark the range VM_IO */ + for (unsigned long vma_addr_offset = vma->vm_start; + vma_addr_offset < vma->vm_end; + vma_addr_offset += PMD_SIZE) { + int block, mapped_block; + unsigned long map_size = min(PMD_SIZE, vma->vm_end - vma_addr_offset); + + block = (vma_addr_offset - vma->vm_start) / PMD_SIZE; + mapped_block = *(mappings_block + block); + /* + * It's wrong to use rempa_pfn_range; this will install PTE-level entries. + * The whole point of 2 MiB allocs is to improve TLB perf! + * We should use something like mm/huge_memory.c#insert_pfn_pmd + * but that is currently static. + * TODO: figure out the best way to install PMDs. + */ + rc = remap_pfn_range(vma, + vma_addr_offset, + (guestmemfs_base >> PAGE_SHIFT) + (mapped_block * 512), + map_size, + vma->vm_page_prot); + } + return 0; +} + const struct inode_operations guestmemfs_file_inode_operations = { .setattr = inode_setattr, .getattr = simple_getattr, @@ -48,5 +89,5 @@ const struct inode_operations guestmemfs_file_inode_operations = { const struct file_operations guestmemfs_file_fops = { .owner = THIS_MODULE, - .iterate_shared = NULL, + .mmap = mmap, }; diff --git a/fs/guestmemfs/guestmemfs.c b/fs/guestmemfs/guestmemfs.c index c45c796c497a..38f20ad25286 100644 --- a/fs/guestmemfs/guestmemfs.c +++ b/fs/guestmemfs/guestmemfs.c @@ -9,7 +9,7 @@ #include #include -static phys_addr_t guestmemfs_base, guestmemfs_size; +phys_addr_t guestmemfs_base, guestmemfs_size; struct guestmemfs_sb *psb; static int statfs(struct dentry *root, struct kstatfs *buf) diff --git a/fs/guestmemfs/guestmemfs.h b/fs/guestmemfs/guestmemfs.h index 7ea03ac8ecca..0f2788ce740e 100644 --- a/fs/guestmemfs/guestmemfs.h +++ b/fs/guestmemfs/guestmemfs.h @@ -8,6 +8,9 @@ #define GUESTMEMFS_FILENAME_LEN 255 #define GUESTMEMFS_PSB(sb) ((struct guestmemfs_sb *)sb->s_fs_info) +/* Units of bytes */ +extern phys_addr_t guestmemfs_base, guestmemfs_size; + struct guestmemfs_sb { /* Inode number */ unsigned long next_free_ino;