 migrate: correct lock ordering for hugetlb file folios · gregkh/linux@e7396d2 · 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 e7396d2 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 feb8243 commit e7396d2 Copy full SHA for e7396d2 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 @@ -1289,6 +1289,7 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, 1289 1289 struct page * new_hpage ; 1290 1290 struct anon_vma * anon_vma = NULL ; 1291 1291 struct address_space * mapping = NULL ; 1292 + enum ttu_flags ttu = TTU_MIGRATION | TTU_IGNORE_MLOCK ; 1292 1293 1293 1294 /* 1294 1295 * Migratability of hugepages depends on architectures and their size. @@ -1336,9 +1337,6 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, 1336 1337 goto put_anon ; 1337 1338 1338 1339 if ( page_mapped ( hpage )) { 1339 - bool mapping_locked = false; 1340 - enum ttu_flags ttu = TTU_MIGRATION | TTU_IGNORE_MLOCK ; 1341 - 1342 1340 if (! PageAnon ( hpage )) { 1343 1341 /* 1344 1342 * In shared mappings, try_to_unmap could potentially @@ -1350,23 +1348,23 @@ static int unmap_and_move_huge_page(new_page_t get_new_page, 1350 1348 if ( unlikely (! mapping )) 1351 1349 goto unlock_put_anon ; 1352 1350 1353 - mapping_locked = true; 1354 1351 ttu |= TTU_RMAP_LOCKED ; 1355 1352 } 1356 1353 1357 1354 try_to_unmap ( hpage , ttu ); 1358 1355 page_was_mapped = 1 ; 1359 - 1360 - if ( mapping_locked ) 1361 - i_mmap_unlock_write ( mapping ); 1362 1356 } 1363 1357 1364 1358 if (! page_mapped ( hpage )) 1365 1359 rc = move_to_new_page ( new_hpage , hpage , mode ); 1366 1360 1367 1361 if ( page_was_mapped ) 1368 1362 remove_migration_ptes ( hpage , 1369 - rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage , false); 1363 + rc == MIGRATEPAGE_SUCCESS ? new_hpage : hpage , 1364 + ( ttu &amp; TTU_RMAP_LOCKED ) ? true : false); 1365 + 1366 + if ( ttu &amp; TTU_RMAP_LOCKED ) 1367 + i_mmap_unlock_write ( mapping ); 1370 1368 1371 1369 unlock_put_anon : 1372 1370 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. 