Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2350465rdb; Mon, 5 Feb 2024 04:05:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJmHqW6tCQOUftPsYTQK7+vIqGBr4cc+4CbzrhfTEaEthZT0p0B1D8wA43jHZUjQEo7W48 X-Received: by 2002:aa7:d85a:0:b0:55e:fcfa:607b with SMTP id f26-20020aa7d85a000000b0055efcfa607bmr5637951eds.32.1707134722340; Mon, 05 Feb 2024 04:05:22 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707134722; cv=pass; d=google.com; s=arc-20160816; b=AANI5k/UQccWJj3N6lS3P/6FxgKHla3gMk/Sg/uARvwpMl56lCQJuFdphZWYhQpMt8 dyatKmdLgLdRcNWTjXkwA75BlSme3CgzCH4mAdNk9AlKP7PlTdlaVQhn9Rgx3Bwmnxw/ PlmuXTY9q+uMnr0qkuZop7SYfdZsEcVXy2FE1VTm2vGFd7gZAZUyQmc3bfpEFAkSBIJ/ 4t64pMeKw+ytQu16M2MdhO7vJFRHpmvUC4oZQ5MWUoU5l7HuGlOvhJe3i36w90/q7XIV CNlWHAMkNxnY6EiQFnWrcboTwZaxrQJw83n2W4BoHzXqDeG61DfHZFzhZrYkOba+Liqv 3d3Q== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=0/oASfY5H1JyciZlNLWYHif+Zbb5czst+5Ds70M97DI=; fh=XCkgnZL1S43jYiR/xvIPkpRhfVJj+8xsOF6/Ld/r3VY=; b=LOqjxQbO60ik8Cb7m3q0dFolBDzmX5DJ4CfaXYY/BOVgwMB+61YgJZLpO8TZKyUbVE Cbty7KVBO7Xfcr/0/aMqFUySDuHVvKHi5fqtZvMODXHhlpajb+zi30qSgYjtQssEqAOn DtjIZOhfFwndhvAaOiRSiDkcUIlH5eseTeTY8nR78ArS9CKhZ1xX5u6a+0lMORYS4DmL +Mu8RiUUJRXKttHuhnGkglyol4uXGaAIVSbnopByOtVFMf201x135lXz1V8Z2ZJfJv8h FPR1Dz9za3Mbff1y4ewEFd+Yth+T1nvgTmJe93tlRsM66dOnErGQNOouc6S/hEIYI+D/ 5RzQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=Bo62Hq4w; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-52547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52547-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com X-Forwarded-Encrypted: i=1; AJvYcCXnKq6DcZ+WNf2oVHxOC48m7MUOZp8wBtyhTzlzJ8a+7+rQT7TIkkRhenDlB+JKkcYTtxiaWxWPUocoQNkCnuA4aeb2TzPNJ3SmM1kRHQ== Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id eg13-20020a056402288d00b005588b35087asi3757949edb.307.2024.02.05.04.05.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 04:05:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=Bo62Hq4w; arc=pass (i=1 spf=pass spfdomain=amazon.com dkim=pass dkdomain=amazon.com dmarc=pass fromdomain=amazon.com); spf=pass (google.com: domain of linux-kernel+bounces-52547-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52547-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id DDCA01F22360 for ; Mon, 5 Feb 2024 12:05:21 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 71FE41BF5C; Mon, 5 Feb 2024 12:03:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b="Bo62Hq4w" Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4225F1BF47; Mon, 5 Feb 2024 12:03:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.119.213.156 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707134595; cv=none; b=ayQzVLTr9ZfuqcEx6ngTRe1vg8OYjIKDD+qM4xCtWVEPB5GGS4qlqvAuablf/8/keoNDCLoYcZtPit6GLvQttKAE7NU560vnhB6npThdG7YjORgPU1uTloiQZto2YsDBsgWdfu3+2zVTYjU5D6yLRzkKaQytItuLkSovjAyfm4I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707134595; c=relaxed/simple; bh=X0DVeAhkLd+qmZjH5rmDRSG3fWO+VNwd2pVbOIDFh9k=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=gvdoX61FDn4IdJ5Xkw3OKznvZiI6C34TzEMV9nvxsdv8p2dvnlGSih61vm9pZ4NAOFd33N+zuo/Z10QTIwfWhT6VUc7wg89H2XpcuhTnVil3xtTLsMg4GK4+o+AU37+wUXfwdpHr5cc4owyIZY0SHpNG7apMF5ayeNV+bDohor4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass (1024-bit key) header.d=amazon.com header.i=@amazon.com header.b=Bo62Hq4w; arc=none smtp.client-ip=52.119.213.156 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D044UWB004.ant.amazon.com (10.13.139.134) To EX19D014EUC004.ant.amazon.com (10.252.51.182) 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; -- 2.40.1