xref: /freebsd/sys/compat/linuxkpi/common/include/linux/list.h (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2016 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _LINUXKPI_LINUX_LIST_H_
30 #define _LINUXKPI_LINUX_LIST_H_
31 
32 #ifndef _STANDALONE
33 /*
34  * Since LIST_HEAD conflicts with the Linux definition we must include any
35  * FreeBSD header which requires it here so it is resolved with the correct
36  * definition prior to the undef.
37  */
38 #include <linux/types.h>
39 
40 #include <sys/param.h>
41 #include <sys/kernel.h>
42 #include <sys/queue.h>
43 #include <sys/cpuset.h>
44 #include <sys/jail.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/conf.h>
50 #include <sys/socket.h>
51 #include <sys/mbuf.h>
52 
53 #include <net/bpf.h>
54 #include <net/ethernet.h>
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_types.h>
58 #include <net/if_media.h>
59 #include <net/vnet.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/in_var.h>
64 #include <netinet/tcp_lro.h>
65 
66 #include <netinet6/in6_var.h>
67 #include <netinet6/nd6.h>
68 
69 #include <net80211/ieee80211.h>
70 #include <net80211/ieee80211_var.h>
71 #include <net80211/ieee80211_node.h>
72 
73 #include <vm/vm.h>
74 #include <vm/vm_object.h>
75 #include <vm/pmap.h>
76 #endif
77 
78 #ifndef prefetch
79 #define	prefetch(x)
80 #endif
81 
82 #define LINUX_LIST_HEAD_INIT(name) { &(name), &(name) }
83 
84 #define LINUX_LIST_HEAD(name) \
85 	struct list_head name = LINUX_LIST_HEAD_INIT(name)
86 
87 static inline void
88 INIT_LIST_HEAD(struct list_head *list)
89 {
90 
91 	list->next = list->prev = list;
92 }
93 
94 static inline int
95 list_empty(const struct list_head *head)
96 {
97 
98 	return (head->next == head);
99 }
100 
101 static inline int
102 list_empty_careful(const struct list_head *head)
103 {
104 	struct list_head *next = head->next;
105 
106 	return ((next == head) && (next == head->prev));
107 }
108 
109 static inline void
110 __list_del(struct list_head *prev, struct list_head *next)
111 {
112 	next->prev = prev;
113 	WRITE_ONCE(prev->next, next);
114 }
115 
116 static inline void
117 __list_del_entry(struct list_head *entry)
118 {
119 
120 	__list_del(entry->prev, entry->next);
121 }
122 
123 static inline void
124 list_del(struct list_head *entry)
125 {
126 
127 	__list_del(entry->prev, entry->next);
128 }
129 
130 static inline void
131 list_replace(struct list_head *old, struct list_head *new)
132 {
133 	new->next = old->next;
134 	new->next->prev = new;
135 	new->prev = old->prev;
136 	new->prev->next = new;
137 }
138 
139 static inline void
140 list_replace_init(struct list_head *old, struct list_head *new)
141 {
142 	list_replace(old, new);
143 	INIT_LIST_HEAD(old);
144 }
145 
146 static inline void
147 linux_list_add(struct list_head *new, struct list_head *prev,
148     struct list_head *next)
149 {
150 
151 	next->prev = new;
152 	new->next = next;
153 	new->prev = prev;
154 	prev->next = new;
155 }
156 
157 static inline void
158 list_del_init(struct list_head *entry)
159 {
160 
161 	list_del(entry);
162 	INIT_LIST_HEAD(entry);
163 }
164 
165 #define	list_entry(ptr, type, field)	container_of(ptr, type, field)
166 
167 #define	list_first_entry(ptr, type, member) \
168 	list_entry((ptr)->next, type, member)
169 
170 #define	list_last_entry(ptr, type, member)	\
171 	list_entry((ptr)->prev, type, member)
172 
173 #define	list_first_entry_or_null(ptr, type, member) \
174 	(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
175 
176 #define	list_next_entry(ptr, member)					\
177 	list_entry(((ptr)->member.next), typeof(*(ptr)), member)
178 
179 #define	list_safe_reset_next(ptr, n, member) \
180 	(n) = list_next_entry(ptr, member)
181 
182 #define	list_prev_entry(ptr, member)					\
183 	list_entry(((ptr)->member.prev), typeof(*(ptr)), member)
184 
185 #define	list_for_each(p, head)						\
186 	for (p = (head)->next; p != (head); p = (p)->next)
187 
188 #define	list_for_each_safe(p, n, head)					\
189 	for (p = (head)->next, n = (p)->next; p != (head); p = n, n = (p)->next)
190 
191 #define list_for_each_entry(p, h, field)				\
192 	for (p = list_entry((h)->next, typeof(*p), field); &(p)->field != (h); \
193 	    p = list_entry((p)->field.next, typeof(*p), field))
194 
195 #define list_for_each_entry_safe(p, n, h, field)			\
196 	for (p = list_entry((h)->next, typeof(*p), field),		\
197 	    n = list_entry((p)->field.next, typeof(*p), field); &(p)->field != (h);\
198 	    p = n, n = list_entry(n->field.next, typeof(*n), field))
199 
200 #define	list_for_each_entry_from(p, h, field) \
201 	for ( ; &(p)->field != (h); \
202 	    p = list_entry((p)->field.next, typeof(*p), field))
203 
204 #define	list_for_each_entry_continue(p, h, field)			\
205 	for (p = list_next_entry((p), field); &(p)->field != (h);	\
206 	    p = list_next_entry((p), field))
207 
208 #define	list_for_each_entry_safe_from(pos, n, head, member)			\
209 	for (n = list_entry((pos)->member.next, typeof(*pos), member);		\
210 	     &(pos)->member != (head);						\
211 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
212 
213 #define	list_for_each_entry_reverse(p, h, field)			\
214 	for (p = list_entry((h)->prev, typeof(*p), field); &(p)->field != (h); \
215 	    p = list_entry((p)->field.prev, typeof(*p), field))
216 
217 #define	list_for_each_entry_safe_reverse(p, n, h, field)		\
218 	for (p = list_entry((h)->prev, typeof(*p), field),		\
219 	    n = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
220 	    p = n, n = list_entry(n->field.prev, typeof(*n), field))
221 
222 #define	list_for_each_entry_continue_reverse(p, h, field) \
223 	for (p = list_entry((p)->field.prev, typeof(*p), field); &(p)->field != (h); \
224 	    p = list_entry((p)->field.prev, typeof(*p), field))
225 
226 #define	list_for_each_prev(p, h) for (p = (h)->prev; p != (h); p = (p)->prev)
227 
228 #define	list_for_each_entry_from_reverse(p, h, field)	\
229 	for (; &p->field != (h);			\
230 	     p = list_prev_entry(p, field))
231 
232 static inline void
233 list_add(struct list_head *new, struct list_head *head)
234 {
235 
236 	linux_list_add(new, head, head->next);
237 }
238 
239 static inline void
240 list_add_tail(struct list_head *new, struct list_head *head)
241 {
242 
243 	linux_list_add(new, head->prev, head);
244 }
245 
246 static inline void
247 list_move(struct list_head *list, struct list_head *head)
248 {
249 
250 	list_del(list);
251 	list_add(list, head);
252 }
253 
254 static inline void
255 list_move_tail(struct list_head *entry, struct list_head *head)
256 {
257 
258 	list_del(entry);
259 	list_add_tail(entry, head);
260 }
261 
262 static inline void
263 list_rotate_to_front(struct list_head *entry, struct list_head *head)
264 {
265 
266 	list_move_tail(entry, head);
267 }
268 
269 static inline void
270 list_bulk_move_tail(struct list_head *head, struct list_head *first,
271     struct list_head *last)
272 {
273 	first->prev->next = last->next;
274 	last->next->prev = first->prev;
275 	head->prev->next = first;
276 	first->prev = head->prev;
277 	last->next = head;
278 	head->prev = last;
279 }
280 
281 static inline void
282 linux_list_splice(const struct list_head *list, struct list_head *prev,
283     struct list_head *next)
284 {
285 	struct list_head *first;
286 	struct list_head *last;
287 
288 	if (list_empty(list))
289 		return;
290 	first = list->next;
291 	last = list->prev;
292 	first->prev = prev;
293 	prev->next = first;
294 	last->next = next;
295 	next->prev = last;
296 }
297 
298 static inline void
299 list_splice(const struct list_head *list, struct list_head *head)
300 {
301 
302 	linux_list_splice(list, head, head->next);
303 }
304 
305 static inline void
306 list_splice_tail(struct list_head *list, struct list_head *head)
307 {
308 
309 	linux_list_splice(list, head->prev, head);
310 }
311 
312 static inline void
313 list_splice_init(struct list_head *list, struct list_head *head)
314 {
315 
316 	linux_list_splice(list, head, head->next);
317 	INIT_LIST_HEAD(list);
318 }
319 
320 static inline void
321 list_splice_tail_init(struct list_head *list, struct list_head *head)
322 {
323 
324 	linux_list_splice(list, head->prev, head);
325 	INIT_LIST_HEAD(list);
326 }
327 
328 #undef LIST_HEAD
329 #define LIST_HEAD(name)	struct list_head name = { &(name), &(name) }
330 
331 struct hlist_head {
332 	struct hlist_node *first;
333 };
334 
335 struct hlist_node {
336 	struct hlist_node *next, **pprev;
337 };
338 #define	HLIST_HEAD_INIT { }
339 #define	HLIST_HEAD(name) struct hlist_head name = HLIST_HEAD_INIT
340 #define	INIT_HLIST_HEAD(head) (head)->first = NULL
341 #define	INIT_HLIST_NODE(node)						\
342 do {									\
343 	(node)->next = NULL;						\
344 	(node)->pprev = NULL;						\
345 } while (0)
346 
347 static inline int
348 hlist_unhashed(const struct hlist_node *h)
349 {
350 
351 	return !h->pprev;
352 }
353 
354 static inline int
355 hlist_empty(const struct hlist_head *h)
356 {
357 
358 	return !READ_ONCE(h->first);
359 }
360 
361 static inline void
362 hlist_del(struct hlist_node *n)
363 {
364 
365 	WRITE_ONCE(*(n->pprev), n->next);
366 	if (n->next != NULL)
367 		n->next->pprev = n->pprev;
368 }
369 
370 static inline void
371 hlist_del_init(struct hlist_node *n)
372 {
373 
374 	if (hlist_unhashed(n))
375 		return;
376 	hlist_del(n);
377 	INIT_HLIST_NODE(n);
378 }
379 
380 static inline void
381 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
382 {
383 
384 	n->next = h->first;
385 	if (h->first != NULL)
386 		h->first->pprev = &n->next;
387 	WRITE_ONCE(h->first, n);
388 	n->pprev = &h->first;
389 }
390 
391 static inline void
392 hlist_add_before(struct hlist_node *n, struct hlist_node *next)
393 {
394 
395 	n->pprev = next->pprev;
396 	n->next = next;
397 	next->pprev = &n->next;
398 	WRITE_ONCE(*(n->pprev), n);
399 }
400 
401 static inline void
402 hlist_add_behind(struct hlist_node *n, struct hlist_node *prev)
403 {
404 
405 	n->next = prev->next;
406 	WRITE_ONCE(prev->next, n);
407 	n->pprev = &prev->next;
408 
409 	if (n->next != NULL)
410 		n->next->pprev = &n->next;
411 }
412 
413 static inline void
414 hlist_move_list(struct hlist_head *old, struct hlist_head *new)
415 {
416 
417 	new->first = old->first;
418 	if (new->first)
419 		new->first->pprev = &new->first;
420 	old->first = NULL;
421 }
422 
423 static inline int list_is_singular(const struct list_head *head)
424 {
425 	return !list_empty(head) && (head->next == head->prev);
426 }
427 
428 static inline void __list_cut_position(struct list_head *list,
429 		struct list_head *head, struct list_head *entry)
430 {
431 	struct list_head *new_first = entry->next;
432 	list->next = head->next;
433 	list->next->prev = list;
434 	list->prev = entry;
435 	entry->next = list;
436 	head->next = new_first;
437 	new_first->prev = head;
438 }
439 
440 static inline void list_cut_position(struct list_head *list,
441 		struct list_head *head, struct list_head *entry)
442 {
443 	if (list_empty(head))
444 		return;
445 	if (list_is_singular(head) &&
446 		(head->next != entry && head != entry))
447 		return;
448 	if (entry == head)
449 		INIT_LIST_HEAD(list);
450 	else
451 		__list_cut_position(list, head, entry);
452 }
453 
454 static inline int list_is_first(const struct list_head *list,
455 				const struct list_head *head)
456 {
457 
458 	return (list->prev == head);
459 }
460 
461 static inline int list_is_last(const struct list_head *list,
462 				const struct list_head *head)
463 {
464 	return list->next == head;
465 }
466 
467 static inline size_t
468 list_count_nodes(const struct list_head *list)
469 {
470 	const struct list_head *lh;
471 	size_t count;
472 
473 	count = 0;
474 	list_for_each(lh, list) {
475 		count++;
476 	}
477 
478 	return (count);
479 }
480 
481 #define	hlist_entry(ptr, type, field)	container_of(ptr, type, field)
482 
483 #define	hlist_for_each(p, head)						\
484 	for (p = (head)->first; p; p = (p)->next)
485 
486 #define	hlist_for_each_safe(p, n, head)					\
487 	for (p = (head)->first; p && ({ n = (p)->next; 1; }); p = n)
488 
489 #define	hlist_entry_safe(ptr, type, member) \
490 	((ptr) ? hlist_entry(ptr, type, member) : NULL)
491 
492 #define	hlist_for_each_entry(pos, head, member)				\
493 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
494 	     pos;							\
495 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
496 
497 #define	hlist_for_each_entry_continue(pos, member)			\
498 	for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member); \
499 	     (pos);							\
500 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
501 
502 #define	hlist_for_each_entry_from(pos, member)				\
503 	for (; (pos);								\
504 	     pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
505 
506 #define	hlist_for_each_entry_safe(pos, n, head, member)			\
507 	for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
508 	     (pos) && ({ n = (pos)->member.next; 1; });			\
509 	     pos = hlist_entry_safe(n, typeof(*(pos)), member))
510 
511 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION >= 51300
512 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
513     const struct list_head *a, const struct list_head *b));
514 #else
515 extern void list_sort(void *priv, struct list_head *head, int (*cmp)(void *priv,
516     struct list_head *a, struct list_head *b));
517 #endif
518 
519 #endif /* _LINUXKPI_LINUX_LIST_H_ */
520