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: 13401339 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 1AB9AE82CAE for ; Wed, 27 Sep 2023 16:22:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE1C28D0093; Wed, 27 Sep 2023 12:22:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BCA1F8D0098; Wed, 27 Sep 2023 12:22:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A6A748D0099; Wed, 27 Sep 2023 12:22:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 8ED728D0093 for ; Wed, 27 Sep 2023 12:22:47 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 64AB440B08 for ; Wed, 27 Sep 2023 16:22:47 +0000 (UTC) X-FDA: 81282895974.04.78BD8A3 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf02.hostedemail.com (Postfix) with ESMTP id B29238002B for ; Wed, 27 Sep 2023 16:22:45 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=none; spf=neutral (imf02.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695831765; a=rsa-sha256; cv=none; b=MOel8X68q4ynOJEIJSqwnqOKTjiGVNyB5+zKk/F+9kWEJZ6sq7tWpU146DIpO45CpQYt7E PXa+YFkSRX8c7AyEZs9aS190JkoTP32Elz7Fn91pFHBLE0WYRalTY2t5/y5BO14thRgiuW eVNRc8FrycAAXFN8Y66t+KbI+zCs3/4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; spf=neutral (imf02.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695831765; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=/aF57w/Uo9XQaOfzqfReof8ER8RZzKgHL2lKDiV6uDw=; b=sEy+XFCHdmxyq2CwZfZZZE4rpOsX2/Rgq2uWYyyOvAkCTKtsKYTKnHSn4krNRhd+ZTxvU3 HmGpl92W+GwHQp84pPdzRUpzY1ihOn17EEwMlrXbphS5HI3BHD+LWuH3letR2UW0+E1/qv Eu9pQm+WvgPq28Z9vTrQ5DYkKcuJ8fY= 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: B29238002B X-Stat-Signature: 98xdoj7desaw8jo468beqq43engmq8ax X-HE-Tag: 1695831765-894570 X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linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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