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