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