Skip to contentSkip to conversation

mm/hugetlb: Remove prepare_hugepage_range()

PATCH Applied Peter Xu 3 participants 5 messages Jun 27, 2025 – Jul 2, 2025

Subsystems (5)
lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 mm/hugetlb: Remove prepare_hugepage_range()

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, even though it may not be 100% identical checks.

For example, for both of the architectures, there will be a trivial difference on how stack top was defined. The old code uses STACK_TOP which may be slightly smaller than TASK_SIZE on either of them, but the hope is that shouldn't be a problem.

It means the whole API is pretty much obsolete at least now, remove it completely.

---
 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(-)
arch/loongarch/include/asm/hugetlb.h +0 -14
index 4dc4b3e04225..ab68b594f889 100644
@@ -10,20 +10,6 @@
 
 uint64_t pmd_to_entrylo(unsigned long pmd_val);
 
-#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 = 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)
arch/mips/include/asm/hugetlb.h +0 -14
index fbc71ddcf0f6..8c460ce01ffe 100644
@@ -11,20 +11,6 @@
 
 #include <asm/page.h>
 
-#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 = 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,
fs/hugetlbfs/inode.c +2 -6
index 00b2d1a032fd..81a6acddd690 100644
@@ -179,12 +179,8 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 
 	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 = ALIGN(addr, huge_page_size(h));
 
include/asm-generic/hugetlb.h +0 -8
index 3e0a8fe9b108..4bce4f07f44f 100644
@@ -114,14 +114,6 @@ static inline int huge_pte_none_mostly(pte_t pte)
 }
 #endif
 
-#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)
include/linux/hugetlb.h +0 -6
index c6c87eae4a8d..474de8e2a8f2 100644
@@ -359,12 +359,6 @@ static inline void hugetlb_show_meminfo_node(int nid)
 {
 }
 
-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)
 {
 }
Signature · 1 line
2.49.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Peter Xu

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, even though it may not be 100% identical checks.

Quoted 7 lines

For example, for both of the architectures, there will be a trivial difference on how stack top was defined. The old code uses STACK_TOP which may be slightly smaller than TASK_SIZE on either of them, but the hope is that shouldn't be a problem.

It means the whole API is pretty much obsolete at least now, remove it completely.

Agreed, this API is now redundant.

Quoted 115 lines
---
 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(-)
arch/loongarch/include/asm/hugetlb.h +0 -14
index 4dc4b3e04225..ab68b594f889 100644
@@ -10,20 +10,6 @@
 
 uint64_t pmd_to_entrylo(unsigned long pmd_val);
 
-#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 = 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)
arch/mips/include/asm/hugetlb.h +0 -14
index fbc71ddcf0f6..8c460ce01ffe 100644
@@ -11,20 +11,6 @@
 
 #include <asm/page.h>
 
-#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 = 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,
fs/hugetlbfs/inode.c +2 -6
index 00b2d1a032fd..81a6acddd690 100644
@@ -179,12 +179,8 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 
 	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 = ALIGN(addr, huge_page_size(h));
 
include/asm-generic/hugetlb.h +0 -8
index 3e0a8fe9b108..4bce4f07f44f 100644
@@ -114,14 +114,6 @@ static inline int huge_pte_none_mostly(pte_t pte)
 }
 #endif
 
-#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)
include/linux/hugetlb.h +0 -0
index c6c87eae4a8d..474de8e2a8f2 100644
@@ -359,12 +359,6 @@ static inline void hugetlb_show_meminfo_node(int nid)
 {
 }

include/linux/hugetlb.h static inline void hugetlb_show_meminfo_node(int nid) · line 364

Quoted 10 lines
include/linux/hugetlb.h +0 -6
 
-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)
 {
 }

A small nit - there is a now stale in code comment still referring to prepare_hugepage_range() in hugetlbfs_file_mmap().

Otherwise LGTM.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Peter Xu
Quoted 5 lines · Peter Xu

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, even though it may not be 100% identical checks.

Quoted 7 lines

For example, for both of the architectures, there will be a trivial difference on how stack top was defined. The old code uses STACK_TOP which may be slightly smaller than TASK_SIZE on either of them, but the hope is that shouldn't be a problem.

It means the whole API is pretty much obsolete at least now, remove it completely.

Thanks for rewording this change, apologies for the late response on your change log. I think it's fine.

I think the only meaningful difference is that the failure would have aborted entirely if stack_top - len < addr, but with this change it will attempt to search in the opposite direction. Unless I missed something?

I suspect that this wasn't meant to happen in the first place anyways, and I bet there are no tests for it and no real-world harm in changing an error scenario into a potential successful mapping.

Quoted 129 lines
---
 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(-)
