1
0

080-14-fib_trie-Push-assignment-of-child-to-parent-down-int.patch 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Wed, 31 Dec 2014 10:56:43 -0800
  3. Subject: [PATCH] fib_trie: Push assignment of child to parent down into
  4. inflate/halve
  5. This change makes it so that the assignment of the tnode to the parent is
  6. handled directly within whatever function is currently handling the node be
  7. it inflate, halve, or resize. By doing this we can avoid some of the need
  8. to set NULL pointers in the tree while we are resizing the subnodes.
  9. Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com>
  10. Signed-off-by: David S. Miller <davem@davemloft.net>
  11. ---
  12. --- a/net/ipv4/fib_trie.c
  13. +++ b/net/ipv4/fib_trie.c
  14. @@ -146,9 +146,7 @@ struct trie {
  15. #endif
  16. };
  17. -static void tnode_put_child_reorg(struct tnode *tn, unsigned long i,
  18. - struct tnode *n, int wasfull);
  19. -static struct tnode *resize(struct trie *t, struct tnode *tn);
  20. +static void resize(struct trie *t, struct tnode *tn);
  21. /* tnodes to free after resize(); protected by RTNL */
  22. static struct callback_head *tnode_free_head;
  23. static size_t tnode_free_size;
  24. @@ -396,22 +394,13 @@ static inline int tnode_full(const struc
  25. return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
  26. }
  27. -static inline void put_child(struct tnode *tn, unsigned long i,
  28. - struct tnode *n)
  29. -{
  30. - tnode_put_child_reorg(tn, i, n, -1);
  31. -}
  32. -
  33. - /*
  34. - * Add a child at position i overwriting the old value.
  35. - * Update the value of full_children and empty_children.
  36. - */
  37. -
  38. -static void tnode_put_child_reorg(struct tnode *tn, unsigned long i,
  39. - struct tnode *n, int wasfull)
  40. +/* Add a child at position i overwriting the old value.
  41. + * Update the value of full_children and empty_children.
  42. + */
  43. +static void put_child(struct tnode *tn, unsigned long i, struct tnode *n)
  44. {
  45. struct tnode *chi = rtnl_dereference(tn->child[i]);
  46. - int isfull;
  47. + int isfull, wasfull;
  48. BUG_ON(i >= tnode_child_length(tn));
  49. @@ -422,10 +411,9 @@ static void tnode_put_child_reorg(struct
  50. tn->empty_children--;
  51. /* update fullChildren */
  52. - if (wasfull == -1)
  53. - wasfull = tnode_full(tn, chi);
  54. -
  55. + wasfull = tnode_full(tn, chi);
  56. isfull = tnode_full(tn, n);
  57. +
  58. if (wasfull && !isfull)
  59. tn->full_children--;
  60. else if (!wasfull && isfull)
  61. @@ -458,9 +446,10 @@ static void tnode_clean_free(struct tnod
  62. node_free(tn);
  63. }
  64. -static struct tnode *inflate(struct trie *t, struct tnode *oldtnode)
  65. +static int inflate(struct trie *t, struct tnode *oldtnode)
  66. {
  67. unsigned long olen = tnode_child_length(oldtnode);
  68. + struct tnode *tp = node_parent(oldtnode);
  69. struct tnode *tn;
  70. unsigned long i;
  71. t_key m;
  72. @@ -468,9 +457,8 @@ static struct tnode *inflate(struct trie
  73. pr_debug("In inflate\n");
  74. tn = tnode_new(oldtnode->key, oldtnode->pos - 1, oldtnode->bits + 1);
  75. -
  76. if (!tn)
  77. - return ERR_PTR(-ENOMEM);
  78. + return -ENOMEM;
  79. /*
  80. * Preallocate and store tnodes before the actual work so we
  81. @@ -564,30 +552,36 @@ static struct tnode *inflate(struct trie
  82. put_child(left, j, rtnl_dereference(inode->child[j]));
  83. put_child(right, j, rtnl_dereference(inode->child[j + size]));
  84. }
  85. - put_child(tn, 2*i, resize(t, left));
  86. - put_child(tn, 2*i+1, resize(t, right));
  87. +
  88. + put_child(tn, 2 * i, left);
  89. + put_child(tn, 2 * i + 1, right);
  90. tnode_free_safe(inode);
  91. +
  92. + resize(t, left);
  93. + resize(t, right);
  94. }
  95. +
  96. + put_child_root(tp, t, tn->key, tn);
  97. tnode_free_safe(oldtnode);
  98. - return tn;
  99. + return 0;
  100. nomem:
  101. tnode_clean_free(tn);
  102. - return ERR_PTR(-ENOMEM);
  103. + return -ENOMEM;
  104. }
  105. -static struct tnode *halve(struct trie *t, struct tnode *oldtnode)
  106. +static int halve(struct trie *t, struct tnode *oldtnode)
  107. {
  108. unsigned long olen = tnode_child_length(oldtnode);
  109. + struct tnode *tp = node_parent(oldtnode);
  110. struct tnode *tn, *left, *right;
  111. int i;
  112. pr_debug("In halve\n");
  113. tn = tnode_new(oldtnode->key, oldtnode->pos + 1, oldtnode->bits - 1);
  114. -
  115. if (!tn)
  116. - return ERR_PTR(-ENOMEM);
  117. + return -ENOMEM;
  118. /*
  119. * Preallocate and store tnodes before the actual work so we
  120. @@ -606,8 +600,10 @@ static struct tnode *halve(struct trie *
  121. newn = tnode_new(left->key, oldtnode->pos, 1);
  122. - if (!newn)
  123. - goto nomem;
  124. + if (!newn) {
  125. + tnode_clean_free(tn);
  126. + return -ENOMEM;
  127. + }
  128. put_child(tn, i/2, newn);
  129. }
  130. @@ -635,16 +631,18 @@ static struct tnode *halve(struct trie *
  131. /* Two nonempty children */
  132. newBinNode = tnode_get_child(tn, i/2);
  133. - put_child(tn, i/2, NULL);
  134. put_child(newBinNode, 0, left);
  135. put_child(newBinNode, 1, right);
  136. - put_child(tn, i/2, resize(t, newBinNode));
  137. +
  138. + put_child(tn, i / 2, newBinNode);
  139. +
  140. + resize(t, newBinNode);
  141. }
  142. +
  143. + put_child_root(tp, t, tn->key, tn);
  144. tnode_free_safe(oldtnode);
  145. - return tn;
  146. -nomem:
  147. - tnode_clean_free(tn);
  148. - return ERR_PTR(-ENOMEM);
  149. +
  150. + return 0;
  151. }
  152. /* From "Implementing a dynamic compressed trie" by Stefan Nilsson of
  153. @@ -704,45 +702,48 @@ nomem:
  154. * tnode_child_length(tn)
  155. *
  156. */
  157. -static bool should_inflate(const struct tnode *tn)
  158. +static bool should_inflate(const struct tnode *tp, const struct tnode *tn)
  159. {
  160. unsigned long used = tnode_child_length(tn);
  161. unsigned long threshold = used;
  162. /* Keep root node larger */
  163. - threshold *= node_parent(tn) ? inflate_threshold :
  164. - inflate_threshold_root;
  165. + threshold *= tp ? inflate_threshold : inflate_threshold_root;
  166. used += tn->full_children;
  167. used -= tn->empty_children;
  168. return tn->pos && ((50 * used) >= threshold);
  169. }
  170. -static bool should_halve(const struct tnode *tn)
  171. +static bool should_halve(const struct tnode *tp, const struct tnode *tn)
  172. {
  173. unsigned long used = tnode_child_length(tn);
  174. unsigned long threshold = used;
  175. /* Keep root node larger */
  176. - threshold *= node_parent(tn) ? halve_threshold :
  177. - halve_threshold_root;
  178. + threshold *= tp ? halve_threshold : halve_threshold_root;
  179. used -= tn->empty_children;
  180. return (tn->bits > 1) && ((100 * used) < threshold);
  181. }
  182. #define MAX_WORK 10
  183. -static struct tnode *resize(struct trie *t, struct tnode *tn)
  184. +static void resize(struct trie *t, struct tnode *tn)
  185. {
  186. - struct tnode *old_tn, *n = NULL;
  187. + struct tnode *tp = node_parent(tn), *n = NULL;
  188. + struct tnode __rcu **cptr;
  189. int max_work;
  190. - if (!tn)
  191. - return NULL;
  192. -
  193. pr_debug("In tnode_resize %p inflate_threshold=%d threshold=%d\n",
  194. tn, inflate_threshold, halve_threshold);
  195. + /* track the tnode via the pointer from the parent instead of
  196. + * doing it ourselves. This way we can let RCU fully do its
  197. + * thing without us interfering
  198. + */
  199. + cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
  200. + BUG_ON(tn != rtnl_dereference(*cptr));
  201. +
  202. /* No children */
  203. if (tn->empty_children > (tnode_child_length(tn) - 1))
  204. goto no_children;
  205. @@ -755,39 +756,35 @@ static struct tnode *resize(struct trie
  206. * nonempty nodes that are above the threshold.
  207. */
  208. max_work = MAX_WORK;
  209. - while (should_inflate(tn) && max_work--) {
  210. - old_tn = tn;
  211. - tn = inflate(t, tn);
  212. -
  213. - if (IS_ERR(tn)) {
  214. - tn = old_tn;
  215. + while (should_inflate(tp, tn) && max_work--) {
  216. + if (inflate(t, tn)) {
  217. #ifdef CONFIG_IP_FIB_TRIE_STATS
  218. this_cpu_inc(t->stats->resize_node_skipped);
  219. #endif
  220. break;
  221. }
  222. +
  223. + tn = rtnl_dereference(*cptr);
  224. }
  225. /* Return if at least one inflate is run */
  226. if (max_work != MAX_WORK)
  227. - return tn;
  228. + return;
  229. /* Halve as long as the number of empty children in this
  230. * node is above threshold.
  231. */
  232. max_work = MAX_WORK;
  233. - while (should_halve(tn) && max_work--) {
  234. - old_tn = tn;
  235. - tn = halve(t, tn);
  236. - if (IS_ERR(tn)) {
  237. - tn = old_tn;
  238. + while (should_halve(tp, tn) && max_work--) {
  239. + if (halve(t, tn)) {
  240. #ifdef CONFIG_IP_FIB_TRIE_STATS
  241. this_cpu_inc(t->stats->resize_node_skipped);
  242. #endif
  243. break;
  244. }
  245. - }
  246. + tn = rtnl_dereference(*cptr);
  247. + }
  248. /* Only one child remains */
  249. if (tn->empty_children == (tnode_child_length(tn) - 1)) {
  250. @@ -797,11 +794,12 @@ one_child:
  251. n = tnode_get_child(tn, --i);
  252. no_children:
  253. /* compress one level */
  254. - node_set_parent(n, NULL);
  255. + put_child_root(tp, t, tn->key, n);
  256. + node_set_parent(n, tp);
  257. +
  258. + /* drop dead node */
  259. tnode_free_safe(tn);
  260. - return n;
  261. }
  262. - return tn;
  263. }
  264. /* readside must use rcu_read_lock currently dump routines
  265. @@ -882,34 +880,19 @@ static struct tnode *fib_find_node(struc
  266. static void trie_rebalance(struct trie *t, struct tnode *tn)
  267. {
  268. - int wasfull;
  269. - t_key cindex, key;
  270. struct tnode *tp;
  271. - key = tn->key;
  272. -
  273. - while (tn != NULL && (tp = node_parent(tn)) != NULL) {
  274. - cindex = get_index(key, tp);
  275. - wasfull = tnode_full(tp, tnode_get_child(tp, cindex));
  276. - tn = resize(t, tn);
  277. -
  278. - tnode_put_child_reorg(tp, cindex, tn, wasfull);
  279. -
  280. - tp = node_parent(tn);
  281. - if (!tp)
  282. - rcu_assign_pointer(t->trie, tn);
  283. + while ((tp = node_parent(tn)) != NULL) {
  284. + resize(t, tn);
  285. tnode_free_flush();
  286. - if (!tp)
  287. - break;
  288. tn = tp;
  289. }
  290. /* Handle last (top) tnode */
  291. if (IS_TNODE(tn))
  292. - tn = resize(t, tn);
  293. + resize(t, tn);
  294. - rcu_assign_pointer(t->trie, tn);
  295. tnode_free_flush();
  296. }