080-04-fib_trie-Merge-tnode_free-and-leaf_free-into-node_fr.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Wed, 31 Dec 2014 10:55:41 -0800
  3. Subject: [PATCH] fib_trie: Merge tnode_free and leaf_free into node_free
  4. Both the leaf and the tnode had an rcu_head in them, but they had them in
  5. slightly different places. Since we now have them in the same spot and
  6. know that any node with bits == 0 is a leaf and the rest are either vmalloc
  7. or kmalloc tnodes depending on the value of bits it makes it easy to combine
  8. the functions and reduce overhead.
  9. In addition I have taken advantage of the rcu_head pointer to go ahead and
  10. put together a simple linked list instead of using the tnode pointer as
  11. this way we can merge either type of structure for freeing.
  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. @@ -95,15 +95,17 @@ struct tnode {
  18. unsigned char bits; /* 2log(KEYLENGTH) bits needed */
  19. unsigned char pos; /* 2log(KEYLENGTH) bits needed */
  20. struct tnode __rcu *parent;
  21. - union {
  22. - struct rcu_head rcu;
  23. - struct tnode *tnode_free;
  24. - };
  25. + struct rcu_head rcu;
  26. + /* everything above this comment must be the same as rt_trie_node */
  27. unsigned int full_children; /* KEYLENGTH bits needed */
  28. unsigned int empty_children; /* KEYLENGTH bits needed */
  29. struct rt_trie_node __rcu *child[0];
  30. };
  31. +/* This struct represents the shared bits between tnode and leaf. If any
  32. + * ordering is changed here is must also be updated in tnode and leaf as
  33. + * well.
  34. + */
  35. struct rt_trie_node {
  36. t_key key;
  37. unsigned char bits;
  38. @@ -118,6 +120,7 @@ struct leaf {
  39. unsigned char pos;
  40. struct tnode __rcu *parent;
  41. struct rcu_head rcu;
  42. + /* everything above this comment must be the same as rt_trie_node */
  43. struct hlist_head list;
  44. };
  45. @@ -163,7 +166,7 @@ static struct rt_trie_node *resize(struc
  46. static struct tnode *inflate(struct trie *t, struct tnode *tn);
  47. static struct tnode *halve(struct trie *t, struct tnode *tn);
  48. /* tnodes to free after resize(); protected by RTNL */
  49. -static struct tnode *tnode_free_head;
  50. +static struct callback_head *tnode_free_head;
  51. static size_t tnode_free_size;
  52. /*
  53. @@ -336,17 +339,23 @@ static inline void alias_free_mem_rcu(st
  54. call_rcu(&fa->rcu, __alias_free_mem);
  55. }
  56. -static void __leaf_free_rcu(struct rcu_head *head)
  57. -{
  58. - struct leaf *l = container_of(head, struct leaf, rcu);
  59. - kmem_cache_free(trie_leaf_kmem, l);
  60. -}
  61. +#define TNODE_KMALLOC_MAX \
  62. + ilog2((PAGE_SIZE - sizeof(struct tnode)) / sizeof(struct rt_trie_node *))
  63. -static inline void free_leaf(struct leaf *l)
  64. +static void __node_free_rcu(struct rcu_head *head)
  65. {
  66. - call_rcu(&l->rcu, __leaf_free_rcu);
  67. + struct rt_trie_node *n = container_of(head, struct rt_trie_node, rcu);
  68. +
  69. + if (IS_LEAF(n))
  70. + kmem_cache_free(trie_leaf_kmem, n);
  71. + else if (n->bits <= TNODE_KMALLOC_MAX)
  72. + kfree(n);
  73. + else
  74. + vfree(n);
  75. }
  76. +#define node_free(n) call_rcu(&n->rcu, __node_free_rcu)
  77. +
  78. static inline void free_leaf_info(struct leaf_info *leaf)
  79. {
  80. kfree_rcu(leaf, rcu);
  81. @@ -360,43 +369,24 @@ static struct tnode *tnode_alloc(size_t
  82. return vzalloc(size);
  83. }
  84. -static void __tnode_free_rcu(struct rcu_head *head)
  85. -{
  86. - struct tnode *tn = container_of(head, struct tnode, rcu);
  87. - size_t size = sizeof(struct tnode) +
  88. - (sizeof(struct rt_trie_node *) << tn->bits);
  89. -
  90. - if (size <= PAGE_SIZE)
  91. - kfree(tn);
  92. - else
  93. - vfree(tn);
  94. -}
  95. -
  96. -static inline void tnode_free(struct tnode *tn)
  97. -{
  98. - if (IS_LEAF(tn))
  99. - free_leaf((struct leaf *) tn);
  100. - else
  101. - call_rcu(&tn->rcu, __tnode_free_rcu);
  102. -}
  103. -
  104. static void tnode_free_safe(struct tnode *tn)
  105. {
  106. BUG_ON(IS_LEAF(tn));
  107. - tn->tnode_free = tnode_free_head;
  108. - tnode_free_head = tn;
  109. - tnode_free_size += sizeof(struct tnode) +
  110. - (sizeof(struct rt_trie_node *) << tn->bits);
  111. + tn->rcu.next = tnode_free_head;
  112. + tnode_free_head = &tn->rcu;
  113. }
  114. static void tnode_free_flush(void)
  115. {
  116. - struct tnode *tn;
  117. + struct callback_head *head;
  118. +
  119. + while ((head = tnode_free_head)) {
  120. + struct tnode *tn = container_of(head, struct tnode, rcu);
  121. +
  122. + tnode_free_head = head->next;
  123. + tnode_free_size += offsetof(struct tnode, child[1 << tn->bits]);
  124. - while ((tn = tnode_free_head)) {
  125. - tnode_free_head = tn->tnode_free;
  126. - tn->tnode_free = NULL;
  127. - tnode_free(tn);
  128. + node_free(tn);
  129. }
  130. if (tnode_free_size >= PAGE_SIZE * sync_pages) {
  131. @@ -437,7 +427,7 @@ static struct leaf_info *leaf_info_new(i
  132. static struct tnode *tnode_new(t_key key, int pos, int bits)
  133. {
  134. - size_t sz = sizeof(struct tnode) + (sizeof(struct rt_trie_node *) << bits);
  135. + size_t sz = offsetof(struct tnode, child[1 << bits]);
  136. struct tnode *tn = tnode_alloc(sz);
  137. unsigned int shift = pos + bits;
  138. @@ -666,15 +656,15 @@ no_children:
  139. static void tnode_clean_free(struct tnode *tn)
  140. {
  141. + struct rt_trie_node *tofree;
  142. int i;
  143. - struct tnode *tofree;
  144. for (i = 0; i < tnode_child_length(tn); i++) {
  145. - tofree = (struct tnode *)rtnl_dereference(tn->child[i]);
  146. + tofree = rtnl_dereference(tn->child[i]);
  147. if (tofree)
  148. - tnode_free(tofree);
  149. + node_free(tofree);
  150. }
  151. - tnode_free(tn);
  152. + node_free(tn);
  153. }
  154. static struct tnode *inflate(struct trie *t, struct tnode *tn)
  155. @@ -717,7 +707,7 @@ static struct tnode *inflate(struct trie
  156. inode->bits - 1);
  157. if (!right) {
  158. - tnode_free(left);
  159. + node_free(left);
  160. goto nomem;
  161. }
  162. @@ -1068,7 +1058,7 @@ static struct list_head *fib_insert_node
  163. li = leaf_info_new(plen);
  164. if (!li) {
  165. - free_leaf(l);
  166. + node_free(l);
  167. return NULL;
  168. }
  169. @@ -1100,7 +1090,7 @@ static struct list_head *fib_insert_node
  170. if (!tn) {
  171. free_leaf_info(li);
  172. - free_leaf(l);
  173. + node_free(l);
  174. return NULL;
  175. }
  176. @@ -1580,7 +1570,7 @@ static void trie_leaf_remove(struct trie
  177. } else
  178. RCU_INIT_POINTER(t->trie, NULL);
  179. - free_leaf(l);
  180. + node_free(l);
  181. }
  182. /*