]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/memory.c
[PATCH] mm: uml kill unused
[mirror_ubuntu-jammy-kernel.git] / mm / memory.c
CommitLineData
1da177e4
LT
1/*
2 * linux/mm/memory.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
6
7/*
8 * demand-loading started 01.12.91 - seems it is high on the list of
9 * things wanted, and it should be easy to implement. - Linus
10 */
11
12/*
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
15 *
16 * Tested sharing by executing about 30 /bin/sh: under the old kernel it
17 * would have taken more than the 6M I have free, but it worked well as
18 * far as I could see.
19 *
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
21 */
22
23/*
24 * Real VM (paging to/from disk) started 18.12.91. Much more work and
25 * thought has to go into this. Oh, well..
26 * 19.12.91 - works, somewhat. Sometimes I get faults, don't know why.
27 * Found it. Everything seems to work now.
28 * 20.12.91 - Ok, making the swap-device changeable like the root.
29 */
30
31/*
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
34 *
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
37 *
38 * Aug/Sep 2004 Changed to four level page tables (Andi Kleen)
39 */
40
41#include <linux/kernel_stat.h>
42#include <linux/mm.h>
43#include <linux/hugetlb.h>
44#include <linux/mman.h>
45#include <linux/swap.h>
46#include <linux/highmem.h>
47#include <linux/pagemap.h>
48#include <linux/rmap.h>
49#include <linux/module.h>
50#include <linux/init.h>
51
52#include <asm/pgalloc.h>
53#include <asm/uaccess.h>
54#include <asm/tlb.h>
55#include <asm/tlbflush.h>
56#include <asm/pgtable.h>
57
58#include <linux/swapops.h>
59#include <linux/elf.h>
60
d41dee36 61#ifndef CONFIG_NEED_MULTIPLE_NODES
1da177e4
LT
62/* use the per-pgdat data instead for discontigmem - mbligh */
63unsigned long max_mapnr;
64struct page *mem_map;
65
66EXPORT_SYMBOL(max_mapnr);
67EXPORT_SYMBOL(mem_map);
68#endif
69
70unsigned long num_physpages;
71/*
72 * A number of key systems in x86 including ioremap() rely on the assumption
73 * that high_memory defines the upper bound on direct map memory, then end
74 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
75 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
76 * and ZONE_HIGHMEM.
77 */
78void * high_memory;
79unsigned long vmalloc_earlyreserve;
80
81EXPORT_SYMBOL(num_physpages);
82EXPORT_SYMBOL(high_memory);
83EXPORT_SYMBOL(vmalloc_earlyreserve);
84
85/*
86 * If a p?d_bad entry is found while walking page tables, report
87 * the error, before resetting entry to p?d_none. Usually (but
88 * very seldom) called out from the p?d_none_or_clear_bad macros.
89 */
90
91void pgd_clear_bad(pgd_t *pgd)
92{
93 pgd_ERROR(*pgd);
94 pgd_clear(pgd);
95}
96
97void pud_clear_bad(pud_t *pud)
98{
99 pud_ERROR(*pud);
100 pud_clear(pud);
101}
102
103void pmd_clear_bad(pmd_t *pmd)
104{
105 pmd_ERROR(*pmd);
106 pmd_clear(pmd);
107}
108
109/*
110 * Note: this doesn't free the actual pages themselves. That
111 * has been handled earlier when unmapping all the memory regions.
112 */
e0da382c 113static void free_pte_range(struct mmu_gather *tlb, pmd_t *pmd)
1da177e4 114{
e0da382c
HD
115 struct page *page = pmd_page(*pmd);
116 pmd_clear(pmd);
117 pte_free_tlb(tlb, page);
118 dec_page_state(nr_page_table_pages);
119 tlb->mm->nr_ptes--;
1da177e4
LT
120}
121
e0da382c
HD
122static inline void free_pmd_range(struct mmu_gather *tlb, pud_t *pud,
123 unsigned long addr, unsigned long end,
124 unsigned long floor, unsigned long ceiling)
1da177e4
LT
125{
126 pmd_t *pmd;
127 unsigned long next;
e0da382c 128 unsigned long start;
1da177e4 129
e0da382c 130 start = addr;
1da177e4 131 pmd = pmd_offset(pud, addr);
1da177e4
LT
132 do {
133 next = pmd_addr_end(addr, end);
134 if (pmd_none_or_clear_bad(pmd))
135 continue;
e0da382c 136 free_pte_range(tlb, pmd);
1da177e4
LT
137 } while (pmd++, addr = next, addr != end);
138
e0da382c
HD
139 start &= PUD_MASK;
140 if (start < floor)
141 return;
142 if (ceiling) {
143 ceiling &= PUD_MASK;
144 if (!ceiling)
145 return;
1da177e4 146 }
e0da382c
HD
147 if (end - 1 > ceiling - 1)
148 return;
149
150 pmd = pmd_offset(pud, start);
151 pud_clear(pud);
152 pmd_free_tlb(tlb, pmd);
1da177e4
LT
153}
154
e0da382c
HD
155static inline void free_pud_range(struct mmu_gather *tlb, pgd_t *pgd,
156 unsigned long addr, unsigned long end,
157 unsigned long floor, unsigned long ceiling)
1da177e4
LT
158{
159 pud_t *pud;
160 unsigned long next;
e0da382c 161 unsigned long start;
1da177e4 162
e0da382c 163 start = addr;
1da177e4 164 pud = pud_offset(pgd, addr);
1da177e4
LT
165 do {
166 next = pud_addr_end(addr, end);
167 if (pud_none_or_clear_bad(pud))
168 continue;
e0da382c 169 free_pmd_range(tlb, pud, addr, next, floor, ceiling);
1da177e4
LT
170 } while (pud++, addr = next, addr != end);
171
e0da382c
HD
172 start &= PGDIR_MASK;
173 if (start < floor)
174 return;
175 if (ceiling) {
176 ceiling &= PGDIR_MASK;
177 if (!ceiling)
178 return;
1da177e4 179 }
e0da382c
HD
180 if (end - 1 > ceiling - 1)
181 return;
182
183 pud = pud_offset(pgd, start);
184 pgd_clear(pgd);
185 pud_free_tlb(tlb, pud);
1da177e4
LT
186}
187
188/*
e0da382c
HD
189 * This function frees user-level page tables of a process.
190 *
1da177e4
LT
191 * Must be called with pagetable lock held.
192 */
3bf5ee95 193void free_pgd_range(struct mmu_gather **tlb,
e0da382c
HD
194 unsigned long addr, unsigned long end,
195 unsigned long floor, unsigned long ceiling)
1da177e4
LT
196{
197 pgd_t *pgd;
198 unsigned long next;
e0da382c
HD
199 unsigned long start;
200
201 /*
202 * The next few lines have given us lots of grief...
203 *
204 * Why are we testing PMD* at this top level? Because often
205 * there will be no work to do at all, and we'd prefer not to
206 * go all the way down to the bottom just to discover that.
207 *
208 * Why all these "- 1"s? Because 0 represents both the bottom
209 * of the address space and the top of it (using -1 for the
210 * top wouldn't help much: the masks would do the wrong thing).
211 * The rule is that addr 0 and floor 0 refer to the bottom of
212 * the address space, but end 0 and ceiling 0 refer to the top
213 * Comparisons need to use "end - 1" and "ceiling - 1" (though
214 * that end 0 case should be mythical).
215 *
216 * Wherever addr is brought up or ceiling brought down, we must
217 * be careful to reject "the opposite 0" before it confuses the
218 * subsequent tests. But what about where end is brought down
219 * by PMD_SIZE below? no, end can't go down to 0 there.
220 *
221 * Whereas we round start (addr) and ceiling down, by different
222 * masks at different levels, in order to test whether a table
223 * now has no other vmas using it, so can be freed, we don't
224 * bother to round floor or end up - the tests don't need that.
225 */
1da177e4 226
e0da382c
HD
227 addr &= PMD_MASK;
228 if (addr < floor) {
229 addr += PMD_SIZE;
230 if (!addr)
231 return;
232 }
233 if (ceiling) {
234 ceiling &= PMD_MASK;
235 if (!ceiling)
236 return;
237 }
238 if (end - 1 > ceiling - 1)
239 end -= PMD_SIZE;
240 if (addr > end - 1)
241 return;
242
243 start = addr;
3bf5ee95 244 pgd = pgd_offset((*tlb)->mm, addr);
1da177e4
LT
245 do {
246 next = pgd_addr_end(addr, end);
247 if (pgd_none_or_clear_bad(pgd))
248 continue;
3bf5ee95 249 free_pud_range(*tlb, pgd, addr, next, floor, ceiling);
1da177e4 250 } while (pgd++, addr = next, addr != end);
e0da382c 251
4d6ddfa9 252 if (!(*tlb)->fullmm)
3bf5ee95 253 flush_tlb_pgtables((*tlb)->mm, start, end);
e0da382c
HD
254}
255
256void free_pgtables(struct mmu_gather **tlb, struct vm_area_struct *vma,
3bf5ee95 257 unsigned long floor, unsigned long ceiling)
e0da382c
HD
258{
259 while (vma) {
260 struct vm_area_struct *next = vma->vm_next;
261 unsigned long addr = vma->vm_start;
262
8f4f8c16
HD
263 /*
264 * Hide vma from rmap and vmtruncate before freeing pgtables
265 */
266 anon_vma_unlink(vma);
267 unlink_file_vma(vma);
268
3bf5ee95
HD
269 if (is_hugepage_only_range(vma->vm_mm, addr, HPAGE_SIZE)) {
270 hugetlb_free_pgd_range(tlb, addr, vma->vm_end,
e0da382c 271 floor, next? next->vm_start: ceiling);
3bf5ee95
HD
272 } else {
273 /*
274 * Optimization: gather nearby vmas into one call down
275 */
276 while (next && next->vm_start <= vma->vm_end + PMD_SIZE
277 && !is_hugepage_only_range(vma->vm_mm, next->vm_start,
278 HPAGE_SIZE)) {
279 vma = next;
280 next = vma->vm_next;
8f4f8c16
HD
281 anon_vma_unlink(vma);
282 unlink_file_vma(vma);
3bf5ee95
HD
283 }
284 free_pgd_range(tlb, addr, vma->vm_end,
285 floor, next? next->vm_start: ceiling);
286 }
e0da382c
HD
287 vma = next;
288 }
1da177e4
LT
289}
290
1bb3630e 291int __pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1da177e4 292{
c74df32c 293 struct page *new = pte_alloc_one(mm, address);
1bb3630e
HD
294 if (!new)
295 return -ENOMEM;
296
c74df32c 297 spin_lock(&mm->page_table_lock);
1bb3630e
HD
298 if (pmd_present(*pmd)) /* Another has populated it */
299 pte_free(new);
300 else {
1da177e4
LT
301 mm->nr_ptes++;
302 inc_page_state(nr_page_table_pages);
303 pmd_populate(mm, pmd, new);
304 }
c74df32c 305 spin_unlock(&mm->page_table_lock);
1bb3630e 306 return 0;
1da177e4
LT
307}
308
1bb3630e 309int __pte_alloc_kernel(pmd_t *pmd, unsigned long address)
1da177e4 310{
1bb3630e
HD
311 pte_t *new = pte_alloc_one_kernel(&init_mm, address);
312 if (!new)
313 return -ENOMEM;
314
315 spin_lock(&init_mm.page_table_lock);
316 if (pmd_present(*pmd)) /* Another has populated it */
317 pte_free_kernel(new);
318 else
319 pmd_populate_kernel(&init_mm, pmd, new);
320 spin_unlock(&init_mm.page_table_lock);
321 return 0;
1da177e4
LT
322}
323
ae859762
HD
324static inline void add_mm_rss(struct mm_struct *mm, int file_rss, int anon_rss)
325{
326 if (file_rss)
327 add_mm_counter(mm, file_rss, file_rss);
328 if (anon_rss)
329 add_mm_counter(mm, anon_rss, anon_rss);
330}
331
b5810039
NP
332/*
333 * This function is called to print an error when a pte in a
334 * !VM_RESERVED region is found pointing to an invalid pfn (which
335 * is an error.
336 *
337 * The calling function must still handle the error.
338 */
339void print_bad_pte(struct vm_area_struct *vma, pte_t pte, unsigned long vaddr)
340{
341 printk(KERN_ERR "Bad pte = %08llx, process = %s, "
342 "vm_flags = %lx, vaddr = %lx\n",
343 (long long)pte_val(pte),
344 (vma->vm_mm == current->mm ? current->comm : "???"),
345 vma->vm_flags, vaddr);
346 dump_stack();
347}
348
1da177e4
LT
349/*
350 * copy one vm_area from one task to the other. Assumes the page tables
351 * already present in the new task to be cleared in the whole range
352 * covered by this vma.
1da177e4
LT
353 */
354
8c103762 355static inline void
1da177e4 356copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
b5810039 357 pte_t *dst_pte, pte_t *src_pte, struct vm_area_struct *vma,
8c103762 358 unsigned long addr, int *rss)
1da177e4 359{
b5810039 360 unsigned long vm_flags = vma->vm_flags;
1da177e4
LT
361 pte_t pte = *src_pte;
362 struct page *page;
363 unsigned long pfn;
364
365 /* pte contains position in swap or file, so copy. */
366 if (unlikely(!pte_present(pte))) {
367 if (!pte_file(pte)) {
368 swap_duplicate(pte_to_swp_entry(pte));
369 /* make sure dst_mm is on swapoff's mmlist. */
370 if (unlikely(list_empty(&dst_mm->mmlist))) {
371 spin_lock(&mmlist_lock);
372 list_add(&dst_mm->mmlist, &src_mm->mmlist);
373 spin_unlock(&mmlist_lock);
374 }
375 }
ae859762 376 goto out_set_pte;
1da177e4
LT
377 }
378
b5810039
NP
379 /* If the region is VM_RESERVED, the mapping is not
380 * mapped via rmap - duplicate the pte as is.
381 */
382 if (vm_flags & VM_RESERVED)
383 goto out_set_pte;
384
1da177e4 385 pfn = pte_pfn(pte);
b5810039
NP
386 /* If the pte points outside of valid memory but
387 * the region is not VM_RESERVED, we have a problem.
1da177e4 388 */
b5810039
NP
389 if (unlikely(!pfn_valid(pfn))) {
390 print_bad_pte(vma, pte, addr);
391 goto out_set_pte; /* try to do something sane */
392 }
1da177e4 393
b5810039 394 page = pfn_to_page(pfn);
1da177e4
LT
395
396 /*
397 * If it's a COW mapping, write protect it both
398 * in the parent and the child
399 */
400 if ((vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE) {
401 ptep_set_wrprotect(src_mm, addr, src_pte);
402 pte = *src_pte;
403 }
404
405 /*
406 * If it's a shared mapping, mark it clean in
407 * the child
408 */
409 if (vm_flags & VM_SHARED)
410 pte = pte_mkclean(pte);
411 pte = pte_mkold(pte);
412 get_page(page);
1da177e4 413 page_dup_rmap(page);
8c103762 414 rss[!!PageAnon(page)]++;
ae859762
HD
415
416out_set_pte:
417 set_pte_at(dst_mm, addr, dst_pte, pte);
1da177e4
LT
418}
419
420static int copy_pte_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
421 pmd_t *dst_pmd, pmd_t *src_pmd, struct vm_area_struct *vma,
422 unsigned long addr, unsigned long end)
423{
424 pte_t *src_pte, *dst_pte;
c74df32c 425 spinlock_t *src_ptl, *dst_ptl;
e040f218 426 int progress = 0;
8c103762 427 int rss[2];
1da177e4
LT
428
429again:
ae859762 430 rss[1] = rss[0] = 0;
c74df32c 431 dst_pte = pte_alloc_map_lock(dst_mm, dst_pmd, addr, &dst_ptl);
1da177e4
LT
432 if (!dst_pte)
433 return -ENOMEM;
434 src_pte = pte_offset_map_nested(src_pmd, addr);
c74df32c
HD
435 src_ptl = &src_mm->page_table_lock;
436 spin_lock(src_ptl);
1da177e4 437
1da177e4
LT
438 do {
439 /*
440 * We are holding two locks at this point - either of them
441 * could generate latencies in another task on another CPU.
442 */
e040f218
HD
443 if (progress >= 32) {
444 progress = 0;
445 if (need_resched() ||
c74df32c
HD
446 need_lockbreak(src_ptl) ||
447 need_lockbreak(dst_ptl))
e040f218
HD
448 break;
449 }
1da177e4
LT
450 if (pte_none(*src_pte)) {
451 progress++;
452 continue;
453 }
8c103762 454 copy_one_pte(dst_mm, src_mm, dst_pte, src_pte, vma, addr, rss);
1da177e4
LT
455 progress += 8;
456 } while (dst_pte++, src_pte++, addr += PAGE_SIZE, addr != end);
1da177e4 457
c74df32c 458 spin_unlock(src_ptl);
1da177e4 459 pte_unmap_nested(src_pte - 1);
ae859762 460 add_mm_rss(dst_mm, rss[0], rss[1]);
c74df32c
HD
461 pte_unmap_unlock(dst_pte - 1, dst_ptl);
462 cond_resched();
1da177e4
LT
463 if (addr != end)
464 goto again;
465 return 0;
466}
467
468static inline int copy_pmd_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
469 pud_t *dst_pud, pud_t *src_pud, struct vm_area_struct *vma,
470 unsigned long addr, unsigned long end)
471{
472 pmd_t *src_pmd, *dst_pmd;
473 unsigned long next;
474
475 dst_pmd = pmd_alloc(dst_mm, dst_pud, addr);
476 if (!dst_pmd)
477 return -ENOMEM;
478 src_pmd = pmd_offset(src_pud, addr);
479 do {
480 next = pmd_addr_end(addr, end);
481 if (pmd_none_or_clear_bad(src_pmd))
482 continue;
483 if (copy_pte_range(dst_mm, src_mm, dst_pmd, src_pmd,
484 vma, addr, next))
485 return -ENOMEM;
486 } while (dst_pmd++, src_pmd++, addr = next, addr != end);
487 return 0;
488}
489
490static inline int copy_pud_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
491 pgd_t *dst_pgd, pgd_t *src_pgd, struct vm_area_struct *vma,
492 unsigned long addr, unsigned long end)
493{
494 pud_t *src_pud, *dst_pud;
495 unsigned long next;
496
497 dst_pud = pud_alloc(dst_mm, dst_pgd, addr);
498 if (!dst_pud)
499 return -ENOMEM;
500 src_pud = pud_offset(src_pgd, addr);
501 do {
502 next = pud_addr_end(addr, end);
503 if (pud_none_or_clear_bad(src_pud))
504 continue;
505 if (copy_pmd_range(dst_mm, src_mm, dst_pud, src_pud,
506 vma, addr, next))
507 return -ENOMEM;
508 } while (dst_pud++, src_pud++, addr = next, addr != end);
509 return 0;
510}
511
512int copy_page_range(struct mm_struct *dst_mm, struct mm_struct *src_mm,
513 struct vm_area_struct *vma)
514{
515 pgd_t *src_pgd, *dst_pgd;
516 unsigned long next;
517 unsigned long addr = vma->vm_start;
518 unsigned long end = vma->vm_end;
519
d992895b
NP
520 /*
521 * Don't copy ptes where a page fault will fill them correctly.
522 * Fork becomes much lighter when there are big shared or private
523 * readonly mappings. The tradeoff is that copy_page_range is more
524 * efficient than faulting.
525 */
526 if (!(vma->vm_flags & (VM_HUGETLB|VM_NONLINEAR|VM_RESERVED))) {
527 if (!vma->anon_vma)
528 return 0;
529 }
530
1da177e4
LT
531 if (is_vm_hugetlb_page(vma))
532 return copy_hugetlb_page_range(dst_mm, src_mm, vma);
533
534 dst_pgd = pgd_offset(dst_mm, addr);
535 src_pgd = pgd_offset(src_mm, addr);
536 do {
537 next = pgd_addr_end(addr, end);
538 if (pgd_none_or_clear_bad(src_pgd))
539 continue;
540 if (copy_pud_range(dst_mm, src_mm, dst_pgd, src_pgd,
541 vma, addr, next))
542 return -ENOMEM;
543 } while (dst_pgd++, src_pgd++, addr = next, addr != end);
544 return 0;
545}
546
b5810039
NP
547static void zap_pte_range(struct mmu_gather *tlb,
548 struct vm_area_struct *vma, pmd_t *pmd,
1da177e4
LT
549 unsigned long addr, unsigned long end,
550 struct zap_details *details)
551{
b5810039 552 struct mm_struct *mm = tlb->mm;
1da177e4 553 pte_t *pte;
508034a3 554 spinlock_t *ptl;
ae859762
HD
555 int file_rss = 0;
556 int anon_rss = 0;
1da177e4 557
508034a3 558 pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
1da177e4
LT
559 do {
560 pte_t ptent = *pte;
561 if (pte_none(ptent))
562 continue;
563 if (pte_present(ptent)) {
564 struct page *page = NULL;
b5810039
NP
565 if (!(vma->vm_flags & VM_RESERVED)) {
566 unsigned long pfn = pte_pfn(ptent);
567 if (unlikely(!pfn_valid(pfn)))
568 print_bad_pte(vma, ptent, addr);
569 else
570 page = pfn_to_page(pfn);
1da177e4
LT
571 }
572 if (unlikely(details) && page) {
573 /*
574 * unmap_shared_mapping_pages() wants to
575 * invalidate cache without truncating:
576 * unmap shared but keep private pages.
577 */
578 if (details->check_mapping &&
579 details->check_mapping != page->mapping)
580 continue;
581 /*
582 * Each page->index must be checked when
583 * invalidating or truncating nonlinear.
584 */
585 if (details->nonlinear_vma &&
586 (page->index < details->first_index ||
587 page->index > details->last_index))
588 continue;
589 }
b5810039 590 ptent = ptep_get_and_clear_full(mm, addr, pte,
a600388d 591 tlb->fullmm);
1da177e4
LT
592 tlb_remove_tlb_entry(tlb, pte, addr);
593 if (unlikely(!page))
594 continue;
595 if (unlikely(details) && details->nonlinear_vma
596 && linear_page_index(details->nonlinear_vma,
597 addr) != page->index)
b5810039 598 set_pte_at(mm, addr, pte,
1da177e4 599 pgoff_to_pte(page->index));
1da177e4 600 if (PageAnon(page))
86d912f4 601 anon_rss--;
6237bcd9
HD
602 else {
603 if (pte_dirty(ptent))
604 set_page_dirty(page);
605 if (pte_young(ptent))
606 mark_page_accessed(page);
86d912f4 607 file_rss--;
6237bcd9 608 }
1da177e4
LT
609 page_remove_rmap(page);
610 tlb_remove_page(tlb, page);
611 continue;
612 }
613 /*
614 * If details->check_mapping, we leave swap entries;
615 * if details->nonlinear_vma, we leave file entries.
616 */
617 if (unlikely(details))
618 continue;
619 if (!pte_file(ptent))
620 free_swap_and_cache(pte_to_swp_entry(ptent));
b5810039 621 pte_clear_full(mm, addr, pte, tlb->fullmm);
1da177e4 622 } while (pte++, addr += PAGE_SIZE, addr != end);
ae859762 623
86d912f4 624 add_mm_rss(mm, file_rss, anon_rss);
508034a3 625 pte_unmap_unlock(pte - 1, ptl);
1da177e4
LT
626}
627
b5810039
NP
628static inline void zap_pmd_range(struct mmu_gather *tlb,
629 struct vm_area_struct *vma, pud_t *pud,
1da177e4
LT
630 unsigned long addr, unsigned long end,
631 struct zap_details *details)
632{
633 pmd_t *pmd;
634 unsigned long next;
635
636 pmd = pmd_offset(pud, addr);
637 do {
638 next = pmd_addr_end(addr, end);
639 if (pmd_none_or_clear_bad(pmd))
640 continue;
b5810039 641 zap_pte_range(tlb, vma, pmd, addr, next, details);
1da177e4
LT
642 } while (pmd++, addr = next, addr != end);
643}
644
b5810039
NP
645static inline void zap_pud_range(struct mmu_gather *tlb,
646 struct vm_area_struct *vma, pgd_t *pgd,
1da177e4
LT
647 unsigned long addr, unsigned long end,
648 struct zap_details *details)
649{
650 pud_t *pud;
651 unsigned long next;
652
653 pud = pud_offset(pgd, addr);
654 do {
655 next = pud_addr_end(addr, end);
656 if (pud_none_or_clear_bad(pud))
657 continue;
b5810039 658 zap_pmd_range(tlb, vma, pud, addr, next, details);
1da177e4
LT
659 } while (pud++, addr = next, addr != end);
660}
661
662static void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
663 unsigned long addr, unsigned long end,
664 struct zap_details *details)
665{
666 pgd_t *pgd;
667 unsigned long next;
668
669 if (details && !details->check_mapping && !details->nonlinear_vma)
670 details = NULL;
671
672 BUG_ON(addr >= end);
673 tlb_start_vma(tlb, vma);
674 pgd = pgd_offset(vma->vm_mm, addr);
675 do {
676 next = pgd_addr_end(addr, end);
677 if (pgd_none_or_clear_bad(pgd))
678 continue;
b5810039 679 zap_pud_range(tlb, vma, pgd, addr, next, details);
1da177e4
LT
680 } while (pgd++, addr = next, addr != end);
681 tlb_end_vma(tlb, vma);
682}
683
684#ifdef CONFIG_PREEMPT
685# define ZAP_BLOCK_SIZE (8 * PAGE_SIZE)
686#else
687/* No preempt: go for improved straight-line efficiency */
688# define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
689#endif
690
691/**
692 * unmap_vmas - unmap a range of memory covered by a list of vma's
693 * @tlbp: address of the caller's struct mmu_gather
1da177e4
LT
694 * @vma: the starting vma
695 * @start_addr: virtual address at which to start unmapping
696 * @end_addr: virtual address at which to end unmapping
697 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
698 * @details: details of nonlinear truncation or shared cache invalidation
699 *
ee39b37b 700 * Returns the end address of the unmapping (restart addr if interrupted).
1da177e4 701 *
508034a3 702 * Unmap all pages in the vma list.
1da177e4 703 *
508034a3
HD
704 * We aim to not hold locks for too long (for scheduling latency reasons).
705 * So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
1da177e4
LT
706 * return the ending mmu_gather to the caller.
707 *
708 * Only addresses between `start' and `end' will be unmapped.
709 *
710 * The VMA list must be sorted in ascending virtual address order.
711 *
712 * unmap_vmas() assumes that the caller will flush the whole unmapped address
713 * range after unmap_vmas() returns. So the only responsibility here is to
714 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
715 * drops the lock and schedules.
716 */
508034a3 717unsigned long unmap_vmas(struct mmu_gather **tlbp,
1da177e4
LT
718 struct vm_area_struct *vma, unsigned long start_addr,
719 unsigned long end_addr, unsigned long *nr_accounted,
720 struct zap_details *details)
721{
722 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
723 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
724 int tlb_start_valid = 0;
ee39b37b 725 unsigned long start = start_addr;
1da177e4 726 spinlock_t *i_mmap_lock = details? details->i_mmap_lock: NULL;
4d6ddfa9 727 int fullmm = (*tlbp)->fullmm;
1da177e4
LT
728
729 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
1da177e4
LT
730 unsigned long end;
731
732 start = max(vma->vm_start, start_addr);
733 if (start >= vma->vm_end)
734 continue;
735 end = min(vma->vm_end, end_addr);
736 if (end <= vma->vm_start)
737 continue;
738
739 if (vma->vm_flags & VM_ACCOUNT)
740 *nr_accounted += (end - start) >> PAGE_SHIFT;
741
1da177e4
LT
742 while (start != end) {
743 unsigned long block;
744
745 if (!tlb_start_valid) {
746 tlb_start = start;
747 tlb_start_valid = 1;
748 }
749
750 if (is_vm_hugetlb_page(vma)) {
751 block = end - start;
752 unmap_hugepage_range(vma, start, end);
753 } else {
754 block = min(zap_bytes, end - start);
755 unmap_page_range(*tlbp, vma, start,
756 start + block, details);
757 }
758
759 start += block;
760 zap_bytes -= block;
761 if ((long)zap_bytes > 0)
762 continue;
763
764 tlb_finish_mmu(*tlbp, tlb_start, start);
765
766 if (need_resched() ||
1da177e4
LT
767 (i_mmap_lock && need_lockbreak(i_mmap_lock))) {
768 if (i_mmap_lock) {
508034a3 769 *tlbp = NULL;
1da177e4
LT
770 goto out;
771 }
1da177e4 772 cond_resched();
1da177e4
LT
773 }
774
508034a3 775 *tlbp = tlb_gather_mmu(vma->vm_mm, fullmm);
1da177e4
LT
776 tlb_start_valid = 0;
777 zap_bytes = ZAP_BLOCK_SIZE;
778 }
779 }
780out:
ee39b37b 781 return start; /* which is now the end (or restart) address */
1da177e4
LT
782}
783
784/**
785 * zap_page_range - remove user pages in a given range
786 * @vma: vm_area_struct holding the applicable pages
787 * @address: starting address of pages to zap
788 * @size: number of bytes to zap
789 * @details: details of nonlinear truncation or shared cache invalidation
790 */
ee39b37b 791unsigned long zap_page_range(struct vm_area_struct *vma, unsigned long address,
1da177e4
LT
792 unsigned long size, struct zap_details *details)
793{
794 struct mm_struct *mm = vma->vm_mm;
795 struct mmu_gather *tlb;
796 unsigned long end = address + size;
797 unsigned long nr_accounted = 0;
798
1da177e4 799 lru_add_drain();
1da177e4 800 tlb = tlb_gather_mmu(mm, 0);
365e9c87 801 update_hiwater_rss(mm);
508034a3
HD
802 end = unmap_vmas(&tlb, vma, address, end, &nr_accounted, details);
803 if (tlb)
804 tlb_finish_mmu(tlb, address, end);
ee39b37b 805 return end;
1da177e4
LT
806}
807
808/*
809 * Do a quick page-table lookup for a single page.
1da177e4 810 */
deceb6cd
HD
811struct page *follow_page(struct mm_struct *mm, unsigned long address,
812 unsigned int flags)
1da177e4
LT
813{
814 pgd_t *pgd;
815 pud_t *pud;
816 pmd_t *pmd;
817 pte_t *ptep, pte;
deceb6cd 818 spinlock_t *ptl;
1da177e4
LT
819 unsigned long pfn;
820 struct page *page;
821
deceb6cd
HD
822 page = follow_huge_addr(mm, address, flags & FOLL_WRITE);
823 if (!IS_ERR(page)) {
824 BUG_ON(flags & FOLL_GET);
825 goto out;
826 }
1da177e4 827
deceb6cd 828 page = NULL;
1da177e4
LT
829 pgd = pgd_offset(mm, address);
830 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
deceb6cd 831 goto no_page_table;
1da177e4
LT
832
833 pud = pud_offset(pgd, address);
834 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
deceb6cd 835 goto no_page_table;
1da177e4
LT
836
837 pmd = pmd_offset(pud, address);
838 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
deceb6cd
HD
839 goto no_page_table;
840
841 if (pmd_huge(*pmd)) {
842 BUG_ON(flags & FOLL_GET);
843 page = follow_huge_pmd(mm, address, pmd, flags & FOLL_WRITE);
1da177e4 844 goto out;
deceb6cd 845 }
1da177e4 846
deceb6cd 847 ptep = pte_offset_map_lock(mm, pmd, address, &ptl);
1da177e4
LT
848 if (!ptep)
849 goto out;
850
851 pte = *ptep;
deceb6cd
HD
852 if (!pte_present(pte))
853 goto unlock;
854 if ((flags & FOLL_WRITE) && !pte_write(pte))
855 goto unlock;
856 pfn = pte_pfn(pte);
857 if (!pfn_valid(pfn))
858 goto unlock;
1da177e4 859
deceb6cd
HD
860 page = pfn_to_page(pfn);
861 if (flags & FOLL_GET)
862 get_page(page);
863 if (flags & FOLL_TOUCH) {
864 if ((flags & FOLL_WRITE) &&
865 !pte_dirty(pte) && !PageDirty(page))
866 set_page_dirty(page);
867 mark_page_accessed(page);
868 }
869unlock:
870 pte_unmap_unlock(ptep, ptl);
1da177e4 871out:
deceb6cd 872 return page;
1da177e4 873
deceb6cd
HD
874no_page_table:
875 /*
876 * When core dumping an enormous anonymous area that nobody
877 * has touched so far, we don't want to allocate page tables.
878 */
879 if (flags & FOLL_ANON) {
880 page = ZERO_PAGE(address);
881 if (flags & FOLL_GET)
882 get_page(page);
883 BUG_ON(flags & FOLL_WRITE);
884 }
885 return page;
1da177e4
LT
886}
887
1da177e4
LT
888int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
889 unsigned long start, int len, int write, int force,
890 struct page **pages, struct vm_area_struct **vmas)
891{
892 int i;
deceb6cd 893 unsigned int vm_flags;
1da177e4
LT
894
895 /*
896 * Require read or write permissions.
897 * If 'force' is set, we only require the "MAY" flags.
898 */
deceb6cd
HD
899 vm_flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
900 vm_flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
1da177e4
LT
901 i = 0;
902
903 do {
deceb6cd
HD
904 struct vm_area_struct *vma;
905 unsigned int foll_flags;
1da177e4
LT
906
907 vma = find_extend_vma(mm, start);
908 if (!vma && in_gate_area(tsk, start)) {
909 unsigned long pg = start & PAGE_MASK;
910 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
911 pgd_t *pgd;
912 pud_t *pud;
913 pmd_t *pmd;
914 pte_t *pte;
915 if (write) /* user gate pages are read-only */
916 return i ? : -EFAULT;
917 if (pg > TASK_SIZE)
918 pgd = pgd_offset_k(pg);
919 else
920 pgd = pgd_offset_gate(mm, pg);
921 BUG_ON(pgd_none(*pgd));
922 pud = pud_offset(pgd, pg);
923 BUG_ON(pud_none(*pud));
924 pmd = pmd_offset(pud, pg);
690dbe1c
HD
925 if (pmd_none(*pmd))
926 return i ? : -EFAULT;
1da177e4 927 pte = pte_offset_map(pmd, pg);
690dbe1c
HD
928 if (pte_none(*pte)) {
929 pte_unmap(pte);
930 return i ? : -EFAULT;
931 }
1da177e4
LT
932 if (pages) {
933 pages[i] = pte_page(*pte);
934 get_page(pages[i]);
935 }
936 pte_unmap(pte);
937 if (vmas)
938 vmas[i] = gate_vma;
939 i++;
940 start += PAGE_SIZE;
941 len--;
942 continue;
943 }
944
b5810039 945 if (!vma || (vma->vm_flags & (VM_IO | VM_RESERVED))
deceb6cd 946 || !(vm_flags & vma->vm_flags))
1da177e4
LT
947 return i ? : -EFAULT;
948
949 if (is_vm_hugetlb_page(vma)) {
950 i = follow_hugetlb_page(mm, vma, pages, vmas,
951 &start, &len, i);
952 continue;
953 }
deceb6cd
HD
954
955 foll_flags = FOLL_TOUCH;
956 if (pages)
957 foll_flags |= FOLL_GET;
958 if (!write && !(vma->vm_flags & VM_LOCKED) &&
959 (!vma->vm_ops || !vma->vm_ops->nopage))
960 foll_flags |= FOLL_ANON;
961
1da177e4 962 do {
08ef4729 963 struct page *page;
1da177e4 964
deceb6cd
HD
965 if (write)
966 foll_flags |= FOLL_WRITE;
a68d2ebc 967
deceb6cd
HD
968 cond_resched();
969 while (!(page = follow_page(mm, start, foll_flags))) {
970 int ret;
971 ret = __handle_mm_fault(mm, vma, start,
972 foll_flags & FOLL_WRITE);
a68d2ebc
LT
973 /*
974 * The VM_FAULT_WRITE bit tells us that do_wp_page has
975 * broken COW when necessary, even if maybe_mkwrite
976 * decided not to set pte_write. We can thus safely do
977 * subsequent page lookups as if they were reads.
978 */
979 if (ret & VM_FAULT_WRITE)
deceb6cd 980 foll_flags &= ~FOLL_WRITE;
a68d2ebc
LT
981
982 switch (ret & ~VM_FAULT_WRITE) {
1da177e4
LT
983 case VM_FAULT_MINOR:
984 tsk->min_flt++;
985 break;
986 case VM_FAULT_MAJOR:
987 tsk->maj_flt++;
988 break;
989 case VM_FAULT_SIGBUS:
990 return i ? i : -EFAULT;
991 case VM_FAULT_OOM:
992 return i ? i : -ENOMEM;
993 default:
994 BUG();
995 }
1da177e4
LT
996 }
997 if (pages) {
08ef4729
HD
998 pages[i] = page;
999 flush_dcache_page(page);
1da177e4
LT
1000 }
1001 if (vmas)
1002 vmas[i] = vma;
1003 i++;
1004 start += PAGE_SIZE;
1005 len--;
08ef4729 1006 } while (len && start < vma->vm_end);
08ef4729 1007 } while (len);
1da177e4
LT
1008 return i;
1009}
1da177e4
LT
1010EXPORT_SYMBOL(get_user_pages);
1011
1012static int zeromap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1013 unsigned long addr, unsigned long end, pgprot_t prot)
1014{
1015 pte_t *pte;
c74df32c 1016 spinlock_t *ptl;
1da177e4 1017
c74df32c 1018 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1da177e4
LT
1019 if (!pte)
1020 return -ENOMEM;
1021 do {
b5810039
NP
1022 struct page *page = ZERO_PAGE(addr);
1023 pte_t zero_pte = pte_wrprotect(mk_pte(page, prot));
1024 page_cache_get(page);
1025 page_add_file_rmap(page);
1026 inc_mm_counter(mm, file_rss);
1da177e4
LT
1027 BUG_ON(!pte_none(*pte));
1028 set_pte_at(mm, addr, pte, zero_pte);
1029 } while (pte++, addr += PAGE_SIZE, addr != end);
c74df32c 1030 pte_unmap_unlock(pte - 1, ptl);
1da177e4
LT
1031 return 0;
1032}
1033
1034static inline int zeromap_pmd_range(struct mm_struct *mm, pud_t *pud,
1035 unsigned long addr, unsigned long end, pgprot_t prot)
1036{
1037 pmd_t *pmd;
1038 unsigned long next;
1039
1040 pmd = pmd_alloc(mm, pud, addr);
1041 if (!pmd)
1042 return -ENOMEM;
1043 do {
1044 next = pmd_addr_end(addr, end);
1045 if (zeromap_pte_range(mm, pmd, addr, next, prot))
1046 return -ENOMEM;
1047 } while (pmd++, addr = next, addr != end);
1048 return 0;
1049}
1050
1051static inline int zeromap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1052 unsigned long addr, unsigned long end, pgprot_t prot)
1053{
1054 pud_t *pud;
1055 unsigned long next;
1056
1057 pud = pud_alloc(mm, pgd, addr);
1058 if (!pud)
1059 return -ENOMEM;
1060 do {
1061 next = pud_addr_end(addr, end);
1062 if (zeromap_pmd_range(mm, pud, addr, next, prot))
1063 return -ENOMEM;
1064 } while (pud++, addr = next, addr != end);
1065 return 0;
1066}
1067
1068int zeromap_page_range(struct vm_area_struct *vma,
1069 unsigned long addr, unsigned long size, pgprot_t prot)
1070{
1071 pgd_t *pgd;
1072 unsigned long next;
1073 unsigned long end = addr + size;
1074 struct mm_struct *mm = vma->vm_mm;
1075 int err;
1076
1077 BUG_ON(addr >= end);
1078 pgd = pgd_offset(mm, addr);
1079 flush_cache_range(vma, addr, end);
1da177e4
LT
1080 do {
1081 next = pgd_addr_end(addr, end);
1082 err = zeromap_pud_range(mm, pgd, addr, next, prot);
1083 if (err)
1084 break;
1085 } while (pgd++, addr = next, addr != end);
1da177e4
LT
1086 return err;
1087}
1088
1089/*
1090 * maps a range of physical memory into the requested pages. the old
1091 * mappings are removed. any references to nonexistent pages results
1092 * in null mappings (currently treated as "copy-on-access")
1093 */
1094static int remap_pte_range(struct mm_struct *mm, pmd_t *pmd,
1095 unsigned long addr, unsigned long end,
1096 unsigned long pfn, pgprot_t prot)
1097{
1098 pte_t *pte;
c74df32c 1099 spinlock_t *ptl;
1da177e4 1100
c74df32c 1101 pte = pte_alloc_map_lock(mm, pmd, addr, &ptl);
1da177e4
LT
1102 if (!pte)
1103 return -ENOMEM;
1104 do {
1105 BUG_ON(!pte_none(*pte));
b5810039 1106 set_pte_at(mm, addr, pte, pfn_pte(pfn, prot));
1da177e4
LT
1107 pfn++;
1108 } while (pte++, addr += PAGE_SIZE, addr != end);
c74df32c 1109 pte_unmap_unlock(pte - 1, ptl);
1da177e4
LT
1110 return 0;
1111}
1112
1113static inline int remap_pmd_range(struct mm_struct *mm, pud_t *pud,
1114 unsigned long addr, unsigned long end,
1115 unsigned long pfn, pgprot_t prot)
1116{
1117 pmd_t *pmd;
1118 unsigned long next;
1119
1120 pfn -= addr >> PAGE_SHIFT;
1121 pmd = pmd_alloc(mm, pud, addr);
1122 if (!pmd)
1123 return -ENOMEM;
1124 do {
1125 next = pmd_addr_end(addr, end);
1126 if (remap_pte_range(mm, pmd, addr, next,
1127 pfn + (addr >> PAGE_SHIFT), prot))
1128 return -ENOMEM;
1129 } while (pmd++, addr = next, addr != end);
1130 return 0;
1131}
1132
1133static inline int remap_pud_range(struct mm_struct *mm, pgd_t *pgd,
1134 unsigned long addr, unsigned long end,
1135 unsigned long pfn, pgprot_t prot)
1136{
1137 pud_t *pud;
1138 unsigned long next;
1139
1140 pfn -= addr >> PAGE_SHIFT;
1141 pud = pud_alloc(mm, pgd, addr);
1142 if (!pud)
1143 return -ENOMEM;
1144 do {
1145 next = pud_addr_end(addr, end);
1146 if (remap_pmd_range(mm, pud, addr, next,
1147 pfn + (addr >> PAGE_SHIFT), prot))
1148 return -ENOMEM;
1149 } while (pud++, addr = next, addr != end);
1150 return 0;
1151}
1152
1153/* Note: this is only safe if the mm semaphore is held when called. */
1154int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr,
1155 unsigned long pfn, unsigned long size, pgprot_t prot)
1156{
1157 pgd_t *pgd;
1158 unsigned long next;
2d15cab8 1159 unsigned long end = addr + PAGE_ALIGN(size);
1da177e4
LT
1160 struct mm_struct *mm = vma->vm_mm;
1161 int err;
1162
1163 /*
1164 * Physically remapped pages are special. Tell the
1165 * rest of the world about it:
1166 * VM_IO tells people not to look at these pages
1167 * (accesses can have side effects).
b5810039
NP
1168 * VM_RESERVED tells the core MM not to "manage" these pages
1169 * (e.g. refcount, mapcount, try to swap them out).
1da177e4
LT
1170 */
1171 vma->vm_flags |= VM_IO | VM_RESERVED;
1172
1173 BUG_ON(addr >= end);
1174 pfn -= addr >> PAGE_SHIFT;
1175 pgd = pgd_offset(mm, addr);
1176 flush_cache_range(vma, addr, end);
1da177e4
LT
1177 do {
1178 next = pgd_addr_end(addr, end);
1179 err = remap_pud_range(mm, pgd, addr, next,
1180 pfn + (addr >> PAGE_SHIFT), prot);
1181 if (err)
1182 break;
1183 } while (pgd++, addr = next, addr != end);
1da177e4
LT
1184 return err;
1185}
1186EXPORT_SYMBOL(remap_pfn_range);
1187
8f4e2101
HD
1188/*
1189 * handle_pte_fault chooses page fault handler according to an entry
1190 * which was read non-atomically. Before making any commitment, on
1191 * those architectures or configurations (e.g. i386 with PAE) which
1192 * might give a mix of unmatched parts, do_swap_page and do_file_page
1193 * must check under lock before unmapping the pte and proceeding
1194 * (but do_wp_page is only called after already making such a check;
1195 * and do_anonymous_page and do_no_page can safely check later on).
1196 */
1197static inline int pte_unmap_same(struct mm_struct *mm,
1198 pte_t *page_table, pte_t orig_pte)
1199{
1200 int same = 1;
1201#if defined(CONFIG_SMP) || defined(CONFIG_PREEMPT)
1202 if (sizeof(pte_t) > sizeof(unsigned long)) {
1203 spin_lock(&mm->page_table_lock);
1204 same = pte_same(*page_table, orig_pte);
1205 spin_unlock(&mm->page_table_lock);
1206 }
1207#endif
1208 pte_unmap(page_table);
1209 return same;
1210}
1211
1da177e4
LT
1212/*
1213 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1214 * servicing faults for write access. In the normal case, do always want
1215 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1216 * that do not have writing enabled, when used by access_process_vm.
1217 */
1218static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1219{
1220 if (likely(vma->vm_flags & VM_WRITE))
1221 pte = pte_mkwrite(pte);
1222 return pte;
1223}
1224
1da177e4
LT
1225/*
1226 * This routine handles present pages, when users try to write
1227 * to a shared page. It is done by copying the page to a new address
1228 * and decrementing the shared-page counter for the old page.
1229 *
1da177e4
LT
1230 * Note that this routine assumes that the protection checks have been
1231 * done by the caller (the low-level page fault routine in most cases).
1232 * Thus we can safely just mark it writable once we've done any necessary
1233 * COW.
1234 *
1235 * We also mark the page dirty at this point even though the page will
1236 * change only once the write actually happens. This avoids a few races,
1237 * and potentially makes it more efficient.
1238 *
8f4e2101
HD
1239 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1240 * but allow concurrent faults), with pte both mapped and locked.
1241 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 1242 */
65500d23
HD
1243static int do_wp_page(struct mm_struct *mm, struct vm_area_struct *vma,
1244 unsigned long address, pte_t *page_table, pmd_t *pmd,
8f4e2101 1245 spinlock_t *ptl, pte_t orig_pte)
1da177e4
LT
1246{
1247 struct page *old_page, *new_page;
65500d23 1248 unsigned long pfn = pte_pfn(orig_pte);
1da177e4 1249 pte_t entry;
65500d23 1250 int ret = VM_FAULT_MINOR;
1da177e4 1251
b5810039
NP
1252 BUG_ON(vma->vm_flags & VM_RESERVED);
1253
1da177e4
LT
1254 if (unlikely(!pfn_valid(pfn))) {
1255 /*
65500d23 1256 * Page table corrupted: show pte and kill process.
1da177e4 1257 */
b5810039 1258 print_bad_pte(vma, orig_pte, address);
65500d23
HD
1259 ret = VM_FAULT_OOM;
1260 goto unlock;
1da177e4
LT
1261 }
1262 old_page = pfn_to_page(pfn);
1263
d296e9cd 1264 if (PageAnon(old_page) && !TestSetPageLocked(old_page)) {
1da177e4
LT
1265 int reuse = can_share_swap_page(old_page);
1266 unlock_page(old_page);
1267 if (reuse) {
1268 flush_cache_page(vma, address, pfn);
65500d23
HD
1269 entry = pte_mkyoung(orig_pte);
1270 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1da177e4
LT
1271 ptep_set_access_flags(vma, address, page_table, entry, 1);
1272 update_mmu_cache(vma, address, entry);
1273 lazy_mmu_prot_update(entry);
65500d23
HD
1274 ret |= VM_FAULT_WRITE;
1275 goto unlock;
1da177e4
LT
1276 }
1277 }
1da177e4
LT
1278
1279 /*
1280 * Ok, we need to copy. Oh, well..
1281 */
b5810039 1282 page_cache_get(old_page);
8f4e2101 1283 pte_unmap_unlock(page_table, ptl);
1da177e4
LT
1284
1285 if (unlikely(anon_vma_prepare(vma)))
65500d23 1286 goto oom;
1da177e4
LT
1287 if (old_page == ZERO_PAGE(address)) {
1288 new_page = alloc_zeroed_user_highpage(vma, address);
1289 if (!new_page)
65500d23 1290 goto oom;
1da177e4
LT
1291 } else {
1292 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1293 if (!new_page)
65500d23 1294 goto oom;
1da177e4
LT
1295 copy_user_highpage(new_page, old_page, address);
1296 }
65500d23 1297
1da177e4
LT
1298 /*
1299 * Re-check the pte - we dropped the lock
1300 */
8f4e2101 1301 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
65500d23 1302 if (likely(pte_same(*page_table, orig_pte))) {
b5810039
NP
1303 page_remove_rmap(old_page);
1304 if (!PageAnon(old_page)) {
4294621f 1305 inc_mm_counter(mm, anon_rss);
b5810039 1306 dec_mm_counter(mm, file_rss);
4294621f 1307 }
1da177e4 1308 flush_cache_page(vma, address, pfn);
65500d23
HD
1309 entry = mk_pte(new_page, vma->vm_page_prot);
1310 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1311 ptep_establish(vma, address, page_table, entry);
1312 update_mmu_cache(vma, address, entry);
1313 lazy_mmu_prot_update(entry);
1da177e4
LT
1314 lru_cache_add_active(new_page);
1315 page_add_anon_rmap(new_page, vma, address);
1316
1317 /* Free the old page.. */
1318 new_page = old_page;
f33ea7f4 1319 ret |= VM_FAULT_WRITE;
1da177e4 1320 }
1da177e4
LT
1321 page_cache_release(new_page);
1322 page_cache_release(old_page);
65500d23 1323unlock:
8f4e2101 1324 pte_unmap_unlock(page_table, ptl);
f33ea7f4 1325 return ret;
65500d23 1326oom:
1da177e4
LT
1327 page_cache_release(old_page);
1328 return VM_FAULT_OOM;
1329}
1330
1331/*
1332 * Helper functions for unmap_mapping_range().
1333 *
1334 * __ Notes on dropping i_mmap_lock to reduce latency while unmapping __
1335 *
1336 * We have to restart searching the prio_tree whenever we drop the lock,
1337 * since the iterator is only valid while the lock is held, and anyway
1338 * a later vma might be split and reinserted earlier while lock dropped.
1339 *
1340 * The list of nonlinear vmas could be handled more efficiently, using
1341 * a placeholder, but handle it in the same way until a need is shown.
1342 * It is important to search the prio_tree before nonlinear list: a vma
1343 * may become nonlinear and be shifted from prio_tree to nonlinear list
1344 * while the lock is dropped; but never shifted from list to prio_tree.
1345 *
1346 * In order to make forward progress despite restarting the search,
1347 * vm_truncate_count is used to mark a vma as now dealt with, so we can
1348 * quickly skip it next time around. Since the prio_tree search only
1349 * shows us those vmas affected by unmapping the range in question, we
1350 * can't efficiently keep all vmas in step with mapping->truncate_count:
1351 * so instead reset them all whenever it wraps back to 0 (then go to 1).
1352 * mapping->truncate_count and vma->vm_truncate_count are protected by
1353 * i_mmap_lock.
1354 *
1355 * In order to make forward progress despite repeatedly restarting some
ee39b37b 1356 * large vma, note the restart_addr from unmap_vmas when it breaks out:
1da177e4
LT
1357 * and restart from that address when we reach that vma again. It might
1358 * have been split or merged, shrunk or extended, but never shifted: so
1359 * restart_addr remains valid so long as it remains in the vma's range.
1360 * unmap_mapping_range forces truncate_count to leap over page-aligned
1361 * values so we can save vma's restart_addr in its truncate_count field.
1362 */
1363#define is_restart_addr(truncate_count) (!((truncate_count) & ~PAGE_MASK))
1364
1365static void reset_vma_truncate_counts(struct address_space *mapping)
1366{
1367 struct vm_area_struct *vma;
1368 struct prio_tree_iter iter;
1369
1370 vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, 0, ULONG_MAX)
1371 vma->vm_truncate_count = 0;
1372 list_for_each_entry(vma, &mapping->i_mmap_nonlinear, shared.vm_set.list)
1373 vma->vm_truncate_count = 0;
1374}
1375
1376static int unmap_mapping_range_vma(struct vm_area_struct *vma,
1377 unsigned long start_addr, unsigned long end_addr,
1378 struct zap_details *details)
1379{
1380 unsigned long restart_addr;
1381 int need_break;
1382
1383again:
1384 restart_addr = vma->vm_truncate_count;
1385 if (is_restart_addr(restart_addr) && start_addr < restart_addr) {
1386 start_addr = restart_addr;
1387 if (start_addr >= end_addr) {
1388 /* Top of vma has been split off since last time */
1389 vma->vm_truncate_count = details->truncate_count;
1390 return 0;
1391 }
1392 }
1393
ee39b37b
HD
1394 restart_addr = zap_page_range(vma, start_addr,
1395 end_addr - start_addr, details);
1da177e4
LT
1396 need_break = need_resched() ||
1397 need_lockbreak(details->i_mmap_lock);
1398
ee39b37b 1399 if (restart_addr >= end_addr) {
1da177e4
LT
1400 /* We have now completed this vma: mark it so */
1401 vma->vm_truncate_count = details->truncate_count;
1402 if (!need_break)
1403 return 0;
1404 } else {
1405 /* Note restart_addr in vma's truncate_count field */
ee39b37b 1406 vma->vm_truncate_count = restart_addr;
1da177e4
LT
1407 if (!need_break)
1408 goto again;
1409 }
1410
1411 spin_unlock(details->i_mmap_lock);
1412 cond_resched();
1413 spin_lock(details->i_mmap_lock);
1414 return -EINTR;
1415}
1416
1417static inline void unmap_mapping_range_tree(struct prio_tree_root *root,
1418 struct zap_details *details)
1419{
1420 struct vm_area_struct *vma;
1421 struct prio_tree_iter iter;
1422 pgoff_t vba, vea, zba, zea;
1423
1424restart:
1425 vma_prio_tree_foreach(vma, &iter, root,
1426 details->first_index, details->last_index) {
1427 /* Skip quickly over those we have already dealt with */
1428 if (vma->vm_truncate_count == details->truncate_count)
1429 continue;
1430
1431 vba = vma->vm_pgoff;
1432 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1433 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1434 zba = details->first_index;
1435 if (zba < vba)
1436 zba = vba;
1437 zea = details->last_index;
1438 if (zea > vea)
1439 zea = vea;
1440
1441 if (unmap_mapping_range_vma(vma,
1442 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1443 ((zea - vba + 1) << PAGE_SHIFT) + vma->vm_start,
1444 details) < 0)
1445 goto restart;
1446 }
1447}
1448
1449static inline void unmap_mapping_range_list(struct list_head *head,
1450 struct zap_details *details)
1451{
1452 struct vm_area_struct *vma;
1453
1454 /*
1455 * In nonlinear VMAs there is no correspondence between virtual address
1456 * offset and file offset. So we must perform an exhaustive search
1457 * across *all* the pages in each nonlinear VMA, not just the pages
1458 * whose virtual address lies outside the file truncation point.
1459 */
1460restart:
1461 list_for_each_entry(vma, head, shared.vm_set.list) {
1462 /* Skip quickly over those we have already dealt with */
1463 if (vma->vm_truncate_count == details->truncate_count)
1464 continue;
1465 details->nonlinear_vma = vma;
1466 if (unmap_mapping_range_vma(vma, vma->vm_start,
1467 vma->vm_end, details) < 0)
1468 goto restart;
1469 }
1470}
1471
1472/**
1473 * unmap_mapping_range - unmap the portion of all mmaps
1474 * in the specified address_space corresponding to the specified
1475 * page range in the underlying file.
3d41088f 1476 * @mapping: the address space containing mmaps to be unmapped.
1da177e4
LT
1477 * @holebegin: byte in first page to unmap, relative to the start of
1478 * the underlying file. This will be rounded down to a PAGE_SIZE
1479 * boundary. Note that this is different from vmtruncate(), which
1480 * must keep the partial page. In contrast, we must get rid of
1481 * partial pages.
1482 * @holelen: size of prospective hole in bytes. This will be rounded
1483 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1484 * end of the file.
1485 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1486 * but 0 when invalidating pagecache, don't throw away private data.
1487 */
1488void unmap_mapping_range(struct address_space *mapping,
1489 loff_t const holebegin, loff_t const holelen, int even_cows)
1490{
1491 struct zap_details details;
1492 pgoff_t hba = holebegin >> PAGE_SHIFT;
1493 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1494
1495 /* Check for overflow. */
1496 if (sizeof(holelen) > sizeof(hlen)) {
1497 long long holeend =
1498 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1499 if (holeend & ~(long long)ULONG_MAX)
1500 hlen = ULONG_MAX - hba + 1;
1501 }
1502
1503 details.check_mapping = even_cows? NULL: mapping;
1504 details.nonlinear_vma = NULL;
1505 details.first_index = hba;
1506 details.last_index = hba + hlen - 1;
1507 if (details.last_index < details.first_index)
1508 details.last_index = ULONG_MAX;
1509 details.i_mmap_lock = &mapping->i_mmap_lock;
1510
1511 spin_lock(&mapping->i_mmap_lock);
1512
1513 /* serialize i_size write against truncate_count write */
1514 smp_wmb();
1515 /* Protect against page faults, and endless unmapping loops */
1516 mapping->truncate_count++;
1517 /*
1518 * For archs where spin_lock has inclusive semantics like ia64
1519 * this smp_mb() will prevent to read pagetable contents
1520 * before the truncate_count increment is visible to
1521 * other cpus.
1522 */
1523 smp_mb();
1524 if (unlikely(is_restart_addr(mapping->truncate_count))) {
1525 if (mapping->truncate_count == 0)
1526 reset_vma_truncate_counts(mapping);
1527 mapping->truncate_count++;
1528 }
1529 details.truncate_count = mapping->truncate_count;
1530
1531 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1532 unmap_mapping_range_tree(&mapping->i_mmap, &details);
1533 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear)))
1534 unmap_mapping_range_list(&mapping->i_mmap_nonlinear, &details);
1535 spin_unlock(&mapping->i_mmap_lock);
1536}
1537EXPORT_SYMBOL(unmap_mapping_range);
1538
1539/*
1540 * Handle all mappings that got truncated by a "truncate()"
1541 * system call.
1542 *
1543 * NOTE! We have to be ready to update the memory sharing
1544 * between the file and the memory map for a potential last
1545 * incomplete page. Ugly, but necessary.
1546 */
1547int vmtruncate(struct inode * inode, loff_t offset)
1548{
1549 struct address_space *mapping = inode->i_mapping;
1550 unsigned long limit;
1551
1552 if (inode->i_size < offset)
1553 goto do_expand;
1554 /*
1555 * truncation of in-use swapfiles is disallowed - it would cause
1556 * subsequent swapout to scribble on the now-freed blocks.
1557 */
1558 if (IS_SWAPFILE(inode))
1559 goto out_busy;
1560 i_size_write(inode, offset);
1561 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1562 truncate_inode_pages(mapping, offset);
1563 goto out_truncate;
1564
1565do_expand:
1566 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1567 if (limit != RLIM_INFINITY && offset > limit)
1568 goto out_sig;
1569 if (offset > inode->i_sb->s_maxbytes)
1570 goto out_big;
1571 i_size_write(inode, offset);
1572
1573out_truncate:
1574 if (inode->i_op && inode->i_op->truncate)
1575 inode->i_op->truncate(inode);
1576 return 0;
1577out_sig:
1578 send_sig(SIGXFSZ, current, 0);
1579out_big:
1580 return -EFBIG;
1581out_busy:
1582 return -ETXTBSY;
1583}
1584
1585EXPORT_SYMBOL(vmtruncate);
1586
1587/*
1588 * Primitive swap readahead code. We simply read an aligned block of
1589 * (1 << page_cluster) entries in the swap area. This method is chosen
1590 * because it doesn't cost us any seek time. We also make sure to queue
1591 * the 'original' request together with the readahead ones...
1592 *
1593 * This has been extended to use the NUMA policies from the mm triggering
1594 * the readahead.
1595 *
1596 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1597 */
1598void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1599{
1600#ifdef CONFIG_NUMA
1601 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1602#endif
1603 int i, num;
1604 struct page *new_page;
1605 unsigned long offset;
1606
1607 /*
1608 * Get the number of handles we should do readahead io to.
1609 */
1610 num = valid_swaphandles(entry, &offset);
1611 for (i = 0; i < num; offset++, i++) {
1612 /* Ok, do the async read-ahead now */
1613 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1614 offset), vma, addr);
1615 if (!new_page)
1616 break;
1617 page_cache_release(new_page);
1618#ifdef CONFIG_NUMA
1619 /*
1620 * Find the next applicable VMA for the NUMA policy.
1621 */
1622 addr += PAGE_SIZE;
1623 if (addr == 0)
1624 vma = NULL;
1625 if (vma) {
1626 if (addr >= vma->vm_end) {
1627 vma = next_vma;
1628 next_vma = vma ? vma->vm_next : NULL;
1629 }
1630 if (vma && addr < vma->vm_start)
1631 vma = NULL;
1632 } else {
1633 if (next_vma && addr >= next_vma->vm_start) {
1634 vma = next_vma;
1635 next_vma = vma->vm_next;
1636 }
1637 }
1638#endif
1639 }
1640 lru_add_drain(); /* Push any new pages onto the LRU now */
1641}
1642
1643/*
8f4e2101
HD
1644 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1645 * but allow concurrent faults), and pte mapped but not yet locked.
1646 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 1647 */
65500d23
HD
1648static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,
1649 unsigned long address, pte_t *page_table, pmd_t *pmd,
1650 int write_access, pte_t orig_pte)
1da177e4 1651{
8f4e2101 1652 spinlock_t *ptl;
1da177e4 1653 struct page *page;
65500d23 1654 swp_entry_t entry;
1da177e4
LT
1655 pte_t pte;
1656 int ret = VM_FAULT_MINOR;
1657
8f4e2101
HD
1658 if (!pte_unmap_same(mm, page_table, orig_pte))
1659 goto out;
65500d23
HD
1660
1661 entry = pte_to_swp_entry(orig_pte);
1da177e4
LT
1662 page = lookup_swap_cache(entry);
1663 if (!page) {
1664 swapin_readahead(entry, address, vma);
1665 page = read_swap_cache_async(entry, vma, address);
1666 if (!page) {
1667 /*
8f4e2101
HD
1668 * Back out if somebody else faulted in this pte
1669 * while we released the pte lock.
1da177e4 1670 */
8f4e2101 1671 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
1da177e4
LT
1672 if (likely(pte_same(*page_table, orig_pte)))
1673 ret = VM_FAULT_OOM;
65500d23 1674 goto unlock;
1da177e4
LT
1675 }
1676
1677 /* Had to read the page from swap area: Major fault */
1678 ret = VM_FAULT_MAJOR;
1679 inc_page_state(pgmajfault);
1680 grab_swap_token();
1681 }
1682
1683 mark_page_accessed(page);
1684 lock_page(page);
1685
1686 /*
8f4e2101 1687 * Back out if somebody else already faulted in this pte.
1da177e4 1688 */
8f4e2101 1689 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
9e9bef07 1690 if (unlikely(!pte_same(*page_table, orig_pte)))
b8107480 1691 goto out_nomap;
b8107480
KK
1692
1693 if (unlikely(!PageUptodate(page))) {
1694 ret = VM_FAULT_SIGBUS;
1695 goto out_nomap;
1da177e4
LT
1696 }
1697
1698 /* The page isn't present yet, go ahead with the fault. */
1da177e4 1699
4294621f 1700 inc_mm_counter(mm, anon_rss);
1da177e4
LT
1701 pte = mk_pte(page, vma->vm_page_prot);
1702 if (write_access && can_share_swap_page(page)) {
1703 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1704 write_access = 0;
1705 }
1da177e4
LT
1706
1707 flush_icache_page(vma, page);
1708 set_pte_at(mm, address, page_table, pte);
1709 page_add_anon_rmap(page, vma, address);
1710
c475a8ab
HD
1711 swap_free(entry);
1712 if (vm_swap_full())
1713 remove_exclusive_swap_page(page);
1714 unlock_page(page);
1715
1da177e4
LT
1716 if (write_access) {
1717 if (do_wp_page(mm, vma, address,
8f4e2101 1718 page_table, pmd, ptl, pte) == VM_FAULT_OOM)
1da177e4
LT
1719 ret = VM_FAULT_OOM;
1720 goto out;
1721 }
1722
1723 /* No need to invalidate - it was non-present before */
1724 update_mmu_cache(vma, address, pte);
1725 lazy_mmu_prot_update(pte);
65500d23 1726unlock:
8f4e2101 1727 pte_unmap_unlock(page_table, ptl);
1da177e4
LT
1728out:
1729 return ret;
b8107480 1730out_nomap:
8f4e2101 1731 pte_unmap_unlock(page_table, ptl);
b8107480
KK
1732 unlock_page(page);
1733 page_cache_release(page);
65500d23 1734 return ret;
1da177e4
LT
1735}
1736
1737/*
8f4e2101
HD
1738 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1739 * but allow concurrent faults), and pte mapped but not yet locked.
1740 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 1741 */
65500d23
HD
1742static int do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1743 unsigned long address, pte_t *page_table, pmd_t *pmd,
1744 int write_access)
1da177e4 1745{
8f4e2101
HD
1746 struct page *page;
1747 spinlock_t *ptl;
1da177e4 1748 pte_t entry;
1da177e4 1749
1da177e4
LT
1750 if (write_access) {
1751 /* Allocate our own private page. */
1752 pte_unmap(page_table);
1da177e4
LT
1753
1754 if (unlikely(anon_vma_prepare(vma)))
65500d23
HD
1755 goto oom;
1756 page = alloc_zeroed_user_highpage(vma, address);
1da177e4 1757 if (!page)
65500d23 1758 goto oom;
1da177e4 1759
65500d23
HD
1760 entry = mk_pte(page, vma->vm_page_prot);
1761 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
8f4e2101
HD
1762
1763 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
1764 if (!pte_none(*page_table))
1765 goto release;
1766 inc_mm_counter(mm, anon_rss);
1da177e4
LT
1767 lru_cache_add_active(page);
1768 SetPageReferenced(page);
65500d23 1769 page_add_anon_rmap(page, vma, address);
b5810039 1770 } else {
8f4e2101
HD
1771 /* Map the ZERO_PAGE - vm_page_prot is readonly */
1772 page = ZERO_PAGE(address);
1773 page_cache_get(page);
1774 entry = mk_pte(page, vma->vm_page_prot);
1775
1776 ptl = &mm->page_table_lock;
1777 spin_lock(ptl);
1778 if (!pte_none(*page_table))
1779 goto release;
b5810039
NP
1780 inc_mm_counter(mm, file_rss);
1781 page_add_file_rmap(page);
1da177e4
LT
1782 }
1783
65500d23 1784 set_pte_at(mm, address, page_table, entry);
1da177e4
LT
1785
1786 /* No need to invalidate - it was non-present before */
65500d23 1787 update_mmu_cache(vma, address, entry);
1da177e4 1788 lazy_mmu_prot_update(entry);
65500d23 1789unlock:
8f4e2101 1790 pte_unmap_unlock(page_table, ptl);
1da177e4 1791 return VM_FAULT_MINOR;
8f4e2101
HD
1792release:
1793 page_cache_release(page);
1794 goto unlock;
65500d23 1795oom:
1da177e4
LT
1796 return VM_FAULT_OOM;
1797}
1798
1799/*
1800 * do_no_page() tries to create a new page mapping. It aggressively
1801 * tries to share with existing pages, but makes a separate copy if
1802 * the "write_access" parameter is true in order to avoid the next
1803 * page fault.
1804 *
1805 * As this is called only for pages that do not currently exist, we
1806 * do not need to flush old virtual caches or the TLB.
1807 *
8f4e2101
HD
1808 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1809 * but allow concurrent faults), and pte mapped but not yet locked.
1810 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 1811 */
65500d23
HD
1812static int do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1813 unsigned long address, pte_t *page_table, pmd_t *pmd,
1814 int write_access)
1da177e4 1815{
8f4e2101 1816 spinlock_t *ptl;
65500d23 1817 struct page *new_page;
1da177e4
LT
1818 struct address_space *mapping = NULL;
1819 pte_t entry;
1820 unsigned int sequence = 0;
1821 int ret = VM_FAULT_MINOR;
1822 int anon = 0;
1823
1da177e4 1824 pte_unmap(page_table);
1da177e4
LT
1825
1826 if (vma->vm_file) {
1827 mapping = vma->vm_file->f_mapping;
1828 sequence = mapping->truncate_count;
1829 smp_rmb(); /* serializes i_size against truncate_count */
1830 }
1831retry:
1da177e4
LT
1832 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1833 /*
1834 * No smp_rmb is needed here as long as there's a full
1835 * spin_lock/unlock sequence inside the ->nopage callback
1836 * (for the pagecache lookup) that acts as an implicit
1837 * smp_mb() and prevents the i_size read to happen
1838 * after the next truncate_count read.
1839 */
1840
1841 /* no page was available -- either SIGBUS or OOM */
1842 if (new_page == NOPAGE_SIGBUS)
1843 return VM_FAULT_SIGBUS;
1844 if (new_page == NOPAGE_OOM)
1845 return VM_FAULT_OOM;
1846
1847 /*
1848 * Should we do an early C-O-W break?
1849 */
1850 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1851 struct page *page;
1852
1853 if (unlikely(anon_vma_prepare(vma)))
1854 goto oom;
1855 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1856 if (!page)
1857 goto oom;
1858 copy_user_highpage(page, new_page, address);
1859 page_cache_release(new_page);
1860 new_page = page;
1861 anon = 1;
1862 }
1863
8f4e2101 1864 page_table = pte_offset_map_lock(mm, pmd, address, &ptl);
1da177e4
LT
1865 /*
1866 * For a file-backed vma, someone could have truncated or otherwise
1867 * invalidated this page. If unmap_mapping_range got called,
1868 * retry getting the page.
1869 */
1870 if (mapping && unlikely(sequence != mapping->truncate_count)) {
8f4e2101 1871 pte_unmap_unlock(page_table, ptl);
1da177e4 1872 page_cache_release(new_page);
65500d23
HD
1873 cond_resched();
1874 sequence = mapping->truncate_count;
1875 smp_rmb();
1da177e4
LT
1876 goto retry;
1877 }
1da177e4
LT
1878
1879 /*
1880 * This silly early PAGE_DIRTY setting removes a race
1881 * due to the bad i386 page protection. But it's valid
1882 * for other architectures too.
1883 *
1884 * Note that if write_access is true, we either now have
1885 * an exclusive copy of the page, or this is a shared mapping,
1886 * so we can make it writable and dirty to avoid having to
1887 * handle that later.
1888 */
1889 /* Only go through if we didn't race with anybody else... */
1890 if (pte_none(*page_table)) {
1da177e4
LT
1891 flush_icache_page(vma, new_page);
1892 entry = mk_pte(new_page, vma->vm_page_prot);
1893 if (write_access)
1894 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1895 set_pte_at(mm, address, page_table, entry);
1896 if (anon) {
4294621f 1897 inc_mm_counter(mm, anon_rss);
1da177e4
LT
1898 lru_cache_add_active(new_page);
1899 page_add_anon_rmap(new_page, vma, address);
b5810039 1900 } else if (!(vma->vm_flags & VM_RESERVED)) {
4294621f 1901 inc_mm_counter(mm, file_rss);
1da177e4 1902 page_add_file_rmap(new_page);
4294621f 1903 }
1da177e4
LT
1904 } else {
1905 /* One of our sibling threads was faster, back out. */
1da177e4 1906 page_cache_release(new_page);
65500d23 1907 goto unlock;
1da177e4
LT
1908 }
1909
1910 /* no need to invalidate: a not-present page shouldn't be cached */
1911 update_mmu_cache(vma, address, entry);
1912 lazy_mmu_prot_update(entry);
65500d23 1913unlock:
8f4e2101 1914 pte_unmap_unlock(page_table, ptl);
1da177e4
LT
1915 return ret;
1916oom:
1917 page_cache_release(new_page);
65500d23 1918 return VM_FAULT_OOM;
1da177e4
LT
1919}
1920
1921/*
1922 * Fault of a previously existing named mapping. Repopulate the pte
1923 * from the encoded file_pte if possible. This enables swappable
1924 * nonlinear vmas.
8f4e2101
HD
1925 *
1926 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1927 * but allow concurrent faults), and pte mapped but not yet locked.
1928 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4 1929 */
65500d23
HD
1930static int do_file_page(struct mm_struct *mm, struct vm_area_struct *vma,
1931 unsigned long address, pte_t *page_table, pmd_t *pmd,
1932 int write_access, pte_t orig_pte)
1da177e4 1933{
65500d23 1934 pgoff_t pgoff;
1da177e4
LT
1935 int err;
1936
8f4e2101
HD
1937 if (!pte_unmap_same(mm, page_table, orig_pte))
1938 return VM_FAULT_MINOR;
1da177e4 1939
65500d23
HD
1940 if (unlikely(!(vma->vm_flags & VM_NONLINEAR))) {
1941 /*
1942 * Page table corrupted: show pte and kill process.
1943 */
b5810039 1944 print_bad_pte(vma, orig_pte, address);
65500d23
HD
1945 return VM_FAULT_OOM;
1946 }
1947 /* We can then assume vm->vm_ops && vma->vm_ops->populate */
1948
1949 pgoff = pte_to_pgoff(orig_pte);
1950 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE,
1951 vma->vm_page_prot, pgoff, 0);
1da177e4
LT
1952 if (err == -ENOMEM)
1953 return VM_FAULT_OOM;
1954 if (err)
1955 return VM_FAULT_SIGBUS;
1956 return VM_FAULT_MAJOR;
1957}
1958
1959/*
1960 * These routines also need to handle stuff like marking pages dirty
1961 * and/or accessed for architectures that don't do it in hardware (most
1962 * RISC architectures). The early dirtying is also good on the i386.
1963 *
1964 * There is also a hook called "update_mmu_cache()" that architectures
1965 * with external mmu caches can use to update those (ie the Sparc or
1966 * PowerPC hashed page tables that act as extended TLBs).
1967 *
c74df32c
HD
1968 * We enter with non-exclusive mmap_sem (to exclude vma changes,
1969 * but allow concurrent faults), and pte mapped but not yet locked.
1970 * We return with mmap_sem still held, but pte unmapped and unlocked.
1da177e4
LT
1971 */
1972static inline int handle_pte_fault(struct mm_struct *mm,
65500d23
HD
1973 struct vm_area_struct *vma, unsigned long address,
1974 pte_t *pte, pmd_t *pmd, int write_access)
1da177e4
LT
1975{
1976 pte_t entry;
8f4e2101 1977 spinlock_t *ptl;
1da177e4
LT
1978
1979 entry = *pte;
1980 if (!pte_present(entry)) {
65500d23
HD
1981 if (pte_none(entry)) {
1982 if (!vma->vm_ops || !vma->vm_ops->nopage)
1983 return do_anonymous_page(mm, vma, address,
1984 pte, pmd, write_access);
1985 return do_no_page(mm, vma, address,
1986 pte, pmd, write_access);
1987 }
1da177e4 1988 if (pte_file(entry))
65500d23
HD
1989 return do_file_page(mm, vma, address,
1990 pte, pmd, write_access, entry);
1991 return do_swap_page(mm, vma, address,
1992 pte, pmd, write_access, entry);
1da177e4
LT
1993 }
1994
8f4e2101
HD
1995 ptl = &mm->page_table_lock;
1996 spin_lock(ptl);
1997 if (unlikely(!pte_same(*pte, entry)))
1998 goto unlock;
1da177e4
LT
1999 if (write_access) {
2000 if (!pte_write(entry))
8f4e2101
HD
2001 return do_wp_page(mm, vma, address,
2002 pte, pmd, ptl, entry);
1da177e4
LT
2003 entry = pte_mkdirty(entry);
2004 }
2005 entry = pte_mkyoung(entry);
2006 ptep_set_access_flags(vma, address, pte, entry, write_access);
2007 update_mmu_cache(vma, address, entry);
2008 lazy_mmu_prot_update(entry);
8f4e2101
HD
2009unlock:
2010 pte_unmap_unlock(pte, ptl);
1da177e4
LT
2011 return VM_FAULT_MINOR;
2012}
2013
2014/*
2015 * By the time we get here, we already hold the mm semaphore
2016 */
65500d23 2017int __handle_mm_fault(struct mm_struct *mm, struct vm_area_struct *vma,
1da177e4
LT
2018 unsigned long address, int write_access)
2019{
2020 pgd_t *pgd;
2021 pud_t *pud;
2022 pmd_t *pmd;
2023 pte_t *pte;
2024
2025 __set_current_state(TASK_RUNNING);
2026
2027 inc_page_state(pgfault);
2028
ac9b9c66
HD
2029 if (unlikely(is_vm_hugetlb_page(vma)))
2030 return hugetlb_fault(mm, vma, address, write_access);
1da177e4 2031
1da177e4 2032 pgd = pgd_offset(mm, address);
1da177e4
LT
2033 pud = pud_alloc(mm, pgd, address);
2034 if (!pud)
c74df32c 2035 return VM_FAULT_OOM;
1da177e4
LT
2036 pmd = pmd_alloc(mm, pud, address);
2037 if (!pmd)
c74df32c 2038 return VM_FAULT_OOM;
1da177e4
LT
2039 pte = pte_alloc_map(mm, pmd, address);
2040 if (!pte)
c74df32c 2041 return VM_FAULT_OOM;
1da177e4 2042
c74df32c 2043 return handle_pte_fault(mm, vma, address, pte, pmd, write_access);
1da177e4
LT
2044}
2045
2046#ifndef __PAGETABLE_PUD_FOLDED
2047/*
2048 * Allocate page upper directory.
872fec16 2049 * We've already handled the fast-path in-line.
1da177e4 2050 */
1bb3630e 2051int __pud_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1da177e4 2052{
c74df32c
HD
2053 pud_t *new = pud_alloc_one(mm, address);
2054 if (!new)
1bb3630e 2055 return -ENOMEM;
1da177e4 2056
872fec16 2057 spin_lock(&mm->page_table_lock);
1bb3630e 2058 if (pgd_present(*pgd)) /* Another has populated it */
1da177e4 2059 pud_free(new);
1bb3630e
HD
2060 else
2061 pgd_populate(mm, pgd, new);
c74df32c 2062 spin_unlock(&mm->page_table_lock);
1bb3630e 2063 return 0;
1da177e4
LT
2064}
2065#endif /* __PAGETABLE_PUD_FOLDED */
2066
2067#ifndef __PAGETABLE_PMD_FOLDED
2068/*
2069 * Allocate page middle directory.
872fec16 2070 * We've already handled the fast-path in-line.
1da177e4 2071 */
1bb3630e 2072int __pmd_alloc(struct mm_struct *mm, pud_t *pud, unsigned long address)
1da177e4 2073{
c74df32c
HD
2074 pmd_t *new = pmd_alloc_one(mm, address);
2075 if (!new)
1bb3630e 2076 return -ENOMEM;
1da177e4 2077
872fec16 2078 spin_lock(&mm->page_table_lock);
1da177e4 2079#ifndef __ARCH_HAS_4LEVEL_HACK
1bb3630e 2080 if (pud_present(*pud)) /* Another has populated it */
1da177e4 2081 pmd_free(new);
1bb3630e
HD
2082 else
2083 pud_populate(mm, pud, new);
1da177e4 2084#else
1bb3630e 2085 if (pgd_present(*pud)) /* Another has populated it */
1da177e4 2086 pmd_free(new);
1bb3630e
HD
2087 else
2088 pgd_populate(mm, pud, new);
1da177e4 2089#endif /* __ARCH_HAS_4LEVEL_HACK */
c74df32c 2090 spin_unlock(&mm->page_table_lock);
1bb3630e 2091 return 0;
1da177e4
LT
2092}
2093#endif /* __PAGETABLE_PMD_FOLDED */
2094
2095int make_pages_present(unsigned long addr, unsigned long end)
2096{
2097 int ret, len, write;
2098 struct vm_area_struct * vma;
2099
2100 vma = find_vma(current->mm, addr);
2101 if (!vma)
2102 return -1;
2103 write = (vma->vm_flags & VM_WRITE) != 0;
2104 if (addr >= end)
2105 BUG();
2106 if (end > vma->vm_end)
2107 BUG();
2108 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
2109 ret = get_user_pages(current, current->mm, addr,
2110 len, write, 0, NULL, NULL);
2111 if (ret < 0)
2112 return ret;
2113 return ret == len ? 0 : -1;
2114}
2115
2116/*
2117 * Map a vmalloc()-space virtual address to the physical page.
2118 */
2119struct page * vmalloc_to_page(void * vmalloc_addr)
2120{
2121 unsigned long addr = (unsigned long) vmalloc_addr;
2122 struct page *page = NULL;
2123 pgd_t *pgd = pgd_offset_k(addr);
2124 pud_t *pud;
2125 pmd_t *pmd;
2126 pte_t *ptep, pte;
2127
2128 if (!pgd_none(*pgd)) {
2129 pud = pud_offset(pgd, addr);
2130 if (!pud_none(*pud)) {
2131 pmd = pmd_offset(pud, addr);
2132 if (!pmd_none(*pmd)) {
2133 ptep = pte_offset_map(pmd, addr);
2134 pte = *ptep;
2135 if (pte_present(pte))
2136 page = pte_page(pte);
2137 pte_unmap(ptep);
2138 }
2139 }
2140 }
2141 return page;
2142}
2143
2144EXPORT_SYMBOL(vmalloc_to_page);
2145
2146/*
2147 * Map a vmalloc()-space virtual address to the physical page frame number.
2148 */
2149unsigned long vmalloc_to_pfn(void * vmalloc_addr)
2150{
2151 return page_to_pfn(vmalloc_to_page(vmalloc_addr));
2152}
2153
2154EXPORT_SYMBOL(vmalloc_to_pfn);
2155
1da177e4
LT
2156#if !defined(__HAVE_ARCH_GATE_AREA)
2157
2158#if defined(AT_SYSINFO_EHDR)
5ce7852c 2159static struct vm_area_struct gate_vma;
1da177e4
LT
2160
2161static int __init gate_vma_init(void)
2162{
2163 gate_vma.vm_mm = NULL;
2164 gate_vma.vm_start = FIXADDR_USER_START;
2165 gate_vma.vm_end = FIXADDR_USER_END;
2166 gate_vma.vm_page_prot = PAGE_READONLY;
b5810039 2167 gate_vma.vm_flags = VM_RESERVED;
1da177e4
LT
2168 return 0;
2169}
2170__initcall(gate_vma_init);
2171#endif
2172
2173struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
2174{
2175#ifdef AT_SYSINFO_EHDR
2176 return &gate_vma;
2177#else
2178 return NULL;
2179#endif
2180}
2181
2182int in_gate_area_no_task(unsigned long addr)
2183{
2184#ifdef AT_SYSINFO_EHDR
2185 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
2186 return 1;
2187#endif
2188 return 0;
2189}
2190
2191#endif /* __HAVE_ARCH_GATE_AREA */