mm/thp: Re-apply mkdirty for small pages after split

Message ID 20221125185857.3110155-1-peterx@redhat.com
State New
Headers
Series mm/thp: Re-apply mkdirty for small pages after split |

Commit Message

Peter Xu Nov. 25, 2022, 6:58 p.m. UTC
  We used to have 624a2c94f5b7 fixing the regression reported here by Anatoly
Pugachev on sparc64:

https://lore.kernel.org/r/20221021160603.GA23307@u164.east.ru

Where we temporarily ignored the dirty bit for small pages.

Then, Hev <r@hev.cc> also reported similar issue on loongarch:

(the original mail was private, but Anatoly copied the list here)
https://lore.kernel.org/r/CADxRZqxqb7f_WhMh=jweZP+ynf_JwGd-0VwbYgp4P+T0-AXosw@mail.gmail.com

Hev pointed out that the issue is having HW write bit set within the
pte_mkdirty() so the split pte can be written after split even if e.g. they
were shared by more than one processes, causing data corrupt.

Hev also tried to explain why loongarch set HW write bit in mkdirty:

https://lore.kernel.org/r/CAHirt9itKO_K_HPboXh5AyJtt16Zf0cD73PtHvM=na39u_ztxA@mail.gmail.com

One way to fix it is as what Huacai proposed here for loongarch (then we
can re-apply the dirty bit in thp split):

https://lore.kernel.org/r/20221117042532.4064448-1-chenhuacai@loongson.cnn

We may need similar thing for sparc64, though.

For now since we've found the root cause of the dirty bit issue the simpler
solution (which won't lose the dirty bit for small) that will work for both
is we wr-protect after pte_mkdirty(), so the HW write bit can be persistent
after thp split.

Add a comment for wrprotect, so we will not mess up the ordering later.

With 624a2c94f5b7 this is not a fix anymore, but just brings back the dirty
bit for thp split safely, so we re-apply the optimization but in safe way.

Provide a T-B credit to Hev too (not the exact same patch but the same
outcome) for loongarch.

Cc: Anatoly Pugachev <matorola@gmail.com>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Raghavendra K T <raghavendra.kt@amd.com>
Cc: Thorsten Leemhuis <regressions@leemhuis.info>
Tested-by: Hev <r@hev.cc> # loongarch
Signed-off-by: Peter Xu <peterx@redhat.com>
---

PS: Anatoly, feel free to try this too on sparc64.  Applicable to either
branch (Linus's, or akpm's) as long as latest.

Signed-off-by: Peter Xu <peterx@redhat.com>
---
 mm/huge_memory.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
  

Patch

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index ae1e2d80bb6e..5f93ba064323 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -2192,16 +2192,18 @@  static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 			entry = maybe_mkwrite(entry, vma);
 			if (anon_exclusive)
 				SetPageAnonExclusive(page + i);
-			if (!write)
-				entry = pte_wrprotect(entry);
 			if (!young)
 				entry = pte_mkold(entry);
+			/* NOTE: this may set soft-dirty too on some archs */
+			if (dirty)
+				entry = pte_mkdirty(entry);
 			/*
-			 * NOTE: we don't do pte_mkdirty when dirty==true
-			 * because it breaks sparc64 which can sigsegv
-			 * random process.  Need to revisit when we figure
-			 * out what is special with sparc64.
+			 * NOTE: this needs to happen after pte_mkdirty,
+			 * because some archs (sparc64, loongarch) could
+			 * set hw write bit when mkdirty.
 			 */
+			if (!write)
+				entry = pte_wrprotect(entry);
 			if (soft_dirty)
 				entry = pte_mksoft_dirty(entry);
 			if (uffd_wp)