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 CA16FC433FE for ; Fri, 21 Oct 2022 10:17:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72C288E0002; Fri, 21 Oct 2022 06:17:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 70DE58E0001; Fri, 21 Oct 2022 06:17:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CC118E0002; Fri, 21 Oct 2022 06:17:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 4C5568E0001 for ; Fri, 21 Oct 2022 06:17:35 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0D63C81221 for ; Fri, 21 Oct 2022 10:17:34 +0000 (UTC) X-FDA: 80044554870.17.AD9A50B Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf16.hostedemail.com (Postfix) with ESMTP id 7C3E2180035 for ; Fri, 21 Oct 2022 10:17:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666347453; 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=E3QDV5bvpINsOFyLcx9T/2Y+QtSOtpwopuhiVQt5H2k=; b=BH57iwwPaKyGY1+qajF5m/0YAbCe0npvsae08WKTPZegJDLX6O+uWCxK7GkZNxkdmu29ur k6zlSohFGYIHAKlxmg8hZqP+7EOUIQXO6E1OC5ZKjPNT4KymMoV0mbAl/1x+OPrC6Iwc46 M/qME8aImgGFnN2iXWShc6bUQ7PYlpM= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-65-u8rHP2ZyOr-iRMImFykHpg-1; Fri, 21 Oct 2022 06:17:33 -0400 X-MC-Unique: u8rHP2ZyOr-iRMImFykHpg-1 Received: by mail-wm1-f69.google.com with SMTP id n6-20020a7bc5c6000000b003c6bbe5d5cfso3130674wmk.4 for ; Fri, 21 Oct 2022 03:17:32 -0700 (PDT) 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:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=E3QDV5bvpINsOFyLcx9T/2Y+QtSOtpwopuhiVQt5H2k=; b=K4jLyG5V6LuiN35JFgvmxEGZalbERjD7LSvBAoRIOjpoK/88p50EAmcus97JF9UZ0v S0R5xLUFWIxHmQl5JaXj/Z6H91H5xt1SB3JbJjdJWaosvVzJ6XO0vXlW/g4DWoSTl1ft HWsM6R7WmwBWVmclYgFzqF/UHV8VJGfVl8boBXy2ee9y3VtyqJSZhP0Ed99vg2ZwuC+U Ae28A9ZkH70j3ISDFCZ0VjRy8+vl/v5xeCjwufMdoqFkTjTtc6ioP3CNYyf2ryX2JFKR CpP8qLu2KNt9s/QeSW/s1y8OqVUygxQBmQPeXKYjU2MYex/DlPWefQ5iV0Gy9eclC1SM R8DQ== X-Gm-Message-State: ACrzQf1ZMC1NkvTlu5JxtwU9NBN/eXvHpYChXsnPSH4VakHrGKoNLiI9 73n3/vS7HdlBkMFCw8kxEiIRgjRb71ByXfPz3C8eueqw8t8pJjPsj0qf39aHTmmzoNTZ4kIlISk 2W15HAgAFqvw= X-Received: by 2002:adf:ed91:0:b0:22e:4f88:a9fc with SMTP id c17-20020adfed91000000b0022e4f88a9fcmr11133550wro.669.1666347451830; Fri, 21 Oct 2022 03:17:31 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5T3TPG97oH6eFiVLiEc0lkPKhsWPvV7Y08N5Ht7VT2WUk8+lwFfVX+Tfwj7rL455mJbht/1A== X-Received: by 2002:adf:ed91:0:b0:22e:4f88:a9fc with SMTP id c17-20020adfed91000000b0022e4f88a9fcmr11133531wro.669.1666347451521; Fri, 21 Oct 2022 03:17:31 -0700 (PDT) Received: from ?IPV6:2003:cb:c708:1700:e40d:574c:c991:5f78? (p200300cbc7081700e40d574cc9915f78.dip0.t-ipconnect.de. [2003:cb:c708:1700:e40d:574c:c991:5f78]) by smtp.gmail.com with ESMTPSA id o5-20020a5d62c5000000b00228cbac7a25sm18936278wrv.64.2022.10.21.03.17.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Oct 2022 03:17:31 -0700 (PDT) Message-ID: <92a38ccc-273c-1c32-6305-09bd4f215550@redhat.com> Date: Fri, 21 Oct 2022 12:17:30 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.1 Subject: Re: [PATCH v3 2/5] ksm: support unsharing zero pages placed by KSM 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> From: David Hildenbrand Organization: Red Hat In-Reply-To: <20221011022215.322269-1-xu.xin16@zte.com.cn> 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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666347454; a=rsa-sha256; cv=none; b=D1WWFE7iOfkknJJe5tK8MkRLrlsu3t6g8/ayXq9EQ9LmxKPmGSPOtAYp1iO0fn7m4YljVC xi/MMBvyMzlo4yIXHQxbMvPVKreZRqMk8SwjsOzACqLYZvrECgq2GgKxWneK7MmTOfP/qO qFEjYDbiZM0iToSXCCokX77IYyp1itM= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BH57iwwP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666347454; 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=E3QDV5bvpINsOFyLcx9T/2Y+QtSOtpwopuhiVQt5H2k=; b=ddOvyZU0XDRYL/vSK/ZWeP8i8BE/vvtQP6JUO6PDk7gxqK/uMtBKJA+I0THcK4GZoduDBp vVAtIxl5qzv5WROsrb0cmaVIZINtnJdbdmZC/R6ZBNmF2zZB42KbjdvUY65jnjrnl3GGG9 UQdPhczGiKWb/tN4j7KS80rVmp4b8X8= Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=BH57iwwP; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf16.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com X-Rspamd-Server: rspam04 X-Rspam-User: X-Stat-Signature: j48twq43ay93h368i7jhnt1ufk6nrc5u X-Rspamd-Queue-Id: 7C3E2180035 X-HE-Tag: 1666347454-117943 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 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. -- Thanks, David / dhildenb