Skip to content

Commit 527cabf

Browse files
mjkravetzgregkh
authored andcommitted
hugetlbfs: fix races and page leaks during migration
commit cb6acd0 upstream. hugetlb pages should only be migrated if they are 'active'. The routines set/clear_page_huge_active() modify the active state of hugetlb pages. When a new hugetlb page is allocated at fault time, set_page_huge_active is called before the page is locked. Therefore, another thread could race and migrate the page while it is being added to page table by the fault code. This race is somewhat hard to trigger, but can be seen by strategically adding udelay to simulate worst case scheduling behavior. Depending on 'how' the code races, various BUG()s could be triggered. To address this issue, simply delay the set_page_huge_active call until after the page is successfully added to the page table. Hugetlb pages can also be leaked at migration time if the pages are associated with a file in an explicitly mounted hugetlbfs filesystem. For example, consider a two node system with 4GB worth of huge pages available. A program mmaps a 2G file in a hugetlbfs filesystem. It then migrates the pages associated with the file from one node to another. When the program exits, huge page counts are as follows: node0 1024 free_hugepages 1024 nr_hugepages node1 0 free_hugepages 1024 nr_hugepages Filesystem Size Used Avail Use% Mounted on nodev 4.0G 2.0G 2.0G 50% /var/opt/hugepool That is as expected. 2G of huge pages are taken from the free_hugepages counts, and 2G is the size of the file in the explicitly mounted filesystem. If the file is then removed, the counts become: node0 1024 free_hugepages 1024 nr_hugepages node1 1024 free_hugepages 1024 nr_hugepages Filesystem Size Used Avail Use% Mounted on nodev 4.0G 2.0G 2.0G 50% /var/opt/hugepool Note that the filesystem still shows 2G of pages used, while there actually are no huge pages in use. The only way to 'fix' the filesystem accounting is to unmount the filesystem If a hugetlb page is associated with an explicitly mounted filesystem, this information in contained in the page_private field. At migration time, this information is not preserved. To fix, simply transfer page_private from old to new page at migration time if necessary. There is a related race with removing a huge page from a file and migration. When a huge page is removed from the pagecache, the page_mapping() field is cleared, yet page_private remains set until the page is actually freed by free_huge_page(). A page could be migrated while in this state. However, since page_mapping() is not set the hugetlbfs specific routine to transfer page_private is not called and we leak the page count in the filesystem. To fix that, check for this condition before migrating a huge page. If the condition is detected, return EBUSY for the page. Link: http://lkml.kernel.org/r/74510272-7319-7372-9ea6-ec914734c179@oracle.com Link: http://lkml.kernel.org/r/20190212221400.3512-1-mike.kravetz@oracle.com Fixes: bcc5422 ("mm: hugetlb: introduce page_huge_active") Signed-off-by: Mike Kravetz <mike.kravetz@oracle.com> Reviewed-by: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Andrea Arcangeli <aarcange@redhat.com> Cc: "Kirill A . Shutemov" <kirill.shutemov@linux.intel.com> Cc: Mel Gorman <mgorman@techsingularity.net> Cc: Davidlohr Bueso <dave@stgolabs.net> Cc: <stable@vger.kernel.org> [mike.kravetz@oracle.com: v2] Link: http://lkml.kernel.org/r/7534d322-d782-8ac6-1c8d-a8dc380eb3ab@oracle.com [mike.kravetz@oracle.com: update comment and changelog] Link: http://lkml.kernel.org/r/420bcfd6-158b-38e4-98da-26d0cd85bd01@oracle.com Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent f0233ca commit 527cabf

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎fs/hugetlbfs/inode.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,18 @@ static int hugetlbfs_migrate_page(struct address_space *mapping,
859859
rc = migrate_huge_page_move_mapping(mapping, newpage, page);
860860
if (rc != MIGRATEPAGE_SUCCESS)
861861
return rc;
862+
863+
/*
864+
* page_private is subpool pointer in hugetlb pages. Transfer to
865+
* new page. PagePrivate is not associated with page_private for
866+
* hugetlb pages and can not be set here as only page_huge_active
867+
* pages can be migrated.
868+
*/
869+
if (page_private(page)) {
870+
set_page_private(newpage, page_private(page));
871+
set_page_private(page, 0);
872+
}
873+
862874
if (mode != MIGRATE_SYNC_NO_COPY)
863875
migrate_page_copy(newpage, page);
864876
else

‎mm/hugetlb.c‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3624,7 +3624,6 @@ static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
36243624
copy_user_huge_page(new_page, old_page, address, vma,
36253625
pages_per_huge_page(h));
36263626
__SetPageUptodate(new_page);
3627-
set_page_huge_active(new_page);
36283627

36293628
mmun_start = haddr;
36303629
mmun_end = mmun_start + huge_page_size(h);
@@ -3646,6 +3645,7 @@ static vm_fault_t hugetlb_cow(struct mm_struct *mm, struct vm_area_struct *vma,
36463645
make_huge_pte(vma, new_page, 1));
36473646
page_remove_rmap(old_page, true);
36483647
hugepage_add_new_anon_rmap(new_page, vma, haddr);
3648+
set_page_huge_active(new_page);
36493649
/* Make the old page be freed below */
36503650
new_page = old_page;
36513651
}
@@ -3730,6 +3730,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
37303730
pte_t new_pte;
37313731
spinlock_t *ptl;
37323732
unsigned long haddr = address & huge_page_mask(h);
3733+
bool new_page = false;
37333734

