aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
authorBenjamin Herrenschmidt <benh@kernel.crashing.org>2009-03-29 23:04:53 -0400
committerBenjamin Herrenschmidt <benh@kernel.crashing.org>2009-03-29 23:04:53 -0400
commit9ff9a26b786c35ee8d2a66222924a807ec851a9f (patch)
treedb432a17bccca1ca2c16907f0ee83ac449ed4012 /mm
parent0a3108beea9143225119d5e7c72a8e2c64f3eb7d (diff)
parent0d34fb8e93ceba7b6dad0062dbb4a0813bacd75b (diff)
Merge commit 'origin/master' into next
Manual merge of: arch/powerpc/include/asm/elf.h drivers/i2c/busses/i2c-mpc.c
Diffstat (limited to 'mm')
-rw-r--r--mm/Makefile4
-rw-r--r--mm/allocpercpu.c32
-rw-r--r--mm/backing-dev.c26
-rw-r--r--mm/bootmem.c35
-rw-r--r--mm/filemap.c7
-rw-r--r--mm/highmem.c65
-rw-r--r--mm/mmap.c4
-rw-r--r--mm/page-writeback.c4
-rw-r--r--mm/percpu.c1326
-rw-r--r--mm/readahead.c25
-rw-r--r--mm/shmem.c2
-rw-r--r--mm/slob.c43
-rw-r--r--mm/slub.c82
-rw-r--r--mm/vmalloc.c97
14 files changed, 1644 insertions, 108 deletions
diff --git a/mm/Makefile b/mm/Makefile
index 72255be57f89..818569b68f46 100644
--- a/mm/Makefile
+++ b/mm/Makefile
@@ -30,6 +30,10 @@ obj-$(CONFIG_FAILSLAB) += failslab.o
30obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o 30obj-$(CONFIG_MEMORY_HOTPLUG) += memory_hotplug.o
31obj-$(CONFIG_FS_XIP) += filemap_xip.o 31obj-$(CONFIG_FS_XIP) += filemap_xip.o
32obj-$(CONFIG_MIGRATION) += migrate.o 32obj-$(CONFIG_MIGRATION) += migrate.o
33ifdef CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
34obj-$(CONFIG_SMP) += percpu.o
35else
33obj-$(CONFIG_SMP) += allocpercpu.o 36obj-$(CONFIG_SMP) += allocpercpu.o
37endif
34obj-$(CONFIG_QUICKLIST) += quicklist.o 38obj-$(CONFIG_QUICKLIST) += quicklist.o
35obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o 39obj-$(CONFIG_CGROUP_MEM_RES_CTLR) += memcontrol.o page_cgroup.o
diff --git a/mm/allocpercpu.c b/mm/allocpercpu.c
index 4297bc41bfd2..1882923bc706 100644
--- a/mm/allocpercpu.c
+++ b/mm/allocpercpu.c
@@ -99,45 +99,51 @@ static int __percpu_populate_mask(void *__pdata, size_t size, gfp_t gfp,
99 __percpu_populate_mask((__pdata), (size), (gfp), &(mask)) 99 __percpu_populate_mask((__pdata), (size), (gfp), &(mask))
100 100
101/** 101/**
102 * percpu_alloc_mask - initial setup of per-cpu data 102 * alloc_percpu - initial setup of per-cpu data
103 * @size: size of per-cpu object 103 * @size: size of per-cpu object
104 * @gfp: may sleep or not etc. 104 * @align: alignment
105 * @mask: populate per-data for cpu's selected through mask bits
106 * 105 *
107 * Populating per-cpu data for all online cpu's would be a typical use case, 106 * Allocate dynamic percpu area. Percpu objects are populated with
108 * which is simplified by the percpu_alloc() wrapper. 107 * zeroed buffers.
109 * Per-cpu objects are populated with zeroed buffers.
110 */ 108 */
111void *__percpu_alloc_mask(size_t size, gfp_t gfp, cpumask_t *mask) 109void *__alloc_percpu(size_t size, size_t align)
112{ 110{
113 /* 111 /*
114 * We allocate whole cache lines to avoid false sharing 112 * We allocate whole cache lines to avoid false sharing
115 */ 113 */
116 size_t sz = roundup(nr_cpu_ids * sizeof(void *), cache_line_size()); 114 size_t sz = roundup(nr_cpu_ids * sizeof(void *), cache_line_size());
117 void *pdata = kzalloc(sz, gfp); 115 void *pdata = kzalloc(sz, GFP_KERNEL);
118 void *__pdata = __percpu_disguise(pdata); 116 void *__pdata = __percpu_disguise(pdata);
119 117
118 /*
119 * Can't easily make larger alignment work with kmalloc. WARN
120 * on it. Larger alignment should only be used for module
121 * percpu sections on SMP for which this path isn't used.
122 */
123 WARN_ON_ONCE(align > SMP_CACHE_BYTES);
124
120 if (unlikely(!pdata)) 125 if (unlikely(!pdata))
121 return NULL; 126 return NULL;
122 if (likely(!__percpu_populate_mask(__pdata, size, gfp, mask))) 127 if (likely(!__percpu_populate_mask(__pdata, size, GFP_KERNEL,
128 &cpu_possible_map)))
123 return __pdata; 129 return __pdata;
124 kfree(pdata); 130 kfree(pdata);
125 return NULL; 131 return NULL;
126} 132}
127EXPORT_SYMBOL_GPL(__percpu_alloc_mask); 133EXPORT_SYMBOL_GPL(__alloc_percpu);
128 134
129/** 135/**
130 * percpu_free - final cleanup of per-cpu data 136 * free_percpu - final cleanup of per-cpu data
131 * @__pdata: object to clean up 137 * @__pdata: object to clean up
132 * 138 *
133 * We simply clean up any per-cpu object left. No need for the client to 139 * We simply clean up any per-cpu object left. No need for the client to
134 * track and specify through a bis mask which per-cpu objects are to free. 140 * track and specify through a bis mask which per-cpu objects are to free.
135 */ 141 */
136void percpu_free(void *__pdata) 142void free_percpu(void *__pdata)
137{ 143{
138 if (unlikely(!__pdata)) 144 if (unlikely(!__pdata))
139 return; 145 return;
140 __percpu_depopulate_mask(__pdata, &cpu_possible_map); 146 __percpu_depopulate_mask(__pdata, &cpu_possible_map);
141 kfree(__percpu_disguise(__pdata)); 147 kfree(__percpu_disguise(__pdata));
142} 148}
143EXPORT_SYMBOL_GPL(percpu_free); 149EXPORT_SYMBOL_GPL(free_percpu);
diff --git a/mm/backing-dev.c b/mm/backing-dev.c
index 8e8587444132..be68c956a660 100644
--- a/mm/backing-dev.c
+++ b/mm/backing-dev.c
@@ -2,11 +2,24 @@
2#include <linux/wait.h> 2#include <linux/wait.h>
3#include <linux/backing-dev.h> 3#include <linux/backing-dev.h>
4#include <linux/fs.h> 4#include <linux/fs.h>
5#include <linux/pagemap.h>
5#include <linux/sched.h> 6#include <linux/sched.h>
6#include <linux/module.h> 7#include <linux/module.h>
7#include <linux/writeback.h> 8#include <linux/writeback.h>
8#include <linux/device.h> 9#include <linux/device.h>
9 10
11void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
12{
13}
14EXPORT_SYMBOL(default_unplug_io_fn);
15
16struct backing_dev_info default_backing_dev_info = {
17 .ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE,
18 .state = 0,
19 .capabilities = BDI_CAP_MAP_COPY,
20 .unplug_io_fn = default_unplug_io_fn,
21};
22EXPORT_SYMBOL_GPL(default_backing_dev_info);
10 23
11static struct class *bdi_class; 24static struct class *bdi_class;
12 25
@@ -166,9 +179,20 @@ static __init int bdi_class_init(void)
166 bdi_debug_init(); 179 bdi_debug_init();
167 return 0; 180 return 0;
168} 181}
169
170postcore_initcall(bdi_class_init); 182postcore_initcall(bdi_class_init);
171 183
184static int __init default_bdi_init(void)
185{
186 int err;
187
188 err = bdi_init(&default_backing_dev_info);
189 if (!err)
190 bdi_register(&default_backing_dev_info, NULL, "default");
191
192 return err;
193}
194subsys_initcall(default_bdi_init);
195
172int bdi_register(struct backing_dev_info *bdi, struct device *parent, 196int bdi_register(struct backing_dev_info *bdi, struct device *parent,
173 const char *fmt, ...) 197 const char *fmt, ...)
174{ 198{
diff --git a/mm/bootmem.c b/mm/bootmem.c
index 51a0ccf61e0e..daf92713f7de 100644
--- a/mm/bootmem.c
+++ b/mm/bootmem.c
@@ -382,7 +382,6 @@ int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
382 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags); 382 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
383} 383}
384 384
385#ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
386/** 385/**
387 * reserve_bootmem - mark a page range as usable 386 * reserve_bootmem - mark a page range as usable
388 * @addr: starting address of the range 387 * @addr: starting address of the range
@@ -403,7 +402,6 @@ int __init reserve_bootmem(unsigned long addr, unsigned long size,
403 402
404 return mark_bootmem(start, end, 1, flags); 403 return mark_bootmem(start, end, 1, flags);
405} 404}
406#endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
407 405
408static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx, 406static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx,
409 unsigned long step) 407 unsigned long step)
@@ -429,8 +427,8 @@ static unsigned long align_off(struct bootmem_data *bdata, unsigned long off,
429} 427}
430 428
431static void * __init alloc_bootmem_core(struct bootmem_data *bdata, 429static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
432 unsigned long size, unsigned long align, 430 unsigned long size, unsigned long align,
433 unsigned long goal, unsigned long limit) 431 unsigned long goal, unsigned long limit)
434{ 432{
435 unsigned long fallback = 0; 433 unsigned long fallback = 0;
436 unsigned long min, max, start, sidx, midx, step; 434 unsigned long min, max, start, sidx, midx, step;
@@ -530,17 +528,34 @@ find_block:
530 return NULL; 528 return NULL;
531} 529}
532 530
531static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
532 unsigned long size, unsigned long align,
533 unsigned long goal, unsigned long limit)
534{
535#ifdef CONFIG_HAVE_ARCH_BOOTMEM
536 bootmem_data_t *p_bdata;
537
538 p_bdata = bootmem_arch_preferred_node(bdata, size, align, goal, limit);
539 if (p_bdata)
540 return alloc_bootmem_core(p_bdata, size, align, goal, limit);
541#endif
542 return NULL;
543}
544
533static void * __init ___alloc_bootmem_nopanic(unsigned long size, 545static void * __init ___alloc_bootmem_nopanic(unsigned long size,
534 unsigned long align, 546 unsigned long align,
535 unsigned long goal, 547 unsigned long goal,
536 unsigned long limit) 548 unsigned long limit)
537{ 549{
538 bootmem_data_t *bdata; 550 bootmem_data_t *bdata;
551 void *region;
539 552
540restart: 553restart:
541 list_for_each_entry(bdata, &bdata_list, list) { 554 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
542 void *region; 555 if (region)
556 return region;
543 557
558 list_for_each_entry(bdata, &bdata_list, list) {
544 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal)) 559 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
545 continue; 560 continue;
546 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit)) 561 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
@@ -618,6 +633,10 @@ static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
618{ 633{
619 void *ptr; 634 void *ptr;
620 635
636 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
637 if (ptr)
638 return ptr;
639
621 ptr = alloc_bootmem_core(bdata, size, align, goal, limit); 640 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
622 if (ptr) 641 if (ptr)
623 return ptr; 642 return ptr;
@@ -674,6 +693,10 @@ void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
674{ 693{
675 void *ptr; 694 void *ptr;
676 695
696 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
697 if (ptr)
698 return ptr;
699
677 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); 700 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
678 if (ptr) 701 if (ptr)
679 return ptr; 702 return ptr;
diff --git a/mm/filemap.c b/mm/filemap.c
index 23acefe51808..126d3973b3d1 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1823,7 +1823,7 @@ static size_t __iovec_copy_from_user_inatomic(char *vaddr,
1823 int copy = min(bytes, iov->iov_len - base); 1823 int copy = min(bytes, iov->iov_len - base);
1824 1824
1825 base = 0; 1825 base = 0;
1826 left = __copy_from_user_inatomic_nocache(vaddr, buf, copy); 1826 left = __copy_from_user_inatomic(vaddr, buf, copy);
1827 copied += copy; 1827 copied += copy;
1828 bytes -= copy; 1828 bytes -= copy;
1829 vaddr += copy; 1829 vaddr += copy;
@@ -1851,8 +1851,7 @@ size_t iov_iter_copy_from_user_atomic(struct page *page,
1851 if (likely(i->nr_segs == 1)) { 1851 if (likely(i->nr_segs == 1)) {
1852 int left; 1852 int left;
1853 char __user *buf = i->iov->iov_base + i->iov_offset; 1853 char __user *buf = i->iov->iov_base + i->iov_offset;
1854 left = __copy_from_user_inatomic_nocache(kaddr + offset, 1854 left = __copy_from_user_inatomic(kaddr + offset, buf, bytes);
1855 buf, bytes);
1856 copied = bytes - left; 1855 copied = bytes - left;
1857 } else { 1856 } else {
1858 copied = __iovec_copy_from_user_inatomic(kaddr + offset, 1857 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
@@ -1880,7 +1879,7 @@ size_t iov_iter_copy_from_user(struct page *page,
1880 if (likely(i->nr_segs == 1)) { 1879 if (likely(i->nr_segs == 1)) {
1881 int left; 1880 int left;
1882 char __user *buf = i->iov->iov_base + i->iov_offset; 1881 char __user *buf = i->iov->iov_base + i->iov_offset;
1883 left = __copy_from_user_nocache(kaddr + offset, buf, bytes); 1882 left = __copy_from_user(kaddr + offset, buf, bytes);
1884 copied = bytes - left; 1883 copied = bytes - left;
1885 } else { 1884 } else {
1886 copied = __iovec_copy_from_user_inatomic(kaddr + offset, 1885 copied = __iovec_copy_from_user_inatomic(kaddr + offset,
diff --git a/mm/highmem.c b/mm/highmem.c
index b36b83b920ff..910198037bf5 100644
--- a/mm/highmem.c
+++ b/mm/highmem.c
@@ -67,6 +67,25 @@ pte_t * pkmap_page_table;
67 67
68static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait); 68static DECLARE_WAIT_QUEUE_HEAD(pkmap_map_wait);
69 69
70/*
71 * Most architectures have no use for kmap_high_get(), so let's abstract
72 * the disabling of IRQ out of the locking in that case to save on a
73 * potential useless overhead.
74 */
75#ifdef ARCH_NEEDS_KMAP_HIGH_GET
76#define lock_kmap() spin_lock_irq(&kmap_lock)
77#define unlock_kmap() spin_unlock_irq(&kmap_lock)
78#define lock_kmap_any(flags) spin_lock_irqsave(&kmap_lock, flags)
79#define unlock_kmap_any(flags) spin_unlock_irqrestore(&kmap_lock, flags)
80#else
81#define lock_kmap() spin_lock(&kmap_lock)
82#define unlock_kmap() spin_unlock(&kmap_lock)
83#define lock_kmap_any(flags) \
84 do { spin_lock(&kmap_lock); (void)(flags); } while (0)
85#define unlock_kmap_any(flags) \
86 do { spin_unlock(&kmap_lock); (void)(flags); } while (0)
87#endif
88
70static void flush_all_zero_pkmaps(void) 89static void flush_all_zero_pkmaps(void)
71{ 90{
72 int i; 91 int i;
@@ -113,9 +132,9 @@ static void flush_all_zero_pkmaps(void)
113 */ 132 */
114void kmap_flush_unused(void) 133void kmap_flush_unused(void)
115{ 134{
116 spin_lock(&kmap_lock); 135 lock_kmap();
117 flush_all_zero_pkmaps(); 136 flush_all_zero_pkmaps();
118 spin_unlock(&kmap_lock); 137 unlock_kmap();
119} 138}
120 139
121static inline unsigned long map_new_virtual(struct page *page) 140static inline unsigned long map_new_virtual(struct page *page)
@@ -145,10 +164,10 @@ start:
145 164
146 __set_current_state(TASK_UNINTERRUPTIBLE); 165 __set_current_state(TASK_UNINTERRUPTIBLE);
147 add_wait_queue(&pkmap_map_wait, &wait); 166 add_wait_queue(&pkmap_map_wait, &wait);
148 spin_unlock(&kmap_lock); 167 unlock_kmap();
149 schedule(); 168 schedule();
150 remove_wait_queue(&pkmap_map_wait, &wait); 169 remove_wait_queue(&pkmap_map_wait, &wait);
151 spin_lock(&kmap_lock); 170 lock_kmap();
152 171
153 /* Somebody else might have mapped it while we slept */ 172 /* Somebody else might have mapped it while we slept */
154 if (page_address(page)) 173 if (page_address(page))
@@ -184,29 +203,59 @@ void *kmap_high(struct page *page)
184 * For highmem pages, we can't trust "virtual" until 203 * For highmem pages, we can't trust "virtual" until
185 * after we have the lock. 204 * after we have the lock.
186 */ 205 */
187 spin_lock(&kmap_lock); 206 lock_kmap();
188 vaddr = (unsigned long)page_address(page); 207 vaddr = (unsigned long)page_address(page);
189 if (!vaddr) 208 if (!vaddr)
190 vaddr = map_new_virtual(page); 209 vaddr = map_new_virtual(page);
191 pkmap_count[PKMAP_NR(vaddr)]++; 210 pkmap_count[PKMAP_NR(vaddr)]++;
192 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2); 211 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 2);
193 spin_unlock(&kmap_lock); 212 unlock_kmap();
194 return (void*) vaddr; 213 return (void*) vaddr;
195} 214}
196 215
197EXPORT_SYMBOL(kmap_high); 216EXPORT_SYMBOL(kmap_high);
198 217
218#ifdef ARCH_NEEDS_KMAP_HIGH_GET
219/**
220 * kmap_high_get - pin a highmem page into memory
221 * @page: &struct page to pin
222 *
223 * Returns the page's current virtual memory address, or NULL if no mapping
224 * exists. When and only when a non null address is returned then a
225 * matching call to kunmap_high() is necessary.
226 *
227 * This can be called from any context.
228 */
229void *kmap_high_get(struct page *page)
230{
231 unsigned long vaddr, flags;
232
233 lock_kmap_any(flags);
234 vaddr = (unsigned long)page_address(page);
235 if (vaddr) {
236 BUG_ON(pkmap_count[PKMAP_NR(vaddr)] < 1);
237 pkmap_count[PKMAP_NR(vaddr)]++;
238 }
239 unlock_kmap_any(flags);
240 return (void*) vaddr;
241}
242#endif
243
199/** 244/**
200 * kunmap_high - map a highmem page into memory 245 * kunmap_high - map a highmem page into memory
201 * @page: &struct page to unmap 246 * @page: &struct page to unmap
247 *
248 * If ARCH_NEEDS_KMAP_HIGH_GET is not defined then this may be called
249 * only from user context.
202 */ 250 */
203void kunmap_high(struct page *page) 251void kunmap_high(struct page *page)
204{ 252{
205 unsigned long vaddr; 253 unsigned long vaddr;
206 unsigned long nr; 254 unsigned long nr;
255 unsigned long flags;
207 int need_wakeup; 256 int need_wakeup;
208 257
209 spin_lock(&kmap_lock); 258 lock_kmap_any(flags);
210 vaddr = (unsigned long)page_address(page); 259 vaddr = (unsigned long)page_address(page);
211 BUG_ON(!vaddr); 260 BUG_ON(!vaddr);
212 nr = PKMAP_NR(vaddr); 261 nr = PKMAP_NR(vaddr);
@@ -232,7 +281,7 @@ void kunmap_high(struct page *page)
232 */ 281 */
233 need_wakeup = waitqueue_active(&pkmap_map_wait); 282 need_wakeup = waitqueue_active(&pkmap_map_wait);
234 } 283 }
235 spin_unlock(&kmap_lock); 284 unlock_kmap_any(flags);
236 285
237 /* do wake-up, if needed, race-free outside of the spin lock */ 286 /* do wake-up, if needed, race-free outside of the spin lock */
238 if (need_wakeup) 287 if (need_wakeup)
diff --git a/mm/mmap.c b/mm/mmap.c
index 00ced3ee49a8..1abb9185a686 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -20,6 +20,7 @@
20#include <linux/fs.h> 20#include <linux/fs.h>
21#include <linux/personality.h> 21#include <linux/personality.h>
22#include <linux/security.h> 22#include <linux/security.h>
23#include <linux/ima.h>
23#include <linux/hugetlb.h> 24#include <linux/hugetlb.h>
24#include <linux/profile.h> 25#include <linux/profile.h>
25#include <linux/module.h> 26#include <linux/module.h>
@@ -1049,6 +1050,9 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
1049 error = security_file_mmap(file, reqprot, prot, flags, addr, 0); 1050 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1050 if (error) 1051 if (error)
1051 return error; 1052 return error;
1053 error = ima_file_mmap(file, prot);
1054 if (error)
1055 return error;
1052 1056
1053 return mmap_region(file, addr, len, flags, vm_flags, pgoff); 1057 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
1054} 1058}
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 74dc57c74349..40ca7cdb653e 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -66,7 +66,7 @@ static inline long sync_writeback_pages(void)
66/* 66/*
67 * Start background writeback (via pdflush) at this percentage 67 * Start background writeback (via pdflush) at this percentage
68 */ 68 */
69int dirty_background_ratio = 5; 69int dirty_background_ratio = 10;
70 70
71/* 71/*
72 * dirty_background_bytes starts at 0 (disabled) so that it is a function of 72 * dirty_background_bytes starts at 0 (disabled) so that it is a function of
@@ -83,7 +83,7 @@ int vm_highmem_is_dirtyable;
83/* 83/*
84 * The generator of dirty data starts writeback at this percentage 84 * The generator of dirty data starts writeback at this percentage
85 */ 85 */
86int vm_dirty_ratio = 10; 86int vm_dirty_ratio = 20;
87 87
88/* 88/*
89 * vm_dirty_bytes starts at 0 (disabled) so that it is a function of 89 * vm_dirty_bytes starts at 0 (disabled) so that it is a function of
diff --git a/mm/percpu.c b/mm/percpu.c
new file mode 100644
index 000000000000..1aa5d8fbca12
--- /dev/null
+++ b/mm/percpu.c
@@ -0,0 +1,1326 @@
1/*
2 * linux/mm/percpu.c - percpu memory allocator
3 *
4 * Copyright (C) 2009 SUSE Linux Products GmbH
5 * Copyright (C) 2009 Tejun Heo <tj@kernel.org>
6 *
7 * This file is released under the GPLv2.
8 *
9 * This is percpu allocator which can handle both static and dynamic
10 * areas. Percpu areas are allocated in chunks in vmalloc area. Each
11 * chunk is consisted of num_possible_cpus() units and the first chunk
12 * is used for static percpu variables in the kernel image (special
13 * boot time alloc/init handling necessary as these areas need to be
14 * brought up before allocation services are running). Unit grows as
15 * necessary and all units grow or shrink in unison. When a chunk is
16 * filled up, another chunk is allocated. ie. in vmalloc area
17 *
18 * c0 c1 c2
19 * ------------------- ------------------- ------------
20 * | u0 | u1 | u2 | u3 | | u0 | u1 | u2 | u3 | | u0 | u1 | u
21 * ------------------- ...... ------------------- .... ------------
22 *
23 * Allocation is done in offset-size areas of single unit space. Ie,
24 * an area of 512 bytes at 6k in c1 occupies 512 bytes at 6k of c1:u0,
25 * c1:u1, c1:u2 and c1:u3. Percpu access can be done by configuring
26 * percpu base registers UNIT_SIZE apart.
27 *
28 * There are usually many small percpu allocations many of them as
29 * small as 4 bytes. The allocator organizes chunks into lists
30 * according to free size and tries to allocate from the fullest one.
31 * Each chunk keeps the maximum contiguous area size hint which is
32 * guaranteed to be eqaul to or larger than the maximum contiguous
33 * area in the chunk. This helps the allocator not to iterate the
34 * chunk maps unnecessarily.
35 *
36 * Allocation state in each chunk is kept using an array of integers
37 * on chunk->map. A positive value in the map represents a free
38 * region and negative allocated. Allocation inside a chunk is done
39 * by scanning this map sequentially and serving the first matching
40 * entry. This is mostly copied from the percpu_modalloc() allocator.
41 * Chunks are also linked into a rb tree to ease address to chunk
42 * mapping during free.
43 *
44 * To use this allocator, arch code should do the followings.
45 *
46 * - define CONFIG_HAVE_DYNAMIC_PER_CPU_AREA
47 *
48 * - define __addr_to_pcpu_ptr() and __pcpu_ptr_to_addr() to translate
49 * regular address to percpu pointer and back if they need to be
50 * different from the default
51 *
52 * - use pcpu_setup_first_chunk() during percpu area initialization to
53 * setup the first chunk containing the kernel static percpu area
54 */
55
56#include <linux/bitmap.h>
57#include <linux/bootmem.h>
58#include <linux/list.h>
59#include <linux/mm.h>
60#include <linux/module.h>
61#include <linux/mutex.h>
62#include <linux/percpu.h>
63#include <linux/pfn.h>
64#include <linux/rbtree.h>
65#include <linux/slab.h>
66#include <linux/spinlock.h>
67#include <linux/vmalloc.h>
68#include <linux/workqueue.h>
69
70#include <asm/cacheflush.h>
71#include <asm/sections.h>
72#include <asm/tlbflush.h>
73
74#define PCPU_SLOT_BASE_SHIFT 5 /* 1-31 shares the same slot */
75#define PCPU_DFL_MAP_ALLOC 16 /* start a map with 16 ents */
76
77/* default addr <-> pcpu_ptr mapping, override in asm/percpu.h if necessary */
78#ifndef __addr_to_pcpu_ptr
79#define __addr_to_pcpu_ptr(addr) \
80 (void *)((unsigned long)(addr) - (unsigned long)pcpu_base_addr \
81 + (unsigned long)__per_cpu_start)
82#endif
83#ifndef __pcpu_ptr_to_addr
84#define __pcpu_ptr_to_addr(ptr) \
85 (void *)((unsigned long)(ptr) + (unsigned long)pcpu_base_addr \
86 - (unsigned long)__per_cpu_start)
87#endif
88
89struct pcpu_chunk {
90 struct list_head list; /* linked to pcpu_slot lists */
91 struct rb_node rb_node; /* key is chunk->vm->addr */
92 int free_size; /* free bytes in the chunk */
93 int contig_hint; /* max contiguous size hint */
94 struct vm_struct *vm; /* mapped vmalloc region */
95 int map_used; /* # of map entries used */
96 int map_alloc; /* # of map entries allocated */
97 int *map; /* allocation map */
98 bool immutable; /* no [de]population allowed */
99 struct page **page; /* points to page array */
100 struct page *page_ar[]; /* #cpus * UNIT_PAGES */
101};
102
103static int pcpu_unit_pages __read_mostly;
104static int pcpu_unit_size __read_mostly;
105static int pcpu_chunk_size __read_mostly;
106static int pcpu_nr_slots __read_mostly;
107static size_t pcpu_chunk_struct_size __read_mostly;
108
109/* the address of the first chunk which starts with the kernel static area */
110void *pcpu_base_addr __read_mostly;
111EXPORT_SYMBOL_GPL(pcpu_base_addr);
112
113/* optional reserved chunk, only accessible for reserved allocations */
114static struct pcpu_chunk *pcpu_reserved_chunk;
115/* offset limit of the reserved chunk */
116static int pcpu_reserved_chunk_limit;
117
118/*
119 * Synchronization rules.
120 *
121 * There are two locks - pcpu_alloc_mutex and pcpu_lock. The former
122 * protects allocation/reclaim paths, chunks and chunk->page arrays.
123 * The latter is a spinlock and protects the index data structures -
124 * chunk slots, rbtree, chunks and area maps in chunks.
125 *
126 * During allocation, pcpu_alloc_mutex is kept locked all the time and
127 * pcpu_lock is grabbed and released as necessary. All actual memory
128 * allocations are done using GFP_KERNEL with pcpu_lock released.
129 *
130 * Free path accesses and alters only the index data structures, so it
131 * can be safely called from atomic context. When memory needs to be
132 * returned to the system, free path schedules reclaim_work which
133 * grabs both pcpu_alloc_mutex and pcpu_lock, unlinks chunks to be
134 * reclaimed, release both locks and frees the chunks. Note that it's
135 * necessary to grab both locks to remove a chunk from circulation as
136 * allocation path might be referencing the chunk with only
137 * pcpu_alloc_mutex locked.
138 */
139static DEFINE_MUTEX(pcpu_alloc_mutex); /* protects whole alloc and reclaim */
140static DEFINE_SPINLOCK(pcpu_lock); /* protects index data structures */
141
142static struct list_head *pcpu_slot __read_mostly; /* chunk list slots */
143static struct rb_root pcpu_addr_root = RB_ROOT; /* chunks by address */
144
145/* reclaim work to release fully free chunks, scheduled from free path */
146static void pcpu_reclaim(struct work_struct *work);
147static DECLARE_WORK(pcpu_reclaim_work, pcpu_reclaim);
148
149static int __pcpu_size_to_slot(int size)
150{
151 int highbit = fls(size); /* size is in bytes */
152 return max(highbit - PCPU_SLOT_BASE_SHIFT + 2, 1);
153}
154
155static int pcpu_size_to_slot(int size)
156{
157 if (size == pcpu_unit_size)
158 return pcpu_nr_slots - 1;
159 return __pcpu_size_to_slot(size);
160}
161
162static int pcpu_chunk_slot(const struct pcpu_chunk *chunk)
163{
164 if (chunk->free_size < sizeof(int) || chunk->contig_hint < sizeof(int))
165 return 0;
166
167 return pcpu_size_to_slot(chunk->free_size);
168}
169
170static int pcpu_page_idx(unsigned int cpu, int page_idx)
171{
172 return cpu * pcpu_unit_pages + page_idx;
173}
174
175static struct page **pcpu_chunk_pagep(struct pcpu_chunk *chunk,
176 unsigned int cpu, int page_idx)
177{
178 return &chunk->page[pcpu_page_idx(cpu, page_idx)];
179}
180
181static unsigned long pcpu_chunk_addr(struct pcpu_chunk *chunk,
182 unsigned int cpu, int page_idx)
183{
184 return (unsigned long)chunk->vm->addr +
185 (pcpu_page_idx(cpu, page_idx) << PAGE_SHIFT);
186}
187
188static bool pcpu_chunk_page_occupied(struct pcpu_chunk *chunk,
189 int page_idx)
190{
191 return *pcpu_chunk_pagep(chunk, 0, page_idx) != NULL;
192}
193
194/**
195 * pcpu_mem_alloc - allocate memory
196 * @size: bytes to allocate
197 *
198 * Allocate @size bytes. If @size is smaller than PAGE_SIZE,
199 * kzalloc() is used; otherwise, vmalloc() is used. The returned
200 * memory is always zeroed.
201 *
202 * CONTEXT:
203 * Does GFP_KERNEL allocation.
204 *
205 * RETURNS:
206 * Pointer to the allocated area on success, NULL on failure.
207 */
208static void *pcpu_mem_alloc(size_t size)
209{
210 if (size <= PAGE_SIZE)
211 return kzalloc(size, GFP_KERNEL);
212 else {
213 void *ptr = vmalloc(size);
214 if (ptr)
215 memset(ptr, 0, size);
216 return ptr;
217 }
218}
219
220/**
221 * pcpu_mem_free - free memory
222 * @ptr: memory to free
223 * @size: size of the area
224 *
225 * Free @ptr. @ptr should have been allocated using pcpu_mem_alloc().
226 */
227static void pcpu_mem_free(void *ptr, size_t size)
228{
229 if (size <= PAGE_SIZE)
230 kfree(ptr);
231 else
232 vfree(ptr);
233}
234
235/**
236 * pcpu_chunk_relocate - put chunk in the appropriate chunk slot
237 * @chunk: chunk of interest
238 * @oslot: the previous slot it was on
239 *
240 * This function is called after an allocation or free changed @chunk.
241 * New slot according to the changed state is determined and @chunk is
242 * moved to the slot. Note that the reserved chunk is never put on
243 * chunk slots.
244 *
245 * CONTEXT:
246 * pcpu_lock.
247 */
248static void pcpu_chunk_relocate(struct pcpu_chunk *chunk, int oslot)
249{
250 int nslot = pcpu_chunk_slot(chunk);
251
252 if (chunk != pcpu_reserved_chunk && oslot != nslot) {
253 if (oslot < nslot)
254 list_move(&chunk->list, &pcpu_slot[nslot]);
255 else
256 list_move_tail(&chunk->list, &pcpu_slot[nslot]);
257 }
258}
259
260static struct rb_node **pcpu_chunk_rb_search(void *addr,
261 struct rb_node **parentp)
262{
263 struct rb_node **p = &pcpu_addr_root.rb_node;
264 struct rb_node *parent = NULL;
265 struct pcpu_chunk *chunk;
266
267 while (*p) {
268 parent = *p;
269 chunk = rb_entry(parent, struct pcpu_chunk, rb_node);
270
271 if (addr < chunk->vm->addr)
272 p = &(*p)->rb_left;
273 else if (addr > chunk->vm->addr)
274 p = &(*p)->rb_right;
275 else
276 break;
277 }
278
279 if (parentp)
280 *parentp = parent;
281 return p;
282}
283
284/**
285 * pcpu_chunk_addr_search - search for chunk containing specified address
286 * @addr: address to search for
287 *
288 * Look for chunk which might contain @addr. More specifically, it
289 * searchs for the chunk with the highest start address which isn't
290 * beyond @addr.
291 *
292 * CONTEXT:
293 * pcpu_lock.
294 *
295 * RETURNS:
296 * The address of the found chunk.
297 */
298static struct pcpu_chunk *pcpu_chunk_addr_search(void *addr)
299{
300 struct rb_node *n, *parent;
301 struct pcpu_chunk *chunk;
302
303 /* is it in the reserved chunk? */
304 if (pcpu_reserved_chunk) {
305 void *start = pcpu_reserved_chunk->vm->addr;
306
307 if (addr >= start && addr < start + pcpu_reserved_chunk_limit)
308 return pcpu_reserved_chunk;
309 }
310
311 /* nah... search the regular ones */
312 n = *pcpu_chunk_rb_search(addr, &parent);
313 if (!n) {
314 /* no exactly matching chunk, the parent is the closest */
315 n = parent;
316 BUG_ON(!n);
317 }
318 chunk = rb_entry(n, struct pcpu_chunk, rb_node);
319
320 if (addr < chunk->vm->addr) {
321 /* the parent was the next one, look for the previous one */
322 n = rb_prev(n);
323 BUG_ON(!n);
324 chunk = rb_entry(n, struct pcpu_chunk, rb_node);
325 }
326
327 return chunk;
328}
329
330/**
331 * pcpu_chunk_addr_insert - insert chunk into address rb tree
332 * @new: chunk to insert
333 *
334 * Insert @new into address rb tree.
335 *
336 * CONTEXT:
337 * pcpu_lock.
338 */
339static void pcpu_chunk_addr_insert(struct pcpu_chunk *new)
340{
341 struct rb_node **p, *parent;
342
343 p = pcpu_chunk_rb_search(new->vm->addr, &parent);
344 BUG_ON(*p);
345 rb_link_node(&new->rb_node, parent, p);
346 rb_insert_color(&new->rb_node, &pcpu_addr_root);
347}
348
349/**
350 * pcpu_extend_area_map - extend area map for allocation
351 * @chunk: target chunk
352 *
353 * Extend area map of @chunk so that it can accomodate an allocation.
354 * A single allocation can split an area into three areas, so this
355 * function makes sure that @chunk->map has at least two extra slots.
356 *
357 * CONTEXT:
358 * pcpu_alloc_mutex, pcpu_lock. pcpu_lock is released and reacquired
359 * if area map is extended.
360 *
361 * RETURNS:
362 * 0 if noop, 1 if successfully extended, -errno on failure.
363 */
364static int pcpu_extend_area_map(struct pcpu_chunk *chunk)
365{
366 int new_alloc;
367 int *new;
368 size_t size;
369
370 /* has enough? */
371 if (chunk->map_alloc >= chunk->map_used + 2)
372 return 0;
373
374 spin_unlock_irq(&pcpu_lock);
375
376 new_alloc = PCPU_DFL_MAP_ALLOC;
377 while (new_alloc < chunk->map_used + 2)
378 new_alloc *= 2;
379
380 new = pcpu_mem_alloc(new_alloc * sizeof(new[0]));
381 if (!new) {
382 spin_lock_irq(&pcpu_lock);
383 return -ENOMEM;
384 }
385
386 /*
387 * Acquire pcpu_lock and switch to new area map. Only free
388 * could have happened inbetween, so map_used couldn't have
389 * grown.
390 */
391 spin_lock_irq(&pcpu_lock);
392 BUG_ON(new_alloc < chunk->map_used + 2);
393
394 size = chunk->map_alloc * sizeof(chunk->map[0]);
395 memcpy(new, chunk->map, size);
396
397 /*
398 * map_alloc < PCPU_DFL_MAP_ALLOC indicates that the chunk is
399 * one of the first chunks and still using static map.
400 */
401 if (chunk->map_alloc >= PCPU_DFL_MAP_ALLOC)
402 pcpu_mem_free(chunk->map, size);
403
404 chunk->map_alloc = new_alloc;
405 chunk->map = new;
406 return 0;
407}
408
409/**
410 * pcpu_split_block - split a map block
411 * @chunk: chunk of interest
412 * @i: index of map block to split
413 * @head: head size in bytes (can be 0)
414 * @tail: tail size in bytes (can be 0)
415 *
416 * Split the @i'th map block into two or three blocks. If @head is
417 * non-zero, @head bytes block is inserted before block @i moving it
418 * to @i+1 and reducing its size by @head bytes.
419 *
420 * If @tail is non-zero, the target block, which can be @i or @i+1
421 * depending on @head, is reduced by @tail bytes and @tail byte block
422 * is inserted after the target block.
423 *
424 * @chunk->map must have enough free slots to accomodate the split.
425 *
426 * CONTEXT:
427 * pcpu_lock.
428 */
429static void pcpu_split_block(struct pcpu_chunk *chunk, int i,
430 int head, int tail)
431{
432 int nr_extra = !!head + !!tail;
433
434 BUG_ON(chunk->map_alloc < chunk->map_used + nr_extra);
435
436 /* insert new subblocks */
437 memmove(&chunk->map[i + nr_extra], &chunk->map[i],
438 sizeof(chunk->map[0]) * (chunk->map_used - i));
439 chunk->map_used += nr_extra;
440
441 if (head) {
442 chunk->map[i + 1] = chunk->map[i] - head;
443 chunk->map[i++] = head;
444 }
445 if (tail) {
446 chunk->map[i++] -= tail;
447 chunk->map[i] = tail;
448 }
449}
450
451/**
452 * pcpu_alloc_area - allocate area from a pcpu_chunk
453 * @chunk: chunk of interest
454 * @size: wanted size in bytes
455 * @align: wanted align
456 *
457 * Try to allocate @size bytes area aligned at @align from @chunk.
458 * Note that this function only allocates the offset. It doesn't
459 * populate or map the area.
460 *
461 * @chunk->map must have at least two free slots.
462 *
463 * CONTEXT:
464 * pcpu_lock.
465 *
466 * RETURNS:
467 * Allocated offset in @chunk on success, -1 if no matching area is
468 * found.
469 */
470static int pcpu_alloc_area(struct pcpu_chunk *chunk, int size, int align)
471{
472 int oslot = pcpu_chunk_slot(chunk);
473 int max_contig = 0;
474 int i, off;
475
476 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++])) {
477 bool is_last = i + 1 == chunk->map_used;
478 int head, tail;
479
480 /* extra for alignment requirement */
481 head = ALIGN(off, align) - off;
482 BUG_ON(i == 0 && head != 0);
483
484 if (chunk->map[i] < 0)
485 continue;
486 if (chunk->map[i] < head + size) {
487 max_contig = max(chunk->map[i], max_contig);
488 continue;
489 }
490
491 /*
492 * If head is small or the previous block is free,
493 * merge'em. Note that 'small' is defined as smaller
494 * than sizeof(int), which is very small but isn't too
495 * uncommon for percpu allocations.
496 */
497 if (head && (head < sizeof(int) || chunk->map[i - 1] > 0)) {
498 if (chunk->map[i - 1] > 0)
499 chunk->map[i - 1] += head;
500 else {
501 chunk->map[i - 1] -= head;
502 chunk->free_size -= head;
503 }
504 chunk->map[i] -= head;
505 off += head;
506 head = 0;
507 }
508
509 /* if tail is small, just keep it around */
510 tail = chunk->map[i] - head - size;
511 if (tail < sizeof(int))
512 tail = 0;
513
514 /* split if warranted */
515 if (head || tail) {
516 pcpu_split_block(chunk, i, head, tail);
517 if (head) {
518 i++;
519 off += head;
520 max_contig = max(chunk->map[i - 1], max_contig);
521 }
522 if (tail)
523 max_contig = max(chunk->map[i + 1], max_contig);
524 }
525
526 /* update hint and mark allocated */
527 if (is_last)
528 chunk->contig_hint = max_contig; /* fully scanned */
529 else
530 chunk->contig_hint = max(chunk->contig_hint,
531 max_contig);
532
533 chunk->free_size -= chunk->map[i];
534 chunk->map[i] = -chunk->map[i];
535
536 pcpu_chunk_relocate(chunk, oslot);
537 return off;
538 }
539
540 chunk->contig_hint = max_contig; /* fully scanned */
541 pcpu_chunk_relocate(chunk, oslot);
542
543 /* tell the upper layer that this chunk has no matching area */
544 return -1;
545}
546
547/**
548 * pcpu_free_area - free area to a pcpu_chunk
549 * @chunk: chunk of interest
550 * @freeme: offset of area to free
551 *
552 * Free area starting from @freeme to @chunk. Note that this function
553 * only modifies the allocation map. It doesn't depopulate or unmap
554 * the area.
555 *
556 * CONTEXT:
557 * pcpu_lock.
558 */
559static void pcpu_free_area(struct pcpu_chunk *chunk, int freeme)
560{
561 int oslot = pcpu_chunk_slot(chunk);
562 int i, off;
563
564 for (i = 0, off = 0; i < chunk->map_used; off += abs(chunk->map[i++]))
565 if (off == freeme)
566 break;
567 BUG_ON(off != freeme);
568 BUG_ON(chunk->map[i] > 0);
569
570 chunk->map[i] = -chunk->map[i];
571 chunk->free_size += chunk->map[i];
572
573 /* merge with previous? */
574 if (i > 0 && chunk->map[i - 1] >= 0) {
575 chunk->map[i - 1] += chunk->map[i];
576 chunk->map_used--;
577 memmove(&chunk->map[i], &chunk->map[i + 1],
578 (chunk->map_used - i) * sizeof(chunk->map[0]));
579 i--;
580 }
581 /* merge with next? */
582 if (i + 1 < chunk->map_used && chunk->map[i + 1] >= 0) {
583 chunk->map[i] += chunk->map[i + 1];
584 chunk->map_used--;
585 memmove(&chunk->map[i + 1], &chunk->map[i + 2],
586 (chunk->map_used - (i + 1)) * sizeof(chunk->map[0]));
587 }
588
589 chunk->contig_hint = max(chunk->map[i], chunk->contig_hint);
590 pcpu_chunk_relocate(chunk, oslot);
591}
592
593/**
594 * pcpu_unmap - unmap pages out of a pcpu_chunk
595 * @chunk: chunk of interest
596 * @page_start: page index of the first page to unmap
597 * @page_end: page index of the last page to unmap + 1
598 * @flush: whether to flush cache and tlb or not
599 *
600 * For each cpu, unmap pages [@page_start,@page_end) out of @chunk.
601 * If @flush is true, vcache is flushed before unmapping and tlb
602 * after.
603 */
604static void pcpu_unmap(struct pcpu_chunk *chunk, int page_start, int page_end,
605 bool flush)
606{
607 unsigned int last = num_possible_cpus() - 1;
608 unsigned int cpu;
609
610 /* unmap must not be done on immutable chunk */
611 WARN_ON(chunk->immutable);
612
613 /*
614 * Each flushing trial can be very expensive, issue flush on
615 * the whole region at once rather than doing it for each cpu.
616 * This could be an overkill but is more scalable.
617 */
618 if (flush)
619 flush_cache_vunmap(pcpu_chunk_addr(chunk, 0, page_start),
620 pcpu_chunk_addr(chunk, last, page_end));
621
622 for_each_possible_cpu(cpu)
623 unmap_kernel_range_noflush(
624 pcpu_chunk_addr(chunk, cpu, page_start),
625 (page_end - page_start) << PAGE_SHIFT);
626
627 /* ditto as flush_cache_vunmap() */
628 if (flush)
629 flush_tlb_kernel_range(pcpu_chunk_addr(chunk, 0, page_start),
630 pcpu_chunk_addr(chunk, last, page_end));
631}
632
633/**
634 * pcpu_depopulate_chunk - depopulate and unmap an area of a pcpu_chunk
635 * @chunk: chunk to depopulate
636 * @off: offset to the area to depopulate
637 * @size: size of the area to depopulate in bytes
638 * @flush: whether to flush cache and tlb or not
639 *
640 * For each cpu, depopulate and unmap pages [@page_start,@page_end)
641 * from @chunk. If @flush is true, vcache is flushed before unmapping
642 * and tlb after.
643 *
644 * CONTEXT:
645 * pcpu_alloc_mutex.
646 */
647static void pcpu_depopulate_chunk(struct pcpu_chunk *chunk, int off, int size,
648 bool flush)
649{
650 int page_start = PFN_DOWN(off);
651 int page_end = PFN_UP(off + size);
652 int unmap_start = -1;
653 int uninitialized_var(unmap_end);
654 unsigned int cpu;
655 int i;
656
657 for (i = page_start; i < page_end; i++) {
658 for_each_possible_cpu(cpu) {
659 struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
660
661 if (!*pagep)
662 continue;
663
664 __free_page(*pagep);
665
666 /*
667 * If it's partial depopulation, it might get
668 * populated or depopulated again. Mark the
669 * page gone.
670 */
671 *pagep = NULL;
672
673 unmap_start = unmap_start < 0 ? i : unmap_start;
674 unmap_end = i + 1;
675 }
676 }
677
678 if (unmap_start >= 0)
679 pcpu_unmap(chunk, unmap_start, unmap_end, flush);
680}
681
682/**
683 * pcpu_map - map pages into a pcpu_chunk
684 * @chunk: chunk of interest
685 * @page_start: page index of the first page to map
686 * @page_end: page index of the last page to map + 1
687 *
688 * For each cpu, map pages [@page_start,@page_end) into @chunk.
689 * vcache is flushed afterwards.
690 */
691static int pcpu_map(struct pcpu_chunk *chunk, int page_start, int page_end)
692{
693 unsigned int last = num_possible_cpus() - 1;
694 unsigned int cpu;
695 int err;
696
697 /* map must not be done on immutable chunk */
698 WARN_ON(chunk->immutable);
699
700 for_each_possible_cpu(cpu) {
701 err = map_kernel_range_noflush(
702 pcpu_chunk_addr(chunk, cpu, page_start),
703 (page_end - page_start) << PAGE_SHIFT,
704 PAGE_KERNEL,
705 pcpu_chunk_pagep(chunk, cpu, page_start));
706 if (err < 0)
707 return err;
708 }
709
710 /* flush at once, please read comments in pcpu_unmap() */
711 flush_cache_vmap(pcpu_chunk_addr(chunk, 0, page_start),
712 pcpu_chunk_addr(chunk, last, page_end));
713 return 0;
714}
715
716/**
717 * pcpu_populate_chunk - populate and map an area of a pcpu_chunk
718 * @chunk: chunk of interest
719 * @off: offset to the area to populate
720 * @size: size of the area to populate in bytes
721 *
722 * For each cpu, populate and map pages [@page_start,@page_end) into
723 * @chunk. The area is cleared on return.
724 *
725 * CONTEXT:
726 * pcpu_alloc_mutex, does GFP_KERNEL allocation.
727 */
728static int pcpu_populate_chunk(struct pcpu_chunk *chunk, int off, int size)
729{
730 const gfp_t alloc_mask = GFP_KERNEL | __GFP_HIGHMEM | __GFP_COLD;
731 int page_start = PFN_DOWN(off);
732 int page_end = PFN_UP(off + size);
733 int map_start = -1;
734 int uninitialized_var(map_end);
735 unsigned int cpu;
736 int i;
737
738 for (i = page_start; i < page_end; i++) {
739 if (pcpu_chunk_page_occupied(chunk, i)) {
740 if (map_start >= 0) {
741 if (pcpu_map(chunk, map_start, map_end))
742 goto err;
743 map_start = -1;
744 }
745 continue;
746 }
747
748 map_start = map_start < 0 ? i : map_start;
749 map_end = i + 1;
750
751 for_each_possible_cpu(cpu) {
752 struct page **pagep = pcpu_chunk_pagep(chunk, cpu, i);
753
754 *pagep = alloc_pages_node(cpu_to_node(cpu),
755 alloc_mask, 0);
756 if (!*pagep)
757 goto err;
758 }
759 }
760
761 if (map_start >= 0 && pcpu_map(chunk, map_start, map_end))
762 goto err;
763
764 for_each_possible_cpu(cpu)
765 memset(chunk->vm->addr + cpu * pcpu_unit_size + off, 0,
766 size);
767
768 return 0;
769err:
770 /* likely under heavy memory pressure, give memory back */
771 pcpu_depopulate_chunk(chunk, off, size, true);
772 return -ENOMEM;
773}
774
775static void free_pcpu_chunk(struct pcpu_chunk *chunk)
776{
777 if (!chunk)
778 return;
779 if (chunk->vm)
780 free_vm_area(chunk->vm);
781 pcpu_mem_free(chunk->map, chunk->map_alloc * sizeof(chunk->map[0]));
782 kfree(chunk);
783}
784
785static struct pcpu_chunk *alloc_pcpu_chunk(void)
786{
787 struct pcpu_chunk *chunk;
788
789 chunk = kzalloc(pcpu_chunk_struct_size, GFP_KERNEL);
790 if (!chunk)
791 return NULL;
792
793 chunk->map = pcpu_mem_alloc(PCPU_DFL_MAP_ALLOC * sizeof(chunk->map[0]));
794 chunk->map_alloc = PCPU_DFL_MAP_ALLOC;
795 chunk->map[chunk->map_used++] = pcpu_unit_size;
796 chunk->page = chunk->page_ar;
797
798 chunk->vm = get_vm_area(pcpu_chunk_size, GFP_KERNEL);
799 if (!chunk->vm) {
800 free_pcpu_chunk(chunk);
801 return NULL;
802 }
803
804 INIT_LIST_HEAD(&chunk->list);
805 chunk->free_size = pcpu_unit_size;
806 chunk->contig_hint = pcpu_unit_size;
807
808 return chunk;
809}
810
811/**
812 * pcpu_alloc - the percpu allocator
813 * @size: size of area to allocate in bytes
814 * @align: alignment of area (max PAGE_SIZE)
815 * @reserved: allocate from the reserved chunk if available
816 *
817 * Allocate percpu area of @size bytes aligned at @align.
818 *
819 * CONTEXT:
820 * Does GFP_KERNEL allocation.
821 *
822 * RETURNS:
823 * Percpu pointer to the allocated area on success, NULL on failure.
824 */
825static void *pcpu_alloc(size_t size, size_t align, bool reserved)
826{
827 struct pcpu_chunk *chunk;
828 int slot, off;
829
830 if (unlikely(!size || size > PCPU_MIN_UNIT_SIZE || align > PAGE_SIZE)) {
831 WARN(true, "illegal size (%zu) or align (%zu) for "
832 "percpu allocation\n", size, align);
833 return NULL;
834 }
835
836 mutex_lock(&pcpu_alloc_mutex);
837 spin_lock_irq(&pcpu_lock);
838
839 /* serve reserved allocations from the reserved chunk if available */
840 if (reserved && pcpu_reserved_chunk) {
841 chunk = pcpu_reserved_chunk;
842 if (size > chunk->contig_hint ||
843 pcpu_extend_area_map(chunk) < 0)
844 goto fail_unlock;
845 off = pcpu_alloc_area(chunk, size, align);
846 if (off >= 0)
847 goto area_found;
848 goto fail_unlock;
849 }
850
851restart:
852 /* search through normal chunks */
853 for (slot = pcpu_size_to_slot(size); slot < pcpu_nr_slots; slot++) {
854 list_for_each_entry(chunk, &pcpu_slot[slot], list) {
855 if (size > chunk->contig_hint)
856 continue;
857
858 switch (pcpu_extend_area_map(chunk)) {
859 case 0:
860 break;
861 case 1:
862 goto restart; /* pcpu_lock dropped, restart */
863 default:
864 goto fail_unlock;
865 }
866
867 off = pcpu_alloc_area(chunk, size, align);
868 if (off >= 0)
869 goto area_found;
870 }
871 }
872
873 /* hmmm... no space left, create a new chunk */
874 spin_unlock_irq(&pcpu_lock);
875
876 chunk = alloc_pcpu_chunk();
877 if (!chunk)
878 goto fail_unlock_mutex;
879
880 spin_lock_irq(&pcpu_lock);
881 pcpu_chunk_relocate(chunk, -1);
882 pcpu_chunk_addr_insert(chunk);
883 goto restart;
884
885area_found:
886 spin_unlock_irq(&pcpu_lock);
887
888 /* populate, map and clear the area */
889 if (pcpu_populate_chunk(chunk, off, size)) {
890 spin_lock_irq(&pcpu_lock);
891 pcpu_free_area(chunk, off);
892 goto fail_unlock;
893 }
894
895 mutex_unlock(&pcpu_alloc_mutex);
896
897 return __addr_to_pcpu_ptr(chunk->vm->addr + off);
898
899fail_unlock:
900 spin_unlock_irq(&pcpu_lock);
901fail_unlock_mutex:
902 mutex_unlock(&pcpu_alloc_mutex);
903 return NULL;
904}
905
906/**
907 * __alloc_percpu - allocate dynamic percpu area
908 * @size: size of area to allocate in bytes
909 * @align: alignment of area (max PAGE_SIZE)
910 *
911 * Allocate percpu area of @size bytes aligned at @align. Might
912 * sleep. Might trigger writeouts.
913 *
914 * CONTEXT:
915 * Does GFP_KERNEL allocation.
916 *
917 * RETURNS:
918 * Percpu pointer to the allocated area on success, NULL on failure.
919 */
920void *__alloc_percpu(size_t size, size_t align)
921{
922 return pcpu_alloc(size, align, false);
923}
924EXPORT_SYMBOL_GPL(__alloc_percpu);
925
926/**
927 * __alloc_reserved_percpu - allocate reserved percpu area
928 * @size: size of area to allocate in bytes
929 * @align: alignment of area (max PAGE_SIZE)
930 *
931 * Allocate percpu area of @size bytes aligned at @align from reserved
932 * percpu area if arch has set it up; otherwise, allocation is served
933 * from the same dynamic area. Might sleep. Might trigger writeouts.
934 *
935 * CONTEXT:
936 * Does GFP_KERNEL allocation.
937 *
938 * RETURNS:
939 * Percpu pointer to the allocated area on success, NULL on failure.
940 */
941void *__alloc_reserved_percpu(size_t size, size_t align)
942{
943 return pcpu_alloc(size, align, true);
944}
945
946/**
947 * pcpu_reclaim - reclaim fully free chunks, workqueue function
948 * @work: unused
949 *
950 * Reclaim all fully free chunks except for the first one.
951 *
952 * CONTEXT:
953 * workqueue context.
954 */
955static void pcpu_reclaim(struct work_struct *work)
956{
957 LIST_HEAD(todo);
958 struct list_head *head = &pcpu_slot[pcpu_nr_slots - 1];
959 struct pcpu_chunk *chunk, *next;
960
961 mutex_lock(&pcpu_alloc_mutex);
962 spin_lock_irq(&pcpu_lock);
963
964 list_for_each_entry_safe(chunk, next, head, list) {
965 WARN_ON(chunk->immutable);
966
967 /* spare the first one */
968 if (chunk == list_first_entry(head, struct pcpu_chunk, list))
969 continue;
970
971 rb_erase(&chunk->rb_node, &pcpu_addr_root);
972 list_move(&chunk->list, &todo);
973 }
974
975 spin_unlock_irq(&pcpu_lock);
976 mutex_unlock(&pcpu_alloc_mutex);
977
978 list_for_each_entry_safe(chunk, next, &todo, list) {
979 pcpu_depopulate_chunk(chunk, 0, pcpu_unit_size, false);
980 free_pcpu_chunk(chunk);
981 }
982}
983
984/**
985 * free_percpu - free percpu area
986 * @ptr: pointer to area to free
987 *
988 * Free percpu area @ptr.
989 *
990 * CONTEXT:
991 * Can be called from atomic context.
992 */
993void free_percpu(void *ptr)
994{
995 void *addr = __pcpu_ptr_to_addr(ptr);
996 struct pcpu_chunk *chunk;
997 unsigned long flags;
998 int off;
999
1000 if (!ptr)
1001 return;
1002
1003 spin_lock_irqsave(&pcpu_lock, flags);
1004
1005 chunk = pcpu_chunk_addr_search(addr);
1006 off = addr - chunk->vm->addr;
1007
1008 pcpu_free_area(chunk, off);
1009
1010 /* if there are more than one fully free chunks, wake up grim reaper */
1011 if (chunk->free_size == pcpu_unit_size) {
1012 struct pcpu_chunk *pos;
1013
1014 list_for_each_entry(pos, &pcpu_slot[pcpu_nr_slots - 1], list)
1015 if (pos != chunk) {
1016 schedule_work(&pcpu_reclaim_work);
1017 break;
1018 }
1019 }
1020
1021 spin_unlock_irqrestore(&pcpu_lock, flags);
1022}
1023EXPORT_SYMBOL_GPL(free_percpu);
1024
1025/**
1026 * pcpu_setup_first_chunk - initialize the first percpu chunk
1027 * @get_page_fn: callback to fetch page pointer
1028 * @static_size: the size of static percpu area in bytes
1029 * @reserved_size: the size of reserved percpu area in bytes
1030 * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
1031 * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
1032 * @base_addr: mapped address, NULL for auto
1033 * @populate_pte_fn: callback to allocate pagetable, NULL if unnecessary
1034 *
1035 * Initialize the first percpu chunk which contains the kernel static
1036 * perpcu area. This function is to be called from arch percpu area
1037 * setup path. The first two parameters are mandatory. The rest are
1038 * optional.
1039 *
1040 * @get_page_fn() should return pointer to percpu page given cpu
1041 * number and page number. It should at least return enough pages to
1042 * cover the static area. The returned pages for static area should
1043 * have been initialized with valid data. If @unit_size is specified,
1044 * it can also return pages after the static area. NULL return
1045 * indicates end of pages for the cpu. Note that @get_page_fn() must
1046 * return the same number of pages for all cpus.
1047 *
1048 * @reserved_size, if non-zero, specifies the amount of bytes to
1049 * reserve after the static area in the first chunk. This reserves
1050 * the first chunk such that it's available only through reserved
1051 * percpu allocation. This is primarily used to serve module percpu
1052 * static areas on architectures where the addressing model has
1053 * limited offset range for symbol relocations to guarantee module
1054 * percpu symbols fall inside the relocatable range.
1055 *
1056 * @dyn_size, if non-negative, determines the number of bytes
1057 * available for dynamic allocation in the first chunk. Specifying
1058 * non-negative value makes percpu leave alone the area beyond
1059 * @static_size + @reserved_size + @dyn_size.
1060 *
1061 * @unit_size, if non-negative, specifies unit size and must be
1062 * aligned to PAGE_SIZE and equal to or larger than @static_size +
1063 * @reserved_size + if non-negative, @dyn_size.
1064 *
1065 * Non-null @base_addr means that the caller already allocated virtual
1066 * region for the first chunk and mapped it. percpu must not mess
1067 * with the chunk. Note that @base_addr with 0 @unit_size or non-NULL
1068 * @populate_pte_fn doesn't make any sense.
1069 *
1070 * @populate_pte_fn is used to populate the pagetable. NULL means the
1071 * caller already populated the pagetable.
1072 *
1073 * If the first chunk ends up with both reserved and dynamic areas, it
1074 * is served by two chunks - one to serve the core static and reserved
1075 * areas and the other for the dynamic area. They share the same vm
1076 * and page map but uses different area allocation map to stay away
1077 * from each other. The latter chunk is circulated in the chunk slots
1078 * and available for dynamic allocation like any other chunks.
1079 *
1080 * RETURNS:
1081 * The determined pcpu_unit_size which can be used to initialize
1082 * percpu access.
1083 */
1084size_t __init pcpu_setup_first_chunk(pcpu_get_page_fn_t get_page_fn,
1085 size_t static_size, size_t reserved_size,
1086 ssize_t dyn_size, ssize_t unit_size,
1087 void *base_addr,
1088 pcpu_populate_pte_fn_t populate_pte_fn)
1089{
1090 static struct vm_struct first_vm;
1091 static int smap[2], dmap[2];
1092 size_t size_sum = static_size + reserved_size +
1093 (dyn_size >= 0 ? dyn_size : 0);
1094 struct pcpu_chunk *schunk, *dchunk = NULL;
1095 unsigned int cpu;
1096 int nr_pages;
1097 int err, i;
1098
1099 /* santiy checks */
1100 BUILD_BUG_ON(ARRAY_SIZE(smap) >= PCPU_DFL_MAP_ALLOC ||
1101 ARRAY_SIZE(dmap) >= PCPU_DFL_MAP_ALLOC);
1102 BUG_ON(!static_size);
1103 if (unit_size >= 0) {
1104 BUG_ON(unit_size < size_sum);
1105 BUG_ON(unit_size & ~PAGE_MASK);
1106 BUG_ON(unit_size < PCPU_MIN_UNIT_SIZE);
1107 } else
1108 BUG_ON(base_addr);
1109 BUG_ON(base_addr && populate_pte_fn);
1110
1111 if (unit_size >= 0)
1112 pcpu_unit_pages = unit_size >> PAGE_SHIFT;
1113 else
1114 pcpu_unit_pages = max_t(int, PCPU_MIN_UNIT_SIZE >> PAGE_SHIFT,
1115 PFN_UP(size_sum));
1116
1117 pcpu_unit_size = pcpu_unit_pages << PAGE_SHIFT;
1118 pcpu_chunk_size = num_possible_cpus() * pcpu_unit_size;
1119 pcpu_chunk_struct_size = sizeof(struct pcpu_chunk)
1120 + num_possible_cpus() * pcpu_unit_pages * sizeof(struct page *);
1121
1122 if (dyn_size < 0)
1123 dyn_size = pcpu_unit_size - static_size - reserved_size;
1124
1125 /*
1126 * Allocate chunk slots. The additional last slot is for
1127 * empty chunks.
1128 */
1129 pcpu_nr_slots = __pcpu_size_to_slot(pcpu_unit_size) + 2;
1130 pcpu_slot = alloc_bootmem(pcpu_nr_slots * sizeof(pcpu_slot[0]));
1131 for (i = 0; i < pcpu_nr_slots; i++)
1132 INIT_LIST_HEAD(&pcpu_slot[i]);
1133
1134 /*
1135 * Initialize static chunk. If reserved_size is zero, the
1136 * static chunk covers static area + dynamic allocation area
1137 * in the first chunk. If reserved_size is not zero, it
1138 * covers static area + reserved area (mostly used for module
1139 * static percpu allocation).
1140 */
1141 schunk = alloc_bootmem(pcpu_chunk_struct_size);
1142 INIT_LIST_HEAD(&schunk->list);
1143 schunk->vm = &first_vm;
1144 schunk->map = smap;
1145 schunk->map_alloc = ARRAY_SIZE(smap);
1146 schunk->page = schunk->page_ar;
1147
1148 if (reserved_size) {
1149 schunk->free_size = reserved_size;
1150 pcpu_reserved_chunk = schunk; /* not for dynamic alloc */
1151 } else {
1152 schunk->free_size = dyn_size;
1153 dyn_size = 0; /* dynamic area covered */
1154 }
1155 schunk->contig_hint = schunk->free_size;
1156
1157 schunk->map[schunk->map_used++] = -static_size;
1158 if (schunk->free_size)
1159 schunk->map[schunk->map_used++] = schunk->free_size;
1160
1161 pcpu_reserved_chunk_limit = static_size + schunk->free_size;
1162
1163 /* init dynamic chunk if necessary */
1164 if (dyn_size) {
1165 dchunk = alloc_bootmem(sizeof(struct pcpu_chunk));
1166 INIT_LIST_HEAD(&dchunk->list);
1167 dchunk->vm = &first_vm;
1168 dchunk->map = dmap;
1169 dchunk->map_alloc = ARRAY_SIZE(dmap);
1170 dchunk->page = schunk->page_ar; /* share page map with schunk */
1171
1172 dchunk->contig_hint = dchunk->free_size = dyn_size;
1173 dchunk->map[dchunk->map_used++] = -pcpu_reserved_chunk_limit;
1174 dchunk->map[dchunk->map_used++] = dchunk->free_size;
1175 }
1176
1177 /* allocate vm address */
1178 first_vm.flags = VM_ALLOC;
1179 first_vm.size = pcpu_chunk_size;
1180
1181 if (!base_addr)
1182 vm_area_register_early(&first_vm, PAGE_SIZE);
1183 else {
1184 /*
1185 * Pages already mapped. No need to remap into
1186 * vmalloc area. In this case the first chunks can't
1187 * be mapped or unmapped by percpu and are marked
1188 * immutable.
1189 */
1190 first_vm.addr = base_addr;
1191 schunk->immutable = true;
1192 if (dchunk)
1193 dchunk->immutable = true;
1194 }
1195
1196 /* assign pages */
1197 nr_pages = -1;
1198 for_each_possible_cpu(cpu) {
1199 for (i = 0; i < pcpu_unit_pages; i++) {
1200 struct page *page = get_page_fn(cpu, i);
1201
1202 if (!page)
1203 break;
1204 *pcpu_chunk_pagep(schunk, cpu, i) = page;
1205 }
1206
1207 BUG_ON(i < PFN_UP(static_size));
1208
1209 if (nr_pages < 0)
1210 nr_pages = i;
1211 else
1212 BUG_ON(nr_pages != i);
1213 }
1214
1215 /* map them */
1216 if (populate_pte_fn) {
1217 for_each_possible_cpu(cpu)
1218 for (i = 0; i < nr_pages; i++)
1219 populate_pte_fn(pcpu_chunk_addr(schunk,
1220 cpu, i));
1221
1222 err = pcpu_map(schunk, 0, nr_pages);
1223 if (err)
1224 panic("failed to setup static percpu area, err=%d\n",
1225 err);
1226 }
1227
1228 /* link the first chunk in */
1229 if (!dchunk) {
1230 pcpu_chunk_relocate(schunk, -1);
1231 pcpu_chunk_addr_insert(schunk);
1232 } else {
1233 pcpu_chunk_relocate(dchunk, -1);
1234 pcpu_chunk_addr_insert(dchunk);
1235 }
1236
1237 /* we're done */
1238 pcpu_base_addr = (void *)pcpu_chunk_addr(schunk, 0, 0);
1239 return pcpu_unit_size;
1240}
1241
1242/*
1243 * Embedding first chunk setup helper.
1244 */
1245static void *pcpue_ptr __initdata;
1246static size_t pcpue_size __initdata;
1247static size_t pcpue_unit_size __initdata;
1248
1249static struct page * __init pcpue_get_page(unsigned int cpu, int pageno)
1250{
1251 size_t off = (size_t)pageno << PAGE_SHIFT;
1252
1253 if (off >= pcpue_size)
1254 return NULL;
1255
1256 return virt_to_page(pcpue_ptr + cpu * pcpue_unit_size + off);
1257}
1258
1259/**
1260 * pcpu_embed_first_chunk - embed the first percpu chunk into bootmem
1261 * @static_size: the size of static percpu area in bytes
1262 * @reserved_size: the size of reserved percpu area in bytes
1263 * @dyn_size: free size for dynamic allocation in bytes, -1 for auto
1264 * @unit_size: unit size in bytes, must be multiple of PAGE_SIZE, -1 for auto
1265 *
1266 * This is a helper to ease setting up embedded first percpu chunk and
1267 * can be called where pcpu_setup_first_chunk() is expected.
1268 *
1269 * If this function is used to setup the first chunk, it is allocated
1270 * as a contiguous area using bootmem allocator and used as-is without
1271 * being mapped into vmalloc area. This enables the first chunk to
1272 * piggy back on the linear physical mapping which often uses larger
1273 * page size.
1274 *
1275 * When @dyn_size is positive, dynamic area might be larger than
1276 * specified to fill page alignment. Also, when @dyn_size is auto,
1277 * @dyn_size does not fill the whole first chunk but only what's
1278 * necessary for page alignment after static and reserved areas.
1279 *
1280 * If the needed size is smaller than the minimum or specified unit
1281 * size, the leftover is returned to the bootmem allocator.
1282 *
1283 * RETURNS:
1284 * The determined pcpu_unit_size which can be used to initialize
1285 * percpu access on success, -errno on failure.
1286 */
1287ssize_t __init pcpu_embed_first_chunk(size_t static_size, size_t reserved_size,
1288 ssize_t dyn_size, ssize_t unit_size)
1289{
1290 unsigned int cpu;
1291
1292 /* determine parameters and allocate */
1293 pcpue_size = PFN_ALIGN(static_size + reserved_size +
1294 (dyn_size >= 0 ? dyn_size : 0));
1295 if (dyn_size != 0)
1296 dyn_size = pcpue_size - static_size - reserved_size;
1297
1298 if (unit_size >= 0) {
1299 BUG_ON(unit_size < pcpue_size);
1300 pcpue_unit_size = unit_size;
1301 } else
1302 pcpue_unit_size = max_t(size_t, pcpue_size, PCPU_MIN_UNIT_SIZE);
1303
1304 pcpue_ptr = __alloc_bootmem_nopanic(
1305 num_possible_cpus() * pcpue_unit_size,
1306 PAGE_SIZE, __pa(MAX_DMA_ADDRESS));
1307 if (!pcpue_ptr)
1308 return -ENOMEM;
1309
1310 /* return the leftover and copy */
1311 for_each_possible_cpu(cpu) {
1312 void *ptr = pcpue_ptr + cpu * pcpue_unit_size;
1313
1314 free_bootmem(__pa(ptr + pcpue_size),
1315 pcpue_unit_size - pcpue_size);
1316 memcpy(ptr, __per_cpu_load, static_size);
1317 }
1318
1319 /* we're ready, commit */
1320 pr_info("PERCPU: Embedded %zu pages at %p, static data %zu bytes\n",
1321 pcpue_size >> PAGE_SHIFT, pcpue_ptr, static_size);
1322
1323 return pcpu_setup_first_chunk(pcpue_get_page, static_size,
1324 reserved_size, dyn_size,
1325 pcpue_unit_size, pcpue_ptr, NULL);
1326}
diff --git a/mm/readahead.c b/mm/readahead.c
index bec83c15a78f..9ce303d4b810 100644
--- a/mm/readahead.c
+++ b/mm/readahead.c
@@ -17,19 +17,6 @@
17#include <linux/pagevec.h> 17#include <linux/pagevec.h>
18#include <linux/pagemap.h> 18#include <linux/pagemap.h>
19 19
20void default_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
21{
22}
23EXPORT_SYMBOL(default_unplug_io_fn);
24
25struct backing_dev_info default_backing_dev_info = {
26 .ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_CACHE_SIZE,
27 .state = 0,
28 .capabilities = BDI_CAP_MAP_COPY,
29 .unplug_io_fn = default_unplug_io_fn,
30};
31EXPORT_SYMBOL_GPL(default_backing_dev_info);
32
33/* 20/*
34 * Initialise a struct file's readahead state. Assumes that the caller has 21 * Initialise a struct file's readahead state. Assumes that the caller has
35 * memset *ra to zero. 22 * memset *ra to zero.
@@ -233,18 +220,6 @@ unsigned long max_sane_readahead(unsigned long nr)
233 + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2); 220 + node_page_state(numa_node_id(), NR_FREE_PAGES)) / 2);
234} 221}
235 222
236static int __init readahead_init(void)
237{
238 int err;
239
240 err = bdi_init(&default_backing_dev_info);
241 if (!err)
242 bdi_register(&default_backing_dev_info, NULL, "default");
243
244 return err;
245}
246subsys_initcall(readahead_init);
247
248/* 223/*
249 * Submit IO for the read-ahead request in file_ra_state. 224 * Submit IO for the read-ahead request in file_ra_state.
250 */ 225 */
diff --git a/mm/shmem.c b/mm/shmem.c
index 4103a239ce84..7ec78e24a30d 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -28,6 +28,7 @@
28#include <linux/mm.h> 28#include <linux/mm.h>
29#include <linux/module.h> 29#include <linux/module.h>
30#include <linux/swap.h> 30#include <linux/swap.h>
31#include <linux/ima.h>
31 32
32static struct vfsmount *shm_mnt; 33static struct vfsmount *shm_mnt;
33 34
@@ -2665,6 +2666,7 @@ int shmem_zero_setup(struct vm_area_struct *vma)
2665 if (IS_ERR(file)) 2666 if (IS_ERR(file))
2666 return PTR_ERR(file); 2667 return PTR_ERR(file);
2667 2668
2669 ima_shm_check(file);
2668 if (vma->vm_file) 2670 if (vma->vm_file)
2669 fput(vma->vm_file); 2671 fput(vma->vm_file);
2670 vma->vm_file = file; 2672 vma->vm_file = file;
diff --git a/mm/slob.c b/mm/slob.c
index 52bc8a2bd9ef..0bfa680a8981 100644
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -126,9 +126,9 @@ static LIST_HEAD(free_slob_medium);
126static LIST_HEAD(free_slob_large); 126static LIST_HEAD(free_slob_large);
127 127
128/* 128/*
129 * slob_page: True for all slob pages (false for bigblock pages) 129 * is_slob_page: True for all slob pages (false for bigblock pages)
130 */ 130 */
131static inline int slob_page(struct slob_page *sp) 131static inline int is_slob_page(struct slob_page *sp)
132{ 132{
133 return PageSlobPage((struct page *)sp); 133 return PageSlobPage((struct page *)sp);
134} 134}
@@ -143,6 +143,11 @@ static inline void clear_slob_page(struct slob_page *sp)
143 __ClearPageSlobPage((struct page *)sp); 143 __ClearPageSlobPage((struct page *)sp);
144} 144}
145 145
146static inline struct slob_page *slob_page(const void *addr)
147{
148 return (struct slob_page *)virt_to_page(addr);
149}
150
146/* 151/*
147 * slob_page_free: true for pages on free_slob_pages list. 152 * slob_page_free: true for pages on free_slob_pages list.
148 */ 153 */
@@ -230,7 +235,7 @@ static int slob_last(slob_t *s)
230 return !((unsigned long)slob_next(s) & ~PAGE_MASK); 235 return !((unsigned long)slob_next(s) & ~PAGE_MASK);
231} 236}
232 237
233static void *slob_new_page(gfp_t gfp, int order, int node) 238static void *slob_new_pages(gfp_t gfp, int order, int node)
234{ 239{
235 void *page; 240 void *page;
236 241
@@ -247,12 +252,17 @@ static void *slob_new_page(gfp_t gfp, int order, int node)
247 return page_address(page); 252 return page_address(page);
248} 253}
249 254
255static void slob_free_pages(void *b, int order)
256{
257 free_pages((unsigned long)b, order);
258}
259
250/* 260/*
251 * Allocate a slob block within a given slob_page sp. 261 * Allocate a slob block within a given slob_page sp.
252 */ 262 */
253static void *slob_page_alloc(struct slob_page *sp, size_t size, int align) 263static void *slob_page_alloc(struct slob_page *sp, size_t size, int align)
254{ 264{
255 slob_t *prev, *cur, *aligned = 0; 265 slob_t *prev, *cur, *aligned = NULL;
256 int delta = 0, units = SLOB_UNITS(size); 266 int delta = 0, units = SLOB_UNITS(size);
257 267
258 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) { 268 for (prev = NULL, cur = sp->free; ; prev = cur, cur = slob_next(cur)) {
@@ -349,10 +359,10 @@ static void *slob_alloc(size_t size, gfp_t gfp, int align, int node)
349 359
350 /* Not enough space: must allocate a new page */ 360 /* Not enough space: must allocate a new page */
351 if (!b) { 361 if (!b) {
352 b = slob_new_page(gfp & ~__GFP_ZERO, 0, node); 362 b = slob_new_pages(gfp & ~__GFP_ZERO, 0, node);
353 if (!b) 363 if (!b)
354 return 0; 364 return NULL;
355 sp = (struct slob_page *)virt_to_page(b); 365 sp = slob_page(b);
356 set_slob_page(sp); 366 set_slob_page(sp);
357 367
358 spin_lock_irqsave(&slob_lock, flags); 368 spin_lock_irqsave(&slob_lock, flags);
@@ -384,7 +394,7 @@ static void slob_free(void *block, int size)
384 return; 394 return;
385 BUG_ON(!size); 395 BUG_ON(!size);
386 396
387 sp = (struct slob_page *)virt_to_page(block); 397 sp = slob_page(block);
388 units = SLOB_UNITS(size); 398 units = SLOB_UNITS(size);
389 399
390 spin_lock_irqsave(&slob_lock, flags); 400 spin_lock_irqsave(&slob_lock, flags);
@@ -393,10 +403,11 @@ static void slob_free(void *block, int size)
393 /* Go directly to page allocator. Do not pass slob allocator */ 403 /* Go directly to page allocator. Do not pass slob allocator */
394 if (slob_page_free(sp)) 404 if (slob_page_free(sp))
395 clear_slob_page_free(sp); 405 clear_slob_page_free(sp);
406 spin_unlock_irqrestore(&slob_lock, flags);
396 clear_slob_page(sp); 407 clear_slob_page(sp);
397 free_slob_page(sp); 408 free_slob_page(sp);
398 free_page((unsigned long)b); 409 free_page((unsigned long)b);
399 goto out; 410 return;
400 } 411 }
401 412
402 if (!slob_page_free(sp)) { 413 if (!slob_page_free(sp)) {
@@ -476,7 +487,7 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node)
476 } else { 487 } else {
477 void *ret; 488 void *ret;
478 489
479 ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node); 490 ret = slob_new_pages(gfp | __GFP_COMP, get_order(size), node);
480 if (ret) { 491 if (ret) {
481 struct page *page; 492 struct page *page;
482 page = virt_to_page(ret); 493 page = virt_to_page(ret);
@@ -494,8 +505,8 @@ void kfree(const void *block)
494 if (unlikely(ZERO_OR_NULL_PTR(block))) 505 if (unlikely(ZERO_OR_NULL_PTR(block)))
495 return; 506 return;
496 507
497 sp = (struct slob_page *)virt_to_page(block); 508 sp = slob_page(block);
498 if (slob_page(sp)) { 509 if (is_slob_page(sp)) {
499 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); 510 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
500 unsigned int *m = (unsigned int *)(block - align); 511 unsigned int *m = (unsigned int *)(block - align);
501 slob_free(m, *m + align); 512 slob_free(m, *m + align);
@@ -513,8 +524,8 @@ size_t ksize(const void *block)
513 if (unlikely(block == ZERO_SIZE_PTR)) 524 if (unlikely(block == ZERO_SIZE_PTR))
514 return 0; 525 return 0;
515 526
516 sp = (struct slob_page *)virt_to_page(block); 527 sp = slob_page(block);
517 if (slob_page(sp)) { 528 if (is_slob_page(sp)) {
518 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); 529 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
519 unsigned int *m = (unsigned int *)(block - align); 530 unsigned int *m = (unsigned int *)(block - align);
520 return SLOB_UNITS(*m) * SLOB_UNIT; 531 return SLOB_UNITS(*m) * SLOB_UNIT;
@@ -573,7 +584,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
573 if (c->size < PAGE_SIZE) 584 if (c->size < PAGE_SIZE)
574 b = slob_alloc(c->size, flags, c->align, node); 585 b = slob_alloc(c->size, flags, c->align, node);
575 else 586 else
576 b = slob_new_page(flags, get_order(c->size), node); 587 b = slob_new_pages(flags, get_order(c->size), node);
577 588
578 if (c->ctor) 589 if (c->ctor)
579 c->ctor(b); 590 c->ctor(b);
@@ -587,7 +598,7 @@ static void __kmem_cache_free(void *b, int size)
587 if (size < PAGE_SIZE) 598 if (size < PAGE_SIZE)
588 slob_free(b, size); 599 slob_free(b, size);
589 else 600 else
590 free_pages((unsigned long)b, get_order(size)); 601 slob_free_pages(b, get_order(size));
591} 602}
592 603
593static void kmem_rcu_free(struct rcu_head *head) 604static void kmem_rcu_free(struct rcu_head *head)
diff --git a/mm/slub.c b/mm/slub.c
index 0280eee6cf37..c65a4edafc33 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -374,14 +374,8 @@ static struct track *get_track(struct kmem_cache *s, void *object,
374static void set_track(struct kmem_cache *s, void *object, 374static void set_track(struct kmem_cache *s, void *object,
375 enum track_item alloc, unsigned long addr) 375 enum track_item alloc, unsigned long addr)
376{ 376{
377 struct track *p; 377 struct track *p = get_track(s, object, alloc);
378
379 if (s->offset)
380 p = object + s->offset + sizeof(void *);
381 else
382 p = object + s->inuse;
383 378
384 p += alloc;
385 if (addr) { 379 if (addr) {
386 p->addr = addr; 380 p->addr = addr;
387 p->cpu = smp_processor_id(); 381 p->cpu = smp_processor_id();
@@ -1335,7 +1329,7 @@ static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
1335 n = get_node(s, zone_to_nid(zone)); 1329 n = get_node(s, zone_to_nid(zone));
1336 1330
1337 if (n && cpuset_zone_allowed_hardwall(zone, flags) && 1331 if (n && cpuset_zone_allowed_hardwall(zone, flags) &&
1338 n->nr_partial > n->min_partial) { 1332 n->nr_partial > s->min_partial) {
1339 page = get_partial_node(n); 1333 page = get_partial_node(n);
1340 if (page) 1334 if (page)
1341 return page; 1335 return page;
@@ -1387,7 +1381,7 @@ static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
1387 slab_unlock(page); 1381 slab_unlock(page);
1388 } else { 1382 } else {
1389 stat(c, DEACTIVATE_EMPTY); 1383 stat(c, DEACTIVATE_EMPTY);
1390 if (n->nr_partial < n->min_partial) { 1384 if (n->nr_partial < s->min_partial) {
1391 /* 1385 /*
1392 * Adding an empty slab to the partial slabs in order 1386 * Adding an empty slab to the partial slabs in order
1393 * to avoid page allocator overhead. This slab needs 1387 * to avoid page allocator overhead. This slab needs
@@ -1724,7 +1718,7 @@ static __always_inline void slab_free(struct kmem_cache *s,
1724 c = get_cpu_slab(s, smp_processor_id()); 1718 c = get_cpu_slab(s, smp_processor_id());
1725 debug_check_no_locks_freed(object, c->objsize); 1719 debug_check_no_locks_freed(object, c->objsize);
1726 if (!(s->flags & SLAB_DEBUG_OBJECTS)) 1720 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1727 debug_check_no_obj_freed(object, s->objsize); 1721 debug_check_no_obj_freed(object, c->objsize);
1728 if (likely(page == c->page && c->node >= 0)) { 1722 if (likely(page == c->page && c->node >= 0)) {
1729 object[c->offset] = c->freelist; 1723 object[c->offset] = c->freelist;
1730 c->freelist = object; 1724 c->freelist = object;
@@ -1844,6 +1838,7 @@ static inline int calculate_order(int size)
1844 int order; 1838 int order;
1845 int min_objects; 1839 int min_objects;
1846 int fraction; 1840 int fraction;
1841 int max_objects;
1847 1842
1848 /* 1843 /*
1849 * Attempt to find best configuration for a slab. This 1844 * Attempt to find best configuration for a slab. This
@@ -1856,6 +1851,9 @@ static inline int calculate_order(int size)
1856 min_objects = slub_min_objects; 1851 min_objects = slub_min_objects;
1857 if (!min_objects) 1852 if (!min_objects)
1858 min_objects = 4 * (fls(nr_cpu_ids) + 1); 1853 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1854 max_objects = (PAGE_SIZE << slub_max_order)/size;
1855 min_objects = min(min_objects, max_objects);
1856
1859 while (min_objects > 1) { 1857 while (min_objects > 1) {
1860 fraction = 16; 1858 fraction = 16;
1861 while (fraction >= 4) { 1859 while (fraction >= 4) {
@@ -1865,7 +1863,7 @@ static inline int calculate_order(int size)
1865 return order; 1863 return order;
1866 fraction /= 2; 1864 fraction /= 2;
1867 } 1865 }
1868 min_objects /= 2; 1866 min_objects --;
1869 } 1867 }
1870 1868
1871 /* 1869 /*
@@ -1928,17 +1926,6 @@ static void
1928init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s) 1926init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
1929{ 1927{
1930 n->nr_partial = 0; 1928 n->nr_partial = 0;
1931
1932 /*
1933 * The larger the object size is, the more pages we want on the partial
1934 * list to avoid pounding the page allocator excessively.
1935 */
1936 n->min_partial = ilog2(s->size);
1937 if (n->min_partial < MIN_PARTIAL)
1938 n->min_partial = MIN_PARTIAL;
1939 else if (n->min_partial > MAX_PARTIAL)
1940 n->min_partial = MAX_PARTIAL;
1941
1942 spin_lock_init(&n->list_lock); 1929 spin_lock_init(&n->list_lock);
1943 INIT_LIST_HEAD(&n->partial); 1930 INIT_LIST_HEAD(&n->partial);
1944#ifdef CONFIG_SLUB_DEBUG 1931#ifdef CONFIG_SLUB_DEBUG
@@ -2181,6 +2168,15 @@ static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2181} 2168}
2182#endif 2169#endif
2183 2170
2171static void set_min_partial(struct kmem_cache *s, unsigned long min)
2172{
2173 if (min < MIN_PARTIAL)
2174 min = MIN_PARTIAL;
2175 else if (min > MAX_PARTIAL)
2176 min = MAX_PARTIAL;
2177 s->min_partial = min;
2178}
2179
2184/* 2180/*
2185 * calculate_sizes() determines the order and the distribution of data within 2181 * calculate_sizes() determines the order and the distribution of data within
2186 * a slab object. 2182 * a slab object.
@@ -2319,6 +2315,11 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2319 if (!calculate_sizes(s, -1)) 2315 if (!calculate_sizes(s, -1))
2320 goto error; 2316 goto error;
2321 2317
2318 /*
2319 * The larger the object size is, the more pages we want on the partial
2320 * list to avoid pounding the page allocator excessively.
2321 */
2322 set_min_partial(s, ilog2(s->size));
2322 s->refcount = 1; 2323 s->refcount = 1;
2323#ifdef CONFIG_NUMA 2324#ifdef CONFIG_NUMA
2324 s->remote_node_defrag_ratio = 1000; 2325 s->remote_node_defrag_ratio = 1000;
@@ -2475,7 +2476,7 @@ EXPORT_SYMBOL(kmem_cache_destroy);
2475 * Kmalloc subsystem 2476 * Kmalloc subsystem
2476 *******************************************************************/ 2477 *******************************************************************/
2477 2478
2478struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned; 2479struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
2479EXPORT_SYMBOL(kmalloc_caches); 2480EXPORT_SYMBOL(kmalloc_caches);
2480 2481
2481static int __init setup_slub_min_order(char *str) 2482static int __init setup_slub_min_order(char *str)
@@ -2537,7 +2538,7 @@ panic:
2537} 2538}
2538 2539
2539#ifdef CONFIG_ZONE_DMA 2540#ifdef CONFIG_ZONE_DMA
2540static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1]; 2541static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
2541 2542
2542static void sysfs_add_func(struct work_struct *w) 2543static void sysfs_add_func(struct work_struct *w)
2543{ 2544{
@@ -2658,7 +2659,7 @@ void *__kmalloc(size_t size, gfp_t flags)
2658{ 2659{
2659 struct kmem_cache *s; 2660 struct kmem_cache *s;
2660 2661
2661 if (unlikely(size > PAGE_SIZE)) 2662 if (unlikely(size > SLUB_MAX_SIZE))
2662 return kmalloc_large(size, flags); 2663 return kmalloc_large(size, flags);
2663 2664
2664 s = get_slab(size, flags); 2665 s = get_slab(size, flags);
@@ -2686,7 +2687,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
2686{ 2687{
2687 struct kmem_cache *s; 2688 struct kmem_cache *s;
2688 2689
2689 if (unlikely(size > PAGE_SIZE)) 2690 if (unlikely(size > SLUB_MAX_SIZE))
2690 return kmalloc_large_node(size, flags, node); 2691 return kmalloc_large_node(size, flags, node);
2691 2692
2692 s = get_slab(size, flags); 2693 s = get_slab(size, flags);
@@ -2986,7 +2987,7 @@ void __init kmem_cache_init(void)
2986 caches++; 2987 caches++;
2987 } 2988 }
2988 2989
2989 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) { 2990 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
2990 create_kmalloc_cache(&kmalloc_caches[i], 2991 create_kmalloc_cache(&kmalloc_caches[i],
2991 "kmalloc", 1 << i, GFP_KERNEL); 2992 "kmalloc", 1 << i, GFP_KERNEL);
2992 caches++; 2993 caches++;
@@ -3023,7 +3024,7 @@ void __init kmem_cache_init(void)
3023 slab_state = UP; 3024 slab_state = UP;
3024 3025
3025 /* Provide the correct kmalloc names now that the caches are up */ 3026 /* Provide the correct kmalloc names now that the caches are up */
3026 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) 3027 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
3027 kmalloc_caches[i]. name = 3028 kmalloc_caches[i]. name =
3028 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i); 3029 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3029 3030
@@ -3223,7 +3224,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
3223{ 3224{
3224 struct kmem_cache *s; 3225 struct kmem_cache *s;
3225 3226
3226 if (unlikely(size > PAGE_SIZE)) 3227 if (unlikely(size > SLUB_MAX_SIZE))
3227 return kmalloc_large(size, gfpflags); 3228 return kmalloc_large(size, gfpflags);
3228 3229
3229 s = get_slab(size, gfpflags); 3230 s = get_slab(size, gfpflags);
@@ -3239,7 +3240,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3239{ 3240{
3240 struct kmem_cache *s; 3241 struct kmem_cache *s;
3241 3242
3242 if (unlikely(size > PAGE_SIZE)) 3243 if (unlikely(size > SLUB_MAX_SIZE))
3243 return kmalloc_large_node(size, gfpflags, node); 3244 return kmalloc_large_node(size, gfpflags, node);
3244 3245
3245 s = get_slab(size, gfpflags); 3246 s = get_slab(size, gfpflags);
@@ -3836,6 +3837,26 @@ static ssize_t order_show(struct kmem_cache *s, char *buf)
3836} 3837}
3837SLAB_ATTR(order); 3838SLAB_ATTR(order);
3838 3839
3840static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3841{
3842 return sprintf(buf, "%lu\n", s->min_partial);
3843}
3844
3845static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3846 size_t length)
3847{
3848 unsigned long min;
3849 int err;
3850
3851 err = strict_strtoul(buf, 10, &min);
3852 if (err)
3853 return err;
3854
3855 set_min_partial(s, min);
3856 return length;
3857}
3858SLAB_ATTR(min_partial);
3859
3839static ssize_t ctor_show(struct kmem_cache *s, char *buf) 3860static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3840{ 3861{
3841 if (s->ctor) { 3862 if (s->ctor) {
@@ -4151,6 +4172,7 @@ static struct attribute *slab_attrs[] = {
4151 &object_size_attr.attr, 4172 &object_size_attr.attr,
4152 &objs_per_slab_attr.attr, 4173 &objs_per_slab_attr.attr,
4153 &order_attr.attr, 4174 &order_attr.attr,
4175 &min_partial_attr.attr,
4154 &objects_attr.attr, 4176 &objects_attr.attr,
4155 &objects_partial_attr.attr, 4177 &objects_partial_attr.attr,
4156 &total_objects_attr.attr, 4178 &total_objects_attr.attr,
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 520a75980269..af58324c361a 100644
--- a/mm/vmalloc.c
+++ b/mm/vmalloc.c
@@ -24,6 +24,7 @@
24#include <linux/radix-tree.h> 24#include <linux/radix-tree.h>
25#include <linux/rcupdate.h> 25#include <linux/rcupdate.h>
26#include <linux/bootmem.h> 26#include <linux/bootmem.h>
27#include <linux/pfn.h>
27 28
28#include <asm/atomic.h> 29#include <asm/atomic.h>
29#include <asm/uaccess.h> 30#include <asm/uaccess.h>
@@ -152,8 +153,8 @@ static int vmap_pud_range(pgd_t *pgd, unsigned long addr,
152 * 153 *
153 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N] 154 * Ie. pte at addr+N*PAGE_SIZE shall point to pfn corresponding to pages[N]
154 */ 155 */
155static int vmap_page_range(unsigned long start, unsigned long end, 156static int vmap_page_range_noflush(unsigned long start, unsigned long end,
156 pgprot_t prot, struct page **pages) 157 pgprot_t prot, struct page **pages)
157{ 158{
158 pgd_t *pgd; 159 pgd_t *pgd;
159 unsigned long next; 160 unsigned long next;
@@ -169,13 +170,22 @@ static int vmap_page_range(unsigned long start, unsigned long end,
169 if (err) 170 if (err)
170 break; 171 break;
171 } while (pgd++, addr = next, addr != end); 172 } while (pgd++, addr = next, addr != end);
172 flush_cache_vmap(start, end);
173 173
174 if (unlikely(err)) 174 if (unlikely(err))
175 return err; 175 return err;
176 return nr; 176 return nr;
177} 177}
178 178
179static int vmap_page_range(unsigned long start, unsigned long end,
180 pgprot_t prot, struct page **pages)
181{
182 int ret;
183
184 ret = vmap_page_range_noflush(start, end, prot, pages);
185 flush_cache_vmap(start, end);
186 return ret;
187}
188
179static inline int is_vmalloc_or_module_addr(const void *x) 189static inline int is_vmalloc_or_module_addr(const void *x)
180{ 190{
181 /* 191 /*
@@ -990,6 +1000,32 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t pro
990} 1000}
991EXPORT_SYMBOL(vm_map_ram); 1001EXPORT_SYMBOL(vm_map_ram);
992 1002
1003/**
1004 * vm_area_register_early - register vmap area early during boot
1005 * @vm: vm_struct to register
1006 * @align: requested alignment
1007 *
1008 * This function is used to register kernel vm area before
1009 * vmalloc_init() is called. @vm->size and @vm->flags should contain
1010 * proper values on entry and other fields should be zero. On return,
1011 * vm->addr contains the allocated address.
1012 *
1013 * DO NOT USE THIS FUNCTION UNLESS YOU KNOW WHAT YOU'RE DOING.
1014 */
1015void __init vm_area_register_early(struct vm_struct *vm, size_t align)
1016{
1017 static size_t vm_init_off __initdata;
1018 unsigned long addr;
1019
1020 addr = ALIGN(VMALLOC_START + vm_init_off, align);
1021 vm_init_off = PFN_ALIGN(addr + vm->size) - VMALLOC_START;
1022
1023 vm->addr = (void *)addr;
1024
1025 vm->next = vmlist;
1026 vmlist = vm;
1027}
1028
993void __init vmalloc_init(void) 1029void __init vmalloc_init(void)
994{ 1030{
995 struct vmap_area *va; 1031 struct vmap_area *va;
@@ -1017,6 +1053,58 @@ void __init vmalloc_init(void)
1017 vmap_initialized = true; 1053 vmap_initialized = true;
1018} 1054}
1019 1055
1056/**
1057 * map_kernel_range_noflush - map kernel VM area with the specified pages
1058 * @addr: start of the VM area to map
1059 * @size: size of the VM area to map
1060 * @prot: page protection flags to use
1061 * @pages: pages to map
1062 *
1063 * Map PFN_UP(@size) pages at @addr. The VM area @addr and @size
1064 * specify should have been allocated using get_vm_area() and its
1065 * friends.
1066 *
1067 * NOTE:
1068 * This function does NOT do any cache flushing. The caller is
1069 * responsible for calling flush_cache_vmap() on to-be-mapped areas
1070 * before calling this function.
1071 *
1072 * RETURNS:
1073 * The number of pages mapped on success, -errno on failure.
1074 */
1075int map_kernel_range_noflush(unsigned long addr, unsigned long size,
1076 pgprot_t prot, struct page **pages)
1077{
1078 return vmap_page_range_noflush(addr, addr + size, prot, pages);
1079}
1080
1081/**
1082 * unmap_kernel_range_noflush - unmap kernel VM area
1083 * @addr: start of the VM area to unmap
1084 * @size: size of the VM area to unmap
1085 *
1086 * Unmap PFN_UP(@size) pages at @addr. The VM area @addr and @size
1087 * specify should have been allocated using get_vm_area() and its
1088 * friends.
1089 *
1090 * NOTE:
1091 * This function does NOT do any cache flushing. The caller is
1092 * responsible for calling flush_cache_vunmap() on to-be-mapped areas
1093 * before calling this function and flush_tlb_kernel_range() after.
1094 */
1095void unmap_kernel_range_noflush(unsigned long addr, unsigned long size)
1096{
1097 vunmap_page_range(addr, addr + size);
1098}
1099
1100/**
1101 * unmap_kernel_range - unmap kernel VM area and flush cache and TLB
1102 * @addr: start of the VM area to unmap
1103 * @size: size of the VM area to unmap
1104 *
1105 * Similar to unmap_kernel_range_noflush() but flushes vcache before
1106 * the unmapping and tlb after.
1107 */
1020void unmap_kernel_range(unsigned long addr, unsigned long size) 1108void unmap_kernel_range(unsigned long addr, unsigned long size)
1021{ 1109{
1022 unsigned long end = addr + size; 1110 unsigned long end = addr + size;
@@ -1267,6 +1355,7 @@ EXPORT_SYMBOL(vfree);
1267void vunmap(const void *addr) 1355void vunmap(const void *addr)
1268{ 1356{
1269 BUG_ON(in_interrupt()); 1357 BUG_ON(in_interrupt());
1358 might_sleep();
1270 __vunmap(addr, 0); 1359 __vunmap(addr, 0);
1271} 1360}
1272EXPORT_SYMBOL(vunmap); 1361EXPORT_SYMBOL(vunmap);
@@ -1286,6 +1375,8 @@ void *vmap(struct page **pages, unsigned int count,
1286{ 1375{
1287 struct vm_struct *area; 1376 struct vm_struct *area;
1288 1377
1378 might_sleep();
1379
1289 if (count > num_physpages) 1380 if (count > num_physpages)
1290 return NULL; 1381 return NULL;
1291 1382