Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6627103rwb; Mon, 5 Dec 2022 15:24:40 -0800 (PST) X-Google-Smtp-Source: AA0mqf54pZItSXwhtgSZE6rYW4cdMAY07AcHZ3vjouc2oIs3XeMYV7adYeipR20cOv7cudTYi6CK X-Received: by 2002:a17:906:1182:b0:78d:99f2:a94e with SMTP id n2-20020a170906118200b0078d99f2a94emr68827367eja.232.1670282680512; Mon, 05 Dec 2022 15:24:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670282680; cv=none; d=google.com; s=arc-20160816; b=mk1SrmGTzz/rVNlz6Mr1z4IT1Xpwk6HnBAnCNxElDD3YRI0AmPgmAwufoiCitmk6vu COp1FkfFzUVgi0QmHqeINd6K9qdSqWy93CSMSB47fYDSZ3SnLTxVQ2s6+HMEuNuQS1lJ IMcYNbSNS9R48lTmaNVxt7mCyCOAcuMCeVVmT7YWpt/BudjcwTzzZI99OIfirfgAxM6N MVBXXLwg64nauaGASGZiaJjU1F9yV/8/P69/HTPtxWeRhzlwgQGA+kmVZvrpGTMZy4it iv/9IK75Ikgs9XYlNeBZ0xwmcIiB1jvOJ9kPVeUnPk5wTysWtA4MgIfFPEgBP/GkuYa5 U23g== 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 :dkim-signature; bh=6SIrU+qHgmD/alHJaDLXicupWPCEB92hw5xZgg3ikzQ=; b=MdA+cRTjNTZzSlprEiLzO2LfCvJWcPlC70IFfQjOxVTbvJi9IZBwNCluqCncHueMA3 KkILhLehJ5bt2jj9eraEsYfwgxPDLcMekSBExVJBhY5X4TFFVK3nrv+9v57vhoNZf66G WU/gopPECdvutdTUZBdWYQjVz7Ne8+y4caV4fRYq9cznGEVCMOiSsaC3NuyE8hFWHo2n 9xOZ3goeSvQJsjcOIHFQqKi3OTAIjYCfnfxEmF0lBTNBtHO4sY5YQmp64yY6DLdo5m+x Oho8B5xhYuhznxabiyrnMxx+QGEgir6dt//6IEj8x2IuAMC3l294uAG451VoQ+JdShuL /Hqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YUkqaVKS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o24-20020a17090608d800b007aae0b30283si10587576eje.691.2022.12.05.15.24.17; Mon, 05 Dec 2022 15:24:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=YUkqaVKS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233593AbiLEXJM (ORCPT + 80 others); Mon, 5 Dec 2022 18:09:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33062 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232384AbiLEXIp (ORCPT ); Mon, 5 Dec 2022 18:08:45 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D97671EED4 for ; Mon, 5 Dec 2022 15:08:40 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5D335614C5 for ; Mon, 5 Dec 2022 23:08:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5F026C433D6; Mon, 5 Dec 2022 23:08:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1670281719; bh=KP45fkenUtVNkuekA5wpVSKSKg+M620wUBu7fatA2gQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YUkqaVKSH3C/skAJJU1XZFSGjNvz+JOJJWRnZ5XtjszTIzwOpUh4pjUCZTrrOwXAT bFSmtLVDGZER/Az8C7PjezIHAT4JlOqfi9TAGN/0mcz3WDm8cmNVc4yBn4zNi3RAZ/ DmhCB1GIVkVEXGMHS/kH6CRjtTSn+1CatzHn8g/uKFjDRGEKMf9YqjFTpp2PT/qbDb WlXPLxuGxYUXKKs59TePia0AkcOTAyJ2ZzOQSTjszfS0uAvBYQvkBM9m3rcuYZRrBc AmkFiri3/8jsmiAzeSUpNskE/fX3fHK0/Ia3Uk2ZWJvdSMTsUclzavi9uMo8iK8GqP L/IIAH/E655uQ== From: SeongJae Park To: Andrew Morton Cc: damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park Subject: [PATCH 06/11] mm/damon/sysfs-schemes: implement filter directory Date: Mon, 5 Dec 2022 23:08:25 +0000 Message-Id: <20221205230830.144349-7-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221205230830.144349-1-sj@kernel.org> References: <20221205230830.144349-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: SeongJae Park --- mm/damon/sysfs-schemes.c | 128 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index 50c8148cb474..afbfc55a8e84 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -258,6 +258,134 @@ static struct kobj_type damon_sysfs_stats_ktype = { .default_groups = damon_sysfs_stats_groups, }; +/* + * filter directory + */ + +struct damon_sysfs_scheme_filter { + struct kobject kobj; + enum damos_filter_type type; + bool matching; + char *memcg_path; +}; + +/* Should match with enum damos_filter_type */ +static const char * const damon_sysfs_scheme_filter_type_strs[] = { + "anon", + "memcg", +}; + +static ssize_t type_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + + return sysfs_emit(buf, "%s\n", + damon_sysfs_scheme_filter_type_strs[filter->type]); +} + +static ssize_t type_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + enum damos_filter_type type; + ssize_t ret = -EINVAL; + + for (type = 0; type < NR_DAMOS_FILTER_TYPES; type++) { + if (sysfs_streq(buf, damon_sysfs_scheme_filter_type_strs[ + type])) { + filter->type = type; + ret = count; + break; + } + } + return ret; +} + +static ssize_t matching_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + + return sysfs_emit(buf, "%c\n", filter->matching ? 'Y' : 'N'); +} + +static ssize_t matching_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + bool matching; + int err = kstrtobool(buf, &matching); + + if (err) + return err; + + filter->matching = matching; + return count; +} + +static ssize_t memcg_path_show(struct kobject *kobj, + struct kobj_attribute *attr, char *buf) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + + return sysfs_emit(buf, "%s\n", + filter->memcg_path ? filter->memcg_path : ""); +} + +static ssize_t memcg_path_store(struct kobject *kobj, + struct kobj_attribute *attr, const char *buf, size_t count) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + char *path = kmalloc(sizeof(*path) * (count + 1), GFP_KERNEL); + + if (!path) + return -ENOMEM; + + strncpy(path, buf, count); + path[count] = '\0'; + filter->memcg_path = path; + return count; +} + +static void damon_sysfs_scheme_filter_release(struct kobject *kobj) +{ + struct damon_sysfs_scheme_filter *filter = container_of(kobj, + struct damon_sysfs_scheme_filter, kobj); + + kfree(filter->memcg_path); + kfree(filter); +} + +static struct kobj_attribute damon_sysfs_scheme_filter_type_attr = + __ATTR_RW_MODE(type, 0600); + +static struct kobj_attribute damon_sysfs_scheme_filter_matching_attr = + __ATTR_RW_MODE(matching, 0600); + +static struct kobj_attribute damon_sysfs_scheme_filter_memcg_path_attr = + __ATTR_RW_MODE(memcg_path, 0600); + +static struct attribute *damon_sysfs_scheme_filter_attrs[] = { + &damon_sysfs_scheme_filter_type_attr.attr, + &damon_sysfs_scheme_filter_matching_attr.attr, + &damon_sysfs_scheme_filter_memcg_path_attr.attr, + NULL, +}; +ATTRIBUTE_GROUPS(damon_sysfs_scheme_filter); + +static struct kobj_type damon_sysfs_scheme_filter_ktype = { + .release = damon_sysfs_scheme_filter_release, + .sysfs_ops = &kobj_sysfs_ops, + .default_groups = damon_sysfs_scheme_filter_groups, +}; + /* * filters directory */ -- 2.25.1