mac80211: Update to version 5.4.36-1
[oweals/openwrt.git] / package / kernel / mac80211 / patches / subsys / 300-mac80211-optimize-skb-resizing.patch
1 From: Felix Fietkau <nbd@nbd.name>
2 Date: Sun, 17 Mar 2019 18:11:30 +0100
3 Subject: [PATCH] mac80211: optimize skb resizing
4
5 When forwarding unicast packets from ethernet to batman-adv over 802.11s
6 (with forwarding disabled), the typical required headroom to transmit
7 encrypted packets on mt76 is 32 (802.11) + 6 (802.11s) + 8 (CCMP) +
8 2 (padding) + 6 (LLC) + 18 (batman-adv) - 14 (old ethernet header) = 58 bytes.
9
10 On systems where NET_SKB_PAD is 64 this leads to a call to pskb_expand_head
11 for every packet, since mac80211 also tries to allocate 16 bytes status
12 headroom for radiotap headers.
13
14 This patch fixes these unnecessary reallocations by only requiring the extra
15 status headroom in ieee80211_tx_monitor()
16 If however a reallocation happens before that call, the status headroom gets
17 added there as well, in order to avoid double reallocation.
18
19 The patch also cleans up the code by moving the headroom calculation to
20 ieee80211_skb_resize.
21
22 Signed-off-by: Felix Fietkau <nbd@nbd.name>
23 ---
24
25 --- a/net/mac80211/ieee80211_i.h
26 +++ b/net/mac80211/ieee80211_i.h
27 @@ -1782,6 +1782,9 @@ int ieee80211_tx_control_port(struct wip
28                               const u8 *dest, __be16 proto, bool unencrypted);
29  int ieee80211_probe_mesh_link(struct wiphy *wiphy, struct net_device *dev,
30                               const u8 *buf, size_t len);
31 +int ieee80211_skb_resize(struct ieee80211_local *local,
32 +                        struct ieee80211_sub_if_data *sdata,
33 +                        struct sk_buff *skb, int hdrlen, int hdr_add);
34  
35  /* HT */
36  void ieee80211_apply_htcap_overrides(struct ieee80211_sub_if_data *sdata,
37 --- a/net/mac80211/status.c
38 +++ b/net/mac80211/status.c
39 @@ -815,6 +815,11 @@ void ieee80211_tx_monitor(struct ieee802
40         struct net_device *prev_dev = NULL;
41         int rtap_len;
42  
43 +       if (ieee80211_skb_resize(local, NULL, skb, 0, 0)) {
44 +               dev_kfree_skb(skb);
45 +               return;
46 +       }
47 +
48         /* send frame to monitor interfaces now */
49         rtap_len = ieee80211_tx_radiotap_len(info, status);
50         if (WARN_ON_ONCE(skb_headroom(skb) < rtap_len)) {
51 --- a/net/mac80211/tx.c
52 +++ b/net/mac80211/tx.c
53 @@ -1943,37 +1943,53 @@ static bool ieee80211_tx(struct ieee8021
54  }
55  
56  /* device xmit handlers */
57 -
58 -static int ieee80211_skb_resize(struct ieee80211_sub_if_data *sdata,
59 -                               struct sk_buff *skb,
60 -                               int head_need, bool may_encrypt)
61 +int ieee80211_skb_resize(struct ieee80211_local *local,
62 +                        struct ieee80211_sub_if_data *sdata,
63 +                        struct sk_buff *skb, int hdr_len, int hdr_extra)
64  {
65 -       struct ieee80211_local *local = sdata->local;
66 +       struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
67         struct ieee80211_hdr *hdr;
68 -       bool enc_tailroom;
69 -       int tail_need = 0;
70 -
71 -       hdr = (struct ieee80211_hdr *) skb->data;
72 -       enc_tailroom = may_encrypt &&
73 -                      (sdata->crypto_tx_tailroom_needed_cnt ||
74 -                       ieee80211_is_mgmt(hdr->frame_control));
75 -
76 -       if (enc_tailroom) {
77 -               tail_need = IEEE80211_ENCRYPT_TAILROOM;
78 -               tail_need -= skb_tailroom(skb);
79 -               tail_need = max_t(int, tail_need, 0);
80 +       int head_need, head_max;
81 +       int tail_need, tail_max;
82 +       bool enc_tailroom = false;
83 +
84 +       if (sdata && !hdr_len &&
85 +           !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT)) {
86 +               hdr = (struct ieee80211_hdr *) skb->data;
87 +               enc_tailroom = (sdata->crypto_tx_tailroom_needed_cnt ||
88 +                               ieee80211_is_mgmt(hdr->frame_control));
89 +               hdr_len += sdata->encrypt_headroom;
90 +       }
91 +
92 +       head_need = head_max = hdr_len;
93 +       tail_need = tail_max = 0;
94 +       if (!sdata) {
95 +               head_need = head_max = local->tx_headroom;
96 +       } else {
97 +               head_max += hdr_extra;
98 +               head_max += max_t(int, local->tx_headroom,
99 +                                 local->hw.extra_tx_headroom);
100 +               head_need += local->hw.extra_tx_headroom;
101 +
102 +               tail_max = IEEE80211_ENCRYPT_TAILROOM;
103 +               if (enc_tailroom)
104 +                       tail_need = tail_max;
105         }
106  
107         if (skb_cloned(skb) &&
108             (!ieee80211_hw_check(&local->hw, SUPPORTS_CLONED_SKBS) ||
109              !skb_clone_writable(skb, ETH_HLEN) || enc_tailroom))
110                 I802_DEBUG_INC(local->tx_expand_skb_head_cloned);
111 -       else if (head_need || tail_need)
112 +       else if (head_need > skb_headroom(skb) ||
113 +                tail_need > skb_tailroom(skb))
114                 I802_DEBUG_INC(local->tx_expand_skb_head);
115         else
116                 return 0;
117  
118 -       if (pskb_expand_head(skb, head_need, tail_need, GFP_ATOMIC)) {
119 +       head_max = max_t(int, 0, head_max - skb_headroom(skb));
120 +       tail_max = max_t(int, 0, tail_max - skb_tailroom(skb));
121 +
122 +       if (pskb_expand_head(skb, head_max, tail_max, GFP_ATOMIC)) {
123                 wiphy_debug(local->hw.wiphy,
124                             "failed to reallocate TX buffer\n");
125                 return -ENOMEM;
126 @@ -1989,18 +2005,8 @@ void ieee80211_xmit(struct ieee80211_sub
127         struct ieee80211_local *local = sdata->local;
128         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
129         struct ieee80211_hdr *hdr;
130 -       int headroom;
131 -       bool may_encrypt;
132 -
133 -       may_encrypt = !(info->flags & IEEE80211_TX_INTFL_DONT_ENCRYPT);
134  
135 -       headroom = local->tx_headroom;
136 -       if (may_encrypt)
137 -               headroom += sdata->encrypt_headroom;
138 -       headroom -= skb_headroom(skb);
139 -       headroom = max_t(int, 0, headroom);
140 -
141 -       if (ieee80211_skb_resize(sdata, skb, headroom, may_encrypt)) {
142 +       if (ieee80211_skb_resize(local, sdata, skb, 0, 0)) {
143                 ieee80211_free_txskb(&local->hw, skb);
144                 return;
145         }
146 @@ -2790,29 +2796,13 @@ static struct sk_buff *ieee80211_build_h
147         }
148  
149         skb_pull(skb, skip_header_bytes);
150 -       head_need = hdrlen + encaps_len + meshhdrlen - skb_headroom(skb);
151 +       head_need = hdrlen + encaps_len + meshhdrlen;
152  
153 -       /*
154 -        * So we need to modify the skb header and hence need a copy of
155 -        * that. The head_need variable above doesn't, so far, include
156 -        * the needed header space that we don't need right away. If we
157 -        * can, then we don't reallocate right now but only after the
158 -        * frame arrives at the master device (if it does...)
159 -        *
160 -        * If we cannot, however, then we will reallocate to include all
161 -        * the ever needed space. Also, if we need to reallocate it anyway,
162 -        * make it big enough for everything we may ever need.
163 -        */
164 -
165 -       if (head_need > 0 || skb_cloned(skb)) {
166 -               head_need += sdata->encrypt_headroom;
167 -               head_need += local->tx_headroom;
168 -               head_need = max_t(int, 0, head_need);
169 -               if (ieee80211_skb_resize(sdata, skb, head_need, true)) {
170 -                       ieee80211_free_txskb(&local->hw, skb);
171 -                       skb = NULL;
172 -                       return ERR_PTR(-ENOMEM);
173 -               }
174 +       if (ieee80211_skb_resize(local, sdata, skb, head_need,
175 +                                sdata->encrypt_headroom)) {
176 +               ieee80211_free_txskb(&local->hw, skb);
177 +               skb = NULL;
178 +               return ERR_PTR(-ENOMEM);
179         }
180  
181         if (encaps_data)
182 @@ -3427,7 +3417,6 @@ static bool ieee80211_xmit_fast(struct i
183         struct ieee80211_local *local = sdata->local;
184         u16 ethertype = (skb->data[12] << 8) | skb->data[13];
185         int extra_head = fast_tx->hdr_len - (ETH_HLEN - 2);
186 -       int hw_headroom = sdata->local->hw.extra_tx_headroom;
187         struct ethhdr eth;
188         struct ieee80211_tx_info *info;
189         struct ieee80211_hdr *hdr = (void *)fast_tx->hdr;
190 @@ -3479,10 +3468,7 @@ static bool ieee80211_xmit_fast(struct i
191          * as the may-encrypt argument for the resize to not account for
192          * more room than we already have in 'extra_head'
193          */
194 -       if (unlikely(ieee80211_skb_resize(sdata, skb,
195 -                                         max_t(int, extra_head + hw_headroom -
196 -                                                    skb_headroom(skb), 0),
197 -                                         false))) {
198 +       if (unlikely(ieee80211_skb_resize(local, sdata, skb, extra_head, 0))) {
199                 kfree_skb(skb);
200                 return true;
201         }