37343735
/*
37353736
* Currently, we are forced to kill the process in the event the
@@ -3791,7 +3792,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
37913792
}
37923793
clear_huge_page(page, address, pages_per_huge_page(h));
37933794
__SetPageUptodate(page);
3794-
set_page_huge_active(page);
3795+
new_page = true;
37953796

37963797
if (vma->vm_flags & VM_MAYSHARE) {
37973798
int err = huge_add_to_page_cache(page, mapping, idx);
@@ -3862,6 +3863,15 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm,
38623863
}
38633864

38643865
spin_unlock(ptl);
3866+
3867+
/*
3868+
* Only make newly allocated pages active. Existing pages found
3869+
* in the pagecache could be !page_huge_active() if they have been
3870+
* isolated for migration.
3871+
*/
3872+
if (new_page)
3873+
set_page_huge_active(page);
3874+
38653875
unlock_page(page);
38663876
out:
38673877
return ret;
@@ -4096,7 +4106,6 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
40964106
* the set_pte_at() write.
40974107
*/
40984108
__SetPageUptodate(page);
4099-
set_page_huge_active(page);
41004109

41014110
mapping = dst_vma->vm_file->f_mapping;
41024111
idx = vma_hugecache_offset(h, dst_vma, dst_addr);
@@ -4164,6 +4173,7 @@ int hugetlb_mcopy_atomic_pte(struct mm_struct *dst_mm,
41644173
update_mmu_cache(dst_vma, dst_addr, dst_pte);
41654174

41664175
spin_unlock(ptl);
4176+
set_page_huge_active(page);
41674177
if (vm_shared)
41684178
unlock_page(page);
41694179
ret = 0;

‎mm/migrate.c‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,16 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
13031303
lock_page(hpage);
13041304
}
13051305

1306+
/*
1307+
* Check for pages which are in the process of being freed. Without
1308+
* page_mapping() set, hugetlbfs specific move page routine will not
1309+
* be called and we could leak usage counts for subpools.
1310+
*/
1311+
if (page_private(hpage) && !page_mapping(hpage)) {
1312+
rc = -EBUSY;
1313+
goto out_unlock;
1314+
}
1315+
13061316
if (PageAnon(hpage))
13071317
anon_vma = page_get_anon_vma(hpage);
13081318

@@ -1333,6 +1343,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
13331343
put_new_page = NULL;
13341344
}
13351345

1346+
out_unlock:
13361347
unlock_page(hpage);
13371348
out:
13381349
if (rc != -EAGAIN)

0 commit comments

Comments
 (0)