From patchwork Wed Dec 13 18:27:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13491749 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 9F3E2C4332F for ; Wed, 13 Dec 2023 18:27:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 60FFD6B0504; Wed, 13 Dec 2023 13:27:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4D97E8D0049; Wed, 13 Dec 2023 13:27:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D77C6B0504; Wed, 13 Dec 2023 13:27:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0BBC66B0504 for ; Wed, 13 Dec 2023 13:27:50 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id C6F7E40269 for ; Wed, 13 Dec 2023 18:27:49 +0000 (UTC) X-FDA: 81562628658.10.4DA3F42 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf07.hostedemail.com (Postfix) with ESMTP id 19DDC4002B for ; Wed, 13 Dec 2023 18:27:47 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1702492068; 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:in-reply-to:references:references; bh=hjv8NkWfXoQ+7hZi7rLJ9o88UnzNicsuLM05tnOh3cI=; b=Y9JgvDtVDQStho//kJu6y6ne4Rn8jzH34DgF+bj3sRT8x3v7N5MXfnzLmmcZFbcuqoYQFe DTuRbUYXvH+k4N/rGmjvJmXuskuw6B62kul3AGCZgSL/yPAk3XcRgKcaLsd6KOT/8o/ALd +JS8v5nMJSeQxuE6GQxe3td/9BQYPmc= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=none; dmarc=none; spf=neutral (imf07.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=1702492068; a=rsa-sha256; cv=none; b=1ZvHBNBJVZ24MR95AAx8aYyu06gih0pMmq9jBDYZYrhDQ6Ls+EIR6m5x9Pf7fWHw3EIMys r8LAOKoEOtZ6PUzd1qR9L/nwTvxd47tzOnVkUPNemmpPaX/m+LaNIlF+ZALGBpC6la/ryv lijTGv8RuaJWmgfl0j41L8asHPHEOmo= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id AC6E210CFA728; Wed, 13 Dec 2023 10:27:33 -0800 (PST) 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 2/4] mm/ksm: add sysfs knobs for advisor Date: Wed, 13 Dec 2023 10:27:27 -0800 Message-Id: <20231213182729.587081-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231213182729.587081-1-shr@devkernel.io> References: <20231213182729.587081-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspamd-Queue-Id: 19DDC4002B X-R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6u raHsJUWrLgC0Hgxfrk3S2PsGrFUC2LhDK0dB2 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are: - advisor_mode: none: no advisor (default) scan-time: scan time advisor - advisor_max_cpu: 70 (default, cpu usage percent) - advisor_min_pages_to_scan: 500 (default) - advisor_max_pages_to_scan: 30000 (default) - advisor_target_scan_time: 200 (default in seconds) The new values will take effect on the next scan round. Signed-off-by: Stefan Roesch --- mm/ksm.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 4f7b71a1f3112..f7387a6d02050 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -302,10 +302,10 @@ unsigned long ksm_zero_pages; static unsigned long ksm_pages_skipped; /* Don't scan more than max pages per batch. */ -static unsigned long ksm_advisor_max_pages = 30000; +static unsigned long ksm_advisor_max_pages_to_scan = 30000; /* At least scan this many pages per batch. */ -static unsigned long ksm_advisor_min_pages = 500; +static unsigned long ksm_advisor_min_pages_to_scan = 500; /* Min CPU for scanning pages per scan */ static unsigned int ksm_advisor_min_cpu = 10; @@ -341,6 +341,16 @@ enum ksm_advisor_type { }; static enum ksm_advisor_type ksm_advisor; +static void set_advisor_defaults(void) +{ + if (ksm_advisor == KSM_ADVISOR_NONE) { + ksm_thread_pages_to_scan = DEFAULT_PAGES_TO_SCAN; + } else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) { + advisor_ctx = (const struct advisor_ctx){ 0 }; + ksm_thread_pages_to_scan = ksm_advisor_min_pages_to_scan; + } +} + static inline void advisor_start_scan(void) { if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) @@ -434,7 +444,7 @@ static void scan_time_advisor(void) pages = min(pages, per_page_cost * ksm_advisor_max_cpu); pages = max(pages, per_page_cost * ksm_advisor_min_cpu); - pages = min(pages, ksm_advisor_max_pages); + pages = min(pages, ksm_advisor_max_pages_to_scan); /* Update advisor context */ advisor_ctx.change = change; @@ -3722,6 +3732,127 @@ static ssize_t smart_scan_store(struct kobject *kobj, } KSM_ATTR(smart_scan); +static ssize_t advisor_mode_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + const char *output; + + if (ksm_advisor == KSM_ADVISOR_NONE) + output = "[none] scan-time"; + else if (ksm_advisor == KSM_ADVISOR_SCAN_TIME) + output = "none [scan-time]"; + + return sysfs_emit(buf, "%s\n", output); +} + +static ssize_t advisor_mode_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count) +{ + if (sysfs_streq("scan-time", buf)) + ksm_advisor = KSM_ADVISOR_SCAN_TIME; + else if (sysfs_streq("none", buf)) + ksm_advisor = KSM_ADVISOR_NONE; + else + return -EINVAL; + + /* Set advisor default values */ + set_advisor_defaults(); + + return count; +} +KSM_ATTR(advisor_mode); + +static ssize_t advisor_max_cpu_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor_max_cpu); +} + +static ssize_t advisor_max_cpu_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_cpu = value; + return count; +} +KSM_ATTR(advisor_max_cpu); + +static ssize_t advisor_min_pages_to_scan_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_min_pages_to_scan); +} + +static ssize_t advisor_min_pages_to_scan_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_min_pages_to_scan = value; + return count; +} +KSM_ATTR(advisor_min_pages_to_scan); + +static ssize_t advisor_max_pages_to_scan_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_max_pages_to_scan); +} + +static ssize_t advisor_max_pages_to_scan_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_pages_to_scan = value; + return count; +} +KSM_ATTR(advisor_max_pages_to_scan); + +static ssize_t advisor_target_scan_time_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_target_scan_time); +} + +static ssize_t advisor_target_scan_time_store(struct kobject *kobj, + struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int err; + unsigned long value; + + err = kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + if (value < 1) + return -EINVAL; + + ksm_advisor_target_scan_time = value; + return count; +} +KSM_ATTR(advisor_target_scan_time); + static struct attribute *ksm_attrs[] = { &sleep_millisecs_attr.attr, &pages_to_scan_attr.attr, @@ -3744,6 +3875,11 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_max_cpu_attr.attr, + &advisor_min_pages_to_scan_attr.attr, + &advisor_max_pages_to_scan_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, };