From patchwork Wed Oct 4 19:02:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Roesch X-Patchwork-Id: 13409341 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 3FBBFE7C4EC for ; Wed, 4 Oct 2023 19:03:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA76E8D00AF; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C55888D0002; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B017F8D00AF; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9B54F8D0002 for ; Wed, 4 Oct 2023 15:03:19 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 6110880410 for ; Wed, 4 Oct 2023 19:03:19 +0000 (UTC) X-FDA: 81308702118.27.118F792 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by imf13.hostedemail.com (Postfix) with ESMTP id C2E4320032 for ; Wed, 4 Oct 2023 19:03:17 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=none; spf=neutral (imf13.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=1696446197; a=rsa-sha256; cv=none; b=Hewhc1NcHWIJA2UbypmQPojsLJD+JNDuxU1nPds7Q7sjx4SzLleRpSuWQeMdG0LvbjHV9s ZlF0DjE9h0C+TT751hk49cUuqv5wFuL2Brrb1Kl/ieW8q6N6AhnTnMIc2H0oWrhGG36lE4 VyJteBBwlyqdL8ZAwZTXTMFdR5wbbfU= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=none; spf=neutral (imf13.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=1696446197; 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=OwQWDABFLnnr40odHJRGCCaIkSZd/6hq/j8i4+KYZoE=; b=CO2QqR2z307BcqfJphDtrYlEW3zOgxvPbhQ1DPXZDaeS2OCjgAZYppf5vh/luC6QrActBq 4ggZU1HqKY4H2G+yQQgNvZX52kUVNRbWXHi1D8+GW4/5NPUkAWaEqvngC//HV1VMpOHsV4 O6i9AWA8MFPJZoeETOb2yGn/mTCPU14= Received: by devbig1114.prn1.facebook.com (Postfix, from userid 425415) id B20EFD08BFFD; Wed, 4 Oct 2023 12:03:02 -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 2/4] mm/ksm: add sysfs knobs for advisor Date: Wed, 4 Oct 2023 12:02:47 -0700 Message-Id: <20231004190249.829015-3-shr@devkernel.io> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20231004190249.829015-1-shr@devkernel.io> References: <20231004190249.829015-1-shr@devkernel.io> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-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ysV3v84BFw5KAQxiJahGuDD 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: This adds four new knobs for the KSM advisor to influence its behaviour. The knobs are: - advisor_mode: 0: no advisor (default) 1: scan time advisor - advisor_min_pages: 500 (default) - advisor_max_pages: 5000 (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 | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/mm/ksm.c b/mm/ksm.c index c9edfb293024..12e70f806b2b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -341,6 +341,14 @@ static void init_advisor(void) advisor_ctx.change = 0; } +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) + ksm_thread_pages_to_scan = ksm_advisor_min_pages; +} + /* * Use previous scan time if available, otherwise use current scan time as an * approximation for the previous scan time. @@ -3692,6 +3700,102 @@ 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) +{ + return sysfs_emit(buf, "%u\n", ksm_advisor); +} + +static ssize_t advisor_mode_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, + size_t count) +{ + unsigned int mode; + int err; + + err = kstrtouint(buf, 10, &mode); + if (err) + return -EINVAL; + if (mode > KSM_ADVISOR_LAST) + return -EINVAL; + + /* Set advisor default values */ + ksm_advisor = mode; + init_advisor(); + set_advisor_defaults(); + + return count; +} +KSM_ATTR(advisor_mode); + +static ssize_t advisor_min_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_min_pages); +} + +static ssize_t advisor_min_pages_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 = value; + return count; +} +KSM_ATTR(advisor_min_pages); + +static ssize_t advisor_max_pages_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + return sysfs_emit(buf, "%lu\n", ksm_advisor_max_pages); +} + +static ssize_t advisor_max_pages_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 = value; + return count; +} +KSM_ATTR(advisor_max_pages); + +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, @@ -3714,6 +3818,10 @@ static struct attribute *ksm_attrs[] = { &use_zero_pages_attr.attr, &general_profit_attr.attr, &smart_scan_attr.attr, + &advisor_mode_attr.attr, + &advisor_min_pages_attr.attr, + &advisor_max_pages_attr.attr, + &advisor_target_scan_time_attr.attr, NULL, };