357-mac80211-optimize-skb-resizing.patch 7.0 KB

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