Blame view

mm/mremap.c 15.6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   *	mm/mremap.c
   *
   *	(C) Copyright 1996 Linus Torvalds
   *
046c68842   Alan Cox   mm: update my add...
6
   *	Address space accounting code	<alan@lxorguk.ukuu.org.uk>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
   *	(C) Copyright 2002 Red Hat Inc, All Rights Reserved
   */
  
  #include <linux/mm.h>
  #include <linux/hugetlb.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
12
  #include <linux/shm.h>
1ff829957   Hugh Dickins   ksm: prevent mrem...
13
  #include <linux/ksm.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
14
15
  #include <linux/mman.h>
  #include <linux/swap.h>
c59ede7b7   Randy.Dunlap   [PATCH] move capa...
16
  #include <linux/capability.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include <linux/fs.h>
6dec97dc9   Cyrill Gorcunov   mm: move_ptes -- ...
18
  #include <linux/swapops.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
  #include <linux/highmem.h>
  #include <linux/security.h>
  #include <linux/syscalls.h>
cddb8a5c1   Andrea Arcangeli   mmu-notifiers: core
22
  #include <linux/mmu_notifier.h>
2581d2023   Paul McQuade   mm/mremap.c: use ...
23
  #include <linux/uaccess.h>
4abad2ca4   Laurent Dufour   mm: new arch_rema...
24
  #include <linux/mm-arch-hooks.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
  #include <asm/cacheflush.h>
  #include <asm/tlbflush.h>
