From patchwork Sun Nov 12 19:46:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13453382 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 EA82FC4332F for ; Sun, 12 Nov 2023 19:46:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 34D988D0002; Sun, 12 Nov 2023 14:46:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 265468D001D; Sun, 12 Nov 2023 14:46:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F330D8D0002; Sun, 12 Nov 2023 14:46:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id BA6218D0002 for ; Sun, 12 Nov 2023 14:46:18 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 7ED06806D7 for ; Sun, 12 Nov 2023 19:46:18 +0000 (UTC) X-FDA: 81450333636.13.0411145 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id D04341C0007 for ; Sun, 12 Nov 2023 19:46:16 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MIh9ucCT; spf=pass (imf21.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699818376; 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:dkim-signature; bh=245TcY1/4hwVjJLNcDhHKPMaQGy/jXqRs0r2aKC+E6g=; b=f8LrESllY+iE48a8eWW8RmeZxhYpjzu2Sl/W67UT64ypzPYEuFUQTFRPYnPxijrmfu00w0 Evw8wxPH4qm1YrNKHn/MMWOgTRvLTO9yt2K6TOPwcreHxQp7Wv0cjUK6l+ZTPazaDOQqFx b5DCxVJhpUu3Chgfmj9MJhX67h7efh8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699818376; a=rsa-sha256; cv=none; b=QB2d34fzlbniLqlBYwzhn0cI8ymTzkza5R5VpjcUksmddLsn20ndHnMbkI/CPfuOcjlJzJ GH+ieyYQReC0aIHzU5us0FxwVdfCjr8T7D6IgtwAKeynESJsjoPnZPXr1btssfVpBytXvs vrSmpYbndiY7BDj/Cn81T4p6Nqc2cT4= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MIh9ucCT; spf=pass (imf21.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 8C22760C68; Sun, 12 Nov 2023 19:46:14 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EA98DC433CA; Sun, 12 Nov 2023 19:46:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1699818374; bh=8h4xcSrlngKXz6YrUu+DyY/DYWtQeX+QuoziQCKM6Qo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MIh9ucCTepKRwsUBhaeQTqSbnm6Ezok1Z01it4NK1/7eOF+AvLvl6S3L6KK2DYzVH n7S/Ax4akpIniSkGmPWk5EotaJGJRKGD0wb4v2jqu4rBew/mk+/s3xUP1hbXrajNhl hKnWmdQ6ytt9PQA2R8X7/YeiL6Cf4w+xiRQDnt4+ZChxDQ1slQLhoHdNxXMPoOzQRH u/ooQB77RM6qCtHj/YNpr02MYV3BB+ChlSlFYUWRBUy/x0RPqP5okkEfKNS9VNVd6X 8GqIg/MdrDblrjoPI2J08jhGzzseTnJqWhcufBWbXAqcpC7XrxmfPVHY/JRftmpWmP Rowj0FpL6fkQA== From: SeongJae Park To: Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/8] mm/damon/sysfs-schemes: implement scheme quota goals directory Date: Sun, 12 Nov 2023 19:46:01 +0000 Message-Id: <20231112194607.61399-3-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231112194607.61399-1-sj@kernel.org> References: <20231112194607.61399-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: D04341C0007 X-Rspam-User: X-Stat-Signature: wy7ubu4ga3uh56bfs7orfhzeiyjffeot X-Rspamd-Server: rspam03 X-HE-Tag: 1699818376-305681 X-HE-Meta: U2FsdGVkX1/PdRJkUptuIXLLKymXfwpQBa8UmR9V6p3aUmYvgMrt9Svw+EIfjChKD+baQWyuV30qMXnM6z5LJRKfCRmEVApcZkjv1NBBtutcwPzQaUcf1Cw6tE6f0avB38nDGtGXE6JMwPiHPWeOrF8JD71F60zZkIlOoYErGt9jx8XfCyCv11w56muDQzLwUVGuW6ilq4uZGSZkWQsU3bP3+ldsRXV7tzCQuOOg3GA/p51JXzb2BxJ+jxwwzdMeZZYFDkAlaxEtRZxJ4edztQ1ywaVkMfQykcXFSN0yxemS27SwjkW9fE5k9g75GKMjJwmG3zM0TZ7/r50llonh2sgGwkkEaWyopK52Ic6lCgvn/t4VnCOjehOrGIO5OCFXpczKtvWORpCvW4NfCf6zAUXuhZI4rzWOBOVAdqkXdbSFyOAaWAYeqf/e5M0Iq5unH/zrnIukMZ+kDU71TfpDSKGFOpSbvvG+Bw4bx24wdbAWBeaERg7CISf+gJmRZtCesfsK2xqn0rKYZmZgWFl5y0GXNA47N+aZbwnfVvx10R2qRBwAHNu+4h6uC/htkszm9Io9Qls3kzy1YxmyTvDxNzbLxT9RuWXytiY512AqLf0mnS79FOopdlt4HvAaa5h/gQKiAyuZ/4b3BelDgCVuA4nN3z5TJq6/YZx0CD33Z1RQSKGYckmLrv7XxA6tRHjaWKukbNj5mlUM3HEy4/nqFQ2QejCIP4GInmluxSCMOrbMlQqjU2BQUQ/mAtJgeq/tEOXYNTlRysBer5VgA+ulIHFIZHZFWC8Zwvza9a1oIjIPdmLbW513ujo+o3P0tKuiJm/1wV9YkNH1P7B7LC2XpPmt27vErIkmLYSZfE4wCjeCilbOWe7Tgmj0iUj4UKZiGaI3BInfGt3agquTB0SVTPy7K5NCDOvfVlNrtDp138VnZUAFHOCiHS3+n6Q6DI3KX39Jj894HZ9MVCEaqK7 Rl7vvsh8 71d8NYnyQXRvUf/c7lS5dp712VtabRIYOcvWVacqFc1XZZHHsk5manV2Yp0+nZg9V0+eA1UF7VmdM0QrfJKlzB9YkxTNBy9WDu/+RV+Y/y9CVRXVBwP7W5m4raK7+bGi8wN+g8OLpcgISRtFkPcZrb7EYXNMiGnfQ99AqkKiXJA7BIwydAhXcjtkkSWTsmObaFfiRRL0TnqQaJEQBjizVHisKfgTsT+/Wu7qqhnyDxdUqlko= 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: Implement DAMON sysfs directories and files for the goals of DAMOS quota. It allows users set multiple goals for their aim, with target values. It further allows users to enter the current score value as a feedback for DAMOS. Following commit will connect the user inputs to DAMOS quota auto-tuning. Signed-off-by: SeongJae Park --- mm/damon/sysfs-schemes.c | 224 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 221 insertions(+), 3 deletions(-) diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index be667236b8e6..d3b57348f07b 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -812,6 +812,203 @@ static const struct kobj_type damon_sysfs_watermarks_ktype = { .default_groups = damon_sysfs_watermarks_groups, }; +/* + * quota goal directory + */ + +struct damos_sysfs_quota_goal { + struct kobject kobj; + unsigned long target_value; + unsigned long current_value; +}; + +static struct damos_sysfs_quota_goal *damos_sysfs_quota_goal_alloc(void) +{ + return kzalloc(sizeof(struct damos_sysfs_quota_goal), GFP_KERNEL); +} + +static ssize_t target_value_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damos_sysfs_quota_goal *goal = container_of(kobj, struct + damos_sysfs_quota_goal, kobj); + + return sysfs_emit(buf, "%lu\n", goal->target_value); +} + +static ssize_t target_value_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damos_sysfs_quota_goal *goal = container_of(kobj, struct + damos_sysfs_quota_goal, kobj); + int err = kstrtoul(buf, 0, &goal->target_value); + + return err ? err : count; +} + +static ssize_t current_value_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damos_sysfs_quota_goal *goal = container_of(kobj, struct + damos_sysfs_quota_goal, kobj); + + return sysfs_emit(buf, "%lu\n", goal->current_value); +} + +static ssize_t current_value_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damos_sysfs_quota_goal *goal = container_of(kobj, struct + damos_sysfs_quota_goal, kobj); + int err = kstrtoul(buf, 0, &goal->current_value); + + /* feed callback should check existence of this file and read value */ + return err ? err : count; +} + +static void damos_sysfs_quota_goal_release(struct kobject *kobj) +{ + /* or, notify this release to the feed callback */ + kfree(container_of(kobj, struct damos_sysfs_quota_goal, kobj)); +} + +static struct kobj_attribute damos_sysfs_quota_goal_target_value_attr = + __ATTR_RW_MODE(target_value, 0600); + +static struct kobj_attribute damos_sysfs_quota_goal_current_value_attr = + __ATTR_RW_MODE(current_value, 0600); + +static struct attribute *damos_sysfs_quota_goal_attrs[] = { + &damos_sysfs_quota_goal_target_value_attr.attr, + &damos_sysfs_quota_goal_current_value_attr.attr, + NULL, +}; +ATTRIBUTE_GROUPS(damos_sysfs_quota_goal); + +static const struct kobj_type damos_sysfs_quota_goal_ktype = { + .release = damos_sysfs_quota_goal_release, + .sysfs_ops = &kobj_sysfs_ops, + .default_groups = damos_sysfs_quota_goal_groups, +}; + +/* + * quota goals directory + */ + +struct damos_sysfs_quota_goals { + struct kobject kobj; + struct damos_sysfs_quota_goal **goals_arr; /* counted by nr */ + int nr; +}; + +static struct damos_sysfs_quota_goals *damos_sysfs_quota_goals_alloc(void) +{ + return kzalloc(sizeof(struct damos_sysfs_quota_goals), GFP_KERNEL); +} + +static void damos_sysfs_quota_goals_rm_dirs( + struct damos_sysfs_quota_goals *goals) +{ + struct damos_sysfs_quota_goal **goals_arr = goals->goals_arr; + int i; + + for (i = 0; i < goals->nr; i++) + kobject_put(&goals_arr[i]->kobj); + goals->nr = 0; + kfree(goals_arr); + goals->goals_arr = NULL; +} + +static int damos_sysfs_quota_goals_add_dirs( + struct damos_sysfs_quota_goals *goals, int nr_goals) +{ + struct damos_sysfs_quota_goal **goals_arr, *goal; + int err, i; + + damos_sysfs_quota_goals_rm_dirs(goals); + if (!nr_goals) + return 0; + + goals_arr = kmalloc_array(nr_goals, sizeof(*goals_arr), + GFP_KERNEL | __GFP_NOWARN); + if (!goals_arr) + return -ENOMEM; + goals->goals_arr = goals_arr; + + for (i = 0; i < nr_goals; i++) { + goal = damos_sysfs_quota_goal_alloc(); + if (!goal) { + damos_sysfs_quota_goals_rm_dirs(goals); + return -ENOMEM; + } + + err = kobject_init_and_add(&goal->kobj, + &damos_sysfs_quota_goal_ktype, &goals->kobj, + "%d", i); + if (err) { + kobject_put(&goal->kobj); + damos_sysfs_quota_goals_rm_dirs(goals); + return err; + } + + goals_arr[i] = goal; + goals->nr++; + } + return 0; +} + +static ssize_t nr_goals_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damos_sysfs_quota_goals *goals = container_of(kobj, + struct damos_sysfs_quota_goals, kobj); + + return sysfs_emit(buf, "%d\n", goals->nr); +} + +static ssize_t nr_goals_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damos_sysfs_quota_goals *goals; + int nr, err = kstrtoint(buf, 0, &nr); + + if (err) + return err; + if (nr < 0) + return -EINVAL; + + goals = container_of(kobj, struct damos_sysfs_quota_goals, kobj); + + if (!mutex_trylock(&damon_sysfs_lock)) + return -EBUSY; + err = damos_sysfs_quota_goals_add_dirs(goals, nr); + mutex_unlock(&damon_sysfs_lock); + if (err) + return err; + + return count; +} + +static void damos_sysfs_quota_goals_release(struct kobject *kobj) +{ + kfree(container_of(kobj, struct damos_sysfs_quota_goals, kobj)); +} + +static struct kobj_attribute damos_sysfs_quota_goals_nr_attr = + __ATTR_RW_MODE(nr_goals, 0600); + +static struct attribute *damos_sysfs_quota_goals_attrs[] = { + &damos_sysfs_quota_goals_nr_attr.attr, + NULL, +}; +ATTRIBUTE_GROUPS(damos_sysfs_quota_goals); + +static const struct kobj_type damos_sysfs_quota_goals_ktype = { + .release = damos_sysfs_quota_goals_release, + .sysfs_ops = &kobj_sysfs_ops, + .default_groups = damos_sysfs_quota_goals_groups, +}; + /* * scheme/weights directory */ @@ -930,6 +1127,7 @@ static const struct kobj_type damon_sysfs_weights_ktype = { struct damon_sysfs_quotas { struct kobject kobj; struct damon_sysfs_weights *weights; + struct damos_sysfs_quota_goals *goals; unsigned long ms; unsigned long sz; unsigned long reset_interval_ms; @@ -943,6 +1141,7 @@ static struct damon_sysfs_quotas *damon_sysfs_quotas_alloc(void) static int damon_sysfs_quotas_add_dirs(struct damon_sysfs_quotas *quotas) { struct damon_sysfs_weights *weights; + struct damos_sysfs_quota_goals *goals; int err; weights = damon_sysfs_weights_alloc(0, 0, 0); @@ -951,16 +1150,35 @@ static int damon_sysfs_quotas_add_dirs(struct damon_sysfs_quotas *quotas) err = kobject_init_and_add(&weights->kobj, &damon_sysfs_weights_ktype, "as->kobj, "weights"); - if (err) + if (err) { kobject_put(&weights->kobj); - else - quotas->weights = weights; + return err; + } + quotas->weights = weights; + + goals = damos_sysfs_quota_goals_alloc(); + if (!goals) { + kobject_put(&weights->kobj); + return -ENOMEM; + } + err = kobject_init_and_add(&goals->kobj, + &damos_sysfs_quota_goals_ktype, "as->kobj, + "goals"); + if (err) { + kobject_put(&weights->kobj); + kobject_put(&goals->kobj); + } else { + quotas->goals = goals; + } + return err; } static void damon_sysfs_quotas_rm_dirs(struct damon_sysfs_quotas *quotas) { kobject_put("as->weights->kobj); + damos_sysfs_quota_goals_rm_dirs(quotas->goals); + kobject_put("as->goals->kobj); } static ssize_t ms_show(struct kobject *kobj, struct kobj_attribute *attr,