Whamcloud - gitweb
LU-18834 obdclass: check overflow when doing sum in stats 66/58566/3
authorEmoly Liu <emoly@whamcloud.com>
Fri, 28 Mar 2025 10:30:54 +0000 (18:30 +0800)
committerOleg Drokin <green@whamcloud.com>
Thu, 10 Apr 2025 06:59:28 +0000 (06:59 +0000)
Check overflow when calculating cl_sum and cl_sumsquare in stats.

Signed-off-by: Emoly Liu <emoly@whamcloud.com>
Change-Id: Icdf31324ef0f06adc184e6fe5348acad25962d53
Reviewed-on: https://review.whamcloud.com/c/fs/lustre-release/+/58566
Reviewed-by: Andreas Dilger <adilger@whamcloud.com>
Reviewed-by: Zhenyu Xu <bobijam@hotmail.com>
Reviewed-by: Oleg Drokin <green@whamcloud.com>
Tested-by: jenkins <devops@whamcloud.com>
Tested-by: Maloo <maloo@whamcloud.com>
lustre/obdclass/lprocfs_status.c

index 8937980..7370153 100644 (file)
@@ -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);