Skip to contentSkip to conversation

mm: thp: fix soft dirty for migration when split

PATCH Superseded Peter Xu 2 participants 5 messages Dec 6, 2018 – Dec 11, 2018

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Peter Xu

author
#0 lore raw

PATCH 1/1 mm: thp: fix soft dirty for migration when split

When splitting a huge migrating PMD, we'll transfer the soft dirty bit from the huge page to the small pages. However we're possibly using a wrong data since when fetching the bit we're using pmd_soft_dirty() upon a migration entry. Fix it up.

CC Andrea Arcangeli <[email protected]>
CC Andrew Morton <[email protected]>
CC "Kirill A. Shutemov" <[email protected]>
CC Matthew Wilcox <[email protected]>
CC Michal Hocko <[email protected]>
CC Dave Jiang <[email protected]>
CC "Aneesh Kumar K.V" <[email protected]>
CC Souptick Joarder <[email protected]>
CC Konstantin Khlebnikov <[email protected]>

---

I noticed this during code reading. Only compile tested. I'm sending a patch directly for review comments since it's relatively straightforward and not easy to test. Please have a look, thanks.

---
 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +4 -1
index f2d19e4fe854..fb0787c3dd3b 100644
@@ -2161,7 +2161,10 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 		SetPageDirty(page);
 	write = pmd_write(old_pmd);
 	young = pmd_young(old_pmd);
