Skip to contentSkip to conversation

drivers/autotune: Fix UBSAN warnings

PATCH New Jan Kiszka 1 participant 1 message Jul 26, 2019

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH v3 1/1 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.

---
 kernel/drivers/autotune/autotune.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/drivers/autotune/autotune.c +3 -3
index 2629bf9d47..77ec9a1a21 100644
@@ -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);
Signature · 1 line
2.16.4