From patchwork Thu Feb 17 16:19:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: SeongJae Park X-Patchwork-Id: 12750414 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 DEF04C433F5 for ; Thu, 17 Feb 2022 16:19:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 50B546B007D; Thu, 17 Feb 2022 11:19:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 442166B007B; Thu, 17 Feb 2022 11:19:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 112856B007D; Thu, 17 Feb 2022 11:19:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id ED2436B007B for ; Thu, 17 Feb 2022 11:19:49 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B3F5092762 for ; Thu, 17 Feb 2022 16:19:49 +0000 (UTC) X-FDA: 79152782898.22.D1DA55B Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf19.hostedemail.com (Postfix) with ESMTP id 499001A0008 for ; Thu, 17 Feb 2022 16:19:49 +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 dfw.source.kernel.org (Postfix) with ESMTPS id 783C261210; Thu, 17 Feb 2022 16:19:48 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id B20C3C340F3; Thu, 17 Feb 2022 16:19:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1645114788; bh=86/d0xi9LNikqugSFOyNSfFep2UlHDk2g/2oy5k4oEQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eEk1KP0P1Ru7nw3eTQhYjXWvpF2DTvf7W1pMSZs/zqUJGjp0e/VnEskIvolzcBV4T tNqpoWqrdv97UM7fZR5E4nbWaBA/Dums9OYKbqHzsjsRUIm0u7+plWVOFSK/8kyjm2 xikQexBGTgbH1868dxj3O5B958yvTEW1YhBlPyeqNLSPP+02SvrteDRL9Nxf2fFVDl q1v+mWARrsMnqJxqWHsVydvX5RvPIJ6/ChqQmRhjMFXSd5La+y9aQk5O/ApcTH0ufG CN+cMWTM0aWjP+mDh0CohnqWCO9HxlsYK/QJ2NXIU4p49FBuNtHYq3mmGZ4HrS90nt CvdTI/uiVFp5Q== From: SeongJae Park To: akpm@linux-foundation.org Cc: rientjes@google.com, xhao@linux.alibaba.com, linux-damon@amazon.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, SeongJae Park Subject: [RFC PATCH 3/4] mm/damon/sysfs: Link DAMON to 'state' file read/write functions Date: Thu, 17 Feb 2022 16:19:37 +0000 Message-Id: <20220217161938.8874-4-sj@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220217161938.8874-1-sj@kernel.org> References: <20220217161938.8874-1-sj@kernel.org> Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eEk1KP0P; spf=pass (imf19.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 X-Rspamd-Server: rspam07 X-Rspam-User: X-Rspamd-Queue-Id: 499001A0008 X-Stat-Signature: qryueeo3q1ampjq6wojt6hqrx68zjn7m X-HE-Tag: 1645114789-660688 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: This commit implements 'state' DAMON sysfs interface file read/write functions. In detail, writing 'on' or 'off' to the 'state' file turns DAMON for the context on or off, accordingly. Reading the file shows the state. Signed-off-by: SeongJae Park --- mm/damon/sysfs.c | 166 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 162 insertions(+), 4 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 59bdc7268dc6..721ee086265f 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -793,18 +793,176 @@ static void damon_sysfs_kdamond_rm_dirs(struct damon_sysfs_kdamond *kdamond) kobject_put(&kdamond->contexts->kobj); } +static bool damon_sysfs_ctx_running(struct damon_ctx *ctx) +{ + bool running; + + mutex_lock(&ctx->kdamond_lock); + running = ctx->kdamond != NULL; + mutex_unlock(&ctx->kdamond_lock); + return running; +} + static ssize_t damon_sysfs_kdamond_state_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { - /* TODO: Link with DAMON */ - return sysfs_emit(buf, "off\n"); + struct damon_sysfs_kdamond *kdamond = container_of(kobj, + struct damon_sysfs_kdamond, kobj); + struct damon_ctx *ctx = kdamond->damon_ctx; + bool running; + + if (!ctx) + running = false; + else + running = damon_sysfs_ctx_running(ctx); + + return sysfs_emit(buf, "%s\n", running ? "on" : "off"); +} + +static void damon_sysfs_destroy_targets(struct damon_ctx *ctx) +{ + struct damon_target *t, *next; + + damon_for_each_target_safe(t, next, ctx) { + if (ctx->ops.id == DAMON_OPS_VADDR) + put_pid(t->pid); + damon_destroy_target(t); + } +} + +static int damon_sysfs_set_targets(struct damon_ctx *ctx, + struct damon_sysfs_context *sysfs_ctx) +{ + struct damon_sysfs_targets *targets = sysfs_ctx->targets; + int i; + + for (i = 0; i < targets->nr_targets; i++) { + struct damon_target *t; + + t = damon_new_target(); + if (!t) { + damon_sysfs_destroy_targets(ctx); + return -ENOMEM; + } + if (ctx->ops.id == DAMON_OPS_VADDR) { + t->pid = find_get_pid(targets->targets_arr[i]->pid); + if (!t->pid) { + damon_sysfs_destroy_targets(ctx); + return -EINVAL; + } + } + damon_add_target(ctx, t); + } + return 0; +} + +static inline bool target_has_pid(const struct damon_ctx *ctx) +{ + return ctx->ops.id == DAMON_OPS_VADDR; +} + +static void damon_sysfs_before_terminate(struct damon_ctx *ctx) +{ + struct damon_target *t, *next; + + if (!target_has_pid(ctx)) + return; + + mutex_lock(&ctx->kdamond_lock); + damon_for_each_target_safe(t, next, ctx) { + put_pid(t->pid); + damon_destroy_target(t); + } + mutex_unlock(&ctx->kdamond_lock); +} + +static struct damon_ctx *damon_sysfs_build_ctx( + struct damon_sysfs_context *sys_ctx) +{ + struct damon_ctx *ctx = damon_new_ctx(); + struct damon_sysfs_attrs *sys_attrs = sys_ctx->attrs; + struct damon_sysfs_ul_range *sys_nr_regions = sys_attrs->nr_regions; + struct damon_sysfs_intervals *sys_intervals = sys_attrs->intervals; + int err; + + if (!ctx) + return ERR_PTR(-ENOMEM); + + err = damon_select_ops(ctx, sys_ctx->ops_id); + if (err) + goto out; + + err = damon_set_attrs(ctx, sys_intervals->sample_us, + sys_intervals->aggr_us, sys_intervals->update_us, + sys_nr_regions->min, sys_nr_regions->max); + if (err) + goto out; + err = damon_sysfs_set_targets(ctx, sys_ctx); + if (err) + goto out; + ctx->callback.before_terminate = damon_sysfs_before_terminate; + return ctx; + +out: + damon_destroy_ctx(ctx); + return ERR_PTR(err); } static ssize_t damon_sysfs_kdamond_state_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { - /* TODO: Link with DAMON */ - return count; + struct damon_sysfs_kdamond *kdamond = container_of(kobj, + struct damon_sysfs_kdamond, kobj); + struct damon_ctx *ctx; + ssize_t ret; + + if (!mutex_trylock(&damon_sysfs_lock)) + return -EBUSY; + if (!strncmp(buf, "on\n", count)) { + if (kdamond->damon_ctx && + damon_sysfs_ctx_running(kdamond->damon_ctx)) { + ret = -EBUSY; + goto out; + } + if (kdamond->contexts->nr != 1) { + ret = -EINVAL; + goto out; + } + + if (kdamond->damon_ctx) + damon_destroy_ctx(kdamond->damon_ctx); + kdamond->damon_ctx = NULL; + + ctx = damon_sysfs_build_ctx( + kdamond->contexts->contexts_arr[0]); + if (IS_ERR(ctx)) { + ret = PTR_ERR(ctx); + goto out; + } + ret = damon_start(&ctx, 1, false); + if (ret) { + damon_destroy_ctx(ctx); + goto out; + } + kdamond->damon_ctx = ctx; + } else if (!strncmp(buf, "off\n", count)) { + if (!kdamond->damon_ctx) { + ret = -EINVAL; + goto out; + } + ret = damon_stop(&kdamond->damon_ctx, 1); + /* + * kdamond->damon_ctx will be freed in next on, or + * kdamonds_nr_store() + */ + } else { + ret = -EINVAL; + } +out: + mutex_unlock(&damon_sysfs_lock); + if (!ret) + ret = count; + return ret; } static ssize_t damon_sysfs_kdamond_pid_show(struct kobject *kobj,