xref: /openbmc/linux/lib/llist.c (revision 6a6d7602)
145051539SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f49f23abSHuang Ying /*
3f49f23abSHuang Ying  * Lock-less NULL terminated single linked list
4f49f23abSHuang Ying  *
5f49f23abSHuang Ying  * The basic atomic operation of this list is cmpxchg on long.  On
6f49f23abSHuang Ying  * architectures that don't have NMI-safe cmpxchg implementation, the
72c30245cSIngo Molnar  * list can NOT be used in NMI handlers.  So code that uses the list in
82c30245cSIngo Molnar  * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
9f49f23abSHuang Ying  *
10f49f23abSHuang Ying  * Copyright 2010,2011 Intel Corp.
11f49f23abSHuang Ying  *   Author: Huang Ying <ying.huang@intel.com>
12f49f23abSHuang Ying  */
13f49f23abSHuang Ying #include <linux/kernel.h>
148bc3bcc9SPaul Gortmaker #include <linux/export.h>
15f49f23abSHuang Ying #include <linux/llist.h>
16f49f23abSHuang Ying 
17f49f23abSHuang Ying 
18f49f23abSHuang Ying /**
19f49f23abSHuang Ying  * llist_add_batch - add several linked entries in batch
20f49f23abSHuang Ying  * @new_first:	first entry in batch to be added
21f49f23abSHuang Ying  * @new_last:	last entry in batch to be added
22f49f23abSHuang Ying  * @head:	the head for your lock-less list
23781f7fd9SHuang Ying  *
24781f7fd9SHuang Ying  * Return whether list is empty before adding.
25f49f23abSHuang Ying  */
llist_add_batch(struct llist_node * new_first,struct llist_node * new_last,struct llist_head * head)26781f7fd9SHuang Ying bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
27f49f23abSHuang Ying 		     struct llist_head *head)
28f49f23abSHuang Ying {
29*6a6d7602SUros Bizjak 	struct llist_node *first = READ_ONCE(head->first);
30f49f23abSHuang Ying 
31fb4214dbSOleg Nesterov 	do {
32*6a6d7602SUros Bizjak 		new_last->next = first;
334f1d2a03SUros Bizjak 	} while (!try_cmpxchg(&head->first, &first, new_first));
34781f7fd9SHuang Ying 
35fb4214dbSOleg Nesterov 	return !first;
36f49f23abSHuang Ying }
37f49f23abSHuang Ying EXPORT_SYMBOL_GPL(llist_add_batch);
38f49f23abSHuang Ying 
39f49f23abSHuang Ying /**
40f49f23abSHuang Ying  * llist_del_first - delete the first entry of lock-less list
41f49f23abSHuang Ying  * @head:	the head for your lock-less list
42f49f23abSHuang Ying  *
43f49f23abSHuang Ying  * If list is empty, return NULL, otherwise, return the first entry
44f49f23abSHuang Ying  * deleted, this is the newest added one.
45f49f23abSHuang Ying  *
46f49f23abSHuang Ying  * Only one llist_del_first user can be used simultaneously with
47f49f23abSHuang Ying  * multiple llist_add users without lock.  Because otherwise
48f49f23abSHuang Ying  * llist_del_first, llist_add, llist_add (or llist_del_all, llist_add,
49f49f23abSHuang Ying  * llist_add) sequence in another user may change @head->first->next,
50f49f23abSHuang Ying  * but keep @head->first.  If multiple consumers are needed, please
51f49f23abSHuang Ying  * use llist_del_all or use lock between consumers.
52f49f23abSHuang Ying  */
llist_del_first(struct llist_head * head)53f49f23abSHuang Ying struct llist_node *llist_del_first(struct llist_head *head)
54f49f23abSHuang Ying {
554f1d2a03SUros Bizjak 	struct llist_node *entry, *next;
56f49f23abSHuang Ying 
572cf12f82SDmitry Vyukov 	entry = smp_load_acquire(&head->first);
584f1d2a03SUros Bizjak 	do {
59f49f23abSHuang Ying 		if (entry == NULL)
60f49f23abSHuang Ying 			return NULL;
612cf12f82SDmitry Vyukov 		next = READ_ONCE(entry->next);
624f1d2a03SUros Bizjak 	} while (!try_cmpxchg(&head->first, &entry, next));
63f49f23abSHuang Ying 
64f49f23abSHuang Ying 	return entry;
65f49f23abSHuang Ying }
66f49f23abSHuang Ying EXPORT_SYMBOL_GPL(llist_del_first);
67b89241e8SChristoph Hellwig 
68b89241e8SChristoph Hellwig /**
69b89241e8SChristoph Hellwig  * llist_reverse_order - reverse order of a llist chain
70b89241e8SChristoph Hellwig  * @head:	first item of the list to be reversed
71b89241e8SChristoph Hellwig  *
720791a605SAndrew Morton  * Reverse the order of a chain of llist entries and return the
73b89241e8SChristoph Hellwig  * new first entry.
74b89241e8SChristoph Hellwig  */
llist_reverse_order(struct llist_node * head)75b89241e8SChristoph Hellwig struct llist_node *llist_reverse_order(struct llist_node *head)
76b89241e8SChristoph Hellwig {
77b89241e8SChristoph Hellwig 	struct llist_node *new_head = NULL;
78b89241e8SChristoph Hellwig 
79b89241e8SChristoph Hellwig 	while (head) {
80b89241e8SChristoph Hellwig 		struct llist_node *tmp = head;
81b89241e8SChristoph Hellwig 		head = head->next;
82b89241e8SChristoph Hellwig 		tmp->next = new_head;
83b89241e8SChristoph Hellwig 		new_head = tmp;
84b89241e8SChristoph Hellwig 	}
85b89241e8SChristoph Hellwig 
86b89241e8SChristoph Hellwig 	return new_head;
87b89241e8SChristoph Hellwig }
88b89241e8SChristoph Hellwig EXPORT_SYMBOL_GPL(llist_reverse_order);
89