Skip to content

Commit c2dc78b

Browse files
cmzxoakpm00
authored andcommitted
mm/ksm: fix ksm_zero_pages accounting
We normally ksm_zero_pages++ in ksmd when page is merged with zero page, but ksm_zero_pages-- is done from page tables side, where there is no any accessing protection of ksm_zero_pages. So we can read very exceptional value of ksm_zero_pages in rare cases, such as -1, which is very confusing to users. Fix it by changing to use atomic_long_t, and the same case with the mm->ksm_zero_pages. Link: https://lkml.kernel.org/r/[email protected] Fixes: e294206 ("ksm: count all zero pages placed by KSM") Fixes: 6080d19 ("ksm: add ksm zero pages for each process") Signed-off-by: Chengming Zhou <[email protected]> Acked-by: David Hildenbrand <[email protected]> Cc: Andrea Arcangeli <[email protected]> Cc: Hugh Dickins <[email protected]> Cc: Ran Xiaokai <[email protected]> Cc: Stefan Roesch <[email protected]> Cc: xu xin <[email protected]> Cc: Yang Yang <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 730cdc2 commit c2dc78b

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

fs/proc/base.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3214,7 +3214,7 @@ static int proc_pid_ksm_stat(struct seq_file *m, struct pid_namespace *ns,
32143214
mm = get_task_mm(task);
32153215
if (mm) {
32163216
seq_printf(m, "ksm_rmap_items %lu\n", mm->ksm_rmap_items);
3217-
seq_printf(m, "ksm_zero_pages %lu\n", mm->ksm_zero_pages);
3217+
seq_printf(m, "ksm_zero_pages %ld\n", mm_ksm_zero_pages(mm));
32183218
seq_printf(m, "ksm_merging_pages %lu\n", mm->ksm_merging_pages);
32193219
seq_printf(m, "ksm_process_profit %ld\n", ksm_process_profit(mm));
32203220
mmput(mm);

include/linux/ksm.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,27 @@ void __ksm_exit(struct mm_struct *mm);
3333
*/
3434
#define is_ksm_zero_pte(pte) (is_zero_pfn(pte_pfn(pte)) && pte_dirty(pte))
3535

36-
extern unsigned long ksm_zero_pages;
36+
extern atomic_long_t ksm_zero_pages;
37+
38+
static inline void ksm_map_zero_page(struct mm_struct *mm)
39+
{
40+
atomic_long_inc(&ksm_zero_pages);
41+
atomic_long_inc(&mm->ksm_zero_pages);
42+
}
3743

3844
static inline void ksm_might_unmap_zero_page(struct mm_struct *mm, pte_t pte)
3945
{
4046
if (is_ksm_zero_pte(pte)) {
41-
ksm_zero_pages--;
42-
mm->ksm_zero_pages--;
47+
atomic_long_dec(&ksm_zero_pages);
48+
atomic_long_dec(&mm->ksm_zero_pages);
4349
}
4450
}
4551

52+
static inline long mm_ksm_zero_pages(struct mm_struct *mm)
53+
{
54+
return atomic_long_read(&mm->ksm_zero_pages);
55+
}
56+
4657
static inline int ksm_fork(struct mm_struct *mm, struct mm_struct *oldmm)
4758
{
4859
if (test_bit(MMF_VM_MERGEABLE, &oldmm->flags))

include/linux/mm_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ struct mm_struct {
985985
* Represent how many empty pages are merged with kernel zero
986986
* pages when enabling KSM use_zero_pages.
987987
*/
988-
unsigned long ksm_zero_pages;
988+
atomic_long_t ksm_zero_pages;
989989
#endif /* CONFIG_KSM */
990990
#ifdef CONFIG_LRU_GEN_WALKS_MMU
991991
struct {

mm/ksm.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ static bool ksm_use_zero_pages __read_mostly;
296296
static bool ksm_smart_scan = true;
297297

298298
/* The number of zero pages which is placed by KSM */
299-
unsigned long ksm_zero_pages;
299+
atomic_long_t ksm_zero_pages = ATOMIC_LONG_INIT(0);
300300

301301
/* The number of pages that have been skipped due to "smart scanning" */
302302
static unsigned long ksm_pages_skipped;
@@ -1429,8 +1429,7 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,
14291429
* the dirty bit in zero page's PTE is set.
14301430
*/
14311431
newpte = pte_mkdirty(pte_mkspecial(pfn_pte(page_to_pfn(kpage), vma->vm_page_prot)));
1432-
ksm_zero_pages++;
1433-
mm->ksm_zero_pages++;
1432+
ksm_map_zero_page(mm);
14341433
/*
14351434
* We're replacing an anonymous page with a zero page, which is
14361435
* not anonymous. We need to do proper accounting otherwise we
@@ -3374,7 +3373,7 @@ static void wait_while_offlining(void)
33743373
#ifdef CONFIG_PROC_FS
33753374
long ksm_process_profit(struct mm_struct *mm)
33763375
{
3377-
return (long)(mm->ksm_merging_pages + mm->ksm_zero_pages) * PAGE_SIZE -
3376+
return (long)(mm->ksm_merging_pages + mm_ksm_zero_pages(mm)) * PAGE_SIZE -
33783377
mm->ksm_rmap_items * sizeof(struct ksm_rmap_item);
33793378
}
33803379
#endif /* CONFIG_PROC_FS */
@@ -3663,7 +3662,7 @@ KSM_ATTR_RO(pages_skipped);
36633662
static ssize_t ksm_zero_pages_show(struct kobject *kobj,
36643663
struct kobj_attribute *attr, char *buf)
36653664
{
3666-
return sysfs_emit(buf, "%ld\n", ksm_zero_pages);
3665+
return sysfs_emit(buf, "%ld\n", atomic_long_read(&ksm_zero_pages));
36673666
}
36683667
KSM_ATTR_RO(ksm_zero_pages);
36693668

@@ -3672,7 +3671,7 @@ static ssize_t general_profit_show(struct kobject *kobj,
36723671
{
36733672
long general_profit;
36743673

3675-
general_profit = (ksm_pages_sharing + ksm_zero_pages) * PAGE_SIZE -
3674+
general_profit = (ksm_pages_sharing + atomic_long_read(&ksm_zero_pages)) * PAGE_SIZE -
36763675
ksm_rmap_items * sizeof(struct ksm_rmap_item);
36773676

36783677
return sysfs_emit(buf, "%ld\n", general_profit);

0 commit comments

Comments
 (0)