Skip to contentSkip to conversation

mm/page_alloc: fix alignment for alloc_contig_pages_noprof()

PATCH In review Wei Yang 6 participants 17 messages Sep 23, 2025 – Sep 25, 2025

Subsystems (2)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm/page_alloc: fix alignment for alloc_contig_pages_noprof()

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Use roundup() to calculate the correct alignment.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index a1bcc1e003c7..a17a6014e3db 100644
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
 					gfp_zone(gfp_mask), nodemask) {
 		spin_lock_irqsave(&zone->lock, flags);
 
-		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
 		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
 			if (pfn_range_valid_contig(pfn, nr_pages)) {
 				/*
Signature · 1 line
2.34.1
reviewer
#1 lore raw
Quoted 6 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 33 lines

Use roundup() to calculate the correct alignment.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index a1bcc1e003c7..a17a6014e3db 100644
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
 					gfp_zone(gfp_mask), nodemask) {
 		spin_lock_irqsave(&zone->lock, flags);
 
-		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
 		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
 			if (pfn_range_valid_contig(pfn, nr_pages)) {
 				/*
Signature · 1 line
2.34.1

LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

#2 lore raw
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

Quoted 39 lines
Quoted 34 lines

Use roundup() to calculate the correct alignment.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index a1bcc1e003c7..a17a6014e3db 100644
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
 					gfp_zone(gfp_mask), nodemask) {
 		spin_lock_irqsave(&zone->lock, flags);
 
-		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
 		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
 			if (pfn_range_valid_contig(pfn, nr_pages)) {
 				/*
Signature · 1 line
2.34.1

LGTM. Reviewed-by: Zi Yan <[email protected]>

Best Regards,
Yan, Zi

#3 lore raw
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#4 lore raw
Quoted 22 lines · David Hildenbrand
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

Quoted 7 lines

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Something like the code below:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Best Regards,
Yan, Zi

reviewer
#5 lore raw
Quoted 28 lines · Zi Yan
Quoted 26 lines · David Hildenbrand
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

Quoted 7 lines
Quoted 3 lines

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Something like the code below:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Please let's not try to fix a non-existent problem here. If there is no alignement requirement then make sure we document it and drop the existing ALIGN which doesn't seem to serve any actual purpose rather than make the code more subtle and more obscure for no known reason.

Signature · 2 lines
Michal Hocko
SUSE Labs
#6 lore raw
Quoted 33 lines · Michal Hocko
Quoted 32 lines · Zi Yan
Quoted 29 lines · David Hildenbrand
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Quoted 8 lines
Quoted 3 lines

Something like the code below:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Please let's not try to fix a non-existent problem here. If there is no alignement requirement then make sure we document it and drop the existing ALIGN which doesn't seem to serve any actual purpose rather than make the code more subtle and more obscure for no known reason.

I think we need at least one initial align because the zone_start_pfn might not be aligned to the desired order/nr_pages.

So I don't think we can drop it.

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#7 lore raw
Quoted 38 lines · David Hildenbrand
Quoted 37 lines · Michal Hocko
Quoted 35 lines · Zi Yan
Quoted 22 lines · David Hildenbrand
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

Quoted 7 lines

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Something like the code below:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Quoted 7 lines
Quoted 4 lines

Please let's not try to fix a non-existent problem here. If there is no alignement requirement then make sure we document it and drop the existing ALIGN which doesn't seem to serve any actual purpose rather than make the code more subtle and more obscure for no known reason.

I think we need at least one initial align because the zone_start_pfn might not be aligned to the desired order/nr_pages.

I thought we have concluded that there is no user expecting a specific alignment. Or have I misunderstood that?

Signature · 2 lines
Michal Hocko
SUSE Labs
#8 lore raw
Quoted 44 lines · Michal Hocko
Quoted 43 lines · David Hildenbrand
Quoted 41 lines · Michal Hocko
Quoted 28 lines · Zi Yan
Quoted 26 lines · David Hildenbrand
Quoted 14 lines · Anshuman Khandual
Quoted 6 lines · Zi Yan
Quoted 4 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Quoted 6 lines
Quoted 2 lines

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

The result would not be any value lower than zone->zone_start_pfn, so the worst case is getting an unaligned PFN range. I guess most of the time nr_pages would be power of 2.

Quoted 6 lines

Agreed.

Also as Dev had pointed out earlier, this function gives no guarantee on alignment of non-power-of-2 requests. Hence I don't have a strong opinion either way, but does it really qualify for a "Fixes:" tag ?

I'd say if there is nothing to fix, then this patch is not required.

What likely does make sense would be that a non-power-of-2 would be aligned to the smallest contained power of 2.

Quoted 7 lines
Quoted 3 lines

E.g., a 6 MiB request would be aligned to 2 MiB instead of multiples of 6 MiB.

Not sure what the existing ALIGN would do with that ...

Something like the code below:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Please let's not try to fix a non-existent problem here. If there is no alignement requirement then make sure we document it and drop the existing ALIGN which doesn't seem to serve any actual purpose rather than make the code more subtle and more obscure for no known reason.

Quoted 5 lines
Quoted 2 lines

I think we need at least one initial align because the zone_start_pfn might not be aligned to the desired order/nr_pages.

I thought we have concluded that there is no user expecting a specific alignment. Or have I misunderstood that?

We concluded that for users that don't pass in non-power-2 requests.

hugetlb->folio_alloc_gigantic()->alloc_contig_pages_noprof() certainly expects a suitable alignment :)

I'll note that if no alignment is required, the whole loop in alloc_contig_pages_noprof() is not optimal, because we could be skipping suitable ranges. But I guess nobody cares about that right now -- and at least it's simple.

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#9 lore raw
Quoted 1 line · David Hildenbrand

On 24.09.25 14:01, Michal Hocko wrote:

[...]

Quoted 4 lines
Quoted 2 lines

I thought we have concluded that there is no user expecting a specific alignment. Or have I misunderstood that?

We concluded that for users that don't pass in non-power-2 requests.

Ok, my misreading of the discussion then.

Quoted 2 lines

hugetlb->folio_alloc_gigantic()->alloc_contig_pages_noprof() certainly expects a suitable alignment :)

True that.

So effectivelly there is nothing to be done here right. Those that provide power-two requests will get what they have been getting and those non-existent others will get whatever as we do not provide any guarantee in that case. Maybe something we could add as a comment.

Makes sense?

Signature · 2 lines
Michal Hocko
SUSE Labs
#10 lore raw
Quoted 10 lines · Michal Hocko
Quoted 1 line · David Hildenbrand

On 24.09.25 14:01, Michal Hocko wrote:

[...]

Quoted 4 lines
Quoted 2 lines

I thought we have concluded that there is no user expecting a specific alignment. Or have I misunderstood that?

We concluded that for users that don't pass in non-power-2 requests.

Ok, my misreading of the discussion then.

Quoted 8 lines
Quoted 2 lines

hugetlb->folio_alloc_gigantic()->alloc_contig_pages_noprof() certainly expects a suitable alignment :)

True that.

So effectivelly there is nothing to be done here right. Those that provide power-two requests will get what they have been getting and those non-existent others

I think kfence might be doing a non-power-of-two allocation. TDX maybe as well.

But neither seems to depend on some alignment, so all good.

Quoted 2 lines

will get whatever as we do not provide any
guarantee in that case. Maybe something we could add as a comment.

We currently have

"The allocated memory is always aligned to a page boundary. If nr_pages is a power of two, then allocated range is also guaranteed to be aligned to same nr_pages (e.g. 1GB request would be aligned to 1GB)."

What would be your suggestion?

Signature · 3 lines
Cheers

David / dhildenb
reviewer
#11 lore raw
Quoted 28 lines · David Hildenbrand
Quoted 10 lines · Michal Hocko
Quoted 1 line · David Hildenbrand

On 24.09.25 14:01, Michal Hocko wrote:

[...]

Quoted 4 lines
Quoted 2 lines

I thought we have concluded that there is no user expecting a specific alignment. Or have I misunderstood that?

We concluded that for users that don't pass in non-power-2 requests.

Ok, my misreading of the discussion then.

Quoted 8 lines
Quoted 2 lines

hugetlb->folio_alloc_gigantic()->alloc_contig_pages_noprof() certainly expects a suitable alignment :)

True that.

So effectivelly there is nothing to be done here right. Those that provide power-two requests will get what they have been getting and those non-existent others

I think kfence might be doing a non-power-of-two allocation. TDX maybe as well.

But neither seems to depend on some alignment, so all good.

Quoted 2 lines

will get whatever as we do not provide any
guarantee in that case. Maybe something we could add as a comment.

Quoted 7 lines

We currently have

"The allocated memory is always aligned to a page boundary. If nr_pages is a power of two, then allocated range is also guaranteed to be aligned to same nr_pages (e.g. 1GB request would be aligned to 1GB)."

What would be your suggestion?

Suggestion to self to read through the doc before suggesting editings and suggestion to the patch to not do anything.

Thanks for the patience with me.

Signature · 2 lines
Michal Hocko
SUSE Labs
#12 lore raw
In reply to Wei Yang
Quoted 10 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Use roundup() to calculate the correct alignment.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")

The ALIGN() statement was even before this, but not sure if a potential caller with non-power-of-2 nr_pages was present then.

Quoted 19 lines
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
  mm/page_alloc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index a1bcc1e003c7..a17a6014e3db 100644

mm/page_alloc.c struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · line 7101

Quoted 8 lines
mm/page_alloc.c +1 -1
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
  					gfp_zone(gfp_mask), nodemask) {
  		spin_lock_irqsave(&zone->lock, flags);
 
-		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
  		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
  			if (pfn_range_valid_contig(pfn, nr_pages)) {
  				/*

The kernel-doc above alloc_contig_pages_noprof reads:

"If nr_pages is a power of two, then allocated range is always guaranteed to be aligned to same nr_pages." This means that the function gives no guarantee on alignment of non-power-of-2 requests - and why should it? It doesn't make any sense to align pfn to 7 if nr_pages is 7.

reviewer
#13 lore raw
Quoted 44 lines · Dev Jain
Quoted 10 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Use roundup() to calculate the correct alignment.

Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")

The ALIGN() statement was even before this, but not sure if a potential caller with non-power-of-2 nr_pages was present then.

Quoted 19 lines
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
  mm/page_alloc.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +0 -0
index a1bcc1e003c7..a17a6014e3db 100644

mm/page_alloc.c struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask, · line 7101

Quoted 7 lines
mm/page_alloc.c +1 -0
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
  					gfp_zone(gfp_mask), nodemask) {
  		spin_lock_irqsave(&zone->lock, flags);
 -		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
  		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
  			if (pfn_range_valid_contig(pfn, nr_pages)) {
  				/*

The kernel-doc above alloc_contig_pages_noprof reads:

Quoted 4 lines

"If nr_pages is a power of two, then allocated range is always guaranteed to be aligned to same nr_pages." This means that the function gives no guarantee on alignment of non-power-of-2 requests - and why should it? It doesn't make any sense to align pfn to 7 if nr_pages is 7.

I agree with this. But giving a non power of 2 to ALIGN is unexpected, right? So maybe only use ALIGN for power of 2 nr_pages and leave other cases alone? Or like David suggested in another email, align nr_pages down to power of 2.

Something like:

pfn = ALIGN(zone->zone_start_pfn, rounddown_pow_of_two(nr_pages));

Best Regards,
Yan, Zi

reviewer
#14 lore raw
In reply to Wei Yang
Quoted 8 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Use roundup() to calculate the correct alignment.

What is the problem you are trying to fix here? The ALIGN is certainly not well defined for nr_pages that is not power of two but does any caller of alloc_contig_pages assumes any specific alignment, especially when nr_pages is not power of two?

Quoted 30 lines
Fixes 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()")
Cc Anshuman Khandual <[email protected]>
Cc Oscar Salvador <[email protected]>
Cc Brendan Jackman <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Suren Baghdasaryan <[email protected]>
Cc Vlastimil Babka <[email protected]>
Cc Wei Yang <[email protected]>
Cc Zi Yan <[email protected]>
---
 mm/page_alloc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
mm/page_alloc.c +1 -1
index a1bcc1e003c7..a17a6014e3db 100644
@@ -7095,7 +7095,7 @@ struct page *alloc_contig_pages_noprof(unsigned long nr_pages, gfp_t gfp_mask,
 					gfp_zone(gfp_mask), nodemask) {
 		spin_lock_irqsave(&zone->lock, flags);
 
-		pfn = ALIGN(zone->zone_start_pfn, nr_pages);
+		pfn = roundup(zone->zone_start_pfn, nr_pages);
 		while (zone_spans_last_pfn(zone, pfn, nr_pages)) {
 			if (pfn_range_valid_contig(pfn, nr_pages)) {
 				/*
Signature · 1 line
2.34.1
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#15 lore raw
Quoted 8 lines · Michal Hocko
Quoted 7 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Quoted 7 lines
Quoted 1 line

Use roundup() to calculate the correct alignment.

What is the problem you are trying to fix here? The ALIGN is certainly not well defined for nr_pages that is not power of two but does any caller of alloc_contig_pages assumes any specific alignment, especially when nr_pages is not power of two?

There is no real problem here. The initial thought is ALIGN() don't expect non-power_of_two value.

The discussion above sounds reasonable, we don't need alignment for non-power_of_two nr_pages.

So this looks we can start iteration from zone->zone_start_pfn for non-power_of_two nr_pages.

IMHO, How about the change below?

    pfn = zone->zone_start_pfn;
    if (is_power_of_2(nr_pages))
        pfn = ALIGN(pfn, nr_pages)

And it looks making code consist with the doc above.

Signature · 2 lines
Wei Yang
Help you, Help me
reviewer
#16 lore raw
Quoted 23 lines · Wei Yang
Quoted 8 lines · Michal Hocko
Quoted 7 lines · Wei Yang

Commit 5e27a2df03b8 ("mm/page_alloc: add alloc_contig_pages()") introduced generic method for alloc_contig_pages(). But the alignment calculation seems wrong.

Since ALIGN() only accept power of two value, while nr_pages could be any positive one, the result is not defined.

Quoted 7 lines
Quoted 1 line

Use roundup() to calculate the correct alignment.

What is the problem you are trying to fix here? The ALIGN is certainly not well defined for nr_pages that is not power of two but does any caller of alloc_contig_pages assumes any specific alignment, especially when nr_pages is not power of two?

There is no real problem here. The initial thought is ALIGN() don't expect non-power_of_two value.

The discussion above sounds reasonable, we don't need alignment for non-power_of_two nr_pages.

Quoted 8 lines

So this looks we can start iteration from zone->zone_start_pfn for non-power_of_two nr_pages.

IMHO, How about the change below?

    pfn = zone->zone_start_pfn;
    if (is_power_of_2(nr_pages))
        pfn = ALIGN(pfn, nr_pages)

Why should we do that if we claim there is no alignment enforced and therefore it shouldn't be expected. Something like this would merely create a confusion and subtle bugs in the worst case.

Signature · 2 lines
Michal Hocko
SUSE Labs