From patchwork Tue Sep 12 17:52:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13869 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp582070vqx; Tue, 12 Sep 2023 10:57:58 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEoEOaTOD1esA1EGgktjJOKWMrZxqjwKxdiCiwhWq2bFA9Vd1PgPqI5ALo3ivE+UjpF/4UG X-Received: by 2002:a17:90a:7e94:b0:271:90d6:f335 with SMTP id j20-20020a17090a7e9400b0027190d6f335mr5853pjl.25.1694541478106; Tue, 12 Sep 2023 10:57:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694541478; cv=none; d=google.com; s=arc-20160816; b=1G8/smujT0staho7sgeu/ev18QEiCzJgQlCvtmuLIXlRqfs9xjx8/2wiMArMB4lwYa rjarU7BY7ZfOdu44GOwnWwHLgZKe2jc1v12ex3itD1bbFiu6woWPUoULneBJ8y1yXPTV 2+P65cyTiUGbmDeEVoQyTRZ/6Qg3yH1W73fxD/d7znPqmdGlLvkDFpyDJo38m0oh6A1p A6mvJMoBtyjJNVHImyA2ylDwMcgAb6yHp45mJ7whYmuqzMYiE7WVwaHcnRRNCCsWFPW9 jBYk+qG2Wuv14ocRIphckZ+SIE5fFqDqFi5EIMR+0D8kCa0+i4xUVZEdqACNTcpGACRu HG2A== 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=l+n9FJ6lOfF30jIQ75PT+DFsBoxSFVNR8rLKeRoXW5k=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=uoypuZZsZ2K1bdeb3BN7Xx9Oe6b6vgTvn0XBr/gGCye4aY0wUjlMUyXEyVKfdFLnRn k69ZBLKwb79jLNBXP7YCi7V9xoKVWOKw76O0c0Y2+Aqj4Nq76MashM6/nZBC731TchI0 7OOrCDz9DGGZzx9I6RIXG7WrjlJzXHkzMARN8FGTfJemkgXnawH0neufCcwbPJYySCuo Yzco0/y0CjjZJc+xggx5QoO0H+pS6DsxtJy+2HJhcNrgxD3GUSFwQovrKZ9A+WcbT/o4 SOCU3co6uZISswszX07pgp9/X90Qd3si8mzvqltzI0nmSS5tWuJcVbv5mAcTNlLXzp6s 11sw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id mm20-20020a17090b359400b0026b4d5844ccsi8190340pjb.27.2023.09.12.10.57.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 10:57:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id A393D818CC90; Tue, 12 Sep 2023 10:53:59 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237267AbjILRxq (ORCPT + 36 others); Tue, 12 Sep 2023 13:53:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236933AbjILRxX (ORCPT ); Tue, 12 Sep 2023 13:53:23 -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 C6EF01FF0 for ; Tue, 12 Sep 2023 10:52:54 -0700 (PDT) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id D9E9DBCD1627; Tue, 12 Sep 2023 10:52:38 -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 v1 0/4] Smart scanning mode for KSM Date: Tue, 12 Sep 2023 10:52:24 -0700 Message-Id: <20230912175228.952039-1-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 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 (agentk.vger.email [0.0.0.0]); Tue, 12 Sep 2023 10:53:59 -0700 (PDT) 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 agentk.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776855525181001157 X-GMAIL-MSGID: 1776855525181001157 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 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 | 87 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) base-commit: 15bcc9730fcd7526a3b92eff105d6701767a53bb