Received: by 2002:a05:7412:8d10:b0:f3:1519:9f41 with SMTP id bj16csp5505514rdb; Wed, 13 Dec 2023 10:28:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IH6otJ/aLE32Iu4/w/z5jMNlVsiwLXPGJfDmfphyboe0Gs9jF+c+GSRVhigDjpUb5rYH2NJ X-Received: by 2002:a17:903:120e:b0:1d3:2a94:cb33 with SMTP id l14-20020a170903120e00b001d32a94cb33mr2683032plh.8.1702492095365; Wed, 13 Dec 2023 10:28:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702492095; cv=none; d=google.com; s=arc-20160816; b=saMtrHn/FCrZOyjdlGPP6GkBrF3r6RnUUSfGr4kkffSZyV7gwGEmHUP2tjy47+k9Cd D1KYQKYlDBQGc5Thj2TYReM4iRj79+hyhcgGWO+8943ubdcVPc5VVjFiC3RQ9jU0G30w goYSWN3PeJiRJvSQkP7ke3PgmzZ1HSsFnJ3zb0BYEGZD2z0teYqkVwoUJObwnB5DsZ0n Auki0RLzsVoUKXJiKUPPB4350+QpAy8x/or8PjPWxLVvgh+OniMrsoEFiK24LvV1jJEm Kn3DhPpTkOmkg6NgGvSZDMV3TvkS+fE5SEUn5k58hNguH2ErnUzDgOvwvQ1Jrsl7bm3Y hvZA== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=hjv8NkWfXoQ+7hZi7rLJ9o88UnzNicsuLM05tnOh3cI=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=HtPS7H8cn6ksJBPRQ9WSa9uTPsuLP3iKJbTH4SAmww0nlyMVbPybKbhAi8D4QV9yEn dThlzWlkvEUVTOrMV0SvMtaxW/NG0hBNXroWP+q9lymrVqR8WsiDlnD7gV2KRMcAQu78 BfQqIxfDtxy5KRVQ8fBSnDdjL5nMEGuNstuqfXD3O7jypytEdpJN8nGUPigH5y2UTG/S d2TQ7iKfns2XjYiRDDosx8SOuHSSU6ob9/U59A8VhzaDz6m45jqUzEfRf3TjQ9yNGe7L wXpaI7x6zLit6dojMigczIXHVicRBYf4/Szkd1ZIccA1aUE92Mp9sJYbo1w91mVm9Gvg 05+g== 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 Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id q16-20020a17090311d000b001d0750f2942si10111815plh.253.2023.12.13.10.28.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Dec 2023 10:28:15 -0800 (PST) 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 C794C826EB7D; Wed, 13 Dec 2023 10:28:11 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442530AbjLMS1u (ORCPT + 99 others); Wed, 13 Dec 2023 13:27:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233764AbjLMS1k (ORCPT ); Wed, 13 Dec 2023 13:27:40 -0500 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 6DBD710C for ; Wed, 13 Dec 2023 10:27:46 -0800 (PST) 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 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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, 13 Dec 2023 10:28:12 -0800 (PST) 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; =20 /* Don't scan more than max pages per batch. */ -static unsigned long ksm_advisor_max_pages =3D 30000; +static unsigned long ksm_advisor_max_pages_to_scan =3D 30000; =20 /* At least scan this many pages per batch. */ -static unsigned long ksm_advisor_min_pages =3D 500; +static unsigned long ksm_advisor_min_pages_to_scan =3D 500; =20 /* Min CPU for scanning pages per scan */ static unsigned int ksm_advisor_min_cpu =3D 10; @@ -341,6 +341,16 @@ enum ksm_advisor_type { }; static enum ksm_advisor_type ksm_advisor; =20 +static void set_advisor_defaults(void) +{ + if (ksm_advisor =3D=3D KSM_ADVISOR_NONE) { + ksm_thread_pages_to_scan =3D DEFAULT_PAGES_TO_SCAN; + } else if (ksm_advisor =3D=3D KSM_ADVISOR_SCAN_TIME) { + advisor_ctx =3D (const struct advisor_ctx){ 0 }; + ksm_thread_pages_to_scan =3D ksm_advisor_min_pages_to_scan; + } +} + static inline void advisor_start_scan(void) { if (ksm_advisor =3D=3D KSM_ADVISOR_SCAN_TIME) @@ -434,7 +444,7 @@ static void scan_time_advisor(void) =20 pages =3D min(pages, per_page_cost * ksm_advisor_max_cpu); pages =3D max(pages, per_page_cost * ksm_advisor_min_cpu); - pages =3D min(pages, ksm_advisor_max_pages); + pages =3D min(pages, ksm_advisor_max_pages_to_scan); =20 /* Update advisor context */ advisor_ctx.change =3D change; @@ -3722,6 +3732,127 @@ static ssize_t smart_scan_store(struct kobject *k= obj, } KSM_ATTR(smart_scan); =20 +static ssize_t advisor_mode_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + const char *output; + + if (ksm_advisor =3D=3D KSM_ADVISOR_NONE) + output =3D "[none] scan-time"; + else if (ksm_advisor =3D=3D KSM_ADVISOR_SCAN_TIME) + output =3D "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 =3D KSM_ADVISOR_SCAN_TIME; + else if (sysfs_streq("none", buf)) + ksm_advisor =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_cpu =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_min_pages_to_scan =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_pages_to_scan =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + if (value < 1) + return -EINVAL; + + ksm_advisor_target_scan_time =3D value; + return count; +} +KSM_ATTR(advisor_target_scan_time); + static struct attribute *ksm_attrs[] =3D { &sleep_millisecs_attr.attr, &pages_to_scan_attr.attr, @@ -3744,6 +3875,11 @@ static struct attribute *ksm_attrs[] =3D { &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, }; =20 --=20 2.39.3