From patchwork Fri Aug 9 16:09:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13758944 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 D4B49C52D7C for ; Fri, 9 Aug 2024 16:10:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B8A376B00B9; Fri, 9 Aug 2024 12:10:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AEB186B00BA; Fri, 9 Aug 2024 12:10:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 93DC66B00BB; Fri, 9 Aug 2024 12:10:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6F7F26B00B9 for ; Fri, 9 Aug 2024 12:10:08 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2C0D1161242 for ; Fri, 9 Aug 2024 16:10:08 +0000 (UTC) X-FDA: 82433193696.11.6BC8EC8 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf13.hostedemail.com (Postfix) with ESMTP id F15922002E for ; Fri, 9 Aug 2024 16:10:05 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OzyMs3Hq; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723219772; a=rsa-sha256; cv=none; b=AOFSf9agwj5MoLghfhc9m2s28ZdawyMLq3DMqRHWRd47krrUwek+BnwbaJ+kD94ugkKb79 pMHWfe4KsOL6a1LejjzVqSVO/m/lV5XSRaMmcBHdoa3l/JgLZIkQUum7VNBQt+Z3SYlwVg foYW6Tda1LLqfyXSd4JvoPElItM+BzE= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=OzyMs3Hq; spf=pass (imf13.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723219772; 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=ZuxbhJTEmyvVitdF8dMwph69q8J8jFGoN74z1I241hpXcswSn0RUi05DChOaVMrP8eN8AX 6NUAYKxvb5np8PE27ZmloGXOYBV9Qz6Oeo6Tp3/px8ODumENTq2ex6+OxLDHpHpfqwnau2 IurbF5H7jDmm1drYDP58LwJ1t1Qgnds= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723219805; 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=OzyMs3HqYEtNjYS/7rcDP1LadYYk4UWvwse0/DbNl2Jk3onuCjvZ5N3ULszOvEOw2NjGl7 UoMOJ6pYQCMlU3UeO2LIldMuU+xT6nvrdo7PLTmXS2hqbRrIIXn+2n4afxJnHEXqEY6oio JGfHTAx9OAFYM5X7dtE/B06GT7kVuUE= Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-198-Hlxief1hPo6gZVkAOH9FWg-1; Fri, 09 Aug 2024 12:10:02 -0400 X-MC-Unique: Hlxief1hPo6gZVkAOH9FWg-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7a1e1828321so6887985a.1 for ; Fri, 09 Aug 2024 09:10:02 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723219797; x=1723824597; 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=KHsPbLh9/V/Ng230BErFuSgKsHG7+OH2Kju9d2g3xdED+6wuJBK1DLyOmN6yuaH3RI Dox+IZBPSGWaGB3En+kjvI8KfjXxPnzUptpCBELH+nJjhC743qTuYV310nETLvwMp6Py 8I7VbvbH1Mzc8S+gmRLsFLeeND6+fuPD6/OzkttDdyTCsjRVIGFt7dQ82kXkPg9sE8kf uMuYksuzkcjWaIF+Q7TedvOaU6/N3YeHU34DHzXoIYzPVzyA4+3OlnKd3vzGj53FT/VY ZUHhPiYxPIeAo7cFRCgTenhsAmOlOPgiqhDBL3gCYVHjTSOty/g23SoY1ftyJjT2iVbn LV2A== X-Gm-Message-State: AOJu0Yw2AlwD5b4s3ixVmA2M40je/mYRewG3ADyzInYZBP+1K/HuhqId 2fdEHKmxOyjCVQ4NpwiQQ2g9xR0M5fzuwnSRIUfvHVU6F9ktU1n4o70y6NlnQdnIncSKC3yY4zg pAbOLTfduSPEZUDy1hQgQtjslAmaYVRLnbYwQeWgU4qhQXsjKAFwQDzyYLhGhkiquUxlafzuhm3 bSasnxWCNYOXPtr00xJxxQZD0sKC/LjCO5 X-Received: by 2002:a05:622a:1b8d:b0:446:58f5:e6ff with SMTP id d75a77b69052e-4531251d52cmr13545211cf.2.1723219797424; Fri, 09 Aug 2024 09:09:57 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHJY3GNDsLRixrwL3Yd/1vgrL/iS2SbMHaD3fo6KrtERFLLt1PJ4ncszNtj/1mVMrjvOJRHSw== X-Received: by 2002:a05:622a:1b8d:b0:446:58f5:e6ff with SMTP id d75a77b69052e-4531251d52cmr13544641cf.2.1723219796830; Fri, 09 Aug 2024 09:09:56 -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 d75a77b69052e-451c870016csm22526741cf.19.2024.08.09.09.09.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Aug 2024 09:09:56 -0700 (PDT) From: Peter Xu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Sean Christopherson , Oscar Salvador , Jason Gunthorpe , Axel Rasmussen , linux-arm-kernel@lists.infradead.org, x86@kernel.org, peterx@redhat.com, Will Deacon , Gavin Shan , Paolo Bonzini , Zi Yan , Andrew Morton , Catalin Marinas , Ingo Molnar , Alistair Popple , Borislav Petkov , David Hildenbrand , Thomas Gleixner , kvm@vger.kernel.org, Dave Hansen , Alex Williamson , Yan Zhao Subject: [PATCH 19/19] vfio/pci: Implement huge_fault support Date: Fri, 9 Aug 2024 12:09:09 -0400 Message-ID: <20240809160909.1023470-20-peterx@redhat.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240809160909.1023470-1-peterx@redhat.com> References: <20240809160909.1023470-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Stat-Signature: 1ub5zdx4ywobbtjsyppt1wbefnw74x5c X-Rspamd-Queue-Id: F15922002E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723219805-21848 X-HE-Meta: U2FsdGVkX18l8ie4JyPP45xnnFlox2TmaBBBK0G2pU3OQ62LYBAFIZcLW9xItZG+DP7WL3FvDQ9AzqDwhTZAxImRwd1dHA0BPWXAT9ai1yi2hDthcorFmWPRwuNTMdLi8bwOH/TULBlmRG5ZarY/bzkGfL4+UKBgKX311Z9+fKI6bR727O4wI3Z/JGyldBR7KKeIwwf9IdO3g3GcDz367nMPihEHUWQt5UsZ8BmypKY02m6d7+0+WaFgEEDVMnTfNrl/lKhE2kxcC50OAzZO+Y08k8b5ZCd38MNPJe+O+tY3R4nA9+X98341hlRHBoYuiXyybdSEIO0pkkU4K7AeC6du+R48JfNIR+kx8izf4fE/F/SwdvuZUm6SIJqcSIvOJkBU8zZhDljVwzMBB8ziHCUPdns/cYSofUSWWxGrycYMubxj50MwSxFZcpY9CaJqlA3YfPWksvEpXytKRyYtpY7iEwER3b5Udjb83lDT8suNL5lYQEwoqmrGjYwUz1rRlci00jsci4E5OxbNogO7wZ7RxK9dkwRq+dsHHOjGe351D8revDERUBWDsscHr30uoVEzxzOAVp2xLmbO/cBoU5BFsCiMPGJqkA4SgK/Qcj1zRm6I/YdplbvzgmzfD5zrMwq2hvLT7lJwhZGNgQupfvGHvhYtv/3HmDaQB2cv9Uaa5p99HncAJlykMN2xpU6DfvCANFe2km+Hi6LXj7aI167oOHwvm1LkmC5IiSaOqczDVsIJUwVpSPqZ2KgZG7yl2I4o5N/boCZj7+sQV8ty2lhnL1iLOlz+T1KWH7EbTxKh7aZJkV8cNH2QMsV1ujCjtrAGYDFR77+DuFqHY05ebCfXYvTEccG4zdGM7OO73LSNetPgXUA+wPDoyOrQiEpOGypezabllahhKRHmGchVShyJyg0gTbV/OO41BUeNEae5TzWVLGWTgpTjvcn/Jm2fEY3nPXu4/It/i/PsQXM YMRJOH3n scRrh/10Ei6R5BUI7fAq8aVzCaUTgzlkEIsvRsk9RbTeYsrtinJxYNC3ItpiSjNiwq2C/dkXyLB+k9lm3Py4/kzL+T1F3eLmcpnpHd8diGtkajt1SjSMdEVNYBQmm10V7HNrn/hUiqnJxnMh6O9i0eqqMKfOfqQYtIuP3QSzjY8ZUijSJbA38fW9XQo/dDvEG7dJ0NR4ChPi0Trf1HQuTee4qw1JZomyW2HeHngzuQE42LWRZbCQVhmHa/4LMk1/5wW/yRA1WypNDmktDgw6gDNhhkIQo4XKPuQ9bbJfhfzv922kcDxSonkK1LtcUUpk9gifmI6qbwj/vtNpE8YIn0Wg7uHZnHtwJA90DdrIiGT1BwUi9leAIGYxw0oRfUlHHUScIGA8dr46hiD8X2HsLrfxRXZqz8zdakPjsT/kWQMwuOcp802sAaQ18ccuuYWqfOL3/eeM9EeVc+ZzFmczCFl6asrYi/BSLyUjBi75P3GAktfTGPAmabUU77JYKYo2k05StIDkYvFH0/gt7LbthWthvQ0KaZYAVWQxbik2X0QCxoHmJjklUoxOuLyiUIRXaUxwh 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)