[PATCH] V4L: follow changes in saa7146
[linux-2.6/history.git] / mm / memory.c
blobc43881bbd00d286553226f766bbedc09d6890a14
1 /*
2 * linux/mm/memory.c
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
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
13 * Ok, demand-loading was easy, shared pages a little bit tricker. Shared
14 * pages started 02.12.91, seems to work. - Linus.
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.
20 * Also corrected some "invalidate()"s - I wasn't doing enough of them.
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.
32 * 05.04.94 - Multi-page memory management added for v1.1.
33 * Idea by Alex Bligh (alex@cconcepts.co.uk)
35 * 16.07.99 - Support of BIGMEM added by Gerhard Wichert, Siemens AG
36 * (Gerhard.Wichert@pdb.siemens.de)
39 #include <linux/kernel_stat.h>
40 #include <linux/mm.h>
41 #include <linux/hugetlb.h>
42 #include <linux/mman.h>
43 #include <linux/swap.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/rmap.h>
47 #include <linux/module.h>
48 #include <linux/init.h>
50 #include <asm/pgalloc.h>
51 #include <asm/uaccess.h>
52 #include <asm/tlb.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
56 #include <linux/swapops.h>
57 #include <linux/elf.h>
59 #ifndef CONFIG_DISCONTIGMEM
60 /* use the per-pgdat data instead for discontigmem - mbligh */
61 unsigned long max_mapnr;
62 struct page *mem_map;
64 EXPORT_SYMBOL(max_mapnr);
65 EXPORT_SYMBOL(mem_map);
66 #endif
68 unsigned long num_physpages;
70 * A number of key systems in x86 including ioremap() rely on the assumption
71 * that high_memory defines the upper bound on direct map memory, then end
72 * of ZONE_NORMAL. Under CONFIG_DISCONTIG this means that max_low_pfn and
73 * highstart_pfn must be the same; there must be no gap between ZONE_NORMAL
74 * and ZONE_HIGHMEM.
76 void * high_memory;
77 struct page *highmem_start_page;
78 unsigned long vmalloc_earlyreserve;
80 EXPORT_SYMBOL(num_physpages);
81 EXPORT_SYMBOL(highmem_start_page);
82 EXPORT_SYMBOL(high_memory);
83 EXPORT_SYMBOL(vmalloc_earlyreserve);
86 * We special-case the C-O-W ZERO_PAGE, because it's such
87 * a common occurrence (no need to read the page to know
88 * that it's zero - better for the cache and memory subsystem).
90 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
92 if (from == ZERO_PAGE(address)) {
93 clear_user_highpage(to, address);
94 return;
96 copy_user_highpage(to, from, address);
100 * Note: this doesn't free the actual pages themselves. That
101 * has been handled earlier when unmapping all the memory regions.
103 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
105 struct page *page;
107 if (pmd_none(*dir))
108 return;
109 if (unlikely(pmd_bad(*dir))) {
110 pmd_ERROR(*dir);
111 pmd_clear(dir);
112 return;
114 page = pmd_page(*dir);
115 pmd_clear(dir);
116 dec_page_state(nr_page_table_pages);
117 tlb->mm->nr_ptes--;
118 pte_free_tlb(tlb, page);
121 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir)
123 int j;
124 pmd_t * pmd;
126 if (pgd_none(*dir))
127 return;
128 if (unlikely(pgd_bad(*dir))) {
129 pgd_ERROR(*dir);
130 pgd_clear(dir);
131 return;
133 pmd = pmd_offset(dir, 0);
134 pgd_clear(dir);
135 for (j = 0; j < PTRS_PER_PMD ; j++)
136 free_one_pmd(tlb, pmd+j);
137 pmd_free_tlb(tlb, pmd);
141 * This function clears all user-level page tables of a process - this
142 * is needed by execve(), so that old pages aren't in the way.
144 * Must be called with pagetable lock held.
146 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
148 pgd_t * page_dir = tlb->mm->pgd;
150 page_dir += first;
151 do {
152 free_one_pgd(tlb, page_dir);
153 page_dir++;
154 } while (--nr);
157 pte_t fastcall * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
159 if (!pmd_present(*pmd)) {
160 struct page *new;
162 spin_unlock(&mm->page_table_lock);
163 new = pte_alloc_one(mm, address);
164 spin_lock(&mm->page_table_lock);
165 if (!new)
166 return NULL;
168 * Because we dropped the lock, we should re-check the
169 * entry, as somebody else could have populated it..
171 if (pmd_present(*pmd)) {
172 pte_free(new);
173 goto out;
175 mm->nr_ptes++;
176 inc_page_state(nr_page_table_pages);
177 pmd_populate(mm, pmd, new);
179 out:
180 return pte_offset_map(pmd, address);
183 pte_t fastcall * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
185 if (!pmd_present(*pmd)) {
186 pte_t *new;
188 spin_unlock(&mm->page_table_lock);
189 new = pte_alloc_one_kernel(mm, address);
190 spin_lock(&mm->page_table_lock);
191 if (!new)
192 return NULL;
195 * Because we dropped the lock, we should re-check the
196 * entry, as somebody else could have populated it..
198 if (pmd_present(*pmd)) {
199 pte_free_kernel(new);
200 goto out;
202 pmd_populate_kernel(mm, pmd, new);
204 out:
205 return pte_offset_kernel(pmd, address);
207 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
208 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
211 * copy one vm_area from one task to the other. Assumes the page tables
212 * already present in the new task to be cleared in the whole range
213 * covered by this vma.
215 * 08Jan98 Merged into one routine from several inline routines to reduce
216 * variable count and make things faster. -jj
218 * dst->page_table_lock is held on entry and exit,
219 * but may be dropped within pmd_alloc() and pte_alloc_map().
221 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
222 struct vm_area_struct *vma)
224 pgd_t * src_pgd, * dst_pgd;
225 unsigned long address = vma->vm_start;
226 unsigned long end = vma->vm_end;
227 unsigned long cow;
229 if (is_vm_hugetlb_page(vma))
230 return copy_hugetlb_page_range(dst, src, vma);
232 cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
233 src_pgd = pgd_offset(src, address)-1;
234 dst_pgd = pgd_offset(dst, address)-1;
236 for (;;) {
237 pmd_t * src_pmd, * dst_pmd;
239 src_pgd++; dst_pgd++;
241 /* copy_pmd_range */
243 if (pgd_none(*src_pgd))
244 goto skip_copy_pmd_range;
245 if (unlikely(pgd_bad(*src_pgd))) {
246 pgd_ERROR(*src_pgd);
247 pgd_clear(src_pgd);
248 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
249 if (!address || (address >= end))
250 goto out;
251 continue;
254 src_pmd = pmd_offset(src_pgd, address);
255 dst_pmd = pmd_alloc(dst, dst_pgd, address);
256 if (!dst_pmd)
257 goto nomem;
259 do {
260 pte_t * src_pte, * dst_pte;
262 /* copy_pte_range */
264 if (pmd_none(*src_pmd))
265 goto skip_copy_pte_range;
266 if (unlikely(pmd_bad(*src_pmd))) {
267 pmd_ERROR(*src_pmd);
268 pmd_clear(src_pmd);
269 skip_copy_pte_range:
270 address = (address + PMD_SIZE) & PMD_MASK;
271 if (address >= end)
272 goto out;
273 goto cont_copy_pmd_range;
276 dst_pte = pte_alloc_map(dst, dst_pmd, address);
277 if (!dst_pte)
278 goto nomem;
279 spin_lock(&src->page_table_lock);
280 src_pte = pte_offset_map_nested(src_pmd, address);
281 do {
282 pte_t pte = *src_pte;
283 struct page *page;
284 unsigned long pfn;
286 /* copy_one_pte */
288 if (pte_none(pte))
289 goto cont_copy_pte_range_noset;
290 /* pte contains position in swap, so copy. */
291 if (!pte_present(pte)) {
292 if (!pte_file(pte)) {
293 swap_duplicate(pte_to_swp_entry(pte));
294 if (list_empty(&dst->mmlist)) {
295 spin_lock(&mmlist_lock);
296 list_add(&dst->mmlist,
297 &src->mmlist);
298 spin_unlock(&mmlist_lock);
301 set_pte(dst_pte, pte);
302 goto cont_copy_pte_range_noset;
304 pfn = pte_pfn(pte);
305 /* the pte points outside of valid memory, the
306 * mapping is assumed to be good, meaningful
307 * and not mapped via rmap - duplicate the
308 * mapping as is.
310 page = NULL;
311 if (pfn_valid(pfn))
312 page = pfn_to_page(pfn);
314 if (!page || PageReserved(page)) {
315 set_pte(dst_pte, pte);
316 goto cont_copy_pte_range_noset;
320 * If it's a COW mapping, write protect it both
321 * in the parent and the child
323 if (cow) {
324 ptep_set_wrprotect(src_pte);
325 pte = *src_pte;
329 * If it's a shared mapping, mark it clean in
330 * the child
332 if (vma->vm_flags & VM_SHARED)
333 pte = pte_mkclean(pte);
334 pte = pte_mkold(pte);
335 get_page(page);
336 dst->rss++;
337 set_pte(dst_pte, pte);
338 page_dup_rmap(page);
339 cont_copy_pte_range_noset:
340 address += PAGE_SIZE;
341 if (address >= end) {
342 pte_unmap_nested(src_pte);
343 pte_unmap(dst_pte);
344 goto out_unlock;
346 src_pte++;
347 dst_pte++;
348 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
349 pte_unmap_nested(src_pte-1);
350 pte_unmap(dst_pte-1);
351 spin_unlock(&src->page_table_lock);
352 cond_resched_lock(&dst->page_table_lock);
353 cont_copy_pmd_range:
354 src_pmd++;
355 dst_pmd++;
356 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
358 out_unlock:
359 spin_unlock(&src->page_table_lock);
360 out:
361 return 0;
362 nomem:
363 return -ENOMEM;
366 static void zap_pte_range(struct mmu_gather *tlb,
367 pmd_t *pmd, unsigned long address,
368 unsigned long size, struct zap_details *details)
370 unsigned long offset;
371 pte_t *ptep;
373 if (pmd_none(*pmd))
374 return;
375 if (unlikely(pmd_bad(*pmd))) {
376 pmd_ERROR(*pmd);
377 pmd_clear(pmd);
378 return;
380 ptep = pte_offset_map(pmd, address);
381 offset = address & ~PMD_MASK;
382 if (offset + size > PMD_SIZE)
383 size = PMD_SIZE - offset;
384 size &= PAGE_MASK;
385 if (details && !details->check_mapping && !details->nonlinear_vma)
386 details = NULL;
387 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
388 pte_t pte = *ptep;
389 if (pte_none(pte))
390 continue;
391 if (pte_present(pte)) {
392 struct page *page = NULL;
393 unsigned long pfn = pte_pfn(pte);
394 if (pfn_valid(pfn)) {
395 page = pfn_to_page(pfn);
396 if (PageReserved(page))
397 page = NULL;
399 if (unlikely(details) && page) {
401 * unmap_shared_mapping_pages() wants to
402 * invalidate cache without truncating:
403 * unmap shared but keep private pages.
405 if (details->check_mapping &&
406 details->check_mapping != page->mapping)
407 continue;
409 * Each page->index must be checked when
410 * invalidating or truncating nonlinear.
412 if (details->nonlinear_vma &&
413 (page->index < details->first_index ||
414 page->index > details->last_index))
415 continue;
417 pte = ptep_get_and_clear(ptep);
418 tlb_remove_tlb_entry(tlb, ptep, address+offset);
419 if (unlikely(!page))
420 continue;
421 if (unlikely(details) && details->nonlinear_vma
422 && linear_page_index(details->nonlinear_vma,
423 address+offset) != page->index)
424 set_pte(ptep, pgoff_to_pte(page->index));
425 if (pte_dirty(pte))
426 set_page_dirty(page);
427 if (pte_young(pte) && !PageAnon(page))
428 mark_page_accessed(page);
429 tlb->freed++;
430 page_remove_rmap(page);
431 tlb_remove_page(tlb, page);
432 continue;
435 * If details->check_mapping, we leave swap entries;
436 * if details->nonlinear_vma, we leave file entries.
438 if (unlikely(details))
439 continue;
440 if (!pte_file(pte))
441 free_swap_and_cache(pte_to_swp_entry(pte));
442 pte_clear(ptep);
444 pte_unmap(ptep-1);
447 static void zap_pmd_range(struct mmu_gather *tlb,
448 pgd_t * dir, unsigned long address,
449 unsigned long size, struct zap_details *details)
451 pmd_t * pmd;
452 unsigned long end;
454 if (pgd_none(*dir))
455 return;
456 if (unlikely(pgd_bad(*dir))) {
457 pgd_ERROR(*dir);
458 pgd_clear(dir);
459 return;
461 pmd = pmd_offset(dir, address);
462 end = address + size;
463 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
464 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
465 do {
466 zap_pte_range(tlb, pmd, address, end - address, details);
467 address = (address + PMD_SIZE) & PMD_MASK;
468 pmd++;
469 } while (address && (address < end));
472 static void unmap_page_range(struct mmu_gather *tlb,
473 struct vm_area_struct *vma, unsigned long address,
474 unsigned long end, struct zap_details *details)
476 pgd_t * dir;
478 BUG_ON(address >= end);
479 dir = pgd_offset(vma->vm_mm, address);
480 tlb_start_vma(tlb, vma);
481 do {
482 zap_pmd_range(tlb, dir, address, end - address, details);
483 address = (address + PGDIR_SIZE) & PGDIR_MASK;
484 dir++;
485 } while (address && (address < end));
486 tlb_end_vma(tlb, vma);
489 /* Dispose of an entire struct mmu_gather per rescheduling point */
490 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
491 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
492 #endif
494 /* For UP, 256 pages at a time gives nice low latency */
495 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
496 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
497 #endif
499 /* No preempt: go for improved straight-line efficiency */
500 #if !defined(CONFIG_PREEMPT)
501 #define ZAP_BLOCK_SIZE (1024 * PAGE_SIZE)
502 #endif
505 * unmap_vmas - unmap a range of memory covered by a list of vma's
506 * @tlbp: address of the caller's struct mmu_gather
507 * @mm: the controlling mm_struct
508 * @vma: the starting vma
509 * @start_addr: virtual address at which to start unmapping
510 * @end_addr: virtual address at which to end unmapping
511 * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
512 * @details: details of nonlinear truncation or shared cache invalidation
514 * Returns the number of vma's which were covered by the unmapping.
516 * Unmap all pages in the vma list. Called under page_table_lock.
518 * We aim to not hold page_table_lock for too long (for scheduling latency
519 * reasons). So zap pages in ZAP_BLOCK_SIZE bytecounts. This means we need to
520 * return the ending mmu_gather to the caller.
522 * Only addresses between `start' and `end' will be unmapped.
524 * The VMA list must be sorted in ascending virtual address order.
526 * unmap_vmas() assumes that the caller will flush the whole unmapped address
527 * range after unmap_vmas() returns. So the only responsibility here is to
528 * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
529 * drops the lock and schedules.
531 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
532 struct vm_area_struct *vma, unsigned long start_addr,
533 unsigned long end_addr, unsigned long *nr_accounted,
534 struct zap_details *details)
536 unsigned long zap_bytes = ZAP_BLOCK_SIZE;
537 unsigned long tlb_start = 0; /* For tlb_finish_mmu */
538 int tlb_start_valid = 0;
539 int ret = 0;
540 int atomic = details && details->atomic;
542 for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
543 unsigned long start;
544 unsigned long end;
546 start = max(vma->vm_start, start_addr);
547 if (start >= vma->vm_end)
548 continue;
549 end = min(vma->vm_end, end_addr);
550 if (end <= vma->vm_start)
551 continue;
553 if (vma->vm_flags & VM_ACCOUNT)
554 *nr_accounted += (end - start) >> PAGE_SHIFT;
556 ret++;
557 while (start != end) {
558 unsigned long block;
560 if (!tlb_start_valid) {
561 tlb_start = start;
562 tlb_start_valid = 1;
565 if (is_vm_hugetlb_page(vma)) {
566 block = end - start;
567 unmap_hugepage_range(vma, start, end);
568 } else {
569 block = min(zap_bytes, end - start);
570 unmap_page_range(*tlbp, vma, start,
571 start + block, details);
574 start += block;
575 zap_bytes -= block;
576 if ((long)zap_bytes > 0)
577 continue;
578 if (!atomic && need_resched()) {
579 int fullmm = tlb_is_full_mm(*tlbp);
580 tlb_finish_mmu(*tlbp, tlb_start, start);
581 cond_resched_lock(&mm->page_table_lock);
582 *tlbp = tlb_gather_mmu(mm, fullmm);
583 tlb_start_valid = 0;
585 zap_bytes = ZAP_BLOCK_SIZE;
588 return ret;
592 * zap_page_range - remove user pages in a given range
593 * @vma: vm_area_struct holding the applicable pages
594 * @address: starting address of pages to zap
595 * @size: number of bytes to zap
596 * @details: details of nonlinear truncation or shared cache invalidation
598 void zap_page_range(struct vm_area_struct *vma, unsigned long address,
599 unsigned long size, struct zap_details *details)
601 struct mm_struct *mm = vma->vm_mm;
602 struct mmu_gather *tlb;
603 unsigned long end = address + size;
604 unsigned long nr_accounted = 0;
606 if (is_vm_hugetlb_page(vma)) {
607 zap_hugepage_range(vma, address, size);
608 return;
611 lru_add_drain();
612 spin_lock(&mm->page_table_lock);
613 tlb = tlb_gather_mmu(mm, 0);
614 unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted, details);
615 tlb_finish_mmu(tlb, address, end);
616 spin_unlock(&mm->page_table_lock);
620 * Do a quick page-table lookup for a single page.
621 * mm->page_table_lock must be held.
623 struct page *
624 follow_page(struct mm_struct *mm, unsigned long address, int write)
626 pgd_t *pgd;
627 pmd_t *pmd;
628 pte_t *ptep, pte;
629 unsigned long pfn;
630 struct page *page;
632 page = follow_huge_addr(mm, address, write);
633 if (! IS_ERR(page))
634 return page;
636 pgd = pgd_offset(mm, address);
637 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
638 goto out;
640 pmd = pmd_offset(pgd, address);
641 if (pmd_none(*pmd))
642 goto out;
643 if (pmd_huge(*pmd))
644 return follow_huge_pmd(mm, address, pmd, write);
645 if (unlikely(pmd_bad(*pmd)))
646 goto out;
648 ptep = pte_offset_map(pmd, address);
649 if (!ptep)
650 goto out;
652 pte = *ptep;
653 pte_unmap(ptep);
654 if (pte_present(pte)) {
655 if (write && !pte_write(pte))
656 goto out;
657 pfn = pte_pfn(pte);
658 if (pfn_valid(pfn)) {
659 page = pfn_to_page(pfn);
660 if (write && !pte_dirty(pte) && !PageDirty(page))
661 set_page_dirty(page);
662 mark_page_accessed(page);
663 return page;
667 out:
668 return NULL;
672 * Given a physical address, is there a useful struct page pointing to
673 * it? This may become more complex in the future if we start dealing
674 * with IO-aperture pages for direct-IO.
677 static inline struct page *get_page_map(struct page *page)
679 if (!pfn_valid(page_to_pfn(page)))
680 return NULL;
681 return page;
685 static inline int
686 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
687 unsigned long address)
689 pgd_t *pgd;
690 pmd_t *pmd;
692 /* Check if the vma is for an anonymous mapping. */
693 if (vma->vm_ops && vma->vm_ops->nopage)
694 return 0;
696 /* Check if page directory entry exists. */
697 pgd = pgd_offset(mm, address);
698 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
699 return 1;
701 /* Check if page middle directory entry exists. */
702 pmd = pmd_offset(pgd, address);
703 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
704 return 1;
706 /* There is a pte slot for 'address' in 'mm'. */
707 return 0;
711 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
712 unsigned long start, int len, int write, int force,
713 struct page **pages, struct vm_area_struct **vmas)
715 int i;
716 unsigned int flags;
719 * Require read or write permissions.
720 * If 'force' is set, we only require the "MAY" flags.
722 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
723 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
724 i = 0;
726 do {
727 struct vm_area_struct * vma;
729 vma = find_extend_vma(mm, start);
730 if (!vma && in_gate_area(tsk, start)) {
731 unsigned long pg = start & PAGE_MASK;
732 struct vm_area_struct *gate_vma = get_gate_vma(tsk);
733 pgd_t *pgd;
734 pmd_t *pmd;
735 pte_t *pte;
736 if (write) /* user gate pages are read-only */
737 return i ? : -EFAULT;
738 pgd = pgd_offset_gate(mm, pg);
739 if (!pgd)
740 return i ? : -EFAULT;
741 pmd = pmd_offset(pgd, pg);
742 if (!pmd)
743 return i ? : -EFAULT;
744 pte = pte_offset_map(pmd, pg);
745 if (!pte)
746 return i ? : -EFAULT;
747 if (!pte_present(*pte)) {
748 pte_unmap(pte);
749 return i ? : -EFAULT;
751 if (pages) {
752 pages[i] = pte_page(*pte);
753 get_page(pages[i]);
755 pte_unmap(pte);
756 if (vmas)
757 vmas[i] = gate_vma;
758 i++;
759 start += PAGE_SIZE;
760 len--;
761 continue;
764 if (!vma || (pages && (vma->vm_flags & VM_IO))
765 || !(flags & vma->vm_flags))
766 return i ? : -EFAULT;
768 if (is_vm_hugetlb_page(vma)) {
769 i = follow_hugetlb_page(mm, vma, pages, vmas,
770 &start, &len, i);
771 continue;
773 spin_lock(&mm->page_table_lock);
774 do {
775 struct page *map;
776 int lookup_write = write;
777 while (!(map = follow_page(mm, start, lookup_write))) {
779 * Shortcut for anonymous pages. We don't want
780 * to force the creation of pages tables for
781 * insanly big anonymously mapped areas that
782 * nobody touched so far. This is important
783 * for doing a core dump for these mappings.
785 if (!lookup_write &&
786 untouched_anonymous_page(mm,vma,start)) {
787 map = ZERO_PAGE(start);
788 break;
790 spin_unlock(&mm->page_table_lock);
791 switch (handle_mm_fault(mm,vma,start,write)) {
792 case VM_FAULT_MINOR:
793 tsk->min_flt++;
794 break;
795 case VM_FAULT_MAJOR:
796 tsk->maj_flt++;
797 break;
798 case VM_FAULT_SIGBUS:
799 return i ? i : -EFAULT;
800 case VM_FAULT_OOM:
801 return i ? i : -ENOMEM;
802 default:
803 BUG();
806 * Now that we have performed a write fault
807 * and surely no longer have a shared page we
808 * shouldn't write, we shouldn't ignore an
809 * unwritable page in the page table if
810 * we are forcing write access.
812 lookup_write = write && !force;
813 spin_lock(&mm->page_table_lock);
815 if (pages) {
816 pages[i] = get_page_map(map);
817 if (!pages[i]) {
818 spin_unlock(&mm->page_table_lock);
819 while (i--)
820 page_cache_release(pages[i]);
821 i = -EFAULT;
822 goto out;
824 flush_dcache_page(pages[i]);
825 if (!PageReserved(pages[i]))
826 page_cache_get(pages[i]);
828 if (vmas)
829 vmas[i] = vma;
830 i++;
831 start += PAGE_SIZE;
832 len--;
833 } while(len && start < vma->vm_end);
834 spin_unlock(&mm->page_table_lock);
835 } while(len);
836 out:
837 return i;
840 EXPORT_SYMBOL(get_user_pages);
842 static void zeromap_pte_range(pte_t * pte, unsigned long address,
843 unsigned long size, pgprot_t prot)
845 unsigned long end;
847 address &= ~PMD_MASK;
848 end = address + size;
849 if (end > PMD_SIZE)
850 end = PMD_SIZE;
851 do {
852 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
853 BUG_ON(!pte_none(*pte));
854 set_pte(pte, zero_pte);
855 address += PAGE_SIZE;
856 pte++;
857 } while (address && (address < end));
860 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
861 unsigned long size, pgprot_t prot)
863 unsigned long base, end;
865 base = address & PGDIR_MASK;
866 address &= ~PGDIR_MASK;
867 end = address + size;
868 if (end > PGDIR_SIZE)
869 end = PGDIR_SIZE;
870 do {
871 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
872 if (!pte)
873 return -ENOMEM;
874 zeromap_pte_range(pte, base + address, end - address, prot);
875 pte_unmap(pte);
876 address = (address + PMD_SIZE) & PMD_MASK;
877 pmd++;
878 } while (address && (address < end));
879 return 0;
882 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
884 int error = 0;
885 pgd_t * dir;
886 unsigned long beg = address;
887 unsigned long end = address + size;
888 struct mm_struct *mm = vma->vm_mm;
890 dir = pgd_offset(mm, address);
891 flush_cache_range(vma, beg, end);
892 if (address >= end)
893 BUG();
895 spin_lock(&mm->page_table_lock);
896 do {
897 pmd_t *pmd = pmd_alloc(mm, dir, address);
898 error = -ENOMEM;
899 if (!pmd)
900 break;
901 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
902 if (error)
903 break;
904 address = (address + PGDIR_SIZE) & PGDIR_MASK;
905 dir++;
906 } while (address && (address < end));
908 * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
910 flush_tlb_range(vma, beg, end);
911 spin_unlock(&mm->page_table_lock);
912 return error;
916 * maps a range of physical memory into the requested pages. the old
917 * mappings are removed. any references to nonexistent pages results
918 * in null mappings (currently treated as "copy-on-access")
920 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
921 unsigned long phys_addr, pgprot_t prot)
923 unsigned long end;
924 unsigned long pfn;
926 address &= ~PMD_MASK;
927 end = address + size;
928 if (end > PMD_SIZE)
929 end = PMD_SIZE;
930 pfn = phys_addr >> PAGE_SHIFT;
931 do {
932 BUG_ON(!pte_none(*pte));
933 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
934 set_pte(pte, pfn_pte(pfn, prot));
935 address += PAGE_SIZE;
936 pfn++;
937 pte++;
938 } while (address && (address < end));
941 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
942 unsigned long phys_addr, pgprot_t prot)
944 unsigned long base, end;
946 base = address & PGDIR_MASK;
947 address &= ~PGDIR_MASK;
948 end = address + size;
949 if (end > PGDIR_SIZE)
950 end = PGDIR_SIZE;
951 phys_addr -= address;
952 do {
953 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
954 if (!pte)
955 return -ENOMEM;
956 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
957 pte_unmap(pte);
958 address = (address + PMD_SIZE) & PMD_MASK;
959 pmd++;
960 } while (address && (address < end));
961 return 0;
964 /* Note: this is only safe if the mm semaphore is held when called. */
965 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
967 int error = 0;
968 pgd_t * dir;
969 unsigned long beg = from;
970 unsigned long end = from + size;
971 struct mm_struct *mm = vma->vm_mm;
973 phys_addr -= from;
974 dir = pgd_offset(mm, from);
975 flush_cache_range(vma, beg, end);
976 if (from >= end)
977 BUG();
979 spin_lock(&mm->page_table_lock);
980 do {
981 pmd_t *pmd = pmd_alloc(mm, dir, from);
982 error = -ENOMEM;
983 if (!pmd)
984 break;
985 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
986 if (error)
987 break;
988 from = (from + PGDIR_SIZE) & PGDIR_MASK;
989 dir++;
990 } while (from && (from < end));
992 * Why flush? remap_pte_range has a BUG_ON for !pte_none()
994 flush_tlb_range(vma, beg, end);
995 spin_unlock(&mm->page_table_lock);
996 return error;
999 EXPORT_SYMBOL(remap_page_range);
1002 * Do pte_mkwrite, but only if the vma says VM_WRITE. We do this when
1003 * servicing faults for write access. In the normal case, do always want
1004 * pte_mkwrite. But get_user_pages can cause write faults for mappings
1005 * that do not have writing enabled, when used by access_process_vm.
1007 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1009 if (likely(vma->vm_flags & VM_WRITE))
1010 pte = pte_mkwrite(pte);
1011 return pte;
1015 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1017 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
1018 pte_t *page_table)
1020 pte_t entry;
1022 flush_cache_page(vma, address);
1023 entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1024 vma);
1025 ptep_establish(vma, address, page_table, entry);
1026 update_mmu_cache(vma, address, entry);
1030 * This routine handles present pages, when users try to write
1031 * to a shared page. It is done by copying the page to a new address
1032 * and decrementing the shared-page counter for the old page.
1034 * Goto-purists beware: the only reason for goto's here is that it results
1035 * in better assembly code.. The "default" path will see no jumps at all.
1037 * Note that this routine assumes that the protection checks have been
1038 * done by the caller (the low-level page fault routine in most cases).
1039 * Thus we can safely just mark it writable once we've done any necessary
1040 * COW.
1042 * We also mark the page dirty at this point even though the page will
1043 * change only once the write actually happens. This avoids a few races,
1044 * and potentially makes it more efficient.
1046 * We hold the mm semaphore and the page_table_lock on entry and exit
1047 * with the page_table_lock released.
1049 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1050 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1052 struct page *old_page, *new_page;
1053 unsigned long pfn = pte_pfn(pte);
1054 pte_t entry;
1056 if (unlikely(!pfn_valid(pfn))) {
1058 * This should really halt the system so it can be debugged or
1059 * at least the kernel stops what it's doing before it corrupts
1060 * data, but for the moment just pretend this is OOM.
1062 pte_unmap(page_table);
1063 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1064 address);
1065 spin_unlock(&mm->page_table_lock);
1066 return VM_FAULT_OOM;
1068 old_page = pfn_to_page(pfn);
1070 if (!TestSetPageLocked(old_page)) {
1071 int reuse = can_share_swap_page(old_page);
1072 unlock_page(old_page);
1073 if (reuse) {
1074 flush_cache_page(vma, address);
1075 entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1076 vma);
1077 ptep_set_access_flags(vma, address, page_table, entry, 1);
1078 update_mmu_cache(vma, address, entry);
1079 pte_unmap(page_table);
1080 spin_unlock(&mm->page_table_lock);
1081 return VM_FAULT_MINOR;
1084 pte_unmap(page_table);
1087 * Ok, we need to copy. Oh, well..
1089 if (!PageReserved(old_page))
1090 page_cache_get(old_page);
1091 spin_unlock(&mm->page_table_lock);
1093 if (unlikely(anon_vma_prepare(vma)))
1094 goto no_new_page;
1095 new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1096 if (!new_page)
1097 goto no_new_page;
1098 copy_cow_page(old_page,new_page,address);
1101 * Re-check the pte - we dropped the lock
1103 spin_lock(&mm->page_table_lock);
1104 page_table = pte_offset_map(pmd, address);
1105 if (likely(pte_same(*page_table, pte))) {
1106 if (PageReserved(old_page))
1107 ++mm->rss;
1108 else
1109 page_remove_rmap(old_page);
1110 break_cow(vma, new_page, address, page_table);
1111 lru_cache_add_active(new_page);
1112 page_add_anon_rmap(new_page, vma, address);
1114 /* Free the old page.. */
1115 new_page = old_page;
1117 pte_unmap(page_table);
1118 page_cache_release(new_page);
1119 page_cache_release(old_page);
1120 spin_unlock(&mm->page_table_lock);
1121 return VM_FAULT_MINOR;
1123 no_new_page:
1124 page_cache_release(old_page);
1125 return VM_FAULT_OOM;
1129 * Helper function for unmap_mapping_range().
1131 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1132 struct zap_details *details)
1134 struct vm_area_struct *vma;
1135 struct prio_tree_iter iter;
1136 pgoff_t vba, vea, zba, zea;
1138 vma_prio_tree_foreach(vma, &iter, root,
1139 details->first_index, details->last_index) {
1140 vba = vma->vm_pgoff;
1141 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1142 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1143 zba = details->first_index;
1144 if (zba < vba)
1145 zba = vba;
1146 zea = details->last_index;
1147 if (zea > vea)
1148 zea = vea;
1149 zap_page_range(vma,
1150 ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1151 (zea - zba + 1) << PAGE_SHIFT, details);
1156 * unmap_mapping_range - unmap the portion of all mmaps
1157 * in the specified address_space corresponding to the specified
1158 * page range in the underlying file.
1159 * @address_space: the address space containing mmaps to be unmapped.
1160 * @holebegin: byte in first page to unmap, relative to the start of
1161 * the underlying file. This will be rounded down to a PAGE_SIZE
1162 * boundary. Note that this is different from vmtruncate(), which
1163 * must keep the partial page. In contrast, we must get rid of
1164 * partial pages.
1165 * @holelen: size of prospective hole in bytes. This will be rounded
1166 * up to a PAGE_SIZE boundary. A holelen of zero truncates to the
1167 * end of the file.
1168 * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1169 * but 0 when invalidating pagecache, don't throw away private data.
1171 void unmap_mapping_range(struct address_space *mapping,
1172 loff_t const holebegin, loff_t const holelen, int even_cows)
1174 struct zap_details details;
1175 pgoff_t hba = holebegin >> PAGE_SHIFT;
1176 pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1178 /* Check for overflow. */
1179 if (sizeof(holelen) > sizeof(hlen)) {
1180 long long holeend =
1181 (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1182 if (holeend & ~(long long)ULONG_MAX)
1183 hlen = ULONG_MAX - hba + 1;
1186 details.check_mapping = even_cows? NULL: mapping;
1187 details.nonlinear_vma = NULL;
1188 details.first_index = hba;
1189 details.last_index = hba + hlen - 1;
1190 details.atomic = 1; /* A spinlock is held */
1191 if (details.last_index < details.first_index)
1192 details.last_index = ULONG_MAX;
1194 spin_lock(&mapping->i_mmap_lock);
1195 /* Protect against page fault */
1196 atomic_inc(&mapping->truncate_count);
1198 if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1199 unmap_mapping_range_list(&mapping->i_mmap, &details);
1202 * In nonlinear VMAs there is no correspondence between virtual address
1203 * offset and file offset. So we must perform an exhaustive search
1204 * across *all* the pages in each nonlinear VMA, not just the pages
1205 * whose virtual address lies outside the file truncation point.
1207 if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1208 struct vm_area_struct *vma;
1209 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1210 shared.vm_set.list) {
1211 details.nonlinear_vma = vma;
1212 zap_page_range(vma, vma->vm_start,
1213 vma->vm_end - vma->vm_start, &details);
1216 spin_unlock(&mapping->i_mmap_lock);
1218 EXPORT_SYMBOL(unmap_mapping_range);
1221 * Handle all mappings that got truncated by a "truncate()"
1222 * system call.
1224 * NOTE! We have to be ready to update the memory sharing
1225 * between the file and the memory map for a potential last
1226 * incomplete page. Ugly, but necessary.
1228 int vmtruncate(struct inode * inode, loff_t offset)
1230 struct address_space *mapping = inode->i_mapping;
1231 unsigned long limit;
1233 if (inode->i_size < offset)
1234 goto do_expand;
1236 * truncation of in-use swapfiles is disallowed - it would cause
1237 * subsequent swapout to scribble on the now-freed blocks.
1239 if (IS_SWAPFILE(inode))
1240 goto out_busy;
1241 i_size_write(inode, offset);
1242 unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1243 truncate_inode_pages(mapping, offset);
1244 goto out_truncate;
1246 do_expand:
1247 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1248 if (limit != RLIM_INFINITY && offset > limit)
1249 goto out_sig;
1250 if (offset > inode->i_sb->s_maxbytes)
1251 goto out_big;
1252 i_size_write(inode, offset);
1254 out_truncate:
1255 if (inode->i_op && inode->i_op->truncate)
1256 inode->i_op->truncate(inode);
1257 return 0;
1258 out_sig:
1259 send_sig(SIGXFSZ, current, 0);
1260 out_big:
1261 return -EFBIG;
1262 out_busy:
1263 return -ETXTBSY;
1266 EXPORT_SYMBOL(vmtruncate);
1269 * Primitive swap readahead code. We simply read an aligned block of
1270 * (1 << page_cluster) entries in the swap area. This method is chosen
1271 * because it doesn't cost us any seek time. We also make sure to queue
1272 * the 'original' request together with the readahead ones...
1274 * This has been extended to use the NUMA policies from the mm triggering
1275 * the readahead.
1277 * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1279 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1281 #ifdef CONFIG_NUMA
1282 struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1283 #endif
1284 int i, num;
1285 struct page *new_page;
1286 unsigned long offset;
1289 * Get the number of handles we should do readahead io to.
1291 num = valid_swaphandles(entry, &offset);
1292 for (i = 0; i < num; offset++, i++) {
1293 /* Ok, do the async read-ahead now */
1294 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1295 offset), vma, addr);
1296 if (!new_page)
1297 break;
1298 page_cache_release(new_page);
1299 #ifdef CONFIG_NUMA
1301 * Find the next applicable VMA for the NUMA policy.
1303 addr += PAGE_SIZE;
1304 if (addr == 0)
1305 vma = NULL;
1306 if (vma) {
1307 if (addr >= vma->vm_end) {
1308 vma = next_vma;
1309 next_vma = vma ? vma->vm_next : NULL;
1311 if (vma && addr < vma->vm_start)
1312 vma = NULL;
1313 } else {
1314 if (next_vma && addr >= next_vma->vm_start) {
1315 vma = next_vma;
1316 next_vma = vma->vm_next;
1319 #endif
1321 lru_add_drain(); /* Push any new pages onto the LRU now */
1325 * We hold the mm semaphore and the page_table_lock on entry and
1326 * should release the pagetable lock on exit..
1328 static int do_swap_page(struct mm_struct * mm,
1329 struct vm_area_struct * vma, unsigned long address,
1330 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1332 struct page *page;
1333 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1334 pte_t pte;
1335 int ret = VM_FAULT_MINOR;
1337 pte_unmap(page_table);
1338 spin_unlock(&mm->page_table_lock);
1339 page = lookup_swap_cache(entry);
1340 if (!page) {
1341 swapin_readahead(entry, address, vma);
1342 page = read_swap_cache_async(entry, vma, address);
1343 if (!page) {
1345 * Back out if somebody else faulted in this pte while
1346 * we released the page table lock.
1348 spin_lock(&mm->page_table_lock);
1349 page_table = pte_offset_map(pmd, address);
1350 if (likely(pte_same(*page_table, orig_pte)))
1351 ret = VM_FAULT_OOM;
1352 else
1353 ret = VM_FAULT_MINOR;
1354 pte_unmap(page_table);
1355 spin_unlock(&mm->page_table_lock);
1356 goto out;
1359 /* Had to read the page from swap area: Major fault */
1360 ret = VM_FAULT_MAJOR;
1361 inc_page_state(pgmajfault);
1362 grab_swap_token();
1365 mark_page_accessed(page);
1366 lock_page(page);
1369 * Back out if somebody else faulted in this pte while we
1370 * released the page table lock.
1372 spin_lock(&mm->page_table_lock);
1373 page_table = pte_offset_map(pmd, address);
1374 if (unlikely(!pte_same(*page_table, orig_pte))) {
1375 pte_unmap(page_table);
1376 spin_unlock(&mm->page_table_lock);
1377 unlock_page(page);
1378 page_cache_release(page);
1379 ret = VM_FAULT_MINOR;
1380 goto out;
1383 /* The page isn't present yet, go ahead with the fault. */
1385 swap_free(entry);
1386 if (vm_swap_full())
1387 remove_exclusive_swap_page(page);
1389 mm->rss++;
1390 pte = mk_pte(page, vma->vm_page_prot);
1391 if (write_access && can_share_swap_page(page)) {
1392 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1393 write_access = 0;
1395 unlock_page(page);
1397 flush_icache_page(vma, page);
1398 set_pte(page_table, pte);
1399 page_add_anon_rmap(page, vma, address);
1401 if (write_access) {
1402 if (do_wp_page(mm, vma, address,
1403 page_table, pmd, pte) == VM_FAULT_OOM)
1404 ret = VM_FAULT_OOM;
1405 goto out;
1408 /* No need to invalidate - it was non-present before */
1409 update_mmu_cache(vma, address, pte);
1410 pte_unmap(page_table);
1411 spin_unlock(&mm->page_table_lock);
1412 out:
1413 return ret;
1417 * We are called with the MM semaphore and page_table_lock
1418 * spinlock held to protect against concurrent faults in
1419 * multithreaded programs.
1421 static int
1422 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1423 pte_t *page_table, pmd_t *pmd, int write_access,
1424 unsigned long addr)
1426 pte_t entry;
1427 struct page * page = ZERO_PAGE(addr);
1429 /* Read-only mapping of ZERO_PAGE. */
1430 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1432 /* ..except if it's a write access */
1433 if (write_access) {
1434 /* Allocate our own private page. */
1435 pte_unmap(page_table);
1436 spin_unlock(&mm->page_table_lock);
1438 if (unlikely(anon_vma_prepare(vma)))
1439 goto no_mem;
1440 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1441 if (!page)
1442 goto no_mem;
1443 clear_user_highpage(page, addr);
1445 spin_lock(&mm->page_table_lock);
1446 page_table = pte_offset_map(pmd, addr);
1448 if (!pte_none(*page_table)) {
1449 pte_unmap(page_table);
1450 page_cache_release(page);
1451 spin_unlock(&mm->page_table_lock);
1452 goto out;
1454 mm->rss++;
1455 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1456 vma->vm_page_prot)),
1457 vma);
1458 lru_cache_add_active(page);
1459 mark_page_accessed(page);
1460 page_add_anon_rmap(page, vma, addr);
1463 set_pte(page_table, entry);
1464 pte_unmap(page_table);
1466 /* No need to invalidate - it was non-present before */
1467 update_mmu_cache(vma, addr, entry);
1468 spin_unlock(&mm->page_table_lock);
1469 out:
1470 return VM_FAULT_MINOR;
1471 no_mem:
1472 return VM_FAULT_OOM;
1476 * do_no_page() tries to create a new page mapping. It aggressively
1477 * tries to share with existing pages, but makes a separate copy if
1478 * the "write_access" parameter is true in order to avoid the next
1479 * page fault.
1481 * As this is called only for pages that do not currently exist, we
1482 * do not need to flush old virtual caches or the TLB.
1484 * This is called with the MM semaphore held and the page table
1485 * spinlock held. Exit with the spinlock released.
1487 static int
1488 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1489 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1491 struct page * new_page;
1492 struct address_space *mapping = NULL;
1493 pte_t entry;
1494 int sequence = 0;
1495 int ret = VM_FAULT_MINOR;
1496 int anon = 0;
1498 if (!vma->vm_ops || !vma->vm_ops->nopage)
1499 return do_anonymous_page(mm, vma, page_table,
1500 pmd, write_access, address);
1501 pte_unmap(page_table);
1502 spin_unlock(&mm->page_table_lock);
1504 if (vma->vm_file) {
1505 mapping = vma->vm_file->f_mapping;
1506 sequence = atomic_read(&mapping->truncate_count);
1508 smp_rmb(); /* Prevent CPU from reordering lock-free ->nopage() */
1509 retry:
1510 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1512 /* no page was available -- either SIGBUS or OOM */
1513 if (new_page == NOPAGE_SIGBUS)
1514 return VM_FAULT_SIGBUS;
1515 if (new_page == NOPAGE_OOM)
1516 return VM_FAULT_OOM;
1519 * Should we do an early C-O-W break?
1521 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1522 struct page *page;
1524 if (unlikely(anon_vma_prepare(vma)))
1525 goto oom;
1526 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1527 if (!page)
1528 goto oom;
1529 copy_user_highpage(page, new_page, address);
1530 page_cache_release(new_page);
1531 new_page = page;
1532 anon = 1;
1535 spin_lock(&mm->page_table_lock);
1537 * For a file-backed vma, someone could have truncated or otherwise
1538 * invalidated this page. If unmap_mapping_range got called,
1539 * retry getting the page.
1541 if (mapping &&
1542 (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1543 sequence = atomic_read(&mapping->truncate_count);
1544 spin_unlock(&mm->page_table_lock);
1545 page_cache_release(new_page);
1546 goto retry;
1548 page_table = pte_offset_map(pmd, address);
1551 * This silly early PAGE_DIRTY setting removes a race
1552 * due to the bad i386 page protection. But it's valid
1553 * for other architectures too.
1555 * Note that if write_access is true, we either now have
1556 * an exclusive copy of the page, or this is a shared mapping,
1557 * so we can make it writable and dirty to avoid having to
1558 * handle that later.
1560 /* Only go through if we didn't race with anybody else... */
1561 if (pte_none(*page_table)) {
1562 if (!PageReserved(new_page))
1563 ++mm->rss;
1564 flush_icache_page(vma, new_page);
1565 entry = mk_pte(new_page, vma->vm_page_prot);
1566 if (write_access)
1567 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1568 set_pte(page_table, entry);
1569 if (anon) {
1570 lru_cache_add_active(new_page);
1571 page_add_anon_rmap(new_page, vma, address);
1572 } else
1573 page_add_file_rmap(new_page);
1574 pte_unmap(page_table);
1575 } else {
1576 /* One of our sibling threads was faster, back out. */
1577 pte_unmap(page_table);
1578 page_cache_release(new_page);
1579 spin_unlock(&mm->page_table_lock);
1580 goto out;
1583 /* no need to invalidate: a not-present page shouldn't be cached */
1584 update_mmu_cache(vma, address, entry);
1585 spin_unlock(&mm->page_table_lock);
1586 out:
1587 return ret;
1588 oom:
1589 page_cache_release(new_page);
1590 ret = VM_FAULT_OOM;
1591 goto out;
1595 * Fault of a previously existing named mapping. Repopulate the pte
1596 * from the encoded file_pte if possible. This enables swappable
1597 * nonlinear vmas.
1599 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1600 unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1602 unsigned long pgoff;
1603 int err;
1605 BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1607 * Fall back to the linear mapping if the fs does not support
1608 * ->populate:
1610 if (!vma->vm_ops || !vma->vm_ops->populate ||
1611 (write_access && !(vma->vm_flags & VM_SHARED))) {
1612 pte_clear(pte);
1613 return do_no_page(mm, vma, address, write_access, pte, pmd);
1616 pgoff = pte_to_pgoff(*pte);
1618 pte_unmap(pte);
1619 spin_unlock(&mm->page_table_lock);
1621 err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1622 if (err == -ENOMEM)
1623 return VM_FAULT_OOM;
1624 if (err)
1625 return VM_FAULT_SIGBUS;
1626 return VM_FAULT_MAJOR;
1630 * These routines also need to handle stuff like marking pages dirty
1631 * and/or accessed for architectures that don't do it in hardware (most
1632 * RISC architectures). The early dirtying is also good on the i386.
1634 * There is also a hook called "update_mmu_cache()" that architectures
1635 * with external mmu caches can use to update those (ie the Sparc or
1636 * PowerPC hashed page tables that act as extended TLBs).
1638 * Note the "page_table_lock". It is to protect against kswapd removing
1639 * pages from under us. Note that kswapd only ever _removes_ pages, never
1640 * adds them. As such, once we have noticed that the page is not present,
1641 * we can drop the lock early.
1643 * The adding of pages is protected by the MM semaphore (which we hold),
1644 * so we don't need to worry about a page being suddenly been added into
1645 * our VM.
1647 * We enter with the pagetable spinlock held, we are supposed to
1648 * release it when done.
1650 static inline int handle_pte_fault(struct mm_struct *mm,
1651 struct vm_area_struct * vma, unsigned long address,
1652 int write_access, pte_t *pte, pmd_t *pmd)
1654 pte_t entry;
1656 entry = *pte;
1657 if (!pte_present(entry)) {
1659 * If it truly wasn't present, we know that kswapd
1660 * and the PTE updates will not touch it later. So
1661 * drop the lock.
1663 if (pte_none(entry))
1664 return do_no_page(mm, vma, address, write_access, pte, pmd);
1665 if (pte_file(entry))
1666 return do_file_page(mm, vma, address, write_access, pte, pmd);
1667 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1670 if (write_access) {
1671 if (!pte_write(entry))
1672 return do_wp_page(mm, vma, address, pte, pmd, entry);
1674 entry = pte_mkdirty(entry);
1676 entry = pte_mkyoung(entry);
1677 ptep_set_access_flags(vma, address, pte, entry, write_access);
1678 update_mmu_cache(vma, address, entry);
1679 pte_unmap(pte);
1680 spin_unlock(&mm->page_table_lock);
1681 return VM_FAULT_MINOR;
1685 * By the time we get here, we already hold the mm semaphore
1687 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1688 unsigned long address, int write_access)
1690 pgd_t *pgd;
1691 pmd_t *pmd;
1693 __set_current_state(TASK_RUNNING);
1694 pgd = pgd_offset(mm, address);
1696 inc_page_state(pgfault);
1698 if (is_vm_hugetlb_page(vma))
1699 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1702 * We need the page table lock to synchronize with kswapd
1703 * and the SMP-safe atomic PTE updates.
1705 spin_lock(&mm->page_table_lock);
1706 pmd = pmd_alloc(mm, pgd, address);
1708 if (pmd) {
1709 pte_t * pte = pte_alloc_map(mm, pmd, address);
1710 if (pte)
1711 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1713 spin_unlock(&mm->page_table_lock);
1714 return VM_FAULT_OOM;
1718 * Allocate page middle directory.
1720 * We've already handled the fast-path in-line, and we own the
1721 * page table lock.
1723 * On a two-level page table, this ends up actually being entirely
1724 * optimized away.
1726 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1728 pmd_t *new;
1730 spin_unlock(&mm->page_table_lock);
1731 new = pmd_alloc_one(mm, address);
1732 spin_lock(&mm->page_table_lock);
1733 if (!new)
1734 return NULL;
1737 * Because we dropped the lock, we should re-check the
1738 * entry, as somebody else could have populated it..
1740 if (pgd_present(*pgd)) {
1741 pmd_free(new);
1742 goto out;
1744 pgd_populate(mm, pgd, new);
1745 out:
1746 return pmd_offset(pgd, address);
1749 int make_pages_present(unsigned long addr, unsigned long end)
1751 int ret, len, write;
1752 struct vm_area_struct * vma;
1754 vma = find_vma(current->mm, addr);
1755 if (!vma)
1756 return -1;
1757 write = (vma->vm_flags & VM_WRITE) != 0;
1758 if (addr >= end)
1759 BUG();
1760 if (end > vma->vm_end)
1761 BUG();
1762 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1763 ret = get_user_pages(current, current->mm, addr,
1764 len, write, 0, NULL, NULL);
1765 if (ret < 0)
1766 return ret;
1767 return ret == len ? 0 : -1;
1771 * Map a vmalloc()-space virtual address to the physical page.
1773 struct page * vmalloc_to_page(void * vmalloc_addr)
1775 unsigned long addr = (unsigned long) vmalloc_addr;
1776 struct page *page = NULL;
1777 pgd_t *pgd = pgd_offset_k(addr);
1778 pmd_t *pmd;
1779 pte_t *ptep, pte;
1781 if (!pgd_none(*pgd)) {
1782 pmd = pmd_offset(pgd, addr);
1783 if (!pmd_none(*pmd)) {
1784 preempt_disable();
1785 ptep = pte_offset_map(pmd, addr);
1786 pte = *ptep;
1787 if (pte_present(pte))
1788 page = pte_page(pte);
1789 pte_unmap(ptep);
1790 preempt_enable();
1793 return page;
1796 EXPORT_SYMBOL(vmalloc_to_page);
1798 #if !defined(CONFIG_ARCH_GATE_AREA)
1800 #if defined(AT_SYSINFO_EHDR)
1801 struct vm_area_struct gate_vma;
1803 static int __init gate_vma_init(void)
1805 gate_vma.vm_mm = NULL;
1806 gate_vma.vm_start = FIXADDR_USER_START;
1807 gate_vma.vm_end = FIXADDR_USER_END;
1808 gate_vma.vm_page_prot = PAGE_READONLY;
1809 gate_vma.vm_flags = 0;
1810 return 0;
1812 __initcall(gate_vma_init);
1813 #endif
1815 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1817 #ifdef AT_SYSINFO_EHDR
1818 return &gate_vma;
1819 #else
1820 return NULL;
1821 #endif
1824 int in_gate_area(struct task_struct *task, unsigned long addr)
1826 #ifdef AT_SYSINFO_EHDR
1827 if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
1828 return 1;
1829 #endif
1830 return 0;
1833 #endif