-	soft_dirty = pmd_soft_dirty(old_pmd);
+	if (unlikely(pmd_migration))
+		soft_dirty = pmd_swp_soft_dirty(old_pmd);
+	else
+		soft_dirty = pmd_soft_dirty(old_pmd);
 
 	/*
 	 * Withdraw the table only after we mark the pmd entry invalid.
Signature · 1 line
2.17.1
author
#1 lore raw
Quoted 4 lines · Peter Xu

When splitting a huge migrating PMD, we'll transfer the soft dirty bit from the huge page to the small pages. However we're possibly using a wrong data since when fetching the bit we're using pmd_soft_dirty() upon a migration entry. Fix it up.

Note that if my understanding is correct about the problem then if without the patch there is chance to lose some of the dirty bits in the migrating pmd pages (on x86_64 we're fetching bit 11 which is part of swap offset instead of bit 2) and it could potentially corrupt the memory of an userspace program which depends on the dirty bit.

Quoted 41 lines
CC Andrea Arcangeli <[email protected]>
CC Andrew Morton <[email protected]>
CC "Kirill A. Shutemov" <[email protected]>
CC Matthew Wilcox <[email protected]>
CC Michal Hocko <[email protected]>
CC Dave Jiang <[email protected]>
CC "Aneesh Kumar K.V" <[email protected]>
CC Souptick Joarder <[email protected]>
CC Konstantin Khlebnikov <[email protected]>

---

I noticed this during code reading. Only compile tested. I'm sending a patch directly for review comments since it's relatively straightforward and not easy to test. Please have a look, thanks.

---
 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
mm/huge_memory.c +4 -1
index f2d19e4fe854..fb0787c3dd3b 100644
@@ -2161,7 +2161,10 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,
 		SetPageDirty(page);
 	write = pmd_write(old_pmd);
 	young = pmd_young(old_pmd);
-	soft_dirty = pmd_soft_dirty(old_pmd);
+	if (unlikely(pmd_migration))
+		soft_dirty = pmd_swp_soft_dirty(old_pmd);
+	else
+		soft_dirty = pmd_soft_dirty(old_pmd);
 
 	/*
 	 * Withdraw the table only after we mark the pmd entry invalid.
Signature · 1 line
2.17.1

Regards,

Signature · 1 line
Peter Xu
#2 lore raw
Quoted 7 lines · Peter Xu
Quoted 4 lines · Peter Xu

When splitting a huge migrating PMD, we'll transfer the soft dirty bit from the huge page to the small pages. However we're possibly using a wrong data since when fetching the bit we're using pmd_soft_dirty() upon a migration entry. Fix it up.

Quoted 5 lines

Note that if my understanding is correct about the problem then if without the patch there is chance to lose some of the dirty bits in the migrating pmd pages (on x86_64 we're fetching bit 11 which is part of swap offset instead of bit 2) and it could potentially corrupt the memory of an userspace program which depends on the dirty bit.

It seems this code is broken in case of pmd_migraion:

old_pmd = pmdp_invalidate(vma, haddr, pmd);

#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_migration = is_pmd_migration_entry(old_pmd);
if (pmd_migration) {
swp_entry_t entry;

entry = pmd_to_swp_entry(old_pmd);
page = pfn_to_page(swp_offset(entry));
} else
#endif
page = pmd_page(old_pmd);
VM_BUG_ON_PAGE(!page_count(page), page);
page_ref_add(page, HPAGE_PMD_NR - 1);
if (pmd_dirty(old_pmd))
SetPageDirty(page);
write = pmd_write(old_pmd);
young = pmd_young(old_pmd);
soft_dirty = pmd_soft_dirty(old_pmd);

Not just soft_dirt - all bits (dirty, write, young) have diffrent encoding or not present at all for migration entry.

Quoted 23 lines
Quoted 23 lines
CC Andrea Arcangeli <[email protected]>
CC Andrew Morton <[email protected]>
CC "Kirill A. Shutemov" <[email protected]>
CC Matthew Wilcox <[email protected]>
CC Michal Hocko <[email protected]>
CC Dave Jiang <[email protected]>
CC "Aneesh Kumar K.V" <[email protected]>
CC Souptick Joarder <[email protected]>
CC Konstantin Khlebnikov <[email protected]>

---

I noticed this during code reading. Only compile tested. I'm sending a patch directly for review comments since it's relatively straightforward and not easy to test. Please have a look, thanks.

---
 mm/huge_memory.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 5 lines

mm/huge_memory.c static void __split_huge_pmd_locked(struct

Quoted 5 lines
mm/huge_memory.c +0 -0
index f2d19e4fe854..fb0787c3dd3b 100644
@@ -2161,7 +2161,10 @@ static void __split_huge_pmd_locked(struct

vm_area_struct *vma, pmd_t *pmd,

Quoted 20 lines
Quoted 12 lines
mm/huge_memory.c +4 -2
              SetPageDirty(page);
      write = pmd_write(old_pmd);
      young = pmd_young(old_pmd);
-     soft_dirty = pmd_soft_dirty(old_pmd);
+     if (unlikely(pmd_migration))
+             soft_dirty = pmd_swp_soft_dirty(old_pmd);
+     else
+             soft_dirty = pmd_soft_dirty(old_pmd);
 
      /*
       * Withdraw the table only after we mark the pmd entry invalid.
--
Quoted 2 lines

2.17.1

Regards,

--
Peter Xu

author
#3 lore raw
Quoted 36 lines · Konstantin Khlebnikov
Quoted 7 lines · Peter Xu
Quoted 4 lines · Peter Xu

When splitting a huge migrating PMD, we'll transfer the soft dirty bit from the huge page to the small pages. However we're possibly using a wrong data since when fetching the bit we're using pmd_soft_dirty() upon a migration entry. Fix it up.

Quoted 5 lines

Note that if my understanding is correct about the problem then if without the patch there is chance to lose some of the dirty bits in the migrating pmd pages (on x86_64 we're fetching bit 11 which is part of swap offset instead of bit 2) and it could potentially corrupt the memory of an userspace program which depends on the dirty bit.

It seems this code is broken in case of pmd_migraion:

old_pmd = pmdp_invalidate(vma, haddr, pmd);

#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_migration = is_pmd_migration_entry(old_pmd);
if (pmd_migration) {
swp_entry_t entry;

entry = pmd_to_swp_entry(old_pmd);
page = pfn_to_page(swp_offset(entry));
} else
#endif
page = pmd_page(old_pmd);
VM_BUG_ON_PAGE(!page_count(page), page);
page_ref_add(page, HPAGE_PMD_NR - 1);
if (pmd_dirty(old_pmd))
SetPageDirty(page);
write = pmd_write(old_pmd);
young = pmd_young(old_pmd);
soft_dirty = pmd_soft_dirty(old_pmd);

Quoted 2 lines

Not just soft_dirt - all bits (dirty, write, young) have diffrent encoding or not present at all for migration entry.

Hi, Konstantin,

Actually I noticed it but I thought it didn't hurt since both write/young flags are not used at all when applying to the small pages when pmd_migration==true. But indeed there's at least an unexpected side effect of an extra call to SetPageDirty() that I missed.

I'll repost soon. Thanks!

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 41 lines · Peter Xu
Quoted 36 lines · Konstantin Khlebnikov
Quoted 7 lines · Peter Xu
Quoted 4 lines · Peter Xu

When splitting a huge migrating PMD, we'll transfer the soft dirty bit from the huge page to the small pages. However we're possibly using a wrong data since when fetching the bit we're using pmd_soft_dirty() upon a migration entry. Fix it up.

Quoted 5 lines

Note that if my understanding is correct about the problem then if without the patch there is chance to lose some of the dirty bits in the migrating pmd pages (on x86_64 we're fetching bit 11 which is part of swap offset instead of bit 2) and it could potentially corrupt the memory of an userspace program which depends on the dirty bit.

It seems this code is broken in case of pmd_migraion:

old_pmd = pmdp_invalidate(vma, haddr, pmd);

#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
pmd_migration = is_pmd_migration_entry(old_pmd);
if (pmd_migration) {
swp_entry_t entry;

entry = pmd_to_swp_entry(old_pmd);
page = pfn_to_page(swp_offset(entry));
} else
#endif
page = pmd_page(old_pmd);
VM_BUG_ON_PAGE(!page_count(page), page);
page_ref_add(page, HPAGE_PMD_NR - 1);
if (pmd_dirty(old_pmd))
SetPageDirty(page);
write = pmd_write(old_pmd);
young = pmd_young(old_pmd);
soft_dirty = pmd_soft_dirty(old_pmd);

Quoted 2 lines

Not just soft_dirt - all bits (dirty, write, young) have diffrent encoding or not present at all for migration entry.

Quoted 6 lines

Hi, Konstantin,

Actually I noticed it but I thought it didn't hurt since both write/young flags are not used at all when applying to the small pages when pmd_migration==true. But indeed there's at least an unexpected side effect of an extra call to SetPageDirty() that I missed.

"write" is used for making smaller migration entry:

swp_entry = make_migration_entry(page + i, write);

Quoted 6 lines

I'll repost soon. Thanks!

--
Peter Xu