xref: /freebsd/sys/compat/linuxkpi/common/include/linux/rculist.h (revision edf8578117e8844e02c0121147f45e4609b30680)
1 /*-
2  * Copyright (c) 2015 François Tigeot
3  * Copyright (c) 2016-2020 Mellanox Technologies, Ltd.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _LINUXKPI_LINUX_RCULIST_H_
29 #define	_LINUXKPI_LINUX_RCULIST_H_
30 
31 #include <linux/list.h>
32 #include <linux/rcupdate.h>
33 
34 #define	list_entry_rcu(ptr, type, member) \
35 	container_of(READ_ONCE(ptr), type, member)
36 
37 #define	list_next_rcu(head)	(*((struct list_head **)(&(head)->next)))
38 #define	list_prev_rcu(head)	(*((struct list_head **)(&(head)->prev)))
39 
40 #define	list_for_each_entry_rcu(pos, head, member) \
41 	for (pos = list_entry_rcu((head)->next, typeof(*(pos)), member); \
42 	     &(pos)->member != (head);					\
43 	     pos = list_entry_rcu((pos)->member.next, typeof(*(pos)), member))
44 
45 #define	list_for_each_entry_from_rcu(pos, head, member) \
46 	for (; \
47 	     &(pos)->member != (head);					\
48 	     pos = list_entry_rcu((pos)->member.next, typeof(*(pos)), member))
49 
50 #define	list_for_each_entry_lockless(pos, head, member) \
51 	list_for_each_entry_rcu(pos, head, member)
52 
53 static inline void
54 linux_list_add_rcu(struct list_head *new, struct list_head *prev,
55     struct list_head *next)
56 {
57 	new->next = next;
58 	new->prev = prev;
59 	rcu_assign_pointer(list_next_rcu(prev), new);
60 	next->prev = new;
61 }
62 
63 static inline void
64 list_add_rcu(struct list_head *new, struct list_head *head)
65 {
66 	linux_list_add_rcu(new, head, head->next);
67 }
68 
69 static inline void
70 list_add_tail_rcu(struct list_head *new, struct list_head *head)
71 {
72 	linux_list_add_rcu(new, head->prev, head);
73 }
74 
75 static inline void
76 __list_del_rcu(struct list_head *prev, struct list_head *next)
77 {
78 	next->prev = prev;
79 	rcu_assign_pointer(list_next_rcu(prev), next);
80 }
81 
82 static inline void
83 __list_del_entry_rcu(struct list_head *entry)
84 {
85 	__list_del_rcu(entry->prev, entry->next);
86 }
87 
88 static inline void
89 list_del_rcu(struct list_head *entry)
90 {
91 	__list_del_rcu(entry->prev, entry->next);
92 }
93 
94 #define	hlist_first_rcu(head)	(*((struct hlist_node **)(&(head)->first)))
95 #define	hlist_next_rcu(node)	(*((struct hlist_node **)(&(node)->next)))
96 #define	hlist_pprev_rcu(node)	(*((struct hlist_node **)((node)->pprev)))
97 
98 static inline void
99 hlist_add_behind_rcu(struct hlist_node *n, struct hlist_node *prev)
100 {
101 	n->next = prev->next;
102 	n->pprev = &prev->next;
103 	rcu_assign_pointer(hlist_next_rcu(prev), n);
104 	if (n->next)
105 		n->next->pprev = &n->next;
106 }
107 
108 #define	hlist_for_each_entry_rcu(pos, head, member)			\
109 	for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
110 	        typeof(*(pos)), member);				\
111 	     (pos);							\
112 	     pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(	\
113 			&(pos)->member)), typeof(*(pos)), member))
114 
115 static inline void
116 hlist_del_rcu(struct hlist_node *n)
117 {
118 	struct hlist_node *next = n->next;
119 	struct hlist_node **pprev = n->pprev;
120 
121 	WRITE_ONCE(*pprev, next);
122 	if (next)
123 		next->pprev = pprev;
124 }
125 
126 static inline void
127 hlist_add_head_rcu(struct hlist_node *n, struct hlist_head *h)
128 {
129 	struct hlist_node *first = h->first;
130 
131 	n->next = first;
132 	n->pprev = &h->first;
133 	rcu_assign_pointer(hlist_first_rcu(h), n);
134 	if (first)
135 		first->pprev = &n->next;
136 }
137 
138 static inline void
139 hlist_del_init_rcu(struct hlist_node *n)
140 {
141 	if (!hlist_unhashed(n)) {
142 		hlist_del_rcu(n);
143 		n->pprev = NULL;
144 	}
145 }
146 
147 #endif					/* _LINUXKPI_LINUX_RCULIST_H_ */
148