From: Hungyu Lin <dennylin0707@gmail.com>
To: bsingharora@gmail.com, yang.yang29@zte.com.cn
Cc: akpm@linux-foundation.org, brads@mainlining.org,
wang.yaxin@zte.com.cn, wang.yong12@zte.com.cn, xu.xin@linux.dev,
jiang.kun2@zte.com.cn, linux-kernel@vger.kernel.org,
stable@vger.kernel.org, Hungyu Lin <dennylin0707@gmail.com>
Subject: [PATCH v2] delayacct: Preserve delay extrema when aggregating taskstats
Date: Thu, 22 Oct 2026 16:06:48 +0000 [thread overview]
Message-ID: <20261022160648.37411-1-dennylin0707@gmail.com> (raw)
delayacct_add_tsk() adds each task's statistics to the accumulated TGID
statistics, but overwrites the delay maxima, minima and peak timestamps.
Consequently, the last task determines these fields, and a task with no
recorded delay can erase an earlier task's peak despite a nonzero total.
For example, a four-thread process has a worker writing copy-on-write
pages and a last-created thread with no WPCOPY events. With the group
stopped, getdelays -d -t 49 reports the following before and after the
fix (WPCOPY excerpt, spacing compacted and timestamp column omitted):
Before:
WPCOPY count delay total delay average delay max delay min
4103 5163661 0.001ms 0.000000ms 0.000000ms
After:
WPCOPY count delay total delay average delay max delay min
4101 4680983 0.001ms 0.049292ms 0.000208ms
The timings vary between runs. Before the fix, the zero extrema disagree
with the individual worker's nonzero values. After the fix, the group
extrema match the largest maximum and smallest nonzero minimum across
the individual threads.
Keep the largest maximum and its associated timestamp, and combine the
minima with min_not_zero() since zero denotes an unrecorded delay. Apply
this to CPU delay and the seven delay types handled by UPDATE_DELAY().
Single-task queries still start with zeroed statistics and retain the
task's values.
Fixes: 658eb5ab916d ("delayacct: add delay max to record delay peak")
Fixes: f65c64f311ee ("delayacct: add delay min to record delay peak")
Cc: stable@vger.kernel.org
Reviewed-by: Bradley Morgan <brads@mainlining.org>
Signed-off-by: Hungyu Lin <dennylin0707@gmail.com>
---
Changes in v2:
- Add a getdelays -d -t before/after example to the commit message, as
requested by Andrew Morton.
- Add Bradley Morgan's Reviewed-by.
- No code changes.
kernel/delayacct.c | 23 +++++++++++++++--------
1 file changed, 15 insertions(+), 8 deletions(-)
diff --git a/kernel/delayacct.c b/kernel/delayacct.c
index 479e860aff70..ea1424524654 100644
--- a/kernel/delayacct.c
+++ b/kernel/delayacct.c
@@ -12,14 +12,18 @@
#include <linux/taskstats.h>
#include <linux/sysctl.h>
#include <linux/delayacct.h>
+#include <linux/minmax.h>
#include <linux/module.h>
#define UPDATE_DELAY(type) \
do { \
- d->type##_delay_max = tsk->delays->type##_delay_max; \
- d->type##_delay_min = tsk->delays->type##_delay_min; \
- d->type##_delay_max_ts.tv_sec = tsk->delays->type##_delay_max_ts.tv_sec; \
- d->type##_delay_max_ts.tv_nsec = tsk->delays->type##_delay_max_ts.tv_nsec; \
+ if (tsk->delays->type##_delay_max > d->type##_delay_max) { \
+ d->type##_delay_max = tsk->delays->type##_delay_max; \
+ d->type##_delay_max_ts.tv_sec = tsk->delays->type##_delay_max_ts.tv_sec; \
+ d->type##_delay_max_ts.tv_nsec = tsk->delays->type##_delay_max_ts.tv_nsec; \
+ } \
+ d->type##_delay_min = min_not_zero(d->type##_delay_min, \
+ tsk->delays->type##_delay_min); \
tmp = d->type##_delay_total + tsk->delays->type##_delay; \
d->type##_delay_total = (tmp < d->type##_delay_total) ? 0 : tmp; \
d->type##_count += tsk->delays->type##_count; \
@@ -174,10 +178,13 @@ int delayacct_add_tsk(struct taskstats *d, struct task_struct *tsk)
d->cpu_count += t1;
- d->cpu_delay_max = tsk->sched_info.max_run_delay;
- d->cpu_delay_min = tsk->sched_info.min_run_delay;
- d->cpu_delay_max_ts.tv_sec = tsk->sched_info.max_run_delay_ts.tv_sec;
- d->cpu_delay_max_ts.tv_nsec = tsk->sched_info.max_run_delay_ts.tv_nsec;
+ if (tsk->sched_info.max_run_delay > d->cpu_delay_max) {
+ d->cpu_delay_max = tsk->sched_info.max_run_delay;
+ d->cpu_delay_max_ts.tv_sec = tsk->sched_info.max_run_delay_ts.tv_sec;
+ d->cpu_delay_max_ts.tv_nsec = tsk->sched_info.max_run_delay_ts.tv_nsec;
+ }
+ d->cpu_delay_min = min_not_zero(d->cpu_delay_min,
+ tsk->sched_info.min_run_delay);
tmp = (s64)d->cpu_delay_total + t2;
d->cpu_delay_total = (tmp < (s64)d->cpu_delay_total) ? 0 : tmp;
tmp = (s64)d->cpu_run_virtual_total + t3;
--
2.43.0
next reply other threads:[~2026-10-22 16:12 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-22 16:06 Hungyu Lin [this message]
2026-10-22 16:18 ` Bradley Morgan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261022160648.37411-1-dennylin0707@gmail.com \
--to=dennylin0707@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=brads@mainlining.org \
--cc=bsingharora@gmail.com \
--cc=jiang.kun2@zte.com.cn \
--cc=linux-kernel@vger.kernel.org \
--cc=stable@vger.kernel.org \
--cc=wang.yaxin@zte.com.cn \
--cc=wang.yong12@zte.com.cn \
--cc=xu.xin@linux.dev \
--cc=yang.yang29@zte.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®