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