Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp2819690rdb; Wed, 4 Oct 2023 12:23:46 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6cGtbLo0JjTeIZyHC6FNoWae0p6AhWBJiheP9u0nHckn3icqdkE9uUwVEXPjdCjC3Rq3T X-Received: by 2002:a05:6a20:7f9a:b0:163:a041:336c with SMTP id d26-20020a056a207f9a00b00163a041336cmr3849234pzj.48.1696447426226; Wed, 04 Oct 2023 12:23:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696447426; cv=none; d=google.com; s=arc-20160816; b=OZ/tSDy8Hxt38Skp/X73uu9ieTlh4P4OMCsp2oh36cqEwY8SUBppO35WO44ddgXT3J zS7Kwb96r7vZeGPKel54e/eDRKr/NeV7+FIb7s+wazNM+XMdPh93qW2HjYQigFdIM11L TxdvUyVrQc3GvTvSDPmdq7cZWIR8xngEcQvL134K4jDzwMW9Ygc7vSaRbdOEz7zNsJmG Y50/DyDy+gJESOPj1NyzicTTG3IXbkM0SgQ8dKOR5NVMHPHK77X44Be3ndK2l22b7sCP mI33+V1+PqW8e5hma15lfZfKv/xX5YNCQNRMRoHC0VDYh9WdzPMUqKbZDqqlcCMp5zL5 w96A== 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=OwQWDABFLnnr40odHJRGCCaIkSZd/6hq/j8i4+KYZoE=; fh=sS+J4OyOC0EcVLWJpS3mBHGeO+0+dYZJ+ImCUfzsrH4=; b=hrPFf3NOV9fZfZxKVGG510b1ueqv7Au32M8dmW5cUYlNH4rMGs6kx+Ch/ub0ofX0Ff 6mlouZP4rKqyZiias9uTnRUPulyvIBqrV7JB2F6Tb4MewkQSyiFs8PrfoWZNj2thMBfp UxvMXOeOOJnpudAR5I1Dp4MDoymRMDyF/H1XTox4A3zN9u/O/Y6Z0QAAu0dKjMmAtaBy ebeEoaB69zWyPaL/5FxK8OrESMCsaUcHFyQDdjVwwy0mKt22ApTto+wkfKs3KzzjyB26 acvP+CxDRjZpY5mUk05qO5qs1BSkATELBIvzkCDt0qDAGPdAY9tSvBnYMrzwabhTr8/z YNiQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id z11-20020a170903018b00b001bc553d16e0si4337551plg.507.2023.10.04.12.23.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Oct 2023 12:23:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 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 agentk.vger.email (Postfix) with ESMTP id 2D97781C7098; Wed, 4 Oct 2023 12:23:35 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245395AbjJDTHm (ORCPT + 99 others); Wed, 4 Oct 2023 15:07:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343888AbjJDTHY (ORCPT ); Wed, 4 Oct 2023 15:07:24 -0400 Received: from 66-220-144-178.mail-mxout.facebook.com (66-220-144-178.mail-mxout.facebook.com [66.220.144.178]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F370430ED for ; Wed, 4 Oct 2023 12:03:15 -0700 (PDT) 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 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=2.8 required=5.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.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 (agentk.vger.email [0.0.0.0]); Wed, 04 Oct 2023 12:23:35 -0700 (PDT) X-Spam-Level: ** 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 =3D 0; } =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) + ksm_thread_pages_to_scan =3D 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 *k= obj, } KSM_ATTR(smart_scan); =20 +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 =3D kstrtouint(buf, 10, &mode); + if (err) + return -EINVAL; + if (mode > KSM_ADVISOR_LAST) + return -EINVAL; + + /* Set advisor default values */ + ksm_advisor =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_min_pages =3D 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 =3D kstrtoul(buf, 10, &value); + if (err) + return -EINVAL; + + ksm_advisor_max_pages =3D 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 =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, @@ -3714,6 +3818,10 @@ static struct attribute *ksm_attrs[] =3D { &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, }; =20 --=20 2.39.3