From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4984AC433FE for ; Wed, 9 Nov 2022 10:40:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D9B346B0072; Wed, 9 Nov 2022 05:40:55 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D4BBA8E0002; Wed, 9 Nov 2022 05:40:55 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3BDA8E0001; Wed, 9 Nov 2022 05:40:55 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B5B886B0072 for ; Wed, 9 Nov 2022 05:40:55 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8F95F161426 for ; Wed, 9 Nov 2022 10:40:55 +0000 (UTC) X-FDA: 80113560870.06.D580968 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf11.hostedemail.com (Postfix) with ESMTP id 2D3054001A for ; Wed, 9 Nov 2022 10:40:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667990405; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=QHh2DP+7XCzgs9NqlI4mZS2rPIPMdYcdAmHYPRcL3I4=; b=AbpYjH7HDkmkGPYdp24cZQ9mwaYGk2f0c2exW9Q9UUedNh6410NUHhns6iZsdmWjX/Z+l7 IulqIUyWsNr7ztdX/1Q7Z3BFhLDzosnce4/m61L1BveXI+l2boRPaQIjWTZoOMO2M35g82 Emmqn4KO6A5fv8Dl2EH3dhIQeukVRa8= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-660-QQjPx-x4MT-rvr5EJDMhMQ-1; Wed, 09 Nov 2022 05:40:04 -0500 X-MC-Unique: QQjPx-x4MT-rvr5EJDMhMQ-1 Received: by mail-wr1-f71.google.com with SMTP id s7-20020adfa287000000b00236c367fcddso4846331wra.6 for ; Wed, 09 Nov 2022 02:40:04 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=QHh2DP+7XCzgs9NqlI4mZS2rPIPMdYcdAmHYPRcL3I4=; b=bRFIVMAB0BEKl5Bg3uSEaZnhy02T9bow1Xff0zv4AmPKihBUG+zHZu5Bt7UaP3oSaC aWggcU9Jbgm5dicQ+J3hFXa0cCymSIIu5KkjPJpDI1jJprD+Bf8dNFnbbYxe3pNro05E dK+qiVM5Ay+g5OBCDCX0Pe/gBJsasdhXqJGtzsaaQw+Nn1Wo2jrMdd2TOuGvEIoeunAs 9SrnqXS1Ng98kckb4n8c8RDqWv2TdsSsRl/AeumYGuS2pQhxq8XEiUijtKeIhW0w4lGZ pwaxCpXPO9xA41bHSzlz9PVbjvp0he8BWANbGSRe6B5Rm9O50HQRo7KVm8M/vmOD6I0K Ujzw== X-Gm-Message-State: ACrzQf0rtLqSl5KYz9FrBunWUpACvijeWDgB5XgbLcrZ89WvJ8TOny8c i0XOXdeGDRKUI3IYZk2ds9bi8v5cpsULK96mTrrG0Z/1Wk6WLgOypLc0nl/XQ0mGf3YuHJYOnz2 EUOWT2Ptn8jY= X-Received: by 2002:adf:f98a:0:b0:236:677c:2407 with SMTP id f10-20020adff98a000000b00236677c2407mr38997618wrr.578.1667990403040; Wed, 09 Nov 2022 02:40:03 -0800 (PST) X-Google-Smtp-Source: AMsMyM7LI+bX7eSwNrmbYuFFFX8YGehweOccrbkXTAREf/6mrXpLXKud+Dxs6Fe9VnJVa8i49wZorQ== X-Received: by 2002:adf:f98a:0:b0:236:677c:2407 with SMTP id f10-20020adff98a000000b00236677c2407mr38997595wrr.578.1667990402674; Wed, 09 Nov 2022 02:40:02 -0800 (PST) Received: from ?IPV6:2003:cb:c704:b000:3b0e:74a3:bc8:9937? (p200300cbc704b0003b0e74a30bc89937.dip0.t-ipconnect.de. [2003:cb:c704:b000:3b0e:74a3:bc8:9937]) by smtp.gmail.com with ESMTPSA id bj19-20020a0560001e1300b002302dc43d77sm13047010wrb.115.2022.11.09.02.40.01 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 09 Nov 2022 02:40:02 -0800 (PST) Message-ID: <3e61fb01-5277-80a4-610e-0608475637f8@redhat.com> Date: Wed, 9 Nov 2022 11:40:01 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: Re: [PATCH v3 2/5] ksm: support unsharing zero pages placed by KSM From: David Hildenbrand To: xu.xin.sc@gmail.com, akpm@linux-foundation.org Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, xu xin , Claudio Imbrenda , Xuexin Jiang , Xiaokai Ran , Yang Yang References: <20221011022006.322158-1-xu.xin16@zte.com.cn> <20221011022215.322269-1-xu.xin16@zte.com.cn> <92a38ccc-273c-1c32-6305-09bd4f215550@redhat.com> <07eb01c0-3702-7446-11ed-bab2fbb68cc2@redhat.com> Organization: Red Hat In-Reply-To: <07eb01c0-3702-7446-11ed-bab2fbb68cc2@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667990451; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QHh2DP+7XCzgs9NqlI4mZS2rPIPMdYcdAmHYPRcL3I4=; b=6dG/PoCEuLDVTrQ+bT5Gq1TZ3K1rI3wMWHyka1+sb1+HQr0gYtoVtz93Gz2LTeMPzh7AbJ ULrnkLgU2m0FxLa5klgb/efj5J+Ph1P0lQ78UBRJFerWk1ZedM5lVjRAvuAnbdkvjOf0rU FlvJAYwVRD9JfF23hZWon0bY5RuFUjc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AbpYjH7H; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667990451; a=rsa-sha256; cv=none; b=SpgtGJA+vCQJZ7EgZqkdyYoZMtPjvD5heE4iFPm4dwk1kB5BSjSpyeUsPDQXVse+8swaPx I5d+pg43XWVTvOUthH6ZpKYP53BaZs5ZnPE/8c3vaFhp7zDtVNbm8cDEj2JQUfUaWh4r16 6MVO+Vd4VWwhV4mzPDgrvMLgBrewC2w= X-Rspamd-Queue-Id: 2D3054001A Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=AbpYjH7H; spf=pass (imf11.hostedemail.com: domain of david@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-Rspamd-Server: rspam10 X-Rspam-User: X-Stat-Signature: etcc4pee1brqfwsh1eo7uttp1c6audzb X-HE-Tag: 1667990450-330052 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On 21.10.22 14:54, David Hildenbrand wrote: > On 21.10.22 12:17, David Hildenbrand wrote: >> On 11.10.22 04:22, xu.xin.sc@gmail.com wrote: >>> From: xu xin >>> >>> 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 >>> Cc: Claudio Imbrenda >>> Cc: Xuexin Jiang >>> Signed-off-by: xu xin >>> Co-developed-by: Xiaokai Ran >>> Signed-off-by: Xiaokai Ran >>> Co-developed-by: Yang Yang >>> Signed-off-by: Yang Yang >>> Signed-off-by: xu xin >>> --- >>> 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). > Posted: https://lkml.kernel.org/r/20221107161740.144456-1-david@redhat.com With that, we can use FAULT_FLAG_UNSHARE also to break COW on the shared zeropage. -- Thanks, David / dhildenb