* [PATCH -v6 0/3] Lockless memory allocator and list
@ 2010-11-29 7:03 Huang Ying
2010-11-29 7:03 ` [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless Huang Ying
2010-11-29 7:03 ` [PATCH -v6 3/3] lib, Add lock-less NULL terminated single list Huang Ying
0 siblings, 2 replies; 5+ messages in thread
From: Huang Ying @ 2010-11-29 7:03 UTC (permalink / raw)
To: Len Brown
Cc: linux-kernel, Andi Kleen, ying.huang, linux-acpi, Peter Zijlstra,
Andrew Morton, Linus Torvalds, Ingo Molnar
This patchset adds a lockless memory allocator and a lock-less
list.
v6:
- Revise ARCH_HAVE_NMI_SAFE_CMPXCHG definition for some architectures
according to architecture maitainers' comments.
v5:
- Add ARCH_HAVE_NMI_SAFE_CMPXCHG
- Add spin_trylock_irqsave based fallback in lockless memory allocator
if ARCH_HAVE_NMI_SAFE_CMPXCHG=n
- Make lockless list depends on ARCH_HAVE_NMI_SAFE_CMPXCHG
v4:
- Split from APEI patchset
- Update patch description and comments according to ML comments
v3:
- Rework lockless memory allocator and list according to ML comments
[PATCH -v6 1/3] Add Kconfig option ARCH_HAVE_NMI_SAFE_CMPXCHG
[PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless
[PATCH -v6 3/3] lib, Add lock-less NULL terminated single list
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless
2010-11-29 7:03 [PATCH -v6 0/3] Lockless memory allocator and list Huang Ying
@ 2010-11-29 7:03 ` Huang Ying
2010-11-29 12:11 ` Peter Zijlstra
2010-11-29 7:03 ` [PATCH -v6 3/3] lib, Add lock-less NULL terminated single list Huang Ying
1 sibling, 1 reply; 5+ messages in thread
From: Huang Ying @ 2010-11-29 7:03 UTC (permalink / raw)
To: Len Brown
Cc: linux-kernel, Andi Kleen, ying.huang, linux-acpi, Peter Zijlstra,
Andrew Morton, Linus Torvalds, Ingo Molnar
This version of the gen_pool memory allocator supports lockless
operation.
This makes it safe to use in NMI handlers and other special
unblockable contexts that could otherwise deadlock on locks. This is
implemented by using atomic operations and retries on any conflicts.
The disadvantage is that there may be livelocks in extreme cases. For
better scalability, one gen_pool allocator can be used for each CPU.
The lockless operation only works if there is enough memory available.
If new memory is added to the pool a lock has to be still taken. So
any user relying on locklessness has to ensure that sufficient memory
is preallocated.
The basic atomic operation of this allocator is cmpxchg on long. On
architectures that don't have NMI-safe cmpxchg implementation, a
spin_trylock_irqsave based fallback is used for gen_pool_alloc, so it
can be used in NMI handler safely. But gen_pool_free can not be used
in NMI handler in these architectures, because memory free can not
fail.
Signed-off-by: Huang Ying <ying.huang@intel.com>
Reviewed-by: Andi Kleen <ak@linux.intel.com>
---
include/linux/bitmap.h | 1
include/linux/genalloc.h | 48 ++++++-
lib/bitmap.c | 2
lib/genalloc.c | 309 ++++++++++++++++++++++++++++++++++++++++-------
4 files changed, 312 insertions(+), 48 deletions(-)
--- a/include/linux/bitmap.h
+++ b/include/linux/bitmap.h
@@ -142,6 +142,7 @@ extern void bitmap_release_region(unsign
extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
extern void bitmap_copy_le(void *dst, const unsigned long *src, int nbits);
+#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
#define BITMAP_LAST_WORD_MASK(nbits) \
( \
((nbits) % BITS_PER_LONG) ? \
--- a/include/linux/genalloc.h
+++ b/include/linux/genalloc.h
@@ -1,8 +1,29 @@
+#ifndef GENALLOC_H
+#define GENALLOC_H
/*
- * Basic general purpose allocator for managing special purpose memory
- * not managed by the regular kmalloc/kfree interface.
- * Uses for this includes on-device special memory, uncached memory
- * etc.
+ * Basic general purpose allocator for managing special purpose
+ * memory, for example, memory that is not managed by the regular
+ * kmalloc/kfree interface. Uses for this includes on-device special
+ * memory, uncached memory etc.
+ *
+ * It is safe to use the allocator in NMI handlers and other special
+ * unblockable contexts that could otherwise deadlock on locks. This
+ * is implemented by using atomic operations and retries on any
+ * conflicts. The disadvantage is that there may be livelocks in
+ * extreme cases. For better scalability, one allocator can be used
+ * for each CPU.
+ *
+ * The lockless operation only works if there is enough memory
+ * available. If new memory is added to the pool a lock has to be
+ * still taken. So any user relying on locklessness has to ensure
+ * that sufficient memory is preallocated.
+ *
+ * The basic atomic operation of this allocator is cmpxchg on long.
+ * On architectures that don't have NMI-safe cmpxchg implementation, a
+ * spin_trylock_irqsave based fallback is used for gen_pool_alloc, so
+ * it can be used in NMI handler safely. But gen_pool_free can not be
+ * used in NMI handler in these architectures, because memory free can
+ * not fail.
*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
@@ -13,7 +34,7 @@
* General purpose special memory pool descriptor.
*/
struct gen_pool {
- rwlock_t lock;
+ spinlock_t lock;
struct list_head chunks; /* list of chunks in this pool */
int min_alloc_order; /* minimum allocation order */
};
@@ -22,15 +43,32 @@ struct gen_pool {
* General purpose special memory pool chunk descriptor.
*/
struct gen_pool_chunk {
+#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
spinlock_t lock;
+#endif
struct list_head next_chunk; /* next chunk in pool */
+ atomic_t avail;
unsigned long start_addr; /* starting address of memory chunk */
unsigned long end_addr; /* ending address of memory chunk */
unsigned long bits[0]; /* bitmap for allocating memory chunk */
};
+/**
+ * gen_pool_for_each_chunk - iterate over chunks of generic memory pool
+ * @chunk: the struct gen_pool_chunk * to use as a loop cursor
+ * @pool: the generic memory pool
+ *
+ * Not lockless, proper mutual exclusion is needed to use this macro
+ * with other gen_pool function simultaneously.
+ */
+#define gen_pool_for_each_chunk(chunk, pool) \
+ list_for_each_entry_rcu(chunk, &(pool)->chunks, next_chunk)
+
extern struct gen_pool *gen_pool_create(int, int);
extern int gen_pool_add(struct gen_pool *, unsigned long, size_t, int);
extern void gen_pool_destroy(struct gen_pool *);
extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
+extern size_t gen_pool_avail(struct gen_pool *);
+extern size_t gen_pool_size(struct gen_pool *);
+#endif /* GENALLOC_H */
--- a/lib/bitmap.c
+++ b/lib/bitmap.c
@@ -271,8 +271,6 @@ int __bitmap_weight(const unsigned long
}
EXPORT_SYMBOL(__bitmap_weight);
-#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
-
void bitmap_set(unsigned long *map, int start, int nr)
{
unsigned long *p = map + BIT_WORD(start);
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -1,8 +1,27 @@
/*
- * Basic general purpose allocator for managing special purpose memory
- * not managed by the regular kmalloc/kfree interface.
- * Uses for this includes on-device special memory, uncached memory
- * etc.
+ * Basic general purpose allocator for managing special purpose
+ * memory, for example, memory that is not managed by the regular
+ * kmalloc/kfree interface. Uses for this includes on-device special
+ * memory, uncached memory etc.
+ *
+ * It is safe to use the allocator in NMI handlers and other special
+ * unblockable contexts that could otherwise deadlock on locks. This
+ * is implemented by using atomic operations and retries on any
+ * conflicts. The disadvantage is that there may be livelocks in
+ * extreme cases. For better scalability, one allocator can be used
+ * for each CPU.
+ *
+ * The lockless operation only works if there is enough memory
+ * available. If new memory is added to the pool a lock has to be
+ * still taken. So any user relying on locklessness has to ensure
+ * that sufficient memory is preallocated.
+ *
+ * The basic atomic operation of this allocator is cmpxchg on long.
+ * On architectures that don't have NMI-safe cmpxchg implementation, a
+ * spin_trylock_irqsave based fallback is used for gen_pool_alloc, so
+ * it can be used in NMI handler safely. But gen_pool_free can not be
+ * used in NMI handler in these architectures, because memory free can
+ * not fail.
*
* Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
*
@@ -13,8 +32,109 @@
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/bitmap.h>
+#include <linux/rculist.h>
+#include <linux/interrupt.h>
#include <linux/genalloc.h>
+#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
+static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
+{
+ unsigned long val, nval;
+
+ nval = *addr;
+ do {
+ val = nval;
+ if (val & mask_to_set)
+ return -EBUSY;
+ } while ((nval = cmpxchg(addr, val, val | mask_to_set)) != val);
+
+ return 0;
+}
+
+static int clear_bits_ll(unsigned long *addr, unsigned long mask_to_clear)
+{
+ unsigned long val, nval;
+
+ nval = *addr;
+ do {
+ val = nval;
+ if ((val & mask_to_clear) != mask_to_clear)
+ return -EBUSY;
+ } while ((nval = cmpxchg(addr, val, val & ~mask_to_clear)) != val);
+
+ return 0;
+}
+
+/*
+ * bitmap_set_ll - set the specified number of bits at the specified position
+ * @map: pointer to a bitmap
+ * @start: a bit position in @map
+ * @nr: number of bits to set
+ *
+ * Set @nr bits start from @start in @map lock-lessly. Several users
+ * can set/clear the same bitmap simultaneously without lock. If two
+ * users set the same bit, one user will return remain bits, otherwise
+ * return 0.
+ */
+static int bitmap_set_ll(unsigned long *map, int start, int nr)
+{
+ unsigned long *p = map + BIT_WORD(start);
+ const int size = start + nr;
+ int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
+ unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
+
+ while (nr - bits_to_set >= 0) {
+ if (set_bits_ll(p, mask_to_set))
+ return nr;
+ nr -= bits_to_set;
+ bits_to_set = BITS_PER_LONG;
+ mask_to_set = ~0UL;
+ p++;
+ }
+ if (nr) {
+ mask_to_set &= BITMAP_LAST_WORD_MASK(size);
+ if (set_bits_ll(p, mask_to_set))
+ return nr;
+ }
+
+ return 0;
+}
+
+/*
+ * bitmap_clear_ll - clear the specified number of bits at the specified position
+ * @map: pointer to a bitmap
+ * @start: a bit position in @map
+ * @nr: number of bits to set
+ *
+ * Clear @nr bits start from @start in @map lock-lessly. Several users
+ * can set/clear the same bitmap simultaneously without lock. If two
+ * users clear the same bit, one user will return remain bits,
+ * otherwise return 0.
+ */
+static int bitmap_clear_ll(unsigned long *map, int start, int nr)
+{
+ unsigned long *p = map + BIT_WORD(start);
+ const int size = start + nr;
+ int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
+ unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
+
+ while (nr - bits_to_clear >= 0) {
+ if (clear_bits_ll(p, mask_to_clear))
+ return nr;
+ nr -= bits_to_clear;
+ bits_to_clear = BITS_PER_LONG;
+ mask_to_clear = ~0UL;
+ p++;
+ }
+ if (nr) {
+ mask_to_clear &= BITMAP_LAST_WORD_MASK(size);
+ if (clear_bits_ll(p, mask_to_clear))
+ return nr;
+ }
+
+ return 0;
+}
+#endif
/**
* gen_pool_create - create a new special memory pool
@@ -30,7 +150,7 @@ struct gen_pool *gen_pool_create(int min
pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
if (pool != NULL) {
- rwlock_init(&pool->lock);
+ spin_lock_init(&pool->lock);
INIT_LIST_HEAD(&pool->chunks);
pool->min_alloc_order = min_alloc_order;
}
@@ -58,15 +178,18 @@ int gen_pool_add(struct gen_pool *pool,
chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid);
if (unlikely(chunk == NULL))
- return -1;
+ return -ENOMEM;
+#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
spin_lock_init(&chunk->lock);
+#endif
chunk->start_addr = addr;
chunk->end_addr = addr + size;
+ atomic_set(&chunk->avail, size);
- write_lock(&pool->lock);
- list_add(&chunk->next_chunk, &pool->chunks);
- write_unlock(&pool->lock);
+ spin_lock(&pool->lock);
+ list_add_rcu(&chunk->next_chunk, &pool->chunks);
+ spin_unlock(&pool->lock);
return 0;
}
@@ -102,6 +225,71 @@ void gen_pool_destroy(struct gen_pool *p
}
EXPORT_SYMBOL(gen_pool_destroy);
+#ifdef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
+#define gen_pool_chunk_trylock(chunk, flags) 1
+#define gen_pool_chunk_lock(chunk, flags)
+#define gen_pool_chunk_unlock(chunk, flags)
+
+static int gen_pool_chunk_bitmap_set(struct gen_pool_chunk *chunk,
+ int start, int nr)
+{
+ int remain, cremain;
+
+ remain = bitmap_set_ll(chunk->bits, start, nr);
+ if (remain) {
+ if (nr - remain) {
+ cremain = bitmap_clear_ll(chunk->bits,
+ start, nr - remain);
+ BUG_ON(cremain);
+ }
+ }
+
+ return remain;
+}
+
+static int gen_pool_chunk_bitmap_clear(struct gen_pool_chunk *chunk,
+ int start, int nr)
+{
+ return bitmap_clear_ll(chunk->bits, start, nr);
+}
+#else
+static int gen_pool_chunk_trylock(struct gen_pool_chunk *chunk,
+ unsigned long *flags)
+{
+ if (in_nmi() && !spin_trylock_irqsave(&chunk->lock, *flags))
+ return 0;
+ else
+ spin_lock_irqsave(&chunk->lock, *flags);
+ return 1;
+}
+
+static inline void gen_pool_chunk_lock(struct gen_pool_chunk *chunk,
+ unsigned long *flags)
+{
+ spin_lock_irqsave(&chunk->lock, *flags);
+}
+
+static inline void gen_pool_chunk_unlock(struct gen_pool_chunk *chunk,
+ unsigned long *flags)
+{
+ spin_unlock_irqrestore(&chunk->lock, *flags);
+}
+
+static inline int gen_pool_chunk_bitmap_set(struct gen_pool_chunk *chunk,
+ int start, int nr)
+{
+ bitmap_set(chunk->bits, start, nr);
+ return 0;
+}
+
+static inline int gen_pool_chunk_bitmap_clear(struct gen_pool_chunk *chunk,
+ int start, int nr)
+{
+ bitmap_clear(chunk->bits, start, nr);
+ return 0;
+}
+#endif
+
/**
* gen_pool_alloc - allocate special memory from the pool
* @pool: pool to allocate from
@@ -112,39 +300,40 @@ EXPORT_SYMBOL(gen_pool_destroy);
*/
unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
{
- struct list_head *_chunk;
struct gen_pool_chunk *chunk;
unsigned long addr, flags;
int order = pool->min_alloc_order;
- int nbits, start_bit, end_bit;
+ int nbits, start_bit = 0, end_bit, remain;
if (size == 0)
return 0;
+ flags = 0;
nbits = (size + (1UL << order) - 1) >> order;
-
- read_lock(&pool->lock);
- list_for_each(_chunk, &pool->chunks) {
- chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
+ list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
+ if (size > atomic_read(&chunk->avail))
+ continue;
end_bit = (chunk->end_addr - chunk->start_addr) >> order;
-
- spin_lock_irqsave(&chunk->lock, flags);
- start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit, 0,
- nbits, 0);
+ if (!gen_pool_chunk_trylock(chunk, &flags))
+ continue;
+retry:
+ start_bit = bitmap_find_next_zero_area(chunk->bits, end_bit,
+ start_bit, nbits, 0);
if (start_bit >= end_bit) {
- spin_unlock_irqrestore(&chunk->lock, flags);
+ gen_pool_chunk_unlock(chunk, &flags);
continue;
}
+ remain = gen_pool_chunk_bitmap_set(chunk, start_bit, nbits);
+ if (remain)
+ goto retry;
+ gen_pool_chunk_unlock(chunk, &flags);
addr = chunk->start_addr + ((unsigned long)start_bit << order);
-
- bitmap_set(chunk->bits, start_bit, nbits);
- spin_unlock_irqrestore(&chunk->lock, flags);
- read_unlock(&pool->lock);
+ size = nbits << order;
+ atomic_sub(size, &chunk->avail);
return addr;
}
- read_unlock(&pool->lock);
return 0;
}
EXPORT_SYMBOL(gen_pool_alloc);
@@ -155,33 +344,71 @@ EXPORT_SYMBOL(gen_pool_alloc);
* @addr: starting address of memory to free back to pool
* @size: size in bytes of memory to free
*
- * Free previously allocated special memory back to the specified pool.
+ * Free previously allocated special memory back to the specified
+ * pool. Can not be used in NMI handler on architectures without
+ * NMI-safe cmpxchg implementation.
*/
void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
{
- struct list_head *_chunk;
struct gen_pool_chunk *chunk;
unsigned long flags;
int order = pool->min_alloc_order;
- int bit, nbits;
-
- nbits = (size + (1UL << order) - 1) >> order;
+ int start_bit, nbits, remain;
- read_lock(&pool->lock);
- list_for_each(_chunk, &pool->chunks) {
- chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
+#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG
+ BUG_ON(in_nmi());
+#endif
+ flags = 0;
+ nbits = (size + (1UL << order) - 1) >> order;
+ list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
if (addr >= chunk->start_addr && addr < chunk->end_addr) {
BUG_ON(addr + size > chunk->end_addr);
- spin_lock_irqsave(&chunk->lock, flags);
- bit = (addr - chunk->start_addr) >> order;
- while (nbits--)
- __clear_bit(bit++, chunk->bits);
- spin_unlock_irqrestore(&chunk->lock, flags);
- break;
+ start_bit = (addr - chunk->start_addr) >> order;
+ gen_pool_chunk_lock(chunk, &flags);
+ remain = gen_pool_chunk_bitmap_clear(chunk, start_bit,
+ nbits);
+ gen_pool_chunk_unlock(chunk, &flags);
+ BUG_ON(remain);
+ size = nbits << order;
+ atomic_add(size, &chunk->avail);
+ return;
}
}
- BUG_ON(nbits > 0);
- read_unlock(&pool->lock);
+ BUG();
}
EXPORT_SYMBOL(gen_pool_free);
+
+/**
+ * gen_pool_avail - get available free space of the pool
+ * @pool: pool to get available free space
+ *
+ * Return available free space of the specified pool.
+ */
+size_t gen_pool_avail(struct gen_pool *pool)
+{
+ struct gen_pool_chunk *chunk;
+ size_t avail = 0;
+
+ list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
+ avail += atomic_read(&chunk->avail);
+ return avail;
+}
+EXPORT_SYMBOL_GPL(gen_pool_avail);
+
+/**
+ * gen_pool_size - get size in bytes of memory managed by the pool
+ * @pool: pool to get size
+ *
+ * Return size in bytes of memory managed by the pool.
+ */
+size_t gen_pool_size(struct gen_pool *pool)
+{
+ struct gen_pool_chunk *chunk;
+ size_t size = 0;
+
+ list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
+ size += chunk->end_addr - chunk->start_addr;
+ return size;
+}
+EXPORT_SYMBOL_GPL(gen_pool_size);
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH -v6 3/3] lib, Add lock-less NULL terminated single list
2010-11-29 7:03 [PATCH -v6 0/3] Lockless memory allocator and list Huang Ying
2010-11-29 7:03 ` [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless Huang Ying
@ 2010-11-29 7:03 ` Huang Ying
1 sibling, 0 replies; 5+ messages in thread
From: Huang Ying @ 2010-11-29 7:03 UTC (permalink / raw)
To: Len Brown
Cc: linux-kernel, Andi Kleen, ying.huang, linux-acpi, Peter Zijlstra,
Andrew Morton, Linus Torvalds, Ingo Molnar
Cmpxchg is used to implement adding new entry to the list, deleting
all entries from the list, deleting first entry of the list and some
other operations.
Because this is a single list, so the tail can not be accessed in O(1).
This can be used in NMI handler.
If there are multiple producers and multiple consumers, llist_add can
be used in producers and llist_del_all can be used in consumers. They
can work simultaneously without lock. But llist_del_first can not be
used here. Because llist_del_first depends on list->first->next does
not changed if list->first is not changed during its operation, but
llist_del_first, llist_add, llist_add sequence in another consumer may
violate that.
If there are multiple producers and one consumer, llist_add can be
used in producers and llist_del_all or llist_del_first can be used in
the consumer.
The list entries deleted via llist_del_all can be traversed with
traversing function such as llist_for_each etc. But the list entries
can not be traversed safely before deleted from the list without
proper synchronization with the list consumers.
Signed-off-by: Huang Ying <ying.huang@intel.com>
Reviewed-by: Andi Kleen <ak@linux.intel.com>
---
include/linux/llist.h | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++
lib/Kconfig | 4 ++
lib/Makefile | 2 +
lib/llist.c | 80 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 178 insertions(+)
create mode 100644 include/linux/llist.h
create mode 100644 lib/llist.c
--- /dev/null
+++ b/include/linux/llist.h
@@ -0,0 +1,92 @@
+#ifndef LLIST_H
+#define LLIST_H
+/*
+ * Lock-less NULL terminated single linked list
+ *
+ * If there are multiple producers and multiple consumers, llist_add
+ * can be used in producers and llist_del_all can be used in
+ * consumers. They can work simultaneously without lock. But
+ * llist_del_first can not be used here. Because llist_del_first
+ * depends on list->first->next does not changed if list->first is not
+ * changed during its operation, but llist_del_first, llist_add,
+ * llist_add sequence in another consumer may violate that.
+ *
+ * If there are multiple producers and one consumer, llist_add can be
+ * used in producers and llist_del_all or llist_del_first can be used
+ * in the consumer.
+ *
+ * The list entries deleted via llist_del_all can be traversed with
+ * traversing function such as llist_for_each etc. But the list
+ * entries can not be traversed safely before deleted from the list
+ * without proper synchronization with the list consumers.
+ */
+
+struct llist_head {
+ struct llist_node *first;
+};
+
+struct llist_node {
+ struct llist_node *next;
+};
+
+#define LLIST_HEAD_INIT(name) { NULL }
+#define LLIST_HEAD(name) struct llist_head name = LLIST_HEAD_INIT(name)
+
+/**
+ * init_llist_head - initialize lock-less list head
+ * @head: the head for your lock-less list
+ */
+static inline void init_llist_head(struct llist_head *list)
+{
+ list->first = NULL;
+}
+
+/**
+ * llist_entry - get the struct of this entry
+ * @ptr: the &struct llist_node pointer.
+ * @type: the type of the struct this is embedded in.
+ * @member: the name of the llist_node within the struct.
+ */
+#define llist_entry(ptr, type, member) \
+ container_of(ptr, type, member)
+
+/**
+ * llist_for_each - iterate over some entries of a lock-less list
+ * @pos: the &struct llist_node to use as a loop cursor
+ * @node: the first entry of deleted list entries
+ *
+ * In general, some entries of the lock-less list can be traversed
+ * safely only after being deleted from list, so start with an entry
+ * instead of list head.
+ */
+#define llist_for_each(pos, node) \
+ for (pos = (node); pos; pos = pos->next)
+
+/**
+ * llist_for_each_entry - iterate over some entries of lock-less list of given type
+ * @pos: the type * to use as a loop cursor.
+ * @node: the fist entry of deleted list entries.
+ * @member: the name of the llist_node with the struct.
+ *
+ * In general, some entries of the lock-less list can be traversed
+ * safely only after being removed from list, so start with an entry
+ * instead of list head.
+ */
+#define llist_for_each_entry(pos, node, member) \
+ for (pos = llist_entry((node), typeof(*pos), member); \
+ &pos->member != NULL; \
+ pos = llist_entry(pos->member.next, typeof(*pos), member))
+
+/**
+ * llist_empty - tests whether a lock-less list is empty
+ * @head: the list to test
+ */
+static inline int llist_empty(const struct llist_head *head)
+{
+ return head->first == NULL;
+}
+
+void llist_add(struct llist_node *new, struct llist_head *head);
+struct llist_node *llist_del_first(struct llist_head *head);
+struct llist_node *llist_del_all(struct llist_head *head);
+#endif /* LLIST_H */
--- a/lib/Kconfig
+++ b/lib/Kconfig
@@ -210,4 +210,8 @@ config GENERIC_ATOMIC64
config LRU_CACHE
tristate
+config LLIST
+ bool
+ depends on ARCH_HAVE_NMI_SAFE_CMPXCHG
+
endmenu
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -106,6 +106,8 @@ obj-$(CONFIG_GENERIC_ATOMIC64) += atomic
obj-$(CONFIG_ATOMIC64_SELFTEST) += atomic64_test.o
+obj-$(CONFIG_LLIST) += llist.o
+
hostprogs-y := gen_crc32table
clean-files := crc32table.h
--- /dev/null
+++ b/lib/llist.c
@@ -0,0 +1,80 @@
+/*
+ * Lock-less NULL terminated single linked list
+ *
+ * Copyright 2010 Intel Corp.
+ * Author: Huang Ying <ying.huang@intel.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 as published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/llist.h>
+#include <linux/errno.h>
+
+#include <asm/system.h>
+
+/**
+ * llist_add - add a new entry
+ * @new: new entry to be added
+ * @head: the head for your lock-less list
+ */
+void llist_add(struct llist_node *new, struct llist_head *head)
+{
+ struct llist_node *entry;
+
+ do {
+ entry = head->first;
+ new->next = entry;
+ } while (cmpxchg(&head->first, entry, new) != entry);
+}
+EXPORT_SYMBOL_GPL(llist_add);
+
+/**
+ * llist_del_first - delete the first entry of lock-less list
+ * @head: the head for your lock-less list
+ *
+ * If list is empty, return NULL, otherwise, return the first entry deleted.
+ *
+ * Only one llist_del_first user can be used simultaneously with
+ * multiple llist_add users without lock. Because otherwise
+ * llist_del_first, llist_add, llist_add sequence in another user may
+ * change @head->first->next, but keep @head->first. If multiple
+ * consumers are needed, please use llist_del_all.
+ */
+struct llist_node *llist_del_first(struct llist_head *head)
+{
+ struct llist_node *entry;
+
+ do {
+ entry = head->first;
+ if (entry == NULL)
+ return NULL;
+ } while (cmpxchg(&head->first, entry, entry->next) != entry);
+
+ return entry;
+}
+EXPORT_SYMBOL_GPL(llist_del_first);
+
+/**
+ * llist_del_all - delete all entries from lock-less list
+ * @head: the head of lock-less list to delete all entries
+ *
+ * If list is empty, return NULL, otherwise, delete all entries and
+ * return the pointer to the first entry.
+ */
+struct llist_node *llist_del_all(struct llist_head *head)
+{
+ return xchg(&head->first, NULL);
+}
+EXPORT_SYMBOL_GPL(llist_del_all);
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless
2010-11-29 7:03 ` [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless Huang Ying
@ 2010-11-29 12:11 ` Peter Zijlstra
2010-11-30 1:08 ` Huang Ying
0 siblings, 1 reply; 5+ messages in thread
From: Peter Zijlstra @ 2010-11-29 12:11 UTC (permalink / raw)
To: Huang Ying
Cc: Len Brown, linux-kernel, Andi Kleen, linux-acpi, Andrew Morton,
Linus Torvalds, Ingo Molnar
On Mon, 2010-11-29 at 15:03 +0800, Huang Ying wrote:
> This version of the gen_pool memory allocator supports lockless
> operation.
>
> This makes it safe to use in NMI handlers and other special
> unblockable contexts that could otherwise deadlock on locks. This is
> implemented by using atomic operations and retries on any conflicts.
> The disadvantage is that there may be livelocks in extreme cases. For
> better scalability, one gen_pool allocator can be used for each CPU.
>
> The lockless operation only works if there is enough memory available.
> If new memory is added to the pool a lock has to be still taken. So
> any user relying on locklessness has to ensure that sufficient memory
> is preallocated.
>
> The basic atomic operation of this allocator is cmpxchg on long. On
> architectures that don't have NMI-safe cmpxchg implementation, a
> spin_trylock_irqsave based fallback is used for gen_pool_alloc, so it
> can be used in NMI handler safely. But gen_pool_free can not be used
> in NMI handler in these architectures, because memory free can not
> fail.
I still don't see a reason to merge this.
It makes the genalloc thing slower for every other user (more LOCK'ed
ops) and there is no new user presented in this series.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless
2010-11-29 12:11 ` Peter Zijlstra
@ 2010-11-30 1:08 ` Huang Ying
0 siblings, 0 replies; 5+ messages in thread
From: Huang Ying @ 2010-11-30 1:08 UTC (permalink / raw)
To: Peter Zijlstra
Cc: Len Brown, linux-kernel@vger.kernel.org, Andi Kleen,
linux-acpi@vger.kernel.org, Andrew Morton, Linus Torvalds,
Ingo Molnar
Hi, Peter,
On Mon, 2010-11-29 at 20:11 +0800, Peter Zijlstra wrote:
> On Mon, 2010-11-29 at 15:03 +0800, Huang Ying wrote:
> > This version of the gen_pool memory allocator supports lockless
> > operation.
> >
> > This makes it safe to use in NMI handlers and other special
> > unblockable contexts that could otherwise deadlock on locks. This is
> > implemented by using atomic operations and retries on any conflicts.
> > The disadvantage is that there may be livelocks in extreme cases. For
> > better scalability, one gen_pool allocator can be used for each CPU.
> >
> > The lockless operation only works if there is enough memory available.
> > If new memory is added to the pool a lock has to be still taken. So
> > any user relying on locklessness has to ensure that sufficient memory
> > is preallocated.
> >
> > The basic atomic operation of this allocator is cmpxchg on long. On
> > architectures that don't have NMI-safe cmpxchg implementation, a
> > spin_trylock_irqsave based fallback is used for gen_pool_alloc, so it
> > can be used in NMI handler safely. But gen_pool_free can not be used
> > in NMI handler in these architectures, because memory free can not
> > fail.
>
> I still don't see a reason to merge this.
>
> It makes the genalloc thing slower for every other user (more LOCK'ed
> ops) and there is no new user presented in this series.
As far as I know, all genalloc users are not performance sensitive.
After all, it is mainly used to manage some device memory before. As for
users, I think this is a "chicken and egg" problem. And I have plan to
use it in APEI code.
Best Regards,
Huang Ying
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-11-30 1:08 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-29 7:03 [PATCH -v6 0/3] Lockless memory allocator and list Huang Ying
2010-11-29 7:03 ` [PATCH -v6 2/3] lib, Make gen_pool memory allocator lockless Huang Ying
2010-11-29 12:11 ` Peter Zijlstra
2010-11-30 1:08 ` Huang Ying
2010-11-29 7:03 ` [PATCH -v6 3/3] lib, Add lock-less NULL terminated single list Huang Ying
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).