From mboxrd@z Thu Jan 1 00:00:00 1970 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 178ACC83F09 for ; Sun, 6 Jul 2025 20:00:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4AAC36B03F9; Sun, 6 Jul 2025 16:00:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 405126B03FA; Sun, 6 Jul 2025 16:00:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20AAE6B03FB; Sun, 6 Jul 2025 16:00:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 100DF6B03F9 for ; Sun, 6 Jul 2025 16:00:30 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id BA2B0127CAF for ; Sun, 6 Jul 2025 20:00:29 +0000 (UTC) X-FDA: 83634906978.21.3A602D4 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf16.hostedemail.com (Postfix) with ESMTP id 326B918000D for ; Sun, 6 Jul 2025 20:00:28 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lVFVqjHu; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1751832028; a=rsa-sha256; cv=none; b=R3pkU68pKzCu9cIPYzatGYRPd1aV4VmMFPjiAsruxRb6ok1pcj6Bm374dBkPAl7/+/rk8u jufskvy0FKa0VcC7nYKQrgEmNApDWlZxcn5y8g6CmvEd3STfCYM/F8QuydWfhMlIebZPtP dH2Vhn21D8PIgdp42GFkVU4XUr59RIQ= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=lVFVqjHu; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of sj@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=sj@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1751832028; 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=NLJw7y5ewPrS6DeoUewgviABRG3pGCpD9/Dj5b3oysY=; b=zIzGROM8+REJLt0fo25GaDq2Yk23Te5jeiyiTXdwCA1OxpESdn7NXURhDRaY2+B9AF/WBp h/LdgkabqVKjTejpUjQTy/3SQOdXKIEqV4zgGW9UMUR2dD/g0IrN1cdzsN9EvqmmQilioC s4SXX9/iKzaXXgW6bJPHzEHYPf50r8Y= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 5BF94A53E08; Sun, 6 Jul 2025 20:00:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0AA7DC4CEF3; Sun, 6 Jul 2025 20:00:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1751832027; bh=5isUX/qdRWz6RuSCLUkT6PmSqq6/sjQX47PJKxk/1f4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lVFVqjHu6ZkO7yJPWtOZqMA6G9AjWhKGP0HBTOzvLejArhKM0PiTXlmvWo82MLve9 CbF7gJxWPPUB87pIZ+b2UU77a7LzhLY1eSKGrO9D+jjxliS630HbmFlbbbv/t8XVki feq7AUbvIxI7icwwZXFPFFNq8YICq8Zrvm6KBM5ZTmCDyKRw8LV+3omANvexbWHrMi p19qWsrp8EOE25923yMHww+e6dMStquIt4jAIGSX1NR7OLP1LuGI2lnJZKIrqH/5sq 8k7+fWo5ZXMYyODZvAfyt4gzf2rawoB9JYmPG7uluGBw6jykwwJ0Zdb3qXj1WS9IDY 12OmR6OqAzc+w== From: SeongJae Park To: Cc: SeongJae Park , Andrew Morton , damon@lists.linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [RFC PATCH 02/14] mm/damon/core: introduce repeat mode damon_call() Date: Sun, 6 Jul 2025 13:00:06 -0700 Message-Id: <20250706200018.42704-3-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250706200018.42704-1-sj@kernel.org> References: <20250706200018.42704-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 326B918000D X-Stat-Signature: 58nuawy7izcqguh6cyk6pn9duda4our1 X-Rspam-User: X-HE-Tag: 1751832028-765110 X-HE-Meta: U2FsdGVkX1+Rv3e3fojEqMtdRSVxogVjLHTFwut+LeSkCCU7FMvL2Ko/o8GWSdA5O8edK2BFqxSGg1yTg37zYA7L2pQUQV+lcBGQSFWEzHgvI2R9YWgbFvQt5K2VCMxwC0S61OimYI1hg0CZLr4dGscvXu66CQPXx3ZVzCbR+4dS2+V9jQjbqfnE+AwErVQHWKq9pn+9xrAwMyZYkCbRnuvQ4PbACnKY0HtKUKOU6dJr3NhrQJNXOrPvkMQtF5eEgI8G3wH0oVxTdtqHyxPnVs7Ai0RWefoHDLTiT9XpevVU8I7vePwNE23uwarC/bC9N+gA0z8uh8ErKzq0aQw0Kfr1bdmWEeR8LawBY9bpohnji/3wtrnqhN3ErlaYoyQuauEuQjzQEbvzuhJ5QiBwvqstWsihP6qcESjMTniCbYrvBJXxQvqB/7BRpm9fnDWd++5OIWzTQY0GRuDoBa8ZxHYkPHMF7Rt5e378J9Vt6NNhpr1YUAG7oFjkaWYZeCLgC8UgEHkVlCoHx1pY+EMihiGZEOUcygISknLjE1zvO0rYqOe/u48SxaYZwqt37UnJHFLy/moKjX+zOfxubTt7w47coXnjhrt462a6NZjtg6WOVFSmVXjIQX7Z90dCnxhaAYw3aWFSXfjqhU89fj1NaY6q+5xAveqCWk/PVPDc8DoVmpS5IeYy0S9iUbPPb1he2PxLf2HxScXUPI1hqqFzyXP6EPCzEVUJWqcQQuH5pO2aA8BxQ/sDg9SyEJpIO7x3OkoHVAYNG9+SIhLB/UAbOI3jUpG0qYP81ZpY2o3WRpKFxGkirVZxx/HQB0aAFWtQG5saGRaQ7MkT15ldkQO+7aMdINQ0snnaWhVAavVsbvBU+RKeuGrJXusXi/1PiR5w6njDfOhJVkzGGStOIzkzweZ5RnMLm/b5zBQ+J6sT0Tqm6G3DLMaKCPkAPgnubd6/d2ihXqqsQi0zrOLi7Yb 5v0S+Crr 1zR9mv9WJANDBMjTauL/fQXE7ZjKQ29+n2d9lMjMYLCMN2i0vMuo05awML23e3dMsyJ72OVoc/+mv1ivfQQZF1dcUXGI3hDv5LeG/h2SNwlbWVDZKb3y/MWZAmaWrOEz/ldVsCk3p82Tnzi3Ikt5Ea/BITGZ8N5q2C7Ew9VEAMfV69quLo8qEZ/8ruQy+WWB08NkIJXcSUCZv0aobHbl8jU8YVF2QnLJV+5s+aVYSHG3xFWRPoCHKACqGTRaWhGmsXbD5glDyMx2FVQVmNTGTEJKlJA== 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: List-Subscribe: List-Unsubscribe: damon_call() can be useful for reading or writing DAMON internal data for one time. A common pattern of DAMON core usage from DAMON modules is doing such reads and writes repeatedly, for example, to periodically update the DAMOS stats. To do that with damon_call(), callers should call damon_call() repeatedly, with their own delay loop. Each caller doing that is repetitive. Introduce a repeat mode damon_call(). Callers can use the mode by setting a new field in damon_call_control. If the mode is turned on, damon_call() returns success immediately, and DAMON repeats invoking the callback function inside the kdamond main loop. Signed-off-by: SeongJae Park --- include/linux/damon.h | 2 ++ mm/damon/core.c | 23 +++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 9176a1c7d5a8..2d51b575d660 100644 --- a/include/linux/damon.h +++ b/include/linux/damon.h @@ -659,6 +659,7 @@ struct damon_callback { * * @fn: Function to be called back. * @data: Data that will be passed to @fn. + * @repeat: Repeat invocations. * @return_code: Return code from @fn invocation. * * Control damon_call(), which requests specific kdamond to invoke a given @@ -667,6 +668,7 @@ struct damon_callback { struct damon_call_control { int (*fn)(void *data); void *data; + bool repeat; int return_code; /* private: internal use only */ /* informs if the kdamond finished handling of the request */ diff --git a/mm/damon/core.c b/mm/damon/core.c index 01e62bdcdd67..7a4dc76dd023 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1339,8 +1339,9 @@ bool damon_is_running(struct damon_ctx *ctx) * * Ask DAMON worker thread (kdamond) of @ctx to call a function with an * argument data that respectively passed via &damon_call_control->fn and - * &damon_call_control->data of @control, and wait until the kdamond finishes - * handling of the request. + * &damon_call_control->data of @control. If &damon_call_control->repeat of + * @control is set, further wait until the kdamond finishes handling of the + * request. Otherwise, return as soon as the request is made. * * The kdamond executes the function with the argument in the main loop, just * after a sampling of the iteration is finished. The function can hence @@ -1352,7 +1353,8 @@ bool damon_is_running(struct damon_ctx *ctx) */ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control) { - init_completion(&control->completion); + if (!control->repeat) + init_completion(&control->completion); control->canceled = false; INIT_LIST_HEAD(&control->list); @@ -1361,6 +1363,8 @@ int damon_call(struct damon_ctx *ctx, struct damon_call_control *control) mutex_unlock(&ctx->call_controls_lock); if (!damon_is_running(ctx)) return -EINVAL; + if (control->repeat) + return 0; wait_for_completion(&control->completion); if (control->canceled) return -ECANCELED; @@ -2389,6 +2393,7 @@ static void kdamond_usleep(unsigned long usecs) static void kdamond_call(struct damon_ctx *ctx, bool cancel) { struct damon_call_control *control; + LIST_HEAD(repeat_controls); int ret = 0; while (true) { @@ -2407,8 +2412,18 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel) mutex_lock(&ctx->call_controls_lock); list_del(&control->list); mutex_unlock(&ctx->call_controls_lock); - complete(&control->completion); + if (!control->repeat) + complete(&control->completion); + else + list_add(&control->list, &repeat_controls); } + control = list_first_entry_or_null(&repeat_controls, + struct damon_call_control, list); + if (!control || cancel) + return; + mutex_lock(&ctx->call_controls_lock); + list_add_tail(&control->list, &ctx->call_controls); + mutex_unlock(&ctx->call_controls_lock); } /* Returns negative error code if it's not activated but should return */ -- 2.39.5