Skip to content

Commit 179ef71

Browse files
cyrillostorvalds
authored andcommitted
mm: save soft-dirty bits on swapped pages
Andy Lutomirski reported that if a page with _PAGE_SOFT_DIRTY bit set get swapped out, the bit is getting lost and no longer available when pte read back. To resolve this we introduce _PTE_SWP_SOFT_DIRTY bit which is saved in pte entry for the page being swapped out. When such page is to be read back from a swap cache we check for bit presence and if it's there we clear it and restore the former _PAGE_SOFT_DIRTY bit back. One of the problem was to find a place in pte entry where we can save the _PTE_SWP_SOFT_DIRTY bit while page is in swap. The _PAGE_PSE was chosen for that, it doesn't intersect with swap entry format stored in pte. Reported-by: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Cyrill Gorcunov <gorcunov@openvz.org> Acked-by: Pavel Emelyanov <xemul@parallels.com> Cc: Matt Mackall <mpm@selenic.com> Cc: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> Cc: Marcelo Tosatti <mtosatti@redhat.com> Cc: KOSAKI Motohiro <kosaki.motohiro@gmail.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Cc: Peter Zijlstra <peterz@infradead.org> Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com> Reviewed-by: Minchan Kim <minchan@kernel.org> Reviewed-by: Wanpeng Li <liwanp@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3e6b11d commit 179ef71

8 files changed

Lines changed: 84 additions & 9 deletions

File tree

‎arch/x86/include/asm/pgtable.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,21 @@ static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
314314
return pmd_set_flags(pmd, _PAGE_SOFT_DIRTY);
315315
}
316316

317+
static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
318+
{
319+
return pte_set_flags(pte, _PAGE_SWP_SOFT_DIRTY);
320+
}
321+
322+
static inline int pte_swp_soft_dirty(pte_t pte)
323+
{
324+
return pte_flags(pte) & _PAGE_SWP_SOFT_DIRTY;
325+
}
326+
327+
static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
328+
{
329+
return pte_clear_flags(pte, _PAGE_SWP_SOFT_DIRTY);
330+
}
331+
317332
/*
318333
* Mask out unsupported bits in a present pgprot. Non-present pgprots
319334
* can use those bits for other purposes, so leave them be.

‎arch/x86/include/asm/pgtable_types.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@
6767
#define _PAGE_SOFT_DIRTY (_AT(pteval_t, 0))
6868
#endif
6969

70+
/*
71+
* Tracking soft dirty bit when a page goes to a swap is tricky.
72+
* We need a bit which can be stored in pte _and_ not conflict
73+
* with swap entry format. On x86 bits 6 and 7 are *not* involved
74+
* into swap entry computation, but bit 6 is used for nonlinear
75+
* file mapping, so we borrow bit 7 for soft dirty tracking.
76+
*/
77+
#ifdef CONFIG_MEM_SOFT_DIRTY
78+
#define _PAGE_SWP_SOFT_DIRTY _PAGE_PSE
79+
#else
80+
#define _PAGE_SWP_SOFT_DIRTY (_AT(pteval_t, 0))
81+
#endif
82+
7083
#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
7184
#define _PAGE_NX (_AT(pteval_t, 1) << _PAGE_BIT_NX)
7285
#else

