From patchwork Wed Nov 10 11:47:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: alexs@kernel.org X-Patchwork-Id: 12611953 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0DC20C433EF for ; Wed, 10 Nov 2021 11:48:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9A1B061075 for ; Wed, 10 Nov 2021 11:48:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 9A1B061075 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 1EED36B006C; Wed, 10 Nov 2021 06:48:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 19DF56B0071; Wed, 10 Nov 2021 06:48:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 08D7D6B0072; Wed, 10 Nov 2021 06:48:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0245.hostedemail.com [216.40.44.245]) by kanga.kvack.org (Postfix) with ESMTP id EE4076B006C for ; Wed, 10 Nov 2021 06:48:01 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A889C82E71EB for ; Wed, 10 Nov 2021 11:48:01 +0000 (UTC) X-FDA: 78792846762.12.17DFE28 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf14.hostedemail.com (Postfix) with ESMTP id 20B8E6001993 for ; Wed, 10 Nov 2021 11:48:01 +0000 (UTC) Received: by mail.kernel.org (Postfix) with ESMTPSA id 1453261075; Wed, 10 Nov 2021 11:47:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1636544859; bh=HAO6aFEMaWpdHaMdIfE22fc41HEPpX/j7kdI4dDL97c=; h=From:To:Cc:Subject:Date:From; b=AZ1A8gW0cBa1s97CL/kF6K5RgjQULkrjQ71GONhbxMs/wfrjY8QdcyYLRdEX3M5ue XYD2mOH5MwMGlvf5y1hVcU83MVih0nHLll7MvWkSbVm3eDH8ELqNY8ag4F1cL7UqOI 15xRTw5GJkTxj/et2niPO4eeNgSOu4V2LpJ9lTTpVnXopmByn1oBXsnvS/Up+rB8/m 4EfNirtpfuzmeo/ravwCZfoh89hZnVvMol4UhB9bRopOeF9qowSbQepEvydQmckznX kJ3PgQ8+FMzrXDnOBDmsohurVYJFADlmwDeWyOdKnFVIgD+i5zG1dJNQyUyUX4Sq+8 UJCAZ97yKcZeA== From: alexs@kernel.org To: Cc: Alex Shi , SeongJae Park , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH] mm/damon: remove damon_lock Date: Wed, 10 Nov 2021 19:47:21 +0800 Message-Id: <20211110114721.133808-1-alexs@kernel.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=AZ1A8gW0; spf=pass (imf14.hostedemail.com: domain of alexs@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=alexs@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 20B8E6001993 X-Stat-Signature: nu1pwp96hfyt5zwz9jdpyzzaden8x1hn X-HE-Tag: 1636544881-742004 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: From: Alex Shi Variable nr_running_ctxs guards by damon_lock, but a lock for a int variable seems a bit heavy, a atomic_t is enough. Signed-off-by: Alex Shi Cc: SeongJae Park Cc: Andrew Morton Cc: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org --- include/linux/damon.h | 1 - mm/damon/core.c | 31 +++++-------------------------- mm/damon/dbgfs.c | 8 +++++--- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index b4d4be3cc987..e5dcc6336ef2 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -453,7 +453,6 @@ int damon_set_attrs(struct damon_ctx *ctx, unsigned long sample_int, unsigned long min_nr_reg, unsigned long max_nr_reg); int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, ssize_t nr_schemes); -int damon_nr_running_ctxs(void); int damon_start(struct damon_ctx **ctxs, int nr_ctxs); int damon_stop(struct damon_ctx **ctxs, int nr_ctxs); diff --git a/mm/damon/core.c b/mm/damon/core.c index c381b3c525d0..e821e36d5c10 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -26,8 +26,7 @@ /* Get a random number in [l, r) */ #define damon_rand(l, r) (l + prandom_u32_max(r - l)) -static DEFINE_MUTEX(damon_lock); -static int nr_running_ctxs; +atomic_t nr_running_ctxs; /* * Construct a damon_region struct @@ -356,20 +355,6 @@ int damon_set_schemes(struct damon_ctx *ctx, struct damos **schemes, return 0; } -/** - * damon_nr_running_ctxs() - Return number of currently running contexts. - */ -int damon_nr_running_ctxs(void) -{ - int nr_ctxs; - - mutex_lock(&damon_lock); - nr_ctxs = nr_running_ctxs; - mutex_unlock(&damon_lock); - - return nr_ctxs; -} - /* Returns the size upper limit for each monitoring region */ static unsigned long damon_region_sz_limit(struct damon_ctx *ctx) { @@ -408,7 +393,7 @@ static int __damon_start(struct damon_ctx *ctx) if (!ctx->kdamond) { err = 0; ctx->kdamond = kthread_run(kdamond_fn, ctx, "kdamond.%d", - nr_running_ctxs); + atomic_read(&nr_running_ctxs)); if (IS_ERR(ctx->kdamond)) { err = PTR_ERR(ctx->kdamond); ctx->kdamond = NULL; @@ -437,19 +422,15 @@ int damon_start(struct damon_ctx **ctxs, int nr_ctxs) int i; int err = 0; - mutex_lock(&damon_lock); - if (nr_running_ctxs) { - mutex_unlock(&damon_lock); + if (atomic_read(&nr_running_ctxs)) return -EBUSY; - } for (i = 0; i < nr_ctxs; i++) { err = __damon_start(ctxs[i]); if (err) break; - nr_running_ctxs++; + atomic_inc(&nr_running_ctxs); } - mutex_unlock(&damon_lock); return err; } @@ -1078,9 +1059,7 @@ static int kdamond_fn(void *data) ctx->kdamond = NULL; mutex_unlock(&ctx->kdamond_lock); - mutex_lock(&damon_lock); - nr_running_ctxs--; - mutex_unlock(&damon_lock); + atomic_dec(&nr_running_ctxs); return 0; } diff --git a/mm/damon/dbgfs.c b/mm/damon/dbgfs.c index eccc14b34901..7182a46f6a2a 100644 --- a/mm/damon/dbgfs.c +++ b/mm/damon/dbgfs.c @@ -20,6 +20,8 @@ static int dbgfs_nr_ctxs; static struct dentry **dbgfs_dirs; static DEFINE_MUTEX(damon_dbgfs_lock); +extern atomic_t nr_running_ctxs; + /* * Returns non-empty string on success, negative error code otherwise. */ @@ -688,7 +690,7 @@ static int dbgfs_mk_context(char *name) struct dentry *root, **new_dirs, *new_dir; struct damon_ctx **new_ctxs, *new_ctx; - if (damon_nr_running_ctxs()) + if (atomic_read(&nr_running_ctxs)) return -EBUSY; new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) * @@ -772,7 +774,7 @@ static int dbgfs_rm_context(char *name) struct damon_ctx **new_ctxs; int i, j; - if (damon_nr_running_ctxs()) + if (atomic_read(&nr_running_ctxs)) return -EBUSY; root = dbgfs_dirs[0]; @@ -853,7 +855,7 @@ static ssize_t dbgfs_monitor_on_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { char monitor_on_buf[5]; - bool monitor_on = damon_nr_running_ctxs() != 0; + bool monitor_on = atomic_read(&nr_running_ctxs) != 0; int len; len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");