mmap.c 50.9 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
#include <linux/slab.h>
#include <linux/shm.h>
#include <linux/mman.h>
#include <linux/pagemap.h>
#include <linux/swap.h>
Andrew Morton's avatar
Andrew Morton committed
14
#include <linux/syscalls.h>
Linus Torvalds's avatar
Linus Torvalds committed
15 16
#include <linux/init.h>
#include <linux/file.h>
Linus Torvalds's avatar
Linus Torvalds committed
17
#include <linux/fs.h>
Linus Torvalds's avatar
Linus Torvalds committed
18
#include <linux/personality.h>
19
#include <linux/security.h>
20
#include <linux/hugetlb.h>
John Levon's avatar
John Levon committed
21
#include <linux/profile.h>
22
#include <linux/module.h>
23
#include <linux/mount.h>
Andrew Morton's avatar
Andrew Morton committed
24
#include <linux/mempolicy.h>
25
#include <linux/rmap.h>
Linus Torvalds's avatar
Linus Torvalds committed
26 27

#include <asm/uaccess.h>
28
#include <asm/cacheflush.h>
29 30
#include <asm/tlb.h>

Linus Torvalds's avatar
Linus Torvalds committed
31 32 33 34 35 36
/*
 * 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* 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
};

57
int sysctl_overcommit_memory = OVERCOMMIT_GUESS;  /* heuristic overcommit */
Andrew Morton's avatar
Andrew Morton committed
58
int sysctl_overcommit_ratio = 50;	/* default is 50% */
59
int sysctl_max_map_count = DEFAULT_MAX_MAP_COUNT;
Andrew Morton's avatar
Andrew Morton committed
60 61
atomic_t vm_committed_space = ATOMIC_INIT(0);

62 63
EXPORT_SYMBOL(sysctl_overcommit_memory);
EXPORT_SYMBOL(sysctl_overcommit_ratio);
64
EXPORT_SYMBOL(sysctl_max_map_count);
65
EXPORT_SYMBOL(vm_committed_space);
Andrew Morton's avatar
Andrew Morton committed
66

67
/*
68
 * Requires inode->i_mapping->i_mmap_lock
69
 */
70
static void __remove_shared_vm_struct(struct vm_area_struct *vma,
71
		struct file *file, struct address_space *mapping)
72
{
Andrew Morton's avatar
Andrew Morton committed
73 74
	if (vma->vm_flags & VM_DENYWRITE)
		atomic_inc(&file->f_dentry->d_inode->i_writecount);
75 76
	if (vma->vm_flags & VM_SHARED)
		mapping->i_mmap_writable--;
77

78
	flush_dcache_mmap_lock(mapping);
79 80
	if (unlikely(vma->vm_flags & VM_NONLINEAR))
		list_del_init(&vma->shared.vm_set.list);
81 82
	else
		vma_prio_tree_remove(vma, &mapping->i_mmap);
83
	flush_dcache_mmap_unlock(mapping);
84 85
}

86
/*
87
 * Remove one vm structure and free it.
88
 */
