klist.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. * Use the lowest bit of n_klist to mark deleted nodes and exclude
  40. * dead ones from iteration.
  41. */
  42. #define KNODE_DEAD 1LU
  43. #define KNODE_KLIST_MASK ~KNODE_DEAD
  44. static struct klist *knode_klist(struct klist_node *knode)
  45. {
  46. return (struct klist *)
  47. ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
  48. }
  49. static bool knode_dead(struct klist_node *knode)
  50. {
  51. return (unsigned long)knode->n_klist & KNODE_DEAD;
  52. }
  53. static void knode_set_klist(struct klist_node *knode, struct klist *klist)
  54. {
  55. knode->n_klist = klist;
  56. /* no knode deserves to start its life dead */
  57. WARN_ON(knode_dead(knode));
  58. }
  59. static void knode_kill(struct klist_node *knode)
  60. {
  61. /* and no knode should die twice ever either, see we're very humane */
  62. WARN_ON(knode_dead(knode));
  63. *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
  64. }
  65. /**
  66. * klist_init - Initialize a klist structure.
  67. * @k: The klist we're initializing.
  68. * @get: The get function for the embedding object (NULL if none)
  69. * @put: The put function for the embedding object (NULL if none)
  70. *
  71. * Initialises the klist structure. If the klist_node structures are
  72. * going to be embedded in refcounted objects (necessary for safe
  73. * deletion) then the get/put arguments are used to initialise
  74. * functions that take and release references on the embedding
  75. * objects.
  76. */
  77. void klist_init(struct klist *k, void (*get)(struct klist_node *),
  78. void (*put)(struct klist_node *))
  79. {
  80. INIT_LIST_HEAD(&k->k_list);
  81. spin_lock_init(&k->k_lock);
  82. k->get = get;
  83. k->put = put;
  84. }
  85. EXPORT_SYMBOL_GPL(klist_init);
  86. static void add_head(struct klist *k, struct klist_node *n)
  87. {
  88. spin_lock(&k->k_lock);
  89. list_add(&n->n_node, &k->k_list);
  90. spin_unlock(&k->k_lock);
  91. }
  92. static void add_tail(struct klist *k, struct klist_node *n)
  93. {
  94. spin_lock(&k->k_lock);
  95. list_add_tail(&n->n_node, &k->k_list);
  96. spin_unlock(&k->k_lock);
  97. }
  98. static void klist_node_init(struct klist *k, struct klist_node *n)
  99. {
  100. INIT_LIST_HEAD(&n->n_node);
  101. init_completion(&n->n_removed);
  102. kref_init(&n->n_ref);
  103. knode_set_klist(n, k);
  104. if (k->get)
  105. k->get(n);
  106. }
  107. /**
  108. * klist_add_head - Initialize a klist_node and add it to front.
  109. * @n: node we're adding.
  110. * @k: klist it's going on.
  111. */
  112. void klist_add_head(struct klist_node *n, struct klist *k)
  113. {
  114. klist_node_init(k, n);
  115. add_head(k, n);
  116. }
  117. EXPORT_SYMBOL_GPL(klist_add_head);
  118. /**
  119. * klist_add_tail - Initialize a klist_node and add it to back.
  120. * @n: node we're adding.
  121. * @k: klist it's going on.
  122. */
  123. void klist_add_tail(struct klist_node *n, struct klist *k)
  124. {
  125. klist_node_init(k, n);
  126. add_tail(k, n);
  127. }
  128. EXPORT_SYMBOL_GPL(klist_add_tail);
  129. /**
  130. * klist_add_after - Init a klist_node and add it after an existing node
  131. * @n: node we're adding.
  132. * @pos: node to put @n after
  133. */
  134. void klist_add_after(struct klist_node *n, struct klist_node *pos)
  135. {
  136. struct klist *k = knode_klist(pos);
  137. klist_node_init(k, n);
  138. spin_lock(&k->k_lock);
  139. list_add(&n->n_node, &pos->n_node);
  140. spin_unlock(&k->k_lock);
  141. }
  142. EXPORT_SYMBOL_GPL(klist_add_after);
  143. /**
  144. * klist_add_before - Init a klist_node and add it before an existing node
  145. * @n: node we're adding.
  146. * @pos: node to put @n after
  147. */
  148. void klist_add_before(struct klist_node *n, struct klist_node *pos)
  149. {
  150. struct klist *k = knode_klist(pos);
  151. klist_node_init(k, n);
  152. spin_lock(&k->k_lock);
  153. list_add_tail(&n->n_node, &pos->n_node);
  154. spin_unlock(&k->k_lock);
  155. }
  156. EXPORT_SYMBOL_GPL(klist_add_before);
  157. static void klist_release(struct kref *kref)
  158. {
  159. struct klist_node *n = container_of(kref, struct klist_node, n_ref);
  160. WARN_ON(!knode_dead(n));
  161. list_del(&n->n_node);
  162. complete(&n->n_removed);
  163. knode_set_klist(n, NULL);
  164. }
  165. static int klist_dec_and_del(struct klist_node *n)
  166. {
  167. return kref_put(&n->n_ref, klist_release);
  168. }
  169. static void klist_put(struct klist_node *n, bool kill)
  170. {
  171. struct klist *k = knode_klist(n);
  172. void (*put)(struct klist_node *) = k->put;
  173. spin_lock(&k->k_lock);
  174. if (kill)
  175. knode_kill(n);
  176. if (!klist_dec_and_del(n))
  177. put = NULL;
  178. spin_unlock(&k->k_lock);
  179. if (put)
  180. put(n);
  181. }
  182. /**
  183. * klist_del - Decrement the reference count of node and try to remove.
  184. * @n: node we're deleting.
  185. */
  186. void klist_del(struct klist_node *n)
  187. {
  188. klist_put(n, true);
  189. }
  190. EXPORT_SYMBOL_GPL(klist_del);
  191. /**
  192. * klist_remove - Decrement the refcount of node and wait for it to go away.
  193. * @n: node we're removing.
  194. */
  195. void klist_remove(struct klist_node *n)
  196. {
  197. klist_del(n);
  198. wait_for_completion(&n->n_removed);
  199. }
  200. EXPORT_SYMBOL_GPL(klist_remove);
  201. /**
  202. * klist_node_attached - Say whether a node is bound to a list or not.
  203. * @n: Node that we're testing.
  204. */
  205. int klist_node_attached(struct klist_node *n)
  206. {
  207. return (n->n_klist != NULL);
  208. }
  209. EXPORT_SYMBOL_GPL(klist_node_attached);
  210. /**
  211. * klist_iter_init_node - Initialize a klist_iter structure.
  212. * @k: klist we're iterating.
  213. * @i: klist_iter we're filling.
  214. * @n: node to start with.
  215. *
  216. * Similar to klist_iter_init(), but starts the action off with @n,
  217. * instead of with the list head.
  218. */
  219. void klist_iter_init_node(struct klist *k, struct klist_iter *i,
  220. struct klist_node *n)
  221. {
  222. i->i_klist = k;
  223. i->i_cur = n;
  224. if (n)
  225. kref_get(&n->n_ref);
  226. }
  227. EXPORT_SYMBOL_GPL(klist_iter_init_node);
  228. /**
  229. * klist_iter_init - Iniitalize a klist_iter structure.
  230. * @k: klist we're iterating.
  231. * @i: klist_iter structure we're filling.
  232. *
  233. * Similar to klist_iter_init_node(), but start with the list head.
  234. */
  235. void klist_iter_init(struct klist *k, struct klist_iter *i)
  236. {
  237. klist_iter_init_node(k, i, NULL);
  238. }
  239. EXPORT_SYMBOL_GPL(klist_iter_init);
  240. /**
  241. * klist_iter_exit - Finish a list iteration.
  242. * @i: Iterator structure.
  243. *
  244. * Must be called when done iterating over list, as it decrements the
  245. * refcount of the current node. Necessary in case iteration exited before
  246. * the end of the list was reached, and always good form.
  247. */
  248. void klist_iter_exit(struct klist_iter *i)
  249. {
  250. if (i->i_cur) {
  251. klist_put(i->i_cur, false);
  252. i->i_cur = NULL;
  253. }
  254. }
  255. EXPORT_SYMBOL_GPL(klist_iter_exit);
  256. static struct klist_node *to_klist_node(struct list_head *n)
  257. {
  258. return container_of(n, struct klist_node, n_node);
  259. }
  260. /**
  261. * klist_next - Ante up next node in list.
  262. * @i: Iterator structure.
  263. *
  264. * First grab list lock. Decrement the reference count of the previous
  265. * node, if there was one. Grab the next node, increment its reference
  266. * count, drop the lock, and return that next node.
  267. */
  268. struct klist_node *klist_next(struct klist_iter *i)
  269. {
  270. void (*put)(struct klist_node *) = i->i_klist->put;
  271. struct klist_node *last = i->i_cur;
  272. struct klist_node *next;
  273. spin_lock(&i->i_klist->k_lock);
  274. if (last) {
  275. next = to_klist_node(last->n_node.next);
  276. if (!klist_dec_and_del(last))
  277. put = NULL;
  278. } else
  279. next = to_klist_node(i->i_klist->k_list.next);
  280. i->i_cur = NULL;
  281. while (next != to_klist_node(&i->i_klist->k_list)) {
  282. if (likely(!knode_dead(next))) {
  283. kref_get(&next->n_ref);
  284. i->i_cur = next;
  285. break;
  286. }
  287. next = to_klist_node(next->n_node.next);
  288. }
  289. spin_unlock(&i->i_klist->k_lock);
  290. if (put && last)
  291. put(last);
  292. return i->i_cur;
  293. }
  294. EXPORT_SYMBOL_GPL(klist_next);