Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Oscar Salvador <osalvador@suse.de>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Dave Hansen <dave.hansen@linux.intel.com>,
	Karsten Desler <kdesler@soohrt.org>,
	Muchun Song <muchun.song@linux.dev>,
	David Hildenbrand <david@kernel.org>,
	Lorenzo Stoakes <ljs@kernel.org>,
	Vlastimil Babka <vbabka@kernel.org>,
	"Liam R . Howlett" <liam@infradead.org>,
	Andreas Larsson <andreas@gaisler.com>,
	"David S . Miller" <davem@davemloft.net>,
	Huacai Chen <chenhuacai@kernel.org>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Gerald Schaefer <gerald.schaefer@linux.ibm.com>,
	linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Oscar Salvador <osalvador@suse.de>
Subject: [PATCH 1/8] mm,hugetlb: Encode extra padding for aligning directly in hugetlb_get_unmapped_area
Date: Sat,  6 Jun 2026 05:49:56 +0200	[thread overview]
Message-ID: <20260606035003.529685-2-osalvador@suse.de> (raw)
In-Reply-To: <20260606035003.529685-1-osalvador@suse.de>

Currently, for hugetlb mappings arches query the align_mask needed in order to
satisfy allocating a gap for the mapping, as it counts the worst-case scenario
which is being off by one page from boundary of the next hugetlb aligned address.

Later on, unmapped_area{_topdown} add this 'align_mask' on top of the
mapping's length we requested to count for worst-case scenario and get a gap
big enough, and then do the proper masking off once we get the address.

This means we have to special case hugetlb in quite a few places to first
1) ignore align_offset and 2) return a proper align_mask, but we can just do
as THP mappings do and encode the align_mask directly in hugetlb_get_unmapped_area
and pass that down to mm_get_unmapped_area_vmflags.

Once we get the address, we can properly mask it off before handing it over to
userspace.

This also allows us to fix a regression on AMD15h family for hugetlb
mappings [1].

[1] https://lore.kernel.org/linux-mm/20260527143643.GO31091@soohrt.org/

Fixes: 7bd3f1e1a9ae ("mm: make hugetlb mappings go through mm_get_unmapped_area_vmflags")
Reported-by: Karsten Desler <kdesler@soohrt.org>
Closes: https://lore.kernel.org/linux-mm/20260527143643.GO31091@soohrt.org/
Signed-off-by: Oscar Salvador <osalvador@suse.de>
Acked-by: Dave Hansen <dave.hansen@linux.intel.com>
---
 fs/hugetlbfs/inode.c    | 25 ++++++++++++++++++++++++-
 include/linux/hugetlb.h |  2 +-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c
index 78d61bf2bd9b..da9bfb5b79f7 100644
--- a/fs/hugetlbfs/inode.c
+++ b/fs/hugetlbfs/inode.c
@@ -184,7 +184,30 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 	if (addr)
 		addr0 = ALIGN(addr, huge_page_size(h));
 
-	return mm_get_unmapped_area_vmflags(file, addr0, len, pgoff, flags, 0);
+	/*
+	 * hugetlb mappings need to be huge page aligned.
+	 * mm_get_unmapped_area_vmflags() only gives back PAGE_SIZE aligned
+	 * areas.
+	 *
+	 * Extend the requested unmapped area for the worse case scenario:
+	 * the address comes back and needs to be aligned up to by one small
+	 * page short of a large page.
+	 */
+	len += huge_page_size(h) - PAGE_SIZE;
+	/*
+	 * pgoff is being used for coloring, but hugetlb mappings need strict
+	 * alignment, so it is always ignored by special casing it down the road.
+	 * Set it to 0 here, so we do not need to be hugetlb-aware later on.
+	 */
+	pgoff = 0;
+	addr0 = mm_get_unmapped_area_vmflags(file, addr0, len, pgoff, flags, 0);
+	if (IS_ERR_VALUE(addr0))
+		return addr0;
+
+	/* Align the address to the next huge_page_size boundary */
+	addr0 = ALIGN(addr0, huge_page_size(h));
+
+	return addr0;
 }
 
 /*
diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 5957bc25efa8..924e9a464214 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -1085,7 +1085,7 @@ bool is_raw_hwpoison_page_in_hugepage(struct page *page);
 
 static inline unsigned long huge_page_mask_align(struct file *file)
 {
-	return PAGE_MASK & ~huge_page_mask(hstate_file(file));
+	return 0;
 }
 
 #else	/* CONFIG_HUGETLB_PAGE */
-- 
2.35.3



  reply	other threads:[~2026-06-06  3:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-06  3:49 [PATCH 0/8] Stop special-casing hugetlb mappings in get_unmapped_area Oscar Salvador
2026-06-06  3:49 ` Oscar Salvador [this message]
2026-06-06  3:49 ` [PATCH 2/8] mm/mmap: Stop special-casing hugetlb in generic_get_unmapped_area path Oscar Salvador
2026-06-06  3:49 ` [PATCH 3/8] arch/x86: Stop special-casing hugetlb mappings in arch_get_unmapped_area Oscar Salvador
2026-06-06  3:49 ` [PATCH 4/8] arch/s390: " Oscar Salvador
2026-06-30 17:10   ` Gerald Schaefer
2026-06-06  3:50 ` [PATCH 5/8] arch/loongarch: Stop special-casing hugetlb in arch_get_unmapped_area_common Oscar Salvador
2026-06-06  3:50 ` [PATCH 6/8] arch/sparc64: Stop special-casing hugetlb mappings in arch_get_unmapped_area Oscar Salvador
2026-06-06  3:50 ` [PATCH 7/8] arch/sparc32: Rip out hugetlb from sys_sparc_32.c Oscar Salvador
2026-06-06  3:50 ` [PATCH 8/8] mm,hugetlb: Kill huge_page_mask_align Oscar Salvador
2026-06-28  6:55 ` [PATCH 0/8] Stop special-casing hugetlb mappings in get_unmapped_area Andrew Morton
2026-06-29 10:21   ` Lorenzo Stoakes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260606035003.529685-2-osalvador@suse.de \
    --to=osalvador@suse.de \
    --cc=agordeev@linux.ibm.com \
    --cc=akpm@linux-foundation.org \
    --cc=andreas@gaisler.com \
    --cc=chenhuacai@kernel.org \
    --cc=dave.hansen@linux.intel.com \
    --cc=davem@davemloft.net \
    --cc=david@kernel.org \
    --cc=gerald.schaefer@linux.ibm.com \
    --cc=kdesler@soohrt.org \
    --cc=liam@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ljs@kernel.org \
    --cc=muchun.song@linux.dev \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox