[PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()

Kiryl Shutsemau posted 17 patches 4 days, 15 hours ago
[PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()
Posted by Kiryl Shutsemau 4 days, 15 hours ago
If page->compound_info encodes a mask, it is expected that vmemmap to be
naturally aligned to the maximum folio size.

Add a VM_BUG_ON() to check the alignment.

Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Acked-by: Zi Yan <ziy@nvidia.com>
---
 mm/sparse.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/sparse.c b/mm/sparse.c
index b5b2b6f7041b..6c9b62607f3f 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -600,6 +600,13 @@ void __init sparse_init(void)
 	BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
 	memblocks_present();
 
+	if (compound_info_has_mask()) {
+		unsigned long alignment;
+
+		alignment = MAX_FOLIO_NR_PAGES * sizeof(struct page);
+		VM_BUG_ON(!IS_ALIGNED((unsigned long) pfn_to_page(0), alignment));
+	}
+
 	pnum_begin = first_present_section_nr();
 	nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));
 
-- 
2.51.2
Re: [PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()
Posted by David Hildenbrand (Arm) 1 day, 17 hours ago
On 2/2/26 16:56, Kiryl Shutsemau wrote:
> If page->compound_info encodes a mask, it is expected that vmemmap to be
> naturally aligned to the maximum folio size.
> 
> Add a VM_BUG_ON() to check the alignment.
> 
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> Acked-by: Zi Yan <ziy@nvidia.com>
> ---
>   mm/sparse.c | 7 +++++++
>   1 file changed, 7 insertions(+)
> 
> diff --git a/mm/sparse.c b/mm/sparse.c
> index b5b2b6f7041b..6c9b62607f3f 100644
> --- a/mm/sparse.c
> +++ b/mm/sparse.c
> @@ -600,6 +600,13 @@ void __init sparse_init(void)
>   	BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
>   	memblocks_present();
>   
> +	if (compound_info_has_mask()) {
> +		unsigned long alignment;
> +
> +		alignment = MAX_FOLIO_NR_PAGES * sizeof(struct page);
> +		VM_BUG_ON(!IS_ALIGNED((unsigned long) pfn_to_page(0), alignment));

No VM_BUG_ON. VM_WARN_ON_ONCE() should be good enough, no?

As discussed in the other thread, is checking for MAX_FOLIO_NR_PAGES 
alignment sufficient?

-- 
Cheers,

David
Re: [PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()
Posted by David Hildenbrand (Arm) 1 day, 17 hours ago
On 2/5/26 14:31, David Hildenbrand (Arm) wrote:
> On 2/2/26 16:56, Kiryl Shutsemau wrote:
>> If page->compound_info encodes a mask, it is expected that vmemmap to be
>> naturally aligned to the maximum folio size.
>>
>> Add a VM_BUG_ON() to check the alignment.
>>
>> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
>> Acked-by: Zi Yan <ziy@nvidia.com>
>> ---
>>   mm/sparse.c | 7 +++++++
>>   1 file changed, 7 insertions(+)
>>
>> diff --git a/mm/sparse.c b/mm/sparse.c
>> index b5b2b6f7041b..6c9b62607f3f 100644
>> --- a/mm/sparse.c
>> +++ b/mm/sparse.c
>> @@ -600,6 +600,13 @@ void __init sparse_init(void)
>>       BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
>>       memblocks_present();
>> +    if (compound_info_has_mask()) {
>> +        unsigned long alignment;
>> +
>> +        alignment = MAX_FOLIO_NR_PAGES * sizeof(struct page);
>> +        VM_BUG_ON(!IS_ALIGNED((unsigned long) pfn_to_page(0), 
>> alignment));
> 
> No VM_BUG_ON. VM_WARN_ON_ONCE() should be good enough, no?
> 
> As discussed in the other thread, is checking for MAX_FOLIO_NR_PAGES 
> alignment sufficient?

And after further discussions, we could use MAX_FOLIO_VMEMMAP_ALIGN 
macro once we have that.

-- 
Cheers,

David
Re: [PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()
Posted by Muchun Song 4 days, 3 hours ago

> On Feb 2, 2026, at 23:56, Kiryl Shutsemau <kas@kernel.org> wrote:
> 
> If page->compound_info encodes a mask, it is expected that vmemmap to be
> naturally aligned to the maximum folio size.
> 
> Add a VM_BUG_ON() to check the alignment.
> 
> Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
> Acked-by: Zi Yan <ziy@nvidia.com>

Reviewed-by: Muchun Song <muchun.song@linux.dev>