Skip to content

Commit ad97b9a

Browse files
Matthew Wilcox (Oracle)gregkh
authored andcommitted
migrate: correct lock ordering for hugetlb file folios
commit b7880cb upstream. Syzbot has found a deadlock (analyzed by Lance Yang): 1) Task (5749): Holds folio_lock, then tries to acquire i_mmap_rwsem(read lock). 2) Task (5754): Holds i_mmap_rwsem(write lock), then tries to acquire folio_lock. migrate_pages() -> migrate_hugetlbs() -> unmap_and_move_huge_page() <- Takes folio_lock! -> remove_migration_ptes() -> __rmap_walk_file() -> i_mmap_lock_read() <- Waits for i_mmap_rwsem(read lock)! hugetlbfs_fallocate() -> hugetlbfs_punch_hole() <- Takes i_mmap_rwsem(write lock)! -> hugetlbfs_zero_partial_page() -> filemap_lock_hugetlb_folio() -> filemap_lock_folio() -> __filemap_get_folio <- Waits for folio_lock! The migration path is the one taking locks in the wrong order according to the documentation at the top of mm/rmap.c. So expand the scope of the existing i_mmap_lock to cover the calls to remove_migration_ptes() too. This is (mostly) how it used to be after commit c0d0381. That was removed by 336bf30 for both file & anon hugetlb pages when it should only have been removed for anon hugetlb pages. Link: https://lkml.kernel.org/r/20260109041345.3863089-2-willy@infradead.org Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org> Fixes: 336bf30 ("hugetlbfs: fix anon huge page migration race") Reported-by: syzbot+2d9c96466c978346b55f@syzkaller.appspotmail.com Link: https://lore.kernel.org/all/68e9715a.050a0220.1186a4.000d.GAE@google.com Debugged-by: Lance Yang <lance.yang@linux.dev> Acked-by: David Hildenbrand (Red Hat) <david@kernel.org> Acked-by: Zi Yan <ziy@nvidia.com> Cc: Alistair Popple <apopple@nvidia.com> Cc: Byungchul Park <byungchul@sk.com> Cc: Gregory Price <gourry@gourry.net> Cc: Jann Horn <jannh@google.com> Cc: Joshua Hahn <joshua.hahnjy@gmail.com> Cc: Liam Howlett <liam.howlett@oracle.com> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Matthew Brost <matthew.brost@intel.com> Cc: Rakie Kim <rakie.kim@sk.com> Cc: Rik van Riel <riel@surriel.com> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: Ying Huang <ying.huang@linux.alibaba.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent ef6e608 commit ad97b9a

1 file changed

Lines changed: 6 additions & 8 deletions

File tree

mm/migrate.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,6 +1291,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
12911291
struct page *new_hpage;
12921292
struct anon_vma *anon_vma = NULL;
12931293
struct address_space *mapping = NULL;
1294+
enum ttu_flags ttu = 0;
12941295

12951296
/*
12961297
* Migratability of hugepages depends on architectures and their size.
@@ -1344,9 +1345,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
13441345
goto put_anon;
13451346

13461347
if (page_mapped(hpage)) {
1347-
bool mapping_locked = false;
1348-
enum ttu_flags ttu = 0;
1349-
13501348
if (!PageAnon(hpage)) {
13511349
/*
13521350
* In shared mappings, try_to_unmap could potentially
@@ -1358,23 +1356,23 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
13581356
if (unlikely(!mapping))
13591357
goto unlock_put_anon;
13601358

1361-
mapping_locked = true;
13621359
ttu |= TTU_RMAP_LOCKED;
13631360
}
13641361

13651362
try_to_migrate(hpage, ttu);
13661363
page_was_mapped = 1;
1367-
1368-
if (mapping_locked)
1369-
i_mmap_unlock_write(mapping);
13701364
}
13711365

13721366
if (!page_mapped(hpage))
13731367
rc = move_to_new_page(new_hpage, hpage, mode);
13741368

13751369
if (page_was_mapped)
13761370
remove_migration_ptes(hpage,
1377-
rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage, false);
1371+
rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage,
1372+
ttu ? true : false);
1373+
1374+
if (ttu & TTU_RMAP_LOCKED)
1375+
i_mmap_unlock_write(mapping);
13781376

13791377
unlock_put_anon:
13801378
unlock_page(new_hpage);

0 commit comments

Comments
 (0)