Skip to content

Commit 7927147

Browse files
xu xinakpm00
authored andcommitted
ksm: support unsharing KSM-placed zero pages
Patch series "ksm: support tracking KSM-placed zero-pages", v10. The core idea of this patch set is to enable users to perceive the number of any pages merged by KSM, regardless of whether use_zero_page switch has been turned on, so that users can know how much free memory increase is really due to their madvise(MERGEABLE) actions. But the problem is, when enabling use_zero_pages, all empty pages will be merged with kernel zero pages instead of with each other as use_zero_pages is disabled, and then these zero-pages are no longer monitored by KSM. The motivations to do this is seen at: https://lore.kernel.org/lkml/[email protected]/ In one word, we hope to implement the support for KSM-placed zero pages tracking without affecting the feature of use_zero_pages, so that app developer can also benefit from knowing the actual KSM profit by getting KSM-placed zero pages to optimize applications eventually when /sys/kernel/mm/ksm/use_zero_pages is enabled. This patch (of 5): When use_zero_pages of ksm is enabled, madvise(addr, len, MADV_UNMERGEABLE) and other ways (like write 2 to /sys/kernel/mm/ksm/run) to trigger unsharing will *not* actually unshare the shared zeropage as placed by KSM (which is against the MADV_UNMERGEABLE documentation). As these KSM-placed zero pages are out of the control of KSM, the related counts of ksm pages don't expose how many zero pages are placed by KSM (these special zero pages are different from those initially mapped zero pages, because the zero pages mapped to MADV_UNMERGEABLE areas are expected to be a complete and unshared page). To not blindly unshare all shared zero_pages in applicable VMAs, the patch use pte_mkdirty (related with architecture) to mark KSM-placed zero pages. Thus, MADV_UNMERGEABLE will only unshare those KSM-placed zero pages. In addition, we'll reuse this mechanism to reliably identify KSM-placed ZeroPages to properly account for them (e.g., calculating the KSM profit that includes zeropages) in the latter patches. The patch will not degrade the performance of use_zero_pages as it doesn't change the way of merging empty pages in use_zero_pages's feature. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: xu xin <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Claudio Imbrenda <[email protected]> Cc: Xuexin Jiang <[email protected]> Reviewed-by: Xiaokai Ran <[email protected]> Reviewed-by: Yang Yang <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent df263d9 commit 7927147

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

include/linux/ksm.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ int ksm_disable(struct mm_struct *mm);
2626

2727
int __ksm_enter(struct mm_struct *mm);
2828
void __ksm_exit(struct mm_struct *mm);
29+
/*
30+
* To identify zeropages that were mapped by KSM, we reuse the dirty bit
31+
* in the PTE. If the PTE is dirty, the zeropage was mapped by KSM when
32+
* deduplicating memory.
33+
*/
34+
#define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
2935

3036
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
3137
{

mm/ksm.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long nex
448448
if (is_migration_entry(entry))
449449
page = pfn_swap_entry_to_page(entry);
450450
}
451-
ret = page && PageKsm(page);
451+
/* return 1 if the page is an normal ksm page or KSM-placed zero page */
452+
ret = (page && PageKsm(page)) || is_ksm_zero_pte(*pte);
452453
pte_unmap_unlock(pte, ptl);
453454
return ret;
454455
}
@@ -1222,8 +1223,12 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
12221223
page_add_anon_rmap(kpage, vma, addr, RMAP_NONE);
12231224
newpte = mk_pte(kpage, vma->vm_page_prot);
12241225
} else {
1225-
newpte = pte_mkspecial(pfn_pte(page_to_pfn(kpage),
1226-
vma->vm_page_prot));
1226+
/*
1227+
* Use pte_mkdirty to mark the zero page mapped by KSM, and then
1228+
* we can easily track all KSM-placed zero pages by checking if
1229+
* the dirty bit in zero page's PTE is set.
1230+
*/
1231+
newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)));
12271232
/*
12281233
* We're replacing an anonymous page with a zero page, which is
12291234
* not anonymous. We need to do proper accounting otherwise we

0 commit comments

Comments
 (0)