Skip to contentSkip to conversation

mm: don't allocate page tables with the folio locked

PATCH Superseded Jose A. Perez de Azpillaga 5 participants 20 messages Nov 27 – Dec 1

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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 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

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 | 16 ++++++++++------
 mm/memory.c     | 20 ++++++++++++++++----
 2 files changed, 26 insertions(+), 10 deletions(-)

base-commit: c6f53520f32302ea64cbcfd7cc25a31081e34548 --
2.56.1

#1 lore raw

PATCH v2 1/3 mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit when the caller did not preallocate one. Both callers that let it allocate hold the folio lock: set_huge_pmd() in khugepaged never preallocates, and finish_fault() has no preallocated table when the page table was removed after __do_fault() checked the PMD.

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.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in do_set_pmd()" (David)

v1: https://lore.kernel.org/[email protected]

 mm/memory.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/memory.c +8 -0
index dfa307a9228c..05d03b32ce52 100644
@@ -5806,9 +5806,17 @@ 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 nofs_flags;
+
 		if (!may_alloc)
 			return VM_FAULT_FALLBACK;
+		/*
+		 * The folio is locked, and reclaim or compaction with __GFP_FS
+		 * could deadlock on it. pte_alloc_one() takes no gfp flags.
+		 */
+		nofs_flags = memalloc_nofs_save();
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
+		memalloc_nofs_restore(nofs_flags);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
 		pgtable_allocated = true;
Signature · 1 line
2.56.1
reviewer Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

Quoted 11 lines · Jose A. Perez de Azpillaga

On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit when the caller did not preallocate one. Both callers that let it allocate hold the folio lock: set_huge_pmd() in khugepaged never preallocates, and finish_fault() has no preallocated table when the page table was removed after __do_fault() checked the PMD.

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").

Quoted 8 lines

pte_alloc_one() takes no gfp flags, so allocate the table in a NOFS scope.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

Make sense to me.

maintainer Reviewed-by on v2 1/3
#3 lore raw

