On 11/12/2024 18:03, David Hildenbrand wrote: > On 30.08.24 12:03, Usama Arif wrote: >> Currently folio->_deferred_list is used to keep track of >> partially_mapped folios that are going to be split under memory >> pressure. In the next patch, all THPs that are faulted in and collapsed >> by khugepaged are also going to be tracked using _deferred_list. >> >> This patch introduces a pageflag to be able to distinguish between >> partially mapped folios and others in the deferred_list at split time in >> deferred_split_scan. Its needed as __folio_remove_rmap decrements >> _mapcount, _large_mapcount and _entire_mapcount, hence it won't be >> possible to distinguish between partially mapped folios and others in >> deferred_split_scan. >> >> Eventhough it introduces an extra flag to track if the folio is >> partially mapped, there is no functional change intended with this >> patch and the flag is not useful in this patch itself, it will >> become useful in the next patch when _deferred_list has non partially >> mapped folios. >> >> Signed-off-by: Usama Arif <usamaarif642@xxxxxxxxx> >> --- >> include/linux/huge_mm.h | 4 ++-- >> include/linux/page-flags.h | 13 +++++++++++- >> mm/huge_memory.c | 41 ++++++++++++++++++++++++++++---------- >> mm/memcontrol.c | 3 ++- >> mm/migrate.c | 3 ++- >> mm/page_alloc.c | 5 +++-- >> mm/rmap.c | 5 +++-- >> mm/vmscan.c | 3 ++- >> 8 files changed, 56 insertions(+), 21 deletions(-) >> >> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h >> index 4da102b74a8c..0b0539f4ee1a 100644 >> --- a/include/linux/huge_mm.h >> +++ b/include/linux/huge_mm.h >> @@ -333,7 +333,7 @@ static inline int split_huge_page(struct page *page) >> { >> return split_huge_page_to_list_to_order(page, NULL, 0); >> } >> -void deferred_split_folio(struct folio *folio); >> +void deferred_split_folio(struct folio *folio, bool partially_mapped); >> void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd, >> unsigned long address, bool freeze, struct folio *folio); >> @@ -502,7 +502,7 @@ static inline int split_huge_page(struct page *page) >> { >> return 0; >> } >> -static inline void deferred_split_folio(struct folio *folio) {} >> +static inline void deferred_split_folio(struct folio *folio, bool partially_mapped) {} >> #define split_huge_pmd(__vma, __pmd, __address) \ >> do { } while (0) >> diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h >> index 2175ebceb41c..1b3a76710487 100644 >> --- a/include/linux/page-flags.h >> +++ b/include/linux/page-flags.h >> @@ -186,6 +186,7 @@ enum pageflags { >> /* At least one page in this folio has the hwpoison flag set */ >> PG_has_hwpoisoned = PG_active, >> PG_large_rmappable = PG_workingset, /* anon or file-backed */ >> + PG_partially_mapped = PG_reclaim, /* was identified to be partially mapped */ >> }; >> #define PAGEFLAGS_MASK ((1UL << NR_PAGEFLAGS) - 1) >> @@ -859,8 +860,18 @@ 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. > > Just stumbled over that. In my understanding, this assumption is wrong. > > I don't think anything prevents other PF_ANY (PG_anon_exclusive, PG_PG_hwpoison) / PF_SECOND (PF_has_hwpoisoned) flags from getting modified concurrently I'm afraid. > Hi David, Just to clear my understanding, what you are suggesting could happen in __folio_set/clear_partially_mapped is: 1) __folio_set/clear_partially_mapped reads the 2nd page flags (x) where one of the other 2nd page flags is lets say not set. 2) One of the other 2nd page flags are set atomically. 3) __folio_set/clear_partially_mapped writes x + changes to partially_mapped. However, the change in step 2 to one of the other 2nd page flag is lost. Is that correct? But that would mean we shouldn't have any page flags (first or second page) as non atomic? although it would depend if they are being changed at the same time point. If you encountered a particular instance of PG_anon_exclusive or PF_has_hwpoisoned being changed at the same point as __folio_set/clear_partially_mapped, could you point to it? I am happy to send a fix to change all set/clear_partially_mapped to atomic, but just want to understand this better. Thanks! Usama