aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Kconfig.debug4
-rw-r--r--lib/Makefile5
-rw-r--r--lib/cpu_rmap.c6
-rw-r--r--lib/crc-t10dif.c11
-rw-r--r--lib/crc32.c17
-rw-r--r--lib/decompress_inflate.c2
-rw-r--r--lib/genalloc.c22
-rw-r--r--lib/lz4/lz4_decompress.c8
-rw-r--r--lib/percpu_ida.c335
-rw-r--r--lib/radix-tree.c41
-rw-r--r--lib/rbtree.c40
-rw-r--r--lib/rbtree_test.c12
12 files changed, 470 insertions, 33 deletions
diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug
index 652bea9054f0..06344d986eb9 100644
--- a/lib/Kconfig.debug
+++ b/lib/Kconfig.debug
@@ -597,7 +597,7 @@ endmenu # "Memory Debugging"
597 597
598config DEBUG_SHIRQ 598config DEBUG_SHIRQ
599 bool "Debug shared IRQ handlers" 599 bool "Debug shared IRQ handlers"
600 depends on DEBUG_KERNEL && GENERIC_HARDIRQS 600 depends on DEBUG_KERNEL
601 help 601 help
602 Enable this to generate a spurious interrupt as soon as a shared 602 Enable this to generate a spurious interrupt as soon as a shared
603 interrupt handler is registered, and just before one is deregistered. 603 interrupt handler is registered, and just before one is deregistered.
@@ -1461,7 +1461,7 @@ config BACKTRACE_SELF_TEST
1461 1461
1462config RBTREE_TEST 1462config RBTREE_TEST
1463 tristate "Red-Black tree test" 1463 tristate "Red-Black tree test"
1464 depends on m && DEBUG_KERNEL 1464 depends on DEBUG_KERNEL
1465 help 1465 help
1466 A benchmark measuring the performance of the rbtree library. 1466 A benchmark measuring the performance of the rbtree library.
1467 Also includes rbtree invariant checks. 1467 Also includes rbtree invariant checks.
diff --git a/lib/Makefile b/lib/Makefile
index f2cb3082697c..f3bb2cb98adf 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -13,7 +13,7 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
13 sha1.o md5.o irq_regs.o reciprocal_div.o argv_split.o \ 13 sha1.o md5.o irq_regs.o reciprocal_div.o argv_split.o \
14 proportions.o flex_proportions.o prio_heap.o ratelimit.o show_mem.o \ 14 proportions.o flex_proportions.o prio_heap.o ratelimit.o show_mem.o \
15 is_single_threaded.o plist.o decompress.o kobject_uevent.o \ 15 is_single_threaded.o plist.o decompress.o kobject_uevent.o \
16 earlycpio.o percpu-refcount.o 16 earlycpio.o percpu-refcount.o percpu_ida.o
17 17
18obj-$(CONFIG_ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS) += usercopy.o 18obj-$(CONFIG_ARCH_HAS_DEBUG_STRICT_USER_COPY_CHECKS) += usercopy.o
19lib-$(CONFIG_MMU) += ioremap.o 19lib-$(CONFIG_MMU) += ioremap.o
@@ -25,7 +25,8 @@ obj-y += lockref.o
25obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \ 25obj-y += bcd.o div64.o sort.o parser.o halfmd4.o debug_locks.o random32.o \
26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \ 26 bust_spinlocks.o hexdump.o kasprintf.o bitmap.o scatterlist.o \
27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \ 27 gcd.o lcm.o list_sort.o uuid.o flex_array.o iovec.o clz_ctz.o \
28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o 28 bsearch.o find_last_bit.o find_next_bit.o llist.o memweight.o kfifo.o \
29 percpu_ida.o
29obj-y += string_helpers.o 30obj-y += string_helpers.o
30obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o 31obj-$(CONFIG_TEST_STRING_HELPERS) += test-string_helpers.o
31obj-y += kstrtox.o 32obj-y += kstrtox.o
diff --git a/lib/cpu_rmap.c b/lib/cpu_rmap.c
index 5fbed5caba6e..4f134d8907a7 100644
--- a/lib/cpu_rmap.c
+++ b/lib/cpu_rmap.c
@@ -8,9 +8,7 @@
8 */ 8 */
9 9
10#include <linux/cpu_rmap.h> 10#include <linux/cpu_rmap.h>
11#ifdef CONFIG_GENERIC_HARDIRQS
12#include <linux/interrupt.h> 11#include <linux/interrupt.h>
13#endif
14#include <linux/export.h> 12#include <linux/export.h>
15 13
16/* 14/*
@@ -213,8 +211,6 @@ int cpu_rmap_update(struct cpu_rmap *rmap, u16 index,
213} 211}
214EXPORT_SYMBOL(cpu_rmap_update); 212EXPORT_SYMBOL(cpu_rmap_update);
215 213
216#ifdef CONFIG_GENERIC_HARDIRQS
217
218/* Glue between IRQ affinity notifiers and CPU rmaps */ 214/* Glue between IRQ affinity notifiers and CPU rmaps */
219 215
220struct irq_glue { 216struct irq_glue {
@@ -309,5 +305,3 @@ int irq_cpu_rmap_add(struct cpu_rmap *rmap, int irq)
309 return rc; 305 return rc;
310} 306}
311EXPORT_SYMBOL(irq_cpu_rmap_add); 307EXPORT_SYMBOL(irq_cpu_rmap_add);
312
313#endif /* CONFIG_GENERIC_HARDIRQS */
diff --git a/lib/crc-t10dif.c b/lib/crc-t10dif.c
index 43bc5b071f96..dfe6ec17c0a5 100644
--- a/lib/crc-t10dif.c
+++ b/lib/crc-t10dif.c
@@ -14,8 +14,10 @@
14#include <linux/err.h> 14#include <linux/err.h>
15#include <linux/init.h> 15#include <linux/init.h>
16#include <crypto/hash.h> 16#include <crypto/hash.h>
17#include <linux/static_key.h>
17 18
18static struct crypto_shash *crct10dif_tfm; 19static struct crypto_shash *crct10dif_tfm;
20static struct static_key crct10dif_fallback __read_mostly;
19 21
20__u16 crc_t10dif(const unsigned char *buffer, size_t len) 22__u16 crc_t10dif(const unsigned char *buffer, size_t len)
21{ 23{
@@ -25,6 +27,9 @@ __u16 crc_t10dif(const unsigned char *buffer, size_t len)
25 } desc; 27 } desc;
26 int err; 28 int err;
27 29
30 if (static_key_false(&crct10dif_fallback))
31 return crc_t10dif_generic(0, buffer, len);
32
28 desc.shash.tfm = crct10dif_tfm; 33 desc.shash.tfm = crct10dif_tfm;
29 desc.shash.flags = 0; 34 desc.shash.flags = 0;
30 *(__u16 *)desc.ctx = 0; 35 *(__u16 *)desc.ctx = 0;
@@ -39,7 +44,11 @@ EXPORT_SYMBOL(crc_t10dif);
39static int __init crc_t10dif_mod_init(void) 44static int __init crc_t10dif_mod_init(void)
40{ 45{
41 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0); 46 crct10dif_tfm = crypto_alloc_shash("crct10dif", 0, 0);
42 return PTR_RET(crct10dif_tfm); 47 if (IS_ERR(crct10dif_tfm)) {
48 static_key_slow_inc(&crct10dif_fallback);
49 crct10dif_tfm = NULL;
50 }
51 return 0;
43} 52}
44 53
45static void __exit crc_t10dif_mod_fini(void) 54static void __exit crc_t10dif_mod_fini(void)
diff --git a/lib/crc32.c b/lib/crc32.c
index 072fbd8234d5..410093dbe51c 100644
--- a/lib/crc32.c
+++ b/lib/crc32.c
@@ -131,11 +131,14 @@ crc32_body(u32 crc, unsigned char const *buf, size_t len, const u32 (*tab)[256])
131#endif 131#endif
132 132
133/** 133/**
134 * crc32_le() - Calculate bitwise little-endian Ethernet AUTODIN II CRC32 134 * crc32_le_generic() - Calculate bitwise little-endian Ethernet AUTODIN II
135 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for 135 * CRC32/CRC32C
136 * other uses, or the previous crc32 value if computing incrementally. 136 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for other
137 * @p: pointer to buffer over which CRC is run 137 * uses, or the previous crc32/crc32c value if computing incrementally.
138 * @p: pointer to buffer over which CRC32/CRC32C is run
138 * @len: length of buffer @p 139 * @len: length of buffer @p
140 * @tab: little-endian Ethernet table
141 * @polynomial: CRC32/CRC32c LE polynomial
139 */ 142 */
140static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p, 143static inline u32 __pure crc32_le_generic(u32 crc, unsigned char const *p,
141 size_t len, const u32 (*tab)[256], 144 size_t len, const u32 (*tab)[256],
@@ -201,11 +204,13 @@ EXPORT_SYMBOL(crc32_le);
201EXPORT_SYMBOL(__crc32c_le); 204EXPORT_SYMBOL(__crc32c_le);
202 205
203/** 206/**
204 * crc32_be() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32 207 * crc32_be_generic() - Calculate bitwise big-endian Ethernet AUTODIN II CRC32
205 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for 208 * @crc: seed value for computation. ~0 for Ethernet, sometimes 0 for
206 * other uses, or the previous crc32 value if computing incrementally. 209 * other uses, or the previous crc32 value if computing incrementally.
207 * @p: pointer to buffer over which CRC is run 210 * @p: pointer to buffer over which CRC32 is run
208 * @len: length of buffer @p 211 * @len: length of buffer @p
212 * @tab: big-endian Ethernet table
213 * @polynomial: CRC32 BE polynomial
209 */ 214 */
210static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p, 215static inline u32 __pure crc32_be_generic(u32 crc, unsigned char const *p,
211 size_t len, const u32 (*tab)[256], 216 size_t len, const u32 (*tab)[256],
diff --git a/lib/decompress_inflate.c b/lib/decompress_inflate.c
index 19ff89e34eec..d619b28c456f 100644
--- a/lib/decompress_inflate.c
+++ b/lib/decompress_inflate.c
@@ -48,7 +48,7 @@ STATIC int INIT gunzip(unsigned char *buf, int len,
48 out_len = 0x8000; /* 32 K */ 48 out_len = 0x8000; /* 32 K */
49 out_buf = malloc(out_len); 49 out_buf = malloc(out_len);
50 } else { 50 } else {
51 out_len = 0x7fffffff; /* no limit */ 51 out_len = ((size_t)~0) - (size_t)out_buf; /* no limit */
52 } 52 }
53 if (!out_buf) { 53 if (!out_buf) {
54 error("Out of memory while allocating output buffer"); 54 error("Out of memory while allocating output buffer");
diff --git a/lib/genalloc.c b/lib/genalloc.c
index b35cfa9bc3d4..26cf20be72b7 100644
--- a/lib/genalloc.c
+++ b/lib/genalloc.c
@@ -37,6 +37,11 @@
37#include <linux/of_address.h> 37#include <linux/of_address.h>
38#include <linux/of_device.h> 38#include <linux/of_device.h>
39 39
40static inline size_t chunk_size(const struct gen_pool_chunk *chunk)
41{
42 return chunk->end_addr - chunk->start_addr + 1;
43}
44
40static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set) 45static int set_bits_ll(unsigned long *addr, unsigned long mask_to_set)
41{ 46{
42 unsigned long val, nval; 47 unsigned long val, nval;
@@ -182,13 +187,13 @@ int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phy
182 int nbytes = sizeof(struct gen_pool_chunk) + 187 int nbytes = sizeof(struct gen_pool_chunk) +
183 BITS_TO_LONGS(nbits) * sizeof(long); 188 BITS_TO_LONGS(nbits) * sizeof(long);
184 189
185 chunk = kmalloc_node(nbytes, GFP_KERNEL | __GFP_ZERO, nid); 190 chunk = kzalloc_node(nbytes, GFP_KERNEL, nid);
186 if (unlikely(chunk == NULL)) 191 if (unlikely(chunk == NULL))
187 return -ENOMEM; 192 return -ENOMEM;
188 193
189 chunk->phys_addr = phys; 194 chunk->phys_addr = phys;
190 chunk->start_addr = virt; 195 chunk->start_addr = virt;
191 chunk->end_addr = virt + size; 196 chunk->end_addr = virt + size - 1;
192 atomic_set(&chunk->avail, size); 197 atomic_set(&chunk->avail, size);
193 198
194 spin_lock(&pool->lock); 199 spin_lock(&pool->lock);
@@ -213,7 +218,7 @@ phys_addr_t gen_pool_virt_to_phys(struct gen_pool *pool, unsigned long addr)
213 218
214 rcu_read_lock(); 219 rcu_read_lock();
215 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { 220 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
216 if (addr >= chunk->start_addr && addr < chunk->end_addr) { 221 if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
217 paddr = chunk->phys_addr + (addr - chunk->start_addr); 222 paddr = chunk->phys_addr + (addr - chunk->start_addr);
218 break; 223 break;
219 } 224 }
@@ -242,7 +247,7 @@ void gen_pool_destroy(struct gen_pool *pool)
242 chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk); 247 chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
243 list_del(&chunk->next_chunk); 248 list_del(&chunk->next_chunk);
244 249
245 end_bit = (chunk->end_addr - chunk->start_addr) >> order; 250 end_bit = chunk_size(chunk) >> order;
246 bit = find_next_bit(chunk->bits, end_bit, 0); 251 bit = find_next_bit(chunk->bits, end_bit, 0);
247 BUG_ON(bit < end_bit); 252 BUG_ON(bit < end_bit);
248 253
@@ -283,7 +288,7 @@ unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
283 if (size > atomic_read(&chunk->avail)) 288 if (size > atomic_read(&chunk->avail))
284 continue; 289 continue;
285 290
286 end_bit = (chunk->end_addr - chunk->start_addr) >> order; 291 end_bit = chunk_size(chunk) >> order;
287retry: 292retry:
288 start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits, 293 start_bit = pool->algo(chunk->bits, end_bit, start_bit, nbits,
289 pool->data); 294 pool->data);
@@ -330,8 +335,8 @@ void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
330 nbits = (size + (1UL << order) - 1) >> order; 335 nbits = (size + (1UL << order) - 1) >> order;
331 rcu_read_lock(); 336 rcu_read_lock();
332 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) { 337 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) {
333 if (addr >= chunk->start_addr && addr < chunk->end_addr) { 338 if (addr >= chunk->start_addr && addr <= chunk->end_addr) {
334 BUG_ON(addr + size > chunk->end_addr); 339 BUG_ON(addr + size - 1 > chunk->end_addr);
335 start_bit = (addr - chunk->start_addr) >> order; 340 start_bit = (addr - chunk->start_addr) >> order;
336 remain = bitmap_clear_ll(chunk->bits, start_bit, nbits); 341 remain = bitmap_clear_ll(chunk->bits, start_bit, nbits);
337 BUG_ON(remain); 342 BUG_ON(remain);
@@ -400,7 +405,7 @@ size_t gen_pool_size(struct gen_pool *pool)
400 405
401 rcu_read_lock(); 406 rcu_read_lock();
402 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk) 407 list_for_each_entry_rcu(chunk, &pool->chunks, next_chunk)
403 size += chunk->end_addr - chunk->start_addr; 408 size += chunk_size(chunk);
404 rcu_read_unlock(); 409 rcu_read_unlock();
405 return size; 410 return size;
406} 411}
@@ -519,7 +524,6 @@ struct gen_pool *devm_gen_pool_create(struct device *dev, int min_alloc_order,
519/** 524/**
520 * dev_get_gen_pool - Obtain the gen_pool (if any) for a device 525 * dev_get_gen_pool - Obtain the gen_pool (if any) for a device
521 * @dev: device to retrieve the gen_pool from 526 * @dev: device to retrieve the gen_pool from
522 * @name: Optional name for the gen_pool, usually NULL
523 * 527 *
524 * Returns the gen_pool for the device if one is present, or NULL. 528 * Returns the gen_pool for the device if one is present, or NULL.
525 */ 529 */
diff --git a/lib/lz4/lz4_decompress.c b/lib/lz4/lz4_decompress.c
index 411be80ddb46..df6839e3ce08 100644
--- a/lib/lz4/lz4_decompress.c
+++ b/lib/lz4/lz4_decompress.c
@@ -283,8 +283,8 @@ _output_error:
283 return (int) (-(((char *) ip) - source)); 283 return (int) (-(((char *) ip) - source));
284} 284}
285 285
286int lz4_decompress(const char *src, size_t *src_len, char *dest, 286int lz4_decompress(const unsigned char *src, size_t *src_len,
287 size_t actual_dest_len) 287 unsigned char *dest, size_t actual_dest_len)
288{ 288{
289 int ret = -1; 289 int ret = -1;
290 int input_len = 0; 290 int input_len = 0;
@@ -302,8 +302,8 @@ exit_0:
302EXPORT_SYMBOL(lz4_decompress); 302EXPORT_SYMBOL(lz4_decompress);
303#endif 303#endif
304 304
305int lz4_decompress_unknownoutputsize(const char *src, size_t src_len, 305int lz4_decompress_unknownoutputsize(const unsigned char *src, size_t src_len,
306 char *dest, size_t *dest_len) 306 unsigned char *dest, size_t *dest_len)
307{ 307{
308 int ret = -1; 308 int ret = -1;
309 int out_len = 0; 309 int out_len = 0;
diff --git a/lib/percpu_ida.c b/lib/percpu_ida.c
new file mode 100644
index 000000000000..bab1ba2a4c71
--- /dev/null
+++ b/lib/percpu_ida.c
@@ -0,0 +1,335 @@
1/*
2 * Percpu IDA library
3 *
4 * Copyright (C) 2013 Datera, Inc. Kent Overstreet
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 */
16
17#include <linux/bitmap.h>
18#include <linux/bitops.h>
19#include <linux/bug.h>
20#include <linux/err.h>
21#include <linux/export.h>
22#include <linux/hardirq.h>
23#include <linux/idr.h>
24#include <linux/init.h>
25#include <linux/kernel.h>
26#include <linux/percpu.h>
27#include <linux/sched.h>
28#include <linux/slab.h>
29#include <linux/string.h>
30#include <linux/spinlock.h>
31#include <linux/percpu_ida.h>
32
33/*
34 * Number of tags we move between the percpu freelist and the global freelist at
35 * a time
36 */
37#define IDA_PCPU_BATCH_MOVE 32U
38
39/* Max size of percpu freelist, */
40#define IDA_PCPU_SIZE ((IDA_PCPU_BATCH_MOVE * 3) / 2)
41
42struct percpu_ida_cpu {
43 /*
44 * Even though this is percpu, we need a lock for tag stealing by remote
45 * CPUs:
46 */
47 spinlock_t lock;
48
49 /* nr_free/freelist form a stack of free IDs */
50 unsigned nr_free;
51 unsigned freelist[];
52};
53
54static inline void move_tags(unsigned *dst, unsigned *dst_nr,
55 unsigned *src, unsigned *src_nr,
56 unsigned nr)
57{
58 *src_nr -= nr;
59 memcpy(dst + *dst_nr, src + *src_nr, sizeof(unsigned) * nr);
60 *dst_nr += nr;
61}
62
63/*
64 * Try to steal tags from a remote cpu's percpu freelist.
65 *
66 * We first check how many percpu freelists have tags - we don't steal tags
67 * unless enough percpu freelists have tags on them that it's possible more than
68 * half the total tags could be stuck on remote percpu freelists.
69 *
70 * Then we iterate through the cpus until we find some tags - we don't attempt
71 * to find the "best" cpu to steal from, to keep cacheline bouncing to a
72 * minimum.
73 */
74static inline void steal_tags(struct percpu_ida *pool,
75 struct percpu_ida_cpu *tags)
76{
77 unsigned cpus_have_tags, cpu = pool->cpu_last_stolen;
78 struct percpu_ida_cpu *remote;
79
80 for (cpus_have_tags = cpumask_weight(&pool->cpus_have_tags);
81 cpus_have_tags * IDA_PCPU_SIZE > pool->nr_tags / 2;
82 cpus_have_tags--) {
83 cpu = cpumask_next(cpu, &pool->cpus_have_tags);
84
85 if (cpu >= nr_cpu_ids) {
86 cpu = cpumask_first(&pool->cpus_have_tags);
87 if (cpu >= nr_cpu_ids)
88 BUG();
89 }
90
91 pool->cpu_last_stolen = cpu;
92 remote = per_cpu_ptr(pool->tag_cpu, cpu);
93
94 cpumask_clear_cpu(cpu, &pool->cpus_have_tags);
95
96 if (remote == tags)
97 continue;
98
99 spin_lock(&remote->lock);
100
101 if (remote->nr_free) {
102 memcpy(tags->freelist,
103 remote->freelist,
104 sizeof(unsigned) * remote->nr_free);
105
106 tags->nr_free = remote->nr_free;
107 remote->nr_free = 0;
108 }
109
110 spin_unlock(&remote->lock);
111
112 if (tags->nr_free)
113 break;
114 }
115}
116
117/*
118 * Pop up to IDA_PCPU_BATCH_MOVE IDs off the global freelist, and push them onto
119 * our percpu freelist:
120 */
121static inline void alloc_global_tags(struct percpu_ida *pool,
122 struct percpu_ida_cpu *tags)
123{
124 move_tags(tags->freelist, &tags->nr_free,
125 pool->freelist, &pool->nr_free,
126 min(pool->nr_free, IDA_PCPU_BATCH_MOVE));
127}
128
129static inline unsigned alloc_local_tag(struct percpu_ida *pool,
130 struct percpu_ida_cpu *tags)
131{
132 int tag = -ENOSPC;
133
134 spin_lock(&tags->lock);
135 if (tags->nr_free)
136 tag = tags->freelist[--tags->nr_free];
137 spin_unlock(&tags->lock);
138
139 return tag;
140}
141
142/**
143 * percpu_ida_alloc - allocate a tag
144 * @pool: pool to allocate from
145 * @gfp: gfp flags
146 *
147 * Returns a tag - an integer in the range [0..nr_tags) (passed to
148 * tag_pool_init()), or otherwise -ENOSPC on allocation failure.
149 *
150 * Safe to be called from interrupt context (assuming it isn't passed
151 * __GFP_WAIT, of course).
152 *
153 * @gfp indicates whether or not to wait until a free id is available (it's not
154 * used for internal memory allocations); thus if passed __GFP_WAIT we may sleep
155 * however long it takes until another thread frees an id (same semantics as a
156 * mempool).
157 *
158 * Will not fail if passed __GFP_WAIT.
159 */
160int percpu_ida_alloc(struct percpu_ida *pool, gfp_t gfp)
161{
162 DEFINE_WAIT(wait);
163 struct percpu_ida_cpu *tags;
164 unsigned long flags;
165 int tag;
166
167 local_irq_save(flags);
168 tags = this_cpu_ptr(pool->tag_cpu);
169
170 /* Fastpath */
171 tag = alloc_local_tag(pool, tags);
172 if (likely(tag >= 0)) {
173 local_irq_restore(flags);
174 return tag;
175 }
176
177 while (1) {
178 spin_lock(&pool->lock);
179
180 /*
181 * prepare_to_wait() must come before steal_tags(), in case
182 * percpu_ida_free() on another cpu flips a bit in
183 * cpus_have_tags
184 *
185 * global lock held and irqs disabled, don't need percpu lock
186 */
187 prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE);
188
189 if (!tags->nr_free)
190 alloc_global_tags(pool, tags);
191 if (!tags->nr_free)
192 steal_tags(pool, tags);
193
194 if (tags->nr_free) {
195 tag = tags->freelist[--tags->nr_free];
196 if (tags->nr_free)
197 cpumask_set_cpu(smp_processor_id(),
198 &pool->cpus_have_tags);
199 }
200
201 spin_unlock(&pool->lock);
202 local_irq_restore(flags);
203
204 if (tag >= 0 || !(gfp & __GFP_WAIT))
205 break;
206
207 schedule();
208
209 local_irq_save(flags);
210 tags = this_cpu_ptr(pool->tag_cpu);
211 }
212
213 finish_wait(&pool->wait, &wait);
214 return tag;
215}
216EXPORT_SYMBOL_GPL(percpu_ida_alloc);
217
218/**
219 * percpu_ida_free - free a tag
220 * @pool: pool @tag was allocated from
221 * @tag: a tag previously allocated with percpu_ida_alloc()
222 *
223 * Safe to be called from interrupt context.
224 */
225void percpu_ida_free(struct percpu_ida *pool, unsigned tag)
226{
227 struct percpu_ida_cpu *tags;
228 unsigned long flags;
229 unsigned nr_free;
230
231 BUG_ON(tag >= pool->nr_tags);
232
233 local_irq_save(flags);
234 tags = this_cpu_ptr(pool->tag_cpu);
235
236 spin_lock(&tags->lock);
237 tags->freelist[tags->nr_free++] = tag;
238
239 nr_free = tags->nr_free;
240 spin_unlock(&tags->lock);
241
242 if (nr_free == 1) {
243 cpumask_set_cpu(smp_processor_id(),
244 &pool->cpus_have_tags);
245 wake_up(&pool->wait);
246 }
247
248 if (nr_free == IDA_PCPU_SIZE) {
249 spin_lock(&pool->lock);
250
251 /*
252 * Global lock held and irqs disabled, don't need percpu
253 * lock
254 */
255 if (tags->nr_free == IDA_PCPU_SIZE) {
256 move_tags(pool->freelist, &pool->nr_free,
257 tags->freelist, &tags->nr_free,
258 IDA_PCPU_BATCH_MOVE);
259
260 wake_up(&pool->wait);
261 }
262 spin_unlock(&pool->lock);
263 }
264
265 local_irq_restore(flags);
266}
267EXPORT_SYMBOL_GPL(percpu_ida_free);
268
269/**
270 * percpu_ida_destroy - release a tag pool's resources
271 * @pool: pool to free
272 *
273 * Frees the resources allocated by percpu_ida_init().
274 */
275void percpu_ida_destroy(struct percpu_ida *pool)
276{
277 free_percpu(pool->tag_cpu);
278 free_pages((unsigned long) pool->freelist,
279 get_order(pool->nr_tags * sizeof(unsigned)));
280}
281EXPORT_SYMBOL_GPL(percpu_ida_destroy);
282
283/**
284 * percpu_ida_init - initialize a percpu tag pool
285 * @pool: pool to initialize
286 * @nr_tags: number of tags that will be available for allocation
287 *
288 * Initializes @pool so that it can be used to allocate tags - integers in the
289 * range [0, nr_tags). Typically, they'll be used by driver code to refer to a
290 * preallocated array of tag structures.
291 *
292 * Allocation is percpu, but sharding is limited by nr_tags - for best
293 * performance, the workload should not span more cpus than nr_tags / 128.
294 */
295int percpu_ida_init(struct percpu_ida *pool, unsigned long nr_tags)
296{
297 unsigned i, cpu, order;
298
299 memset(pool, 0, sizeof(*pool));
300
301 init_waitqueue_head(&pool->wait);
302 spin_lock_init(&pool->lock);
303 pool->nr_tags = nr_tags;
304
305 /* Guard against overflow */
306 if (nr_tags > (unsigned) INT_MAX + 1) {
307 pr_err("percpu_ida_init(): nr_tags too large\n");
308 return -EINVAL;
309 }
310
311 order = get_order(nr_tags * sizeof(unsigned));
312 pool->freelist = (void *) __get_free_pages(GFP_KERNEL, order);
313 if (!pool->freelist)
314 return -ENOMEM;
315
316 for (i = 0; i < nr_tags; i++)
317 pool->freelist[i] = i;
318
319 pool->nr_free = nr_tags;
320
321 pool->tag_cpu = __alloc_percpu(sizeof(struct percpu_ida_cpu) +
322 IDA_PCPU_SIZE * sizeof(unsigned),
323 sizeof(unsigned));
324 if (!pool->tag_cpu)
325 goto err;
326
327 for_each_possible_cpu(cpu)
328 spin_lock_init(&per_cpu_ptr(pool->tag_cpu, cpu)->lock);
329
330 return 0;
331err:
332 percpu_ida_destroy(pool);
333 return -ENOMEM;
334}
335EXPORT_SYMBOL_GPL(percpu_ida_init);
diff --git a/lib/radix-tree.c b/lib/radix-tree.c
index e7964296fd50..7811ed3b4e70 100644
--- a/lib/radix-tree.c
+++ b/lib/radix-tree.c
@@ -32,6 +32,7 @@
32#include <linux/string.h> 32#include <linux/string.h>
33#include <linux/bitops.h> 33#include <linux/bitops.h>
34#include <linux/rcupdate.h> 34#include <linux/rcupdate.h>
35#include <linux/hardirq.h> /* in_interrupt() */
35 36
36 37
37#ifdef __KERNEL__ 38#ifdef __KERNEL__
@@ -207,7 +208,12 @@ radix_tree_node_alloc(struct radix_tree_root *root)
207 struct radix_tree_node *ret = NULL; 208 struct radix_tree_node *ret = NULL;
208 gfp_t gfp_mask = root_gfp_mask(root); 209 gfp_t gfp_mask = root_gfp_mask(root);
209 210
210 if (!(gfp_mask & __GFP_WAIT)) { 211 /*
212 * Preload code isn't irq safe and it doesn't make sence to use
213 * preloading in the interrupt anyway as all the allocations have to
214 * be atomic. So just do normal allocation when in interrupt.
215 */
216 if (!(gfp_mask & __GFP_WAIT) && !in_interrupt()) {
211 struct radix_tree_preload *rtp; 217 struct radix_tree_preload *rtp;
212 218
213 /* 219 /*
@@ -264,7 +270,7 @@ radix_tree_node_free(struct radix_tree_node *node)
264 * To make use of this facility, the radix tree must be initialised without 270 * To make use of this facility, the radix tree must be initialised without
265 * __GFP_WAIT being passed to INIT_RADIX_TREE(). 271 * __GFP_WAIT being passed to INIT_RADIX_TREE().
266 */ 272 */
267int radix_tree_preload(gfp_t gfp_mask) 273static int __radix_tree_preload(gfp_t gfp_mask)
268{ 274{
269 struct radix_tree_preload *rtp; 275 struct radix_tree_preload *rtp;
270 struct radix_tree_node *node; 276 struct radix_tree_node *node;
@@ -288,9 +294,40 @@ int radix_tree_preload(gfp_t gfp_mask)
288out: 294out:
289 return ret; 295 return ret;
290} 296}
297
298/*
299 * Load up this CPU's radix_tree_node buffer with sufficient objects to
300 * ensure that the addition of a single element in the tree cannot fail. On
301 * success, return zero, with preemption disabled. On error, return -ENOMEM
302 * with preemption not disabled.
303 *
304 * To make use of this facility, the radix tree must be initialised without
305 * __GFP_WAIT being passed to INIT_RADIX_TREE().
306 */
307int radix_tree_preload(gfp_t gfp_mask)
308{
309 /* Warn on non-sensical use... */
310 WARN_ON_ONCE(!(gfp_mask & __GFP_WAIT));
311 return __radix_tree_preload(gfp_mask);
312}
291EXPORT_SYMBOL(radix_tree_preload); 313EXPORT_SYMBOL(radix_tree_preload);
292 314
293/* 315/*
316 * The same as above function, except we don't guarantee preloading happens.
317 * We do it, if we decide it helps. On success, return zero with preemption
318 * disabled. On error, return -ENOMEM with preemption not disabled.
319 */
320int radix_tree_maybe_preload(gfp_t gfp_mask)
321{
322 if (gfp_mask & __GFP_WAIT)
323 return __radix_tree_preload(gfp_mask);
324 /* Preloading doesn't help anything with this gfp mask, skip it */
325 preempt_disable();
326 return 0;
327}
328EXPORT_SYMBOL(radix_tree_maybe_preload);
329
330/*
294 * Return the maximum key which can be store into a 331 * Return the maximum key which can be store into a
295 * radix tree with height HEIGHT. 332 * radix tree with height HEIGHT.
296 */ 333 */
diff --git a/lib/rbtree.c b/lib/rbtree.c
index c0e31fe2fabf..65f4effd117f 100644
--- a/lib/rbtree.c
+++ b/lib/rbtree.c
@@ -518,3 +518,43 @@ void rb_replace_node(struct rb_node *victim, struct rb_node *new,
518 *new = *victim; 518 *new = *victim;
519} 519}
520EXPORT_SYMBOL(rb_replace_node); 520EXPORT_SYMBOL(rb_replace_node);
521
522static struct rb_node *rb_left_deepest_node(const struct rb_node *node)
523{
524 for (;;) {
525 if (node->rb_left)
526 node = node->rb_left;
527 else if (node->rb_right)
528 node = node->rb_right;
529 else
530 return (struct rb_node *)node;
531 }
532}
533
534struct rb_node *rb_next_postorder(const struct rb_node *node)
535{
536 const struct rb_node *parent;
537 if (!node)
538 return NULL;
539 parent = rb_parent(node);
540
541 /* If we're sitting on node, we've already seen our children */
542 if (parent && node == parent->rb_left && parent->rb_right) {
543 /* If we are the parent's left node, go to the parent's right
544 * node then all the way down to the left */
545 return rb_left_deepest_node(parent->rb_right);
546 } else
547 /* Otherwise we are the parent's right node, and the parent
548 * should be next */
549 return (struct rb_node *)parent;
550}
551EXPORT_SYMBOL(rb_next_postorder);
552
553struct rb_node *rb_first_postorder(const struct rb_root *root)
554{
555 if (!root->rb_node)
556 return NULL;
557
558 return rb_left_deepest_node(root->rb_node);
559}
560EXPORT_SYMBOL(rb_first_postorder);
diff --git a/lib/rbtree_test.c b/lib/rbtree_test.c
index 122f02f9941b..31dd4ccd3baa 100644
--- a/lib/rbtree_test.c
+++ b/lib/rbtree_test.c
@@ -114,6 +114,16 @@ static int black_path_count(struct rb_node *rb)
114 return count; 114 return count;
115} 115}
116 116
117static void check_postorder(int nr_nodes)
118{
119 struct rb_node *rb;
120 int count = 0;
121 for (rb = rb_first_postorder(&root); rb; rb = rb_next_postorder(rb))
122 count++;
123
124 WARN_ON_ONCE(count != nr_nodes);
125}
126
117static void check(int nr_nodes) 127static void check(int nr_nodes)
118{ 128{
119 struct rb_node *rb; 129 struct rb_node *rb;
@@ -136,6 +146,8 @@ static void check(int nr_nodes)
136 146
137 WARN_ON_ONCE(count != nr_nodes); 147 WARN_ON_ONCE(count != nr_nodes);
138 WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1); 148 WARN_ON_ONCE(count < (1 << black_path_count(rb_last(&root))) - 1);
149
150 check_postorder(nr_nodes);
139} 151}
140 152
141static void check_augmented(int nr_nodes) 153static void check_augmented(int nr_nodes)