89
static void remove_vm_struct(struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
90
{
91
	struct file *file = vma->vm_file;
Linus Torvalds's avatar
Linus Torvalds committed
92

93
	might_sleep();
Linus Torvalds's avatar
Linus Torvalds committed
94
	if (file) {
95
		struct address_space *mapping = file->f_mapping;
96
		spin_lock(&mapping->i_mmap_lock);
97
		__remove_shared_vm_struct(vma, file, mapping);
98
		spin_unlock(&mapping->i_mmap_lock);
Linus Torvalds's avatar
Linus Torvalds committed
99
	}
100 101 102 103
	if (vma->vm_ops && vma->vm_ops->close)
		vma->vm_ops->close(vma);
	if (file)
		fput(file);
104
	anon_vma_unlink(vma);
105 106
	mpol_free(vma_policy(vma));
	kmem_cache_free(vm_area_cachep, vma);
Linus Torvalds's avatar
Linus Torvalds committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

/*
 *  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
122
	down_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
123 124 125 126 127 128 129 130 131 132

	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) {
133
		if (!do_munmap(mm, newbrk, oldbrk-newbrk))
Linus Torvalds's avatar
Linus Torvalds committed
134 135 136 137 138
			goto set_brk;
		goto out;
	}

	/* Check against rlimit.. */
139
	rlim = current->signal->rlim[RLIMIT_DATA].rlim_cur;
Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153
	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
154
	up_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
155 156 157
	return retval;
}

Linus Torvalds's avatar
Linus Torvalds committed
158
#ifdef DEBUG_MM_RB
159 160 161
static int browse_rb(struct rb_root *root)
{
	int i = 0, j;
162 163 164 165 166 167 168 169 170 171 172 173
	struct rb_node *nd, *pn = NULL;
	unsigned long prev = 0, pend = 0;

	for (nd = rb_first(root); nd; nd = rb_next(nd)) {
		struct vm_area_struct *vma;
		vma = rb_entry(nd, struct vm_area_struct, vm_rb);
		if (vma->vm_start < prev)
			printk("vm_start %lx prev %lx\n", vma->vm_start, prev), i = -1;
		if (vma->vm_start < pend)
			printk("vm_start %lx pend %lx\n", vma->vm_start, pend);
		if (vma->vm_start > vma->vm_end)
			printk("vm_end %lx < vm_start %lx\n", vma->vm_end, vma->vm_start);
Linus Torvalds's avatar
Linus Torvalds committed
174
		i++;
175 176 177 178 179
		pn = nd;
	}
	j = 0;
	for (nd = pn; nd; nd = rb_prev(nd)) {
		j++;
Linus Torvalds's avatar
Linus Torvalds committed
180
	}
181 182
	if (i != j)
		printk("backwards %d, forwards %d\n", j, i), i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185
	return i;
}

186 187
void validate_mm(struct mm_struct *mm)
{
Linus Torvalds's avatar
Linus Torvalds committed
188 189
	int bug = 0;
	int i = 0;
190
	struct vm_area_struct *tmp = mm->mmap;
Linus Torvalds's avatar
Linus Torvalds committed
191 192 193 194 195 196
	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;
197
	i = browse_rb(&mm->mm_rb);
Linus Torvalds's avatar
Linus Torvalds committed
198 199 200 201 202 203 204 205 206
	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

207 208 209 210
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
211 212
{
	struct vm_area_struct * vma;
213
	struct rb_node ** __rb_link, * __rb_parent, * rb_prev;
Linus Torvalds's avatar
Linus Torvalds committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

	__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;
}

244 245 246
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
247 248 249 250 251 252 253
{
	if (prev) {
		vma->vm_next = prev->vm_next;
		prev->vm_next = vma;
	} else {
		mm->mmap = vma;
		if (rb_parent)
254 255
			vma->vm_next = rb_entry(rb_parent,
					struct vm_area_struct, vm_rb);
Linus Torvalds's avatar
Linus Torvalds committed
256 257 258 259 260
		else
			vma->vm_next = NULL;
	}
}

Andrew Morton's avatar
Andrew Morton committed
261 262
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
263 264 265 266 267
{
	rb_link_node(&vma->vm_rb, rb_parent, rb_link);
	rb_insert_color(&vma->vm_rb, &mm->mm_rb);
}

268
static inline void __vma_link_file(struct vm_area_struct *vma)
Linus Torvalds's avatar
Linus Torvalds committed
269 270 271 272 273
{
	struct file * file;

	file = vma->vm_file;
	if (file) {
274
		struct address_space *mapping = file->f_mapping;
Linus Torvalds's avatar
Linus Torvalds committed
275 276

		if (vma->vm_flags & VM_DENYWRITE)
277
			atomic_dec(&file->f_dentry->d_inode->i_writecount);
278 279
		if (vma->vm_flags & VM_SHARED)
			mapping->i_mmap_writable++;
Linus Torvalds's avatar
Linus Torvalds committed
280

281
		flush_dcache_mmap_lock(mapping);
282
		if (unlikely(vma->vm_flags & VM_NONLINEAR))
283
			vma_nonlinear_insert(vma, &mapping->i_mmap_nonlinear);
284
		else
285
			vma_prio_tree_insert(vma, &mapping->i_mmap);
286
		flush_dcache_mmap_unlock(mapping);
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289
	}
}

290 291 292 293
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
294 295 296
{
	__vma_link_list(mm, vma, prev, rb_parent);
	__vma_link_rb(mm, vma, rb_link, rb_parent);
297
	__anon_vma_link(vma);
Linus Torvalds's avatar
Linus Torvalds committed
298 299
}

300 301 302
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
303
{
304 305 306
	struct address_space *mapping = NULL;

	if (vma->vm_file)
307
		mapping = vma->vm_file->f_mapping;
308 309

	if (mapping)
310
		spin_lock(&mapping->i_mmap_lock);
311
	anon_vma_lock(vma);
312

Linus Torvalds's avatar
Linus Torvalds committed
313
	__vma_link(mm, vma, prev, rb_link, rb_parent);
314 315
	__vma_link_file(vma);

316
	anon_vma_unlock(vma);
317
	if (mapping)
318
		spin_unlock(&mapping->i_mmap_lock);
Linus Torvalds's avatar
Linus Torvalds committed
319 320 321 322 323

	mm->map_count++;
	validate_mm(mm);
}

324
/*
325 326 327
 * Helper for vma_adjust in the split_vma insert case:
 * insert vm structure into list and rbtree and anon_vma,
 * but it has already been inserted into prio_tree earlier.
328 329 330 331 332 333 334 335 336 337 338 339 340 341
 */
static void
__insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
{
	struct vm_area_struct * __vma, * prev;
	struct rb_node ** rb_link, * rb_parent;

	__vma = find_vma_prepare(mm, vma->vm_start,&prev, &rb_link, &rb_parent);
	if (__vma && __vma->vm_start < vma->vm_end)
		BUG();
	__vma_link(mm, vma, prev, rb_link, rb_parent);
	mm->map_count++;
}

342 343 344 345 346 347 348 349 350 351
static inline void
__vma_unlink(struct mm_struct *mm, struct vm_area_struct *vma,
		struct vm_area_struct *prev)
{
	prev->vm_next = vma->vm_next;
	rb_erase(&vma->vm_rb, &mm->mm_rb);
	if (mm->mmap_cache == vma)
		mm->mmap_cache = prev;
}

Andrew Morton's avatar
Andrew Morton committed
352
/*
353 354
 * We cannot adjust vm_start, vm_end, vm_pgoff fields of a vma that
 * is already present in an i_mmap tree without adjusting the tree.
Andrew Morton's avatar
Andrew Morton committed
355
 * The following helper function should be used when such adjustments
356
 * are necessary.  The "insert" vma (if any) is to be inserted
Andrew Morton's avatar
Andrew Morton committed
357 358 359
 * before we drop the necessary locks.
 */
void vma_adjust(struct vm_area_struct *vma, unsigned long start,
360
	unsigned long end, pgoff_t pgoff, struct vm_area_struct *insert)
Andrew Morton's avatar
Andrew Morton committed
361 362
{
	struct mm_struct *mm = vma->vm_mm;
363
	struct vm_area_struct *next = vma->vm_next;
364
	struct vm_area_struct *importer = NULL;
Andrew Morton's avatar
Andrew Morton committed
365
	struct address_space *mapping = NULL;
366
	struct prio_tree_root *root = NULL;
Andrew Morton's avatar
Andrew Morton committed
367
	struct file *file = vma->vm_file;
368
	struct anon_vma *anon_vma = NULL;
369 370 371 372 373
	long adjust_next = 0;
	int remove_next = 0;

	if (next && !insert) {
		if (end >= next->vm_end) {
374 375 376 377
			/*
			 * vma expands, overlapping all the next, and
			 * perhaps the one after too (mprotect case 6).
			 */
378 379
again:			remove_next = 1 + (end > next->vm_end);
			end = next->vm_end;
380
			anon_vma = next->anon_vma;
381
		} else if (end > next->vm_start) {
382 383
			/*
			 * vma expands, overlapping part of the next:
384 385 386 387
			 * mprotect case 5 shifting the boundary up.
			 */
			adjust_next = (end - next->vm_start) >> PAGE_SHIFT;
			anon_vma = next->anon_vma;
388
			importer = vma;
389 390 391 392 393
		} else if (end < vma->vm_end) {
			/*
			 * vma shrinks, and !insert tells it's not
			 * split_vma inserting another: so it must be
			 * mprotect case 4 shifting the boundary down.
394
			 */
395
			adjust_next = - ((vma->vm_end - end) >> PAGE_SHIFT);
396
			anon_vma = next->anon_vma;
397
			importer = next;
398 399
		}
	}
Andrew Morton's avatar
Andrew Morton committed
400 401 402

	if (file) {
		mapping = file->f_mapping;
403
		if (!(vma->vm_flags & VM_NONLINEAR))
404
			root = &mapping->i_mmap;
Andrew Morton's avatar
Andrew Morton committed
405
		spin_lock(&mapping->i_mmap_lock);
406 407 408 409 410 411 412 413 414
		if (insert) {
			/*
			 * Put into prio_tree now, so instantiated pages
			 * are visible to arm/parisc __flush_dcache_page
			 * throughout; but we cannot insert into address
			 * space until vma start or end is updated.
			 */
			__vma_link_file(insert);
		}
Andrew Morton's avatar
Andrew Morton committed
415
	}
416 417 418 419 420 421 422

	/*
	 * When changing only vma->vm_end, we don't really need
	 * anon_vma lock: but is that case worth optimizing out?
	 */
	if (vma->anon_vma)
		anon_vma = vma->anon_vma;
423
	if (anon_vma) {
424
		spin_lock(&anon_vma->lock);
425 426 427 428 429 430 431 432 433 434
		/*
		 * Easily overlooked: when mprotect shifts the boundary,
		 * make sure the expanding vma has anon_vma set if the
		 * shrinking vma had, to cover any anon pages imported.
		 */
		if (importer && !importer->anon_vma) {
			importer->anon_vma = anon_vma;
			__anon_vma_link(importer);
		}
	}
Andrew Morton's avatar
Andrew Morton committed
435

436 437
	if (root) {
		flush_dcache_mmap_lock(mapping);
438
		vma_prio_tree_remove(vma, root);
439 440
		if (adjust_next)
			vma_prio_tree_remove(next, root);
441
	}
442

Andrew Morton's avatar
Andrew Morton committed
443 444 445
	vma->vm_start = start;
	vma->vm_end = end;
	vma->vm_pgoff = pgoff;
446
	if (adjust_next) {
447 448
		next->vm_start += adjust_next << PAGE_SHIFT;
		next->vm_pgoff += adjust_next;
449 450
	}

451
	if (root) {
452
		if (adjust_next)
453
			vma_prio_tree_insert(next, root);
454
		vma_prio_tree_insert(vma, root);
455
		flush_dcache_mmap_unlock(mapping);
456
	}
Andrew Morton's avatar
Andrew Morton committed
457

458 459 460 461 462 463 464 465
	if (remove_next) {
		/*
		 * vma_merge has merged next into vma, and needs
		 * us to remove next before dropping the locks.
		 */
		__vma_unlink(mm, next, vma);
		if (file)
			__remove_shared_vm_struct(next, file, mapping);
466 467
		if (next->anon_vma)
			__anon_vma_merge(vma, next);
468 469 470 471 472 473 474
	} else if (insert) {
		/*
		 * split_vma has split insert from vma, and needs
		 * us to insert it before dropping the locks
		 * (it may either follow vma or precede it).
		 */
		__insert_vm_struct(mm, insert);
Andrew Morton's avatar
Andrew Morton committed
475 476
	}

477 478
	if (anon_vma)
		spin_unlock(&anon_vma->lock);
Andrew Morton's avatar
Andrew Morton committed
479 480
	if (mapping)
		spin_unlock(&mapping->i_mmap_lock);
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499

	if (remove_next) {
		if (file)
			fput(file);
		mm->map_count--;
		mpol_free(vma_policy(next));
		kmem_cache_free(vm_area_cachep, next);
		/*
		 * In mprotect's case 6 (see comments on vma_merge),
		 * we must remove another next too. It would clutter
		 * up the code too much to do both in one go.
		 */
		if (remove_next == 2) {
			next = vma->vm_next;
			goto again;
		}
	}

	validate_mm(mm);
Andrew Morton's avatar
Andrew Morton committed
500 501
}

502 503 504 505 506 507 508 509 510
/*
 * If the vma has a ->close operation then the driver probably needs to release
 * per-vma resources, so we don't attempt to merge those.
 */
#define VM_SPECIAL (VM_IO | VM_DONTCOPY | VM_DONTEXPAND | VM_RESERVED)

static inline int is_mergeable_vma(struct vm_area_struct *vma,
			struct file *file, unsigned long vm_flags)
{
511
	if (vma->vm_flags != vm_flags)
512 513 514
		return 0;
	if (vma->vm_file != file)
		return 0;
515
	if (vma->vm_ops && vma->vm_ops->close)
516 517 518 519
		return 0;
	return 1;
}

520 521 522 523 524 525
static inline int is_mergeable_anon_vma(struct anon_vma *anon_vma1,
					struct anon_vma *anon_vma2)
{
	return !anon_vma1 || !anon_vma2 || (anon_vma1 == anon_vma2);
}

526
/*
527
 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
528 529
 * in front of (at a lower virtual address and file offset than) the vma.
 *
530 531 532
 * We cannot merge two vmas if they have differently assigned (non-NULL)
 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
 *
533 534 535 536 537 538
 * 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,
539
	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
540
{
541 542
	if (is_mergeable_vma(vma, file, vm_flags) &&
	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
543
		if (vma->vm_pgoff == vm_pgoff)
544 545 546 547 548 549
			return 1;
	}
	return 0;
}

/*
550
 * Return true if we can merge this (vm_flags,anon_vma,file,vm_pgoff)
551
 * beyond (at a higher virtual address and file offset than) the vma.
552 553 554
 *
 * We cannot merge two vmas if they have differently assigned (non-NULL)
 * anon_vmas, nor if same anon_vma is assigned but offsets incompatible.
555 556 557
 */
static int
can_vma_merge_after(struct vm_area_struct *vma, unsigned long vm_flags,
558
	struct anon_vma *anon_vma, struct file *file, pgoff_t vm_pgoff)
559
{
560 561
	if (is_mergeable_vma(vma, file, vm_flags) &&
	    is_mergeable_anon_vma(anon_vma, vma->anon_vma)) {
562 563 564
		pgoff_t vm_pglen;
		vm_pglen = (vma->vm_end - vma->vm_start) >> PAGE_SHIFT;
		if (vma->vm_pgoff + vm_pglen == vm_pgoff)
565 566 567 568 569 570
			return 1;
	}
	return 0;
}

/*
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
 * Given a 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).
 *
 * In most cases - when called for mmap, brk or mremap - [addr,end) is
 * certain not to be mapped by the time vma_merge is called; but when
 * called for mprotect, it is certain to be already mapped (either at
 * an offset within prev, or at the start of next), and the flags of
 * this area are about to be changed to vm_flags - and the no-change
 * case has already been eliminated.
 *
 * The following mprotect cases have to be considered, where AAAA is
 * the area passed down from mprotect_fixup, never extending beyond one
 * vma, PPPPPP is the prev vma specified, and NNNNNN the next vma after:
 *
 *     AAAA             AAAA                AAAA          AAAA
 *    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPPPNNNNNN    PPPPNNNNXXXX
 *    cannot merge    might become    might become    might become
 *                    PPNNNNNNNNNN    PPPPPPPPPPNN    PPPPPPPPPPPP 6 or
 *    mmap, brk or    case 4 below    case 5 below    PPPPPPPPXXXX 7 or
 *    mremap move:                                    PPPPNNNNNNNN 8
 *        AAAA
 *    PPPP    NNNN    PPPPPPPPPPPP    PPPPPPPPNNNN    PPPPNNNNNNNN
 *    might become    case 1 below    case 2 below    case 3 below
 *
 * Odd one out? Case 8, because it extends NNNN but needs flags of XXXX:
 * mprotect_fixup updates vm_flags & vm_page_prot on successful return.
598
 */
599
struct vm_area_struct *vma_merge(struct mm_struct *mm,
600
			struct vm_area_struct *prev, unsigned long addr,
601
			unsigned long end, unsigned long vm_flags,
602 603
		     	struct anon_vma *anon_vma, struct file *file,
			pgoff_t pgoff, struct mempolicy *policy)
Linus Torvalds's avatar
Linus Torvalds committed
604
{
605
	pgoff_t pglen = (end - addr) >> PAGE_SHIFT;
606
	struct vm_area_struct *area, *next;
607

608
	/*
Andrew Morton's avatar
Andrew Morton committed
609 610
	 * We later require that vma->vm_flags == vm_flags,
	 * so this tests vma->vm_flags & VM_SPECIAL, too.
611 612
	 */
	if (vm_flags & VM_SPECIAL)
613
		return NULL;
614

615 616 617
	if (prev)
		next = prev->vm_next;
	else
618
		next = mm->mmap;
619 620 621
	area = next;
	if (next && next->vm_end == end)		/* cases 6, 7, 8 */
		next = next->vm_next;
Linus Torvalds's avatar
Linus Torvalds committed
622

623 624 625
	/*
	 * Can it merge with the predecessor?
	 */
626
	if (prev && prev->vm_end == addr &&
Andrew Morton's avatar
Andrew Morton committed
627
  			mpol_equal(vma_policy(prev), policy) &&
628 629
			can_vma_merge_after(prev, vm_flags,
						anon_vma, file, pgoff)) {
630
		/*
Andrew Morton's avatar
Andrew Morton committed
631
		 * OK, it can.  Can we now merge in the successor as well?
632
		 */
Andrew Morton's avatar
Andrew Morton committed
633 634
		if (next && end == next->vm_start &&
				mpol_equal(policy, vma_policy(next)) &&
635 636 637 638
				can_vma_merge_before(next, vm_flags,
					anon_vma, file, pgoff+pglen) &&
				is_mergeable_anon_vma(prev->anon_vma,
						      next->anon_vma)) {
639
							/* cases 1, 6 */
Andrew Morton's avatar
Andrew Morton committed
640
			vma_adjust(prev, prev->vm_start,
641 642
				next->vm_end, prev->vm_pgoff, NULL);
		} else					/* cases 2, 5, 7 */
Andrew Morton's avatar
Andrew Morton committed
643 644
			vma_adjust(prev, prev->vm_start,
				end, prev->vm_pgoff, NULL);
645
		return prev;
Linus Torvalds's avatar
Linus Torvalds committed
646 647
	}

648
	/*
Andrew Morton's avatar
Andrew Morton committed
649
	 * Can this new request be merged in front of next?
650
	 */
651 652
	if (next && end == next->vm_start &&
 			mpol_equal(policy, vma_policy(next)) &&
653 654
			can_vma_merge_before(next, vm_flags,
					anon_vma, file, pgoff+pglen)) {
655 656 657 658 659
		if (prev && addr < prev->vm_end)	/* case 4 */
			vma_adjust(prev, prev->vm_start,
				addr, prev->vm_pgoff, NULL);
		else					/* cases 3, 8 */
			vma_adjust(area, addr, next->vm_end,
660
				next->vm_pgoff - pglen, NULL);
661
		return area;
Linus Torvalds's avatar
Linus Torvalds committed
662 663
	}

664
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
665 666
}

667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
/*
 * find_mergeable_anon_vma is used by anon_vma_prepare, to check
 * neighbouring vmas for a suitable anon_vma, before it goes off
 * to allocate a new anon_vma.  It checks because a repetitive
 * sequence of mprotects and faults may otherwise lead to distinct
 * anon_vmas being allocated, preventing vma merge in subsequent
 * mprotect.
 */
struct anon_vma *find_mergeable_anon_vma(struct vm_area_struct *vma)
{
	struct vm_area_struct *near;
	unsigned long vm_flags;

	near = vma->vm_next;
	if (!near)
		goto try_prev;

	/*
	 * Since only mprotect tries to remerge vmas, match flags
	 * which might be mprotected into each other later on.
	 * Neither mlock nor madvise tries to remerge at present,
	 * so leave their flags as obstructing a merge.
	 */
	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);

	if (near->anon_vma && vma->vm_end == near->vm_start &&
 			mpol_equal(vma_policy(vma), vma_policy(near)) &&
			can_vma_merge_before(near, vm_flags,
				NULL, vma->vm_file, vma->vm_pgoff +
				((vma->vm_end - vma->vm_start) >> PAGE_SHIFT)))
		return near->anon_vma;
try_prev:
	/*
	 * It is potentially slow to have to call find_vma_prev here.
	 * But it's only on the first write fault on the vma, not
	 * every time, and we could devise a way to avoid it later
	 * (e.g. stash info in next's anon_vma_node when assigning
	 * an anon_vma, or when trying vma_merge).  Another time.
	 */
	if (find_vma_prev(vma->vm_mm, vma->vm_start, &near) != vma)
		BUG();
	if (!near)
		goto none;

	vm_flags = vma->vm_flags & ~(VM_READ|VM_WRITE|VM_EXEC);
	vm_flags |= near->vm_flags & (VM_READ|VM_WRITE|VM_EXEC);

	if (near->anon_vma && near->vm_end == vma->vm_start &&
  			mpol_equal(vma_policy(near), vma_policy(vma)) &&
			can_vma_merge_after(near, vm_flags,
				NULL, vma->vm_file, vma->vm_pgoff))
		return near->anon_vma;
none:
	/*
	 * There's no absolute need to look only at touching neighbours:
	 * we could search further afield for "compatible" anon_vmas.
	 * But it would probably just be a waste of time searching,
	 * or lead to too many vmas hanging off the same anon_vma.
	 * We're trying to allow mprotect remerging later on,
	 * not trying to minimize memory used for anon_vmas.
	 */
	return NULL;
}

732
#ifdef CONFIG_PROC_FS
733 734 735 736 737 738 739 740 741 742 743 744 745 746
void __vm_stat_account(struct mm_struct *mm, unsigned long flags,
						struct file *file, long pages)
{
	const unsigned long stack_flags
		= VM_STACK_FLAGS & (VM_GROWSUP|VM_GROWSDOWN);

#ifdef CONFIG_HUGETLB
	if (flags & VM_HUGETLB) {
		if (!(flags & VM_DONTCOPY))
			mm->shared_vm += pages;
		return;
	}
#endif /* CONFIG_HUGETLB */

747
	if (file) {
748
		mm->shared_vm += pages;
749 750 751
		if ((flags & (VM_EXEC|VM_WRITE)) == VM_EXEC)
			mm->exec_vm += pages;
	} else if (flags & stack_flags)
752
		mm->stack_vm += pages;
753 754
	if (flags & (VM_RESERVED|VM_IO))
		mm->reserved_vm += pages;
755
}
756
#endif /* CONFIG_PROC_FS */
757

Andrew Morton's avatar
Andrew Morton committed
758 759 760 761
/*
 * The caller must hold down_write(current->mm->mmap_sem).
 */

Andrew Morton's avatar
Andrew Morton committed
762 763 764
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
765 766
{
	struct mm_struct * mm = current->mm;
Linus Torvalds's avatar
Linus Torvalds committed
767
	struct vm_area_struct * vma, * prev;
768
	struct inode *inode;
Linus Torvalds's avatar
Linus Torvalds committed
769
	unsigned int vm_flags;
Linus Torvalds's avatar
Linus Torvalds committed
770 771
	int correct_wcount = 0;
	int error;
772
	struct rb_node ** rb_link, * rb_parent;
773
	int accountable = 1;
Andrew Morton's avatar
Andrew Morton committed
774
	unsigned long charged = 0;
Linus Torvalds's avatar
Linus Torvalds committed
775

776
	if (file) {
777 778 779
		if (is_file_hugepages(file))
			accountable = 0;

780 781
		if (!file->f_op || !file->f_op->mmap)
			return -ENODEV;
Linus Torvalds's avatar
Linus Torvalds committed
782

783 784
		if ((prot & PROT_EXEC) &&
		    (file->f_vfsmnt->mnt_flags & MNT_NOEXEC))
785 786
			return -EPERM;
	}
787 788 789 790 791 792 793 794 795
	/*
	 * Does the application expect PROT_READ to imply PROT_EXEC?
	 *
	 * (the exception is when the underlying filesystem is noexec
	 *  mounted, in which case we dont add PROT_EXEC.)
	 */
	if ((prot & PROT_READ) && (current->personality & READ_IMPLIES_EXEC))
		if (!(file && (file->f_vfsmnt->mnt_flags & MNT_NOEXEC)))
			prot |= PROT_EXEC;
796

797
	if (!len)
Linus Torvalds's avatar
Linus Torvalds committed
798 799
		return addr;

800
	/* Careful about overflows.. */
801
	len = PAGE_ALIGN(len);
802 803
	if (!len || len > TASK_SIZE)
		return -EINVAL;
804

Linus Torvalds's avatar
Linus Torvalds committed
805 806 807 808 809
	/* offset overflow? */
	if ((pgoff + (len >> PAGE_SHIFT)) < pgoff)
		return -EINVAL;

	/* Too many mappings? */
810
	if (mm->map_count > sysctl_max_map_count)
Linus Torvalds's avatar
Linus Torvalds committed
811 812
		return -ENOMEM;

Linus Torvalds's avatar
Linus Torvalds committed
813 814 815
	/* 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
816 817 818
	addr = get_unmapped_area(file, addr, len, pgoff, flags);
	if (addr & ~PAGE_MASK)
		return addr;
Linus Torvalds's avatar
Linus Torvalds committed
819 820 821 822 823

	/* 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.
	 */
824 825
	vm_flags = calc_vm_prot_bits(prot) | calc_vm_flag_bits(flags) |
			mm->def_flags | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC;
Linus Torvalds's avatar
Linus Torvalds committed
826

Andrew Morton's avatar
Andrew Morton committed
827
	if (flags & MAP_LOCKED) {
828
		if (!can_do_mlock())
Andrew Morton's avatar
Andrew Morton committed
829 830 831
			return -EPERM;
		vm_flags |= VM_LOCKED;
	}
Linus Torvalds's avatar
Linus Torvalds committed
832
	/* mlock MCL_FUTURE? */
Linus Torvalds's avatar
Linus Torvalds committed
833
	if (vm_flags & VM_LOCKED) {
834 835
		unsigned long locked, lock_limit;
		locked = mm->locked_vm << PAGE_SHIFT;
836
		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
Linus Torvalds's avatar
Linus Torvalds committed
837
		locked += len;
838
		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
Linus Torvalds's avatar
Linus Torvalds committed
839 840 841
			return -EAGAIN;
	}

842 843
	inode = file ? file->f_dentry->d_inode : NULL;

Linus Torvalds's avatar
Linus Torvalds committed
844
	if (file) {
Linus Torvalds's avatar
Linus Torvalds committed
845 846
		switch (flags & MAP_TYPE) {
		case MAP_SHARED:
847
			if ((prot&PROT_WRITE) && !(file->f_mode&FMODE_WRITE))
Linus Torvalds's avatar
Linus Torvalds committed
848 849
				return -EACCES;

850 851 852 853
			/*
			 * Make sure we don't allow writing to an append-only
			 * file..
			 */
854
			if (IS_APPEND(inode) && (file->f_mode & FMODE_WRITE))
Linus Torvalds's avatar
Linus Torvalds committed
855 856
				return -EACCES;

857 858 859
			/*
			 * Make sure there are no mandatory locks on the file.
			 */
860
			if (locks_verify_locked(inode))
Linus Torvalds's avatar
Linus Torvalds committed
861 862
				return -EAGAIN;

Linus Torvalds's avatar
Linus Torvalds committed
863 864 865 866
			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
867 868 869 870 871 872 873 874 875
			/* fall through */
		case MAP_PRIVATE:
			if (!(file->f_mode & FMODE_READ))
				return -EACCES;
			break;

		default:
			return -EINVAL;
		}
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878
	} else {
		switch (flags & MAP_TYPE) {
		case MAP_SHARED:
879 880 881
			vm_flags |= VM_SHARED | VM_MAYSHARE;
			break;
		case MAP_PRIVATE:
882 883 884 885
			/*
			 * Set pgoff according to addr for anon_vma.
			 */
			pgoff = addr >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
886
			break;
887 888
		default:
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
889
		}
Linus Torvalds's avatar
Linus Torvalds committed
890 891
	}

892 893
	error = security_file_mmap(file, prot, flags);
	if (error)
894 895
		return error;
		
Linus Torvalds's avatar
Linus Torvalds committed
896 897
	/* Clear old maps */
	error = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900
munmap_back:
	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
	if (vma && vma->vm_start < addr + len) {
901
		if (do_munmap(mm, addr, len))
Linus Torvalds's avatar
Linus Torvalds committed
902 903 904
			return -ENOMEM;
		goto munmap_back;
	}
Linus Torvalds's avatar
Linus Torvalds committed
905 906 907

	/* Check against address space limit. */
	if ((mm->total_vm << PAGE_SHIFT) + len
908
	    > current->signal->rlim[RLIMIT_AS].rlim_cur)
Linus Torvalds's avatar
Linus Torvalds committed
909 910
		return -ENOMEM;

911
	if (accountable && (!(flags & MAP_NORESERVE) ||
912
			    sysctl_overcommit_memory == OVERCOMMIT_NEVER)) {
913 914 915 916
		if (vm_flags & VM_SHARED) {
			/* Check memory availability in shmem_file_setup? */
			vm_flags |= VM_ACCOUNT;
		} else if (vm_flags & VM_WRITE) {
917 918 919
			/*
			 * Private writable mapping: check memory availability
			 */
920
			charged = len >> PAGE_SHIFT;
921
			if (security_vm_enough_memory(charged))
922 923 924
				return -ENOMEM;
			vm_flags |= VM_ACCOUNT;
		}
Andrew Morton's avatar
Andrew Morton committed
925
	}
Linus Torvalds's avatar
Linus Torvalds committed
926

927 928 929 930 931 932
	/*
	 * Can we just expand an old private anonymous mapping?
	 * The VM_SHARED test is necessary because shmem_zero_setup
	 * will create the file object for a shared anonymous map below.
	 */
	if (!file && !(vm_flags & VM_SHARED) &&
933 934
	    vma_merge(mm, prev, addr, addr + len, vm_flags,
					NULL, NULL, pgoff, NULL))
935
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
936

937 938
	/*
	 * Determine the object being mapped and call the appropriate
Linus Torvalds's avatar
Linus Torvalds committed
939 940 941 942
	 * 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
943 944
	if (!vma) {
		error = -ENOMEM;
Andrew Morton's avatar
Andrew Morton committed
945
		goto unacct_error;
Andrew Morton's avatar
Andrew Morton committed
946 947
	}
	memset(vma, 0, sizeof(*vma));
Linus Torvalds's avatar
Linus Torvalds committed
948 949 950 951

	vma->vm_mm = mm;
	vma->vm_start = addr;
	vma->vm_end = addr + len;
Linus Torvalds's avatar
Linus Torvalds committed
952 953
	vma->vm_flags = vm_flags;
	vma->vm_page_prot = protection_map[vm_flags & 0x0f];
Linus Torvalds's avatar
Linus Torvalds committed
954 955 956
	vma->vm_pgoff = pgoff;

	if (file) {
Linus Torvalds's avatar
Linus Torvalds committed
957 958 959
		error = -EINVAL;
		if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
			goto free_vma;
Linus Torvalds's avatar
Linus Torvalds committed
960
		if (vm_flags & VM_DENYWRITE) {
Linus Torvalds's avatar
Linus Torvalds committed
961 962 963 964 965 966 967 968 969 970
			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;
971
	} else if (vm_flags & VM_SHARED) {
Linus Torvalds's avatar
Linus Torvalds committed
972 973 974 975 976
		error = shmem_zero_setup(vma);
		if (error)
			goto free_vma;
	}

977 978 979 980 981 982 983 984
	/* 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
985 986 987 988 989 990
	/* Can addr have changed??
	 *
	 * Answer: Yes, several device drivers can do it in their
	 *         f_op->mmap method. -DaveM
	 */
	addr = vma->vm_start;
991 992
	pgoff = vma->vm_pgoff;
	vm_flags = vma->vm_flags;
Linus Torvalds's avatar
Linus Torvalds committed
993

994
	if (!file || !vma_merge(mm, prev, addr, vma->vm_end,
995
			vma->vm_flags, NULL, file, pgoff, vma_policy(vma))) {
996
		file = vma->vm_file;
997 998 999 1000 1001 1002 1003 1004 1005
		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);
		}
1006
		mpol_free(vma_policy(vma));
1007 1008
		kmem_cache_free(vm_area_cachep, vma);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1009
out:	
Linus Torvalds's avatar
Linus Torvalds committed
1010
	mm->total_vm += len >> PAGE_SHIFT;
1011
	__vm_stat_account(mm, vm_flags, file, len >> PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
1012
	if (vm_flags & VM_LOCKED) {
Linus Torvalds's avatar
Linus Torvalds committed
1013 1014 1015
		mm->locked_vm += len >> PAGE_SHIFT;
		make_pages_present(addr, addr + len);
	}
Andrew Morton's avatar
Andrew Morton committed
1016 1017
	if (flags & MAP_POPULATE) {
		up_write(&mm->mmap_sem);
1018
		sys_remap_file_pages(addr, len, 0,
Andrew Morton's avatar
Andrew Morton committed
1019 1020 1021
					pgoff, flags & MAP_NONBLOCK);
		down_write(&mm->mmap_sem);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1022 1023 1024 1025
	return addr;

unmap_and_free_vma:
	if (correct_wcount)
1026
		atomic_inc(&inode->i_writecount);
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028
	vma->vm_file = NULL;
	fput(file);
Linus Torvalds's avatar
Linus Torvalds committed
1029

Linus Torvalds's avatar
Linus Torvalds committed
1030
	/* Undo any partial mapping done by a device driver. */
1031
	zap_page_range(vma, vma->vm_start, vma->vm_end - vma->vm_start, NULL);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033
free_vma:
	kmem_cache_free(vm_area_cachep, vma);
Andrew Morton's avatar
Andrew Morton committed
1034 1035 1036
unacct_error:
	if (charged)
		vm_unacct_memory(charged);
Linus Torvalds's avatar
Linus Torvalds committed
1037 1038 1039
	return error;
}

1040 1041
EXPORT_SYMBOL(do_mmap_pgoff);

Linus Torvalds's avatar
Linus Torvalds committed
1042
/* Get an address range which is currently unmapped.
Linus Torvalds's avatar
Linus Torvalds committed
1043 1044 1045 1046 1047 1048 1049 1050 1051
 * 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
1052 1053
 */
#ifndef HAVE_ARCH_UNMAPPED_AREA
1054
unsigned long
1055 1056
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
1057
{
1058
	struct mm_struct *mm = current->mm;
Linus Torvalds's avatar
Linus Torvalds committed
1059
	struct vm_area_struct *vma;
1060
	unsigned long start_addr;
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062

	if (len > TASK_SIZE)
Linus Torvalds's avatar
Linus Torvalds committed
1063
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1064 1065 1066

	if (addr) {
		addr = PAGE_ALIGN(addr);
1067
		vma = find_vma(mm, addr);
Linus Torvalds's avatar
Linus Torvalds committed
1068 1069 1070
		if (TASK_SIZE - len >= addr &&
		    (!vma || addr + len <= vma->vm_start))
			return addr;
1071 1072
	}
	start_addr = addr = mm->free_area_cache;
Linus Torvalds's avatar
Linus Torvalds committed
1073

1074
full_search:
1075
	for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
Linus Torvalds's avatar
Linus Torvalds committed
1076
		/* At this point:  (!vma || addr < vma->vm_end). */
1077 1078 1079 1080 1081 1082 1083 1084 1085
		if (TASK_SIZE - len < addr) {
			/*
			 * Start a new search - just in case we missed
			 * some holes.
			 */
			if (start_addr != TASK_UNMAPPED_BASE) {
				start_addr = addr = TASK_UNMAPPED_BASE;
				goto full_search;
			}
Linus Torvalds's avatar
Linus Torvalds committed
1086
			return -ENOMEM;
1087
		}
1088 1089 1090 1091 1092
		if (!vma || addr + len <= vma->vm_start) {
			/*
			 * Remember the place where we stopped the search:
			 */
			mm->free_area_cache = addr + len;
Linus Torvalds's avatar
Linus Torvalds committed
1093
			return addr;
1094
		}
Linus Torvalds's avatar
Linus Torvalds committed
1095
		addr = vma->vm_end;
Linus Torvalds's avatar
Linus Torvalds committed
1096 1097
	}
}
Linus Torvalds's avatar
Linus Torvalds committed
1098 1099
#endif	

1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
void arch_unmap_area(struct vm_area_struct *area)
{
	/*
	 * 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;
}

/*
 * This mmap-allocator allocates new areas top-down from below the
 * stack's low limit (the base):
 */
1114
#ifndef HAVE_ARCH_UNMAPPED_AREA_TOPDOWN
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
unsigned long
arch_get_unmapped_area_topdown(struct file *filp, const unsigned long addr0,
			  const unsigned long len, const unsigned long pgoff,
			  const unsigned long flags)
{
	struct vm_area_struct *vma, *prev_vma;
	struct mm_struct *mm = current->mm;
	unsigned long base = mm->mmap_base, addr = addr0;
	int first_time = 1;

	/* requested length too big for entire address space */
	if (len > TASK_SIZE)
		return -ENOMEM;

	/* dont allow allocations above current base */
	if (mm->free_area_cache > base)
		mm->free_area_cache = base;

	/* requesting a specific address */
	if (addr) {
		addr = PAGE_ALIGN(addr);
		vma = find_vma(mm, addr);
		if (TASK_SIZE - len >= addr &&
				(!vma || addr + len <= vma->vm_start))
			return addr;
	}

try_again:
	/* make sure it can fit in the remaining address space */
	if (mm->free_area_cache < len)
		goto fail;

	/* either no address requested or cant fit in requested address hole */
	addr = (mm->free_area_cache - len) & PAGE_MASK;
	do {
		/*
		 * Lookup failure means no vma is above this address,
		 * i.e. return with success:
		 */
 	 	if (!(vma = find_vma_prev(mm, addr, &prev_vma)))
			return addr;

		/*
		 * new region fits between prev_vma->vm_end and
		 * vma->vm_start, use it:
		 */
		if (addr+len <= vma->vm_start &&
				(!prev_vma || (addr >= prev_vma->vm_end)))
			/* remember the address as a hint for next time */
			return (mm->free_area_cache = addr);
		else
			/* pull free_area_cache down to the first hole */
			if (mm->free_area_cache == vma->vm_end)
				mm->free_area_cache = vma->vm_start;

		/* try just below the current vma->vm_start */
		addr = vma->vm_start-len;
	} while (len <= vma->vm_start);

fail:
	/*
	 * if hint left us with no space for the requested
	 * mapping then try again:
	 */
	if (first_time) {
		mm->free_area_cache = base;
		first_time = 0;
		goto try_again;
	}
	/*
	 * A failed mmap() very likely causes application failure,
	 * so fall back to the bottom-up function here. This scenario
	 * can happen with large stack limits and large mmap()
	 * allocations.
	 */
	mm->free_area_cache = TASK_UNMAPPED_BASE;
	addr = arch_get_unmapped_area(filp, addr0, len, pgoff, flags);
	/*
	 * Restore the topdown base:
	 */
	mm->free_area_cache = base;

	return addr;
}
1199
#endif
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

void arch_unmap_area_topdown(struct vm_area_struct *area)
{
	/*
	 * Is this a new hole at the highest possible address?
	 */
	if (area->vm_end > area->vm_mm->free_area_cache)
		area->vm_mm->free_area_cache = area->vm_end;
}

1210 1211 1212
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
1213 1214
{
	if (flags & MAP_FIXED) {
1215 1216
		unsigned long ret;

Linus Torvalds's avatar
Linus Torvalds committed
1217
		if (addr > TASK_SIZE - len)
1218
			return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220
		if (addr & ~PAGE_MASK)
			return -EINVAL;
1221 1222
		if (file && is_file_hugepages(file))  {
			/*
1223 1224
			 * Check if the given range is hugepage aligned, and
			 * can be made suitable for hugepages.
1225
			 */
1226
			ret = prepare_hugepage_range(addr, len);
1227 1228 1229 1230 1231 1232
		} else {
			/*
			 * Ensure that a normal request is not falling in a
			 * reserved hugepage range.  For some archs like IA-64,
			 * there is a separate region for hugepages.
			 */
1233
			ret = is_hugepage_only_range(addr, len);
1234
		}
1235
		if (ret)
1236
			return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
1237 1238 1239 1240
		return addr;
	}

	if (file && file->f_op && file->f_op->get_unmapped_area)
1241 1242
		return file->f_op->get_unmapped_area(file, addr, len,
						pgoff, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1243

1244
	return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
Linus Torvalds's avatar
Linus Torvalds committed
1245
}
Linus Torvalds's avatar
Linus Torvalds committed
1246

1247 1248
EXPORT_SYMBOL(get_unmapped_area);

Linus Torvalds's avatar
Linus Torvalds committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
/* 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)) {
1259
			struct rb_node * rb_node;
Linus Torvalds's avatar
Linus Torvalds committed
1260 1261 1262 1263 1264 1265 1266

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

			while (rb_node) {
				struct vm_area_struct * vma_tmp;

1267 1268
				vma_tmp = rb_entry(rb_node,
						struct vm_area_struct, vm_rb);
Linus Torvalds's avatar
Linus Torvalds committed
1269 1270 1271 1272

				if (vma_tmp->vm_end > addr) {
					vma = vma_tmp;
					if (vma_tmp->vm_start <= addr)
Linus Torvalds's avatar
Linus Torvalds committed
1273
						break;
Linus Torvalds's avatar
Linus Torvalds committed
1274 1275 1276
					rb_node = rb_node->rb_left;
				} else
					rb_node = rb_node->rb_right;
Linus Torvalds's avatar
Linus Torvalds committed
1277 1278 1279 1280 1281 1282 1283 1284
			}
			if (vma)
				mm->mmap_cache = vma;
		}
	}
	return vma;
}

1285 1286
EXPORT_SYMBOL(find_vma);

Linus Torvalds's avatar
Linus Torvalds committed
1287
/* Same as find_vma, but also return a pointer to the previous VMA in *pprev. */
1288 1289 1290
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
1291
{
Matthew Wilcox's avatar
Matthew Wilcox committed
1292
	struct vm_area_struct *vma = NULL, *prev = NULL;
1293
	struct rb_node * rb_node;
Matthew Wilcox's avatar
Matthew Wilcox committed
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	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
1314 1315
		}
	}
Matthew Wilcox's avatar
Matthew Wilcox committed
1316

1317
out:
Matthew Wilcox's avatar
Matthew Wilcox committed
1318 1319
	*pprev = prev;
	return prev ? prev->vm_next : vma;
Linus Torvalds's avatar
Linus Torvalds committed
1320 1321
}

Matthew Wilcox's avatar
Matthew Wilcox committed
1322
#ifdef CONFIG_STACK_GROWSUP
Andrew Morton's avatar
Andrew Morton committed
1323
/*
1324
 * vma is the first one with address > vma->vm_end.  Have to extend vma.
Andrew Morton's avatar
Andrew Morton committed
1325 1326 1327 1328 1329
 */
int expand_stack(struct vm_area_struct * vma, unsigned long address)
{
	unsigned long grow;

1330 1331 1332
	if (!(vma->vm_flags & VM_GROWSUP))
		return -EFAULT;

1333 1334 1335 1336 1337 1338 1339 1340
	/*
	 * We must make sure the anon_vma is allocated
	 * so that the anon_vma locking is not a noop.
	 */
	if (unlikely(anon_vma_prepare(vma)))
		return -ENOMEM;
	anon_vma_lock(vma);

Andrew Morton's avatar
Andrew Morton committed
1341 1342
	/*
	 * vma->vm_start/vm_end cannot change under us because the caller
1343 1344
	 * is required to hold the mmap_sem in read mode.  We need the
	 * anon_vma lock to serialize against concurrent expand_stacks.
Andrew Morton's avatar
Andrew Morton committed
1345
	 */
1346
	address += 4 + PAGE_SIZE - 1;
Andrew Morton's avatar
Andrew Morton committed
1347
	address &= PAGE_MASK;
1348
	grow = (address - vma->vm_end) >> PAGE_SHIFT;
Andrew Morton's avatar
Andrew Morton committed
1349 1350

	/* Overcommit.. */
1351
	if (security_vm_enough_memory(grow)) {
1352
		anon_vma_unlock(vma);
Andrew Morton's avatar
Andrew Morton committed
1353 1354 1355
		return -ENOMEM;
	}
	
1356
	if (address - vma->vm_start > current->signal->rlim[RLIMIT_STACK].rlim_cur ||
Andrew Morton's avatar
Andrew Morton committed
1357
			((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1358
			current->signal->rlim[RLIMIT_AS].rlim_cur) {
1359
		anon_vma_unlock(vma);
Andrew Morton's avatar
Andrew Morton committed
1360 1361 1362
		vm_unacct_memory(grow);
		return -ENOMEM;
	}
1363
	vma->vm_end = address;
Andrew Morton's avatar
Andrew Morton committed
1364 1365 1366
	vma->vm_mm->total_vm += grow;
	if (vma->vm_flags & VM_LOCKED)
		vma->vm_mm->locked_vm += grow;
1367
	__vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1368
	anon_vma_unlock(vma);
Andrew Morton's avatar
Andrew Morton committed
1369 1370 1371
	return 0;
}

1372 1373
struct vm_area_struct *
find_extend_vma(struct mm_struct *mm, unsigned long addr)
Matthew Wilcox's avatar
Matthew Wilcox committed
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
{
	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
1389 1390 1391
/*
 * vma is the first one with address < vma->vm_start.  Have to extend vma.
 */
1392
int expand_stack(struct vm_area_struct *vma, unsigned long address)
1393 1394 1395
{
	unsigned long grow;

1396 1397 1398 1399 1400 1401 1402 1403
	/*
	 * We must make sure the anon_vma is allocated
	 * so that the anon_vma locking is not a noop.
	 */
	if (unlikely(anon_vma_prepare(vma)))
		return -ENOMEM;
	anon_vma_lock(vma);

1404 1405
	/*
	 * vma->vm_start/vm_end cannot change under us because the caller
1406 1407
	 * is required to hold the mmap_sem in read mode.  We need the
	 * anon_vma lock to serialize against concurrent expand_stacks.
1408 1409 1410 1411 1412
	 */
	address &= PAGE_MASK;
	grow = (vma->vm_start - address) >> PAGE_SHIFT;

	/* Overcommit.. */
1413
	if (security_vm_enough_memory(grow)) {
1414
		anon_vma_unlock(vma);
1415 1416 1417
		return -ENOMEM;
	}
	
1418
	if (vma->vm_end - address > current->signal->rlim[RLIMIT_STACK].rlim_cur ||
1419
			((vma->vm_mm->total_vm + grow) << PAGE_SHIFT) >
1420
			current->signal->rlim[RLIMIT_AS].rlim_cur) {
1421
		anon_vma_unlock(vma);
1422 1423 1424 1425 1426 1427 1428 1429
		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;
1430
	__vm_stat_account(vma->vm_mm, vma->vm_flags, vma->vm_file, grow);
1431
	anon_vma_unlock(vma);
1432 1433 1434
	return 0;
}

1435 1436
struct vm_area_struct *
find_extend_vma(struct mm_struct * mm, unsigned long addr)
Linus Torvalds's avatar
Linus Torvalds committed
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
{
	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;
}
Matthew Wilcox's avatar
Matthew Wilcox committed
1457
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471

/*
 * 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.
 */
1472
static void free_pgtables(struct mmu_gather *tlb, struct vm_area_struct *prev,
Linus Torvalds's avatar
Linus Torvalds committed
1473 1474 1475 1476 1477
	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;
1478
	struct mm_struct *mm = tlb->mm;
Linus Torvalds's avatar
Linus Torvalds committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505

	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:
1506
	if (last < first)	/* for arches with discontiguous pgd indices */
1507
		return;
Linus Torvalds's avatar
Linus Torvalds committed
1508 1509 1510 1511 1512
	/*
	 * 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
1513 1514
	if (start_index < FIRST_USER_PGD_NR)
		start_index = FIRST_USER_PGD_NR;
Linus Torvalds's avatar
Linus Torvalds committed
1515 1516
	end_index = pgd_index(last);
	if (end_index > start_index) {
1517
		clear_page_tables(tlb, start_index, end_index - start_index);
Linus Torvalds's avatar
Linus Torvalds committed
1518 1519 1520 1521
		flush_tlb_pgtables(mm, first & PGDIR_MASK, last & PGDIR_MASK);
	}
}

1522 1523 1524 1525 1526 1527
/* 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
1528
 */
1529
static void unmap_vma(struct mm_struct *mm, struct vm_area_struct *area)
1530 1531 1532 1533 1534 1535
{
	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;
1536
	vm_stat_unaccount(area);
1537
	area->vm_mm->unmap_area(area);
1538
	remove_vm_struct(area);
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563
}

/*
 * 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,
1564
	struct vm_area_struct *vma,
1565 1566
	struct vm_area_struct *prev,
	unsigned long start,
1567
	unsigned long end)
Linus Torvalds's avatar
Linus Torvalds committed
1568
{
1569
	struct mmu_gather *tlb;
1570
	unsigned long nr_accounted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1571

1572
	lru_add_drain();
1573
	tlb = tlb_gather_mmu(mm, 0);
1574
	unmap_vmas(&tlb, mm, vma, start, end, &nr_accounted, NULL);
1575
	vm_unacct_memory(nr_accounted);
1576 1577 1578 1579 1580

	if (is_hugepage_only_range(start, end - start))
		hugetlb_free_pgtables(tlb, prev, start, end);
	else
		free_pgtables(tlb, prev, start, end);
1581 1582
	tlb_finish_mmu(tlb, start, end);
}
Linus Torvalds's avatar
Linus Torvalds committed
1583

1584
/*
1585 1586
 * Create a list of vma's touched by the unmap, removing them from the mm's
 * vma list as we go..
1587
 */
1588 1589 1590
static void
detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma,
	struct vm_area_struct *prev, unsigned long end)
1591
{
1592 1593
	struct vm_area_struct **insertion_point;
	struct vm_area_struct *tail_vma = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
1594

1595
	insertion_point = (prev ? &prev->vm_next : &mm->mmap);
1596
	do {
1597
		rb_erase(&vma->vm_rb, &mm->mm_rb);
1598
		mm->map_count--;
1599 1600 1601 1602 1603 1604
		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. */
1605
}
1606

1607
/*
1608 1609
 * Split a vma into two pieces at address 'addr', a new vma is allocated
 * either for the first part or the the tail.
1610
 */
1611 1612
int split_vma(struct mm_struct * mm, struct vm_area_struct * vma,
	      unsigned long addr, int new_below)
1613
{
1614
	struct mempolicy *pol;
1615
	struct vm_area_struct *new;
Linus Torvalds's avatar
Linus Torvalds committed
1616

1617
	if (mm->map_count >= sysctl_max_map_count)
1618
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1619

1620 1621 1622
	new = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
	if (!new)
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1623

1624
	/* most fields are the same, copy all, and then fixup */
1625 1626
	*new = *vma;

1627
	if (new_below)
1628
		new->vm_end = addr;
1629
	else {
1630 1631 1632
		new->vm_start = addr;
		new->vm_pgoff += ((addr - vma->vm_start) >> PAGE_SHIFT);
	}
Linus Torvalds's avatar
Linus Torvalds committed
1633

1634 1635 1636 1637 1638 1639 1640
	pol = mpol_copy(vma_policy(vma));
	if (IS_ERR(pol)) {
		kmem_cache_free(vm_area_cachep, new);
		return PTR_ERR(pol);
	}
	vma_set_policy(new, pol);

1641 1642 1643 1644 1645
	if (new->vm_file)
		get_file(new->vm_file);

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

Andrew Morton's avatar
Andrew Morton committed
1647 1648 1649 1650 1651
	if (new_below)
		vma_adjust(vma, addr, vma->vm_end, vma->vm_pgoff +
			((addr - new->vm_start) >> PAGE_SHIFT), new);
	else
		vma_adjust(vma, vma->vm_start, addr, vma->vm_pgoff, new);
1652

1653 1654
	return 0;
}
Linus Torvalds's avatar
Linus Torvalds committed
1655

1656 1657 1658
/* 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
1659
 * Jeremy Fitzhardinge <jeremy@goop.org>
1660
 */
1661
int do_munmap(struct mm_struct *mm, unsigned long start, size_t len)
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
{
	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
1678
	if (is_vm_hugetlb_page(mpnt)) {
1679 1680 1681 1682
		int ret = is_aligned_hugepage_range(start, len);

		if (ret)
			return ret;
Andrew Morton's avatar
Andrew Morton committed
1683 1684
	}

1685 1686 1687 1688 1689 1690 1691
	/* if it doesn't overlap, we have nothing.. */
	end = start + len;
	if (mpnt->vm_start >= end)
		return 0;

	/*
	 * If we need to split any vma, do it now to save pain later.
1692 1693 1694 1695
	 *
	 * Note: mremap's move_vma VM_ACCOUNT handling assumes a partially
	 * unmapped vm_area_struct will remain in use: so lower split_vma
	 * places tmp vma above, and higher split_vma places tmp vma below.
1696 1697
	 */
	if (start > mpnt->vm_start) {
1698
		if (split_vma(mm, mpnt, start, 0))
1699 1700
			return -ENOMEM;
		prev = mpnt;
Linus Torvalds's avatar
Linus Torvalds committed
1701 1702
	}

1703 1704 1705
	/* Does it split the last one? */
	last = find_vma(mm, end);
	if (last && end > last->vm_start) {
1706
		if (split_vma(mm, last, end, 1))
1707 1708
			return -ENOMEM;
	}
1709
	mpnt = prev? prev->vm_next: mm->mmap;
Linus Torvalds's avatar
Linus Torvalds committed
1710

1711 1712 1713
	/*
	 * Remove the vma's, and unmap the actual pages
	 */
1714
	detach_vmas_to_be_unmapped(mm, mpnt, prev, end);
1715
	spin_lock(&mm->page_table_lock);
1716
	unmap_region(mm, mpnt, prev, start, end);
1717
	spin_unlock(&mm->page_table_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1718

1719 1720 1721
	/* Fix up all other VM information */
	unmap_vma_list(mm, mpnt);

Linus Torvalds's avatar
Linus Torvalds committed
1722 1723 1724
	return 0;
}

1725 1726
EXPORT_SYMBOL(do_munmap);

Linus Torvalds's avatar
Linus Torvalds committed
1727 1728 1729 1730 1731
asmlinkage long sys_munmap(unsigned long addr, size_t len)
{
	int ret;
	struct mm_struct *mm = current->mm;

1732 1733
	profile_munmap(addr);

Linus Torvalds's avatar
Linus Torvalds committed
1734
	down_write(&mm->mmap_sem);
1735
	ret = do_munmap(mm, addr, len);
Linus Torvalds's avatar
Linus Torvalds committed
1736
	up_write(&mm->mmap_sem);
Linus Torvalds's avatar
Linus Torvalds committed
1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
	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
1748 1749
	struct vm_area_struct * vma, * prev;
	unsigned long flags;
1750
	struct rb_node ** rb_link, * rb_parent;
1751
	pgoff_t pgoff = addr >> PAGE_SHIFT;
Linus Torvalds's avatar
Linus Torvalds committed
1752 1753 1754 1755 1756

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

1757 1758 1759
	if ((addr + len) > TASK_SIZE || (addr + len) < addr)
		return -EINVAL;

Linus Torvalds's avatar
Linus Torvalds committed
1760 1761 1762 1763
	/*
	 * mlock MCL_FUTURE?
	 */
	if (mm->def_flags & VM_LOCKED) {
1764 1765
		unsigned long locked, lock_limit;
		locked = mm->locked_vm << PAGE_SHIFT;
1766
		lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
Linus Torvalds's avatar
Linus Torvalds committed
1767
		locked += len;
1768
		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
Linus Torvalds's avatar
Linus Torvalds committed
1769 1770 1771 1772 1773 1774
			return -EAGAIN;
	}

	/*
	 * Clear old maps.  this also does some error checking for us
	 */
Linus Torvalds's avatar
Linus Torvalds committed
1775 1776 1777
 munmap_back:
	vma = find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
	if (vma && vma->vm_start < addr + len) {
1778
		if (do_munmap(mm, addr, len))
Linus Torvalds's avatar
Linus Torvalds committed
1779 1780 1781
			return -ENOMEM;
		goto munmap_back;
	}
Linus Torvalds's avatar
Linus Torvalds committed
1782 1783 1784

	/* Check against address space limits *after* clearing old maps... */
	if ((mm->total_vm << PAGE_SHIFT) + len
1785
	    > current->signal->rlim[RLIMIT_AS].rlim_cur)
Linus Torvalds's avatar
Linus Torvalds committed
1786 1787
		return -ENOMEM;

1788
	if (mm->map_count > sysctl_max_map_count)
Linus Torvalds's avatar
Linus Torvalds committed
1789 1790
		return -ENOMEM;

1791
	if (security_vm_enough_memory(len >> PAGE_SHIFT))
Linus Torvalds's avatar
Linus Torvalds committed
1792 1793
		return -ENOMEM;

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

1796
	/* Can we just expand an old private anonymous mapping? */
1797 1798
	if (vma_merge(mm, prev, addr, addr + len, flags,
					NULL, NULL, pgoff, NULL))
Linus Torvalds's avatar
Linus Torvalds committed
1799
		goto out;
Linus Torvalds's avatar
Linus Torvalds committed
1800 1801 1802 1803 1804

	/*
	 * create a vma struct for an anonymous mapping
	 */
	vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
Andrew Morton's avatar
Andrew Morton committed
1805 1806
	if (!vma) {
		vm_unacct_memory(len >> PAGE_SHIFT);
Linus Torvalds's avatar
Linus Torvalds committed
1807
		return -ENOMEM;
Andrew Morton's avatar
Andrew Morton committed
1808
	}
Andrew Morton's avatar
Andrew Morton committed
1809
	memset(vma, 0, sizeof(*vma));
Linus Torvalds's avatar
Linus Torvalds committed
1810 1811 1812 1813

	vma->vm_mm = mm;
	vma->vm_start = addr;
	vma->vm_end = addr + len;
1814
	vma->vm_pgoff = pgoff;
Linus Torvalds's avatar
Linus Torvalds committed
1815 1816
	vma->vm_flags = flags;
	vma->vm_page_prot = protection_map[flags & 0x0f];
Linus Torvalds's avatar
Linus Torvalds committed
1817
	vma_link(mm, vma, prev, rb_link, rb_parent);
Linus Torvalds's avatar
Linus Torvalds committed
1818 1819 1820 1821 1822 1823 1824 1825 1826
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;
}

1827 1828
EXPORT_SYMBOL(do_brk);

Linus Torvalds's avatar
Linus Torvalds committed
1829
/* Release all mmaps. */
1830
void exit_mmap(struct mm_struct *mm)
Linus Torvalds's avatar
Linus Torvalds committed
1831
{
1832
	struct mmu_gather *tlb;
1833 1834
	struct vm_area_struct *vma;
	unsigned long nr_accounted = 0;
Linus Torvalds's avatar
Linus Torvalds committed
1835

1836 1837
	lru_add_drain();

Linus Torvalds's avatar
Linus Torvalds committed
1838
	spin_lock(&mm->page_table_lock);
Linus Torvalds's avatar
Linus Torvalds committed
1839

1840
	tlb = tlb_gather_mmu(mm, 1);
Linus Torvalds's avatar
Linus Torvalds committed
1841
	flush_cache_mm(mm);
1842
	/* Use ~0UL here to ensure all VMAs in the mm are unmapped */
1843
	mm->map_count -= unmap_vmas(&tlb, mm, mm->mmap, 0,
1844
					~0UL, &nr_accounted, NULL);
1845 1846
	vm_unacct_memory(nr_accounted);
	BUG_ON(mm->map_count);	/* This is just debugging */
1847
	clear_page_tables(tlb, FIRST_USER_PGD_NR, USER_PTRS_PER_PGD);
1848
	tlb_finish_mmu(tlb, 0, MM_VM_SIZE(mm));
1849

1850
	vma = mm->mmap;
1851 1852 1853 1854 1855 1856
	mm->mmap = mm->mmap_cache = NULL;
	mm->mm_rb = RB_ROOT;
	mm->rss = 0;
	mm->total_vm = 0;
	mm->locked_vm = 0;

1857
	spin_unlock(&mm->page_table_lock);
1858 1859 1860 1861 1862

	/*
	 * Walk the list again, actually closing and freeing it
	 * without holding any MM locks.
	 */
1863 1864
	while (vma) {
		struct vm_area_struct *next = vma->vm_next;
1865
		remove_vm_struct(vma);
1866
		vma = next;
1867
	}
Linus Torvalds's avatar
Linus Torvalds committed
1868 1869 1870
}

/* Insert vm structure into process list sorted by address
1871
 * and into the inode's i_mmap tree.  If vm_file is non-NULL
1872
 * then i_mmap_lock is taken here.
Linus Torvalds's avatar
Linus Torvalds committed
1873
 */
1874
int insert_vm_struct(struct mm_struct * mm, struct vm_area_struct * vma)
Linus Torvalds's avatar
Linus Torvalds committed
1875
{
Linus Torvalds's avatar
Linus Torvalds committed
1876
	struct vm_area_struct * __vma, * prev;
1877
	struct rb_node ** rb_link, * rb_parent;
Linus Torvalds's avatar
Linus Torvalds committed
1878

1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894
	/*
	 * The vm_pgoff of a purely anonymous vma should be irrelevant
	 * until its first write fault, when page's anon_vma and index
	 * are set.  But now set the vm_pgoff it will almost certainly
	 * end up with (unless mremap moves it elsewhere before that
	 * first wfault), so /proc/pid/maps tells a consistent story.
	 *
	 * By setting it to reflect the virtual start address of the
	 * vma, merges and splits can happen in a seamless way, just
	 * using the existing file pgoff checks and manipulations.
	 * Similarly in do_mmap_pgoff and in do_brk.
	 */
	if (!vma->vm_file) {
		BUG_ON(vma->anon_vma);
		vma->vm_pgoff = vma->vm_start >> PAGE_SHIFT;
	}
1895
	__vma = find_vma_prepare(mm,vma->vm_start,&prev,&rb_link,&rb_parent);
Linus Torvalds's avatar
Linus Torvalds committed
1896
	if (__vma && __vma->vm_start < vma->vm_end)
1897
		return -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
1898
	vma_link(mm, vma, prev, rb_link, rb_parent);
1899
	return 0;
1900 1901 1902 1903 1904 1905
}

/*
 * Copy the vma structure to a new location in the same mm,
 * prior to moving page table entries, to effect an mremap move.
 */
Hugh Dickins's avatar
Hugh Dickins committed
1906
struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
1907
	unsigned long addr, unsigned long len, pgoff_t pgoff)
1908
{
Hugh Dickins's avatar
Hugh Dickins committed
1909 1910
	struct vm_area_struct *vma = *vmap;
	unsigned long vma_start = vma->vm_start;
1911 1912 1913
	struct mm_struct *mm = vma->vm_mm;
	struct vm_area_struct *new_vma, *prev;
	struct rb_node **rb_link, *rb_parent;
Andrew Morton's avatar
Andrew Morton committed
1914
	struct mempolicy *pol;
1915

1916 1917 1918 1919 1920 1921 1922
	/*
	 * If anonymous vma has not yet been faulted, update new pgoff
	 * to match new location, to increase its chance of merging.
	 */
	if (!vma->vm_file && !vma->anon_vma)
		pgoff = addr >> PAGE_SHIFT;

1923
	find_vma_prepare(mm, addr, &prev, &rb_link, &rb_parent);
1924 1925
	new_vma = vma_merge(mm, prev, addr, addr + len, vma->vm_flags,
			vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma));
Hugh Dickins's avatar
Hugh Dickins committed
1926 1927 1928 1929 1930 1931 1932 1933
	if (new_vma) {
		/*
		 * Source vma may have been merged into new_vma
		 */
		if (vma_start >= new_vma->vm_start &&
		    vma_start < new_vma->vm_end)
			*vmap = new_vma;
	} else {
1934 1935 1936
		new_vma = kmem_cache_alloc(vm_area_cachep, SLAB_KERNEL);
		if (new_vma) {
			*new_vma = *vma;
Andrew Morton's avatar
Andrew Morton committed
1937 1938 1939 1940 1941 1942
			pol = mpol_copy(vma_policy(vma));
			if (IS_ERR(pol)) {
				kmem_cache_free(vm_area_cachep, new_vma);
				return NULL;
			}
			vma_set_policy(new_vma, pol);
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953
			new_vma->vm_start = addr;
			new_vma->vm_end = addr + len;
			new_vma->vm_pgoff = pgoff;
			if (new_vma->vm_file)
				get_file(new_vma->vm_file);
			if (new_vma->vm_ops && new_vma->vm_ops->open)
				new_vma->vm_ops->open(new_vma);
			vma_link(mm, new_vma, prev, rb_link, rb_parent);
		}
	}
	return new_vma;
Linus Torvalds's avatar
Linus Torvalds committed
1954
}