klist.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * klist.c - Routines for manipulating klists.
  3. *
  4. * Copyright (C) 2005 Patrick Mochel
  5. *
  6. * This file is released under the GPL v2.
  7. *
  8. * This klist interface provides a couple of structures that wrap around
  9. * struct list_head to provide explicit list "head" (struct klist) and list
  10. * "node" (struct klist_node) objects. For struct klist, a spinlock is
  11. * included that protects access to the actual list itself. struct
  12. * klist_node provides a pointer to the klist that owns it and a kref
  13. * reference count that indicates the number of current users of that node
  14. * in the list.
  15. *
  16. * The entire point is to provide an interface for iterating over a list
  17. * that is safe and allows for modification of the list during the
  18. * iteration (e.g. insertion and removal), including modification of the
  19. * current node on the list.
  20. *
  21. * It works using a 3rd object type - struct klist_iter - that is declared
  22. * and initialized before an iteration. klist_next() is used to acquire the
  23. * next element in the list. It returns NULL if there are no more items.
  24. * Internally, that routine takes the klist's lock, decrements the
  25. * reference count of the previous klist_node and increments the count of
  26. * the next klist_node. It then drops the lock and returns.
  27. *
  28. * There are primitives for adding and removing nodes to/from a klist.
  29. * When deleting, klist_del() will simply decrement the reference count.
  30. * Only when the count goes to 0 is the node removed from the list.
  31. * klist_remove() will try to delete the node from the list and block until
  32. * it is actually removed. This is useful for objects (like devices) that
  33. * have been removed from the system and must be freed (but must wait until
  34. * all accessors have finished).
  35. */
  36. #include <linux/klist.h>
  37. #include <linux/module.h>
  38. /**
  39. * klist_init - Initialize a klist structure.
  40. * @k: The klist we're initializing.
  41. * @get: The get function for the embedding object (NULL if none)
  42. * @put: The put function for the embedding object (NULL if none)
  43. *
  44. * Initialises the klist structure. If the klist_node structures are
  45. * going to be embedded in refcounted objects (necessary for safe
  46. * deletion) then the get/put arguments are used to initialise
  47. * functions that take and release references on the embedding
  48. * objects.
  49. */
  50. void klist_init(struct klist *k, void (*get)(struct klist_node *),
  51. void (*put)(struct klist_node *))
  52. {
  53. INIT_LIST_HEAD(&k->k_list);
  54. spin_lock_init(&k->k_lock);
  55. k->get = get;
  56. k->put = put;
  57. }
  58. EXPORT_SYMBOL_GPL(klist_init);
  59. static void add_head(struct klist *k, struct klist_node *n)
  60. {
  61. spin_lock(&k->k_lock);
  62. list_add(&n->n_node, &k->k_list);
  63. spin_unlock(&k->k_lock);
  64. }
  65. static void add_tail(struct klist *k, struct klist_node *n)
  66. {
  67. spin_lock(&k->k_lock);
  68. list_add_tail(&n->n_node, &k->k_list);
  69. spin_unlock(&k->k_lock);
  70. }
  71. static void klist_node_init(struct klist *k, struct klist_node *n)
  72. {
  73. INIT_LIST_HEAD(&n->n_node);
  74. init_completion(&n->n_removed);
  75. kref_init(&n->n_ref);
  76. n->n_klist = k;
  77. if (k->get)
  78. k->get(n);
  79. }
  80. /**
  81. * klist_add_head - Initialize a klist_node and add it to front.
  82. * @n: node we're adding.
  83. * @k: klist it's going on.
  84. */
  85. void klist_add_head(struct klist_node *n, struct klist *k)
  86. {
  87. klist_node_init(k, n);
  88. add_head(k, n);
  89. }
  90. EXPORT_SYMBOL_GPL(klist_add_head);
  91. /**
  92. * klist_add_tail - Initialize a klist_node and add it to back.
  93. * @n: node we're adding.
  94. * @k: klist it's going on.
  95. */
  96. void klist_add_tail(struct klist_node *n, struct klist *k)
  97. {
  98. klist_node_init(k, n);
  99. add_tail(k, n);
  100. }
  101. EXPORT_SYMBOL_GPL(klist_add_tail);
  102. /**
  103. * klist_add_after - Init a klist_node and add it after an existing node
  104. * @n: node we're adding.
  105. * @pos: node to put @n after
  106. */
  107. void klist_add_after(struct klist_node *n, struct klist_node *pos)
  108. {
  109. struct klist *k = pos->n_klist;
  110. klist_node_init(k, n);
  111. spin_lock(&k->k_lock);
  112. list_add(&n->n_node, &pos->n_node);
  113. spin_unlock(&k->k_lock);
  114. }
  115. EXPORT_SYMBOL_GPL(klist_add_after);
  116. /**
  117. * klist_add_before - Init a klist_node and add it before an existing node
  118. * @n: node we're adding.
  119. * @pos: node to put @n after
  120. */
  121. void klist_add_before(struct klist_node *n, struct klist_node *pos)
  122. {
  123. struct klist *k = pos->n_klist;
  124. klist_node_init(k, n);
  125. spin_lock(&k->k_lock);
  126. list_add_tail(&n->n_node, &pos->n_node);
  127. spin_unlock(&k->k_lock);
  128. }
  129. EXPORT_SYMBOL_GPL(klist_add_before);
  130. static void klist_release(struct kref *kref)
  131. {
  132. struct klist_node *n = container_of(kref, struct klist_node, n_ref);
  133. list_del(&n->n_node);
  134. complete(&n->n_removed);
  135. n->n_klist = NULL;
  136. }
  137. static int klist_dec_and_del(struct klist_node *n)
  138. {
  139. return kref_put(&n->n_ref, klist_release);
  140. }
  141. /**
  142. * klist_del - Decrement the reference count of node and try to remove.
  143. * @n: node we're deleting.
  144. */
  145. void klist_del(struct klist_node *n)
  146. {
  147. struct klist *k = n->n_klist;
  148. void (*put)(struct klist_node *) = k->put;
  149. spin_lock(&k->k_lock);
  150. if (!klist_dec_and_del(n))
  151. put = NULL;
  152. spin_unlock(&k->k_lock);
  153. if (put)
  154. put(n);
  155. }
  156. EXPORT_SYMBOL_GPL(klist_del);
  157. /**
  158. * klist_remove - Decrement the refcount of node and wait for it to go away.
  159. * @n: node we're removing.
  160. */
  161. void klist_remove(struct klist_node *n)
  162. {
  163. klist_del(n);
  164. wait_for_completion(&n->n_removed);
  165. }
  166. EXPORT_SYMBOL_GPL(klist_remove);
  167. /**
  168. * klist_node_attached - Say whether a node is bound to a list or not.
  169. * @n: Node that we're testing.
  170. */
  171. int klist_node_attached(struct klist_node *n)
  172. {
  173. return (n->n_klist != NULL);
  174. }
  175. EXPORT_SYMBOL_GPL(klist_node_attached);
  176. /**
  177. * klist_iter_init_node - Initialize a klist_iter structure.
  178. * @k: klist we're iterating.
  179. * @i: klist_iter we're filling.
  180. * @n: node to start with.
  181. *
  182. * Similar to klist_iter_init(), but starts the action off with @n,
  183. * instead of with the list head.
  184. */
  185. void klist_iter_init_node(struct klist *k, struct klist_iter *i,
  186. struct klist_node *n)
  187. {
  188. i->i_klist = k;
  189. i->i_head = &k->k_list;
  190. i->i_cur = n;
  191. if (n)
  192. kref_get(&n->n_ref);
  193. }
  194. EXPORT_SYMBOL_GPL(klist_iter_init_node);
  195. /**
  196. * klist_iter_init - Iniitalize a klist_iter structure.
  197. * @k: klist we're iterating.
  198. * @i: klist_iter structure we're filling.
  199. *
  200. * Similar to klist_iter_init_node(), but start with the list head.
  201. */
  202. void klist_iter_init(struct klist *k, struct klist_iter *i)
  203. {
  204. klist_iter_init_node(k, i, NULL);
  205. }
  206. EXPORT_SYMBOL_GPL(klist_iter_init);
  207. /**
  208. * klist_iter_exit - Finish a list iteration.
  209. * @i: Iterator structure.
  210. *
  211. * Must be called when done iterating over list, as it decrements the
  212. * refcount of the current node. Necessary in case iteration exited before
  213. * the end of the list was reached, and always good form.
  214. */
  215. void klist_iter_exit(struct klist_iter *i)
  216. {
  217. if (i->i_cur) {
  218. klist_del(i->i_cur);
  219. i->i_cur = NULL;
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(klist_iter_exit);
  223. static struct klist_node *to_klist_node(struct list_head *n)
  224. {
  225. return container_of(n, struct klist_node, n_node);
  226. }
  227. /**
  228. * klist_next - Ante up next node in list.
  229. * @i: Iterator structure.
  230. *
  231. * First grab list lock. Decrement the reference count of the previous
  232. * node, if there was one. Grab the next node, increment its reference
  233. * count, drop the lock, and return that next node.
  234. */
  235. struct klist_node *klist_next(struct klist_iter *i)
  236. {
  237. struct list_head *next;
  238. struct klist_node *lnode = i->i_cur;
  239. struct klist_node *knode = NULL;
  240. void (*put)(struct klist_node *) = i->i_klist->put;
  241. spin_lock(&i->i_klist->k_lock);
  242. if (lnode) {
  243. next = lnode->n_node.next;
  244. if (!klist_dec_and_del(lnode))
  245. put = NULL;
  246. } else
  247. next = i->i_head->next;
  248. if (next != i->i_head) {
  249. knode = to_klist_node(next);
  250. kref_get(&knode->n_ref);
  251. }
  252. i->i_cur = knode;
  253. spin_unlock(&i->i_klist->k_lock);
  254. if (put && lnode)
  255. put(lnode);
  256. return knode;
  257. }
  258. EXPORT_SYMBOL_GPL(klist_next);