mac80211: Switch to a virtual time-based airtime scheduler
PATCH v5 1/1 mac80211: Switch to a virtual time-based airtime scheduler
This switches the airtime scheduler in mac80211 to use a virtual time-based scheduler instead of the round-robin scheduler used before. This has a couple of advantages:
- No need to sync up the round-robin scheduler in firmware/hardware with the round-robin airtime scheduler.
- If several stations are eligible for transmission we can schedule both of them; no need to hard-block the scheduling rotation until the head of the queue has used up its quantum.
- The check of whether a station is eligible for transmission becomes simpler (in ieee80211_txq_may_transmit()).
The drawback is that scheduling becomes slightly more expensive, as we need to maintain an rbtree of TXQs sorted by virtual time. This means that ieee80211_register_airtime() becomes O(logN) in the number of currently scheduled TXQs. However, hopefully this number rarely grows too big (it's only TXQs currently backlogged, not all associated stations), so it shouldn't be too big of an issue.
---
This version resolves the two outstanding issues Felix pointed out back at the RFC: The risk that the vif TXQ will always be scheduled first, and the division in the fast path. This means that as far as I'm concerned, this patch is now "feature complete". Didn't have a chance to do anything other than compile-test it yet, but wanted to get it out before the holidays (which I almost managed, since technically my holiday started two days ago)...
Full changelog:
Changes since v4:
Rebase on latest mac80211-next.
Squash everything into a single patch again, and add back Yibo's co-developed-by tag and signoff.
Move all the per-ac airtime-related data into separate structs.
Use pre-calculated reciprocals to avoid divisions in the fast path.
Record airtime usage for the vif TXQ to avoid it always being scheduled first.
Changes since v3:
Change schedule_pos to previous node once it has chance to be moved/removed from current position in the tree in loop scenario and bring back schedule_round in case that same node is to be scheduled again in the mean time.
Increase airtime grace period to 2000 us in the first patch.
Put per-AC station weight checking in its lock during configuration from application.
Changes since v2:
Changes station airtime weight to be per-AC based to avoid sync issue Remove Co-developed-by and Toke's sign-off as Toke suggested
Changes since v1:
Modify the author of Co-developed-by as Johannes suggested
include/net/mac80211.h | 17 +-- net/mac80211/cfg.c | 30 +++- net/mac80211/debugfs.c | 72 +++++++-- net/mac80211/debugfs_sta.c | 24 +-- net/mac80211/ieee80211_i.h | 73 ++++++++- net/mac80211/iface.c | 3 + net/mac80211/main.c | 10 +- net/mac80211/rx.c | 6 +- net/mac80211/sta_info.c | 69 ++++++--- net/mac80211/sta_info.h | 13 +- net/mac80211/status.c | 17 +++ net/mac80211/tx.c | 296 +++++++++++++++++++++++-------------- 12 files changed, 438 insertions(+), 192 deletions(-)
include/net/mac80211.h +3 -14
@@ -6362,9 +6362,6 @@ static inline void ieee80211_txq_schedule_end(struct ieee80211_hw *hw, u8 ac) { } -void __ieee80211_schedule_txq (struct ieee80211_hw *hw, -struct ieee80211_txq *txq,bool force); -/** * ieee80211_schedule_txq - schedule a TXQ for transmission *
@@ -6377,11 +6374,7 @@ void __ieee80211_schedule_txq(struct ieee80211_hw *hw,* The driver may call this function if it has buffered packets for * this TXQ internally. */ -static inline void -ieee80211_schedule_txq (struct ieee80211_hw *hw,struct ieee80211_txq *txq) -{ -__ieee80211_schedule_txq (hw, txq, true); -} +void ieee80211_schedule_txq (struct ieee80211_hw *hw,struct ieee80211_txq *txq);/** * ieee80211_return_txq - return a TXQ previously acquired by ieee80211_next_txq()
@@ -6393,12 +6386,8 @@ ieee80211_schedule_txq(struct ieee80211_hw *hw, struct ieee80211_txq *txq)* The driver may set force=true if it has buffered packets for this TXQ * internally. */ -static inline void -ieee80211_return_txq (struct ieee80211_hw *hw,struct ieee80211_txq *txq, -bool force) -{ -__ieee80211_schedule_txq (hw, txq, force); -} +void ieee80211_return_txq (struct ieee80211_hw *hw,struct ieee80211_txq *txq, +bool force);/** * ieee80211_txq_may_transmit - check whether TXQ is allowed to transmit
net/mac80211/cfg.c +27 -3
@@ -1333,7 +1333,8 @@ static int sta_apply_parameters(struct ieee80211_local *local,int ret =0 ;struct ieee80211_supported_band *sband;struct ieee80211_sub_if_data *sdata = sta->sdata; -u32 mask, set; +u32 mask, set, tid, ac, old_weight; +struct txq_info *txqi; sband =ieee80211_get_sband (sdata);if (!sband)
@@ -1510,8 +1511,31 @@ static int sta_apply_parameters(struct ieee80211_local *local,if (ieee80211_vif_is_mesh (&sdata->vif))sta_apply_mesh_params (local, sta, params); -if (params->airtime_weight) - sta->airtime_weight = params->airtime_weight; +if (params->airtime_weight) { +for (ac =0 ; ac <IEEE80211_NUM_ACS ; ac++) { +struct airtime_sched_info *air_sched = &local->airtime[ac]; +struct airtime_info *air_info = &sta->airtime[ac]; + +spin_lock_bh (&air_sched->lock); +for (tid =0 ; tid <IEEE80211_NUM_TIDS +1 ; tid++) { +if (air_info->weight == params->airtime_weight || + !sta->sta.txq[tid] || + ac !=ieee80211_ac_from_tid (tid)) +continue ; + + old_weight = air_info->weight; +airtime_weight_set (air_info, params->airtime_weight); + + txqi =to_txq_info (sta->sta.txq[tid]); +if (RB_EMPTY_NODE (&txqi->schedule_order)) +continue ; + +airtime_weight_add (air_sched, + params->airtime_weight - old_weight); + } +spin_unlock_bh (&air_sched->lock); + } + }/* set the STA state after all sta info from usermode has been set */ if (test_sta_flag(sta, WLAN_STA_TDLS_PEER) ||
net/mac80211/debugfs.c +58 -14
97 lines not on this page · raw message
net/mac80211/debugfs_sta.c +12 -12
@@ -200,7 +200,7 @@ static ssize_t sta_airtime_read(struct file *file, char __user *userbuf,size_t bufsz =400 ;char *buf =kzalloc (bufsz,GFP_KERNEL ), *p = buf;u64 rx_airtime =0 , tx_airtime =0 ; -s64 deficit[IEEE80211_NUM_ACS ]; +u64 v_t[IEEE80211_NUM_ACS ];ssize_t rv;int ac;
@@ -208,18 +208,18 @@ static ssize_t sta_airtime_read(struct file *file, char __user *userbuf,return -ENOMEM ;for (ac =0 ; ac <IEEE80211_NUM_ACS ; ac++) { -spin_lock_bh (&local->active_txq_lock[ac]); +spin_lock_bh (&local->airtime[ac].lock); rx_airtime += sta->airtime[ac].rx_airtime; tx_airtime += sta->airtime[ac].tx_airtime; - deficit[ac] = sta->airtime[ac].deficit; -spin_unlock_bh (&local->active_txq_lock[ac]); + v_t[ac] = sta->airtime[ac].v_t; +spin_unlock_bh (&local->airtime[ac].lock); } p +=scnprintf (p, bufsz + buf - p,"RX: %llu us\nTX: %llu us\nWeight: %u\n" -"Deficit: VO: %lld us VI: %lld us BE: %lld us BK: %lld us\n" , - rx_airtime, tx_airtime, sta->airtime_weight, - deficit[0 ], deficit[1 ], deficit[2 ], deficit[3 ]); +"Virt-T: VO: %lld us VI: %lld us BE: %lld us BK: %lld us\n" , + rx_airtime, tx_airtime, sta->airtime[0 ].weight, + v_t[0 ], v_t[1 ], v_t[2 ], v_t[3 ]); rv =simple_read_from_buffer (userbuf, count, ppos, buf, p - buf);kfree (buf);
@@ -234,11 +234,11 @@ static ssize_t sta_airtime_write(struct file *file, const char __user *userbuf,int ac;for (ac =0 ; ac <IEEE80211_NUM_ACS ; ac++) { -spin_lock_bh (&local->active_txq_lock[ac]); +spin_lock_bh (&local->airtime[ac].lock); sta->airtime[ac].rx_airtime =0 ; sta->airtime[ac].tx_airtime =0 ; - sta->airtime[ac].deficit = sta->airtime_weight; -spin_unlock_bh (&local->active_txq_lock[ac]); + sta->airtime[ac].v_t =0 ; +spin_unlock_bh (&local->airtime[ac].lock); }return count;
@@ -261,10 +261,10 @@ static ssize_t sta_aql_read(struct file *file, char __user *userbuf,return -ENOMEM ;for (ac =0 ; ac <IEEE80211_NUM_ACS ; ac++) { -spin_lock_bh (&local->active_txq_lock[ac]); +spin_lock_bh (&local->airtime[ac].lock); q_limit_l[ac] = sta->airtime[ac].aql_limit_low; q_limit_h[ac] = sta->airtime[ac].aql_limit_high; -spin_unlock_bh (&local->active_txq_lock[ac]); +spin_unlock_bh (&local->airtime[ac].lock); q_depth[ac] =atomic_read (&sta->airtime[ac].aql_tx_pending); }
net/mac80211/ieee80211_i.h +66 -7
110 lines not on this page · raw message
net/mac80211/iface.c +3 -0
@@ -1922,6 +1922,9 @@ int ieee80211_if_add(struct ieee80211_local *local, const char *name, } } +for (i =0 ; i <IEEE80211_NUM_ACS ; i++) +init_airtime_info (&sdata->airtime[i], &local->airtime[i]); +ieee80211_set_default_queues (sdata); sdata->ap_power_level =IEEE80211_UNSET_POWER_LEVEL ;
net/mac80211/main.c +6 -4
@@ -665,10 +665,12 @@ struct ieee80211_hw *ieee80211_alloc_hw_nm(size_t priv_data_len,spin_lock_init (&local->queue_stop_reason_lock);for (i =0 ; i <IEEE80211_NUM_ACS ; i++) { -INIT_LIST_HEAD (&local->active_txqs[i]); -spin_lock_init (&local->active_txq_lock[i]); - local->aql_txq_limit_low[i] =IEEE80211_DEFAULT_AQL_TXQ_LIMIT_L ; - local->aql_txq_limit_high[i] = +struct airtime_sched_info *air_sched = &local->airtime[i]; + + air_sched->active_txqs =RB_ROOT_CACHED ; +spin_lock_init (&air_sched->lock); + air_sched->aql_txq_limit_low =IEEE80211_DEFAULT_AQL_TXQ_LIMIT_L ; + air_sched->aql_txq_limit_high =IEEE80211_DEFAULT_AQL_TXQ_LIMIT_H ; }
net/mac80211/rx.c +1 -5
@@ -1586,12 +1586,8 @@ static void sta_ps_start(struct sta_info *sta)for (tid =0 ; tid <IEEE80211_NUM_TIDS ; tid++) {struct ieee80211_txq *txq = sta->sta.txq[tid]; -struct txq_info *txqi =to_txq_info (txq); -spin_lock (&local->active_txq_lock[txq->ac]); -if (!list_empty (&txqi->schedule_order)) -list_del_init (&txqi->schedule_order); -spin_unlock (&local->active_txq_lock[txq->ac]); +ieee80211_unschedule_txq (&local->hw, txq);if (txq_has_queue (txq))set_bit (tid, &sta->txq_buffered_tids);
net/mac80211/sta_info.c +50 -19
102 lines not on this page · raw message
net/mac80211/sta_info.h +10 -3
@@ -127,19 +127,27 @@ enum ieee80211_agg_stop_reason {/* Debugfs flags to enable/disable use of RX/TX airtime in scheduler */ #define AIRTIME_USE_TX BIT(0)#define AIRTIME_USE_RX BIT(1) +#define AIRTIME_GRACE 2000/* usec of grace period before reset */ + +#define IEEE80211_RECIPROCAL_DIVISOR 0x100000000ULL +#define IEEE80211_RECIPROCAL_SHIFT 32struct airtime_info {u64 rx_airtime;u64 tx_airtime; -s64 deficit; +u64 v_t;atomic_t aql_tx_pending;/* Estimated airtime for frames pending */ u32 aql_limit_low;u32 aql_limit_high; +u64 weight_reciprocal; +u16 weight; };void ieee80211_sta_update_pending_airtime (struct ieee80211_local *local,struct sta_info *sta,u8 ac,u16 tx_airtime,bool tx_completed); +void ieee80211_register_airtime (struct ieee80211_txq *txq, +u32 tx_airtime,u32 rx_airtime);struct sta_info ;
@@ -480,7 +488,6 @@ struct ieee80211_sta_rx_stats {* @tid_seq: per-TID sequence numbers for sending to this STA * @airtime: per-AC struct airtime_info describing airtime statistics for this * station -* @airtime_weight: station weight for airtime fairness calculation purposes * @ampdu_mlme: A-MPDU state machine state * @mesh: mesh STA information * @debugfs_dir: debug filesystem directory dentry
@@ -525,6 +532,7 @@ struct ieee80211_sta_rx_stats {* @status_stats.ack_signal_filled: last ACK signal validity * @status_stats.avg_ack_signal: average ACK signal */ +struct sta_info {/* General information, mostly static */ struct list_head list, free_list;
@@ -607,7 +615,6 @@ struct sta_info {u16 tid_seq[IEEE80211_QOS_CTL_TID_MASK +1 ];struct airtime_info airtime[IEEE80211_NUM_ACS ]; -u16 airtime_weight;/* * Aggregation information, locked with lock.
net/mac80211/status.c +17 -0
@@ -1025,6 +1025,23 @@ static void __ieee80211_tx_status(struct ieee80211_hw *hw,ieee80211_lost_packet (sta, info); } } + }else if (wiphy_ext_feature_isset (local->hw.wiphy, +NL80211_EXT_FEATURE_AIRTIME_FAIRNESS )) { +struct ieee80211_sub_if_data *sdata; +struct ieee80211_txq *txq; +u32 airtime; + + sdata =ieee80211_sdata_from_skb (local, skb); + +if (sdata && (txq = sdata->vif.txq)) { + airtime = info->status.tx_time ?: +ieee80211_calc_expected_tx_airtime (hw, + &sdata->vif, +NULL , + skb->len); + +ieee80211_register_airtime (txq, airtime,0 ); + } }/* SNMP counters
net/mac80211/tx.c +185 -111
376 lines not on this page · raw message
Signature · 1 line
2.24.1
Johannes Berg
maintainerQuoted 4 lines · Toke Høiland-Jørgensen
Didn't have a chance to
do anything other than compile-test it yet, but wanted to get it out before the holidays (which I almost managed, since technically my holiday started two days ago)...
Didn't help you much, I at least was already on vacation by then too :P
net/mac80211/sta_info.c void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, · line 1981
Quoted 5 lines
net/mac80211/sta_info.c +1
-0@@ -1948,6 +1978,7 @@ void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, tx_pending, 0); } +
nit, what's that doing here? :)
net/mac80211/sta_info.h enum ieee80211_agg_stop_reason { · line 133
Quoted 2 lines
net/mac80211/sta_info.h +2
-0+#define IEEE80211_RECIPROCAL_DIVISOR 0x100000000ULL +#define IEEE80211_RECIPROCAL_SHIFT 32
Could we live with less precision and use 32-bit arithmetic only? That might help 32-bit systems?
This is basically a 32.32 (31.32 for signed) fixed point number, right? So I guess I'm asking if we could live with 16.16 (or 15.16), or similar.
net/mac80211/sta_info.h struct ieee80211_sta_rx_stats { · line 535
Quoted 5 lines
net/mac80211/sta_info.h +1
-0@@ -525,6 +532,7 @@ struct ieee80211_sta_rx_stats {* @status_stats.ack_signal_filled: last ACK signal validity * @status_stats.avg_ack_signal: average ACK signal */ +
another pointless blank line
net/mac80211/tx.c EXPORT_SYMBOL(ieee80211_txq_airtime_check); · line 3935
Quoted 2 lines
net/mac80211/tx.c +2
-0+ air_info =to_airtime_info (&txqi->txq); + ret = (air_info->v_t <= air_sched->v_t);
no need for parentheses :)
I think overall this looks good. I guess you should subject it to some testing since I can't.
johannes
Quoted 8 lines · Johannes Berg
Quoted 4 lines · Toke Høiland-Jørgensen
Didn't have a chance to
do anything other than compile-test it yet, but wanted to get it out before the holidays (which I almost managed, since technically my holiday started two days ago)...Didn't help you much, I at least was already on vacation by then too :P
Yeah, well, I did say "almost". But at least it gave *me* peace of mind over the holidays :)
Quoted 7 lines
net/mac80211/sta_info.c void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, · line 1981
Quoted 5 lines
net/mac80211/sta_info.c +1
-0@@ -1948,6 +1978,7 @@ void ieee80211_sta_update_pending_airtime(struct ieee80211_local *local, tx_pending, 0); } +nit, what's that doing here? :)
Pining for the fjords?
Quoted 9 lines
net/mac80211/sta_info.h enum ieee80211_agg_stop_reason { · line 133
Quoted 2 lines
net/mac80211/sta_info.h +2
-0+#define IEEE80211_RECIPROCAL_DIVISOR 0x100000000ULL +#define IEEE80211_RECIPROCAL_SHIFT 32Could we live with less precision and use 32-bit arithmetic only? That might help 32-bit systems?
This is basically a 32.32 (31.32 for signed) fixed point number, right? So I guess I'm asking if we could live with 16.16 (or 15.16), or similar.
Hmm, not sure. For the per-station weights, probably; I expect that in most cases individual station weights won't be big enough to cause rounding. However, the weight sum is a different matter. We go above a 10% rounding error once that goes above 2^13, which is certainly not unrealistic. The worst-case error is 50% if the weight sum happens to land at 2^15+1.
The impact of a rounding error ends up being that a station's next transmission is delayed longer than it should be. As long as the rounding error is constant (i.e., the same set of stations keeps being active), this should cancel out, I guess; but since stations tend to cycle between being active and not, I fear it could end up impacting fairness to a measurable degree.
So IDK; we could say we'll live with this in the interest of performance? Or we could decide the performance hit is worth keeping precision? Or do a middle ground thing where we use 32-bit arithmetic for the per-station weights, but go to 64-bit for the weight sum? I don't really have a good grip on how much of a performance impact we're talking about here, so I'm not sure which I prefer...
Quoted 2 lines
I think overall this looks good. I guess you should subject it to some testing since I can't.
Heh, yeah, testing is definitely needed :)
I'm hoping Yibo will take it for a spin. If not, I'll try to see if I can get my old testbed to work; but I seem to recall there being a hardware issue with it, and I don't have physical access anymore, so it may be beyond rescue...
-Toke
Quoted 4 lines · Toke Høiland-Jørgensen
Or do a middle ground thing where we use 32-bit arithmetic for the per-station weights, but go to 64-bit for the weight sum? I don't really have a good grip on how much of a performance impact we're talking about here, so I'm not sure which I prefer...
Double width accumulation is very common in many applications. Double width addition and comparison are _much_ cheaper than double width multiplication and division.
/john
Quoted 9 lines · John Yates
Quoted 4 lines · Toke Høiland-Jørgensen
Or do a middle ground thing where we use 32-bit arithmetic for the per-station weights, but go to 64-bit for the weight sum? I don't really have a good grip on how much of a performance impact we're talking about here, so I'm not sure which I prefer...
Double width accumulation is very common in many applications. Double width addition and comparison are _much_ cheaper than double width multiplication and division.
Yeah, we'd be doing the accumulation in 64bit values in any case; we're talking about mainly multiplication here (the whole point of the reciprocal stuff is to get the division out of the fast path). So how big of an impact is one (or two) extra 64-bit multiplications going to have on a 32bit platform?
-Toke
Quoted 5 lines · Toke Høiland-Jørgensen
Yeah, we'd be doing the accumulation in 64bit values in any case; we're talking about mainly multiplication here (the whole point of the reciprocal stuff is to get the division out of the fast path). So how big of an impact is one (or two) extra 64-bit multiplications going to have on a 32bit platform?
Top line: usually replacing 64 bit divide with multiply is a massive win.
Many platforms make (32 bits * 32 bits) -> 64 bits quite cheap:
- x86 has this as a single instruction: eax * edx -> eax:edx
- arm has much the same, plus a variant that tacks ona 64 bit accumulation!
- mips leaves the 64 bit product in a dedicated register; retrieval requires 2 instructions
- ppc, being more "RISCy", has two instruction: mullo and mulhi (performs multiply twice!)
Best case is when the compiler can recognize a 64 bit multiply as really
widen_32_to_64(left) x widen_32_to_64(right) -> 64_bit_product
In such a case only one of the above multiply cases is necessary. Otherwise one tends to get multiple partial products and double width additions. Still, better than nearly any flavor of 64 bit divide.
/john
Quoted 10 lines · John Yates
Quoted 5 lines · Toke Høiland-Jørgensen
Yeah, we'd be doing the accumulation in 64bit values in any case; we're talking about mainly multiplication here (the whole point of the reciprocal stuff is to get the division out of the fast path). So how big of an impact is one (or two) extra 64-bit multiplications going to have on a 32bit platform?
Top line: usually replacing 64 bit divide with multiply is a massive win.
Quoted 7 lines
Many platforms make (32 bits * 32 bits) -> 64 bits quite cheap:
- x86 has this as a single instruction: eax * edx -> eax:edx
- arm has much the same, plus a variant that tacks ona 64 bit accumulation!
- mips leaves the 64 bit product in a dedicated register; retrieval requires 2 instructions
- ppc, being more "RISCy", has two instruction: mullo and mulhi (performs multiply twice!)
Ah, this is very useful, thanks :)
Quoted 7 lines
Best case is when the compiler can recognize a 64 bit multiply as really
widen_32_to_64(left)xwiden_32_to_64(right)-> 64_bit_productIn such a case only one of the above multiply cases is necessary. Otherwise one tends to get multiple partial products and double width additions. Still, better than nearly any flavor of 64 bit divide.
So going back to the original patch, we don't really need to use 64-bit divides to compute the reciprocals; not sure what I was thinking there. That leaves us with a single 32-bit divide whenever a station is scheduled or unscheduled, and two 64-bit multiplications in ieee80211_register_airtime().
If we assume no more than 8ms of airtime is being reported at a time, we can use 2^19 as the divisor and keep the multiplication in 32 bits without overflowing, which would keep the rounding error <10% for weights <2^15. This should be enough for single-station weights, at least. I think it could also be sufficient for the weight_sum for most uses, actually, so we could start out with that and only revert to 64-bit multiplication if it turns out people are pushing the weighted fairness stuff to a point where this breaks?
Johannes, WDYT? Also, what is a good place to document this?
-Toke