mmap.c 35.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/*
Andrew Morton's avatar
Andrew Morton committed
2
 * mm/mmap.c
Linus Torvalds's avatar
Linus Torvalds committed
3 4
 *
 * Written by obz.
Andrew Morton's avatar
Andrew Morton committed
5 6
 *
 * Address space accounting code	<alan@redhat.com>
Linus Torvalds's avatar
Linus Torvalds committed
7
 */
Andrew Morton's avatar
Andrew Morton committed
8

Linus Torvalds's avatar
Linus Torvalds committed
9 10 11 12 13 14 15
#include <linux/slab.h>
#include <linux/shm.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
#include <linux/init.h>
#include <linux/file.h>
Linus Torvalds's avatar
Linus Torvalds committed
16
#include <linux/fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
#include <linux/personality.h>
18
#include <linux/security.h>
19
#include <linux/hugetlb.h>
John Levon's avatar
John Levon committed
20
#include <linux/profile.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23

#include <asm/uaccess.h>
#include <asm/pgalloc.h>
24 25
#include <asm/tlb.h>

Linus Torvalds's avatar
Linus Torvalds committed
26 27 28 29 30 31
/*
 * WARNING: the debugging will use recursive algorithms so never enable this
 * unless you know what you are doing.
 */
#undef DEBUG_MM_RB

Linus Torvalds's avatar
Linus Torvalds committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/* description of effects of mapping type and prot in current implementation.
 * this is due to the limited x86 page protection hardware.  The expected
 * behavior is in parens:
 *
 * map_type	prot
 *		PROT_NONE	PROT_READ	PROT_WRITE	PROT_EXEC
 * MAP_SHARED	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
 *		w: (no) no	w: (no) no	w: (yes) yes	w: (no) no
 *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
 *		
 * MAP_PRIVATE	r: (no) no	r: (yes) yes	r: (no) yes	r: (no) yes
 *		w: (no) no	w: (no) no	w: (copy) copy	w: (no) no
 *		x: (no) no	x: (no) yes	x: (no) yes	x: (yes) yes
 *
 */
pgprot_t protection_map[16] = {
	__P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
	__S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
};

Andrew Morton's avatar
Andrew Morton committed
52 53 54 55 56 57 58 59
int sysctl_overcommit_memory = 0;	/* default is heuristic overcommit */
int sysctl_overcommit_ratio = 50;	/* default is 50% */
atomic_t vm_committed_space = ATOMIC_INIT(0);

inline void vm_unacct_memory(long pages)
{	
	atomic_sub(pages, &vm_committed_space);
}
Linus Torvalds's avatar
Linus Torvalds committed
60

Andrew Morton's avatar
Andrew Morton committed
61 62 63 64 65 66 67 68 69 70
/*
 * Check that a process has enough memory to allocate a new virtual
 * mapping. 1 means there is enough memory for the allocation to
 * succeed and 0 implies there is not.
 *
 * We currently support three overcommit policies, which are set via the
 * vm.overcommit_memory sysctl.  See Documentation/vm/overcommit-acounting
 *
 * Strict overcommit modes added 2002 Feb 26 by Alan Cox.
 * Additional code 2002 Jul 20 by Robert Love.
Linus Torvalds's avatar
Linus Torvalds committed
71 72 73
 */
int vm_enough_memory(long pages)
{
Andrew Morton's avatar
Andrew Morton committed
74
	unsigned long free, allowed;
Linus Torvalds's avatar
Linus Torvalds committed
75

Andrew Morton's avatar
Andrew Morton committed
76
	atomic_add(pages, &vm_committed_space);
Linus Torvalds's avatar
Linus Torvalds committed
77

Andrew Morton's avatar
Andrew Morton committed
78 79
        /*
	 * Sometimes we want to use more memory than we have
Linus Torvalds's avatar
Linus Torvalds committed
80
	 */
Andrew Morton's avatar
Andrew Morton committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94
	if (sysctl_overcommit_memory == 1)
		return 1;

	if (sysctl_overcommit_memory == 0) {
		free = get_page_cache_size();
		free += nr_free_pages();
		free += nr_swap_pages;

		/*
		 * This double-counts: the nrpages are both in the
		 * page-cache and in the swapper space. At the same time,
		 * this compensates for the swap-space over-allocation
		 * (ie "nr_swap_pages" being too small).
		 */
95
		free += total_swapcache_pages;
Andrew Morton's avatar
Andrew Morton committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

		/*
		 * The code below doesn't account for free space in the
		 * inode and dentry slab cache, slab cache fragmentation,
		 * inodes and dentries which will become freeable under
		 * VM load, etc. Lets just hope all these (complex)
		 * factors balance out...
		 */
		free += (dentry_stat.nr_unused * sizeof(struct dentry)) >>
			PAGE_SHIFT;
		free += (inodes_stat.nr_unused * sizeof(struct inode)) >>
			PAGE_SHIFT;

		if (free > pages)
			return 1;
		vm_unacct_memory(pages);
		return 0;
	}
Linus Torvalds's avatar
Linus Torvalds committed
114

115
	allowed = totalram_pages * sysctl_overcommit_ratio / 100;
Andrew Morton's avatar
Andrew Morton committed
116
	allowed += total_swap_pages;
Linus Torvalds's avatar
Linus Torvalds committed
117

Andrew Morton's avatar
Andrew Morton committed
118 119 120 121 122 123 124 125
	if (atomic_read(&vm_committed_space) < allowed)
		return 1;

	vm_unacct_memory(pages);

	return 0;
}

126 127 128 129 130 131 132 133 134 135 136 137 138
/*
 * Requires inode->i_mapping->i_shared_sem
 */
static inline void
__remove_shared_vm_struct(struct vm_area_struct *vma, struct inode *inode)
{
	if (inode) {
		if (vma->vm_flags & VM_DENYWRITE)
			atomic_inc(&inode->i_writecount);
		list_del_init(&vma->shared);
	}
}

139 140 141
/*
 * Remove one vm structure from the inode's i_mapping address space.
 */
142
static void remove_shared_vm_struct(struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
143
{
144
	struct file *file = vma->vm_file;
Linus Torvalds's avatar
Linus Torvalds committed
145 146 147

	if (file) {
		struct inode *inode = file->f_dentry->d_inode;
148

149
		down(&inode->i_mapping->i_shared_sem);
150
		__remove_shared_vm_struct(vma, inode);
151
		up(&inode->i_mapping->i_shared_sem);
Linus Torvalds's avatar
Linus Torvalds committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
	}
}

/*
 *  sys_brk() for the most part doesn't need the global kernel
 *  lock, except when an application is doing something nasty
 *  like trying to un-brk an area that has already been mapped
 *  to a regular file.  in this case, the unmapping will need
 *  to invoke file system routines that need the global lock.
 */
asmlinkage unsigned long sys_brk(unsigned long brk)
{
	unsigned long rlim, retval;
	unsigned long newbrk, oldbrk;
	struct mm_struct *mm = current->mm;

Linus Torvalds's avatar
Linus Torvalds committed
168
	down_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
169 170 171 172 173 174 175 176 177 178

	if (brk < mm->end_code)
		goto out;
	newbrk = PAGE_ALIGN(brk);
	oldbrk = PAGE_ALIGN(mm->brk);
	if (oldbrk == newbrk)
		goto set_brk;

	/* Always allow shrinking brk. */
	if (brk <= mm->brk) {
179
		if (!do_munmap(mm, newbrk, oldbrk-newbrk))
Linus Torvalds's avatar
Linus Torvalds committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
			goto set_brk;
		goto out;
	}

	/* Check against rlimit.. */
	rlim = current->rlim[RLIMIT_DATA].rlim_cur;
	if (rlim < RLIM_INFINITY && brk - mm->start_data > rlim)
		goto out;

	/* Check against existing mmap mappings. */
	if (find_vma_intersection(mm, oldbrk, newbrk+PAGE_SIZE))
		goto out;

	/* Ok, looks good - let it rip. */
	if (do_brk(oldbrk, newbrk-oldbrk) != oldbrk)
		goto out;
set_brk:
	mm->brk = brk;
out:
	retval = mm->brk;
Linus Torvalds's avatar
Linus Torvalds committed
200
	up_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
201 202 203 204 205 206 207
	return retval;
}

/* Combine the mmap "prot" and "flags" argument into one "vm_flags" used
 * internally. Essentially, translate the "PROT_xxx" and "MAP_xxx" bits
 * into "VM_xxx".
 */
208 209
static inline unsigned long
calc_vm_flags(unsigned long prot, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
#define _trans(x,bit1,bit2) \
((bit1==bit2)?(x&bit1):(x&bit1)?bit2:0)

	unsigned long prot_bits, flag_bits;
	prot_bits =
		_trans(prot, PROT_READ, VM_READ) |
		_trans(prot, PROT_WRITE, VM_WRITE) |
		_trans(prot, PROT_EXEC, VM_EXEC);
	flag_bits =
		_trans(flags, MAP_GROWSDOWN, VM_GROWSDOWN) |
		_trans(flags, MAP_DENYWRITE, VM_DENYWRITE) |
		_trans(flags, MAP_EXECUTABLE, VM_EXECUTABLE);
	return prot_bits | flag_bits;
#undef _trans
}

Linus Torvalds's avatar
Linus Torvalds committed
227
#ifdef DEBUG_MM_RB
228
static int browse_rb(struct rb_node * rb_node) {
Linus Torvalds's avatar
Linus Torvalds committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	int i = 0;
	if (rb_node) {
		i++;
		i += browse_rb(rb_node->rb_left);
		i += browse_rb(rb_node->rb_right);
	}
	return i;
}

static void validate_mm(struct mm_struct * mm) {
	int bug = 0;
	int i = 0;
	struct vm_area_struct * tmp = mm->mmap;
	while (tmp) {
		tmp = tmp->vm_next;
		i++;
	}
	if (i != mm->map_count)
		printk("map_count %d vm_next %d\n", mm->map_count, i), bug = 1;
	i = browse_rb(mm->mm_rb.rb_node);
	if (i != mm->map_count)
		printk("map_count %d rb %d\n", mm->map_count, i), bug = 1;
	if (bug)
		BUG();
}
#else
#define validate_mm(mm) do { } while (0)
#endif

258 259 260 261
static struct vm_area_struct *
find_vma_prepare(struct mm_struct *mm, unsigned long addr,
		struct vm_area_struct **pprev, struct rb_node ***rb_link,
		struct rb_node ** rb_parent)
Linus Torvalds's avatar
Linus Torvalds committed
262 263
{
	struct vm_area_struct * vma;
264
	struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
Linus Torvalds's avatar
Linus Torvalds committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

	__rb_link = &mm->mm_rb.rb_node;
	rb_prev = __rb_parent = NULL;
	vma = NULL;

	while (*__rb_link) {
		struct vm_area_struct *vma_tmp;

		__rb_parent = *__rb_link;
		vma_tmp = rb_entry(__rb_parent, struct vm_area_struct, vm_rb);

		if (vma_tmp->vm_end > addr) {
			vma = vma_tmp;
			if (vma_tmp->vm_start <= addr)
				return vma;
			__rb_link = &__rb_parent->rb_left;
		} else {
			rb_prev = __rb_parent;
			__rb_link = &__rb_parent->rb_right;
		}
	}

	*pprev = NULL;
	if (rb_prev)
		*pprev = rb_entry(rb_prev, struct vm_area_struct, vm_rb);
	*rb_link = __rb_link;
	*rb_parent = __rb_parent;
	return vma;
}

295 296 297
static inline void
__vma_link_list(struct mm_struct *mm, struct vm_area_struct *vma,
		struct vm_area_struct *prev, struct rb_node *rb_parent)
Linus Torvalds's avatar
Linus Torvalds committed
298 299 300 301 302 303 304
{
	if (prev) {
		vma->vm_next = prev->vm_next;
		prev->vm_next = vma;
	} else {
		mm->mmap = vma;
		if (rb_parent)
305 306
			vma->vm_next = rb_entry(rb_parent,
					struct vm_area_struct, vm_rb);
Linus Torvalds's avatar
Linus Torvalds committed
307 308 309 310 311
		else
			vma->vm_next = NULL;
	}
}

312 313
static void __vma_link_rb(struct mm_struct *mm, struct vm_area_struct *vma,
			struct rb_node **rb_link, struct rb_node *rb_parent)
Linus Torvalds's avatar
Linus Torvalds committed
314 315 316 317 318
{
	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
}

319
static inline void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
320 321 322 323 324 325 326 327 328 329 330 331
{
	struct file * file;

	file = vma->vm_file;
	if (file) {
		struct inode * inode = file->f_dentry->d_inode;
		struct address_space *mapping = inode->i_mapping;

		if (vma->vm_flags & VM_DENYWRITE)
			atomic_dec(&inode->i_writecount);

		if (vma->vm_flags & VM_SHARED)
332 333 334
			list_add_tail(&vma->shared, &mapping->i_mmap_shared);
		else
			list_add_tail(&vma->shared, &mapping->i_mmap);
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337
	}
}

338 339 340 341
static void
__vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
	struct vm_area_struct *prev, struct rb_node **rb_link,
	struct rb_node *rb_parent)
Linus Torvalds's avatar
Linus Torvalds committed
342 343 344 345 346 347
{
	__vma_link_list(mm, vma, prev, rb_parent);
	__vma_link_rb(mm, vma, rb_link, rb_parent);
	__vma_link_file(vma);
}

348 349 350
static void vma_link(struct mm_struct *mm, struct vm_area_struct *vma,
			struct vm_area_struct *prev, struct rb_node **rb_link,
			struct rb_node *rb_parent)
Linus Torvalds's avatar
Linus Torvalds committed
351
{
352 353 354 355 356 357
	struct address_space *mapping = NULL;

	if (vma->vm_file)
		mapping = vma->vm_file->f_dentry->d_inode->i_mapping;

	if (mapping)
358
		down(&mapping->i_shared_sem);
Linus Torvalds's avatar
Linus Torvalds committed
359 360
	spin_lock(&mm->page_table_lock);
	__vma_link(mm, vma, prev, rb_link, rb_parent);
361
	spin_unlock(&mm->page_table_lock);
362
	if (mapping)
363
		up(&mapping->i_shared_sem);
Linus Torvalds's avatar
Linus Torvalds committed
364

365
	mark_mm_hugetlb(mm, vma);
Linus Torvalds's avatar
Linus Torvalds committed
366 367 368 369
	mm->map_count++;
	validate_mm(mm);
}

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
/*
 * Return true if we can merge this (vm_flags,file,vm_pgoff,size)
 * in front of (at a lower virtual address and file offset than) the vma.
 *
 * We don't check here for the merged mmap wrapping around the end of pagecache
 * indices (16TB on ia32) because do_mmap_pgoff() does not permit mmap's which
 * wrap, nor mmaps which cover the final page at index -1UL.
 */
