1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sat, 28 May 2022 16:44:53 +0200
3 Subject: [PATCH] mac80211: fix overflow issues in airtime fairness code
5 The airtime weight calculation overflows with a default weight value of 256
6 whenever more than 8ms worth of airtime is reported.
7 Bigger weight values impose even smaller limits on maximum airtime values.
8 This can mess up airtime based calculations for drivers that don't report
9 per-PPDU airtime values, but batch up values instead.
11 Fix this by reordering multiplications/shifts and by reducing unnecessary
12 intermediate precision (which was lost in a later stage anyway).
14 The new shift value limits the maximum weight to 4096, which should be more
15 than enough. Any values bigger than that will be clamped to the upper limit.
17 Signed-off-by: Felix Fietkau <nbd@nbd.name>
20 --- a/net/mac80211/ieee80211_i.h
21 +++ b/net/mac80211/ieee80211_i.h
22 @@ -1666,50 +1666,34 @@ static inline struct airtime_info *to_ai
23 /* To avoid divisions in the fast path, we keep pre-computed reciprocals for
24 * airtime weight calculations. There are two different weights to keep track
25 * of: The per-station weight and the sum of weights per phy.
27 - * For the per-station weights (kept in airtime_info below), we use 32-bit
28 - * reciprocals with a devisor of 2^19. This lets us keep the multiplications and
29 - * divisions for the station weights as 32-bit operations at the cost of a bit
30 - * of rounding error for high weights; but the choice of divisor keeps rounding
31 - * errors <10% for weights <2^15, assuming no more than 8ms of airtime is
32 - * reported at a time.
34 - * For the per-phy sum of weights the values can get higher, so we use 64-bit
35 - * operations for those with a 32-bit divisor, which should avoid any
36 - * significant rounding errors.
37 + * The per-sta shift value supports weight values of 1-4096
39 -#define IEEE80211_RECIPROCAL_DIVISOR_64 0x100000000ULL
40 -#define IEEE80211_RECIPROCAL_SHIFT_64 32
41 -#define IEEE80211_RECIPROCAL_DIVISOR_32 0x80000U
42 -#define IEEE80211_RECIPROCAL_SHIFT_32 19
43 +#define IEEE80211_RECIPROCAL_SHIFT_SUM 24
44 +#define IEEE80211_RECIPROCAL_SHIFT_STA 12
45 +#define IEEE80211_WEIGHT_SHIFT 8
47 -static inline void airtime_weight_set(struct airtime_info *air_info, u16 weight)
48 +static inline void airtime_weight_set(struct airtime_info *air_info, u32 weight)
50 + weight = min_t(u32, weight, BIT(IEEE80211_RECIPROCAL_SHIFT_STA));
51 if (air_info->weight == weight)
54 air_info->weight = weight;
56 - air_info->weight_reciprocal =
57 - IEEE80211_RECIPROCAL_DIVISOR_32 / weight;
59 - air_info->weight_reciprocal = 0;
62 + weight = BIT(IEEE80211_RECIPROCAL_SHIFT_STA) / weight;
63 + air_info->weight_reciprocal = weight;
66 static inline void airtime_weight_sum_set(struct airtime_sched_info *air_sched,
70 if (air_sched->weight_sum == weight_sum)
73 air_sched->weight_sum = weight_sum;
74 - if (air_sched->weight_sum) {
75 - air_sched->weight_sum_reciprocal = IEEE80211_RECIPROCAL_DIVISOR_64;
76 - do_div(air_sched->weight_sum_reciprocal, air_sched->weight_sum);
78 - air_sched->weight_sum_reciprocal = 0;
81 + weight_sum = BIT(IEEE80211_RECIPROCAL_SHIFT_SUM) / weight_sum;
82 + air_sched->weight_sum_reciprocal = weight_sum;
85 /* A problem when trying to enforce airtime fairness is that we want to divide
86 --- a/net/mac80211/sta_info.c
87 +++ b/net/mac80211/sta_info.c
88 @@ -1894,9 +1894,9 @@ void ieee80211_register_airtime(struct i
90 struct ieee80211_sub_if_data *sdata = vif_to_sdata(txq->vif);
91 struct ieee80211_local *local = sdata->local;
92 - u64 weight_sum, weight_sum_reciprocal;
93 struct airtime_sched_info *air_sched;
94 struct airtime_info *air_info;
95 + u64 weight_sum_reciprocal;
98 air_sched = &local->airtime[txq->ac];
99 @@ -1907,27 +1907,21 @@ void ieee80211_register_airtime(struct i
100 if (local->airtime_flags & AIRTIME_USE_RX)
101 airtime += rx_airtime;
103 - /* Weights scale so the unit weight is 256 */
106 spin_lock_bh(&air_sched->lock);
108 air_info->tx_airtime += tx_airtime;
109 air_info->rx_airtime += rx_airtime;
111 - if (air_sched->weight_sum) {
112 - weight_sum = air_sched->weight_sum;
113 + if (air_sched->weight_sum)
114 weight_sum_reciprocal = air_sched->weight_sum_reciprocal;
116 - weight_sum = air_info->weight;
118 weight_sum_reciprocal = air_info->weight_reciprocal;
121 /* Round the calculation of global vt */
122 - air_sched->v_t += (u64)((airtime + (weight_sum >> 1)) *
123 - weight_sum_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_64;
124 - air_info->v_t += (u32)((airtime + (air_info->weight >> 1)) *
125 - air_info->weight_reciprocal) >> IEEE80211_RECIPROCAL_SHIFT_32;
126 + air_sched->v_t += ((u64)airtime * weight_sum_reciprocal) >>
127 + (IEEE80211_RECIPROCAL_SHIFT_SUM - IEEE80211_WEIGHT_SHIFT);
128 + air_info->v_t += (airtime * air_info->weight_reciprocal) >>
129 + (IEEE80211_RECIPROCAL_SHIFT_STA - IEEE80211_WEIGHT_SHIFT);
130 ieee80211_resort_txq(&local->hw, txq);
132 spin_unlock_bh(&air_sched->lock);