From: Yoshinori Sato o include/asm-h8300/mmu.h: Changeset 1.2053 support o mm/nommu.c: add __vm_enough_memory Signed-off-by: Yoshinori Sato Signed-off-by: Andrew Morton --- 25-akpm/include/asm-h8300/mmu.h | 13 ----- 25-akpm/mm/nommu.c | 93 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 13 deletions(-) diff -puN include/asm-h8300/mmu.h~h8-300-mm-update include/asm-h8300/mmu.h --- 25/include/asm-h8300/mmu.h~h8-300-mm-update 2005-01-16 14:42:58.556477584 -0800 +++ 25-akpm/include/asm-h8300/mmu.h 2005-01-16 14:42:58.562476672 -0800 @@ -3,19 +3,8 @@ /* Copyright (C) 2002, David McCullough */ -struct mm_rblock_struct { - int size; - int refcount; - void *kblock; -}; - -struct mm_tblock_struct { - struct mm_rblock_struct *rblock; - struct mm_tblock_struct *next; -}; - typedef struct { - struct mm_tblock_struct tblock; + struct vm_list_struct *vmlist; unsigned long end_brk; } mm_context_t; diff -puN mm/nommu.c~h8-300-mm-update mm/nommu.c --- 25/mm/nommu.c~h8-300-mm-update 2005-01-16 14:42:58.558477280 -0800 +++ 25-akpm/mm/nommu.c 2005-01-16 14:42:58.563476520 -0800 @@ -41,8 +41,12 @@ int sysctl_overcommit_ratio = 50; /* def int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT; int heap_stack_gap = 0; -EXPORT_SYMBOL(sysctl_max_map_count); EXPORT_SYMBOL(mem_map); +EXPORT_SYMBOL(sysctl_max_map_count); +EXPORT_SYMBOL(sysctl_overcommit_memory); +EXPORT_SYMBOL(sysctl_overcommit_ratio); +EXPORT_SYMBOL(vm_committed_space); +EXPORT_SYMBOL(__vm_enough_memory); /* list of shareable VMAs */ struct rb_root nommu_vma_tree = RB_ROOT; @@ -968,3 +972,90 @@ void unmap_mapping_range(struct address_ int even_cows) { } + +/* + * Check that a process has enough memory to allocate a new virtual + * mapping. 0 means there is enough memory for the allocation to + * succeed and -ENOMEM implies there is not. + * + * We currently support three overcommit policies, which are set via the + * vm.overcommit_memory sysctl. See Documentation/vm/overcommit-accounting + * + * Strict overcommit modes added 2002 Feb 26 by Alan Cox. + * Additional code 2002 Jul 20 by Robert Love. + * + * cap_sys_admin is 1 if the process has admin privileges, 0 otherwise. + * + * Note this is a helper function intended to be used by LSMs which + * wish to use this logic. + */ +int __vm_enough_memory(long pages, int cap_sys_admin) +{ + unsigned long free, allowed; + + vm_acct_memory(pages); + + /* + * Sometimes we want to use more memory than we have + */ + if (sysctl_overcommit_memory == OVERCOMMIT_ALWAYS) + return 0; + + if (sysctl_overcommit_memory == OVERCOMMIT_GUESS) { + unsigned long n; + + free = get_page_cache_size(); + free += nr_swap_pages; + + /* + * Any slabs which are created with the + * SLAB_RECLAIM_ACCOUNT flag claim to have contents + * which are reclaimable, under pressure. The dentry + * cache and most inode caches should fall into this + */ + free += atomic_read(&slab_reclaim_pages); + + /* + * Leave the last 3% for root + */ + if (!cap_sys_admin) + free -= free / 32; + + if (free > pages) + return 0; + + /* + * nr_free_pages() is very expensive on large systems, + * only call if we're about to fail. + */ + n = nr_free_pages(); + if (!cap_sys_admin) + n -= n / 32; + free += n; + + if (free > pages) + return 0; + vm_unacct_memory(pages); + return -ENOMEM; + } + + allowed = totalram_pages * sysctl_overcommit_ratio / 100; + /* + * Leave the last 3% for root + */ + if (!cap_sys_admin) + allowed -= allowed / 32; + allowed += total_swap_pages; + + /* Don't let a single process grow too big: + leave 3% of the size of this process for other processes */ + allowed -= current->mm->total_vm / 32; + + if (atomic_read(&vm_committed_space) < allowed) + return 0; + + vm_unacct_memory(pages); + + return -ENOMEM; +} + _