* [PATCH] mm/hugetlb: copy the CMA flag when demoting
@ 2025-05-01 4:43 Frank van der Linden
2025-05-02 7:17 ` Oscar Salvador
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Frank van der Linden @ 2025-05-01 4:43 UTC (permalink / raw)
To: akpm, muchun.song, linux-mm; +Cc: osalvador, Frank van der Linden
Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation
from CMA areas"), a flag is used to mark hugetlb folios as allocated
from CMA. This flag is also used to decide if it should be freed
to CMA.
However, the flag isn't copied to the smaller folios when a hugetlb
folio is broken up for demotion, which would cause it to be freed
incorrectly.
Fix this by copying the flag to the smaller order hugetlb pages
created from the original one.
Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
Signed-off-by: Frank van der Linden <fvdl@google.com>
---
mm/hugetlb.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/mm/hugetlb.c b/mm/hugetlb.c
index e3e6ac991b9c..6ea1be71aa42 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
list_for_each_entry_safe(folio, next, src_list, lru) {
int i;
+ bool cma;
if (folio_test_hugetlb_vmemmap_optimized(folio))
continue;
+ cma = folio_test_hugetlb_cma(folio);
+
list_del(&folio->lru);
split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
@@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
new_folio->mapping = NULL;
init_new_hugetlb_folio(dst, new_folio);
+ /* Copy the CMA flag so that it is freed correctly */
+ if (cma)
+ folio_set_hugetlb_cma(new_folio);
list_add(&new_folio->lru, &dst_list);
}
}
--
2.49.0.967.g6a0df3ecc3-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/hugetlb: copy the CMA flag when demoting
2025-05-01 4:43 [PATCH] mm/hugetlb: copy the CMA flag when demoting Frank van der Linden
@ 2025-05-02 7:17 ` Oscar Salvador
2025-05-02 8:27 ` Anshuman Khandual
2025-05-05 17:02 ` jane.chu
2 siblings, 0 replies; 5+ messages in thread
From: Oscar Salvador @ 2025-05-02 7:17 UTC (permalink / raw)
To: Frank van der Linden; +Cc: akpm, muchun.song, linux-mm
On Thu, May 01, 2025 at 04:43:24AM +0000, Frank van der Linden wrote:
> Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation
> from CMA areas"), a flag is used to mark hugetlb folios as allocated
> from CMA. This flag is also used to decide if it should be freed
> to CMA.
>
> However, the flag isn't copied to the smaller folios when a hugetlb
> folio is broken up for demotion, which would cause it to be freed
> incorrectly.
>
> Fix this by copying the flag to the smaller order hugetlb pages
> created from the original one.
>
> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> Signed-off-by: Frank van der Linden <fvdl@google.com>
Hi Frank,
Reviewed-by: Oscar Salvador <osalvador@suse.de>
By the way, AFAICS we are missing some docs for HPG_cma flag in include/linux/hugetlb.h.
Could you please add it :-P?
Thanks!
--
Oscar Salvador
SUSE Labs
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/hugetlb: copy the CMA flag when demoting
2025-05-01 4:43 [PATCH] mm/hugetlb: copy the CMA flag when demoting Frank van der Linden
2025-05-02 7:17 ` Oscar Salvador
@ 2025-05-02 8:27 ` Anshuman Khandual
2025-05-02 16:23 ` Frank van der Linden
2025-05-05 17:02 ` jane.chu
2 siblings, 1 reply; 5+ messages in thread
From: Anshuman Khandual @ 2025-05-02 8:27 UTC (permalink / raw)
To: Frank van der Linden, akpm, muchun.song, linux-mm; +Cc: osalvador
On 5/1/25 10:13, Frank van der Linden wrote:
> Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation
> from CMA areas"), a flag is used to mark hugetlb folios as allocated
> from CMA. This flag is also used to decide if it should be freed
> to CMA.
>
> However, the flag isn't copied to the smaller folios when a hugetlb
> folio is broken up for demotion, which would cause it to be freed
> incorrectly.
>
> Fix this by copying the flag to the smaller order hugetlb pages
> created from the original one.
>
> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> Signed-off-by: Frank van der Linden <fvdl@google.com>
> ---
> mm/hugetlb.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index e3e6ac991b9c..6ea1be71aa42 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
>
> list_for_each_entry_safe(folio, next, src_list, lru) {
> int i;
> + bool cma;
>
> if (folio_test_hugetlb_vmemmap_optimized(folio))
> continue;
>
> + cma = folio_test_hugetlb_cma(folio);
> +
> list_del(&folio->lru);
>
> split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
> @@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
>
> new_folio->mapping = NULL;
> init_new_hugetlb_folio(dst, new_folio);
> + /* Copy the CMA flag so that it is freed correctly */
> + if (cma)
> + folio_set_hugetlb_cma(new_folio);
> list_add(&new_folio->lru, &dst_list);
> }
> }
Agree with Oscar that HPG_cma need documentation like other flags.
Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/hugetlb: copy the CMA flag when demoting
2025-05-02 8:27 ` Anshuman Khandual
@ 2025-05-02 16:23 ` Frank van der Linden
0 siblings, 0 replies; 5+ messages in thread
From: Frank van der Linden @ 2025-05-02 16:23 UTC (permalink / raw)
To: Anshuman Khandual; +Cc: akpm, muchun.song, linux-mm, osalvador
On Fri, May 2, 2025 at 1:27 AM Anshuman Khandual
<anshuman.khandual@arm.com> wrote:
>
>
>
> On 5/1/25 10:13, Frank van der Linden wrote:
> > Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation
> > from CMA areas"), a flag is used to mark hugetlb folios as allocated
> > from CMA. This flag is also used to decide if it should be freed
> > to CMA.
> >
> > However, the flag isn't copied to the smaller folios when a hugetlb
> > folio is broken up for demotion, which would cause it to be freed
> > incorrectly.
> >
> > Fix this by copying the flag to the smaller order hugetlb pages
> > created from the original one.
> >
> > Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> > Signed-off-by: Frank van der Linden <fvdl@google.com>
> > ---
> > mm/hugetlb.c | 6 ++++++
> > 1 file changed, 6 insertions(+)
> >
> > diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> > index e3e6ac991b9c..6ea1be71aa42 100644
> > --- a/mm/hugetlb.c
> > +++ b/mm/hugetlb.c
> > @@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
> >
> > list_for_each_entry_safe(folio, next, src_list, lru) {
> > int i;
> > + bool cma;
> >
> > if (folio_test_hugetlb_vmemmap_optimized(folio))
> > continue;
> >
> > + cma = folio_test_hugetlb_cma(folio);
> > +
> > list_del(&folio->lru);
> >
> > split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
> > @@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
> >
> > new_folio->mapping = NULL;
> > init_new_hugetlb_folio(dst, new_folio);
> > + /* Copy the CMA flag so that it is freed correctly */
> > + if (cma)
> > + folio_set_hugetlb_cma(new_folio);
> > list_add(&new_folio->lru, &dst_list);
> > }
> > }
>
> Agree with Oscar that HPG_cma need documentation like other flags.
>
> Reviewed-by: Anshuman Khandual <anshuman.khandual@arm.com>
Thanks for the reviews! I'll send a patch to document the flag in the
near future.
- Frank
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm/hugetlb: copy the CMA flag when demoting
2025-05-01 4:43 [PATCH] mm/hugetlb: copy the CMA flag when demoting Frank van der Linden
2025-05-02 7:17 ` Oscar Salvador
2025-05-02 8:27 ` Anshuman Khandual
@ 2025-05-05 17:02 ` jane.chu
2 siblings, 0 replies; 5+ messages in thread
From: jane.chu @ 2025-05-05 17:02 UTC (permalink / raw)
To: Frank van der Linden, akpm, muchun.song, linux-mm; +Cc: osalvador
On 4/30/2025 9:43 PM, Frank van der Linden wrote:
> Since commit d2d786714080 ("mm/hugetlb: enable bootmem allocation
> from CMA areas"), a flag is used to mark hugetlb folios as allocated
> from CMA. This flag is also used to decide if it should be freed
> to CMA.
>
> However, the flag isn't copied to the smaller folios when a hugetlb
> folio is broken up for demotion, which would cause it to be freed
> incorrectly.
>
> Fix this by copying the flag to the smaller order hugetlb pages
> created from the original one.
>
> Fixes: d2d786714080 ("mm/hugetlb: enable bootmem allocation from CMA areas")
> Signed-off-by: Frank van der Linden <fvdl@google.com>
> ---
> mm/hugetlb.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/mm/hugetlb.c b/mm/hugetlb.c
> index e3e6ac991b9c..6ea1be71aa42 100644
> --- a/mm/hugetlb.c
> +++ b/mm/hugetlb.c
> @@ -4034,10 +4034,13 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
>
> list_for_each_entry_safe(folio, next, src_list, lru) {
> int i;
> + bool cma;
>
> if (folio_test_hugetlb_vmemmap_optimized(folio))
> continue;
>
> + cma = folio_test_hugetlb_cma(folio);
> +
> list_del(&folio->lru);
>
> split_page_owner(&folio->page, huge_page_order(src), huge_page_order(dst));
> @@ -4053,6 +4056,9 @@ static long demote_free_hugetlb_folios(struct hstate *src, struct hstate *dst,
>
> new_folio->mapping = NULL;
> init_new_hugetlb_folio(dst, new_folio);
> + /* Copy the CMA flag so that it is freed correctly */
> + if (cma)
> + folio_set_hugetlb_cma(new_folio);
> list_add(&new_folio->lru, &dst_list);
> }
> }
Looks good.
Reviewed-by: Jane Chu <Jane.Chu@oracle.com>
thanks,
-jane
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-05-05 17:03 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-01 4:43 [PATCH] mm/hugetlb: copy the CMA flag when demoting Frank van der Linden
2025-05-02 7:17 ` Oscar Salvador
2025-05-02 8:27 ` Anshuman Khandual
2025-05-02 16:23 ` Frank van der Linden
2025-05-05 17:02 ` jane.chu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox