From patchwork Thu Nov 30 02:36:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13473975 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 9F9D1C10DAA for ; Thu, 30 Nov 2023 02:37:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E2F546B0396; Wed, 29 Nov 2023 21:37:03 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D1B1C8D0001; Wed, 29 Nov 2023 21:37:03 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BBAF26B039E; Wed, 29 Nov 2023 21:37:03 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id AFD6B6B0396 for ; Wed, 29 Nov 2023 21:37:03 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7DDE3A0822 for ; Thu, 30 Nov 2023 02:37:03 +0000 (UTC) X-FDA: 81513058326.01.FEC0568 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf02.hostedemail.com (Postfix) with ESMTP id D399E8000D for ; Thu, 30 Nov 2023 02:37:01 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lRgRzxND; spf=pass (imf02.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=1701311821; 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=VCtYpXCf0WeiwSqGgN3z88f6mgIHVeVdM3LPnGeS/IA=; b=hzhpocMiff8ijf7vuBmfr7rBiMkFCmS94s+1C03qE7rLMcOAA5u8jipQQwHuulDVOKsYcw UJr4m35QkhGAdh8iERJ8vTJz1QpF/002sa5cTT4+qUixqaeQSWXZcQM/uLy0AHEzGNNQAs 6PhEvWNCumTr6QT34vHzSGyKEID7z6U= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1701311821; a=rsa-sha256; cv=none; b=MX6NexUjQDXwe9H3AIKyRW+gcNFRZ0ZjnHNw0n5hiTyLO8NvSZXDXo5MPBfWONOPKoZuyI eUUDN5sFmDpxyy+lvQFDyCdvtBKxYDY05Md6m5od0zwbpB87qNqnfVeOavicS/6Xmnl3lM NZTbjkKWx7YjbUwU7pZsb2qpLaI6818= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lRgRzxND; spf=pass (imf02.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 0821661917; Thu, 30 Nov 2023 02:37:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5BD35C433CD; Thu, 30 Nov 2023 02:37:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1701311820; bh=Zui/g7KxP982XBB/gWdJuIQ1teaLDQkwAJtBsHfxVTg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lRgRzxNDNbm7lr2q7Y53CCXTaCb86uIcOob1lUwGtAF+twP1nzW6iYAepQ1c4Onj9 IS893QvCkslgxKA29cJnnoDFseUyk6dq8zTOFttcFjff9ootD9pK5R6Yl4pNUluDTK T2EpO72ELMrqrEgXJHAuaN5peRey2z3VXHOOhl6LNi0XqzV9RcQ5SHjxcopPexngIB KTHq1eQ9BuXqK7CqmbuDzYmPW97jbwMhc0hoQiHkEVwL5OxIaHKBsaKv8vb2qQh+yx xIoLY5MyFIkGdP8MEgLBpHIfYlTV8Py37fUUhPnX0ahYWFLEgVYsKmxTjn13jga9Td IfYb2F89T94gA== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/9] mm/damon/sysfs-schemes: implement files for scheme quota goals setup Date: Thu, 30 Nov 2023 02:36:45 +0000 Message-Id: <20231130023652.50284-3-sj@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231130023652.50284-1-sj@kernel.org> References: <20231130023652.50284-1-sj@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: D399E8000D X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 5ekom3x5qt1w455jr5fexhz84fiz9gxn X-HE-Tag: 1701311821-656175 X-HE-Meta: U2FsdGVkX18K/AkXb/Jcu3BsCk+vlVyXx3lEDqWkTf3XsQ0yAO7ibaUN8uZq/YGK8hP93ZBgyPyS/WieGMpbBfzutBChJ/OiJWmH0MNu3a9m8ON/uSTVUXFbC8oc2+O5M2pPNZqJE9S7pKEaY5cJ0NC3mRs90Z/Cq0GClbACH9kmHj4rGv7Qy1AGd8zWfw3vYJmllnmlpFz7zqMUEUULE2reYmsW72zMFm1gSZiMJMhp5WMowHTKSz4GqPhSyaXeR3V710G5Og4jkb+Jw7obP19dUY32UoXbuvKTYM7rug2RjyTv5+ucYkrdbZhzNWEbHvCb3vyotdReJaCX5ADirW7URYIc6puy6cCDhejCenRWvn2rxoaqGL5gt3i8RyEZMgUdWxbSIDyZB24ccoz4uJv2xm4PogHDWxKIWzuDKY9kp8hP8rVb6IHzTFAT35dah/evuNWRR2Nj+01he12rEPJpaulOUxrBbMihG9x841obw9VXs2aQoqyE5pDwyeKKKNGu8ec1zAX5ykuC5HWjpDLXNrQA3BczX8jgmPBw8b8od9IAt0UHTvujv0x9Xw12xZdKierNQYZdVi/I2KaqSMb0MgEuHBtGNXhkPRju1Ws8h8VZ9ZFQ48wF1JVg9pC27Z37k8EWBjOkFXOB503lIR5xTLWg09NMVnMkw35IW7Izb7JLKmTVlJr1CLTtKd7fd0vvCxEy++7uEbqY+OoI67xpn2mkvtnO1WGlfD04PM9Oj0mvJq6JraMT+1CwGCKDKFx6dyVtQb9yhXFeo3eeUmrFXghOmIS7ZzmCt4wBVnrQLtANohEecwh4yCFsBowEfeEkhVje6zyV+dWNLdM0yDS3MxxkTg+ykgNNjC+bpNBkWFzQtHfU/k+LlaRDBdgLJuwp82RHddBYj3Z2j2ZvOni872ZTYqOc/tigAeGIQq6XIrLrMbE+b3s5Rk8+Ii58sc+P7iQD6vKEt6n5ldE rmHZMKyT hDS8Cywg4EpEwqvlesTnILWfqLAmPAbUA6pGaSta6NqgfkhHC2GPiJv+SNnLAzyTULZYioVG58JpG6M9dBQAiHNK6jlaBdXltm42pR6P4LJNMplUpp4rEnecZ3rrp+81B3PcfkIbsDKn1YDf2H2HQEa3SMOJgXZxMt9jigpwpUsD2Xq+LK+XT8OALCcAk7yYmquaZnqR88jKj3o9ZddTRGJ2SR8xNs055TD48IBZVU/l57H0= 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. Those allow users set multiple goals for their aim, with target values. Users can further enter the current score value for each goal as feedback for DAMOS. Note that this commit is implementing only the basic file operations, and not connecting the files with the DAMOS core logic. Hence writing something to the files makes no real effect. The following commit will connect the file operations and the core logic. 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 fe0fe2562000..e5531dbd4cf1 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -820,6 +820,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 */ @@ -938,6 +1135,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; @@ -951,6 +1149,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); @@ -959,16 +1158,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,