‎fs/proc/task_mmu.c‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,14 @@ static inline void clear_soft_dirty(struct vm_area_struct *vma,
730730
* of how soft-dirty works.
731731
*/
732732
pte_t ptent = *pte;
733-
ptent = pte_wrprotect(ptent);
734-
ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
733+
734+
if (pte_present(ptent)) {
735+
ptent = pte_wrprotect(ptent);
736+
ptent = pte_clear_flags(ptent, _PAGE_SOFT_DIRTY);
737+
} else if (is_swap_pte(ptent)) {
738+
ptent = pte_swp_clear_soft_dirty(ptent);
739+
}
740+
735741
set_pte_at(vma->vm_mm, addr, pte, ptent);
736742
#endif
737743
}
@@ -752,14 +758,15 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
752758
pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
753759
for (; addr != end; pte++, addr += PAGE_SIZE) {
754760
ptent = *pte;
755-
if (!pte_present(ptent))
756-
continue;
757761

758762
if (cp->type == CLEAR_REFS_SOFT_DIRTY) {
759763
clear_soft_dirty(vma, addr, pte);
760764
continue;
761765
}
762766

767+
if (!pte_present(ptent))
768+
continue;
769+
763770
page = vm_normal_page(vma, addr, ptent);
764771
if (!page)
765772
continue;
@@ -930,8 +937,10 @@ static void pte_to_pagemap_entry(pagemap_entry_t *pme, struct pagemapread *pm,
930937
flags = PM_PRESENT;
931938
page = vm_normal_page(vma, addr, pte);
932939
} else if (is_swap_pte(pte)) {
933-
swp_entry_t entry = pte_to_swp_entry(pte);
934-
940+
swp_entry_t entry;
941+
if (pte_swp_soft_dirty(pte))
942+
flags2 |= __PM_SOFT_DIRTY;
943+
entry = pte_to_swp_entry(pte);
935944
frame = swp_type(entry) |
936945
(swp_offset(entry) << MAX_SWAPFILES_SHIFT);
937946
flags = PM_SWAP;

‎include/asm-generic/pgtable.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,21 @@ static inline pmd_t pmd_mksoft_dirty(pmd_t pmd)
417417
{
418418
return pmd;
419419
}
420+
421+
static inline pte_t pte_swp_mksoft_dirty(pte_t pte)
422+
{
423+
return pte;
424+
}
425+
426+
static inline int pte_swp_soft_dirty(pte_t pte)
427+
{
428+
return 0;
429+
}
430+
431+
static inline pte_t pte_swp_clear_soft_dirty(pte_t pte)
432+
{
433+
return pte;
434+
}
420435
#endif
421436

422437
#ifndef __HAVE_PFNMAP_TRACKING

‎include/linux/swapops.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ static inline swp_entry_t pte_to_swp_entry(pte_t pte)
6767
swp_entry_t arch_entry;
6868

6969
BUG_ON(pte_file(pte));
70+
if (pte_swp_soft_dirty(pte))
71+
pte = pte_swp_clear_soft_dirty(pte);
7072
arch_entry = __pte_to_swp_entry(pte);
7173
return swp_entry(__swp_type(arch_entry), __swp_offset(arch_entry));
7274
}

‎mm/memory.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,6 +3115,8 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
31153115
exclusive = 1;
31163116
}
31173117
flush_icache_page(vma, page);
3118+
if (pte_swp_soft_dirty(orig_pte))
3119+
pte = pte_mksoft_dirty(pte);
31183120
set_pte_at(mm, address, page_table, pte);
31193121
if (page == swapcache)
31203122
do_page_add_anon_rmap(page, vma, address, exclusive);

‎mm/rmap.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
12361236
swp_entry_to_pte(make_hwpoison_entry(page)));
12371237
} else if (PageAnon(page)) {
12381238
swp_entry_t entry = { .val = page_private(page) };
1239+
pte_t swp_pte;
12391240

12401241
if (PageSwapCache(page)) {
12411242
/*
@@ -1264,7 +1265,10 @@ int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
12641265
BUG_ON(TTU_ACTION(flags) != TTU_MIGRATION);
12651266
entry = make_migration_entry(page, pte_write(pteval));
12661267
}
1267-
set_pte_at(mm, address, pte, swp_entry_to_pte(entry));
1268+
swp_pte = swp_entry_to_pte(entry);
1269+
if (pte_soft_dirty(pteval))
1270+
swp_pte = pte_swp_mksoft_dirty(swp_pte);
1271+
set_pte_at(mm, address, pte, swp_pte);
12681272
BUG_ON(pte_file(*pte));
12691273
} else if (IS_ENABLED(CONFIG_MIGRATION) &&
12701274
(TTU_ACTION(flags) == TTU_MIGRATION)) {

‎mm/swapfile.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,21 @@ unsigned int count_swap_pages(int type, int free)
866866
}
867867
#endif /* CONFIG_HIBERNATION */
868868

869+
static inline int maybe_same_pte(pte_t pte, pte_t swp_pte)
870+
{
871+
#ifdef CONFIG_MEM_SOFT_DIRTY
872+
/*
873+
* When pte keeps soft dirty bit the pte generated
874+
* from swap entry does not has it, still it's same
875+
* pte from logical point of view.
876+
*/
877+
pte_t swp_pte_dirty = pte_swp_mksoft_dirty(swp_pte);
878+
return pte_same(pte, swp_pte) || pte_same(pte, swp_pte_dirty);
879+
#else
880+
return pte_same(pte, swp_pte);
881+
#endif
882+
}
883+
869884
/*
870885
* No need to decide whether this PTE shares the swap entry with others,
871886
* just let do_wp_page work it out if a write is requested later - to
@@ -892,7 +907,7 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,
892907
}
893908

894909
pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
895-
if (unlikely(!pte_same(*pte, swp_entry_to_pte(entry)))) {
910+
if (unlikely(!maybe_same_pte(*pte, swp_entry_to_pte(entry)))) {
896911
mem_cgroup_cancel_charge_swapin(memcg);
897912
ret = 0;
898913
goto out;
@@ -947,7 +962,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
947962
* swapoff spends a _lot_ of time in this loop!
948963
* Test inline before going to call unuse_pte.
949964
*/
950-
if (unlikely(pte_same(*pte, swp_pte))) {
965+
if (unlikely(maybe_same_pte(*pte, swp_pte))) {
951966
pte_unmap(pte);
952967
ret = unuse_pte(vma, pmd, addr, entry, page);
953968
if (ret)

0 commit comments

Comments
 (0)