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: 13390255 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 A8DAACD37B0 for ; Mon, 18 Sep 2023 18:58:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 408376B0437; Mon, 18 Sep 2023 14:58:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3B8BA6B0439; Mon, 18 Sep 2023 14:58:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2A7386B043C; Mon, 18 Sep 2023 14:58:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 1A7B26B0437 for ; Mon, 18 Sep 2023 14:58:37 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id F0F834048D for ; Mon, 18 Sep 2023 18:58:36 +0000 (UTC) X-FDA: 81250629432.14.B1DFB30 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf05.hostedemail.com (Postfix) with ESMTP id 6D28E10000D for ; Mon, 18 Sep 2023 18:58:35 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf05.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695063515; 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=2BksyeZtD5bg8naFvQRQvAhO+ynViA0epa8jKPfC7wM=; b=rPYDl/MdagVE8NvawMOS+wJid0sDwEMW8CV7OMnWYztwb9iJxzmIeqHnOBNJ3Ydmp+Z2Kx 2zuLUwT7kHqoDCoqJY0qv7sq5R+4JnNfa9kKwAECnW1IC9FW6Sf/1AT/a0BDRvZzLd7fZI jEuYko5hNNyPGn3fJ7T4znQtPy0/tiA= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf05.hostedemail.com: 66.220.144.178 is neither permitted nor denied by domain of shr@devkernel.io) smtp.mailfrom=shr@devkernel.io ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695063515; a=rsa-sha256; cv=none; b=luOrbCVCoC901zV/O29vBg+8mnaMqUm1I1zY5mGoh0mS6UxwXajTsvWhPs+kQiXbEYn4k6 ZBw3GVluF4nzhBxwiW5gc/xzd7UuKIdQZ51fU3IuzT9NU7LfiaKDlnbI+7x8r962bzCoZr HqJU5kSsuwFMJMxoHMoQB3QNU9rfd8w= 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-Rspamd-Queue-Id: 6D28E10000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 44rsdbscqzzxwruwwryuo8r9j8sy95np X-HE-Tag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, version=1.2.4 Sender: owner-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 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