From patchwork Thu Nov 24 21:21:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 13055372 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 5E8CAC43219 for ; Thu, 24 Nov 2022 21:21:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C74778E0009; Thu, 24 Nov 2022 16:21:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BCF398E0006; Thu, 24 Nov 2022 16:21:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A48578E0009; Thu, 24 Nov 2022 16:21:27 -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 7688C8E0007 for ; Thu, 24 Nov 2022 16:21:27 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 579AE405E3 for ; Thu, 24 Nov 2022 21:21:27 +0000 (UTC) X-FDA: 80169607014.17.B2404BD Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by imf18.hostedemail.com (Postfix) with ESMTP id B5C4C1C000B for ; Thu, 24 Nov 2022 21:21:26 +0000 (UTC) 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 ams.source.kernel.org (Postfix) with ESMTPS id E72B3B82903; Thu, 24 Nov 2022 21:21:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4E820C43145; Thu, 24 Nov 2022 21:21:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1669324883; bh=P6PPKcChryRiegofLGkES15aSEw/aJpUh0oNfEpl4kg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=csE2P2pP80gwh9YjLKh9Ih3bnqn66ojFLUM6VT7+M/2+HoETHDkdMQvfgZ4/Gv7Cm GlyAmfjL2tClgHeSpoe4KSS4bfabmWQT2PfRA7FHssoVA2bn8kLknicRPVPM7TIjoV u+LUNjonDlYIYHphKh0ajOWEP3mOO1Tb4rp+tTArrAVvpTdn0OcpLyYBtu/x6gLIP1 NyBgK0wZkffaQxDltOfBkSd5de+FejYYAI8rX2AnthkOftYfxjEyB29v2P+CJcpzW6 OlLHkaL55NjZ2NxLHXoPdeNtmCcZLrQzzes7yUeE7l0kUXs26fGQdibS4XlRabDgbD 1kyLxx7xuDKXg== From: SeongJae Park To: Andrew Morton Cc: damon@lists.linux.dev, linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park Subject: [RFC PATCH 07/11] mm/damon/sysfs-schemes: connect filter directory and filters directory Date: Thu, 24 Nov 2022 21:21:10 +0000 Message-Id: <20221124212114.136863-8-sj@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221124212114.136863-1-sj@kernel.org> References: <20221124212114.136863-1-sj@kernel.org> MIME-Version: 1.0 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669324887; 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=L5uR5rYrk5WBOSUkHBxBiSbuM/EK1gm61GzegfZyReM=; b=toupNmPhf5qXpNm4ZVoZR5H9Iv6dVkhv9+tamEwM6SctRQ7DVvXQSVp3hmqmsOCIzE9Zpw f8e8xDofwO5N1Zs6ZyI2h6/J8EmUKkWI07GJuCVSANutXArbkKdn2P+stabI6376ReOWJT Tdycd1yBkhf3V8Ztpa3LovHYywUXTI0= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=csE2P2pP; spf=pass (imf18.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669324887; a=rsa-sha256; cv=none; b=2la9n0/8uICudgl1LgaaWuiHlw1ft4tNpn1kcISyz1/TEbChFMs25b652Fy8zPWaeiKrO/ RufKuWYHxU6LdwtFBEUN6t7HSVdSilKcd9cHlztXz0bH2F93lJSPNBv/OHHZgr0NN7u71w IdrM8i71duqNHxL/ne0B73qsi4gg0eg= X-Rspamd-Queue-Id: B5C4C1C000B Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=csE2P2pP; spf=pass (imf18.hostedemail.com: domain of sj@kernel.org designates 145.40.68.75 as permitted sender) smtp.mailfrom=sj@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 6wxsa5iz7c5urb3sqxqnwouppkssf7bx X-HE-Tag: 1669324886-652479 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: Implement 'nr_filters' file under 'filters' directory, which will be used to populate specific number of 'filter' directory under the directory, similar to other 'nr_*' files in DAMON sysfs interface. Signed-off-by: SeongJae Park --- mm/damon/sysfs-schemes.c | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/mm/damon/sysfs-schemes.c b/mm/damon/sysfs-schemes.c index afbfc55a8e84..7f2bab617156 100644 --- a/mm/damon/sysfs-schemes.c +++ b/mm/damon/sysfs-schemes.c @@ -269,6 +269,11 @@ struct damon_sysfs_scheme_filter { char *memcg_path; }; +static struct damon_sysfs_scheme_filter *damon_sysfs_scheme_filter_alloc(void) +{ + return kzalloc(sizeof(struct damon_sysfs_scheme_filter), GFP_KERNEL); +} + /* Should match with enum damos_filter_type */ static const char * const damon_sysfs_scheme_filter_type_strs[] = { "anon", @@ -392,6 +397,7 @@ static struct kobj_type damon_sysfs_scheme_filter_ktype = { struct damon_sysfs_scheme_filters { struct kobject kobj; + struct damon_sysfs_scheme_filter **filters_arr; int nr; }; @@ -401,6 +407,57 @@ damon_sysfs_scheme_filters_alloc(void) return kzalloc(sizeof(struct damon_sysfs_scheme_filters), GFP_KERNEL); } +static void damon_sysfs_scheme_filters_rm_dirs( + struct damon_sysfs_scheme_filters *filters) +{ + struct damon_sysfs_scheme_filter **filters_arr = filters->filters_arr; + int i; + + for (i = 0; i < filters->nr; i++) + kobject_put(&filters_arr[i]->kobj); + filters->nr = 0; + kfree(filters_arr); + filters->filters_arr = NULL; +} + +static int damon_sysfs_scheme_filters_add_dirs( + struct damon_sysfs_scheme_filters *filters, int nr_filters) +{ + struct damon_sysfs_scheme_filter **filters_arr, *filter; + int err, i; + + damon_sysfs_scheme_filters_rm_dirs(filters); + if (!nr_filters) + return 0; + + filters_arr = kmalloc_array(nr_filters, sizeof(*filters_arr), + GFP_KERNEL | __GFP_NOWARN); + if (!filters_arr) + return -ENOMEM; + filters->filters_arr = filters_arr; + + for (i = 0; i < nr_filters; i++) { + filter = damon_sysfs_scheme_filter_alloc(); + if (!filter) { + damon_sysfs_scheme_filters_rm_dirs(filters); + return -ENOMEM; + } + + err = kobject_init_and_add(&filter->kobj, + &damon_sysfs_scheme_filter_ktype, + &filters->kobj, "%d", i); + if (err) { + kobject_put(&filter->kobj); + damon_sysfs_scheme_filters_rm_dirs(filters); + return err; + } + + filters_arr[i] = filter; + filters->nr++; + } + return 0; +} + static ssize_t nr_filters_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -413,6 +470,7 @@ static ssize_t nr_filters_show(struct kobject *kobj, static ssize_t nr_filters_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { + struct damon_sysfs_scheme_filters *filters; int nr, err = kstrtoint(buf, 0, &nr); if (err) @@ -420,6 +478,15 @@ static ssize_t nr_filters_store(struct kobject *kobj, if (nr < 0) return -EINVAL; + filters = container_of(kobj, struct damon_sysfs_scheme_filters, kobj); + + if (!mutex_trylock(&damon_sysfs_lock)) + return -EBUSY; + err = damon_sysfs_scheme_filters_add_dirs(filters, nr); + mutex_unlock(&damon_sysfs_lock); + if (err) + return err; + return count; }