Commit be08584a authored by Jan Kiszka's avatar Jan Kiszka

drivers/autotune: Fix UBSAN warnings

The calculations in add_sample can overflow into undefined behavior - or
even generate wrong results. Address that by giving related fields in
tuner_state more space. This requires one division to be modeled with
xnarch_llimd for 32-bit target archs.
Signed-off-by: Jan Kiszka's avatarJan Kiszka <jan.kiszka@siemens.com>
parent d48071ae
......@@ -57,8 +57,8 @@ struct tuner_state {
int min_lat;
int max_lat;
int prev_mean;
int prev_sqs;
int cur_sqs;
long long prev_sqs;
long long cur_sqs;
unsigned int sum;
unsigned int cur_samples;
unsigned int max_samples;
......@@ -435,7 +435,7 @@ static inline void build_score(struct gravity_tuner *tuner, int step)
n = state->cur_samples;
tuner->scores[step].pmean = state->sum / n;
variance = n > 1 ? state->cur_sqs / (n - 1) : 0;
variance = n > 1 ? xnarch_llimd(state->cur_sqs, 1, n - 1) : 0;
tuner->scores[step].stddev = int_sqrt(variance);
tuner->scores[step].minlat = state->min_lat;
tuner->scores[step].gravity = tuner->get_gravity(tuner);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment