list.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_LIST_H
  3. #define _LINUX_LIST_H
  4. #include <linux/types.h>
  5. #include <linux/stddef.h>
  6. #include <linux/poison.h>
  7. #include <linux/const.h>
  8. #include <linux/kernel.h>
  9. /*
  10. * Simple doubly linked list implementation.
  11. *
  12. * Some of the internal functions ("__xxx") are useful when
  13. * manipulating whole lists rather than single entries, as
  14. * sometimes we already know the next/prev entries and we can
  15. * generate better code by using them directly rather than
  16. * using the generic single-entry routines.
  17. */
  18. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  19. #define LIST_HEAD(name) \
  20. struct list_head name = LIST_HEAD_INIT(name)
  21. /**
  22. * INIT_LIST_HEAD - Initialize a list_head structure
  23. * @list: list_head structure to be initialized.
  24. *
  25. * Initializes the list_head to point to itself. If it is a list header,
  26. * the result is an empty list.
  27. */
  28. static inline void INIT_LIST_HEAD(struct list_head *list)
  29. {
  30. WRITE_ONCE(list->next, list);
  31. list->prev = list;
  32. }
  33. #ifdef CONFIG_DEBUG_LIST
  34. extern bool __list_add_valid(struct list_head *new,
  35. struct list_head *prev,
  36. struct list_head *next);
  37. extern bool __list_del_entry_valid(struct list_head *entry);
  38. #else
  39. static inline bool __list_add_valid(struct list_head *new,
  40. struct list_head *prev,
  41. struct list_head *next)
  42. {
  43. return true;
  44. }
  45. static inline bool __list_del_entry_valid(struct list_head *entry)
  46. {
  47. return true;
  48. }
  49. #endif
  50. /*
  51. * Insert a new entry between two known consecutive entries.
  52. *
  53. * This is only for internal list manipulation where we know
  54. * the prev/next entries already!
  55. */
  56. static inline void __list_add(struct list_head *new,
  57. struct list_head *prev,
  58. struct list_head *next)
  59. {
  60. if (!__list_add_valid(new, prev, next))
  61. return;
  62. next->prev = new;
  63. new->next = next;
  64. new->prev = prev;
  65. WRITE_ONCE(prev->next, new);
  66. }
  67. /**
  68. * list_add - add a new entry
  69. * @new: new entry to be added
  70. * @head: list head to add it after
  71. *
  72. * Insert a new entry after the specified head.
  73. * This is good for implementing stacks.
  74. */
  75. static inline void list_add(struct list_head *new, struct list_head *head)
  76. {
  77. __list_add(new, head, head->next);
  78. }
  79. /**
  80. * list_add_tail - add a new entry
  81. * @new: new entry to be added
  82. * @head: list head to add it before
  83. *
  84. * Insert a new entry before the specified head.
  85. * This is useful for implementing queues.
  86. */
  87. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  88. {
  89. __list_add(new, head->prev, head);
  90. }
  91. /*
  92. * Delete a list entry by making the prev/next entries
  93. * point to each other.
  94. *
  95. * This is only for internal list manipulation where we know
  96. * the prev/next entries already!
  97. */
  98. static inline void __list_del(struct list_head * prev, struct list_head * next)
  99. {
  100. next->prev = prev;
  101. WRITE_ONCE(prev->next, next);
  102. }
  103. /*
  104. * Delete a list entry and clear the 'prev' pointer.
  105. *
  106. * This is a special-purpose list clearing method used in the networking code
  107. * for lists allocated as per-cpu, where we don't want to incur the extra
  108. * WRITE_ONCE() overhead of a regular list_del_init(). The code that uses this
  109. * needs to check the node 'prev' pointer instead of calling list_empty().
  110. */
  111. static inline void __list_del_clearprev(struct list_head *entry)
  112. {
  113. __list_del(entry->prev, entry->next);
  114. entry->prev = NULL;
  115. }
  116. static inline void __list_del_entry(struct list_head *entry)
  117. {
  118. if (!__list_del_entry_valid(entry))
  119. return;
  120. __list_del(entry->prev, entry->next);
  121. }
  122. /**
  123. * list_del - deletes entry from list.
  124. * @entry: the element to delete from the list.
  125. * Note: list_empty() on entry does not return true after this, the entry is
  126. * in an undefined state.
  127. */
  128. static inline void list_del(struct list_head *entry)
  129. {
  130. __list_del_entry(entry);
  131. entry->next = LIST_POISON1;
  132. entry->prev = LIST_POISON2;
  133. }
  134. /**
  135. * list_replace - replace old entry by new one
  136. * @old : the element to be replaced
  137. * @new : the new element to insert
  138. *
  139. * If @old was empty, it will be overwritten.
  140. */
  141. static inline void list_replace(struct list_head *old,
  142. struct list_head *new)
  143. {
  144. new->next = old->next;
  145. new->next->prev = new;
  146. new->prev = old->prev;
  147. new->prev->next = new;
  148. }
  149. /**
  150. * list_replace_init - replace old entry by new one and initialize the old one
  151. * @old : the element to be replaced
  152. * @new : the new element to insert
  153. *
  154. * If @old was empty, it will be overwritten.
  155. */
  156. static inline void list_replace_init(struct list_head *old,
  157. struct list_head *new)
  158. {
  159. list_replace(old, new);
  160. INIT_LIST_HEAD(old);
  161. }
  162. /**
  163. * list_swap - replace entry1 with entry2 and re-add entry1 at entry2's position
  164. * @entry1: the location to place entry2
  165. * @entry2: the location to place entry1
  166. */
  167. static inline void list_swap(struct list_head *entry1,
  168. struct list_head *entry2)
  169. {
  170. struct list_head *pos = entry2->prev;
  171. list_del(entry2);
  172. list_replace(entry1, entry2);
  173. if (pos == entry1)
  174. pos = entry2;
  175. list_add(entry1, pos);
  176. }
  177. /**
  178. * list_del_init - deletes entry from list and reinitialize it.
  179. * @entry: the element to delete from the list.
  180. */
  181. static inline void list_del_init(struct list_head *entry)
  182. {
  183. __list_del_entry(entry);
  184. INIT_LIST_HEAD(entry);
  185. }
  186. /**
  187. * list_move - delete from one list and add as another's head
  188. * @list: the entry to move
  189. * @head: the head that will precede our entry
  190. */
  191. static inline void list_move(struct list_head *list, struct list_head *head)
  192. {
  193. __list_del_entry(list);
  194. list_add(list, head);
  195. }
  196. /**
  197. * list_move_tail - delete from one list and add as another's tail
  198. * @list: the entry to move
  199. * @head: the head that will follow our entry
  200. */
  201. static inline void list_move_tail(struct list_head *list,
  202. struct list_head *head)
  203. {
  204. __list_del_entry(list);
  205. list_add_tail(list, head);
  206. }
  207. /**
  208. * list_bulk_move_tail - move a subsection of a list to its tail
  209. * @head: the head that will follow our entry
  210. * @first: first entry to move
  211. * @last: last entry to move, can be the same as first
  212. *
  213. * Move all entries between @first and including @last before @head.
  214. * All three entries must belong to the same linked list.
  215. */
  216. static inline void list_bulk_move_tail(struct list_head *head,
  217. struct list_head *first,
  218. struct list_head *last)
  219. {
  220. first->prev->next = last->next;
  221. last->next->prev = first->prev;
  222. head->prev->next = first;
  223. first->prev = head->prev;
  224. last->next = head;
  225. head->prev = last;
  226. }
  227. /**
  228. * list_is_first -- tests whether @list is the first entry in list @head
  229. * @list: the entry to test
  230. * @head: the head of the list
  231. */
  232. static inline int list_is_first(const struct list_head *list,
  233. const struct list_head *head)
  234. {
  235. return list->prev == head;
  236. }
  237. /**
  238. * list_is_last - tests whether @list is the last entry in list @head
  239. * @list: the entry to test
  240. * @head: the head of the list
  241. */
  242. static inline int list_is_last(const struct list_head *list,
  243. const struct list_head *head)
  244. {
  245. return list->next == head;
  246. }
  247. /**
  248. * list_empty - tests whether a list is empty
  249. * @head: the list to test.
  250. */
  251. static inline int list_empty(const struct list_head *head)
  252. {
  253. return READ_ONCE(head->next) == head;
  254. }
  255. /**
  256. * list_del_init_careful - deletes entry from list and reinitialize it.
  257. * @entry: the element to delete from the list.
  258. *
  259. * This is the same as list_del_init(), except designed to be used
  260. * together with list_empty_careful() in a way to guarantee ordering
  261. * of other memory operations.
  262. *
  263. * Any memory operations done before a list_del_init_careful() are
  264. * guaranteed to be visible after a list_empty_careful() test.
  265. */
  266. static inline void list_del_init_careful(struct list_head *entry)
  267. {
  268. __list_del_entry(entry);
  269. entry->prev = entry;
  270. smp_store_release(&entry->next, entry);
  271. }
  272. /**
  273. * list_empty_careful - tests whether a list is empty and not being modified
  274. * @head: the list to test
  275. *
  276. * Description:
  277. * tests whether a list is empty _and_ checks that no other CPU might be
  278. * in the process of modifying either member (next or prev)
  279. *
  280. * NOTE: using list_empty_careful() without synchronization
  281. * can only be safe if the only activity that can happen
  282. * to the list entry is list_del_init(). Eg. it cannot be used
  283. * if another CPU could re-list_add() it.
  284. */
  285. static inline int list_empty_careful(const struct list_head *head)
  286. {
  287. struct list_head *next = smp_load_acquire(&head->next);
  288. return (next == head) && (next == head->prev);
  289. }
  290. /**
  291. * list_rotate_left - rotate the list to the left
  292. * @head: the head of the list
  293. */
  294. static inline void list_rotate_left(struct list_head *head)
  295. {
  296. struct list_head *first;
  297. if (!list_empty(head)) {
  298. first = head->next;
  299. list_move_tail(first, head);
  300. }
  301. }
  302. /**
  303. * list_rotate_to_front() - Rotate list to specific item.
  304. * @list: The desired new front of the list.
  305. * @head: The head of the list.
  306. *
  307. * Rotates list so that @list becomes the new front of the list.
  308. */
  309. static inline void list_rotate_to_front(struct list_head *list,
  310. struct list_head *head)
  311. {
  312. /*
  313. * Deletes the list head from the list denoted by @head and
  314. * places it as the tail of @list, this effectively rotates the
  315. * list so that @list is at the front.
  316. */
  317. list_move_tail(head, list);
  318. }
  319. /**
  320. * list_is_singular - tests whether a list has just one entry.
  321. * @head: the list to test.
  322. */
  323. static inline int list_is_singular(const struct list_head *head)
  324. {
  325. return !list_empty(head) && (head->next == head->prev);
  326. }
  327. static inline void __list_cut_position(struct list_head *list,
  328. struct list_head *head, struct list_head *entry)
  329. {
  330. struct list_head *new_first = entry->next;
  331. list->next = head->next;
  332. list->next->prev = list;
  333. list->prev = entry;
  334. entry->next = list;
  335. head->next = new_first;
  336. new_first->prev = head;
  337. }
  338. /**
  339. * list_cut_position - cut a list into two
  340. * @list: a new list to add all removed entries
  341. * @head: a list with entries
  342. * @entry: an entry within head, could be the head itself
  343. * and if so we won't cut the list
  344. *
  345. * This helper moves the initial part of @head, up to and
  346. * including @entry, from @head to @list. You should
  347. * pass on @entry an element you know is on @head. @list
  348. * should be an empty list or a list you do not care about
  349. * losing its data.
  350. *
  351. */
  352. static inline void list_cut_position(struct list_head *list,
  353. struct list_head *head, struct list_head *entry)
  354. {
  355. if (list_empty(head))
  356. return;
  357. if (list_is_singular(head) &&
  358. (head->next != entry && head != entry))
  359. return;
  360. if (entry == head)
  361. INIT_LIST_HEAD(list);
  362. else
  363. __list_cut_position(list, head, entry);
  364. }
  365. /**
  366. * list_cut_before - cut a list into two, before given entry
  367. * @list: a new list to add all removed entries
  368. * @head: a list with entries
  369. * @entry: an entry within head, could be the head itself
  370. *
  371. * This helper moves the initial part of @head, up to but
  372. * excluding @entry, from @head to @list. You should pass
  373. * in @entry an element you know is on @head. @list should
  374. * be an empty list or a list you do not care about losing
  375. * its data.
  376. * If @entry == @head, all entries on @head are moved to
  377. * @list.
  378. */
  379. static inline void list_cut_before(struct list_head *list,
  380. struct list_head *head,
  381. struct list_head *entry)
  382. {
  383. if (head->next == entry) {
  384. INIT_LIST_HEAD(list);
  385. return;
  386. }
  387. list->next = head->next;
  388. list->next->prev = list;
  389. list->prev = entry->prev;
  390. list->prev->next = list;
  391. head->next = entry;
  392. entry->prev = head;
  393. }
  394. static inline void __list_splice(const struct list_head *list,
  395. struct list_head *prev,
  396. struct list_head *next)
  397. {
  398. struct list_head *first = list->next;
  399. struct list_head *last = list->prev;
  400. first->prev = prev;
  401. prev->next = first;
  402. last->next = next;
  403. next->prev = last;
  404. }
  405. /**
  406. * list_splice - join two lists, this is designed for stacks
  407. * @list: the new list to add.
  408. * @head: the place to add it in the first list.
  409. */
  410. static inline void list_splice(const struct list_head *list,
  411. struct list_head *head)
  412. {
  413. if (!list_empty(list))
  414. __list_splice(list, head, head->next);
  415. }
  416. /**
  417. * list_splice_tail - join two lists, each list being a queue
  418. * @list: the new list to add.
  419. * @head: the place to add it in the first list.
  420. */
  421. static inline void list_splice_tail(struct list_head *list,
  422. struct list_head *head)
  423. {
  424. if (!list_empty(list))
  425. __list_splice(list, head->prev, head);
  426. }
  427. /**
  428. * list_splice_init - join two lists and reinitialise the emptied list.
  429. * @list: the new list to add.
  430. * @head: the place to add it in the first list.
  431. *
  432. * The list at @list is reinitialised
  433. */
  434. static inline void list_splice_init(struct list_head *list,
  435. struct list_head *head)
  436. {
  437. if (!list_empty(list)) {
  438. __list_splice(list, head, head->next);
  439. INIT_LIST_HEAD(list);
  440. }
  441. }
  442. /**
  443. * list_splice_tail_init - join two lists and reinitialise the emptied list
  444. * @list: the new list to add.
  445. * @head: the place to add it in the first list.
  446. *
  447. * Each of the lists is a queue.
  448. * The list at @list is reinitialised
  449. */
  450. static inline void list_splice_tail_init(struct list_head *list,
  451. struct list_head *head)
  452. {
  453. if (!list_empty(list)) {
  454. __list_splice(list, head->prev, head);
  455. INIT_LIST_HEAD(list);
  456. }
  457. }
  458. /**
  459. * list_entry - get the struct for this entry
  460. * @ptr: the &struct list_head pointer.
  461. * @type: the type of the struct this is embedded in.
  462. * @member: the name of the list_head within the struct.
  463. */
  464. #define list_entry(ptr, type, member) \
  465. container_of(ptr, type, member)
  466. /**
  467. * list_first_entry - get the first element from a list
  468. * @ptr: the list head to take the element from.
  469. * @type: the type of the struct this is embedded in.
  470. * @member: the name of the list_head within the struct.
  471. *
  472. * Note, that list is expected to be not empty.
  473. */
  474. #define list_first_entry(ptr, type, member) \
  475. list_entry((ptr)->next, type, member)
  476. /**
  477. * list_last_entry - get the last element from a list
  478. * @ptr: the list head to take the element from.
  479. * @type: the type of the struct this is embedded in.
  480. * @member: the name of the list_head within the struct.
  481. *
  482. * Note, that list is expected to be not empty.
  483. */
  484. #define list_last_entry(ptr, type, member) \
  485. list_entry((ptr)->prev, type, member)
  486. /**
  487. * list_first_entry_or_null - get the first element from a list
  488. * @ptr: the list head to take the element from.
  489. * @type: the type of the struct this is embedded in.
  490. * @member: the name of the list_head within the struct.
  491. *
  492. * Note that if the list is empty, it returns NULL.
  493. */
  494. #define list_first_entry_or_null(ptr, type, member) ({ \
  495. struct list_head *head__ = (ptr); \
  496. struct list_head *pos__ = READ_ONCE(head__->next); \
  497. pos__ != head__ ? list_entry(pos__, type, member) : NULL; \
  498. })
  499. /**
  500. * list_next_entry - get the next element in list
  501. * @pos: the type * to cursor
  502. * @member: the name of the list_head within the struct.
  503. */
  504. #define list_next_entry(pos, member) \
  505. list_entry((pos)->member.next, typeof(*(pos)), member)
  506. /**
  507. * list_prev_entry - get the prev element in list
  508. * @pos: the type * to cursor
  509. * @member: the name of the list_head within the struct.
  510. */
  511. #define list_prev_entry(pos, member) \
  512. list_entry((pos)->member.prev, typeof(*(pos)), member)
  513. /**
  514. * list_for_each - iterate over a list
  515. * @pos: the &struct list_head to use as a loop cursor.
  516. * @head: the head for your list.
  517. */
  518. #define list_for_each(pos, head) \
  519. for (pos = (head)->next; pos != (head); pos = pos->next)
  520. /**
  521. * list_for_each_continue - continue iteration over a list
  522. * @pos: the &struct list_head to use as a loop cursor.
  523. * @head: the head for your list.
  524. *
  525. * Continue to iterate over a list, continuing after the current position.
  526. */
  527. #define list_for_each_continue(pos, head) \
  528. for (pos = pos->next; pos != (head); pos = pos->next)
  529. /**
  530. * list_for_each_prev - iterate over a list backwards
  531. * @pos: the &struct list_head to use as a loop cursor.
  532. * @head: the head for your list.
  533. */
  534. #define list_for_each_prev(pos, head) \
  535. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  536. /**
  537. * list_for_each_safe - iterate over a list safe against removal of list entry
  538. * @pos: the &struct list_head to use as a loop cursor.
  539. * @n: another &struct list_head to use as temporary storage
  540. * @head: the head for your list.
  541. */
  542. #define list_for_each_safe(pos, n, head) \
  543. for (pos = (head)->next, n = pos->next; pos != (head); \
  544. pos = n, n = pos->next)
  545. /**
  546. * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
  547. * @pos: the &struct list_head to use as a loop cursor.
  548. * @n: another &struct list_head to use as temporary storage
  549. * @head: the head for your list.
  550. */
  551. #define list_for_each_prev_safe(pos, n, head) \
  552. for (pos = (head)->prev, n = pos->prev; \
  553. pos != (head); \
  554. pos = n, n = pos->prev)
  555. /**
  556. * list_entry_is_head - test if the entry points to the head of the list
  557. * @pos: the type * to cursor
  558. * @head: the head for your list.
  559. * @member: the name of the list_head within the struct.
  560. */
  561. #define list_entry_is_head(pos, head, member) \
  562. (&pos->member == (head))
  563. /**
  564. * list_for_each_entry - iterate over list of given type
  565. * @pos: the type * to use as a loop cursor.
  566. * @head: the head for your list.
  567. * @member: the name of the list_head within the struct.
  568. */
  569. #define list_for_each_entry(pos, head, member) \
  570. for (pos = list_first_entry(head, typeof(*pos), member); \
  571. !list_entry_is_head(pos, head, member); \
  572. pos = list_next_entry(pos, member))
  573. /**
  574. * list_for_each_entry_reverse - iterate backwards over list of given type.
  575. * @pos: the type * to use as a loop cursor.
  576. * @head: the head for your list.
  577. * @member: the name of the list_head within the struct.
  578. */
  579. #define list_for_each_entry_reverse(pos, head, member) \
  580. for (pos = list_last_entry(head, typeof(*pos), member); \
  581. !list_entry_is_head(pos, head, member); \
  582. pos = list_prev_entry(pos, member))
  583. /**
  584. * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
  585. * @pos: the type * to use as a start point
  586. * @head: the head of the list
  587. * @member: the name of the list_head within the struct.
  588. *
  589. * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
  590. */
  591. #define list_prepare_entry(pos, head, member) \
  592. ((pos) ? : list_entry(head, typeof(*pos), member))
  593. /**
  594. * list_for_each_entry_continue - continue iteration over list of given type
  595. * @pos: the type * to use as a loop cursor.
  596. * @head: the head for your list.
  597. * @member: the name of the list_head within the struct.
  598. *
  599. * Continue to iterate over list of given type, continuing after
  600. * the current position.
  601. */
  602. #define list_for_each_entry_continue(pos, head, member) \
  603. for (pos = list_next_entry(pos, member); \
  604. !list_entry_is_head(pos, head, member); \
  605. pos = list_next_entry(pos, member))
  606. /**
  607. * list_for_each_entry_continue_reverse - iterate backwards from the given point
  608. * @pos: the type * to use as a loop cursor.
  609. * @head: the head for your list.
  610. * @member: the name of the list_head within the struct.
  611. *
  612. * Start to iterate over list of given type backwards, continuing after
  613. * the current position.
  614. */
  615. #define list_for_each_entry_continue_reverse(pos, head, member) \
  616. for (pos = list_prev_entry(pos, member); \
  617. !list_entry_is_head(pos, head, member); \
  618. pos = list_prev_entry(pos, member))
  619. /**
  620. * list_for_each_entry_from - iterate over list of given type from the current point
  621. * @pos: the type * to use as a loop cursor.
  622. * @head: the head for your list.
  623. * @member: the name of the list_head within the struct.
  624. *
  625. * Iterate over list of given type, continuing from current position.
  626. */
  627. #define list_for_each_entry_from(pos, head, member) \
  628. for (; !list_entry_is_head(pos, head, member); \
  629. pos = list_next_entry(pos, member))
  630. /**
  631. * list_for_each_entry_from_reverse - iterate backwards over list of given type
  632. * from the current point
  633. * @pos: the type * to use as a loop cursor.
  634. * @head: the head for your list.
  635. * @member: the name of the list_head within the struct.
  636. *
  637. * Iterate backwards over list of given type, continuing from current position.
  638. */
  639. #define list_for_each_entry_from_reverse(pos, head, member) \
  640. for (; !list_entry_is_head(pos, head, member); \
  641. pos = list_prev_entry(pos, member))
  642. /**
  643. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  644. * @pos: the type * to use as a loop cursor.
  645. * @n: another type * to use as temporary storage
  646. * @head: the head for your list.
  647. * @member: the name of the list_head within the struct.
  648. */
  649. #define list_for_each_entry_safe(pos, n, head, member) \
  650. for (pos = list_first_entry(head, typeof(*pos), member), \
  651. n = list_next_entry(pos, member); \
  652. !list_entry_is_head(pos, head, member); \
  653. pos = n, n = list_next_entry(n, member))
  654. /**
  655. * list_for_each_entry_safe_continue - continue list iteration safe against removal
  656. * @pos: the type * to use as a loop cursor.
  657. * @n: another type * to use as temporary storage
  658. * @head: the head for your list.
  659. * @member: the name of the list_head within the struct.
  660. *
  661. * Iterate over list of given type, continuing after current point,
  662. * safe against removal of list entry.
  663. */
  664. #define list_for_each_entry_safe_continue(pos, n, head, member) \
  665. for (pos = list_next_entry(pos, member), \
  666. n = list_next_entry(pos, member); \
  667. !list_entry_is_head(pos, head, member); \
  668. pos = n, n = list_next_entry(n, member))
  669. /**
  670. * list_for_each_entry_safe_from - iterate over list from current point safe against removal
  671. * @pos: the type * to use as a loop cursor.
  672. * @n: another type * to use as temporary storage
  673. * @head: the head for your list.
  674. * @member: the name of the list_head within the struct.
  675. *
  676. * Iterate over list of given type from current point, safe against
  677. * removal of list entry.
  678. */
  679. #define list_for_each_entry_safe_from(pos, n, head, member) \
  680. for (n = list_next_entry(pos, member); \
  681. !list_entry_is_head(pos, head, member); \
  682. pos = n, n = list_next_entry(n, member))
  683. /**
  684. * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
  685. * @pos: the type * to use as a loop cursor.
  686. * @n: another type * to use as temporary storage
  687. * @head: the head for your list.
  688. * @member: the name of the list_head within the struct.
  689. *
  690. * Iterate backwards over list of given type, safe against removal
  691. * of list entry.
  692. */
  693. #define list_for_each_entry_safe_reverse(pos, n, head, member) \
  694. for (pos = list_last_entry(head, typeof(*pos), member), \
  695. n = list_prev_entry(pos, member); \
  696. !list_entry_is_head(pos, head, member); \
  697. pos = n, n = list_prev_entry(n, member))
  698. /**
  699. * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
  700. * @pos: the loop cursor used in the list_for_each_entry_safe loop
  701. * @n: temporary storage used in list_for_each_entry_safe
  702. * @member: the name of the list_head within the struct.
  703. *
  704. * list_safe_reset_next is not safe to use in general if the list may be
  705. * modified concurrently (eg. the lock is dropped in the loop body). An
  706. * exception to this is if the cursor element (pos) is pinned in the list,
  707. * and list_safe_reset_next is called after re-taking the lock and before
  708. * completing the current iteration of the loop body.
  709. */
  710. #define list_safe_reset_next(pos, n, member) \
  711. n = list_next_entry(pos, member)
  712. /*
  713. * Double linked lists with a single pointer list head.
  714. * Mostly useful for hash tables where the two pointer list head is
  715. * too wasteful.
  716. * You lose the ability to access the tail in O(1).
  717. */
  718. #define HLIST_HEAD_INIT { .first = NULL }
  719. #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
  720. #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
  721. static inline void INIT_HLIST_NODE(struct hlist_node *h)
  722. {
  723. h->next = NULL;
  724. h->pprev = NULL;
  725. }
  726. /**
  727. * hlist_unhashed - Has node been removed from list and reinitialized?
  728. * @h: Node to be checked
  729. *
  730. * Not that not all removal functions will leave a node in unhashed
  731. * state. For example, hlist_nulls_del_init_rcu() does leave the
  732. * node in unhashed state, but hlist_nulls_del() does not.
  733. */
  734. static inline int hlist_unhashed(const struct hlist_node *h)
  735. {
  736. return !h->pprev;
  737. }
  738. /**
  739. * hlist_unhashed_lockless - Version of hlist_unhashed for lockless use
  740. * @h: Node to be checked
  741. *
  742. * This variant of hlist_unhashed() must be used in lockless contexts
  743. * to avoid potential load-tearing. The READ_ONCE() is paired with the
  744. * various WRITE_ONCE() in hlist helpers that are defined below.
  745. */
  746. static inline int hlist_unhashed_lockless(const struct hlist_node *h)
  747. {
  748. return !READ_ONCE(h->pprev);
  749. }
  750. /**
  751. * hlist_empty - Is the specified hlist_head structure an empty hlist?
  752. * @h: Structure to check.
  753. */
  754. static inline int hlist_empty(const struct hlist_head *h)
  755. {
  756. return !READ_ONCE(h->first);
  757. }
  758. static inline void __hlist_del(struct hlist_node *n)
  759. {
  760. struct hlist_node *next = n->next;
  761. struct hlist_node **pprev = n->pprev;
  762. WRITE_ONCE(*pprev, next);
  763. if (next)
  764. WRITE_ONCE(next->pprev, pprev);
  765. }
  766. /**
  767. * hlist_del - Delete the specified hlist_node from its list
  768. * @n: Node to delete.
  769. *
  770. * Note that this function leaves the node in hashed state. Use
  771. * hlist_del_init() or similar instead to unhash @n.
  772. */
  773. static inline void hlist_del(struct hlist_node *n)
  774. {
  775. __hlist_del(n);
  776. n->next = LIST_POISON1;
  777. n->pprev = LIST_POISON2;
  778. }
  779. /**
  780. * hlist_del_init - Delete the specified hlist_node from its list and initialize
  781. * @n: Node to delete.
  782. *
  783. * Note that this function leaves the node in unhashed state.
  784. */
  785. static inline void hlist_del_init(struct hlist_node *n)
  786. {
  787. if (!hlist_unhashed(n)) {
  788. __hlist_del(n);
  789. INIT_HLIST_NODE(n);
  790. }
  791. }
  792. /**
  793. * hlist_add_head - add a new entry at the beginning of the hlist
  794. * @n: new entry to be added
  795. * @h: hlist head to add it after
  796. *
  797. * Insert a new entry after the specified head.
  798. * This is good for implementing stacks.
  799. */
  800. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  801. {
  802. struct hlist_node *first = h->first;
  803. WRITE_ONCE(n->next, first);
  804. if (first)
  805. WRITE_ONCE(first->pprev, &n->next);
  806. WRITE_ONCE(h->first, n);
  807. WRITE_ONCE(n->pprev, &h->first);
  808. }
  809. /**
  810. * hlist_add_before - add a new entry before the one specified
  811. * @n: new entry to be added
  812. * @next: hlist node to add it before, which must be non-NULL
  813. */
  814. static inline void hlist_add_before(struct hlist_node *n,
  815. struct hlist_node *next)
  816. {
  817. WRITE_ONCE(n->pprev, next->pprev);
  818. WRITE_ONCE(n->next, next);
  819. WRITE_ONCE(next->pprev, &n->next);
  820. WRITE_ONCE(*(n->pprev), n);
  821. }
  822. /**
  823. * hlist_add_behing - add a new entry after the one specified
  824. * @n: new entry to be added
  825. * @prev: hlist node to add it after, which must be non-NULL
  826. */
  827. static inline void hlist_add_behind(struct hlist_node *n,
  828. struct hlist_node *prev)
  829. {
  830. WRITE_ONCE(n->next, prev->next);
  831. WRITE_ONCE(prev->next, n);
  832. WRITE_ONCE(n->pprev, &prev->next);
  833. if (n->next)
  834. WRITE_ONCE(n->next->pprev, &n->next);
  835. }
  836. /**
  837. * hlist_add_fake - create a fake hlist consisting of a single headless node
  838. * @n: Node to make a fake list out of
  839. *
  840. * This makes @n appear to be its own predecessor on a headless hlist.
  841. * The point of this is to allow things like hlist_del() to work correctly
  842. * in cases where there is no list.
  843. */
  844. static inline void hlist_add_fake(struct hlist_node *n)
  845. {
  846. n->pprev = &n->next;
  847. }
  848. /**
  849. * hlist_fake: Is this node a fake hlist?
  850. * @h: Node to check for being a self-referential fake hlist.
  851. */
  852. static inline bool hlist_fake(struct hlist_node *h)
  853. {
  854. return h->pprev == &h->next;
  855. }
  856. /**
  857. * hlist_is_singular_node - is node the only element of the specified hlist?
  858. * @n: Node to check for singularity.
  859. * @h: Header for potentially singular list.
  860. *
  861. * Check whether the node is the only node of the head without
  862. * accessing head, thus avoiding unnecessary cache misses.
  863. */
  864. static inline bool
  865. hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
  866. {
  867. return !n->next && n->pprev == &h->first;
  868. }
  869. /**
  870. * hlist_move_list - Move an hlist
  871. * @old: hlist_head for old list.
  872. * @new: hlist_head for new list.
  873. *
  874. * Move a list from one list head to another. Fixup the pprev
  875. * reference of the first entry if it exists.
  876. */
  877. static inline void hlist_move_list(struct hlist_head *old,
  878. struct hlist_head *new)
  879. {
  880. new->first = old->first;
  881. if (new->first)
  882. new->first->pprev = &new->first;
  883. old->first = NULL;
  884. }
  885. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  886. #define hlist_for_each(pos, head) \
  887. for (pos = (head)->first; pos ; pos = pos->next)
  888. #define hlist_for_each_safe(pos, n, head) \
  889. for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
  890. pos = n)
  891. #define hlist_entry_safe(ptr, type, member) \
  892. ({ typeof(ptr) ____ptr = (ptr); \
  893. ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  894. })
  895. /**
  896. * hlist_for_each_entry - iterate over list of given type
  897. * @pos: the type * to use as a loop cursor.
  898. * @head: the head for your list.
  899. * @member: the name of the hlist_node within the struct.
  900. */
  901. #define hlist_for_each_entry(pos, head, member) \
  902. for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
  903. pos; \
  904. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  905. /**
  906. * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
  907. * @pos: the type * to use as a loop cursor.
  908. * @member: the name of the hlist_node within the struct.
  909. */
  910. #define hlist_for_each_entry_continue(pos, member) \
  911. for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
  912. pos; \
  913. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  914. /**
  915. * hlist_for_each_entry_from - iterate over a hlist continuing from current point
  916. * @pos: the type * to use as a loop cursor.
  917. * @member: the name of the hlist_node within the struct.
  918. */
  919. #define hlist_for_each_entry_from(pos, member) \
  920. for (; pos; \
  921. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  922. /**
  923. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  924. * @pos: the type * to use as a loop cursor.
  925. * @n: a &struct hlist_node to use as temporary storage
  926. * @head: the head for your list.
  927. * @member: the name of the hlist_node within the struct.
  928. */
  929. #define hlist_for_each_entry_safe(pos, n, head, member) \
  930. for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
  931. pos && ({ n = pos->member.next; 1; }); \
  932. pos = hlist_entry_safe(n, typeof(*pos), member))
  933. #endif