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