From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=35.155.198.111 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795526172; b=nxOuI7Zze+Wwcl1MscpelV0jI1RKPM63VWg0xRNvPn+Lb7Gxwstlcf7vDWyozwOJe0jz jNltwAlIXXJQnLmbvzqPgAbQwsYkJH+lv685FUZNASky0RO/svvC3rEDu8WZIgckibsVP UBydrL0dvG3LhEqbbLXiyDyxzqn2Hs5CQPXh9lkJ4apQ+7onEF5+M36nK4qkpoxJNiOdR lioKoSPKSGHzBCvLuGGOoNmw3FJfmD7OvCcQydb4Mq3l3EgUnOwAHJKGWyALWXNgQHuNC Kx6o4sd1QI+70bAtoLMN0NtDGJ1+0eMY8t6olonSaryT8Vs4JEeNR6jIiAV5HsFyikQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795526172; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:Received:Received:X-Farcaster-Flow-ID:Received:Received: From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type:X-Originating-IP: X-ClientProxiedBy; bh=E71K9DAZXuBW6FMEeF8MHMH1GEx0nXMOaIwtkSO4oO4=; b=sRVbV0NF0VDxGuI6vLbKjFbjAn88nwwEyyhOvP+DcsuASXFROylHgQv1WFWk9qqcBRGQ HS1dBv1+bK6xjnQN7FiUM4YAgUrTb0eSsLKULvV+V83NRyljpC3uVTUmjKRD7uMI5AMVt azlphAJbn4ps3VjH3kwU39ierRmxNgrszcoNnM+oYTtDyrouFARFDa7Dj99gogiWAlV2o DoPXC5elVbOyFQ2LkwlP5UP2vohkzQn8kF2pQvXs6tR8YVrfr8J1ZjXNo9Cb8eJ0t9pjE VCIQhx7KO5P0C1ysT2YA7vI0T47ztotAv7Dx7bjP3AdOsy58Ri/Zdp8Brk6K/DxLX/g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=amazon.com; spf=pass smtp.mailfrom=amazon.com; dkim=pass header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=Jbpt0Pia; arc=none smtp.remote-ip=35.155.198.111 Received: from pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com (pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com [35.155.198.111]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 355D6606A321 for ; Tue, 24 Nov 2026 13:16:11 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 355D6606A321 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 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.com header.i=@amazon.com header.a=rsa-sha256 header.s=amazoncorp2 header.b=Jbpt0Pia DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazoncorp2; t=1795526172; x=1827062172; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=E71K9DAZXuBW6FMEeF8MHMH1GEx0nXMOaIwtkSO4oO4=; b=Jbpt0PiaYS1yqxsTxXSguE6wuyQv53jsutws1B8U7b1mXKYZL3EKu010 kZgW23MAW7N5EDvbFtG3fOwdtDobsomkscl4O2NtSvWmxmKrSXrw0MnSh DFjrccCRd4jlZuvrDRivA+UNjQm0H+/u8u2HnEHVU9devz5t3zjR7undd sHrLD2QCkMzwccoMCo1JGgsH82/7MWCsjBC4zhMZISw62Sr2U9Vy7XFdb lWZwM+ZaCXQvpsqwtL9z5VsruDp4kDO4BlHrizWn18YJDL0ubTNl16VQW l40fmMd7G6f2+xGbf0l5XCY1H8giMyEPipjM+a6urIvU+tf1gfCL0gWAH g==; X-CSE-ConnectionGUID: O1fAtBCZTdGajmGIvDFTPg== X-CSE-MsgGUID: jsTgNV2RTlmvzzgACpj7Mg== X-IronPort-AV: E=Sophos;i="6.27,230,1787011200"; d="scan'208";a="34918374" Received: from ip-10-5-9-48.us-west-2.compute.internal (HELO smtpout.naws.us-west-2.prod.farcaster.email.amazon.dev) ([10.5.9.48]) by internal-pdx-out-009.esa.us-west-2.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Nov 2026 13:16:09 +0000 Received: from EX19MTAUWA002.ant.amazon.com [205.251.233.178:12528] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.50.116:2525] with esmtp (Farcaster) id 0700fbb8-9e13-474a-a14d-0a1c8bebf46f; Tue, 24 Nov 2026 13:16:09 +0000 (UTC) X-Farcaster-Flow-ID: 0700fbb8-9e13-474a-a14d-0a1c8bebf46f Received: from EX19D001UWA001.ant.amazon.com (10.13.138.214) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Tue, 24 Nov 2026 13:16:09 +0000 Received: from dev-dsk-farbere-1a-46ecabed.eu-west-1.amazon.com (172.19.116.181) by EX19D001UWA001.ant.amazon.com (10.13.138.214) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.57; Tue, 24 Nov 2026 13:16:05 +0000 From: Eliav Farber To: Andrew Morton , David Hildenbrand , Greg Kroah-Hartman , Zi Yan CC: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , "Suren Baghdasaryan" , Michal Hocko , Jann Horn , Pedro Falcato , Baolin Wang , Nico Pache , "Ryan Roberts" , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , , , Eliav Farber Subject: [PATCH v3 5/5] uio: implement huge_fault support for physical mappings Date: Tue, 24 Nov 2026 13:15:10 +0000 Message-ID: <20261124131510.38745-6-farbere@amazon.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261124131510.38745-1-farbere@amazon.com> References: <20261124131510.38745-1-farbere@amazon.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: EX19D031UWC002.ant.amazon.com (10.13.139.212) To EX19D001UWA001.ant.amazon.com (10.13.138.214) uio_mmap_physical() maps UIO_MEM_PHYS and UIO_MEM_IOVA regions at mmap() time with 4KiB PTEs, however large and aligned they are. Populate on fault instead, as vfio-pci does since commit f9e54c3a2f5b ("vfio/pci: implement huge_fault support"): call track_pfn_range() rather than remap_pfn_range(), and add ->fault and ->huge_fault handlers using vmf_insert_pfn_order(). A large enough aligned region is then mapped with PMD or PUD entries. Like uio_vma_fault(), both handlers take idev->info_lock and fail with SIGBUS once the device is gone. COW mappings keep the eager remap. The fault handler needs the base pfn, so vm_pgoff now holds that instead of the map index, as remap_pfn_range() does for COW mappings. /proc/pid/maps reports it as the offset of these mappings. Once the device is unregistered, an untouched page gets SIGBUS. The eager remap used to keep it mapped. A remote read through ptrace or /proc/pid/mem of a mapping with no VM_READ now fails too, where the eager remap used to leave a present pte. Signed-off-by: Eliav Farber --- Changes in v3: - Follow track_pfn_range()'s new signature. - Return fixup_user_fault()'s error from .access instead of ignoring it. Changes in v2: - Give COW mappings their own vm_operations_struct, rather than test for COW inside the fault handler. - Corrected the L1TF claim in the commit message. drivers/uio/uio.c | 99 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 90 insertions(+), 9 deletions(-) diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c index f8fa20522660..00f58b68d504 100644 --- a/drivers/uio/uio.c +++ b/drivers/uio/uio.c @@ -733,12 +733,78 @@ static int uio_mmap_logical(struct vm_area_struct *vma) return 0; } -static const struct vm_operations_struct uio_physical_vm_ops = { +/* Populate UIO maps on fault, huge where the arch and alignment allow */ +static vm_fault_t uio_physical_huge_fault(struct vm_fault *vmf, + unsigned int order) +{ + unsigned long addr = vmf->address & ~((PAGE_SIZE << order) - 1); + struct vm_area_struct *vma = vmf->vma; + struct uio_device *idev = vma->vm_private_data; + vm_fault_t ret = VM_FAULT_SIGBUS; + unsigned long pfn; + + /* An order-aligned addr below the VMA would wrap the subtraction */ + if (addr < vma->vm_start) + return VM_FAULT_FALLBACK; + + /* vm_pgoff is the base pfn, not the map index (uio_mmap_physical()) */ + pfn = vma->vm_pgoff + ((addr - vma->vm_start) >> PAGE_SHIFT); + + mutex_lock(&idev->info_lock); + + if (idev->info) + ret = vmf_insert_pfn_order(vmf, pfn, order); + + mutex_unlock(&idev->info_lock); + + return ret; +} + +static vm_fault_t uio_physical_fault(struct vm_fault *vmf) +{ + return uio_physical_huge_fault(vmf, 0); +} + +#ifdef CONFIG_HAVE_IOREMAP_PROT +/* generic_access_phys() needs a present PTE; a lazy mapping may have none */ +static int uio_physical_access(struct vm_area_struct *vma, unsigned long addr, + void *buf, int len, int write) +{ + unsigned int fault_flags = FAULT_FLAG_REMOTE; + int ret; + + if (write) + fault_flags |= FAULT_FLAG_WRITE; + + /* unlocked == NULL keeps the mmap lock held and the vma valid */ + ret = fixup_user_fault(vma->vm_mm, addr, fault_flags, NULL); + if (ret) + return ret; + + return generic_access_phys(vma, addr, buf, len, write); +} +#endif + +/* + * vm_ops for a COW mapping, which vmf_insert_pfn*() cannot populate. Must + * exist even when empty: a NULL vm_ops would make the VMA anonymous. + */ +static const struct vm_operations_struct uio_physical_cow_vm_ops = { #ifdef CONFIG_HAVE_IOREMAP_PROT .access = generic_access_phys, #endif }; +static const struct vm_operations_struct uio_physical_vm_ops = { + .fault = uio_physical_fault, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .huge_fault = uio_physical_huge_fault, +#endif +#ifdef CONFIG_HAVE_IOREMAP_PROT + .access = uio_physical_access, +#endif +}; + static int uio_mmap_physical(struct vm_area_struct *vma) { struct uio_device *idev = vma->vm_private_data; @@ -754,7 +820,6 @@ static int uio_mmap_physical(struct vm_area_struct *vma) if (vma->vm_end - vma->vm_start > mem->size) return -EINVAL; - vma->vm_ops = &uio_physical_vm_ops; if (idev->info->mem[mi].memtype == UIO_MEM_PHYS) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); @@ -764,14 +829,30 @@ static int uio_mmap_physical(struct vm_area_struct *vma) * up above in uio_find_mem_index(), rather than an * actual page offset into the mmap. * - * So we just do the physical mmap without a page - * offset. + * So a COW mapping is remapped below without a page + * offset, and the lazy path overwrites vm_pgoff. */ - return remap_pfn_range(vma, - vma->vm_start, - mem->addr >> PAGE_SHIFT, - vma->vm_end - vma->vm_start, - vma->vm_page_prot); + if (vma_is_cow_mapping(vma)) { + vma->vm_ops = &uio_physical_cow_vm_ops; + + return remap_pfn_range(vma, + vma->vm_start, + mem->addr >> PAGE_SHIFT, + vma->vm_end - vma->vm_start, + vma->vm_page_prot); + } + + vma->vm_ops = &uio_physical_vm_ops; + + /* + * A VMA split adds the split offset to vm_pgoff, so the map index + * stored there would no longer find the map. Store the base pfn + * instead, as remap_pfn_range() does for COW mappings. + */ + vma->vm_pgoff = mem->addr >> PAGE_SHIFT; + + /* Tracking reserves the memory type, for PAT conflict detection */ + return track_pfn_range(vma, mem->addr >> PAGE_SHIFT); } static int uio_mmap_dma_coherent(struct vm_area_struct *vma) -- 2.47.3