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 CCE79C46CA1 for ; Mon, 18 Sep 2023 11:10:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5BB556B02F5; Mon, 18 Sep 2023 07:10:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 56B566B02F7; Mon, 18 Sep 2023 07:10:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 432DD6B02F9; Mon, 18 Sep 2023 07:10:43 -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 382786B02F5 for ; Mon, 18 Sep 2023 07:10:43 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 0CE1440CB9 for ; Mon, 18 Sep 2023 11:10:43 +0000 (UTC) X-FDA: 81249450366.22.54FF0A5 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf04.hostedemail.com (Postfix) with ESMTP id BC84A40016 for ; Mon, 18 Sep 2023 11:10:40 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aXxwivUS; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.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=1695035440; 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=6/cr+xhXNv4f07NXf+l3nV8fbTg3rSUgiXuUzeU2fnA=; b=2FCFcdaZ+MyJjWRvw6J89A6Z68g5W3rm/WrODNSQX/COchus18dSOjA2kv9bSzTe4cr/e1 Sl9YuTnlOECpJDLdWAqyb381Vzi/hwPbT2zxuvIPhXvKF325Zzyq+8o58NvtU0YR7b6vlO 79lmd8o86w/sqgw9EC55F7EJm3C0yh8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=aXxwivUS; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf04.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695035440; a=rsa-sha256; cv=none; b=3tbqfyJgs0tNzq/UFdQfMaMI5D0uAcGWZ5z9S8yOPj8s5j209n9AjTCnQZTk3hFFgbm8Rs 6SduudhPesSWY9u9jjJtUH2y5iWm3bBlRd/M7DmT6iDvG9+kAYqn6VIsyAw3mQtva+ulOg 2lpq48CtS2TDBgtbX3tcWxXtlJXtB+Y= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695035440; 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=6/cr+xhXNv4f07NXf+l3nV8fbTg3rSUgiXuUzeU2fnA=; b=aXxwivUSI3iVsXM3Bsq5o0ndpnk0mZo7oRt3yULrCDv4CIGKYRlqzdhFDZhf2E9pP6+0J2 sqvi6MFfMM7iY74i3MPogsDcK6cgB1IKp6KTexxhWOIgwmLGiFzwk8RKJqmPWy+vLiFjxy 1+gqPtXb4ZRiGlc68oc6xSfmBFpajkA= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-145-euI2Nxl8N-mZGAOyDy4Kxw-1; Mon, 18 Sep 2023 07:10:38 -0400 X-MC-Unique: euI2Nxl8N-mZGAOyDy4Kxw-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-40472855114so30612485e9.0 for ; Mon, 18 Sep 2023 04:10:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695035437; x=1695640237; h=content-transfer-encoding:in-reply-to:subject:organization:from :references:cc:to:content-language:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6/cr+xhXNv4f07NXf+l3nV8fbTg3rSUgiXuUzeU2fnA=; b=fy34gDjKVrox57eo+ej69W8wnrMjma5KF41wGjICxAL4YHN5LjA+ZhRQPcN+GS4vw9 2M2zoc4PU8JmG1XF+YbATquwoyKKEN6PDXjbw6qDye4UyRDXCEeEYC5thTrLVZmOd0oj HYheg6ORhG+vWcK5AxObBhotM48fIcv8cMCcqm3UnzrbGPeh9qivR45rMtXYKoZXjPLH fOBq9Ot5HVJnKfrpohpOg5WovZnIdnIXcWQ5S/eQn4mv1bVsX3sg2ViIYlBwpGNGKE9T kTnjA2kkTtUxEaZdIkdWw82kv+9kN+K4Ds7dK4957/+4ZFZia9SNtmCCGylEq7/2eEQL DBHw== X-Gm-Message-State: AOJu0YyV08ROipa85x/aCXhOfSfWg6clDs23+uChHZ/zAjEsWoqVqqse FygHb8aZ+mxqNV0fsxbVd2OwkPOYIYqgodR/B7oy52m3LF31p55SG6jbBNVOfGoywq6xZknhubR oUwJFGlhAJzw= X-Received: by 2002:a5d:63d1:0:b0:31f:a9db:b838 with SMTP id c17-20020a5d63d1000000b0031fa9dbb838mr6448468wrw.15.1695035437625; Mon, 18 Sep 2023 04:10:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFRs2UBwdGuEnqmlGA4zPmaZO5EZBJQ66/3cNBF2T8XpnS4PAlvdIhR+RuXod4GcbCamLR0jw== X-Received: by 2002:a5d:63d1:0:b0:31f:a9db:b838 with SMTP id c17-20020a5d63d1000000b0031fa9dbb838mr6448451wrw.15.1695035437189; Mon, 18 Sep 2023 04:10:37 -0700 (PDT) Received: from ?IPV6:2003:cb:c74b:3400:ec51:7a3a:274e:cbee? (p200300cbc74b3400ec517a3a274ecbee.dip0.t-ipconnect.de. [2003:cb:c74b:3400:ec51:7a3a:274e:cbee]) by smtp.gmail.com with ESMTPSA id g3-20020a5d64e3000000b003215c6e30cbsm1420122wri.104.2023.09.18.04.10.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Sep 2023 04:10:36 -0700 (PDT) Message-ID: <6447ab02-1a22-efe4-68c9-4f595e2499fc@redhat.com> Date: Mon, 18 Sep 2023 13:10:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 To: Stefan Roesch , kernel-team@fb.com Cc: akpm@linux-foundation.org, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org References: <20230912175228.952039-1-shr@devkernel.io> <20230912175228.952039-2-shr@devkernel.io> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v1 1/4] mm/ksm: add "smart" page scanning mode In-Reply-To: <20230912175228.952039-2-shr@devkernel.io> 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 X-Rspamd-Queue-Id: BC84A40016 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: aatdz8rkzn7tan5n674t8eqxed3eukcm X-HE-Tag: 1695035440-210630 X-HE-Meta: U2FsdGVkX1/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uNx3ybslujv/IOIvsnEuC9UFTyzTZghuX5PXD+lISVvX+IpGpskBLXHHM/LCF0ul+Dl0D+SyvYlglkjoJ4SNX7Zvvsu/IXwVMNBvf94aWaoPYzKs3Pcic/TyoUd8fUB4t6BdCg4pQvwtVtX+19X1tJZNfomBV1Yrn6HxQcq03faEW2gWfeLLwAXmAzXcTk6Nt+5gTgh+j7lhr2EsF3FiXjIRCUgwDCf+x4SJw9shwHbuVIcg//SxG66mWA0GcxF5mf7skU3YwkLsaWV7qLw 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 12.09.23 19:52, Stefan Roesch wrote: > This change adds a "smart" page scanning mode for KSM. So far all the > candidate pages are continuously scanned to find candidates for > de-duplication. There are a considerably number of pages that cannot be > de-duplicated. This is costly in terms of CPU. By using smart scanning > considerable CPU savings can be achieved. > > This change takes the history of scanning pages into account and skips > the page scanning of certain pages for a while if de-deduplication for > this page has not been successful in the past. > > To do this it introduces two new fields in the ksm_rmap_item structure: > age and skip_age. age, is the KSM age and skip_page is the age for how > long page scanning of this page is skipped. The age field is incremented > each time the page is scanned and the page cannot be de-duplicated. > > How often a page is skipped is dependent how often de-duplication has > been tried so far and the number of skips is currently limited to 8. > This value has shown to be effective with different workloads. > > The feature is currently disable by default and can be enabled with the > new smart_scan knob. > > The feature has shown to be very effective: upt to 25% of the page scans > can be eliminated; the pages_to_scan rate can be reduced by 40 - 50% and > a similar de-duplication rate can be maintained. > > Signed-off-by: Stefan Roesch > --- > mm/ksm.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 75 insertions(+) > > diff --git a/mm/ksm.c b/mm/ksm.c > index 981af9c72e7a..bfd5087c7d5a 100644 > --- a/mm/ksm.c > +++ b/mm/ksm.c > @@ -56,6 +56,8 @@ > #define DO_NUMA(x) do { } while (0) > #endif > > +typedef u8 rmap_age_t; > + > /** > * DOC: Overview > * > @@ -193,6 +195,8 @@ struct ksm_stable_node { > * @node: rb node of this rmap_item in the unstable tree > * @head: pointer to stable_node heading this list in the stable tree > * @hlist: link into hlist of rmap_items hanging off that stable_node > + * @age: number of scan iterations since creation > + * @skip_age: skip rmap item until age reaches skip_age > */ > struct ksm_rmap_item { > struct ksm_rmap_item *rmap_list; > @@ -212,6 +216,8 @@ struct ksm_rmap_item { > struct hlist_node hlist; > }; > }; > + rmap_age_t age; > + rmap_age_t skip_age; > }; > > #define SEQNR_MASK 0x0ff /* low bits of unstable tree seqnr */ > @@ -281,6 +287,9 @@ static unsigned int zero_checksum __read_mostly; > /* Whether to merge empty (zeroed) pages with actual zero pages */ > static bool ksm_use_zero_pages __read_mostly; > > +/* Skip pages that couldn't be de-duplicated previously */ > +static bool ksm_smart_scan; > + > /* The number of zero pages which is placed by KSM */ > unsigned long ksm_zero_pages; > > @@ -2305,6 +2314,45 @@ static struct ksm_rmap_item *get_next_rmap_item(struct ksm_mm_slot *mm_slot, > return rmap_item; > } > > +static unsigned int inc_skip_age(rmap_age_t age) > +{ > + if (age <= 3) > + return 1; > + if (age <= 5) > + return 2; > + if (age <= 8) > + return 4; > + > + return 8; > +} > + > +static bool skip_rmap_item(struct page *page, struct ksm_rmap_item *rmap_item) > +{ > + rmap_age_t age; > + > + if (!ksm_smart_scan) > + return false; > + > + if (PageKsm(page)) > + return false; I'm a bit confused about this check here. scan_get_next_rmap_item() would return a PageKsm() page and call cmp_and_merge_page(). cmp_and_merge_page() says: "first see if page can be merged into the stable tree" ... but shouldn't a PageKsm page *already* be in the stable tree? Maybe that's what cmp_and_merge_page() does via: kpage = stable_tree_search(page); if (kpage == page && rmap_item->head == stable_node) { put_page(kpage); return; } Hoping you can enlighten me :) > + > + age = rmap_item->age++; Can't we overflow here? Is that desired, or would you want to stop at the maximum you can store? > + if (age < 3) > + return false; > + > + if (rmap_item->skip_age == age) { > + rmap_item->skip_age = 0; > + return false; > + } > + > + if (rmap_item->skip_age == 0) { > + rmap_item->skip_age = age + inc_skip_age(age); Can't you overflow here as well? > + remove_rmap_item_from_tree(rmap_item); Can you enlighten me why that is required? > + } > + > + return true; > +} > + -- Cheers, David / dhildenb