aboutsummaryrefslogtreecommitdiffstats
path: root/mm
diff options
context:
space:
mode:
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/fremap.c2
-rw-r--r--mm/highmem.c65
-rw-r--r--mm/hugetlb.c28
-rw-r--r--mm/memory.c6
-rw-r--r--mm/migrate.c2
-rw-r--r--mm/mlock.c12
-rw-r--r--mm/mmap.c52
-rw-r--r--mm/mprotect.c5
-rw-r--r--mm/page-writeback.c50
-rw-r--r--mm/page_alloc.c27
-rw-r--r--mm/page_cgroup.c3
-rw-r--r--mm/page_io.c2
-rw-r--r--mm/percpu.c1326
-rw-r--r--mm/readahead.c25
-rw-r--r--mm/rmap.c3
-rw-r--r--mm/shmem.c45
-rw-r--r--mm/slab.c1
-rw-r--r--mm/slob.c44
-rw-r--r--mm/slub.c83
-rw-r--r--mm/swapfile.c4
-rw-r--r--mm/util.c20
-rw-r--r--mm/vmalloc.c117
-rw-r--r--mm/vmscan.c32
28 files changed, 1847 insertions, 211 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/fremap.c b/mm/fremap.c
index 736ba7f3306a..b6ec85abbb39 100644
--- a/mm/fremap.c
+++ b/mm/fremap.c
@@ -198,7 +198,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
198 flags &= MAP_NONBLOCK; 198 flags &= MAP_NONBLOCK;
199 get_file(file); 199 get_file(file);
200 addr = mmap_region(file, start, size, 200 addr = mmap_region(file, start, size,
201 flags, vma->vm_flags, pgoff, 1); 201 flags, vma->vm_flags, pgoff);
202 fput(file); 202 fput(file);
203 if (IS_ERR_VALUE(addr)) { 203 if (IS_ERR_VALUE(addr)) {
204 err = addr; 204 err = addr;
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/hugetlb.c b/mm/hugetlb.c
index 618e98304080..107da3d809a8 100644
--- a/mm/hugetlb.c
+++ b/mm/hugetlb.c
@@ -2269,12 +2269,18 @@ void hugetlb_change_protection(struct vm_area_struct *vma,
2269 2269
2270int hugetlb_reserve_pages(struct inode *inode, 2270int hugetlb_reserve_pages(struct inode *inode,
2271 long from, long to, 2271 long from, long to,
2272 struct vm_area_struct *vma) 2272 struct vm_area_struct *vma,
2273 int acctflag)
2273{ 2274{
2274 long ret, chg; 2275 long ret, chg;
2275 struct hstate *h = hstate_inode(inode); 2276 struct hstate *h = hstate_inode(inode);
2276 2277
2277 if (vma && vma->vm_flags & VM_NORESERVE) 2278 /*
2279 * Only apply hugepage reservation if asked. At fault time, an
2280 * attempt will be made for VM_NORESERVE to allocate a page
2281 * and filesystem quota without using reserves
2282 */
2283 if (acctflag & VM_NORESERVE)
2278 return 0; 2284 return 0;
2279 2285
2280 /* 2286 /*
@@ -2299,13 +2305,31 @@ int hugetlb_reserve_pages(struct inode *inode,
2299 if (chg < 0) 2305 if (chg < 0)
2300 return chg; 2306 return chg;
2301 2307
2308 /* There must be enough filesystem quota for the mapping */
2302 if (hugetlb_get_quota(inode->i_mapping, chg)) 2309 if (hugetlb_get_quota(inode->i_mapping, chg))
2303 return -ENOSPC; 2310 return -ENOSPC;
2311
2312 /*
2313 * Check enough hugepages are available for the reservation.
2314 * Hand back the quota if there are not
2315 */
2304 ret = hugetlb_acct_memory(h, chg); 2316 ret = hugetlb_acct_memory(h, chg);
2305 if (ret < 0) { 2317 if (ret < 0) {
2306 hugetlb_put_quota(inode->i_mapping, chg); 2318 hugetlb_put_quota(inode->i_mapping, chg);
2307 return ret; 2319 return ret;
2308 } 2320 }
2321
2322 /*
2323 * Account for the reservations made. Shared mappings record regions
2324 * that have reservations as they are shared by multiple VMAs.
2325 * When the last VMA disappears, the region map says how much
2326 * the reservation was and the page cache tells how much of
2327 * the reservation was consumed. Private mappings are per-VMA and
2328 * only the consumed reservations are tracked. When the VMA
2329 * disappears, the original reservation is the VMA size and the
2330 * consumed reservations are stored in the map. Hence, nothing
2331 * else has to be done for private mappings here
2332 */
2309 if (!vma || vma->vm_flags & VM_SHARED) 2333 if (!vma || vma->vm_flags & VM_SHARED)
2310 region_add(&inode->i_mapping->private_list, from, to); 2334 region_add(&inode->i_mapping->private_list, from, to);
2311 return 0; 2335 return 0;
diff --git a/mm/memory.c b/mm/memory.c
index baa999e87cd2..2032ad2fc34b 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1665,9 +1665,10 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1665 * behaviour that some programs depend on. We mark the "original" 1665 * behaviour that some programs depend on. We mark the "original"
1666 * un-COW'ed pages by matching them up with "vma->vm_pgoff". 1666 * un-COW'ed pages by matching them up with "vma->vm_pgoff".
1667 */ 1667 */
1668 if (addr == vma->vm_start && end == vma->vm_end) 1668 if (addr == vma->vm_start && end == vma->vm_end) {
1669 vma->vm_pgoff = pfn; 1669 vma->vm_pgoff = pfn;
1670 else if (is_cow_mapping(vma->vm_flags)) 1670 vma->vm_flags |= VM_PFN_AT_MMAP;
1671 } else if (is_cow_mapping(vma->vm_flags))
1671 return -EINVAL; 1672 return -EINVAL;
1672 1673
1673 vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP; 1674 vma->vm_flags |= VM_IO | VM_RESERVED | VM_PFNMAP;
@@ -1679,6 +1680,7 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1679 * needed from higher level routine calling unmap_vmas 1680 * needed from higher level routine calling unmap_vmas
1680 */ 1681 */
1681 vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP); 1682 vma->vm_flags &= ~(VM_IO | VM_RESERVED | VM_PFNMAP);
1683 vma->vm_flags &= ~VM_PFN_AT_MMAP;
1682 return -EINVAL; 1684 return -EINVAL;
1683 } 1685 }
1684 1686
diff --git a/mm/migrate.c b/mm/migrate.c
index 2bb4e1d63520..a9eff3f092f6 100644
--- a/mm/migrate.c
+++ b/mm/migrate.c
@@ -1129,7 +1129,7 @@ int migrate_vmas(struct mm_struct *mm, const nodemask_t *to,
1129 struct vm_area_struct *vma; 1129 struct vm_area_struct *vma;
1130 int err = 0; 1130 int err = 0;
1131 1131
1132 for(vma = mm->mmap; vma->vm_next && !err; vma = vma->vm_next) { 1132 for (vma = mm->mmap; vma && !err; vma = vma->vm_next) {
1133 if (vma->vm_ops && vma->vm_ops->migrate) { 1133 if (vma->vm_ops && vma->vm_ops->migrate) {
1134 err = vma->vm_ops->migrate(vma, to, from, flags); 1134 err = vma->vm_ops->migrate(vma, to, from, flags);
1135 if (err) 1135 if (err)
diff --git a/mm/mlock.c b/mm/mlock.c
index 028ec482fdd4..cbe9e0581b75 100644
--- a/mm/mlock.c
+++ b/mm/mlock.c
@@ -311,7 +311,10 @@ long mlock_vma_pages_range(struct vm_area_struct *vma,
311 is_vm_hugetlb_page(vma) || 311 is_vm_hugetlb_page(vma) ||
312 vma == get_gate_vma(current))) { 312 vma == get_gate_vma(current))) {
313 313
314 return __mlock_vma_pages_range(vma, start, end, 1); 314 __mlock_vma_pages_range(vma, start, end, 1);
315
316 /* Hide errors from mmap() and other callers */
317 return 0;
315 } 318 }
316 319
317 /* 320 /*
@@ -657,7 +660,7 @@ void *alloc_locked_buffer(size_t size)
657 return buffer; 660 return buffer;
658} 661}
659 662
660void free_locked_buffer(void *buffer, size_t size) 663void release_locked_buffer(void *buffer, size_t size)
661{ 664{
662 unsigned long pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT; 665 unsigned long pgsz = PAGE_ALIGN(size) >> PAGE_SHIFT;
663 666
@@ -667,6 +670,11 @@ void free_locked_buffer(void *buffer, size_t size)
667 current->mm->locked_vm -= pgsz; 670 current->mm->locked_vm -= pgsz;
668 671
669 up_write(&current->mm->mmap_sem); 672 up_write(&current->mm->mmap_sem);
673}
674
675void free_locked_buffer(void *buffer, size_t size)
676{
677 release_locked_buffer(buffer, size);
670 678
671 kfree(buffer); 679 kfree(buffer);
672} 680}
diff --git a/mm/mmap.c b/mm/mmap.c
index 214b6a258eeb..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>
@@ -918,7 +919,6 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
918 struct inode *inode; 919 struct inode *inode;
919 unsigned int vm_flags; 920 unsigned int vm_flags;
920 int error; 921 int error;
921 int accountable = 1;
922 unsigned long reqprot = prot; 922 unsigned long reqprot = prot;
923 923
924 /* 924 /*
@@ -1019,8 +1019,6 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
1019 return -EPERM; 1019 return -EPERM;
1020 vm_flags &= ~VM_MAYEXEC; 1020 vm_flags &= ~VM_MAYEXEC;
1021 } 1021 }
1022 if (is_file_hugepages(file))
1023 accountable = 0;
1024 1022
1025 if (!file->f_op || !file->f_op->mmap) 1023 if (!file->f_op || !file->f_op->mmap)
1026 return -ENODEV; 1024 return -ENODEV;
@@ -1052,9 +1050,11 @@ unsigned long do_mmap_pgoff(struct file *file, unsigned long addr,
1052 error = security_file_mmap(file, reqprot, prot, flags, addr, 0); 1050 error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
1053 if (error) 1051 if (error)
1054 return error; 1052 return error;
1053 error = ima_file_mmap(file, prot);
1054 if (error)
1055 return error;
1055 1056
1056 return mmap_region(file, addr, len, flags, vm_flags, pgoff, 1057 return mmap_region(file, addr, len, flags, vm_flags, pgoff);
1057 accountable);
1058} 1058}
1059EXPORT_SYMBOL(do_mmap_pgoff); 1059EXPORT_SYMBOL(do_mmap_pgoff);
1060 1060
@@ -1092,17 +1092,23 @@ int vma_wants_writenotify(struct vm_area_struct *vma)
1092 1092
1093/* 1093/*
1094 * We account for memory if it's a private writeable mapping, 1094 * We account for memory if it's a private writeable mapping,
1095 * and VM_NORESERVE wasn't set. 1095 * not hugepages and VM_NORESERVE wasn't set.
1096 */ 1096 */
1097static inline int accountable_mapping(unsigned int vm_flags) 1097static inline int accountable_mapping(struct file *file, unsigned int vm_flags)
1098{ 1098{
1099 /*
1100 * hugetlb has its own accounting separate from the core VM
1101 * VM_HUGETLB may not be set yet so we cannot check for that flag.
1102 */
1103 if (file && is_file_hugepages(file))
1104 return 0;
1105
1099 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE; 1106 return (vm_flags & (VM_NORESERVE | VM_SHARED | VM_WRITE)) == VM_WRITE;
1100} 1107}
1101 1108
1102unsigned long mmap_region(struct file *file, unsigned long addr, 1109unsigned long mmap_region(struct file *file, unsigned long addr,
1103 unsigned long len, unsigned long flags, 1110 unsigned long len, unsigned long flags,
1104 unsigned int vm_flags, unsigned long pgoff, 1111 unsigned int vm_flags, unsigned long pgoff)
1105 int accountable)
1106{ 1112{
1107 struct mm_struct *mm = current->mm; 1113 struct mm_struct *mm = current->mm;
1108 struct vm_area_struct *vma, *prev; 1114 struct vm_area_struct *vma, *prev;
@@ -1128,18 +1134,22 @@ munmap_back:
1128 1134
1129 /* 1135 /*
1130 * Set 'VM_NORESERVE' if we should not account for the 1136 * Set 'VM_NORESERVE' if we should not account for the
1131 * memory use of this mapping. We only honor MAP_NORESERVE 1137 * memory use of this mapping.
1132 * if we're allowed to overcommit memory.
1133 */ 1138 */
1134 if ((flags & MAP_NORESERVE) && sysctl_overcommit_memory != OVERCOMMIT_NEVER) 1139 if ((flags & MAP_NORESERVE)) {
1135 vm_flags |= VM_NORESERVE; 1140 /* We honor MAP_NORESERVE if allowed to overcommit */
1136 if (!accountable) 1141 if (sysctl_overcommit_memory != OVERCOMMIT_NEVER)
1137 vm_flags |= VM_NORESERVE; 1142 vm_flags |= VM_NORESERVE;
1143
1144 /* hugetlb applies strict overcommit unless MAP_NORESERVE */
1145 if (file && is_file_hugepages(file))
1146 vm_flags |= VM_NORESERVE;
1147 }
1138 1148
1139 /* 1149 /*
1140 * Private writable mapping: check memory availability 1150 * Private writable mapping: check memory availability
1141 */ 1151 */
1142 if (accountable_mapping(vm_flags)) { 1152 if (accountable_mapping(file, vm_flags)) {
1143 charged = len >> PAGE_SHIFT; 1153 charged = len >> PAGE_SHIFT;
1144 if (security_vm_enough_memory(charged)) 1154 if (security_vm_enough_memory(charged))
1145 return -ENOMEM; 1155 return -ENOMEM;
@@ -2078,12 +2088,8 @@ void exit_mmap(struct mm_struct *mm)
2078 unsigned long end; 2088 unsigned long end;
2079 2089
2080 /* mm's last user has gone, and its about to be pulled down */ 2090 /* mm's last user has gone, and its about to be pulled down */
2081 arch_exit_mmap(mm);
2082 mmu_notifier_release(mm); 2091 mmu_notifier_release(mm);
2083 2092
2084 if (!mm->mmap) /* Can happen if dup_mmap() received an OOM */
2085 return;
2086
2087 if (mm->locked_vm) { 2093 if (mm->locked_vm) {
2088 vma = mm->mmap; 2094 vma = mm->mmap;
2089 while (vma) { 2095 while (vma) {
@@ -2092,7 +2098,13 @@ void exit_mmap(struct mm_struct *mm)
2092 vma = vma->vm_next; 2098 vma = vma->vm_next;
2093 } 2099 }
2094 } 2100 }
2101
2102 arch_exit_mmap(mm);
2103
2095 vma = mm->mmap; 2104 vma = mm->mmap;
2105 if (!vma) /* Can happen if dup_mmap() received an OOM */
2106 return;
2107
2096 lru_add_drain(); 2108 lru_add_drain();
2097 flush_cache_mm(mm); 2109 flush_cache_mm(mm);
2098 tlb = tlb_gather_mmu(mm, 1); 2110 tlb = tlb_gather_mmu(mm, 1);
diff --git a/mm/mprotect.c b/mm/mprotect.c
index abe2694e13f4..258197b76fb4 100644
--- a/mm/mprotect.c
+++ b/mm/mprotect.c
@@ -151,10 +151,11 @@ mprotect_fixup(struct vm_area_struct *vma, struct vm_area_struct **pprev,
151 /* 151 /*
152 * If we make a private mapping writable we increase our commit; 152 * If we make a private mapping writable we increase our commit;
153 * but (without finer accounting) cannot reduce our commit if we 153 * but (without finer accounting) cannot reduce our commit if we
154 * make it unwritable again. 154 * make it unwritable again. hugetlb mapping were accounted for
155 * even if read-only so there is no need to account for them here
155 */ 156 */
156 if (newflags & VM_WRITE) { 157 if (newflags & VM_WRITE) {
157 if (!(oldflags & (VM_ACCOUNT|VM_WRITE| 158 if (!(oldflags & (VM_ACCOUNT|VM_WRITE|VM_HUGETLB|
158 VM_SHARED|VM_NORESERVE))) { 159 VM_SHARED|VM_NORESERVE))) {
159 charged = nrpages; 160 charged = nrpages;
160 if (security_vm_enough_memory(charged)) 161 if (security_vm_enough_memory(charged))
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index dc32dae01e5f..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
@@ -209,7 +209,7 @@ int dirty_bytes_handler(struct ctl_table *table, int write,
209 struct file *filp, void __user *buffer, size_t *lenp, 209 struct file *filp, void __user *buffer, size_t *lenp,
210 loff_t *ppos) 210 loff_t *ppos)
211{ 211{
212 int old_bytes = vm_dirty_bytes; 212 unsigned long old_bytes = vm_dirty_bytes;
213 int ret; 213 int ret;
214 214
215 ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos); 215 ret = proc_doulongvec_minmax(table, write, filp, buffer, lenp, ppos);
@@ -240,7 +240,7 @@ void bdi_writeout_inc(struct backing_dev_info *bdi)
240} 240}
241EXPORT_SYMBOL_GPL(bdi_writeout_inc); 241EXPORT_SYMBOL_GPL(bdi_writeout_inc);
242 242
243static inline void task_dirty_inc(struct task_struct *tsk) 243void task_dirty_inc(struct task_struct *tsk)
244{ 244{
245 prop_inc_single(&vm_dirties, &tsk->dirties); 245 prop_inc_single(&vm_dirties, &tsk->dirties);
246} 246}
@@ -1051,20 +1051,23 @@ continue_unlock:
1051 } 1051 }
1052 } 1052 }
1053 1053
1054 if (nr_to_write > 0) 1054 if (nr_to_write > 0) {
1055 nr_to_write--; 1055 nr_to_write--;
1056 else if (wbc->sync_mode == WB_SYNC_NONE) { 1056 if (nr_to_write == 0 &&
1057 /* 1057 wbc->sync_mode == WB_SYNC_NONE) {
1058 * We stop writing back only if we are not 1058 /*
1059 * doing integrity sync. In case of integrity 1059 * We stop writing back only if we are
1060 * sync we have to keep going because someone 1060 * not doing integrity sync. In case of
1061 * may be concurrently dirtying pages, and we 1061 * integrity sync we have to keep going
1062 * might have synced a lot of newly appeared 1062 * because someone may be concurrently
1063 * dirty pages, but have not synced all of the 1063 * dirtying pages, and we might have
1064 * old dirty pages. 1064 * synced a lot of newly appeared dirty
1065 */ 1065 * pages, but have not synced all of the
1066 done = 1; 1066 * old dirty pages.
1067 break; 1067 */
1068 done = 1;
1069 break;
1070 }
1068 } 1071 }
1069 1072
1070 if (wbc->nonblocking && bdi_write_congested(bdi)) { 1073 if (wbc->nonblocking && bdi_write_congested(bdi)) {
@@ -1076,7 +1079,7 @@ continue_unlock:
1076 pagevec_release(&pvec); 1079 pagevec_release(&pvec);
1077 cond_resched(); 1080 cond_resched();
1078 } 1081 }
1079 if (!cycled) { 1082 if (!cycled && !done) {
1080 /* 1083 /*
1081 * range_cyclic: 1084 * range_cyclic:
1082 * We hit the last page and there is more work to be done: wrap 1085 * We hit the last page and there is more work to be done: wrap
@@ -1227,6 +1230,7 @@ int __set_page_dirty_nobuffers(struct page *page)
1227 __inc_zone_page_state(page, NR_FILE_DIRTY); 1230 __inc_zone_page_state(page, NR_FILE_DIRTY);
1228 __inc_bdi_stat(mapping->backing_dev_info, 1231 __inc_bdi_stat(mapping->backing_dev_info,
1229 BDI_RECLAIMABLE); 1232 BDI_RECLAIMABLE);
1233 task_dirty_inc(current);
1230 task_io_account_write(PAGE_CACHE_SIZE); 1234 task_io_account_write(PAGE_CACHE_SIZE);
1231 } 1235 }
1232 radix_tree_tag_set(&mapping->page_tree, 1236 radix_tree_tag_set(&mapping->page_tree,
@@ -1259,7 +1263,7 @@ EXPORT_SYMBOL(redirty_page_for_writepage);
1259 * If the mapping doesn't provide a set_page_dirty a_op, then 1263 * If the mapping doesn't provide a set_page_dirty a_op, then
1260 * just fall through and assume that it wants buffer_heads. 1264 * just fall through and assume that it wants buffer_heads.
1261 */ 1265 */
1262static int __set_page_dirty(struct page *page) 1266int set_page_dirty(struct page *page)
1263{ 1267{
1264 struct address_space *mapping = page_mapping(page); 1268 struct address_space *mapping = page_mapping(page);
1265 1269
@@ -1277,14 +1281,6 @@ static int __set_page_dirty(struct page *page)
1277 } 1281 }
1278 return 0; 1282 return 0;
1279} 1283}
1280
1281int set_page_dirty(struct page *page)
1282{
1283 int ret = __set_page_dirty(page);
1284 if (ret)
1285 task_dirty_inc(current);
1286 return ret;
1287}
1288EXPORT_SYMBOL(set_page_dirty); 1284EXPORT_SYMBOL(set_page_dirty);
1289 1285
1290/* 1286/*
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 22b15a4cde8a..a3803ea8c27d 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -2994,7 +2994,7 @@ static int __meminit next_active_region_index_in_nid(int index, int nid)
2994 * was used and there are no special requirements, this is a convenient 2994 * was used and there are no special requirements, this is a convenient
2995 * alternative 2995 * alternative
2996 */ 2996 */
2997int __meminit early_pfn_to_nid(unsigned long pfn) 2997int __meminit __early_pfn_to_nid(unsigned long pfn)
2998{ 2998{
2999 int i; 2999 int i;
3000 3000
@@ -3005,10 +3005,33 @@ int __meminit early_pfn_to_nid(unsigned long pfn)
3005 if (start_pfn <= pfn && pfn < end_pfn) 3005 if (start_pfn <= pfn && pfn < end_pfn)
3006 return early_node_map[i].nid; 3006 return early_node_map[i].nid;
3007 } 3007 }
3008 /* This is a memory hole */
3009 return -1;
3010}
3011#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */
3012
3013int __meminit early_pfn_to_nid(unsigned long pfn)
3014{
3015 int nid;
3008 3016
3017 nid = __early_pfn_to_nid(pfn);
3018 if (nid >= 0)
3019 return nid;
3020 /* just returns 0 */
3009 return 0; 3021 return 0;
3010} 3022}
3011#endif /* CONFIG_HAVE_ARCH_EARLY_PFN_TO_NID */ 3023
3024#ifdef CONFIG_NODES_SPAN_OTHER_NODES
3025bool __meminit early_pfn_in_nid(unsigned long pfn, int node)
3026{
3027 int nid;
3028
3029 nid = __early_pfn_to_nid(pfn);
3030 if (nid >= 0 && nid != node)
3031 return false;
3032 return true;
3033}
3034#endif
3012 3035
3013/* Basic iterator support to walk early_node_map[] */ 3036/* Basic iterator support to walk early_node_map[] */
3014#define for_each_active_range_index_in_nid(i, nid) \ 3037#define for_each_active_range_index_in_nid(i, nid) \
diff --git a/mm/page_cgroup.c b/mm/page_cgroup.c
index 7006a11350c8..ceecfbb143fa 100644
--- a/mm/page_cgroup.c
+++ b/mm/page_cgroup.c
@@ -114,7 +114,8 @@ static int __init_refok init_section_page_cgroup(unsigned long pfn)
114 nid = page_to_nid(pfn_to_page(pfn)); 114 nid = page_to_nid(pfn_to_page(pfn));
115 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION; 115 table_size = sizeof(struct page_cgroup) * PAGES_PER_SECTION;
116 if (slab_is_available()) { 116 if (slab_is_available()) {
117 base = kmalloc_node(table_size, GFP_KERNEL, nid); 117 base = kmalloc_node(table_size,
118 GFP_KERNEL | __GFP_NOWARN, nid);
118 if (!base) 119 if (!base)
119 base = vmalloc_node(table_size, nid); 120 base = vmalloc_node(table_size, nid);
120 } else { 121 } else {
diff --git a/mm/page_io.c b/mm/page_io.c
index dc6ce0afbded..3023c475e041 100644
--- a/mm/page_io.c
+++ b/mm/page_io.c
@@ -111,7 +111,7 @@ int swap_writepage(struct page *page, struct writeback_control *wbc)
111 goto out; 111 goto out;
112 } 112 }
113 if (wbc->sync_mode == WB_SYNC_ALL) 113 if (wbc->sync_mode == WB_SYNC_ALL)
114 rw |= (1 << BIO_RW_SYNC); 114 rw |= (1 << BIO_RW_SYNCIO) | (1 << BIO_RW_UNPLUG);
115 count_vm_event(PSWPOUT); 115 count_vm_event(PSWPOUT);
116 set_page_writeback(page); 116 set_page_writeback(page);
117 unlock_page(page); 117 unlock_page(page);
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/rmap.c b/mm/rmap.c
index ac4af8cffbf9..16521664010d 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1072,7 +1072,8 @@ static int try_to_unmap_file(struct page *page, int unlock, int migration)
1072 spin_lock(&mapping->i_mmap_lock); 1072 spin_lock(&mapping->i_mmap_lock);
1073 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) { 1073 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
1074 if (MLOCK_PAGES && unlikely(unlock)) { 1074 if (MLOCK_PAGES && unlikely(unlock)) {
1075 if (!(vma->vm_flags & VM_LOCKED)) 1075 if (!((vma->vm_flags & VM_LOCKED) &&
1076 page_mapped_in_vma(page, vma)))
1076 continue; /* must visit all vmas */ 1077 continue; /* must visit all vmas */
1077 ret = SWAP_MLOCK; 1078 ret = SWAP_MLOCK;
1078 } else { 1079 } else {
diff --git a/mm/shmem.c b/mm/shmem.c
index 19d566ccdeea..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
@@ -169,13 +170,13 @@ static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
169 */ 170 */
170static inline int shmem_acct_size(unsigned long flags, loff_t size) 171static inline int shmem_acct_size(unsigned long flags, loff_t size)
171{ 172{
172 return (flags & VM_ACCOUNT) ? 173 return (flags & VM_NORESERVE) ?
173 security_vm_enough_memory_kern(VM_ACCT(size)) : 0; 174 0 : security_vm_enough_memory_kern(VM_ACCT(size));
174} 175}
175 176
176static inline void shmem_unacct_size(unsigned long flags, loff_t size) 177static inline void shmem_unacct_size(unsigned long flags, loff_t size)
177{ 178{
178 if (flags & VM_ACCOUNT) 179 if (!(flags & VM_NORESERVE))
179 vm_unacct_memory(VM_ACCT(size)); 180 vm_unacct_memory(VM_ACCT(size));
180} 181}
181 182
@@ -187,13 +188,13 @@ static inline void shmem_unacct_size(unsigned long flags, loff_t size)
187 */ 188 */
188static inline int shmem_acct_block(unsigned long flags) 189static inline int shmem_acct_block(unsigned long flags)
189{ 190{
190 return (flags & VM_ACCOUNT) ? 191 return (flags & VM_NORESERVE) ?
191 0 : security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)); 192 security_vm_enough_memory_kern(VM_ACCT(PAGE_CACHE_SIZE)) : 0;
192} 193}
193 194
194static inline void shmem_unacct_blocks(unsigned long flags, long pages) 195static inline void shmem_unacct_blocks(unsigned long flags, long pages)
195{ 196{
196 if (!(flags & VM_ACCOUNT)) 197 if (flags & VM_NORESERVE)
197 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE)); 198 vm_unacct_memory(pages * VM_ACCT(PAGE_CACHE_SIZE));
198} 199}
199 200
@@ -1515,8 +1516,8 @@ static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
1515 return 0; 1516 return 0;
1516} 1517}
1517 1518
1518static struct inode * 1519static struct inode *shmem_get_inode(struct super_block *sb, int mode,
1519shmem_get_inode(struct super_block *sb, int mode, dev_t dev) 1520 dev_t dev, unsigned long flags)
1520{ 1521{
1521 struct inode *inode; 1522 struct inode *inode;
1522 struct shmem_inode_info *info; 1523 struct shmem_inode_info *info;
@@ -1537,6 +1538,7 @@ shmem_get_inode(struct super_block *sb, int mode, dev_t dev)
1537 info = SHMEM_I(inode); 1538 info = SHMEM_I(inode);
1538 memset(info, 0, (char *)inode - (char *)info); 1539 memset(info, 0, (char *)inode - (char *)info);
1539 spin_lock_init(&info->lock); 1540 spin_lock_init(&info->lock);
1541 info->flags = flags & VM_NORESERVE;
1540 INIT_LIST_HEAD(&info->swaplist); 1542 INIT_LIST_HEAD(&info->swaplist);
1541 1543
1542 switch (mode & S_IFMT) { 1544 switch (mode & S_IFMT) {
@@ -1779,9 +1781,10 @@ static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
1779static int 1781static int
1780shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 1782shmem_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
1781{ 1783{
1782 struct inode *inode = shmem_get_inode(dir->i_sb, mode, dev); 1784 struct inode *inode;
1783 int error = -ENOSPC; 1785 int error = -ENOSPC;
1784 1786
1787 inode = shmem_get_inode(dir->i_sb, mode, dev, VM_NORESERVE);
1785 if (inode) { 1788 if (inode) {
1786 error = security_inode_init_security(inode, dir, NULL, NULL, 1789 error = security_inode_init_security(inode, dir, NULL, NULL,
1787 NULL); 1790 NULL);
@@ -1920,7 +1923,7 @@ static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *s
1920 if (len > PAGE_CACHE_SIZE) 1923 if (len > PAGE_CACHE_SIZE)
1921 return -ENAMETOOLONG; 1924 return -ENAMETOOLONG;
1922 1925
1923 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0); 1926 inode = shmem_get_inode(dir->i_sb, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
1924 if (!inode) 1927 if (!inode)
1925 return -ENOSPC; 1928 return -ENOSPC;
1926 1929
@@ -2332,7 +2335,7 @@ static int shmem_fill_super(struct super_block *sb,
2332 sb->s_flags |= MS_POSIXACL; 2335 sb->s_flags |= MS_POSIXACL;
2333#endif 2336#endif
2334 2337
2335 inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0); 2338 inode = shmem_get_inode(sb, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
2336 if (!inode) 2339 if (!inode)
2337 goto failed; 2340 goto failed;
2338 inode->i_uid = sbinfo->uid; 2341 inode->i_uid = sbinfo->uid;
@@ -2574,12 +2577,12 @@ int shmem_unuse(swp_entry_t entry, struct page *page)
2574 return 0; 2577 return 0;
2575} 2578}
2576 2579
2577#define shmem_file_operations ramfs_file_operations 2580#define shmem_vm_ops generic_file_vm_ops
2578#define shmem_vm_ops generic_file_vm_ops 2581#define shmem_file_operations ramfs_file_operations
2579#define shmem_get_inode ramfs_get_inode 2582#define shmem_get_inode(sb, mode, dev, flags) ramfs_get_inode(sb, mode, dev)
2580#define shmem_acct_size(a, b) 0 2583#define shmem_acct_size(flags, size) 0
2581#define shmem_unacct_size(a, b) do {} while (0) 2584#define shmem_unacct_size(flags, size) do {} while (0)
2582#define SHMEM_MAX_BYTES LLONG_MAX 2585#define SHMEM_MAX_BYTES LLONG_MAX
2583 2586
2584#endif /* CONFIG_SHMEM */ 2587#endif /* CONFIG_SHMEM */
2585 2588
@@ -2589,7 +2592,7 @@ int shmem_unuse(swp_entry_t entry, struct page *page)
2589 * shmem_file_setup - get an unlinked file living in tmpfs 2592 * shmem_file_setup - get an unlinked file living in tmpfs
2590 * @name: name for dentry (to be seen in /proc/<pid>/maps 2593 * @name: name for dentry (to be seen in /proc/<pid>/maps
2591 * @size: size to be set for the file 2594 * @size: size to be set for the file
2592 * @flags: vm_flags 2595 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
2593 */ 2596 */
2594struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags) 2597struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2595{ 2598{
@@ -2623,13 +2626,10 @@ struct file *shmem_file_setup(char *name, loff_t size, unsigned long flags)
2623 goto put_dentry; 2626 goto put_dentry;
2624 2627
2625 error = -ENOSPC; 2628 error = -ENOSPC;
2626 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0); 2629 inode = shmem_get_inode(root->d_sb, S_IFREG | S_IRWXUGO, 0, flags);
2627 if (!inode) 2630 if (!inode)
2628 goto close_file; 2631 goto close_file;
2629 2632
2630#ifdef CONFIG_SHMEM
2631 SHMEM_I(inode)->flags = (flags & VM_NORESERVE) ? 0 : VM_ACCOUNT;
2632#endif
2633 d_instantiate(dentry, inode); 2633 d_instantiate(dentry, inode);
2634 inode->i_size = size; 2634 inode->i_size = size;
2635 inode->i_nlink = 0; /* It is unlinked */ 2635 inode->i_nlink = 0; /* It is unlinked */
@@ -2666,6 +2666,7 @@ int shmem_zero_setup(struct vm_area_struct *vma)
2666 if (IS_ERR(file)) 2666 if (IS_ERR(file))
2667 return PTR_ERR(file); 2667 return PTR_ERR(file);
2668 2668
2669 ima_shm_check(file);
2669 if (vma->vm_file) 2670 if (vma->vm_file)
2670 fput(vma->vm_file); 2671 fput(vma->vm_file);
2671 vma->vm_file = file; 2672 vma->vm_file = file;
diff --git a/mm/slab.c b/mm/slab.c
index 6b61de8543ec..825c606f691d 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -4461,3 +4461,4 @@ size_t ksize(const void *objp)
4461 4461
4462 return obj_size(virt_to_cache(objp)); 4462 return obj_size(virt_to_cache(objp));
4463} 4463}
4464EXPORT_SYMBOL(ksize);
diff --git a/mm/slob.c b/mm/slob.c
index 4b1c0c1d63cb..7a3411524dac 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)) {
@@ -478,7 +489,7 @@ void *__kmalloc_node(size_t size, gfp_t gfp, int node)
478 } else { 489 } else {
479 void *ret; 490 void *ret;
480 491
481 ret = slob_new_page(gfp | __GFP_COMP, get_order(size), node); 492 ret = slob_new_pages(gfp | __GFP_COMP, get_order(size), node);
482 if (ret) { 493 if (ret) {
483 struct page *page; 494 struct page *page;
484 page = virt_to_page(ret); 495 page = virt_to_page(ret);
@@ -496,8 +507,8 @@ void kfree(const void *block)
496 if (unlikely(ZERO_OR_NULL_PTR(block))) 507 if (unlikely(ZERO_OR_NULL_PTR(block)))
497 return; 508 return;
498 509
499 sp = (struct slob_page *)virt_to_page(block); 510 sp = slob_page(block);
500 if (slob_page(sp)) { 511 if (is_slob_page(sp)) {
501 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); 512 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
502 unsigned int *m = (unsigned int *)(block - align); 513 unsigned int *m = (unsigned int *)(block - align);
503 slob_free(m, *m + align); 514 slob_free(m, *m + align);
@@ -515,14 +526,15 @@ size_t ksize(const void *block)
515 if (unlikely(block == ZERO_SIZE_PTR)) 526 if (unlikely(block == ZERO_SIZE_PTR))
516 return 0; 527 return 0;
517 528
518 sp = (struct slob_page *)virt_to_page(block); 529 sp = slob_page(block);
519 if (slob_page(sp)) { 530 if (is_slob_page(sp)) {
520 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN); 531 int align = max(ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
521 unsigned int *m = (unsigned int *)(block - align); 532 unsigned int *m = (unsigned int *)(block - align);
522 return SLOB_UNITS(*m) * SLOB_UNIT; 533 return SLOB_UNITS(*m) * SLOB_UNIT;
523 } else 534 } else
524 return sp->page.private; 535 return sp->page.private;
525} 536}
537EXPORT_SYMBOL(ksize);
526 538
527struct kmem_cache { 539struct kmem_cache {
528 unsigned int size, align; 540 unsigned int size, align;
@@ -574,7 +586,7 @@ void *kmem_cache_alloc_node(struct kmem_cache *c, gfp_t flags, int node)
574 if (c->size < PAGE_SIZE) 586 if (c->size < PAGE_SIZE)
575 b = slob_alloc(c->size, flags, c->align, node); 587 b = slob_alloc(c->size, flags, c->align, node);
576 else 588 else
577 b = slob_new_page(flags, get_order(c->size), node); 589 b = slob_new_pages(flags, get_order(c->size), node);
578 590
579 if (c->ctor) 591 if (c->ctor)
580 c->ctor(b); 592 c->ctor(b);
@@ -588,7 +600,7 @@ static void __kmem_cache_free(void *b, int size)
588 if (size < PAGE_SIZE) 600 if (size < PAGE_SIZE)
589 slob_free(b, size); 601 slob_free(b, size);
590 else 602 else
591 free_pages((unsigned long)b, get_order(size)); 603 slob_free_pages(b, get_order(size));
592} 604}
593 605
594static void kmem_rcu_free(struct rcu_head *head) 606static void kmem_rcu_free(struct rcu_head *head)
diff --git a/mm/slub.c b/mm/slub.c
index 214eb207c513..c4ea9158c9fb 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
@@ -1725,7 +1719,7 @@ static __always_inline void slab_free(struct kmem_cache *s,
1725 c = get_cpu_slab(s, smp_processor_id()); 1719 c = get_cpu_slab(s, smp_processor_id());
1726 debug_check_no_locks_freed(object, c->objsize); 1720 debug_check_no_locks_freed(object, c->objsize);
1727 if (!(s->flags & SLAB_DEBUG_OBJECTS)) 1721 if (!(s->flags & SLAB_DEBUG_OBJECTS))
1728 debug_check_no_obj_freed(object, s->objsize); 1722 debug_check_no_obj_freed(object, c->objsize);
1729 if (likely(page == c->page && c->node >= 0)) { 1723 if (likely(page == c->page && c->node >= 0)) {
1730 object[c->offset] = c->freelist; 1724 object[c->offset] = c->freelist;
1731 c->freelist = object; 1725 c->freelist = object;
@@ -1845,6 +1839,7 @@ static inline int calculate_order(int size)
1845 int order; 1839 int order;
1846 int min_objects; 1840 int min_objects;
1847 int fraction; 1841 int fraction;
1842 int max_objects;
1848 1843
1849 /* 1844 /*
1850 * Attempt to find best configuration for a slab. This 1845 * Attempt to find best configuration for a slab. This
@@ -1857,6 +1852,9 @@ static inline int calculate_order(int size)
1857 min_objects = slub_min_objects; 1852 min_objects = slub_min_objects;
1858 if (!min_objects) 1853 if (!min_objects)
1859 min_objects = 4 * (fls(nr_cpu_ids) + 1); 1854 min_objects = 4 * (fls(nr_cpu_ids) + 1);
1855 max_objects = (PAGE_SIZE << slub_max_order)/size;
1856 min_objects = min(min_objects, max_objects);
1857
1860 while (min_objects > 1) { 1858 while (min_objects > 1) {
1861 fraction = 16; 1859 fraction = 16;
1862 while (fraction >= 4) { 1860 while (fraction >= 4) {
@@ -1866,7 +1864,7 @@ static inline int calculate_order(int size)
1866 return order; 1864 return order;
1867 fraction /= 2; 1865 fraction /= 2;
1868 } 1866 }
1869 min_objects /= 2; 1867 min_objects --;
1870 } 1868 }
1871 1869
1872 /* 1870 /*
@@ -1929,17 +1927,6 @@ static void
1929init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s) 1927init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
1930{ 1928{
1931 n->nr_partial = 0; 1929 n->nr_partial = 0;
1932
1933 /*
1934 * The larger the object size is, the more pages we want on the partial
1935 * list to avoid pounding the page allocator excessively.
1936 */
1937 n->min_partial = ilog2(s->size);
1938 if (n->min_partial < MIN_PARTIAL)
1939 n->min_partial = MIN_PARTIAL;
1940 else if (n->min_partial > MAX_PARTIAL)
1941 n->min_partial = MAX_PARTIAL;
1942
1943 spin_lock_init(&n->list_lock); 1930 spin_lock_init(&n->list_lock);
1944 INIT_LIST_HEAD(&n->partial); 1931 INIT_LIST_HEAD(&n->partial);
1945#ifdef CONFIG_SLUB_DEBUG 1932#ifdef CONFIG_SLUB_DEBUG
@@ -2182,6 +2169,15 @@ static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
2182} 2169}
2183#endif 2170#endif
2184 2171
2172static void set_min_partial(struct kmem_cache *s, unsigned long min)
2173{
2174 if (min < MIN_PARTIAL)
2175 min = MIN_PARTIAL;
2176 else if (min > MAX_PARTIAL)
2177 min = MAX_PARTIAL;
2178 s->min_partial = min;
2179}
2180
2185/* 2181/*
2186 * calculate_sizes() determines the order and the distribution of data within 2182 * calculate_sizes() determines the order and the distribution of data within
2187 * a slab object. 2183 * a slab object.
@@ -2320,6 +2316,11 @@ static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
2320 if (!calculate_sizes(s, -1)) 2316 if (!calculate_sizes(s, -1))
2321 goto error; 2317 goto error;
2322 2318
2319 /*
2320 * The larger the object size is, the more pages we want on the partial
2321 * list to avoid pounding the page allocator excessively.
2322 */
2323 set_min_partial(s, ilog2(s->size));
2323 s->refcount = 1; 2324 s->refcount = 1;
2324#ifdef CONFIG_NUMA 2325#ifdef CONFIG_NUMA
2325 s->remote_node_defrag_ratio = 1000; 2326 s->remote_node_defrag_ratio = 1000;
@@ -2476,7 +2477,7 @@ EXPORT_SYMBOL(kmem_cache_destroy);
2476 * Kmalloc subsystem 2477 * Kmalloc subsystem
2477 *******************************************************************/ 2478 *******************************************************************/
2478 2479
2479struct kmem_cache kmalloc_caches[PAGE_SHIFT + 1] __cacheline_aligned; 2480struct kmem_cache kmalloc_caches[SLUB_PAGE_SHIFT] __cacheline_aligned;
2480EXPORT_SYMBOL(kmalloc_caches); 2481EXPORT_SYMBOL(kmalloc_caches);
2481 2482
2482static int __init setup_slub_min_order(char *str) 2483static int __init setup_slub_min_order(char *str)
@@ -2538,7 +2539,7 @@ panic:
2538} 2539}
2539 2540
2540#ifdef CONFIG_ZONE_DMA 2541#ifdef CONFIG_ZONE_DMA
2541static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT + 1]; 2542static struct kmem_cache *kmalloc_caches_dma[SLUB_PAGE_SHIFT];
2542 2543
2543static void sysfs_add_func(struct work_struct *w) 2544static void sysfs_add_func(struct work_struct *w)
2544{ 2545{
@@ -2659,7 +2660,7 @@ void *__kmalloc(size_t size, gfp_t flags)
2659{ 2660{
2660 struct kmem_cache *s; 2661 struct kmem_cache *s;
2661 2662
2662 if (unlikely(size > PAGE_SIZE)) 2663 if (unlikely(size > SLUB_MAX_SIZE))
2663 return kmalloc_large(size, flags); 2664 return kmalloc_large(size, flags);
2664 2665
2665 s = get_slab(size, flags); 2666 s = get_slab(size, flags);
@@ -2687,7 +2688,7 @@ void *__kmalloc_node(size_t size, gfp_t flags, int node)
2687{ 2688{
2688 struct kmem_cache *s; 2689 struct kmem_cache *s;
2689 2690
2690 if (unlikely(size > PAGE_SIZE)) 2691 if (unlikely(size > SLUB_MAX_SIZE))
2691 return kmalloc_large_node(size, flags, node); 2692 return kmalloc_large_node(size, flags, node);
2692 2693
2693 s = get_slab(size, flags); 2694 s = get_slab(size, flags);
@@ -2737,6 +2738,7 @@ size_t ksize(const void *object)
2737 */ 2738 */
2738 return s->size; 2739 return s->size;
2739} 2740}
2741EXPORT_SYMBOL(ksize);
2740 2742
2741void kfree(const void *x) 2743void kfree(const void *x)
2742{ 2744{
@@ -2986,7 +2988,7 @@ void __init kmem_cache_init(void)
2986 caches++; 2988 caches++;
2987 } 2989 }
2988 2990
2989 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) { 2991 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
2990 create_kmalloc_cache(&kmalloc_caches[i], 2992 create_kmalloc_cache(&kmalloc_caches[i],
2991 "kmalloc", 1 << i, GFP_KERNEL); 2993 "kmalloc", 1 << i, GFP_KERNEL);
2992 caches++; 2994 caches++;
@@ -3023,7 +3025,7 @@ void __init kmem_cache_init(void)
3023 slab_state = UP; 3025 slab_state = UP;
3024 3026
3025 /* Provide the correct kmalloc names now that the caches are up */ 3027 /* Provide the correct kmalloc names now that the caches are up */
3026 for (i = KMALLOC_SHIFT_LOW; i <= PAGE_SHIFT; i++) 3028 for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++)
3027 kmalloc_caches[i]. name = 3029 kmalloc_caches[i]. name =
3028 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i); 3030 kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
3029 3031
@@ -3223,7 +3225,7 @@ void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
3223{ 3225{
3224 struct kmem_cache *s; 3226 struct kmem_cache *s;
3225 3227
3226 if (unlikely(size > PAGE_SIZE)) 3228 if (unlikely(size > SLUB_MAX_SIZE))
3227 return kmalloc_large(size, gfpflags); 3229 return kmalloc_large(size, gfpflags);
3228 3230
3229 s = get_slab(size, gfpflags); 3231 s = get_slab(size, gfpflags);
@@ -3239,7 +3241,7 @@ void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
3239{ 3241{
3240 struct kmem_cache *s; 3242 struct kmem_cache *s;
3241 3243
3242 if (unlikely(size > PAGE_SIZE)) 3244 if (unlikely(size > SLUB_MAX_SIZE))
3243 return kmalloc_large_node(size, gfpflags, node); 3245 return kmalloc_large_node(size, gfpflags, node);
3244 3246
3245 s = get_slab(size, gfpflags); 3247 s = get_slab(size, gfpflags);
@@ -3836,6 +3838,26 @@ static ssize_t order_show(struct kmem_cache *s, char *buf)
3836} 3838}
3837SLAB_ATTR(order); 3839SLAB_ATTR(order);
3838 3840
3841static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
3842{
3843 return sprintf(buf, "%lu\n", s->min_partial);
3844}
3845
3846static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
3847 size_t length)
3848{
3849 unsigned long min;
3850 int err;
3851
3852 err = strict_strtoul(buf, 10, &min);
3853 if (err)
3854 return err;
3855
3856 set_min_partial(s, min);
3857 return length;
3858}
3859SLAB_ATTR(min_partial);
3860
3839static ssize_t ctor_show(struct kmem_cache *s, char *buf) 3861static ssize_t ctor_show(struct kmem_cache *s, char *buf)
3840{ 3862{
3841 if (s->ctor) { 3863 if (s->ctor) {
@@ -4151,6 +4173,7 @@ static struct attribute *slab_attrs[] = {
4151 &object_size_attr.attr, 4173 &object_size_attr.attr,
4152 &objs_per_slab_attr.attr, 4174 &objs_per_slab_attr.attr,
4153 &order_attr.attr, 4175 &order_attr.attr,
4176 &min_partial_attr.attr,
4154 &objects_attr.attr, 4177 &objects_attr.attr,
4155 &objects_partial_attr.attr, 4178 &objects_partial_attr.attr,
4156 &total_objects_attr.attr, 4179 &total_objects_attr.attr,
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 7e6304dfafab..312fafe0ab6e 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -635,7 +635,7 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
635 635
636 if (!bdev) { 636 if (!bdev) {
637 if (bdev_p) 637 if (bdev_p)
638 *bdev_p = sis->bdev; 638 *bdev_p = bdget(sis->bdev->bd_dev);
639 639
640 spin_unlock(&swap_lock); 640 spin_unlock(&swap_lock);
641 return i; 641 return i;
@@ -647,7 +647,7 @@ int swap_type_of(dev_t device, sector_t offset, struct block_device **bdev_p)
647 struct swap_extent, list); 647 struct swap_extent, list);
648 if (se->start_block == offset) { 648 if (se->start_block == offset) {
649 if (bdev_p) 649 if (bdev_p)
650 *bdev_p = sis->bdev; 650 *bdev_p = bdget(sis->bdev->bd_dev);
651 651
652 spin_unlock(&swap_lock); 652 spin_unlock(&swap_lock);
653 bdput(bdev); 653 bdput(bdev);
diff --git a/mm/util.c b/mm/util.c
index cb00b748ce47..37eaccdf3054 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -129,6 +129,26 @@ void *krealloc(const void *p, size_t new_size, gfp_t flags)
129} 129}
130EXPORT_SYMBOL(krealloc); 130EXPORT_SYMBOL(krealloc);
131 131
132/**
133 * kzfree - like kfree but zero memory
134 * @p: object to free memory of
135 *
136 * The memory of the object @p points to is zeroed before freed.
137 * If @p is %NULL, kzfree() does nothing.
138 */
139void kzfree(const void *p)
140{
141 size_t ks;
142 void *mem = (void *)p;
143
144 if (unlikely(ZERO_OR_NULL_PTR(mem)))
145 return;
146 ks = ksize(mem);
147 memset(mem, 0, ks);
148 kfree(mem);
149}
150EXPORT_SYMBOL(kzfree);
151
132/* 152/*
133 * strndup_user - duplicate an existing string from user space 153 * strndup_user - duplicate an existing string from user space
134 * @s: The string to duplicate 154 * @s: The string to duplicate
diff --git a/mm/vmalloc.c b/mm/vmalloc.c
index 75f49d312e8c..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 /*
@@ -323,6 +333,7 @@ static struct vmap_area *alloc_vmap_area(unsigned long size,
323 unsigned long addr; 333 unsigned long addr;
324 int purged = 0; 334 int purged = 0;
325 335
336 BUG_ON(!size);
326 BUG_ON(size & ~PAGE_MASK); 337 BUG_ON(size & ~PAGE_MASK);
327 338
328 va = kmalloc_node(sizeof(struct vmap_area), 339 va = kmalloc_node(sizeof(struct vmap_area),
@@ -334,6 +345,9 @@ retry:
334 addr = ALIGN(vstart, align); 345 addr = ALIGN(vstart, align);
335 346
336 spin_lock(&vmap_area_lock); 347 spin_lock(&vmap_area_lock);
348 if (addr + size - 1 < addr)
349 goto overflow;
350
337 /* XXX: could have a last_hole cache */ 351 /* XXX: could have a last_hole cache */
338 n = vmap_area_root.rb_node; 352 n = vmap_area_root.rb_node;
339 if (n) { 353 if (n) {
@@ -365,6 +379,8 @@ retry:
365 379
366 while (addr + size > first->va_start && addr + size <= vend) { 380 while (addr + size > first->va_start && addr + size <= vend) {
367 addr = ALIGN(first->va_end + PAGE_SIZE, align); 381 addr = ALIGN(first->va_end + PAGE_SIZE, align);
382 if (addr + size - 1 < addr)
383 goto overflow;
368 384
369 n = rb_next(&first->rb_node); 385 n = rb_next(&first->rb_node);
370 if (n) 386 if (n)
@@ -375,6 +391,7 @@ retry:
375 } 391 }
376found: 392found:
377 if (addr + size > vend) { 393 if (addr + size > vend) {
394overflow:
378 spin_unlock(&vmap_area_lock); 395 spin_unlock(&vmap_area_lock);
379 if (!purged) { 396 if (!purged) {
380 purge_vmap_area_lazy(); 397 purge_vmap_area_lazy();
@@ -498,6 +515,7 @@ static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
498 static DEFINE_SPINLOCK(purge_lock); 515 static DEFINE_SPINLOCK(purge_lock);
499 LIST_HEAD(valist); 516 LIST_HEAD(valist);
500 struct vmap_area *va; 517 struct vmap_area *va;
518 struct vmap_area *n_va;
501 int nr = 0; 519 int nr = 0;
502 520
503 /* 521 /*
@@ -537,7 +555,7 @@ static void __purge_vmap_area_lazy(unsigned long *start, unsigned long *end,
537 555
538 if (nr) { 556 if (nr) {
539 spin_lock(&vmap_area_lock); 557 spin_lock(&vmap_area_lock);
540 list_for_each_entry(va, &valist, purge_list) 558 list_for_each_entry_safe(va, n_va, &valist, purge_list)
541 __free_vmap_area(va); 559 __free_vmap_area(va);
542 spin_unlock(&vmap_area_lock); 560 spin_unlock(&vmap_area_lock);
543 } 561 }
@@ -982,6 +1000,32 @@ void *vm_map_ram(struct page **pages, unsigned int count, int node, pgprot_t pro
982} 1000}
983EXPORT_SYMBOL(vm_map_ram); 1001EXPORT_SYMBOL(vm_map_ram);
984 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
985void __init vmalloc_init(void) 1029void __init vmalloc_init(void)
986{ 1030{
987 struct vmap_area *va; 1031 struct vmap_area *va;
@@ -1009,9 +1053,63 @@ void __init vmalloc_init(void)
1009 vmap_initialized = true; 1053 vmap_initialized = true;
1010} 1054}
1011 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 */
1012void unmap_kernel_range(unsigned long addr, unsigned long size) 1108void unmap_kernel_range(unsigned long addr, unsigned long size)
1013{ 1109{
1014 unsigned long end = addr + size; 1110 unsigned long end = addr + size;
1111
1112 flush_cache_vunmap(addr, end);
1015 vunmap_page_range(addr, end); 1113 vunmap_page_range(addr, end);
1016 flush_tlb_kernel_range(addr, end); 1114 flush_tlb_kernel_range(addr, end);
1017} 1115}
@@ -1106,6 +1204,14 @@ struct vm_struct *__get_vm_area(unsigned long size, unsigned long flags,
1106} 1204}
1107EXPORT_SYMBOL_GPL(__get_vm_area); 1205EXPORT_SYMBOL_GPL(__get_vm_area);
1108 1206
1207struct vm_struct *__get_vm_area_caller(unsigned long size, unsigned long flags,
1208 unsigned long start, unsigned long end,
1209 void *caller)
1210{
1211 return __get_vm_area_node(size, flags, start, end, -1, GFP_KERNEL,
1212 caller);
1213}
1214
1109/** 1215/**
1110 * get_vm_area - reserve a contiguous kernel virtual area 1216 * get_vm_area - reserve a contiguous kernel virtual area
1111 * @size: size of the area 1217 * @size: size of the area
@@ -1249,6 +1355,7 @@ EXPORT_SYMBOL(vfree);
1249void vunmap(const void *addr) 1355void vunmap(const void *addr)
1250{ 1356{
1251 BUG_ON(in_interrupt()); 1357 BUG_ON(in_interrupt());
1358 might_sleep();
1252 __vunmap(addr, 0); 1359 __vunmap(addr, 0);
1253} 1360}
1254EXPORT_SYMBOL(vunmap); 1361EXPORT_SYMBOL(vunmap);
@@ -1268,6 +1375,8 @@ void *vmap(struct page **pages, unsigned int count,
1268{ 1375{
1269 struct vm_struct *area; 1376 struct vm_struct *area;
1270 1377
1378 might_sleep();
1379
1271 if (count > num_physpages) 1380 if (count > num_physpages)
1272 return NULL; 1381 return NULL;
1273 1382
diff --git a/mm/vmscan.c b/mm/vmscan.c
index cf8441345277..479e46719394 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -1262,7 +1262,6 @@ static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
1262 * Move the pages to the [file or anon] inactive list. 1262 * Move the pages to the [file or anon] inactive list.
1263 */ 1263 */
1264 pagevec_init(&pvec, 1); 1264 pagevec_init(&pvec, 1);
1265 pgmoved = 0;
1266 lru = LRU_BASE + file * LRU_FILE; 1265 lru = LRU_BASE + file * LRU_FILE;
1267 1266
1268 spin_lock_irq(&zone->lru_lock); 1267 spin_lock_irq(&zone->lru_lock);
@@ -1274,6 +1273,7 @@ static void shrink_active_list(unsigned long nr_pages, struct zone *zone,
1274 */ 1273 */
1275 reclaim_stat->recent_rotated[!!file] += pgmoved; 1274 reclaim_stat->recent_rotated[!!file] += pgmoved;
1276 1275
1276 pgmoved = 0;
1277 while (!list_empty(&l_inactive)) { 1277 while (!list_empty(&l_inactive)) {
1278 page = lru_to_page(&l_inactive); 1278 page = lru_to_page(&l_inactive);
1279 prefetchw_prev_lru_page(page, &l_inactive, flags); 1279 prefetchw_prev_lru_page(page, &l_inactive, flags);
@@ -1469,7 +1469,7 @@ static void shrink_zone(int priority, struct zone *zone,
1469 int file = is_file_lru(l); 1469 int file = is_file_lru(l);
1470 int scan; 1470 int scan;
1471 1471
1472 scan = zone_page_state(zone, NR_LRU_BASE + l); 1472 scan = zone_nr_pages(zone, sc, l);
1473 if (priority) { 1473 if (priority) {
1474 scan >>= priority; 1474 scan >>= priority;
1475 scan = (scan * percent[file]) / 100; 1475 scan = (scan * percent[file]) / 100;
@@ -2059,31 +2059,31 @@ static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
2059 int pass, struct scan_control *sc) 2059 int pass, struct scan_control *sc)
2060{ 2060{
2061 struct zone *zone; 2061 struct zone *zone;
2062 unsigned long nr_to_scan, ret = 0; 2062 unsigned long ret = 0;
2063 enum lru_list l;
2064 2063
2065 for_each_zone(zone) { 2064 for_each_zone(zone) {
2065 enum lru_list l;
2066 2066
2067 if (!populated_zone(zone)) 2067 if (!populated_zone(zone))
2068 continue; 2068 continue;
2069
2070 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY) 2069 if (zone_is_all_unreclaimable(zone) && prio != DEF_PRIORITY)
2071 continue; 2070 continue;
2072 2071
2073 for_each_evictable_lru(l) { 2072 for_each_evictable_lru(l) {
2073 enum zone_stat_item ls = NR_LRU_BASE + l;
2074 unsigned long lru_pages = zone_page_state(zone, ls);
2075
2074 /* For pass = 0, we don't shrink the active list */ 2076 /* For pass = 0, we don't shrink the active list */
2075 if (pass == 0 && 2077 if (pass == 0 && (l == LRU_ACTIVE_ANON ||
2076 (l == LRU_ACTIVE || l == LRU_ACTIVE_FILE)) 2078 l == LRU_ACTIVE_FILE))
2077 continue; 2079 continue;
2078 2080
2079 zone->lru[l].nr_scan += 2081 zone->lru[l].nr_scan += (lru_pages >> prio) + 1;
2080 (zone_page_state(zone, NR_LRU_BASE + l)
2081 >> prio) + 1;
2082 if (zone->lru[l].nr_scan >= nr_pages || pass > 3) { 2082 if (zone->lru[l].nr_scan >= nr_pages || pass > 3) {
2083 unsigned long nr_to_scan;
2084
2083 zone->lru[l].nr_scan = 0; 2085 zone->lru[l].nr_scan = 0;
2084 nr_to_scan = min(nr_pages, 2086 nr_to_scan = min(nr_pages, lru_pages);
2085 zone_page_state(zone,
2086 NR_LRU_BASE + l));
2087 ret += shrink_list(l, nr_to_scan, zone, 2087 ret += shrink_list(l, nr_to_scan, zone,
2088 sc, prio); 2088 sc, prio);
2089 if (ret >= nr_pages) 2089 if (ret >= nr_pages)
@@ -2091,7 +2091,6 @@ static unsigned long shrink_all_zones(unsigned long nr_pages, int prio,
2091 } 2091 }
2092 } 2092 }
2093 } 2093 }
2094
2095 return ret; 2094 return ret;
2096} 2095}
2097 2096
@@ -2114,7 +2113,6 @@ unsigned long shrink_all_memory(unsigned long nr_pages)
2114 .may_swap = 0, 2113 .may_swap = 0,
2115 .swap_cluster_max = nr_pages, 2114 .swap_cluster_max = nr_pages,
2116 .may_writepage = 1, 2115 .may_writepage = 1,
2117 .swappiness = vm_swappiness,
2118 .isolate_pages = isolate_pages_global, 2116 .isolate_pages = isolate_pages_global,
2119 }; 2117 };
2120 2118
@@ -2148,10 +2146,8 @@ unsigned long shrink_all_memory(unsigned long nr_pages)
2148 int prio; 2146 int prio;
2149 2147
2150 /* Force reclaiming mapped pages in the passes #3 and #4 */ 2148 /* Force reclaiming mapped pages in the passes #3 and #4 */
2151 if (pass > 2) { 2149 if (pass > 2)
2152 sc.may_swap = 1; 2150 sc.may_swap = 1;
2153 sc.swappiness = 100;
2154 }
2155 2151
2156 for (prio = DEF_PRIORITY; prio >= 0; prio--) { 2152 for (prio = DEF_PRIORITY; prio >= 0; prio--) {
2157 unsigned long nr_to_scan = nr_pages - ret; 2153 unsigned long nr_to_scan = nr_pages - ret;