xref: /linux/lib/klist.c (revision 8092f73c)
1*8092f73cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29a19fea4Smochel@digitalimplant.org /*
39a19fea4Smochel@digitalimplant.org  * klist.c - Routines for manipulating klists.
49a19fea4Smochel@digitalimplant.org  *
5c3bb7fadSGreg Kroah-Hartman  * Copyright (C) 2005 Patrick Mochel
6c3bb7fadSGreg Kroah-Hartman  *
79a19fea4Smochel@digitalimplant.org  * This klist interface provides a couple of structures that wrap around
8c3bb7fadSGreg Kroah-Hartman  * struct list_head to provide explicit list "head" (struct klist) and list
9c3bb7fadSGreg Kroah-Hartman  * "node" (struct klist_node) objects. For struct klist, a spinlock is
10c3bb7fadSGreg Kroah-Hartman  * included that protects access to the actual list itself. struct
119a19fea4Smochel@digitalimplant.org  * klist_node provides a pointer to the klist that owns it and a kref
129a19fea4Smochel@digitalimplant.org  * reference count that indicates the number of current users of that node
139a19fea4Smochel@digitalimplant.org  * in the list.
149a19fea4Smochel@digitalimplant.org  *
159a19fea4Smochel@digitalimplant.org  * The entire point is to provide an interface for iterating over a list
169a19fea4Smochel@digitalimplant.org  * that is safe and allows for modification of the list during the
179a19fea4Smochel@digitalimplant.org  * iteration (e.g. insertion and removal), including modification of the
189a19fea4Smochel@digitalimplant.org  * current node on the list.
199a19fea4Smochel@digitalimplant.org  *
209a19fea4Smochel@digitalimplant.org  * It works using a 3rd object type - struct klist_iter - that is declared
219a19fea4Smochel@digitalimplant.org  * and initialized before an iteration. klist_next() is used to acquire the
229a19fea4Smochel@digitalimplant.org  * next element in the list. It returns NULL if there are no more items.
23c3bb7fadSGreg Kroah-Hartman  * Internally, that routine takes the klist's lock, decrements the
24c3bb7fadSGreg Kroah-Hartman  * reference count of the previous klist_node and increments the count of
25c3bb7fadSGreg Kroah-Hartman  * the next klist_node. It then drops the lock and returns.
269a19fea4Smochel@digitalimplant.org  *
279a19fea4Smochel@digitalimplant.org  * There are primitives for adding and removing nodes to/from a klist.
289a19fea4Smochel@digitalimplant.org  * When deleting, klist_del() will simply decrement the reference count.
299a19fea4Smochel@digitalimplant.org  * Only when the count goes to 0 is the node removed from the list.
30c3bb7fadSGreg Kroah-Hartman  * klist_remove() will try to delete the node from the list and block until
31c3bb7fadSGreg Kroah-Hartman  * it is actually removed. This is useful for objects (like devices) that
32c3bb7fadSGreg Kroah-Hartman  * have been removed from the system and must be freed (but must wait until
33c3bb7fadSGreg Kroah-Hartman  * all accessors have finished).
349a19fea4Smochel@digitalimplant.org  */
359a19fea4Smochel@digitalimplant.org 
369a19fea4Smochel@digitalimplant.org #include <linux/klist.h>
378bc3bcc9SPaul Gortmaker #include <linux/export.h>
38210272a2SMatthew Wilcox #include <linux/sched.h>
399a19fea4Smochel@digitalimplant.org 
40a1ed5b0cSTejun Heo /*
41a1ed5b0cSTejun Heo  * Use the lowest bit of n_klist to mark deleted nodes and exclude
42a1ed5b0cSTejun Heo  * dead ones from iteration.
43a1ed5b0cSTejun Heo  */
44a1ed5b0cSTejun Heo #define KNODE_DEAD		1LU
45a1ed5b0cSTejun Heo #define KNODE_KLIST_MASK	~KNODE_DEAD
46a1ed5b0cSTejun Heo 
knode_klist(struct klist_node * knode)47a1ed5b0cSTejun Heo static struct klist *knode_klist(struct klist_node *knode)
48a1ed5b0cSTejun Heo {
49a1ed5b0cSTejun Heo 	return (struct klist *)
50a1ed5b0cSTejun Heo 		((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
51a1ed5b0cSTejun Heo }
52a1ed5b0cSTejun Heo 
knode_dead(struct klist_node * knode)53a1ed5b0cSTejun Heo static bool knode_dead(struct klist_node *knode)
54a1ed5b0cSTejun Heo {
55a1ed5b0cSTejun Heo 	return (unsigned long)knode->n_klist & KNODE_DEAD;
56a1ed5b0cSTejun Heo }
57a1ed5b0cSTejun Heo 
knode_set_klist(struct klist_node * knode,struct klist * klist)58a1ed5b0cSTejun Heo static void knode_set_klist(struct klist_node *knode, struct klist *klist)
59a1ed5b0cSTejun Heo {
60a1ed5b0cSTejun Heo 	knode->n_klist = klist;
61a1ed5b0cSTejun Heo 	/* no knode deserves to start its life dead */
62a1ed5b0cSTejun Heo 	WARN_ON(knode_dead(knode));
63a1ed5b0cSTejun Heo }
64a1ed5b0cSTejun Heo 
knode_kill(struct klist_node * knode)65a1ed5b0cSTejun Heo static void knode_kill(struct klist_node *knode)
66a1ed5b0cSTejun Heo {
67a1ed5b0cSTejun Heo 	/* and no knode should die twice ever either, see we're very humane */
68a1ed5b0cSTejun Heo 	WARN_ON(knode_dead(knode));
69a1ed5b0cSTejun Heo 	*(unsigned long *)&knode->n_klist |= KNODE_DEAD;
70a1ed5b0cSTejun Heo }
719a19fea4Smochel@digitalimplant.org 
729a19fea4Smochel@digitalimplant.org /**
739a19fea4Smochel@digitalimplant.org  * klist_init - Initialize a klist structure.
749a19fea4Smochel@digitalimplant.org  * @k: The klist we're initializing.
7534bb61f9SJames Bottomley  * @get: The get function for the embedding object (NULL if none)
7634bb61f9SJames Bottomley  * @put: The put function for the embedding object (NULL if none)
7734bb61f9SJames Bottomley  *
7834bb61f9SJames Bottomley  * Initialises the klist structure.  If the klist_node structures are
7934bb61f9SJames Bottomley  * going to be embedded in refcounted objects (necessary for safe
8034bb61f9SJames Bottomley  * deletion) then the get/put arguments are used to initialise
8134bb61f9SJames Bottomley  * functions that take and release references on the embedding
8234bb61f9SJames Bottomley  * objects.
839a19fea4Smochel@digitalimplant.org  */
klist_init(struct klist * k,void (* get)(struct klist_node *),void (* put)(struct klist_node *))8434bb61f9SJames Bottomley void klist_init(struct klist *k, void (*get)(struct klist_node *),
8534bb61f9SJames Bottomley 		void (*put)(struct klist_node *))
869a19fea4Smochel@digitalimplant.org {
879a19fea4Smochel@digitalimplant.org 	INIT_LIST_HEAD(&k->k_list);
889a19fea4Smochel@digitalimplant.org 	spin_lock_init(&k->k_lock);
8934bb61f9SJames Bottomley 	k->get = get;
9034bb61f9SJames Bottomley 	k->put = put;
919a19fea4Smochel@digitalimplant.org }
929a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_init);
939a19fea4Smochel@digitalimplant.org 
add_head(struct klist * k,struct klist_node * n)949a19fea4Smochel@digitalimplant.org static void add_head(struct klist *k, struct klist_node *n)
959a19fea4Smochel@digitalimplant.org {
969a19fea4Smochel@digitalimplant.org 	spin_lock(&k->k_lock);
979a19fea4Smochel@digitalimplant.org 	list_add(&n->n_node, &k->k_list);
989a19fea4Smochel@digitalimplant.org 	spin_unlock(&k->k_lock);
999a19fea4Smochel@digitalimplant.org }
1009a19fea4Smochel@digitalimplant.org 
add_tail(struct klist * k,struct klist_node * n)1019a19fea4Smochel@digitalimplant.org static void add_tail(struct klist *k, struct klist_node *n)
1029a19fea4Smochel@digitalimplant.org {
1039a19fea4Smochel@digitalimplant.org 	spin_lock(&k->k_lock);
1049a19fea4Smochel@digitalimplant.org 	list_add_tail(&n->n_node, &k->k_list);
1059a19fea4Smochel@digitalimplant.org 	spin_unlock(&k->k_lock);
1069a19fea4Smochel@digitalimplant.org }
1079a19fea4Smochel@digitalimplant.org 
klist_node_init(struct klist * k,struct klist_node * n)1089a19fea4Smochel@digitalimplant.org static void klist_node_init(struct klist *k, struct klist_node *n)
1099a19fea4Smochel@digitalimplant.org {
1109a19fea4Smochel@digitalimplant.org 	INIT_LIST_HEAD(&n->n_node);
1119a19fea4Smochel@digitalimplant.org 	kref_init(&n->n_ref);
112a1ed5b0cSTejun Heo 	knode_set_klist(n, k);
11334bb61f9SJames Bottomley 	if (k->get)
11434bb61f9SJames Bottomley 		k->get(n);
1159a19fea4Smochel@digitalimplant.org }
1169a19fea4Smochel@digitalimplant.org 
1179a19fea4Smochel@digitalimplant.org /**
1189a19fea4Smochel@digitalimplant.org  * klist_add_head - Initialize a klist_node and add it to front.
1199a19fea4Smochel@digitalimplant.org  * @n: node we're adding.
120d856f1e3SJames Bottomley  * @k: klist it's going on.
1219a19fea4Smochel@digitalimplant.org  */
klist_add_head(struct klist_node * n,struct klist * k)122d856f1e3SJames Bottomley void klist_add_head(struct klist_node *n, struct klist *k)
1239a19fea4Smochel@digitalimplant.org {
1249a19fea4Smochel@digitalimplant.org 	klist_node_init(k, n);
1259a19fea4Smochel@digitalimplant.org 	add_head(k, n);
1269a19fea4Smochel@digitalimplant.org }
1279a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_add_head);
1289a19fea4Smochel@digitalimplant.org 
1299a19fea4Smochel@digitalimplant.org /**
1309a19fea4Smochel@digitalimplant.org  * klist_add_tail - Initialize a klist_node and add it to back.
1319a19fea4Smochel@digitalimplant.org  * @n: node we're adding.
132d856f1e3SJames Bottomley  * @k: klist it's going on.
1339a19fea4Smochel@digitalimplant.org  */
klist_add_tail(struct klist_node * n,struct klist * k)134d856f1e3SJames Bottomley void klist_add_tail(struct klist_node *n, struct klist *k)
1359a19fea4Smochel@digitalimplant.org {
1369a19fea4Smochel@digitalimplant.org 	klist_node_init(k, n);
1379a19fea4Smochel@digitalimplant.org 	add_tail(k, n);
1389a19fea4Smochel@digitalimplant.org }
1399a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_add_tail);
1409a19fea4Smochel@digitalimplant.org 
14193dd4001STejun Heo /**
1420f9859caSKen Helias  * klist_add_behind - Init a klist_node and add it after an existing node
14393dd4001STejun Heo  * @n: node we're adding.
14493dd4001STejun Heo  * @pos: node to put @n after
14593dd4001STejun Heo  */
klist_add_behind(struct klist_node * n,struct klist_node * pos)1460f9859caSKen Helias void klist_add_behind(struct klist_node *n, struct klist_node *pos)
14793dd4001STejun Heo {
148a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(pos);
14993dd4001STejun Heo 
15093dd4001STejun Heo 	klist_node_init(k, n);
15193dd4001STejun Heo 	spin_lock(&k->k_lock);
15293dd4001STejun Heo 	list_add(&n->n_node, &pos->n_node);
15393dd4001STejun Heo 	spin_unlock(&k->k_lock);
15493dd4001STejun Heo }
1550f9859caSKen Helias EXPORT_SYMBOL_GPL(klist_add_behind);
15693dd4001STejun Heo 
15793dd4001STejun Heo /**
15893dd4001STejun Heo  * klist_add_before - Init a klist_node and add it before an existing node
15993dd4001STejun Heo  * @n: node we're adding.
16093dd4001STejun Heo  * @pos: node to put @n after
16193dd4001STejun Heo  */
klist_add_before(struct klist_node * n,struct klist_node * pos)16293dd4001STejun Heo void klist_add_before(struct klist_node *n, struct klist_node *pos)
16393dd4001STejun Heo {
164a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(pos);
16593dd4001STejun Heo 
16693dd4001STejun Heo 	klist_node_init(k, n);
16793dd4001STejun Heo 	spin_lock(&k->k_lock);
16893dd4001STejun Heo 	list_add_tail(&n->n_node, &pos->n_node);
16993dd4001STejun Heo 	spin_unlock(&k->k_lock);
17093dd4001STejun Heo }
17193dd4001STejun Heo EXPORT_SYMBOL_GPL(klist_add_before);
17293dd4001STejun Heo 
173210272a2SMatthew Wilcox struct klist_waiter {
174210272a2SMatthew Wilcox 	struct list_head list;
175210272a2SMatthew Wilcox 	struct klist_node *node;
176210272a2SMatthew Wilcox 	struct task_struct *process;
177210272a2SMatthew Wilcox 	int woken;
178210272a2SMatthew Wilcox };
179210272a2SMatthew Wilcox 
180210272a2SMatthew Wilcox static DEFINE_SPINLOCK(klist_remove_lock);
181210272a2SMatthew Wilcox static LIST_HEAD(klist_remove_waiters);
182210272a2SMatthew Wilcox 
klist_release(struct kref * kref)1839a19fea4Smochel@digitalimplant.org static void klist_release(struct kref *kref)
1849a19fea4Smochel@digitalimplant.org {
185210272a2SMatthew Wilcox 	struct klist_waiter *waiter, *tmp;
1869a19fea4Smochel@digitalimplant.org 	struct klist_node *n = container_of(kref, struct klist_node, n_ref);
1877e9f4b2dSAlan Stern 
188a1ed5b0cSTejun Heo 	WARN_ON(!knode_dead(n));
1899a19fea4Smochel@digitalimplant.org 	list_del(&n->n_node);
190210272a2SMatthew Wilcox 	spin_lock(&klist_remove_lock);
191210272a2SMatthew Wilcox 	list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
192210272a2SMatthew Wilcox 		if (waiter->node != n)
193210272a2SMatthew Wilcox 			continue;
194210272a2SMatthew Wilcox 
195ac5a2962Swang, biao 		list_del(&waiter->list);
196210272a2SMatthew Wilcox 		waiter->woken = 1;
197210272a2SMatthew Wilcox 		mb();
198210272a2SMatthew Wilcox 		wake_up_process(waiter->process);
199210272a2SMatthew Wilcox 	}
200210272a2SMatthew Wilcox 	spin_unlock(&klist_remove_lock);
201a1ed5b0cSTejun Heo 	knode_set_klist(n, NULL);
2029a19fea4Smochel@digitalimplant.org }
2039a19fea4Smochel@digitalimplant.org 
klist_dec_and_del(struct klist_node * n)2049a19fea4Smochel@digitalimplant.org static int klist_dec_and_del(struct klist_node *n)
2059a19fea4Smochel@digitalimplant.org {
2069a19fea4Smochel@digitalimplant.org 	return kref_put(&n->n_ref, klist_release);
2079a19fea4Smochel@digitalimplant.org }
2089a19fea4Smochel@digitalimplant.org 
klist_put(struct klist_node * n,bool kill)209a1ed5b0cSTejun Heo static void klist_put(struct klist_node *n, bool kill)
210a1ed5b0cSTejun Heo {
211a1ed5b0cSTejun Heo 	struct klist *k = knode_klist(n);
212a1ed5b0cSTejun Heo 	void (*put)(struct klist_node *) = k->put;
213a1ed5b0cSTejun Heo 
214a1ed5b0cSTejun Heo 	spin_lock(&k->k_lock);
215a1ed5b0cSTejun Heo 	if (kill)
216a1ed5b0cSTejun Heo 		knode_kill(n);
217a1ed5b0cSTejun Heo 	if (!klist_dec_and_del(n))
218a1ed5b0cSTejun Heo 		put = NULL;
219a1ed5b0cSTejun Heo 	spin_unlock(&k->k_lock);
220a1ed5b0cSTejun Heo 	if (put)
221a1ed5b0cSTejun Heo 		put(n);
222a1ed5b0cSTejun Heo }
223a1ed5b0cSTejun Heo 
2249a19fea4Smochel@digitalimplant.org /**
2259a19fea4Smochel@digitalimplant.org  * klist_del - Decrement the reference count of node and try to remove.
2269a19fea4Smochel@digitalimplant.org  * @n: node we're deleting.
2279a19fea4Smochel@digitalimplant.org  */
klist_del(struct klist_node * n)2289a19fea4Smochel@digitalimplant.org void klist_del(struct klist_node *n)
2299a19fea4Smochel@digitalimplant.org {
230a1ed5b0cSTejun Heo 	klist_put(n, true);
2319a19fea4Smochel@digitalimplant.org }
2329a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_del);
2339a19fea4Smochel@digitalimplant.org 
2349a19fea4Smochel@digitalimplant.org /**
2359a19fea4Smochel@digitalimplant.org  * klist_remove - Decrement the refcount of node and wait for it to go away.
2369a19fea4Smochel@digitalimplant.org  * @n: node we're removing.
2379a19fea4Smochel@digitalimplant.org  */
klist_remove(struct klist_node * n)2389a19fea4Smochel@digitalimplant.org void klist_remove(struct klist_node *n)
2399a19fea4Smochel@digitalimplant.org {
240210272a2SMatthew Wilcox 	struct klist_waiter waiter;
241210272a2SMatthew Wilcox 
242210272a2SMatthew Wilcox 	waiter.node = n;
243210272a2SMatthew Wilcox 	waiter.process = current;
244210272a2SMatthew Wilcox 	waiter.woken = 0;
245210272a2SMatthew Wilcox 	spin_lock(&klist_remove_lock);
246210272a2SMatthew Wilcox 	list_add(&waiter.list, &klist_remove_waiters);
247210272a2SMatthew Wilcox 	spin_unlock(&klist_remove_lock);
248210272a2SMatthew Wilcox 
2497e9f4b2dSAlan Stern 	klist_del(n);
250210272a2SMatthew Wilcox 
251210272a2SMatthew Wilcox 	for (;;) {
252210272a2SMatthew Wilcox 		set_current_state(TASK_UNINTERRUPTIBLE);
253210272a2SMatthew Wilcox 		if (waiter.woken)
254210272a2SMatthew Wilcox 			break;
255210272a2SMatthew Wilcox 		schedule();
256210272a2SMatthew Wilcox 	}
257210272a2SMatthew Wilcox 	__set_current_state(TASK_RUNNING);
2589a19fea4Smochel@digitalimplant.org }
2599a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_remove);
2609a19fea4Smochel@digitalimplant.org 
2619a19fea4Smochel@digitalimplant.org /**
2628b0c250bSmochel@digitalimplant.org  * klist_node_attached - Say whether a node is bound to a list or not.
2638b0c250bSmochel@digitalimplant.org  * @n: Node that we're testing.
2648b0c250bSmochel@digitalimplant.org  */
klist_node_attached(struct klist_node * n)2658b0c250bSmochel@digitalimplant.org int klist_node_attached(struct klist_node *n)
2668b0c250bSmochel@digitalimplant.org {
2678b0c250bSmochel@digitalimplant.org 	return (n->n_klist != NULL);
2688b0c250bSmochel@digitalimplant.org }
2698b0c250bSmochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_node_attached);
2708b0c250bSmochel@digitalimplant.org 
2718b0c250bSmochel@digitalimplant.org /**
2729a19fea4Smochel@digitalimplant.org  * klist_iter_init_node - Initialize a klist_iter structure.
2739a19fea4Smochel@digitalimplant.org  * @k: klist we're iterating.
2749a19fea4Smochel@digitalimplant.org  * @i: klist_iter we're filling.
2759a19fea4Smochel@digitalimplant.org  * @n: node to start with.
2769a19fea4Smochel@digitalimplant.org  *
2779a19fea4Smochel@digitalimplant.org  * Similar to klist_iter_init(), but starts the action off with @n,
2789a19fea4Smochel@digitalimplant.org  * instead of with the list head.
2799a19fea4Smochel@digitalimplant.org  */
klist_iter_init_node(struct klist * k,struct klist_iter * i,struct klist_node * n)280c3bb7fadSGreg Kroah-Hartman void klist_iter_init_node(struct klist *k, struct klist_iter *i,
281c3bb7fadSGreg Kroah-Hartman 			  struct klist_node *n)
2829a19fea4Smochel@digitalimplant.org {
2839a19fea4Smochel@digitalimplant.org 	i->i_klist = k;
28400cd29b7SJames Bottomley 	i->i_cur = NULL;
28500cd29b7SJames Bottomley 	if (n && kref_get_unless_zero(&n->n_ref))
2869a19fea4Smochel@digitalimplant.org 		i->i_cur = n;
2879a19fea4Smochel@digitalimplant.org }
2889a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_init_node);
2899a19fea4Smochel@digitalimplant.org 
2909a19fea4Smochel@digitalimplant.org /**
2919a19fea4Smochel@digitalimplant.org  * klist_iter_init - Iniitalize a klist_iter structure.
2929a19fea4Smochel@digitalimplant.org  * @k: klist we're iterating.
2939a19fea4Smochel@digitalimplant.org  * @i: klist_iter structure we're filling.
2949a19fea4Smochel@digitalimplant.org  *
2959a19fea4Smochel@digitalimplant.org  * Similar to klist_iter_init_node(), but start with the list head.
2969a19fea4Smochel@digitalimplant.org  */
klist_iter_init(struct klist * k,struct klist_iter * i)2979a19fea4Smochel@digitalimplant.org void klist_iter_init(struct klist *k, struct klist_iter *i)
2989a19fea4Smochel@digitalimplant.org {
2999a19fea4Smochel@digitalimplant.org 	klist_iter_init_node(k, i, NULL);
3009a19fea4Smochel@digitalimplant.org }
3019a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_init);
3029a19fea4Smochel@digitalimplant.org 
3039a19fea4Smochel@digitalimplant.org /**
3049a19fea4Smochel@digitalimplant.org  * klist_iter_exit - Finish a list iteration.
3059a19fea4Smochel@digitalimplant.org  * @i: Iterator structure.
3069a19fea4Smochel@digitalimplant.org  *
3079a19fea4Smochel@digitalimplant.org  * Must be called when done iterating over list, as it decrements the
3089a19fea4Smochel@digitalimplant.org  * refcount of the current node. Necessary in case iteration exited before
3099a19fea4Smochel@digitalimplant.org  * the end of the list was reached, and always good form.
3109a19fea4Smochel@digitalimplant.org  */
klist_iter_exit(struct klist_iter * i)3119a19fea4Smochel@digitalimplant.org void klist_iter_exit(struct klist_iter *i)
3129a19fea4Smochel@digitalimplant.org {
3139a19fea4Smochel@digitalimplant.org 	if (i->i_cur) {
314a1ed5b0cSTejun Heo 		klist_put(i->i_cur, false);
3159a19fea4Smochel@digitalimplant.org 		i->i_cur = NULL;
3169a19fea4Smochel@digitalimplant.org 	}
3179a19fea4Smochel@digitalimplant.org }
3189a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_iter_exit);
3199a19fea4Smochel@digitalimplant.org 
to_klist_node(struct list_head * n)3209a19fea4Smochel@digitalimplant.org static struct klist_node *to_klist_node(struct list_head *n)
3219a19fea4Smochel@digitalimplant.org {
3229a19fea4Smochel@digitalimplant.org 	return container_of(n, struct klist_node, n_node);
3239a19fea4Smochel@digitalimplant.org }
3249a19fea4Smochel@digitalimplant.org 
3259a19fea4Smochel@digitalimplant.org /**
3262e0fed7fSAndy Shevchenko  * klist_prev - Ante up prev node in list.
3272e0fed7fSAndy Shevchenko  * @i: Iterator structure.
3282e0fed7fSAndy Shevchenko  *
3292e0fed7fSAndy Shevchenko  * First grab list lock. Decrement the reference count of the previous
3302e0fed7fSAndy Shevchenko  * node, if there was one. Grab the prev node, increment its reference
3312e0fed7fSAndy Shevchenko  * count, drop the lock, and return that prev node.
3322e0fed7fSAndy Shevchenko  */
klist_prev(struct klist_iter * i)3332e0fed7fSAndy Shevchenko struct klist_node *klist_prev(struct klist_iter *i)
3342e0fed7fSAndy Shevchenko {
3352e0fed7fSAndy Shevchenko 	void (*put)(struct klist_node *) = i->i_klist->put;
3362e0fed7fSAndy Shevchenko 	struct klist_node *last = i->i_cur;
3372e0fed7fSAndy Shevchenko 	struct klist_node *prev;
338624fa779SBart Van Assche 	unsigned long flags;
3392e0fed7fSAndy Shevchenko 
340624fa779SBart Van Assche 	spin_lock_irqsave(&i->i_klist->k_lock, flags);
3412e0fed7fSAndy Shevchenko 
3422e0fed7fSAndy Shevchenko 	if (last) {
3432e0fed7fSAndy Shevchenko 		prev = to_klist_node(last->n_node.prev);
3442e0fed7fSAndy Shevchenko 		if (!klist_dec_and_del(last))
3452e0fed7fSAndy Shevchenko 			put = NULL;
3462e0fed7fSAndy Shevchenko 	} else
3472e0fed7fSAndy Shevchenko 		prev = to_klist_node(i->i_klist->k_list.prev);
3482e0fed7fSAndy Shevchenko 
3492e0fed7fSAndy Shevchenko 	i->i_cur = NULL;
3502e0fed7fSAndy Shevchenko 	while (prev != to_klist_node(&i->i_klist->k_list)) {
3512e0fed7fSAndy Shevchenko 		if (likely(!knode_dead(prev))) {
3522e0fed7fSAndy Shevchenko 			kref_get(&prev->n_ref);
3532e0fed7fSAndy Shevchenko 			i->i_cur = prev;
3542e0fed7fSAndy Shevchenko 			break;
3552e0fed7fSAndy Shevchenko 		}
3562e0fed7fSAndy Shevchenko 		prev = to_klist_node(prev->n_node.prev);
3572e0fed7fSAndy Shevchenko 	}
3582e0fed7fSAndy Shevchenko 
359624fa779SBart Van Assche 	spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
3602e0fed7fSAndy Shevchenko 
3612e0fed7fSAndy Shevchenko 	if (put && last)
3622e0fed7fSAndy Shevchenko 		put(last);
3632e0fed7fSAndy Shevchenko 	return i->i_cur;
3642e0fed7fSAndy Shevchenko }
3652e0fed7fSAndy Shevchenko EXPORT_SYMBOL_GPL(klist_prev);
3662e0fed7fSAndy Shevchenko 
3672e0fed7fSAndy Shevchenko /**
3689a19fea4Smochel@digitalimplant.org  * klist_next - Ante up next node in list.
3699a19fea4Smochel@digitalimplant.org  * @i: Iterator structure.
3709a19fea4Smochel@digitalimplant.org  *
3719a19fea4Smochel@digitalimplant.org  * First grab list lock. Decrement the reference count of the previous
3729a19fea4Smochel@digitalimplant.org  * node, if there was one. Grab the next node, increment its reference
3739a19fea4Smochel@digitalimplant.org  * count, drop the lock, and return that next node.
3749a19fea4Smochel@digitalimplant.org  */
klist_next(struct klist_iter * i)3759a19fea4Smochel@digitalimplant.org struct klist_node *klist_next(struct klist_iter *i)
3769a19fea4Smochel@digitalimplant.org {
3777e9f4b2dSAlan Stern 	void (*put)(struct klist_node *) = i->i_klist->put;
378a1ed5b0cSTejun Heo 	struct klist_node *last = i->i_cur;
379a1ed5b0cSTejun Heo 	struct klist_node *next;
380624fa779SBart Van Assche 	unsigned long flags;
3819a19fea4Smochel@digitalimplant.org 
382624fa779SBart Van Assche 	spin_lock_irqsave(&i->i_klist->k_lock, flags);
383a1ed5b0cSTejun Heo 
384a1ed5b0cSTejun Heo 	if (last) {
385a1ed5b0cSTejun Heo 		next = to_klist_node(last->n_node.next);
386a1ed5b0cSTejun Heo 		if (!klist_dec_and_del(last))
3877e9f4b2dSAlan Stern 			put = NULL;
3889a19fea4Smochel@digitalimplant.org 	} else
389a1ed5b0cSTejun Heo 		next = to_klist_node(i->i_klist->k_list.next);
3909a19fea4Smochel@digitalimplant.org 
391a1ed5b0cSTejun Heo 	i->i_cur = NULL;
392a1ed5b0cSTejun Heo 	while (next != to_klist_node(&i->i_klist->k_list)) {
393a1ed5b0cSTejun Heo 		if (likely(!knode_dead(next))) {
394a1ed5b0cSTejun Heo 			kref_get(&next->n_ref);
395a1ed5b0cSTejun Heo 			i->i_cur = next;
396a1ed5b0cSTejun Heo 			break;
3979a19fea4Smochel@digitalimplant.org 		}
398a1ed5b0cSTejun Heo 		next = to_klist_node(next->n_node.next);
399a1ed5b0cSTejun Heo 	}
400a1ed5b0cSTejun Heo 
401624fa779SBart Van Assche 	spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
402a1ed5b0cSTejun Heo 
403a1ed5b0cSTejun Heo 	if (put && last)
404a1ed5b0cSTejun Heo 		put(last);
405a1ed5b0cSTejun Heo 	return i->i_cur;
4069a19fea4Smochel@digitalimplant.org }
4079a19fea4Smochel@digitalimplant.org EXPORT_SYMBOL_GPL(klist_next);
408