From 2fdb3f6c9654c41a48aec6ece600d15a744ce5e8 Mon Sep 17 00:00:00 2001 From: Emoly Liu Date: Fri, 28 Mar 2025 18:30:54 +0800 Subject: [PATCH] LU-18834 obdclass: check overflow when doing sum in stats Check overflow when calculating cl_sum and cl_sumsquare in stats. Signed-off-by: Emoly Liu Change-Id: Icdf31324ef0f06adc184e6fe5348acad25962d53 Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/58566 Reviewed-by: Andreas Dilger Reviewed-by: Zhenyu Xu Reviewed-by: Oleg Drokin Tested-by: jenkins Tested-by: Maloo --- lustre/obdclass/lprocfs_status.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lustre/obdclass/lprocfs_status.c b/lustre/obdclass/lprocfs_status.c index 8937980..7370153 100644 --- a/lustre/obdclass/lprocfs_status.c +++ b/lustre/obdclass/lprocfs_status.c @@ -550,6 +550,18 @@ void lprocfs_stats_unlock(struct lprocfs_stats *stats, } } +static __s64 sum_check(__s64 old, __s64 incr) +{ + __s64 new; + + new = old + incr; + /* check overflow */ + if (unlikely(new < old)) + new = LLONG_MAX; + + return new; +} + /** add up per-cpu counters */ void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx, struct lprocfs_counter *cnt) @@ -577,12 +589,13 @@ void lprocfs_stats_collect(struct lprocfs_stats *stats, int idx, percpu_cntr = lprocfs_stats_counter_get(stats, i, idx); cnt->lc_count += percpu_cntr->lc_count; - cnt->lc_sum += percpu_cntr->lc_sum; if (percpu_cntr->lc_min < cnt->lc_min) cnt->lc_min = percpu_cntr->lc_min; if (percpu_cntr->lc_max > cnt->lc_max) cnt->lc_max = percpu_cntr->lc_max; - cnt->lc_sumsquare += percpu_cntr->lc_sumsquare; + cnt->lc_sum = sum_check(cnt->lc_sum, percpu_cntr->lc_sum); + cnt->lc_sumsquare = sum_check(cnt->lc_sumsquare, + percpu_cntr->lc_sumsquare); } lprocfs_stats_unlock(stats, LPROCFS_GET_NUM_CPU, &flags); -- 1.8.3.1