mm: don't allocate page tables with the folio locked
Subsystems (5)
v3 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.
Patch 1 allocates the deposit table in do_set_pmd() in a NOFS scope. Patch 2 does the same for the tables set_huge_pmd() allocates. Patch 3 makes finish_fault() retry the fault instead of allocating when the page table went away after __do_fault() checked the PMD.
Tested on x86_64 and on ppc64le with the hash MMU in QEMU.
[1] https://lore.kernel.org/[email protected]
Changes in v3:
- 1/3, 2/3: rename nofs_flags to flags, one-line comments (Lorenzo)
- 3/3: new finish_fault() comment, best-effort note at the __do_fault() preallocation, NOPAGE retry in the changelog (Lorenzo)
- shorter changelogs, memcg v1 example only in 1/3 (Lorenzo)
- collect tags
Changes in v2:
- use NOFS in do_set_pmd() and set_huge_pmd() instead of preallocating in khugepaged and dropping the allocation from do_set_pmd() (David)
- finish_fault(): return VM_FAULT_NOPAGE after trying do_set_pmd() (David)
- drop patch 4
- rebase on mm/core.git for-next
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]
Jose A. Perez de Azpillaga (3):
mm/memory: allocate the deposit page table with NOFS in do_set_pmd() mm/khugepaged: allocate page tables with NOFS in set_huge_pmd() mm/memory: don't allocate in finish_fault() with the folio locked
mm/khugepaged.c | 13 +++++++------ mm/memory.c | 19 +++++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-)
base-commit: 89b4b2b15b2063e1bf593e609c3c63313b7ff160
Signature · 1 line
2.56.1
PATCH v3 1/3 mm/memory: allocate the deposit page table with NOFS in do_set_pmd()
ppc64 allocates a page table in do_set_pmd() when it's not preallocated while callers of it hold the folio lock, e.g. set_huge_pmd() and finish_fault() (under certain circumstances).
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").
pte_alloc_one() takes no gfp flags, so allocate the table in a NOFS scope.
---
Changes in v3:
- rename nofs_flags to flags, one-line comment (Lorenzo)
- shorter changelog opener (Lorenzo)
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in do_set_pmd()" (David)
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]
mm/memory.c | 5 +++++ 1 file changed, 5 insertions(+)
mm/memory.c +5 -0
@@ -5806,9 +5806,14 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa* related to pte entry. Use the preallocated table for that. */ if (arch_needs_pgtable_deposit () && !vmf->prealloc_pte) { +unsigned int flags; +if (!may_alloc)return VM_FAULT_FALLBACK ; +/* Folio is locked, so avoid deadlock against concurrent reclaim */ + flags =memalloc_nofs_save (); vmf->prealloc_pte =pte_alloc_one (vma->vm_mm); +memalloc_nofs_restore (flags);if (!vmf->prealloc_pte)return VM_FAULT_OOM ; pgtable_allocated = true;
Signature · 1 line
2.56.1
PATCH v3 2/3 mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()
In reply to Jose A. Perez de Azpillagatry_collapse_pte_mapped_thp() calls set_huge_pmd() with the folio locked. When there is no PMD table, set_huge_pmd() allocates the missing p4d/pud/pmd tables there.
Allocating with __GFP_FS while holding a folio lock can deadlock in reclaim, so allocate these page tables in a NOFS scope.
---
Changes in v3:
- rename nofs_flags to flags, one-line comment (Lorenzo)
- drop the memcg v1 example from the changelog (Lorenzo)
Changes in v2:
- allocate in a NOFS scope instead of before locking the folio (David)
- the deposit table is handled in do_set_pmd() now
- new subject, was "mm/khugepaged: allocate page tables before locking the folio"
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]
mm/khugepaged.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
mm/khugepaged.c +7 -6
@@ -1839,14 +1839,15 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long ammap_assert_locked (vma->vm_mm);if (!pmdp) { +unsigned int flags; + +/* Folio is locked, so avoid deadlock against concurrent reclaim */ + flags =memalloc_nofs_save (); 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); + pudp = p4dp ?pud_alloc (mm, p4dp, addr) :NULL ; + pmdp = pudp ?pmd_alloc (mm, pudp, addr) :NULL ; +memalloc_nofs_restore (flags);if (!pmdp)return SCAN_FAIL ; }
Signature · 1 line
2.56.1
PATCH v3 3/3 mm/memory: don't allocate in finish_fault() with the folio locked
In reply to Jose A. Perez de Azpillaga__do_fault() tries to avoid page table allocation in finish_fault() by pre-allocating one if the PMD is empty.
However, it is no longer safe to assume that a page table found here (thus avoiding pre-allocation) remains in place, since retract_page_tables() no longer takes the mmap and VMA locks. If the table is retracted, finish_fault() allocates one with the folio locked, which can deadlock in reclaim.
If the folio can't be mapped with a PMD, return VM_FAULT_NOPAGE instead, as we already do when pte_offset_map_lock() fails. The retry preallocates the page table, so it only loops if the table is retracted again every time.
---
Changes in v3:
- take Lorenzo's finish_fault() comment
- note at the __do_fault() preallocation that it is best-effort (Lorenzo)
- reword the changelog opener, mention the NOPAGE retry, drop the memcg v1 example (Lorenzo)
Changes in v2:
- return VM_FAULT_NOPAGE after trying do_set_pmd() (David)
- drop the ppc64 sentence, do_set_pmd() is handled on its own now
v2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]
mm/memory.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
mm/memory.c +10 -4
@@ -5701,6 +5701,8 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)* folio_set_writeback(B) * folio_unlock(B) * # flush A, B to clear the writeback +* +* Best-effort: the page table can be retracted after this check. */ if (pmd_none (*vmf->pmd) && !vmf->prealloc_pte) { vmf->prealloc_pte =pte_alloc_one (vma->vm_mm);
@@ -5997,10 +5999,14 @@ vm_fault_t finish_fault(struct vm_fault *vmf)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 ; +/* +* Page table retraction can happen after we decided there was +* no need to prealloc and the folio is locked, so to avoid +* deadlock against concurrent reclaim, just retry the fault. +*/ +if (!vmf->prealloc_pte) +return VM_FAULT_NOPAGE ; +pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); } nr_pages =folio_nr_pages (folio);
Signature · 1 line
2.56.1
Re: [PATCH v3 3/3] mm/memory: don't allocate in finish_fault() with the folio locked
Quoted 14 lines · Jose A. Perez de Azpillaga
__do_fault()tries to avoid page table allocation infinish_fault()by pre-allocating one if the PMD is empty.However, it is no longer safe to assume that a page table found here (thus avoiding pre-allocation) remains in place, since
retract_page_tables()no longer takes the mmap and VMA locks. If the table is retracted,finish_fault()allocates one with the folio locked, which can deadlock in reclaim.If the folio can't be mapped with a PMD, return
VM_FAULT_NOPAGEinstead, as we already do whenpte_offset_map_lock()fails. The retry preallocates the page table, so it only loops if the table is retracted again every time.
Quoted 5 lines
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")Reviewed-by Baolin Wang <[email protected]>Reviewed-by David Hildenbrand (Arm) <[email protected]>Signed-off-by Jose A. Perez de Azpillaga <[email protected]>
Thanks LGTM so:
Quoted 51 lines
---
Changes in v3:
- take Lorenzo'sfinish_fault()comment
- note at the__do_fault()preallocation that it is best-effort (Lorenzo)
- reword the changelog opener, mention the NOPAGE retry, drop the memcg v1 example (Lorenzo)Changes in v2:
- returnVM_FAULT_NOPAGEafter tryingdo_set_pmd()(David)
- drop the ppc64 sentence,do_set_pmd()is handled on its own nowv2: https://lore.kernel.org/[email protected]
v1: https://lore.kernel.org/[email protected]mm/memory.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)mm/memory.c +10
-5@@ -5701,6 +5701,8 @@ static vm_fault_t __do_fault(struct vm_fault *vmf)* folio_set_writeback(B) * folio_unlock(B) * # flush A, B to clear the writeback +* +* Best-effort: the page table can be retracted after this check. */ if (pmd_none (*vmf->pmd) && !vmf->prealloc_pte) { vmf->prealloc_pte =pte_alloc_one (vma->vm_mm);@@ -5997,10 +5999,14 @@ vm_fault_t finish_fault(struct vm_fault *vmf)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 ; +/* +* Page table retraction can happen after we decided there was +* no need to prealloc and the folio is locked, so to avoid +* deadlock against concurrent reclaim, just retry the fault. +*/ +if (!vmf->prealloc_pte) +return VM_FAULT_NOPAGE ; +pmd_install (vma->vm_mm, vmf->pmd, &vmf->prealloc_pte); } nr_pages =folio_nr_pages (folio); --
Quoted 2 lines
2.56.1
--
Cheers, Lorenzo