xref: /linux/lib/klist.c (revision 0f9859ca)
19a19fea4Smochel@digitalimplant.org /*
29a19fea4Smochel@digitalimplant.org  * klist.c - Routines for manipulating klists.
39a19fea4Smochel@digitalimplant.org  *
4c3bb7fadSGreg Kroah-Hartman  * Copyright (C) 2005 Patrick Mochel
5c3bb7fadSGreg Kroah-Hartman  *
6c3bb7fadSGreg Kroah-Hartman  * This file is released under the GPL v2.
79a19fea4Smochel@digitalimplant.org  *
89a19fea4Smochel@digitalimplant.org  * This klist interface provides a couple of structures that wrap around
9c3bb7fadSGreg Kroah-Hartman  * struct list_head to provide explicit list "head" (struct klist) and list
10c3bb7fadSGreg Kroah-Hartman  * "node" (struct klist_node) objects. For struct klist, a spinlock is
11c3bb7fadSGreg Kroah-Hartman  * included that protects access to the actual list itself. struct
129a19fea4Smochel@digitalimplant.org  * klist_node provides a pointer to the klist that owns it and a kref
139a19fea4Smochel@digitalimplant.org  * reference count that indicates the number of current users of that node
149a19fea4Smochel@digitalimplant.org  * in the list.
159a19fea4Smochel@digitalimplant.org  *
169a19fea4Smochel@digitalimplant.org  * The entire point is to provide an interface for iterating over a list
179a19fea4Smochel@digitalimplant.org  * that is safe and allows for modification of the list during the
189a19fea4Smochel@digitalimplant.org  * iteration (e.g. insertion and removal), including modification of the
199a19fea4Smochel@digitalimplant.org  * current node on the list.
209a19fea4Smochel@digitalimplant.org  *
219a19fea4Smochel@digitalimplant.org  * It works using a 3rd object type - struct klist_iter - that is declared
229a19fea4Smochel@digitalimplant.org  * and initialized before an iteration. klist_next() is used to acquire the
239a19fea4Smochel@digitalimplant.org  * next element in the list. It returns NULL if there are no more items.
24c3bb7fadSGreg Kroah-Hartman  * Internally, that routine takes the klist's lock, decrements the
25c3bb7fadSGreg Kroah-Hartman  * reference count of the previous klist_node and increments the count of
26c3bb7fadSGreg Kroah-Hartman  * the next klist_node. It then drops the lock and returns.
279a19fea4Smochel@digitalimplant.org  *
289a19fea4Smochel@digitalimplant.org  * There are primitives for adding and removing nodes to/from a klist.
299a19fea4Smochel@digitalimplant.org  * When deleting, klist_del() will simply decrement the reference count.
309a19fea4Smochel@digitalimplant.org  * Only when the count goes to 0 is the node removed from the list.
31c3bb7fadSGreg Kroah-Hartman  * klist_remove() will try to delete the node from the list and block until
32c3bb7fadSGreg Kroah-Hartman  * it is actually removed. This is useful for objects (like devices) that
33c3bb7fadSGreg Kroah-Hartman  * have been removed from the system and must be freed (but must wait until
34c3bb7fadSGreg Kroah-Hartman  * all accessors have finished).
359a19fea4Smochel@digitalimplant.org  */
369a19fea4Smochel@digitalimplant.org 
379a19fea4Smochel@digitalimplant.org #include <linux/klist.h>
388bc3bcc9SPaul Gortmaker #include <linux/export.h>
39210272a2SMatthew Wilcox #include <linux/sched.h>
409a19fea4Smochel@digitalimplant.org 
41a1ed5b0cSTejun Heo /*
42a1ed5b0cSTejun Heo  * Use the lowest bit of n_klist to mark deleted nodes and exclude
43a1ed5b0cSTejun Heo  * dead ones from iteration.
44a1ed5b0cSTejun Heo  */
45a1ed5b0cSTejun Heo #define KNODE_DEAD		1LU
46a1ed5b0cSTejun Heo #define KNODE_KLIST_MASK	~KNODE_DEAD
47a1ed5b0cSTejun Heo 
48a1ed5b0cSTejun Heo static struct klist *knode_klist(struct klist_node *knode)
49a1ed5b0cSTejun Heo {
50a1ed5b0cSTejun Heo 	return (struct klist *)
51a1ed5b0cSTejun Heo 		((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
52a1ed5b0cSTejun Heo }
53a1ed5b0cSTejun Heo 
54a1ed5b0cSTejun Heo static bool knode_dead(struct klist_node *knode)
55a1ed5b0cSTejun Heo {
56a1ed5b0cSTejun Heo 	return (unsigned long)knode->n_klist & KNODE_DEAD;
57a1ed5b0cSTejun Heo }
58a1ed5b0cSTejun Heo 
59a1ed5b0cSTejun Heo static void knode_set_klist(struct klist_node *knode, struct klist *klist)
60a1ed5b0cSTejun Heo {
61a1ed5b0cSTejun Heo 	knode->n_klist = klist;
62a1ed5b0cSTejun Heo 	/* no knode deserves to start its life dead */
63a1ed5b0cSTejun Heo 	WARN_ON(knode_dead(knode));
64a1ed5b0cSTejun Heo }
65a1ed5b0cSTejun Heo 
66a1ed5b0cSTejun Heo static void knode_kill(struct klist_node *knode)
67a1ed5b0cSTejun Heo {
68a1ed5b0cSTejun Heo 	/* and no knode should die twice ever either, see we're very humane */
69a1ed5b0cSTejun Heo 	WARN_ON(knode_dead(knode));
70a1ed5b0cSTejun Heo 	*(unsigned long *)&knode->n_klist |= KNODE_DEAD;
71a1ed5b0cSTejun Heo }
729a19fea4Smochel@digitalimplant.org 
739a19fea4Smochel@digitalimplant.org /**
749a19fea4Smochel@digitalimplant.org  * klist_init - Initialize a klist structure.
759a19fea4Smochel@digitalimplant.org  * @k: The klist we're initializing.
7634bb61f9SJames Bottomley  * @get: The get function for the embedding object (NULL if none)
7734bb61f9SJames Bottomley  * @put: The put function for the embedding object (NULL if none)
7834bb61f9SJames Bottomley  *
7934bb61f9SJames Bottomley  * Initialises the klist structure.  If the klist_node structures are
8034bb61f9SJames Bottomley  * going to be embedded in refcounted objects (necessary for safe
8134bb61f9SJames Bottomley  * deletion) then the get/put arguments are used to initialise
8234bb61f9SJames Bottomley  * functions that take and release references on the embedding
8334bb61f9SJames Bottomley  * objects.
849a19fea4Smochel@digitalimplant.org  */
8534bb61f9SJames Bottomley void klist_init(struct klist *k, void (*get)(struct klist_node *),
8634bb61f9SJames Bottomley 		void (*put)(struct klist_node *))
879a19fea4Smochel@digitalimplant.org {
889a19fea4Smochel@digitalimplant.org 	INIT_LIST_HEAD(&k->k_list);
899a19fea4Smochel@digitalimplant.org 	spin_lock_init(&k->k_lock);
9034bb61f9SJames Bottomley 	k->get = get;
9134bb61f9SJames Bottomley 	k->put = put;
929a19fea4Smochel@digitalimplant.org }
939a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_init);
949a19fea4Smochel@digitalimplant.org 
959a19fea4Smochel@digitalimplant.org static void add_head(struct klist *k, struct klist_node *n)
969a19fea4Smochel@digitalimplant.org {
979a19fea4Smochel@digitalimplant.org 	spin_lock(&k->k_lock);
989a19fea4Smochel@digitalimplant.org 	list_add(&n->n_node, &k->k_list);
999a19fea4Smochel@digitalimplant.org 	spin_unlock(&k->k_lock);
1009a19fea4Smochel@digitalimplant.org }
1019a19fea4Smochel@digitalimplant.org 
1029a19fea4Smochel@digitalimplant.org static void add_tail(struct klist *k, struct klist_node *n)
1039a19fea4Smochel@digitalimplant.org {
1049a19fea4Smochel@digitalimplant.org 	spin_lock(&k->k_lock);
1059a19fea4Smochel@digitalimplant.org 	list_add_tail(&n->n_node, &k->k_list);
1069a19fea4Smochel@digitalimplant.org 	spin_unlock(&k->k_lock);
1079a19fea4Smochel@digitalimplant.org }
1089a19fea4Smochel@digitalimplant.org 
1099a19fea4Smochel@digitalimplant.org static void klist_node_init(struct klist *k, struct klist_node *n)
1109a19fea4Smochel@digitalimplant.org {
1119a19fea4Smochel@digitalimplant.org 	INIT_LIST_HEAD(&n->n_node);
1129a19fea4Smochel@digitalimplant.org 	kref_init(&n->n_ref);
113a1ed5b0cSTejun Heo 	knode_set_klist(n, k);
11434bb61f9SJames Bottomley 	if (k->get)
11534bb61f9SJames Bottomley 		k->get(n);
1169a19fea4Smochel@digitalimplant.org }
1179a19fea4Smochel@digitalimplant.org 
1189a19fea4Smochel@digitalimplant.org /**
1199a19fea4Smochel@digitalimplant.org  * klist_add_head - Initialize a klist_node and add it to front.
1209a19fea4Smochel@digitalimplant.org  * @n: node we're adding.
121d856f1e3SJames Bottomley  * @k: klist it's going on.
1229a19fea4Smochel@digitalimplant.org  */
123d856f1e3SJames Bottomley void klist_add_head(struct klist_node *n, struct klist *k)
1249a19fea4Smochel@digitalimplant.org {
1259a19fea4Smochel@digitalimplant.org 	klist_node_init(k, n);
1269a19fea4Smochel@digitalimplant.org 	add_head(k, n);
1279a19fea4Smochel@digitalimplant.org }
1289a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_add_head);
1299a19fea4Smochel@digitalimplant.org 
1309a19fea4Smochel@digitalimplant.org /**
1319a19fea4Smochel@digitalimplant.org  * klist_add_tail - Initialize a klist_node and add it to back.
1329a19fea4Smochel@digitalimplant.org  * @n: node we're adding.
133d856f1e3SJames Bottomley  * @k: klist it's going on.
1349a19fea4Smochel@digitalimplant.org  */
135d856f1e3SJames Bottomley void klist_add_tail(struct klist_node *n, struct klist *k)
1369a19fea4Smochel@digitalimplant.org {
1379a19fea4Smochel@digitalimplant.org 	klist_node_init(k, n);
1389a19fea4Smochel@digitalimplant.org 	add_tail(k, n);
1399a19fea4Smochel@digitalimplant.org }
1409a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_add_tail);
1419a19fea4Smochel@digitalimplant.org 
14293dd4001STejun Heo /**
143*0f9859caSKen Helias  * klist_add_behind - Init a klist_node and add it after an existing node
14493dd4001STejun Heo  * @n: node we're adding.
14593dd4001STejun Heo  * @pos: node to put @n after
14693dd4001STejun Heo  */
147*0f9859caSKen Helias void klist_add_behind(struct klist_node *n, struct klist_node *pos)
14893dd4001STejun Heo {
149a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(pos);
15093dd4001STejun Heo 
15193dd4001STejun Heo 	klist_node_init(k, n);
15293dd4001STejun Heo 	spin_lock(&k->k_lock);
15393dd4001STejun Heo 	list_add(&n->n_node, &pos->n_node);
15493dd4001STejun Heo 	spin_unlock(&k->k_lock);
15593dd4001STejun Heo }
156*0f9859caSKen Helias EXPORT_SYMBOL_GPL(klist_add_behind);
15793dd4001STejun Heo 
15893dd4001STejun Heo /**
15993dd4001STejun Heo  * klist_add_before - Init a klist_node and add it before an existing node
16093dd4001STejun Heo  * @n: node we're adding.
16193dd4001STejun Heo  * @pos: node to put @n after
16293dd4001STejun Heo  */
16393dd4001STejun Heo void klist_add_before(struct klist_node *n, struct klist_node *pos)
16493dd4001STejun Heo {
165a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(pos);
16693dd4001STejun Heo 
16793dd4001STejun Heo 	klist_node_init(k, n);
16893dd4001STejun Heo 	spin_lock(&k->k_lock);
16993dd4001STejun Heo 	list_add_tail(&n->n_node, &pos->n_node);
17093dd4001STejun Heo 	spin_unlock(&k->k_lock);
17193dd4001STejun Heo }
17293dd4001STejun Heo EXPORT_SYMBOL_GPL(klist_add_before);
17393dd4001STejun Heo 
174210272a2SMatthew Wilcox struct klist_waiter {
175210272a2SMatthew Wilcox 	struct list_head list;
176210272a2SMatthew Wilcox 	struct klist_node *node;
177210272a2SMatthew Wilcox 	struct task_struct *process;
178210272a2SMatthew Wilcox 	int woken;
179210272a2SMatthew Wilcox };
180210272a2SMatthew Wilcox 
181210272a2SMatthew Wilcox static DEFINE_SPINLOCK(klist_remove_lock);
182210272a2SMatthew Wilcox static LIST_HEAD(klist_remove_waiters);
183210272a2SMatthew Wilcox 
1849a19fea4Smochel@digitalimplant.org static void klist_release(struct kref *kref)
1859a19fea4Smochel@digitalimplant.org {
186210272a2SMatthew Wilcox 	struct klist_waiter *waiter, *tmp;
1879a19fea4Smochel@digitalimplant.org 	struct klist_node *n = container_of(kref, struct klist_node, n_ref);
1887e9f4b2dSAlan Stern 
189a1ed5b0cSTejun Heo 	WARN_ON(!knode_dead(n));
1909a19fea4Smochel@digitalimplant.org 	list_del(&n->n_node);
191210272a2SMatthew Wilcox 	spin_lock(&klist_remove_lock);
192210272a2SMatthew Wilcox 	list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193210272a2SMatthew Wilcox 		if (waiter->node != n)
194210272a2SMatthew Wilcox 			continue;
195210272a2SMatthew Wilcox 
196ac5a2962Swang, biao 		list_del(&waiter->list);
197210272a2SMatthew Wilcox 		waiter->woken = 1;
198210272a2SMatthew Wilcox 		mb();
199210272a2SMatthew Wilcox 		wake_up_process(waiter->process);
200210272a2SMatthew Wilcox 	}
201210272a2SMatthew Wilcox 	spin_unlock(&klist_remove_lock);
202a1ed5b0cSTejun Heo 	knode_set_klist(n, NULL);
2039a19fea4Smochel@digitalimplant.org }
2049a19fea4Smochel@digitalimplant.org 
2059a19fea4Smochel@digitalimplant.org static int klist_dec_and_del(struct klist_node *n)
2069a19fea4Smochel@digitalimplant.org {
2079a19fea4Smochel@digitalimplant.org 	return kref_put(&n->n_ref, klist_release);
2089a19fea4Smochel@digitalimplant.org }
2099a19fea4Smochel@digitalimplant.org 
210a1ed5b0cSTejun Heo static void klist_put(struct klist_node *n, bool kill)
211a1ed5b0cSTejun Heo {
212a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(n);
213a1ed5b0cSTejun Heo 	void (*put)(struct klist_node *) = k->put;
214a1ed5b0cSTejun Heo 
215a1ed5b0cSTejun Heo 	spin_lock(&k->k_lock);
216a1ed5b0cSTejun Heo 	if (kill)
217a1ed5b0cSTejun Heo 		knode_kill(n);
218a1ed5b0cSTejun Heo 	if (!klist_dec_and_del(n))
219a1ed5b0cSTejun Heo 		put = NULL;
220a1ed5b0cSTejun Heo 	spin_unlock(&k->k_lock);
221a1ed5b0cSTejun Heo 	if (put)
222a1ed5b0cSTejun Heo 		put(n);
223a1ed5b0cSTejun Heo }
224a1ed5b0cSTejun Heo 
2259a19fea4Smochel@digitalimplant.org /**
2269a19fea4Smochel@digitalimplant.org  * klist_del - Decrement the reference count of node and try to remove.
2279a19fea4Smochel@digitalimplant.org  * @n: node we're deleting.
2289a19fea4Smochel@digitalimplant.org  */
2299a19fea4Smochel@digitalimplant.org void klist_del(struct klist_node *n)
2309a19fea4Smochel@digitalimplant.org {
231a1ed5b0cSTejun Heo 	klist_put(n, true);
2329a19fea4Smochel@digitalimplant.org }
2339a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_del);
2349a19fea4Smochel@digitalimplant.org 
2359a19fea4Smochel@digitalimplant.org /**
2369a19fea4Smochel@digitalimplant.org  * klist_remove - Decrement the refcount of node and wait for it to go away.
2379a19fea4Smochel@digitalimplant.org  * @n: node we're removing.
2389a19fea4Smochel@digitalimplant.org  */
2399a19fea4Smochel@digitalimplant.org void klist_remove(struct klist_node *n)
2409a19fea4Smochel@digitalimplant.org {
241210272a2SMatthew Wilcox 	struct klist_waiter waiter;
242210272a2SMatthew Wilcox 
243210272a2SMatthew Wilcox 	waiter.node = n;
244210272a2SMatthew Wilcox 	waiter.process = current;
245210272a2SMatthew Wilcox 	waiter.woken = 0;
246210272a2SMatthew Wilcox 	spin_lock(&klist_remove_lock);
247210272a2SMatthew Wilcox 	list_add(&waiter.list, &klist_remove_waiters);
248210272a2SMatthew Wilcox 	spin_unlock(&klist_remove_lock);
249210272a2SMatthew Wilcox 
2507e9f4b2dSAlan Stern 	klist_del(n);
251210272a2SMatthew Wilcox 
252210272a2SMatthew Wilcox 	for (;;) {
253210272a2SMatthew Wilcox 		set_current_state(TASK_UNINTERRUPTIBLE);
254210272a2SMatthew Wilcox 		if (waiter.woken)
255210272a2SMatthew Wilcox 			break;
256210272a2SMatthew Wilcox 		schedule();
257210272a2SMatthew Wilcox 	}
258210272a2SMatthew Wilcox 	__set_current_state(TASK_RUNNING);
2599a19fea4Smochel@digitalimplant.org }
2609a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_remove);
2619a19fea4Smochel@digitalimplant.org 
2629a19fea4Smochel@digitalimplant.org /**
2638b0c250bSmochel@digitalimplant.org  * klist_node_attached - Say whether a node is bound to a list or not.
2648b0c250bSmochel@digitalimplant.org  * @n: Node that we're testing.
2658b0c250bSmochel@digitalimplant.org  */
2668b0c250bSmochel@digitalimplant.org int klist_node_attached(struct klist_node *n)
2678b0c250bSmochel@digitalimplant.org {
2688b0c250bSmochel@digitalimplant.org 	return (n->n_klist != NULL);
2698b0c250bSmochel@digitalimplant.org }
2708b0c250bSmochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_node_attached);
2718b0c250bSmochel@digitalimplant.org 
2728b0c250bSmochel@digitalimplant.org /**
2739a19fea4Smochel@digitalimplant.org  * klist_iter_init_node - Initialize a klist_iter structure.
2749a19fea4Smochel@digitalimplant.org  * @k: klist we're iterating.
2759a19fea4Smochel@digitalimplant.org  * @i: klist_iter we're filling.
2769a19fea4Smochel@digitalimplant.org  * @n: node to start with.
2779a19fea4Smochel@digitalimplant.org  *
2789a19fea4Smochel@digitalimplant.org  * Similar to klist_iter_init(), but starts the action off with @n,
2799a19fea4Smochel@digitalimplant.org  * instead of with the list head.
2809a19fea4Smochel@digitalimplant.org  */
281c3bb7fadSGreg Kroah-Hartman void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282c3bb7fadSGreg Kroah-Hartman 			  struct klist_node *n)
2839a19fea4Smochel@digitalimplant.org {
2849a19fea4Smochel@digitalimplant.org 	i->i_klist = k;
2859a19fea4Smochel@digitalimplant.org 	i->i_cur = n;
286e22dafbcSFrank Pavlic 	if (n)
287e22dafbcSFrank Pavlic 		kref_get(&n->n_ref);
2889a19fea4Smochel@digitalimplant.org }
2899a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_init_node);
2909a19fea4Smochel@digitalimplant.org 
2919a19fea4Smochel@digitalimplant.org /**
2929a19fea4Smochel@digitalimplant.org  * klist_iter_init - Iniitalize a klist_iter structure.
2939a19fea4Smochel@digitalimplant.org  * @k: klist we're iterating.
2949a19fea4Smochel@digitalimplant.org  * @i: klist_iter structure we're filling.
2959a19fea4Smochel@digitalimplant.org  *
2969a19fea4Smochel@digitalimplant.org  * Similar to klist_iter_init_node(), but start with the list head.
2979a19fea4Smochel@digitalimplant.org  */
2989a19fea4Smochel@digitalimplant.org void klist_iter_init(struct klist *k, struct klist_iter *i)
2999a19fea4Smochel@digitalimplant.org {
3009a19fea4Smochel@digitalimplant.org 	klist_iter_init_node(k, i, NULL);
3019a19fea4Smochel@digitalimplant.org }
3029a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_init);
3039a19fea4Smochel@digitalimplant.org 
3049a19fea4Smochel@digitalimplant.org /**
3059a19fea4Smochel@digitalimplant.org  * klist_iter_exit - Finish a list iteration.
3069a19fea4Smochel@digitalimplant.org  * @i: Iterator structure.
3079a19fea4Smochel@digitalimplant.org  *
3089a19fea4Smochel@digitalimplant.org  * Must be called when done iterating over list, as it decrements the
3099a19fea4Smochel@digitalimplant.org  * refcount of the current node. Necessary in case iteration exited before
3109a19fea4Smochel@digitalimplant.org  * the end of the list was reached, and always good form.
3119a19fea4Smochel@digitalimplant.org  */
3129a19fea4Smochel@digitalimplant.org void klist_iter_exit(struct klist_iter *i)
3139a19fea4Smochel@digitalimplant.org {
3149a19fea4Smochel@digitalimplant.org 	if (i->i_cur) {
315a1ed5b0cSTejun Heo 		klist_put(i->i_cur, false);
3169a19fea4Smochel@digitalimplant.org 		i->i_cur = NULL;
3179a19fea4Smochel@digitalimplant.org 	}
3189a19fea4Smochel@digitalimplant.org }
3199a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_exit);
3209a19fea4Smochel@digitalimplant.org 
3219a19fea4Smochel@digitalimplant.org static struct klist_node *to_klist_node(struct list_head *n)
3229a19fea4Smochel@digitalimplant.org {
3239a19fea4Smochel@digitalimplant.org 	return container_of(n, struct klist_node, n_node);
3249a19fea4Smochel@digitalimplant.org }
3259a19fea4Smochel@digitalimplant.org 
3269a19fea4Smochel@digitalimplant.org /**
3279a19fea4Smochel@digitalimplant.org  * klist_next - Ante up next node in list.
3289a19fea4Smochel@digitalimplant.org  * @i: Iterator structure.
3299a19fea4Smochel@digitalimplant.org  *
3309a19fea4Smochel@digitalimplant.org  * First grab list lock. Decrement the reference count of the previous
3319a19fea4Smochel@digitalimplant.org  * node, if there was one. Grab the next node, increment its reference
3329a19fea4Smochel@digitalimplant.org  * count, drop the lock, and return that next node.
3339a19fea4Smochel@digitalimplant.org  */
3349a19fea4Smochel@digitalimplant.org struct klist_node *klist_next(struct klist_iter *i)
3359a19fea4Smochel@digitalimplant.org {
3367e9f4b2dSAlan Stern 	void (*put)(struct klist_node *) = i->i_klist->put;
337a1ed5b0cSTejun Heo 	struct klist_node *last = i->i_cur;
338a1ed5b0cSTejun Heo 	struct klist_node *next;
3399a19fea4Smochel@digitalimplant.org 
3409a19fea4Smochel@digitalimplant.org 	spin_lock(&i->i_klist->k_lock);
341a1ed5b0cSTejun Heo 
342a1ed5b0cSTejun Heo 	if (last) {
343a1ed5b0cSTejun Heo 		next = to_klist_node(last->n_node.next);
344a1ed5b0cSTejun Heo 		if (!klist_dec_and_del(last))
3457e9f4b2dSAlan Stern 			put = NULL;
3469a19fea4Smochel@digitalimplant.org 	} else
347a1ed5b0cSTejun Heo 		next = to_klist_node(i->i_klist->k_list.next);
3489a19fea4Smochel@digitalimplant.org 
349a1ed5b0cSTejun Heo 	i->i_cur = NULL;
350a1ed5b0cSTejun Heo 	while (next != to_klist_node(&i->i_klist->k_list)) {
351a1ed5b0cSTejun Heo 		if (likely(!knode_dead(next))) {
352a1ed5b0cSTejun Heo 			kref_get(&next->n_ref);
353a1ed5b0cSTejun Heo 			i->i_cur = next;
354a1ed5b0cSTejun Heo 			break;
3559a19fea4Smochel@digitalimplant.org 		}
356a1ed5b0cSTejun Heo 		next = to_klist_node(next->n_node.next);
357a1ed5b0cSTejun Heo 	}
358a1ed5b0cSTejun Heo 
3599a19fea4Smochel@digitalimplant.org 	spin_unlock(&i->i_klist->k_lock);
360a1ed5b0cSTejun Heo 
361a1ed5b0cSTejun Heo 	if (put && last)
362a1ed5b0cSTejun Heo 		put(last);
363a1ed5b0cSTejun Heo 	return i->i_cur;
3649a19fea4Smochel@digitalimplant.org }
3659a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_next);
366