080-03-fib_trie-Make-leaf-and-tnode-more-uniform.patch 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Wed, 31 Dec 2014 10:55:35 -0800
  3. Subject: [PATCH] fib_trie: Make leaf and tnode more uniform
  4. This change makes some fundamental changes to the way leaves and tnodes are
  5. constructed. The big differences are:
  6. 1. Leaves now populate pos and bits indicating their full key size.
  7. 2. Trie nodes now mask out their lower bits to be consistent with the leaf
  8. 3. Both structures have been reordered so that rt_trie_node now consisists
  9. of a much larger region including the pos, bits, and rcu portions of
  10. the tnode structure.
  11. On 32b systems this will result in the leaf being 4B larger as the pos and
  12. bits values were added to a hole created by the key as it was only 4B in
  13. length.
  14. Signed-off-by: Alexander Duyck <alexander.h.duyck@redhat.com>
  15. Signed-off-by: David S. Miller <davem@davemloft.net>
  16. ---
  17. --- a/net/ipv4/fib_trie.c
  18. +++ b/net/ipv4/fib_trie.c
  19. @@ -87,24 +87,38 @@
  20. typedef unsigned int t_key;
  21. -#define T_TNODE 0
  22. -#define T_LEAF 1
  23. -#define NODE_TYPE_MASK 0x1UL
  24. -#define NODE_TYPE(node) ((node)->parent & NODE_TYPE_MASK)
  25. +#define IS_TNODE(n) ((n)->bits)
  26. +#define IS_LEAF(n) (!(n)->bits)
  27. -#define IS_TNODE(n) (!(n->parent & T_LEAF))
  28. -#define IS_LEAF(n) (n->parent & T_LEAF)
  29. +struct tnode {
  30. + t_key key;
  31. + unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  32. + unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  33. + struct tnode __rcu *parent;
  34. + union {
  35. + struct rcu_head rcu;
  36. + struct tnode *tnode_free;
  37. + };
  38. + unsigned int full_children; /* KEYLENGTH bits needed */
  39. + unsigned int empty_children; /* KEYLENGTH bits needed */
  40. + struct rt_trie_node __rcu *child[0];
  41. +};
  42. struct rt_trie_node {
  43. - unsigned long parent;
  44. t_key key;
  45. + unsigned char bits;
  46. + unsigned char pos;
  47. + struct tnode __rcu *parent;
  48. + struct rcu_head rcu;
  49. };
  50. struct leaf {
  51. - unsigned long parent;
  52. t_key key;
  53. - struct hlist_head list;
  54. + unsigned char bits;
  55. + unsigned char pos;
  56. + struct tnode __rcu *parent;
  57. struct rcu_head rcu;
  58. + struct hlist_head list;
  59. };
  60. struct leaf_info {
  61. @@ -115,20 +129,6 @@ struct leaf_info {
  62. struct rcu_head rcu;
  63. };
  64. -struct tnode {
  65. - unsigned long parent;
  66. - t_key key;
  67. - unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  68. - unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  69. - unsigned int full_children; /* KEYLENGTH bits needed */
  70. - unsigned int empty_children; /* KEYLENGTH bits needed */
  71. - union {
  72. - struct rcu_head rcu;
  73. - struct tnode *tnode_free;
  74. - };
  75. - struct rt_trie_node __rcu *child[0];
  76. -};
  77. -
  78. #ifdef CONFIG_IP_FIB_TRIE_STATS
  79. struct trie_use_stats {
  80. unsigned int gets;
  81. @@ -176,38 +176,27 @@ static const int sync_pages = 128;
  82. static struct kmem_cache *fn_alias_kmem __read_mostly;
  83. static struct kmem_cache *trie_leaf_kmem __read_mostly;
  84. -/*
  85. - * caller must hold RTNL
  86. - */
  87. -static inline struct tnode *node_parent(const struct rt_trie_node *node)
  88. -{
  89. - unsigned long parent;
  90. +/* caller must hold RTNL */
  91. +#define node_parent(n) rtnl_dereference((n)->parent)
  92. - parent = rcu_dereference_index_check(node->parent, lockdep_rtnl_is_held());
  93. +/* caller must hold RCU read lock or RTNL */
  94. +#define node_parent_rcu(n) rcu_dereference_rtnl((n)->parent)
  95. - return (struct tnode *)(parent & ~NODE_TYPE_MASK);
  96. -}
  97. -
  98. -/*
  99. - * caller must hold RCU read lock or RTNL
  100. - */
  101. -static inline struct tnode *node_parent_rcu(const struct rt_trie_node *node)
  102. +/* wrapper for rcu_assign_pointer */
  103. +static inline void node_set_parent(struct rt_trie_node *node, struct tnode *ptr)
  104. {
  105. - unsigned long parent;
  106. -
  107. - parent = rcu_dereference_index_check(node->parent, rcu_read_lock_held() ||
  108. - lockdep_rtnl_is_held());
  109. -
  110. - return (struct tnode *)(parent & ~NODE_TYPE_MASK);
  111. + if (node)
  112. + rcu_assign_pointer(node->parent, ptr);
  113. }
  114. -/* Same as rcu_assign_pointer
  115. - * but that macro() assumes that value is a pointer.
  116. +#define NODE_INIT_PARENT(n, p) RCU_INIT_POINTER((n)->parent, p)
  117. +
  118. +/* This provides us with the number of children in this node, in the case of a
  119. + * leaf this will return 0 meaning none of the children are accessible.
  120. */
  121. -static inline void node_set_parent(struct rt_trie_node *node, struct tnode *ptr)
  122. +static inline int tnode_child_length(const struct tnode *tn)
  123. {
  124. - smp_wmb();
  125. - node->parent = (unsigned long)ptr | NODE_TYPE(node);
  126. + return (1ul << tn->bits) & ~(1ul);
  127. }
  128. /*
  129. @@ -215,7 +204,7 @@ static inline void node_set_parent(struc
  130. */
  131. static inline struct rt_trie_node *tnode_get_child(const struct tnode *tn, unsigned int i)
  132. {
  133. - BUG_ON(i >= 1U << tn->bits);
  134. + BUG_ON(i >= tnode_child_length(tn));
  135. return rtnl_dereference(tn->child[i]);
  136. }
  137. @@ -225,16 +214,11 @@ static inline struct rt_trie_node *tnode
  138. */
  139. static inline struct rt_trie_node *tnode_get_child_rcu(const struct tnode *tn, unsigned int i)
  140. {
  141. - BUG_ON(i >= 1U << tn->bits);
  142. + BUG_ON(i >= tnode_child_length(tn));
  143. return rcu_dereference_rtnl(tn->child[i]);
  144. }
  145. -static inline int tnode_child_length(const struct tnode *tn)
  146. -{
  147. - return 1 << tn->bits;
  148. -}
  149. -
  150. static inline t_key mask_pfx(t_key k, unsigned int l)
  151. {
  152. return (l == 0) ? 0 : k >> (KEYLENGTH-l) << (KEYLENGTH-l);
  153. @@ -336,11 +320,6 @@ static inline int tkey_mismatch(t_key a,
  154. */
  155. -static inline void check_tnode(const struct tnode *tn)
  156. -{
  157. - WARN_ON(tn && tn->pos+tn->bits > 32);
  158. -}
  159. -
  160. static const int halve_threshold = 25;
  161. static const int inflate_threshold = 50;
  162. static const int halve_threshold_root = 15;
  163. @@ -426,11 +405,20 @@ static void tnode_free_flush(void)
  164. }
  165. }
  166. -static struct leaf *leaf_new(void)
  167. +static struct leaf *leaf_new(t_key key)
  168. {
  169. struct leaf *l = kmem_cache_alloc(trie_leaf_kmem, GFP_KERNEL);
  170. if (l) {
  171. - l->parent = T_LEAF;
  172. + l->parent = NULL;
  173. + /* set key and pos to reflect full key value
  174. + * any trailing zeros in the key should be ignored
  175. + * as the nodes are searched
  176. + */
  177. + l->key = key;
  178. + l->pos = KEYLENGTH;
  179. + /* set bits to 0 indicating we are not a tnode */
  180. + l->bits = 0;
  181. +
  182. INIT_HLIST_HEAD(&l->list);
  183. }
  184. return l;
  185. @@ -451,12 +439,16 @@ static struct tnode *tnode_new(t_key key
  186. {
  187. size_t sz = sizeof(struct tnode) + (sizeof(struct rt_trie_node *) << bits);
  188. struct tnode *tn = tnode_alloc(sz);
  189. + unsigned int shift = pos + bits;
  190. +
  191. + /* verify bits and pos their msb bits clear and values are valid */
  192. + BUG_ON(!bits || (shift > KEYLENGTH));
  193. if (tn) {
  194. - tn->parent = T_TNODE;
  195. + tn->parent = NULL;
  196. tn->pos = pos;
  197. tn->bits = bits;
  198. - tn->key = key;
  199. + tn->key = mask_pfx(key, pos);
  200. tn->full_children = 0;
  201. tn->empty_children = 1<<bits;
  202. }
  203. @@ -473,10 +465,7 @@ static struct tnode *tnode_new(t_key key
  204. static inline int tnode_full(const struct tnode *tn, const struct rt_trie_node *n)
  205. {
  206. - if (n == NULL || IS_LEAF(n))
  207. - return 0;
  208. -
  209. - return ((struct tnode *) n)->pos == tn->pos + tn->bits;
  210. + return n && IS_TNODE(n) && (n->pos == (tn->pos + tn->bits));
  211. }
  212. static inline void put_child(struct tnode *tn, int i,
  213. @@ -514,8 +503,7 @@ static void tnode_put_child_reorg(struct
  214. else if (!wasfull && isfull)
  215. tn->full_children++;
  216. - if (n)
  217. - node_set_parent(n, tn);
  218. + node_set_parent(n, tn);
  219. rcu_assign_pointer(tn->child[i], n);
  220. }
  221. @@ -523,7 +511,7 @@ static void tnode_put_child_reorg(struct
  222. #define MAX_WORK 10
  223. static struct rt_trie_node *resize(struct trie *t, struct tnode *tn)
  224. {
  225. - int i;
  226. + struct rt_trie_node *n = NULL;
  227. struct tnode *old_tn;
  228. int inflate_threshold_use;
  229. int halve_threshold_use;
  230. @@ -536,12 +524,11 @@ static struct rt_trie_node *resize(struc
  231. tn, inflate_threshold, halve_threshold);
  232. /* No children */
  233. - if (tn->empty_children == tnode_child_length(tn)) {
  234. - tnode_free_safe(tn);
  235. - return NULL;
  236. - }
  237. + if (tn->empty_children > (tnode_child_length(tn) - 1))
  238. + goto no_children;
  239. +
  240. /* One child */
  241. - if (tn->empty_children == tnode_child_length(tn) - 1)
  242. + if (tn->empty_children == (tnode_child_length(tn) - 1))
  243. goto one_child;
  244. /*
  245. * Double as long as the resulting node has a number of
  246. @@ -607,11 +594,9 @@ static struct rt_trie_node *resize(struc
  247. *
  248. */
  249. - check_tnode(tn);
  250. -
  251. /* Keep root node larger */
  252. - if (!node_parent((struct rt_trie_node *)tn)) {
  253. + if (!node_parent(tn)) {
  254. inflate_threshold_use = inflate_threshold_root;
  255. halve_threshold_use = halve_threshold_root;
  256. } else {
  257. @@ -637,8 +622,6 @@ static struct rt_trie_node *resize(struc
  258. }
  259. }
  260. - check_tnode(tn);
  261. -
  262. /* Return if at least one inflate is run */
  263. if (max_work != MAX_WORK)
  264. return (struct rt_trie_node *) tn;
  265. @@ -666,21 +649,16 @@ static struct rt_trie_node *resize(struc
  266. /* Only one child remains */
  267. - if (tn->empty_children == tnode_child_length(tn) - 1) {
  268. + if (tn->empty_children == (tnode_child_length(tn) - 1)) {
  269. + unsigned long i;
  270. one_child:
  271. - for (i = 0; i < tnode_child_length(tn); i++) {
  272. - struct rt_trie_node *n;
  273. -
  274. - n = rtnl_dereference(tn->child[i]);
  275. - if (!n)
  276. - continue;
  277. -
  278. - /* compress one level */
  279. -
  280. - node_set_parent(n, NULL);
  281. - tnode_free_safe(tn);
  282. - return n;
  283. - }
  284. + for (i = tnode_child_length(tn); !n && i;)
  285. + n = tnode_get_child(tn, --i);
  286. +no_children:
  287. + /* compress one level */
  288. + node_set_parent(n, NULL);
  289. + tnode_free_safe(tn);
  290. + return n;
  291. }
  292. return (struct rt_trie_node *) tn;
  293. }
  294. @@ -760,8 +738,7 @@ static struct tnode *inflate(struct trie
  295. /* A leaf or an internal node with skipped bits */
  296. - if (IS_LEAF(node) || ((struct tnode *) node)->pos >
  297. - tn->pos + tn->bits - 1) {
  298. + if (IS_LEAF(node) || (node->pos > (tn->pos + tn->bits - 1))) {
  299. put_child(tn,
  300. tkey_extract_bits(node->key, oldtnode->pos, oldtnode->bits + 1),
  301. node);
  302. @@ -958,11 +935,9 @@ fib_find_node(struct trie *t, u32 key)
  303. pos = 0;
  304. n = rcu_dereference_rtnl(t->trie);
  305. - while (n != NULL && NODE_TYPE(n) == T_TNODE) {
  306. + while (n && IS_TNODE(n)) {
  307. tn = (struct tnode *) n;
  308. - check_tnode(tn);
  309. -
  310. if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
  311. pos = tn->pos + tn->bits;
  312. n = tnode_get_child_rcu(tn,
  313. @@ -988,7 +963,7 @@ static void trie_rebalance(struct trie *
  314. key = tn->key;
  315. - while (tn != NULL && (tp = node_parent((struct rt_trie_node *)tn)) != NULL) {
  316. + while (tn != NULL && (tp = node_parent(tn)) != NULL) {
  317. cindex = tkey_extract_bits(key, tp->pos, tp->bits);
  318. wasfull = tnode_full(tp, tnode_get_child(tp, cindex));
  319. tn = (struct tnode *)resize(t, tn);
  320. @@ -996,7 +971,7 @@ static void trie_rebalance(struct trie *
  321. tnode_put_child_reorg(tp, cindex,
  322. (struct rt_trie_node *)tn, wasfull);
  323. - tp = node_parent((struct rt_trie_node *) tn);
  324. + tp = node_parent(tn);
  325. if (!tp)
  326. rcu_assign_pointer(t->trie, (struct rt_trie_node *)tn);
  327. @@ -1048,11 +1023,9 @@ static struct list_head *fib_insert_node
  328. * If it doesn't, we need to replace it with a T_TNODE.
  329. */
  330. - while (n != NULL && NODE_TYPE(n) == T_TNODE) {
  331. + while (n && IS_TNODE(n)) {
  332. tn = (struct tnode *) n;
  333. - check_tnode(tn);
  334. -
  335. if (tkey_sub_equals(tn->key, pos, tn->pos-pos, key)) {
  336. tp = tn;
  337. pos = tn->pos + tn->bits;
  338. @@ -1087,12 +1060,11 @@ static struct list_head *fib_insert_node
  339. insert_leaf_info(&l->list, li);
  340. goto done;
  341. }
  342. - l = leaf_new();
  343. + l = leaf_new(key);
  344. if (!l)
  345. return NULL;
  346. - l->key = key;
  347. li = leaf_info_new(plen);
  348. if (!li) {
  349. @@ -1569,7 +1541,7 @@ backtrace:
  350. if (chopped_off <= pn->bits) {
  351. cindex &= ~(1 << (chopped_off-1));
  352. } else {
  353. - struct tnode *parent = node_parent_rcu((struct rt_trie_node *) pn);
  354. + struct tnode *parent = node_parent_rcu(pn);
  355. if (!parent)
  356. goto failed;
  357. @@ -1597,7 +1569,7 @@ EXPORT_SYMBOL_GPL(fib_table_lookup);
  358. */
  359. static void trie_leaf_remove(struct trie *t, struct leaf *l)
  360. {
  361. - struct tnode *tp = node_parent((struct rt_trie_node *) l);
  362. + struct tnode *tp = node_parent(l);
  363. pr_debug("entering trie_leaf_remove(%p)\n", l);
  364. @@ -2374,7 +2346,7 @@ static int fib_trie_seq_show(struct seq_
  365. if (IS_TNODE(n)) {
  366. struct tnode *tn = (struct tnode *) n;
  367. - __be32 prf = htonl(mask_pfx(tn->key, tn->pos));
  368. + __be32 prf = htonl(tn->key);
  369. seq_indent(seq, iter->depth-1);
  370. seq_printf(seq, " +-- %pI4/%d %d %d %d\n",