From patchwork Mon Aug 26 20:43:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13778413 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 2080BC5472C for ; Mon, 26 Aug 2024 20:44:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DD9D06B00AF; Mon, 26 Aug 2024 16:44:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D3B336B00B0; Mon, 26 Aug 2024 16:44:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B65C06B00B1; Mon, 26 Aug 2024 16:44:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8CC316B00AF for ; Mon, 26 Aug 2024 16:44:39 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0E907413CB for ; Mon, 26 Aug 2024 20:44:39 +0000 (UTC) X-FDA: 82495575078.15.03E67AF Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id EF4AB20019 for ; Mon, 26 Aug 2024 20:44:36 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FDIAs+ZA; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724705007; a=rsa-sha256; cv=none; b=nvaIGY9KSNqJH7olxLLv847cGAkh97/G063CWVt2ZJVN2Fro0kHoQVDwn6TThhGL8tB9w1 2XU9mrb5iaTQqexMRxnjZp6ZGc39wiJeMaZfNxxzEer3RKFWOqnS5T/GVthY6uLQ1w5Zyr 3Zz14aDjqcXIiM6Om6eFMmGks0diD9s= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=FDIAs+ZA; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724705007; 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=9MPOki3YF/wyEgBnejzItxwjIF1moGifGifGb5OwzY4=; b=VzykeHYWvlLjkpv729jdn14O+XhAKuOhECX8Cnz278scRoxXcBQd4EjiMAftUPYt2z4L7K D8bD5d5B8vQUORnAxDo/wmfopDje3N123gMLnLKGL1RgPSaGj2zqLpkGdsyrnCrLU28Ksy 7nktJ0YFel8WRkCQA+BOG68uLqtN4Ng= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1724705076; 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=9MPOki3YF/wyEgBnejzItxwjIF1moGifGifGb5OwzY4=; b=FDIAs+ZALbgen0k8cu0tRJgAA+EKXDxjQUttBBD+BpXE+wKtY3Qxb/eO5q1BAPGjAFnNSv mkY5io3dvWZf41lPLORMvb3ES8DbvLBnmbk6SLl/D5+qIgk3ghQkRZdjAsViHpgg5SbktJ mpRWwQPYXj0rZNG92At3Cw/CAWshGIw= Received: from mail-qk1-f199.google.com (mail-qk1-f199.google.com [209.85.222.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-563-8gTl8bUsN6CArjyc04SRFw-1; Mon, 26 Aug 2024 16:44:35 -0400 X-MC-Unique: 8gTl8bUsN6CArjyc04SRFw-1 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7a1dbbe6e4cso581918985a.3 for ; Mon, 26 Aug 2024 13:44:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724705074; x=1725309874; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9MPOki3YF/wyEgBnejzItxwjIF1moGifGifGb5OwzY4=; b=ldJXNRVBGHMkjP/3WT+qFsADHEvjze/KO84x56zu+ds85V7+R+59uOAvs5GqtDn0aq Epjzh2Q7SjJ2u0dSQpyvI6zavfj5cXqyESmfAlWJ8YttAnuFu/oIa8wwDmYoIhLfaT/z YShfGilDGdf4UfKDCBTOerl3D1voxpF9k3VkNqayG31RxeK6nS7+5Q8YHWRxDG8dsvvo jJN8vlt0Lt+C6O1Rw7SFHHVZXpAAiTTd5vl1o/mGJdMWmcfS6H89CIjBjRIQVtgeZWY7 NX1egHAHb3RKY8soBnIQ8jdav1OSYpbUV7lCOuTEdEVZkhVwDoud6QSvbKnHX5lzn4xU tGEQ== X-Forwarded-Encrypted: i=1; AJvYcCVrFPJsMKN4bgZHA+NHP0LaAVpGwF6ZE5DoXlqQapvwSIADInSt7N3MSMDtnPtepnnczPgNNUrntg==@kvack.org X-Gm-Message-State: AOJu0YzDFliqePPbdhGfF6Lg2g8cQkRJJ6Lm+WNSbFfPUbmIovMNK8MH YA5UZOChAboseFI70CVrVLBvoxcjgwTEHWo4iQ7DF0/TxYOQfzlZw9K8oPnjTJdkUg/qSNbTN+z 4yvWoWY0ERxtItaUJx7uaT818M7z0IzUOyUaqFnEEs1mCIBPhxtXsk7By X-Received: by 2002:a05:620a:4007:b0:79f:595:f64a with SMTP id af79cd13be357-7a6897b7b14mr1477183385a.58.1724705074688; Mon, 26 Aug 2024 13:44:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHpx2qE8XXNwFA/VbcshgEEhM7utf/nDzzwdfyOyUpm1p8Kz8GxiojqL+PSarI5vXlPZn99hQ== X-Received: by 2002:a05:620a:4007:b0:79f:595:f64a with SMTP id af79cd13be357-7a6897b7b14mr1477180385a.58.1724705074328; Mon, 26 Aug 2024 13:44:34 -0700 (PDT) Received: from x1n.redhat.com (pool-99-254-121-117.cpe.net.cable.rogers.com. [99.254.121.117]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7a67f3fd6c1sm491055185a.121.2024.08.26.13.44.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Aug 2024 13:44:33 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Gavin Shan , Catalin Marinas , x86@kernel.org, Ingo Molnar , Andrew Morton , Paolo Bonzini , Dave Hansen , Thomas Gleixner , Alistair Popple , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Sean Christopherson , peterx@redhat.com, Oscar Salvador , Jason Gunthorpe , Borislav Petkov , Zi Yan , Axel Rasmussen , David Hildenbrand , Yan Zhao , Will Deacon , Kefeng Wang , Alex Williamson Subject: [PATCH v2 19/19] vfio/pci: Implement huge_fault support Date: Mon, 26 Aug 2024 16:43:53 -0400 Message-ID: <20240826204353.2228736-20-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240826204353.2228736-1-peterx@redhat.com> References: <20240826204353.2228736-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: EF4AB20019 X-Stat-Signature: s6styomhng9bsoeeebe65kawky3k1af3 X-Rspam-User: X-HE-Tag: 1724705076-20634 X-HE-Meta: U2FsdGVkX1/1v4n4EHBQmirFfTZJqQm1h1uip83oUjuEmwhsSDZiBQhQSXdb4NDT6SUKOziVmiv1NVH4bMaRMO8FEdl+tkBrzL0dkNQCjEVVE9MNNRF2jHTRRwEZ5bljgoCFaaOKdntD7ac5ajnXQmAqCGwOecgOBVnFc0ILH3Bevt8oVftRf+CrukoYYIXPwYvx/zHQzLP20lIE742YZr2ChHW8DvsV2l/OkDqHGVaR8NRt+goBvzVCWaF4zw1aTuDSJopFnE+wop9ZG0ajLJHMARLruAOwXfPNUmBxW4GHaPhYfHhp7dxSMwoFVqZzpOBiek+5vyR8XdiyBSQbAa0QvotsZyworN25GjEkyyZvFJx/mx0JLcYyMAfwetSIDtvjp9EizytEZRxK0b/o7w0GPDucGNsLng+X0yO6ublXbW3Q79ldoToUx9TXDZylUUxp83IxY6q+6VZ2medZ1bQ+aV+oP3ZzI1iUUz0ISX+7MZw6ULMOcm0zFiOTqO1AoLu5UvseGxL+EGwXKF5ut81/QzyK+pgJuJ2LQ+Mqn6UFRu+db71wVi7KEdyU8wt8V0SWzKeXO/RjFtkUaqYvINGrXi41Afn3mqCCxGSBV1G9IWFATXmdCjSj3yHWdmJHn1j8kyoC5u4s596xGx0FvDCWTa3dZ/frYc+8y0PkkRYXCqlgVjfJ6tmw2ch1IV1/OB/J8Ztpe4E7LEaPM1P8tHCLMO/+ce/2v4lA3zizzBU6ThM4z2GeP2Jf9b5QupxoAaiF7WxIMI1mgzAeF6bMiy8mR+IJ/jfGD/3iOF9CHjp9u3GiwMSsOZ7jSasCthLWZCE3WfGVxofmS4K0xVK+FI63lHW4PVNTrIy46PJkPi7Cr2q9oUYnVP1wvTANfflx1hvmniFvYOArNzkTMg9Snff2dRMvMbfj5If/l7ZTI0LVe8ojfyJqz6+FvUiLcJ+xTIgh9nRAuhWNasBLF7J IqnqV5Hm MBcp8IPkE7MDqVnDcaiRCG/cM9qeznscM9uWQj6SVJOj6vuWnfF7GTZpNyW1iR+X/yyGFCoAdpik1FYc+6h2XL7X6pVOlzGU/+alypFxmr1glt6uXSZ11phHRsFNFUIzc+6ZsK6odIZ6/BOOpPQomRi2v/XWkRdVp6WWf75dTOkctZa1Usyy4O424I5so6Nd36+q8+ae7ctxhLeEz94HKRFPNeuGpZFRtv+m5/36qaoeVtpnr4jq3Y1SGMnwf7YEjkLI3WtukQ40/ZCHdQPJ7jA5OdwsFOzL3ygQYYaXFpKsFUP3YnzBxTtZkv0REB9Y1kQXgz4nswkkoha/+KbvFvbUPF26VZjPyHcYLUlUjbxQZGpvOsIObgkIsBE7wMMukVMac+kjKW5osXuQp/EwTV2h/PlwNy6ix+EJ9EvEGHMTpM/vI+7BoSMdYgpYywEJwfAKt 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: From: Alex Williamson With the addition of pfnmap support in vmf_insert_pfn_{pmd,pud}() we can take advantage of PMD and PUD faults to PCI BAR mmaps and create more efficient mappings. PCI BARs are always a power of two and will typically get at least PMD alignment without userspace even trying. Userspace alignment for PUD mappings is also not too difficult. Consolidate faults through a single handler with a new wrapper for standard single page faults. The pre-faulting behavior of commit d71a989cf5d9 ("vfio/pci: Insert full vma on mmap'd MMIO fault") is removed in this refactoring since huge_fault will cover the bulk of the faults and results in more efficient page table usage. We also want to avoid that pre-faulted single page mappings preempt huge page mappings. Cc: kvm@vger.kernel.org Signed-off-by: Alex Williamson Signed-off-by: Peter Xu --- drivers/vfio/pci/vfio_pci_core.c | 60 +++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_core.c index ba0ce0075b2f..2d7478e9a62d 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -1657,14 +1658,20 @@ static unsigned long vma_to_pfn(struct vm_area_struct *vma) return (pci_resource_start(vdev->pdev, index) >> PAGE_SHIFT) + pgoff; } -static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) +static vm_fault_t vfio_pci_mmap_huge_fault(struct vm_fault *vmf, + unsigned int order) { struct vm_area_struct *vma = vmf->vma; struct vfio_pci_core_device *vdev = vma->vm_private_data; unsigned long pfn, pgoff = vmf->pgoff - vma->vm_pgoff; - unsigned long addr = vma->vm_start; vm_fault_t ret = VM_FAULT_SIGBUS; + if (order && (vmf->address & ((PAGE_SIZE << order) - 1) || + vmf->address + (PAGE_SIZE << order) > vma->vm_end)) { + ret = VM_FAULT_FALLBACK; + goto out; + } + pfn = vma_to_pfn(vma); down_read(&vdev->memory_lock); @@ -1672,30 +1679,49 @@ static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) if (vdev->pm_runtime_engaged || !__vfio_pci_memory_enabled(vdev)) goto out_unlock; - ret = vmf_insert_pfn(vma, vmf->address, pfn + pgoff); - if (ret & VM_FAULT_ERROR) - goto out_unlock; - - /* - * Pre-fault the remainder of the vma, abort further insertions and - * supress error if fault is encountered during pre-fault. - */ - for (; addr < vma->vm_end; addr += PAGE_SIZE, pfn++) { - if (addr == vmf->address) - continue; - - if (vmf_insert_pfn(vma, addr, pfn) & VM_FAULT_ERROR) - break; + switch (order) { + case 0: + ret = vmf_insert_pfn(vma, vmf->address, pfn + pgoff); + break; +#ifdef CONFIG_ARCH_SUPPORTS_PMD_PFNMAP + case PMD_ORDER: + ret = vmf_insert_pfn_pmd(vmf, __pfn_to_pfn_t(pfn + pgoff, + PFN_DEV), false); + break; +#endif +#ifdef CONFIG_ARCH_SUPPORTS_PUD_PFNMAP + case PUD_ORDER: + ret = vmf_insert_pfn_pud(vmf, __pfn_to_pfn_t(pfn + pgoff, + PFN_DEV), false); + break; +#endif + default: + ret = VM_FAULT_FALLBACK; } out_unlock: up_read(&vdev->memory_lock); +out: + dev_dbg_ratelimited(&vdev->pdev->dev, + "%s(,order = %d) BAR %ld page offset 0x%lx: 0x%x\n", + __func__, order, + vma->vm_pgoff >> + (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT), + pgoff, (unsigned int)ret); return ret; } +static vm_fault_t vfio_pci_mmap_page_fault(struct vm_fault *vmf) +{ + return vfio_pci_mmap_huge_fault(vmf, 0); +} + static const struct vm_operations_struct vfio_pci_mmap_ops = { - .fault = vfio_pci_mmap_fault, + .fault = vfio_pci_mmap_page_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = vfio_pci_mmap_huge_fault, +#endif }; int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma)