mm/codetag: sub in advance when free non-compound high order pages
PATCH 1/1 mm/codetag: sub in advance when free non-compound high order pages
When page is non-compound, page[0] could be released by other thread right after put_page_testzero failed in current thread, pgalloc_tag_sub_pages afterwards would manipulate an invalid page for accounting remaining pages:
[timeline] [thread1] [thread2]
| alloc_page non-compound
V
| get_page, rf counter inc
V
| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
V
V
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
--- mm/page_alloc.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-)
mm/page_alloc.c +33 -3
@@ -1163,12 +1163,25 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); } +static inline void pgalloc_tag_add_pages (struct page *page,unsigned int nr) +{ +struct alloc_tag *tag; + +if (!mem_alloc_profiling_enabled ()) +return ; + + tag =__pgalloc_tag_get (page); +if (tag) +this_cpu_add (tag->counters->bytes,PAGE_SIZE * nr); +} +#else /* CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {}static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_add_pages (struct page *page,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */
@@ -5065,11 +5078,28 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* +* For remaining pages other than the first page of +* a non-compound allocation, we decrease its tag +* pages in advance, in case the first page is released +* by other thread inbetween our put_page_testzero and any +* accounting behavior afterwards. +*/ +unsigned int remaining_tag_pages =0 ; -if (put_page_testzero (page)) +if (order >0 && !head) { +if (unlikely (page_ref_count (page) >1 )) { + remaining_tag_pages = (1 << order) -1 ; +pgalloc_tag_sub_pages (page, remaining_tag_pages); + } + } + +if (put_page_testzero (page)) { +/* no need special treat for remaining pages, add it back. */ +if (unlikely (remaining_tag_pages >0 )) +pgalloc_tag_add_pages (page, remaining_tag_pages);__free_frozen_pages (page, order, fpi_flags); -else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); + }else if (!head) {while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);
Signature · 1 line
2.39.2
Quoted 18 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
Quoted 7 lines
V
VMove the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>
Hmm, I think the problem was introduced by 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled() checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.
It would likely be nicer to fix it by going back to that approach for ___free_pages(), while hopefully keeping the optimisations of 51ff4d7486f0 for the other call sites where it applies?
Quoted 66 lines
--- mm/page_alloc.c | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-)mm/page_alloc.c +33
-3@@ -1163,12 +1163,25 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); } +static inline void pgalloc_tag_add_pages (struct page *page,unsigned int nr) +{ +struct alloc_tag *tag; + +if (!mem_alloc_profiling_enabled ()) +return ; + + tag =__pgalloc_tag_get (page); +if (tag) +this_cpu_add (tag->counters->bytes,PAGE_SIZE * nr); +} +#else /* CONFIG_MEM_ALLOC_PROFILING */ static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {}static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_add_pages (struct page *page,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */ @@ -5065,11 +5078,28 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* +* For remaining pages other than the first page of +* a non-compound allocation, we decrease its tag +* pages in advance, in case the first page is released +* by other thread inbetween our put_page_testzero and any +* accounting behavior afterwards. +*/ +unsigned int remaining_tag_pages =0 ; -if (put_page_testzero (page)) +if (order >0 && !head) { +if (unlikely (page_ref_count (page) >1 )) { + remaining_tag_pages = (1 << order) -1 ; +pgalloc_tag_sub_pages (page, remaining_tag_pages); + } + } + +if (put_page_testzero (page)) { +/* no need special treat for remaining pages, add it back. */ +if (unlikely (remaining_tag_pages >0 )) +pgalloc_tag_add_pages (page, remaining_tag_pages);__free_frozen_pages (page, order, fpi_flags); -else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); + }else if (!head) {while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
V
Quoted 8 lines
Quoted 4 lines
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>Hmm, I think the problem was introduced by
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.
Oh, you're right. I forgot to check history......
Quoted 4 lines
It would likely be nicer to fix it by going back to that approach for
___free_pages(), while hopefully keeping the optimisations of51ff4d7486f0for the other call sites where it applies?
After checking that commit, I kind of feels the changes in __free_pages are
the major optimization of the commit....
What about revert that commit and make optimization by condition checks, similar to what this patch did?
David
Quoted 39 lines · David Wang
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
VQuoted 8 lines
Quoted 4 lines
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>Hmm, I think the problem was introduced by
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.Oh, you're right. I forgot to check history......
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
It would likely be nicer to fix it by going back to that approach for
___free_pages(), while hopefully keeping the optimisations of51ff4d7486f0for the other call sites where it applies?After checking that commit, I kind of feels the changes in
__free_pagesare
the major optimization of the commit....
We could have both pgalloc_tag_get() to use in __free_page() as before 51ff4d7486f0, and keep __pgalloc_tag_get() to use in pgalloc_tag_split() and pgalloc_tag_swap().
I think __free_page() didn't benefit from the stated purpose of "avoiding mem_alloc_profiling_enabled() ... which is often called after that check was already done"
Quoted 2 lines
What about revert that commit and make optimization by condition checks, similar to what this patch did?
The downside of the condition checks is they make the code more complex and might actually increase overhead when mem_alloc_profiling_enabled() is false, as those checks add non-static branches outside of the static branch that's mem_alloc_profiling_enabled().
I think __free_pages() before 51ff4d7486f0 was quite ok.
- pgalloc_tag_get() is done unconditionally, but its code is all inside the mem_alloc_profiling_enabled() static branch so that's a no-op when profiling is not enabled
- pgalloc_tag_sub_pages() is also all behind the static branch inside. Also it's a very rare path anyway, most freeing should go through the put_page_testzero() being true.
Quoted 2 lines
David
Suren Baghdasaryan
maintainerQuoted 27 lines · Vlastimil Babka
Quoted 25 lines · David Wang
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
V
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>
Thanks for reporting!
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Hmm, I think the problem was introduced by
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.
Right, sorry I missed that.
Quoted 10 lines
Quoted 10 lines
Oh, you're right. I forgot to check history......
Quoted 4 lines
It would likely be nicer to fix it by going back to that approach for
___free_pages(), while hopefully keeping the optimisations of51ff4d7486f0for the other call sites where it applies?
Quoted 6 lines
Quoted 2 lines
After checking that commit, I kind of feels the changes in
__free_pagesare
the major optimization of the commit....We could have both
pgalloc_tag_get()to use in__free_page()as before51ff4d7486f0, and keep__pgalloc_tag_get()to use inpgalloc_tag_split()andpgalloc_tag_swap().
Yes, we can add back pgalloc_tag_get() which would call __pgalloc_tag_get() if mem_alloc_profiling_enabled() is true and change pgalloc_tag_sub_pages() back to use tags instead of pages. __free_pages() is the only user of that function, so that change should not affect anything else.
Quoted 15 lines
I think
__free_page()didn't benefit from the stated purpose of "avoidingmem_alloc_profiling_enabled()... which is often called after that check was already done"Quoted 2 lines
What about revert that commit and make optimization by condition checks, similar to what this patch did?
The downside of the condition checks is they make the code more complex and might actually increase overhead when
mem_alloc_profiling_enabled()is false, as those checks add non-static branches outside of the static branch that'smem_alloc_profiling_enabled().I think
__free_pages()before51ff4d7486f0was quite ok.
Quoted 7 lines
-
pgalloc_tag_get()is done unconditionally, but its code is all inside themem_alloc_profiling_enabled()static branch so that's a no-op when profiling is not enabled-
pgalloc_tag_sub_pages()is also all behind the static branch inside. Also it's a very rare path anyway, most freeing should go through theput_page_testzero()being true.
Yeah, the main goal of that change in __free_page() was to make __pgalloc_tag_get() a local function for alloc_tags and limiting the direct use of struct alloc_tag in the core mm code. Obviously I screwed up forgetting why we had to store the tag in the first place. An additional comment in __free_page() is probably a good idea to avoid confusion in the future.
Thanks,
Suren.
Quoted 4 lines
Quoted 3 lines
David
Quoted 59 lines · Suren Baghdasaryan
Quoted 27 lines · Vlastimil Babka
Quoted 25 lines · David Wang
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
VQuoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>Thanks for reporting!
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Hmm, I think the problem was introduced by
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.Right, sorry I missed that.
Quoted 10 lines
Quoted 10 lines
Oh, you're right. I forgot to check history......
Quoted 4 lines
It would likely be nicer to fix it by going back to that approach for
___free_pages(), while hopefully keeping the optimisations of51ff4d7486f0for the other call sites where it applies?Quoted 6 lines
Quoted 2 lines
After checking that commit, I kind of feels the changes in
__free_pagesare
the major optimization of the commit....We could have both
pgalloc_tag_get()to use in__free_page()as before51ff4d7486f0, and keep__pgalloc_tag_get()to use inpgalloc_tag_split()andpgalloc_tag_swap().
Quoted 5 lines
Yes, we can add back
pgalloc_tag_get()which would call__pgalloc_tag_get()ifmem_alloc_profiling_enabled()is true and changepgalloc_tag_sub_pages()back to use tags instead of pages.__free_pages()is the only user of that function, so that change should not affect anything else.
Adding back pgalloc_tag_get() seems just reverting 51ff4d7486f0..... Do you want me to do it or you take over and make further adjustments?
Quoted 37 lines
Quoted 16 lines
I think
__free_page()didn't benefit from the stated purpose of "avoidingmem_alloc_profiling_enabled()... which is often called after that check was already done"Quoted 2 lines
What about revert that commit and make optimization by condition checks, similar to what this patch did?
The downside of the condition checks is they make the code more complex and might actually increase overhead when
mem_alloc_profiling_enabled()is false, as those checks add non-static branches outside of the static branch that'smem_alloc_profiling_enabled().I think
__free_pages()before51ff4d7486f0was quite ok.Quoted 7 lines
-
pgalloc_tag_get()is done unconditionally, but its code is all inside themem_alloc_profiling_enabled()static branch so that's a no-op when profiling is not enabled-
pgalloc_tag_sub_pages()is also all behind the static branch inside. Also it's a very rare path anyway, most freeing should go through theput_page_testzero()being true.Yeah, the main goal of that change in
__free_page()was to make__pgalloc_tag_get()a local function foralloc_tagsand limiting the direct use of structalloc_tagin the core mm code. Obviously I screwed up forgetting why we had to store the tag in the first place. An additional comment in__free_page()is probably a good idea to avoid confusion in the future.
Thanks,
Suren.Quoted 4 lines
Quoted 3 lines
David
Suren Baghdasaryan
maintainerQuoted 64 lines · David Wang
Quoted 59 lines · Suren Baghdasaryan
Quoted 27 lines · Vlastimil Babka
Quoted 25 lines · David Wang
Quoted 22 lines · Vlastimil Babka
Quoted 21 lines · David Wang
When page is non-compound, page[0] could be released by other thread right after
put_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
VQuoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Move the tag page accounting ahead, and only account remaining pages for non-compound pages with non-zero order.
Signed-off-by David Wang <[email protected]>Thanks for reporting!
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Hmm, I think the problem was introduced by
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks"). Previously we'd get the tag pointer upfront and avoid the page use-after-free.Right, sorry I missed that.
Quoted 16 lines
Quoted 6 lines
Oh, you're right. I forgot to check history......
Quoted 1 line
Quoted 6 lines
Quoted 3 lines
It would likely be nicer to fix it by going back to that approach for
___free_pages(), while hopefully keeping the optimisations of51ff4d7486f0for the other call sites where it applies?After checking that commit, I kind of feels the changes in
__free_pagesare
the major optimization of the commit....We could have both
pgalloc_tag_get()to use in__free_page()as before51ff4d7486f0, and keep__pgalloc_tag_get()to use inpgalloc_tag_split()andpgalloc_tag_swap().
Quoted 8 lines
Quoted 5 lines
Yes, we can add back
pgalloc_tag_get()which would call__pgalloc_tag_get()ifmem_alloc_profiling_enabled()is true and changepgalloc_tag_sub_pages()back to use tags instead of pages.__free_pages()is the only user of that function, so that change should not affect anything else.Adding back
pgalloc_tag_get()seems just reverting51ff4d7486f0.....
Not quite. pgalloc_tag_split() and pgalloc_tag_swap() will still be using __pgalloc_tag_get(), so would avoid the extra checks.
Quoted 1 line
Do you want me to do it or you take over and make further adjustments?
The change I suggested should be simple, so take a stab at it and I'll review and ack. If you prefer me to make the change, let me know.
Quoted 40 lines
Quoted 40 lines
Quoted 19 lines
I think
__free_page()didn't benefit from the stated purpose of "avoidingmem_alloc_profiling_enabled()... which is often called after that check was already done"Quoted 2 lines
What about revert that commit and make optimization by condition checks, similar to what this patch did?
The downside of the condition checks is they make the code more complex and might actually increase overhead when
mem_alloc_profiling_enabled()is false, as those checks add non-static branches outside of the static branch that'smem_alloc_profiling_enabled().I think
__free_pages()before51ff4d7486f0was quite ok.Quoted 7 lines
-
pgalloc_tag_get()is done unconditionally, but its code is all inside themem_alloc_profiling_enabled()static branch so that's a no-op when profiling is not enabled-
pgalloc_tag_sub_pages()is also all behind the static branch inside. Also it's a very rare path anyway, most freeing should go through theput_page_testzero()being true.Yeah, the main goal of that change in
__free_page()was to make__pgalloc_tag_get()a local function foralloc_tagsand limiting the direct use of structalloc_tagin the core mm code. Obviously I screwed up forgetting why we had to store the tag in the first place. An additional comment in__free_page()is probably a good idea to avoid confusion in the future.
Thanks,
Suren.Quoted 4 lines
Quoted 3 lines
David
PATCH v2 1/1 mm/codetag: move tag retrieval back upfront in __free_pages()
Commit 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled()
checks") introduces a possible use-after-free scenario, when page
is non-compound, page[0] could be released by other thread right after put_page_testzero failed in current thread, pgalloc_tag_sub_pages afterwards would manipulate an invalid page for accounting remaining pages:
[timeline] [thread1] [thread2]
| alloc_page non-compound
V
| get_page, rf counter inc
V
| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
V
Restore __free_pages() to its state before, retrieve alloc tag beforehand.
--- include/linux/pgalloc_tag.h | 7 +++++++ mm/page_alloc.c | 15 ++++++--------- 2 files changed, 13 insertions(+), 9 deletions(-)
include/linux/pgalloc_tag.h +7 -0
@@ -188,6 +188,13 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page)return tag; } +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) +{ +if (mem_alloc_profiling_enabled ()) +return __pgalloc_tag_get (page); +return NULL ; +} +void pgalloc_tag_split (struct folio *folio,int old_order,int new_order);void pgalloc_tag_swap (struct folio *new,struct folio *old);
mm/page_alloc.c +6 -9
@@ -1151,14 +1151,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)__pgalloc_tag_sub (page, nr); } -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) +/* When tag is not NULL, assuming mem_alloc_profiling_enabled */ +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) { -struct alloc_tag *tag; - -if (!mem_alloc_profiling_enabled ()) -return ; - - tag =__pgalloc_tag_get (page);if (tag)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); }
@@ -1168,7 +1163,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {} -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */
@@ -5065,11 +5060,13 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* get alloc tag in case the page is released by others */ +struct alloc_tag *tag =pgalloc_tag_get (page);if (put_page_testzero (page))__free_frozen_pages (page, order, fpi_flags);else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); +pgalloc_tag_sub_pages (tag, (1 << order) -1 );while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);
Signature · 1 line
2.39.2
Re:[PATCH v2] mm/codetag: move tag retrieval back upfront in __free_pages()
Sorry, I made a mistake, pgalloc_tag_get should have a dummy definition when CONFIG_MEM_ALLOC_PROFILING not defined, will send another patch.
Quoted 97 lines · David Wang
Commit
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()checks") introduces a possible use-after-free scenario, when page is non-compound, page[0] could be released by other thread right afterput_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid pageV
Restore
__free_pages()to its state before, retrieve alloc tag beforehand.Fixes 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled() checks")Signed-off-by David Wang <[email protected]>---
include/linux/pgalloc_tag.h| 7 +++++++mm/page_alloc.c| 15 ++++++---------2 files changed, 13 insertions(+), 9 deletions(-)include/linux/pgalloc_tag.h +7
-0@@ -188,6 +188,13 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page)return tag; } +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) +{ +if (mem_alloc_profiling_enabled ()) +return __pgalloc_tag_get (page); +return NULL ; +} +void pgalloc_tag_split (struct folio *folio,int old_order,int new_order);void pgalloc_tag_swap (struct folio *new,struct folio *old);mm/page_alloc.c +6
-9@@ -1151,14 +1151,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)__pgalloc_tag_sub (page, nr); } -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) +/* When tag is not NULL, assuming mem_alloc_profiling_enabled */ +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) { -struct alloc_tag *tag; - -if (!mem_alloc_profiling_enabled ()) -return ; - - tag =__pgalloc_tag_get (page);if (tag)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); }@@ -1168,7 +1163,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {} -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */ @@ -5065,11 +5060,13 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* get alloc tag in case the page is released by others */ +struct alloc_tag *tag =pgalloc_tag_get (page);if (put_page_testzero (page))__free_frozen_pages (page, order, fpi_flags);else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); +pgalloc_tag_sub_pages (tag, (1 << order) -1 );while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);Signature · 1 line
2.39.2
PATCH v3 1/1 mm/codetag: move tag retrieval back upfront in __free_pages()
In reply to Suren BaghdasaryanCommit 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled()
checks") introduces a possible use-after-free scenario, when page
is non-compound, page[0] could be released by other thread right after put_page_testzero failed in current thread, pgalloc_tag_sub_pages afterwards would manipulate an invalid page for accounting remaining pages:
[timeline] [thread1] [thread2]
| alloc_page non-compound
V
| get_page, rf counter inc
V
| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
V
Restore __free_pages() to its state before, retrieve alloc tag beforehand.
--- include/linux/pgalloc_tag.h | 8 ++++++++ mm/page_alloc.c | 15 ++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)
include/linux/pgalloc_tag.h +8 -0
@@ -188,6 +188,13 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page)return tag; } +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) +{ +if (mem_alloc_profiling_enabled ()) +return __pgalloc_tag_get (page); +return NULL ; +} +void pgalloc_tag_split (struct folio *folio,int old_order,int new_order);void pgalloc_tag_swap (struct folio *new,struct folio *old);
@@ -199,6 +206,7 @@ static inline void clear_page_tag_ref(struct page *page) {}static inline void alloc_tag_sec_init (void ) {}static inline void pgalloc_tag_split (struct folio *folio,int old_order,int new_order) {}static inline void pgalloc_tag_swap (struct folio *new,struct folio *old) {} +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) {return NULL ; }#endif /* CONFIG_MEM_ALLOC_PROFILING */
mm/page_alloc.c +6 -9
@@ -1151,14 +1151,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)__pgalloc_tag_sub (page, nr); } -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) +/* When tag is not NULL, assuming mem_alloc_profiling_enabled */ +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) { -struct alloc_tag *tag; - -if (!mem_alloc_profiling_enabled ()) -return ; - - tag =__pgalloc_tag_get (page);if (tag)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); }
@@ -1168,7 +1163,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {} -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */
@@ -5065,11 +5060,13 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* get alloc tag in case the page is released by others */ +struct alloc_tag *tag =pgalloc_tag_get (page);if (put_page_testzero (page))__free_frozen_pages (page, order, fpi_flags);else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); +pgalloc_tag_sub_pages (tag, (1 << order) -1 );while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);
Signature · 1 line
2.39.2
Re: [PATCH v3] mm/codetag: move tag retrieval back upfront in __free_pages()
Quoted 21 lines · David Wang
Commit
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()
checks") introduces a possible use-after-free scenario, when page
is non-compound, page[0] could be released by other thread right afterput_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
Quoted 7 lines
V
Restore
__free_pages()to its state before, retrieve alloc tag beforehand.Fixes 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled() checks")Signed-off-by David Wang <[email protected]>
Thanks!
Quoted 79 lines
--- include/linux/pgalloc_tag.h | 8 ++++++++ mm/page_alloc.c | 15 ++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)include/linux/pgalloc_tag.h +8
-0@@ -188,6 +188,13 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page)return tag; } +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) +{ +if (mem_alloc_profiling_enabled ()) +return __pgalloc_tag_get (page); +return NULL ; +} +void pgalloc_tag_split (struct folio *folio,int old_order,int new_order);void pgalloc_tag_swap (struct folio *new,struct folio *old);@@ -199,6 +206,7 @@ static inline void clear_page_tag_ref(struct page *page) {}static inline void alloc_tag_sec_init (void ) {}static inline void pgalloc_tag_split (struct folio *folio,int old_order,int new_order) {}static inline void pgalloc_tag_swap (struct folio *new,struct folio *old) {} +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) {return NULL ; }#endif /* CONFIG_MEM_ALLOC_PROFILING */ mm/page_alloc.c +6
-10@@ -1151,14 +1151,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)__pgalloc_tag_sub (page, nr); } -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) +/* When tag is not NULL, assuming mem_alloc_profiling_enabled */ +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) { -struct alloc_tag *tag; - -if (!mem_alloc_profiling_enabled ()) -return ; - - tag =__pgalloc_tag_get (page);if (tag)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); }@@ -1168,7 +1163,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {} -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */ @@ -5065,11 +5060,13 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* get alloc tag in case the page is released by others */ +struct alloc_tag *tag =pgalloc_tag_get (page);if (put_page_testzero (page))__free_frozen_pages (page, order, fpi_flags);else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); +pgalloc_tag_sub_pages (tag, (1 << order) -1 );while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags); --2.39.2
Re: [PATCH v3] mm/codetag: move tag retrieval back upfront in __free_pages()
In reply to David WangQuoted 20 lines · David Wang
Commit
51ff4d7486f0("mm: avoid extramem_alloc_profiling_enabled()
checks") introduces a possible use-after-free scenario, when page
is non-compound, page[0] could be released by other thread right afterput_page_testzerofailed in current thread,pgalloc_tag_sub_pagesafterwards would manipulate an invalid page for accounting remaining pages:[timeline] [thread1] [thread2]
|alloc_pagenon-compound
V
|get_page, rf counter inc
V| in ___free_pages | put_page_testzero fails V | put_page, page released V | in ___free_pages, | pgalloc_tag_sub_pages | manipulate an invalid page
Quoted 6 lines
V
Restore
__free_pages()to its state before, retrieve alloc tag beforehand.Fixes 51ff4d7486f0 ("mm: avoid extra mem_alloc_profiling_enabled() checks")
That's a 6.15-rc1 commit, so no cc: stable needed, but should go to mm-hotfixes.
Quoted 1 line
Signed-off-by David Wang <[email protected]>
Thanks!
Quoted 76 lines
--- include/linux/pgalloc_tag.h | 8 ++++++++ mm/page_alloc.c | 15 ++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-)include/linux/pgalloc_tag.h +8
-0@@ -188,6 +188,13 @@ static inline struct alloc_tag *__pgalloc_tag_get(struct page *page)return tag; } +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) +{ +if (mem_alloc_profiling_enabled ()) +return __pgalloc_tag_get (page); +return NULL ; +} +void pgalloc_tag_split (struct folio *folio,int old_order,int new_order);void pgalloc_tag_swap (struct folio *new,struct folio *old);@@ -199,6 +206,7 @@ static inline void clear_page_tag_ref(struct page *page) {}static inline void alloc_tag_sec_init (void ) {}static inline void pgalloc_tag_split (struct folio *folio,int old_order,int new_order) {}static inline void pgalloc_tag_swap (struct folio *new,struct folio *old) {} +static inline struct alloc_tag *pgalloc_tag_get (struct page *page) {return NULL ; }#endif /* CONFIG_MEM_ALLOC_PROFILING */ mm/page_alloc.c +6
-9@@ -1151,14 +1151,9 @@ static inline void pgalloc_tag_sub(struct page *page, unsigned int nr)__pgalloc_tag_sub (page, nr); } -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) +/* When tag is not NULL, assuming mem_alloc_profiling_enabled */ +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) { -struct alloc_tag *tag; - -if (!mem_alloc_profiling_enabled ()) -return ; - - tag =__pgalloc_tag_get (page);if (tag)this_cpu_sub (tag->counters->bytes,PAGE_SIZE * nr); }@@ -1168,7 +1163,7 @@ static inline void pgalloc_tag_sub_pages(struct page *page, unsigned int nr)static inline void pgalloc_tag_add (struct page *page,struct task_struct *task,unsigned int nr) {}static inline void pgalloc_tag_sub (struct page *page,unsigned int nr) {} -static inline void pgalloc_tag_sub_pages (struct page *page,unsigned int nr) {} +static inline void pgalloc_tag_sub_pages (struct alloc_tag *tag,unsigned int nr) {}#endif /* CONFIG_MEM_ALLOC_PROFILING */ @@ -5065,11 +5060,13 @@ static void ___free_pages(struct page *page, unsigned int order, {/* get PageHead before we drop reference */ int head =PageHead (page); +/* get alloc tag in case the page is released by others */ +struct alloc_tag *tag =pgalloc_tag_get (page);if (put_page_testzero (page))__free_frozen_pages (page, order, fpi_flags);else if (!head) { -pgalloc_tag_sub_pages (page, (1 << order) -1 ); +pgalloc_tag_sub_pages (tag, (1 << order) -1 );while (order-- >0 )__free_frozen_pages (page + (1 << order), order, fpi_flags);