1
0

080-12-fib_trie-Move-resize-to-after-inflate-halve.patch 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Wed, 31 Dec 2014 10:56:31 -0800
  3. Subject: [PATCH] fib_trie: Move resize to after inflate/halve
  4. This change consists of a cut/paste of resize to behind inflate and halve
  5. so that I could remove the two function prototypes.
  6. Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com>
  7. Signed-off-by: David S. Miller <davem@davemloft.net>
  8. ---
  9. --- a/net/ipv4/fib_trie.c
  10. +++ b/net/ipv4/fib_trie.c
  11. @@ -149,8 +149,6 @@ struct trie {
  12. static void tnode_put_child_reorg(struct tnode *tn, unsigned long i,
  13. struct tnode *n, int wasfull);
  14. static struct tnode *resize(struct trie *t, struct tnode *tn);
  15. -static struct tnode *inflate(struct trie *t, struct tnode *tn);
  16. -static struct tnode *halve(struct trie *t, struct tnode *tn);
  17. /* tnodes to free after resize(); protected by RTNL */
  18. static struct callback_head *tnode_free_head;
  19. static size_t tnode_free_size;
  20. @@ -447,161 +445,6 @@ static void put_child_root(struct tnode
  21. rcu_assign_pointer(t->trie, n);
  22. }
  23. -#define MAX_WORK 10
  24. -static struct tnode *resize(struct trie *t, struct tnode *tn)
  25. -{
  26. - struct tnode *old_tn, *n = NULL;
  27. - int inflate_threshold_use;
  28. - int halve_threshold_use;
  29. - int max_work;
  30. -
  31. - if (!tn)
  32. - return NULL;
  33. -
  34. - pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  35. - tn, inflate_threshold, halve_threshold);
  36. -
  37. - /* No children */
  38. - if (tn->empty_children > (tnode_child_length(tn) - 1))
  39. - goto no_children;
  40. -
  41. - /* One child */
  42. - if (tn->empty_children == (tnode_child_length(tn) - 1))
  43. - goto one_child;
  44. - /*
  45. - * Double as long as the resulting node has a number of
  46. - * nonempty nodes that are above the threshold.
  47. - */
  48. -
  49. - /*
  50. - * From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  51. - * the Helsinki University of Technology and Matti Tikkanen of Nokia
  52. - * Telecommunications, page 6:
  53. - * "A node is doubled if the ratio of non-empty children to all
  54. - * children in the *doubled* node is at least 'high'."
  55. - *
  56. - * 'high' in this instance is the variable 'inflate_threshold'. It
  57. - * is expressed as a percentage, so we multiply it with
  58. - * tnode_child_length() and instead of multiplying by 2 (since the
  59. - * child array will be doubled by inflate()) and multiplying
  60. - * the left-hand side by 100 (to handle the percentage thing) we
  61. - * multiply the left-hand side by 50.
  62. - *
  63. - * The left-hand side may look a bit weird: tnode_child_length(tn)
  64. - * - tn->empty_children is of course the number of non-null children
  65. - * in the current node. tn->full_children is the number of "full"
  66. - * children, that is non-null tnodes with a skip value of 0.
  67. - * All of those will be doubled in the resulting inflated tnode, so
  68. - * we just count them one extra time here.
  69. - *
  70. - * A clearer way to write this would be:
  71. - *
  72. - * to_be_doubled = tn->full_children;
  73. - * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
  74. - * tn->full_children;
  75. - *
  76. - * new_child_length = tnode_child_length(tn) * 2;
  77. - *
  78. - * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
  79. - * new_child_length;
  80. - * if (new_fill_factor >= inflate_threshold)
  81. - *
  82. - * ...and so on, tho it would mess up the while () loop.
  83. - *
  84. - * anyway,
  85. - * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
  86. - * inflate_threshold
  87. - *
  88. - * avoid a division:
  89. - * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
  90. - * inflate_threshold * new_child_length
  91. - *
  92. - * expand not_to_be_doubled and to_be_doubled, and shorten:
  93. - * 100 * (tnode_child_length(tn) - tn->empty_children +
  94. - * tn->full_children) >= inflate_threshold * new_child_length
  95. - *
  96. - * expand new_child_length:
  97. - * 100 * (tnode_child_length(tn) - tn->empty_children +
  98. - * tn->full_children) >=
  99. - * inflate_threshold * tnode_child_length(tn) * 2
  100. - *
  101. - * shorten again:
  102. - * 50 * (tn->full_children + tnode_child_length(tn) -
  103. - * tn->empty_children) >= inflate_threshold *
  104. - * tnode_child_length(tn)
  105. - *
  106. - */
  107. -
  108. - /* Keep root node larger */
  109. -
  110. - if (!node_parent(tn)) {
  111. - inflate_threshold_use = inflate_threshold_root;
  112. - halve_threshold_use = halve_threshold_root;
  113. - } else {
  114. - inflate_threshold_use = inflate_threshold;
  115. - halve_threshold_use = halve_threshold;
  116. - }
  117. -
  118. - max_work = MAX_WORK;
  119. - while ((tn->full_children > 0 && max_work-- &&
  120. - 50 * (tn->full_children + tnode_child_length(tn)
  121. - - tn->empty_children)
  122. - >= inflate_threshold_use * tnode_child_length(tn))) {
  123. -
  124. - old_tn = tn;
  125. - tn = inflate(t, tn);
  126. -
  127. - if (IS_ERR(tn)) {
  128. - tn = old_tn;
  129. -#ifdef CONFIG_IP_FIB_TRIE_STATS
  130. - this_cpu_inc(t->stats->resize_node_skipped);
  131. -#endif
  132. - break;
  133. - }
  134. - }
  135. -
  136. - /* Return if at least one inflate is run */
  137. - if (max_work != MAX_WORK)
  138. - return tn;
  139. -
  140. - /*
  141. - * Halve as long as the number of empty children in this
  142. - * node is above threshold.
  143. - */
  144. -
  145. - max_work = MAX_WORK;
  146. - while (tn->bits > 1 && max_work-- &&
  147. - 100 * (tnode_child_length(tn) - tn->empty_children) <
  148. - halve_threshold_use * tnode_child_length(tn)) {
  149. -
  150. - old_tn = tn;
  151. - tn = halve(t, tn);
  152. - if (IS_ERR(tn)) {
  153. - tn = old_tn;
  154. -#ifdef CONFIG_IP_FIB_TRIE_STATS
  155. - this_cpu_inc(t->stats->resize_node_skipped);
  156. -#endif
  157. - break;
  158. - }
  159. - }
  160. -
  161. -
  162. - /* Only one child remains */
  163. - if (tn->empty_children == (tnode_child_length(tn) - 1)) {
  164. - unsigned long i;
  165. -one_child:
  166. - for (i = tnode_child_length(tn); !n && i;)
  167. - n = tnode_get_child(tn, --i);
  168. -no_children:
  169. - /* compress one level */
  170. - node_set_parent(n, NULL);
  171. - tnode_free_safe(tn);
  172. - return n;
  173. - }
  174. - return tn;
  175. -}
  176. -
  177. -
  178. static void tnode_clean_free(struct tnode *tn)
  179. {
  180. struct tnode *tofree;
  181. @@ -804,6 +647,160 @@ nomem:
  182. return ERR_PTR(-ENOMEM);
  183. }
  184. +#define MAX_WORK 10
  185. +static struct tnode *resize(struct trie *t, struct tnode *tn)
  186. +{
  187. + struct tnode *old_tn, *n = NULL;
  188. + int inflate_threshold_use;
  189. + int halve_threshold_use;
  190. + int max_work;
  191. +
  192. + if (!tn)
  193. + return NULL;
  194. +
  195. + pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  196. + tn, inflate_threshold, halve_threshold);
  197. +
  198. + /* No children */
  199. + if (tn->empty_children > (tnode_child_length(tn) - 1))
  200. + goto no_children;
  201. +
  202. + /* One child */
  203. + if (tn->empty_children == (tnode_child_length(tn) - 1))
  204. + goto one_child;
  205. + /*
  206. + * Double as long as the resulting node has a number of
  207. + * nonempty nodes that are above the threshold.
  208. + */
  209. +
  210. + /*
  211. + * From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  212. + * the Helsinki University of Technology and Matti Tikkanen of Nokia
  213. + * Telecommunications, page 6:
  214. + * "A node is doubled if the ratio of non-empty children to all
  215. + * children in the *doubled* node is at least 'high'."
  216. + *
  217. + * 'high' in this instance is the variable 'inflate_threshold'. It
  218. + * is expressed as a percentage, so we multiply it with
  219. + * tnode_child_length() and instead of multiplying by 2 (since the
  220. + * child array will be doubled by inflate()) and multiplying
  221. + * the left-hand side by 100 (to handle the percentage thing) we
  222. + * multiply the left-hand side by 50.
  223. + *
  224. + * The left-hand side may look a bit weird: tnode_child_length(tn)
  225. + * - tn->empty_children is of course the number of non-null children
  226. + * in the current node. tn->full_children is the number of "full"
  227. + * children, that is non-null tnodes with a skip value of 0.
  228. + * All of those will be doubled in the resulting inflated tnode, so
  229. + * we just count them one extra time here.
  230. + *
  231. + * A clearer way to write this would be:
  232. + *
  233. + * to_be_doubled = tn->full_children;
  234. + * not_to_be_doubled = tnode_child_length(tn) - tn->empty_children -
  235. + * tn->full_children;
  236. + *
  237. + * new_child_length = tnode_child_length(tn) * 2;
  238. + *
  239. + * new_fill_factor = 100 * (not_to_be_doubled + 2*to_be_doubled) /
  240. + * new_child_length;
  241. + * if (new_fill_factor >= inflate_threshold)
  242. + *
  243. + * ...and so on, tho it would mess up the while () loop.
  244. + *
  245. + * anyway,
  246. + * 100 * (not_to_be_doubled + 2*to_be_doubled) / new_child_length >=
  247. + * inflate_threshold
  248. + *
  249. + * avoid a division:
  250. + * 100 * (not_to_be_doubled + 2*to_be_doubled) >=
  251. + * inflate_threshold * new_child_length
  252. + *
  253. + * expand not_to_be_doubled and to_be_doubled, and shorten:
  254. + * 100 * (tnode_child_length(tn) - tn->empty_children +
  255. + * tn->full_children) >= inflate_threshold * new_child_length
  256. + *
  257. + * expand new_child_length:
  258. + * 100 * (tnode_child_length(tn) - tn->empty_children +
  259. + * tn->full_children) >=
  260. + * inflate_threshold * tnode_child_length(tn) * 2
  261. + *
  262. + * shorten again:
  263. + * 50 * (tn->full_children + tnode_child_length(tn) -
  264. + * tn->empty_children) >= inflate_threshold *
  265. + * tnode_child_length(tn)
  266. + *
  267. + */
  268. +
  269. + /* Keep root node larger */
  270. +
  271. + if (!node_parent(tn)) {
  272. + inflate_threshold_use = inflate_threshold_root;
  273. + halve_threshold_use = halve_threshold_root;
  274. + } else {
  275. + inflate_threshold_use = inflate_threshold;
  276. + halve_threshold_use = halve_threshold;
  277. + }
  278. +
  279. + max_work = MAX_WORK;
  280. + while ((tn->full_children > 0 && max_work-- &&
  281. + 50 * (tn->full_children + tnode_child_length(tn)
  282. + - tn->empty_children)
  283. + >= inflate_threshold_use * tnode_child_length(tn))) {
  284. +
  285. + old_tn = tn;
  286. + tn = inflate(t, tn);
  287. +
  288. + if (IS_ERR(tn)) {
  289. + tn = old_tn;
  290. +#ifdef CONFIG_IP_FIB_TRIE_STATS
  291. + this_cpu_inc(t->stats->resize_node_skipped);
  292. +#endif
  293. + break;
  294. + }
  295. + }
  296. +
  297. + /* Return if at least one inflate is run */
  298. + if (max_work != MAX_WORK)
  299. + return tn;
  300. +
  301. + /*
  302. + * Halve as long as the number of empty children in this
  303. + * node is above threshold.
  304. + */
  305. +
  306. + max_work = MAX_WORK;
  307. + while (tn->bits > 1 && max_work-- &&
  308. + 100 * (tnode_child_length(tn) - tn->empty_children) <
  309. + halve_threshold_use * tnode_child_length(tn)) {
  310. +
  311. + old_tn = tn;
  312. + tn = halve(t, tn);
  313. + if (IS_ERR(tn)) {
  314. + tn = old_tn;
  315. +#ifdef CONFIG_IP_FIB_TRIE_STATS
  316. + this_cpu_inc(t->stats->resize_node_skipped);
  317. +#endif
  318. + break;
  319. + }
  320. + }
  321. +
  322. +
  323. + /* Only one child remains */
  324. + if (tn->empty_children == (tnode_child_length(tn) - 1)) {
  325. + unsigned long i;
  326. +one_child:
  327. + for (i = tnode_child_length(tn); !n && i;)
  328. + n = tnode_get_child(tn, --i);
  329. +no_children:
  330. + /* compress one level */
  331. + node_set_parent(n, NULL);
  332. + tnode_free_safe(tn);
  333. + return n;
  334. + }
  335. + return tn;
  336. +}
  337. +
  338. /* readside must use rcu_read_lock currently dump routines
  339. via get_fa_head and dump */