proc: remove VMA rbtree use from nommu
[linux-2.6-block.git] / mm / nommu.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4
LT
2/*
3 * linux/mm/nommu.c
4 *
5 * Replacement code for mm functions to support CPU's that don't
6 * have any form of memory management unit (thus no virtual memory).
7 *
dd19d293 8 * See Documentation/admin-guide/mm/nommu-mmap.rst
1da177e4 9 *
8feae131 10 * Copyright (c) 2004-2008 David Howells <dhowells@redhat.com>
1da177e4
LT
11 * Copyright (c) 2000-2003 David McCullough <davidm@snapgear.com>
12 * Copyright (c) 2000-2001 D Jeff Dionne <jeff@uClinux.org>
13 * Copyright (c) 2002 Greg Ungerer <gerg@snapgear.com>
29c185e5 14 * Copyright (c) 2007-2010 Paul Mundt <lethal@linux-sh.org>
1da177e4
LT
15 */
16
b1de0d13
MH
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
b95f1b31 19#include <linux/export.h>
1da177e4 20#include <linux/mm.h>
6e84f315 21#include <linux/sched/mm.h>
615d6e87 22#include <linux/vmacache.h>
1da177e4
LT
23#include <linux/mman.h>
24#include <linux/swap.h>
25#include <linux/file.h>
26#include <linux/highmem.h>
27#include <linux/pagemap.h>
28#include <linux/slab.h>
29#include <linux/vmalloc.h>
1da177e4 30#include <linux/backing-dev.h>
3b32123d 31#include <linux/compiler.h>
1da177e4
LT
32#include <linux/mount.h>
33#include <linux/personality.h>
34#include <linux/security.h>
35#include <linux/syscalls.h>
120a795d 36#include <linux/audit.h>
b1de0d13 37#include <linux/printk.h>
1da177e4 38
7c0f6ba6 39#include <linux/uaccess.h>
1da177e4
LT
40#include <asm/tlb.h>
41#include <asm/tlbflush.h>
eb8cdec4 42#include <asm/mmu_context.h>
8feae131
DH
43#include "internal.h"
44
1da177e4 45void *high_memory;
944b6874 46EXPORT_SYMBOL(high_memory);
1da177e4
LT
47struct page *mem_map;
48unsigned long max_mapnr;
5b8bf307 49EXPORT_SYMBOL(max_mapnr);
4266c97a 50unsigned long highest_memmap_pfn;
fc4d5c29 51int sysctl_nr_trim_pages = CONFIG_NOMMU_INITIAL_TRIM_EXCESS;
1da177e4
LT
52int heap_stack_gap = 0;
53
33e5d769 54atomic_long_t mmap_pages_allocated;
8feae131 55
1da177e4 56EXPORT_SYMBOL(mem_map);
1da177e4 57
8feae131
DH
58/* list of mapped, potentially shareable regions */
59static struct kmem_cache *vm_region_jar;
60struct rb_root nommu_region_tree = RB_ROOT;
61DECLARE_RWSEM(nommu_region_sem);
1da177e4 62
f0f37e2f 63const struct vm_operations_struct generic_file_vm_ops = {
1da177e4
LT
64};
65
1da177e4
LT
66/*
67 * Return the total memory allocated for this pointer, not
68 * just what the caller asked for.
69 *
70 * Doesn't have to be accurate, i.e. may have races.
71 */
72unsigned int kobjsize(const void *objp)
73{
74 struct page *page;
75
4016a139
MH
76 /*
77 * If the object we have should not have ksize performed on it,
78 * return size of 0
79 */
5a1603be 80 if (!objp || !virt_addr_valid(objp))
6cfd53fc
PM
81 return 0;
82
83 page = virt_to_head_page(objp);
6cfd53fc
PM
84
85 /*
86 * If the allocator sets PageSlab, we know the pointer came from
87 * kmalloc().
88 */
1da177e4
LT
89 if (PageSlab(page))
90 return ksize(objp);
91
ab2e83ea
PM
92 /*
93 * If it's not a compound page, see if we have a matching VMA
94 * region. This test is intentionally done in reverse order,
95 * so if there's no VMA, we still fall through and hand back
96 * PAGE_SIZE for 0-order pages.
97 */
98 if (!PageCompound(page)) {
99 struct vm_area_struct *vma;
100
101 vma = find_vma(current->mm, (unsigned long)objp);
102 if (vma)
103 return vma->vm_end - vma->vm_start;
104 }
105
6cfd53fc
PM
106 /*
107 * The ksize() function is only guaranteed to work for pointers
5a1603be 108 * returned by kmalloc(). So handle arbitrary pointers here.
6cfd53fc 109 */
a50b854e 110 return page_size(page);
1da177e4
LT
111}
112
dfc2f91a
PM
113/**
114 * follow_pfn - look up PFN at a user virtual address
115 * @vma: memory mapping
116 * @address: user virtual address
117 * @pfn: location to store found PFN
118 *
119 * Only IO mappings and raw PFN mappings are allowed.
120 *
121 * Returns zero and the pfn at @pfn on success, -ve otherwise.
122 */
123int follow_pfn(struct vm_area_struct *vma, unsigned long address,
124 unsigned long *pfn)
125{
126 if (!(vma->vm_flags & (VM_IO | VM_PFNMAP)))
127 return -EINVAL;
128
129 *pfn = address >> PAGE_SHIFT;
130 return 0;
131}
132EXPORT_SYMBOL(follow_pfn);
133
f1c4069e 134LIST_HEAD(vmap_area_list);
1da177e4 135
b3bdda02 136void vfree(const void *addr)
1da177e4
LT
137{
138 kfree(addr);
139}
b5073173 140EXPORT_SYMBOL(vfree);
1da177e4 141
88dca4ca 142void *__vmalloc(unsigned long size, gfp_t gfp_mask)
1da177e4
LT
143{
144 /*
8518609d
RD
145 * You can't specify __GFP_HIGHMEM with kmalloc() since kmalloc()
146 * returns only a logical address.
1da177e4 147 */
84097518 148 return kmalloc(size, (gfp_mask | __GFP_COMP) & ~__GFP_HIGHMEM);
1da177e4 149}
b5073173 150EXPORT_SYMBOL(__vmalloc);
1da177e4 151
041de93f
CH
152void *__vmalloc_node_range(unsigned long size, unsigned long align,
153 unsigned long start, unsigned long end, gfp_t gfp_mask,
154 pgprot_t prot, unsigned long vm_flags, int node,
155 const void *caller)
156{
157 return __vmalloc(size, gfp_mask);
158}
159
2b905948
CH
160void *__vmalloc_node(unsigned long size, unsigned long align, gfp_t gfp_mask,
161 int node, const void *caller)
a7c3e901 162{
2b905948 163 return __vmalloc(size, gfp_mask);
a7c3e901
MH
164}
165
ed81745a 166static void *__vmalloc_user_flags(unsigned long size, gfp_t flags)
f905bc44
PM
167{
168 void *ret;
169
88dca4ca 170 ret = __vmalloc(size, flags);
f905bc44
PM
171 if (ret) {
172 struct vm_area_struct *vma;
173
d8ed45c5 174 mmap_write_lock(current->mm);
f905bc44
PM
175 vma = find_vma(current->mm, (unsigned long)ret);
176 if (vma)
177 vma->vm_flags |= VM_USERMAP;
d8ed45c5 178 mmap_write_unlock(current->mm);
f905bc44
PM
179 }
180
181 return ret;
182}
ed81745a
AN
183
184void *vmalloc_user(unsigned long size)
185{
186 return __vmalloc_user_flags(size, GFP_KERNEL | __GFP_ZERO);
187}
f905bc44
PM
188EXPORT_SYMBOL(vmalloc_user);
189
b3bdda02 190struct page *vmalloc_to_page(const void *addr)
1da177e4
LT
191{
192 return virt_to_page(addr);
193}
b5073173 194EXPORT_SYMBOL(vmalloc_to_page);
1da177e4 195
b3bdda02 196unsigned long vmalloc_to_pfn(const void *addr)
1da177e4
LT
197{
198 return page_to_pfn(virt_to_page(addr));
199}
b5073173 200EXPORT_SYMBOL(vmalloc_to_pfn);
1da177e4
LT
201
202long vread(char *buf, char *addr, unsigned long count)
203{
9bde916b
CG
204 /* Don't allow overflow */
205 if ((unsigned long) buf + count < count)
206 count = -(unsigned long) buf;
207
1da177e4
LT
208 memcpy(buf, addr, count);
209 return count;
210}
211
1da177e4 212/*
e1c05067 213 * vmalloc - allocate virtually contiguous memory
1da177e4
LT
214 *
215 * @size: allocation size
216 *
217 * Allocate enough pages to cover @size from the page level
e1c05067 218 * allocator and map them into contiguous kernel virtual space.
1da177e4 219 *
c1c8897f 220 * For tight control over page level allocator and protection flags
1da177e4
LT
221 * use __vmalloc() instead.
222 */
223void *vmalloc(unsigned long size)
224{
176056fd 225 return __vmalloc(size, GFP_KERNEL);
1da177e4 226}
f6138882
AM
227EXPORT_SYMBOL(vmalloc);
228
0fc74d82
LT
229void *vmalloc_huge(unsigned long size, gfp_t gfp_mask) __weak __alias(__vmalloc);
230
e1ca7788 231/*
e1c05067 232 * vzalloc - allocate virtually contiguous memory with zero fill
e1ca7788
DY
233 *
234 * @size: allocation size
235 *
236 * Allocate enough pages to cover @size from the page level
e1c05067 237 * allocator and map them into contiguous kernel virtual space.
e1ca7788
DY
238 * The memory allocated is set to zero.
239 *
240 * For tight control over page level allocator and protection flags
241 * use __vmalloc() instead.
242 */
243void *vzalloc(unsigned long size)
244{
176056fd 245 return __vmalloc(size, GFP_KERNEL | __GFP_ZERO);
e1ca7788
DY
246}
247EXPORT_SYMBOL(vzalloc);
248
249/**
250 * vmalloc_node - allocate memory on a specific node
251 * @size: allocation size
252 * @node: numa node
253 *
254 * Allocate enough pages to cover @size from the page level
255 * allocator and map them into contiguous kernel virtual space.
256 *
257 * For tight control over page level allocator and protection flags
258 * use __vmalloc() instead.
259 */
f6138882
AM
260void *vmalloc_node(unsigned long size, int node)
261{
262 return vmalloc(size);
263}
9a14f653 264EXPORT_SYMBOL(vmalloc_node);
e1ca7788
DY
265
266/**
267 * vzalloc_node - allocate memory on a specific node with zero fill
268 * @size: allocation size
269 * @node: numa node
270 *
271 * Allocate enough pages to cover @size from the page level
272 * allocator and map them into contiguous kernel virtual space.
273 * The memory allocated is set to zero.
274 *
275 * For tight control over page level allocator and protection flags
276 * use __vmalloc() instead.
277 */
278void *vzalloc_node(unsigned long size, int node)
279{
280 return vzalloc(size);
281}
282EXPORT_SYMBOL(vzalloc_node);
1da177e4 283
b5073173
PM
284/**
285 * vmalloc_32 - allocate virtually contiguous memory (32bit addressable)
1da177e4
LT
286 * @size: allocation size
287 *
288 * Allocate enough 32bit PA addressable pages to cover @size from the
e1c05067 289 * page level allocator and map them into contiguous kernel virtual space.
1da177e4
LT
290 */
291void *vmalloc_32(unsigned long size)
292{
88dca4ca 293 return __vmalloc(size, GFP_KERNEL);
1da177e4 294}
b5073173
PM
295EXPORT_SYMBOL(vmalloc_32);
296
297/**
298 * vmalloc_32_user - allocate zeroed virtually contiguous 32bit memory
299 * @size: allocation size
300 *
301 * The resulting memory area is 32bit addressable and zeroed so it can be
302 * mapped to userspace without leaking data.
f905bc44
PM
303 *
304 * VM_USERMAP is set on the corresponding VMA so that subsequent calls to
305 * remap_vmalloc_range() are permissible.
b5073173
PM
306 */
307void *vmalloc_32_user(unsigned long size)
308{
f905bc44
PM
309 /*
310 * We'll have to sort out the ZONE_DMA bits for 64-bit,
311 * but for now this can simply use vmalloc_user() directly.
312 */
313 return vmalloc_user(size);
b5073173
PM
314}
315EXPORT_SYMBOL(vmalloc_32_user);
1da177e4
LT
316
317void *vmap(struct page **pages, unsigned int count, unsigned long flags, pgprot_t prot)
318{
319 BUG();
320 return NULL;
321}
b5073173 322EXPORT_SYMBOL(vmap);
1da177e4 323
b3bdda02 324void vunmap(const void *addr)
1da177e4
LT
325{
326 BUG();
327}
b5073173 328EXPORT_SYMBOL(vunmap);
1da177e4 329
d4efd79a 330void *vm_map_ram(struct page **pages, unsigned int count, int node)
eb6434d9
PM
331{
332 BUG();
333 return NULL;
334}
335EXPORT_SYMBOL(vm_map_ram);
336
337void vm_unmap_ram(const void *mem, unsigned int count)
338{
339 BUG();
340}
341EXPORT_SYMBOL(vm_unmap_ram);
342
343void vm_unmap_aliases(void)
344{
345}
346EXPORT_SYMBOL_GPL(vm_unmap_aliases);
347
29c185e5
PM
348void free_vm_area(struct vm_struct *area)
349{
350 BUG();
351}
352EXPORT_SYMBOL_GPL(free_vm_area);
353
b5073173
PM
354int vm_insert_page(struct vm_area_struct *vma, unsigned long addr,
355 struct page *page)
356{
357 return -EINVAL;
358}
359EXPORT_SYMBOL(vm_insert_page);
360
a667d745
SJ
361int vm_map_pages(struct vm_area_struct *vma, struct page **pages,
362 unsigned long num)
363{
364 return -EINVAL;
365}
366EXPORT_SYMBOL(vm_map_pages);
367
368int vm_map_pages_zero(struct vm_area_struct *vma, struct page **pages,
369 unsigned long num)
370{
371 return -EINVAL;
372}
373EXPORT_SYMBOL(vm_map_pages_zero);
374
1da177e4
LT
375/*
376 * sys_brk() for the most part doesn't need the global kernel
377 * lock, except when an application is doing something nasty
378 * like trying to un-brk an area that has already been mapped
379 * to a regular file. in this case, the unmapping will need
380 * to invoke file system routines that need the global lock.
381 */
6a6160a7 382SYSCALL_DEFINE1(brk, unsigned long, brk)
1da177e4
LT
383{
384 struct mm_struct *mm = current->mm;
385
386 if (brk < mm->start_brk || brk > mm->context.end_brk)
387 return mm->brk;
388
389 if (mm->brk == brk)
390 return mm->brk;
391
392 /*
393 * Always allow shrinking brk
394 */
395 if (brk <= mm->brk) {
396 mm->brk = brk;
397 return brk;
398 }
399
400 /*
401 * Ok, looks good - let it rip.
402 */
a75a2df6 403 flush_icache_user_range(mm->brk, brk);
1da177e4
LT
404 return mm->brk = brk;
405}
406
8feae131 407/*
3edf41d8 408 * initialise the percpu counter for VM and region record slabs
8feae131
DH
409 */
410void __init mmap_init(void)
1da177e4 411{
00a62ce9
KM
412 int ret;
413
908c7f19 414 ret = percpu_counter_init(&vm_committed_as, 0, GFP_KERNEL);
00a62ce9 415 VM_BUG_ON(ret);
5d097056 416 vm_region_jar = KMEM_CACHE(vm_region, SLAB_PANIC|SLAB_ACCOUNT);
1da177e4 417}
1da177e4 418
3034097a 419/*
8feae131
DH
420 * validate the region tree
421 * - the caller must hold the region lock
3034097a 422 */
8feae131
DH
423#ifdef CONFIG_DEBUG_NOMMU_REGIONS
424static noinline void validate_nommu_regions(void)
3034097a 425{
8feae131
DH
426 struct vm_region *region, *last;
427 struct rb_node *p, *lastp;
3034097a 428
8feae131
DH
429 lastp = rb_first(&nommu_region_tree);
430 if (!lastp)
431 return;
432
433 last = rb_entry(lastp, struct vm_region, vm_rb);
c9427bc0
GT
434 BUG_ON(last->vm_end <= last->vm_start);
435 BUG_ON(last->vm_top < last->vm_end);
8feae131
DH
436
437 while ((p = rb_next(lastp))) {
438 region = rb_entry(p, struct vm_region, vm_rb);
439 last = rb_entry(lastp, struct vm_region, vm_rb);
440
c9427bc0
GT
441 BUG_ON(region->vm_end <= region->vm_start);
442 BUG_ON(region->vm_top < region->vm_end);
443 BUG_ON(region->vm_start < last->vm_top);
3034097a 444
8feae131
DH
445 lastp = p;
446 }
3034097a 447}
8feae131 448#else
33e5d769
DH
449static void validate_nommu_regions(void)
450{
451}
8feae131 452#endif
3034097a
DH
453
454/*
8feae131 455 * add a region into the global tree
3034097a 456 */
8feae131 457static void add_nommu_region(struct vm_region *region)
3034097a 458{
8feae131
DH
459 struct vm_region *pregion;
460 struct rb_node **p, *parent;
3034097a 461
8feae131
DH
462 validate_nommu_regions();
463
8feae131
DH
464 parent = NULL;
465 p = &nommu_region_tree.rb_node;
466 while (*p) {
467 parent = *p;
468 pregion = rb_entry(parent, struct vm_region, vm_rb);
469 if (region->vm_start < pregion->vm_start)
470 p = &(*p)->rb_left;
471 else if (region->vm_start > pregion->vm_start)
472 p = &(*p)->rb_right;
473 else if (pregion == region)
474 return;
475 else
476 BUG();
3034097a
DH
477 }
478
8feae131
DH
479 rb_link_node(&region->vm_rb, parent, p);
480 rb_insert_color(&region->vm_rb, &nommu_region_tree);
3034097a 481
8feae131 482 validate_nommu_regions();
3034097a 483}
3034097a 484
930e652a 485/*
8feae131 486 * delete a region from the global tree
930e652a 487 */
8feae131 488static void delete_nommu_region(struct vm_region *region)
930e652a 489{
8feae131 490 BUG_ON(!nommu_region_tree.rb_node);
930e652a 491
8feae131
DH
492 validate_nommu_regions();
493 rb_erase(&region->vm_rb, &nommu_region_tree);
494 validate_nommu_regions();
57c8f63e
GU
495}
496
6fa5f80b 497/*
8feae131 498 * free a contiguous series of pages
6fa5f80b 499 */
8feae131 500static void free_page_series(unsigned long from, unsigned long to)
6fa5f80b 501{
8feae131 502 for (; from < to; from += PAGE_SIZE) {
9330723c 503 struct page *page = virt_to_page((void *)from);
8feae131 504
33e5d769 505 atomic_long_dec(&mmap_pages_allocated);
8feae131 506 put_page(page);
6fa5f80b 507 }
6fa5f80b
DH
508}
509
3034097a 510/*
8feae131 511 * release a reference to a region
33e5d769 512 * - the caller must hold the region semaphore for writing, which this releases
dd8632a1 513 * - the region may not have been added to the tree yet, in which case vm_top
8feae131 514 * will equal vm_start
3034097a 515 */
8feae131
DH
516static void __put_nommu_region(struct vm_region *region)
517 __releases(nommu_region_sem)
1da177e4 518{
8feae131 519 BUG_ON(!nommu_region_tree.rb_node);
1da177e4 520
1e2ae599 521 if (--region->vm_usage == 0) {
dd8632a1 522 if (region->vm_top > region->vm_start)
8feae131
DH
523 delete_nommu_region(region);
524 up_write(&nommu_region_sem);
525
526 if (region->vm_file)
527 fput(region->vm_file);
528
529 /* IO memory and memory shared directly out of the pagecache
530 * from ramfs/tmpfs mustn't be released here */
22cc877b 531 if (region->vm_flags & VM_MAPPED_COPY)
dd8632a1 532 free_page_series(region->vm_start, region->vm_top);
8feae131
DH
533 kmem_cache_free(vm_region_jar, region);
534 } else {
535 up_write(&nommu_region_sem);
1da177e4 536 }
8feae131 537}
1da177e4 538
8feae131
DH
539/*
540 * release a reference to a region
541 */
542static void put_nommu_region(struct vm_region *region)
543{
544 down_write(&nommu_region_sem);
545 __put_nommu_region(region);
1da177e4
LT
546}
547
d4af56c5
LH
548void vma_mas_store(struct vm_area_struct *vma, struct ma_state *mas)
549{
550 mas_set_range(mas, vma->vm_start, vma->vm_end - 1);
551 mas_store_prealloc(mas, vma);
552}
553
554void vma_mas_remove(struct vm_area_struct *vma, struct ma_state *mas)
555{
556 mas->index = vma->vm_start;
557 mas->last = vma->vm_end - 1;
558 mas_store_prealloc(mas, NULL);
559}
560
3034097a 561/*
8feae131
DH
562 * add a VMA into a process's mm_struct in the appropriate place in the list
563 * and tree and add to the address space's page tree also if not an anonymous
564 * page
c1e8d7c6 565 * - should be called with mm->mmap_lock held writelocked
3034097a 566 */
8feae131 567static void add_vma_to_mm(struct mm_struct *mm, struct vm_area_struct *vma)
1da177e4 568{
6038def0 569 struct vm_area_struct *pvma, *prev;
1da177e4 570 struct address_space *mapping;
6038def0 571 struct rb_node **p, *parent, *rb_prev;
8feae131 572
8feae131
DH
573 BUG_ON(!vma->vm_region);
574
575 mm->map_count++;
576 vma->vm_mm = mm;
1da177e4
LT
577
578 /* add the VMA to the mapping */
579 if (vma->vm_file) {
580 mapping = vma->vm_file->f_mapping;
581
83cde9e8 582 i_mmap_lock_write(mapping);
1da177e4 583 flush_dcache_mmap_lock(mapping);
6b2dbba8 584 vma_interval_tree_insert(vma, &mapping->i_mmap);
1da177e4 585 flush_dcache_mmap_unlock(mapping);
83cde9e8 586 i_mmap_unlock_write(mapping);
1da177e4
LT
587 }
588
8feae131 589 /* add the VMA to the tree */
6038def0 590 parent = rb_prev = NULL;
8feae131 591 p = &mm->mm_rb.rb_node;
1da177e4
LT
592 while (*p) {
593 parent = *p;
594 pvma = rb_entry(parent, struct vm_area_struct, vm_rb);
595
8feae131
DH
596 /* sort by: start addr, end addr, VMA struct addr in that order
597 * (the latter is necessary as we may get identical VMAs) */
598 if (vma->vm_start < pvma->vm_start)
1da177e4 599 p = &(*p)->rb_left;
6038def0
NK
600 else if (vma->vm_start > pvma->vm_start) {
601 rb_prev = parent;
1da177e4 602 p = &(*p)->rb_right;
6038def0 603 } else if (vma->vm_end < pvma->vm_end)
8feae131 604 p = &(*p)->rb_left;
6038def0
NK
605 else if (vma->vm_end > pvma->vm_end) {
606 rb_prev = parent;
8feae131 607 p = &(*p)->rb_right;
6038def0 608 } else if (vma < pvma)
8feae131 609 p = &(*p)->rb_left;
6038def0
NK
610 else if (vma > pvma) {
611 rb_prev = parent;
8feae131 612 p = &(*p)->rb_right;
6038def0 613 } else
8feae131 614 BUG();
1da177e4
LT
615 }
616
617 rb_link_node(&vma->vm_rb, parent, p);
8feae131
DH
618 rb_insert_color(&vma->vm_rb, &mm->mm_rb);
619
620 /* add VMA to the VMA list also */
6038def0
NK
621 prev = NULL;
622 if (rb_prev)
623 prev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
8feae131 624
aba6dfb7 625 __vma_link_list(mm, vma, prev);
1da177e4
LT
626}
627
3034097a 628/*
8feae131 629 * delete a VMA from its owning mm_struct and address space
3034097a 630 */
8feae131 631static void delete_vma_from_mm(struct vm_area_struct *vma)
1da177e4 632{
615d6e87 633 int i;
1da177e4 634 struct address_space *mapping;
8feae131 635 struct mm_struct *mm = vma->vm_mm;
615d6e87 636 struct task_struct *curr = current;
8feae131 637
8feae131 638 mm->map_count--;
615d6e87
DB
639 for (i = 0; i < VMACACHE_SIZE; i++) {
640 /* if the vma is cached, invalidate the entire cache */
314ff785 641 if (curr->vmacache.vmas[i] == vma) {
e020d5bd 642 vmacache_invalidate(mm);
615d6e87
DB
643 break;
644 }
645 }
1da177e4
LT
646
647 /* remove the VMA from the mapping */
648 if (vma->vm_file) {
649 mapping = vma->vm_file->f_mapping;
650
83cde9e8 651 i_mmap_lock_write(mapping);
1da177e4 652 flush_dcache_mmap_lock(mapping);
6b2dbba8 653 vma_interval_tree_remove(vma, &mapping->i_mmap);
1da177e4 654 flush_dcache_mmap_unlock(mapping);
83cde9e8 655 i_mmap_unlock_write(mapping);
1da177e4
LT
656 }
657
8feae131
DH
658 /* remove from the MM's tree and list */
659 rb_erase(&vma->vm_rb, &mm->mm_rb);
b951bf2c 660
1b9fc5b2 661 __vma_unlink_list(mm, vma);
8feae131
DH
662}
663
664/*
665 * destroy a VMA record
666 */
667static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
668{
8feae131
DH
669 if (vma->vm_ops && vma->vm_ops->close)
670 vma->vm_ops->close(vma);
e9714acf 671 if (vma->vm_file)
8feae131 672 fput(vma->vm_file);
8feae131 673 put_nommu_region(vma->vm_region);
3928d4f5 674 vm_area_free(vma);
8feae131
DH
675}
676
677/*
678 * look up the first VMA in which addr resides, NULL if none
c1e8d7c6 679 * - should be called with mm->mmap_lock at least held readlocked
8feae131
DH
680 */
681struct vm_area_struct *find_vma(struct mm_struct *mm, unsigned long addr)
682{
683 struct vm_area_struct *vma;
8feae131
DH
684
685 /* check the cache first */
615d6e87
DB
686 vma = vmacache_find(mm, addr);
687 if (likely(vma))
8feae131
DH
688 return vma;
689
e922c4c5 690 /* trawl the list (there may be multiple mappings in which addr
8feae131 691 * resides) */
e922c4c5 692 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8feae131
DH
693 if (vma->vm_start > addr)
694 return NULL;
695 if (vma->vm_end > addr) {
615d6e87 696 vmacache_update(addr, vma);
8feae131
DH
697 return vma;
698 }
699 }
700
701 return NULL;
702}
703EXPORT_SYMBOL(find_vma);
704
705/*
706 * find a VMA
707 * - we don't extend stack VMAs under NOMMU conditions
708 */
709struct vm_area_struct *find_extend_vma(struct mm_struct *mm, unsigned long addr)
710{
7561e8ca 711 return find_vma(mm, addr);
8feae131
DH
712}
713
714/*
715 * expand a stack to a given address
716 * - not supported under NOMMU conditions
717 */
718int expand_stack(struct vm_area_struct *vma, unsigned long address)
719{
720 return -ENOMEM;
721}
722
723/*
724 * look up the first VMA exactly that exactly matches addr
c1e8d7c6 725 * - should be called with mm->mmap_lock at least held readlocked
8feae131
DH
726 */
727static struct vm_area_struct *find_vma_exact(struct mm_struct *mm,
728 unsigned long addr,
729 unsigned long len)
730{
731 struct vm_area_struct *vma;
8feae131
DH
732 unsigned long end = addr + len;
733
734 /* check the cache first */
615d6e87
DB
735 vma = vmacache_find_exact(mm, addr, end);
736 if (vma)
8feae131
DH
737 return vma;
738
e922c4c5 739 /* trawl the list (there may be multiple mappings in which addr
8feae131 740 * resides) */
e922c4c5 741 for (vma = mm->mmap; vma; vma = vma->vm_next) {
8feae131
DH
742 if (vma->vm_start < addr)
743 continue;
744 if (vma->vm_start > addr)
745 return NULL;
746 if (vma->vm_end == end) {
615d6e87 747 vmacache_update(addr, vma);
8feae131
DH
748 return vma;
749 }
750 }
751
752 return NULL;
1da177e4
LT
753}
754
755/*
756 * determine whether a mapping should be permitted and, if so, what sort of
757 * mapping we're capable of supporting
758 */
759static int validate_mmap_request(struct file *file,
760 unsigned long addr,
761 unsigned long len,
762 unsigned long prot,
763 unsigned long flags,
764 unsigned long pgoff,
765 unsigned long *_capabilities)
766{
8feae131 767 unsigned long capabilities, rlen;
1da177e4
LT
768 int ret;
769
770 /* do the simple checks first */
22cc877b 771 if (flags & MAP_FIXED)
1da177e4 772 return -EINVAL;
1da177e4
LT
773
774 if ((flags & MAP_TYPE) != MAP_PRIVATE &&
775 (flags & MAP_TYPE) != MAP_SHARED)
776 return -EINVAL;
777
f81cff0d 778 if (!len)
1da177e4
LT
779 return -EINVAL;
780
f81cff0d 781 /* Careful about overflows.. */
8feae131
DH
782 rlen = PAGE_ALIGN(len);
783 if (!rlen || rlen > TASK_SIZE)
f81cff0d
MF
784 return -ENOMEM;
785
1da177e4 786 /* offset overflow? */
8feae131 787 if ((pgoff + (rlen >> PAGE_SHIFT)) < pgoff)
f81cff0d 788 return -EOVERFLOW;
1da177e4
LT
789
790 if (file) {
1da177e4 791 /* files must support mmap */
72c2d531 792 if (!file->f_op->mmap)
1da177e4
LT
793 return -ENODEV;
794
795 /* work out if what we've got could possibly be shared
796 * - we support chardevs that provide their own "memory"
797 * - we support files/blockdevs that are memory backed
798 */
b4caecd4
CH
799 if (file->f_op->mmap_capabilities) {
800 capabilities = file->f_op->mmap_capabilities(file);
801 } else {
1da177e4
LT
802 /* no explicit capabilities set, so assume some
803 * defaults */
496ad9aa 804 switch (file_inode(file)->i_mode & S_IFMT) {
1da177e4
LT
805 case S_IFREG:
806 case S_IFBLK:
b4caecd4 807 capabilities = NOMMU_MAP_COPY;
1da177e4
LT
808 break;
809
810 case S_IFCHR:
811 capabilities =
b4caecd4
CH
812 NOMMU_MAP_DIRECT |
813 NOMMU_MAP_READ |
814 NOMMU_MAP_WRITE;
1da177e4
LT
815 break;
816
817 default:
818 return -EINVAL;
819 }
820 }
821
822 /* eliminate any capabilities that we can't support on this
823 * device */
824 if (!file->f_op->get_unmapped_area)
b4caecd4 825 capabilities &= ~NOMMU_MAP_DIRECT;
6e242a1c 826 if (!(file->f_mode & FMODE_CAN_READ))
b4caecd4 827 capabilities &= ~NOMMU_MAP_COPY;
1da177e4 828
28d7a6ae
GY
829 /* The file shall have been opened with read permission. */
830 if (!(file->f_mode & FMODE_READ))
831 return -EACCES;
832
1da177e4
LT
833 if (flags & MAP_SHARED) {
834 /* do checks for writing, appending and locking */
835 if ((prot & PROT_WRITE) &&
836 !(file->f_mode & FMODE_WRITE))
837 return -EACCES;
838
496ad9aa 839 if (IS_APPEND(file_inode(file)) &&
1da177e4
LT
840 (file->f_mode & FMODE_WRITE))
841 return -EACCES;
842
b4caecd4 843 if (!(capabilities & NOMMU_MAP_DIRECT))
1da177e4
LT
844 return -ENODEV;
845
1da177e4 846 /* we mustn't privatise shared mappings */
b4caecd4 847 capabilities &= ~NOMMU_MAP_COPY;
ac714904 848 } else {
1da177e4
LT
849 /* we're going to read the file into private memory we
850 * allocate */
b4caecd4 851 if (!(capabilities & NOMMU_MAP_COPY))
1da177e4
LT
852 return -ENODEV;
853
854 /* we don't permit a private writable mapping to be
855 * shared with the backing device */
856 if (prot & PROT_WRITE)
b4caecd4 857 capabilities &= ~NOMMU_MAP_DIRECT;
1da177e4
LT
858 }
859
b4caecd4
CH
860 if (capabilities & NOMMU_MAP_DIRECT) {
861 if (((prot & PROT_READ) && !(capabilities & NOMMU_MAP_READ)) ||
862 ((prot & PROT_WRITE) && !(capabilities & NOMMU_MAP_WRITE)) ||
863 ((prot & PROT_EXEC) && !(capabilities & NOMMU_MAP_EXEC))
3c7b2045 864 ) {
b4caecd4 865 capabilities &= ~NOMMU_MAP_DIRECT;
3c7b2045 866 if (flags & MAP_SHARED) {
22cc877b 867 pr_warn("MAP_SHARED not completely supported on !MMU\n");
3c7b2045
BS
868 return -EINVAL;
869 }
870 }
871 }
872
1da177e4
LT
873 /* handle executable mappings and implied executable
874 * mappings */
90f8572b 875 if (path_noexec(&file->f_path)) {
1da177e4
LT
876 if (prot & PROT_EXEC)
877 return -EPERM;
ac714904 878 } else if ((prot & PROT_READ) && !(prot & PROT_EXEC)) {
1da177e4
LT
879 /* handle implication of PROT_EXEC by PROT_READ */
880 if (current->personality & READ_IMPLIES_EXEC) {
b4caecd4 881 if (capabilities & NOMMU_MAP_EXEC)
1da177e4
LT
882 prot |= PROT_EXEC;
883 }
ac714904 884 } else if ((prot & PROT_READ) &&
1da177e4 885 (prot & PROT_EXEC) &&
b4caecd4 886 !(capabilities & NOMMU_MAP_EXEC)
1da177e4
LT
887 ) {
888 /* backing file is not executable, try to copy */
b4caecd4 889 capabilities &= ~NOMMU_MAP_DIRECT;
1da177e4 890 }
ac714904 891 } else {
1da177e4
LT
892 /* anonymous mappings are always memory backed and can be
893 * privately mapped
894 */
b4caecd4 895 capabilities = NOMMU_MAP_COPY;
1da177e4
LT
896
897 /* handle PROT_EXEC implication by PROT_READ */
898 if ((prot & PROT_READ) &&
899 (current->personality & READ_IMPLIES_EXEC))
900 prot |= PROT_EXEC;
901 }
902
903 /* allow the security API to have its say */
e5467859 904 ret = security_mmap_addr(addr);
1da177e4
LT
905 if (ret < 0)
906 return ret;
907
908 /* looks okay */
909 *_capabilities = capabilities;
910 return 0;
911}
912
913/*
914 * we've determined that we can make the mapping, now translate what we
915 * now know into VMA flags
916 */
917static unsigned long determine_vm_flags(struct file *file,
918 unsigned long prot,
919 unsigned long flags,
920 unsigned long capabilities)
921{
922 unsigned long vm_flags;
923
e6bfb709 924 vm_flags = calc_vm_prot_bits(prot, 0) | calc_vm_flag_bits(flags);
1da177e4
LT
925 /* vm_flags |= mm->def_flags; */
926
b4caecd4 927 if (!(capabilities & NOMMU_MAP_DIRECT)) {
1da177e4 928 /* attempt to share read-only copies of mapped file chunks */
3c7b2045 929 vm_flags |= VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
1da177e4
LT
930 if (file && !(prot & PROT_WRITE))
931 vm_flags |= VM_MAYSHARE;
3c7b2045 932 } else {
1da177e4
LT
933 /* overlay a shareable mapping on the backing device or inode
934 * if possible - used for chardevs, ramfs/tmpfs/shmfs and
935 * romfs/cramfs */
b4caecd4 936 vm_flags |= VM_MAYSHARE | (capabilities & NOMMU_VMFLAGS);
1da177e4 937 if (flags & MAP_SHARED)
3c7b2045 938 vm_flags |= VM_SHARED;
1da177e4
LT
939 }
940
941 /* refuse to let anyone share private mappings with this process if
942 * it's being traced - otherwise breakpoints set in it may interfere
943 * with another untraced process
944 */
a288eecc 945 if ((flags & MAP_PRIVATE) && current->ptrace)
1da177e4
LT
946 vm_flags &= ~VM_MAYSHARE;
947
948 return vm_flags;
949}
950
951/*
8feae131
DH
952 * set up a shared mapping on a file (the driver or filesystem provides and
953 * pins the storage)
1da177e4 954 */
8feae131 955static int do_mmap_shared_file(struct vm_area_struct *vma)
1da177e4
LT
956{
957 int ret;
958
f74ac015 959 ret = call_mmap(vma->vm_file, vma);
dd8632a1
PM
960 if (ret == 0) {
961 vma->vm_region->vm_top = vma->vm_region->vm_end;
645d83c5 962 return 0;
dd8632a1 963 }
1da177e4
LT
964 if (ret != -ENOSYS)
965 return ret;
966
3fa30460
DH
967 /* getting -ENOSYS indicates that direct mmap isn't possible (as
968 * opposed to tried but failed) so we can only give a suitable error as
969 * it's not possible to make a private copy if MAP_SHARED was given */
1da177e4
LT
970 return -ENODEV;
971}
972
973/*
974 * set up a private mapping or an anonymous shared mapping
975 */
8feae131
DH
976static int do_mmap_private(struct vm_area_struct *vma,
977 struct vm_region *region,
645d83c5
DH
978 unsigned long len,
979 unsigned long capabilities)
1da177e4 980{
dbc8358c 981 unsigned long total, point;
1da177e4 982 void *base;
8feae131 983 int ret, order;
1da177e4
LT
984
985 /* invoke the file's mapping function so that it can keep track of
986 * shared mappings on devices or memory
987 * - VM_MAYSHARE will be set if it may attempt to share
988 */
b4caecd4 989 if (capabilities & NOMMU_MAP_DIRECT) {
f74ac015 990 ret = call_mmap(vma->vm_file, vma);
dd8632a1 991 if (ret == 0) {
1da177e4 992 /* shouldn't return success if we're not sharing */
dd8632a1
PM
993 BUG_ON(!(vma->vm_flags & VM_MAYSHARE));
994 vma->vm_region->vm_top = vma->vm_region->vm_end;
645d83c5 995 return 0;
1da177e4 996 }
dd8632a1
PM
997 if (ret != -ENOSYS)
998 return ret;
1da177e4
LT
999
1000 /* getting an ENOSYS error indicates that direct mmap isn't
1001 * possible (as opposed to tried but failed) so we'll try to
1002 * make a private copy of the data and map that instead */
1003 }
1004
8feae131 1005
1da177e4
LT
1006 /* allocate some memory to hold the mapping
1007 * - note that this may not return a page-aligned address if the object
1008 * we're allocating is smaller than a page
1009 */
f67d9b15 1010 order = get_order(len);
8feae131 1011 total = 1 << order;
f67d9b15 1012 point = len >> PAGE_SHIFT;
dd8632a1 1013
dbc8358c 1014 /* we don't want to allocate a power-of-2 sized page set */
22cc877b 1015 if (sysctl_nr_trim_pages && total - point >= sysctl_nr_trim_pages)
dbc8358c 1016 total = point;
8feae131 1017
da616534 1018 base = alloc_pages_exact(total << PAGE_SHIFT, GFP_KERNEL);
dbc8358c
JK
1019 if (!base)
1020 goto enomem;
1021
1022 atomic_long_add(total, &mmap_pages_allocated);
1da177e4 1023
8feae131
DH
1024 region->vm_flags = vma->vm_flags |= VM_MAPPED_COPY;
1025 region->vm_start = (unsigned long) base;
f67d9b15 1026 region->vm_end = region->vm_start + len;
dd8632a1 1027 region->vm_top = region->vm_start + (total << PAGE_SHIFT);
8feae131
DH
1028
1029 vma->vm_start = region->vm_start;
1030 vma->vm_end = region->vm_start + len;
1da177e4
LT
1031
1032 if (vma->vm_file) {
1033 /* read the contents of a file into the copy */
1da177e4
LT
1034 loff_t fpos;
1035
1036 fpos = vma->vm_pgoff;
1037 fpos <<= PAGE_SHIFT;
1038
b4bf802a 1039 ret = kernel_read(vma->vm_file, base, len, &fpos);
1da177e4
LT
1040 if (ret < 0)
1041 goto error_free;
1042
1043 /* clear the last little bit */
f67d9b15
BL
1044 if (ret < len)
1045 memset(base + ret, 0, len - ret);
1da177e4 1046
bfd40eaf
KS
1047 } else {
1048 vma_set_anonymous(vma);
1da177e4
LT
1049 }
1050
1051 return 0;
1052
1053error_free:
7223bb4a 1054 free_page_series(region->vm_start, region->vm_top);
8feae131
DH
1055 region->vm_start = vma->vm_start = 0;
1056 region->vm_end = vma->vm_end = 0;
dd8632a1 1057 region->vm_top = 0;
1da177e4
LT
1058 return ret;
1059
1060enomem:
b1de0d13 1061 pr_err("Allocation of length %lu from process %d (%s) failed\n",
05ae6fa3 1062 len, current->pid, current->comm);
9af744d7 1063 show_free_areas(0, NULL);
1da177e4
LT
1064 return -ENOMEM;
1065}
1066
1067/*
1068 * handle mapping creation for uClinux
1069 */
1fcfd8db
ON
1070unsigned long do_mmap(struct file *file,
1071 unsigned long addr,
1072 unsigned long len,
1073 unsigned long prot,
1074 unsigned long flags,
1fcfd8db 1075 unsigned long pgoff,
897ab3e0
MR
1076 unsigned long *populate,
1077 struct list_head *uf)
1da177e4 1078{
8feae131
DH
1079 struct vm_area_struct *vma;
1080 struct vm_region *region;
1da177e4 1081 struct rb_node *rb;
45e55300 1082 vm_flags_t vm_flags;
1fcfd8db 1083 unsigned long capabilities, result;
1da177e4
LT
1084 int ret;
1085
41badc15 1086 *populate = 0;
bebeb3d6 1087
1da177e4
LT
1088 /* decide whether we should attempt the mapping, and if so what sort of
1089 * mapping */
1090 ret = validate_mmap_request(file, addr, len, prot, flags, pgoff,
1091 &capabilities);
22cc877b 1092 if (ret < 0)
1da177e4
LT
1093 return ret;
1094
06aab5a3
DH
1095 /* we ignore the address hint */
1096 addr = 0;
f67d9b15 1097 len = PAGE_ALIGN(len);
06aab5a3 1098
1da177e4
LT
1099 /* we've determined that we can make the mapping, now translate what we
1100 * now know into VMA flags */
45e55300 1101 vm_flags = determine_vm_flags(file, prot, flags, capabilities);
1da177e4 1102
8feae131
DH
1103 /* we're going to need to record the mapping */
1104 region = kmem_cache_zalloc(vm_region_jar, GFP_KERNEL);
1105 if (!region)
1106 goto error_getting_region;
1107
490fc053 1108 vma = vm_area_alloc(current->mm);
8feae131
DH
1109 if (!vma)
1110 goto error_getting_vma;
1da177e4 1111
1e2ae599 1112 region->vm_usage = 1;
8feae131
DH
1113 region->vm_flags = vm_flags;
1114 region->vm_pgoff = pgoff;
1115
8feae131
DH
1116 vma->vm_flags = vm_flags;
1117 vma->vm_pgoff = pgoff;
1da177e4 1118
8feae131 1119 if (file) {
cb0942b8
AV
1120 region->vm_file = get_file(file);
1121 vma->vm_file = get_file(file);
8feae131
DH
1122 }
1123
1124 down_write(&nommu_region_sem);
1125
1126 /* if we want to share, we need to check for regions created by other
1da177e4 1127 * mmap() calls that overlap with our proposed mapping
8feae131 1128 * - we can only share with a superset match on most regular files
1da177e4
LT
1129 * - shared mappings on character devices and memory backed files are
1130 * permitted to overlap inexactly as far as we are concerned for in
1131 * these cases, sharing is handled in the driver or filesystem rather
1132 * than here
1133 */
1134 if (vm_flags & VM_MAYSHARE) {
8feae131
DH
1135 struct vm_region *pregion;
1136 unsigned long pglen, rpglen, pgend, rpgend, start;
1da177e4 1137
8feae131
DH
1138 pglen = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
1139 pgend = pgoff + pglen;
165b2392 1140
8feae131
DH
1141 for (rb = rb_first(&nommu_region_tree); rb; rb = rb_next(rb)) {
1142 pregion = rb_entry(rb, struct vm_region, vm_rb);
1da177e4 1143
8feae131 1144 if (!(pregion->vm_flags & VM_MAYSHARE))
1da177e4
LT
1145 continue;
1146
1147 /* search for overlapping mappings on the same file */
496ad9aa
AV
1148 if (file_inode(pregion->vm_file) !=
1149 file_inode(file))
1da177e4
LT
1150 continue;
1151
8feae131 1152 if (pregion->vm_pgoff >= pgend)
1da177e4
LT
1153 continue;
1154
8feae131
DH
1155 rpglen = pregion->vm_end - pregion->vm_start;
1156 rpglen = (rpglen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1157 rpgend = pregion->vm_pgoff + rpglen;
1158 if (pgoff >= rpgend)
1da177e4
LT
1159 continue;
1160
8feae131
DH
1161 /* handle inexactly overlapping matches between
1162 * mappings */
1163 if ((pregion->vm_pgoff != pgoff || rpglen != pglen) &&
1164 !(pgoff >= pregion->vm_pgoff && pgend <= rpgend)) {
1165 /* new mapping is not a subset of the region */
b4caecd4 1166 if (!(capabilities & NOMMU_MAP_DIRECT))
1da177e4
LT
1167 goto sharing_violation;
1168 continue;
1169 }
1170
8feae131 1171 /* we've found a region we can share */
1e2ae599 1172 pregion->vm_usage++;
8feae131
DH
1173 vma->vm_region = pregion;
1174 start = pregion->vm_start;
1175 start += (pgoff - pregion->vm_pgoff) << PAGE_SHIFT;
1176 vma->vm_start = start;
1177 vma->vm_end = start + len;
1178
22cc877b 1179 if (pregion->vm_flags & VM_MAPPED_COPY)
8feae131 1180 vma->vm_flags |= VM_MAPPED_COPY;
22cc877b 1181 else {
8feae131
DH
1182 ret = do_mmap_shared_file(vma);
1183 if (ret < 0) {
1184 vma->vm_region = NULL;
1185 vma->vm_start = 0;
1186 vma->vm_end = 0;
1e2ae599 1187 pregion->vm_usage--;
8feae131
DH
1188 pregion = NULL;
1189 goto error_just_free;
1190 }
1191 }
1192 fput(region->vm_file);
1193 kmem_cache_free(vm_region_jar, region);
1194 region = pregion;
1195 result = start;
1196 goto share;
1da177e4
LT
1197 }
1198
1da177e4
LT
1199 /* obtain the address at which to make a shared mapping
1200 * - this is the hook for quasi-memory character devices to
1201 * tell us the location of a shared mapping
1202 */
b4caecd4 1203 if (capabilities & NOMMU_MAP_DIRECT) {
1da177e4
LT
1204 addr = file->f_op->get_unmapped_area(file, addr, len,
1205 pgoff, flags);
bb005a59 1206 if (IS_ERR_VALUE(addr)) {
1da177e4 1207 ret = addr;
bb005a59 1208 if (ret != -ENOSYS)
8feae131 1209 goto error_just_free;
1da177e4
LT
1210
1211 /* the driver refused to tell us where to site
1212 * the mapping so we'll have to attempt to copy
1213 * it */
bb005a59 1214 ret = -ENODEV;
b4caecd4 1215 if (!(capabilities & NOMMU_MAP_COPY))
8feae131 1216 goto error_just_free;
1da177e4 1217
b4caecd4 1218 capabilities &= ~NOMMU_MAP_DIRECT;
8feae131
DH
1219 } else {
1220 vma->vm_start = region->vm_start = addr;
1221 vma->vm_end = region->vm_end = addr + len;
1da177e4
LT
1222 }
1223 }
1224 }
1225
8feae131 1226 vma->vm_region = region;
1da177e4 1227
645d83c5 1228 /* set up the mapping
b4caecd4 1229 * - the region is filled in if NOMMU_MAP_DIRECT is still set
645d83c5 1230 */
1da177e4 1231 if (file && vma->vm_flags & VM_SHARED)
8feae131 1232 ret = do_mmap_shared_file(vma);
1da177e4 1233 else
645d83c5 1234 ret = do_mmap_private(vma, region, len, capabilities);
1da177e4 1235 if (ret < 0)
645d83c5
DH
1236 goto error_just_free;
1237 add_nommu_region(region);
8feae131 1238
ea637639 1239 /* clear anonymous mappings that don't ask for uninitialized data */
0bf5f949
CH
1240 if (!vma->vm_file &&
1241 (!IS_ENABLED(CONFIG_MMAP_ALLOW_UNINITIALIZED) ||
1242 !(flags & MAP_UNINITIALIZED)))
ea637639
JZ
1243 memset((void *)region->vm_start, 0,
1244 region->vm_end - region->vm_start);
1245
1da177e4 1246 /* okay... we have a mapping; now we have to register it */
8feae131 1247 result = vma->vm_start;
1da177e4 1248
1da177e4
LT
1249 current->mm->total_vm += len >> PAGE_SHIFT;
1250
8feae131
DH
1251share:
1252 add_vma_to_mm(current->mm, vma);
1da177e4 1253
cfe79c00
MF
1254 /* we flush the region from the icache only when the first executable
1255 * mapping of it is made */
1256 if (vma->vm_flags & VM_EXEC && !region->vm_icache_flushed) {
a75a2df6 1257 flush_icache_user_range(region->vm_start, region->vm_end);
cfe79c00
MF
1258 region->vm_icache_flushed = true;
1259 }
1da177e4 1260
cfe79c00 1261 up_write(&nommu_region_sem);
1da177e4 1262
8feae131 1263 return result;
1da177e4 1264
8feae131
DH
1265error_just_free:
1266 up_write(&nommu_region_sem);
1267error:
89a86402
DH
1268 if (region->vm_file)
1269 fput(region->vm_file);
8feae131 1270 kmem_cache_free(vm_region_jar, region);
89a86402
DH
1271 if (vma->vm_file)
1272 fput(vma->vm_file);
3928d4f5 1273 vm_area_free(vma);
8feae131
DH
1274 return ret;
1275
1276sharing_violation:
1277 up_write(&nommu_region_sem);
22cc877b 1278 pr_warn("Attempt to share mismatched mappings\n");
8feae131
DH
1279 ret = -EINVAL;
1280 goto error;
1da177e4 1281
8feae131
DH
1282error_getting_vma:
1283 kmem_cache_free(vm_region_jar, region);
22cc877b
LR
1284 pr_warn("Allocation of vma for %lu byte allocation from process %d failed\n",
1285 len, current->pid);
9af744d7 1286 show_free_areas(0, NULL);
1da177e4
LT
1287 return -ENOMEM;
1288
8feae131 1289error_getting_region:
22cc877b
LR
1290 pr_warn("Allocation of vm region for %lu byte allocation from process %d failed\n",
1291 len, current->pid);
9af744d7 1292 show_free_areas(0, NULL);
1da177e4
LT
1293 return -ENOMEM;
1294}
6be5ceb0 1295
a90f590a
DB
1296unsigned long ksys_mmap_pgoff(unsigned long addr, unsigned long len,
1297 unsigned long prot, unsigned long flags,
1298 unsigned long fd, unsigned long pgoff)
66f0dc48
HD
1299{
1300 struct file *file = NULL;
1301 unsigned long retval = -EBADF;
1302
120a795d 1303 audit_mmap_fd(fd, flags);
66f0dc48
HD
1304 if (!(flags & MAP_ANONYMOUS)) {
1305 file = fget(fd);
1306 if (!file)
1307 goto out;
1308 }
1309
ad1ed293 1310 retval = vm_mmap_pgoff(file, addr, len, prot, flags, pgoff);
66f0dc48
HD
1311
1312 if (file)
1313 fput(file);
1314out:
1315 return retval;
1316}
1317
a90f590a
DB
1318SYSCALL_DEFINE6(mmap_pgoff, unsigned long, addr, unsigned long, len,
1319 unsigned long, prot, unsigned long, flags,
1320 unsigned long, fd, unsigned long, pgoff)
1321{
1322 return ksys_mmap_pgoff(addr, len, prot, flags, fd, pgoff);
1323}
1324
a4679373
CH
1325#ifdef __ARCH_WANT_SYS_OLD_MMAP
1326struct mmap_arg_struct {
1327 unsigned long addr;
1328 unsigned long len;
1329 unsigned long prot;
1330 unsigned long flags;
1331 unsigned long fd;
1332 unsigned long offset;
1333};
1334
1335SYSCALL_DEFINE1(old_mmap, struct mmap_arg_struct __user *, arg)
1336{
1337 struct mmap_arg_struct a;
1338
1339 if (copy_from_user(&a, arg, sizeof(a)))
1340 return -EFAULT;
1824cb75 1341 if (offset_in_page(a.offset))
a4679373
CH
1342 return -EINVAL;
1343
a90f590a
DB
1344 return ksys_mmap_pgoff(a.addr, a.len, a.prot, a.flags, a.fd,
1345 a.offset >> PAGE_SHIFT);
a4679373
CH
1346}
1347#endif /* __ARCH_WANT_SYS_OLD_MMAP */
1348
1da177e4 1349/*
8feae131
DH
1350 * split a vma into two pieces at address 'addr', a new vma is allocated either
1351 * for the first part or the tail.
1da177e4 1352 */
8feae131
DH
1353int split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
1354 unsigned long addr, int new_below)
1da177e4 1355{
8feae131
DH
1356 struct vm_area_struct *new;
1357 struct vm_region *region;
1358 unsigned long npages;
1da177e4 1359
779c1023
DH
1360 /* we're only permitted to split anonymous regions (these should have
1361 * only a single usage on the region) */
1362 if (vma->vm_file)
8feae131 1363 return -ENOMEM;
1da177e4 1364
8feae131
DH
1365 if (mm->map_count >= sysctl_max_map_count)
1366 return -ENOMEM;
1da177e4 1367
8feae131
DH
1368 region = kmem_cache_alloc(vm_region_jar, GFP_KERNEL);
1369 if (!region)
1370 return -ENOMEM;
1da177e4 1371
3928d4f5 1372 new = vm_area_dup(vma);
8feae131
DH
1373 if (!new) {
1374 kmem_cache_free(vm_region_jar, region);
1375 return -ENOMEM;
1376 }
1377
1378 /* most fields are the same, copy all, and then fixup */
8feae131
DH
1379 *region = *vma->vm_region;
1380 new->vm_region = region;
1381
1382 npages = (addr - vma->vm_start) >> PAGE_SHIFT;
1383
1384 if (new_below) {
dd8632a1 1385 region->vm_top = region->vm_end = new->vm_end = addr;
8feae131
DH
1386 } else {
1387 region->vm_start = new->vm_start = addr;
1388 region->vm_pgoff = new->vm_pgoff += npages;
1da177e4 1389 }
8feae131
DH
1390
1391 if (new->vm_ops && new->vm_ops->open)
1392 new->vm_ops->open(new);
1393
1394 delete_vma_from_mm(vma);
1395 down_write(&nommu_region_sem);
1396 delete_nommu_region(vma->vm_region);
1397 if (new_below) {
1398 vma->vm_region->vm_start = vma->vm_start = addr;
1399 vma->vm_region->vm_pgoff = vma->vm_pgoff += npages;
1400 } else {
1401 vma->vm_region->vm_end = vma->vm_end = addr;
dd8632a1 1402 vma->vm_region->vm_top = addr;
8feae131
DH
1403 }
1404 add_nommu_region(vma->vm_region);
1405 add_nommu_region(new->vm_region);
1406 up_write(&nommu_region_sem);
1407 add_vma_to_mm(mm, vma);
1408 add_vma_to_mm(mm, new);
1409 return 0;
1da177e4
LT
1410}
1411
3034097a 1412/*
8feae131
DH
1413 * shrink a VMA by removing the specified chunk from either the beginning or
1414 * the end
3034097a 1415 */
8feae131
DH
1416static int shrink_vma(struct mm_struct *mm,
1417 struct vm_area_struct *vma,
1418 unsigned long from, unsigned long to)
1da177e4 1419{
8feae131 1420 struct vm_region *region;
1da177e4 1421
8feae131
DH
1422 /* adjust the VMA's pointers, which may reposition it in the MM's tree
1423 * and list */
1424 delete_vma_from_mm(vma);
1425 if (from > vma->vm_start)
1426 vma->vm_end = from;
1427 else
1428 vma->vm_start = to;
1429 add_vma_to_mm(mm, vma);
1da177e4 1430
8feae131
DH
1431 /* cut the backing region down to size */
1432 region = vma->vm_region;
1e2ae599 1433 BUG_ON(region->vm_usage != 1);
8feae131
DH
1434
1435 down_write(&nommu_region_sem);
1436 delete_nommu_region(region);
dd8632a1
PM
1437 if (from > region->vm_start) {
1438 to = region->vm_top;
1439 region->vm_top = region->vm_end = from;
1440 } else {
8feae131 1441 region->vm_start = to;
dd8632a1 1442 }
8feae131
DH
1443 add_nommu_region(region);
1444 up_write(&nommu_region_sem);
1445
1446 free_page_series(from, to);
1447 return 0;
1448}
1da177e4 1449
8feae131
DH
1450/*
1451 * release a mapping
1452 * - under NOMMU conditions the chunk to be unmapped must be backed by a single
1453 * VMA, though it need not cover the whole VMA
1454 */
897ab3e0 1455int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, struct list_head *uf)
8feae131
DH
1456{
1457 struct vm_area_struct *vma;
f67d9b15 1458 unsigned long end;
8feae131 1459 int ret;
1da177e4 1460
f67d9b15 1461 len = PAGE_ALIGN(len);
8feae131
DH
1462 if (len == 0)
1463 return -EINVAL;
365e9c87 1464
f67d9b15
BL
1465 end = start + len;
1466
8feae131
DH
1467 /* find the first potentially overlapping VMA */
1468 vma = find_vma(mm, start);
1469 if (!vma) {
ac714904 1470 static int limit;
33e5d769 1471 if (limit < 5) {
22cc877b
LR
1472 pr_warn("munmap of memory not mmapped by process %d (%s): 0x%lx-0x%lx\n",
1473 current->pid, current->comm,
1474 start, start + len - 1);
33e5d769
DH
1475 limit++;
1476 }
8feae131
DH
1477 return -EINVAL;
1478 }
1da177e4 1479
8feae131
DH
1480 /* we're allowed to split an anonymous VMA but not a file-backed one */
1481 if (vma->vm_file) {
1482 do {
22cc877b 1483 if (start > vma->vm_start)
8feae131 1484 return -EINVAL;
8feae131
DH
1485 if (end == vma->vm_end)
1486 goto erase_whole_vma;
d75a310c
NK
1487 vma = vma->vm_next;
1488 } while (vma);
8feae131
DH
1489 return -EINVAL;
1490 } else {
1491 /* the chunk must be a subset of the VMA found */
1492 if (start == vma->vm_start && end == vma->vm_end)
1493 goto erase_whole_vma;
22cc877b 1494 if (start < vma->vm_start || end > vma->vm_end)
8feae131 1495 return -EINVAL;
1824cb75 1496 if (offset_in_page(start))
8feae131 1497 return -EINVAL;
1824cb75 1498 if (end != vma->vm_end && offset_in_page(end))
8feae131 1499 return -EINVAL;
8feae131
DH
1500 if (start != vma->vm_start && end != vma->vm_end) {
1501 ret = split_vma(mm, vma, start, 1);
22cc877b 1502 if (ret < 0)
8feae131 1503 return ret;
8feae131
DH
1504 }
1505 return shrink_vma(mm, vma, start, end);
1506 }
1da177e4 1507
8feae131
DH
1508erase_whole_vma:
1509 delete_vma_from_mm(vma);
1510 delete_vma(mm, vma);
1da177e4
LT
1511 return 0;
1512}
1513
bfce281c 1514int vm_munmap(unsigned long addr, size_t len)
3034097a 1515{
bfce281c 1516 struct mm_struct *mm = current->mm;
3034097a 1517 int ret;
3034097a 1518
d8ed45c5 1519 mmap_write_lock(mm);
897ab3e0 1520 ret = do_munmap(mm, addr, len, NULL);
d8ed45c5 1521 mmap_write_unlock(mm);
3034097a
DH
1522 return ret;
1523}
a46ef99d
LT
1524EXPORT_SYMBOL(vm_munmap);
1525
1526SYSCALL_DEFINE2(munmap, unsigned long, addr, size_t, len)
1527{
bfce281c 1528 return vm_munmap(addr, len);
a46ef99d 1529}
3034097a
DH
1530
1531/*
8feae131 1532 * release all the mappings made in a process's VM space
3034097a 1533 */
8feae131 1534void exit_mmap(struct mm_struct *mm)
1da177e4 1535{
8feae131 1536 struct vm_area_struct *vma;
1da177e4 1537
8feae131
DH
1538 if (!mm)
1539 return;
1da177e4 1540
8feae131 1541 mm->total_vm = 0;
1da177e4 1542
8feae131
DH
1543 while ((vma = mm->mmap)) {
1544 mm->mmap = vma->vm_next;
1545 delete_vma_from_mm(vma);
1546 delete_vma(mm, vma);
04c34961 1547 cond_resched();
1da177e4
LT
1548 }
1549}
1550
5d22fc25 1551int vm_brk(unsigned long addr, unsigned long len)
1da177e4
LT
1552{
1553 return -ENOMEM;
1554}
1555
1556/*
6fa5f80b
DH
1557 * expand (or shrink) an existing mapping, potentially moving it at the same
1558 * time (controlled by the MREMAP_MAYMOVE flag and available VM space)
1da177e4 1559 *
6fa5f80b 1560 * under NOMMU conditions, we only permit changing a mapping's size, and only
8feae131
DH
1561 * as long as it stays within the region allocated by do_mmap_private() and the
1562 * block is not shareable
1da177e4 1563 *
6fa5f80b 1564 * MREMAP_FIXED is not supported under NOMMU conditions
1da177e4 1565 */
4b377bab 1566static unsigned long do_mremap(unsigned long addr,
1da177e4
LT
1567 unsigned long old_len, unsigned long new_len,
1568 unsigned long flags, unsigned long new_addr)
1569{
6fa5f80b 1570 struct vm_area_struct *vma;
1da177e4
LT
1571
1572 /* insanity checks first */
f67d9b15
BL
1573 old_len = PAGE_ALIGN(old_len);
1574 new_len = PAGE_ALIGN(new_len);
8feae131 1575 if (old_len == 0 || new_len == 0)
1da177e4
LT
1576 return (unsigned long) -EINVAL;
1577
1824cb75 1578 if (offset_in_page(addr))
8feae131
DH
1579 return -EINVAL;
1580
1da177e4
LT
1581 if (flags & MREMAP_FIXED && new_addr != addr)
1582 return (unsigned long) -EINVAL;
1583
8feae131 1584 vma = find_vma_exact(current->mm, addr, old_len);
6fa5f80b
DH
1585 if (!vma)
1586 return (unsigned long) -EINVAL;
1da177e4 1587
6fa5f80b 1588 if (vma->vm_end != vma->vm_start + old_len)
1da177e4
LT
1589 return (unsigned long) -EFAULT;
1590
6fa5f80b 1591 if (vma->vm_flags & VM_MAYSHARE)
1da177e4
LT
1592 return (unsigned long) -EPERM;
1593
8feae131 1594 if (new_len > vma->vm_region->vm_end - vma->vm_region->vm_start)
1da177e4
LT
1595 return (unsigned long) -ENOMEM;
1596
1597 /* all checks complete - do it */
6fa5f80b 1598 vma->vm_end = vma->vm_start + new_len;
6fa5f80b
DH
1599 return vma->vm_start;
1600}
1601
6a6160a7
HC
1602SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
1603 unsigned long, new_len, unsigned long, flags,
1604 unsigned long, new_addr)
6fa5f80b
DH
1605{
1606 unsigned long ret;
1607
d8ed45c5 1608 mmap_write_lock(current->mm);
6fa5f80b 1609 ret = do_mremap(addr, old_len, new_len, flags, new_addr);
d8ed45c5 1610 mmap_write_unlock(current->mm);
6fa5f80b 1611 return ret;
1da177e4
LT
1612}
1613
df06b37f
KB
1614struct page *follow_page(struct vm_area_struct *vma, unsigned long address,
1615 unsigned int foll_flags)
1da177e4
LT
1616{
1617 return NULL;
1618}
1619
8f3b1327
BL
1620int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1621 unsigned long pfn, unsigned long size, pgprot_t prot)
1da177e4 1622{
8f3b1327
BL
1623 if (addr != (pfn << PAGE_SHIFT))
1624 return -EINVAL;
1625
314e51b9 1626 vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
66aa2b4b 1627 return 0;
1da177e4 1628}
22c4af40 1629EXPORT_SYMBOL(remap_pfn_range);
1da177e4 1630
3c0b9de6
LT
1631int vm_iomap_memory(struct vm_area_struct *vma, phys_addr_t start, unsigned long len)
1632{
1633 unsigned long pfn = start >> PAGE_SHIFT;
1634 unsigned long vm_len = vma->vm_end - vma->vm_start;
1635
1636 pfn += vma->vm_pgoff;
1637 return io_remap_pfn_range(vma, vma->vm_start, pfn, vm_len, vma->vm_page_prot);
1638}
1639EXPORT_SYMBOL(vm_iomap_memory);
1640
f905bc44
PM
1641int remap_vmalloc_range(struct vm_area_struct *vma, void *addr,
1642 unsigned long pgoff)
1643{
1644 unsigned int size = vma->vm_end - vma->vm_start;
1645
1646 if (!(vma->vm_flags & VM_USERMAP))
1647 return -EINVAL;
1648
1649 vma->vm_start = (unsigned long)(addr + (pgoff << PAGE_SHIFT));
1650 vma->vm_end = vma->vm_start + size;
1651
1652 return 0;
1653}
1654EXPORT_SYMBOL(remap_vmalloc_range);
1655
2bcd6454 1656vm_fault_t filemap_fault(struct vm_fault *vmf)
b0e15190
DH
1657{
1658 BUG();
d0217ac0 1659 return 0;
b0e15190 1660}
b5073173 1661EXPORT_SYMBOL(filemap_fault);
0ec76a11 1662
3f98a28c 1663vm_fault_t filemap_map_pages(struct vm_fault *vmf,
bae473a4 1664 pgoff_t start_pgoff, pgoff_t end_pgoff)
f1820361
KS
1665{
1666 BUG();
3f98a28c 1667 return 0;
f1820361
KS
1668}
1669EXPORT_SYMBOL(filemap_map_pages);
1670
d3f5ffca
JH
1671int __access_remote_vm(struct mm_struct *mm, unsigned long addr, void *buf,
1672 int len, unsigned int gup_flags)
0ec76a11 1673{
0ec76a11 1674 struct vm_area_struct *vma;
442486ec 1675 int write = gup_flags & FOLL_WRITE;
0ec76a11 1676
d8ed45c5 1677 if (mmap_read_lock_killable(mm))
1e426fe2 1678 return 0;
0ec76a11
DH
1679
1680 /* the access must start within one of the target process's mappings */
0159b141
DH
1681 vma = find_vma(mm, addr);
1682 if (vma) {
0ec76a11
DH
1683 /* don't overrun this mapping */
1684 if (addr + len >= vma->vm_end)
1685 len = vma->vm_end - addr;
1686
1687 /* only read or write mappings where it is permitted */
d00c7b99 1688 if (write && vma->vm_flags & VM_MAYWRITE)
7959722b
JZ
1689 copy_to_user_page(vma, NULL, addr,
1690 (void *) addr, buf, len);
d00c7b99 1691 else if (!write && vma->vm_flags & VM_MAYREAD)
7959722b
JZ
1692 copy_from_user_page(vma, NULL, addr,
1693 buf, (void *) addr, len);
0ec76a11
DH
1694 else
1695 len = 0;
1696 } else {
1697 len = 0;
1698 }
1699
d8ed45c5 1700 mmap_read_unlock(mm);
f55f199b
MF
1701
1702 return len;
1703}
1704
1705/**
b7701a5f 1706 * access_remote_vm - access another process' address space
f55f199b
MF
1707 * @mm: the mm_struct of the target address space
1708 * @addr: start address to access
1709 * @buf: source or destination buffer
1710 * @len: number of bytes to transfer
6347e8d5 1711 * @gup_flags: flags modifying lookup behaviour
f55f199b
MF
1712 *
1713 * The caller must hold a reference on @mm.
1714 */
1715int access_remote_vm(struct mm_struct *mm, unsigned long addr,
6347e8d5 1716 void *buf, int len, unsigned int gup_flags)
f55f199b 1717{
d3f5ffca 1718 return __access_remote_vm(mm, addr, buf, len, gup_flags);
f55f199b
MF
1719}
1720
1721/*
1722 * Access another process' address space.
1723 * - source/target buffer must be kernel space
1724 */
f307ab6d
LS
1725int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
1726 unsigned int gup_flags)
f55f199b
MF
1727{
1728 struct mm_struct *mm;
1729
1730 if (addr + len < addr)
1731 return 0;
1732
1733 mm = get_task_mm(tsk);
1734 if (!mm)
1735 return 0;
1736
d3f5ffca 1737 len = __access_remote_vm(mm, addr, buf, len, gup_flags);
f55f199b 1738
0ec76a11
DH
1739 mmput(mm);
1740 return len;
1741}
fcd35857 1742EXPORT_SYMBOL_GPL(access_process_vm);
7e660872
DH
1743
1744/**
1745 * nommu_shrink_inode_mappings - Shrink the shared mappings on an inode
1746 * @inode: The inode to check
1747 * @size: The current filesize of the inode
1748 * @newsize: The proposed filesize of the inode
1749 *
1750 * Check the shared mappings on an inode on behalf of a shrinking truncate to
c08b342c
RD
1751 * make sure that any outstanding VMAs aren't broken and then shrink the
1752 * vm_regions that extend beyond so that do_mmap() doesn't
7e660872
DH
1753 * automatically grant mappings that are too large.
1754 */
1755int nommu_shrink_inode_mappings(struct inode *inode, size_t size,
1756 size_t newsize)
1757{
1758 struct vm_area_struct *vma;
7e660872
DH
1759 struct vm_region *region;
1760 pgoff_t low, high;
1761 size_t r_size, r_top;
1762
1763 low = newsize >> PAGE_SHIFT;
1764 high = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
1765
1766 down_write(&nommu_region_sem);
1acf2e04 1767 i_mmap_lock_read(inode->i_mapping);
7e660872
DH
1768
1769 /* search for VMAs that fall within the dead zone */
6b2dbba8 1770 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, low, high) {
7e660872
DH
1771 /* found one - only interested if it's shared out of the page
1772 * cache */
1773 if (vma->vm_flags & VM_SHARED) {
1acf2e04 1774 i_mmap_unlock_read(inode->i_mapping);
7e660872
DH
1775 up_write(&nommu_region_sem);
1776 return -ETXTBSY; /* not quite true, but near enough */
1777 }
1778 }
1779
1780 /* reduce any regions that overlap the dead zone - if in existence,
1781 * these will be pointed to by VMAs that don't overlap the dead zone
1782 *
1783 * we don't check for any regions that start beyond the EOF as there
1784 * shouldn't be any
1785 */
1acf2e04 1786 vma_interval_tree_foreach(vma, &inode->i_mapping->i_mmap, 0, ULONG_MAX) {
7e660872
DH
1787 if (!(vma->vm_flags & VM_SHARED))
1788 continue;
1789
1790 region = vma->vm_region;
1791 r_size = region->vm_top - region->vm_start;
1792 r_top = (region->vm_pgoff << PAGE_SHIFT) + r_size;
1793
1794 if (r_top > newsize) {
1795 region->vm_top -= r_top - newsize;
1796 if (region->vm_end > region->vm_top)
1797 region->vm_end = region->vm_top;
1798 }
1799 }
1800
1acf2e04 1801 i_mmap_unlock_read(inode->i_mapping);
7e660872
DH
1802 up_write(&nommu_region_sem);
1803 return 0;
1804}
c9b1d098
AS
1805
1806/*
1807 * Initialise sysctl_user_reserve_kbytes.
1808 *
1809 * This is intended to prevent a user from starting a single memory hogging
1810 * process, such that they cannot recover (kill the hog) in OVERCOMMIT_NEVER
1811 * mode.
1812 *
1813 * The default value is min(3% of free memory, 128MB)
1814 * 128MB is enough to recover with sshd/login, bash, and top/kill.
1815 */
1816static int __meminit init_user_reserve(void)
1817{
1818 unsigned long free_kbytes;
1819
c41f012a 1820 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
c9b1d098
AS
1821
1822 sysctl_user_reserve_kbytes = min(free_kbytes / 32, 1UL << 17);
1823 return 0;
1824}
a4bc6fc7 1825subsys_initcall(init_user_reserve);
4eeab4f5
AS
1826
1827/*
1828 * Initialise sysctl_admin_reserve_kbytes.
1829 *
1830 * The purpose of sysctl_admin_reserve_kbytes is to allow the sys admin
1831 * to log in and kill a memory hogging process.
1832 *
1833 * Systems with more than 256MB will reserve 8MB, enough to recover
1834 * with sshd, bash, and top in OVERCOMMIT_GUESS. Smaller systems will
1835 * only reserve 3% of free pages by default.
1836 */
1837static int __meminit init_admin_reserve(void)
1838{
1839 unsigned long free_kbytes;
1840
c41f012a 1841 free_kbytes = global_zone_page_state(NR_FREE_PAGES) << (PAGE_SHIFT - 10);
4eeab4f5
AS
1842
1843 sysctl_admin_reserve_kbytes = min(free_kbytes / 32, 1UL << 13);
1844 return 0;
1845}
a4bc6fc7 1846subsys_initcall(init_admin_reserve);