linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()
@ 2024-12-10  9:34 David Hildenbrand
  2024-12-10  9:48 ` Vlastimil Babka
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: David Hildenbrand @ 2024-12-10  9:34 UTC (permalink / raw)
  To: linux-kernel
  Cc: linux-mm, David Hildenbrand, Vlastimil Babka, Andrew Morton,
	Johannes Weiner, Zi Yan, Yu Zhao, stable

In split_large_buddy(), we might call pfn_to_page() on a PFN that might
not exist. In corner cases, such as when freeing the highest pageblock in
the last memory section, this could result with CONFIG_SPARSEMEM &&
!CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and
and __section_mem_map_addr() dereferencing that NULL pointer.

Let's fix it, and avoid doing a pfn_to_page() call for the first
iteration, where we already have the page.

So far this was found by code inspection, but let's just CC stable as
the fix is easy.

Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
Reported-by: Vlastimil Babka <vbabka@suse.cz>
Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Zi Yan <ziy@nvidia.com>
Cc: Yu Zhao <yuzhao@google.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: David Hildenbrand <david@redhat.com>
---
 mm/page_alloc.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 48a291c485df4..a52c6022c65cb 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1238,13 +1238,15 @@ static void split_large_buddy(struct zone *zone, struct page *page,
 	if (order > pageblock_order)
 		order = pageblock_order;
 
-	while (pfn != end) {
+	do {
 		int mt = get_pfnblock_migratetype(page, pfn);
 
 		__free_one_page(page, pfn, zone, order, mt, fpi);
 		pfn += 1 << order;
+		if (pfn == end)
+			break;
 		page = pfn_to_page(pfn);
-	}
+	} while (1);
 }
 
 static void free_one_page(struct zone *zone, struct page *page,
-- 
2.47.1



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()
  2024-12-10  9:34 [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy() David Hildenbrand
@ 2024-12-10  9:48 ` Vlastimil Babka
  2024-12-10 10:10   ` David Hildenbrand
  2024-12-10 16:17 ` Zi Yan
  2024-12-10 19:25 ` Johannes Weiner
  2 siblings, 1 reply; 5+ messages in thread
From: Vlastimil Babka @ 2024-12-10  9:48 UTC (permalink / raw)
  To: David Hildenbrand, linux-kernel
  Cc: linux-mm, Andrew Morton, Johannes Weiner, Zi Yan, Yu Zhao, stable

On 12/10/24 10:34, David Hildenbrand wrote:
> In split_large_buddy(), we might call pfn_to_page() on a PFN that might
> not exist. In corner cases, such as when freeing the highest pageblock in
> the last memory section, this could result with CONFIG_SPARSEMEM &&
> !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and
> and __section_mem_map_addr() dereferencing that NULL pointer.
> 
> Let's fix it, and avoid doing a pfn_to_page() call for the first
> iteration, where we already have the page.
> 
> So far this was found by code inspection, but let's just CC stable as
> the fix is easy.
> 
> Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
> Reported-by: Vlastimil Babka <vbabka@suse.cz>
> Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Yu Zhao <yuzhao@google.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

Thanks!

> ---
>  mm/page_alloc.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 48a291c485df4..a52c6022c65cb 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -1238,13 +1238,15 @@ static void split_large_buddy(struct zone *zone, struct page *page,
>  	if (order > pageblock_order)
>  		order = pageblock_order;
>  
> -	while (pfn != end) {
> +	do {
>  		int mt = get_pfnblock_migratetype(page, pfn);
>  
>  		__free_one_page(page, pfn, zone, order, mt, fpi);
>  		pfn += 1 << order;
> +		if (pfn == end)
> +			break;
>  		page = pfn_to_page(pfn);
> -	}
> +	} while (1);
>  }
>  
>  static void free_one_page(struct zone *zone, struct page *page,



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()
  2024-12-10  9:48 ` Vlastimil Babka
