blob: 332a4fbf18ff0812095e9ed980361a9e8d8c5000 [file] [log] [blame]
Thomas Gleixner8092f732019-06-03 07:45:04 +02001// SPDX-License-Identifier: GPL-2.0-only
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08002/*
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07003 * klist.c - Routines for manipulating klists.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08004 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07005 * Copyright (C) 2005 Patrick Mochel
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08006 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07007 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080014 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070015 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080019 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070020 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080026 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070027 * 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).
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080034 */
35
36#include <linux/klist.h>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050037#include <linux/export.h>
Matthew Wilcox210272a2008-10-16 14:57:54 -060038#include <linux/sched.h>
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080039
Tejun Heoa1ed5b02008-08-25 19:50:16 +020040/*
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}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080071
72/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070073 * 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)
James Bottomley34bb61f2005-09-06 16:56:51 -070077 *
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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080083 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070084void klist_init(struct klist *k, void (*get)(struct klist_node *),
James Bottomley34bb61f2005-09-06 16:56:51 -070085 void (*put)(struct klist_node *))
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080086{
87 INIT_LIST_HEAD(&k->k_list);
88 spin_lock_init(&k->k_lock);
James Bottomley34bb61f2005-09-06 16:56:51 -070089 k->get = get;
90 k->put = put;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080091}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080092EXPORT_SYMBOL_GPL(klist_init);
93
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070094static void add_head(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080095{
96 spin_lock(&k->k_lock);
97 list_add(&n->n_node, &k->k_list);
98 spin_unlock(&k->k_lock);
99}
100
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700101static void add_tail(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800102{
103 spin_lock(&k->k_lock);
104 list_add_tail(&n->n_node, &k->k_list);
105 spin_unlock(&k->k_lock);
106}
107
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700108static void klist_node_init(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800109{
110 INIT_LIST_HEAD(&n->n_node);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800111 kref_init(&n->n_ref);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200112 knode_set_klist(n, k);
James Bottomley34bb61f2005-09-06 16:56:51 -0700113 if (k->get)
114 k->get(n);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800115}
116
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800117/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700118 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800121 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700122void klist_add_head(struct klist_node *n, struct klist *k)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800123{
124 klist_node_init(k, n);
125 add_head(k, n);
126}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800127EXPORT_SYMBOL_GPL(klist_add_head);
128
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800129/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700130 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800133 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700134void klist_add_tail(struct klist_node *n, struct klist *k)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800135{
136 klist_node_init(k, n);
137 add_tail(k, n);
138}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800139EXPORT_SYMBOL_GPL(klist_add_tail);
140
Tejun Heo93dd4002008-04-22 18:58:46 +0900141/**
Ken Helias0f9859c2014-08-06 16:09:18 -0700142 * klist_add_behind - Init a klist_node and add it after an existing node
Tejun Heo93dd4002008-04-22 18:58:46 +0900143 * @n: node we're adding.
144 * @pos: node to put @n after
145 */
Ken Helias0f9859c2014-08-06 16:09:18 -0700146void klist_add_behind(struct klist_node *n, struct klist_node *pos)
Tejun Heo93dd4002008-04-22 18:58:46 +0900147{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200148 struct klist *k = knode_klist(pos);
Tejun Heo93dd4002008-04-22 18:58:46 +0900149
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}
Ken Helias0f9859c2014-08-06 16:09:18 -0700155EXPORT_SYMBOL_GPL(klist_add_behind);
Tejun Heo93dd4002008-04-22 18:58:46 +0900156
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{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200164 struct klist *k = knode_klist(pos);
Tejun Heo93dd4002008-04-22 18:58:46 +0900165
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
Matthew Wilcox210272a2008-10-16 14:57:54 -0600173struct 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
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700183static void klist_release(struct kref *kref)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800184{
Matthew Wilcox210272a2008-10-16 14:57:54 -0600185 struct klist_waiter *waiter, *tmp;
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700186 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
Alan Stern7e9f4b22006-09-18 16:28:06 -0400187
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200188 WARN_ON(!knode_dead(n));
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800189 list_del(&n->n_node);
Matthew Wilcox210272a2008-10-16 14:57:54 -0600190 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
wang, biaoac5a2962013-05-16 09:50:13 +0800195 list_del(&waiter->list);
Matthew Wilcox210272a2008-10-16 14:57:54 -0600196 waiter->woken = 1;
197 mb();
198 wake_up_process(waiter->process);
Matthew Wilcox210272a2008-10-16 14:57:54 -0600199 }
200 spin_unlock(&klist_remove_lock);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200201 knode_set_klist(n, NULL);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800202}
203
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700204static int klist_dec_and_del(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800205{
206 return kref_put(&n->n_ref, klist_release);
207}
208
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200209static 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
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800224/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700225 * klist_del - Decrement the reference count of node and try to remove.
226 * @n: node we're deleting.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800227 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700228void klist_del(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800229{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200230 klist_put(n, true);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800231}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800232EXPORT_SYMBOL_GPL(klist_del);
233
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800234/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700235 * klist_remove - Decrement the refcount of node and wait for it to go away.
236 * @n: node we're removing.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800237 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700238void klist_remove(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800239{
Matthew Wilcox210272a2008-10-16 14:57:54 -0600240 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
Alan Stern7e9f4b22006-09-18 16:28:06 -0400249 klist_del(n);
Matthew Wilcox210272a2008-10-16 14:57:54 -0600250
251 for (;;) {
252 set_current_state(TASK_UNINTERRUPTIBLE);
253 if (waiter.woken)
254 break;
255 schedule();
256 }
257 __set_current_state(TASK_RUNNING);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800258}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800259EXPORT_SYMBOL_GPL(klist_remove);
260
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800261/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700262 * klist_node_attached - Say whether a node is bound to a list or not.
263 * @n: Node that we're testing.
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800264 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700265int klist_node_attached(struct klist_node *n)
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800266{
267 return (n->n_klist != NULL);
268}
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800269EXPORT_SYMBOL_GPL(klist_node_attached);
270
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800271/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700272 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800276 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700277 * Similar to klist_iter_init(), but starts the action off with @n,
278 * instead of with the list head.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800279 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700280void klist_iter_init_node(struct klist *k, struct klist_iter *i,
281 struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800282{
283 i->i_klist = k;
James Bottomley00cd29b2016-01-13 08:10:31 -0800284 i->i_cur = NULL;
285 if (n && kref_get_unless_zero(&n->n_ref))
286 i->i_cur = n;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800287}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800288EXPORT_SYMBOL_GPL(klist_iter_init_node);
289
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800290/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700291 * klist_iter_init - Iniitalize a klist_iter structure.
292 * @k: klist we're iterating.
293 * @i: klist_iter structure we're filling.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800294 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700295 * Similar to klist_iter_init_node(), but start with the list head.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800296 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700297void klist_iter_init(struct klist *k, struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800298{
299 klist_iter_init_node(k, i, NULL);
300}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800301EXPORT_SYMBOL_GPL(klist_iter_init);
302
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800303/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700304 * klist_iter_exit - Finish a list iteration.
305 * @i: Iterator structure.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800306 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700307 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800310 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700311void klist_iter_exit(struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800312{
313 if (i->i_cur) {
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200314 klist_put(i->i_cur, false);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800315 i->i_cur = NULL;
316 }
317}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800318EXPORT_SYMBOL_GPL(klist_iter_exit);
319
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700320static struct klist_node *to_klist_node(struct list_head *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800321{
322 return container_of(n, struct klist_node, n_node);
323}
324
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800325/**
Andy Shevchenko2e0fed72015-07-27 18:03:59 +0300326 * 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;
Bart Van Assche624fa772018-06-22 14:54:49 -0700338 unsigned long flags;
Andy Shevchenko2e0fed72015-07-27 18:03:59 +0300339
Bart Van Assche624fa772018-06-22 14:54:49 -0700340 spin_lock_irqsave(&i->i_klist->k_lock, flags);
Andy Shevchenko2e0fed72015-07-27 18:03:59 +0300341
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
Bart Van Assche624fa772018-06-22 14:54:49 -0700359 spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
Andy Shevchenko2e0fed72015-07-27 18:03:59 +0300360
361 if (put && last)
362 put(last);
363 return i->i_cur;
364}
365EXPORT_SYMBOL_GPL(klist_prev);
366
367/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700368 * klist_next - Ante up next node in list.
369 * @i: Iterator structure.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800370 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700371 * 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.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800374 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700375struct klist_node *klist_next(struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800376{
Alan Stern7e9f4b22006-09-18 16:28:06 -0400377 void (*put)(struct klist_node *) = i->i_klist->put;
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200378 struct klist_node *last = i->i_cur;
379 struct klist_node *next;
Bart Van Assche624fa772018-06-22 14:54:49 -0700380 unsigned long flags;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800381
Bart Van Assche624fa772018-06-22 14:54:49 -0700382 spin_lock_irqsave(&i->i_klist->k_lock, flags);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200383
384 if (last) {
385 next = to_klist_node(last->n_node.next);
386 if (!klist_dec_and_del(last))
Alan Stern7e9f4b22006-09-18 16:28:06 -0400387 put = NULL;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800388 } else
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200389 next = to_klist_node(i->i_klist->k_list.next);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800390
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200391 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);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800399 }
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200400
Bart Van Assche624fa772018-06-22 14:54:49 -0700401 spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200402
403 if (put && last)
404 put(last);
405 return i->i_cur;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800406}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800407EXPORT_SYMBOL_GPL(klist_next);