arch/loongarch/include/asm/hugetlb.h +0 -14
index 4dc4b3e04225..ab68b594f889 100644
@@ -10,20 +10,6 @@
 
 uint64_t pmd_to_entrylo(unsigned long pmd_val);
 
-#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 = 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)
arch/mips/include/asm/hugetlb.h +0 -14
index fbc71ddcf0f6..8c460ce01ffe 100644
@@ -11,20 +11,6 @@
 
 #include <asm/page.h>
 
-#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 = 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,
fs/hugetlbfs/inode.c +2 -6
index 00b2d1a032fd..81a6acddd690 100644
@@ -179,12 +179,8 @@ hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
 
 	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 = ALIGN(addr, huge_page_size(h));
 
include/asm-generic/hugetlb.h +0 -8
index 3e0a8fe9b108..4bce4f07f44f 100644
@@ -114,14 +114,6 @@ static inline int huge_pte_none_mostly(pte_t pte)
 }
 #endif
 
-#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)
include/linux/hugetlb.h +0 -6
index c6c87eae4a8d..474de8e2a8f2 100644
@@ -359,12 +359,6 @@ static inline void hugetlb_show_meminfo_node(int nid)
 {
 }
 
-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)
 {
 }
Signature · 1 line
2.49.0
author
#3 lore raw
Quoted 11 lines · Liam R. Howlett
Quoted 10 lines · Peter Xu

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, even though it may not be 100% identical checks.

For example, for both of the architectures, there will be a trivial difference on how stack top was defined. The old code uses STACK_TOP which may be slightly smaller than TASK_SIZE on either of them, but the hope is that shouldn't be a problem.

Quoted 5 lines
Quoted 2 lines

It means the whole API is pretty much obsolete at least now, remove it completely.

Thanks for rewording this change, apologies for the late response on your change log. I think it's fine.

That's totally OK, thanks for keeping an eye.

Quoted 4 lines

I think the only meaningful difference is that the failure would have aborted entirely if stack_top - len < addr, but with this change it will attempt to search in the opposite direction. Unless I missed something?

IIUC the prepare_hugepage_range() API shouldn't affect the direction of VA allocation yet, but rather a (likely proven unnecessary by this patch) way to fail fast for hugetlbfs for no good reason.

It is currently only invoked with MAP_FIXED, and if it returns 0 it'll further move on to the core VA allocator. Then the allocation can happen either TOP->DOWN or DOWN->TOP.

So "stack_top - len < addr" check is meaningful no matter if MMF_TOPDOWN or not, because we want to make sure it won't overflow in any direction. It's just that it's already covered at least by the two archs providing this hugetlbfs specific hook.

Quoted 4 lines

I suspect that this wasn't meant to happen in the first place anyways, and I bet there are no tests for it and no real-world harm in changing an error scenario into a potential successful mapping.

Yes, checking stack for hugetlbfs so far just looks redundant. Maybe keep digging the history we may found why we had it before, but so far it looks not useful at all.

Quoted 2 lines

Thanks,

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 29 lines · Peter Xu
Quoted 11 lines · Liam R. Howlett
Quoted 10 lines · Peter Xu

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, even though it may not be 100% identical checks.

For example, for both of the architectures, there will be a trivial difference on how stack top was defined. The old code uses STACK_TOP which may be slightly smaller than TASK_SIZE on either of them, but the hope is that shouldn't be a problem.

Quoted 5 lines
Quoted 2 lines

It means the whole API is pretty much obsolete at least now, remove it completely.

Thanks for rewording this change, apologies for the late response on your change log. I think it's fine.

That's totally OK, thanks for keeping an eye.

Quoted 4 lines

I think the only meaningful difference is that the failure would have aborted entirely if stack_top - len < addr, but with this change it will attempt to search in the opposite direction. Unless I missed something?

IIUC the prepare_hugepage_range() API shouldn't affect the direction of VA allocation yet, but rather a (likely proven unnecessary by this patch) way to fail fast for hugetlbfs for no good reason.

Quoted 8 lines

It is currently only invoked with MAP_FIXED, and if it returns 0 it'll further move on to the core VA allocator. Then the allocation can happen either TOP->DOWN or DOWN->TOP.

So "stack_top - len < addr" check is meaningful no matter if MMF_TOPDOWN or not, because we want to make sure it won't overflow in any direction. It's just that it's already covered at least by the two archs providing this hugetlbfs specific hook.

What I meant was, before your change the failure would happen before the we get to the core VA allocator, so the reverse direction would never happen.

If this is only used with MAP_FIXED, then I don't think it matters as many tests are skipped anyways. It might be worth mentioning that the MAP_FIXED flag is checked in different orders based on the arch, again I think this change resulted from forked archs cloning the code and keeping up to date.

Thanks,
Liam