From patchwork Mon Feb 5 12:01:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gowans, James" X-Patchwork-Id: 13545318 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 71869C4828D for ; Mon, 5 Feb 2024 12:03:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F23996B0092; Mon, 5 Feb 2024 07:03:15 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id ED1E16B0093; Mon, 5 Feb 2024 07:03:15 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D74346B0095; Mon, 5 Feb 2024 07:03:15 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C438E6B0092 for ; Mon, 5 Feb 2024 07:03:15 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A6E8C140A7F for ; Mon, 5 Feb 2024 12:03:15 +0000 (UTC) X-FDA: 81757614750.29.3451A5A Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by imf17.hostedemail.com (Postfix) with ESMTP id 269DB40009 for ; Mon, 5 Feb 2024 12:03:12 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=Bo62Hq4w; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf17.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707134593; 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=0/oASfY5H1JyciZlNLWYHif+Zbb5czst+5Ds70M97DI=; b=ck4AG3tC9Z9dcgudZn2OaftXdiDZ3a3xrPcSHWGMPnRW+CqaVZPWN0GyLMBm3yQFtpdhXZ /yA2bRYgvivOmVfOXOWcTz65seldsy7DWkRgmIpshZGtdBDWF71KqQQBAhWupTUAd45shX W3LY93agwCp/DjbelgtGHcpvknASRIQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=amazon.com header.s=amazon201209 header.b=Bo62Hq4w; dmarc=pass (policy=quarantine) header.from=amazon.com; spf=pass (imf17.hostedemail.com: domain of "prvs=75897cb1d=jgowans@amazon.com" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=75897cb1d=jgowans@amazon.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707134593; a=rsa-sha256; cv=none; b=dKtqya5SnJvmdisbBb7z9wpbvv4cmJmHy6ougp3snOs4B/PBlhpPGr1SiQmf+ISFdhOkcT MOBMB67Wee5xpZIgRxeOTjrkjC39A+cBFpJIOPO3dbQuVjyuDDp/FPit5L2cFt2ULlW46O 9DQpLyYe/ggN9dn9chqnbLjk0vWwCS0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1707134594; x=1738670594; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=0/oASfY5H1JyciZlNLWYHif+Zbb5czst+5Ds70M97DI=; b=Bo62Hq4wZkiJjgZ4E8Art4Q5jS9hXwFz/gPSBAwIZ/MeOo9pcaoKWuCH OxyO1VwyVw+B4tTI0M7vt3288m2mDKFdDtq56Wc3Eh3GaorD92eDgQ2vV BU8RTrl0enJ4PYciaw55awgS/kEHlLTXXPMfWqugIF6gTWf+ytZap3iuJ g=; X-IronPort-AV: E=Sophos;i="6.05,245,1701129600"; d="scan'208";a="632119672" 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-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Feb 2024 12:03:11 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.17.79:5859] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.31.207:2525] with esmtp (Farcaster) id 80b5446f-c47d-4e7f-ae7e-e8668aadf81b; Mon, 5 Feb 2024 12:03:10 +0000 (UTC) X-Farcaster-Flow-ID: 80b5446f-c47d-4e7f-ae7e-e8668aadf81b Received: from EX19D014EUC004.ant.amazon.com (10.252.51.182) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:03:10 +0000 Received: from dev-dsk-jgowans-1a-a3faec1f.eu-west-1.amazon.com (172.19.112.191) by EX19D014EUC004.ant.amazon.com (10.252.51.182) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1118.40; Mon, 5 Feb 2024 12:03:03 +0000 From: James Gowans To: CC: Eric Biederman , , "Joerg Roedel" , Will Deacon , , Alexander Viro , "Christian Brauner" , , Paolo Bonzini , Sean Christopherson , , Andrew Morton , , Alexander Graf , David Woodhouse , "Jan H . Schoenherr" , Usama Arif , Anthony Yznaga , Stanislav Kinsburskii , , , Subject: [RFC 05/18] pkernfs: add file mmap callback Date: Mon, 5 Feb 2024 12:01:50 +0000 Message-ID: <20240205120203.60312-6-jgowans@amazon.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240205120203.60312-1-jgowans@amazon.com> References: <20240205120203.60312-1-jgowans@amazon.com> MIME-Version: 1.0 X-Originating-IP: [172.19.112.191] X-ClientProxiedBy: EX19D044UWB004.ant.amazon.com (10.13.139.134) To EX19D014EUC004.ant.amazon.com (10.252.51.182) X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 269DB40009 X-Stat-Signature: ihpow9a569fbha94gnjpck7uaxrinoe7 X-Rspam-User: X-HE-Tag: 1707134592-85510 X-HE-Meta: U2FsdGVkX19/jMZoLiWK4v2gezb4NkgOvZAY8oKOnlEKauXwmBbiH/SADTma7rjsFyJhTTYKyuPtsOe6osfT/Q21mOKvVyUSqDkJu/63upC4lJIr7EIp5jGKU0iW5MRc38nuEr7257ugw0dwZhyYs05t6+FlPQ1vK4w905E4hxawbENUrkwBOjd9uu3k5zdwKDTctPid+x5o41UPW6SJm8ccuutNPIpCjTkobhwPfQvZzc4nCUkjRuHGDQsN0h4Gdg0YkqjXAwUzKhda2g6wmb5WoxfK1xKZGE5TvsCTYPRhmIwonvIEx94BiS2bhxJ8PeYGsrTcPSSSmCzw5JHognFuQVinXWGDVo3xgOfQY4HowtD9wtUP1rZUnhue88b2If9P+bIaJrAVeB2QVjTbErR9DVNxjudiVdXnCPPqJdOUDxk8yu82qiJ2Tdzec8y/rYyUbzw5pxCXLRgiix3VG45w0BvjWQk9HXenTy1xsD3Wci/ypAHSCKNQlsEGP9hdvWDkXBm14nD7OT5smEPiGDYZY7LIRLEv12NV0z7Tw9ezdoNjYwatNDvMvtvOYtuDKHjYlhybAiwKXJyINeevBlzYcTLB7f59vkX3fBoAW9ICd5gS8yIzIAIoWnExn3Sq0QrXk/gQY5EQzflZRxkGCZRA6Wu3odavDQ24qEflul1DFeNYMVFAMNlmL7P7I4quSU9OuCeb6oJHgKytn2PWQ2MA68vQNauZuk4HmCpedzU3GOpAg/7Vz5c6fz+qj18c40JZ50X74nNWuECia8wpie57p1hsZQs0DH/ev4UqOJ+7WOq7DFhDY9rLlg41BL/PcHSdNCKeIkSFKgk5sIj4dwFAoPaTI+86raYblFhiyxCKjdFbdfl7dKvDs3N3PvYt6Ix9w+QZrUdS/cDIyvO9zGVFhz3r7R/YOlF+enC5d11L4K/we72/YdqNYF90mIji7E7c1jTNe99WTa0FaiH smR0AQPp LRK0+fU2Rymo3oA9pUQTHG+Qd+ULL88tI5dB+nkSEzLDpllS4WNnZA0rwg74GKvCtT8ENEC+QM45JxO2YfFB+Ta6yxrMLHGfspISx7RUT8as1v8M7Wd0Icu4LGpIbK/ZDgC6s00ZkmzeInnVOStoKTiY5Ix9tT0PDYQY5vE7+/WlBU1ZB6Xo3bzEPFROQnbNUkHExCPfFWkDMkapyH2r5jpOEpQAAWlXA5esRRE0R3RgdGEnY75/ze44uCehLqT0KKrooh/v2TaUwffonmyCm6w53n9OjeWDmA5P6m5mEemZfS4PUNqLROiBen5eyEj6LO6BNioBa5RXS5qRzoYf9SClvcCSdwId2KC+3bJWtvKg1WoHO/jzXXGJvj6IC9GcE1h4C1XczMJjWY9aNaZ9M8+NcOqqHmRmMAzFX1zKZJWtqed/SA21L7w0FpahAKhjXXW8gRQi9e/NLHu1tp4mgIT9dy9M/mKM22PmXDSIA/l09yeKYuAqTUVMBy34thv7Ze3TCULCjmFY32cD1MOKcx9Z9BOX7dn6czncuVQKQJVKca48= 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 useable 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 useable when this virtual address range is given to VFIO's MAP_DMA. --- fs/pkernfs/file.c | 42 +++++++++++++++++++++++++++++++++++++++++- fs/pkernfs/pkernfs.c | 2 +- fs/pkernfs/pkernfs.h | 2 ++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/fs/pkernfs/file.c b/fs/pkernfs/file.c index 27a637423178..844b6cc63840 100644 --- a/fs/pkernfs/file.c +++ b/fs/pkernfs/file.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only #include "pkernfs.h" +#include static int truncate(struct inode *inode, loff_t newsize) { @@ -42,6 +43,45 @@ 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 pkernfs_inode *pkernfs_inode; + + pkernfs_inode = pkernfs_get_persisted_inode(filp->f_inode->i_sb, filp->f_inode->i_ino); + + mappings_block = (unsigned long *)pkernfs_addr_for_block(filp->f_inode->i_sb, + pkernfs_inode->mappings_block); + + /* 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; + + 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, + (pkernfs_base >> PAGE_SHIFT) + (mapped_block * 512), + PMD_SIZE, + vma->vm_page_prot); + } + return 0; +} + const struct inode_operations pkernfs_file_inode_operations = { .setattr = inode_setattr, .getattr = simple_getattr, @@ -49,5 +89,5 @@ const struct inode_operations pkernfs_file_inode_operations = { const struct file_operations pkernfs_file_fops = { .owner = THIS_MODULE, - .iterate_shared = NULL, + .mmap = mmap, }; diff --git a/fs/pkernfs/pkernfs.c b/fs/pkernfs/pkernfs.c index 199c2c648bca..f010c2d76c76 100644 --- a/fs/pkernfs/pkernfs.c +++ b/fs/pkernfs/pkernfs.c @@ -7,7 +7,7 @@ #include #include -static phys_addr_t pkernfs_base, pkernfs_size; +phys_addr_t pkernfs_base, pkernfs_size; void *pkernfs_mem; static const struct super_operations pkernfs_super_ops = { }; diff --git a/fs/pkernfs/pkernfs.h b/fs/pkernfs/pkernfs.h index 8b4fee8c5b2e..1a7aa783a9be 100644 --- a/fs/pkernfs/pkernfs.h +++ b/fs/pkernfs/pkernfs.h @@ -6,6 +6,8 @@ #define PKERNFS_FILENAME_LEN 255 extern void *pkernfs_mem; +/* Units of bytes */ +extern phys_addr_t pkernfs_base, pkernfs_size; struct pkernfs_sb { unsigned long magic_number;