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 0DDA7C83F1D for ; Sat, 12 Jul 2025 19:50:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 93E356B00AE; Sat, 12 Jul 2025 15:50:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8EE086B00B0; Sat, 12 Jul 2025 15:50:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DE226B00B2; Sat, 12 Jul 2025 15:50:25 -0400 (EDT) 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 661046B00AE for ; Sat, 12 Jul 2025 15:50:25 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 206901D8BAF for ; Sat, 12 Jul 2025 19:50:25 +0000 (UTC) X-FDA: 83656654410.15.B6F72DE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf13.hostedemail.com (Postfix) with ESMTP id 6BC3020005 for ; Sat, 12 Jul 2025 19:50:23 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=evOAGIJS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 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=1752349823; 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=lOdGZ9uXBs1xvEWxM8kpo6Wv0RMrNZNAYSJG5FxD7wc=; b=RoXBAZD5VRLDtxtDSVsdWdssK0bExZXUVMqM2mg0uGFYcxhplofok0xQDcDPhpKA13fiNT rzbN+mIZyB+/GNAMVy9rmAB9G2it9HR+pXgAkBk7JC+3W76GIqDZJxTrcIcm9+RCA6JHDu t9q/oK39ic169Xt+PJnSI+P4YuKL1Dc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752349823; a=rsa-sha256; cv=none; b=6TOLWgHVA5Lsll19oGyNmTgg57jyNMysMlMhDGQO2g02461LZR/+GnCqtI6UyMsAcRR95c 7QMOXV3T5yL6MJkFUN2VJ8GqZS8SS3qEs40ciE5Wm3bafb0QbJrklHf8xttZcUOktjFN/e rUzbUXC4zx9B7s56ihtyPzLAsUkOM8M= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=evOAGIJS; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf13.hostedemail.com: domain of sj@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=sj@kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id 9D9C85C48DB; Sat, 12 Jul 2025 19:50:22 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 46295C4CEEF; Sat, 12 Jul 2025 19:50:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752349822; bh=OtU+NRyCIh0C5uwTmklBNfReGv9IlgGR+JKEfON4Vh4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=evOAGIJSbKOfOxDLbqgOJsH3hGip86AqldafX8BSdrC5/Knp7Y1ygDTUGjExVRe8G ZAB3SrqhBChhKnYkASVe6TFRB06jtG1JpvNu4nB8FXLl77hFOqV96oKGZno+fSsC5H zCLa7V0y4yWVNT21+/kVlYbhOnAJmxaPfolF/XAwk6LPMyESEoVLsKEHstH72pAh8C hlsakP6Df8On06CAh9p8hZ7W67q0hK+TaBH+7mVLoKH3xdn0j0K/Afm2/9qf3MHw6+ SpeLDPTBxZxdeogCyKebfYwTvxbONKQC0oFx1dzSzDiJE2YJshbERtlu7d9GIXe1HT xDMWYWQU8v31A== From: SeongJae Park To: Andrew Morton Cc: SeongJae Park , damon@lists.linux.dev, kernel-team@meta.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 02/14] mm/damon/core: introduce repeat mode damon_call() Date: Sat, 12 Jul 2025 12:50:04 -0700 Message-Id: <20250712195016.151108-3-sj@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250712195016.151108-1-sj@kernel.org> References: <20250712195016.151108-1-sj@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Stat-Signature: dwq3go73qurxme3us16egydcorowan5y X-Rspamd-Queue-Id: 6BC3020005 X-Rspamd-Server: rspam10 X-Rspam-User: X-HE-Tag: 1752349823-372695 X-HE-Meta: U2FsdGVkX18WPSPBb05mWI9AyR4bXSYUbqYdKz1zIhNNdyaI10TkQBfZNdyKhOoTcY144yZQ69KrhqwcKaNF1Pa8oL3xaaXyHkioKZkH8KrzNAk3giUF8xMGh74floGLmoyi1EgRiB0h7yNmYvYhCfT2r5UpRY6uOUQTxK5/7trfiWKGVr54upeaI3vPRMbcwVugEWO19ZRugl03Cy/OUYZ13ldsiocCVJlU26R52elljAqclW7Nq8Pb3JBBO2HSVrVhLWOgx7qPDtQSa9sECo5FyV+yF4C4KA8Z0723I+kUtgMr1NaN0AVO88n7hzmFI9mydvMoXKHax4ZUXh3HY9tnXS79efESBG22l2pxR0DxPpE5+v9L5UJ/vo2Y3PnwvS4zKp453DUhxw3okfNRUwJlem4LnK/UsOeAHnnroUQXzyHbxegvrzPZqucAxJlH/WBgUrnCVwvwLaXmkvKyAVtRWdU8pN2+jA975I7e880ewErZBZF0LKLEVhPpm2PZwHGD5E80Gp0wNCspbdvHkGl7oL1czuoJrkU6q6GMv8Dgij5u/T7M/JE/dW6ao4k1+0gTNWf5jL87aN6EjZtY9DxQo0WDNE0XSmsJ6xqBo1TPcOuVjkzTE+01CB9ImE4nyzNppWG4hbHnZSpGAmR1nY9PPzIoXCJUc213rCDeocI2SxtJQFG/mT9625XyH2v6ZPAKGicAUiRQqgAO/SJLo3eZ1yd78Dm+flA2oCv2szGY/M/nyvOIzb51RyAhtPkIYL34j3bP8FInb4y9U0GAcU9uSs7SAWjKH434wYX/lhavKwrq8HGRdzW2auITZhv0Qm52e5LLpdXtj1YgCpYCGa1w7qZRNIUz2Wh3ZeQU7qDpZFVxrDfRMCuYdCrSML0xHHzi7NR+Q39tw78YsDfjo5I7/PN7t/0wTr5K/+pe7/aBG3e91I/KwPGXxmuoqLMClM4221Uu8LBWWMwEtwq w0/RapYb uxbHS755RM0494SbFIfg3hLYh4+mYPQfKP7l+hfxWRz9My0rhspTz0I5x2mmSz9sNEufuxyVR9uXSJYKOih5k9QApWHdvz1PfCBgrb5xQasdmnkbbt/E/ZGIN4NTehOxP6Y/TQMDGOgUD2ONNlgZionsRMqFoH2bA7AporXJ6yUjrX1wKRWww9cx4ffJ6oudFz1wlU/cMDp7Y0YUtAEZZkEwZUkMpDXL/ey/knx6WLbjqubnoNkOTzu9b7R+qbr4EqUAq0ecEfgxwuxEgPxvVN3sqEA== 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 | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/include/linux/damon.h b/include/linux/damon.h index 562c7876ba88..b83987275ff9 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 b0a0b98f6889..ffb87497dbb5 100644 --- a/mm/damon/core.c +++ b/mm/damon/core.c @@ -1379,8 +1379,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 @@ -1392,7 +1393,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); @@ -1401,6 +1403,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; @@ -2429,6 +2433,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) { @@ -2437,7 +2442,7 @@ static void kdamond_call(struct damon_ctx *ctx, bool cancel) struct damon_call_control, list); mutex_unlock(&ctx->call_controls_lock); if (!control) - return; + break; if (cancel) { control->canceled = true; } else { @@ -2447,8 +2452,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