mm: don't allocate page tables with the folio locked
Subsystems (6)
cover mm: don't allocate page tables with the folio locked
David asked for the remaining places that allocate with __GFP_FS while holding a folio lock [1]. The fault path and khugepaged still allocate page tables that way, and collapse_file() calls filemap_release_folio() and swaps in shmem folios with folio locks held.
Patch 1 allocates the page tables khugepaged needs before locking the folio.
Patch 2 makes finish_fault() retry the fault instead of allocating when the page table went away after __do_fault() checked the PMD. Patch 3 drops the allocation from do_set_pmd(), no caller needs it anymore. Patch 4 runs the part of collapse_file() that holds folio locks in a NOFS scope, like __folio_split().
I don't have a real deadlock for patch 4, it just does what [2] does for splitting, so I left it without a stable tag.
Based on mm/core.git for-next, patch 3 needs the do_set_pmd() changes there.
Tested on x86_64 and on ppc64le with the hash MMU in QEMU.
[1] https://lore.kernel.org/[email protected]
[2] https://lore.kernel.org/[email protected]
Jose A. Perez de Azpillaga (4):
mm/khugepaged: allocate page tables before locking the folio mm/memory: don't allocate in finish_fault() with the folio locked mm/memory: never allocate a page table in do_set_pmd() mm/khugepaged: use NOFS while collapse_file() holds folio locks
include/linux/mm.h | 3 +- mm/filemap.c | 2 +- mm/khugepaged.c | 91 ++++++++++++++++++++++++++++++++++------------ mm/memory.c | 39 ++++++++------------ 4 files changed, 85 insertions(+), 50 deletions(-)
base-commit: 98f6b507621822477db7ec7cef8519de0dd243c4
Signature · 1 line
2.56.1
PATCH 1/4 mm/khugepaged: allocate page tables before locking the folio
try_collapse_pte_mapped_thp() calls set_huge_pmd() with the folio locked. set_huge_pmd() allocates the missing p4d/pud/pmd tables there, and on ppc64 with the hash MMU do_set_pmd() also allocates the page table to deposit.
Allocating with __GFP_FS while holding a folio lock can deadlock in reclaim. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.
Allocate these page tables before locking the folio. The mmap lock keeps the upper levels around, and an unused deposit table is freed after unlocking the folio.
--- mm/khugepaged.c | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-)
mm/khugepaged.c +56 -23
@@ -1822,37 +1822,57 @@ static void collect_mm_slot(struct khugepaged_mm_slot *mm_slot) } } +/* +* Allocate the page tables needed to map a PMD at @haddr. Must be called +* before locking the folio: allocating with __GFP_FS while holding a folio +* lock can deadlock in reclaim. +*/ +static enum scan_result alloc_huge_pmd_tables (struct mm_struct *mm, +unsigned long haddr,pmd_t *pmd,pgtable_t *prealloc_pte) +{ +if (!pmd) { +pgd_t *pgd =pgd_offset (mm, haddr); +p4d_t *p4d; +pud_t *pud; + + p4d =p4d_alloc (mm, pgd, haddr); +if (!p4d) +return SCAN_FAIL ; + pud =pud_alloc (mm, p4d, haddr); +if (!pud) +return SCAN_FAIL ; +if (!pmd_alloc (mm, pud, haddr)) +return SCAN_FAIL ; + } + +/* Archs like ppc64 deposit a page table along with the PMD mapping. */ +if (arch_needs_pgtable_deposit ()) { + *prealloc_pte =pte_alloc_one (mm); +if (!*prealloc_pte) +return SCAN_FAIL ; + } +return SCAN_SUCCEED ; +} +/* folio must be locked, and mmap_lock must be held */ static enum scan_result set_huge_pmd (struct vm_area_struct *vma,unsigned long addr, -pmd_t *pmdp,struct folio *folio,struct page *page) +pmd_t *pmdp,struct folio *folio,struct page *page, +pgtable_t *prealloc_pte) { -struct mm_struct *mm = vma->vm_mm;struct vm_fault vmf = { .vma = vma, .address = addr, .flags =0 , + .pmd = pmdp, + .prealloc_pte = *prealloc_pte, }; -pgd_t *pgdp; -p4d_t *p4dp; -pud_t *pudp; +vm_fault_t ret;mmap_assert_locked (vma->vm_mm); -if (!pmdp) { - pgdp =pgd_offset (mm, addr); - p4dp =p4d_alloc (mm, pgdp, addr); -if (!p4dp) -return SCAN_FAIL ; - pudp =pud_alloc (mm, p4dp, addr); -if (!pudp) -return SCAN_FAIL ; - pmdp =pmd_alloc (mm, pudp, addr); -if (!pmdp) -return SCAN_FAIL ; - } - - vmf.pmd = pmdp; -if (do_set_pmd (&vmf, folio, page,/* may_alloc= */ true)) + ret =do_set_pmd (&vmf, folio, page,/* may_alloc= */ false); + *prealloc_pte = vmf.prealloc_pte; +if (ret)return SCAN_FAIL ;folio_get (folio);
@@ -1881,6 +1901,7 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsignunsigned long haddr = addr &HPAGE_PMD_MASK ;unsigned long end = haddr +HPAGE_PMD_SIZE ;struct vm_area_struct *vma =vma_lookup (mm, haddr); +pgtable_t prealloc_pte =NULL ;struct folio *folio;pte_t *start_pte, *pte;pmd_t *pmd, pgt_pmd;
@@ -1916,10 +1937,18 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsignif (userfaultfd_protected (vma))return SCAN_PTE_UFFD ; +if (install_pmd) { + result =alloc_huge_pmd_tables (mm, haddr, pmd, &prealloc_pte); +if (result !=SCAN_SUCCEED ) + goto free_pte; + } + folio =filemap_lock_folio (vma->vm_file->f_mapping,linear_page_index (vma, haddr)); -if (IS_ERR (folio)) -return SCAN_PAGE_NULL ; +if (IS_ERR (folio)) { + result =SCAN_PAGE_NULL ; + goto free_pte; + }if (!is_pmd_order (folio_order (folio))) { result =SCAN_PAGE_COMPOUND ;
@@ -2067,7 +2096,8 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign maybe_install_pmd:/* step 5: install pmd entry */ result = install_pmd - ?set_huge_pmd (vma, haddr, pmd, folio, &folio->page) + ?set_huge_pmd (vma, haddr, pmd, folio, &folio->page, + &prealloc_pte) :SCAN_SUCCEED ; goto drop_folio; abort:
@@ -2086,6 +2116,9 @@ static enum scan_result try_collapse_pte_mapped_thp(struct mm_struct *mm, unsign drop_folio:folio_unlock (folio);folio_put (folio); +free_pte: +if (prealloc_pte) +pte_free (mm, prealloc_pte);return result; }
Signature · 1 line
2.56.1
PATCH 2/4 mm/memory: don't allocate in finish_fault() with the folio locked
In reply to Jose A. Perez de Azpillaga__do_fault() preallocates a page table only when the PMD is empty, so that finish_fault() does not have to allocate one with the folio locked.
Since retract_page_tables() no longer takes the mmap and VMA locks, the page table can be removed while the fault waits in ->fault(). finish_fault() then finds an empty PMD without a preallocated table, and calls pte_alloc() with the folio locked. On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit in the same case.
Allocating with __GFP_FS while holding a folio lock can deadlock in reclaim, for example the memcg reclaim deadlock on cgroup v1 described in commit 63f3655f9501 ("mm, memcg: fix reclaim deadlock with writeback").
Return VM_FAULT_NOPAGE instead, as we already do when pte_offset_map_lock() fails. The retried fault preallocates the page table before locking the folio.
--- mm/memory.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
mm/memory.c +9 -4
@@ -5986,16 +5986,21 @@ vm_fault_t finish_fault(struct vm_fault *vmf) }if (pmd_none (*vmf->pmd)) { +/* +* The page table was removed after __do_fault() found it, so +* nothing was preallocated. Allocating with the folio locked +* can deadlock in reclaim, so retry the fault instead. +*/ +if (!vmf->prealloc_pte) +return VM_FAULT_NOPAGE ; +if (!needs_fallback &&folio_test_pmd_mappable (folio)) { ret =do_set_pmd (vmf, folio, page,/* may_alloc= */ true);if (ret !=VM_FAULT_FALLBACK )return ret; } -if (vmf->prealloc_pte) -pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); -else if (unlikely (pte_alloc (vma->vm_mm, vmf->pmd))) -return VM_FAULT_OOM ; +pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); } nr_pages =folio_nr_pages (folio);
Signature · 1 line
2.56.1
PATCH 3/4 mm/memory: never allocate a page table in do_set_pmd()
In reply to Jose A. Perez de Azpillagado_set_pmd() is always called with the folio locked, so it should not allocate the page table to deposit. finish_fault() and khugepaged now always pass one, and filemap_map_pmd() already passes may_alloc=false.
Drop may_alloc and fall back when no page table was preallocated.
No functional change intended.
--- include/linux/mm.h | 3 +-- mm/filemap.c | 2 +- mm/khugepaged.c | 2 +- mm/memory.c | 26 +++++++------------------- 4 files changed, 10 insertions(+), 23 deletions(-)
include/linux/mm.h +1 -2
@@ -2160,8 +2160,7 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)return pte; } -vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page, -bool may_alloc); +vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page);void set_pte_range (struct vm_fault *vmf,struct folio *folio,struct page *page,unsigned int nr,unsigned long addr);
mm/filemap.c +1 -1
@@ -3751,7 +3751,7 @@ static bool filemap_map_pmd(struct vm_fault *vmf, struct folio *folio,if (pmd_none (*vmf->pmd) &&folio_test_pmd_mappable (folio)) {struct page *page =folio_file_page (folio, start); -vm_fault_t ret =do_set_pmd (vmf, folio, page,/* may_alloc= */ false); +vm_fault_t ret =do_set_pmd (vmf, folio, page);if (!ret) {/* The page is mapped successfully, reference consumed. */ folio_unlock (folio);
mm/khugepaged.c +1 -1
@@ -1870,7 +1870,7 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long ammap_assert_locked (vma->vm_mm); - ret =do_set_pmd (&vmf, folio, page,/* may_alloc= */ false); + ret =do_set_pmd (&vmf, folio, page); *prealloc_pte = vmf.prealloc_pte;if (ret)return SCAN_FAIL ;
mm/memory.c +7 -19
@@ -5758,13 +5758,11 @@ static void deposit_prealloc_pte(struct vm_fault *vmf) vmf->prealloc_pte =NULL ; } -vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page, -bool may_alloc) +vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page) {struct vm_area_struct *vma = vmf->vma;bool write = vmf->flags &FAULT_FLAG_WRITE ;unsigned long haddr = vmf->address &HPAGE_PMD_MASK ; -bool pgtable_allocated = false;pmd_t entry;vm_fault_t ret =VM_FAULT_FALLBACK ;
@@ -5803,16 +5801,11 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa/* * Archs like ppc64 need additional space to store information -* related to pte entry. Use the preallocated table for that. +* related to pte entry. Use the table the caller preallocated +* before locking the folio for that. */ -if (arch_needs_pgtable_deposit () && !vmf->prealloc_pte) { -if (!may_alloc) -return VM_FAULT_FALLBACK ; - vmf->prealloc_pte =pte_alloc_one (vma->vm_mm); -if (!vmf->prealloc_pte) -return VM_FAULT_OOM ; - pgtable_allocated = true; - } +if (arch_needs_pgtable_deposit () && !vmf->prealloc_pte) +return ret; vmf->ptl =pmd_lock (vma->vm_mm, vmf->pmd);if (unlikely (!pmd_none (*vmf->pmd)))
@@ -5842,15 +5835,10 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pacount_vm_event (THP_FILE_MAPPED ); out:spin_unlock (vmf->ptl); -if (pgtable_allocated && vmf->prealloc_pte) { -pte_free (vma->vm_mm, vmf->prealloc_pte); - vmf->prealloc_pte =NULL ; - } return ret; }#else -vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page, -bool may_alloc) +vm_fault_t do_set_pmd (struct vm_fault *vmf,struct folio *folio,struct page *page) {return VM_FAULT_FALLBACK ; }
@@ -5995,7 +5983,7 @@ vm_fault_t finish_fault(struct vm_fault *vmf)return VM_FAULT_NOPAGE ;if (!needs_fallback &&folio_test_pmd_mappable (folio)) { - ret =do_set_pmd (vmf, folio, page,/* may_alloc= */ true); + ret =do_set_pmd (vmf, folio, page);if (ret !=VM_FAULT_FALLBACK )return ret; }
Signature · 1 line
2.56.1
PATCH 4/4 mm/khugepaged: use NOFS while collapse_file() holds folio locks
In reply to Jose A. Perez de Azpillagacollapse_file() keeps the folios it collapses locked until it is done. With those locks held, it calls filemap_release_folio() with GFP_KERNEL, and for shmem it swaps folios in with the mapping gfp, which includes __GFP_FS.
As when splitting folios, allocating with __GFP_FS while holding folio locks can deadlock in direct reclaim or compaction, and __GFP_FS also lets filemap_release_folio() wait on the filesystem.
Run the part that holds folio locks in a NOFS scope, as __folio_split() does. The huge folio is still allocated before that.
--- mm/khugepaged.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-)
mm/khugepaged.c +11 -1
@@ -2302,6 +2302,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,LIST_HEAD (pagelist);XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );enum scan_result result =SCAN_SUCCEED ; +unsigned int nofs_flags;int nr_none =0 ;bool do_readahead = false;bool readahead_done = false;
@@ -2320,6 +2321,13 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, goto out; new_pfn =folio_pfn (new_folio); +/* +* We allocate while holding folio locks below, when swapping in shmem +* folios and in filemap_release_folio(). Allocating with __GFP_FS +* there can deadlock in reclaim or compaction. +*/ + nofs_flags =memalloc_nofs_save (); +mapping_set_update (&xas, mapping); retry:
@@ -2698,7 +2706,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,folio_put_refs (folio,2 +folio_nr_pages (folio)); } - goto out; + goto out_nofs; rollback:/* Something went wrong: roll back page cache changes */
@@ -2744,6 +2752,8 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, } out_put:folio_put (new_folio); +out_nofs: +memalloc_nofs_restore (nofs_flags); out:VM_BUG_ON (!list_empty (&pagelist));trace_mm_khugepaged_collapse_file (mm, new_pfn, index, addr, is_shmem, file,HPAGE_PMD_NR , result);
Signature · 1 line
2.56.1
David Hildenbrand (Arm)
maintainerRe: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
Quoted 8 lines · Jose A. Perez de Azpillaga
collapse_file()keeps the folios it collapses locked until it is done. With those locks held, it callsfilemap_release_folio()withGFP_KERNEL, and for shmem it swaps folios in with the mapping gfp, which includes__GFP_FS.As when splitting folios, allocating with
__GFP_FSwhile holding folio locks can deadlock in direct reclaim or compaction, and__GFP_FSalso letsfilemap_release_folio()wait on the filesystem.
How is that possible with my fixes?
Signature · 3 lines
Cheers, David
Re: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 8 lines · Jose A. Perez de Azpillaga
collapse_file()keeps the folios it collapses locked until it is done. With those locks held, it callsfilemap_release_folio()withGFP_KERNEL, and for shmem it swaps folios in with the mapping gfp, which includes__GFP_FS.As when splitting folios, allocating with
__GFP_FSwhile holding folio locks can deadlock in direct reclaim or compaction, and__GFP_FSalso letsfilemap_release_folio()wait on the filesystem.How is that possible with my fixes?
it isn't, sorry. I'll drop 4/4.
Signature · 2 lines
cheers, jose a. p-a
David Hildenbrand (Arm)
maintainerRe: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
Quoted 7 lines · Jose A. Perez de Azpillaga
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 5 lines · Jose A. Perez de Azpillaga
collapse_file()keeps the folios it collapses locked until it is done. With those locks held, it callsfilemap_release_folio()withGFP_KERNEL, and for shmem it swaps folios in with the mapping gfp, which includes__GFP_FS.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
As when splitting folios, allocating with
__GFP_FSwhile holding folio locks can deadlock in direct reclaim or compaction, and__GFP_FSalso letsfilemap_release_folio()wait on the filesystem.How is that possible with my fixes?
it isn't, sorry. I'll drop 4/4.
So there are no other allocations in there besides the one done by splitting?
Signature · 3 lines
Cheers, David
Re: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
Quoted 8 lines · David Hildenbrand (Arm)
Quoted 7 lines · Jose A. Perez de Azpillaga
Quoted 6 lines · David Hildenbrand (Arm)
Quoted 5 lines · Jose A. Perez de Azpillaga
collapse_file()keeps the folios it collapses locked until it is done. With those locks held, it callsfilemap_release_folio()withGFP_KERNEL, and for shmem it swaps folios in with the mapping gfp, which includes__GFP_FS.
Quoted 7 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
As when splitting folios, allocating with
__GFP_FSwhile holding folio locks can deadlock in direct reclaim or compaction, and__GFP_FSalso letsfilemap_release_folio()wait on the filesystem.How is that possible with my fixes?
it isn't, sorry. I'll drop 4/4.
So there are no other allocations in there besides the one done by splitting?
there are, the shmem swapin, but they can't deadlock.
Signature · 2 lines
cheers, jose a. p-a
David Hildenbrand (Arm)
maintainerRe: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
Quoted 6 lines · Jose A. Perez de Azpillaga
Quoted 3 lines · David Hildenbrand (Arm)
Quoted 1 line · Jose A. Perez de Azpillaga
it isn't, sorry. I'll drop 4/4.
So there are no other allocations in there besides the one done by splitting?
there are, the shmem swapin, but they can't deadlock.
Okay, good.
Signature · 3 lines
Cheers, David
[email protected]
AI reviewRe: [PATCH 4/4] mm/khugepaged: use NOFS while collapse_file() holds folio locks
In reply to Jose A. Perez de AzpillagaThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] mm/khugepaged: filemap_release_folio() still gets __GFP_FS in collapse_file() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] mm/khugepaged: filemap_release_folio() still gets __GFP_FS in collapse_file()
--
commit eca7ae08f59655cebfe256885070eec102a63325
Author: Jose A. Perez de Azpillaga <[email protected]>
mm/khugepaged: use NOFS while collapse_file() holds folio locks
This commit wraps the folio-locked section of collapse_file() in a memalloc_nofs_save() scope to avoid __GFP_FS allocations and filesystem waits while holding folio locks.
Quoted 13 lines
mm/khugepaged.c +1
-0@@ -2302,6 +2302,7 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr,LIST_HEAD (pagelist);XA_STATE_ORDER (xas, &mapping->i_pages, start,HPAGE_PMD_ORDER );enum scan_result result =SCAN_SUCCEED ; +unsigned int nofs_flags;int nr_none =0 ;bool do_readahead = false;bool readahead_done = false;
mm/khugepaged.c static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, · line 2329
Quoted 9 lines
mm/khugepaged.c +6
-0@@ -2320,6 +2321,13 @@ static enum scan_result collapse_file(struct mm_struct *mm, unsigned long addr, goto out; new_pfn =folio_pfn (new_folio); +/* +* We allocate while holding folio locks below, when swapping in shmem +* folios and in filemap_release_folio(). Allocating with __GFP_FS +* there can deadlock in reclaim or compaction. +*/ + nofs_flags =memalloc_nofs_save ();
[Severity: Medium]
Does filemap_release_folio() in collapse_file() still receive __GFP_FS despite memalloc_nofs_save()?
collapse_file() passes a literal GFP_KERNEL to filemap_release_folio(folio, GFP_KERNEL), and filemap_release_folio() forwards that mask unmodified to mapping->a_ops->release_folio(folio, gfp) without applying current_gfp_context():
fs/netfs/misc.c:netfs_release_folio() {
if (unlikely(folio_test_private_2(folio))) { /* [DEPRECATED] */
if (current_is_kswapd() || !(gfp & __GFP_FS))
return false;
folio_wait_private_2(folio);
}
return fscache_release_folio(folio, gfp);
}Callbacks such as netfs_release_folio() and nfs_fscache_release_folio() test gfp & __GFP_FS directly, so they will still see __GFP_FS set and wait in folio_wait_private_2() while collapse_file() holds folio locks. Compare with __folio_freeze_split_file(), which passes current_gfp_context() (or GFP_NOFS) to filemap_release_folio().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
David Hildenbrand (Arm)
maintainerRe: [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio
In reply to Jose A. Perez de AzpillagaQuoted 13 lines · Jose A. Perez de Azpillaga
try_collapse_pte_mapped_thp()callsset_huge_pmd()with the folio locked.set_huge_pmd()allocates the missing p4d/pud/pmd tables there, and on ppc64 with the hash MMUdo_set_pmd()also allocates the page table to deposit.Allocating with
__GFP_FSwhile holding a folio lock can deadlock in reclaim. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.Allocate these page tables before locking the folio. The mmap lock keeps the upper levels around, and an unused deposit table is freed after unlocking the folio.
Quoted 8 lines
Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")Signed-off-by Jose A. Perez de Azpillaga <[email protected]>--- mm/khugepaged.c | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-)
That's a lot of churn for a corner case (powerpc with hash MMU).
What about simply running in NOFS context when allcoating that page table?
Apparently pte_alloc_one() doesn't consume GFP flags ... but we could add NOFS around the allocation.
Any reason you went for this more complicated solution?
Signature · 3 lines
Cheers, David
David Hildenbrand (Arm)
maintainerRe: [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked
In reply to Jose A. Perez de AzpillagaQuoted 30 lines · Jose A. Perez de Azpillaga
__do_fault()preallocates a page table only when the PMD is empty, so thatfinish_fault()does not have to allocate one with the folio locked.Since
retract_page_tables()no longer takes the mmap and VMA locks, the page table can be removed while the fault waits in ->fault().finish_fault()then finds an empty PMD without a preallocated table, and callspte_alloc()with the folio locked. On ppc64 with the hash MMU,do_set_pmd()allocates the page table to deposit in the same case.Allocating with
__GFP_FSwhile holding a folio lock can deadlock in reclaim, for example the memcg reclaim deadlock on cgroup v1 described in commit63f3655f9501("mm, memcg: fix reclaim deadlock with writeback").Return
VM_FAULT_NOPAGEinstead, as we already do whenpte_offset_map_lock()fails. The retried fault preallocates the page table before locking the folio.Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")Signed-off-by Jose A. Perez de Azpillaga <[email protected]>--- mm/memory.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)mm/memory.c +0
-0
mm/memory.c vm_fault_t finish_fault(struct vm_fault *vmf) · line 5995
Quoted 10 lines
mm/memory.c +7
-0@@ -5986,16 +5986,21 @@ vm_fault_t finish_fault(struct vm_fault *vmf) }if (pmd_none (*vmf->pmd)) { +/* +* The page table was removed after __do_fault() found it, so +* nothing was preallocated. Allocating with the folio locked +* can deadlock in reclaim, so retry the fault instead. +*/ +if (!vmf->prealloc_pte) +return VM_FAULT_NOPAGE ;
Hm, but do_set_pmd() really only needs one for ppc64. Assuming we allocate it as NOFS in there for that corner cases ...
mm/memory.c vm_fault_t finish_fault(struct vm_fault *vmf) · line 5998
Quoted 11 lines
mm/memory.c +1
-4+if (!needs_fallback &&folio_test_pmd_mappable (folio)) { ret =do_set_pmd (vmf, folio, page,/* may_alloc= */ true);if (ret !=VM_FAULT_FALLBACK )return ret; } -if (vmf->prealloc_pte) -pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); -else if (unlikely (pte_alloc (vma->vm_mm, vmf->pmd))) -return VM_FAULT_OOM ;
Could we instead fallback here, after we tried to install the PMD ?
Signature · 3 lines
Cheers, David
Re: [PATCH 1/4] mm/khugepaged: allocate page tables before locking the folio
In reply to David Hildenbrand (Arm)Quoted 23 lines · David Hildenbrand (Arm)
Quoted 13 lines · Jose A. Perez de Azpillaga
try_collapse_pte_mapped_thp()callsset_huge_pmd()with the folio locked.set_huge_pmd()allocates the missing p4d/pud/pmd tables there, and on ppc64 with the hash MMUdo_set_pmd()also allocates the page table to deposit.Allocating with
__GFP_FSwhile holding a folio lock can deadlock in reclaim. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.Allocate these page tables before locking the folio. The mmap lock keeps the upper levels around, and an unused deposit table is freed after unlocking the folio.
Quoted 8 lines
Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")Signed-off-by Jose A. Perez de Azpillaga <[email protected]>--- mm/khugepaged.c | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-)
Quoted 9 lines
That's a lot of churn for a corner case (powerpc with hash MMU).
What about simply running in NOFS context when allcoating that page table?
Apparently
pte_alloc_one()doesn't consume GFP flags ... but we could add NOFS around the allocation.Any reason you went for this more complicated solution?
no reason, it was just the solution I came up with. I'll rework it with NOFS then, it's a lot less churn.
Signature · 2 lines
cheers, jose a. p-a
Re: [PATCH 2/4] mm/memory: don't allocate in finish_fault() with the folio locked
In reply to David Hildenbrand (Arm)Quoted 33 lines · David Hildenbrand (Arm)
Quoted 32 lines · Jose A. Perez de Azpillaga
__do_fault()preallocates a page table only when the PMD is empty, so thatfinish_fault()does not have to allocate one with the folio locked.Since
retract_page_tables()no longer takes the mmap and VMA locks, the page table can be removed while the fault waits in ->fault().finish_fault()then finds an empty PMD without a preallocated table, and callspte_alloc()with the folio locked. On ppc64 with the hash MMU,do_set_pmd()allocates the page table to deposit in the same case.Allocating with
__GFP_FSwhile holding a folio lock can deadlock in reclaim, for example the memcg reclaim deadlock on cgroup v1 described in commit63f3655f9501("mm, memcg: fix reclaim deadlock with writeback").Return
VM_FAULT_NOPAGEinstead, as we already do whenpte_offset_map_lock()fails. The retried fault preallocates the page table before locking the folio.Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")Signed-off-by Jose A. Perez de Azpillaga <[email protected]>--- mm/memory.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)mm/memory.c +0
-0@@ -5986,16 +5986,21 @@ vm_fault_t finish_fault(struct vm_fault *vmf) }
Quoted 12 lines
mm/memory.c vm_fault_t finish_fault(struct vm_fault *vmf) · line 5995
Quoted 8 lines
mm/memory.c +7
-0if (pmd_none (*vmf->pmd)) { +/* +* The page table was removed after __do_fault() found it, so +* nothing was preallocated. Allocating with the folio locked +* can deadlock in reclaim, so retry the fault instead. +*/ +if (!vmf->prealloc_pte) +return VM_FAULT_NOPAGE ;Hm, but
do_set_pmd()really only needs one for ppc64. Assuming we allocate it as NOFS in there for that corner cases ...
yes, I'll allocate it with NOFS in do_set_pmd() instead of 3/4.
Quoted 7 lines
Quoted 7 lines
mm/memory.c +1
-0+if (!needs_fallback &&folio_test_pmd_mappable (folio)) { ret =do_set_pmd (vmf, folio, page,/* may_alloc= */ true);if (ret !=VM_FAULT_FALLBACK )return ret; }
Quoted 6 lines
mm/memory.c vm_fault_t finish_fault(struct vm_fault *vmf) · line 5998
Quoted 4 lines
mm/memory.c +0
-4-if (vmf->prealloc_pte) -pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); -else if (unlikely (pte_alloc (vma->vm_mm, vmf->pmd))) -return VM_FAULT_OOM ;Could we instead fallback here, after we tried to install the PMD ?
sure, I'll return VM_FAULT_NOPAGE there when nothing was preallocated :)
Signature · 2 lines
cheers, jose a. p-a