aboutsummaryrefslogtreecommitdiffstats
path: root/mm/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'mm/util.c')
-rw-r--r--mm/util.c128
1 files changed, 125 insertions, 3 deletions
diff --git a/mm/util.c b/mm/util.c
index 4fb14ca5a419..6cc81e7b8705 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -283,9 +283,7 @@ EXPORT_SYMBOL_GPL(__get_user_pages_fast);
283int __weak get_user_pages_fast(unsigned long start, 283int __weak get_user_pages_fast(unsigned long start,
284 int nr_pages, int write, struct page **pages) 284 int nr_pages, int write, struct page **pages)
285{ 285{
286 struct mm_struct *mm = current->mm; 286 return get_user_pages_unlocked(start, nr_pages, write, 0, pages);
287 return get_user_pages_unlocked(current, mm, start, nr_pages,
288 write, 0, pages);
289} 287}
290EXPORT_SYMBOL_GPL(get_user_pages_fast); 288EXPORT_SYMBOL_GPL(get_user_pages_fast);
291 289
@@ -396,6 +394,13 @@ int __page_mapcount(struct page *page)
396} 394}
397EXPORT_SYMBOL_GPL(__page_mapcount); 395EXPORT_SYMBOL_GPL(__page_mapcount);
398 396
397int sysctl_overcommit_memory __read_mostly = OVERCOMMIT_GUESS;
398int sysctl_overcommit_ratio __read_mostly = 50;
399unsigned long sysctl_overcommit_kbytes __read_mostly;
400int sysctl_max_map_count __read_mostly = DEFAULT_MAX_MAP_COUNT;
401unsigned long sysctl_user_reserve_kbytes __read_mostly = 1UL << 17; /* 128MB */
402unsigned long sysctl_admin_reserve_kbytes __read_mostly = 1UL << 13; /* 8MB */
403
399int overcommit_ratio_handler(struct ctl_table *table, int write, 404int overcommit_ratio_handler(struct ctl_table *table, int write,
400 void __user *buffer, size_t *lenp, 405 void __user *buffer, size_t *lenp,
401 loff_t *ppos) 406 loff_t *ppos)
@@ -437,6 +442,123 @@ unsigned long vm_commit_limit(void)
437 return allowed; 442 return allowed;
438} 443}
439 444
445/*
446 * Make sure vm_committed_as in one cacheline and not cacheline shared with
447 * other variables. It can be updated by several CPUs frequently.
448 */
449struct percpu_counter vm_committed_as ____cacheline_aligned_in_smp;
450
451/*
452 * The global memory commitment made in the system can be a metric
453 * that can be used to drive ballooning decisions when Linux is hosted
454 * as a guest. On Hyper-V, the host implements a policy engine for dynamically
455 * balancing memory across competing virtual machines that are hosted.
456 * Several metrics drive this policy engine including the guest reported
457 * memory commitment.
458 */
459unsigned long vm_memory_committed(void)
460{
461 return percpu_counter_read_positive(&vm_committed_as);
462}
463EXPORT_SYMBOL_GPL(vm_memory_committed);
464
465/*
466 * Check that a process has enough memory to allocate a new virtual
467 * mapping. 0 means there is enough memory for the allocation to
468 * succeed and -ENOMEM implies there is not.
469 *
470 * We currently support three overcommit policies, which are set via the
471 * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting
472 *
473 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
474 * Additional code 2002 Jul 20 by Robert Love.
475 *
476 * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise.
477 *
478 * Note this is a helper function intended to be used by LSMs which
479 * wish to use this logic.
480 */
481int __vm_enough_memory(struct mm_struct *mm, long pages, int cap_sys_admin)
482{
483 long free, allowed, reserve;
484
485 VM_WARN_ONCE(percpu_counter_read(&vm_committed_as) <
486 -(s64)vm_committed_as_batch * num_online_cpus(),
487 "memory commitment underflow");
488
489 vm_acct_memory(pages);
490
491 /*
492 * Sometimes we want to use more memory than we have
493 */
494 if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS)
495 return 0;
496
497 if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) {
498 free = global_page_state(NR_FREE_PAGES);
499 free += global_page_state(NR_FILE_PAGES);
500
501 /*
502 * shmem pages shouldn't be counted as free in this
503 * case, they can't be purged, only swapped out, and
504 * that won't affect the overall amount of available
505 * memory in the system.
506 */
507 free -= global_page_state(NR_SHMEM);
508
509 free += get_nr_swap_pages();
510
511 /*
512 * Any slabs which are created with the
513 * SLAB_RECLAIM_ACCOUNT flag claim to have contents
514 * which are reclaimable, under pressure. The dentry
515 * cache and most inode caches should fall into this
516 */
517 free += global_page_state(NR_SLAB_RECLAIMABLE);
518
519 /*
520 * Leave reserved pages. The pages are not for anonymous pages.
521 */
522 if (free <= totalreserve_pages)
523 goto error;
524 else
525 free -= totalreserve_pages;
526
527 /*
528 * Reserve some for root
529 */
530 if (!cap_sys_admin)
531 free -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
532
533 if (free > pages)
534 return 0;
535
536 goto error;
537 }
538
539 allowed = vm_commit_limit();
540 /*
541 * Reserve some for root
542 */
543 if (!cap_sys_admin)
544 allowed -= sysctl_admin_reserve_kbytes >> (PAGE_SHIFT - 10);
545
546 /*
547 * Don't let a single process grow so big a user can't recover
548 */
549 if (mm) {
550 reserve = sysctl_user_reserve_kbytes >> (PAGE_SHIFT - 10);
551 allowed -= min_t(long, mm->total_vm / 32, reserve);
552 }
553
554 if (percpu_counter_read_positive(&vm_committed_as) < allowed)
555 return 0;
556error:
557 vm_unacct_memory(pages);
558
559 return -ENOMEM;
560}
561
440/** 562/**
441 * get_cmdline() - copy the cmdline value to a buffer. 563 * get_cmdline() - copy the cmdline value to a buffer.
442 * @task: the task whose cmdline value to copy. 564 * @task: the task whose cmdline value to copy.