080-22-fib_trie-Add-collapse-and-should_collapse-to-resize.patch 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Thu, 22 Jan 2015 15:51:26 -0800
  3. Subject: [PATCH] fib_trie: Add collapse() and should_collapse() to resize
  4. This patch really does two things.
  5. First it pulls the logic for determining if we should collapse one node out
  6. of the tree and the actual code doing the collapse into a separate pair of
  7. functions. This helps to make the changes to these areas more readable.
  8. Second it encodes the upper 32b of the empty_children value onto the
  9. full_children value in the case of bits == KEYLENGTH. By doing this we are
  10. able to handle the case of a 32b node where empty_children would appear to
  11. be 0 when it was actually 1ul << 32.
  12. Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com>
  13. Signed-off-by: David S. Miller <davem@davemloft.net>
  14. ---
  15. --- a/net/ipv4/fib_trie.c
  16. +++ b/net/ipv4/fib_trie.c
  17. @@ -83,7 +83,8 @@
  18. #define MAX_STAT_DEPTH 32
  19. -#define KEYLENGTH (8*sizeof(t_key))
  20. +#define KEYLENGTH (8*sizeof(t_key))
  21. +#define KEY_MAX ((t_key)~0)
  22. typedef unsigned int t_key;
  23. @@ -102,8 +103,8 @@ struct tnode {
  24. union {
  25. /* The fields in this struct are valid if bits > 0 (TNODE) */
  26. struct {
  27. - unsigned int full_children; /* KEYLENGTH bits needed */
  28. - unsigned int empty_children; /* KEYLENGTH bits needed */
  29. + t_key empty_children; /* KEYLENGTH bits needed */
  30. + t_key full_children; /* KEYLENGTH bits needed */
  31. struct tnode __rcu *child[0];
  32. };
  33. /* This list pointer if valid if bits == 0 (LEAF) */
  34. @@ -302,6 +303,16 @@ static struct tnode *tnode_alloc(size_t
  35. return vzalloc(size);
  36. }
  37. +static inline void empty_child_inc(struct tnode *n)
  38. +{
  39. + ++n->empty_children ? : ++n->full_children;
  40. +}
  41. +
  42. +static inline void empty_child_dec(struct tnode *n)
  43. +{
  44. + n->empty_children-- ? : n->full_children--;
  45. +}
  46. +
  47. static struct tnode *leaf_new(t_key key)
  48. {
  49. struct tnode *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
  50. @@ -335,7 +346,7 @@ static struct leaf_info *leaf_info_new(i
  51. static struct tnode *tnode_new(t_key key, int pos, int bits)
  52. {
  53. - size_t sz = offsetof(struct tnode, child[1 << bits]);
  54. + size_t sz = offsetof(struct tnode, child[1ul << bits]);
  55. struct tnode *tn = tnode_alloc(sz);
  56. unsigned int shift = pos + bits;
  57. @@ -348,8 +359,10 @@ static struct tnode *tnode_new(t_key key
  58. tn->pos = pos;
  59. tn->bits = bits;
  60. tn->key = (shift < KEYLENGTH) ? (key >> shift) << shift : 0;
  61. - tn->full_children = 0;
  62. - tn->empty_children = 1<<bits;
  63. + if (bits == KEYLENGTH)
  64. + tn->full_children = 1;
  65. + else
  66. + tn->empty_children = 1ul << bits;
  67. }
  68. pr_debug("AT %p s=%zu %zu\n", tn, sizeof(struct tnode),
  69. @@ -375,11 +388,11 @@ static void put_child(struct tnode *tn,
  70. BUG_ON(i >= tnode_child_length(tn));
  71. - /* update emptyChildren */
  72. + /* update emptyChildren, overflow into fullChildren */
  73. if (n == NULL && chi != NULL)
  74. - tn->empty_children++;
  75. - else if (n != NULL && chi == NULL)
  76. - tn->empty_children--;
  77. + empty_child_inc(tn);
  78. + if (n != NULL && chi == NULL)
  79. + empty_child_dec(tn);
  80. /* update fullChildren */
  81. wasfull = tnode_full(tn, chi);
  82. @@ -630,6 +643,24 @@ static int halve(struct trie *t, struct
  83. return 0;
  84. }
  85. +static void collapse(struct trie *t, struct tnode *oldtnode)
  86. +{
  87. + struct tnode *n, *tp;
  88. + unsigned long i;
  89. +
  90. + /* scan the tnode looking for that one child that might still exist */
  91. + for (n = NULL, i = tnode_child_length(oldtnode); !n && i;)
  92. + n = tnode_get_child(oldtnode, --i);
  93. +
  94. + /* compress one level */
  95. + tp = node_parent(oldtnode);
  96. + put_child_root(tp, t, oldtnode->key, n);
  97. + node_set_parent(n, tp);
  98. +
  99. + /* drop dead node */
  100. + node_free(oldtnode);
  101. +}
  102. +
  103. static unsigned char update_suffix(struct tnode *tn)
  104. {
  105. unsigned char slen = tn->pos;
  106. @@ -729,10 +760,12 @@ static bool should_inflate(const struct
  107. /* Keep root node larger */
  108. threshold *= tp ? inflate_threshold : inflate_threshold_root;
  109. - used += tn->full_children;
  110. used -= tn->empty_children;
  111. + used += tn->full_children;
  112. - return tn->pos && ((50 * used) >= threshold);
  113. + /* if bits == KEYLENGTH then pos = 0, and will fail below */
  114. +
  115. + return (used > 1) && tn->pos && ((50 * used) >= threshold);
  116. }
  117. static bool should_halve(const struct tnode *tp, const struct tnode *tn)
  118. @@ -744,13 +777,29 @@ static bool should_halve(const struct tn
  119. threshold *= tp ? halve_threshold : halve_threshold_root;
  120. used -= tn->empty_children;
  121. - return (tn->bits > 1) && ((100 * used) < threshold);
  122. + /* if bits == KEYLENGTH then used = 100% on wrap, and will fail below */
  123. +
  124. + return (used > 1) && (tn->bits > 1) && ((100 * used) < threshold);
  125. +}
  126. +
  127. +static bool should_collapse(const struct tnode *tn)
  128. +{
  129. + unsigned long used = tnode_child_length(tn);
  130. +
  131. + used -= tn->empty_children;
  132. +
  133. + /* account for bits == KEYLENGTH case */
  134. + if ((tn->bits == KEYLENGTH) && tn->full_children)
  135. + used -= KEY_MAX;
  136. +
  137. + /* One child or none, time to drop us from the trie */
  138. + return used < 2;
  139. }
  140. #define MAX_WORK 10
  141. static void resize(struct trie *t, struct tnode *tn)
  142. {
  143. - struct tnode *tp = node_parent(tn), *n = NULL;
  144. + struct tnode *tp = node_parent(tn);
  145. struct tnode __rcu **cptr;
  146. int max_work = MAX_WORK;
  147. @@ -764,14 +813,6 @@ static void resize(struct trie *t, struc
  148. cptr = tp ? &tp->child[get_index(tn->key, tp)] : &t->trie;
  149. BUG_ON(tn != rtnl_dereference(*cptr));
  150. - /* No children */
  151. - if (tn->empty_children > (tnode_child_length(tn) - 1))
  152. - goto no_children;
  153. -
  154. - /* One child */
  155. - if (tn->empty_children == (tnode_child_length(tn) - 1))
  156. - goto one_child;
  157. -
  158. /* Double as long as the resulting node has a number of
  159. * nonempty nodes that are above the threshold.
  160. */
  161. @@ -807,19 +848,8 @@ static void resize(struct trie *t, struc
  162. }
  163. /* Only one child remains */
  164. - if (tn->empty_children == (tnode_child_length(tn) - 1)) {
  165. - unsigned long i;
  166. -one_child:
  167. - for (i = tnode_child_length(tn); !n && i;)
  168. - n = tnode_get_child(tn, --i);
  169. -no_children:
  170. - /* compress one level */
  171. - put_child_root(tp, t, tn->key, n);
  172. - node_set_parent(n, tp);
  173. -
  174. - /* drop dead node */
  175. - tnode_free_init(tn);
  176. - tnode_free(tn);
  177. + if (should_collapse(tn)) {
  178. + collapse(t, tn);
  179. return;
  180. }