Fix some bugs about HugeTLB code
Subsystems (3)
v2 cover Fix some bugs about HugeTLB code
This patch series aims to fix some bugs and add some improvements.
Changelog since v1 -> v2:
- Export set_page_huge_active() in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.
Muchun Song (6):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 69 +++++++++++++++++++++++++++++++++++++++++++------ mm/migrate.c | 6 +++++ 4 files changed, 71 insertions(+), 9 deletions(-)
Signature · 1 line
2.11.0
PATCH v2 1/6 mm: migrate: do not migrate HugeTLB page whose refcount is one
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
--- mm/migrate.c | 6 ++++++ 1 file changed, 6 insertions(+)
mm/migrate.c +6 -0
@@ -1279,6 +1279,12 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,return -ENOSYS ; } +if (page_count (hpage) ==1 ) { +/* page was freed from under us. So we are done. */ +putback_active_hugepage (hpage); +return MIGRATEPAGE_SUCCESS ; + } + new_hpage =get_new_page (hpage, private);if (!new_hpage)return -ENOMEM ;
Signature · 1 line
2.11.0
Re: [PATCH v2 1/6] mm: migrate: do not migrate HugeTLB page whose refcount is one
Quoted 2 lines · Muchun Song
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
Is this common enough that it would warrant the explicit check for each migration?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 1/6] mm: migrate: do not migrate HugeTLB page whose refcount is one
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
Is this common enough that it would warrant the explicit check for each migration?
Are you worried about the overhead caused by the check? Thanks.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 1/6] mm: migrate: do not migrate HugeTLB page whose refcount is one
Quoted 10 lines · Muchun Song
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
Is this common enough that it would warrant the explicit check for each migration?
Are you worried about the overhead caused by the check? Thanks.
I am not as worried as I would like to see some actual justification for this optimization.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 1/6] mm: migrate: do not migrate HugeTLB page whose refcount is one
Quoted 8 lines · Michal Hocko
Quoted 6 lines · Muchun Song
Quoted 5 lines · Michal Hocko
Quoted 2 lines · Muchun Song
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Is this common enough that it would warrant the explicit check for each migration?
Are you worried about the overhead caused by the check? Thanks.
I am not as worried as I would like to see some actual justification for this optimization.
OK. The HugeTLB page can be freed after it was isolated but before being migrated. Right? If we catch this case, it is an optimization. I do this just like unmap_and_move() does for a base page.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 1/6] mm: migrate: do not migrate HugeTLB page whose refcount is one
Quoted 12 lines · Muchun Song
Quoted 11 lines · Michal Hocko
Quoted 9 lines · Muchun Song
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
If the refcount is one when it is migrated, it means that the page was freed from under us. So we are done and do not need to migrate.
Is this common enough that it would warrant the explicit check for each migration?
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
Are you worried about the overhead caused by the check? Thanks.
I am not as worried as I would like to see some actual justification for this optimization.
OK. The HugeTLB page can be freed after it was isolated but before being migrated. Right? If we catch this case, it is an optimization. I do this just like
unmap_and_move()does for a base page.
If your only justification is that this path to be consistent with the regular pages then make it explicit in the changelog. Please think of people reading this code in the future and scratching their heads whether this is something that can be altered and fail to find the reasoning. Was this a huge optimization and some workload might suffer? Can this happen in the first place or how likely it is?
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v2 2/6 mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
In reply to Muchun SongBecause we only can isolate a active page via isolate_huge_page() and hugetlbfs_fallocate() forget to mark it as active, we cannot isolate and migrate those pages.
Only export set_page_huge_active, just leave clear_page_huge_active as static. Because there are no external users.
--- fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)
fs/hugetlbfs/inode.c +2 -1
@@ -735,9 +735,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,mutex_unlock (&hugetlb_fault_mutex_table[hash]); +set_page_huge_active (page);/* * unlock_page because locked by add_to_page_cache() -* page_put due to reference from alloc_huge_page() +* put_page() due to reference from alloc_huge_page() */ unlock_page (page);put_page (page);
include/linux/hugetlb.h +2 -0
@@ -770,6 +770,8 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, }#endif +void set_page_huge_active (struct page *page); +#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};
mm/hugetlb.c +1 -1
@@ -1348,7 +1348,7 @@ bool page_huge_active(struct page *page) }/* never called for tail page */ -static void set_page_huge_active (struct page *page) +void set_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page);SetPagePrivate (&page[1 ]);
Signature · 1 line
2.11.0
Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.
I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a later isolate_huge_page failure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "
Now to the fix. I believe that this patch shows that the set_page_huge_active is just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?
Quoted 55 lines
Only export
set_page_huge_active, just leaveclear_page_huge_activeas static. Because there are no external users.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)fs/hugetlbfs/inode.c +2
-1@@ -735,9 +735,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,mutex_unlock (&hugetlb_fault_mutex_table[hash]); +set_page_huge_active (page);/* * unlock_page because locked by add_to_page_cache() -* page_put due to reference from alloc_huge_page() +* put_page() due to reference from alloc_huge_page() */ unlock_page (page);put_page (page);include/linux/hugetlb.h +2
-0@@ -770,6 +770,8 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, }#endif +void set_page_huge_active (struct page *page); +#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};mm/hugetlb.c +1
-1@@ -1348,7 +1348,7 @@ bool page_huge_active(struct page *page) }/* never called for tail page */ -static void set_page_huge_active (struct page *page) +void set_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page);SetPagePrivate (&page[1 ]);Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
Quoted 15 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a laterisolate_huge_pagefailure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "
Quoted 3 lines
Now to the fix. I believe that this patch shows that the
set_page_huge_activeis just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?
I looked into that yesterday. The primary issue is in page fault code, hugetlb_no_page is an example. If page_huge_active is set, then it can be isolated for migration. So, migration could race with the page fault and the page could be migrated before being added to the page table of the faulting task. This was an issue when hugetlb_no_page set_page_huge_active right after allocating and clearing the huge page. Commit cb6acd01e2e4 moved the set_page_huge_active after adding the page to the page table to address this issue.
Signature · 1 line
Mike Kravetz
Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
Quoted 20 lines · Mike Kravetz
Quoted 15 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a laterisolate_huge_pagefailure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "Quoted 3 lines
Now to the fix. I believe that this patch shows that the
set_page_huge_activeis just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?
Quoted 8 lines
I looked into that yesterday. The primary issue is in page fault code,
hugetlb_no_pageis an example. Ifpage_huge_activeis set, then it can be isolated for migration. So, migration could race with the page fault and the page could be migrated before being added to the page table of the faulting task. This was an issue whenhugetlb_no_pageset_page_huge_activeright after allocating and clearing the huge page. Commitcb6acd01e2e4moved theset_page_huge_activeafter adding the page to the page table to address this issue.
Thanks for the clarification. I was not aware of this subtlety. The existing comment is not helping much TBH. I am still digesting the suggested race. The page is new and exclusive and not visible via page tables yet, so the only source of the migration would be pfn based (hotplug, poisoning), right?
Btw. s@set_page_huge_active@set_page_huge_migrateable@ would help readability IMHO. With a comment explaining that this _has_ to be called after the page is fully initialized.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
Quoted 30 lines · Michal Hocko
Quoted 20 lines · Mike Kravetz
Quoted 15 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a laterisolate_huge_pagefailure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "Quoted 3 lines
Now to the fix. I believe that this patch shows that the
set_page_huge_activeis just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?Quoted 8 lines
I looked into that yesterday. The primary issue is in page fault code,
hugetlb_no_pageis an example. Ifpage_huge_activeis set, then it can be isolated for migration. So, migration could race with the page fault and the page could be migrated before being added to the page table of the faulting task. This was an issue whenhugetlb_no_pageset_page_huge_activeright after allocating and clearing the huge page. Commitcb6acd01e2e4moved theset_page_huge_activeafter adding the page to the page table to address this issue.
Quoted 5 lines
Thanks for the clarification. I was not aware of this subtlety. The existing comment is not helping much TBH. I am still digesting the suggested race. The page is new and exclusive and not visible via page tables yet, so the only source of the migration would be pfn based (hotplug, poisoning), right?
That is correct.
Quoted 3 lines
Btw. s@set_page_huge_active@set_page_huge_migrateable@ would help readability IMHO. With a comment explaining that this _has_ to be called after the page is fully initialized.
Agree, I will add that as a future enhancement.
Signature · 1 line
Mike Kravetz
Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
Quoted 37 lines · Mike Kravetz
Quoted 30 lines · Michal Hocko
Quoted 20 lines · Mike Kravetz
Quoted 15 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a laterisolate_huge_pagefailure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "Quoted 3 lines
Now to the fix. I believe that this patch shows that the
set_page_huge_activeis just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?Quoted 8 lines
I looked into that yesterday. The primary issue is in page fault code,
hugetlb_no_pageis an example. Ifpage_huge_activeis set, then it can be isolated for migration. So, migration could race with the page fault and the page could be migrated before being added to the page table of the faulting task. This was an issue whenhugetlb_no_pageset_page_huge_activeright after allocating and clearing the huge page. Commitcb6acd01e2e4moved theset_page_huge_activeafter adding the page to the page table to address this issue.Quoted 5 lines
Thanks for the clarification. I was not aware of this subtlety. The existing comment is not helping much TBH. I am still digesting the suggested race. The page is new and exclusive and not visible via page tables yet, so the only source of the migration would be pfn based (hotplug, poisoning), right?
Quoted 8 lines
That is correct.
Quoted 3 lines
Btw. s@set_page_huge_active@set_page_huge_migrateable@ would help readability IMHO. With a comment explaining that this _has_ to be called after the page is fully initialized.
Agree, I will add that as a future enhancement.
Thanks!
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 2/6] mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
In reply to Michal HockoQuoted 6 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Because we only can isolate a active page via
isolate_huge_page()andhugetlbfs_fallocate()forget to mark it as active, we cannot isolate and migrate those pages.
Quoted 9 lines
I've little bit hard time to understand this initially and had to dive into the code to make sense of it. I would consider the following wording easier to grasp. Feel free to reuse if you like.
"
If a new hugetlb page is allocated during fallocate it will not be marked as active (set_page_huge_active) which will result in a laterisolate_huge_pagefailure when the page migration code would like to move that page. Such a failure would be unexpected and wrong. "
Thanks for your suggestion. I will reuse it.
Quoted 64 lines
Now to the fix. I believe that this patch shows that the
set_page_huge_activeis just too subtle. Is there any reason why we cannot make all freshly allocated huge pages active by default?Quoted 54 lines
Only export
set_page_huge_active, just leaveclear_page_huge_activeas static. Because there are no external users.Fixes 70c3547e36f5 (hugetlbfs: add hugetlbfs_fallocate())Signed-off-by Muchun Song <[email protected]>--- fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-)fs/hugetlbfs/inode.c +2
-1@@ -735,9 +735,10 @@ static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,mutex_unlock (&hugetlb_fault_mutex_table[hash]); +set_page_huge_active (page);/* * unlock_page because locked by add_to_page_cache() -* page_put due to reference from alloc_huge_page() +* put_page() due to reference from alloc_huge_page() */ unlock_page (page);put_page (page);include/linux/hugetlb.h +2
-0@@ -770,6 +770,8 @@ static inline void huge_ptep_modify_prot_commit(struct vm_area_struct *vma, }#endif +void set_page_huge_active (struct page *page); +#else /* CONFIG_HUGETLB_PAGE */ struct hstate {};mm/hugetlb.c +1
-2@@ -1348,7 +1348,7 @@ bool page_huge_active(struct page *page) }/* never called for tail page */ -static void set_page_huge_active (struct page *page) +void set_page_huge_active (struct page *page) {VM_BUG_ON_PAGE (!PageHeadHuge (page), page);SetPagePrivate (&page[1 ]); --Quoted 1 line
2.11.0
--
Michal Hocko
SUSE Labs
PATCH v2 3/6 mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Muchun SongThere is a race condition between __free_huge_page() and dissolve_free_huge_page().
CPU0: CPU1:
// page_count(page) == 1put_page(page)
__free_huge_page(page)
dissolve_free_huge_page(page)
spin_lock(&hugetlb_lock)
// PageHuge(page) && !page_count(page)
update_and_free_page(page)
// page is freed to the buddy
spin_unlock(&hugetlb_lock)
spin_lock(&hugetlb_lock)
clear_page_huge_active(page)
enqueue_huge_page(page)
// It is wrong, the page is already freed
spin_unlock(&hugetlb_lock)The race windows is between put_page() and spin_lock() which is in the __free_huge_page().
We should make sure that the page is already on the free list when it is dissolved.
--- mm/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)
mm/hugetlb.c +38 -0
@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);static int num_fault_mutexes;struct mutex *hugetlb_fault_mutex_table____cacheline_aligned_in_smp ; +static inline bool PageHugeFreed (struct page *head) +{ +return (unsigned long )head[3 ].mapping ==-1U ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ + head[3 ].mapping = (void *)-1U ; +} + +static inline void ClearPageHugeFreed (struct page *head) +{ + head[3 ].mapping =NULL ; +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);
@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)
@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;
@@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif +/* +* Because we reuse the mapping field of some tail page structs, we should +* reset those mapping to initial value before @head is freed to the buddy +* allocator. The invalid value will be checked in the free_tail_pages_check(). +*/ +static inline void reset_tail_page_mapping (struct hstate *h,struct page *head) +{ +if (!hstate_is_gigantic (h)) + head[3 ].mapping =TAIL_MAPPING ; +} +static void update_and_free_page (struct hstate *h,struct page *page) {int i;
@@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; +reset_tail_page_mapping (h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--;for (i =0 ; i <pages_per_huge_page (h); i++) {
@@ -1504,6 +1533,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }
@@ -1770,6 +1800,14 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) + goto out; +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable.
Signature · 1 line
2.11.0
Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)
Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().
The race window reall is between put_page and dissolve_free_huge_page. And the result is that the put_page path would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.
Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in __free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check the page->lru in the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Quoted 99 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)mm/hugetlb.c +38
-0@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);static int num_fault_mutexes;struct mutex *hugetlb_fault_mutex_table____cacheline_aligned_in_smp ; +static inline bool PageHugeFreed (struct page *head) +{ +return (unsigned long )head[3 ].mapping ==-1U ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ + head[3 ].mapping = (void *)-1U ; +} + +static inline void ClearPageHugeFreed (struct page *head) +{ + head[3 ].mapping =NULL ; +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;@@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif +/* +* Because we reuse the mapping field of some tail page structs, we should +* reset those mapping to initial value before @head is freed to the buddy +* allocator. The invalid value will be checked in the free_tail_pages_check(). +*/ +static inline void reset_tail_page_mapping (struct hstate *h,struct page *head) +{ +if (!hstate_is_gigantic (h)) + head[3 ].mapping =TAIL_MAPPING ; +} +static void update_and_free_page (struct hstate *h,struct page *page) {int i;@@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; +reset_tail_page_mapping (h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--;for (i =0 ; i <pages_per_huge_page (h); i++) {@@ -1504,6 +1533,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }@@ -1770,6 +1800,14 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) + goto out; +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable. Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 33 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
The tricky part with expanding lock scope will be the potential call to hugepage_subpool_put_pages as it may also try to acquire the hugetlb_lock.
I am not sure what you mean by 'check the page->lru'? If we knew the page was on the free list, then we could dissolve. But, I do not think there is an easy way to determine that from page->lru. A hugetlb page is either going to be on the active list or free list.
Quoted 53 lines
Quoted 53 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)mm/hugetlb.c +17
-0@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);static int num_fault_mutexes;struct mutex *hugetlb_fault_mutex_table____cacheline_aligned_in_smp ; +static inline bool PageHugeFreed (struct page *head) +{ +return (unsigned long )head[3 ].mapping ==-1U ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ + head[3 ].mapping = (void *)-1U ; +} + +static inline void ClearPageHugeFreed (struct page *head) +{ + head[3 ].mapping =NULL ; +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;@@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif
Quoted 5 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1315
Quoted 5 lines
mm/hugetlb.c +5
-0+/* +* Because we reuse the mapping field of some tail page structs, we should +* reset those mapping to initial value before @head is freed to the buddy +* allocator. The invalid value will be checked in the free_tail_pages_check(). +*/
When I suggested using head[3].mapping for this state, I was not aware of this requirement. My suggestion was only following the convention used in PageHugeTemporary. I would not have made the suggestion if I had realized this was required. Sorry.
Signature · 1 line
Mike Kravetz
Quoted 43 lines
Quoted 42 lines
mm/hugetlb.c +16
-0+static inline void reset_tail_page_mapping (struct hstate *h,struct page *head) +{ +if (!hstate_is_gigantic (h)) + head[3 ].mapping =TAIL_MAPPING ; +} +static void update_and_free_page (struct hstate *h,struct page *page) {int i;@@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; +reset_tail_page_mapping (h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--;for (i =0 ; i <pages_per_huge_page (h); i++) {@@ -1504,6 +1533,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }@@ -1770,6 +1800,14 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) + goto out; +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable. Signature · 1 line
2.11.0
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 103 lines · Mike Kravetz
Quoted 33 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?The tricky part with expanding lock scope will be the potential call to
hugepage_subpool_put_pagesas it may also try to acquire thehugetlb_lock.I am not sure what you mean by 'check the
page->lru'? If we knew the page was on the free list, then we could dissolve. But, I do not think there is an easy way to determine that frompage->lru. A hugetlb page is either going to be on the active list or free list.Quoted 53 lines
Quoted 53 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)mm/hugetlb.c +17
-0@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);static int num_fault_mutexes;struct mutex *hugetlb_fault_mutex_table____cacheline_aligned_in_smp ; +static inline bool PageHugeFreed (struct page *head) +{ +return (unsigned long )head[3 ].mapping ==-1U ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ + head[3 ].mapping = (void *)-1U ; +} + +static inline void ClearPageHugeFreed (struct page *head) +{ + head[3 ].mapping =NULL ; +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;@@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif
Quoted 10 lines
Quoted 5 lines
mm/hugetlb.c static inline void destroy_compound_gigantic_page(struct page *page, · line 1315
Quoted 5 lines
mm/hugetlb.c +5
-0+/* +* Because we reuse the mapping field of some tail page structs, we should +* reset those mapping to initial value before @head is freed to the buddy +* allocator. The invalid value will be checked in the free_tail_pages_check(). +*/ When I suggested using head[3].mapping for this state, I was not aware of this requirement. My suggestion was only following the convention used in PageHugeTemporary. I would not have made the suggestion if I had realized this was required. Sorry.
Yeah, PageHugeTemporary is lucky. free_tail_pages_check() not check head[2].mapping.
I will revert to the previous version (using head[3].private). Thanks.
Quoted 46 lines
--
Mike KravetzQuoted 43 lines
Quoted 42 lines
mm/hugetlb.c +16
-1+static inline void reset_tail_page_mapping (struct hstate *h,struct page *head) +{ +if (!hstate_is_gigantic (h)) + head[3 ].mapping =TAIL_MAPPING ; +} +static void update_and_free_page (struct hstate *h,struct page *page) {int i;@@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; +reset_tail_page_mapping (h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--;for (i =0 ; i <pages_per_huge_page (h); i++) {@@ -1504,6 +1533,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }@@ -1770,6 +1800,14 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) + goto out; +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable. -- 2.11.0
Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Mike KravetzQuoted 41 lines · Mike Kravetz
Quoted 33 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Quoted 2 lines
The tricky part with expanding lock scope will be the potential call to
hugepage_subpool_put_pagesas it may also try to acquire thehugetlb_lock.
Can we rearrange the code and move hugepage_subpool_put_pages after all this is done? Or is there any strong reason for the particular ordering?
Quoted 4 lines
I am not sure what you mean by 'check the
page->lru'? If we knew the page was on the free list, then we could dissolve. But, I do not think there is an easy way to determine that frompage->lru. A hugetlb page is either going to be on the active list or free list.
Can it be on the active list with ref count = 0?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 42 lines · Michal Hocko
Quoted 41 lines · Mike Kravetz
Quoted 39 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Quoted 5 lines
Quoted 2 lines
The tricky part with expanding lock scope will be the potential call to
hugepage_subpool_put_pagesas it may also try to acquire thehugetlb_lock.Can we rearrange the code and move
hugepage_subpool_put_pagesafter all this is done? Or is there any strong reason for the particular ordering?
The reservation code is so fragile, I always get nervous when making any changes. However, the straight forward patch below passes some simple testing. The only difference I can see is that global counts are adjusted before sub-pool counts. This should not be an issue as global and sub-pool counts are adjusted independently (not under the same lock). Allocation code checks sub-pool counts before global counts. So, there is a SMALL potential that a racing allocation which previously succeeded would now fail. I do not think this is an issue in practice.
mm/hugetlb.c +27 -20
@@ -1395,6 +1395,11 @@ static void __free_huge_page(struct page *page) (struct hugepage_subpool *)page_private (page);bool restore_reserve; +spin_lock (&hugetlb_lock); +/* check for race with dissolve_free_huge_page/update_and_free_page */ +if (!PageHuge (page)) +return ; +VM_BUG_ON_PAGE (page_count (page), page);VM_BUG_ON_PAGE (page_mapcount (page), page);
@@ -1403,26 +1408,6 @@ static void __free_huge_page(struct page *page) restore_reserve =PagePrivate (page);ClearPagePrivate (page); -/* -* If PagePrivate() was set on page, page allocation consumed a -* reservation. If the page was associated with a subpool, there -* would have been a page reserved in the subpool before allocation -* via hugepage_subpool_get_pages(). Since we are 'restoring' the -* reservtion, do not call hugepage_subpool_put_pages() as this will -* remove the reserved page from the subpool. -*/ -if (!restore_reserve) { -/* -* A return code of zero implies that the subpool will be -* under its minimum size if the reservation is not restored -* after page is free. Therefore, force restore_reserve -* operation. -*/ -if (hugepage_subpool_put_pages (spool,1 ) ==0 ) - restore_reserve = true; - } - -spin_lock (&hugetlb_lock);clear_page_huge_active (page);hugetlb_cgroup_uncharge_page (hstate_index (h),pages_per_huge_page (h), page);
@@ -1446,6 +1431,28 @@ static void __free_huge_page(struct page *page)enqueue_huge_page (h, page); }spin_unlock (&hugetlb_lock); + +/* +* If PagePrivate() was set on page, page allocation consumed a +* reservation. If the page was associated with a subpool, there +* would have been a page reserved in the subpool before allocation +* via hugepage_subpool_get_pages(). Since we are 'restoring' the +* reservtion, do not call hugepage_subpool_put_pages() as this will +* remove the reserved page from the subpool. +*/ +if (!restore_reserve) { +/* +* A return code of zero implies that the subpool will be +* under its minimum size if the reservation is not restored +* after page is free. Therefore, we need to add 1 to the +* global reserve count. +*/ +if (hugepage_subpool_put_pages (spool,1 ) ==0 ) { +spin_lock (&hugetlb_lock); + h->resv_huge_pages++; +spin_unlock (&hugetlb_lock); + } + } }/*
Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 64 lines · Mike Kravetz
Quoted 42 lines · Michal Hocko
Quoted 41 lines · Mike Kravetz
Quoted 39 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?Quoted 5 lines
Quoted 2 lines
The tricky part with expanding lock scope will be the potential call to
hugepage_subpool_put_pagesas it may also try to acquire thehugetlb_lock.Can we rearrange the code and move
hugepage_subpool_put_pagesafter all this is done? Or is there any strong reason for the particular ordering?The reservation code is so fragile, I always get nervous when making any changes. However, the straight forward patch below passes some simple testing. The only difference I can see is that global counts are adjusted before sub-pool counts. This should not be an issue as global and sub-pool counts are adjusted independently (not under the same lock). Allocation code checks sub-pool counts before global counts. So, there is a SMALL potential that a racing allocation which previously succeeded would now fail. I do not think this is an issue in practice.
mm/hugetlb.c +0
-0
mm/hugetlb.c static void __free_huge_page(struct page *page) · line 1402
Quoted 8 lines
mm/hugetlb.c +5
-0@@ -1395,6 +1395,11 @@ static void __free_huge_page(struct page *page) (struct hugepage_subpool *)page_private (page);bool restore_reserve; +spin_lock (&hugetlb_lock); +/* check for race with dissolve_free_huge_page/update_and_free_page */ +if (!PageHuge (page)) +return ; +
This really wants to unlock the lock, right? But this is indeed what I've had in mind.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Michal HockoQuoted 34 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Did you mean that we iterate over the free list to check whether the page is on the free list? If so, I do not think it is a good solution than introducing another state. Because if there are a lot of pages on the free list, it may take some time to do it with holding hugetlb_lock. Right? Actually, we have some tail page structs to store the state. At least it's not in short supply right now.
Thanks.
Quoted 104 lines
Quoted 99 lines
Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+)mm/hugetlb.c +38
-1@@ -79,6 +79,21 @@ DEFINE_SPINLOCK(hugetlb_lock);static int num_fault_mutexes;struct mutex *hugetlb_fault_mutex_table____cacheline_aligned_in_smp ; +static inline bool PageHugeFreed (struct page *head) +{ +return (unsigned long )head[3 ].mapping ==-1U ; +} + +static inline void SetPageHugeFreed (struct page *head) +{ + head[3 ].mapping = (void *)-1U ; +} + +static inline void ClearPageHugeFreed (struct page *head) +{ + head[3 ].mapping =NULL ; +} +/* Forward declaration */ static int hugetlb_acct_memory (struct hstate *h,long delta);@@ -1028,6 +1043,7 @@ static void enqueue_huge_page(struct hstate *h, struct page *page)list_move (&page->lru, &h->hugepage_freelists[nid]); h->free_huge_pages++; h->free_huge_pages_node[nid]++; +SetPageHugeFreed (page); }static struct page *dequeue_huge_page_node_exact (struct hstate *h,int nid)@@ -1044,6 +1060,7 @@ static struct page *dequeue_huge_page_node_exact(struct hstate *h, int nid)list_move (&page->lru, &h->hugepage_activelist);set_page_refcounted (page); +ClearPageHugeFreed (page); h->free_huge_pages--; h->free_huge_pages_node[nid]--;return page;@@ -1291,6 +1308,17 @@ static inline void destroy_compound_gigantic_page(struct page *page, unsigned int order) { }#endif +/* +* Because we reuse the mapping field of some tail page structs, we should +* reset those mapping to initial value before @head is freed to the buddy +* allocator. The invalid value will be checked in the free_tail_pages_check(). +*/ +static inline void reset_tail_page_mapping (struct hstate *h,struct page *head) +{ +if (!hstate_is_gigantic (h)) + head[3 ].mapping =TAIL_MAPPING ; +} +static void update_and_free_page (struct hstate *h,struct page *page) {int i;@@ -1298,6 +1326,7 @@ static void update_and_free_page(struct hstate *h, struct page *page)if (hstate_is_gigantic (h) && !gigantic_page_runtime_supported ())return ; +reset_tail_page_mapping (h, page); h->nr_huge_pages--; h->nr_huge_pages_node[page_to_nid (page)]--;for (i =0 ; i <pages_per_huge_page (h); i++) {@@ -1504,6 +1533,7 @@ static void prep_new_huge_page(struct hstate *h, struct page *page, int nid)spin_lock (&hugetlb_lock); h->nr_huge_pages++; h->nr_huge_pages_node[nid]++; +ClearPageHugeFreed (page);spin_unlock (&hugetlb_lock); }@@ -1770,6 +1800,14 @@ int dissolve_free_huge_page(struct page *page)int nid =page_to_nid (head);if (h->free_huge_pages - h->resv_huge_pages ==0 ) goto out; + +/* +* We should make sure that the page is already on the free list +* when it is dissolved. +*/ +if (unlikely (!PageHugeFreed (head))) + goto out; +/* * Move PageHWPoison flag from head page to the raw error page, * which makes any subpages rather than the error page reusable. -- Quoted 1 line
2.11.0
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 42 lines · Muchun Song
Quoted 34 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Quoted 2 lines
Did you mean that we iterate over the free list to check whether the page is on the free list?
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 44 lines · Michal Hocko
Quoted 42 lines · Muchun Song
Quoted 40 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?
Quoted 6 lines
Quoted 2 lines
Did you mean that we iterate over the free list to check whether the page is on the free list?
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
The page can be on the free list or active list or empty when it is freed to the pool. How to check whether it is on the free list?
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 48 lines · Muchun Song
Quoted 47 lines · Michal Hocko
Quoted 44 lines · Muchun Song
Quoted 34 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?Did you mean that we iterate over the free list to check whether the page is on the free list?
Quoted 6 lines
Quoted 3 lines
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
The page can be on the free list or active list or empty when it is freed to the pool. How to check whether it is on the free list?
As I've said, I might be missing something here. But if the page is freed why does it matter whether it is on a active list or free list from the dissolve operation POV?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 54 lines · Michal Hocko
Quoted 52 lines · Muchun Song
Quoted 50 lines · Michal Hocko
Quoted 42 lines · Muchun Song
Quoted 34 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Quoted 6 lines
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?Quoted 2 lines
Did you mean that we iterate over the free list to check whether the page is on the free list?
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
Quoted 6 lines
Quoted 2 lines
The page can be on the free list or active list or empty when it is freed to the pool. How to check whether it is on the free list?
As I've said, I might be missing something here. But if the page is freed why does it matter whether it is on a active list or free list from the dissolve operation POV?
As you said "check the page->lru". I have a question. How to check the page->lru in the dissolve path?
BTW, dissolve_free_huge_page aims to free the page to buddy allocator. put_page (for HugeTLB page) aims to free the page to the hugepage pool.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 58 lines · Muchun Song
Quoted 57 lines · Michal Hocko
Quoted 54 lines · Muchun Song
Quoted 44 lines · Michal Hocko
Quoted 42 lines · Muchun Song
Quoted 40 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?Quoted 6 lines
Quoted 2 lines
Did you mean that we iterate over the free list to check whether the page is on the free list?
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
The page can be on the free list or active list or empty when it is freed to the pool. How to check whether it is on the free list?
Quoted 6 lines
Quoted 3 lines
As I've said, I might be missing something here. But if the page is freed why does it matter whether it is on a active list or free list from the dissolve operation POV?
As you said "check the
page->lru". I have a question. How to check thepage->lruin the dissolve path?
list_empty?
Quoted 3 lines
BTW,
dissolve_free_huge_pageaims to free the page to buddy allocator.put_page(for HugeTLB page) aims to free the page to the hugepage pool.
Right. Can we simply back off in the dissolving path when ref count is 0 && PageHuge() if list_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 60 lines · Michal Hocko
Quoted 58 lines · Muchun Song
Quoted 57 lines · Michal Hocko
Quoted 54 lines · Muchun Song
Quoted 44 lines · Michal Hocko
Quoted 42 lines · Muchun Song
Quoted 40 lines · Michal Hocko
Quoted 20 lines · Muchun Song
There is a race condition between
__free_huge_page()anddissolve_free_huge_page().CPU0: CPU1:
//
page_count(page)== 1put_page(page)
__free_huge_page(page)dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) // PageHuge(page) && !page_count(page) update_and_free_page(page) // page is freed to the buddy spin_unlock(&hugetlb_lock) spin_lock(&hugetlb_lock) clear_page_huge_active(page) enqueue_huge_page(page) // It is wrong, the page is already freed spin_unlock(&hugetlb_lock)Quoted 2 lines
The race windows is between
put_page()andspin_lock()which is in the__free_huge_page().The race window reall is between
put_pageanddissolve_free_huge_page. And the result is that theput_pagepath would clobber an unrelated page (either free or already reused page) which is quite serious. Fortunatelly pages are dissolved very rarely. I believe that user would require to be privileged to hit this by intention.Quoted 2 lines
We should make sure that the page is already on the free list when it is dissolved.
Another option would be to check for PageHuge in
__free_huge_page. Have you considered that rather than add yet another state? The scope of the spinlock would have to be extended. If that sounds more tricky then can we check thepage->lruin the dissolve path? If the page is still PageHuge and reference count 0 then there shouldn't be many options where it can be queued, right?Quoted 6 lines
Quoted 2 lines
Did you mean that we iterate over the free list to check whether the page is on the free list?
No I meant to check that the page is enqueued which along with ref count = 0 should mean it has been released to the pool unless I am missing something.
The page can be on the free list or active list or empty when it is freed to the pool. How to check whether it is on the free list?
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
As I've said, I might be missing something here. But if the page is freed why does it matter whether it is on a active list or free list from the dissolve operation POV?
As you said "check the
page->lru". I have a question. How to check thepage->lruin the dissolve path?
list_empty?
No.
Quoted 8 lines
Quoted 4 lines
BTW,
dissolve_free_huge_pageaims to free the page to buddy allocator.put_page(for HugeTLB page) aims to free the page to the hugepage pool.Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?
The list_empty(&page->lru) may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. So list_empty(&page->lru) is always false.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.
The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse page->lru again. So scratch that and sorry for the detour.
But that made me think some more and one way to reliably detect the race should be PageHuge() check in the freeing path. This is what dissolve path does already. PageHuge becomes false during update_and_free_page() while holding the hugetlb_lock. So can we use that?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 20 lines · Michal Hocko
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse
page->lruagain. So scratch that and sorry for the detour.
Quoted 4 lines
But that made me think some more and one way to reliably detect the race should be
PageHuge()check in the freeing path. This is what dissolve path does already. PageHuge becomes false duringupdate_and_free_page()while holding thehugetlb_lock. So can we use that?
It may make the thing complex. Apart from freeing it to the buddy allocator, free_huge_page also does something else for us. If we detect the race in the freeing path, if it is not a HugeTLB page, the freeing path just returns. We also should move those things to the dissolve path. Right?
But I find a tricky problem to solve. See free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse the page->mapping. If the page is already freed by the dissolve path. We should not touch the page->mapping. So we need to check PageHuge(). The check and llist_add() should be protected by hugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 32 lines · Muchun Song
Quoted 20 lines · Michal Hocko
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse
page->lruagain. So scratch that and sorry for the detour.Quoted 4 lines
But that made me think some more and one way to reliably detect the race should be
PageHuge()check in the freeing path. This is what dissolve path does already. PageHuge becomes false duringupdate_and_free_page()while holding thehugetlb_lock. So can we use that?It may make the thing complex. Apart from freeing it to the buddy allocator,
free_huge_pagealso does something else for us. If we detect the race in the freeing path, if it is not a HugeTLB page, the freeing path just returns. We also should move those things to the dissolve path. Right?
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.
You are correct. This is also an issue/potential problem with this race. It seems that adding the state information might be the least complex way to address issue.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 45 lines · Mike Kravetz
Quoted 32 lines · Muchun Song
Quoted 20 lines · Michal Hocko
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse
page->lruagain. So scratch that and sorry for the detour.Quoted 4 lines
But that made me think some more and one way to reliably detect the race should be
PageHuge()check in the freeing path. This is what dissolve path does already. PageHuge becomes false duringupdate_and_free_page()while holding thehugetlb_lock. So can we use that?It may make the thing complex. Apart from freeing it to the buddy allocator,
free_huge_pagealso does something else for us. If we detect the race in the freeing path, if it is not a HugeTLB page, the freeing path just returns. We also should move those things to the dissolve path. Right?Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.
Quoted 3 lines
You are correct. This is also an issue/potential problem with this race. It seems that adding the state information might be the least complex way to address issue.
Yeah, I agree with you. Adding a state is a simple solution.
Quoted 3 lines
--
Mike Kravetz
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
In reply to Muchun SongQuoted 26 lines · Muchun Song
Quoted 20 lines · Michal Hocko
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse
page->lruagain. So scratch that and sorry for the detour.Quoted 4 lines
But that made me think some more and one way to reliably detect the race should be
PageHuge()check in the freeing path. This is what dissolve path does already. PageHuge becomes false duringupdate_and_free_page()while holding thehugetlb_lock. So can we use that?
Quoted 5 lines
It may make the thing complex. Apart from freeing it to the buddy allocator,
free_huge_pagealso does something else for us. If we detect the race in the freeing path, if it is not a HugeTLB page, the freeing path just returns. We also should move those things to the dissolve path. Right?
Not sure what you mean. Dissolving is a subset of the freeing path. It effectivelly only implements the update_and_free_page branch (aka free to buddy). It skips some of the existing steps because it believes it sees a freed page. But as you have pointed out this is racy and I strongly suspect it is simply wrong in those assumptions. E.g. hugetlb cgroup accounting can get wrong right?
The more I think about it the more I think that dissolving path should simply have a common helper with __free_huge_page that would release the huge page to the allocator. The only thing that should be specific to the dissolving path is HWpoison handling. It shouldn't be playing with accounting and what not. Btw the HWpoison handling is suspicious as well, a lost race would mean this doesn't happen. But maybe there is some fixup handled later on...
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.
I am not sure I follow you here but yes PageHuge under hugetlb_lock should be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 34 lines · Michal Hocko
Quoted 26 lines · Muchun Song
Quoted 20 lines · Michal Hocko
Quoted 1 line · Muchun Song
On Thu, Jan 7, 2021 at 8:38 PM Michal Hocko <[email protected]> wrote:
[...]
Quoted 8 lines
Quoted 3 lines
Right. Can we simply back off in the dissolving path when ref count is 0 &&
PageHuge()iflist_empty(page->lru)? Is there any other scenario when the all above is true and the page is not being freed?The
list_empty(&page->lru)may always return false. The page before freeing is on the active list (hstate->hugepage_activelist).Then it is on the free list after freeing. Solist_empty(&page->lru)is always false.The point I was trying to make is that the page has to be enqueued when it is dissolved and freed. If the page is not enqueued then something racing. But then I have realized that this is not a great check to detect the race because pages are going to be released to buddy allocator and that will reuse
page->lruagain. So scratch that and sorry for the detour.Quoted 4 lines
But that made me think some more and one way to reliably detect the race should be
PageHuge()check in the freeing path. This is what dissolve path does already. PageHuge becomes false duringupdate_and_free_page()while holding thehugetlb_lock. So can we use that?Quoted 5 lines
It may make the thing complex. Apart from freeing it to the buddy allocator,
free_huge_pagealso does something else for us. If we detect the race in the freeing path, if it is not a HugeTLB page, the freeing path just returns. We also should move those things to the dissolve path. Right?
Quoted 6 lines
Not sure what you mean. Dissolving is a subset of the freeing path. It effectivelly only implements the
update_and_free_pagebranch (aka free to buddy). It skips some of the existing steps because it believes it sees a freed page. But as you have pointed out this is racy and I strongly suspect it is simply wrong in those assumptions. E.g. hugetlb cgroup accounting can get wrong right?
OK. I know what you mean. The update_and_free_page should do the freeing which is similar to __free_huge_page().
Quoted 20 lines
The more I think about it the more I think that dissolving path should simply have a common helper with
__free_huge_pagethat would release the huge page to the allocator. The only thing that should be specific to the dissolving path is HWpoison handling. It shouldn't be playing with accounting and what not. Btw the HWpoison handling is suspicious as well, a lost race would mean this doesn't happen. But maybe there is some fixup handled later on...Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.
Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.
CPU0: CPU1:free_huge_page(page)
if (PageHuge(page))
dissolve_free_huge_page(page)
spin_lock(&hugetlb_lock)
update_and_free_page(page)
spin_unlock(&hugetlb_lock)
llist_add(page->mapping)
// the mapping is corruptedThe PageHuge(page) and llist_add() should be protected by hugetlb_lock. Right? If so, we cannot hold hugetlb_lock in free_huge_page() path.
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 25 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corrupted
Quoted 3 lines
The PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.
OK, I see. I completely forgot about this snowflake. I thought that free_huge_page was a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch. Although it would be much more nicer if the lock was held at this layer rather than both free_huge_page and __free_huge_page. But that clean up can be done on top.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 31 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 25 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corrupted
Quoted 8 lines
Quoted 3 lines
The PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.OK, I see. I completely forgot about this snowflake. I thought that
free_huge_pagewas a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch.
Because we hold the hugetlb_lock without disable irq. So if an interrupt occurs after we hold the lock. And we also free a HugeTLB page. Then it leads to deadlock.
task context: interrupt context:
put_page(page)
spin_lock(&hugetlb_lock)
put_page(page)
spin_lock(&hugetlb_lock)
// deadlock spin_unlock(&hugetlb_lock)
Quoted 7 lines
Although it would be much more nicer if the lock was held at this layer rather than both
free_huge_pageand__free_huge_page. But that clean up can be done on top.--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 36 lines · Muchun Song
Quoted 35 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 28 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corruptedThe PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.
Quoted 8 lines
Quoted 4 lines
OK, I see. I completely forgot about this snowflake. I thought that
free_huge_pagewas a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch.Because we hold the
hugetlb_lockwithout disable irq. So if an interrupt occurs after we hold the lock. And we also free a HugeTLB page. Then it leads to deadlock.
There is nothing really to prevent making hugetlb_lock irq safe, isn't it?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 43 lines · Michal Hocko
Quoted 41 lines · Muchun Song
Quoted 39 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 25 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corruptedQuoted 3 lines
The PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.OK, I see. I completely forgot about this snowflake. I thought that
free_huge_pagewas a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch.
Quoted 6 lines
Quoted 3 lines
Because we hold the
hugetlb_lockwithout disable irq. So if an interrupt occurs after we hold the lock. And we also free a HugeTLB page. Then it leads to deadlock.There is nothing really to prevent making
hugetlb_lockirq safe, isn't it?
Yeah. We can make the hugetlb_lock irq safe. But why have we not done this? Maybe the commit changelog can provide more information.
See https://github.com/torvalds/linux/commit/c77c0a8ac4c522638a8242fcb9de9496e3cdbb2d
Quoted 3 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 48 lines · Muchun Song
Quoted 47 lines · Michal Hocko
Quoted 44 lines · Muchun Song
Quoted 31 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 25 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corruptedQuoted 8 lines
Quoted 3 lines
The PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.OK, I see. I completely forgot about this snowflake. I thought that
free_huge_pagewas a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch.Because we hold the
hugetlb_lockwithout disable irq. So if an interrupt occurs after we hold the lock. And we also free a HugeTLB page. Then it leads to deadlock.
Quoted 7 lines
Quoted 2 lines
There is nothing really to prevent making
hugetlb_lockirq safe, isn't it?Yeah. We can make the
hugetlb_lockirq safe. But why have we not done this? Maybe the commit changelog can provide more information.See https://github.com/torvalds/linux/commit/c77c0a8ac4c522638a8242fcb9de9496e3cdbb2d
Dang! Maybe it is the time to finally stack one workaround on top of the other and put this code into the shape. The amount of hackery and subtle details has just grown beyond healthy!
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 3/6] mm: hugetlb: fix a race between freeing and dissolving the page
Quoted 53 lines · Michal Hocko
Quoted 51 lines · Muchun Song
Quoted 49 lines · Michal Hocko
Quoted 36 lines · Muchun Song
Quoted 35 lines · Michal Hocko
Quoted 3 lines · Muchun Song
Quoted 2 lines · Michal Hocko
On Thu 07-01-21 23:11:22, Muchun Song wrote:
[..]
Quoted 28 lines
Quoted 11 lines
Quoted 10 lines
But I find a tricky problem to solve. See
free_huge_page(). If we are in non-task context, we should schedule a work to free the page. We reuse thepage->mapping. If the page is already freed by the dissolve path. We should not touch thepage->mapping. So we need to checkPageHuge(). The check andllist_add()should be protected byhugetlb_lock. But we cannot do that. Right? If dissolve happens after it is linked to the list. We also should remove it from the list (hpage_freelist). It seems to make the thing more complex.Quoted 3 lines
I am not sure I follow you here but yes PageHuge under
hugetlb_lockshould be the reliable way to check for the race. I am not sure why we really need to care about mapping or other state.CPU0: CPU1:
free_huge_page(page)
if (PageHuge(page))dissolve_free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) spin_unlock(&hugetlb_lock) llist_add(page->mapping) // the mapping is corruptedThe PageHuge(page) and
llist_add()should be protected byhugetlb_lock. Right? If so, we cannot holdhugetlb_lockinfree_huge_page()path.Quoted 8 lines
Quoted 4 lines
OK, I see. I completely forgot about this snowflake. I thought that
free_huge_pagewas a typo missing initial __. Anyway you are right that this path needs a check as well. But I don't see why we couldn't use the lock here. The lock can be held only inside the !in_task branch.Because we hold the
hugetlb_lockwithout disable irq. So if an interrupt occurs after we hold the lock. And we also free a HugeTLB page. Then it leads to deadlock.There is nothing really to prevent making
hugetlb_lockirq safe, isn't it?
Quoted 8 lines
Quoted 4 lines
Yeah. We can make the
hugetlb_lockirq safe. But why have we not done this? Maybe the commit changelog can provide more information.See https://github.com/torvalds/linux/commit/c77c0a8ac4c522638a8242fcb9de9496e3cdbb2d
Dang! Maybe it is the time to finally stack one workaround on top of the other and put this code into the shape. The amount of hackery and subtle details has just grown beyond healthy!
From readability point of view, maybe making the hugetlb_lock irq safe is an improvement (in the feature).
The details are always messy. :)
Quoted 3 lines
--
Michal Hocko
SUSE Labs
PATCH v2 4/6 mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
In reply to Muchun SongWhen dissolve_free_huge_page() races with __free_huge_page(), we can do a retry. Because the race window is small.
--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)
mm/hugetlb.c +21 -5
@@ -1775,10 +1775,11 @@ static int free_pool_huge_page(struct hstate *h, nodemask_t *nodes_allowed,* nothing for in-use hugepages and non-hugepages. * This function returns values like below: * -* -EBUSY: failed to dissolved free hugepages or the hugepage is in-use -* (allocated or reserved.) -* 0: successfully dissolved free hugepages or the page is not a -* hugepage (considered as already dissolved) +* -EAGAIN: race with __free_huge_page() and can do a retry +* -EBUSY: failed to dissolved free hugepages or the hugepage is in-use +* (allocated or reserved.) +* 0: successfully dissolved free hugepages or the page is not a +* hugepage (considered as already dissolved) */ int dissolve_free_huge_page (struct page *page) {
@@ -1805,8 +1806,10 @@ int dissolve_free_huge_page(struct page *page)* We should make sure that the page is already on the free list * when it is dissolved. */ -if (unlikely (!PageHugeFreed (head))) +if (unlikely (!PageHugeFreed (head))) { + rc = -EAGAIN ; goto out; + }/* * Move PageHWPoison flag from head page to the raw error page,
@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work); +return rc; }
@@ -1847,7 +1858,12 @@ int dissolve_free_huge_pages(unsigned long start_pfn, unsigned long end_pfn)for (pfn = start_pfn; pfn < end_pfn; pfn +=1 << minimum_order) { page =pfn_to_page (pfn); +retry: rc =dissolve_free_huge_page (page); +if (rc == -EAGAIN ) { +cpu_relax (); + goto retry; + }if (rc)break ; }
Signature · 1 line
2.11.0
Re: [PATCH v2 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
Quoted 2 lines · Muchun Song
When
dissolve_free_huge_page()races with__free_huge_page(), we can do a retry. Because the race window is small.
Is this a bug fix or mere optimization. I have hard time to tell from the description.
Quoted 6 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)mm/hugetlb.c +0
-0
[...]
mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1837
Quoted 11 lines
mm/hugetlb.c +7
-0@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work);
Is it safe to wait for the work to finish from this context?
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
When
dissolve_free_huge_page()races with__free_huge_page(), we can do a retry. Because the race window is small.Is this a bug fix or mere optimization. I have hard time to tell from the description.
It is optimization. Thanks.
Quoted 8 lines
Quoted 7 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)mm/hugetlb.c +0
-0[...]
Quoted 13 lines
mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1837
Quoted 11 lines
mm/hugetlb.c +7
-0@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work);Is it safe to wait for the work to finish from this context?
Yes. It is safe.
Quoted 4 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
Quoted 33 lines · Muchun Song
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
When
dissolve_free_huge_page()races with__free_huge_page(), we can do a retry. Because the race window is small.Is this a bug fix or mere optimization. I have hard time to tell from the description.
It is optimization. Thanks.
Quoted 8 lines
Quoted 7 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)mm/hugetlb.c +0
-0[...]
Quoted 13 lines
mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1837
Quoted 11 lines
mm/hugetlb.c +7
-0@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work);Is it safe to wait for the work to finish from this context?
Quoted 1 line
Yes. It is safe.
Please expand on why in the changelog. Same for the optimization including some numbers showing it really helps.
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [External] Re: [PATCH v2 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
Quoted 35 lines · Michal Hocko
Quoted 33 lines · Muchun Song
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
When
dissolve_free_huge_page()races with__free_huge_page(), we can do a retry. Because the race window is small.Is this a bug fix or mere optimization. I have hard time to tell from the description.
It is optimization. Thanks.
Quoted 21 lines
Quoted 7 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)mm/hugetlb.c +0
-0[...]
mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1837
Quoted 11 lines
mm/hugetlb.c +7
-0@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work);Is it safe to wait for the work to finish from this context?
Quoted 4 lines
Quoted 1 line
Yes. It is safe.
Please expand on why in the changelog. Same for the optimization including some numbers showing it really helps.
OK. Changelog should be updated. Do you agree the race window is quite small? If so, why is it not an optimization? Don’t we dissolve the page as successfully as possible when we call dissolve_free_huge_page()? I am confused about numbers showing. Because this is not a performance optimization, but an increase in the success rate of dissolving.
Thanks.
Quoted 4 lines
--
Michal Hocko
SUSE Labs
Re: [External] Re: [PATCH v2 4/6] mm: hugetlb: add return -EAGAIN for dissolve_free_huge_page
Quoted 36 lines · Muchun Song
Quoted 35 lines · Michal Hocko
Quoted 33 lines · Muchun Song
Quoted 7 lines · Michal Hocko
Quoted 2 lines · Muchun Song
When
dissolve_free_huge_page()races with__free_huge_page(), we can do a retry. Because the race window is small.Is this a bug fix or mere optimization. I have hard time to tell from the description.
It is optimization. Thanks.
Quoted 21 lines
Quoted 7 lines
Signed-off-by Muchun Song <[email protected]>--- mm/hugetlb.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-)mm/hugetlb.c +0
-0[...]
mm/hugetlb.c int dissolve_free_huge_page(struct page *page) · line 1837
Quoted 11 lines
mm/hugetlb.c +7
-0@@ -1825,6 +1828,14 @@ int dissolve_free_huge_page(struct page *page) } out:spin_unlock (&hugetlb_lock); + +/* +* If the freeing of the HugeTLB page is put on a work queue, we should +* flush the work before retrying. +*/ +if (unlikely (rc == -EAGAIN )) +flush_work (&free_hpage_work);Is it safe to wait for the work to finish from this context?
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
Yes. It is safe.
Please expand on why in the changelog. Same for the optimization including some numbers showing it really helps.
OK. Changelog should be updated. Do you agree the race window is quite small?
Yes, the race is very rare and the window itself should be relatively small. This doesn't really answer the question about blocking though.
Quoted 5 lines
If so, why is it not an optimization? Don’t we dissolve the page as successfully as possible when we call
dissolve_free_huge_page()? I am confused about numbers showing. Because this is not a performance optimization, but an increase in the success rate of dissolving.
And it is a very theoretical one, right? Can you even trigger it? What would happen if the race is lost? Is it serious? This all would be part of the changelog ideally. This is a tricky area that spans hugetlb, memory hotplug and hwpoisoning. All of them tricky.
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v2 5/6 mm: hugetlb: fix a race between isolating and freeing page
In reply to Muchun SongThere is a race between isolate_huge_page() and __free_huge_page().
CPU0: CPU1:
if (PageHuge(page))
put_page(page)
__free_huge_page(page)
spin_lock(&hugetlb_lock)
update_and_free_page(page)
set_compound_page_dtor(page,
NULL_COMPOUND_DTOR)
spin_unlock(&hugetlb_lock)
isolate_huge_page(page)
// trigger BUG_ON
VM_BUG_ON_PAGE(!PageHead(page), page)
spin_lock(&hugetlb_lock)
page_huge_active(page)
// trigger BUG_ON
VM_BUG_ON_PAGE(!PageHuge(page), page)
spin_unlock(&hugetlb_lock)When we isolate a HugeTLB page on CPU0. Meanwhile, we free it to the buddy allocator on CPU1. Then, we can trigger a BUG_ON on CPU0. Because it is already freed to the buddy allocator.
--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/hugetlb.c +2 -2
@@ -5587,9 +5587,9 @@ bool isolate_huge_page(struct page *page, struct list_head *list) {bool ret = true; -VM_BUG_ON_PAGE (!PageHead (page), page);spin_lock (&hugetlb_lock); -if (!page_huge_active (page) || !get_page_unless_zero (page)) { +if (!PageHeadHuge (page) || !page_huge_active (page) || + !get_page_unless_zero (page)) { ret = false; goto unlock; }
Signature · 1 line
2.11.0
Re: [PATCH v2 5/6] mm: hugetlb: fix a race between isolating and freeing page
Quoted 21 lines · Muchun Song
There is a race between
isolate_huge_page()and__free_huge_page().CPU0: CPU1:
if (PageHuge(page))
put_page(page) __free_huge_page(page) spin_lock(&hugetlb_lock) update_and_free_page(page) set_compound_page_dtor(page, NULL_COMPOUND_DTOR) spin_unlock(&hugetlb_lock) isolate_huge_page(page) // trigger BUG_ON VM_BUG_ON_PAGE(!PageHead(page), page) spin_lock(&hugetlb_lock) page_huge_active(page) // trigger BUG_ON VM_BUG_ON_PAGE(!PageHuge(page), page) spin_unlock(&hugetlb_lock)
Quoted 7 lines
When we isolate a HugeTLB page on CPU0. Meanwhile, we free it to the buddy allocator on CPU1. Then, we can trigger a
BUG_ONon CPU0. Because it is already freed to the buddy allocator.Fixes c8721bbbdd36 ("mm: memory-hotplug: enable memory hotplug to handle hugepage")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>
Thanks!
Quoted 22 lines
--- mm/hugetlb.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)mm/hugetlb.c +2
-2@@ -5587,9 +5587,9 @@ bool isolate_huge_page(struct page *page, struct list_head *list) {bool ret = true; -VM_BUG_ON_PAGE (!PageHead (page), page);spin_lock (&hugetlb_lock); -if (!page_huge_active (page) || !get_page_unless_zero (page)) { +if (!PageHeadHuge (page) || !page_huge_active (page) || + !get_page_unless_zero (page)) { ret = false; goto unlock; }Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
PATCH v2 6/6 mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
In reply to Muchun SongThe page_huge_active() can be called from scan_movable_pages() which do not hold a reference count to the HugeTLB page. So when we call page_huge_active() from scan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger a BUG_ON which is in the page_huge_active() when CONFIG_DEBUG_VM is enabled. Just remove the VM_BUG_ON_PAGE.
--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)
mm/hugetlb.c +0 -1
@@ -1372,7 +1372,6 @@ struct hstate *size_to_hstate(unsigned long size)*/ bool page_huge_active (struct page *page) { -VM_BUG_ON_PAGE (!PageHuge (page), page);return PageHead (page) &&PagePrivate (&page[1 ]); }
Signature · 1 line
2.11.0
Re: [PATCH v2 6/6] mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
Quoted 10 lines · Muchun Song
The
page_huge_active()can be called fromscan_movable_pages()which do not hold a reference count to the HugeTLB page. So when we callpage_huge_active()fromscan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger aBUG_ONwhich is in thepage_huge_active()whenCONFIG_DEBUG_VMis enabled. Just remove theVM_BUG_ON_PAGE.Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>
The BUG_ON looks like a wrong thing to do regardless of the memory hotplug use. Which is admittedly ugly as well.
Quoted 18 lines
--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)mm/hugetlb.c +0
-1@@ -1372,7 +1372,6 @@ struct hstate *size_to_hstate(unsigned long size)*/ bool page_huge_active (struct page *page) { -VM_BUG_ON_PAGE (!PageHuge (page), page);return PageHead (page) &&PagePrivate (&page[1 ]); }Signature · 1 line
2.11.0
Signature · 2 lines
Michal Hocko SUSE Labs
Re: [PATCH v2 6/6] mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
In reply to Muchun SongQuoted 19 lines · Muchun Song
The
page_huge_active()can be called fromscan_movable_pages()which do not hold a reference count to the HugeTLB page. So when we callpage_huge_active()fromscan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger aBUG_ONwhich is in thepage_huge_active()whenCONFIG_DEBUG_VMis enabled. Just remove theVM_BUG_ON_PAGE.Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)mm/hugetlb.c +0
-0
mm/hugetlb.c struct hstate *size_to_hstate(unsigned long size) · line 1376
Quoted 6 lines
mm/hugetlb.c +0
-1@@ -1372,7 +1372,6 @@ struct hstate *size_to_hstate(unsigned long size)*/ bool page_huge_active (struct page *page) { -VM_BUG_ON_PAGE (!PageHuge (page), page);return PageHead (page) &&PagePrivate (&page[1 ]); }
After more thought, should that return statement be changed to?
return PageHeadHuge(page) && PagePrivate(&page[1]);
We only want to test that PagePrivate flag for hugetlb head pages. Although, the possibility that the hugetlb page was freed and turned into another compound page in this race window is REALLY small.
Signature · 1 line
Mike Kravetz
Re: [External] Re: [PATCH v2 6/6] mm: hugetlb: remove VM_BUG_ON_PAGE from page_huge_active
Quoted 29 lines · Mike Kravetz
Quoted 19 lines · Muchun Song
The
page_huge_active()can be called fromscan_movable_pages()which do not hold a reference count to the HugeTLB page. So when we callpage_huge_active()fromscan_movable_pages(), the HugeTLB page can be freed parallel. Then we will trigger aBUG_ONwhich is in thepage_huge_active()whenCONFIG_DEBUG_VMis enabled. Just remove theVM_BUG_ON_PAGE.Fixes 7e1f049efb86 ("mm: hugetlb: cleanup using paeg_huge_active()")Signed-off-by Muchun Song <[email protected]>Reviewed-by Mike Kravetz <[email protected]>--- mm/hugetlb.c | 1 - 1 file changed, 1 deletion(-)mm/hugetlb.c +0
-0mm/hugetlb.c struct hstate *size_to_hstate(unsigned long size) · line 1376
Quoted 6 lines
mm/hugetlb.c +0
-1@@ -1372,7 +1372,6 @@ struct hstate *size_to_hstate(unsigned long size)*/ bool page_huge_active (struct page *page) { -VM_BUG_ON_PAGE (!PageHuge (page), page);return PageHead (page) &&PagePrivate (&page[1 ]); }After more thought, should that return statement be changed to?
Quoted 1 line
return PageHeadHuge(page) && PagePrivate(&page[1]);
Agree.
Quoted 4 lines
We only want to test that PagePrivate flag for hugetlb head pages. Although, the possibility that the hugetlb page was freed and turned into another compound page in this race window is REALLY small.
Yeah. Thanks. I will update to PageHeadHuge().
Quoted 2 lines
--
Mike Kravetz
Quoted 15 lines · Muchun Song
This patch series aims to fix some bugs and add some improvements.
Changelog since v1 -> v2:
- Exportset_page_huge_active()in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.Muchun Song (6):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: add return-EAGAINfordissolve_free_huge_page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: removeVM_BUG_ON_PAGEfrompage_huge_active
Quoted 6 lines
fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 69 +++++++++++++++++++++++++++++++++++++++++++------ mm/migrate.c | 6 +++++ 4 files changed, 71 insertions(+), 9 deletions(-)
Repeating my question regarding ccing stable on selected fixes.
Signature · 3 lines
Thanks, David / dhildenb
Quoted 17 lines · David Hildenbrand
Quoted 15 lines · Muchun Song
This patch series aims to fix some bugs and add some improvements.
Changelog since v1 -> v2:
- Exportset_page_huge_active()in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.Muchun Song (6):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: add return-EAGAINfordissolve_free_huge_page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: removeVM_BUG_ON_PAGEfrompage_huge_active
Quoted 9 lines
Quoted 6 lines
fs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 69 +++++++++++++++++++++++++++++++++++++++++++------ mm/migrate.c | 6 +++++ 4 files changed, 71 insertions(+), 9 deletions(-)Repeating my question regarding ccing stable on selected fixes.
Just add a CC tag in the commit log of the fix patches? Right? Sorry, I'm a novice about this. Thanks.
Quoted 5 lines
--
Thanks,David / dhildenb
Quoted 25 lines · Muchun Song
Quoted 24 lines · David Hildenbrand
Quoted 21 lines · Muchun Song
This patch series aims to fix some bugs and add some improvements.
Changelog since v1 -> v2:
- Exportset_page_huge_active()in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.Muchun Song (6):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: add return-EAGAINfordissolve_free_huge_page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: removeVM_BUG_ON_PAGEfrompage_huge_activefs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 69 +++++++++++++++++++++++++++++++++++++++++++------ mm/migrate.c | 6 +++++ 4 files changed, 71 insertions(+), 9 deletions(-)
Quoted 5 lines
Quoted 2 lines
Repeating my question regarding ccing stable on selected fixes.
Just add a CC tag in the commit log of the fix patches? Right? Sorry, I'm a novice about this. Thanks.
Sure, here is some information:
https://www.kernel.org/doc/html/v4.10/process/stable-kernel-rules.html
Applicable patches should be moved to the beginning of the series.
Add "Cc: [email protected]" similar to "Fixes:" to the respective patches. In the ideal case, indicate the applicable earliest stable release where it applies.
E.g., take a look at (random commit)
commit 20b329129009caf1c646152abe09b697227e1c37
Author: Bob Peterson <[email protected]> Date: Wed Nov 18 08:54:31 2020 -0500
gfs2: Fix regression in freeze_go_sync
...
Fixes: 541656d3a513 ("gfs2: freeze should work on read-only mounts")
Cc: [email protected] # v5.8+...
Consequently, actually cc when sending out these patches (e.g., let "git send-email" do it automatically).
Signature · 3 lines
Thanks, David / dhildenb
Quoted 51 lines · David Hildenbrand
Quoted 25 lines · Muchun Song
Quoted 24 lines · David Hildenbrand
Quoted 21 lines · Muchun Song
This patch series aims to fix some bugs and add some improvements.
Changelog since v1 -> v2:
- Exportset_page_huge_active()in patch #2 to fix.
- Using head[3].mapping to indicate the page is freed in patch #3.
- Flush @free_hpage_work in patch #4.Muchun Song (6):
mm: migrate: do not migrate HugeTLB page whose refcount is one
mm: hugetlbfs: fix cannot migrate the fallocated HugeTLB page
mm: hugetlb: fix a race between freeing and dissolving the page
mm: hugetlb: add return-EAGAINfordissolve_free_huge_page
mm: hugetlb: fix a race between isolating and freeing page
mm: hugetlb: removeVM_BUG_ON_PAGEfrompage_huge_activefs/hugetlbfs/inode.c | 3 ++- include/linux/hugetlb.h | 2 ++ mm/hugetlb.c | 69 +++++++++++++++++++++++++++++++++++++++++++------ mm/migrate.c | 6 +++++ 4 files changed, 71 insertions(+), 9 deletions(-)Quoted 5 lines
Quoted 2 lines
Repeating my question regarding ccing stable on selected fixes.
Just add a CC tag in the commit log of the fix patches? Right? Sorry, I'm a novice about this. Thanks.
Sure, here is some information:
https://www.kernel.org/doc/html/v4.10/process/stable-kernel-rules.html
Applicable patches should be moved to the beginning of the series.
Add "Cc: [email protected]" similar to "Fixes:" to the respective patches. In the ideal case, indicate the applicable earliest stable release where it applies.
E.g., take a look at (random commit)
commit
20b329129009caf1c646152abe09b697227e1c37Author: Bob Peterson <[email protected]> Date: Wed Nov 18 08:54:31 2020 -0500gfs2: Fix regression in
freeze_go_sync
...
Quoted 7 lines
Fixes: 541656d3a513 ("gfs2: freeze should work on read-only mounts") Cc: [email protected] # v5.8+...
Consequently, actually cc when sending out these patches (e.g., let "git send-email" do it automatically).
Great! Very thanks.
Quoted 6 lines
--
Thanks,David / dhildenb