Re: [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 11 lines · Jose A. Perez de Azpillaga

On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit when the caller did not preallocate one. Both callers that let it allocate hold the folio lock: set_huge_pmd() in khugepaged never preallocates, and finish_fault() has no preallocated table when the page table was removed after __do_fault() checked the PMD.

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").

Quoted 8 lines

pte_alloc_one() takes no gfp flags, so allocate the table in a NOFS scope.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 1/3
#4 lore raw

Re: [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 5 lines · Jose A. Perez de Azpillaga

On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit when the caller did not preallocate one. Both callers that let it allocate hold the folio lock: set_huge_pmd() in khugepaged never preallocates, and finish_fault() has no preallocated table when the page table was removed after __do_fault() checked the PMD.

Coudl we de-schloppen this a bit?

E.g.:

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).

Quoted 6 lines

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").

Quoted 7 lines

pte_alloc_one() takes no gfp flags, so allocate the table in a NOFS scope.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

Looks reasonable so with changes requested and to commit msg:

Quoted 15 lines

---
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in do_set_pmd()" (David)

v1: https://lore.kernel.org/[email protected]

 mm/memory.c | 8 ++++++++
 1 file changed, 8 insertions(+)
mm/memory.c +0 -0
index dfa307a9228c..05d03b32ce52 100644

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5809

Quoted 4 lines
mm/memory.c +1 -0
@@ -5806,9 +5806,17 @@ 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 nofs_flags;

'flags' please. It's a small block-scope var we don't really need to spell it out!

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5814

Quoted 5 lines
mm/memory.c +3 -0
+
 		if (!may_alloc)
 			return VM_FAULT_FALLBACK;
+		/*
+		 * The folio is locked, and reclaim or compaction with __GFP_FS

It IS locked? Under all circumstances? Presumably so.

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5816

Quoted 2 lines
mm/memory.c +2 -0
+		 * could deadlock on it. pte_alloc_one() takes no gfp flags.
+		 */

This is schloppy and silly. 'pte_alloc_one() takes no flags'. Which one can observe from literally looking 2 lines below... It's also asking for bitrot.

Just make it something like:

	/* Folio is locked, so avoid deadlock against concurrent reclaim */

Please.

Quoted 9 lines
mm/memory.c +2 -1
+		nofs_flags = memalloc_nofs_save();
 		vmf->prealloc_pte = pte_alloc_one(vma->vm_mm);
+		memalloc_nofs_restore(nofs_flags);
 		if (!vmf->prealloc_pte)
 			return VM_FAULT_OOM;
 		pgtable_allocated = true;
--

2.56.1

--
Cheers, Lorenzo

#5 lore raw

Re: [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

Quoted 4 lines · Lorenzo Stoakes (ARM)

Looks reasonable so with changes requested and to commit msg:

thanks for the review! I'll address all of it in v3 :)

[snip]

Quoted 4 lines

mm/memory.c vm_fault_t do_set_pmd(struct vm_fault *vmf, struct folio *folio, struct page *pa · line 5814

Quoted 2 lines
mm/memory.c +2 -0
+		/*
+		 * The folio is locked, and reclaim or compaction with __GFP_FS

It IS locked? Under all circumstances? Presumably so.

yes, finish_fault() and set_huge_pmd() both hold the folio lock there.

Signature · 2 lines
cheers,
jose a. p-a
reviewer Reviewed-by on v2 1/3
#6 lore raw

Re: [PATCH v2 1/3] mm/memory: allocate the deposit page table with NOFS in do_set_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 23 lines · Jose A. Perez de Azpillaga

On ppc64 with the hash MMU, do_set_pmd() allocates the page table to deposit when the caller did not preallocate one. Both callers that let it allocate hold the folio lock: set_huge_pmd() in khugepaged never preallocates, and finish_fault() has no preallocated table when the page table was removed after __do_fault() checked the PMD.

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.

Fixes 34488399fa08 ("mm/madvise: add file and shmem support to MADV_COLLAPSE")
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---
Changes in v2:
- new, replaces v1 3/4 "mm/memory: never allocate a page table in do_set_pmd()" (David)

Quoted 5 lines

v1: https://lore.kernel.org/[email protected]

 mm/memory.c | 8 ++++++++
 1 file changed, 8 insertions(+)

Makes sense. With Lorenzo’s feedback addressed, feel free to add

Thanks.

Best Regards,
Yan, Zi

#7 lore raw

PATCH v2 2/3 mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()

In reply to Jose A. Perez de Azpillaga

try_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. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.

Allocate these page tables in a NOFS scope.

Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")

---
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"

v1: https://lore.kernel.org/[email protected]

 mm/khugepaged.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
mm/khugepaged.c +10 -6
index 93d4a6f159e8..2ac8b5621db0 100644
@@ -1839,14 +1839,18 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
 	mmap_assert_locked(vma->vm_mm);
 
 	if (!pmdp) {
+		unsigned int nofs_flags;
+
+		/*
+		 * The folio is locked, and reclaim or compaction with __GFP_FS
+		 * could deadlock on it.
+		 */
+		nofs_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(nofs_flags);
 		if (!pmdp)
 			return SCAN_FAIL;
 	}
Signature · 1 line
2.56.1
reviewer Reviewed-by on v2 2/3
#8 lore raw

Re: [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()

Quoted 8 lines · Jose A. Perez de Azpillaga

try_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. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.

Quoted 6 lines

Allocate these page tables in a NOFS scope.

Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")

---

LGTM.

maintainer Reviewed-by on v2 2/3
#9 lore raw

Re: [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 8 lines · Jose A. Perez de Azpillaga

try_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. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.

Quoted 6 lines

Allocate these page tables in a NOFS scope.

Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")

---

Signature · 3 lines
Cheers,

David
reviewer Reviewed-by on v2 2/3
#10 lore raw

Re: [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 8 lines · Jose A. Perez de Azpillaga

try_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. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.

Quoted 5 lines

Allocate these page tables in a NOFS scope.

Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")

LGTM, with stuff below addressed:

Quoted 17 lines

---
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"

v1: https://lore.kernel.org/[email protected]

 mm/khugepaged.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
mm/khugepaged.c +0 -0
index 93d4a6f159e8..2ac8b5621db0 100644

mm/khugepaged.c static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a · line 1842

Quoted 4 lines
mm/khugepaged.c +1 -0
@@ -1839,14 +1839,18 @@ static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a
 	mmap_assert_locked(vma->vm_mm);
 
 	if (!pmdp) {
+		unsigned int nofs_flags;

'flags' please.

mm/khugepaged.c static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a · line 1847

Quoted 5 lines
mm/khugepaged.c +5 -0
+
+		/*
+		 * The folio is locked, and reclaim or compaction with __GFP_FS
+		 * could deadlock on it.
+		 */

Again let's tighten this up please:

	/* Folio is locked, so avoid deadlock against concurrent reclaim */

mm/khugepaged.c static enum scan_result set_huge_pmd(struct vm_area_struct *vma, unsigned long a · line 1852

Quoted 11 lines
mm/khugepaged.c +3 -6
+		nofs_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;

This looks like a David-ism :)) [ that is a good thing lol ]

Quoted 7 lines
mm/khugepaged.c +1 -1
+		memalloc_nofs_restore(nofs_flags);
 		if (!pmdp)
 			return SCAN_FAIL;
 	}
--

2.56.1

--
Cheers, Lorenzo

reviewer Reviewed-by on v2 2/3
#11 lore raw

Re: [PATCH v2 2/3] mm/khugepaged: allocate page tables with NOFS in set_huge_pmd()

In reply to Jose A. Perez de Azpillaga
Quoted 20 lines · Jose A. Perez de Azpillaga

try_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. For example, memcg reclaim on cgroup v1 waits for writeback that may be waiting for the folio lock we hold.

Allocate these page tables in a NOFS scope.

Fixes f8a01513f574 ("mm/khugepaged: do not fail collapse_pte_mapped_thp() on SCAN_PMD_NULL")

---
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"

Quoted 5 lines

v1: https://lore.kernel.org/[email protected]

 mm/khugepaged.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

LGTM.

Best Regards,
Yan, Zi

#12 lore raw

PATCH v2 3/3 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.

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").

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 retried fault preallocates the page table before locking the folio.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---
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

v1: https://lore.kernel.org/[email protected]

 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +8 -4
index 05d03b32ce52..dd0cb09ba7ef 100644
@@ -6000,10 +6000,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;
+		/*
+		 * 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;
+		pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
 	}
 
 	nr_pages = folio_nr_pages(folio);
Signature · 1 line
2.56.1
reviewer Reviewed-by on v2 3/3
#13 lore raw

Re: [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked

Quoted 13 lines · 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.

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").

Quoted 8 lines

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 retried fault preallocates the page table before locking the folio.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

LGTM.

maintainer Reviewed-by on v2 3/3
#14 lore raw

Re: [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked

In reply to Jose A. Perez de Azpillaga
Quoted 13 lines · 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.

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").

Quoted 8 lines

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 retried fault preallocates the page table before locking the folio.

Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---

Signature · 3 lines
Cheers,

David
#15 lore raw

Re: [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked

In reply to Jose A. Perez de Azpillaga
Quoted 2 lines · 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.

This is written in a sort of way, so as to automatically confused, whoever reads it, with its comma.

How about:

__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 < stuff about locks below >

?

Quoted 9 lines

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.

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").

Do we have to have this example in every commit message? :)) I mean I guess fine. I just find cgroup v1 very deprecate-y.

Quoted 4 lines

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 retried fault preallocates the page table before locking the folio.

Is that safe from this race, or does it just mean you could go on a VM_FAULT_NOPAGE loop again? Maybe worth mentioning?

It feels like the pre-faulting mechanism is now a sort of best-effort thing since it can't now correctly detect if it's necessary or not. Maybe worth a comment at the point of pre-allocation saying that?

Quoted 21 lines
Fixes 1d65b771bc08 ("mm/khugepaged: retract_page_tables() without mmap or vma lock")

---
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

v1: https://lore.kernel.org/[email protected]

 mm/memory.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
mm/memory.c +0 -0
index 05d03b32ce52..dd0cb09ba7ef 100644
@@ -6000,10 +6000,14 @@ vm_fault_t finish_fault(struct vm_fault *vmf)
 				return ret;
 		}

mm/memory.c vm_fault_t finish_fault(struct vm_fault *vmf) · line 6007

Quoted 10 lines
mm/memory.c +5 -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;
+		/*
+		 * 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.
+		 */

"Dearest Diary, Today I deadlocked on an allocation and it was the most wondrous deadlock evah!11"

Let's maybe deschlop here, e.g.:

/*
* 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. */

OK not fewer words but I think clearer :)

Quoted 9 lines
mm/memory.c +3 -1
+		if (!vmf->prealloc_pte)
+			return VM_FAULT_NOPAGE;
+		pmd_install(vma->vm_mm, vmf->pmd, &vmf->prealloc_pte);
 	}
 
 	nr_pages = folio_nr_pages(folio);
--

2.56.1

--
Cheers, Lorenzo

#16 lore raw

Re: [PATCH v2 3/3] mm/memory: don't allocate in finish_fault() with the folio locked

Quoted 6 lines · Lorenzo Stoakes (ARM)
Quoted 3 lines

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 retried fault preallocates the page table before locking the folio.

Is that safe from this race, or does it just mean you could go on a VM_FAULT_NOPAGE loop again? Maybe worth mentioning?

yes, the retry preallocates, so it only loops if the table gets retracted again every time... I'll mention it in v3.

Quoted 3 lines

It feels like the pre-faulting mechanism is now a sort of best-effort thing since it can't now correctly detect if it's necessary or not. Maybe worth a comment at the point of pre-allocation saying that?

sure, will add one in v3.

Signature · 2 lines
cheers,
jose a. p-a
#17 lore raw
In reply to Jose A. Perez de Azpillaga
Quoted 10 lines · Jose A. Perez de Azpillaga

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.

While doing some digging over the weekend, I think we have another case where we allocate with the folio lock held: shmem_replace_folio().

I think the calling code can make the decision (shmem_should_replace_folio) without holding the folio lock.

Can you take care of that as well? Can do separately from this series.

Signature · 3 lines
Cheers,

David
#18 lore raw
Quoted 12 lines · David Hildenbrand (Arm)
Quoted 10 lines · Jose A. Perez de Azpillaga

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.

Quoted 7 lines

While doing some digging over the weekend, I think we have another case where we allocate with the folio lock held: shmem_replace_folio().

I think the calling code can make the decision (shmem_should_replace_folio) without holding the folio lock.

Can you take care of that as well? Can do separately from this series.

sure, why not? I'll send it separately :)

Signature · 2 lines
cheers,
jose a. p-a
#19 lore raw
In reply to Jose A. Perez de Azpillaga
Quoted 4 lines · Jose A. Perez de Azpillaga

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.

Quoted 8 lines

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]

Thanks, I'll wait a bit more and then apply it.

Signature · 3 lines
Cheers,

David