Skip to content

Commit 21cc2b5

Browse files
name2965akpm00
authored andcommitted
mm/hugetlb: add missing hugetlb_lock in __unmap_hugepage_range()
When restoring a reservation for an anonymous page, we need to check to freeing a surplus. However, __unmap_hugepage_range() causes data race because it reads h->surplus_huge_pages without the protection of hugetlb_lock. And adjust_reservation is a boolean variable that indicates whether reservations for anonymous pages in each folio should be restored. Therefore, it should be initialized to false for each round of the loop. However, this variable is not initialized to false except when defining the current adjust_reservation variable. This means that once adjust_reservation is set to true even once within the loop, reservations for anonymous pages will be restored unconditionally in all subsequent rounds, regardless of the folio's state. To fix this, we need to add the missing hugetlb_lock, unlock the page_table_lock earlier so that we don't lock the hugetlb_lock inside the page_table_lock lock, and initialize adjust_reservation to false on each round within the loop. Link: https://lkml.kernel.org/r/[email protected] Fixes: df7a6d1 ("mm/hugetlb: restore the reservation if needed") Signed-off-by: Jeongjun Park <[email protected]> Reported-by: [email protected] Closes: https://syzkaller.appspot.com/bug?extid=417aeb05fd190f3a6da9 Reviewed-by: Sidhartha Kumar <[email protected]> Cc: Breno Leitao <[email protected]> Cc: David Hildenbrand <[email protected]> Cc: Muchun Song <[email protected]> Cc: Oscar Salvador <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 669602b commit 21cc2b5

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mm/hugetlb.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5851,7 +5851,7 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
58515851
spinlock_t *ptl;
58525852
struct hstate *h = hstate_vma(vma);
58535853
unsigned long sz = huge_page_size(h);
5854-
bool adjust_reservation = false;
5854+
bool adjust_reservation;
58555855
unsigned long last_addr_mask;
58565856
bool force_flush = false;
58575857

@@ -5944,21 +5944,24 @@ void __unmap_hugepage_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
59445944
sz);
59455945
hugetlb_count_sub(pages_per_huge_page(h), mm);
59465946
hugetlb_remove_rmap(folio);
5947+
spin_unlock(ptl);
59475948

59485949
/*
59495950
* Restore the reservation for anonymous page, otherwise the
59505951
* backing page could be stolen by someone.
59515952
* If there we are freeing a surplus, do not set the restore
59525953
* reservation bit.
59535954
*/
5955+
adjust_reservation = false;
5956+
5957+
spin_lock_irq(&hugetlb_lock);
59545958
if (!h->surplus_huge_pages && __vma_private_lock(vma) &&
59555959
folio_test_anon(folio)) {
59565960
folio_set_hugetlb_restore_reserve(folio);
59575961
/* Reservation to be adjusted after the spin lock */
59585962
adjust_reservation = true;
59595963
}
5960-
5961-
spin_unlock(ptl);
5964+
spin_unlock_irq(&hugetlb_lock);
59625965

59635966
/*
59645967
* Adjust the reservation for the region that will have the

0 commit comments

Comments
 (0)