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