From patchwork Wed Sep 27 16:22:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 14602 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp2782966vqu; Wed, 27 Sep 2023 10:25:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFj09dcSENG1LsC/oNMgWgPGVgDwFhVIBo+xnkLGJJwDmXDCCSUxI423Eg2xQP4SQGKvMMU X-Received: by 2002:a17:90a:128e:b0:268:5558:de4c with SMTP id g14-20020a17090a128e00b002685558de4cmr2263804pja.38.1695835511791; Wed, 27 Sep 2023 10:25:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695835511; cv=none; d=google.com; s=arc-20160816; b=QSEVHXohhtdrye4CDUSZ8XZqmJ0VN2gEIcGDvi/6JuO4scK9VcKHSs87lyWXatiozF l6FfETK/EaLwBiDTO+CC93eTK8Uv3eWAD63FTb1XF1cO1co4Tu/wwqGqpN096TBcY5Wu qXAS15lpx4UZyqeJqAeQ+18TaTBZMDkC8j6sDvPt29gac1W6cR7SP0mNZhNbBYTy/XQm 5ta1m6Sqx+Sxlhp0naTP/ILqBfo780CxuBgZcwu2r5UTNjCbC4nGFyETdpTaGiWy7WdE xrFvXcBDX3TiaI2ndUZQnIv1ivAGTKSMY41EaxDLwUZOm3lnkkp5DmyanKGPTtr0PhCd RKrA== 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=/aF57w/Uo9XQaOfzqfReof8ER8RZzKgHL2lKDiV6uDw=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=okCREhA0ASJ6a6w6CZNnrr2R4AFgnyjDp2hvNYbag75lszQTFe0yGm9c6cvVqXwyJP nJSJKBpEBwgn8AlbNuxFTtWIMTtDdA+Q7iiRJWAqsYdPRzyHwDmBUrEvmIKHmvD8Hnhi zMBs1kaLYF3G6YOldZ7Tjc4mITThe2alyKyZTTCZXcNz5qoRghD9vc0yv6wuU6fx86kQ 1NN6wbJpRI8gFcCrq/xUZE+o6MQnsggPvn5kfD4ZKm6n/Gewka1xjL5m5W9KQQb9C3IK hmWM/HtNdn0cOWKMmADtFIStB8wmNvfHNRj76fTYBs1LvGDmATxigcAvo7o2eNrIjbrm zQww== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id v14-20020a17090a7c0e00b002748712d225si18437716pjf.1.2023.09.27.10.25.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Sep 2023 10:25:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 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 morse.vger.email (Postfix) with ESMTP id 3EEF0803424D; Wed, 27 Sep 2023 09:23:33 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231597AbjI0QWu (ORCPT + 19 others); Wed, 27 Sep 2023 12:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230164AbjI0QWr (ORCPT ); Wed, 27 Sep 2023 12:22:47 -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 6C490DE for ; Wed, 27 Sep 2023 09:22:46 -0700 (PDT) Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id 30125CA4DC0D; Wed, 27 Sep 2023 09:22:32 -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 v4 0/4] Smart scanning mode for KSM Date: Wed, 27 Sep 2023 09:22:18 -0700 Message-Id: <20230927162222.3827551-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 morse.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 (morse.vger.email [0.0.0.0]); Wed, 27 Sep 2023 09:23:33 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778212417492489358 X-GMAIL-MSGID: 1778212417492489358 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 how often we skip a page. 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 enabled by default. With /sys/kernel/mm/ksm/smart_scan smart scanning can be disabled. 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: - V4: - Changed default from disabled to enabled - Changed commit message for first patch to mention it is enabled - Changed documentation to mention it is enabled by default - Changed cover letter description to mention it is enabled by default - V3: - Renamed field skip_age to remaining_skips - Moved fields after old_checksum - Changed should_skip_rmap_item to use remaining_skips field - 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 | 115 +++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) base-commit: 15bcc9730fcd7526a3b92eff105d6701767a53bb