static int
can_vma_merge_before(struct vm_area_struct *vma, unsigned long vm_flags,
	struct file *file, unsigned long vm_pgoff, unsigned long size)
{
	if (vma->vm_file == file && vma->vm_flags == vm_flags) {
		if (!file)
			return 1;	/* anon mapping */
		if (vma->vm_pgoff == vm_pgoff + size)
			return 1;
	}
	return 0;
}

/*
 * Return true if we can merge this (vm_flags,file,vm_pgoff)
 * beyond (at a higher virtual address and file offset than) the vma.
 */
static int
can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
	struct file *file, unsigned long vm_pgoff)
{
	if (vma->vm_file == file && vma->vm_flags == vm_flags) {
		unsigned long vma_size;

		if (!file)
			return 1;	/* anon mapping */

		vma_size = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
		if (vma->vm_pgoff + vma_size == vm_pgoff)
			return 1;
	}
	return 0;
}

/*
 * Given a new mapping request (addr,end,vm_flags,file,pgoff), figure out
 * whether that can be merged with its predecessor or its successor.  Or
 * both (it neatly fills a hole).
 */
static int vma_merge(struct mm_struct *mm, struct vm_area_struct *prev,
			struct rb_node *rb_parent, unsigned long addr, 
			unsigned long end, unsigned long vm_flags,
			struct file *file, unsigned long pgoff)
Linus Torvalds's avatar
Linus Torvalds committed
421 422
{
	spinlock_t * lock = &mm->page_table_lock;
423

Linus Torvalds's avatar
Linus Torvalds committed
424 425 426 427 428
	if (!prev) {
		prev = rb_entry(rb_parent, struct vm_area_struct, vm_rb);
		goto merge_next;
	}

429 430 431 432 433 434
	/*
	 * Can it merge with the predecessor?
	 */
	if (prev->vm_end == addr &&
			can_vma_merge_after(prev, vm_flags, file, pgoff)) {
		struct vm_area_struct *next;
435
		struct inode *inode = file ? file->f_dentry->d_inode : NULL;
436 437 438 439 440 441 442
		int need_up = 0;

		if (unlikely(file && prev->vm_next &&
				prev->vm_next->vm_file == file)) {
			down(&inode->i_mapping->i_shared_sem);
			need_up = 1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
443 444
		spin_lock(lock);
		prev->vm_end = end;
445 446 447 448

		/*
		 * OK, it did.  Can we now merge in the successor as well?
		 */
Linus Torvalds's avatar
Linus Torvalds committed
449
		next = prev->vm_next;
450 451 452
		if (next && prev->vm_end == next->vm_start &&
				can_vma_merge_before(next, vm_flags, file,
					pgoff, (end - addr) >> PAGE_SHIFT)) {
Linus Torvalds's avatar
Linus Torvalds committed
453 454
			prev->vm_end = next->vm_end;
			__vma_unlink(mm, next, prev);
455
			__remove_shared_vm_struct(next, inode);
Linus Torvalds's avatar
Linus Torvalds committed
456
			spin_unlock(lock);
457 458
			if (need_up)
				up(&inode->i_mapping->i_shared_sem);
Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462 463 464

			mm->map_count--;
			kmem_cache_free(vm_area_cachep, next);
			return 1;
		}
		spin_unlock(lock);
465 466
		if (need_up)
			up(&inode->i_mapping->i_shared_sem);
Linus Torvalds's avatar
Linus Torvalds committed
467 468 469
		return 1;
	}

470 471 472
	/*
	 * Can this new request be merged in front of prev->vm_next?
	 */
Linus Torvalds's avatar
Linus Torvalds committed
473 474 475
	prev = prev->vm_next;
	if (prev) {
 merge_next:
476 477
		if (!can_vma_merge_before(prev, vm_flags, file,
				pgoff, (end - addr) >> PAGE_SHIFT))
Linus Torvalds's avatar
Linus Torvalds committed
478 479 480 481
			return 0;
		if (end == prev->vm_start) {
			spin_lock(lock);
			prev->vm_start = addr;
482
			prev->vm_pgoff -= (end - addr) >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
483 484 485 486 487 488 489 490
			spin_unlock(lock);
			return 1;
		}
	}

	return 0;
}

Andrew Morton's avatar
Andrew Morton committed
491 492 493 494
/*
 * The caller must hold down_write(current->mm->mmap_sem).
 */

Andrew Morton's avatar
Andrew Morton committed
495 496 497
unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
			unsigned long len, unsigned long prot,
			unsigned long flags, unsigned long pgoff)
