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  * $FreeBSD$
28  */
29 
30 #ifndef _LINUXKPI_LINUX_RCULIST_H_
31 #define	_LINUXKPI_LINUX_RCULIST_H_
32 
33 #include <linux/list.h>
34 #include <linux/rcupdate.h>
35 
36 #define	list_entry_rcu(ptr, type, member) \
37 	container_of(READ_ONCE(ptr), type, member)
38 
39 #define	list_next_rcu(head)	(*((struct list_head **)(&(head)->next)))
40 #define	list_prev_rcu(head)	(*((struct list_head **)(&(head)->prev)))
41 
42 #define	list_for_each_entry_rcu(pos, head, member) \
43 	for (pos = list_entry_rcu((head)->next, typeof(*(pos)), member); \
44 	     &(pos)->member != (head);					\
45 	     pos = list_entry_rcu((pos)->member.next, typeof(*(pos)), member))
46 
47 #define	list_for_each_entry_from_rcu(pos, head, member) \
48 	for (; \
49 	     &(pos)->member != (head);					\
50 	     pos = list_entry_rcu((pos)->member.next, typeof(*(pos)), member))
51 
52 #define	list_for_each_entry_lockless(pos, head, member) \
53 	list_for_each_entry_rcu(pos, head, member)
54 
55 static inline void
56 linux_list_add_rcu(struct list_head *new, struct list_head *prev,
57     struct list_head *next)
58 {
59 	new->next = next;
60 	new->prev = prev;
61 	rcu_assign_pointer(list_next_rcu(prev), new);
62 	next->prev = new;
63 }
64 
65 static inline void
66 list_add_rcu(struct list_head *new, struct list_head *head)
67 {
68 	linux_list_add_rcu(new, head, head->next);
69 }
70 
71 static inline void
72 list_add_tail_rcu(struct list_head *new, struct list_head *head)
73 {
74 	linux_list_add_rcu(new, head->prev, head);
75 }
76 
77 static inline void
78 __list_del_rcu(struct list_head *prev, struct list_head *next)
79 {
80 	next->prev = prev;
81 	rcu_assign_pointer(list_next_rcu(prev), next);
82 }
83 
84 static inline void
85 __list_del_entry_rcu(struct list_head *entry)
86 {
87 	__list_del_rcu(entry->prev, entry->next);
88 }
89 
90 static inline void
91 list_del_rcu(struct list_head *entry)
92 {
93 	__list_del_rcu(entry->prev, entry->next);
94 }
95 
96 #define	hlist_first_rcu(head)	(*((struct hlist_node **)(&(head)->first)))
97 #define	hlist_next_rcu(node)	(*((struct hlist_node **)(&(node)->next)))
98 #define	hlist_pprev_rcu(node)	(*((struct hlist_node **)((node)->pprev)))
99 
100 static inline void
101 hlist_add_behind_rcu(struct hlist_node *n, struct hlist_node *prev)
102 {
103 	n->next = prev->next;
104 	n->pprev = &prev->next;
105 	rcu_assign_pointer(hlist_next_rcu(prev), n);
106 	if (n->next)
107 		n->next->pprev = &n->next;
108 }
109 
110 #define	hlist_for_each_entry_rcu(pos, head, member)			\
111 	for (pos = hlist_entry_safe (rcu_dereference_raw(hlist_first_rcu(head)),\
112 	        typeof(*(pos)), member);				\
113 	     (pos);							\
114 	     pos = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(	\
115 			&(pos)->member)), typeof(*(pos)), member))
116 
117 static inline void
118 hlist_del_rcu(struct hlist_node *n)
119 {
120 	struct hlist_node *next = n->next;
121 	struct hlist_node **pprev = n->pprev;
122 
123 	WRITE_ONCE(*pprev, next);
124 	if (next)
125 		next->pprev = pprev;
126 }
127 
128 static inline void
129 hlist_add_head_rcu(struct hlist_node *n, struct hlist_head *h)
130 {
131 	struct hlist_node *first = h->first;
132 
133 	n->next = first;
134 	n->pprev = &h->first;
135 	rcu_assign_pointer(hlist_first_rcu(h), n);
136 	if (first)
137 		first->pprev = &n->next;
138 }
139 
140 static inline void
141 hlist_del_init_rcu(struct hlist_node *n)
142 {
143 	if (!hlist_unhashed(n)) {
144 		hlist_del_rcu(n);
145 		n->pprev = NULL;
146 	}
147 }
148 
149 #endif					/* _LINUXKPI_LINUX_RCULIST_H_ */
150