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