From patchwork Mon Sep 18 18:58:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 14164 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp2891293vqi; Mon, 18 Sep 2023 12:11:03 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFDeqHGw2IoxElvt+sOxBOVNLzaU98I9EMM2bbNyWDTlFsbOpr1Rb6EbeEcXbby1bsnP/Bj X-Received: by 2002:a17:902:7202:b0:1bf:22b7:86d with SMTP id ba2-20020a170902720200b001bf22b7086dmr7407634plb.3.1695064263225; Mon, 18 Sep 2023 12:11:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695064263; cv=none; d=google.com; s=arc-20160816; b=lkMKaEUTULE1o4jn0GqarX+3+dhLKKc17DerdEzNq4hwPtlKC7Ph8SvTTXVmDZmYwd 3wJn/NkXtMIwuLI6ewmFZUbsRXB3arl3yyNhM9mpKYlSDWfVSUVaCoZruQtcOfwgDD78 6CdqOYcM5JMlYzyAg3lzdaxncjye9RieXe80lMIfjVmcK/u7wK5UGnOzYJeQWGAtL4jE Cdsy4vtxQEc1I0CWJLr+/6SRXvsaLq2kaD0c2NW4Eajn5xWPUVSbtGeiOfY08Kd6hsZw mi3xJwvnwcWoCqvH9lZnTRT1VcJWFc9gWBmfiAiG9dyX9FB4z1Sx6/JtHt3UOelkSITv 7drA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=2BksyeZtD5bg8naFvQRQvAhO+ynViA0epa8jKPfC7wM=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=rfXSo5yEbscjkBNMkSIW1HvXQ/gZzb6UGD2YAF9/wH/4GVQT9SiNuQX9rzVcRxWts9 ZrDLKgS1KfSdU23ol3/U4lqCRcX1n6wsjG4XIvja8yMU6tJc5r3ADRtLKh6CkzJVoQwA ctHXbi1+q5UdQpUObTbKFeJrW5u3tlBbTQV9e8RgqaLRaiXyGIC6S7/5zUOdTlNtZk+Q yCvRg6h6rVugA4I2pWbXlIWKkvJXoOjkRvHVW2wiNZaG6mPPNbj4yLR68+aKO7m/d9j+ wmH0ROCj64LvBByXO/lMHphbIjS9AuPA7POYz4aSk3xuziowz2qHF4k0UDKKzYANUlCm yd8Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id b13-20020a170902d50d00b001b9d0ad0d40si8270365plg.128.2023.09.18.12.11.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Sep 2023 12:11:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 8A5A880D2AE8; Mon, 18 Sep 2023 11:58:51 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229892AbjIRS6p (ORCPT + 25 others); Mon, 18 Sep 2023 14:58:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229839AbjIRS6m (ORCPT ); Mon, 18 Sep 2023 14:58:42 -0400 Received: from 66-220-144-179.mail-mxout.facebook.com (66-220-144-179.mail-mxout.facebook.com [66.220.144.179]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12BDC116 for ; Mon, 18 Sep 2023 11:58:36 -0700 (PDT) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 0F2F0C25F823; Mon, 18 Sep 2023 11:58:22 -0700 (PDT) From: Stefan Roesch To: kernel-team@fb.com Cc: shr@devkernel.io, akpm@linux-foundation.org, david@redhat.com, hannes@cmpxchg.org, riel@surriel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2 0/4] Smart scanning mode for KSM Date: Mon, 18 Sep 2023 11:58:12 -0700 Message-Id: <20230918185816.1518366-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,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 fry.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 (fry.vger.email [0.0.0.0]); Mon, 18 Sep 2023 11:58:51 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777403705225338133 X-GMAIL-MSGID: 1777403705225338133 This patch series adds "smart scanning" for KSM. What is smart scanning? ======================= KSM evaluates all the candidate pages for each scan. It does not use historic information from previous scans. This has the effect that candidate pages that couldn't be used for KSM de-duplication continue to be evaluated for each scan. The idea of "smart scanning" is to keep historic information. With the historic information we can temporarily skip the candidate page for one or several scans. Details: ======== "Smart scanning" is to keep two small counters to store if the page has been used for KSM. One counter stores how often we already tried to use the page for KSM and the other counter stores when a page will be used as a candidate page again. How often we skip the candidate page depends how often a page failed KSM de-duplication. The code skips a maximum of 8 times. During testing this has shown to be a good compromise for different workloads. New sysfs knob: =============== Smart scanning is not enabled by default. With /sys/kernel/mm/ksm/smart_scan smart scanning can be enabled. Monitoring: =========== To monitor how effective smart scanning is a new sysfs knob has been introduced. /sys/kernel/mm/pages_skipped report how many pages have been skipped by smart scanning. Results: ======== - Various workloads have shown a 20% - 25% reduction in page scans For the instagram workload for instance, the number of pages scanned has been reduced from over 20M pages per scan to less than 15M pages. - Less pages scans also resulted in an overall higher de-duplication rate as some shorter lived pages could be de-duplicated additionally - Less pages scanned allows to reduce the pages_to_scan parameter and this resulted in a 25% reduction in terms of CPU. - The improvements have been observed for workloads that enable KSM with madvise as well as prctl Changes: - V2: - Renamed function inc_skip_age() to skip_age() - Added comment to skip_age() function - Renamed function skip_rmap_item() to should_skip_rmap_item() - Added more comments to should_skip_rmap_item function - Added explicit modification of age with overflow check Stefan Roesch (4): mm/ksm: add "smart" page scanning mode mm/ksm: add pages_skipped metric mm/ksm: document smart scan mode mm/ksm: document pages_skipped sysfs knob Documentation/admin-guide/mm/ksm.rst | 11 +++ mm/ksm.c | 120 +++++++++++++++++++++++++++ 2 files changed, 131 insertions(+) base-commit: 15bcc9730fcd7526a3b92eff105d6701767a53bb