[PATCH] C99 designated initializers for arch/sh
[linux-2.6/history.git] / mm / memory.c
blob70403c0cb90216e9b2f7a5b401a9127c4119f026
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/mman.h>
42 #include <linux/swap.h>
43 #include <linux/highmem.h>
44 #include <linux/pagemap.h>
45 #include <linux/vcache.h>
47 #include <asm/pgalloc.h>
48 #include <asm/rmap.h>
49 #include <asm/uaccess.h>
50 #include <asm/tlb.h>
51 #include <asm/tlbflush.h>
53 #include <linux/swapops.h>
55 #ifndef CONFIG_DISCONTIGMEM
56 /* use the per-pgdat data instead for discontigmem - mbligh */
57 unsigned long max_mapnr;
58 struct page *mem_map;
59 #endif
61 unsigned long num_physpages;
62 void * high_memory;
63 struct page *highmem_start_page;
66 * We special-case the C-O-W ZERO_PAGE, because it's such
67 * a common occurrence (no need to read the page to know
68 * that it's zero - better for the cache and memory subsystem).
70 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
72 if (from == ZERO_PAGE(address)) {
73 clear_user_highpage(to, address);
74 return;
76 copy_user_highpage(to, from, address);
80 * Note: this doesn't free the actual pages themselves. That
81 * has been handled earlier when unmapping all the memory regions.
83 static inline void free_one_pmd(mmu_gather_t *tlb, pmd_t * dir)
85 struct page *page;
87 if (pmd_none(*dir))
88 return;
89 if (pmd_bad(*dir)) {
90 pmd_ERROR(*dir);
91 pmd_clear(dir);
92 return;
94 page = pmd_page(*dir);
95 pmd_clear(dir);
96 pgtable_remove_rmap(page);
97 pte_free_tlb(tlb, page);
100 static inline void free_one_pgd(mmu_gather_t *tlb, pgd_t * dir)
102 int j;
103 pmd_t * pmd;
105 if (pgd_none(*dir))
106 return;
107 if (pgd_bad(*dir)) {
108 pgd_ERROR(*dir);
109 pgd_clear(dir);
110 return;
112 pmd = pmd_offset(dir, 0);
113 pgd_clear(dir);
114 for (j = 0; j < PTRS_PER_PMD ; j++) {
115 prefetchw(pmd+j+(PREFETCH_STRIDE/16));
116 free_one_pmd(tlb, pmd+j);
118 pmd_free_tlb(tlb, pmd);
122 * This function clears all user-level page tables of a process - this
123 * is needed by execve(), so that old pages aren't in the way.
125 * Must be called with pagetable lock held.
127 void clear_page_tables(mmu_gather_t *tlb, unsigned long first, int nr)
129 pgd_t * page_dir = tlb->mm->pgd;
131 page_dir += first;
132 do {
133 free_one_pgd(tlb, page_dir);
134 page_dir++;
135 } while (--nr);
138 pte_t * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
140 if (!pmd_present(*pmd)) {
141 struct page *new;
143 spin_unlock(&mm->page_table_lock);
144 new = pte_alloc_one(mm, address);
145 spin_lock(&mm->page_table_lock);
146 if (!new)
147 return NULL;
150 * Because we dropped the lock, we should re-check the
151 * entry, as somebody else could have populated it..
153 if (pmd_present(*pmd)) {
154 pte_free(new);
155 goto out;
157 pgtable_add_rmap(new, mm, address);
158 pmd_populate(mm, pmd, new);
160 out:
161 if (pmd_present(*pmd))
162 return pte_offset_map(pmd, address);
163 return NULL;
166 pte_t * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
168 if (!pmd_present(*pmd)) {
169 pte_t *new;
171 spin_unlock(&mm->page_table_lock);
172 new = pte_alloc_one_kernel(mm, address);
173 spin_lock(&mm->page_table_lock);
174 if (!new)
175 return NULL;
178 * Because we dropped the lock, we should re-check the
179 * entry, as somebody else could have populated it..
181 if (pmd_present(*pmd)) {
182 pte_free_kernel(new);
183 goto out;
185 pgtable_add_rmap(virt_to_page(new), mm, address);
186 pmd_populate_kernel(mm, pmd, new);
188 out:
189 return pte_offset_kernel(pmd, address);
191 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
192 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
195 * copy one vm_area from one task to the other. Assumes the page tables
196 * already present in the new task to be cleared in the whole range
197 * covered by this vma.
199 * 08Jan98 Merged into one routine from several inline routines to reduce
200 * variable count and make things faster. -jj
202 * dst->page_table_lock is held on entry and exit,
203 * but may be dropped within pmd_alloc() and pte_alloc_map().
205 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
206 struct vm_area_struct *vma)
208 pgd_t * src_pgd, * dst_pgd;
209 unsigned long address = vma->vm_start;
210 unsigned long end = vma->vm_end;
211 unsigned long cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
213 if (is_vm_hugetlb_page(vma))
214 return copy_hugetlb_page_range(dst, src, vma);
216 src_pgd = pgd_offset(src, address)-1;
217 dst_pgd = pgd_offset(dst, address)-1;
219 for (;;) {
220 pmd_t * src_pmd, * dst_pmd;
222 src_pgd++; dst_pgd++;
224 /* copy_pmd_range */
226 if (pgd_none(*src_pgd))
227 goto skip_copy_pmd_range;
228 if (pgd_bad(*src_pgd)) {
229 pgd_ERROR(*src_pgd);
230 pgd_clear(src_pgd);
231 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
232 if (!address || (address >= end))
233 goto out;
234 continue;
237 src_pmd = pmd_offset(src_pgd, address);
238 dst_pmd = pmd_alloc(dst, dst_pgd, address);
239 if (!dst_pmd)
240 goto nomem;
242 do {
243 pte_t * src_pte, * dst_pte;
245 /* copy_pte_range */
247 if (pmd_none(*src_pmd))
248 goto skip_copy_pte_range;
249 if (pmd_bad(*src_pmd)) {
250 pmd_ERROR(*src_pmd);
251 pmd_clear(src_pmd);
252 skip_copy_pte_range: address = (address + PMD_SIZE) & PMD_MASK;
253 if (address >= end)
254 goto out;
255 goto cont_copy_pmd_range;
258 dst_pte = pte_alloc_map(dst, dst_pmd, address);
259 if (!dst_pte)
260 goto nomem;
261 spin_lock(&src->page_table_lock);
262 src_pte = pte_offset_map_nested(src_pmd, address);
263 do {
264 pte_t pte = *src_pte;
265 struct page *ptepage;
266 unsigned long pfn;
268 /* copy_one_pte */
270 if (pte_none(pte))
271 goto cont_copy_pte_range_noset;
272 /* pte contains position in swap, so copy. */
273 if (!pte_present(pte)) {
274 swap_duplicate(pte_to_swp_entry(pte));
275 set_pte(dst_pte, pte);
276 goto cont_copy_pte_range_noset;
278 ptepage = pte_page(pte);
279 pfn = pte_pfn(pte);
280 if (!pfn_valid(pfn))
281 goto cont_copy_pte_range;
282 ptepage = pfn_to_page(pfn);
283 if (PageReserved(ptepage))
284 goto cont_copy_pte_range;
286 /* If it's a COW mapping, write protect it both in the parent and the child */
287 if (cow) {
288 ptep_set_wrprotect(src_pte);
289 pte = *src_pte;
292 /* If it's a shared mapping, mark it clean in the child */
293 if (vma->vm_flags & VM_SHARED)
294 pte = pte_mkclean(pte);
295 pte = pte_mkold(pte);
296 get_page(ptepage);
297 dst->rss++;
299 cont_copy_pte_range: set_pte(dst_pte, pte);
300 page_add_rmap(ptepage, dst_pte);
301 cont_copy_pte_range_noset: address += PAGE_SIZE;
302 if (address >= end) {
303 pte_unmap_nested(src_pte);
304 pte_unmap(dst_pte);
305 goto out_unlock;
307 src_pte++;
308 dst_pte++;
309 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
310 pte_unmap_nested(src_pte-1);
311 pte_unmap(dst_pte-1);
312 spin_unlock(&src->page_table_lock);
314 cont_copy_pmd_range: src_pmd++;
315 dst_pmd++;
316 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
318 out_unlock:
319 spin_unlock(&src->page_table_lock);
320 out:
321 return 0;
322 nomem:
323 return -ENOMEM;
326 static void zap_pte_range(mmu_gather_t *tlb, pmd_t * pmd, unsigned long address, unsigned long size)
328 unsigned long offset;
329 pte_t *ptep;
331 if (pmd_none(*pmd))
332 return;
333 if (pmd_bad(*pmd)) {
334 pmd_ERROR(*pmd);
335 pmd_clear(pmd);
336 return;
338 ptep = pte_offset_map(pmd, address);
339 offset = address & ~PMD_MASK;
340 if (offset + size > PMD_SIZE)
341 size = PMD_SIZE - offset;
342 size &= PAGE_MASK;
343 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
344 pte_t pte = *ptep;
345 if (pte_none(pte))
346 continue;
347 if (pte_present(pte)) {
348 unsigned long pfn = pte_pfn(pte);
350 pte = ptep_get_and_clear(ptep);
351 tlb_remove_tlb_entry(tlb, ptep, address+offset);
352 if (pfn_valid(pfn)) {
353 struct page *page = pfn_to_page(pfn);
354 if (!PageReserved(page)) {
355 if (pte_dirty(pte))
356 set_page_dirty(page);
357 if (page->mapping && pte_young(pte) &&
358 !PageSwapCache(page))
359 mark_page_accessed(page);
360 tlb->freed++;
361 page_remove_rmap(page, ptep);
362 tlb_remove_page(tlb, page);
365 } else {
366 free_swap_and_cache(pte_to_swp_entry(pte));
367 pte_clear(ptep);
370 pte_unmap(ptep-1);
373 static void zap_pmd_range(mmu_gather_t *tlb, pgd_t * dir, unsigned long address, unsigned long size)
375 pmd_t * pmd;
376 unsigned long end;
378 if (pgd_none(*dir))
379 return;
380 if (pgd_bad(*dir)) {
381 pgd_ERROR(*dir);
382 pgd_clear(dir);
383 return;
385 pmd = pmd_offset(dir, address);
386 end = address + size;
387 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
388 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
389 do {
390 zap_pte_range(tlb, pmd, address, end - address);
391 address = (address + PMD_SIZE) & PMD_MASK;
392 pmd++;
393 } while (address < end);
396 void unmap_page_range(mmu_gather_t *tlb, struct vm_area_struct *vma, unsigned long address, unsigned long end)
398 pgd_t * dir;
400 BUG_ON(address >= end);
402 dir = pgd_offset(vma->vm_mm, address);
403 tlb_start_vma(tlb, vma);
404 do {
405 zap_pmd_range(tlb, dir, address, end - address);
406 address = (address + PGDIR_SIZE) & PGDIR_MASK;
407 dir++;
408 } while (address && (address < end));
409 tlb_end_vma(tlb, vma);
412 /* Dispose of an entire mmu_gather_t per rescheduling point */
413 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
414 #define ZAP_BLOCK_SIZE (FREE_PTE_NR * PAGE_SIZE)
415 #endif
417 /* For UP, 256 pages at a time gives nice low latency */
418 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
419 #define ZAP_BLOCK_SIZE (256 * PAGE_SIZE)
420 #endif
422 /* No preempt: go for the best straight-line efficiency */
423 #if !defined(CONFIG_PREEMPT)
424 #define ZAP_BLOCK_SIZE (~(0UL))
425 #endif
428 * zap_page_range - remove user pages in a given range
429 * @vma: vm_area_struct holding the applicable pages
430 * @address: starting address of pages to zap
431 * @size: number of bytes to zap
433 void zap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size)
435 struct mm_struct *mm = vma->vm_mm;
436 mmu_gather_t *tlb;
437 unsigned long end, block;
439 spin_lock(&mm->page_table_lock);
442 * This was once a long-held spinlock. Now we break the
443 * work up into ZAP_BLOCK_SIZE units and relinquish the
444 * lock after each interation. This drastically lowers
445 * lock contention and allows for a preemption point.
447 while (size) {
448 block = (size > ZAP_BLOCK_SIZE) ? ZAP_BLOCK_SIZE : size;
449 end = address + block;
451 flush_cache_range(vma, address, end);
452 tlb = tlb_gather_mmu(mm, 0);
453 unmap_page_range(tlb, vma, address, end);
454 tlb_finish_mmu(tlb, address, end);
456 cond_resched_lock(&mm->page_table_lock);
458 address += block;
459 size -= block;
462 spin_unlock(&mm->page_table_lock);
466 * Do a quick page-table lookup for a single page.
467 * mm->page_table_lock must be held.
469 struct page *
470 follow_page(struct mm_struct *mm, unsigned long address, int write)
472 pgd_t *pgd;
473 pmd_t *pmd;
474 pte_t *ptep, pte;
475 unsigned long pfn;
477 pgd = pgd_offset(mm, address);
478 if (pgd_none(*pgd) || pgd_bad(*pgd))
479 goto out;
481 pmd = pmd_offset(pgd, address);
482 if (pmd_none(*pmd) || pmd_bad(*pmd))
483 goto out;
485 ptep = pte_offset_map(pmd, address);
486 if (!ptep)
487 goto out;
489 pte = *ptep;
490 pte_unmap(ptep);
491 if (pte_present(pte)) {
492 if (!write || (pte_write(pte) && pte_dirty(pte))) {
493 pfn = pte_pfn(pte);
494 if (pfn_valid(pfn))
495 return pfn_to_page(pfn);
499 out:
500 return NULL;
504 * Given a physical address, is there a useful struct page pointing to
505 * it? This may become more complex in the future if we start dealing
506 * with IO-aperture pages for direct-IO.
509 static inline struct page *get_page_map(struct page *page)
511 if (!pfn_valid(page_to_pfn(page)))
512 return 0;
513 return page;
517 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
518 unsigned long start, int len, int write, int force,
519 struct page **pages, struct vm_area_struct **vmas)
521 int i;
522 unsigned int flags;
525 * Require read or write permissions.
526 * If 'force' is set, we only require the "MAY" flags.
528 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
529 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
530 i = 0;
532 do {
533 struct vm_area_struct * vma;
535 vma = find_extend_vma(mm, start);
537 if (!vma || (pages && (vma->vm_flags & VM_IO))
538 || !(flags & vma->vm_flags))
539 return i ? : -EFAULT;
541 if (is_vm_hugetlb_page(vma)) {
542 i = follow_hugetlb_page(mm, vma, pages, vmas,
543 &start, &len, i);
544 continue;
546 spin_lock(&mm->page_table_lock);
547 do {
548 struct page *map;
549 while (!(map = follow_page(mm, start, write))) {
550 spin_unlock(&mm->page_table_lock);
551 switch (handle_mm_fault(mm,vma,start,write)) {
552 case VM_FAULT_MINOR:
553 tsk->min_flt++;
554 break;
555 case VM_FAULT_MAJOR:
556 tsk->maj_flt++;
557 break;
558 case VM_FAULT_SIGBUS:
559 return i ? i : -EFAULT;
560 case VM_FAULT_OOM:
561 return i ? i : -ENOMEM;
562 default:
563 BUG();
565 spin_lock(&mm->page_table_lock);
567 if (pages) {
568 pages[i] = get_page_map(map);
569 if (!pages[i]) {
570 spin_unlock(&mm->page_table_lock);
571 while (i--)
572 page_cache_release(pages[i]);
573 i = -EFAULT;
574 goto out;
576 if (!PageReserved(pages[i]))
577 page_cache_get(pages[i]);
579 if (vmas)
580 vmas[i] = vma;
581 i++;
582 start += PAGE_SIZE;
583 len--;
584 } while(len && start < vma->vm_end);
585 spin_unlock(&mm->page_table_lock);
586 } while(len);
587 out:
588 return i;
591 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
592 unsigned long size, pgprot_t prot)
594 unsigned long end;
596 address &= ~PMD_MASK;
597 end = address + size;
598 if (end > PMD_SIZE)
599 end = PMD_SIZE;
600 do {
601 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
602 BUG_ON(!pte_none(*pte));
603 set_pte(pte, zero_pte);
604 address += PAGE_SIZE;
605 pte++;
606 } while (address && (address < end));
609 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
610 unsigned long size, pgprot_t prot)
612 unsigned long end;
614 address &= ~PGDIR_MASK;
615 end = address + size;
616 if (end > PGDIR_SIZE)
617 end = PGDIR_SIZE;
618 do {
619 pte_t * pte = pte_alloc_map(mm, pmd, address);
620 if (!pte)
621 return -ENOMEM;
622 zeromap_pte_range(pte, address, end - address, prot);
623 pte_unmap(pte);
624 address = (address + PMD_SIZE) & PMD_MASK;
625 pmd++;
626 } while (address && (address < end));
627 return 0;
630 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
632 int error = 0;
633 pgd_t * dir;
634 unsigned long beg = address;
635 unsigned long end = address + size;
636 struct mm_struct *mm = vma->vm_mm;
638 dir = pgd_offset(mm, address);
639 flush_cache_range(vma, beg, end);
640 if (address >= end)
641 BUG();
643 spin_lock(&mm->page_table_lock);
644 do {
645 pmd_t *pmd = pmd_alloc(mm, dir, address);
646 error = -ENOMEM;
647 if (!pmd)
648 break;
649 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
650 if (error)
651 break;
652 address = (address + PGDIR_SIZE) & PGDIR_MASK;
653 dir++;
654 } while (address && (address < end));
655 flush_tlb_range(vma, beg, end);
656 spin_unlock(&mm->page_table_lock);
657 return error;
661 * maps a range of physical memory into the requested pages. the old
662 * mappings are removed. any references to nonexistent pages results
663 * in null mappings (currently treated as "copy-on-access")
665 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
666 unsigned long phys_addr, pgprot_t prot)
668 unsigned long end;
669 unsigned long pfn;
671 address &= ~PMD_MASK;
672 end = address + size;
673 if (end > PMD_SIZE)
674 end = PMD_SIZE;
675 pfn = phys_addr >> PAGE_SHIFT;
676 do {
677 BUG_ON(!pte_none(*pte));
678 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
679 set_pte(pte, pfn_pte(pfn, prot));
680 address += PAGE_SIZE;
681 pfn++;
682 pte++;
683 } while (address && (address < end));
686 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
687 unsigned long phys_addr, pgprot_t prot)
689 unsigned long base, end;
691 base = address & PGDIR_MASK;
692 address &= ~PGDIR_MASK;
693 end = address + size;
694 if (end > PGDIR_SIZE)
695 end = PGDIR_SIZE;
696 phys_addr -= address;
697 do {
698 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
699 if (!pte)
700 return -ENOMEM;
701 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
702 pte_unmap(pte);
703 address = (address + PMD_SIZE) & PMD_MASK;
704 pmd++;
705 } while (address && (address < end));
706 return 0;
709 /* Note: this is only safe if the mm semaphore is held when called. */
710 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
712 int error = 0;
713 pgd_t * dir;
714 unsigned long beg = from;
715 unsigned long end = from + size;
716 struct mm_struct *mm = vma->vm_mm;
718 phys_addr -= from;
719 dir = pgd_offset(mm, from);
720 flush_cache_range(vma, beg, end);
721 if (from >= end)
722 BUG();
724 spin_lock(&mm->page_table_lock);
725 do {
726 pmd_t *pmd = pmd_alloc(mm, dir, from);
727 error = -ENOMEM;
728 if (!pmd)
729 break;
730 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
731 if (error)
732 break;
733 from = (from + PGDIR_SIZE) & PGDIR_MASK;
734 dir++;
735 } while (from && (from < end));
736 flush_tlb_range(vma, beg, end);
737 spin_unlock(&mm->page_table_lock);
738 return error;
742 * Establish a new mapping:
743 * - flush the old one
744 * - update the page tables
745 * - inform the TLB about the new one
747 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
749 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
751 set_pte(page_table, entry);
752 flush_tlb_page(vma, address);
753 update_mmu_cache(vma, address, entry);
757 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
759 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
760 pte_t *page_table)
762 invalidate_vcache(address, vma->vm_mm, new_page);
763 flush_page_to_ram(new_page);
764 flush_cache_page(vma, address);
765 establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
769 * This routine handles present pages, when users try to write
770 * to a shared page. It is done by copying the page to a new address
771 * and decrementing the shared-page counter for the old page.
773 * Goto-purists beware: the only reason for goto's here is that it results
774 * in better assembly code.. The "default" path will see no jumps at all.
776 * Note that this routine assumes that the protection checks have been
777 * done by the caller (the low-level page fault routine in most cases).
778 * Thus we can safely just mark it writable once we've done any necessary
779 * COW.
781 * We also mark the page dirty at this point even though the page will
782 * change only once the write actually happens. This avoids a few races,
783 * and potentially makes it more efficient.
785 * We hold the mm semaphore and the page_table_lock on entry and exit
786 * with the page_table_lock released.
788 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
789 unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
791 struct page *old_page, *new_page;
792 unsigned long pfn = pte_pfn(pte);
794 if (!pfn_valid(pfn))
795 goto bad_wp_page;
796 old_page = pfn_to_page(pfn);
798 if (!TestSetPageLocked(old_page)) {
799 int reuse = can_share_swap_page(old_page);
800 unlock_page(old_page);
801 if (reuse) {
802 flush_cache_page(vma, address);
803 establish_pte(vma, address, page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
804 pte_unmap(page_table);
805 spin_unlock(&mm->page_table_lock);
806 return VM_FAULT_MINOR;
809 pte_unmap(page_table);
812 * Ok, we need to copy. Oh, well..
814 page_cache_get(old_page);
815 spin_unlock(&mm->page_table_lock);
817 new_page = alloc_page(GFP_HIGHUSER);
818 if (!new_page)
819 goto no_mem;
820 copy_cow_page(old_page,new_page,address);
823 * Re-check the pte - we dropped the lock
825 spin_lock(&mm->page_table_lock);
826 page_table = pte_offset_map(pmd, address);
827 if (pte_same(*page_table, pte)) {
828 if (PageReserved(old_page))
829 ++mm->rss;
830 page_remove_rmap(old_page, page_table);
831 break_cow(vma, new_page, address, page_table);
832 page_add_rmap(new_page, page_table);
833 lru_cache_add(new_page);
835 /* Free the old page.. */
836 new_page = old_page;
838 pte_unmap(page_table);
839 spin_unlock(&mm->page_table_lock);
840 page_cache_release(new_page);
841 page_cache_release(old_page);
842 return VM_FAULT_MINOR;
844 bad_wp_page:
845 pte_unmap(page_table);
846 spin_unlock(&mm->page_table_lock);
847 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n", address);
849 * This should really halt the system so it can be debugged or
850 * at least the kernel stops what it's doing before it corrupts
851 * data, but for the moment just pretend this is OOM.
853 return VM_FAULT_OOM;
854 no_mem:
855 page_cache_release(old_page);
856 return VM_FAULT_OOM;
859 static void vmtruncate_list(struct list_head *head, unsigned long pgoff)
861 unsigned long start, end, len, diff;
862 struct vm_area_struct *vma;
863 struct list_head *curr;
865 list_for_each(curr, head) {
866 vma = list_entry(curr, struct vm_area_struct, shared);
867 start = vma->vm_start;
868 end = vma->vm_end;
869 len = end - start;
871 /* mapping wholly truncated? */
872 if (vma->vm_pgoff >= pgoff) {
873 zap_page_range(vma, start, len);
874 continue;
877 /* mapping wholly unaffected? */
878 len = len >> PAGE_SHIFT;
879 diff = pgoff - vma->vm_pgoff;
880 if (diff >= len)
881 continue;
883 /* Ok, partially affected.. */
884 start += diff << PAGE_SHIFT;
885 len = (len - diff) << PAGE_SHIFT;
886 zap_page_range(vma, start, len);
891 * Handle all mappings that got truncated by a "truncate()"
892 * system call.
894 * NOTE! We have to be ready to update the memory sharing
895 * between the file and the memory map for a potential last
896 * incomplete page. Ugly, but necessary.
898 int vmtruncate(struct inode * inode, loff_t offset)
900 unsigned long pgoff;
901 struct address_space *mapping = inode->i_mapping;
902 unsigned long limit;
904 if (inode->i_size < offset)
905 goto do_expand;
906 inode->i_size = offset;
907 spin_lock(&mapping->i_shared_lock);
908 if (list_empty(&mapping->i_mmap) && list_empty(&mapping->i_mmap_shared))
909 goto out_unlock;
911 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
912 if (!list_empty(&mapping->i_mmap))
913 vmtruncate_list(&mapping->i_mmap, pgoff);
914 if (!list_empty(&mapping->i_mmap_shared))
915 vmtruncate_list(&mapping->i_mmap_shared, pgoff);
917 out_unlock:
918 spin_unlock(&mapping->i_shared_lock);
919 truncate_inode_pages(mapping, offset);
920 goto out_truncate;
922 do_expand:
923 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
924 if (limit != RLIM_INFINITY && offset > limit)
925 goto out_sig;
926 if (offset > inode->i_sb->s_maxbytes)
927 goto out;
928 inode->i_size = offset;
930 out_truncate:
931 if (inode->i_op && inode->i_op->truncate)
932 inode->i_op->truncate(inode);
933 return 0;
934 out_sig:
935 send_sig(SIGXFSZ, current, 0);
936 out:
937 return -EFBIG;
941 * Primitive swap readahead code. We simply read an aligned block of
942 * (1 << page_cluster) entries in the swap area. This method is chosen
943 * because it doesn't cost us any seek time. We also make sure to queue
944 * the 'original' request together with the readahead ones...
946 void swapin_readahead(swp_entry_t entry)
948 int i, num;
949 struct page *new_page;
950 unsigned long offset;
953 * Get the number of handles we should do readahead io to.
955 num = valid_swaphandles(entry, &offset);
956 for (i = 0; i < num; offset++, i++) {
957 /* Ok, do the async read-ahead now */
958 new_page = read_swap_cache_async(swp_entry(swp_type(entry), offset));
959 if (!new_page)
960 break;
961 page_cache_release(new_page);
963 return;
967 * We hold the mm semaphore and the page_table_lock on entry and
968 * should release the pagetable lock on exit..
970 static int do_swap_page(struct mm_struct * mm,
971 struct vm_area_struct * vma, unsigned long address,
972 pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
974 struct page *page;
975 swp_entry_t entry = pte_to_swp_entry(orig_pte);
976 pte_t pte;
977 int ret = VM_FAULT_MINOR;
979 pte_unmap(page_table);
980 spin_unlock(&mm->page_table_lock);
981 page = lookup_swap_cache(entry);
982 if (!page) {
983 swapin_readahead(entry);
984 page = read_swap_cache_async(entry);
985 if (!page) {
987 * Back out if somebody else faulted in this pte while
988 * we released the page table lock.
990 spin_lock(&mm->page_table_lock);
991 page_table = pte_offset_map(pmd, address);
992 if (pte_same(*page_table, orig_pte))
993 ret = VM_FAULT_OOM;
994 else
995 ret = VM_FAULT_MINOR;
996 pte_unmap(page_table);
997 spin_unlock(&mm->page_table_lock);
998 return ret;
1001 /* Had to read the page from swap area: Major fault */
1002 ret = VM_FAULT_MAJOR;
1003 inc_page_state(pgmajfault);
1006 mark_page_accessed(page);
1007 lock_page(page);
1010 * Back out if somebody else faulted in this pte while we
1011 * released the page table lock.
1013 spin_lock(&mm->page_table_lock);
1014 page_table = pte_offset_map(pmd, address);
1015 if (!pte_same(*page_table, orig_pte)) {
1016 pte_unmap(page_table);
1017 spin_unlock(&mm->page_table_lock);
1018 unlock_page(page);
1019 page_cache_release(page);
1020 return VM_FAULT_MINOR;
1023 /* The page isn't present yet, go ahead with the fault. */
1025 swap_free(entry);
1026 if (vm_swap_full())
1027 remove_exclusive_swap_page(page);
1029 mm->rss++;
1030 pte = mk_pte(page, vma->vm_page_prot);
1031 if (write_access && can_share_swap_page(page))
1032 pte = pte_mkdirty(pte_mkwrite(pte));
1033 unlock_page(page);
1035 flush_page_to_ram(page);
1036 flush_icache_page(vma, page);
1037 set_pte(page_table, pte);
1038 page_add_rmap(page, page_table);
1040 /* No need to invalidate - it was non-present before */
1041 update_mmu_cache(vma, address, pte);
1042 pte_unmap(page_table);
1043 spin_unlock(&mm->page_table_lock);
1044 return ret;
1048 * We are called with the MM semaphore and page_table_lock
1049 * spinlock held to protect against concurrent faults in
1050 * multithreaded programs.
1052 static int do_anonymous_page(struct mm_struct * mm, struct vm_area_struct * vma, pte_t *page_table, pmd_t *pmd, int write_access, unsigned long addr)
1054 pte_t entry;
1055 struct page * page = ZERO_PAGE(addr);
1057 /* Read-only mapping of ZERO_PAGE. */
1058 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1060 /* ..except if it's a write access */
1061 if (write_access) {
1062 /* Allocate our own private page. */
1063 pte_unmap(page_table);
1064 spin_unlock(&mm->page_table_lock);
1066 page = alloc_page(GFP_HIGHUSER);
1067 if (!page)
1068 goto no_mem;
1069 clear_user_highpage(page, addr);
1071 spin_lock(&mm->page_table_lock);
1072 page_table = pte_offset_map(pmd, addr);
1074 if (!pte_none(*page_table)) {
1075 pte_unmap(page_table);
1076 page_cache_release(page);
1077 spin_unlock(&mm->page_table_lock);
1078 return VM_FAULT_MINOR;
1080 mm->rss++;
1081 flush_page_to_ram(page);
1082 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1083 lru_cache_add(page);
1084 mark_page_accessed(page);
1087 set_pte(page_table, entry);
1088 page_add_rmap(page, page_table); /* ignores ZERO_PAGE */
1089 pte_unmap(page_table);
1091 /* No need to invalidate - it was non-present before */
1092 update_mmu_cache(vma, addr, entry);
1093 spin_unlock(&mm->page_table_lock);
1094 return VM_FAULT_MINOR;
1096 no_mem:
1097 return VM_FAULT_OOM;
1101 * do_no_page() tries to create a new page mapping. It aggressively
1102 * tries to share with existing pages, but makes a separate copy if
1103 * the "write_access" parameter is true in order to avoid the next
1104 * page fault.
1106 * As this is called only for pages that do not currently exist, we
1107 * do not need to flush old virtual caches or the TLB.
1109 * This is called with the MM semaphore held and the page table
1110 * spinlock held. Exit with the spinlock released.
1112 static int do_no_page(struct mm_struct * mm, struct vm_area_struct * vma,
1113 unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1115 struct page * new_page;
1116 pte_t entry;
1118 if (!vma->vm_ops || !vma->vm_ops->nopage)
1119 return do_anonymous_page(mm, vma, page_table, pmd, write_access, address);
1120 pte_unmap(page_table);
1121 spin_unlock(&mm->page_table_lock);
1123 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1125 /* no page was available -- either SIGBUS or OOM */
1126 if (new_page == NOPAGE_SIGBUS)
1127 return VM_FAULT_SIGBUS;
1128 if (new_page == NOPAGE_OOM)
1129 return VM_FAULT_OOM;
1132 * Should we do an early C-O-W break?
1134 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1135 struct page * page = alloc_page(GFP_HIGHUSER);
1136 if (!page) {
1137 page_cache_release(new_page);
1138 return VM_FAULT_OOM;
1140 copy_user_highpage(page, new_page, address);
1141 page_cache_release(new_page);
1142 lru_cache_add(page);
1143 new_page = page;
1146 spin_lock(&mm->page_table_lock);
1147 page_table = pte_offset_map(pmd, address);
1150 * This silly early PAGE_DIRTY setting removes a race
1151 * due to the bad i386 page protection. But it's valid
1152 * for other architectures too.
1154 * Note that if write_access is true, we either now have
1155 * an exclusive copy of the page, or this is a shared mapping,
1156 * so we can make it writable and dirty to avoid having to
1157 * handle that later.
1159 /* Only go through if we didn't race with anybody else... */
1160 if (pte_none(*page_table)) {
1161 ++mm->rss;
1162 flush_page_to_ram(new_page);
1163 flush_icache_page(vma, new_page);
1164 entry = mk_pte(new_page, vma->vm_page_prot);
1165 if (write_access)
1166 entry = pte_mkwrite(pte_mkdirty(entry));
1167 set_pte(page_table, entry);
1168 page_add_rmap(new_page, page_table);
1169 pte_unmap(page_table);
1170 } else {
1171 /* One of our sibling threads was faster, back out. */
1172 pte_unmap(page_table);
1173 page_cache_release(new_page);
1174 spin_unlock(&mm->page_table_lock);
1175 return VM_FAULT_MINOR;
1178 /* no need to invalidate: a not-present page shouldn't be cached */
1179 update_mmu_cache(vma, address, entry);
1180 spin_unlock(&mm->page_table_lock);
1181 return VM_FAULT_MAJOR;
1185 * These routines also need to handle stuff like marking pages dirty
1186 * and/or accessed for architectures that don't do it in hardware (most
1187 * RISC architectures). The early dirtying is also good on the i386.
1189 * There is also a hook called "update_mmu_cache()" that architectures
1190 * with external mmu caches can use to update those (ie the Sparc or
1191 * PowerPC hashed page tables that act as extended TLBs).
1193 * Note the "page_table_lock". It is to protect against kswapd removing
1194 * pages from under us. Note that kswapd only ever _removes_ pages, never
1195 * adds them. As such, once we have noticed that the page is not present,
1196 * we can drop the lock early.
1198 * The adding of pages is protected by the MM semaphore (which we hold),
1199 * so we don't need to worry about a page being suddenly been added into
1200 * our VM.
1202 * We enter with the pagetable spinlock held, we are supposed to
1203 * release it when done.
1205 static inline int handle_pte_fault(struct mm_struct *mm,
1206 struct vm_area_struct * vma, unsigned long address,
1207 int write_access, pte_t *pte, pmd_t *pmd)
1209 pte_t entry;
1211 entry = *pte;
1212 if (!pte_present(entry)) {
1214 * If it truly wasn't present, we know that kswapd
1215 * and the PTE updates will not touch it later. So
1216 * drop the lock.
1218 if (pte_none(entry))
1219 return do_no_page(mm, vma, address, write_access, pte, pmd);
1220 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1223 if (write_access) {
1224 if (!pte_write(entry))
1225 return do_wp_page(mm, vma, address, pte, pmd, entry);
1227 entry = pte_mkdirty(entry);
1229 entry = pte_mkyoung(entry);
1230 establish_pte(vma, address, pte, entry);
1231 pte_unmap(pte);
1232 spin_unlock(&mm->page_table_lock);
1233 return VM_FAULT_MINOR;
1237 * By the time we get here, we already hold the mm semaphore
1239 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1240 unsigned long address, int write_access)
1242 pgd_t *pgd;
1243 pmd_t *pmd;
1245 current->state = TASK_RUNNING;
1246 pgd = pgd_offset(mm, address);
1248 inc_page_state(pgfault);
1250 * We need the page table lock to synchronize with kswapd
1251 * and the SMP-safe atomic PTE updates.
1253 spin_lock(&mm->page_table_lock);
1254 pmd = pmd_alloc(mm, pgd, address);
1256 if (pmd) {
1257 pte_t * pte = pte_alloc_map(mm, pmd, address);
1258 if (pte)
1259 return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1261 spin_unlock(&mm->page_table_lock);
1262 return VM_FAULT_OOM;
1266 * Allocate page middle directory.
1268 * We've already handled the fast-path in-line, and we own the
1269 * page table lock.
1271 * On a two-level page table, this ends up actually being entirely
1272 * optimized away.
1274 pmd_t *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1276 pmd_t *new;
1278 spin_unlock(&mm->page_table_lock);
1279 new = pmd_alloc_one(mm, address);
1280 spin_lock(&mm->page_table_lock);
1281 if (!new)
1282 return NULL;
1285 * Because we dropped the lock, we should re-check the
1286 * entry, as somebody else could have populated it..
1288 if (pgd_present(*pgd)) {
1289 pmd_free(new);
1290 goto out;
1292 pgd_populate(mm, pgd, new);
1293 out:
1294 return pmd_offset(pgd, address);
1297 int make_pages_present(unsigned long addr, unsigned long end)
1299 int ret, len, write;
1300 struct vm_area_struct * vma;
1302 vma = find_vma(current->mm, addr);
1303 write = (vma->vm_flags & VM_WRITE) != 0;
1304 if (addr >= end)
1305 BUG();
1306 if (end > vma->vm_end)
1307 BUG();
1308 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1309 ret = get_user_pages(current, current->mm, addr,
1310 len, write, 0, NULL, NULL);
1311 return ret == len ? 0 : -1;
1315 * Map a vmalloc()-space virtual address to the physical page.
1317 struct page * vmalloc_to_page(void * vmalloc_addr)
1319 unsigned long addr = (unsigned long) vmalloc_addr;
1320 struct page *page = NULL;
1321 pgd_t *pgd = pgd_offset_k(addr);
1322 pmd_t *pmd;
1323 pte_t *ptep, pte;
1325 if (!pgd_none(*pgd)) {
1326 pmd = pmd_offset(pgd, addr);
1327 if (!pmd_none(*pmd)) {
1328 preempt_disable();
1329 ptep = pte_offset_map(pmd, addr);
1330 pte = *ptep;
1331 if (pte_present(pte))
1332 page = pte_page(pte);
1333 pte_unmap(ptep);
1334 preempt_enable();
1337 return page;