ba470de43   Rik van Riel   mmap: handle mloc...
28
  #include "internal.h"
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
29
  static pmd_t *get_old_pmd(struct mm_struct *mm, unsigned long addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  {
  	pgd_t *pgd;
  	pud_t *pud;
  	pmd_t *pmd;
  
  	pgd = pgd_offset(mm, addr);
  	if (pgd_none_or_clear_bad(pgd))
  		return NULL;
  
  	pud = pud_offset(pgd, addr);
  	if (pud_none_or_clear_bad(pud))
  		return NULL;
  
  	pmd = pmd_offset(pud, addr);
37a1c49a9   Andrea Arcangeli   thp: mremap suppo...
44
  	if (pmd_none(*pmd))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
  		return NULL;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
46
  	return pmd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
47
  }
8ac1f8320   Andrea Arcangeli   thp: pte alloc tr...
48
49
  static pmd_t *alloc_new_pmd(struct mm_struct *mm, struct vm_area_struct *vma,
  			    unsigned long addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
  {
  	pgd_t *pgd;
  	pud_t *pud;
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
53
  	pmd_t *pmd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
55
  
  	pgd = pgd_offset(mm, addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
  	pud = pud_alloc(mm, pgd, addr);
  	if (!pud)
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
58
  		return NULL;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
59

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  	pmd = pmd_alloc(mm, pud, addr);
57a8f0cdb   Hugh Dickins   mm: revert mremap...
61
  	if (!pmd)
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
62
  		return NULL;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
63

8ac1f8320   Andrea Arcangeli   thp: pte alloc tr...
64
  	VM_BUG_ON(pmd_trans_huge(*pmd));
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
65

7be7a5469   Hugh Dickins   [PATCH] mm: move_...
66
  	return pmd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
67
  }
1d069b7dd   Hugh Dickins   huge pagecache: e...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  static void take_rmap_locks(struct vm_area_struct *vma)
  {
  	if (vma->vm_file)
  		i_mmap_lock_write(vma->vm_file->f_mapping);
  	if (vma->anon_vma)
  		anon_vma_lock_write(vma->anon_vma);
  }
  
  static void drop_rmap_locks(struct vm_area_struct *vma)
  {
  	if (vma->anon_vma)
  		anon_vma_unlock_write(vma->anon_vma);
  	if (vma->vm_file)
  		i_mmap_unlock_write(vma->vm_file->f_mapping);
  }
6dec97dc9   Cyrill Gorcunov   mm: move_ptes -- ...
83
84
85
86
87
88
89
90
91
92
93
  static pte_t move_soft_dirty_pte(pte_t pte)
  {
  	/*
  	 * Set soft dirty bit so we can notice
  	 * in userspace the ptes were moved.
  	 */
  #ifdef CONFIG_MEM_SOFT_DIRTY
  	if (pte_present(pte))
  		pte = pte_mksoft_dirty(pte);
  	else if (is_swap_pte(pte))
  		pte = pte_swp_mksoft_dirty(pte);
6dec97dc9   Cyrill Gorcunov   mm: move_ptes -- ...
94
95
96
  #endif
  	return pte;
  }
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
97
98
99
  static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
  		unsigned long old_addr, unsigned long old_end,
  		struct vm_area_struct *new_vma, pmd_t *new_pmd,
5d1904204   Aaron Lu   mremap: fix race ...
100
  		unsigned long new_addr, bool need_rmap_locks, bool *need_flush)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  	struct mm_struct *mm = vma->vm_mm;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
103
  	pte_t *old_pte, *new_pte, pte;
4c21e2f24   Hugh Dickins   [PATCH] mm: split...
104
  	spinlock_t *old_ptl, *new_ptl;
5d1904204   Aaron Lu   mremap: fix race ...
105
106
  	bool force_flush = false;
  	unsigned long len = old_end - old_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
107

38a76013a   Michel Lespinasse   mm: avoid taking ...
108
  	/*
c8c06efa8   Davidlohr Bueso   mm: convert i_mma...
109
  	 * When need_rmap_locks is true, we take the i_mmap_rwsem and anon_vma
38a76013a   Michel Lespinasse   mm: avoid taking ...
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  	 * locks to ensure that rmap will always observe either the old or the
  	 * new ptes. This is the easiest way to avoid races with
  	 * truncate_pagecache(), page migration, etc...
  	 *
  	 * When need_rmap_locks is false, we use other ways to avoid
  	 * such races:
  	 *
  	 * - During exec() shift_arg_pages(), we use a specially tagged vma
  	 *   which rmap call sites look for using is_vma_temporary_stack().
  	 *
  	 * - During mremap(), new_vma is often known to be placed after vma
  	 *   in rmap traversal order. This ensures rmap will always observe
  	 *   either the old pte, or the new pte, or both (the page table locks
  	 *   serialize access to individual ptes, but only rmap traversal
  	 *   order guarantees that we won't miss both the old and new ptes).
  	 */
1d069b7dd   Hugh Dickins   huge pagecache: e...
126
127
  	if (need_rmap_locks)
  		take_rmap_locks(vma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128

4c21e2f24   Hugh Dickins   [PATCH] mm: split...
129
130
131
132
  	/*
  	 * We don't have to worry about the ordering of src and dst
  	 * pte locks because exclusive mmap_sem prevents deadlock.
  	 */
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
133
  	old_pte = pte_offset_map_lock(mm, old_pmd, old_addr, &old_ptl);
ece0e2b64   Peter Zijlstra   mm: remove pte_*m...
134
  	new_pte = pte_offset_map(new_pmd, new_addr);
4c21e2f24   Hugh Dickins   [PATCH] mm: split...
135
136
  	new_ptl = pte_lockptr(mm, new_pmd);
  	if (new_ptl != old_ptl)
f20dc5f7c   Ingo Molnar   [PATCH] lockdep: ...
137
  		spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
6606c3e0d   Zachary Amsden   [PATCH] paravirt:...
138
  	arch_enter_lazy_mmu_mode();
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
139
140
141
142
143
  
  	for (; old_addr < old_end; old_pte++, old_addr += PAGE_SIZE,
  				   new_pte++, new_addr += PAGE_SIZE) {
  		if (pte_none(*old_pte))
  			continue;
5d1904204   Aaron Lu   mremap: fix race ...
144

a2ce2666a   Aaron Lu   mremap: move_ptes...
145
  		pte = ptep_get_and_clear(mm, old_addr, old_pte);
5d1904204   Aaron Lu   mremap: fix race ...
146
  		/*
a2ce2666a   Aaron Lu   mremap: move_ptes...
147
148
  		 * If we are remapping a dirty PTE, make sure
  		 * to flush TLB before we drop the PTL for the
5d1904204   Aaron Lu   mremap: fix race ...
149
  		 * old PTE or we may race with page_mkclean().
a2ce2666a   Aaron Lu   mremap: move_ptes...
150
151
152
153
  		 *
  		 * This check has to be done after we removed the
  		 * old PTE from page tables or another thread may
  		 * dirty it after the check and before the removal.
5d1904204   Aaron Lu   mremap: fix race ...
154
  		 */
a2ce2666a   Aaron Lu   mremap: move_ptes...
155
  		if (pte_present(pte) && pte_dirty(pte))
5d1904204   Aaron Lu   mremap: fix race ...
156
  			force_flush = true;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
157
  		pte = move_pte(pte, new_vma->vm_page_prot, old_addr, new_addr);
6dec97dc9   Cyrill Gorcunov   mm: move_ptes -- ...
158
159
  		pte = move_soft_dirty_pte(pte);
  		set_pte_at(mm, new_addr, new_pte, pte);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
  	}
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
161

6606c3e0d   Zachary Amsden   [PATCH] paravirt:...
162
  	arch_leave_lazy_mmu_mode();
4c21e2f24   Hugh Dickins   [PATCH] mm: split...
163
164
  	if (new_ptl != old_ptl)
  		spin_unlock(new_ptl);
ece0e2b64   Peter Zijlstra   mm: remove pte_*m...
165
  	pte_unmap(new_pte - 1);
5d1904204   Aaron Lu   mremap: fix race ...
166
167
168
169
  	if (force_flush)
  		flush_tlb_range(vma, old_end - len, old_end);
  	else
  		*need_flush = true;
c74df32c7   Hugh Dickins   [PATCH] mm: ptd_a...
170
  	pte_unmap_unlock(old_pte - 1, old_ptl);
1d069b7dd   Hugh Dickins   huge pagecache: e...
171
172
  	if (need_rmap_locks)
  		drop_rmap_locks(vma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  }
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
174
  #define LATENCY_LIMIT	(64 * PAGE_SIZE)
b6a2fea39   Ollie Wild   mm: variable leng...
175
  unsigned long move_page_tables(struct vm_area_struct *vma,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
  		unsigned long old_addr, struct vm_area_struct *new_vma,
38a76013a   Michel Lespinasse   mm: avoid taking ...
177
178
  		unsigned long new_addr, unsigned long len,
  		bool need_rmap_locks)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  {
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
180
181
  	unsigned long extent, next, old_end;
  	pmd_t *old_pmd, *new_pmd;
7b6efc2bc   Andrea Arcangeli   mremap: avoid sen...
182
  	bool need_flush = false;
2ec74c3ef   Sagi Grimberg   mm: move all mmu ...
183
184
  	unsigned long mmun_start;	/* For mmu_notifiers */
  	unsigned long mmun_end;		/* For mmu_notifiers */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185

7be7a5469   Hugh Dickins   [PATCH] mm: move_...
186
187
  	old_end = old_addr + len;
  	flush_cache_range(vma, old_addr, old_end);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188

2ec74c3ef   Sagi Grimberg   mm: move all mmu ...
189
190
191
  	mmun_start = old_addr;
  	mmun_end   = old_end;
  	mmu_notifier_invalidate_range_start(vma->vm_mm, mmun_start, mmun_end);
7b6efc2bc   Andrea Arcangeli   mremap: avoid sen...
192

7be7a5469   Hugh Dickins   [PATCH] mm: move_...
193
  	for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
194
  		cond_resched();
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
195
  		next = (old_addr + PMD_SIZE) & PMD_MASK;
ebed48460   Andrea Arcangeli   mremap: check for...
196
  		/* even if next overflowed, extent below will be ok */
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
197
  		extent = next - old_addr;
ebed48460   Andrea Arcangeli   mremap: check for...
198
199
  		if (extent > old_end - old_addr)
  			extent = old_end - old_addr;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
200
201
202
  		old_pmd = get_old_pmd(vma->vm_mm, old_addr);
  		if (!old_pmd)
  			continue;
8ac1f8320   Andrea Arcangeli   thp: pte alloc tr...
203
  		new_pmd = alloc_new_pmd(vma->vm_mm, vma, new_addr);
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
204
205
  		if (!new_pmd)
  			break;
37a1c49a9   Andrea Arcangeli   thp: mremap suppo...
206
  		if (pmd_trans_huge(*old_pmd)) {
dd18dbc2d   Kirill A. Shutemov   mm, thp: close ra...
207
  			if (extent == HPAGE_PMD_SIZE) {
4b471e889   Kirill A. Shutemov   mm, thp: remove i...
208
  				bool moved;
dd18dbc2d   Kirill A. Shutemov   mm, thp: close ra...
209
210
  				/* See comment in move_ptes() */
  				if (need_rmap_locks)
1d069b7dd   Hugh Dickins   huge pagecache: e...
211
  					take_rmap_locks(vma);
bf8616d5f   Hugh Dickins   huge mm: move_hug...
212
  				moved = move_huge_pmd(vma, old_addr, new_addr,
5d1904204   Aaron Lu   mremap: fix race ...
213
214
  						    old_end, old_pmd, new_pmd,
  						    &need_flush);
dd18dbc2d   Kirill A. Shutemov   mm, thp: close ra...
215
  				if (need_rmap_locks)
1d069b7dd   Hugh Dickins   huge pagecache: e...
216
  					drop_rmap_locks(vma);
5d1904204   Aaron Lu   mremap: fix race ...
217
  				if (moved)
4b471e889   Kirill A. Shutemov   mm, thp: remove i...
218
  					continue;
dd18dbc2d   Kirill A. Shutemov   mm, thp: close ra...
219
  			}
4b471e889   Kirill A. Shutemov   mm, thp: remove i...
220
  			split_huge_pmd(vma, old_pmd, old_addr);
337d9abf1   Naoya Horiguchi   mm: thp: check pm...
221
  			if (pmd_trans_unstable(old_pmd))
6b9116a65   Kirill A. Shutemov   mm, dax: check fo...
222
  				continue;
37a1c49a9   Andrea Arcangeli   thp: mremap suppo...
223
  		}
3ed3a4f0d   Kirill A. Shutemov   mm: cleanup *pte_...
224
  		if (pte_alloc(new_vma->vm_mm, new_pmd, new_addr))
37a1c49a9   Andrea Arcangeli   thp: mremap suppo...
225
  			break;
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
226
227
228
229
230
  		next = (new_addr + PMD_SIZE) & PMD_MASK;
  		if (extent > next - new_addr)
  			extent = next - new_addr;
  		if (extent > LATENCY_LIMIT)
  			extent = LATENCY_LIMIT;
5d1904204   Aaron Lu   mremap: fix race ...
231
232
  		move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
  			  new_pmd, new_addr, need_rmap_locks, &need_flush);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
233
  	}
5d1904204   Aaron Lu   mremap: fix race ...
234
  	if (need_flush)
7b6efc2bc   Andrea Arcangeli   mremap: avoid sen...
235
  		flush_tlb_range(vma, old_end-len, old_addr);
2ec74c3ef   Sagi Grimberg   mm: move all mmu ...
236
  	mmu_notifier_invalidate_range_end(vma->vm_mm, mmun_start, mmun_end);
7be7a5469   Hugh Dickins   [PATCH] mm: move_...
237
238
  
  	return len + old_addr - old_end;	/* how much done */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
240
241
242
  }
  
  static unsigned long move_vma(struct vm_area_struct *vma,
  		unsigned long old_addr, unsigned long old_len,
81909b842   Michel Lespinasse   mm: use mm_popula...
243
  		unsigned long new_len, unsigned long new_addr, bool *locked)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
244
245
246
247
248
249
250
  {
  	struct mm_struct *mm = vma->vm_mm;
  	struct vm_area_struct *new_vma;
  	unsigned long vm_flags = vma->vm_flags;
  	unsigned long new_pgoff;
  	unsigned long moved_len;
  	unsigned long excess = 0;
365e9c87a   Hugh Dickins   [PATCH] mm: updat...
251
  	unsigned long hiwater_vm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
252
  	int split = 0;
7103ad323   Hugh Dickins   ksm: mremap use e...
253
  	int err;
38a76013a   Michel Lespinasse   mm: avoid taking ...
254
  	bool need_rmap_locks;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
255
256
257
258
259
260
261
  
  	/*
  	 * We'd prefer to avoid failure later on in do_munmap:
  	 * which may split one vma into three before unmapping.
  	 */
  	if (mm->map_count >= sysctl_max_map_count - 3)
  		return -ENOMEM;
1ff829957   Hugh Dickins   ksm: prevent mrem...
262
263
264
265
266
267
268
  	/*
  	 * Advise KSM to break any KSM pages in the area to be moved:
  	 * it would be confusing if they were to turn up at the new
  	 * location, where they happen to coincide with different KSM
  	 * pages recently unmapped.  But leave vma->vm_flags as it was,
  	 * so KSM can come around to merge on vma and new_vma afterwards.
  	 */
7103ad323   Hugh Dickins   ksm: mremap use e...
269
270
271
272
  	err = ksm_madvise(vma, old_addr, old_addr + old_len,
  						MADV_UNMERGEABLE, &vm_flags);
  	if (err)
  		return err;
1ff829957   Hugh Dickins   ksm: prevent mrem...
273

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
  	new_pgoff = vma->vm_pgoff + ((old_addr - vma->vm_start) >> PAGE_SHIFT);
38a76013a   Michel Lespinasse   mm: avoid taking ...
275
276
  	new_vma = copy_vma(&vma, new_addr, new_len, new_pgoff,
  			   &need_rmap_locks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
  	if (!new_vma)
  		return -ENOMEM;
38a76013a   Michel Lespinasse   mm: avoid taking ...
279
280
  	moved_len = move_page_tables(vma, old_addr, new_vma, new_addr, old_len,
  				     need_rmap_locks);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  	if (moved_len < old_len) {
df1eab303   Oleg Nesterov   mremap: don't lea...
282
  		err = -ENOMEM;
5477e70a6   Oleg Nesterov   mm: move ->mremap...
283
284
  	} else if (vma->vm_ops && vma->vm_ops->mremap) {
  		err = vma->vm_ops->mremap(new_vma);
df1eab303   Oleg Nesterov   mremap: don't lea...
285
286
287
  	}
  
  	if (unlikely(err)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
288
289
290
291
292
  		/*
  		 * On error, move entries back from new area to old,
  		 * which will succeed since page tables still there,
  		 * and then proceed to unmap new area instead of old.
  		 */
38a76013a   Michel Lespinasse   mm: avoid taking ...
293
294
  		move_page_tables(new_vma, new_addr, vma, old_addr, moved_len,
  				 true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
296
297
  		vma = new_vma;
  		old_len = new_len;
  		old_addr = new_addr;
df1eab303   Oleg Nesterov   mremap: don't lea...
298
  		new_addr = err;
4abad2ca4   Laurent Dufour   mm: new arch_rema...
299
  	} else {
4abad2ca4   Laurent Dufour   mm: new arch_rema...
300
301
  		arch_remap(mm, old_addr, old_addr + old_len,
  			   new_addr, new_addr + new_len);
b2edffdd9   Al Viro   fix mremap() vs. ...
302
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
306
307
308
309
310
311
  
  	/* Conceal VM_ACCOUNT so old reservation is not undone */
  	if (vm_flags & VM_ACCOUNT) {
  		vma->vm_flags &= ~VM_ACCOUNT;
  		excess = vma->vm_end - vma->vm_start - old_len;
  		if (old_addr > vma->vm_start &&
  		    old_addr + old_len < vma->vm_end)
  			split = 1;
  	}
717990629   Kirill Korotaev   [PATCH] mm acct a...
312
  	/*
365e9c87a   Hugh Dickins   [PATCH] mm: updat...
313
314
315
316
317
318
319
  	 * If we failed to move page tables we still do total_vm increment
  	 * since do_munmap() will decrement it by old_len == new_len.
  	 *
  	 * Since total_vm is about to be raised artificially high for a
  	 * moment, we need to restore high watermark afterwards: if stats
  	 * are taken meanwhile, total_vm and hiwater_vm appear too high.
  	 * If this were a serious issue, we'd add a flag to do_munmap().
717990629   Kirill Korotaev   [PATCH] mm acct a...
320
  	 */
365e9c87a   Hugh Dickins   [PATCH] mm: updat...
321
  	hiwater_vm = mm->hiwater_vm;
846383359   Konstantin Khlebnikov   mm: rework virtua...
322
  	vm_stat_account(mm, vma->vm_flags, new_len >> PAGE_SHIFT);
717990629   Kirill Korotaev   [PATCH] mm acct a...
323

d9fe4fab1   Toshi Kani   x86/mm/pat: Add u...
324
325
326
  	/* Tell pfnmap has moved from this vma */
  	if (unlikely(vma->vm_flags & VM_PFNMAP))
  		untrack_pfn_moved(vma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
331
  	if (do_munmap(mm, old_addr, old_len) < 0) {
  		/* OOM: unable to split vma, just get accounts right */
  		vm_unacct_memory(excess >> PAGE_SHIFT);
  		excess = 0;
  	}
365e9c87a   Hugh Dickins   [PATCH] mm: updat...
332
  	mm->hiwater_vm = hiwater_vm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
333
334
335
336
337
338
339
  
  	/* Restore VM_ACCOUNT if one or two pieces of vma left */
  	if (excess) {
  		vma->vm_flags |= VM_ACCOUNT;
  		if (split)
  			vma->vm_next->vm_flags |= VM_ACCOUNT;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
340
341
  	if (vm_flags & VM_LOCKED) {
  		mm->locked_vm += new_len >> PAGE_SHIFT;
81909b842   Michel Lespinasse   mm: use mm_popula...
342
  		*locked = true;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
  	}
  
  	return new_addr;
  }
54f5de709   Al Viro   untangling do_mre...
347
348
349
350
351
  static struct vm_area_struct *vma_to_resize(unsigned long addr,
  	unsigned long old_len, unsigned long new_len, unsigned long *p)
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma = find_vma(mm, addr);
1d3916869   Oleg Nesterov   mremap: don't do ...
352
  	unsigned long pgoff;
54f5de709   Al Viro   untangling do_mre...
353
354
  
  	if (!vma || vma->vm_start > addr)
6cd576130   Derek   mm/mremap.c: clea...
355
  		return ERR_PTR(-EFAULT);
54f5de709   Al Viro   untangling do_mre...
356
357
  
  	if (is_vm_hugetlb_page(vma))
6cd576130   Derek   mm/mremap.c: clea...
358
  		return ERR_PTR(-EINVAL);
54f5de709   Al Viro   untangling do_mre...
359
360
361
  
  	/* We can't remap across vm area boundaries */
  	if (old_len > vma->vm_end - addr)
6cd576130   Derek   mm/mremap.c: clea...
362
  		return ERR_PTR(-EFAULT);
54f5de709   Al Viro   untangling do_mre...
363

1d3916869   Oleg Nesterov   mremap: don't do ...
364
365
  	if (new_len == old_len)
  		return vma;
982134ba6   Linus Torvalds   mm: avoid wrappin...
366
  	/* Need to be careful about a growing mapping */
1d3916869   Oleg Nesterov   mremap: don't do ...
367
368
369
370
371
372
373
  	pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
  	pgoff += vma->vm_pgoff;
  	if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
  		return ERR_PTR(-EINVAL);
  
  	if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
  		return ERR_PTR(-EFAULT);
54f5de709   Al Viro   untangling do_mre...
374
375
376
377
  
  	if (vma->vm_flags & VM_LOCKED) {
  		unsigned long locked, lock_limit;
  		locked = mm->locked_vm << PAGE_SHIFT;
59e99e5b9   Jiri Slaby   mm: use rlimit he...
378
  		lock_limit = rlimit(RLIMIT_MEMLOCK);
54f5de709   Al Viro   untangling do_mre...
379
380
  		locked += new_len - old_len;
  		if (locked > lock_limit && !capable(CAP_IPC_LOCK))
6cd576130   Derek   mm/mremap.c: clea...
381
  			return ERR_PTR(-EAGAIN);
54f5de709   Al Viro   untangling do_mre...
382
  	}
846383359   Konstantin Khlebnikov   mm: rework virtua...
383
384
  	if (!may_expand_vm(mm, vma->vm_flags,
  				(new_len - old_len) >> PAGE_SHIFT))
6cd576130   Derek   mm/mremap.c: clea...
385
  		return ERR_PTR(-ENOMEM);
54f5de709   Al Viro   untangling do_mre...
386
387
388
  
  	if (vma->vm_flags & VM_ACCOUNT) {
  		unsigned long charged = (new_len - old_len) >> PAGE_SHIFT;
191c54244   Al Viro   mm: collapse secu...
389
  		if (security_vm_enough_memory_mm(mm, charged))
6cd576130   Derek   mm/mremap.c: clea...
390
  			return ERR_PTR(-ENOMEM);
54f5de709   Al Viro   untangling do_mre...
391
392
393
394
  		*p = charged;
  	}
  
  	return vma;
54f5de709   Al Viro   untangling do_mre...
395
  }
81909b842   Michel Lespinasse   mm: use mm_popula...
396
397
  static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
  		unsigned long new_addr, unsigned long new_len, bool *locked)
ecc1a8993   Al Viro   do_mremap() untan...
398
399
400
401
402
  {
  	struct mm_struct *mm = current->mm;
  	struct vm_area_struct *vma;
  	unsigned long ret = -EINVAL;
  	unsigned long charged = 0;
097eed103   Al Viro   fix the arch chec...
403
  	unsigned long map_flags;
ecc1a8993   Al Viro   do_mremap() untan...
404

f19cb115a   Alexander Kuleshov   mm/mremap: use of...
405
  	if (offset_in_page(new_addr))
ecc1a8993   Al Viro   do_mremap() untan...
406
407
408
409
  		goto out;
  
  	if (new_len > TASK_SIZE || new_addr > TASK_SIZE - new_len)
  		goto out;
9943242ca   Oleg Nesterov   mremap: simplify ...
410
411
  	/* Ensure the old/new locations do not overlap */
  	if (addr + old_len > new_addr && new_addr + new_len > addr)
ecc1a8993   Al Viro   do_mremap() untan...
412
  		goto out;
ecc1a8993   Al Viro   do_mremap() untan...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  	ret = do_munmap(mm, new_addr, new_len);
  	if (ret)
  		goto out;
  
  	if (old_len >= new_len) {
  		ret = do_munmap(mm, addr+new_len, old_len - new_len);
  		if (ret && old_len != new_len)
  			goto out;
  		old_len = new_len;
  	}
  
  	vma = vma_to_resize(addr, old_len, new_len, &charged);
  	if (IS_ERR(vma)) {
  		ret = PTR_ERR(vma);
  		goto out;
  	}
097eed103   Al Viro   fix the arch chec...
429
430
431
  	map_flags = MAP_FIXED;
  	if (vma->vm_flags & VM_MAYSHARE)
  		map_flags |= MAP_SHARED;
9206de95b   Al Viro   Take arch_mmap_ch...
432

097eed103   Al Viro   fix the arch chec...
433
434
435
  	ret = get_unmapped_area(vma->vm_file, new_addr, new_len, vma->vm_pgoff +
  				((addr - vma->vm_start) >> PAGE_SHIFT),
  				map_flags);
f19cb115a   Alexander Kuleshov   mm/mremap: use of...
436
  	if (offset_in_page(ret))
097eed103   Al Viro   fix the arch chec...
437
  		goto out1;
81909b842   Michel Lespinasse   mm: use mm_popula...
438
  	ret = move_vma(vma, addr, old_len, new_len, new_addr, locked);
f19cb115a   Alexander Kuleshov   mm/mremap: use of...
439
  	if (!(offset_in_page(ret)))
097eed103   Al Viro   fix the arch chec...
440
441
442
  		goto out;
  out1:
  	vm_unacct_memory(charged);
ecc1a8993   Al Viro   do_mremap() untan...
443
444
445
446
  
  out:
  	return ret;
  }
1a0ef85f8   Al Viro   do_mremap() untan...
447
448
  static int vma_expandable(struct vm_area_struct *vma, unsigned long delta)
  {
f106af4e9   Al Viro   fix checks for ex...
449
  	unsigned long end = vma->vm_end + delta;
9206de95b   Al Viro   Take arch_mmap_ch...
450
  	if (end < vma->vm_end) /* overflow */
f106af4e9   Al Viro   fix checks for ex...
451
  		return 0;
9206de95b   Al Viro   Take arch_mmap_ch...
452
  	if (vma->vm_next && vma->vm_next->vm_start < end) /* intersection */
f106af4e9   Al Viro   fix checks for ex...
453
454
455
  		return 0;
  	if (get_unmapped_area(NULL, vma->vm_start, end - vma->vm_start,
  			      0, MAP_FIXED) & ~PAGE_MASK)
1a0ef85f8   Al Viro   do_mremap() untan...
456
  		return 0;
1a0ef85f8   Al Viro   do_mremap() untan...
457
458
  	return 1;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
459
460
461
462
463
464
465
  /*
   * Expand (or shrink) an existing mapping, potentially moving it at the
   * same time (controlled by the MREMAP_MAYMOVE flag and available VM space)
   *
   * MREMAP_FIXED option added 5-Dec-1999 by Benjamin LaHaise
   * This option implies MREMAP_MAYMOVE.
   */
63a81db13   Al Viro   merge do_mremap()...
466
467
468
  SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
  		unsigned long, new_len, unsigned long, flags,
  		unsigned long, new_addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
469
  {
d0de32d9b   Hugh Dickins   [PATCH] mm: do_mr...
470
  	struct mm_struct *mm = current->mm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
473
  	struct vm_area_struct *vma;
  	unsigned long ret = -EINVAL;
  	unsigned long charged = 0;
81909b842   Michel Lespinasse   mm: use mm_popula...
474
  	bool locked = false;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
475
476
  
  	if (flags & ~(MREMAP_FIXED | MREMAP_MAYMOVE))
9a2458a63   Rasmus Villemoes   mm: mremap: valid...
477
478
479
480
  		return ret;
  
  	if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481

f19cb115a   Alexander Kuleshov   mm/mremap: use of...
482
  	if (offset_in_page(addr))
9a2458a63   Rasmus Villemoes   mm: mremap: valid...
483
  		return ret;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
484
485
486
487
488
489
490
491
492
493
  
  	old_len = PAGE_ALIGN(old_len);
  	new_len = PAGE_ALIGN(new_len);
  
  	/*
  	 * We allow a zero old-len as a special case
  	 * for DOS-emu "duplicate shm area" thing. But
  	 * a zero new-len is nonsensical.
  	 */
  	if (!new_len)
9a2458a63   Rasmus Villemoes   mm: mremap: valid...
494
  		return ret;
dc0ef0df7   Michal Hocko   mm: make mmap_sem...
495
496
  	if (down_write_killable(&current->mm->mmap_sem))
  		return -EINTR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
497

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
498
  	if (flags & MREMAP_FIXED) {
9a2458a63   Rasmus Villemoes   mm: mremap: valid...
499
500
  		ret = mremap_to(addr, old_len, new_addr, new_len,
  				&locked);
ecc1a8993   Al Viro   do_mremap() untan...
501
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
502
503
504
505
506
507
508
509
  	}
  
  	/*
  	 * Always allow a shrinking remap: that just unmaps
  	 * the unnecessary pages..
  	 * do_munmap does all the needed commit accounting
  	 */
  	if (old_len >= new_len) {
d0de32d9b   Hugh Dickins   [PATCH] mm: do_mr...
510
  		ret = do_munmap(mm, addr+new_len, old_len - new_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
512
513
  		if (ret && old_len != new_len)
  			goto out;
  		ret = addr;
ecc1a8993   Al Viro   do_mremap() untan...
514
  		goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
515
516
517
  	}
  
  	/*
ecc1a8993   Al Viro   do_mremap() untan...
518
  	 * Ok, we need to grow..
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
  	 */
54f5de709   Al Viro   untangling do_mre...
520
521
522
  	vma = vma_to_resize(addr, old_len, new_len, &charged);
  	if (IS_ERR(vma)) {
  		ret = PTR_ERR(vma);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
523
  		goto out;
119f657c7   Andrew Morton   [PATCH] RLIMIT_AS...
524
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
525

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
526
  	/* old_len exactly to the end of the area..
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
527
  	 */
ecc1a8993   Al Viro   do_mremap() untan...
528
  	if (old_len == vma->vm_end - addr) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
529
  		/* can we just expand the current mapping? */
1a0ef85f8   Al Viro   do_mremap() untan...
530
  		if (vma_expandable(vma, new_len - old_len)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
531
  			int pages = (new_len - old_len) >> PAGE_SHIFT;
5beb49305   Rik van Riel   mm: change anon_v...
532
533
534
535
536
  			if (vma_adjust(vma, vma->vm_start, addr + new_len,
  				       vma->vm_pgoff, NULL)) {
  				ret = -ENOMEM;
  				goto out;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
537

846383359   Konstantin Khlebnikov   mm: rework virtua...
538
  			vm_stat_account(mm, vma->vm_flags, pages);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  			if (vma->vm_flags & VM_LOCKED) {
d0de32d9b   Hugh Dickins   [PATCH] mm: do_mr...
540
  				mm->locked_vm += pages;
81909b842   Michel Lespinasse   mm: use mm_popula...
541
542
  				locked = true;
  				new_addr = addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
544
545
546
547
548
549
550
551
552
553
554
  			}
  			ret = addr;
  			goto out;
  		}
  	}
  
  	/*
  	 * We weren't able to just expand or shrink the area,
  	 * we need to create a new one and move it..
  	 */
  	ret = -ENOMEM;
  	if (flags & MREMAP_MAYMOVE) {
ecc1a8993   Al Viro   do_mremap() untan...
555
556
557
558
559
  		unsigned long map_flags = 0;
  		if (vma->vm_flags & VM_MAYSHARE)
  			map_flags |= MAP_SHARED;
  
  		new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
935874141   Al Viro   fix pgoff in "hav...
560
561
562
  					vma->vm_pgoff +
  					((addr - vma->vm_start) >> PAGE_SHIFT),
  					map_flags);
f19cb115a   Alexander Kuleshov   mm/mremap: use of...
563
  		if (offset_in_page(new_addr)) {
ecc1a8993   Al Viro   do_mremap() untan...
564
565
  			ret = new_addr;
  			goto out;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
566
  		}
ecc1a8993   Al Viro   do_mremap() untan...
567

81909b842   Michel Lespinasse   mm: use mm_popula...
568
  		ret = move_vma(vma, addr, old_len, new_len, new_addr, &locked);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
570
  	}
  out:
f19cb115a   Alexander Kuleshov   mm/mremap: use of...
571
  	if (offset_in_page(ret)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
  		vm_unacct_memory(charged);
d456fb9e5   Oleg Nesterov   mremap: don't do ...
573
574
  		locked = 0;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
575
  	up_write(&current->mm->mmap_sem);
81909b842   Michel Lespinasse   mm: use mm_popula...
576
577
  	if (locked && new_len > old_len)
  		mm_populate(new_addr + old_len, new_len - old_len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
578
579
  	return ret;
  }