Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp6098049rdb; Mon, 18 Sep 2023 04:20:59 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF989k/nqWeOXfVHYOZXA4UqTZ5e9hE8QZILW7NodAPkQjUzrKSa73xAbnnZu6P8yatvgHM X-Received: by 2002:a05:6a20:7f9e:b0:149:602e:9239 with SMTP id d30-20020a056a207f9e00b00149602e9239mr12298402pzj.21.1695036059554; Mon, 18 Sep 2023 04:20:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695036059; cv=none; d=google.com; s=arc-20160816; b=0YxzAm1Fo/G2OY2P3g6Br6+JIh6wQkWEARuLKIEV5iG3OdxbcBktu9C1Cco2IyPoeV QQX87fNcWfXlEbIp3MlMIjtj0vUukmflk+frS6fcOYCylq77qkgjXOi7D88j7WQVwlj0 Rih8AMxeiGAom2qKAl4PpBv6JKt1Ts4VMlfCOZ/MlLrElujRrfvceaRoE55FdMmCSI5w +AD9+1IXmGhimGPKUorPO6gA5Rjjt7tQwndbigQkl0SK5D6GkQaM8vDhaFuM5RyrtXXh Asrudf2fvVuFCnCDHbVZVuK7F3brEA40Y8bOA02n0IfQ6RTyovDt9ELbr0cqsWQ6ziAl hbig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :organization:from:references:cc:to:content-language:user-agent :mime-version:date:message-id:dkim-signature; bh=6/cr+xhXNv4f07NXf+l3nV8fbTg3rSUgiXuUzeU2fnA=; fh=rj364kmaeZMVM5HdKawlrVtJQHBt+LI407FoTGJZ9pc=; b=uDaALbmBtBb+yBrgL553zHWQKag7uY8NHnjhs122TUM6VME61dS2PJ+nTT5SGM+EBZ 9Js1bGyoY4GU7dstiT2qLg7V2/rnHo1dD8RmmU4yLLKEfaZiVFrDn5HhZM/C3Zcr2dUh GkwmrVsK5a9nWORArKGu0bDABkpnWLsRJbfE7piwHbQpmOE+Zmmsd43GLAJcfyZnEdt/ W7VabYT7xGLgAgIAjT1tYgt1ADq1SBDOjWFEIjPUpbqUUOX6hcmVWgFIPO71U4UkckS7 mogLlcev92k0nPqnx6x+1JX0Sgh6tF2gePy7dElVjBL5V6j2O27ZqDtaQcY+iYxjOtKU nSEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aXxwivUS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from groat.vger.email (groat.vger.email. [2620:137:e000::3:5]) by mx.google.com with ESMTPS id cm10-20020a056a00338a00b0068fbace5bfcsi7869140pfb.149.2023.09.18.04.20.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 04:20:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) client-ip=2620:137:e000::3:5; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aXxwivUS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:5 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id C2C1A80965B0; Mon, 18 Sep 2023 04:12:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241345AbjIRLMU (ORCPT + 99 others); Mon, 18 Sep 2023 07:12:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241468AbjIRLME (ORCPT ); Mon, 18 Sep 2023 07:12:04 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 42FED185 for ; Mon, 18 Sep 2023 04:10:41 -0700 (PDT) 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-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-108-nR8oECNEN5K14rXmqw3VlQ-1; Mon, 18 Sep 2023 07:10:38 -0400 X-MC-Unique: nR8oECNEN5K14rXmqw3VlQ-1 Received: by mail-wr1-f72.google.com with SMTP id ffacd0b85a97d-31aef4011cfso2917881f8f.2 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=pMIZ5atdW9v2X9bQ4oSP1HjRIBZaMa2hF8VJ702hhoacbdZn2HzpcQe06rycIQcddM 4um+CdWvZKOH9mLA82xNbf+y9S+O5k320W+zsh7LQaX1SmjxY/XBR2buBmShzvYTMci0 qwwpu1mmGdg7hNPsR6wuiW3j/egMt277/WqE0yW7wvz9gp1onraq3wPJXDeO0npfkn64 V1mvIF6yoNHFrMqKnqJ+z3zJLri2wmxlVZkdd18GdteqKrtzj4gPy0gmN22pIcv0YtnS 8nwkOhFPbLysqvF0fwVz9e/SI88IGRuqQf7WUEP32oahrrmpancX1AOyRbHB5AHuwGX9 pCOg== X-Gm-Message-State: AOJu0YwsM4Uy6rrPj7Rk4oflIpYfKtn+PuacMVWIfinF4QZwENck0Y7i MyUph0grneouIomvNTJP68ugGOUDQpZ6kJiOPyvw7k2ve9tx6ajClCtmfUqGI6ASxTkxg/vG2fO smMfDmajwnX/VC94XpM51r/aU X-Received: by 2002:a5d:63d1:0:b0:31f:a9db:b838 with SMTP id c17-20020a5d63d1000000b0031fa9dbb838mr6448466wrw.15.1695035437625; Mon, 18 Sep 2023 04:10:37 -0700 (PDT) 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 Content-Language: en-US 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> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 18 Sep 2023 04:12:48 -0700 (PDT) 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