klist.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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/export.h>
  38. #include <linux/sched.h>
  39. /*
  40. * Use the lowest bit of n_klist to mark deleted nodes and exclude
  41. * dead ones from iteration.
  42. */
  43. #define KNODE_DEAD 1LU
  44. #define KNODE_KLIST_MASK ~KNODE_DEAD
  45. static struct klist *knode_klist(struct klist_node *knode)
  46. {
  47. return (struct klist *)
  48. ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
  49. }
  50. static bool knode_dead(struct klist_node *knode)
  51. {
  52. return (unsigned long)knode->n_klist & KNODE_DEAD;
  53. }
  54. static void knode_set_klist(struct klist_node *knode, struct klist *klist)
  55. {
  56. knode->n_klist = klist;
  57. /* no knode deserves to start its life dead */
  58. WARN_ON(knode_dead(knode));
  59. }
  60. static void knode_kill(struct klist_node *knode)
  61. {
  62. /* and no knode should die twice ever either, see we're very humane */
  63. WARN_ON(knode_dead(knode));
  64. *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
  65. }
  66. /**
  67. * klist_init - Initialize a klist structure.
  68. * @k: The klist we're initializing.
  69. * @get: The get function for the embedding object (NULL if none)
  70. * @put: The put function for the embedding object (NULL if none)
  71. *
  72. * Initialises the klist structure. If the klist_node structures are
  73. * going to be embedded in refcounted objects (necessary for safe
  74. * deletion) then the get/put arguments are used to initialise
  75. * functions that take and release references on the embedding
  76. * objects.
  77. */
  78. void klist_init(struct klist *k, void (*get)(struct klist_node *),
  79. void (*put)(struct klist_node *))
  80. {
  81. INIT_LIST_HEAD(&k->k_list);
  82. spin_lock_init(&k->k_lock);
  83. k->get = get;
  84. k->put = put;
  85. }
  86. EXPORT_SYMBOL_GPL(klist_init);
  87. static void add_head(struct klist *k, struct klist_node *n)
  88. {
  89. spin_lock(&k->k_lock);
  90. list_add(&n->n_node, &k->k_list);
  91. spin_unlock(&k->k_lock);
  92. }
  93. static void add_tail(struct klist *k, struct klist_node *n)
  94. {
  95. spin_lock(&k->k_lock);
  96. list_add_tail(&n->n_node, &k->k_list);
  97. spin_unlock(&k->k_lock);
  98. }
  99. static void klist_node_init(struct klist *k, struct klist_node *n)
  100. {
  101. INIT_LIST_HEAD(&n->n_node);
  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_behind - 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_behind(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_behind);
  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. struct klist_waiter {
  158. struct list_head list;
  159. struct klist_node *node;
  160. struct task_struct *process;
  161. int woken;
  162. };
  163. static DEFINE_SPINLOCK(klist_remove_lock);
  164. static LIST_HEAD(klist_remove_waiters);
  165. static void klist_release(struct kref *kref)
  166. {
  167. struct klist_waiter *waiter, *tmp;
  168. struct klist_node *n = container_of(kref, struct klist_node, n_ref);
  169. WARN_ON(!knode_dead(n));
  170. list_del(&n->n_node);
  171. spin_lock(&klist_remove_lock);
  172. list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
  173. if (waiter->node != n)
  174. continue;
  175. list_del(&waiter->list);
  176. waiter->woken = 1;
  177. mb();
  178. wake_up_process(waiter->process);
  179. }
  180. spin_unlock(&klist_remove_lock);
  181. knode_set_klist(n, NULL);
  182. }
  183. static int klist_dec_and_del(struct klist_node *n)
  184. {
  185. return kref_put(&n->n_ref, klist_release);
  186. }
  187. static void klist_put(struct klist_node *n, bool kill)
  188. {
  189. struct klist *k = knode_klist(n);
  190. void (*put)(struct klist_node *) = k->put;
  191. spin_lock(&k->k_lock);
  192. if (kill)
  193. knode_kill(n);
  194. if (!klist_dec_and_del(n))
  195. put = NULL;
  196. spin_unlock(&k->k_lock);
  197. if (put)
  198. put(n);
  199. }
  200. /**
  201. * klist_del - Decrement the reference count of node and try to remove.
  202. * @n: node we're deleting.
  203. */
  204. void klist_del(struct klist_node *n)
  205. {
  206. klist_put(n, true);
  207. }
  208. EXPORT_SYMBOL_GPL(klist_del);
  209. /**
  210. * klist_remove - Decrement the refcount of node and wait for it to go away.
  211. * @n: node we're removing.
  212. */
  213. void klist_remove(struct klist_node *n)
  214. {
  215. struct klist_waiter waiter;
  216. waiter.node = n;
  217. waiter.process = current;
  218. waiter.woken = 0;
  219. spin_lock(&klist_remove_lock);
  220. list_add(&waiter.list, &klist_remove_waiters);
  221. spin_unlock(&klist_remove_lock);
  222. klist_del(n);
  223. for (;;) {
  224. set_current_state(TASK_UNINTERRUPTIBLE);
  225. if (waiter.woken)
  226. break;
  227. schedule();
  228. }
  229. __set_current_state(TASK_RUNNING);
  230. }
  231. EXPORT_SYMBOL_GPL(klist_remove);
  232. /**
  233. * klist_node_attached - Say whether a node is bound to a list or not.
  234. * @n: Node that we're testing.
  235. */
  236. int klist_node_attached(struct klist_node *n)
  237. {
  238. return (n->n_klist != NULL);
  239. }
  240. EXPORT_SYMBOL_GPL(klist_node_attached);
  241. /**
  242. * klist_iter_init_node - Initialize a klist_iter structure.
  243. * @k: klist we're iterating.
  244. * @i: klist_iter we're filling.
  245. * @n: node to start with.
  246. *
  247. * Similar to klist_iter_init(), but starts the action off with @n,
  248. * instead of with the list head.
  249. */
  250. void klist_iter_init_node(struct klist *k, struct klist_iter *i,
  251. struct klist_node *n)
  252. {
  253. i->i_klist = k;
  254. i->i_cur = NULL;
  255. if (n && kref_get_unless_zero(&n->n_ref))
  256. i->i_cur = n;
  257. }
  258. EXPORT_SYMBOL_GPL(klist_iter_init_node);
  259. /**
  260. * klist_iter_init - Iniitalize a klist_iter structure.
  261. * @k: klist we're iterating.
  262. * @i: klist_iter structure we're filling.
  263. *
  264. * Similar to klist_iter_init_node(), but start with the list head.
  265. */
  266. void klist_iter_init(struct klist *k, struct klist_iter *i)
  267. {
  268. klist_iter_init_node(k, i, NULL);
  269. }
  270. EXPORT_SYMBOL_GPL(klist_iter_init);
  271. /**
  272. * klist_iter_exit - Finish a list iteration.
  273. * @i: Iterator structure.
  274. *
  275. * Must be called when done iterating over list, as it decrements the
  276. * refcount of the current node. Necessary in case iteration exited before
  277. * the end of the list was reached, and always good form.
  278. */
  279. void klist_iter_exit(struct klist_iter *i)
  280. {
  281. if (i->i_cur) {
  282. klist_put(i->i_cur, false);
  283. i->i_cur = NULL;
  284. }
  285. }
  286. EXPORT_SYMBOL_GPL(klist_iter_exit);
  287. static struct klist_node *to_klist_node(struct list_head *n)
  288. {
  289. return container_of(n, struct klist_node, n_node);
  290. }
  291. /**
  292. * klist_prev - Ante up prev node in list.
  293. * @i: Iterator structure.
  294. *
  295. * First grab list lock. Decrement the reference count of the previous
  296. * node, if there was one. Grab the prev node, increment its reference
  297. * count, drop the lock, and return that prev node.
  298. */
  299. struct klist_node *klist_prev(struct klist_iter *i)
  300. {
  301. void (*put)(struct klist_node *) = i->i_klist->put;
  302. struct klist_node *last = i->i_cur;
  303. struct klist_node *prev;
  304. spin_lock(&i->i_klist->k_lock);
  305. if (last) {
  306. prev = to_klist_node(last->n_node.prev);
  307. if (!klist_dec_and_del(last))
  308. put = NULL;
  309. } else
  310. prev = to_klist_node(i->i_klist->k_list.prev);
  311. i->i_cur = NULL;
  312. while (prev != to_klist_node(&i->i_klist->k_list)) {
  313. if (likely(!knode_dead(prev))) {
  314. kref_get(&prev->n_ref);
  315. i->i_cur = prev;
  316. break;
  317. }
  318. prev = to_klist_node(prev->n_node.prev);
  319. }
  320. spin_unlock(&i->i_klist->k_lock);
  321. if (put && last)
  322. put(last);
  323. return i->i_cur;
  324. }
  325. EXPORT_SYMBOL_GPL(klist_prev);
  326. /**
  327. * klist_next - Ante up next node in list.
  328. * @i: Iterator structure.
  329. *
  330. * First grab list lock. Decrement the reference count of the previous
  331. * node, if there was one. Grab the next node, increment its reference
  332. * count, drop the lock, and return that next node.
  333. */
  334. struct klist_node *klist_next(struct klist_iter *i)
  335. {
  336. void (*put)(struct klist_node *) = i->i_klist->put;
  337. struct klist_node *last = i->i_cur;
  338. struct klist_node *next;
  339. spin_lock(&i->i_klist->k_lock);
  340. if (last) {
  341. next = to_klist_node(last->n_node.next);
  342. if (!klist_dec_and_del(last))
  343. put = NULL;
  344. } else
  345. next = to_klist_node(i->i_klist->k_list.next);
  346. i->i_cur = NULL;
  347. while (next != to_klist_node(&i->i_klist->k_list)) {
  348. if (likely(!knode_dead(next))) {
  349. kref_get(&next->n_ref);
  350. i->i_cur = next;
  351. break;
  352. }
  353. next = to_klist_node(next->n_node.next);
  354. }
  355. spin_unlock(&i->i_klist->k_lock);
  356. if (put && last)
  357. put(last);
  358. return i->i_cur;
  359. }
  360. EXPORT_SYMBOL_GPL(klist_next);