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