added some suse-specific patches to the kernel.
[linux-flexiantxendom0-3.2.10.git] / mm / memory.c
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
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/vcache.h>
47 #include <linux/rmap-locking.h>
48
49 #include <asm/pgalloc.h>
50 #include <asm/rmap.h>
51 #include <asm/uaccess.h>
52 #include <asm/tlb.h>
53 #include <asm/tlbflush.h>
54 #include <asm/pgtable.h>
55
56 #include <linux/swapops.h>
57
58 #ifndef CONFIG_DISCONTIGMEM
59 /* use the per-pgdat data instead for discontigmem - mbligh */
60 unsigned long max_mapnr;
61 struct page *mem_map;
62 #endif
63
64 unsigned long num_physpages;
65 void * high_memory;
66 struct page *highmem_start_page;
67
68 /*
69  * We special-case the C-O-W ZERO_PAGE, because it's such
70  * a common occurrence (no need to read the page to know
71  * that it's zero - better for the cache and memory subsystem).
72  */
73 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
74 {
75         if (from == ZERO_PAGE(address)) {
76                 clear_user_highpage(to, address);
77                 return;
78         }
79         copy_user_highpage(to, from, address);
80 }
81
82 /*
83  * Note: this doesn't free the actual pages themselves. That
84  * has been handled earlier when unmapping all the memory regions.
85  */
86 static inline void free_one_pmd(struct mmu_gather *tlb, pmd_t * dir)
87 {
88         struct page *page;
89
90         if (pmd_none(*dir))
91                 return;
92         if (pmd_bad(*dir)) {
93                 pmd_ERROR(*dir);
94                 pmd_clear(dir);
95                 return;
96         }
97         page = pmd_page(*dir);
98         pmd_clear(dir);
99         pgtable_remove_rmap(page);
100         pte_free_tlb(tlb, page);
101 }
102
103 static inline void free_one_pgd(struct mmu_gather *tlb, pgd_t * dir)
104 {
105         int j;
106         pmd_t * pmd;
107
108         if (pgd_none(*dir))
109                 return;
110         if (pgd_bad(*dir)) {
111                 pgd_ERROR(*dir);
112                 pgd_clear(dir);
113                 return;
114         }
115         pmd = pmd_offset(dir, 0);
116         pgd_clear(dir);
117         for (j = 0; j < PTRS_PER_PMD ; j++)
118                 free_one_pmd(tlb, pmd+j);
119         pmd_free_tlb(tlb, pmd);
120 }
121
122 /*
123  * This function clears all user-level page tables of a process - this
124  * is needed by execve(), so that old pages aren't in the way.
125  *
126  * Must be called with pagetable lock held.
127  */
128 void clear_page_tables(struct mmu_gather *tlb, unsigned long first, int nr)
129 {
130         pgd_t * page_dir = tlb->mm->pgd;
131
132         page_dir += first;
133         do {
134                 free_one_pgd(tlb, page_dir);
135                 page_dir++;
136         } while (--nr);
137 }
138
139 pte_t * pte_alloc_map(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
140 {
141         if (!pmd_present(*pmd)) {
142                 struct page *new;
143
144                 spin_unlock(&mm->page_table_lock);
145                 new = pte_alloc_one(mm, address);
146                 spin_lock(&mm->page_table_lock);
147                 if (!new)
148                         return NULL;
149
150                 /*
151                  * Because we dropped the lock, we should re-check the
152                  * entry, as somebody else could have populated it..
153                  */
154                 if (pmd_present(*pmd)) {
155                         pte_free(new);
156                         goto out;
157                 }
158                 pgtable_add_rmap(new, mm, address);
159                 pmd_populate(mm, pmd, new);
160         }
161 out:
162         return pte_offset_map(pmd, address);
163 }
164
165 pte_t * pte_alloc_kernel(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
166 {
167         if (!pmd_present(*pmd)) {
168                 pte_t *new;
169
170                 spin_unlock(&mm->page_table_lock);
171                 new = pte_alloc_one_kernel(mm, address);
172                 spin_lock(&mm->page_table_lock);
173                 if (!new)
174                         return NULL;
175
176                 /*
177                  * Because we dropped the lock, we should re-check the
178                  * entry, as somebody else could have populated it..
179                  */
180                 if (pmd_present(*pmd)) {
181                         pte_free_kernel(new);
182                         goto out;
183                 }
184                 pgtable_add_rmap(virt_to_page(new), mm, address);
185                 pmd_populate_kernel(mm, pmd, new);
186         }
187 out:
188         return pte_offset_kernel(pmd, address);
189 }
190 #define PTE_TABLE_MASK  ((PTRS_PER_PTE-1) * sizeof(pte_t))
191 #define PMD_TABLE_MASK  ((PTRS_PER_PMD-1) * sizeof(pmd_t))
192
193 /*
194  * copy one vm_area from one task to the other. Assumes the page tables
195  * already present in the new task to be cleared in the whole range
196  * covered by this vma.
197  *
198  * 08Jan98 Merged into one routine from several inline routines to reduce
199  *         variable count and make things faster. -jj
200  *
201  * dst->page_table_lock is held on entry and exit,
202  * but may be dropped within pmd_alloc() and pte_alloc_map().
203  */
204 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
205                         struct vm_area_struct *vma)
206 {
207         pgd_t * src_pgd, * dst_pgd;
208         unsigned long address = vma->vm_start;
209         unsigned long end = vma->vm_end;
210         unsigned long cow;
211         struct pte_chain *pte_chain = NULL;
212
213         if (is_vm_hugetlb_page(vma))
214                 return copy_hugetlb_page_range(dst, src, vma);
215
216         pte_chain = pte_chain_alloc(GFP_ATOMIC);
217         if (!pte_chain) {
218                 spin_unlock(&dst->page_table_lock);
219                 pte_chain = pte_chain_alloc(GFP_KERNEL);
220                 spin_lock(&dst->page_table_lock);
221                 if (!pte_chain)
222                         goto nomem;
223         }
224         
225         cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
226         src_pgd = pgd_offset(src, address)-1;
227         dst_pgd = pgd_offset(dst, address)-1;
228
229         for (;;) {
230                 pmd_t * src_pmd, * dst_pmd;
231
232                 src_pgd++; dst_pgd++;
233                 
234                 /* copy_pmd_range */
235                 
236                 if (pgd_none(*src_pgd))
237                         goto skip_copy_pmd_range;
238                 if (pgd_bad(*src_pgd)) {
239                         pgd_ERROR(*src_pgd);
240                         pgd_clear(src_pgd);
241 skip_copy_pmd_range:    address = (address + PGDIR_SIZE) & PGDIR_MASK;
242                         if (!address || (address >= end))
243                                 goto out;
244                         continue;
245                 }
246
247                 src_pmd = pmd_offset(src_pgd, address);
248                 dst_pmd = pmd_alloc(dst, dst_pgd, address);
249                 if (!dst_pmd)
250                         goto nomem;
251
252                 do {
253                         pte_t * src_pte, * dst_pte;
254                 
255                         /* copy_pte_range */
256                 
257                         if (pmd_none(*src_pmd))
258                                 goto skip_copy_pte_range;
259                         if (pmd_bad(*src_pmd)) {
260                                 pmd_ERROR(*src_pmd);
261                                 pmd_clear(src_pmd);
262 skip_copy_pte_range:
263                                 address = (address + PMD_SIZE) & PMD_MASK;
264                                 if (address >= end)
265                                         goto out;
266                                 goto cont_copy_pmd_range;
267                         }
268
269                         dst_pte = pte_alloc_map(dst, dst_pmd, address);
270                         if (!dst_pte)
271                                 goto nomem;
272                         spin_lock(&src->page_table_lock);       
273                         src_pte = pte_offset_map_nested(src_pmd, address);
274                         do {
275                                 pte_t pte = *src_pte;
276                                 struct page *page;
277                                 unsigned long pfn;
278
279                                 /* copy_one_pte */
280
281                                 if (pte_none(pte))
282                                         goto cont_copy_pte_range_noset;
283                                 /* pte contains position in swap, so copy. */
284                                 if (!pte_present(pte)) {
285                                         if (!pte_file(pte))
286                                                 swap_duplicate(pte_to_swp_entry(pte));
287                                         set_pte(dst_pte, pte);
288                                         goto cont_copy_pte_range_noset;
289                                 }
290                                 pfn = pte_pfn(pte);
291                                 /* the pte points outside of valid memory, the
292                                  * mapping is assumed to be good, meaningful
293                                  * and not mapped via rmap - duplicate the
294                                  * mapping as is.
295                                  */
296                                 page = NULL;
297                                 if (pfn_valid(pfn)) 
298                                         page = pfn_to_page(pfn); 
299
300                                 if (!page || PageReserved(page)) {
301                                         set_pte(dst_pte, pte);
302                                         goto cont_copy_pte_range_noset;
303                                 }
304
305                                 /*
306                                  * If it's a COW mapping, write protect it both
307                                  * in the parent and the child
308                                  */
309                                 if (cow) {
310                                         ptep_set_wrprotect(src_pte);
311                                         pte = *src_pte;
312                                 }
313
314                                 /*
315                                  * If it's a shared mapping, mark it clean in
316                                  * the child
317                                  */
318                                 if (vma->vm_flags & VM_SHARED)
319                                         pte = pte_mkclean(pte);
320                                 pte = pte_mkold(pte);
321                                 get_page(page);
322                                 dst->rss++;
323
324                                 set_pte(dst_pte, pte);
325                                 pte_chain = page_add_rmap(page, dst_pte,
326                                                         pte_chain);
327                                 if (pte_chain)
328                                         goto cont_copy_pte_range_noset;
329                                 pte_chain = pte_chain_alloc(GFP_ATOMIC);
330                                 if (pte_chain)
331                                         goto cont_copy_pte_range_noset;
332
333                                 /*
334                                  * pte_chain allocation failed, and we need to
335                                  * run page reclaim.
336                                  */
337                                 pte_unmap_nested(src_pte);
338                                 pte_unmap(dst_pte);
339                                 spin_unlock(&src->page_table_lock);     
340                                 spin_unlock(&dst->page_table_lock);     
341                                 pte_chain = pte_chain_alloc(GFP_KERNEL);
342                                 spin_lock(&dst->page_table_lock);       
343                                 if (!pte_chain)
344                                         goto nomem;
345                                 spin_lock(&src->page_table_lock);
346                                 dst_pte = pte_offset_map(dst_pmd, address);
347                                 src_pte = pte_offset_map_nested(src_pmd,
348                                                                 address);
349 cont_copy_pte_range_noset:
350                                 address += PAGE_SIZE;
351                                 if (address >= end) {
352                                         pte_unmap_nested(src_pte);
353                                         pte_unmap(dst_pte);
354                                         goto out_unlock;
355                                 }
356                                 src_pte++;
357                                 dst_pte++;
358                         } while ((unsigned long)src_pte & PTE_TABLE_MASK);
359                         pte_unmap_nested(src_pte-1);
360                         pte_unmap(dst_pte-1);
361                         spin_unlock(&src->page_table_lock);
362                 
363 cont_copy_pmd_range:
364                         src_pmd++;
365                         dst_pmd++;
366                 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
367         }
368 out_unlock:
369         spin_unlock(&src->page_table_lock);
370 out:
371         pte_chain_free(pte_chain);
372         return 0;
373 nomem:
374         pte_chain_free(pte_chain);
375         return -ENOMEM;
376 }
377
378 static void
379 zap_pte_range(struct mmu_gather *tlb, pmd_t * pmd,
380                 unsigned long address, unsigned long size)
381 {
382         unsigned long offset;
383         pte_t *ptep;
384
385         if (pmd_none(*pmd))
386                 return;
387         if (pmd_bad(*pmd)) {
388                 pmd_ERROR(*pmd);
389                 pmd_clear(pmd);
390                 return;
391         }
392         ptep = pte_offset_map(pmd, address);
393         offset = address & ~PMD_MASK;
394         if (offset + size > PMD_SIZE)
395                 size = PMD_SIZE - offset;
396         size &= PAGE_MASK;
397         for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
398                 pte_t pte = *ptep;
399                 if (pte_none(pte))
400                         continue;
401                 if (pte_present(pte)) {
402                         unsigned long pfn = pte_pfn(pte);
403
404                         pte = ptep_get_and_clear(ptep);
405                         tlb_remove_tlb_entry(tlb, ptep, address+offset);
406                         if (pfn_valid(pfn)) {
407                                 struct page *page = pfn_to_page(pfn);
408                                 if (!PageReserved(page)) {
409                                         if (pte_dirty(pte))
410                                                 set_page_dirty(page);
411                                         if (page->mapping && pte_young(pte) &&
412                                                         !PageSwapCache(page))
413                                                 mark_page_accessed(page);
414                                         tlb->freed++;
415                                         page_remove_rmap(page, ptep);
416                                         tlb_remove_page(tlb, page);
417                                 }
418                         }
419                 } else {
420                         if (!pte_file(pte))
421                                 free_swap_and_cache(pte_to_swp_entry(pte));
422                         pte_clear(ptep);
423                 }
424         }
425         pte_unmap(ptep-1);
426 }
427
428 static void
429 zap_pmd_range(struct mmu_gather *tlb, pgd_t * dir,
430                 unsigned long address, unsigned long size)
431 {
432         pmd_t * pmd;
433         unsigned long end;
434
435         if (pgd_none(*dir))
436                 return;
437         if (pgd_bad(*dir)) {
438                 pgd_ERROR(*dir);
439                 pgd_clear(dir);
440                 return;
441         }
442         pmd = pmd_offset(dir, address);
443         end = address + size;
444         if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
445                 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
446         do {
447                 zap_pte_range(tlb, pmd, address, end - address);
448                 address = (address + PMD_SIZE) & PMD_MASK; 
449                 pmd++;
450         } while (address < end);
451 }
452
453 void unmap_page_range(struct mmu_gather *tlb, struct vm_area_struct *vma,
454                         unsigned long address, unsigned long end)
455 {
456         pgd_t * dir;
457
458         if (is_vm_hugetlb_page(vma)) {
459                 unmap_hugepage_range(vma, address, end);
460                 return;
461         }
462
463         BUG_ON(address >= end);
464
465         dir = pgd_offset(vma->vm_mm, address);
466         tlb_start_vma(tlb, vma);
467         do {
468                 zap_pmd_range(tlb, dir, address, end - address);
469                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
470                 dir++;
471         } while (address && (address < end));
472         tlb_end_vma(tlb, vma);
473 }
474
475 /* Dispose of an entire struct mmu_gather per rescheduling point */
476 #if defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
477 #define ZAP_BLOCK_SIZE  (FREE_PTE_NR * PAGE_SIZE)
478 #endif
479
480 /* For UP, 256 pages at a time gives nice low latency */
481 #if !defined(CONFIG_SMP) && defined(CONFIG_PREEMPT)
482 #define ZAP_BLOCK_SIZE  (256 * PAGE_SIZE)
483 #endif
484
485 /* No preempt: go for the best straight-line efficiency */
486 #if !defined(CONFIG_PREEMPT)
487 #define ZAP_BLOCK_SIZE  (~(0UL))
488 #endif
489
490 /**
491  * unmap_vmas - unmap a range of memory covered by a list of vma's
492  * @tlbp: address of the caller's struct mmu_gather
493  * @mm: the controlling mm_struct
494  * @vma: the starting vma
495  * @start_addr: virtual address at which to start unmapping
496  * @end_addr: virtual address at which to end unmapping
497  * @nr_accounted: Place number of unmapped pages in vm-accountable vma's here
498  *
499  * Returns the number of vma's which were covered by the unmapping.
500  *
501  * Unmap all pages in the vma list.  Called under page_table_lock.
502  *
503  * We aim to not hold page_table_lock for too long (for scheduling latency
504  * reasons).  So zap pages in ZAP_BLOCK_SIZE bytecounts.  This means we need to
505  * return the ending mmu_gather to the caller.
506  *
507  * Only addresses between `start' and `end' will be unmapped.
508  *
509  * The VMA list must be sorted in ascending virtual address order.
510  *
511  * unmap_vmas() assumes that the caller will flush the whole unmapped address
512  * range after unmap_vmas() returns.  So the only responsibility here is to
513  * ensure that any thus-far unmapped pages are flushed before unmap_vmas()
514  * drops the lock and schedules.
515  */
516 int unmap_vmas(struct mmu_gather **tlbp, struct mm_struct *mm,
517                 struct vm_area_struct *vma, unsigned long start_addr,
518                 unsigned long end_addr, unsigned long *nr_accounted)
519 {
520         unsigned long zap_bytes = ZAP_BLOCK_SIZE;
521         unsigned long tlb_start;        /* For tlb_finish_mmu */
522         int tlb_start_valid = 0;
523         int ret = 0;
524
525         if (vma) {      /* debug.  killme. */
526                 if (end_addr <= vma->vm_start)
527                         printk("%s: end_addr(0x%08lx) <= vm_start(0x%08lx)\n",
528                                 __FUNCTION__, end_addr, vma->vm_start);
529                 if (start_addr >= vma->vm_end)
530                         printk("%s: start_addr(0x%08lx) <= vm_end(0x%08lx)\n",
531                                 __FUNCTION__, start_addr, vma->vm_end);
532         }
533
534         for ( ; vma && vma->vm_start < end_addr; vma = vma->vm_next) {
535                 unsigned long start;
536                 unsigned long end;
537
538                 start = max(vma->vm_start, start_addr);
539                 if (start >= vma->vm_end)
540                         continue;
541                 end = min(vma->vm_end, end_addr);
542                 if (end <= vma->vm_start)
543                         continue;
544
545                 if (vma->vm_flags & VM_ACCOUNT)
546                         *nr_accounted += (end - start) >> PAGE_SHIFT;
547
548                 ret++;
549                 while (start != end) {
550                         unsigned long block;
551
552                         if (is_vm_hugetlb_page(vma))
553                                 block = end - start;
554                         else
555                                 block = min(zap_bytes, end - start);
556
557                         if (!tlb_start_valid) {
558                                 tlb_start = start;
559                                 tlb_start_valid = 1;
560                         }
561
562                         unmap_page_range(*tlbp, vma, start, start + block);
563                         start += block;
564                         zap_bytes -= block;
565                         if ((long)zap_bytes > 0)
566                                 continue;
567                         if (need_resched()) {
568                                 tlb_finish_mmu(*tlbp, tlb_start, start);
569                                 cond_resched_lock(&mm->page_table_lock);
570                                 *tlbp = tlb_gather_mmu(mm, 0);
571                                 tlb_start_valid = 0;
572                         }
573                         zap_bytes = ZAP_BLOCK_SIZE;
574                 }
575                 if (vma->vm_next && vma->vm_next->vm_start < vma->vm_end)
576                         printk("%s: VMA list is not sorted correctly!\n",
577                                 __FUNCTION__);          
578         }
579         return ret;
580 }
581
582 /**
583  * zap_page_range - remove user pages in a given range
584  * @vma: vm_area_struct holding the applicable pages
585  * @address: starting address of pages to zap
586  * @size: number of bytes to zap
587  */
588 void zap_page_range(struct vm_area_struct *vma,
589                         unsigned long address, unsigned long size)
590 {
591         struct mm_struct *mm = vma->vm_mm;
592         struct mmu_gather *tlb;
593         unsigned long end = address + size;
594         unsigned long nr_accounted = 0;
595
596         might_sleep();
597
598         if (is_vm_hugetlb_page(vma)) {
599                 zap_hugepage_range(vma, address, size);
600                 return;
601         }
602
603         lru_add_drain();
604         spin_lock(&mm->page_table_lock);
605         tlb = tlb_gather_mmu(mm, 0);
606         unmap_vmas(&tlb, mm, vma, address, end, &nr_accounted);
607         tlb_finish_mmu(tlb, address, end);
608         spin_unlock(&mm->page_table_lock);
609 }
610
611 /*
612  * Do a quick page-table lookup for a single page.
613  * mm->page_table_lock must be held.
614  */
615 struct page *
616 follow_page(struct mm_struct *mm, unsigned long address, int write) 
617 {
618         pgd_t *pgd;
619         pmd_t *pmd;
620         pte_t *ptep, pte;
621         unsigned long pfn;
622         struct vm_area_struct *vma;
623
624         vma = hugepage_vma(mm, address);
625         if (vma)
626                 return follow_huge_addr(mm, vma, address, write);
627
628         pgd = pgd_offset(mm, address);
629         if (pgd_none(*pgd) || pgd_bad(*pgd))
630                 goto out;
631
632         pmd = pmd_offset(pgd, address);
633         if (pmd_none(*pmd))
634                 goto out;
635         if (pmd_huge(*pmd))
636                 return follow_huge_pmd(mm, address, pmd, write);
637         if (pmd_bad(*pmd))
638                 goto out;
639
640         ptep = pte_offset_map(pmd, address);
641         if (!ptep)
642                 goto out;
643
644         pte = *ptep;
645         pte_unmap(ptep);
646         if (pte_present(pte)) {
647                 if (!write || (pte_write(pte) && pte_dirty(pte))) {
648                         pfn = pte_pfn(pte);
649                         if (pfn_valid(pfn))
650                                 return pfn_to_page(pfn);
651                 }
652         }
653
654 out:
655         return NULL;
656 }
657
658 /* 
659  * Given a physical address, is there a useful struct page pointing to
660  * it?  This may become more complex in the future if we start dealing
661  * with IO-aperture pages for direct-IO.
662  */
663
664 static inline struct page *get_page_map(struct page *page)
665 {
666         if (!pfn_valid(page_to_pfn(page)))
667                 return 0;
668         return page;
669 }
670
671
672 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
673                 unsigned long start, int len, int write, int force,
674                 struct page **pages, struct vm_area_struct **vmas)
675 {
676         int i;
677         unsigned int flags;
678
679         /* 
680          * Require read or write permissions.
681          * If 'force' is set, we only require the "MAY" flags.
682          */
683         flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
684         flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
685         i = 0;
686
687         do {
688                 struct vm_area_struct * vma;
689
690                 vma = find_extend_vma(mm, start);
691
692 #ifdef VSYSCALL_START
693                 if (!vma && start >= VSYSCALL_START && start < VSYSCALL_END) {
694                         static struct vm_area_struct fixmap_vma = {
695                                 /* Catch users - if there are any valid
696                                    ones, we can make this be "&init_mm" or
697                                    something.  */
698                                 .vm_mm = NULL,
699                                 .vm_start = FIXADDR_START,
700                                 .vm_end = FIXADDR_TOP,
701                                 .vm_page_prot = PAGE_READONLY,
702                                 .vm_flags = VM_READ | VM_EXEC,
703                         };
704                         unsigned long pg = start & PAGE_MASK;
705                         pgd_t *pgd;
706                         pmd_t *pmd;
707                         pte_t *pte;
708                         pgd = pgd_offset_k(pg);
709                         if (!pgd)
710                                 return i ? : -EFAULT;
711                         pmd = pmd_offset(pgd, pg);
712                         if (!pmd)
713                                 return i ? : -EFAULT;
714                         pte = pte_offset_kernel(pmd, pg);
715                         if (!pte || !pte_present(*pte) || !pte_user(*pte) ||
716                             !(write ? pte_write(*pte) : pte_read(*pte)))
717                                 return i ? : -EFAULT;
718                         if (pages) {
719                                 pages[i] = pte_page(*pte);
720                                 get_page(pages[i]);
721                         }
722                         if (vmas)
723                                 vmas[i] = &fixmap_vma;
724                         i++;
725                         start += PAGE_SIZE;
726                         len--;
727                         continue;
728                 }
729 #endif
730
731                 if (!vma || (pages && (vma->vm_flags & VM_IO))
732                                 || !(flags & vma->vm_flags))
733                         return i ? : -EFAULT;
734
735                 if (is_vm_hugetlb_page(vma)) {
736                         i = follow_hugetlb_page(mm, vma, pages, vmas,
737                                                 &start, &len, i);
738                         continue;
739                 }
740                 spin_lock(&mm->page_table_lock);
741                 do {
742                         struct page *map;
743                         while (!(map = follow_page(mm, start, write))) {
744                                 spin_unlock(&mm->page_table_lock);
745                                 switch (handle_mm_fault(mm,vma,start,write)) {
746                                 case VM_FAULT_MINOR:
747                                         tsk->min_flt++;
748                                         break;
749                                 case VM_FAULT_MAJOR:
750                                         tsk->maj_flt++;
751                                         break;
752                                 case VM_FAULT_SIGBUS:
753                                         return i ? i : -EFAULT;
754                                 case VM_FAULT_OOM:
755                                         return i ? i : -ENOMEM;
756                                 default:
757                                         BUG();
758                                 }
759                                 spin_lock(&mm->page_table_lock);
760                         }
761                         if (pages) {
762                                 pages[i] = get_page_map(map);
763                                 if (!pages[i]) {
764                                         spin_unlock(&mm->page_table_lock);
765                                         while (i--)
766                                                 page_cache_release(pages[i]);
767                                         i = -EFAULT;
768                                         goto out;
769                                 }
770                                 flush_dcache_page(pages[i]);
771                                 if (!PageReserved(pages[i]))
772                                         page_cache_get(pages[i]);
773                         }
774                         if (vmas)
775                                 vmas[i] = vma;
776                         i++;
777                         start += PAGE_SIZE;
778                         len--;
779                 } while(len && start < vma->vm_end);
780                 spin_unlock(&mm->page_table_lock);
781         } while(len);
782 out:
783         return i;
784 }
785
786 static void zeromap_pte_range(pte_t * pte, unsigned long address,
787                                      unsigned long size, pgprot_t prot)
788 {
789         unsigned long end;
790
791         address &= ~PMD_MASK;
792         end = address + size;
793         if (end > PMD_SIZE)
794                 end = PMD_SIZE;
795         do {
796                 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
797                 BUG_ON(!pte_none(*pte));
798                 set_pte(pte, zero_pte);
799                 address += PAGE_SIZE;
800                 pte++;
801         } while (address && (address < end));
802 }
803
804 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
805                                     unsigned long size, pgprot_t prot)
806 {
807         unsigned long end;
808
809         address &= ~PGDIR_MASK;
810         end = address + size;
811         if (end > PGDIR_SIZE)
812                 end = PGDIR_SIZE;
813         do {
814                 pte_t * pte = pte_alloc_map(mm, pmd, address);
815                 if (!pte)
816                         return -ENOMEM;
817                 zeromap_pte_range(pte, address, end - address, prot);
818                 pte_unmap(pte);
819                 address = (address + PMD_SIZE) & PMD_MASK;
820                 pmd++;
821         } while (address && (address < end));
822         return 0;
823 }
824
825 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
826 {
827         int error = 0;
828         pgd_t * dir;
829         unsigned long beg = address;
830         unsigned long end = address + size;
831         struct mm_struct *mm = vma->vm_mm;
832
833         dir = pgd_offset(mm, address);
834         flush_cache_range(vma, beg, end);
835         if (address >= end)
836                 BUG();
837
838         spin_lock(&mm->page_table_lock);
839         do {
840                 pmd_t *pmd = pmd_alloc(mm, dir, address);
841                 error = -ENOMEM;
842                 if (!pmd)
843                         break;
844                 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
845                 if (error)
846                         break;
847                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
848                 dir++;
849         } while (address && (address < end));
850         flush_tlb_range(vma, beg, end);
851         spin_unlock(&mm->page_table_lock);
852         return error;
853 }
854
855 /*
856  * maps a range of physical memory into the requested pages. the old
857  * mappings are removed. any references to nonexistent pages results
858  * in null mappings (currently treated as "copy-on-access")
859  */
860 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
861         unsigned long phys_addr, pgprot_t prot)
862 {
863         unsigned long end;
864         unsigned long pfn;
865
866         address &= ~PMD_MASK;
867         end = address + size;
868         if (end > PMD_SIZE)
869                 end = PMD_SIZE;
870         pfn = phys_addr >> PAGE_SHIFT;
871         do {
872                 BUG_ON(!pte_none(*pte));
873                 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
874                         set_pte(pte, pfn_pte(pfn, prot));
875                 address += PAGE_SIZE;
876                 pfn++;
877                 pte++;
878         } while (address && (address < end));
879 }
880
881 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
882         unsigned long phys_addr, pgprot_t prot)
883 {
884         unsigned long base, end;
885
886         base = address & PGDIR_MASK;
887         address &= ~PGDIR_MASK;
888         end = address + size;
889         if (end > PGDIR_SIZE)
890                 end = PGDIR_SIZE;
891         phys_addr -= address;
892         do {
893                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
894                 if (!pte)
895                         return -ENOMEM;
896                 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
897                 pte_unmap(pte);
898                 address = (address + PMD_SIZE) & PMD_MASK;
899                 pmd++;
900         } while (address && (address < end));
901         return 0;
902 }
903
904 /*  Note: this is only safe if the mm semaphore is held when called. */
905 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
906 {
907         int error = 0;
908         pgd_t * dir;
909         unsigned long beg = from;
910         unsigned long end = from + size;
911         struct mm_struct *mm = vma->vm_mm;
912
913         phys_addr -= from;
914         dir = pgd_offset(mm, from);
915         flush_cache_range(vma, beg, end);
916         if (from >= end)
917                 BUG();
918
919         spin_lock(&mm->page_table_lock);
920         do {
921                 pmd_t *pmd = pmd_alloc(mm, dir, from);
922                 error = -ENOMEM;
923                 if (!pmd)
924                         break;
925                 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
926                 if (error)
927                         break;
928                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
929                 dir++;
930         } while (from && (from < end));
931         flush_tlb_range(vma, beg, end);
932         spin_unlock(&mm->page_table_lock);
933         return error;
934 }
935
936 /*
937  * Establish a new mapping:
938  *  - flush the old one
939  *  - update the page tables
940  *  - inform the TLB about the new one
941  *
942  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
943  */
944 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
945 {
946         set_pte(page_table, entry);
947         flush_tlb_page(vma, address);
948         update_mmu_cache(vma, address, entry);
949 }
950
951 /*
952  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
953  */
954 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address, 
955                 pte_t *page_table)
956 {
957         invalidate_vcache(address, vma->vm_mm, new_page);
958         flush_cache_page(vma, address);
959         establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
960 }
961
962 /*
963  * This routine handles present pages, when users try to write
964  * to a shared page. It is done by copying the page to a new address
965  * and decrementing the shared-page counter for the old page.
966  *
967  * Goto-purists beware: the only reason for goto's here is that it results
968  * in better assembly code.. The "default" path will see no jumps at all.
969  *
970  * Note that this routine assumes that the protection checks have been
971  * done by the caller (the low-level page fault routine in most cases).
972  * Thus we can safely just mark it writable once we've done any necessary
973  * COW.
974  *
975  * We also mark the page dirty at this point even though the page will
976  * change only once the write actually happens. This avoids a few races,
977  * and potentially makes it more efficient.
978  *
979  * We hold the mm semaphore and the page_table_lock on entry and exit
980  * with the page_table_lock released.
981  */
982 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
983         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
984 {
985         struct page *old_page, *new_page;
986         unsigned long pfn = pte_pfn(pte);
987         struct pte_chain *pte_chain = NULL;
988         int ret;
989
990         if (unlikely(!pfn_valid(pfn))) {
991                 /*
992                  * This should really halt the system so it can be debugged or
993                  * at least the kernel stops what it's doing before it corrupts
994                  * data, but for the moment just pretend this is OOM.
995                  */
996                 pte_unmap(page_table);
997                 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
998                                 address);
999                 goto oom;
1000         }
1001         old_page = pfn_to_page(pfn);
1002
1003         if (!TestSetPageLocked(old_page)) {
1004                 int reuse = can_share_swap_page(old_page);
1005                 unlock_page(old_page);
1006                 if (reuse) {
1007                         flush_cache_page(vma, address);
1008                         establish_pte(vma, address, page_table,
1009                                 pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
1010                         pte_unmap(page_table);
1011                         ret = VM_FAULT_MINOR;
1012                         goto out;
1013                 }
1014         }
1015         pte_unmap(page_table);
1016
1017         /*
1018          * Ok, we need to copy. Oh, well..
1019          */
1020         page_cache_get(old_page);
1021         spin_unlock(&mm->page_table_lock);
1022
1023         pte_chain = pte_chain_alloc(GFP_KERNEL);
1024         if (!pte_chain)
1025                 goto no_mem;
1026         new_page = alloc_page(GFP_HIGHUSER);
1027         if (!new_page)
1028                 goto no_mem;
1029         copy_cow_page(old_page,new_page,address);
1030
1031         /*
1032          * Re-check the pte - we dropped the lock
1033          */
1034         spin_lock(&mm->page_table_lock);
1035         page_table = pte_offset_map(pmd, address);
1036         if (pte_same(*page_table, pte)) {
1037                 if (PageReserved(old_page))
1038                         ++mm->rss;
1039                 page_remove_rmap(old_page, page_table);
1040                 break_cow(vma, new_page, address, page_table);
1041                 pte_chain = page_add_rmap(new_page, page_table, pte_chain);
1042                 lru_cache_add_active(new_page);
1043
1044                 /* Free the old page.. */
1045                 new_page = old_page;
1046         }
1047         pte_unmap(page_table);
1048         page_cache_release(new_page);
1049         page_cache_release(old_page);
1050         ret = VM_FAULT_MINOR;
1051         goto out;
1052
1053 no_mem:
1054         page_cache_release(old_page);
1055 oom:
1056         ret = VM_FAULT_OOM;
1057 out:
1058         spin_unlock(&mm->page_table_lock);
1059         pte_chain_free(pte_chain);
1060         return ret;
1061 }
1062
1063 static void vmtruncate_list(struct list_head *head, unsigned long pgoff)
1064 {
1065         unsigned long start, end, len, diff;
1066         struct vm_area_struct *vma;
1067         struct list_head *curr;
1068
1069         list_for_each(curr, head) {
1070                 vma = list_entry(curr, struct vm_area_struct, shared);
1071                 start = vma->vm_start;
1072                 end = vma->vm_end;
1073                 len = end - start;
1074
1075                 /* mapping wholly truncated? */
1076                 if (vma->vm_pgoff >= pgoff) {
1077                         zap_page_range(vma, start, len);
1078                         continue;
1079                 }
1080
1081                 /* mapping wholly unaffected? */
1082                 len = len >> PAGE_SHIFT;
1083                 diff = pgoff - vma->vm_pgoff;
1084                 if (diff >= len)
1085                         continue;
1086
1087                 /* Ok, partially affected.. */
1088                 start += diff << PAGE_SHIFT;
1089                 len = (len - diff) << PAGE_SHIFT;
1090                 zap_page_range(vma, start, len);
1091         }
1092 }
1093
1094 /*
1095  * Handle all mappings that got truncated by a "truncate()"
1096  * system call.
1097  *
1098  * NOTE! We have to be ready to update the memory sharing
1099  * between the file and the memory map for a potential last
1100  * incomplete page.  Ugly, but necessary.
1101  */
1102 int vmtruncate(struct inode * inode, loff_t offset)
1103 {
1104         unsigned long pgoff;
1105         struct address_space *mapping = inode->i_mapping;
1106         unsigned long limit;
1107
1108         if (inode->i_size < offset)
1109                 goto do_expand;
1110         inode->i_size = offset;
1111         down(&mapping->i_shared_sem);
1112         if (list_empty(&mapping->i_mmap) && list_empty(&mapping->i_mmap_shared))
1113                 goto out_unlock;
1114
1115         pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1116         if (!list_empty(&mapping->i_mmap))
1117                 vmtruncate_list(&mapping->i_mmap, pgoff);
1118         if (!list_empty(&mapping->i_mmap_shared))
1119                 vmtruncate_list(&mapping->i_mmap_shared, pgoff);
1120
1121 out_unlock:
1122         up(&mapping->i_shared_sem);
1123         truncate_inode_pages(mapping, offset);
1124         goto out_truncate;
1125
1126 do_expand:
1127         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1128         if (limit != RLIM_INFINITY && offset > limit)
1129                 goto out_sig;
1130         if (offset > inode->i_sb->s_maxbytes)
1131                 goto out;
1132         inode->i_size = offset;
1133
1134 out_truncate:
1135         if (inode->i_op && inode->i_op->truncate)
1136                 inode->i_op->truncate(inode);
1137         return 0;
1138 out_sig:
1139         send_sig(SIGXFSZ, current, 0);
1140 out:
1141         return -EFBIG;
1142 }
1143
1144 /* 
1145  * Primitive swap readahead code. We simply read an aligned block of
1146  * (1 << page_cluster) entries in the swap area. This method is chosen
1147  * because it doesn't cost us any seek time.  We also make sure to queue
1148  * the 'original' request together with the readahead ones...  
1149  */
1150 void swapin_readahead(swp_entry_t entry)
1151 {
1152         int i, num;
1153         struct page *new_page;
1154         unsigned long offset;
1155
1156         /*
1157          * Get the number of handles we should do readahead io to.
1158          */
1159         num = valid_swaphandles(entry, &offset);
1160         for (i = 0; i < num; offset++, i++) {
1161                 /* Ok, do the async read-ahead now */
1162                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1163                                                 offset));
1164                 if (!new_page)
1165                         break;
1166                 page_cache_release(new_page);
1167         }
1168         lru_add_drain();        /* Push any new pages onto the LRU now */
1169 }
1170
1171 /*
1172  * We hold the mm semaphore and the page_table_lock on entry and
1173  * should release the pagetable lock on exit..
1174  */
1175 static int do_swap_page(struct mm_struct * mm,
1176         struct vm_area_struct * vma, unsigned long address,
1177         pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1178 {
1179         struct page *page;
1180         swp_entry_t entry = pte_to_swp_entry(orig_pte);
1181         pte_t pte;
1182         int ret = VM_FAULT_MINOR;
1183         struct pte_chain *pte_chain = NULL;
1184
1185         pte_unmap(page_table);
1186         spin_unlock(&mm->page_table_lock);
1187         page = lookup_swap_cache(entry);
1188         if (!page) {
1189                 swapin_readahead(entry);
1190                 page = read_swap_cache_async(entry);
1191                 if (!page) {
1192                         /*
1193                          * Back out if somebody else faulted in this pte while
1194                          * we released the page table lock.
1195                          */
1196                         spin_lock(&mm->page_table_lock);
1197                         page_table = pte_offset_map(pmd, address);
1198                         if (pte_same(*page_table, orig_pte))
1199                                 ret = VM_FAULT_OOM;
1200                         else
1201                                 ret = VM_FAULT_MINOR;
1202                         pte_unmap(page_table);
1203                         spin_unlock(&mm->page_table_lock);
1204                         goto out;
1205                 }
1206
1207                 /* Had to read the page from swap area: Major fault */
1208                 ret = VM_FAULT_MAJOR;
1209                 inc_page_state(pgmajfault);
1210         }
1211
1212         mark_page_accessed(page);
1213         pte_chain = pte_chain_alloc(GFP_KERNEL);
1214         if (!pte_chain) {
1215                 ret = -ENOMEM;
1216                 goto out;
1217         }
1218         lock_page(page);
1219
1220         /*
1221          * Back out if somebody else faulted in this pte while we
1222          * released the page table lock.
1223          */
1224         spin_lock(&mm->page_table_lock);
1225         page_table = pte_offset_map(pmd, address);
1226         if (!pte_same(*page_table, orig_pte)) {
1227                 pte_unmap(page_table);
1228                 spin_unlock(&mm->page_table_lock);
1229                 unlock_page(page);
1230                 page_cache_release(page);
1231                 ret = VM_FAULT_MINOR;
1232                 goto out;
1233         }
1234
1235         /* The page isn't present yet, go ahead with the fault. */
1236                 
1237         swap_free(entry);
1238         if (vm_swap_full())
1239                 remove_exclusive_swap_page(page);
1240
1241         mm->rss++;
1242         pte = mk_pte(page, vma->vm_page_prot);
1243         if (write_access && can_share_swap_page(page))
1244                 pte = pte_mkdirty(pte_mkwrite(pte));
1245         unlock_page(page);
1246
1247         flush_icache_page(vma, page);
1248         set_pte(page_table, pte);
1249         pte_chain = page_add_rmap(page, page_table, pte_chain);
1250
1251         /* No need to invalidate - it was non-present before */
1252         update_mmu_cache(vma, address, pte);
1253         pte_unmap(page_table);
1254         spin_unlock(&mm->page_table_lock);
1255 out:
1256         pte_chain_free(pte_chain);
1257         return ret;
1258 }
1259
1260 /*
1261  * We are called with the MM semaphore and page_table_lock
1262  * spinlock held to protect against concurrent faults in
1263  * multithreaded programs. 
1264  */
1265 static int
1266 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1267                 pte_t *page_table, pmd_t *pmd, int write_access,
1268                 unsigned long addr)
1269 {
1270         pte_t entry;
1271         struct page * page = ZERO_PAGE(addr);
1272         struct pte_chain *pte_chain;
1273         int ret;
1274
1275         pte_chain = pte_chain_alloc(GFP_ATOMIC);
1276         if (!pte_chain) {
1277                 pte_unmap(page_table);
1278                 spin_unlock(&mm->page_table_lock);
1279                 pte_chain = pte_chain_alloc(GFP_KERNEL);
1280                 if (!pte_chain)
1281                         goto no_mem;
1282                 spin_lock(&mm->page_table_lock);
1283                 page_table = pte_offset_map(pmd, addr);
1284         }
1285                 
1286         /* Read-only mapping of ZERO_PAGE. */
1287         entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1288
1289         /* ..except if it's a write access */
1290         if (write_access) {
1291                 /* Allocate our own private page. */
1292                 pte_unmap(page_table);
1293                 spin_unlock(&mm->page_table_lock);
1294
1295                 page = alloc_page(GFP_HIGHUSER);
1296                 if (!page)
1297                         goto no_mem;
1298                 clear_user_highpage(page, addr);
1299
1300                 spin_lock(&mm->page_table_lock);
1301                 page_table = pte_offset_map(pmd, addr);
1302
1303                 if (!pte_none(*page_table)) {
1304                         pte_unmap(page_table);
1305                         page_cache_release(page);
1306                         spin_unlock(&mm->page_table_lock);
1307                         ret = VM_FAULT_MINOR;
1308                         goto out;
1309                 }
1310                 mm->rss++;
1311                 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1312                 lru_cache_add_active(page);
1313                 mark_page_accessed(page);
1314         }
1315
1316         set_pte(page_table, entry);
1317         /* ignores ZERO_PAGE */
1318         pte_chain = page_add_rmap(page, page_table, pte_chain);
1319         pte_unmap(page_table);
1320
1321         /* No need to invalidate - it was non-present before */
1322         update_mmu_cache(vma, addr, entry);
1323         spin_unlock(&mm->page_table_lock);
1324         ret = VM_FAULT_MINOR;
1325         goto out;
1326
1327 no_mem:
1328         ret = VM_FAULT_OOM;
1329 out:
1330         pte_chain_free(pte_chain);
1331         return ret;
1332 }
1333
1334 /*
1335  * do_no_page() tries to create a new page mapping. It aggressively
1336  * tries to share with existing pages, but makes a separate copy if
1337  * the "write_access" parameter is true in order to avoid the next
1338  * page fault.
1339  *
1340  * As this is called only for pages that do not currently exist, we
1341  * do not need to flush old virtual caches or the TLB.
1342  *
1343  * This is called with the MM semaphore held and the page table
1344  * spinlock held. Exit with the spinlock released.
1345  */
1346 static int
1347 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1348         unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1349 {
1350         struct page * new_page;
1351         pte_t entry;
1352         struct pte_chain *pte_chain;
1353         int ret;
1354
1355         if (!vma->vm_ops || !vma->vm_ops->nopage)
1356                 return do_anonymous_page(mm, vma, page_table,
1357                                         pmd, write_access, address);
1358         pte_unmap(page_table);
1359         spin_unlock(&mm->page_table_lock);
1360
1361         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1362
1363         /* no page was available -- either SIGBUS or OOM */
1364         if (new_page == NOPAGE_SIGBUS)
1365                 return VM_FAULT_SIGBUS;
1366         if (new_page == NOPAGE_OOM)
1367                 return VM_FAULT_OOM;
1368
1369         pte_chain = pte_chain_alloc(GFP_KERNEL);
1370         if (!pte_chain)
1371                 goto oom;
1372
1373         /*
1374          * Should we do an early C-O-W break?
1375          */
1376         if (write_access && !(vma->vm_flags & VM_SHARED)) {
1377                 struct page * page = alloc_page(GFP_HIGHUSER);
1378                 if (!page) {
1379                         page_cache_release(new_page);
1380                         goto oom;
1381                 }
1382                 copy_user_highpage(page, new_page, address);
1383                 page_cache_release(new_page);
1384                 lru_cache_add_active(page);
1385                 new_page = page;
1386         }
1387
1388         spin_lock(&mm->page_table_lock);
1389         page_table = pte_offset_map(pmd, address);
1390
1391         /*
1392          * This silly early PAGE_DIRTY setting removes a race
1393          * due to the bad i386 page protection. But it's valid
1394          * for other architectures too.
1395          *
1396          * Note that if write_access is true, we either now have
1397          * an exclusive copy of the page, or this is a shared mapping,
1398          * so we can make it writable and dirty to avoid having to
1399          * handle that later.
1400          */
1401         /* Only go through if we didn't race with anybody else... */
1402         if (pte_none(*page_table)) {
1403                 ++mm->rss;
1404                 flush_icache_page(vma, new_page);
1405                 entry = mk_pte(new_page, vma->vm_page_prot);
1406                 if (write_access)
1407                         entry = pte_mkwrite(pte_mkdirty(entry));
1408                 set_pte(page_table, entry);
1409                 pte_chain = page_add_rmap(new_page, page_table, pte_chain);
1410                 pte_unmap(page_table);
1411         } else {
1412                 /* One of our sibling threads was faster, back out. */
1413                 pte_unmap(page_table);
1414                 page_cache_release(new_page);
1415                 spin_unlock(&mm->page_table_lock);
1416                 ret = VM_FAULT_MINOR;
1417                 goto out;
1418         }
1419
1420         /* no need to invalidate: a not-present page shouldn't be cached */
1421         update_mmu_cache(vma, address, entry);
1422         spin_unlock(&mm->page_table_lock);
1423         ret = VM_FAULT_MAJOR;
1424         goto out;
1425 oom:
1426         ret = VM_FAULT_OOM;
1427 out:
1428         pte_chain_free(pte_chain);
1429         return ret;
1430 }
1431
1432 /*
1433  * Fault of a previously existing named mapping. Repopulate the pte
1434  * from the encoded file_pte if possible. This enables swappable
1435  * nonlinear vmas.
1436  */
1437 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1438         unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1439 {
1440         unsigned long pgoff;
1441         int err;
1442
1443         BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1444         /*
1445          * Fall back to the linear mapping if the fs does not support
1446          * ->populate:
1447          */
1448         if (!vma->vm_ops || !vma->vm_ops->populate || 
1449                         (write_access && !(vma->vm_flags & VM_SHARED))) {
1450                 pte_clear(pte);
1451                 return do_no_page(mm, vma, address, write_access, pte, pmd);
1452         }
1453
1454         pgoff = pte_to_pgoff(*pte);
1455
1456         pte_unmap(pte);
1457         spin_unlock(&mm->page_table_lock);
1458
1459         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1460         if (err == -ENOMEM)
1461                 return VM_FAULT_OOM;
1462         if (err)
1463                 return VM_FAULT_SIGBUS;
1464         return VM_FAULT_MAJOR;
1465 }
1466
1467 /*
1468  * These routines also need to handle stuff like marking pages dirty
1469  * and/or accessed for architectures that don't do it in hardware (most
1470  * RISC architectures).  The early dirtying is also good on the i386.
1471  *
1472  * There is also a hook called "update_mmu_cache()" that architectures
1473  * with external mmu caches can use to update those (ie the Sparc or
1474  * PowerPC hashed page tables that act as extended TLBs).
1475  *
1476  * Note the "page_table_lock". It is to protect against kswapd removing
1477  * pages from under us. Note that kswapd only ever _removes_ pages, never
1478  * adds them. As such, once we have noticed that the page is not present,
1479  * we can drop the lock early.
1480  *
1481  * The adding of pages is protected by the MM semaphore (which we hold),
1482  * so we don't need to worry about a page being suddenly been added into
1483  * our VM.
1484  *
1485  * We enter with the pagetable spinlock held, we are supposed to
1486  * release it when done.
1487  */
1488 static inline int handle_pte_fault(struct mm_struct *mm,
1489         struct vm_area_struct * vma, unsigned long address,
1490         int write_access, pte_t *pte, pmd_t *pmd)
1491 {
1492         pte_t entry;
1493
1494         entry = *pte;
1495         if (!pte_present(entry)) {
1496                 /*
1497                  * If it truly wasn't present, we know that kswapd
1498                  * and the PTE updates will not touch it later. So
1499                  * drop the lock.
1500                  */
1501                 if (pte_none(entry))
1502                         return do_no_page(mm, vma, address, write_access, pte, pmd);
1503                 if (pte_file(entry))
1504                         return do_file_page(mm, vma, address, write_access, pte, pmd);
1505                 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1506         }
1507
1508         if (write_access) {
1509                 if (!pte_write(entry))
1510                         return do_wp_page(mm, vma, address, pte, pmd, entry);
1511
1512                 entry = pte_mkdirty(entry);
1513         }
1514         entry = pte_mkyoung(entry);
1515         establish_pte(vma, address, pte, entry);
1516         pte_unmap(pte);
1517         spin_unlock(&mm->page_table_lock);
1518         return VM_FAULT_MINOR;
1519 }
1520
1521 /*
1522  * By the time we get here, we already hold the mm semaphore
1523  */
1524 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1525         unsigned long address, int write_access)
1526 {
1527         pgd_t *pgd;
1528         pmd_t *pmd;
1529
1530         __set_current_state(TASK_RUNNING);
1531         pgd = pgd_offset(mm, address);
1532
1533         inc_page_state(pgfault);
1534
1535         if (is_vm_hugetlb_page(vma))
1536                 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1537
1538         /*
1539          * We need the page table lock to synchronize with kswapd
1540          * and the SMP-safe atomic PTE updates.
1541          */
1542         spin_lock(&mm->page_table_lock);
1543         pmd = pmd_alloc(mm, pgd, address);
1544
1545         if (pmd) {
1546                 pte_t * pte = pte_alloc_map(mm, pmd, address);
1547                 if (pte)
1548                         return handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1549         }
1550         spin_unlock(&mm->page_table_lock);
1551         return VM_FAULT_OOM;
1552 }
1553
1554 /*
1555  * Allocate page middle directory.
1556  *
1557  * We've already handled the fast-path in-line, and we own the
1558  * page table lock.
1559  *
1560  * On a two-level page table, this ends up actually being entirely
1561  * optimized away.
1562  */
1563 pmd_t *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1564 {
1565         pmd_t *new;
1566
1567         spin_unlock(&mm->page_table_lock);
1568         new = pmd_alloc_one(mm, address);
1569         spin_lock(&mm->page_table_lock);
1570         if (!new)
1571                 return NULL;
1572
1573         /*
1574          * Because we dropped the lock, we should re-check the
1575          * entry, as somebody else could have populated it..
1576          */
1577         if (pgd_present(*pgd)) {
1578                 pmd_free(new);
1579                 goto out;
1580         }
1581         pgd_populate(mm, pgd, new);
1582 out:
1583         return pmd_offset(pgd, address);
1584 }
1585
1586 int make_pages_present(unsigned long addr, unsigned long end)
1587 {
1588         int ret, len, write;
1589         struct vm_area_struct * vma;
1590
1591         vma = find_vma(current->mm, addr);
1592         write = (vma->vm_flags & VM_WRITE) != 0;
1593         if (addr >= end)
1594                 BUG();
1595         if (end > vma->vm_end)
1596                 BUG();
1597         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1598         ret = get_user_pages(current, current->mm, addr,
1599                         len, write, 0, NULL, NULL);
1600         return ret == len ? 0 : -1;
1601 }
1602
1603 /* 
1604  * Map a vmalloc()-space virtual address to the physical page.
1605  */
1606 struct page * vmalloc_to_page(void * vmalloc_addr)
1607 {
1608         unsigned long addr = (unsigned long) vmalloc_addr;
1609         struct page *page = NULL;
1610         pgd_t *pgd = pgd_offset_k(addr);
1611         pmd_t *pmd;
1612         pte_t *ptep, pte;
1613   
1614         if (!pgd_none(*pgd)) {
1615                 pmd = pmd_offset(pgd, addr);
1616                 if (!pmd_none(*pmd)) {
1617                         preempt_disable();
1618                         ptep = pte_offset_map(pmd, addr);
1619                         pte = *ptep;
1620                         if (pte_present(pte))
1621                                 page = pte_page(pte);
1622                         pte_unmap(ptep);
1623                         preempt_enable();
1624                 }
1625         }
1626         return page;
1627 }