* [PATCH v2 0/2] mm/damon: fixes for the jiffies-related issues @ 2025-10-30 2:07 Quanmin Yan 2025-10-30 2:07 ` [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable Quanmin Yan 2025-10-30 2:07 ` [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies " Quanmin Yan 0 siblings, 2 replies; 5+ messages in thread From: Quanmin Yan @ 2025-10-30 2:07 UTC (permalink / raw) To: sj Cc: akpm, damon, linux-kernel, linux-mm, yanquanmin1, wangkefeng.wang, zuoze1 On 32-bit systems, the kernel initializes jiffies to "-5 minutes" to make jiffies wrap bugs appear earlier. However, this may cause the time_before() series of functions to return unexpected values, resulting in DAMON not functioning as intended. Meanwhile, similar issues exist in some specific user operation scenarios. This patchset addresses these issues. The first patch is about the DAMON_STAT module, and the second patch is about the core layer's sysfs. Changes from v1 (https://lore.kernel.org/all/20251028061927.1378746-1-yanquanmin1@huawei.com/) - Added commit information describing another similar issue. (found by SJ) - Making last_refresh_jiffies a global variable and initialize it on damon_stat_start(). (suggested by SJ) - Added a new patch that fixes the same root cause issue in the core layer's sysfs. (found and suggested by SJ) Quanmin Yan (2): mm/damon/stat: change last_refresh_jiffies to a global variable mm/damon/sysfs: change next_update_jiffies to a global variable mm/damon/stat.c | 9 ++++++--- mm/damon/sysfs.c | 10 +++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable 2025-10-30 2:07 [PATCH v2 0/2] mm/damon: fixes for the jiffies-related issues Quanmin Yan @ 2025-10-30 2:07 ` Quanmin Yan 2025-10-30 14:31 ` SeongJae Park 2025-10-30 2:07 ` [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies " Quanmin Yan 1 sibling, 1 reply; 5+ messages in thread From: Quanmin Yan @ 2025-10-30 2:07 UTC (permalink / raw) To: sj Cc: akpm, damon, linux-kernel, linux-mm, yanquanmin1, wangkefeng.wang, zuoze1 In DAMON_STAT's damon_stat_damon_call_fn(), time_before_eq() is used to avoid unnecessarily frequent stat update. On 32-bit systems, the kernel initializes jiffies to "-5 minutes" to make jiffies wrap bugs appear earlier. However, this causes time_before_eq() in DAMON_STAT to unexpectedly return true during the first 5 minutes after boot on 32-bit systems (see [1] for more explanation, which fixes another jiffies-related issue before). As a result, DAMON_STAT does not update any monitoring results during that period, which becomes more confusing when DAMON_STAT_ENABLED_DEFAULT is enabled. There is also an issue unrelated to the system’s word size[2]: if the user stops DAMON_STAT just after last_refresh_jiffies is updated and restarts it after 5 seconds or a longer delay, last_refresh_jiffies will retain an older value, causing time_before_eq() to return false and the update to happen earlier than expected. Fix these issues by making last_refresh_jiffies a global variable and initializing it each time DAMON_STAT is started. [1] https://lkml.kernel.org/r/20250822025057.1740854-1-ekffu200098@gmail.com [2] https://lore.kernel.org/all/20251028143250.50144-1-sj@kernel.org/ Fixes: fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth") Suggested-by: SeongJae Park <sj@kernel.org> Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com> --- mm/damon/stat.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mm/damon/stat.c b/mm/damon/stat.c index 6c4503d2aee3..ed8e3629d31a 100644 --- a/mm/damon/stat.c +++ b/mm/damon/stat.c @@ -46,6 +46,8 @@ MODULE_PARM_DESC(aggr_interval_us, static struct damon_ctx *damon_stat_context; +static unsigned long damon_stat_last_refresh_jiffies; + static void damon_stat_set_estimated_memory_bandwidth(struct damon_ctx *c) { struct damon_target *t; @@ -130,13 +132,12 @@ static void damon_stat_set_idletime_percentiles(struct damon_ctx *c) static int damon_stat_damon_call_fn(void *data) { struct damon_ctx *c = data; - static unsigned long last_refresh_jiffies; /* avoid unnecessarily frequent stat update */ - if (time_before_eq(jiffies, last_refresh_jiffies + + if (time_before_eq(jiffies, damon_stat_last_refresh_jiffies + msecs_to_jiffies(5 * MSEC_PER_SEC))) return 0; - last_refresh_jiffies = jiffies; + damon_stat_last_refresh_jiffies = jiffies; aggr_interval_us = c->attrs.aggr_interval; damon_stat_set_estimated_memory_bandwidth(c); @@ -211,6 +212,8 @@ static int damon_stat_start(void) err = damon_start(&damon_stat_context, 1, true); if (err) return err; + + damon_stat_last_refresh_jiffies = jiffies; call_control.data = damon_stat_context; return damon_call(damon_stat_context, &call_control); } -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable 2025-10-30 2:07 ` [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable Quanmin Yan @ 2025-10-30 14:31 ` SeongJae Park 0 siblings, 0 replies; 5+ messages in thread From: SeongJae Park @ 2025-10-30 14:31 UTC (permalink / raw) To: Quanmin Yan Cc: SeongJae Park, akpm, damon, linux-kernel, linux-mm, wangkefeng.wang, zuoze1 On Thu, 30 Oct 2025 10:07:45 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote: > In DAMON_STAT's damon_stat_damon_call_fn(), time_before_eq() is used to > avoid unnecessarily frequent stat update. > > On 32-bit systems, the kernel initializes jiffies to "-5 minutes" to make > jiffies wrap bugs appear earlier. However, this causes time_before_eq() > in DAMON_STAT to unexpectedly return true during the first 5 minutes > after boot on 32-bit systems (see [1] for more explanation, which fixes > another jiffies-related issue before). As a result, DAMON_STAT does not > update any monitoring results during that period, which becomes more > confusing when DAMON_STAT_ENABLED_DEFAULT is enabled. > > There is also an issue unrelated to the system’s word size[2]: if the > user stops DAMON_STAT just after last_refresh_jiffies is updated and > restarts it after 5 seconds or a longer delay, last_refresh_jiffies will > retain an older value, causing time_before_eq() to return false and the > update to happen earlier than expected. > > Fix these issues by making last_refresh_jiffies a global variable and > initializing it each time DAMON_STAT is started. > > [1] https://lkml.kernel.org/r/20250822025057.1740854-1-ekffu200098@gmail.com > [2] https://lore.kernel.org/all/20251028143250.50144-1-sj@kernel.org/ Thank you for finding and fixing these! > > Fixes: fabdd1e911da ("mm/damon/stat: calculate and expose estimated memory bandwidth") > Suggested-by: SeongJae Park <sj@kernel.org> > Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com> Reviewed-by: SeongJae Park <sj@kernel.org> Thanks, SJ [...] ^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies to a global variable 2025-10-30 2:07 [PATCH v2 0/2] mm/damon: fixes for the jiffies-related issues Quanmin Yan 2025-10-30 2:07 ` [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable Quanmin Yan @ 2025-10-30 2:07 ` Quanmin Yan 2025-10-30 14:32 ` SeongJae Park 1 sibling, 1 reply; 5+ messages in thread From: Quanmin Yan @ 2025-10-30 2:07 UTC (permalink / raw) To: sj Cc: akpm, damon, linux-kernel, linux-mm, yanquanmin1, wangkefeng.wang, zuoze1 In DAMON’s damon_sysfs_repeat_call_fn(), time_before() is used to compare the current jiffies with next_update_jiffies to determine whether to update the sysfs files at this moment. On 32-bit systems, the kernel initializes jiffies to "-5 minutes" to make jiffies wrap bugs appear earlier. However, this causes time_before() in damon_sysfs_repeat_call_fn() to unexpectedly return true during the first 5 minutes after boot on 32-bit systems (see [1] for more explanation, which fixes another jiffies-related issue before). As a result, DAMON does not update sysfs files during that period. There is also an issue unrelated to the system’s word size[2]: if the user stops DAMON just after next_update_jiffies is updated and restarts it after 'refresh_ms' or a longer delay, next_update_jiffies will retain an older value, causing time_before() to return false and the update to happen earlier than expected. Fix these issues by making next_update_jiffies a global variable and initializing it each time DAMON is started. [1] https://lkml.kernel.org/r/20250822025057.1740854-1-ekffu200098@gmail.com [2] https://lore.kernel.org/all/20251029013038.66625-1-sj@kernel.org/ Fixes: d809a7c64ba8 ("mm/damon/sysfs: implement refresh_ms file internal work") Suggested-by: SeongJae Park <sj@kernel.org> Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com> --- mm/damon/sysfs.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mm/damon/sysfs.c b/mm/damon/sysfs.c index 43ee9ce4dd84..e2bd2d7becdd 100644 --- a/mm/damon/sysfs.c +++ b/mm/damon/sysfs.c @@ -1601,16 +1601,17 @@ static struct damon_ctx *damon_sysfs_build_ctx( return ctx; } +static unsigned long damon_sysfs_next_update_jiffies; + static int damon_sysfs_repeat_call_fn(void *data) { struct damon_sysfs_kdamond *sysfs_kdamond = data; - static unsigned long next_update_jiffies; if (!sysfs_kdamond->refresh_ms) return 0; - if (time_before(jiffies, next_update_jiffies)) + if (time_before(jiffies, damon_sysfs_next_update_jiffies)) return 0; - next_update_jiffies = jiffies + + damon_sysfs_next_update_jiffies = jiffies + msecs_to_jiffies(sysfs_kdamond->refresh_ms); if (!mutex_trylock(&damon_sysfs_lock)) @@ -1656,6 +1657,9 @@ static int damon_sysfs_turn_damon_on(struct damon_sysfs_kdamond *kdamond) } kdamond->damon_ctx = ctx; + damon_sysfs_next_update_jiffies = + jiffies + msecs_to_jiffies(kdamond->refresh_ms); + repeat_call_control->fn = damon_sysfs_repeat_call_fn; repeat_call_control->data = kdamond; repeat_call_control->repeat = true; -- 2.43.0 ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies to a global variable 2025-10-30 2:07 ` [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies " Quanmin Yan @ 2025-10-30 14:32 ` SeongJae Park 0 siblings, 0 replies; 5+ messages in thread From: SeongJae Park @ 2025-10-30 14:32 UTC (permalink / raw) To: Quanmin Yan Cc: SeongJae Park, akpm, damon, linux-kernel, linux-mm, wangkefeng.wang, zuoze1 On Thu, 30 Oct 2025 10:07:46 +0800 Quanmin Yan <yanquanmin1@huawei.com> wrote: > In DAMON’s damon_sysfs_repeat_call_fn(), time_before() is used to compare > the current jiffies with next_update_jiffies to determine whether to > update the sysfs files at this moment. > > On 32-bit systems, the kernel initializes jiffies to "-5 minutes" to make > jiffies wrap bugs appear earlier. However, this causes time_before() in > damon_sysfs_repeat_call_fn() to unexpectedly return true during the first > 5 minutes after boot on 32-bit systems (see [1] for more explanation, > which fixes another jiffies-related issue before). As a result, DAMON > does not update sysfs files during that period. > > There is also an issue unrelated to the system’s word size[2]: if the > user stops DAMON just after next_update_jiffies is updated and restarts > it after 'refresh_ms' or a longer delay, next_update_jiffies will retain > an older value, causing time_before() to return false and the update to > happen earlier than expected. > > Fix these issues by making next_update_jiffies a global variable and > initializing it each time DAMON is started. > > [1] https://lkml.kernel.org/r/20250822025057.1740854-1-ekffu200098@gmail.com > [2] https://lore.kernel.org/all/20251029013038.66625-1-sj@kernel.org/ Thank you for finding and fixing these! > > Fixes: d809a7c64ba8 ("mm/damon/sysfs: implement refresh_ms file internal work") > Suggested-by: SeongJae Park <sj@kernel.org> > Signed-off-by: Quanmin Yan <yanquanmin1@huawei.com> Reviewed-by: SeongJae Park <sj@kernel.org> Thanks, SJ [...] ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2025-10-30 14:32 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2025-10-30 2:07 [PATCH v2 0/2] mm/damon: fixes for the jiffies-related issues Quanmin Yan 2025-10-30 2:07 ` [PATCH v2 1/2] mm/damon/stat: change last_refresh_jiffies to a global variable Quanmin Yan 2025-10-30 14:31 ` SeongJae Park 2025-10-30 2:07 ` [PATCH v2 2/2] mm/damon/sysfs: change next_update_jiffies " Quanmin Yan 2025-10-30 14:32 ` SeongJae Park
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox