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