summaryrefslogtreecommitdiff
path: root/include/linux/list_private.h
blob: 19b01d16bedab848e80f183f6f57703a12e348dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* SPDX-License-Identifier: GPL-2.0 */

/*
 * Copyright (c) 2025, Google LLC.
 * Pasha Tatashin <pasha.tatashin@soleen.com>
 */
#ifndef _LINUX_LIST_PRIVATE_H
#define _LINUX_LIST_PRIVATE_H

/**
 * DOC: Private List Primitives
 *
 * Provides a set of list primitives identical in function to those in
 * ``<linux/list.h>``, but designed for cases where the embedded
 * ``&struct list_head`` is private member.
 */

#include <linux/compiler.h>
#include <linux/list.h>

#define __list_private_offset(type, member)					\
	((size_t)(&ACCESS_PRIVATE(((type *)0), member)))

/**
 * list_private_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the identifier passed to ACCESS_PRIVATE.
 */
#define list_private_entry(ptr, type, member) ({				\
	const struct list_head *__mptr = (ptr);					\
	(type *)((char *)__mptr - __list_private_offset(type, member));		\
})

/**
 * list_private_first_entry - get the first element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the identifier passed to ACCESS_PRIVATE.
 */
#define list_private_first_entry(ptr, type, member)				\
	list_private_entry((ptr)->next, type, member)

/**
 * list_private_last_entry - get the last element from a list
 * @ptr:	the list head to take the element from.
 * @type:	the type of the struct this is embedded in.
 * @member:	the identifier passed to ACCESS_PRIVATE.
 */
#define list_private_last_entry(ptr, type, member)				\
	list_private_entry((ptr)->prev, type, member)

/**
 * list_private_next_entry - get the next element in list
 * @pos:	the type * to cursor
 * @member:	the name of the list_head within the struct.
 */
#define list_private_next_entry(pos, member)					\
	list_private_entry(ACCESS_PRIVATE(pos, member).next, typeof(*(pos)), member)

/**
 * list_private_next_entry_circular - get the next element in list
 * @pos:	the type * to cursor.
 * @head:	the list head to take the element from.
 * @member:	the name of the list_head within the struct.
 *
 * Wraparound if pos is the last element (return the first element).
 * Note, that list is expected to be not empty.
 */
#define list_private_next_entry_circular(pos, head, member)			\
	(list_is_last(&ACCESS_PRIVATE(pos, member), head) ?			\
	list_private_first_entry(head, typeof(*(pos)), member) :		\
	list_private_next_entry(pos, member))

/**
 * list_private_prev_entry - get the prev element in list
 * @pos:	the type * to cursor
 * @member:	the name of the list_head within the struct.
 */
#define list_private_prev_entry(pos, member)					\
	list_private_entry(ACCESS_PRIVATE(pos, member).prev, typeof(*(pos)), member)

/**
 * list_private_prev_entry_circular - get the prev element in list
 * @pos:	the type * to cursor.
 * @head:	the list head to take the element from.
 * @member:	the name of the list_head within the struct.
 *
 * Wraparound if pos is the first element (return the last element).
 * Note, that list is expected to be not empty.
 */
#define list_private_prev_entry_circular(pos, head, member)			\
	(list_is_first(&ACCESS_PRIVATE(pos, member), head) ?			\
	list_private_last_entry(head, typeof(*(pos)), member) :			\
	list_private_prev_entry(pos, member))

/**
 * list_private_entry_is_head - test if the entry points to the head of the list
 * @pos:	the type * to cursor
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_private_entry_is_head(pos, head, member)				\
	list_is_head(&ACCESS_PRIVATE(pos, member), (head))

/**
 * list_private_for_each_entry - iterate over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_private_for_each_entry(pos, head, member)				\
	for (pos = list_private_first_entry(head, typeof(*pos), member);	\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_next_entry(pos, member))

/**
 * list_private_for_each_entry_reverse - iterate backwards over list of given type.
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_private_for_each_entry_reverse(pos, head, member)			\
	for (pos = list_private_last_entry(head, typeof(*pos), member);		\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_prev_entry(pos, member))

/**
 * list_private_for_each_entry_continue - continue iteration over list of given type
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Continue to iterate over list of given type, continuing after
 * the current position.
 */
#define list_private_for_each_entry_continue(pos, head, member)			\
	for (pos = list_private_next_entry(pos, member);			\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_next_entry(pos, member))

/**
 * list_private_for_each_entry_continue_reverse - iterate backwards from the given point
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Start to iterate over list of given type backwards, continuing after
 * the current position.
 */
#define list_private_for_each_entry_continue_reverse(pos, head, member)		\
	for (pos = list_private_prev_entry(pos, member);			\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_prev_entry(pos, member))

/**
 * list_private_for_each_entry_from - iterate over list of given type from the current point
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Iterate over list of given type, continuing from current position.
 */
#define list_private_for_each_entry_from(pos, head, member)			\
	for (; !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_next_entry(pos, member))

/**
 * list_private_for_each_entry_from_reverse - iterate backwards over list of given type
 *                                    from the current point
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Iterate backwards over list of given type, continuing from current position.
 */
#define list_private_for_each_entry_from_reverse(pos, head, member)		\
	for (; !list_private_entry_is_head(pos, head, member);			\
	     pos = list_private_prev_entry(pos, member))

/**
 * list_private_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 */
#define list_private_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_private_first_entry(head, typeof(*pos), member),	\
		n = list_private_next_entry(pos, member);			\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = n, n = list_private_next_entry(n, member))

/**
 * list_private_for_each_entry_safe_continue - continue list iteration safe against removal
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Iterate over list of given type, continuing after current point,
 * safe against removal of list entry.
 */
#define list_private_for_each_entry_safe_continue(pos, n, head, member)		\
	for (pos = list_private_next_entry(pos, member),			\
		n = list_private_next_entry(pos, member);			\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = n, n = list_private_next_entry(n, member))

/**
 * list_private_for_each_entry_safe_from - iterate over list from current point safe against removal
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Iterate over list of given type from current point, safe against
 * removal of list entry.
 */
#define list_private_for_each_entry_safe_from(pos, n, head, member)		\
	for (n = list_private_next_entry(pos, member);				\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = n, n = list_private_next_entry(n, member))

/**
 * list_private_for_each_entry_safe_reverse - iterate backwards over list safe against removal
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_head within the struct.
 *
 * Iterate backwards over list of given type, safe against removal
 * of list entry.
 */
#define list_private_for_each_entry_safe_reverse(pos, n, head, member)		\
	for (pos = list_private_last_entry(head, typeof(*pos), member),		\
		n = list_private_prev_entry(pos, member);			\
	     !list_private_entry_is_head(pos, head, member);			\
	     pos = n, n = list_private_prev_entry(n, member))

/**
 * list_private_safe_reset_next - reset a stale list_for_each_entry_safe loop
 * @pos:	the loop cursor used in the list_for_each_entry_safe loop
 * @n:		temporary storage used in list_for_each_entry_safe
 * @member:	the name of the list_head within the struct.
 *
 * list_safe_reset_next is not safe to use in general if the list may be
 * modified concurrently (eg. the lock is dropped in the loop body). An
 * exception to this is if the cursor element (pos) is pinned in the list,
 * and list_safe_reset_next is called after re-taking the lock and before
 * completing the current iteration of the loop body.
 */
#define list_private_safe_reset_next(pos, n, member)				\
	n = list_private_next_entry(pos, member)

#endif /* _LINUX_LIST_PRIVATE_H */