2
0

080-10-fib_trie-Use-unsigned-long-for-anything-dealing-with.patch 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. From: Alexander Duyck <alexander.h.duyck@redhat.com>
  2. Date: Wed, 31 Dec 2014 10:56:18 -0800
  3. Subject: [PATCH] fib_trie: Use unsigned long for anything dealing with a
  4. shift by bits
  5. This change makes it so that anything that can be shifted by, or compared
  6. to a value shifted by bits is updated to be an unsigned long. This is
  7. mostly a precaution against an insanely huge address space that somehow
  8. starts coming close to the 2^32 root node size which would require
  9. something like 1.5 billion addresses.
  10. I chose unsigned long instead of unsigned long long since I do not believe
  11. it is possible to allocate a 32 bit tnode on a 32 bit system as the memory
  12. consumed would be 16GB + 28B which exceeds the addressible space for any
  13. one process.
  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. @@ -146,8 +146,8 @@ struct trie {
  20. #endif
  21. };
  22. -static void tnode_put_child_reorg(struct tnode *tn, int i, struct tnode *n,
  23. - int wasfull);
  24. +static void tnode_put_child_reorg(struct tnode *tn, unsigned long i,
  25. + struct tnode *n, int wasfull);
  26. static struct tnode *resize(struct trie *t, struct tnode *tn);
  27. static struct tnode *inflate(struct trie *t, struct tnode *tn);
  28. static struct tnode *halve(struct trie *t, struct tnode *tn);
  29. @@ -183,25 +183,23 @@ static inline void node_set_parent(struc
  30. /* This provides us with the number of children in this node, in the case of a
  31. * leaf this will return 0 meaning none of the children are accessible.
  32. */
  33. -static inline int tnode_child_length(const struct tnode *tn)
  34. +static inline unsigned long tnode_child_length(const struct tnode *tn)
  35. {
  36. return (1ul << tn->bits) & ~(1ul);
  37. }
  38. -/*
  39. - * caller must hold RTNL
  40. - */
  41. -static inline struct tnode *tnode_get_child(const struct tnode *tn, unsigned int i)
  42. +/* caller must hold RTNL */
  43. +static inline struct tnode *tnode_get_child(const struct tnode *tn,
  44. + unsigned long i)
  45. {
  46. BUG_ON(i >= tnode_child_length(tn));
  47. return rtnl_dereference(tn->child[i]);
  48. }
  49. -/*
  50. - * caller must hold RCU read lock or RTNL
  51. - */
  52. -static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn, unsigned int i)
  53. +/* caller must hold RCU read lock or RTNL */
  54. +static inline struct tnode *tnode_get_child_rcu(const struct tnode *tn,
  55. + unsigned long i)
  56. {
  57. BUG_ON(i >= tnode_child_length(tn));
  58. @@ -400,7 +398,7 @@ static inline int tnode_full(const struc
  59. return n && ((n->pos + n->bits) == tn->pos) && IS_TNODE(n);
  60. }
  61. -static inline void put_child(struct tnode *tn, int i,
  62. +static inline void put_child(struct tnode *tn, unsigned long i,
  63. struct tnode *n)
  64. {
  65. tnode_put_child_reorg(tn, i, n, -1);
  66. @@ -411,13 +409,13 @@ static inline void put_child(struct tnod
  67. * Update the value of full_children and empty_children.
  68. */
  69. -static void tnode_put_child_reorg(struct tnode *tn, int i, struct tnode *n,
  70. - int wasfull)
  71. +static void tnode_put_child_reorg(struct tnode *tn, unsigned long i,
  72. + struct tnode *n, int wasfull)
  73. {
  74. struct tnode *chi = rtnl_dereference(tn->child[i]);
  75. int isfull;
  76. - BUG_ON(i >= 1<<tn->bits);
  77. + BUG_ON(i >= tnode_child_length(tn));
  78. /* update emptyChildren */
  79. if (n == NULL && chi != NULL)
  80. @@ -607,10 +605,10 @@ no_children:
  81. static void tnode_clean_free(struct tnode *tn)
  82. {
  83. struct tnode *tofree;
  84. - int i;
  85. + unsigned long i;
  86. for (i = 0; i < tnode_child_length(tn); i++) {
  87. - tofree = rtnl_dereference(tn->child[i]);
  88. + tofree = tnode_get_child(tn, i);
  89. if (tofree)
  90. node_free(tofree);
  91. }
  92. @@ -619,10 +617,10 @@ static void tnode_clean_free(struct tnod
  93. static struct tnode *inflate(struct trie *t, struct tnode *oldtnode)
  94. {
  95. - int olen = tnode_child_length(oldtnode);
  96. + unsigned long olen = tnode_child_length(oldtnode);
  97. struct tnode *tn;
  98. + unsigned long i;
  99. t_key m;
  100. - int i;
  101. pr_debug("In inflate\n");
  102. @@ -664,7 +662,7 @@ static struct tnode *inflate(struct trie
  103. for (i = 0; i < olen; i++) {
  104. struct tnode *inode = tnode_get_child(oldtnode, i);
  105. struct tnode *left, *right;
  106. - int size, j;
  107. + unsigned long size, j;
  108. /* An empty child */
  109. if (inode == NULL)
  110. @@ -737,7 +735,7 @@ nomem:
  111. static struct tnode *halve(struct trie *t, struct tnode *oldtnode)
  112. {
  113. - int olen = tnode_child_length(oldtnode);
  114. + unsigned long olen = tnode_child_length(oldtnode);
  115. struct tnode *tn, *left, *right;
  116. int i;
  117. @@ -1532,9 +1530,9 @@ static int trie_flush_leaf(struct tnode
  118. static struct tnode *leaf_walk_rcu(struct tnode *p, struct tnode *c)
  119. {
  120. do {
  121. - t_key idx = c ? idx = get_index(c->key, p) + 1 : 0;
  122. + unsigned long idx = c ? idx = get_index(c->key, p) + 1 : 0;
  123. - while (idx < 1u << p->bits) {
  124. + while (idx < tnode_child_length(p)) {
  125. c = tnode_get_child_rcu(p, idx++);
  126. if (!c)
  127. continue;
  128. @@ -1786,8 +1784,8 @@ struct fib_trie_iter {
  129. static struct tnode *fib_trie_get_next(struct fib_trie_iter *iter)
  130. {
  131. + unsigned long cindex = iter->index;
  132. struct tnode *tn = iter->tnode;
  133. - unsigned int cindex = iter->index;
  134. struct tnode *p;
  135. /* A single entry routing table */
  136. @@ -1797,7 +1795,7 @@ static struct tnode *fib_trie_get_next(s
  137. pr_debug("get_next iter={node=%p index=%d depth=%d}\n",
  138. iter->tnode, iter->index, iter->depth);
  139. rescan:
  140. - while (cindex < (1<<tn->bits)) {
  141. + while (cindex < tnode_child_length(tn)) {
  142. struct tnode *n = tnode_get_child_rcu(tn, cindex);
  143. if (n) {
  144. @@ -1874,15 +1872,16 @@ static void trie_collect_stats(struct tr
  145. hlist_for_each_entry_rcu(li, &n->list, hlist)
  146. ++s->prefixes;
  147. } else {
  148. - int i;
  149. + unsigned long i;
  150. s->tnodes++;
  151. if (n->bits < MAX_STAT_DEPTH)
  152. s->nodesizes[n->bits]++;
  153. - for (i = 0; i < tnode_child_length(n); i++)
  154. + for (i = 0; i < tnode_child_length(n); i++) {
  155. if (!rcu_access_pointer(n->child[i]))
  156. s->nullpointers++;
  157. + }
  158. }
  159. }
  160. rcu_read_unlock();