Skip to content

Commit

Permalink
mm: convert partially_mapped set/clear operations to be atomic
Browse files Browse the repository at this point in the history
commit 42b2eb69835b0fda797f70eb5b4fc213dbe3a7ea upstream.

Other page flags in the 2nd page, like PG_hwpoison and PG_anon_exclusive
can get modified concurrently.  Changes to other page flags might be lost
if they are happening at the same time as non-atomic partially_mapped
operations.  Hence, make partially_mapped operations atomic.

Link: https://lkml.kernel.org/r/[email protected]
Fixes: 8422acd ("mm: introduce a pageflag for partially mapped folios")
Reported-by: David Hildenbrand <[email protected]>
Link: https://lore.kernel.org/all/[email protected]/
Signed-off-by: Usama Arif <[email protected]>
Acked-by: David Hildenbrand <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Acked-by: Roman Gushchin <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Domenico Cerasuolo <[email protected]>
Cc: Jonathan Corbet <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Mike Rapoport (Microsoft) <[email protected]>
Cc: Nico Pache <[email protected]>
Cc: Rik van Riel <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Shakeel Butt <[email protected]>
Cc: Yu Zhao <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
uarif1 authored and gregkh committed Dec 27, 2024
1 parent a095090 commit a086c8d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 14 deletions.
12 changes: 2 additions & 10 deletions include/linux/page-flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -860,18 +860,10 @@ static inline void ClearPageCompound(struct page *page)
ClearPageHead(page);
}
FOLIO_FLAG(large_rmappable, FOLIO_SECOND_PAGE)
FOLIO_TEST_FLAG(partially_mapped, FOLIO_SECOND_PAGE)
/*
* PG_partially_mapped is protected by deferred_split split_queue_lock,
* so its safe to use non-atomic set/clear.
*/
__FOLIO_SET_FLAG(partially_mapped, FOLIO_SECOND_PAGE)
__FOLIO_CLEAR_FLAG(partially_mapped, FOLIO_SECOND_PAGE)
FOLIO_FLAG(partially_mapped, FOLIO_SECOND_PAGE)
#else
FOLIO_FLAG_FALSE(large_rmappable)
FOLIO_TEST_FLAG_FALSE(partially_mapped)
__FOLIO_SET_FLAG_NOOP(partially_mapped)
__FOLIO_CLEAR_FLAG_NOOP(partially_mapped)
FOLIO_FLAG_FALSE(partially_mapped)
#endif

#define PG_head_mask ((1UL << PG_head))
Expand Down
8 changes: 4 additions & 4 deletions mm/huge_memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -3503,7 +3503,7 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list,
!list_empty(&folio->_deferred_list)) {
ds_queue->split_queue_len--;
if (folio_test_partially_mapped(folio)) {
__folio_clear_partially_mapped(folio);
folio_clear_partially_mapped(folio);
mod_mthp_stat(folio_order(folio),
MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
}
Expand Down Expand Up @@ -3615,7 +3615,7 @@ bool __folio_unqueue_deferred_split(struct folio *folio)
if (!list_empty(&folio->_deferred_list)) {
ds_queue->split_queue_len--;
if (folio_test_partially_mapped(folio)) {
__folio_clear_partially_mapped(folio);
folio_clear_partially_mapped(folio);
mod_mthp_stat(folio_order(folio),
MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
}
Expand Down Expand Up @@ -3659,7 +3659,7 @@ void deferred_split_folio(struct folio *folio, bool partially_mapped)
spin_lock_irqsave(&ds_queue->split_queue_lock, flags);
if (partially_mapped) {
if (!folio_test_partially_mapped(folio)) {
__folio_set_partially_mapped(folio);
folio_set_partially_mapped(folio);
if (folio_test_pmd_mappable(folio))
count_vm_event(THP_DEFERRED_SPLIT_PAGE);
count_mthp_stat(folio_order(folio), MTHP_STAT_SPLIT_DEFERRED);
Expand Down Expand Up @@ -3752,7 +3752,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink,
} else {
/* We lost race with folio_put() */
if (folio_test_partially_mapped(folio)) {
__folio_clear_partially_mapped(folio);
folio_clear_partially_mapped(folio);
mod_mthp_stat(folio_order(folio),
MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1);
}
Expand Down

0 comments on commit a086c8d

Please sign in to comment.