18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci#ifndef LLIST_H
38c2ecf20Sopenharmony_ci#define LLIST_H
48c2ecf20Sopenharmony_ci/*
58c2ecf20Sopenharmony_ci * Lock-less NULL terminated single linked list
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Cases where locking is not needed:
88c2ecf20Sopenharmony_ci * If there are multiple producers and multiple consumers, llist_add can be
98c2ecf20Sopenharmony_ci * used in producers and llist_del_all can be used in consumers simultaneously
108c2ecf20Sopenharmony_ci * without locking. Also a single consumer can use llist_del_first while
118c2ecf20Sopenharmony_ci * multiple producers simultaneously use llist_add, without any locking.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Cases where locking is needed:
148c2ecf20Sopenharmony_ci * If we have multiple consumers with llist_del_first used in one consumer, and
158c2ecf20Sopenharmony_ci * llist_del_first or llist_del_all used in other consumers, then a lock is
168c2ecf20Sopenharmony_ci * needed.  This is because llist_del_first depends on list->first->next not
178c2ecf20Sopenharmony_ci * changing, but without lock protection, there's no way to be sure about that
188c2ecf20Sopenharmony_ci * if a preemption happens in the middle of the delete operation and on being
198c2ecf20Sopenharmony_ci * preempted back, the list->first is the same as before causing the cmpxchg in
208c2ecf20Sopenharmony_ci * llist_del_first to succeed. For example, while a llist_del_first operation
218c2ecf20Sopenharmony_ci * is in progress in one consumer, then a llist_del_first, llist_add,
228c2ecf20Sopenharmony_ci * llist_add (or llist_del_all, llist_add, llist_add) sequence in another
238c2ecf20Sopenharmony_ci * consumer may cause violations.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * This can be summarized as follows:
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci *           |   add    | del_first |  del_all
288c2ecf20Sopenharmony_ci * add       |    -     |     -     |     -
298c2ecf20Sopenharmony_ci * del_first |          |     L     |     L
308c2ecf20Sopenharmony_ci * del_all   |          |           |     -
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Where, a particular row's operation can happen concurrently with a column's
338c2ecf20Sopenharmony_ci * operation, with "-" being no lock needed, while "L" being lock is needed.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * The list entries deleted via llist_del_all can be traversed with
368c2ecf20Sopenharmony_ci * traversing function such as llist_for_each etc.  But the list
378c2ecf20Sopenharmony_ci * entries can not be traversed safely before deleted from the list.
388c2ecf20Sopenharmony_ci * The order of deleted entries is from the newest to the oldest added
398c2ecf20Sopenharmony_ci * one.  If you want to traverse from the oldest to the newest, you
408c2ecf20Sopenharmony_ci * must reverse the order by yourself before traversing.
418c2ecf20Sopenharmony_ci *
428c2ecf20Sopenharmony_ci * The basic atomic operation of this list is cmpxchg on long.  On
438c2ecf20Sopenharmony_ci * architectures that don't have NMI-safe cmpxchg implementation, the
448c2ecf20Sopenharmony_ci * list can NOT be used in NMI handlers.  So code that uses the list in
458c2ecf20Sopenharmony_ci * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * Copyright 2010,2011 Intel Corp.
488c2ecf20Sopenharmony_ci *   Author: Huang Ying <ying.huang@intel.com>
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#include <linux/atomic.h>
528c2ecf20Sopenharmony_ci#include <linux/kernel.h>
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistruct llist_head {
558c2ecf20Sopenharmony_ci	struct llist_node *first;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistruct llist_node {
598c2ecf20Sopenharmony_ci	struct llist_node *next;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define LLIST_HEAD_INIT(name)	{ NULL }
638c2ecf20Sopenharmony_ci#define LLIST_HEAD(name)	struct llist_head name = LLIST_HEAD_INIT(name)
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci/**
668c2ecf20Sopenharmony_ci * init_llist_head - initialize lock-less list head
678c2ecf20Sopenharmony_ci * @head:	the head for your lock-less list
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistatic inline void init_llist_head(struct llist_head *list)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	list->first = NULL;
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/**
758c2ecf20Sopenharmony_ci * llist_entry - get the struct of this entry
768c2ecf20Sopenharmony_ci * @ptr:	the &struct llist_node pointer.
778c2ecf20Sopenharmony_ci * @type:	the type of the struct this is embedded in.
788c2ecf20Sopenharmony_ci * @member:	the name of the llist_node within the struct.
798c2ecf20Sopenharmony_ci */
808c2ecf20Sopenharmony_ci#define llist_entry(ptr, type, member)		\
818c2ecf20Sopenharmony_ci	container_of(ptr, type, member)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/**
848c2ecf20Sopenharmony_ci * member_address_is_nonnull - check whether the member address is not NULL
858c2ecf20Sopenharmony_ci * @ptr:	the object pointer (struct type * that contains the llist_node)
868c2ecf20Sopenharmony_ci * @member:	the name of the llist_node within the struct.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * This macro is conceptually the same as
898c2ecf20Sopenharmony_ci *	&ptr->member != NULL
908c2ecf20Sopenharmony_ci * but it works around the fact that compilers can decide that taking a member
918c2ecf20Sopenharmony_ci * address is never a NULL pointer.
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * Real objects that start at a high address and have a member at NULL are
948c2ecf20Sopenharmony_ci * unlikely to exist, but such pointers may be returned e.g. by the
958c2ecf20Sopenharmony_ci * container_of() macro.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ci#define member_address_is_nonnull(ptr, member)	\
988c2ecf20Sopenharmony_ci	((uintptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0)
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/**
1018c2ecf20Sopenharmony_ci * llist_for_each - iterate over some deleted entries of a lock-less list
1028c2ecf20Sopenharmony_ci * @pos:	the &struct llist_node to use as a loop cursor
1038c2ecf20Sopenharmony_ci * @node:	the first entry of deleted list entries
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * In general, some entries of the lock-less list can be traversed
1068c2ecf20Sopenharmony_ci * safely only after being deleted from list, so start with an entry
1078c2ecf20Sopenharmony_ci * instead of list head.
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * If being used on entries deleted from lock-less list directly, the
1108c2ecf20Sopenharmony_ci * traverse order is from the newest to the oldest added entry.  If
1118c2ecf20Sopenharmony_ci * you want to traverse from the oldest to the newest, you must
1128c2ecf20Sopenharmony_ci * reverse the order by yourself before traversing.
1138c2ecf20Sopenharmony_ci */
1148c2ecf20Sopenharmony_ci#define llist_for_each(pos, node)			\
1158c2ecf20Sopenharmony_ci	for ((pos) = (node); pos; (pos) = (pos)->next)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/**
1188c2ecf20Sopenharmony_ci * llist_for_each_safe - iterate over some deleted entries of a lock-less list
1198c2ecf20Sopenharmony_ci *			 safe against removal of list entry
1208c2ecf20Sopenharmony_ci * @pos:	the &struct llist_node to use as a loop cursor
1218c2ecf20Sopenharmony_ci * @n:		another &struct llist_node to use as temporary storage
1228c2ecf20Sopenharmony_ci * @node:	the first entry of deleted list entries
1238c2ecf20Sopenharmony_ci *
1248c2ecf20Sopenharmony_ci * In general, some entries of the lock-less list can be traversed
1258c2ecf20Sopenharmony_ci * safely only after being deleted from list, so start with an entry
1268c2ecf20Sopenharmony_ci * instead of list head.
1278c2ecf20Sopenharmony_ci *
1288c2ecf20Sopenharmony_ci * If being used on entries deleted from lock-less list directly, the
1298c2ecf20Sopenharmony_ci * traverse order is from the newest to the oldest added entry.  If
1308c2ecf20Sopenharmony_ci * you want to traverse from the oldest to the newest, you must
1318c2ecf20Sopenharmony_ci * reverse the order by yourself before traversing.
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ci#define llist_for_each_safe(pos, n, node)			\
1348c2ecf20Sopenharmony_ci	for ((pos) = (node); (pos) && ((n) = (pos)->next, true); (pos) = (n))
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * llist_for_each_entry - iterate over some deleted entries of lock-less list of given type
1388c2ecf20Sopenharmony_ci * @pos:	the type * to use as a loop cursor.
1398c2ecf20Sopenharmony_ci * @node:	the fist entry of deleted list entries.
1408c2ecf20Sopenharmony_ci * @member:	the name of the llist_node with the struct.
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * In general, some entries of the lock-less list can be traversed
1438c2ecf20Sopenharmony_ci * safely only after being removed from list, so start with an entry
1448c2ecf20Sopenharmony_ci * instead of list head.
1458c2ecf20Sopenharmony_ci *
1468c2ecf20Sopenharmony_ci * If being used on entries deleted from lock-less list directly, the
1478c2ecf20Sopenharmony_ci * traverse order is from the newest to the oldest added entry.  If
1488c2ecf20Sopenharmony_ci * you want to traverse from the oldest to the newest, you must
1498c2ecf20Sopenharmony_ci * reverse the order by yourself before traversing.
1508c2ecf20Sopenharmony_ci */
1518c2ecf20Sopenharmony_ci#define llist_for_each_entry(pos, node, member)				\
1528c2ecf20Sopenharmony_ci	for ((pos) = llist_entry((node), typeof(*(pos)), member);	\
1538c2ecf20Sopenharmony_ci	     member_address_is_nonnull(pos, member);			\
1548c2ecf20Sopenharmony_ci	     (pos) = llist_entry((pos)->member.next, typeof(*(pos)), member))
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/**
1578c2ecf20Sopenharmony_ci * llist_for_each_entry_safe - iterate over some deleted entries of lock-less list of given type
1588c2ecf20Sopenharmony_ci *			       safe against removal of list entry
1598c2ecf20Sopenharmony_ci * @pos:	the type * to use as a loop cursor.
1608c2ecf20Sopenharmony_ci * @n:		another type * to use as temporary storage
1618c2ecf20Sopenharmony_ci * @node:	the first entry of deleted list entries.
1628c2ecf20Sopenharmony_ci * @member:	the name of the llist_node with the struct.
1638c2ecf20Sopenharmony_ci *
1648c2ecf20Sopenharmony_ci * In general, some entries of the lock-less list can be traversed
1658c2ecf20Sopenharmony_ci * safely only after being removed from list, so start with an entry
1668c2ecf20Sopenharmony_ci * instead of list head.
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * If being used on entries deleted from lock-less list directly, the
1698c2ecf20Sopenharmony_ci * traverse order is from the newest to the oldest added entry.  If
1708c2ecf20Sopenharmony_ci * you want to traverse from the oldest to the newest, you must
1718c2ecf20Sopenharmony_ci * reverse the order by yourself before traversing.
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_ci#define llist_for_each_entry_safe(pos, n, node, member)			       \
1748c2ecf20Sopenharmony_ci	for (pos = llist_entry((node), typeof(*pos), member);		       \
1758c2ecf20Sopenharmony_ci	     member_address_is_nonnull(pos, member) &&			       \
1768c2ecf20Sopenharmony_ci	        (n = llist_entry(pos->member.next, typeof(*n), member), true); \
1778c2ecf20Sopenharmony_ci	     pos = n)
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/**
1808c2ecf20Sopenharmony_ci * llist_empty - tests whether a lock-less list is empty
1818c2ecf20Sopenharmony_ci * @head:	the list to test
1828c2ecf20Sopenharmony_ci *
1838c2ecf20Sopenharmony_ci * Not guaranteed to be accurate or up to date.  Just a quick way to
1848c2ecf20Sopenharmony_ci * test whether the list is empty without deleting something from the
1858c2ecf20Sopenharmony_ci * list.
1868c2ecf20Sopenharmony_ci */
1878c2ecf20Sopenharmony_cistatic inline bool llist_empty(const struct llist_head *head)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	return READ_ONCE(head->first) == NULL;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_cistatic inline struct llist_node *llist_next(struct llist_node *node)
1938c2ecf20Sopenharmony_ci{
1948c2ecf20Sopenharmony_ci	return node->next;
1958c2ecf20Sopenharmony_ci}
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciextern bool llist_add_batch(struct llist_node *new_first,
1988c2ecf20Sopenharmony_ci			    struct llist_node *new_last,
1998c2ecf20Sopenharmony_ci			    struct llist_head *head);
2008c2ecf20Sopenharmony_ci/**
2018c2ecf20Sopenharmony_ci * llist_add - add a new entry
2028c2ecf20Sopenharmony_ci * @new:	new entry to be added
2038c2ecf20Sopenharmony_ci * @head:	the head for your lock-less list
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * Returns true if the list was empty prior to adding this entry.
2068c2ecf20Sopenharmony_ci */
2078c2ecf20Sopenharmony_cistatic inline bool llist_add(struct llist_node *new, struct llist_head *head)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	return llist_add_batch(new, new, head);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci/**
2138c2ecf20Sopenharmony_ci * llist_del_all - delete all entries from lock-less list
2148c2ecf20Sopenharmony_ci * @head:	the head of lock-less list to delete all entries
2158c2ecf20Sopenharmony_ci *
2168c2ecf20Sopenharmony_ci * If list is empty, return NULL, otherwise, delete all entries and
2178c2ecf20Sopenharmony_ci * return the pointer to the first entry.  The order of entries
2188c2ecf20Sopenharmony_ci * deleted is from the newest to the oldest added one.
2198c2ecf20Sopenharmony_ci */
2208c2ecf20Sopenharmony_cistatic inline struct llist_node *llist_del_all(struct llist_head *head)
2218c2ecf20Sopenharmony_ci{
2228c2ecf20Sopenharmony_ci	return xchg(&head->first, NULL);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ciextern struct llist_node *llist_del_first(struct llist_head *head);
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistruct llist_node *llist_reverse_order(struct llist_node *head);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci#endif /* LLIST_H */
230