@ 2024-12-10 10:10   ` David Hildenbrand
  0 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2024-12-10 10:10 UTC (permalink / raw)
  To: Vlastimil Babka, linux-kernel
  Cc: linux-mm, Andrew Morton, Johannes Weiner, Zi Yan, Yu Zhao, stable

On 10.12.24 10:48, Vlastimil Babka wrote:
> On 12/10/24 10:34, David Hildenbrand wrote:
>> In split_large_buddy(), we might call pfn_to_page() on a PFN that might
>> not exist. In corner cases, such as when freeing the highest pageblock in
>> the last memory section, this could result with CONFIG_SPARSEMEM &&
>> !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and
>> and __section_mem_map_addr() dereferencing that NULL pointer.
>>
>> Let's fix it, and avoid doing a pfn_to_page() call for the first
>> iteration, where we already have the page.
>>
>> So far this was found by code inspection, but let's just CC stable as
>> the fix is easy.
>>
>> Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
>> Reported-by: Vlastimil Babka <vbabka@suse.cz>
>> Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
>> Cc: Andrew Morton <akpm@linux-foundation.org>
>> Cc: Johannes Weiner <hannes@cmpxchg.org>
>> Cc: Zi Yan <ziy@nvidia.com>
>> Cc: Yu Zhao <yuzhao@google.com>
>> Cc: <stable@vger.kernel.org>
>> Signed-off-by: David Hildenbrand <david@redhat.com>
> 
> Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

BTW, staring at nr_isolate_pageblock accounting, I just stumbled over 
prep_move_freepages_block(), and I am not sure about the 
zone_spans_pfn() checks in there.

With overlapping zones, these are not reliably, which makes me believe 
that maybe these checks are not required at all. Or that there is a bug. 
Or that there is some implication that these checks are only required on 
systems without overlapping zones :)

-- 
Cheers,

David / dhildenb



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()
  2024-12-10  9:34 [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy() David Hildenbrand
  2024-12-10  9:48 ` Vlastimil Babka
@ 2024-12-10 16:17 ` Zi Yan
  2024-12-10 19:25 ` Johannes Weiner
  2 siblings, 0 replies; 5+ messages in thread
From: Zi Yan @ 2024-12-10 16:17 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: linux-kernel, linux-mm, Vlastimil Babka, Andrew Morton,
	Johannes Weiner, Yu Zhao, stable

On 10 Dec 2024, at 4:34, David Hildenbrand wrote:

> In split_large_buddy(), we might call pfn_to_page() on a PFN that might
> not exist. In corner cases, such as when freeing the highest pageblock in
> the last memory section, this could result with CONFIG_SPARSEMEM &&
> !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and
> and __section_mem_map_addr() dereferencing that NULL pointer.
>
> Let's fix it, and avoid doing a pfn_to_page() call for the first
> iteration, where we already have the page.
>
> So far this was found by code inspection, but let's just CC stable as
> the fix is easy.
>
> Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
> Reported-by: Vlastimil Babka <vbabka@suse.cz>
> Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Yu Zhao <yuzhao@google.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>
Reviewed-by: Zi Yan <ziy@nvidia.com>

Best Regards,
Yan, Zi


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy()
  2024-12-10  9:34 [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy() David Hildenbrand
  2024-12-10  9:48 ` Vlastimil Babka
  2024-12-10 16:17 ` Zi Yan
@ 2024-12-10 19:25 ` Johannes Weiner
  2 siblings, 0 replies; 5+ messages in thread
From: Johannes Weiner @ 2024-12-10 19:25 UTC (permalink / raw)
  To: David Hildenbrand
  Cc: linux-kernel, linux-mm, Vlastimil Babka, Andrew Morton, Zi Yan,
	Yu Zhao, stable

On Tue, Dec 10, 2024 at 10:34:37AM +0100, David Hildenbrand wrote:
> In split_large_buddy(), we might call pfn_to_page() on a PFN that might
> not exist. In corner cases, such as when freeing the highest pageblock in
> the last memory section, this could result with CONFIG_SPARSEMEM &&
> !CONFIG_SPARSEMEM_EXTREME in __pfn_to_section() returning NULL and
> and __section_mem_map_addr() dereferencing that NULL pointer.
> 
> Let's fix it, and avoid doing a pfn_to_page() call for the first
> iteration, where we already have the page.
> 
> So far this was found by code inspection, but let's just CC stable as
> the fix is easy.
> 
> Fixes: fd919a85cd55 ("mm: page_isolation: prepare for hygienic freelists")
> Reported-by: Vlastimil Babka <vbabka@suse.cz>
> Closes: https://lkml.kernel.org/r/e1a898ba-a717-4d20-9144-29df1a6c8813@suse.cz
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Johannes Weiner <hannes@cmpxchg.org>
> Cc: Zi Yan <ziy@nvidia.com>
> Cc: Yu Zhao <yuzhao@google.com>
> Cc: <stable@vger.kernel.org>
> Signed-off-by: David Hildenbrand <david@redhat.com>

Acked-by: Johannes Weiner <hannes@cmpxchg.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-12-10 19:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-10  9:34 [PATCH v1] mm/page_alloc: don't call pfn_to_page() on possibly non-existent PFN in split_large_buddy() David Hildenbrand
2024-12-10  9:48 ` Vlastimil Babka
2024-12-10 10:10   ` David Hildenbrand
2024-12-10 16:17 ` Zi Yan
2024-12-10 19:25 ` Johannes Weiner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox