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