xref: /linux/include/linux/list_nulls.h (revision 02b99b38)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2bbaffacaSEric Dumazet #ifndef _LINUX_LIST_NULLS_H
3bbaffacaSEric Dumazet #define _LINUX_LIST_NULLS_H
4bbaffacaSEric Dumazet 
5a3449dedSYing Xue #include <linux/poison.h>
6a3449dedSYing Xue #include <linux/const.h>
7a3449dedSYing Xue 
8bbaffacaSEric Dumazet /*
9bbaffacaSEric Dumazet  * Special version of lists, where end of list is not a NULL pointer,
10bbaffacaSEric Dumazet  * but a 'nulls' marker, which can have many different values.
11bbaffacaSEric Dumazet  * (up to 2^31 different values guaranteed on all platforms)
12bbaffacaSEric Dumazet  *
13bbaffacaSEric Dumazet  * In the standard hlist, termination of a list is the NULL pointer.
14bbaffacaSEric Dumazet  * In this special 'nulls' variant, we use the fact that objects stored in
15bbaffacaSEric Dumazet  * a list are aligned on a word (4 or 8 bytes alignment).
16bbaffacaSEric Dumazet  * We therefore use the last significant bit of 'ptr' :
17bbaffacaSEric Dumazet  * Set to 1 : This is a 'nulls' end-of-list marker (ptr >> 1)
18bbaffacaSEric Dumazet  * Set to 0 : This is a pointer to some object (ptr)
19bbaffacaSEric Dumazet  */
20bbaffacaSEric Dumazet 
21bbaffacaSEric Dumazet struct hlist_nulls_head {
22bbaffacaSEric Dumazet 	struct hlist_nulls_node *first;
23bbaffacaSEric Dumazet };
24bbaffacaSEric Dumazet 
25bbaffacaSEric Dumazet struct hlist_nulls_node {
26bbaffacaSEric Dumazet 	struct hlist_nulls_node *next, **pprev;
27bbaffacaSEric Dumazet };
28f89bd6f8SThomas Graf #define NULLS_MARKER(value) (1UL | (((long)value) << 1))
29bbaffacaSEric Dumazet #define INIT_HLIST_NULLS_HEAD(ptr, nulls) \
30f89bd6f8SThomas Graf 	((ptr)->first = (struct hlist_nulls_node *) NULLS_MARKER(nulls))
31bbaffacaSEric Dumazet 
32bbaffacaSEric Dumazet #define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
334fe84359SAlexei Starovoitov 
344fe84359SAlexei Starovoitov #define hlist_nulls_entry_safe(ptr, type, member) \
354fe84359SAlexei Starovoitov 	({ typeof(ptr) ____ptr = (ptr); \
364fe84359SAlexei Starovoitov 	   !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \
374fe84359SAlexei Starovoitov 	})
38bbaffacaSEric Dumazet /**
39bbaffacaSEric Dumazet  * ptr_is_a_nulls - Test if a ptr is a nulls
40bbaffacaSEric Dumazet  * @ptr: ptr to be tested
41bbaffacaSEric Dumazet  *
42bbaffacaSEric Dumazet  */
is_a_nulls(const struct hlist_nulls_node * ptr)43bbaffacaSEric Dumazet static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
44bbaffacaSEric Dumazet {
45bbaffacaSEric Dumazet 	return ((unsigned long)ptr & 1);
46bbaffacaSEric Dumazet }
47bbaffacaSEric Dumazet 
48bbaffacaSEric Dumazet /**
49bbaffacaSEric Dumazet  * get_nulls_value - Get the 'nulls' value of the end of chain
50bbaffacaSEric Dumazet  * @ptr: end of chain
51bbaffacaSEric Dumazet  *
52bbaffacaSEric Dumazet  * Should be called only if is_a_nulls(ptr);
53bbaffacaSEric Dumazet  */
get_nulls_value(const struct hlist_nulls_node * ptr)54bbaffacaSEric Dumazet static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
55bbaffacaSEric Dumazet {
56bbaffacaSEric Dumazet 	return ((unsigned long)ptr) >> 1;
57bbaffacaSEric Dumazet }
58bbaffacaSEric Dumazet 
59*02b99b38SPaul E. McKenney /**
60*02b99b38SPaul E. McKenney  * hlist_nulls_unhashed - Has node been removed and reinitialized?
61*02b99b38SPaul E. McKenney  * @h: Node to be checked
62*02b99b38SPaul E. McKenney  *
63*02b99b38SPaul E. McKenney  * Not that not all removal functions will leave a node in unhashed state.
64*02b99b38SPaul E. McKenney  * For example, hlist_del_init_rcu() leaves the node in unhashed state,
65*02b99b38SPaul E. McKenney  * but hlist_nulls_del() does not.
66*02b99b38SPaul E. McKenney  */
hlist_nulls_unhashed(const struct hlist_nulls_node * h)67bbaffacaSEric Dumazet static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
68bbaffacaSEric Dumazet {
69bbaffacaSEric Dumazet 	return !h->pprev;
70bbaffacaSEric Dumazet }
71bbaffacaSEric Dumazet 
72*02b99b38SPaul E. McKenney /**
73*02b99b38SPaul E. McKenney  * hlist_nulls_unhashed_lockless - Has node been removed and reinitialized?
74*02b99b38SPaul E. McKenney  * @h: Node to be checked
75*02b99b38SPaul E. McKenney  *
76*02b99b38SPaul E. McKenney  * Not that not all removal functions will leave a node in unhashed state.
77*02b99b38SPaul E. McKenney  * For example, hlist_del_init_rcu() leaves the node in unhashed state,
78*02b99b38SPaul E. McKenney  * but hlist_nulls_del() does not.  Unlike hlist_nulls_unhashed(), this
79*02b99b38SPaul E. McKenney  * function may be used locklessly.
80*02b99b38SPaul E. McKenney  */
hlist_nulls_unhashed_lockless(const struct hlist_nulls_node * h)81*02b99b38SPaul E. McKenney static inline int hlist_nulls_unhashed_lockless(const struct hlist_nulls_node *h)
82*02b99b38SPaul E. McKenney {
83*02b99b38SPaul E. McKenney 	return !READ_ONCE(h->pprev);
84*02b99b38SPaul E. McKenney }
85*02b99b38SPaul E. McKenney 
hlist_nulls_empty(const struct hlist_nulls_head * h)86bbaffacaSEric Dumazet static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
87bbaffacaSEric Dumazet {
881658d35eSPaul E. McKenney 	return is_a_nulls(READ_ONCE(h->first));
89bbaffacaSEric Dumazet }
90bbaffacaSEric Dumazet 
hlist_nulls_add_head(struct hlist_nulls_node * n,struct hlist_nulls_head * h)91d219dce7SPablo Neira Ayuso static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
92d219dce7SPablo Neira Ayuso 					struct hlist_nulls_head *h)
93d219dce7SPablo Neira Ayuso {
94d219dce7SPablo Neira Ayuso 	struct hlist_nulls_node *first = h->first;
95d219dce7SPablo Neira Ayuso 
96d219dce7SPablo Neira Ayuso 	n->next = first;
97860c8802SPaul E. McKenney 	WRITE_ONCE(n->pprev, &h->first);
98d219dce7SPablo Neira Ayuso 	h->first = n;
99d219dce7SPablo Neira Ayuso 	if (!is_a_nulls(first))
100860c8802SPaul E. McKenney 		WRITE_ONCE(first->pprev, &n->next);
101d219dce7SPablo Neira Ayuso }
102d219dce7SPablo Neira Ayuso 
__hlist_nulls_del(struct hlist_nulls_node * n)103bbaffacaSEric Dumazet static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
104bbaffacaSEric Dumazet {
105bbaffacaSEric Dumazet 	struct hlist_nulls_node *next = n->next;
106bbaffacaSEric Dumazet 	struct hlist_nulls_node **pprev = n->pprev;
1077f5f873cSPaul E. McKenney 
1087f5f873cSPaul E. McKenney 	WRITE_ONCE(*pprev, next);
109bbaffacaSEric Dumazet 	if (!is_a_nulls(next))
110860c8802SPaul E. McKenney 		WRITE_ONCE(next->pprev, pprev);
111bbaffacaSEric Dumazet }
112bbaffacaSEric Dumazet 
hlist_nulls_del(struct hlist_nulls_node * n)113d219dce7SPablo Neira Ayuso static inline void hlist_nulls_del(struct hlist_nulls_node *n)
114d219dce7SPablo Neira Ayuso {
115d219dce7SPablo Neira Ayuso 	__hlist_nulls_del(n);
116860c8802SPaul E. McKenney 	WRITE_ONCE(n->pprev, LIST_POISON2);
117d219dce7SPablo Neira Ayuso }
118d219dce7SPablo Neira Ayuso 
119bbaffacaSEric Dumazet /**
120bbaffacaSEric Dumazet  * hlist_nulls_for_each_entry	- iterate over list of given type
121bbaffacaSEric Dumazet  * @tpos:	the type * to use as a loop cursor.
122bbaffacaSEric Dumazet  * @pos:	the &struct hlist_node to use as a loop cursor.
123bbaffacaSEric Dumazet  * @head:	the head for your list.
124bbaffacaSEric Dumazet  * @member:	the name of the hlist_node within the struct.
125bbaffacaSEric Dumazet  *
126bbaffacaSEric Dumazet  */
127bbaffacaSEric Dumazet #define hlist_nulls_for_each_entry(tpos, pos, head, member)		       \
128bbaffacaSEric Dumazet 	for (pos = (head)->first;					       \
129bbaffacaSEric Dumazet 	     (!is_a_nulls(pos)) &&					       \
130bbaffacaSEric Dumazet 		({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
131bbaffacaSEric Dumazet 	     pos = pos->next)
132bbaffacaSEric Dumazet 
133bbaffacaSEric Dumazet /**
134bbaffacaSEric Dumazet  * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
135bbaffacaSEric Dumazet  * @tpos:	the type * to use as a loop cursor.
136bbaffacaSEric Dumazet  * @pos:	the &struct hlist_node to use as a loop cursor.
137bbaffacaSEric Dumazet  * @member:	the name of the hlist_node within the struct.
138bbaffacaSEric Dumazet  *
139bbaffacaSEric Dumazet  */
140bbaffacaSEric Dumazet #define hlist_nulls_for_each_entry_from(tpos, pos, member)	\
141bbaffacaSEric Dumazet 	for (; (!is_a_nulls(pos)) && 				\
142bbaffacaSEric Dumazet 		({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
143bbaffacaSEric Dumazet 	     pos = pos->next)
144bbaffacaSEric Dumazet 
145bbaffacaSEric Dumazet #endif
146