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