linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: David Hildenbrand <david@redhat.com>
To: xu.xin.sc@gmail.com, akpm@linux-foundation.org
Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	xu xin <xu.xin16@zte.com.cn>,
	Claudio Imbrenda <imbrenda@linux.ibm.com>,
	Xuexin Jiang <jiang.xuexin@zte.com.cn>,
	Xiaokai Ran <ran.xiaokai@zte.com.cn>,
	Yang Yang <yang.yang29@zte.com.cn>
Subject: Re: [PATCH v3 2/5] ksm: support unsharing zero pages placed by KSM
Date: Fri, 21 Oct 2022 14:54:22 +0200	[thread overview]
Message-ID: <07eb01c0-3702-7446-11ed-bab2fbb68cc2@redhat.com> (raw)
In-Reply-To: <92a38ccc-273c-1c32-6305-09bd4f215550@redhat.com>

On 21.10.22 12:17, David Hildenbrand wrote:
> On 11.10.22 04:22, xu.xin.sc@gmail.com wrote:
>> From: xu xin <xu.xin16@zte.com.cn>
>>
>> use_zero_pages may be very useful, not just because of cache colouring
>> as described in doc, but also because use_zero_pages can accelerate
>> merging empty pages when there are plenty of empty pages (full of zeros)
>> as the time of page-by-page comparisons (unstable_tree_search_insert) is
>> saved.
>>
>> But when enabling use_zero_pages, madvise(addr, len, MADV_UNMERGEABLE) and
>> other ways (like write 2 to /sys/kernel/mm/ksm/run) to trigger unsharing
>> will *not* unshare the shared zeropage as placed by KSM (which may be
>> against the MADV_UNMERGEABLE documentation at least).
>>
>> To not blindly unshare all shared zero_pages in applicable VMAs, the patch
>> introduces a dedicated flag ZERO_PAGE_FLAG to mark the rmap_items of those
>> shared zero_pages. and guarantee that these rmap_items will be not freed
>> during the time of zero_pages not being writing, so we can only unshare
>> the *KSM-placed* zero_pages.
>>
>> 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.
>>
>> Fixes: e86c59b1b12d ("mm/ksm: improve deduplication of zero pages with colouring")
>> Reported-by: David Hildenbrand <david@redhat.com>
>> Cc: Claudio Imbrenda <imbrenda@linux.ibm.com>
>> Cc: Xuexin Jiang <jiang.xuexin@zte.com.cn>
>> Signed-off-by: xu xin <xu.xin16@zte.com.cn>
>> Co-developed-by: Xiaokai Ran <ran.xiaokai@zte.com.cn>
>> Signed-off-by: Xiaokai Ran <ran.xiaokai@zte.com.cn>
>> Co-developed-by: Yang Yang <yang.yang29@zte.com.cn>
>> Signed-off-by: Yang Yang <yang.yang29@zte.com.cn>
>> Signed-off-by: xu xin <xu.xin16@zte.com.cn>
>> ---
>>    mm/ksm.c | 136 ++++++++++++++++++++++++++++++++++++++++++-------------
>>    1 file changed, 105 insertions(+), 31 deletions(-)
>>
>> diff --git a/mm/ksm.c b/mm/ksm.c
>> index 13c60f1071d8..e351d7b6d15e 100644
>> --- a/mm/ksm.c
>> +++ b/mm/ksm.c
>> @@ -213,6 +213,7 @@ struct ksm_rmap_item {
>>    #define SEQNR_MASK	0x0ff	/* low bits of unstable tree seqnr */
>>    #define UNSTABLE_FLAG	0x100	/* is a node of the unstable tree */
>>    #define STABLE_FLAG	0x200	/* is listed from the stable tree */
>> +#define ZERO_PAGE_FLAG 0x400 /* is zero page placed by KSM */
>>    
>>    /* The stable and unstable tree heads */
>>    static struct rb_root one_stable_tree[1] = { RB_ROOT };
>> @@ -381,14 +382,6 @@ static inline struct ksm_rmap_item *alloc_rmap_item(void)
>>    	return rmap_item;
>>    }
>>    
>> -static inline void free_rmap_item(struct ksm_rmap_item *rmap_item)
>> -{
>> -	ksm_rmap_items--;
>> -	rmap_item->mm->ksm_rmap_items--;
>> -	rmap_item->mm = NULL;	/* debug safety */
>> -	kmem_cache_free(rmap_item_cache, rmap_item);
>> -}
>> -
>>    static inline struct ksm_stable_node *alloc_stable_node(void)
>>    {
>>    	/*
>> @@ -420,7 +413,8 @@ static inline bool ksm_test_exit(struct mm_struct *mm)
>>    }
>>    
>>    /*
>> - * We use break_ksm to break COW on a ksm page: it's a stripped down
>> + * We use break_ksm to break COW on a ksm page or KSM-placed zero page (only
>> + * happen when enabling use_zero_pages): it's a stripped down
>>     *
>>     *	if (get_user_pages(addr, 1, FOLL_WRITE, &page, NULL) == 1)
>>     *		put_page(page);
>> @@ -434,7 +428,8 @@ static inline bool ksm_test_exit(struct mm_struct *mm)
>>     * of the process that owns 'vma'.  We also do not want to enforce
>>     * protection keys here anyway.
>>     */
>> -static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
>> +static int break_ksm(struct vm_area_struct *vma, unsigned long addr,
>> +				     bool ksm_check_bypass)
>>    {
>>    	struct page *page;
>>    	vm_fault_t ret = 0;
>> @@ -449,6 +444,16 @@ static int break_ksm(struct vm_area_struct *vma, unsigned long addr)
>>    			ret = handle_mm_fault(vma, addr,
>>    					      FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE,
>>    					      NULL);
>> +		else if (ksm_check_bypass && is_zero_pfn(page_to_pfn(page))) {
>> +			/*
>> +			 * Although it's not ksm page, it's zero page as placed by
>> +			 * KSM use_zero_page, so we should unshare it when
>> +			 * ksm_check_bypass is true.
>> +			 */
>> +			ret = handle_mm_fault(vma, addr,
>> +						  FAULT_FLAG_WRITE | FAULT_FLAG_REMOTE,
>> +						  NULL);
>> +		}
> 
> Please don't duplicate that page fault triggering code.
> 
> Also, please be aware that this collides with
> 
> https://lkml.kernel.org/r/20221021101141.84170-1-david@redhat.com
> 
> Adjustments should be comparatively easy.

... except that I'm still working on FAULT_FLAG_UNSHARE support for the 
shared zeropage. That will be posted soonish (within next 2 weeks).

-- 
Thanks,

David / dhildenb



  reply	other threads:[~2022-10-21 12:54 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-11  2:20 [PATCH v3 0/5] ksm: support tracking KSM-placed zero-pages xu.xin.sc
2022-10-11  2:21 ` [PATCH v3 1/5] ksm: abstract the function try_to_get_old_rmap_item xu.xin.sc
2022-10-11  2:22 ` [PATCH v3 2/5] ksm: support unsharing zero pages placed by KSM xu.xin.sc
2022-10-21 10:17   ` David Hildenbrand
2022-10-21 12:54     ` David Hildenbrand [this message]
2022-11-09 10:40       ` David Hildenbrand
2022-11-14  3:02         ` xu xin
2022-10-11  2:22 ` [PATCH v3 3/5] ksm: count all " xu.xin.sc
2022-10-11  2:22 ` [PATCH v3 4/5] ksm: count zero pages for each process xu.xin.sc
2022-10-11  2:23 ` [PATCH v3 5/5] ksm: add zero_pages_sharing documentation xu.xin.sc
2022-10-17 23:55 ` [PATCH v3 0/5] ksm: support tracking KSM-placed zero-pages Andrew Morton
2022-10-18  9:00   ` xu xin
2022-10-18 22:54     ` [PATCH " Andrew Morton
2022-10-21 10:18       ` David Hildenbrand
2022-10-24  3:07         ` xu xin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=07eb01c0-3702-7446-11ed-bab2fbb68cc2@redhat.com \
    --to=david@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=imbrenda@linux.ibm.com \
    --cc=jiang.xuexin@zte.com.cn \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=ran.xiaokai@zte.com.cn \
    --cc=xu.xin.sc@gmail.com \
    --cc=xu.xin16@zte.com.cn \
    --cc=yang.yang29@zte.com.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox