From nobody Fri Jul 24 21:54:40 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3F3E318ED7; Fri, 24 Jul 2026 07:06:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.35 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876781; cv=none; b=lh5lUgjpSvm5pc73WTlqfSPd59DPdcDeufVJxj9DHq7OkyUO1LQZElDuf34UVV1a1sHB8JJfNg99sTlTOYd1Ud2o7GA5p+AmMnPJWmP+pVyGkfplnl600SuDn9rgCFkQSq8TFBsIJGu2mZAbdqet/aBscOJ0aBX/08FoSLieoCI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876781; c=relaxed/simple; bh=6WCdg28sWOTF59YMUpyZnB0BCNx6onDsUUfupAkPKkU=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=WChwjhdzYMy17BDeFtAS9idKxtEh4o3Cv9FYRy9pL87L3jwXMkPOKJuCeaAqSXqVCbbjR76bF2g6wl6vqDiGujYbpurDAB9M7W/A0+USLQ4SpRfNgBr+Lrv8Bd13hiTGSjynqTsUz+4u5xohvN26PQluQeLdaUIQeLwxdISAQJo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.35 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4h5zYP0SnPz8Xs7F; Fri, 24 Jul 2026 15:06:17 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl1.zte.com.cn with SMTP id 66O761F2098588; Fri, 24 Jul 2026 15:06:01 +0800 (+08) (envelope-from wang.yaxin@zte.com.cn) Received: from mapi (xaxapp05[null]) by mapi (Zmail) with MAPI id mid32; Fri, 24 Jul 2026 15:06:03 +0800 (CST) X-Zmail-TransId: 2afc6a630edb40f-b89f6 X-Mailer: Zmail v1.0 Message-ID: <20260724150603447clGkb8QaVNMCIgvOxsJNC@zte.com.cn> In-Reply-To: <2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn> References: 2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn Date: Fri, 24 Jul 2026 15:06:03 +0800 (CST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: Cc: , , , , , , Subject: =?UTF-8?B?W1BBVENIIHY0IDEvM10gZGVsYXl0b3A6IHJlZmFjdG9yIHJlcGV0aXRpdmUgZGVsYXkgZmllbGRzIGludG8gYXJyYXkgd2l0aCBlbnVt?= X-MAIL: mse-fl1.zte.com.cn 66O761F2098588 X-TLS: YES X-ENVELOPE-SENDER: wang.yaxin@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 24 Jul 2026 15:06:17 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A630EE9.001/4h5zYP0SnPz8Xs7F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Wang Yaxin Replace the 9 groups of (count, delay_total, delay_max, delay_max_ts) named fields in struct task_info with a struct delay_metrics array indexed by enum delay_type. This eliminates all unsafe pointer arithmetic via offsetof() from compare_tasks(), field_delay_max_and_ts(), and get_field_delay_values(). The struct field_desc now stores an enum delay_type index instead of four separate unsigned long offset values. Signed-off-by: Wang Yaxin Acked-by: Xu Xin --- tools/accounting/delaytop.c | 220 +++++++++++++++--------------------- 1 file changed, 88 insertions(+), 132 deletions(-) diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c index 1c40bb477320..f1d26ff98792 100644 --- a/tools/accounting/delaytop.c +++ b/tools/accounting/delaytop.c @@ -61,31 +61,51 @@ #define MAX_MSG_SIZE 1024 #define MAX_TASKS 1000 #define MAX_BUF_LEN 256 -#define SET_TASK_STAT(task_count, field) tasks[task_count].field =3D stats= .field #define BOOL_FPRINT(stream, fmt, ...) \ ({ \ int ret =3D fprintf(stream, fmt, ##__VA_ARGS__); \ ret >=3D 0; \ }) -#define TASK_AVG(task, field) average_ms((task).field##_delay_total, (task= ).field##_count) +#define TASK_AVG(task, type) \ + average_ms((task).delays[DELAY_##type].delay_total, \ + (task).delays[DELAY_##type].count) #define PSI_LINE_FORMAT "%-12s %6.1f%%/%6.1f%%/%6.1f%%/%8llu(ms)\n" #define DELAY_FMT_DEFAULT "%8.2f %8.2f %8.2f %8.2f\n" #define DELAY_FMT_MEMVERBOSE "%8.2f %8.2f %8.2f %8.2f %8.2f %8.2f\n" -#define SORT_FIELD(name, cmd, modes) \ - {#name, #cmd, \ - offsetof(struct task_info, name##_delay_total), \ - offsetof(struct task_info, name##_count), \ - offsetof(struct task_info, name##_delay_max), \ - offsetof(struct task_info, name##_delay_max_ts), \ - modes} -#define SORT_FIELD_NO_MAX(name, cmd, modes) \ - {#name, #cmd, \ - offsetof(struct task_info, name##_delay_total), \ - offsetof(struct task_info, name##_count), \ - 0, \ - 0, \ - modes} -#define END_FIELD {NULL, 0, 0, 0, 0, 0, 0} +#define COPY_DELAY(task_idx, type, stats_prefix) \ + do { \ + tasks[task_idx].delays[DELAY_##type].count =3D \ + stats.stats_prefix##_count; \ + tasks[task_idx].delays[DELAY_##type].delay_total =3D \ + stats.stats_prefix##_delay_total; \ + tasks[task_idx].delays[DELAY_##type].delay_max =3D \ + stats.stats_prefix##_delay_max; \ + tasks[task_idx].delays[DELAY_##type].delay_max_ts =3D \ + stats.stats_prefix##_delay_max_ts; \ + } while (0) +#define SORT_FIELD(name, cmd, type, modes, has_max) \ + {#name, #cmd, type, modes, has_max} +#define END_FIELD {NULL, 0, 0, 0, false} + +enum delay_type { + DELAY_CPU, + DELAY_BLKIO, + DELAY_SWAPIN, + DELAY_FREEPAGES, + DELAY_THRASHING, + DELAY_COMPACT, + DELAY_WPCOPY, + DELAY_IRQ, + DELAY_MEM, + NUM_DELAY_TYPES +}; + +struct delay_metrics { + unsigned long long count; + unsigned long long delay_total; + unsigned long long delay_max; + struct __kernel_timespec delay_max_ts; +}; /* Display mode types */ #define MODE_TYPE_ALL (0xFFFFFFFF) @@ -116,40 +136,7 @@ struct task_info { int pid; int tgid; char command[TASK_COMM_LEN]; - unsigned long long cpu_count; - unsigned long long cpu_delay_total; - unsigned long long cpu_delay_max; - struct __kernel_timespec cpu_delay_max_ts; - unsigned long long blkio_count; - unsigned long long blkio_delay_total; - unsigned long long blkio_delay_max; - struct __kernel_timespec blkio_delay_max_ts; - unsigned long long swapin_count; - unsigned long long swapin_delay_total; - unsigned long long swapin_delay_max; - struct __kernel_timespec swapin_delay_max_ts; - unsigned long long freepages_count; - unsigned long long freepages_delay_total; - unsigned long long freepages_delay_max; - struct __kernel_timespec freepages_delay_max_ts; - unsigned long long thrashing_count; - unsigned long long thrashing_delay_total; - unsigned long long thrashing_delay_max; - struct __kernel_timespec thrashing_delay_max_ts; - unsigned long long compact_count; - unsigned long long compact_delay_total; - unsigned long long compact_delay_max; - struct __kernel_timespec compact_delay_max_ts; - unsigned long long wpcopy_count; - unsigned long long wpcopy_delay_total; - unsigned long long wpcopy_delay_max; - struct __kernel_timespec wpcopy_delay_max_ts; - unsigned long long irq_count; - unsigned long long irq_delay_total; - unsigned long long irq_delay_max; - struct __kernel_timespec irq_delay_max_ts; - unsigned long long mem_count; - unsigned long long mem_delay_total; + struct delay_metrics delays[NUM_DELAY_TYPES]; }; /* Container statistics structure */ @@ -165,11 +152,9 @@ struct container_stats { struct field_desc { const char *name; /* Field name for cmdline argument */ const char *cmd_char; /* Interactive command */ - unsigned long total_offset; /* Offset of total delay in task_info */ - unsigned long count_offset; /* Offset of count in task_info */ - unsigned long max_offset; /* Offset of max delay in task_info */ - unsigned long max_ts_offset; /* Offset of max delay timestamp in task_inf= o */ + enum delay_type type; /* Index into task_info.delays[] */ size_t supported_modes; /* Supported display modes */ + bool has_max; /* Whether this field has max/ts */ }; /* Program settings structure */ @@ -193,15 +178,15 @@ static int task_count; static int running =3D 1; static struct container_stats container_stats; static const struct field_desc sort_fields[] =3D { - SORT_FIELD(cpu, c, MODE_DEFAULT | MODE_TYPE), - SORT_FIELD(blkio, i, MODE_DEFAULT | MODE_TYPE), - SORT_FIELD(irq, q, MODE_DEFAULT | MODE_TYPE), - SORT_FIELD_NO_MAX(mem, m, MODE_DEFAULT | MODE_MEMVERBOSE), - SORT_FIELD(swapin, s, MODE_MEMVERBOSE | MODE_TYPE), - SORT_FIELD(freepages, r, MODE_MEMVERBOSE | MODE_TYPE), - SORT_FIELD(thrashing, t, MODE_MEMVERBOSE | MODE_TYPE), - SORT_FIELD(compact, p, MODE_MEMVERBOSE | MODE_TYPE), - SORT_FIELD(wpcopy, w, MODE_MEMVERBOSE | MODE_TYPE), + SORT_FIELD(cpu, c, DELAY_CPU, MODE_DEFAULT | MODE_TYPE, true), + SORT_FIELD(blkio, i, DELAY_BLKIO, MODE_DEFAULT | MODE_TYPE, true), + SORT_FIELD(irq, q, DELAY_IRQ, MODE_DEFAULT | MODE_TYPE, true), + SORT_FIELD(mem, m, DELAY_MEM, MODE_DEFAULT | MODE_MEMVERBOSE, false), + SORT_FIELD(swapin, s, DELAY_SWAPIN, MODE_MEMVERBOSE | MODE_TYPE, true), + SORT_FIELD(freepages, r, DELAY_FREEPAGES, MODE_MEMVERBOSE | MODE_TYPE, tr= ue), + SORT_FIELD(thrashing, t, DELAY_THRASHING, MODE_MEMVERBOSE | MODE_TYPE, tr= ue), + SORT_FIELD(compact, p, DELAY_COMPACT, MODE_MEMVERBOSE | MODE_TYPE, true), + SORT_FIELD(wpcopy, w, DELAY_WPCOPY, MODE_MEMVERBOSE | MODE_TYPE, true), END_FIELD }; static int sort_selected; @@ -433,20 +418,20 @@ static void parse_args(int argc, char **argv) /* Calculate average delay in milliseconds for overall memory */ static void set_mem_delay_total(struct task_info *t) { - t->mem_delay_total =3D t->swapin_delay_total + - t->freepages_delay_total + - t->thrashing_delay_total + - t->compact_delay_total + - t->wpcopy_delay_total; + t->delays[DELAY_MEM].delay_total =3D t->delays[DELAY_SWAPIN].delay_total + + t->delays[DELAY_FREEPAGES].delay_total + + t->delays[DELAY_THRASHING].delay_total + + t->delays[DELAY_COMPACT].delay_total + + t->delays[DELAY_WPCOPY].delay_total; } static void set_mem_count(struct task_info *t) { - t->mem_count =3D t->swapin_count + - t->freepages_count + - t->thrashing_count + - t->compact_count + - t->wpcopy_count; + t->delays[DELAY_MEM].count =3D t->delays[DELAY_SWAPIN].count + + t->delays[DELAY_FREEPAGES].count + + t->delays[DELAY_THRASHING].count + + t->delays[DELAY_COMPACT].count + + t->delays[DELAY_WPCOPY].count; } /* Create a raw netlink socket and bind */ @@ -758,38 +743,14 @@ static void fetch_and_fill_task_info(int pid, const c= har *comm) strncpy(tasks[task_count].command, comm, TASK_COMM_LEN - 1); tasks[task_count].command[TASK_COMM_LEN - 1] =3D '\0'; - SET_TASK_STAT(task_count, cpu_count); - SET_TASK_STAT(task_count, cpu_delay_total); - SET_TASK_STAT(task_count, cpu_delay_max); - SET_TASK_STAT(task_count, cpu_delay_max_ts); - SET_TASK_STAT(task_count, blkio_count); - SET_TASK_STAT(task_count, blkio_delay_total); - SET_TASK_STAT(task_count, blkio_delay_max); - SET_TASK_STAT(task_count, blkio_delay_max_ts); - SET_TASK_STAT(task_count, swapin_count); - SET_TASK_STAT(task_count, swapin_delay_total); - SET_TASK_STAT(task_count, swapin_delay_max); - SET_TASK_STAT(task_count, swapin_delay_max_ts); - SET_TASK_STAT(task_count, freepages_count); - SET_TASK_STAT(task_count, freepages_delay_total); - SET_TASK_STAT(task_count, freepages_delay_max); - SET_TASK_STAT(task_count, freepages_delay_max_ts); - SET_TASK_STAT(task_count, thrashing_count); - SET_TASK_STAT(task_count, thrashing_delay_total); - SET_TASK_STAT(task_count, thrashing_delay_max); - SET_TASK_STAT(task_count, thrashing_delay_max_ts); - SET_TASK_STAT(task_count, compact_count); - SET_TASK_STAT(task_count, compact_delay_total); - SET_TASK_STAT(task_count, compact_delay_max); - SET_TASK_STAT(task_count, compact_delay_max_ts); - SET_TASK_STAT(task_count, wpcopy_count); - SET_TASK_STAT(task_count, wpcopy_delay_total); - SET_TASK_STAT(task_count, wpcopy_delay_max); - SET_TASK_STAT(task_count, wpcopy_delay_max_ts); - SET_TASK_STAT(task_count, irq_count); - SET_TASK_STAT(task_count, irq_delay_total); - SET_TASK_STAT(task_count, irq_delay_max); - SET_TASK_STAT(task_count, irq_delay_max_ts); + COPY_DELAY(task_count, CPU, cpu); + COPY_DELAY(task_count, BLKIO, blkio); + COPY_DELAY(task_count, SWAPIN, swapin); + COPY_DELAY(task_count, FREEPAGES, freepages); + COPY_DELAY(task_count, THRASHING, thrashing); + COPY_DELAY(task_count, COMPACT, compact); + COPY_DELAY(task_count, WPCOPY, wpcopy); + COPY_DELAY(task_count, IRQ, irq); set_mem_count(&tasks[task_count]); set_mem_delay_total(&tasks[task_count]); task_count++; @@ -912,10 +873,10 @@ static int compare_tasks(const void *a, const void *b) return 0; } - total1 =3D *(unsigned long long *)((char *)t1 + cfg.sort_field->total_off= set); - total2 =3D *(unsigned long long *)((char *)t2 + cfg.sort_field->total_off= set); - count1 =3D *(unsigned long long *)((char *)t1 + cfg.sort_field->count_off= set); - count2 =3D *(unsigned long long *)((char *)t2 + cfg.sort_field->count_off= set); + total1 =3D t1->delays[cfg.sort_field->type].delay_total; + total2 =3D t2->delays[cfg.sort_field->type].delay_total; + count1 =3D t1->delays[cfg.sort_field->type].count; + count2 =3D t2->delays[cfg.sort_field->type].count; avg1 =3D average_ms(total1, count1); avg2 =3D average_ms(total2, count2); @@ -929,22 +890,17 @@ static int compare_tasks(const void *a, const void *b) static void field_delay_max_and_ts(const struct task_info *task, const str= uct field_desc *field, unsigned long long *max_ns, struct __kernel_timespec *max_ts) { - if (!field || !field->max_offset) { + if (!field || !field->has_max) { *max_ns =3D 0; if (max_ts) memset(max_ts, 0, sizeof(*max_ts)); return; } - *max_ns =3D *(unsigned long long *)((char *)task + field->max_offset); + *max_ns =3D task->delays[field->type].delay_max; - if (max_ts) { - if (field->max_ts_offset) - *max_ts =3D *(struct __kernel_timespec *)((char *)task + - field->max_ts_offset); - else - memset(max_ts, 0, sizeof(*max_ts)); - } + if (max_ts) + *max_ts =3D task->delays[field->type].delay_max_ts; } /* Get delay values for a specific field */ @@ -954,15 +910,15 @@ static void get_field_delay_values(const struct task_= info *task, const struct fi { unsigned long long total, count, max; - if (!field || !field->max_offset) { + if (!field) { *avg_ms =3D 0; *max_ms =3D 0; memset(max_ts, 0, sizeof(*max_ts)); return; } - total =3D *(unsigned long long *)((char *)task + field->total_offset); - count =3D *(unsigned long long *)((char *)task + field->count_offset); + total =3D task->delays[field->type].delay_total; + count =3D task->delays[field->type].count; *avg_ms =3D average_ms(total, count); field_delay_max_and_ts(task, field, &max, max_ts); @@ -1180,18 +1136,18 @@ static void display_results(int psi_ret) avg_ms, max_ms, format_kernel_timespec(&max_ts)); } else if (cfg.display_mode =3D=3D MODE_MEMVERBOSE) { suc &=3D BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE, - TASK_AVG(tasks[i], mem), - TASK_AVG(tasks[i], swapin), - TASK_AVG(tasks[i], freepages), - TASK_AVG(tasks[i], thrashing), - TASK_AVG(tasks[i], compact), - TASK_AVG(tasks[i], wpcopy)); + TASK_AVG(tasks[i], MEM), + TASK_AVG(tasks[i], SWAPIN), + TASK_AVG(tasks[i], FREEPAGES), + TASK_AVG(tasks[i], THRASHING), + TASK_AVG(tasks[i], COMPACT), + TASK_AVG(tasks[i], WPCOPY)); } else { suc &=3D BOOL_FPRINT(out, DELAY_FMT_DEFAULT, - TASK_AVG(tasks[i], cpu), - TASK_AVG(tasks[i], blkio), - TASK_AVG(tasks[i], irq), - TASK_AVG(tasks[i], mem)); + TASK_AVG(tasks[i], CPU), + TASK_AVG(tasks[i], BLKIO), + TASK_AVG(tasks[i], IRQ), + TASK_AVG(tasks[i], MEM)); } } --=20 2.27.0 From nobody Fri Jul 24 21:54:40 2026 Received: from mxct.zte.com.cn (mxct.zte.com.cn [183.62.165.209]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 30F274908B7; Fri, 24 Jul 2026 07:07:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=183.62.165.209 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876837; cv=none; b=B3joCiDJCu/GbXl6zpCg4X6DZ2JNRkz+LMCGi4iKnibBtfDP92X6SlB6wtlEE6A26qo69Nof2xHVmK5olsnzXiFCA70TT5jHUL+W/MpktDQbTJUKuZi7NvhpnmtKhaV9VzL/mVL6dAS8VPWCZlMnbQ7d41J2VRSUk1y1Wgg5KnQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876837; c=relaxed/simple; bh=7UUh8bclcRyme2mbvyKJ6Ap40+Lkcnh1K9H1O+kpXtE=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=Cpjtf4QC8L+o/gqfGg+B//v6P8uFnTU0hL+NZTLwC1EsiMXqqYUlqPxSFyLgr+SQ9V7qBI4a+XwFHwIM71vboHNOxv14s7qRCq51/h/rk80GRMPvpvr2iZvNX/n/hHcZCXIIpyIJIFF40Iv4TB38NO1zYVXAveS16GZJsxgsHgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=183.62.165.209 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl2.zte.com.cn (unknown [10.5.228.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxct.zte.com.cn (FangMail) with ESMTPS id 4h5zZN5x7Hz57KTp; Fri, 24 Jul 2026 15:07:08 +0800 (CST) Received: from xaxapp02.zte.com.cn ([10.88.97.241]) by mse-fl2.zte.com.cn with SMTP id 66O76xGu060859; Fri, 24 Jul 2026 15:06:59 +0800 (+08) (envelope-from wang.yaxin@zte.com.cn) Received: from mapi (xaxapp04[null]) by mapi (Zmail) with MAPI id mid32; Fri, 24 Jul 2026 15:07:00 +0800 (CST) X-Zmail-TransId: 2afb6a630f14daa-b6394 X-Mailer: Zmail v1.0 Message-ID: <20260724150700955ST0xNghN8r7XiXr9PfwXH@zte.com.cn> In-Reply-To: <2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn> References: 2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn Date: Fri, 24 Jul 2026 15:07:00 +0800 (CST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: Cc: , , , , , , Subject: =?UTF-8?B?W1BBVENIIHY0IDIvM10gdG9vbHMvYWNjb3VudGluZzogZmFjdG9yIG91dCBzaGFyZWQgZm9ybWF0X3RpbWVzcGVjKCkgaW1wbGVtZW50YXRpb24=?= X-MAIL: mse-fl2.zte.com.cn 66O76xGu060859 X-TLS: YES X-ENVELOPE-SENDER: wang.yaxin@zte.com.cn X-SOURCE-IP: 10.5.228.133 unknown Fri, 24 Jul 2026 15:07:08 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A630F1C.003/4h5zZN5x7Hz57KTp Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Wang Yaxin The same __kernel_timespec formatting logic existed independently in both getdelays.c and delaytop.c with minor differences (strftime vs snprintf, __kernel_time64_t vs time_t). Create a shared format_timespec.c/h with a canonical implementation (strftime + time_t), remove the static copies from both files, and link both programs against the common object. Also simplify the Makefile with a pattern rule for %.o and a static pattern rule for the two programs that need format_timespec.o. Signed-off-by: Wang Yaxin Acked-by: Xu Xin --- tools/accounting/Makefile | 12 ++++++- tools/accounting/delaytop.c | 39 ++------------------ tools/accounting/format_timespec.c | 39 ++++++++++++++++++++ tools/accounting/format_timespec.h | 9 +++++ tools/accounting/getdelays.c | 32 ++--------------- tools/include/uapi/linux/time_types.h | 51 +++++++++++++++++++++++++++ 6 files changed, 115 insertions(+), 67 deletions(-) create mode 100644 tools/accounting/format_timespec.c create mode 100644 tools/accounting/format_timespec.h create mode 100644 tools/include/uapi/linux/time_types.h diff --git a/tools/accounting/Makefile b/tools/accounting/Makefile index 007c0bb8cbbb..5224e03f5e94 100644 --- a/tools/accounting/Makefile +++ b/tools/accounting/Makefile @@ -3,8 +3,18 @@ CC :=3D $(CROSS_COMPILE)gcc CFLAGS :=3D -I../include/uapi/ PROGS :=3D getdelays procacct delaytop +OBJS :=3D format_timespec.o all: $(PROGS) +getdelays delaytop: %: %.o $(OBJS) + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +procacct: procacct.o + $(CC) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +%.o: %.c + $(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $< + clean: - rm -fr $(PROGS) + rm -fr $(PROGS) *.o diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c index f1d26ff98792..1144ca325447 100644 --- a/tools/accounting/delaytop.c +++ b/tools/accounting/delaytop.c @@ -44,6 +44,8 @@ #include #include +#include "format_timespec.h" + #define PSI_PATH "/proc/pressure" #define PSI_CPU_PATH "/proc/pressure/cpu" #define PSI_MEMORY_PATH "/proc/pressure/memory" @@ -817,41 +819,6 @@ static double average_ms(unsigned long long total, uns= igned long long count) return (double)total / 1000000.0 / count; } -/* - * Format __kernel_timespec to human readable string (YYYY-MM-DDTHH:MM:SS) - * Returns formatted string or "N/A" if timestamp is zero - */ -static const char *format_kernel_timespec(struct __kernel_timespec *ts) -{ - static char buffer[32]; - time_t time_sec; - struct tm tm_info; - - /* Check if timestamp is zero (not set) */ - if (ts->tv_sec =3D=3D 0 && ts->tv_nsec =3D=3D 0) - return "N/A"; - - /* Avoid Y2038 truncation: check if timestamp fits in time_t on 32-bit pl= atforms */ - if (sizeof(time_t) < sizeof(ts->tv_sec) && - ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1)) - return "N/A"; - - time_sec =3D (time_t)ts->tv_sec; - - if (localtime_r(&time_sec, &tm_info) =3D=3D NULL) - return "N/A"; - - snprintf(buffer, sizeof(buffer), "%04d-%02d-%02dT%02d:%02d:%02d", - tm_info.tm_year + 1900, - tm_info.tm_mon + 1, - tm_info.tm_mday, - tm_info.tm_hour, - tm_info.tm_min, - tm_info.tm_sec); - - return buffer; -} - /* Comparison function for sorting tasks */ static int compare_tasks(const void *a, const void *b) { @@ -1133,7 +1100,7 @@ static void display_results(int psi_ret) &max_ms, &max_ts); suc &=3D BOOL_FPRINT(out, "%12.2f %12.2f %20s\n", - avg_ms, max_ms, format_kernel_timespec(&max_ts)); + avg_ms, max_ms, format_timespec(&max_ts)); } else if (cfg.display_mode =3D=3D MODE_MEMVERBOSE) { suc &=3D BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE, TASK_AVG(tasks[i], MEM), diff --git a/tools/accounting/format_timespec.c b/tools/accounting/format_t= imespec.c new file mode 100644 index 000000000000..1dba50cac895 --- /dev/null +++ b/tools/accounting/format_timespec.c @@ -0,0 +1,39 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Shared __kernel_timespec formatting for tools/accounting/ + * + * Formats a __kernel_timespec to an ISO 8601 timestamp string + * (YYYY-MM-DDTHH:MM:SS). Returns "N/A" if the timestamp is zero + * or does not fit in time_t. + */ +#include +#include +#include "format_timespec.h" + +const char *format_timespec(const struct __kernel_timespec *ts) +{ + static char buffer[32]; + struct tm tm_info; + time_t time_sec; + + if (ts->tv_sec =3D=3D 0 && ts->tv_nsec =3D=3D 0) + return "N/A"; + + /* + * On 32-bit platforms time_t is 32-bit and cannot represent + * dates beyond Y2038. The kernel timestamp is always 64-bit, + * so reject values that would overflow. + */ + if (sizeof(time_t) < sizeof(ts->tv_sec) && + ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1)) + return "N/A"; + + time_sec =3D ts->tv_sec; + + if (!localtime_r(&time_sec, &tm_info)) + return "N/A"; + + strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm_info); + + return buffer; +} diff --git a/tools/accounting/format_timespec.h b/tools/accounting/format_t= imespec.h new file mode 100644 index 000000000000..960496ebf5c2 --- /dev/null +++ b/tools/accounting/format_timespec.h @@ -0,0 +1,9 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef FORMAT_TIMESPEC_H +#define FORMAT_TIMESPEC_H + +#include + +const char *format_timespec(const struct __kernel_timespec *ts); + +#endif diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c index 6ac30d4f96f7..d3193f670d89 100644 --- a/tools/accounting/getdelays.c +++ b/tools/accounting/getdelays.c @@ -30,6 +30,8 @@ #include #include +#include "format_timespec.h" + /* * Generic macros for dealing with netlink sockets. Might be duplicated * elsewhere. It is recommended that commercial grade applications use @@ -221,36 +223,6 @@ static int get_family_id(int sd) #define average_ms(t, c) (t / 1000000ULL / (c ? c : 1)) #define delay_ms(t) (t / 1000000ULL) -/* - * Format __kernel_timespec to human readable string (YYYY-MM-DD HH:MM:SS) - * Returns formatted string or "N/A" if timestamp is zero - */ -static const char *format_timespec(struct __kernel_timespec *ts) -{ - static char buffer[32]; - struct tm tm_info; - __kernel_time_t time_sec; - - /* Check if timestamp is zero (not set) */ - if (ts->tv_sec =3D=3D 0 && ts->tv_nsec =3D=3D 0) - return "N/A"; - - /* Avoid Y2038 truncation on 32-bit platforms */ - if (sizeof(time_sec) < sizeof(ts->tv_sec) && - ts->tv_sec > (__u64)((1ULL << (sizeof(time_sec) * 8 - 1)) - 1)) - return "N/A"; - - time_sec =3D ts->tv_sec; - - /* Use thread-safe localtime_r */ - if (localtime_r(&time_sec, &tm_info) =3D=3D NULL) - return "N/A"; - - strftime(buffer, sizeof(buffer), "%Y-%m-%dT%H:%M:%S", &tm_info); - - return buffer; -} - /* * Version compatibility note: * Field availability depends on taskstats version (t->version), diff --git a/tools/include/uapi/linux/time_types.h b/tools/include/uapi/lin= ux/time_types.h new file mode 100644 index 000000000000..8abdca230a54 --- /dev/null +++ b/tools/include/uapi/linux/time_types.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ +#ifndef _TOOLS_UAPI_LINUX_TIME_TYPES_H +#define _TOOLS_UAPI_LINUX_TIME_TYPES_H + +#include +#include + +/* + * Copied from include/uapi/linux/time_types.h + * + * Uses the arch-dependent __kernel_long_t rather than a hardcoded + * 'long' so that x32 (ILP32 on x86_64), where the kernel defines + * __kernel_long_t as 'long long', gets the correct struct layout. + * + * __kernel_long_t is provided by - included + * below via on most systems, or directly here. + */ + +struct __kernel_timespec { + long long tv_sec; + long long tv_nsec; +}; + +struct __kernel_itimerspec { + struct __kernel_timespec it_interval; + struct __kernel_timespec it_value; +}; + +#ifndef __kernel_old_timeval +struct __kernel_old_timeval { + __kernel_long_t tv_sec; + __kernel_long_t tv_usec; +}; +#endif + +struct __kernel_old_timespec { + __kernel_long_t tv_sec; + long tv_nsec; +}; + +struct __kernel_old_itimerval { + struct __kernel_old_timeval it_interval; + struct __kernel_old_timeval it_value; +}; + +struct __kernel_sock_timeval { + __s64 tv_sec; + __s64 tv_usec; +}; + +#endif /* _TOOLS_UAPI_LINUX_TIME_TYPES_H */ --=20 2.27.0 From nobody Fri Jul 24 21:54:40 2026 Received: from mxhk.zte.com.cn (mxhk.zte.com.cn [160.30.148.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1FF423515C9; Fri, 24 Jul 2026 07:07:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=160.30.148.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876875; cv=none; b=ZE1jDny3cUHvI2wz1lNnT0lvTo7hhHNFMDNsu6M5438bqEUpuR/dWgyHCZYMB/5NtFIf8MDp3g+T2Us4bMoEAn8nzu/rf+imT3V9EpIQwa7zIfKi+kCI/SpmYQd75s2lvAKtkgV38oEo4j6BSK8LCIOfSKqNtnBp+w9wnNBSboo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784876875; c=relaxed/simple; bh=SpLyHNLWG6+QJ4hIasOWRwoyJkezmghy3PvsWvRrMIA=; h=Message-ID:In-Reply-To:References:Date:Mime-Version:From:To:Cc: Subject:Content-Type; b=UHfivMQjoCUc1pTm9fDylNnBLq7uHycB1Qq/044Gez+GMxs4AAyYMcMnOW7jvAwQuNvVhYwa3nzmmUIXiONne8K3LjpCMnJtoCJtt82G/0r9ZhMc8O5yy9FZVY3Ra7SMvQ2ELY+LiBaCqFEvyd0ce+ClP8VDIDx2c+WUp6Waw8c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn; spf=pass smtp.mailfrom=zte.com.cn; arc=none smtp.client-ip=160.30.148.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=zte.com.cn Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=zte.com.cn Received: from mse-fl1.zte.com.cn (unknown [10.5.228.132]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mxhk.zte.com.cn (FangMail) with ESMTPS id 4h5zbD1YN0z4yjG1; Fri, 24 Jul 2026 15:07:52 +0800 (CST) Received: from xaxapp04.zte.com.cn ([10.99.98.157]) by mse-fl1.zte.com.cn with SMTP id 66O77k6G001727; Fri, 24 Jul 2026 15:07:46 +0800 (+08) (envelope-from wang.yaxin@zte.com.cn) Received: from mapi (xaxapp02[null]) by mapi (Zmail) with MAPI id mid32; Fri, 24 Jul 2026 15:07:48 +0800 (CST) X-Zmail-TransId: 2afa6a630f44685-d0c54 X-Mailer: Zmail v1.0 Message-ID: <20260724150748934bZZxt5fvWe2gYwh7ZEIxP@zte.com.cn> In-Reply-To: <2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn> References: 2026072415043227725SuvoyQ3SGDOmHLp8tdn@zte.com.cn Date: Fri, 24 Jul 2026 15:07:48 +0800 (CST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 From: To: Cc: , , , , , , Subject: =?UTF-8?B?W1BBVENIIHY0IDMvM10gdG9vbHMvYWNjb3VudGluZzogc2ltcGxpZnkgMzItYml0IHRpbWVfdCBvdmVyZmxvdyBjaGVjayBpbiBmb3JtYXRfdGltZXNwZWMoKQ==?= X-MAIL: mse-fl1.zte.com.cn 66O77k6G001727 X-TLS: YES X-ENVELOPE-SENDER: wang.yaxin@zte.com.cn X-SOURCE-IP: 10.5.228.132 unknown Fri, 24 Jul 2026 15:07:52 +0800 X-CLEAN: YES X-Fangmail-Anti-Spam-Filtered: true X-Fangmail-MID-QID: 6A630F48.000/4h5zbD1YN0z4yjG1 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Wang Yaxin Replace the Y2038 overflow guard in format_timespec() with a direct narrowing truncation check ((long long)time_sec !=3D ts->tv_sec), which is both simpler and more robust across platforms. Also move the time_sec assignment earlier to avoid duplication. Add nla_len < NLA_HDRLEN sanity checks in the netlink attribute parsing loops in getdelays.c and delaytop.c to prevent integer underflow in NLA_PAYLOAD() on malformed responses. While at it, fix a minor alignment issue in delaytop.c by adding a leading space to the output format string. Signed-off-by: Wang Yaxin Acked-by: Xu Xin --- tools/accounting/delaytop.c | 6 +++++- tools/accounting/format_timespec.c | 12 +++++------- tools/accounting/getdelays.c | 4 ++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tools/accounting/delaytop.c b/tools/accounting/delaytop.c index 1144ca325447..d32aedbfa07d 100644 --- a/tools/accounting/delaytop.c +++ b/tools/accounting/delaytop.c @@ -728,10 +728,14 @@ static void fetch_and_fill_task_info(int pid, const c= har *comm) nl_len =3D GENLMSG_PAYLOAD(&resp.n); na =3D (struct nlattr *) GENLMSG_DATA(&resp); while (nl_len > 0) { + if (na->nla_len < NLA_HDRLEN) + break; if (na->nla_type =3D=3D TASKSTATS_TYPE_AGGR_PID) { nested =3D (struct nlattr *) NLA_DATA(na); nested_len =3D NLA_PAYLOAD(na->nla_len); while (nested_len > 0) { + if (nested->nla_len < NLA_HDRLEN) + break; if (nested->nla_type =3D=3D TASKSTATS_TYPE_STATS) { size_t payload_len =3D NLA_PAYLOAD(nested->nla_len); @@ -1099,7 +1103,7 @@ static void display_results(int psi_ret) get_field_delay_values(&tasks[i], cfg.type_field, &avg_ms, &max_ms, &max_ts); - suc &=3D BOOL_FPRINT(out, "%12.2f %12.2f %20s\n", + suc &=3D BOOL_FPRINT(out, " %12.2f %12.2f %20s\n", avg_ms, max_ms, format_timespec(&max_ts)); } else if (cfg.display_mode =3D=3D MODE_MEMVERBOSE) { suc &=3D BOOL_FPRINT(out, DELAY_FMT_MEMVERBOSE, diff --git a/tools/accounting/format_timespec.c b/tools/accounting/format_t= imespec.c index 1dba50cac895..d7bfd307c00b 100644 --- a/tools/accounting/format_timespec.c +++ b/tools/accounting/format_timespec.c @@ -14,22 +14,20 @@ const char *format_timespec(const struct __kernel_times= pec *ts) { static char buffer[32]; struct tm tm_info; - time_t time_sec; + time_t time_sec =3D ts->tv_sec; if (ts->tv_sec =3D=3D 0 && ts->tv_nsec =3D=3D 0) return "N/A"; /* * On 32-bit platforms time_t is 32-bit and cannot represent - * dates beyond Y2038. The kernel timestamp is always 64-bit, - * so reject values that would overflow. + * timestamps outside [INT32_MIN, INT32_MAX]. A 64-bit kernel + * timestamp that does not survive the narrowing truncation is + * rejected to avoid silent data corruption. */ - if (sizeof(time_t) < sizeof(ts->tv_sec) && - ts->tv_sec > (__u64)((1ULL << (sizeof(time_t) * 8 - 1)) - 1)) + if ((long long)time_sec !=3D ts->tv_sec) return "N/A"; - time_sec =3D ts->tv_sec; - if (!localtime_r(&time_sec, &tm_info)) return "N/A"; diff --git a/tools/accounting/getdelays.c b/tools/accounting/getdelays.c index d3193f670d89..f47b9d008352 100644 --- a/tools/accounting/getdelays.c +++ b/tools/accounting/getdelays.c @@ -659,6 +659,8 @@ int main(int argc, char *argv[]) na =3D (struct nlattr *) GENLMSG_DATA(&msg); len =3D 0; while (len < rep_len) { + if (na->nla_len < NLA_HDRLEN) + break; len +=3D NLA_ALIGN(na->nla_len); switch (na->nla_type) { case TASKSTATS_TYPE_AGGR_TGID: @@ -669,6 +671,8 @@ int main(int argc, char *argv[]) /* For nested attributes, na follows */ na =3D (struct nlattr *) NLA_DATA(na); while (len2 < aggr_len) { + if (na->nla_len < NLA_HDRLEN) + break; switch (na->nla_type) { case TASKSTATS_TYPE_PID: rtid =3D *(int *) NLA_DATA(na); --=20 2.27.0