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 DF790C4332F for ; Fri, 21 Oct 2022 12:54:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CC288E0002; Fri, 21 Oct 2022 08:54:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 57D228E0001; Fri, 21 Oct 2022 08:54:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 46B278E0002; Fri, 21 Oct 2022 08:54:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 385118E0001 for ; Fri, 21 Oct 2022 08:54:30 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id EF4D11C6D03 for ; Fri, 21 Oct 2022 12:54:29 +0000 (UTC) X-FDA: 80044950258.07.E3FBB9F Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id A38DB120019 for ; Fri, 21 Oct 2022 12:54:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666356868; 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=8Qu9bM/ahIqpq1zoKdEkpkNS5tjo6ta77M3NNw/zjMY=; b=CwYNG4YguhjmiAFKG5b+2HPFv7yY7TUVHe4UkcT4/ypIOBQk+qWDorY/hhHSSAzUbxHpFn JZsoO44cxzFTGhnjgvSXFFVbPGGCjrSyEByKKPvEdOagi5GEOqmp4zX1W5zu+uBbsQg0FS CqLOnI68Uiusghwpcoh6Qa6Si7eF1Ao= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-41-MySG4P3MNG2ZdztMUBTrcQ-1; Fri, 21 Oct 2022 08:54:27 -0400 X-MC-Unique: MySG4P3MNG2ZdztMUBTrcQ-1 Received: by mail-wr1-f69.google.com with SMTP id q21-20020adfb195000000b00235fc1cd01fso838947wra.11 for ; Fri, 21 Oct 2022 05:54:25 -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: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=8Qu9bM/ahIqpq1zoKdEkpkNS5tjo6ta77M3NNw/zjMY=; b=k7J5cUSgE50Pw/9w/NnMYy1CO084+wrWxkIhXei0cPMHJCyB0123cYwkho3+XoFCa8 R6BX+kwJG3KAZeNEinSq8B+3EuC8TPfyQa62Er8WQ/8ZLM7Fv5u3ImHu/PaiaiAWsdHi NHJuke14TuG78clQCtqG8cZJzi5cmSUTVpK46//ms5/hGEqZbjcTRFRIHVYvPo3bRXoz kb9PhFlkpAto+Aygh7B4givVljKv8eM3x9C/GJ8aBnEKRXckXjvgihHz0Acrk4DpvwhH XyFtT1jeNj078F7KotN1wXzpToACSdw6CCkXIpjMZxeJvokvZsCYfjdu4sOZ732UlVCE ZdRQ== X-Gm-Message-State: ACrzQf2Sa5W9SA6bG5A31O6/3ohLA0yyDGvK8AVazeJJiKnxGxFu+g35 oKZi3ZY26TwgTceYbWAkTV2KlJVdOy3P0T7byrWaIDpPO4cAUTnIdgt9aYbEMbvKQa7aoCIXJVY oRs2QkjTyeEM= X-Received: by 2002:a5d:6504:0:b0:22e:44b0:4cf5 with SMTP id x4-20020a5d6504000000b0022e44b04cf5mr11838958wru.362.1666356864582; Fri, 21 Oct 2022 05:54:24 -0700 (PDT) X-Google-Smtp-Source: AMsMyM50IKEiJEUnefKIx9fjMl6Gfm/amcquyyV6TcAMib7aQM+Bu46eZiZ5YVaDqDHfxpfLhg0orQ== X-Received: by 2002:a5d:6504:0:b0:22e:44b0:4cf5 with SMTP id x4-20020a5d6504000000b0022e44b04cf5mr11838940wru.362.1666356864291; Fri, 21 Oct 2022 05:54:24 -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 l12-20020adffe8c000000b002356c051b9csm4830999wrr.66.2022.10.21.05.54.23 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Oct 2022 05:54:23 -0700 (PDT) Message-ID: <07eb01c0-3702-7446-11ed-bab2fbb68cc2@redhat.com> Date: Fri, 21 Oct 2022 14:54:22 +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 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> Organization: Red Hat In-Reply-To: <92a38ccc-273c-1c32-6305-09bd4f215550@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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666356869; a=rsa-sha256; cv=none; b=PjvQlormOmNxTV/yXrZZ4FdCSoN9Q++VmcGpCBam915ZNfBfK+4jpH22GippT1BJXRW/b5 2WAMqP4/1zbA4nuRnsCOhU1a/0LB3qsn6DRSB6JMEL/gewEN555pN5rNOHXmMsyxWUc70M DUrYXfgiJjekFMs8OZMYMjIZ/Kolel4= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CwYNG4Yg; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.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=1666356869; 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=8Qu9bM/ahIqpq1zoKdEkpkNS5tjo6ta77M3NNw/zjMY=; b=H72uZ4cOAIfAtKykLc+jLZ1TBUnd/vwpgss7X/EN4HtqdLnfW16opovSk+Qya9l2BUiSo2 lpRyHdPFDGwwHbr7dm6rMo3U++6/l9F4a+34Rdrt6KhslKCbbG6Wy28NxfCWRxVGjNIXCA 9+m4ublrIvj869bFDwN6CsTgKp79gf0= Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=CwYNG4Yg; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.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: oixwenecsa7qqn3xkpdmo6c44npccs9t X-Rspamd-Queue-Id: A38DB120019 X-HE-Tag: 1666356868-552143 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 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). -- Thanks, David / dhildenb