From nobody Fri Oct 10 13:31:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 E547819C558 for ; Fri, 13 Jun 2025 13:41:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822087; cv=none; b=uE3V6q+4l0LClHUQLT6b1x925LwZN+LSY5uzhdMGxwpCdZuNdVCa3ysK5Htw54y1XymTJw81OW8cE81+B+LM7JLTVVach0UAsec37H4yPz5P2DIR3BgN2tTcL/7w0anlxTh4YY/HTvzs0HwkIA8trwOK7vOZ6PYT3ady7i632UI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822087; c=relaxed/simple; bh=gTS/EUIP8VCBOB3dazG/2BtugSlfm9QH354rN6o3IrE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d6egHUScEtO/LqBtqTnsYX5fUDb2oqogjaqL4/8LMiQ0/eJ/d1zakyDslLh8P99k6+WOB5JmSIxtYr4ZB61ncO5lK1o5YdHi8vlrEO/fIUdFBr1Nakty9Sg7mzhbCkoxgoWkkKvLRizsDe0DfL9voQE0b5jLYNHTmtt6NTqh15M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=fKPAeiDH; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="fKPAeiDH" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749822084; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=z7W5IE9G9/u3/1BfK2SYpnNa28U9EpjOusASuWz1vAk=; b=fKPAeiDHVTjBPT8xl0lpCcQp1oU46UnHsQYLFppnvM7elKT83o3bavMS4jGmxNb5jtO/Ph +aJxz2b6SBaMuU5Y2L+2VRkenVVqGodRUihxo2aUhJlPTtu9aY5x0Rm6DumcPk5Mgv/2jh uN+v4AxqXGCrfKCP8U7meLkRfnPLgsk= 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-70-B_jeZO5gNUSjzW_XQoZyQA-1; Fri, 13 Jun 2025 09:41:18 -0400 X-MC-Unique: B_jeZO5gNUSjzW_XQoZyQA-1 X-Mimecast-MFC-AGG-ID: B_jeZO5gNUSjzW_XQoZyQA_1749822078 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7c5d608e6f5so534686585a.0 for ; Fri, 13 Jun 2025 06:41:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749822077; x=1750426877; 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=z7W5IE9G9/u3/1BfK2SYpnNa28U9EpjOusASuWz1vAk=; b=qf25tW34iq8KbFZJ/y+tT4RpTjwwWm69ACqI/xadiVY1cZ8X7bu6b7RauTRu1sV1un 2GPKnO09WsOFREntsXS9hwB96cxENN9RE01h9A0SC+euzCyNRn/sdeuuLQNBushstU8T u8+asU1KpuB+n7uOMrc/ppSRHEUHPgCokm1zkgyqYtSRHda+1WnjkUdNc988rwTyBxHj Il7iqXKPkYQ5dX6vyBja8pf/8RfJ1E4jnNW67NzJ2WhnORLTbaA9R8FZgNaqtgxBNcQv IAkhLA1ERUq/xewM5Iany7H7bsz+pBLWtb7Y9Vjbqzzamc6jBgnh8BpvysgVwkdbuTft qbEg== X-Gm-Message-State: AOJu0Yw9Zc3vtXnxeMriVIUBYagK+ek5RLlrU+CnyshiMOGQotqTX8NV FnEhqW6QahIx8HKPUakA/Lkgij/gqB0BXMWaXJcH/4M0mnw2h9/i5QznbcZ3VE0CpupM6521PoA lH9drjxZI1RDT0SfeXJ8XqTgr0PKQ2+ho9bney5e8NB+2tcHvMbz/njWEXch5X0P72XQXvvl77J WXy9CkjeL+47SMpI6e7NJhMN9Z12vkNwh1tUI8YnSCmPdpR0o= X-Gm-Gg: ASbGncu1Cme7mCD8M2X74Ibuny3w3QPMV3K0cuLW0ZVvjF70Di1CRYqhNrTgmqoc4sS ctaSK1JNgPCdwifv9Iv7cv0Auqsixt7C5KgpN2+muS4wh4eBQVUXART4cqkU2DOroZ4wnriw3WL HdTPSPXShWa3FZq08y9eQTFSTcLTSAShWsHjxNMO9YDnLtGXSgw6fxyRfCTygXD9eXAEwXgiHSg CqFL3RxoFfAGJr+XY4XM7z6/jzRm5Qiqx42HevQZgKaCYDcsvSReJcelvMeYLrUahN3htXJyjVD Dhp1Fq9SvT0= X-Received: by 2002:a05:620a:3728:b0:7d3:914b:abe8 with SMTP id af79cd13be357-7d3bc4475a7mr706293885a.36.1749822077573; Fri, 13 Jun 2025 06:41:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFLW9momYNJwfmPgxDbKad4mtJlNzLyJURmiqrfzObWILrSALY5rlf3s+E58Q/NYKr9xf6MiQ== X-Received: by 2002:a05:620a:3728:b0:7d3:914b:abe8 with SMTP id af79cd13be357-7d3bc4475a7mr706288985a.36.1749822077027; Fri, 13 Jun 2025 06:41:17 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3b8ee3f72sm171519285a.94.2025.06.13.06.41.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 06:41:15 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org Cc: Andrew Morton , Alex Williamson , Zi Yan , Jason Gunthorpe , Alex Mastro , David Hildenbrand , Nico Pache , peterx@redhat.com Subject: [PATCH 1/5] mm: Deduplicate mm_get_unmapped_area() Date: Fri, 13 Jun 2025 09:41:07 -0400 Message-ID: <20250613134111.469884-2-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250613134111.469884-1-peterx@redhat.com> References: <20250613134111.469884-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Essentially it sets vm_flags=3D=3D0 for mm_get_unmapped_area_vmflags(). Use the helper instead to dedup the lines. Signed-off-by: Peter Xu Acked-by: David Hildenbrand Reviewed-by: Jason Gunthorpe Reviewed-by: Lorenzo Stoakes Reviewed-by: Oscar Salvador Reviewed-by: Pedro Falcato Reviewed-by: Zi Yan --- mm/mmap.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 09c563c95112..422f5b9d9660 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -871,9 +871,8 @@ mm_get_unmapped_area(struct mm_struct *mm, struct file = *file, unsigned long addr, unsigned long len, unsigned long pgoff, unsigned long flags) { - if (test_bit(MMF_TOPDOWN, &mm->flags)) - return arch_get_unmapped_area_topdown(file, addr, len, pgoff, flags, 0); - return arch_get_unmapped_area(file, addr, len, pgoff, flags, 0); + return mm_get_unmapped_area_vmflags(mm, file, addr, len, + pgoff, flags, 0); } EXPORT_SYMBOL(mm_get_unmapped_area); =20 --=20 2.49.0 From nobody Fri Oct 10 13:31:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 81982191F84 for ; Fri, 13 Jun 2025 13:41:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822085; cv=none; b=YuqBhSGBaHHcHqWrF5tHy658u/O8Hl4R/FGXmmQ+eKJ6PrnwUYsu1QES+W2QZFwunbblIwhR8avvaNIIqSEvRLIKZWRHBvP60tlUmkSJVGy/qrjx+zsyieYypBHI82g6AnjEzDAKj66fFTadgdJwaWPxJDQ+cfU8bZI4oQeu9A0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822085; c=relaxed/simple; bh=oBujyHW3fR3UArBsFd0eQdvuPTosgfHx6P9PRFKYox8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RwR3ugaP99JggYq2Zr228jvcTtM4uKxNwODcYBIp0UIW2ySU0yqblY0rBmNGYcMoMWYLY/Hi/SrDoQsSr1KqdQVeLYHam9WThyHg8jrd1xtiQcVJ5nThjdG93P+LPEj7d18/+vg6PLhRQ+VpWQv17UcEDl0K0WWmhje6TzIU2jY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=LuubT669; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="LuubT669" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749822081; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CurRk13BLi453FdNUVmvRv3/1kchAf7liLqepSAurz8=; b=LuubT669ueDH/xKrJFloiM00Z9RVEWMQ+DYwBJW+PkN4UWUbkjK9mDQA1y04bySSLHTipq GPZG++dULivASt9Lf7maMkjfFC052twrT/+FqM+239BPt+LKGlrdM7QXhDqo+A6d9gElaX DIVWbJCwJczl5YnsRCehZtOtyn4A4Cs= 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-250-dvUiS6ZNNq-tQB227trzew-1; Fri, 13 Jun 2025 09:41:20 -0400 X-MC-Unique: dvUiS6ZNNq-tQB227trzew-1 X-Mimecast-MFC-AGG-ID: dvUiS6ZNNq-tQB227trzew_1749822080 Received: by mail-qk1-f199.google.com with SMTP id af79cd13be357-7c5cd0f8961so480348985a.1 for ; Fri, 13 Jun 2025 06:41:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749822079; x=1750426879; 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=CurRk13BLi453FdNUVmvRv3/1kchAf7liLqepSAurz8=; b=UKddhGJB3P6yHkoiGHd2IfO6YrhLq5AYIavu1xoAJZQmYg5YMwODwkY0qIO04JY+9d 11o7OnMG6PQW0TJlNCCv5xEB8GBENq1e3NVhLE99gp2+Y5lKygAnNX1gMghRziIMa4If S1QNiNZd3ehqjcElhe0QPfhjXHMAPV+y4Zxc6pr/uISn1FIfWu/IPJZRObXsx071e592 GMIsXUhXBDn7YJ8M7xrk2bREgVKGhb5ZD9tx9/U454ETAe42RiKaEnt08eh0N/b9DjzX rMZicZhX+VZgdyBR8xHoOWbGHHOQeY1PAgmmc2b8pIpiAk3V9WVElHSfzwl+us6El5DR 0k8g== X-Gm-Message-State: AOJu0Yzmmkeme10VmfcY8B2op7gc3+B8S/HgH2Cogpjkzl3v8+mfJOxQ YRSL1a/YmQAL7u+EVFLEPTzMv+rDxh+IaTtXMvOeUApE6de8p6ugCh1Xcqio606kb9WUQzLvZ+8 ch+4t2Cnu8+yK2gPuBk8VwRIFeVnK9IPwEKlQd/mLkBNaGteG2ohBjGlpy3/f73Rhr1uqdVClua Q3KTqE41vOJp/fCtJvXNeBr949BcCgUkoSEItZnolOvDlPMmU= X-Gm-Gg: ASbGncvjvzbwk2EH6GHuh0M373UqFIIXTJ0Mr7NQ3+s1NItAc10EGHgNAyai4juxxcb iYNYAaWIRE7iq0TLlOg0qwhNjw9tNfAmTI2KqO0fz18uMfO+D/QzNielMLDcVbOjBHUROfX8vgi 3+vqezHu2WE9WvCiYFg25sGgT1AnwvQUyOs4gktPUNPGlBUSD4iMYkbT+jDihnBDx9rq63zxM+O 87Mf7lavXnkhmKOcfAN5Cmt4Z02zqkxlEM3Ft9ckfiplY4gYj0D9JIm3p/XkGFc+hngM8TCBuO5 NI+EnCCfpv4= X-Received: by 2002:a05:620a:44d0:b0:7d2:27ff:2133 with SMTP id af79cd13be357-7d3bc3b0704mr463530385a.1.1749822079373; Fri, 13 Jun 2025 06:41:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGmKoTuxvL7ffTQWjIUtdV/ylXkzGCctDnDjUG//mfabF7LSk4R8g8f8pkMatkvnCJNdWJBNQ== X-Received: by 2002:a05:620a:44d0:b0:7d2:27ff:2133 with SMTP id af79cd13be357-7d3bc3b0704mr463525385a.1.1749822078879; Fri, 13 Jun 2025 06:41:18 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3b8ee3f72sm171519285a.94.2025.06.13.06.41.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 06:41:18 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org Cc: Andrew Morton , Alex Williamson , Zi Yan , Jason Gunthorpe , Alex Mastro , David Hildenbrand , Nico Pache , peterx@redhat.com, Huacai Chen , Thomas Bogendoerfer , Muchun Song , Oscar Salvador , loongarch@lists.linux.dev, linux-mips@vger.kernel.org Subject: [PATCH 2/5] mm/hugetlb: Remove prepare_hugepage_range() Date: Fri, 13 Jun 2025 09:41:08 -0400 Message-ID: <20250613134111.469884-3-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250613134111.469884-1-peterx@redhat.com> References: <20250613134111.469884-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Only mips and loongarch implemented this API, however what it does was checking against stack overflow for either len or addr. That's already done in arch's arch_get_unmapped_area*() functions, hence not needed. It means the whole API is pretty much obsolete at least now, remove it completely. Cc: Huacai Chen Cc: Thomas Bogendoerfer Cc: Muchun Song Cc: Oscar Salvador Cc: loongarch@lists.linux.dev Cc: linux-mips@vger.kernel.org Signed-off-by: Peter Xu Acked-by: David Hildenbrand Reviewed-by: Jason Gunthorpe Reviewed-by: Oscar Salvador --- arch/loongarch/include/asm/hugetlb.h | 14 -------------- arch/mips/include/asm/hugetlb.h | 14 -------------- fs/hugetlbfs/inode.c | 8 ++------ include/asm-generic/hugetlb.h | 8 -------- include/linux/hugetlb.h | 6 ------ 5 files changed, 2 insertions(+), 48 deletions(-) diff --git a/arch/loongarch/include/asm/hugetlb.h b/arch/loongarch/include/= asm/hugetlb.h index 4dc4b3e04225..ab68b594f889 100644 --- a/arch/loongarch/include/asm/hugetlb.h +++ b/arch/loongarch/include/asm/hugetlb.h @@ -10,20 +10,6 @@ =20 uint64_t pmd_to_entrylo(unsigned long pmd_val); =20 -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, - unsigned long len) -{ - unsigned long task_size =3D STACK_TOP; - - if (len > task_size) - return -ENOMEM; - if (task_size - len < addr) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTE_CLEAR static inline void huge_pte_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, unsigned long sz) diff --git a/arch/mips/include/asm/hugetlb.h b/arch/mips/include/asm/hugetl= b.h index fbc71ddcf0f6..8c460ce01ffe 100644 --- a/arch/mips/include/asm/hugetlb.h +++ b/arch/mips/include/asm/hugetlb.h @@ -11,20 +11,6 @@ =20 #include =20 -#define __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, - unsigned long len) -{ - unsigned long task_size =3D STACK_TOP; - - if (len > task_size) - return -ENOMEM; - if (task_size - len < addr) - return -EINVAL; - return 0; -} - #define __HAVE_ARCH_HUGE_PTEP_GET_AND_CLEAR static inline pte_t huge_ptep_get_and_clear(struct mm_struct *mm, unsigned long addr, pte_t *ptep, diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index fc03dd541b4d..32dff13463d2 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -179,12 +179,8 @@ hugetlb_get_unmapped_area(struct file *file, unsigned = long addr, =20 if (len & ~huge_page_mask(h)) return -EINVAL; - if (flags & MAP_FIXED) { - if (addr & ~huge_page_mask(h)) - return -EINVAL; - if (prepare_hugepage_range(file, addr, len)) - return -EINVAL; - } + if ((flags & MAP_FIXED) && (addr & ~huge_page_mask(h))) + return -EINVAL; if (addr) addr0 =3D ALIGN(addr, huge_page_size(h)); =20 diff --git a/include/asm-generic/hugetlb.h b/include/asm-generic/hugetlb.h index 3e0a8fe9b108..4bce4f07f44f 100644 --- a/include/asm-generic/hugetlb.h +++ b/include/asm-generic/hugetlb.h @@ -114,14 +114,6 @@ static inline int huge_pte_none_mostly(pte_t pte) } #endif =20 -#ifndef __HAVE_ARCH_PREPARE_HUGEPAGE_RANGE -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - return 0; -} -#endif - #ifndef __HAVE_ARCH_HUGE_PTEP_SET_WRPROTECT static inline void huge_ptep_set_wrprotect(struct mm_struct *mm, unsigned long addr, pte_t *ptep) diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h index 42f374e828a2..85acdfdbe9f0 100644 --- a/include/linux/hugetlb.h +++ b/include/linux/hugetlb.h @@ -359,12 +359,6 @@ static inline void hugetlb_show_meminfo_node(int nid) { } =20 -static inline int prepare_hugepage_range(struct file *file, - unsigned long addr, unsigned long len) -{ - return -EINVAL; -} - static inline void hugetlb_vma_lock_read(struct vm_area_struct *vma) { } --=20 2.49.0 From nobody Fri Oct 10 13:31:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 70C1F198E81 for ; Fri, 13 Jun 2025 13:41:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822086; cv=none; b=HtN9kPQkIky4YyV/0Nxfl0fpak/afUMwdtxcHJM7+oyuHA1A3yLCYxqaFItj15YhKi+GZntcolB7wfPOZ1Kgnk/UVV82fSGRfZ2VAtitaCYXLMiHFu8soc/cZeMynseNOpBsIeSI+AezNe/B2XbKcH56q8BzdSjkzkBShZ0Tt7c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822086; c=relaxed/simple; bh=IG4FGgbLqBxGp2REQK9VMac2EIQLzuWAExl8Q4V6oog=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=JjKEygQpPQ640U5+Twy0KzTQNiPrdxAjp0sZC6GB9xkpdUnXRpyTOE9zu164LOsQaVWmg67BqP6FbDLXvOmEZZEo/FAQ5iMxxFu+2tTM+NZKdesiXqdp+5WOAzJrkFh8/w55ti/GSLOpjyBKxuBAJBvquWIqxSD61RCbT7pKzc0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=dDs1l449; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="dDs1l449" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749822083; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v/F+9x18GPW2fMOX0xB9ZVXh7FNtANzBJLNRs0BqoEY=; b=dDs1l4492mDuV6Q8jCpSPfGVB2xRLgwKgnCi8UT8kSFeaAJPlQIhM5pD8JK3egdgNUyote XSnm7TseQn94+SG1Rk5DtVeJOK8QPRxOgbt5hlcUXRSNChDOgnncMDRRX5eOGRBR+ka7MD 9B0i22oVcY843feKY4oDdzfY7EpynuA= 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-377-eylsNshfPSa450f6MxbVBQ-1; Fri, 13 Jun 2025 09:41:22 -0400 X-MC-Unique: eylsNshfPSa450f6MxbVBQ-1 X-Mimecast-MFC-AGG-ID: eylsNshfPSa450f6MxbVBQ_1749822082 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7c5bb68b386so685978085a.3 for ; Fri, 13 Jun 2025 06:41:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749822081; x=1750426881; 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=v/F+9x18GPW2fMOX0xB9ZVXh7FNtANzBJLNRs0BqoEY=; b=H0Jy+NFHYsO89ZT2Jp54g0Ou6+W0uZt41tfKJRFixFQOYbCrYKe23dPlw1AP3Xp4Bq aw5FlbDnKwVDsokvSk5C0z7AIqzwCgFb0hB/KseW/8uCQYJa8lLeCs3GyxAj5cafPWZE yDxWj3ZPyY+X33FKSPY/iF0epn9DTjiF78y4nAd+NiZAyzOdi7reJPs9yYVwB937J7aG fiYqLyEXQPpH8Trfmu9vO/h9hy+SHt9rYHNp0Qi5q6TuuxDb24F8/dNnLeA5uPftBA1b BjDUP9ZVA4m2aNx0jOgaLVKVZAcwm3Exh2Uhp5oX/19B3K1z1WesASoKu7s44de6f2jp Wrqw== X-Gm-Message-State: AOJu0Yw8yLOuaqjfygxyWIUrevOMWksPXJlbe/+5YwsnaNB0jHD7ITpp LQTrEXRwC88JpnZD4YAsI2ibXqnAcxWYpff6G+RNfTT88Iqq4nxiW6i6Zc4VkVeKGxcBrNw+TiF DKP8ajeR0o+Haj5VVODLQujnP2DwyB/s8knN4ad4oAANkUifkLWZoEvy9haER3kWKVsiPsvc9W8 QD9G3A5C6As7ruUW97c41l8hpsf/vqRuZagJxoqi2Dz3WVwRI= X-Gm-Gg: ASbGnctv3N/UtmZk+XIS7OIgf257oehxgHYmPaqzQ8qwBiRjqW0R6LCPIY1kziOe88/ LKfr7IpU+YAoABQHY2nDtoSkykCpaByJQxNMwG3oZizalpfOshPM6CAhJiEZYOJXfKyz0DvL5fv bv6LPya6jVMlag1PGXdcUojVgF1NXpe09R62BT945ACw57YJuskV6lyfppskFqtr0S2iErQ3yn4 4OLa+1o33RgLNajVG9jiOgYd2k4r7SmRMvZG9QSI41qUrlE4tILXbgqOTNRvlMyqKRqtNe4pgZl Ow5U1CEZuL8= X-Received: by 2002:a05:620a:2628:b0:7d3:ad22:7851 with SMTP id af79cd13be357-7d3bc47c25bmr596013085a.54.1749822081385; Fri, 13 Jun 2025 06:41:21 -0700 (PDT) X-Google-Smtp-Source: AGHT+IED+auoBj/loYopQZCrkvQWTc00gLx9IMXiqmfkt7NzTeF6mlRUzM1RQY37rgcn1p4Rs3RbhA== X-Received: by 2002:a05:620a:2628:b0:7d3:ad22:7851 with SMTP id af79cd13be357-7d3bc47c25bmr596008385a.54.1749822080787; Fri, 13 Jun 2025 06:41:20 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3b8ee3f72sm171519285a.94.2025.06.13.06.41.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 06:41:19 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org Cc: Andrew Morton , Alex Williamson , Zi Yan , Jason Gunthorpe , Alex Mastro , David Hildenbrand , Nico Pache , peterx@redhat.com, Baolin Wang , Lorenzo Stoakes , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song Subject: [PATCH 3/5] mm: Rename __thp_get_unmapped_area to mm_get_unmapped_area_aligned Date: Fri, 13 Jun 2025 09:41:09 -0400 Message-ID: <20250613134111.469884-4-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250613134111.469884-1-peterx@redhat.com> References: <20250613134111.469884-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This function is pretty handy for any type of VMA to provide a size-aligned VMA address when mmap(). Rename the function and export it. About the rename: - Dropping "THP" because it doesn't really have much to do with THP internally. - The suffix "_aligned" imply it is a helper to generate aligned virtual address based on what is specified (which can be not PMD_SIZE). Cc: Zi Yan Cc: Baolin Wang Cc: Lorenzo Stoakes Cc: "Liam R. Howlett" Cc: Ryan Roberts Cc: Dev Jain Cc: Barry Song Signed-off-by: Peter Xu --- include/linux/huge_mm.h | 14 +++++++++++++- mm/huge_memory.c | 6 ++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 2f190c90192d..706488d92bb6 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -339,7 +339,10 @@ unsigned long thp_get_unmapped_area(struct file *filp,= unsigned long addr, unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned lo= ng addr, unsigned long len, unsigned long pgoff, unsigned long flags, vm_flags_t vm_flags); - +unsigned long mm_get_unmapped_area_aligned(struct file *filp, + unsigned long addr, unsigned long len, + loff_t off, unsigned long flags, unsigned long size, + vm_flags_t vm_flags); bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pin= s); int split_huge_page_to_list_to_order(struct page *page, struct list_head *= list, unsigned int new_order); @@ -543,6 +546,15 @@ thp_get_unmapped_area_vmflags(struct file *filp, unsig= ned long addr, return 0; } =20 +static inline unsigned long +mm_get_unmapped_area_aligned(struct file *filp, + unsigned long addr, unsigned long len, + loff_t off, unsigned long flags, unsigned long size, + vm_flags_t vm_flags) +{ + return 0; +} + static inline bool can_split_folio(struct folio *folio, int caller_pins, int *pextra_pins) { diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4734de1dc0ae..52f13a70562f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -1088,7 +1088,7 @@ static inline bool is_transparent_hugepage(const stru= ct folio *folio) folio_test_large_rmappable(folio); } =20 -static unsigned long __thp_get_unmapped_area(struct file *filp, +unsigned long mm_get_unmapped_area_aligned(struct file *filp, unsigned long addr, unsigned long len, loff_t off, unsigned long flags, unsigned long size, vm_flags_t vm_flags) @@ -1132,6 +1132,7 @@ static unsigned long __thp_get_unmapped_area(struct f= ile *filp, ret +=3D off_sub; return ret; } +EXPORT_SYMBOL_GPL(mm_get_unmapped_area_aligned); =20 unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned lo= ng addr, unsigned long len, unsigned long pgoff, unsigned long flags, @@ -1140,7 +1141,8 @@ unsigned long thp_get_unmapped_area_vmflags(struct fi= le *filp, unsigned long add unsigned long ret; loff_t off =3D (loff_t)pgoff << PAGE_SHIFT; =20 - ret =3D __thp_get_unmapped_area(filp, addr, len, off, flags, PMD_SIZE, vm= _flags); + ret =3D mm_get_unmapped_area_aligned(filp, addr, len, off, flags, + PMD_SIZE, vm_flags); if (ret) return ret; =20 --=20 2.49.0 From nobody Fri Oct 10 13:31:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 3DCA819CD17 for ; Fri, 13 Jun 2025 13:41:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822087; cv=none; b=nrxWIBmb0NYlgXlb4/D4Yse19QHKNMxbRJphuk0FM0lSUJr7m+ZXqZzsRtCZSyNQIlk3vBJvjkTdeuWu7E53bLAOznmyrrCqDZD9K9aiClRJDtmzbWqjxhDtO900ukkGrSC6Y0e9ZhwV1+8C+RjmMwSzyY+sSUD5HJtQORFtNCg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822087; c=relaxed/simple; bh=dpUMDqRxYyy7EaOTCpbZ2yM1jpnDsm+aUl6N3AthocU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dD042Lhh7kHYkRKg9EltZtGZkVNuBXC14GUtc3FDrqK2aj9wgbVifWHme62wQlIj9xFGIUMXA/sVJ3r9SvODFhP2tLoYJFaEo9hYulm7QYgNijQh9KKcgC119DWUEljUUnR6Fc6mGKEPCSJIXSHcmHPCMiY4dV/lDpSaDyiT6WY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=a96OmX46; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="a96OmX46" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749822085; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=srvKo/GW1EbJBPf9Cl0G9P4CrJB0MZAkcimCCvUCpgo=; b=a96OmX46C9eye53EyEderJilc6ft4WSjYYJZEQgaQqC7A8Ueh8tcv/pH2gv2sCNysEeagO Owz1MDexk3a7o10ARiptNkIgcwLIAsmn10WKI7TAOIwdiM++Agw5vRTck8aW7pg/TodfBD tcNn3KwyX5hhFfuU09UNv7NHTnoQhK0= 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-650-_Dn6ugkjNSOa30tOK8kkSg-1; Fri, 13 Jun 2025 09:41:24 -0400 X-MC-Unique: _Dn6ugkjNSOa30tOK8kkSg-1 X-Mimecast-MFC-AGG-ID: _Dn6ugkjNSOa30tOK8kkSg_1749822084 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7d2107d6b30so328118185a.0 for ; Fri, 13 Jun 2025 06:41:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749822083; x=1750426883; 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=srvKo/GW1EbJBPf9Cl0G9P4CrJB0MZAkcimCCvUCpgo=; b=myjrwccsrEy/Ty6Dr0C6VgplOYNaMAEfJ2+UwUkFdq4KmVE0QihdKXK56uuNiCKcwv gBESZUtD1+hGk+qyYZ9didrlkbHDlHYQvh6oq/ZQ7Hcc5B2S4eRd184y6NAHZKqktwVk bDPWCi4985PDoEDNTajK8LXq6jZFBFiVpsAfQgaktvXEn4fcRtAugcVAoF+CEdAIQanM sllTzB0VJY5aZbIj7eMk7GNehc7Df9Oma9umBKBOLJfI4YmuqjZ2KjcQt5Rn//tEWH2M PvgIjlapDHt5kxpijbVcPicCV41ukg6SW0+w5s7S2+rSYoIDFc1wbDV4ncui7Ar/7+LQ xqsA== X-Gm-Message-State: AOJu0YxH66FQX3lyzSRK1eJWGBSQpl47L+y/s2qVqr4kmpQK+9dfjsK5 EZ+H//NGgSBAeGqo/uAB54Zcb0dt+9Gqfy4nzjsJ0ZRfKtz/KKCR/IGYRgIWto++oQzKIfJUNLo pLdxlcZg6fy09VpjbrIKGn0TdmpyDtiTX0uxnE/Off0+ErV5WujsXUhzYpUWsRMYxpfLHyEsoeF yW0YXgf+Q3c7RF2z5rmTkA5gvQznkEYgl33IMXO8tPM+X22Ak= X-Gm-Gg: ASbGnctnYMJv7VJO5OyA9H+m8Jw3VnS6ytjpQN/f7psn8YGcHmF3YLrtcRCPFh2uuGt LV90eBnZ7oOk9CeChaxoKsvPakyRjSPupYBFGWkZa11ijPgHQqSEW1+FKr/I3iWPrUxUzd0N1p8 neIThtHtPAlcMB9BFuKSX3UU4cGlMQ4qilB63/RhB6cniWCyT9ewZMRZi3v0y7OIJ6evpU4d3K2 drkhd88hzOl2D++HIeL59QCBngkAsNr3Ktu6ZuQ3hH6ZgSB1Z+I/oDtjJ6qtsU/8z5UWUHzDTiX vIPqf+ig18o= X-Received: by 2002:a05:620a:2627:b0:7cd:3f01:7c83 with SMTP id af79cd13be357-7d3bc44a1f9mr399254285a.39.1749822083063; Fri, 13 Jun 2025 06:41:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGy9xhb6Ir/WTjwOiKTV6R2xWTKghNAist5R7llcPyhmuhj1jELKcIWu8eGBTg4hcMRpO8q+w== X-Received: by 2002:a05:620a:2627:b0:7cd:3f01:7c83 with SMTP id af79cd13be357-7d3bc44a1f9mr399250685a.39.1749822082633; Fri, 13 Jun 2025 06:41:22 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3b8ee3f72sm171519285a.94.2025.06.13.06.41.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 06:41:21 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org Cc: Andrew Morton , Alex Williamson , Zi Yan , Jason Gunthorpe , Alex Mastro , David Hildenbrand , Nico Pache , peterx@redhat.com Subject: [PATCH 4/5] vfio: Introduce vfio_device_ops.get_unmapped_area hook Date: Fri, 13 Jun 2025 09:41:10 -0400 Message-ID: <20250613134111.469884-5-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250613134111.469884-1-peterx@redhat.com> References: <20250613134111.469884-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a hook to vfio_device_ops to allow sub-modules provide virtual addresses for an mmap() request. Note that the fallback will be mm_get_unmapped_area(), which should maintain the old behavior of generic VA allocation (__get_unmapped_area). It's a bit unfortunate that is needed, as the current get_unmapped_area() file ops cannot support a retval which fallbacks to the default. So that is needed both here and whenever sub-module will opt-in with its own. Signed-off-by: Peter Xu Reviewed-by: David Hildenbrand Reviewed-by: Jason Gunthorpe --- drivers/vfio/vfio_main.c | 18 ++++++++++++++++++ include/linux/vfio.h | 7 +++++++ 2 files changed, 25 insertions(+) diff --git a/drivers/vfio/vfio_main.c b/drivers/vfio/vfio_main.c index 1fd261efc582..19db8e58d223 100644 --- a/drivers/vfio/vfio_main.c +++ b/drivers/vfio/vfio_main.c @@ -1354,6 +1354,23 @@ static int vfio_device_fops_mmap(struct file *filep,= struct vm_area_struct *vma) return device->ops->mmap(device, vma); } =20 +static unsigned long vfio_device_get_unmapped_area(struct file *file, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags) +{ + struct vfio_device_file *df =3D file->private_data; + struct vfio_device *device =3D df->device; + + if (!device->ops->get_unmapped_area) + return mm_get_unmapped_area(current->mm, file, addr, + len, pgoff, flags); + + return device->ops->get_unmapped_area(device, file, addr, len, + pgoff, flags); +} + const struct file_operations vfio_device_fops =3D { .owner =3D THIS_MODULE, .open =3D vfio_device_fops_cdev_open, @@ -1363,6 +1380,7 @@ const struct file_operations vfio_device_fops =3D { .unlocked_ioctl =3D vfio_device_fops_unl_ioctl, .compat_ioctl =3D compat_ptr_ioctl, .mmap =3D vfio_device_fops_mmap, + .get_unmapped_area =3D vfio_device_get_unmapped_area, }; =20 static struct vfio_device *vfio_device_from_file(struct file *file) diff --git a/include/linux/vfio.h b/include/linux/vfio.h index 707b00772ce1..48fe71c61ed2 100644 --- a/include/linux/vfio.h +++ b/include/linux/vfio.h @@ -108,6 +108,7 @@ struct vfio_device { * @dma_unmap: Called when userspace unmaps IOVA from the container * this device is attached to. * @device_feature: Optional, fill in the VFIO_DEVICE_FEATURE ioctl + * @get_unmapped_area: Optional, provide virtual address hint for mmap() */ struct vfio_device_ops { char *name; @@ -135,6 +136,12 @@ struct vfio_device_ops { void (*dma_unmap)(struct vfio_device *vdev, u64 iova, u64 length); int (*device_feature)(struct vfio_device *device, u32 flags, void __user *arg, size_t argsz); + unsigned long (*get_unmapped_area)(struct vfio_device *device, + struct file *file, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags); }; =20 #if IS_ENABLED(CONFIG_IOMMUFD) --=20 2.49.0 From nobody Fri Oct 10 13:31:31 2025 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 6F0771C84A6 for ; Fri, 13 Jun 2025 13:41:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822090; cv=none; b=ds+qA00adsD40KbVoljSLICOM6glR8RNbSrG+rmYX2/TNJnQs29bOwYG/EnjR0TH6jKgKvCVZmYrIgD7PqHFpHOc6zYRb+AJXvygCQaxhU5Zthz5Vm9NZHM/6S7XAVCMHs3BpGiIg6TbnxP9XBgpk2tdmx2JgXV8c3k2gwNwfB4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749822090; c=relaxed/simple; bh=/Hj2o0NKvMzVIjNKTfgbctNmnmS2OLsACrglt6/q3Wg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c99i6SDWy+xMRGCV9P3s5z+i5rHFdLwc4KyTrF+Qf0SEMEn1wKxn7GF4FIkmuDGbnqCKLkbqrD2RAtFT5OttHSMxq0kojZq+Ezlm/1Lg+O3GogjQJXGkldLrJeYfcVZBLNkDbezlm2y1zesQ01NOmMy2MoUD7om6XzXemNpjFDc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ZfCuYgeM; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ZfCuYgeM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1749822087; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1Rg4eLLEgx6FDFHzHNf6mLR85Vz7bTkf4DxmFxHRoP8=; b=ZfCuYgeMNZ5rlCEGHfvR5+089zXLLFvmoUujnRc+4SaUyJr9OFZu2RZV7aRDYy6XK5j8rv MamZT8XLNbScf8PML+w1e/NIpg0oDH5QGJdQ/M5rFcV3ZY+AhoL7nyLUmZ1S5nsol2rqOQ o+iQ6NmP8Geuau+fMBJGXH+XFEeCsdc= 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-692-PQMQZBN9MWOzi7p0GDseqg-1; Fri, 13 Jun 2025 09:41:26 -0400 X-MC-Unique: PQMQZBN9MWOzi7p0GDseqg-1 X-Mimecast-MFC-AGG-ID: PQMQZBN9MWOzi7p0GDseqg_1749822085 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-7d2107d6b30so328121085a.0 for ; Fri, 13 Jun 2025 06:41:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749822085; x=1750426885; 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=1Rg4eLLEgx6FDFHzHNf6mLR85Vz7bTkf4DxmFxHRoP8=; b=nhuVQPielATYPvVAcNTmV5lN3hzumqd2aTinZemJLvLgZSu/rAJdKwGvlsn/jBxQZN 3RmxuhkaeBraK+kHOvwaO7ymkbb0wFQKci06XSJKjctOnyzZCcCWkk5WddKIwbTlma60 V1ia91Vn/RQJIO7rGzS2TXV7MO+OY1CJmXqSldZrNfSAP342ZZvKQbRQC93lSm5+Sv1A mgHtIOEjwbtRBljRF2RKJKUcSVDI3kirIUCUL4YErKF/wEYc66PRnCbqetdyRAzuCjgY wtdU1yiJNUw5gz17DYlENHVOtI4ZQub//wuKI4CTApu8nDjK07l9T7rdSzAHXgb/vm/t 7ksg== X-Gm-Message-State: AOJu0Yy/hMdeiBl/EuQCQBwdgix45wIO+PG07PJKybSEUkVOlrdYuTXv BMQAe+2LGJ0U5SNeKw6FHY2yRL/RPl6Qu1WlrThxZFWdxdH/PaLZdq3h74bThJrHX9otfzL9Zea dQA/+99f2zQ2FLv4twRyUAPv4Ll9szNeHL39Hl6aZSvfFNfnl+56xTEnIGCzUm6uMTvd7V+yFVb y9xN5Y73NE9PeUlhE0gsgmca8fVyuUhaKBSswAWrzoVkD6/gw= X-Gm-Gg: ASbGncvJh0ii+aSgSpSuwp2ZwCdlsq1NnLkCBUdnBxiQ8lAYmGLSrBWwhEC9IjqreFu 3d5cy8dl4LHJbGVYEqF8UNFvDcGa18//j6P6HVkkpbTA/zNVEwIUMmnCk9RZ45MDxun6bOY63Rh 8Ir7YXiauok1vqATYJD7r5RaK1gR3TDVAZdWgm+om1myuIYuOAuRSX/1QAWOtohdBkztozYC1zm wNOI8PvYPih4XclrijMFxkuPCaq5aZ5KU5dhwVbHlPW25TXiahuQSmNSm2hIy+BQdXnVR2TrNYH PlKJp+xyPnc= X-Received: by 2002:a05:620a:1721:b0:7d3:aad4:6f87 with SMTP id af79cd13be357-7d3bc37ba17mr568289585a.7.1749822084980; Fri, 13 Jun 2025 06:41:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFqZxKV2mT6BD/bi39sPGRhzywu281UAzYUirAEIW40JfRfajmXwptkVPgbiPNtRrb6I7XH6g== X-Received: by 2002:a05:620a:1721:b0:7d3:aad4:6f87 with SMTP id af79cd13be357-7d3bc37ba17mr568285785a.7.1749822084512; Fri, 13 Jun 2025 06:41:24 -0700 (PDT) Received: from x1.com ([85.131.185.92]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7d3b8ee3f72sm171519285a.94.2025.06.13.06.41.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Jun 2025 06:41:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org Cc: Andrew Morton , Alex Williamson , Zi Yan , Jason Gunthorpe , Alex Mastro , David Hildenbrand , Nico Pache , peterx@redhat.com Subject: [PATCH 5/5] vfio-pci: Best-effort huge pfnmaps with !MAP_FIXED mappings Date: Fri, 13 Jun 2025 09:41:11 -0400 Message-ID: <20250613134111.469884-6-peterx@redhat.com> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250613134111.469884-1-peterx@redhat.com> References: <20250613134111.469884-1-peterx@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This patch enables best-effort mmap() for vfio-pci bars even without MAP_FIXED, so as to utilize huge pfnmaps as much as possible. It should also avoid userspace changes (switching to MAP_FIXED with pre-aligned VA addresses) to start enabling huge pfnmaps on VFIO bars. Here the trick is making sure the MMIO PFNs will be aligned with the VAs allocated from mmap() when !MAP_FIXED, so that whatever returned from mmap(!MAP_FIXED) of vfio-pci MMIO regions will be automatically suitable for huge pfnmaps as much as possible. To achieve that, a custom vfio_device's get_unmapped_area() for vfio-pci devices is needed. Note that MMIO physical addresses should normally be guaranteed to be always bar-size aligned, hence the bar offset can logically be directly used to do the calculation. However to make it strict and clear (rather than relying on spec details), we still try to fetch the bar's physical addresses from pci_dev.resource[]. Signed-off-by: Alex Williamson Signed-off-by: Peter Xu Reported-by: Alex Mastro --- drivers/vfio/pci/vfio_pci.c | 3 ++ drivers/vfio/pci/vfio_pci_core.c | 65 ++++++++++++++++++++++++++++++++ include/linux/vfio_pci_core.h | 6 +++ 3 files changed, 74 insertions(+) diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c index 5ba39f7623bb..d9ae6cdbea28 100644 --- a/drivers/vfio/pci/vfio_pci.c +++ b/drivers/vfio/pci/vfio_pci.c @@ -144,6 +144,9 @@ static const struct vfio_device_ops vfio_pci_ops =3D { .detach_ioas =3D vfio_iommufd_physical_detach_ioas, .pasid_attach_ioas =3D vfio_iommufd_physical_pasid_attach_ioas, .pasid_detach_ioas =3D vfio_iommufd_physical_pasid_detach_ioas, +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP + .get_unmapped_area =3D vfio_pci_core_get_unmapped_area, +#endif }; =20 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id= *id) diff --git a/drivers/vfio/pci/vfio_pci_core.c b/drivers/vfio/pci/vfio_pci_c= ore.c index 6328c3a05bcd..835bc168f8b7 100644 --- a/drivers/vfio/pci/vfio_pci_core.c +++ b/drivers/vfio/pci/vfio_pci_core.c @@ -1641,6 +1641,71 @@ static unsigned long vma_to_pfn(struct vm_area_struc= t *vma) return (pci_resource_start(vdev->pdev, index) >> PAGE_SHIFT) + pgoff; } =20 +#ifdef CONFIG_ARCH_SUPPORTS_HUGE_PFNMAP +/* + * Hint function to provide mmap() virtual address candidate so as to be + * able to map huge pfnmaps as much as possible. It is done by aligning + * the VA to the PFN to be mapped in the specific bar. + * + * Note that this function does the minimum check on mmap() parameters to + * make the PFN calculation valid only. The majority of mmap() sanity check + * will be done later in mmap(). + */ +unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *device, + struct file *file, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags) +{ + struct vfio_pci_core_device *vdev =3D + container_of(device, struct vfio_pci_core_device, vdev); + struct pci_dev *pdev =3D vdev->pdev; + unsigned long ret, phys_len, req_start, phys_addr; + unsigned int index; + + index =3D pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); + + /* Currently, only bars 0-5 supports huge pfnmap */ + if (index >=3D VFIO_PCI_ROM_REGION_INDEX) + goto fallback; + + /* Bar offset */ + req_start =3D (pgoff << PAGE_SHIFT) & ((1UL << VFIO_PCI_OFFSET_SHIFT) - 1= ); + phys_len =3D PAGE_ALIGN(pci_resource_len(pdev, index)); + + /* + * Make sure we at least can get a valid physical address to do the + * math. If this happens, it will probably fail mmap() later.. + */ + if (req_start >=3D phys_len) + goto fallback; + + phys_len =3D MIN(phys_len, len); + /* Calculate the start of physical address to be mapped */ + phys_addr =3D pci_resource_start(pdev, index) + req_start; + + /* Choose the alignment */ + if (IS_ENABLED(CONFIG_ARCH_SUPPORTS_PUD_PFNMAP) && phys_len >=3D PUD_SIZE= ) { + ret =3D mm_get_unmapped_area_aligned(file, addr, len, phys_addr, + flags, PUD_SIZE, 0); + if (ret) + return ret; + } + + if (phys_len >=3D PMD_SIZE) { + ret =3D mm_get_unmapped_area_aligned(file, addr, len, phys_addr, + flags, PMD_SIZE, 0); + if (ret) + return ret; + } + +fallback: + return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); +} +EXPORT_SYMBOL_GPL(vfio_pci_core_get_unmapped_area); +#endif + static vm_fault_t vfio_pci_mmap_huge_fault(struct vm_fault *vmf, unsigned int order) { diff --git a/include/linux/vfio_pci_core.h b/include/linux/vfio_pci_core.h index fbb472dd99b3..e59699e01901 100644 --- a/include/linux/vfio_pci_core.h +++ b/include/linux/vfio_pci_core.h @@ -119,6 +119,12 @@ ssize_t vfio_pci_core_read(struct vfio_device *core_vd= ev, char __user *buf, size_t count, loff_t *ppos); ssize_t vfio_pci_core_write(struct vfio_device *core_vdev, const char __us= er *buf, size_t count, loff_t *ppos); +unsigned long vfio_pci_core_get_unmapped_area(struct vfio_device *device, + struct file *file, + unsigned long addr, + unsigned long len, + unsigned long pgoff, + unsigned long flags); int vfio_pci_core_mmap(struct vfio_device *core_vdev, struct vm_area_struc= t *vma); void vfio_pci_core_request(struct vfio_device *core_vdev, unsigned int cou= nt); int vfio_pci_core_match(struct vfio_device *core_vdev, char *buf); --=20 2.49.0