 migrate: correct lock ordering for hugetlb file folios · gregkh/linux@ad97b9a · GitHub Skip to content Navigation Menu Toggle navigation Sign in Appearance settings Platform AI CODE CREATION GitHub Copilot Write better code with AI GitHub Spark Build and deploy intelligent apps GitHub Models Manage and compare prompts MCP Registry New Integrate external tools DEVELOPER WORKFLOWS Actions Automate any workflow Codespaces Instant dev environments Issues Plan and track work Code Review Manage code changes APPLICATION SECURITY GitHub Advanced Security Find and fix vulnerabilities Code security Secure your code as you build Secret protection  EXPLORE Why GitHub Documentation Blog Changelog Marketplace View all features Solutions BY COMPANY SIZE Enterprises Small and medium teams Startups Nonprofits BY USE CASE App Modernization DevSecOps DevOps CI/CD View all use cases BY INDUSTRY Healthcare Financial services Manufacturing Government View all industries View all solutions Resources EXPLORE BY TOPIC AI Software Development DevOps Security View all topics EXPLORE BY TYPE Customer stories Events &amp; webinars Ebooks &amp; reports Business insights GitHub Skills SUPPORT &amp; SERVICES Documentation Customer support Community forum Trust center Partners View all resources Open Source COMMUNITY GitHub Sponsors Fund open source developers PROGRAMS Security Lab Maintainer Community Accelerator GitHub Stars Archive Program REPOSITORIES Topics Trending Collections Enterprise ENTERPRISE SOLUTIONS Enterprise platform AI-powered developer platform AVAILABLE ADD-ONS GitHub Advanced Security Enterprise-grade security features Copilot for Business Enterprise-grade AI features Premium Support Enterprise-grade 24/7 support Pricing Search or jump to... Search code, repositories, users, issues, pull requests... --> Search Clear Search syntax tips Provide feedback --> We read every piece of feedback, and take your input very seriously.  Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly --> Name Query To see all available qualifiers, see our documentation . Cancel Create saved search Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} gregkh / linux Public Notifications You must be signed in to change notification settings Fork 676 Star 636 Code Actions Security and quality 0 Insights Additional navigation options Code Actions Security and quality Insights Commit ad97b9a Browse files Browse files Matthew Wilcox (Oracle) authored and gregkh committed 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() -&gt; migrate_hugetlbs() -&gt; unmap_and_move_huge_page() &lt;- Takes folio_lock! -&gt; remove_migration_ptes() -&gt; __rmap_walk_file() -&gt; i_mmap_lock_read() &lt;- Waits for i_mmap_rwsem(read lock)! hugetlbfs_fallocate() -&gt; hugetlbfs_punch_hole() &lt;- Takes i_mmap_rwsem(write lock)! -&gt; hugetlbfs_zero_partial_page() -&gt; filemap_lock_hugetlb_folio() -&gt; filemap_lock_folio() -&gt; __filemap_get_folio &lt;- 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 &amp; 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) &lt;willy@infradead.org&gt; 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 &lt;lance.yang@linux.dev&gt; Acked-by: David Hildenbrand (Red Hat) &lt;david@kernel.org&gt; Acked-by: Zi Yan &lt;ziy@nvidia.com&gt; Cc: Alistair Popple &lt;apopple@nvidia.com&gt; Cc: Byungchul Park &lt;byungchul@sk.com&gt; Cc: Gregory Price &lt;gourry@gourry.net&gt; Cc: Jann Horn &lt;jannh@google.com&gt; Cc: Joshua Hahn &lt;joshua.hahnjy@gmail.com&gt; Cc: Liam Howlett &lt;liam.howlett@oracle.com&gt; Cc: Lorenzo Stoakes &lt;lorenzo.stoakes@oracle.com&gt; Cc: Matthew Brost &lt;matthew.brost@intel.com&gt; Cc: Rakie Kim &lt;rakie.kim@sk.com&gt; Cc: Rik van Riel &lt;riel@surriel.com&gt; Cc: Vlastimil Babka &lt;vbabka@suse.cz&gt; Cc: Ying Huang &lt;ying.huang@linux.alibaba.com&gt; Cc: &lt;stable@vger.kernel.org&gt; Signed-off-by: Andrew Morton &lt;akpm@linux-foundation.org&gt; Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt; 1 parent ef6e608 commit ad97b9a Copy full SHA for ad97b9a 1 file changed + 6 - 8 Lines changed: 6 additions &amp; 8 deletions File tree Expand file tree Collapse file tree Open diff view settings Filter options mm migrate.c Expand file tree Collapse file tree Open diff view settings Collapse file ‎ mm/migrate.c ‎ Copy file name to clipboard Expand all lines: mm/migrate.c + 6 - 8 Lines changed: 6 additions &amp; 8 deletions Original file line number Diff line number Diff line change @@ -1291,6 +1291,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, 1291 1291 struct page * new_hpage ; 1292 1292 struct anon_vma * anon_vma = NULL ; 1293 1293 struct address_space * mapping = NULL ; 1294 + enum ttu_flags ttu = 0 ; 1294 1295 1295 1296 /* 1296 1297 * 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, 1344 1345 goto put_anon ; 1345 1346 1346 1347 if ( page_mapped ( hpage )) { 1347 - bool mapping_locked = false; 1348 - enum ttu_flags ttu = 0 ; 1349 - 1350 1348 if (! PageAnon ( hpage )) { 1351 1349 /* 1352 1350 * 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, 1358 1356 if ( unlikely (! mapping )) 1359 1357 goto unlock_put_anon ; 1360 1358 1361 - mapping_locked = true; 1362 1359 ttu |= TTU_RMAP_LOCKED ; 1363 1360 } 1364 1361 1365 1362 try_to_migrate ( hpage , ttu ); 1366 1363 page_was_mapped = 1 ; 1367 - 1368 - if ( mapping_locked ) 1369 - i_mmap_unlock_write ( mapping ); 1370 1364 } 1371 1365 1372 1366 if (! page_mapped ( hpage )) 1373 1367 rc = move_to_new_page ( new_hpage , hpage , mode ); 1374 1368 1375 1369 if ( page_was_mapped ) 1376 1370 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 &amp; TTU_RMAP_LOCKED ) 1375 + i_mmap_unlock_write ( mapping ); 1378 1376 1379 1377 unlock_put_anon : 1380 1378 unlock_page ( new_hpage ); 0 commit comments Comments 0 ( 0 ) Footer &copy; 2026 GitHub,&nbsp;Inc. Footer navigation Terms Privacy Security Status Community Docs Contact Manage cookies Do not share my personal information You can’t perform that action at this time. 