Linus Torvalds's avatar
Linus Torvalds committed
498 499
{
	struct mm_struct * mm = current->mm;
Linus Torvalds's avatar
Linus Torvalds committed
500
	struct vm_area_struct * vma, * prev;
501
	struct inode *inode;
Linus Torvalds's avatar
Linus Torvalds committed
502
	unsigned int vm_flags;
Linus Torvalds's avatar
Linus Torvalds committed
503 504
	int correct_wcount = 0;
	int error;
505
	struct rb_node ** rb_link, * rb_parent;
Andrew Morton's avatar
Andrew Morton committed
506
	unsigned long charged = 0;
Linus Torvalds's avatar
Linus Torvalds committed
507

Linus Torvalds's avatar
Linus Torvalds committed
508 509 510
	if (file && (!file->f_op || !file->f_op->mmap))
		return -ENODEV;

511
	if (!len)
Linus Torvalds's avatar
Linus Torvalds committed
512 513
		return addr;

Linus Torvalds's avatar
Linus Torvalds committed
514
	if (len > TASK_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
515 516
		return -EINVAL;

517 518
	len = PAGE_ALIGN(len);

Linus Torvalds's avatar
Linus Torvalds committed
519 520 521 522 523 524 525 526
	/* offset overflow? */
	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
		return -EINVAL;

	/* Too many mappings? */
	if (mm->map_count > MAX_MAP_COUNT)
		return -ENOMEM;

Linus Torvalds's avatar
Linus Torvalds committed
527 528 529
	/* Obtain the address to map to. we verify (or select) it and ensure
	 * that it represents a valid section of the address space.
	 */
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532
	addr = get_unmapped_area(file, addr, len, pgoff, flags);
	if (addr & ~PAGE_MASK)
		return addr;
Linus Torvalds's avatar
Linus Torvalds committed
533 534 535 536 537

	/* Do simple checking here so the lower-level routines won't have
	 * to. we assume access permissions have been handled by the open
	 * of the memory object, so we don't do any here.
	 */
538 539
	vm_flags = calc_vm_flags(prot,flags) | mm->def_flags |
			VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds's avatar
Linus Torvalds committed
540

Andrew Morton's avatar
Andrew Morton committed
541 542 543 544 545
	if (flags & MAP_LOCKED) {
		if (!capable(CAP_IPC_LOCK))
			return -EPERM;
		vm_flags |= VM_LOCKED;
	}
Linus Torvalds's avatar
Linus Torvalds committed
546
	/* mlock MCL_FUTURE? */
Linus Torvalds's avatar
Linus Torvalds committed
547
	if (vm_flags & VM_LOCKED) {
Linus Torvalds's avatar
Linus Torvalds committed
548 549 550 551 552 553
		unsigned long locked = mm->locked_vm << PAGE_SHIFT;
		locked += len;
		if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
			return -EAGAIN;
	}

554 555
	inode = file ? file->f_dentry->d_inode : NULL;

Linus Torvalds's avatar
Linus Torvalds committed
556
	if (file) {
Linus Torvalds's avatar
Linus Torvalds committed
557 558
		switch (flags & MAP_TYPE) {
		case MAP_SHARED:
559
			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
Linus Torvalds's avatar
Linus Torvalds committed
560 561
				return -EACCES;

562 563 564 565
			/*
			 * Make sure we don't allow writing to an append-only
			 * file..
			 */
566
			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
Linus Torvalds's avatar
Linus Torvalds committed
567 568
				return -EACCES;

569 570 571
			/*
			 * Make sure there are no mandatory locks on the file.
			 */
572
			if (locks_verify_locked(inode))
Linus Torvalds's avatar
Linus Torvalds committed
573 574
				return -EAGAIN;

Linus Torvalds's avatar
Linus Torvalds committed
575 576 577 578
			vm_flags |= VM_SHARED | VM_MAYSHARE;
			if (!(file->f_mode & FMODE_WRITE))
				vm_flags &= ~(VM_MAYWRITE | VM_SHARED);

Linus Torvalds's avatar
Linus Torvalds committed
579 580 581 582 583 584 585 586 587
			/* fall through */
		case MAP_PRIVATE:
			if (!(file->f_mode & FMODE_READ))
				return -EACCES;
			break;

		default:
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
588 589 590 591 592 593 594 595 596 597 598
	} else {
		vm_flags |= VM_SHARED | VM_MAYSHARE;
		switch (flags & MAP_TYPE) {
		default:
			return -EINVAL;
		case MAP_PRIVATE:
			vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
			/* fall through */
		case MAP_SHARED:
			break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
599 600
	}

601 602
	error = security_file_mmap(file, prot, flags);
	if (error)
603 604
		return error;
		
Linus Torvalds's avatar
Linus Torvalds committed
605 606
	/* Clear old maps */
	error = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609
munmap_back:
	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
	if (vma && vma->vm_start < addr + len) {
610
		if (do_munmap(mm, addr, len))
Linus Torvalds's avatar
Linus Torvalds committed
611 612 613
			return -ENOMEM;
		goto munmap_back;
	}
Linus Torvalds's avatar
Linus Torvalds committed
614 615 616 617 618 619

	/* Check against address space limit. */
	if ((mm->total_vm << PAGE_SHIFT) + len
	    > current->rlim[RLIMIT_AS].rlim_cur)
		return -ENOMEM;

620
	if (!(flags & MAP_NORESERVE) || sysctl_overcommit_memory > 1) {
621 622 623 624
		if (vm_flags & VM_SHARED) {
			/* Check memory availability in shmem_file_setup? */
			vm_flags |= VM_ACCOUNT;
		} else if (vm_flags & VM_WRITE) {
625 626 627
			/*
			 * Private writable mapping: check memory availability
			 */
628 629 630 631 632
			charged = len >> PAGE_SHIFT;
			if (!vm_enough_memory(charged))
				return -ENOMEM;
			vm_flags |= VM_ACCOUNT;
		}
Andrew Morton's avatar
Andrew Morton committed
633
	}
Linus Torvalds's avatar
Linus Torvalds committed
634 635

	/* Can we just expand an old anonymous mapping? */
Linus Torvalds's avatar
Linus Torvalds committed
636
	if (!file && !(vm_flags & VM_SHARED) && rb_parent)
637 638
		if (vma_merge(mm, prev, rb_parent, addr, addr + len,
					vm_flags, NULL, 0))
Linus Torvalds's avatar
Linus Torvalds committed
639
			goto out;
Linus Torvalds's avatar
Linus Torvalds committed
640

641 642
	/*
	 * Determine the object being mapped and call the appropriate
Linus Torvalds's avatar
Linus Torvalds committed
643 644 645 646
	 * specific mapper. the address has already been validated, but
	 * not unmapped, but the maps are removed from the list.
	 */
	vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
Andrew Morton's avatar
Andrew Morton committed
647
	error = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
648
	if (!vma)
Andrew Morton's avatar
Andrew Morton committed
649
		goto unacct_error;
Linus Torvalds's avatar
Linus Torvalds committed
650 651 652 653

	vma->vm_mm = mm;
	vma->vm_start = addr;
	vma->vm_end = addr + len;
Linus Torvalds's avatar
Linus Torvalds committed
654 655
	vma->vm_flags = vm_flags;
	vma->vm_page_prot = protection_map[vm_flags & 0x0f];
Linus Torvalds's avatar
Linus Torvalds committed
656 657 658 659
	vma->vm_ops = NULL;
	vma->vm_pgoff = pgoff;
	vma->vm_file = NULL;
	vma->vm_private_data = NULL;
660
	INIT_LIST_HEAD(&vma->shared);
Linus Torvalds's avatar
Linus Torvalds committed
661 662

	if (file) {
Linus Torvalds's avatar
Linus Torvalds committed
663 664 665
		error = -EINVAL;
		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
			goto free_vma;
Linus Torvalds's avatar
Linus Torvalds committed
666
		if (vm_flags & VM_DENYWRITE) {
Linus Torvalds's avatar
Linus Torvalds committed
667 668 669 670 671 672 673 674 675 676
			error = deny_write_access(file);
			if (error)
				goto free_vma;
			correct_wcount = 1;
		}
		vma->vm_file = file;
		get_file(file);
		error = file->f_op->mmap(file, vma);
		if (error)
			goto unmap_and_free_vma;
677
	} else if (vm_flags & VM_SHARED) {
Linus Torvalds's avatar
Linus Torvalds committed
678 679 680 681 682
		error = shmem_zero_setup(vma);
		if (error)
			goto free_vma;
	}

683 684 685 686 687 688 689 690
	/* We set VM_ACCOUNT in a shared mapping's vm_flags, to inform
	 * shmem_zero_setup (perhaps called through /dev/zero's ->mmap)
	 * that memory reservation must be checked; but that reservation
	 * belongs to shared memory object, not to vma: so now clear it.
	 */
	if ((vm_flags & (VM_SHARED|VM_ACCOUNT)) == (VM_SHARED|VM_ACCOUNT))
		vma->vm_flags &= ~VM_ACCOUNT;

Linus Torvalds's avatar
Linus Torvalds committed
691 692 693 694 695 696 697
	/* Can addr have changed??
	 *
	 * Answer: Yes, several device drivers can do it in their
	 *         f_op->mmap method. -DaveM
	 */
	addr = vma->vm_start;

698 699 700 701 702 703 704 705 706 707 708 709 710
	if (!file || !rb_parent || !vma_merge(mm, prev, rb_parent, addr,
				addr + len, vma->vm_flags, file, pgoff)) {
		vma_link(mm, vma, prev, rb_link, rb_parent);
		if (correct_wcount)
			atomic_inc(&inode->i_writecount);
	} else {
		if (file) {
			if (correct_wcount)
				atomic_inc(&inode->i_writecount);
			fput(file);
		}
		kmem_cache_free(vm_area_cachep, vma);
	}
Linus Torvalds's avatar
Linus Torvalds committed
711
out:	
Linus Torvalds's avatar
Linus Torvalds committed
712
	mm->total_vm += len >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
713
	if (vm_flags & VM_LOCKED) {
Linus Torvalds's avatar
Linus Torvalds committed
714 715 716
		mm->locked_vm += len >> PAGE_SHIFT;
		make_pages_present(addr, addr + len);
	}
Andrew Morton's avatar
Andrew Morton committed
717 718 719 720 721 722
	if (flags & MAP_POPULATE) {
		up_write(&mm->mmap_sem);
		sys_remap_file_pages(addr, len, prot,
					pgoff, flags & MAP_NONBLOCK);
		down_write(&mm->mmap_sem);
	}
Linus Torvalds's avatar
Linus Torvalds committed
723 724 725 726
	return addr;

unmap_and_free_vma:
	if (correct_wcount)
727
		atomic_inc(&inode->i_writecount);
Linus Torvalds's avatar
Linus Torvalds committed
728 729
	vma->vm_file = NULL;
	fput(file);
Linus Torvalds's avatar
Linus Torvalds committed
730

Linus Torvalds's avatar
Linus Torvalds committed
731
	/* Undo any partial mapping done by a device driver. */
Linus Torvalds's avatar
Linus Torvalds committed
732
	zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start);
Linus Torvalds's avatar
Linus Torvalds committed
733 734
free_vma:
	kmem_cache_free(vm_area_cachep, vma);
Andrew Morton's avatar
Andrew Morton committed
735 736 737
unacct_error:
	if (charged)
		vm_unacct_memory(charged);
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740 741
	return error;
}

/* Get an address range which is currently unmapped.
Linus Torvalds's avatar
Linus Torvalds committed
742 743 744 745 746 747 748 749 750
 * For shmat() with addr=0.
 *
 * Ugly calling convention alert:
 * Return value with the low bits set means error value,
 * ie
 *	if (ret & ~PAGE_MASK)
 *		error = ret;
 *
 * This function "knows" that -ENOMEM has the bits set.
Linus Torvalds's avatar
Linus Torvalds committed
751 752
 */
#ifndef HAVE_ARCH_UNMAPPED_AREA
753 754 755
static inline unsigned long
arch_get_unmapped_area(struct file *filp, unsigned long addr,
		unsigned long len, unsigned long pgoff, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
756
{
757
	struct mm_struct *mm = current->mm;
Linus Torvalds's avatar
Linus Torvalds committed
758
	struct vm_area_struct *vma;
759
	int found_hole = 0;
Linus Torvalds's avatar
Linus Torvalds committed
760 761

	if (len > TASK_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
762
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
763 764 765

	if (addr) {
		addr = PAGE_ALIGN(addr);
766
		vma = find_vma(mm, addr);
Linus Torvalds's avatar
Linus Torvalds committed
767 768 769 770
		if (TASK_SIZE - len >= addr &&
		    (!vma || addr + len <= vma->vm_start))
			return addr;
	}
771
	addr = mm->free_area_cache;
Linus Torvalds's avatar
Linus Torvalds committed
772

773
	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
Linus Torvalds's avatar
Linus Torvalds committed
774
		/* At this point:  (!vma || addr < vma->vm_end). */
Linus Torvalds's avatar
Linus Torvalds committed
775
		if (TASK_SIZE - len < addr)
Linus Torvalds's avatar
Linus Torvalds committed
776
			return -ENOMEM;
777 778 779 780 781 782 783
		/*
		 * Record the first available hole.
		 */
		if (!found_hole && (!vma || addr < vma->vm_start)) {
			mm->free_area_cache = addr;
			found_hole = 1;
		}
Linus Torvalds's avatar
Linus Torvalds committed
784
		if (!vma || addr + len <= vma->vm_start)
Linus Torvalds's avatar
Linus Torvalds committed
785
			return addr;
Linus Torvalds's avatar
Linus Torvalds committed
786
		addr = vma->vm_end;
Linus Torvalds's avatar
Linus Torvalds committed
787 788
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
789
#else
790 791 792
extern unsigned long
arch_get_unmapped_area(struct file *, unsigned long, unsigned long,
			unsigned long, unsigned long);
Linus Torvalds's avatar
Linus Torvalds committed
793 794
#endif	

795 796 797
unsigned long
get_unmapped_area(struct file *file, unsigned long addr, unsigned long len,
		unsigned long pgoff, unsigned long flags)
Linus Torvalds's avatar
Linus Torvalds committed
798 799
{
	if (flags & MAP_FIXED) {
Linus Torvalds's avatar
Linus Torvalds committed
800
		if (addr > TASK_SIZE - len)
801
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
802 803
		if (addr & ~PAGE_MASK)
			return -EINVAL;
804 805 806 807 808 809 810
		if (is_file_hugepages(file)) {
			unsigned long ret;

			ret = is_aligned_hugepage_range(addr, len);
			if (ret)
				return ret;
		}
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813 814
		return addr;
	}

	if (file && file->f_op && file->f_op->get_unmapped_area)
815 816
		return file->f_op->get_unmapped_area(file, addr, len,
						pgoff, flags);
Linus Torvalds's avatar
Linus Torvalds committed
817 818 819

	return arch_get_unmapped_area(file, addr, len, pgoff, flags);
}
Linus Torvalds's avatar
Linus Torvalds committed
820 821 822 823 824 825 826 827 828 829 830

/* Look up the first VMA which satisfies  addr < vm_end,  NULL if none. */
struct vm_area_struct * find_vma(struct mm_struct * mm, unsigned long addr)
{
	struct vm_area_struct *vma = NULL;

	if (mm) {
		/* Check the cache first. */
		/* (Cache hit rate is typically around 35%.) */
		vma = mm->mmap_cache;
		if (!(vma && vma->vm_end > addr && vma->vm_start <= addr)) {
831
			struct rb_node * rb_node;
Linus Torvalds's avatar
Linus Torvalds committed
832 833 834 835 836 837 838

			rb_node = mm->mm_rb.rb_node;
			vma = NULL;

			while (rb_node) {
				struct vm_area_struct * vma_tmp;

839 840
				vma_tmp = rb_entry(rb_node,
						struct vm_area_struct, vm_rb);
Linus Torvalds's avatar
Linus Torvalds committed
841 842 843 844

				if (vma_tmp->vm_end > addr) {
					vma = vma_tmp;
					if (vma_tmp->vm_start <= addr)
Linus Torvalds's avatar
Linus Torvalds committed
845
						break;
Linus Torvalds's avatar
Linus Torvalds committed
846 847 848
					rb_node = rb_node->rb_left;
				} else
					rb_node = rb_node->rb_right;
Linus Torvalds's avatar
Linus Torvalds committed
849 850 851 852 853 854 855 856 857
			}
			if (vma)
				mm->mmap_cache = vma;
		}
	}
	return vma;
}

/* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
858 859 860
struct vm_area_struct *
find_vma_prev(struct mm_struct *mm, unsigned long addr,
			struct vm_area_struct **pprev)
Linus Torvalds's avatar
Linus Torvalds committed
861
{
862
	struct vm_area_struct *vma = NULL, *prev = NULL;
863
	struct rb_node * rb_node;
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
	if (!mm)
		goto out;

	/* Guard against addr being lower than the first VMA */
	vma = mm->mmap;

	/* Go through the RB tree quickly. */
	rb_node = mm->mm_rb.rb_node;

	while (rb_node) {
		struct vm_area_struct *vma_tmp;
		vma_tmp = rb_entry(rb_node, struct vm_area_struct, vm_rb);

		if (addr < vma_tmp->vm_end) {
			rb_node = rb_node->rb_left;
		} else {
			prev = vma_tmp;
			if (!prev->vm_next || (addr < prev->vm_next->vm_end))
				break;
			rb_node = rb_node->rb_right;
Linus Torvalds's avatar
Linus Torvalds committed
884 885
		}
	}
886

887
out:
888 889
	*pprev = prev;
	return prev ? prev->vm_next : vma;
Linus Torvalds's avatar
Linus Torvalds committed
890 891
}

892
#ifdef CONFIG_STACK_GROWSUP
Andrew Morton's avatar
Andrew Morton committed
893
/*
894
 * vma is the first one with address > vma->vm_end.  Have to extend vma.
Andrew Morton's avatar
Andrew Morton committed
895 896 897 898 899
 */
int expand_stack(struct vm_area_struct * vma, unsigned long address)
{
	unsigned long grow;

900 901 902
	if (!(vma->vm_flags & VM_GROWSUP))
		return -EFAULT;

Andrew Morton's avatar
Andrew Morton committed
903 904
	/*
	 * vma->vm_start/vm_end cannot change under us because the caller
905
	 * is required to hold the mmap_sem in read mode. We need to get
Andrew Morton's avatar
Andrew Morton committed
906 907
	 * the spinlock only before relocating the vma range ourself.
	 */
908
	address += 4 + PAGE_SIZE - 1;
Andrew Morton's avatar
Andrew Morton committed
909 910
	address &= PAGE_MASK;
 	spin_lock(&vma->vm_mm->page_table_lock);
911
	grow = (address - vma->vm_end) >> PAGE_SHIFT;
Andrew Morton's avatar
Andrew Morton committed
912 913

	/* Overcommit.. */
914
	if (!vm_enough_memory(grow)) {
Andrew Morton's avatar
Andrew Morton committed
915 916 917 918
		spin_unlock(&vma->vm_mm->page_table_lock);
		return -ENOMEM;
	}
	
919
	if (address - vma->vm_start > current->rlim[RLIMIT_STACK].rlim_cur ||
Andrew Morton's avatar
Andrew Morton committed
920 921 922 923 924 925
			((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
			current->rlim[RLIMIT_AS].rlim_cur) {
		spin_unlock(&vma->vm_mm->page_table_lock);
		vm_unacct_memory(grow);
		return -ENOMEM;
	}
926
	vma->vm_end = address;
Andrew Morton's avatar
Andrew Morton committed
927 928 929 930 931 932 933
	vma->vm_mm->total_vm += grow;
	if (vma->vm_flags & VM_LOCKED)
		vma->vm_mm->locked_vm += grow;
	spin_unlock(&vma->vm_mm->page_table_lock);
	return 0;
}

934 935
struct vm_area_struct *
find_extend_vma(struct mm_struct *mm, unsigned long addr)
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
{
	struct vm_area_struct *vma, *prev;

	addr &= PAGE_MASK;
	vma = find_vma_prev(mm, addr, &prev);
	if (vma && (vma->vm_start <= addr))
		return vma;
	if (!prev || expand_stack(prev, addr))
		return NULL;
	if (prev->vm_flags & VM_LOCKED) {
		make_pages_present(addr, prev->vm_end);
	}
	return prev;
}
#else
951 952 953
/*
 * vma is the first one with address < vma->vm_start.  Have to extend vma.
 */
954
int expand_stack(struct vm_area_struct *vma, unsigned long address)
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
{
	unsigned long grow;

	/*
	 * vma->vm_start/vm_end cannot change under us because the caller
	 * is required to hold the mmap_sem in read mode. We need to get
	 * the spinlock only before relocating the vma range ourself.
	 */
	address &= PAGE_MASK;
 	spin_lock(&vma->vm_mm->page_table_lock);
	grow = (vma->vm_start - address) >> PAGE_SHIFT;

	/* Overcommit.. */
	if (!vm_enough_memory(grow)) {
		spin_unlock(&vma->vm_mm->page_table_lock);
		return -ENOMEM;
	}
	
	if (vma->vm_end - address > current->rlim[RLIMIT_STACK].rlim_cur ||
			((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
			current->rlim[RLIMIT_AS].rlim_cur) {
		spin_unlock(&vma->vm_mm->page_table_lock);
		vm_unacct_memory(grow);
		return -ENOMEM;
	}
	vma->vm_start = address;
	vma->vm_pgoff -= grow;
	vma->vm_mm->total_vm += grow;
	if (vma->vm_flags & VM_LOCKED)
		vma->vm_mm->locked_vm += grow;
	spin_unlock(&vma->vm_mm->page_table_lock);
	return 0;
}

989 990
struct vm_area_struct *
find_extend_vma(struct mm_struct * mm, unsigned long addr)
Linus Torvalds's avatar
Linus Torvalds committed
991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
	struct vm_area_struct * vma;
	unsigned long start;

	addr &= PAGE_MASK;
	vma = find_vma(mm,addr);
	if (!vma)
		return NULL;
	if (vma->vm_start <= addr)
		return vma;
	if (!(vma->vm_flags & VM_GROWSDOWN))
		return NULL;
	start = vma->vm_start;
	if (expand_stack(vma, addr))
		return NULL;
	if (vma->vm_flags & VM_LOCKED) {
		make_pages_present(addr, start);
	}
	return vma;
}
1011
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025

/*
 * Try to free as many page directory entries as we can,
 * without having to work very hard at actually scanning
 * the page tables themselves.
 *
 * Right now we try to free page tables if we have a nice
 * PGDIR-aligned area that got free'd up. We could be more
 * granular if we want to, but this is fast and simple,
 * and covers the bad cases.
 *
 * "prev", if it exists, points to a vma before the one
 * we just free'd - but there's no telling how much before.
 */
1026
static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028 1029 1030 1031
	unsigned long start, unsigned long end)
{
	unsigned long first = start & PGDIR_MASK;
	unsigned long last = end + PGDIR_SIZE - 1;
	unsigned long start_index, end_index;
1032
	struct mm_struct *mm = tlb->mm;
Linus Torvalds's avatar
Linus Torvalds committed
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059

	if (!prev) {
		prev = mm->mmap;
		if (!prev)
			goto no_mmaps;
		if (prev->vm_end > start) {
			if (last > prev->vm_start)
				last = prev->vm_start;
			goto no_mmaps;
		}
	}
	for (;;) {
		struct vm_area_struct *next = prev->vm_next;

		if (next) {
			if (next->vm_start < start) {
				prev = next;
				continue;
			}
			if (last > next->vm_start)
				last = next->vm_start;
		}
		if (prev->vm_end > first)
			first = prev->vm_end + PGDIR_SIZE - 1;
		break;
	}
no_mmaps:
1060
	if (last < first)	/* for arches with discontiguous pgd indices */
1061
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1062 1063 1064 1065 1066
	/*
	 * If the PGD bits are not consecutive in the virtual address, the
	 * old method of shifting the VA >> by PGDIR_SHIFT doesn't work.
	 */
	start_index = pgd_index(first);
Russell King's avatar
Russell King committed
1067 1068
	if (start_index < FIRST_USER_PGD_NR)
		start_index = FIRST_USER_PGD_NR;
Linus Torvalds's avatar
Linus Torvalds committed
1069 1070
	end_index = pgd_index(last);
	if (end_index > start_index) {
1071
		clear_page_tables(tlb, start_index, end_index - start_index);
Linus Torvalds's avatar
Linus Torvalds committed
1072 1073 1074 1075
		flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
	}
}

1076 1077 1078 1079 1080 1081
/* Normal function to fix up a mapping
 * This function is the default for when an area has no specific
 * function.  This may be used as part of a more specific routine.
 *
 * By the time this function is called, the area struct has been
 * removed from the process mapping list.
Linus Torvalds's avatar
Linus Torvalds committed
1082
 */
1083
static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1084 1085 1086 1087 1088 1089
{
	size_t len = area->vm_end - area->vm_start;

	area->vm_mm->total_vm -= len >> PAGE_SHIFT;
	if (area->vm_flags & VM_LOCKED)
		area->vm_mm->locked_vm -= len >> PAGE_SHIFT;
1090 1091 1092 1093 1094 1095
	/*
	 * Is this a new hole at the lowest possible address?
	 */
	if (area->vm_start >= TASK_UNMAPPED_BASE &&
				area->vm_start < area->vm_mm->free_area_cache)
	      area->vm_mm->free_area_cache = area->vm_start;
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

	remove_shared_vm_struct(area);

	if (area->vm_ops && area->vm_ops->close)
		area->vm_ops->close(area);
	if (area->vm_file)
		fput(area->vm_file);
	kmem_cache_free(vm_area_cachep, area);
}

/*
 * Update the VMA and inode share lists.
 *
 * Ok - we have the memory areas we should free on the 'free' list,
 * so release them, and do the vma updates.
 */
static void unmap_vma_list(struct mm_struct *mm,
	struct vm_area_struct *mpnt)
{
	do {
		struct vm_area_struct *next = mpnt->vm_next;
		unmap_vma(mm, mpnt);
		mpnt = next;
	} while (mpnt != NULL);
	validate_mm(mm);
}

/*
 * Get rid of page table information in the indicated region.
 *
 * Called with the page table lock held.
 */
static void unmap_region(struct mm_struct *mm,
1129
	struct vm_area_struct *vma,
1130 1131
	struct vm_area_struct *prev,
	unsigned long start,
1132
	unsigned long end)
Linus Torvalds's avatar
Linus Torvalds committed
1133
{
1134
	struct mmu_gather *tlb;
1135
	unsigned long nr_accounted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1136

1137
	lru_add_drain();
1138
	tlb = tlb_gather_mmu(mm, 0);
1139 1140
	unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted);
	vm_unacct_memory(nr_accounted);
1141 1142 1143
	free_pgtables(tlb, prev, start, end);
	tlb_finish_mmu(tlb, start, end);
}
Linus Torvalds's avatar
Linus Torvalds committed
1144

1145
/*
1146 1147
 * Create a list of vma's touched by the unmap, removing them from the mm's
 * vma list as we go..
1148 1149 1150
 *
 * Called with the page_table_lock held.
 */
1151 1152 1153
static void
detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
	struct vm_area_struct *prev, unsigned long end)
1154
{
1155 1156
	struct vm_area_struct **insertion_point;
	struct vm_area_struct *tail_vma = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1157

1158
	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1159
	do {
1160
		rb_erase(&vma->vm_rb, &mm->mm_rb);
1161
		mm->map_count--;
1162 1163 1164 1165 1166 1167
		tail_vma = vma;
		vma = vma->vm_next;
	} while (vma && vma->vm_start < end);
	*insertion_point = vma;
	tail_vma->vm_next = NULL;
	mm->mmap_cache = NULL;		/* Kill the cache. */
1168
}
1169

1170
/*
1171 1172
 * Split a vma into two pieces at address 'addr', a new vma is allocated
 * either for the first part or the the tail.
1173
 */
1174 1175
int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
	      unsigned long addr, int new_below)
1176 1177
{
	struct vm_area_struct *new;
Linus Torvalds's avatar
Linus Torvalds committed
1178

1179 1180
	if (mm->map_count >= MAX_MAP_COUNT)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1181

1182 1183 1184
	new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
	if (!new)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1185

1186
	/* most fields are the same, copy all, and then fixup */
1187 1188
	*new = *vma;

1189 1190
	INIT_LIST_HEAD(&new->shared);

1191 1192 1193
	if (new_below) {
		new->vm_end = addr;
		vma->vm_start = addr;
1194
		vma->vm_pgoff += ((addr - new->vm_start) >> PAGE_SHIFT);
1195 1196 1197 1198 1199
	} else {
		vma->vm_end = addr;
		new->vm_start = addr;
		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1200

1201 1202 1203 1204 1205
	if (new->vm_file)
		get_file(new->vm_file);

	if (new->vm_ops && new->vm_ops->open)
		new->vm_ops->open(new);
1206

1207
	insert_vm_struct(mm, new);
1208 1209
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1210

1211 1212 1213
/* Munmap is split into 2 main parts -- this part which finds
 * what needs doing, and the areas themselves, which do the
 * work.  This now handles partial unmappings.
Andrew Morton's avatar
Andrew Morton committed
1214
 * Jeremy Fitzhardinge <jeremy@goop.org>
1215
 */
1216
int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
{
	unsigned long end;
	struct vm_area_struct *mpnt, *prev, *last;

	if ((start & ~PAGE_MASK) || start > TASK_SIZE || len > TASK_SIZE-start)
		return -EINVAL;

	if ((len = PAGE_ALIGN(len)) == 0)
		return -EINVAL;

	/* Find the first overlapping VMA */
	mpnt = find_vma_prev(mm, start, &prev);
	if (!mpnt)
		return 0;
	/* we have  start < mpnt->vm_end  */

Andrew Morton's avatar
Andrew Morton committed
1233
	if (is_vm_hugetlb_page(mpnt)) {
1234 1235 1236 1237
		int ret = is_aligned_hugepage_range(start, len);

		if (ret)
			return ret;
Andrew Morton's avatar
Andrew Morton committed
1238 1239
	}

1240 1241 1242 1243 1244
	/* if it doesn't overlap, we have nothing.. */
	end = start + len;
	if (mpnt->vm_start >= end)
		return 0;

John Levon's avatar
John Levon committed
1245 1246 1247 1248
	/* Something will probably happen, so notify. */
	if (mpnt->vm_file && (mpnt->vm_flags & VM_EXEC))
		profile_exec_unmap(mm);
 
1249 1250 1251 1252
	/*
	 * If we need to split any vma, do it now to save pain later.
	 */
	if (start > mpnt->vm_start) {
1253
		if (split_vma(mm, mpnt, start, 0))
1254 1255 1256
			return -ENOMEM;
		prev = mpnt;
		mpnt = mpnt->vm_next;
Linus Torvalds's avatar
Linus Torvalds committed
1257 1258
	}

1259 1260 1261
	/* Does it split the last one? */
	last = find_vma(mm, end);
	if (last && end > last->vm_start) {
1262
		if (split_vma(mm, last, end, 0))
1263 1264
			return -ENOMEM;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1265

1266 1267 1268 1269
	/*
	 * Remove the vma's, and unmap the actual pages
	 */
	spin_lock(&mm->page_table_lock);
1270
	detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1271
	unmap_region(mm, mpnt, prev, start, end);
1272
	spin_unlock(&mm->page_table_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1273

1274 1275 1276
	/* Fix up all other VM information */
	unmap_vma_list(mm, mpnt);

Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279 1280 1281 1282 1283 1284
	return 0;
}

asmlinkage long sys_munmap(unsigned long addr, size_t len)
{
	int ret;
	struct mm_struct *mm = current->mm;

Linus Torvalds's avatar
Linus Torvalds committed
1285
	down_write(&mm->mmap_sem);
1286
	ret = do_munmap(mm, addr, len);
Linus Torvalds's avatar
Linus Torvalds committed
1287
	up_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	return ret;
}

/*
 *  this is really a simplified "do_mmap".  it only handles
 *  anonymous maps.  eventually we may be able to do some
 *  brk-specific accounting here.
 */
unsigned long do_brk(unsigned long addr, unsigned long len)
{
	struct mm_struct * mm = current->mm;
Linus Torvalds's avatar
Linus Torvalds committed
1299 1300
	struct vm_area_struct * vma, * prev;
	unsigned long flags;
1301
	struct rb_node ** rb_link, * rb_parent;
Linus Torvalds's avatar
Linus Torvalds committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

	len = PAGE_ALIGN(len);
	if (!len)
		return addr;

	/*
	 * mlock MCL_FUTURE?
	 */
	if (mm->def_flags & VM_LOCKED) {
		unsigned long locked = mm->locked_vm << PAGE_SHIFT;
		locked += len;
		if (locked > current->rlim[RLIMIT_MEMLOCK].rlim_cur)
			return -EAGAIN;
	}

	/*
	 * Clear old maps.  this also does some error checking for us
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1320 1321 1322
 munmap_back:
	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
	if (vma && vma->vm_start < addr + len) {
1323
		if (do_munmap(mm, addr, len))
Linus Torvalds's avatar
Linus Torvalds committed
1324 1325 1326
			return -ENOMEM;
		goto munmap_back;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338

	/* Check against address space limits *after* clearing old maps... */
	if ((mm->total_vm << PAGE_SHIFT) + len
	    > current->rlim[RLIMIT_AS].rlim_cur)
		return -ENOMEM;

	if (mm->map_count > MAX_MAP_COUNT)
		return -ENOMEM;

	if (!vm_enough_memory(len >> PAGE_SHIFT))
		return -ENOMEM;

Andrew Morton's avatar
Andrew Morton committed
1339
	flags = VM_DATA_DEFAULT_FLAGS | VM_ACCOUNT | mm->def_flags;
Linus Torvalds's avatar
Linus Torvalds committed
1340

Linus Torvalds's avatar
Linus Torvalds committed
1341
	/* Can we just expand an old anonymous mapping? */
1342 1343
	if (rb_parent && vma_merge(mm, prev, rb_parent, addr, addr + len,
					flags, NULL, 0))
Linus Torvalds's avatar
Linus Torvalds committed
1344
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1345 1346 1347 1348 1349

	/*
	 * create a vma struct for an anonymous mapping
	 */
	vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
Andrew Morton's avatar
Andrew Morton committed
1350 1351
	if (!vma) {
		vm_unacct_memory(len >> PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
1352
		return -ENOMEM;
Andrew Morton's avatar
Andrew Morton committed
1353
	}
Linus Torvalds's avatar
Linus Torvalds committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363

	vma->vm_mm = mm;
	vma->vm_start = addr;
	vma->vm_end = addr + len;
	vma->vm_flags = flags;
	vma->vm_page_prot = protection_map[flags & 0x0f];
	vma->vm_ops = NULL;
	vma->vm_pgoff = 0;
	vma->vm_file = NULL;
	vma->vm_private_data = NULL;
1364
	INIT_LIST_HEAD(&vma->shared);
Linus Torvalds's avatar
Linus Torvalds committed
1365

Linus Torvalds's avatar
Linus Torvalds committed
1366
	vma_link(mm, vma, prev, rb_link, rb_parent);
Linus Torvalds's avatar
Linus Torvalds committed
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376

out:
	mm->total_vm += len >> PAGE_SHIFT;
	if (flags & VM_LOCKED) {
		mm->locked_vm += len >> PAGE_SHIFT;
		make_pages_present(addr, addr + len);
	}
	return addr;
}

Linus Torvalds's avatar
Linus Torvalds committed
1377 1378
/* Build the RB tree corresponding to the VMA list. */
void build_mmap_rb(struct mm_struct * mm)
Linus Torvalds's avatar
Linus Torvalds committed
1379 1380
{
	struct vm_area_struct * vma;
1381
	struct rb_node ** rb_link, * rb_parent;
Linus Torvalds's avatar
Linus Torvalds committed
1382 1383 1384 1385 1386 1387 1388 1389 1390

	mm->mm_rb = RB_ROOT;
	rb_link = &mm->mm_rb.rb_node;
	rb_parent = NULL;
	for (vma = mm->mmap; vma; vma = vma->vm_next) {
		__vma_link_rb(mm, vma, rb_link, rb_parent);
		rb_parent = &vma->vm_rb;
		rb_link = &rb_parent->rb_right;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1391 1392 1393
}

/* Release all mmaps. */
1394
void exit_mmap(struct mm_struct *mm)
Linus Torvalds's avatar
Linus Torvalds committed
1395
{
1396
	struct mmu_gather *tlb;
1397 1398
	struct vm_area_struct *vma;
	unsigned long nr_accounted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1399

John Levon's avatar
John Levon committed
1400 1401
	profile_exit_mmap(mm);
 
1402 1403
	lru_add_drain();

Linus Torvalds's avatar
Linus Torvalds committed
1404
	spin_lock(&mm->page_table_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1405

1406
	tlb = tlb_gather_mmu(mm, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1407
	flush_cache_mm(mm);
1408
	/* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1409
	mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1410
					~0UL, &nr_accounted);
1411 1412
	vm_unacct_memory(nr_accounted);
	BUG_ON(mm->map_count);	/* This is just debugging */
1413
	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1414
	tlb_finish_mmu(tlb, 0, TASK_SIZE);
1415

1416
	vma = mm->mmap;
1417 1418 1419 1420 1421 1422
	mm->mmap = mm->mmap_cache = NULL;
	mm->mm_rb = RB_ROOT;
	mm->rss = 0;
	mm->total_vm = 0;
	mm->locked_vm = 0;

1423
	spin_unlock(&mm->page_table_lock);
1424 1425 1426 1427 1428

	/*
	 * Walk the list again, actually closing and freeing it
	 * without holding any MM locks.
	 */
1429 1430 1431 1432 1433 1434
	while (vma) {
		struct vm_area_struct *next = vma->vm_next;
		remove_shared_vm_struct(vma);
		if (vma->vm_ops) {
			if (vma->vm_ops->close)
				vma->vm_ops->close(vma);
1435
		}
1436 1437 1438 1439
		if (vma->vm_file)
			fput(vma->vm_file);
		kmem_cache_free(vm_area_cachep, vma);
		vma = next;
1440
	}
Linus Torvalds's avatar
Linus Torvalds committed
1441 1442 1443 1444
}

/* Insert vm structure into process list sorted by address
 * and into the inode's i_mmap ring.  If vm_file is non-NULL
1445
 * then i_shared_sem is taken here.
Linus Torvalds's avatar
Linus Torvalds committed
1446
 */
Linus Torvalds's avatar
Linus Torvalds committed
1447
void insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
Linus Torvalds's avatar
Linus Torvalds committed
1448
{
Linus Torvalds's avatar
Linus Torvalds committed
1449
	struct vm_area_struct * __vma, * prev;
1450
	struct rb_node ** rb_link, * rb_parent;
Linus Torvalds's avatar
Linus Torvalds committed
1451

1452
	__vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
Linus Torvalds's avatar
Linus Torvalds committed
1453 1454 1455 1456
	if (__vma && __vma->vm_start < vma->vm_end)
		BUG();
	vma_link(mm, vma, prev, rb_link, rb_parent);
	validate_mm(mm);
Linus Torvalds's avatar
Linus Torvalds committed
1457
}