Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * klist.c - Routines for manipulating klists.
  4 *
  5 * Copyright (C) 2005 Patrick Mochel
  6 *
 
 
  7 * This klist interface provides a couple of structures that wrap around
  8 * struct list_head to provide explicit list "head" (struct klist) and list
  9 * "node" (struct klist_node) objects. For struct klist, a spinlock is
 10 * included that protects access to the actual list itself. struct
 11 * klist_node provides a pointer to the klist that owns it and a kref
 12 * reference count that indicates the number of current users of that node
 13 * in the list.
 14 *
 15 * The entire point is to provide an interface for iterating over a list
 16 * that is safe and allows for modification of the list during the
 17 * iteration (e.g. insertion and removal), including modification of the
 18 * current node on the list.
 19 *
 20 * It works using a 3rd object type - struct klist_iter - that is declared
 21 * and initialized before an iteration. klist_next() is used to acquire the
 22 * next element in the list. It returns NULL if there are no more items.
 23 * Internally, that routine takes the klist's lock, decrements the
 24 * reference count of the previous klist_node and increments the count of
 25 * the next klist_node. It then drops the lock and returns.
 26 *
 27 * There are primitives for adding and removing nodes to/from a klist.
 28 * When deleting, klist_del() will simply decrement the reference count.
 29 * Only when the count goes to 0 is the node removed from the list.
 30 * klist_remove() will try to delete the node from the list and block until
 31 * it is actually removed. This is useful for objects (like devices) that
 32 * have been removed from the system and must be freed (but must wait until
 33 * all accessors have finished).
 34 */
 35
 36#include <linux/klist.h>
 37#include <linux/export.h>
 38#include <linux/sched.h>
 39
 40/*
 41 * Use the lowest bit of n_klist to mark deleted nodes and exclude
 42 * dead ones from iteration.
 43 */
 44#define KNODE_DEAD		1LU
 45#define KNODE_KLIST_MASK	~KNODE_DEAD
 46
 47static struct klist *knode_klist(struct klist_node *knode)
 48{
 49	return (struct klist *)
 50		((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
 51}
 52
 53static bool knode_dead(struct klist_node *knode)
 54{
 55	return (unsigned long)knode->n_klist & KNODE_DEAD;
 56}
 57
 58static void knode_set_klist(struct klist_node *knode, struct klist *klist)
 59{
 60	knode->n_klist = klist;
 61	/* no knode deserves to start its life dead */
 62	WARN_ON(knode_dead(knode));
 63}
 64
 65static void knode_kill(struct klist_node *knode)
 66{
 67	/* and no knode should die twice ever either, see we're very humane */
 68	WARN_ON(knode_dead(knode));
 69	*(unsigned long *)&knode->n_klist |= KNODE_DEAD;
 70}
 71
 72/**
 73 * klist_init - Initialize a klist structure.
 74 * @k: The klist we're initializing.
 75 * @get: The get function for the embedding object (NULL if none)
 76 * @put: The put function for the embedding object (NULL if none)
 77 *
 78 * Initialises the klist structure.  If the klist_node structures are
 79 * going to be embedded in refcounted objects (necessary for safe
 80 * deletion) then the get/put arguments are used to initialise
 81 * functions that take and release references on the embedding
 82 * objects.
 83 */
 84void klist_init(struct klist *k, void (*get)(struct klist_node *),
 85		void (*put)(struct klist_node *))
 86{
 87	INIT_LIST_HEAD(&k->k_list);
 88	spin_lock_init(&k->k_lock);
 89	k->get = get;
 90	k->put = put;
 91}
 92EXPORT_SYMBOL_GPL(klist_init);
 93
 94static void add_head(struct klist *k, struct klist_node *n)
 95{
 96	spin_lock(&k->k_lock);
 97	list_add(&n->n_node, &k->k_list);
 98	spin_unlock(&k->k_lock);
 99}
100
101static void add_tail(struct klist *k, struct klist_node *n)
102{
103	spin_lock(&k->k_lock);
104	list_add_tail(&n->n_node, &k->k_list);
105	spin_unlock(&k->k_lock);
106}
107
108static void klist_node_init(struct klist *k, struct klist_node *n)
109{
110	INIT_LIST_HEAD(&n->n_node);
111	kref_init(&n->n_ref);
112	knode_set_klist(n, k);
113	if (k->get)
114		k->get(n);
115}
116
117/**
118 * klist_add_head - Initialize a klist_node and add it to front.
119 * @n: node we're adding.
120 * @k: klist it's going on.
121 */
122void klist_add_head(struct klist_node *n, struct klist *k)
123{
124	klist_node_init(k, n);
125	add_head(k, n);
126}
127EXPORT_SYMBOL_GPL(klist_add_head);
128
129/**
130 * klist_add_tail - Initialize a klist_node and add it to back.
131 * @n: node we're adding.
132 * @k: klist it's going on.
133 */
134void klist_add_tail(struct klist_node *n, struct klist *k)
135{
136	klist_node_init(k, n);
137	add_tail(k, n);
138}
139EXPORT_SYMBOL_GPL(klist_add_tail);
140
141/**
142 * klist_add_behind - Init a klist_node and add it after an existing node
143 * @n: node we're adding.
144 * @pos: node to put @n after
145 */
146void klist_add_behind(struct klist_node *n, struct klist_node *pos)
147{
148	struct klist *k = knode_klist(pos);
149
150	klist_node_init(k, n);
151	spin_lock(&k->k_lock);
152	list_add(&n->n_node, &pos->n_node);
153	spin_unlock(&k->k_lock);
154}
155EXPORT_SYMBOL_GPL(klist_add_behind);
156
157/**
158 * klist_add_before - Init a klist_node and add it before an existing node
159 * @n: node we're adding.
160 * @pos: node to put @n after
161 */
162void klist_add_before(struct klist_node *n, struct klist_node *pos)
163{
164	struct klist *k = knode_klist(pos);
165
166	klist_node_init(k, n);
167	spin_lock(&k->k_lock);
168	list_add_tail(&n->n_node, &pos->n_node);
169	spin_unlock(&k->k_lock);
170}
171EXPORT_SYMBOL_GPL(klist_add_before);
172
173struct klist_waiter {
174	struct list_head list;
175	struct klist_node *node;
176	struct task_struct *process;
177	int woken;
178};
179
180static DEFINE_SPINLOCK(klist_remove_lock);
181static LIST_HEAD(klist_remove_waiters);
182
183static void klist_release(struct kref *kref)
184{
185	struct klist_waiter *waiter, *tmp;
186	struct klist_node *n = container_of(kref, struct klist_node, n_ref);
187
188	WARN_ON(!knode_dead(n));
189	list_del(&n->n_node);
190	spin_lock(&klist_remove_lock);
191	list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
192		if (waiter->node != n)
193			continue;
194
195		list_del(&waiter->list);
196		waiter->woken = 1;
197		mb();
198		wake_up_process(waiter->process);
199	}
200	spin_unlock(&klist_remove_lock);
201	knode_set_klist(n, NULL);
202}
203
204static int klist_dec_and_del(struct klist_node *n)
205{
206	return kref_put(&n->n_ref, klist_release);
207}
208
209static void klist_put(struct klist_node *n, bool kill)
210{
211	struct klist *k = knode_klist(n);
212	void (*put)(struct klist_node *) = k->put;
213
214	spin_lock(&k->k_lock);
215	if (kill)
216		knode_kill(n);
217	if (!klist_dec_and_del(n))
218		put = NULL;
219	spin_unlock(&k->k_lock);
220	if (put)
221		put(n);
222}
223
224/**
225 * klist_del - Decrement the reference count of node and try to remove.
226 * @n: node we're deleting.
227 */
228void klist_del(struct klist_node *n)
229{
230	klist_put(n, true);
231}
232EXPORT_SYMBOL_GPL(klist_del);
233
234/**
235 * klist_remove - Decrement the refcount of node and wait for it to go away.
236 * @n: node we're removing.
237 */
238void klist_remove(struct klist_node *n)
239{
240	struct klist_waiter waiter;
241
242	waiter.node = n;
243	waiter.process = current;
244	waiter.woken = 0;
245	spin_lock(&klist_remove_lock);
246	list_add(&waiter.list, &klist_remove_waiters);
247	spin_unlock(&klist_remove_lock);
248
249	klist_del(n);
250
251	for (;;) {
252		set_current_state(TASK_UNINTERRUPTIBLE);
253		if (waiter.woken)
254			break;
255		schedule();
256	}
257	__set_current_state(TASK_RUNNING);
258}
259EXPORT_SYMBOL_GPL(klist_remove);
260
261/**
262 * klist_node_attached - Say whether a node is bound to a list or not.
263 * @n: Node that we're testing.
264 */
265int klist_node_attached(struct klist_node *n)
266{
267	return (n->n_klist != NULL);
268}
269EXPORT_SYMBOL_GPL(klist_node_attached);
270
271/**
272 * klist_iter_init_node - Initialize a klist_iter structure.
273 * @k: klist we're iterating.
274 * @i: klist_iter we're filling.
275 * @n: node to start with.
276 *
277 * Similar to klist_iter_init(), but starts the action off with @n,
278 * instead of with the list head.
279 */
280void klist_iter_init_node(struct klist *k, struct klist_iter *i,
281			  struct klist_node *n)
282{
283	i->i_klist = k;
284	i->i_cur = NULL;
285	if (n && kref_get_unless_zero(&n->n_ref))
286		i->i_cur = n;
287}
288EXPORT_SYMBOL_GPL(klist_iter_init_node);
289
290/**
291 * klist_iter_init - Iniitalize a klist_iter structure.
292 * @k: klist we're iterating.
293 * @i: klist_iter structure we're filling.
294 *
295 * Similar to klist_iter_init_node(), but start with the list head.
296 */
297void klist_iter_init(struct klist *k, struct klist_iter *i)
298{
299	klist_iter_init_node(k, i, NULL);
300}
301EXPORT_SYMBOL_GPL(klist_iter_init);
302
303/**
304 * klist_iter_exit - Finish a list iteration.
305 * @i: Iterator structure.
306 *
307 * Must be called when done iterating over list, as it decrements the
308 * refcount of the current node. Necessary in case iteration exited before
309 * the end of the list was reached, and always good form.
310 */
311void klist_iter_exit(struct klist_iter *i)
312{
313	if (i->i_cur) {
314		klist_put(i->i_cur, false);
315		i->i_cur = NULL;
316	}
317}
318EXPORT_SYMBOL_GPL(klist_iter_exit);
319
320static struct klist_node *to_klist_node(struct list_head *n)
321{
322	return container_of(n, struct klist_node, n_node);
323}
324
325/**
326 * klist_prev - Ante up prev node in list.
327 * @i: Iterator structure.
328 *
329 * First grab list lock. Decrement the reference count of the previous
330 * node, if there was one. Grab the prev node, increment its reference
331 * count, drop the lock, and return that prev node.
332 */
333struct klist_node *klist_prev(struct klist_iter *i)
334{
335	void (*put)(struct klist_node *) = i->i_klist->put;
336	struct klist_node *last = i->i_cur;
337	struct klist_node *prev;
338	unsigned long flags;
339
340	spin_lock_irqsave(&i->i_klist->k_lock, flags);
341
342	if (last) {
343		prev = to_klist_node(last->n_node.prev);
344		if (!klist_dec_and_del(last))
345			put = NULL;
346	} else
347		prev = to_klist_node(i->i_klist->k_list.prev);
348
349	i->i_cur = NULL;
350	while (prev != to_klist_node(&i->i_klist->k_list)) {
351		if (likely(!knode_dead(prev))) {
352			kref_get(&prev->n_ref);
353			i->i_cur = prev;
354			break;
355		}
356		prev = to_klist_node(prev->n_node.prev);
357	}
358
359	spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
360
361	if (put && last)
362		put(last);
363	return i->i_cur;
364}
365EXPORT_SYMBOL_GPL(klist_prev);
366
367/**
368 * klist_next - Ante up next node in list.
369 * @i: Iterator structure.
370 *
371 * First grab list lock. Decrement the reference count of the previous
372 * node, if there was one. Grab the next node, increment its reference
373 * count, drop the lock, and return that next node.
374 */
375struct klist_node *klist_next(struct klist_iter *i)
376{
377	void (*put)(struct klist_node *) = i->i_klist->put;
378	struct klist_node *last = i->i_cur;
379	struct klist_node *next;
380	unsigned long flags;
381
382	spin_lock_irqsave(&i->i_klist->k_lock, flags);
383
384	if (last) {
385		next = to_klist_node(last->n_node.next);
386		if (!klist_dec_and_del(last))
387			put = NULL;
388	} else
389		next = to_klist_node(i->i_klist->k_list.next);
390
391	i->i_cur = NULL;
392	while (next != to_klist_node(&i->i_klist->k_list)) {
393		if (likely(!knode_dead(next))) {
394			kref_get(&next->n_ref);
395			i->i_cur = next;
396			break;
397		}
398		next = to_klist_node(next->n_node.next);
399	}
400
401	spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
402
403	if (put && last)
404		put(last);
405	return i->i_cur;
406}
407EXPORT_SYMBOL_GPL(klist_next);
v4.17
 
  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
 37#include <linux/klist.h>
 38#include <linux/export.h>
 39#include <linux/sched.h>
 40
 41/*
 42 * Use the lowest bit of n_klist to mark deleted nodes and exclude
 43 * dead ones from iteration.
 44 */
 45#define KNODE_DEAD		1LU
 46#define KNODE_KLIST_MASK	~KNODE_DEAD
 47
 48static struct klist *knode_klist(struct klist_node *knode)
 49{
 50	return (struct klist *)
 51		((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
 52}
 53
 54static bool knode_dead(struct klist_node *knode)
 55{
 56	return (unsigned long)knode->n_klist & KNODE_DEAD;
 57}
 58
 59static void knode_set_klist(struct klist_node *knode, struct klist *klist)
 60{
 61	knode->n_klist = klist;
 62	/* no knode deserves to start its life dead */
 63	WARN_ON(knode_dead(knode));
 64}
 65
 66static void knode_kill(struct klist_node *knode)
 67{
 68	/* and no knode should die twice ever either, see we're very humane */
 69	WARN_ON(knode_dead(knode));
 70	*(unsigned long *)&knode->n_klist |= KNODE_DEAD;
 71}
 72
 73/**
 74 * klist_init - Initialize a klist structure.
 75 * @k: The klist we're initializing.
 76 * @get: The get function for the embedding object (NULL if none)
 77 * @put: The put function for the embedding object (NULL if none)
 78 *
 79 * Initialises the klist structure.  If the klist_node structures are
 80 * going to be embedded in refcounted objects (necessary for safe
 81 * deletion) then the get/put arguments are used to initialise
 82 * functions that take and release references on the embedding
 83 * objects.
 84 */
 85void klist_init(struct klist *k, void (*get)(struct klist_node *),
 86		void (*put)(struct klist_node *))
 87{
 88	INIT_LIST_HEAD(&k->k_list);
 89	spin_lock_init(&k->k_lock);
 90	k->get = get;
 91	k->put = put;
 92}
 93EXPORT_SYMBOL_GPL(klist_init);
 94
 95static void add_head(struct klist *k, struct klist_node *n)
 96{
 97	spin_lock(&k->k_lock);
 98	list_add(&n->n_node, &k->k_list);
 99	spin_unlock(&k->k_lock);
100}
101
102static void add_tail(struct klist *k, struct klist_node *n)
103{
104	spin_lock(&k->k_lock);
105	list_add_tail(&n->n_node, &k->k_list);
106	spin_unlock(&k->k_lock);
107}
108
109static void klist_node_init(struct klist *k, struct klist_node *n)
110{
111	INIT_LIST_HEAD(&n->n_node);
112	kref_init(&n->n_ref);
113	knode_set_klist(n, k);
114	if (k->get)
115		k->get(n);
116}
117
118/**
119 * klist_add_head - Initialize a klist_node and add it to front.
120 * @n: node we're adding.
121 * @k: klist it's going on.
122 */
123void klist_add_head(struct klist_node *n, struct klist *k)
124{
125	klist_node_init(k, n);
126	add_head(k, n);
127}
128EXPORT_SYMBOL_GPL(klist_add_head);
129
130/**
131 * klist_add_tail - Initialize a klist_node and add it to back.
132 * @n: node we're adding.
133 * @k: klist it's going on.
134 */
135void klist_add_tail(struct klist_node *n, struct klist *k)
136{
137	klist_node_init(k, n);
138	add_tail(k, n);
139}
140EXPORT_SYMBOL_GPL(klist_add_tail);
141
142/**
143 * klist_add_behind - Init a klist_node and add it after an existing node
144 * @n: node we're adding.
145 * @pos: node to put @n after
146 */
147void klist_add_behind(struct klist_node *n, struct klist_node *pos)
148{
149	struct klist *k = knode_klist(pos);
150
151	klist_node_init(k, n);
152	spin_lock(&k->k_lock);
153	list_add(&n->n_node, &pos->n_node);
154	spin_unlock(&k->k_lock);
155}
156EXPORT_SYMBOL_GPL(klist_add_behind);
157
158/**
159 * klist_add_before - Init a klist_node and add it before an existing node
160 * @n: node we're adding.
161 * @pos: node to put @n after
162 */
163void klist_add_before(struct klist_node *n, struct klist_node *pos)
164{
165	struct klist *k = knode_klist(pos);
166
167	klist_node_init(k, n);
168	spin_lock(&k->k_lock);
169	list_add_tail(&n->n_node, &pos->n_node);
170	spin_unlock(&k->k_lock);
171}
172EXPORT_SYMBOL_GPL(klist_add_before);
173
174struct klist_waiter {
175	struct list_head list;
176	struct klist_node *node;
177	struct task_struct *process;
178	int woken;
179};
180
181static DEFINE_SPINLOCK(klist_remove_lock);
182static LIST_HEAD(klist_remove_waiters);
183
184static void klist_release(struct kref *kref)
185{
186	struct klist_waiter *waiter, *tmp;
187	struct klist_node *n = container_of(kref, struct klist_node, n_ref);
188
189	WARN_ON(!knode_dead(n));
190	list_del(&n->n_node);
191	spin_lock(&klist_remove_lock);
192	list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193		if (waiter->node != n)
194			continue;
195
196		list_del(&waiter->list);
197		waiter->woken = 1;
198		mb();
199		wake_up_process(waiter->process);
200	}
201	spin_unlock(&klist_remove_lock);
202	knode_set_klist(n, NULL);
203}
204
205static int klist_dec_and_del(struct klist_node *n)
206{
207	return kref_put(&n->n_ref, klist_release);
208}
209
210static void klist_put(struct klist_node *n, bool kill)
211{
212	struct klist *k = knode_klist(n);
213	void (*put)(struct klist_node *) = k->put;
214
215	spin_lock(&k->k_lock);
216	if (kill)
217		knode_kill(n);
218	if (!klist_dec_and_del(n))
219		put = NULL;
220	spin_unlock(&k->k_lock);
221	if (put)
222		put(n);
223}
224
225/**
226 * klist_del - Decrement the reference count of node and try to remove.
227 * @n: node we're deleting.
228 */
229void klist_del(struct klist_node *n)
230{
231	klist_put(n, true);
232}
233EXPORT_SYMBOL_GPL(klist_del);
234
235/**
236 * klist_remove - Decrement the refcount of node and wait for it to go away.
237 * @n: node we're removing.
238 */
239void klist_remove(struct klist_node *n)
240{
241	struct klist_waiter waiter;
242
243	waiter.node = n;
244	waiter.process = current;
245	waiter.woken = 0;
246	spin_lock(&klist_remove_lock);
247	list_add(&waiter.list, &klist_remove_waiters);
248	spin_unlock(&klist_remove_lock);
249
250	klist_del(n);
251
252	for (;;) {
253		set_current_state(TASK_UNINTERRUPTIBLE);
254		if (waiter.woken)
255			break;
256		schedule();
257	}
258	__set_current_state(TASK_RUNNING);
259}
260EXPORT_SYMBOL_GPL(klist_remove);
261
262/**
263 * klist_node_attached - Say whether a node is bound to a list or not.
264 * @n: Node that we're testing.
265 */
266int klist_node_attached(struct klist_node *n)
267{
268	return (n->n_klist != NULL);
269}
270EXPORT_SYMBOL_GPL(klist_node_attached);
271
272/**
273 * klist_iter_init_node - Initialize a klist_iter structure.
274 * @k: klist we're iterating.
275 * @i: klist_iter we're filling.
276 * @n: node to start with.
277 *
278 * Similar to klist_iter_init(), but starts the action off with @n,
279 * instead of with the list head.
280 */
281void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282			  struct klist_node *n)
283{
284	i->i_klist = k;
285	i->i_cur = NULL;
286	if (n && kref_get_unless_zero(&n->n_ref))
287		i->i_cur = n;
288}
289EXPORT_SYMBOL_GPL(klist_iter_init_node);
290
291/**
292 * klist_iter_init - Iniitalize a klist_iter structure.
293 * @k: klist we're iterating.
294 * @i: klist_iter structure we're filling.
295 *
296 * Similar to klist_iter_init_node(), but start with the list head.
297 */
298void klist_iter_init(struct klist *k, struct klist_iter *i)
299{
300	klist_iter_init_node(k, i, NULL);
301}
302EXPORT_SYMBOL_GPL(klist_iter_init);
303
304/**
305 * klist_iter_exit - Finish a list iteration.
306 * @i: Iterator structure.
307 *
308 * Must be called when done iterating over list, as it decrements the
309 * refcount of the current node. Necessary in case iteration exited before
310 * the end of the list was reached, and always good form.
311 */
312void klist_iter_exit(struct klist_iter *i)
313{
314	if (i->i_cur) {
315		klist_put(i->i_cur, false);
316		i->i_cur = NULL;
317	}
318}
319EXPORT_SYMBOL_GPL(klist_iter_exit);
320
321static struct klist_node *to_klist_node(struct list_head *n)
322{
323	return container_of(n, struct klist_node, n_node);
324}
325
326/**
327 * klist_prev - Ante up prev 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 prev node, increment its reference
332 * count, drop the lock, and return that prev node.
333 */
334struct klist_node *klist_prev(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 *prev;
 
339
340	spin_lock(&i->i_klist->k_lock);
341
342	if (last) {
343		prev = to_klist_node(last->n_node.prev);
344		if (!klist_dec_and_del(last))
345			put = NULL;
346	} else
347		prev = to_klist_node(i->i_klist->k_list.prev);
348
349	i->i_cur = NULL;
350	while (prev != to_klist_node(&i->i_klist->k_list)) {
351		if (likely(!knode_dead(prev))) {
352			kref_get(&prev->n_ref);
353			i->i_cur = prev;
354			break;
355		}
356		prev = to_klist_node(prev->n_node.prev);
357	}
358
359	spin_unlock(&i->i_klist->k_lock);
360
361	if (put && last)
362		put(last);
363	return i->i_cur;
364}
365EXPORT_SYMBOL_GPL(klist_prev);
366
367/**
368 * klist_next - Ante up next node in list.
369 * @i: Iterator structure.
370 *
371 * First grab list lock. Decrement the reference count of the previous
372 * node, if there was one. Grab the next node, increment its reference
373 * count, drop the lock, and return that next node.
374 */
375struct klist_node *klist_next(struct klist_iter *i)
376{
377	void (*put)(struct klist_node *) = i->i_klist->put;
378	struct klist_node *last = i->i_cur;
379	struct klist_node *next;
 
380
381	spin_lock(&i->i_klist->k_lock);
382
383	if (last) {
384		next = to_klist_node(last->n_node.next);
385		if (!klist_dec_and_del(last))
386			put = NULL;
387	} else
388		next = to_klist_node(i->i_klist->k_list.next);
389
390	i->i_cur = NULL;
391	while (next != to_klist_node(&i->i_klist->k_list)) {
392		if (likely(!knode_dead(next))) {
393			kref_get(&next->n_ref);
394			i->i_cur = next;
395			break;
396		}
397		next = to_klist_node(next->n_node.next);
398	}
399
400	spin_unlock(&i->i_klist->k_lock);
401
402	if (put && last)
403		put(last);
404	return i->i_cur;
405}
406EXPORT_SYMBOL_GPL(klist_next);