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))
31*8c6bbda8SSebastian Andrzej Siewior #define HLIST_NULLS_HEAD_INIT(nulls) {.first = (struct hlist_nulls_node *)NULLS_MARKER(nulls)}
32bbaffacaSEric Dumazet
33bbaffacaSEric Dumazet #define hlist_nulls_entry(ptr, type, member) container_of(ptr,type,member)
344fe84359SAlexei Starovoitov
354fe84359SAlexei Starovoitov #define hlist_nulls_entry_safe(ptr, type, member) \
364fe84359SAlexei Starovoitov ({ typeof(ptr) ____ptr = (ptr); \
374fe84359SAlexei Starovoitov !is_a_nulls(____ptr) ? hlist_nulls_entry(____ptr, type, member) : NULL; \
384fe84359SAlexei Starovoitov })
39bbaffacaSEric Dumazet /**
40bbaffacaSEric Dumazet * ptr_is_a_nulls - Test if a ptr is a nulls
41bbaffacaSEric Dumazet * @ptr: ptr to be tested
42bbaffacaSEric Dumazet *
43bbaffacaSEric Dumazet */
is_a_nulls(const struct hlist_nulls_node * ptr)44bbaffacaSEric Dumazet static inline int is_a_nulls(const struct hlist_nulls_node *ptr)
45bbaffacaSEric Dumazet {
46bbaffacaSEric Dumazet return ((unsigned long)ptr & 1);
47bbaffacaSEric Dumazet }
48bbaffacaSEric Dumazet
49bbaffacaSEric Dumazet /**
50bbaffacaSEric Dumazet * get_nulls_value - Get the 'nulls' value of the end of chain
51bbaffacaSEric Dumazet * @ptr: end of chain
52bbaffacaSEric Dumazet *
53bbaffacaSEric Dumazet * Should be called only if is_a_nulls(ptr);
54bbaffacaSEric Dumazet */
get_nulls_value(const struct hlist_nulls_node * ptr)55bbaffacaSEric Dumazet static inline unsigned long get_nulls_value(const struct hlist_nulls_node *ptr)
56bbaffacaSEric Dumazet {
57bbaffacaSEric Dumazet return ((unsigned long)ptr) >> 1;
58bbaffacaSEric Dumazet }
59bbaffacaSEric Dumazet
6002b99b38SPaul E. McKenney /**
6102b99b38SPaul E. McKenney * hlist_nulls_unhashed - Has node been removed and reinitialized?
6202b99b38SPaul E. McKenney * @h: Node to be checked
6302b99b38SPaul E. McKenney *
6402b99b38SPaul E. McKenney * Not that not all removal functions will leave a node in unhashed state.
6502b99b38SPaul E. McKenney * For example, hlist_del_init_rcu() leaves the node in unhashed state,
6602b99b38SPaul E. McKenney * but hlist_nulls_del() does not.
6702b99b38SPaul E. McKenney */
hlist_nulls_unhashed(const struct hlist_nulls_node * h)68bbaffacaSEric Dumazet static inline int hlist_nulls_unhashed(const struct hlist_nulls_node *h)
69bbaffacaSEric Dumazet {
70bbaffacaSEric Dumazet return !h->pprev;
71bbaffacaSEric Dumazet }
72bbaffacaSEric Dumazet
7302b99b38SPaul E. McKenney /**
7402b99b38SPaul E. McKenney * hlist_nulls_unhashed_lockless - Has node been removed and reinitialized?
7502b99b38SPaul E. McKenney * @h: Node to be checked
7602b99b38SPaul E. McKenney *
7702b99b38SPaul E. McKenney * Not that not all removal functions will leave a node in unhashed state.
7802b99b38SPaul E. McKenney * For example, hlist_del_init_rcu() leaves the node in unhashed state,
7902b99b38SPaul E. McKenney * but hlist_nulls_del() does not. Unlike hlist_nulls_unhashed(), this
8002b99b38SPaul E. McKenney * function may be used locklessly.
8102b99b38SPaul E. McKenney */
hlist_nulls_unhashed_lockless(const struct hlist_nulls_node * h)8202b99b38SPaul E. McKenney static inline int hlist_nulls_unhashed_lockless(const struct hlist_nulls_node *h)
8302b99b38SPaul E. McKenney {
8402b99b38SPaul E. McKenney return !READ_ONCE(h->pprev);
8502b99b38SPaul E. McKenney }
8602b99b38SPaul E. McKenney
hlist_nulls_empty(const struct hlist_nulls_head * h)87bbaffacaSEric Dumazet static inline int hlist_nulls_empty(const struct hlist_nulls_head *h)
88bbaffacaSEric Dumazet {
891658d35eSPaul E. McKenney return is_a_nulls(READ_ONCE(h->first));
90bbaffacaSEric Dumazet }
91bbaffacaSEric Dumazet
hlist_nulls_add_head(struct hlist_nulls_node * n,struct hlist_nulls_head * h)92d219dce7SPablo Neira Ayuso static inline void hlist_nulls_add_head(struct hlist_nulls_node *n,
93d219dce7SPablo Neira Ayuso struct hlist_nulls_head *h)
94d219dce7SPablo Neira Ayuso {
95d219dce7SPablo Neira Ayuso struct hlist_nulls_node *first = h->first;
96d219dce7SPablo Neira Ayuso
97d219dce7SPablo Neira Ayuso n->next = first;
98860c8802SPaul E. McKenney WRITE_ONCE(n->pprev, &h->first);
99d219dce7SPablo Neira Ayuso h->first = n;
100d219dce7SPablo Neira Ayuso if (!is_a_nulls(first))
101860c8802SPaul E. McKenney WRITE_ONCE(first->pprev, &n->next);
102d219dce7SPablo Neira Ayuso }
103d219dce7SPablo Neira Ayuso
__hlist_nulls_del(struct hlist_nulls_node * n)104bbaffacaSEric Dumazet static inline void __hlist_nulls_del(struct hlist_nulls_node *n)
105bbaffacaSEric Dumazet {
106bbaffacaSEric Dumazet struct hlist_nulls_node *next = n->next;
107bbaffacaSEric Dumazet struct hlist_nulls_node **pprev = n->pprev;
1087f5f873cSPaul E. McKenney
1097f5f873cSPaul E. McKenney WRITE_ONCE(*pprev, next);
110bbaffacaSEric Dumazet if (!is_a_nulls(next))
111860c8802SPaul E. McKenney WRITE_ONCE(next->pprev, pprev);
112bbaffacaSEric Dumazet }
113bbaffacaSEric Dumazet
hlist_nulls_del(struct hlist_nulls_node * n)114d219dce7SPablo Neira Ayuso static inline void hlist_nulls_del(struct hlist_nulls_node *n)
115d219dce7SPablo Neira Ayuso {
116d219dce7SPablo Neira Ayuso __hlist_nulls_del(n);
117860c8802SPaul E. McKenney WRITE_ONCE(n->pprev, LIST_POISON2);
118d219dce7SPablo Neira Ayuso }
119d219dce7SPablo Neira Ayuso
120bbaffacaSEric Dumazet /**
121bbaffacaSEric Dumazet * hlist_nulls_for_each_entry - iterate over list of given type
122bbaffacaSEric Dumazet * @tpos: the type * to use as a loop cursor.
123bbaffacaSEric Dumazet * @pos: the &struct hlist_node to use as a loop cursor.
124bbaffacaSEric Dumazet * @head: the head for your list.
125bbaffacaSEric Dumazet * @member: the name of the hlist_node within the struct.
126bbaffacaSEric Dumazet *
127bbaffacaSEric Dumazet */
128bbaffacaSEric Dumazet #define hlist_nulls_for_each_entry(tpos, pos, head, member) \
129bbaffacaSEric Dumazet for (pos = (head)->first; \
130bbaffacaSEric Dumazet (!is_a_nulls(pos)) && \
131bbaffacaSEric Dumazet ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
132bbaffacaSEric Dumazet pos = pos->next)
133bbaffacaSEric Dumazet
134bbaffacaSEric Dumazet /**
135bbaffacaSEric Dumazet * hlist_nulls_for_each_entry_from - iterate over a hlist continuing from current point
136bbaffacaSEric Dumazet * @tpos: the type * to use as a loop cursor.
137bbaffacaSEric Dumazet * @pos: the &struct hlist_node to use as a loop cursor.
138bbaffacaSEric Dumazet * @member: the name of the hlist_node within the struct.
139bbaffacaSEric Dumazet *
140bbaffacaSEric Dumazet */
141bbaffacaSEric Dumazet #define hlist_nulls_for_each_entry_from(tpos, pos, member) \
142bbaffacaSEric Dumazet for (; (!is_a_nulls(pos)) && \
143bbaffacaSEric Dumazet ({ tpos = hlist_nulls_entry(pos, typeof(*tpos), member); 1;}); \
144bbaffacaSEric Dumazet pos = pos->next)
145bbaffacaSEric Dumazet
146bbaffacaSEric Dumazet #endif
147