Skip to content

Commit 7ee161f

Browse files
sjp38akpm00
authored andcommitted
mm/damon/sysfs-schemes: implement filter directory
Implement DAMOS filter directory which will be located under the filters directory. The directory provides three files, namely type, matching, and memcg_path. 'type' and 'matching' will be directly connected to the fields of 'struct damos_filter' having same name. 'memcg_path' will receive the path of the memory cgroup of the interest and later converted to memcg id when it's committed. Link: https://lkml.kernel.org/r/20221205230830.144349-7-sj@kernel.org Signed-off-by: SeongJae Park <sj@kernel.org> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent ac35264 commit 7ee161f

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

mm/damon/sysfs-schemes.c

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,134 @@ static struct kobj_type damon_sysfs_stats_ktype = {
258258
.default_groups = damon_sysfs_stats_groups,
259259
};
260260

261+
/*
262+
* filter directory
263+
*/
264+
265+
struct damon_sysfs_scheme_filter {
266+
struct kobject kobj;
267+
enum damos_filter_type type;
268+
bool matching;
269+
char *memcg_path;
270+
};
271+
272+
/* Should match with enum damos_filter_type */
273+
static const char * const damon_sysfs_scheme_filter_type_strs[] = {
274+
"anon",
275+
"memcg",
276+
};
277+
278+
static ssize_t type_show(struct kobject *kobj,
279+
struct kobj_attribute *attr, char *buf)
280+
{
281+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
282+
struct damon_sysfs_scheme_filter, kobj);
283+
284+
return sysfs_emit(buf, "%s\n",
285+
damon_sysfs_scheme_filter_type_strs[filter->type]);
286+
}
287+
288+
static ssize_t type_store(struct kobject *kobj,
289+
struct kobj_attribute *attr, const char *buf, size_t count)
290+
{
291+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
292+
struct damon_sysfs_scheme_filter, kobj);
293+
enum damos_filter_type type;
294+
ssize_t ret = -EINVAL;
295+
296+
for (type = 0; type < NR_DAMOS_FILTER_TYPES; type++) {
297+
if (sysfs_streq(buf, damon_sysfs_scheme_filter_type_strs[
298+
type])) {
299+
filter->type = type;
300+
ret = count;
301+
break;
302+
}
303+
}
304+
return ret;
305+
}
306+
307+
static ssize_t matching_show(struct kobject *kobj,
308+
struct kobj_attribute *attr, char *buf)
309+
{
310+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
311+
struct damon_sysfs_scheme_filter, kobj);
312+
313+
return sysfs_emit(buf, "%c\n", filter->matching ? 'Y' : 'N');
314+
}
315+
316+
static ssize_t matching_store(struct kobject *kobj,
317+
struct kobj_attribute *attr, const char *buf, size_t count)
318+
{
319+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
320+
struct damon_sysfs_scheme_filter, kobj);
321+
bool matching;
322+
int err = kstrtobool(buf, &matching);
323+
324+
if (err)
325+
return err;
326+
327+
filter->matching = matching;
328+
return count;
329+
}
330+
331+
static ssize_t memcg_path_show(struct kobject *kobj,
332+
struct kobj_attribute *attr, char *buf)
333+
{
334+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
335+
struct damon_sysfs_scheme_filter, kobj);
336+
337+
return sysfs_emit(buf, "%s\n",
338+
filter->memcg_path ? filter->memcg_path : "");
339+
}
340+
341+
static ssize_t memcg_path_store(struct kobject *kobj,
342+
struct kobj_attribute *attr, const char *buf, size_t count)
343+
{
344+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
345+
struct damon_sysfs_scheme_filter, kobj);
346+
char *path = kmalloc(sizeof(*path) * (count + 1), GFP_KERNEL);
347+
348+
if (!path)
349+
return -ENOMEM;
350+
351+
strncpy(path, buf, count);
352+
path[count] = '\0';
353+
filter->memcg_path = path;
354+
return count;
355+
}
356+
357+
static void damon_sysfs_scheme_filter_release(struct kobject *kobj)
358+
{
359+
struct damon_sysfs_scheme_filter *filter = container_of(kobj,
360+
struct damon_sysfs_scheme_filter, kobj);
361+
362+
kfree(filter->memcg_path);
363+
kfree(filter);
364+
}
365+
366+
static struct kobj_attribute damon_sysfs_scheme_filter_type_attr =
367+
__ATTR_RW_MODE(type, 0600);
368+
369+
static struct kobj_attribute damon_sysfs_scheme_filter_matching_attr =
370+
__ATTR_RW_MODE(matching, 0600);
371+
372+
static struct kobj_attribute damon_sysfs_scheme_filter_memcg_path_attr =
373+
__ATTR_RW_MODE(memcg_path, 0600);
374+
375+
static struct attribute *damon_sysfs_scheme_filter_attrs[] = {
376+
&damon_sysfs_scheme_filter_type_attr.attr,
377+
&damon_sysfs_scheme_filter_matching_attr.attr,
378+
&damon_sysfs_scheme_filter_memcg_path_attr.attr,
379+
NULL,
380+
};
381+
ATTRIBUTE_GROUPS(damon_sysfs_scheme_filter);
382+
383+
static struct kobj_type damon_sysfs_scheme_filter_ktype = {
384+
.release = damon_sysfs_scheme_filter_release,
385+
.sysfs_ops = &kobj_sysfs_ops,
386+
.default_groups = damon_sysfs_scheme_filter_groups,
387+
};
388+
261389
/*
262390
* filters directory
263391
*/

0 commit comments

Comments
 (0)