Commit 356ea386 authored by andrew.yang's avatar andrew.yang Committed by Linus Torvalds

mm/migrate: fix race between lock page and clear PG_Isolated

When memory is tight, system may start to compact memory for large
continuous memory demands.  If one process tries to lock a memory page
that is being locked and isolated for compaction, it may wait a long time
or even forever.  This is because compaction will perform non-atomic
PG_Isolated clear while holding page lock, this may overwrite PG_waiters
set by the process that can't obtain the page lock and add itself to the
waiting queue to wait for the lock to be unlocked.

  CPU1                            CPU2
  lock_page(page); (successful)
                                  lock_page(); (failed)
  __ClearPageIsolated(page);      SetPageWaiters(page) (may be overwritten)
  unlock_page(page);

The solution is to not perform non-atomic operation on page flags while
holding page lock.

Link: https://lkml.kernel.org/r/20220315030515.20263-1-andrew.yang@mediatek.comSigned-off-by: default avatarandrew.yang <andrew.yang@mediatek.com>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: "Vlastimil Babka" <vbabka@suse.cz>
Cc: David Howells <dhowells@redhat.com>
Cc: "William Kucharski" <william.kucharski@oracle.com>
Cc: David Hildenbrand <david@redhat.com>
Cc: Yang Shi <shy828301@gmail.com>
Cc: Marc Zyngier <maz@kernel.org>
Cc: Nicholas Tang <nicholas.tang@mediatek.com>
Cc: Kuan-Ying Lee <Kuan-Ying.Lee@mediatek.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent fc89213a
...@@ -1000,7 +1000,7 @@ PAGE_TYPE_OPS(Guard, guard) ...@@ -1000,7 +1000,7 @@ PAGE_TYPE_OPS(Guard, guard)
extern bool is_free_buddy_page(struct page *page); extern bool is_free_buddy_page(struct page *page);
__PAGEFLAG(Isolated, isolated, PF_ANY); PAGEFLAG(Isolated, isolated, PF_ANY);
#ifdef CONFIG_MMU #ifdef CONFIG_MMU
#define __PG_MLOCKED (1UL << PG_mlocked) #define __PG_MLOCKED (1UL << PG_mlocked)
......
...@@ -107,7 +107,7 @@ int isolate_movable_page(struct page *page, isolate_mode_t mode) ...@@ -107,7 +107,7 @@ int isolate_movable_page(struct page *page, isolate_mode_t mode)
/* Driver shouldn't use PG_isolated bit of page->flags */ /* Driver shouldn't use PG_isolated bit of page->flags */
WARN_ON_ONCE(PageIsolated(page)); WARN_ON_ONCE(PageIsolated(page));
__SetPageIsolated(page); SetPageIsolated(page);
unlock_page(page); unlock_page(page);
return 0; return 0;
...@@ -126,7 +126,7 @@ static void putback_movable_page(struct page *page) ...@@ -126,7 +126,7 @@ static void putback_movable_page(struct page *page)
mapping = page_mapping(page); mapping = page_mapping(page);
mapping->a_ops->putback_page(page); mapping->a_ops->putback_page(page);
__ClearPageIsolated(page); ClearPageIsolated(page);
} }
/* /*
...@@ -159,7 +159,7 @@ void putback_movable_pages(struct list_head *l) ...@@ -159,7 +159,7 @@ void putback_movable_pages(struct list_head *l)
if (PageMovable(page)) if (PageMovable(page))
putback_movable_page(page); putback_movable_page(page);
else else
__ClearPageIsolated(page); ClearPageIsolated(page);
unlock_page(page); unlock_page(page);
put_page(page); put_page(page);
} else { } else {
...@@ -883,7 +883,7 @@ static int move_to_new_page(struct page *newpage, struct page *page, ...@@ -883,7 +883,7 @@ static int move_to_new_page(struct page *newpage, struct page *page,
VM_BUG_ON_PAGE(!PageIsolated(page), page); VM_BUG_ON_PAGE(!PageIsolated(page), page);
if (!PageMovable(page)) { if (!PageMovable(page)) {
rc = MIGRATEPAGE_SUCCESS; rc = MIGRATEPAGE_SUCCESS;
__ClearPageIsolated(page); ClearPageIsolated(page);
goto out; goto out;
} }
...@@ -905,7 +905,7 @@ static int move_to_new_page(struct page *newpage, struct page *page, ...@@ -905,7 +905,7 @@ static int move_to_new_page(struct page *newpage, struct page *page,
* We clear PG_movable under page_lock so any compactor * We clear PG_movable under page_lock so any compactor
* cannot try to migrate this page. * cannot try to migrate this page.
*/ */
__ClearPageIsolated(page); ClearPageIsolated(page);
} }
/* /*
...@@ -1091,7 +1091,7 @@ static int unmap_and_move(new_page_t get_new_page, ...@@ -1091,7 +1091,7 @@ static int unmap_and_move(new_page_t get_new_page,
if (unlikely(__PageMovable(page))) { if (unlikely(__PageMovable(page))) {
lock_page(page); lock_page(page);
if (!PageMovable(page)) if (!PageMovable(page))
__ClearPageIsolated(page); ClearPageIsolated(page);
unlock_page(page); unlock_page(page);
} }
goto out; goto out;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment