Skip to content

Commit 0bf2edf

Browse files
davidhildenbrandakpm00
authored andcommitted
mm/page_alloc: reject unreasonable folio/compound page sizes in alloc_contig_range_noprof()
Let's reject them early, which in turn makes folio_alloc_gigantic() reject them properly. To avoid converting from order to nr_pages, let's just add MAX_FOLIO_ORDER and calculate MAX_FOLIO_NR_PAGES based on that. While at it, let's just make the order a "const unsigned order". Link: https://lkml.kernel.org/r/20250901150359.867252-7-david@redhat.com Signed-off-by: David Hildenbrand <david@redhat.com> Reviewed-by: Zi Yan <ziy@nvidia.com> Acked-by: SeongJae Park <sj@kernel.org> Reviewed-by: Wei Yang <richard.weiyang@gmail.com> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Reviewed-by: Liam R. Howlett <Liam.Howlett@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 3b864c8 commit 0bf2edf

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

include/linux/mm.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,11 +2055,13 @@ static inline long folio_nr_pages(const struct folio *folio)
20552055

20562056
/* Only hugetlbfs can allocate folios larger than MAX_ORDER */
20572057
#ifdef CONFIG_ARCH_HAS_GIGANTIC_PAGE
2058-
#define MAX_FOLIO_NR_PAGES (1UL << PUD_ORDER)
2058+
#define MAX_FOLIO_ORDER PUD_ORDER
20592059
#else
2060-
#define MAX_FOLIO_NR_PAGES MAX_ORDER_NR_PAGES
2060+
#define MAX_FOLIO_ORDER MAX_PAGE_ORDER
20612061
#endif
20622062

2063+
#define MAX_FOLIO_NR_PAGES (1UL << MAX_FOLIO_ORDER)
2064+
20632065
/*
20642066
* compound_nr() returns the number of pages in this potentially compound
20652067
* page. compound_nr() can be called on a tail page, and is defined to

mm/page_alloc.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6838,6 +6838,7 @@ static int __alloc_contig_verify_gfp_mask(gfp_t gfp_mask, gfp_t *gfp_cc_mask)
68386838
int alloc_contig_range_noprof(unsigned long start, unsigned long end,
68396839
acr_flags_t alloc_flags, gfp_t gfp_mask)
68406840
{
6841+
const unsigned int order = ilog2(end - start);
68416842
unsigned long outer_start, outer_end;
68426843
int ret = 0;
68436844

@@ -6855,6 +6856,14 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end,
68556856
PB_ISOLATE_MODE_CMA_ALLOC :
68566857
PB_ISOLATE_MODE_OTHER;
68576858

6859+
/*
6860+
* In contrast to the buddy, we allow for orders here that exceed
6861+
* MAX_PAGE_ORDER, so we must manually make sure that we are not
6862+
* exceeding the maximum folio order.
6863+
*/
6864+
if (WARN_ON_ONCE((gfp_mask & __GFP_COMP) && order > MAX_FOLIO_ORDER))
6865+
return -EINVAL;
6866+
68586867
gfp_mask = current_gfp_context(gfp_mask);
68596868
if (__alloc_contig_verify_gfp_mask(gfp_mask, (gfp_t *)&cc.gfp_mask))
68606869
return -EINVAL;
@@ -6952,7 +6961,6 @@ int alloc_contig_range_noprof(unsigned long start, unsigned long end,
69526961
free_contig_range(end, outer_end - end);
69536962
} else if (start == outer_start && end == outer_end && is_power_of_2(end - start)) {
69546963
struct page *head = pfn_to_page(start);
6955-
int order = ilog2(end - start);
69566964

69576965
check_new_pages(head, order);
69586966
prep_new_page(head, order, gfp_mask, 0);

0 commit comments

Comments
 (0)