Skip to content

Commit 7f18427

Browse files
yhuang-intellenb
authored andcommitted
lib, Make gen_pool memory allocator lockless
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, the allocator can NOT be used in NMI handler. So code uses the allocator in NMI handler should depend on CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG. Signed-off-by: Huang Ying <ying.huang@intel.com> Reviewed-by: Andi Kleen <ak@linux.intel.com> Reviewed-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent f49f23a commit 7f18427

4 files changed

Lines changed: 272 additions & 65 deletions

File tree

‎include/linux/bitmap.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ extern void bitmap_release_region(unsigned long *bitmap, int pos, int order);
145145
extern int bitmap_allocate_region(unsigned long *bitmap, int pos, int order);
146146
extern void bitmap_copy_le(void *dst, const unsigned long *src, int nbits);
147147

148+
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
148149
#define BITMAP_LAST_WORD_MASK(nbits) \
149150
( \
150151
((nbits) % BITS_PER_LONG) ? \

‎include/linux/genalloc.h‎

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
/*
2-
* Basic general purpose allocator for managing special purpose memory
3-
* not managed by the regular kmalloc/kfree interface.
4-
* Uses for this includes on-device special memory, uncached memory
5-
* etc.
2+
* Basic general purpose allocator for managing special purpose
3+
* memory, for example, memory that is not managed by the regular
4+
* kmalloc/kfree interface. Uses for this includes on-device special
5+
* memory, uncached memory etc.
6+
*
7+
* It is safe to use the allocator in NMI handlers and other special
8+
* unblockable contexts that could otherwise deadlock on locks. This
9+
* is implemented by using atomic operations and retries on any
10+
* conflicts. The disadvantage is that there may be livelocks in
11+
* extreme cases. For better scalability, one allocator can be used
12+
* for each CPU.
13+
*
14+
* The lockless operation only works if there is enough memory
15+
* available. If new memory is added to the pool a lock has to be
16+
* still taken. So any user relying on locklessness has to ensure
17+
* that sufficient memory is preallocated.
18+
*
19+
* The basic atomic operation of this allocator is cmpxchg on long.
20+
* On architectures that don't have NMI-safe cmpxchg implementation,
21+
* the allocator can NOT be used in NMI handler. So code uses the
22+
* allocator in NMI handler should depend on
23+
* CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG.
624
*
725
* This source code is licensed under the GNU General Public License,
826
* Version 2. See the file COPYING for more details.
@@ -15,7 +33,7 @@
1533
* General purpose special memory pool descriptor.
1634
*/
1735
struct gen_pool {
18-
rwlock_t lock;
36+
spinlock_t lock;
1937
struct list_head chunks; /* list of chunks in this pool */
2038
int min_alloc_order; /* minimum allocation order */
2139
};
@@ -24,8 +42,8 @@ struct gen_pool {
2442
* General purpose special memory pool chunk descriptor.
2543
*/
2644
struct gen_pool_chunk {
27-
spinlock_t lock;
2845
struct list_head next_chunk; /* next chunk in pool */
46+
atomic_t avail;
2947
phys_addr_t phys_addr; /* physical starting address of memory chunk */
3048
unsigned long start_addr; /* starting address of memory chunk */
3149
unsigned long end_addr; /* ending address of memory chunk */
@@ -56,4 +74,8 @@ static inline int gen_pool_add(struct gen_pool *pool, unsigned long addr,
5674
extern void gen_pool_destroy(struct gen_pool *);
5775
extern unsigned long gen_pool_alloc(struct gen_pool *, size_t);
5876
extern void gen_pool_free(struct gen_pool *, unsigned long, size_t);
77+
extern void gen_pool_for_each_chunk(struct gen_pool *,
78+
void (*)(struct gen_pool *, struct gen_pool_chunk *, void *), void *);
79+
extern size_t gen_pool_avail(struct gen_pool *);
80+
extern size_t gen_pool_size(struct gen_pool *);
5981
#endif /* __GENALLOC_H__ */

‎lib/bitmap.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,6 @@ int __bitmap_weight(const unsigned long *bitmap, int bits)
271271
}
272272
EXPORT_SYMBOL(__bitmap_weight);
273273

274-
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
275-
276274
void bitmap_set(unsigned long *map, int start, int nr)
277275
{
278276
unsigned long *p = map + BIT_WORD(start);

0 commit comments

Comments
 (0)