Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Lock-less NULL terminated single linked list
0004  *
0005  * The basic atomic operation of this list is cmpxchg on long.  On
0006  * architectures that don't have NMI-safe cmpxchg implementation, the
0007  * list can NOT be used in NMI handlers.  So code that uses the list in
0008  * an NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
0009  *
0010  * Copyright 2010,2011 Intel Corp.
0011  *   Author: Huang Ying <ying.huang@intel.com>
0012  */
0013 #include <linux/kernel.h>
0014 #include <linux/export.h>
0015 #include <linux/llist.h>
0016 
0017 
0018 /**
0019  * llist_add_batch - add several linked entries in batch
0020  * @new_first:  first entry in batch to be added
0021  * @new_last:   last entry in batch to be added
0022  * @head:   the head for your lock-less list
0023  *
0024  * Return whether list is empty before adding.
0025  */
0026 bool llist_add_batch(struct llist_node *new_first, struct llist_node *new_last,
0027              struct llist_head *head)
0028 {
0029     struct llist_node *first;
0030 
0031     do {
0032         new_last->next = first = READ_ONCE(head->first);
0033     } while (cmpxchg(&head->first, first, new_first) != first);
0034 
0035     return !first;
0036 }
0037 EXPORT_SYMBOL_GPL(llist_add_batch);
0038 
0039 /**
0040  * llist_del_first - delete the first entry of lock-less list
0041  * @head:   the head for your lock-less list
0042  *
0043  * If list is empty, return NULL, otherwise, return the first entry
0044  * deleted, this is the newest added one.
0045  *
0046  * Only one llist_del_first user can be used simultaneously with
0047  * multiple llist_add users without lock.  Because otherwise
0048  * llist_del_first, llist_add, llist_add (or llist_del_all, llist_add,
0049  * llist_add) sequence in another user may change @head->first->next,
0050  * but keep @head->first.  If multiple consumers are needed, please
0051  * use llist_del_all or use lock between consumers.
0052  */
0053 struct llist_node *llist_del_first(struct llist_head *head)
0054 {
0055     struct llist_node *entry, *old_entry, *next;
0056 
0057     entry = smp_load_acquire(&head->first);
0058     for (;;) {
0059         if (entry == NULL)
0060             return NULL;
0061         old_entry = entry;
0062         next = READ_ONCE(entry->next);
0063         entry = cmpxchg(&head->first, old_entry, next);
0064         if (entry == old_entry)
0065             break;
0066     }
0067 
0068     return entry;
0069 }
0070 EXPORT_SYMBOL_GPL(llist_del_first);
0071 
0072 /**
0073  * llist_reverse_order - reverse order of a llist chain
0074  * @head:   first item of the list to be reversed
0075  *
0076  * Reverse the order of a chain of llist entries and return the
0077  * new first entry.
0078  */
0079 struct llist_node *llist_reverse_order(struct llist_node *head)
0080 {
0081     struct llist_node *new_head = NULL;
0082 
0083     while (head) {
0084         struct llist_node *tmp = head;
0085         head = head->next;
0086         tmp->next = new_head;
0087         new_head = tmp;
0088     }
0089 
0090     return new_head;
0091 }
0092 EXPORT_SYMBOL_GPL(llist_reverse_order);