Merge to ckrm-E15
[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 (unlikely(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                 pfn = pte_pfn(pte);
703                 if (pfn_valid(pfn)) {
704                         page = pfn_to_page(pfn);
705                         if (write && !pte_dirty(pte) && !PageDirty(page))
706                                 set_page_dirty(page);
707                         mark_page_accessed(page);
708                         return page;
709                 } else {
710                         *pfn_ptr = pfn;
711                         return NULL;
712                 }
713         }
714
715 out:
716         return NULL;
717 }
718
719
720 /* 
721  * Given a physical address, is there a useful struct page pointing to
722  * it?  This may become more complex in the future if we start dealing
723  * with IO-aperture pages for direct-IO.
724  */
725
726 static inline struct page *get_page_map(struct page *page)
727 {
728         if (!pfn_valid(page_to_pfn(page)))
729                 return 0;
730         return page;
731 }
732
733
734 #ifndef CONFIG_X86_4G
735 static inline int
736 untouched_anonymous_page(struct mm_struct* mm, struct vm_area_struct *vma,
737                          unsigned long address)
738 {
739         pgd_t *pgd;
740         pmd_t *pmd;
741
742         /* Check if the vma is for an anonymous mapping. */
743         if (vma->vm_ops && vma->vm_ops->nopage)
744                 return 0;
745
746         /* Check if page directory entry exists. */
747         pgd = pgd_offset(mm, address);
748         if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
749                 return 1;
750
751         /* Check if page middle directory entry exists. */
752         pmd = pmd_offset(pgd, address);
753         if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd)))
754                 return 1;
755
756         /* There is a pte slot for 'address' in 'mm'. */
757         return 0;
758 }
759 #endif
760
761
762 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm,
763                 unsigned long start, int len, int write, int force,
764                 struct page **pages, struct vm_area_struct **vmas)
765 {
766         int i;
767         unsigned int flags;
768
769         /* 
770          * Require read or write permissions.
771          * If 'force' is set, we only require the "MAY" flags.
772          */
773         flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
774         flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
775         i = 0;
776
777         do {
778                 struct vm_area_struct * vma;
779
780                 vma = find_extend_vma(mm, start);
781                 if (!vma && in_gate_area(tsk, start)) {
782                         unsigned long pg = start & PAGE_MASK;
783                         struct vm_area_struct *gate_vma = get_gate_vma(tsk);
784                         pgd_t *pgd;
785                         pmd_t *pmd;
786                         pte_t *pte;
787                         if (write) /* user gate pages are read-only */
788                                 return i ? : -EFAULT;
789                         pgd = pgd_offset_k(pg);
790                         if (!pgd)
791                                 return i ? : -EFAULT;
792                         pmd = pmd_offset(pgd, pg);
793                         if (!pmd)
794                                 return i ? : -EFAULT;
795                         pte = pte_offset_kernel(pmd, pg);
796                         if (!pte || !pte_present(*pte))
797                                 return i ? : -EFAULT;
798                         if (pages) {
799                                 pages[i] = pte_page(*pte);
800                                 get_page(pages[i]);
801                         }
802                         if (vmas)
803                                 vmas[i] = gate_vma;
804                         i++;
805                         start += PAGE_SIZE;
806                         len--;
807                         continue;
808                 }
809
810                 if (!vma || (pages && (vma->vm_flags & VM_IO))
811                                 || !(flags & vma->vm_flags))
812                         return i ? : -EFAULT;
813
814                 if (is_vm_hugetlb_page(vma)) {
815                         i = follow_hugetlb_page(mm, vma, pages, vmas,
816                                                 &start, &len, i);
817                         continue;
818                 }
819                 spin_lock(&mm->page_table_lock);
820                 do {
821                         struct page *map;
822                         int lookup_write = write;
823                         while (!(map = follow_page(mm, start, lookup_write))) {
824                                 /*
825                                  * Shortcut for anonymous pages. We don't want
826                                  * to force the creation of pages tables for
827                                  * insanly big anonymously mapped areas that
828                                  * nobody touched so far. This is important
829                                  * for doing a core dump for these mappings.
830                                  *
831                                  * disable this for 4:4 - it prevents
832                                  * follow_page() from ever seeing these pages.
833                                  *
834                                  * (The 'fix' is dubious anyway, there's
835                                  * nothing that this code avoids which couldnt
836                                  * be triggered from userspace anyway.)
837                                  */
838 #ifndef CONFIG_X86_4G
839                                 if (!lookup_write &&
840                                     untouched_anonymous_page(mm,vma,start)) {
841                                         map = ZERO_PAGE(start);
842                                         break;
843                                 }
844 #endif
845                                 spin_unlock(&mm->page_table_lock);
846                                 switch (handle_mm_fault(mm,vma,start,write)) {
847                                 case VM_FAULT_MINOR:
848                                         tsk->min_flt++;
849                                         break;
850                                 case VM_FAULT_MAJOR:
851                                         tsk->maj_flt++;
852                                         break;
853                                 case VM_FAULT_SIGBUS:
854                                         return i ? i : -EFAULT;
855                                 case VM_FAULT_OOM:
856                                         return i ? i : -ENOMEM;
857                                 default:
858                                         BUG();
859                                 }
860                                 /*
861                                  * Now that we have performed a write fault
862                                  * and surely no longer have a shared page we
863                                  * shouldn't write, we shouldn't ignore an
864                                  * unwritable page in the page table if
865                                  * we are forcing write access.
866                                  */
867                                 lookup_write = write && !force;
868                                 spin_lock(&mm->page_table_lock);
869                         }
870                         if (pages) {
871                                 pages[i] = get_page_map(map);
872                                 if (!pages[i]) {
873                                         spin_unlock(&mm->page_table_lock);
874                                         while (i--)
875                                                 page_cache_release(pages[i]);
876                                         i = -EFAULT;
877                                         goto out;
878                                 }
879                                 flush_dcache_page(pages[i]);
880                                 if (!PageReserved(pages[i]))
881                                         page_cache_get(pages[i]);
882                         }
883                         if (vmas)
884                                 vmas[i] = vma;
885                         i++;
886                         start += PAGE_SIZE;
887                         len--;
888                 } while(len && start < vma->vm_end);
889                 spin_unlock(&mm->page_table_lock);
890         } while(len);
891 out:
892         return i;
893 }
894
895 EXPORT_SYMBOL(get_user_pages);
896
897 static void zeromap_pte_range(pte_t * pte, unsigned long address,
898                                      unsigned long size, pgprot_t prot)
899 {
900         unsigned long end;
901
902         address &= ~PMD_MASK;
903         end = address + size;
904         if (end > PMD_SIZE)
905                 end = PMD_SIZE;
906         do {
907                 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
908                 BUG_ON(!pte_none(*pte));
909                 set_pte(pte, zero_pte);
910                 address += PAGE_SIZE;
911                 pte++;
912         } while (address && (address < end));
913 }
914
915 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
916                                     unsigned long size, pgprot_t prot)
917 {
918         unsigned long base, end;
919
920         base = address & PGDIR_MASK;
921         address &= ~PGDIR_MASK;
922         end = address + size;
923         if (end > PGDIR_SIZE)
924                 end = PGDIR_SIZE;
925         do {
926                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
927                 if (!pte)
928                         return -ENOMEM;
929                 zeromap_pte_range(pte, base + address, end - address, prot);
930                 pte_unmap(pte);
931                 address = (address + PMD_SIZE) & PMD_MASK;
932                 pmd++;
933         } while (address && (address < end));
934         return 0;
935 }
936
937 int zeromap_page_range(struct vm_area_struct *vma, unsigned long address, unsigned long size, pgprot_t prot)
938 {
939         int error = 0;
940         pgd_t * dir;
941         unsigned long beg = address;
942         unsigned long end = address + size;
943         struct mm_struct *mm = vma->vm_mm;
944
945         dir = pgd_offset(mm, address);
946         flush_cache_range(vma, beg, end);
947         if (address >= end)
948                 BUG();
949
950         spin_lock(&mm->page_table_lock);
951         do {
952                 pmd_t *pmd = pmd_alloc(mm, dir, address);
953                 error = -ENOMEM;
954                 if (!pmd)
955                         break;
956                 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
957                 if (error)
958                         break;
959                 address = (address + PGDIR_SIZE) & PGDIR_MASK;
960                 dir++;
961         } while (address && (address < end));
962         /*
963          * Why flush? zeromap_pte_range has a BUG_ON for !pte_none()
964          */
965         flush_tlb_range(vma, beg, end);
966         spin_unlock(&mm->page_table_lock);
967         return error;
968 }
969
970 /*
971  * maps a range of physical memory into the requested pages. the old
972  * mappings are removed. any references to nonexistent pages results
973  * in null mappings (currently treated as "copy-on-access")
974  */
975 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
976         unsigned long phys_addr, pgprot_t prot)
977 {
978         unsigned long end;
979         unsigned long pfn;
980
981         address &= ~PMD_MASK;
982         end = address + size;
983         if (end > PMD_SIZE)
984                 end = PMD_SIZE;
985         pfn = phys_addr >> PAGE_SHIFT;
986         do {
987                 BUG_ON(!pte_none(*pte));
988                 if (!pfn_valid(pfn) || PageReserved(pfn_to_page(pfn)))
989                         set_pte(pte, pfn_pte(pfn, prot));
990                 address += PAGE_SIZE;
991                 pfn++;
992                 pte++;
993         } while (address && (address < end));
994 }
995
996 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
997         unsigned long phys_addr, pgprot_t prot)
998 {
999         unsigned long base, end;
1000
1001         base = address & PGDIR_MASK;
1002         address &= ~PGDIR_MASK;
1003         end = address + size;
1004         if (end > PGDIR_SIZE)
1005                 end = PGDIR_SIZE;
1006         phys_addr -= address;
1007         do {
1008                 pte_t * pte = pte_alloc_map(mm, pmd, base + address);
1009                 if (!pte)
1010                         return -ENOMEM;
1011                 remap_pte_range(pte, base + address, end - address, address + phys_addr, prot);
1012                 pte_unmap(pte);
1013                 address = (address + PMD_SIZE) & PMD_MASK;
1014                 pmd++;
1015         } while (address && (address < end));
1016         return 0;
1017 }
1018
1019 /*  Note: this is only safe if the mm semaphore is held when called. */
1020 int remap_page_range(struct vm_area_struct *vma, unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
1021 {
1022         int error = 0;
1023         pgd_t * dir;
1024         unsigned long beg = from;
1025         unsigned long end = from + size;
1026         struct mm_struct *mm = vma->vm_mm;
1027
1028         phys_addr -= from;
1029         dir = pgd_offset(mm, from);
1030         flush_cache_range(vma, beg, end);
1031         if (from >= end)
1032                 BUG();
1033
1034         spin_lock(&mm->page_table_lock);
1035         do {
1036                 pmd_t *pmd = pmd_alloc(mm, dir, from);
1037                 error = -ENOMEM;
1038                 if (!pmd)
1039                         break;
1040                 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
1041                 if (error)
1042                         break;
1043                 from = (from + PGDIR_SIZE) & PGDIR_MASK;
1044                 dir++;
1045         } while (from && (from < end));
1046         /*
1047          * Why flush? remap_pte_range has a BUG_ON for !pte_none()
1048          */
1049         flush_tlb_range(vma, beg, end);
1050         spin_unlock(&mm->page_table_lock);
1051         return error;
1052 }
1053
1054 EXPORT_SYMBOL(remap_page_range);
1055
1056 /*
1057  * Do pte_mkwrite, but only if the vma says VM_WRITE.  We do this when
1058  * servicing faults for write access.  In the normal case, do always want
1059  * pte_mkwrite.  But get_user_pages can cause write faults for mappings
1060  * that do not have writing enabled, when used by access_process_vm.
1061  */
1062 static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma)
1063 {
1064         if (likely(vma->vm_flags & VM_WRITE))
1065                 pte = pte_mkwrite(pte);
1066         return pte;
1067 }
1068
1069 /*
1070  * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
1071  */
1072 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address, 
1073                 pte_t *page_table)
1074 {
1075         pte_t entry;
1076
1077         flush_cache_page(vma, address);
1078         entry = maybe_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot)),
1079                               vma);
1080         ptep_establish(vma, address, page_table, entry);
1081         update_mmu_cache(vma, address, entry);
1082 }
1083
1084 /*
1085  * This routine handles present pages, when users try to write
1086  * to a shared page. It is done by copying the page to a new address
1087  * and decrementing the shared-page counter for the old page.
1088  *
1089  * Goto-purists beware: the only reason for goto's here is that it results
1090  * in better assembly code.. The "default" path will see no jumps at all.
1091  *
1092  * Note that this routine assumes that the protection checks have been
1093  * done by the caller (the low-level page fault routine in most cases).
1094  * Thus we can safely just mark it writable once we've done any necessary
1095  * COW.
1096  *
1097  * We also mark the page dirty at this point even though the page will
1098  * change only once the write actually happens. This avoids a few races,
1099  * and potentially makes it more efficient.
1100  *
1101  * We hold the mm semaphore and the page_table_lock on entry and exit
1102  * with the page_table_lock released.
1103  */
1104 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
1105         unsigned long address, pte_t *page_table, pmd_t *pmd, pte_t pte)
1106 {
1107         struct page *old_page, *new_page;
1108         unsigned long pfn = pte_pfn(pte);
1109         pte_t entry;
1110
1111         if (unlikely(!pfn_valid(pfn))) {
1112                 /*
1113                  * This should really halt the system so it can be debugged or
1114                  * at least the kernel stops what it's doing before it corrupts
1115                  * data, but for the moment just pretend this is OOM.
1116                  */
1117                 pte_unmap(page_table);
1118                 printk(KERN_ERR "do_wp_page: bogus page at address %08lx\n",
1119                                 address);
1120                 spin_unlock(&mm->page_table_lock);
1121                 return VM_FAULT_OOM;
1122         }
1123         old_page = pfn_to_page(pfn);
1124
1125         if (!TestSetPageLocked(old_page)) {
1126                 int reuse = can_share_swap_page(old_page);
1127                 unlock_page(old_page);
1128                 if (reuse) {
1129                         flush_cache_page(vma, address);
1130                         entry = maybe_mkwrite(pte_mkyoung(pte_mkdirty(pte)),
1131                                               vma);
1132                         ptep_set_access_flags(vma, address, page_table, entry, 1);
1133                         update_mmu_cache(vma, address, entry);
1134                         pte_unmap(page_table);
1135                         spin_unlock(&mm->page_table_lock);
1136                         return VM_FAULT_MINOR;
1137                 }
1138         }
1139         pte_unmap(page_table);
1140
1141         /*
1142          * Ok, we need to copy. Oh, well..
1143          */
1144         page_cache_get(old_page);
1145         spin_unlock(&mm->page_table_lock);
1146
1147         if (unlikely(anon_vma_prepare(vma)))
1148                 goto no_new_page;
1149         new_page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1150         if (!new_page)
1151                 goto no_new_page;
1152         copy_cow_page(old_page,new_page,address);
1153
1154         /*
1155          * Re-check the pte - we dropped the lock
1156          */
1157         spin_lock(&mm->page_table_lock);
1158         page_table = pte_offset_map(pmd, address);
1159         if (likely(pte_same(*page_table, pte))) {
1160                 if (PageReserved(old_page))
1161                         // ++mm->rss;
1162                         vx_rsspages_inc(mm);
1163                 else
1164                         page_remove_rmap(old_page);
1165                 break_cow(vma, new_page, address, page_table);
1166                 lru_cache_add_active(new_page);
1167                 page_add_anon_rmap(new_page, vma, address);
1168
1169                 /* Free the old page.. */
1170                 new_page = old_page;
1171         }
1172         pte_unmap(page_table);
1173         page_cache_release(new_page);
1174         page_cache_release(old_page);
1175         spin_unlock(&mm->page_table_lock);
1176         return VM_FAULT_MINOR;
1177
1178 no_new_page:
1179         page_cache_release(old_page);
1180         return VM_FAULT_OOM;
1181 }
1182
1183 /*
1184  * Helper function for unmap_mapping_range().
1185  */
1186 static inline void unmap_mapping_range_list(struct prio_tree_root *root,
1187                                             struct zap_details *details)
1188 {
1189         struct vm_area_struct *vma = NULL;
1190         struct prio_tree_iter iter;
1191         pgoff_t vba, vea, zba, zea;
1192
1193         while ((vma = vma_prio_tree_next(vma, root, &iter,
1194                         details->first_index, details->last_index)) != NULL) {
1195                 vba = vma->vm_pgoff;
1196                 vea = vba + ((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) - 1;
1197                 /* Assume for now that PAGE_CACHE_SHIFT == PAGE_SHIFT */
1198                 zba = details->first_index;
1199                 if (zba < vba)
1200                         zba = vba;
1201                 zea = details->last_index;
1202                 if (zea > vea)
1203                         zea = vea;
1204                 zap_page_range(vma,
1205                         ((zba - vba) << PAGE_SHIFT) + vma->vm_start,
1206                         (zea - zba + 1) << PAGE_SHIFT, details);
1207         }
1208 }
1209
1210 /**
1211  * unmap_mapping_range - unmap the portion of all mmaps
1212  * in the specified address_space corresponding to the specified
1213  * page range in the underlying file.
1214  * @address_space: the address space containing mmaps to be unmapped.
1215  * @holebegin: byte in first page to unmap, relative to the start of
1216  * the underlying file.  This will be rounded down to a PAGE_SIZE
1217  * boundary.  Note that this is different from vmtruncate(), which
1218  * must keep the partial page.  In contrast, we must get rid of
1219  * partial pages.
1220  * @holelen: size of prospective hole in bytes.  This will be rounded
1221  * up to a PAGE_SIZE boundary.  A holelen of zero truncates to the
1222  * end of the file.
1223  * @even_cows: 1 when truncating a file, unmap even private COWed pages;
1224  * but 0 when invalidating pagecache, don't throw away private data.
1225  */
1226 void unmap_mapping_range(struct address_space *mapping,
1227                 loff_t const holebegin, loff_t const holelen, int even_cows)
1228 {
1229         struct zap_details details;
1230         pgoff_t hba = holebegin >> PAGE_SHIFT;
1231         pgoff_t hlen = (holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1232
1233         /* Check for overflow. */
1234         if (sizeof(holelen) > sizeof(hlen)) {
1235                 long long holeend =
1236                         (holebegin + holelen + PAGE_SIZE - 1) >> PAGE_SHIFT;
1237                 if (holeend & ~(long long)ULONG_MAX)
1238                         hlen = ULONG_MAX - hba + 1;
1239         }
1240
1241         details.check_mapping = even_cows? NULL: mapping;
1242         details.nonlinear_vma = NULL;
1243         details.first_index = hba;
1244         details.last_index = hba + hlen - 1;
1245         details.atomic = 1;     /* A spinlock is held */
1246         if (details.last_index < details.first_index)
1247                 details.last_index = ULONG_MAX;
1248
1249         spin_lock(&mapping->i_mmap_lock);
1250         /* Protect against page fault */
1251         atomic_inc(&mapping->truncate_count);
1252
1253         if (unlikely(!prio_tree_empty(&mapping->i_mmap)))
1254                 unmap_mapping_range_list(&mapping->i_mmap, &details);
1255
1256         /*
1257          * In nonlinear VMAs there is no correspondence between virtual address
1258          * offset and file offset.  So we must perform an exhaustive search
1259          * across *all* the pages in each nonlinear VMA, not just the pages
1260          * whose virtual address lies outside the file truncation point.
1261          */
1262         if (unlikely(!list_empty(&mapping->i_mmap_nonlinear))) {
1263                 struct vm_area_struct *vma;
1264                 list_for_each_entry(vma, &mapping->i_mmap_nonlinear,
1265                                                 shared.vm_set.list) {
1266                         details.nonlinear_vma = vma;
1267                         zap_page_range(vma, vma->vm_start,
1268                                 vma->vm_end - vma->vm_start, &details);
1269                 }
1270         }
1271         spin_unlock(&mapping->i_mmap_lock);
1272 }
1273 EXPORT_SYMBOL(unmap_mapping_range);
1274
1275 /*
1276  * Handle all mappings that got truncated by a "truncate()"
1277  * system call.
1278  *
1279  * NOTE! We have to be ready to update the memory sharing
1280  * between the file and the memory map for a potential last
1281  * incomplete page.  Ugly, but necessary.
1282  */
1283 int vmtruncate(struct inode * inode, loff_t offset)
1284 {
1285         struct address_space *mapping = inode->i_mapping;
1286         unsigned long limit;
1287
1288         if (inode->i_size < offset)
1289                 goto do_expand;
1290         i_size_write(inode, offset);
1291         unmap_mapping_range(mapping, offset + PAGE_SIZE - 1, 0, 1);
1292         truncate_inode_pages(mapping, offset);
1293         goto out_truncate;
1294
1295 do_expand:
1296         limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1297         if (limit != RLIM_INFINITY && offset > limit)
1298                 goto out_sig;
1299         if (offset > inode->i_sb->s_maxbytes)
1300                 goto out;
1301         i_size_write(inode, offset);
1302
1303 out_truncate:
1304         if (inode->i_op && inode->i_op->truncate)
1305                 inode->i_op->truncate(inode);
1306         return 0;
1307 out_sig:
1308         send_sig(SIGXFSZ, current, 0);
1309 out:
1310         return -EFBIG;
1311 }
1312
1313 EXPORT_SYMBOL(vmtruncate);
1314
1315 /* 
1316  * Primitive swap readahead code. We simply read an aligned block of
1317  * (1 << page_cluster) entries in the swap area. This method is chosen
1318  * because it doesn't cost us any seek time.  We also make sure to queue
1319  * the 'original' request together with the readahead ones...  
1320  *
1321  * This has been extended to use the NUMA policies from the mm triggering
1322  * the readahead.
1323  *
1324  * Caller must hold down_read on the vma->vm_mm if vma is not NULL.
1325  */
1326 void swapin_readahead(swp_entry_t entry, unsigned long addr,struct vm_area_struct *vma)
1327 {
1328 #ifdef CONFIG_NUMA
1329         struct vm_area_struct *next_vma = vma ? vma->vm_next : NULL;
1330 #endif
1331         int i, num;
1332         struct page *new_page;
1333         unsigned long offset;
1334
1335         /*
1336          * Get the number of handles we should do readahead io to.
1337          */
1338         num = valid_swaphandles(entry, &offset);
1339         for (i = 0; i < num; offset++, i++) {
1340                 /* Ok, do the async read-ahead now */
1341                 new_page = read_swap_cache_async(swp_entry(swp_type(entry),
1342                                                            offset), vma, addr);
1343                 if (!new_page)
1344                         break;
1345                 page_cache_release(new_page);
1346 #ifdef CONFIG_NUMA
1347                 /*
1348                  * Find the next applicable VMA for the NUMA policy.
1349                  */
1350                 addr += PAGE_SIZE;
1351                 if (addr == 0)
1352                         vma = NULL;
1353                 if (vma) {
1354                         if (addr >= vma->vm_end) {
1355                                 vma = next_vma;
1356                                 next_vma = vma ? vma->vm_next : NULL;
1357                         }
1358                         if (vma && addr < vma->vm_start)
1359                                 vma = NULL;
1360                 } else {
1361                         if (next_vma && addr >= next_vma->vm_start) {
1362                                 vma = next_vma;
1363                                 next_vma = vma->vm_next;
1364                         }
1365                 }
1366 #endif
1367         }
1368         lru_add_drain();        /* Push any new pages onto the LRU now */
1369 }
1370
1371 /*
1372  * We hold the mm semaphore and the page_table_lock on entry and
1373  * should release the pagetable lock on exit..
1374  */
1375 static int do_swap_page(struct mm_struct * mm,
1376         struct vm_area_struct * vma, unsigned long address,
1377         pte_t *page_table, pmd_t *pmd, pte_t orig_pte, int write_access)
1378 {
1379         struct page *page;
1380         swp_entry_t entry = pte_to_swp_entry(orig_pte);
1381         pte_t pte;
1382         int ret = VM_FAULT_MINOR;
1383
1384         pte_unmap(page_table);
1385         spin_unlock(&mm->page_table_lock);
1386         page = lookup_swap_cache(entry);
1387         if (!page) {
1388                 swapin_readahead(entry, address, vma);
1389                 page = read_swap_cache_async(entry, vma, address);
1390                 if (!page) {
1391                         /*
1392                          * Back out if somebody else faulted in this pte while
1393                          * we released the page table lock.
1394                          */
1395                         spin_lock(&mm->page_table_lock);
1396                         page_table = pte_offset_map(pmd, address);
1397                         if (likely(pte_same(*page_table, orig_pte)))
1398                                 ret = VM_FAULT_OOM;
1399                         else
1400                                 ret = VM_FAULT_MINOR;
1401                         pte_unmap(page_table);
1402                         spin_unlock(&mm->page_table_lock);
1403                         goto out;
1404                 }
1405
1406                 /* Had to read the page from swap area: Major fault */
1407                 ret = VM_FAULT_MAJOR;
1408                 inc_page_state(pgmajfault);
1409         }
1410
1411         if (!vx_rsspages_avail(mm, 1)) {
1412                 ret = VM_FAULT_OOM;
1413                 goto out;
1414         }
1415         mark_page_accessed(page);
1416         lock_page(page);
1417
1418         /*
1419          * Back out if somebody else faulted in this pte while we
1420          * released the page table lock.
1421          */
1422         spin_lock(&mm->page_table_lock);
1423         page_table = pte_offset_map(pmd, address);
1424         if (unlikely(!pte_same(*page_table, orig_pte))) {
1425                 pte_unmap(page_table);
1426                 spin_unlock(&mm->page_table_lock);
1427                 unlock_page(page);
1428                 page_cache_release(page);
1429                 ret = VM_FAULT_MINOR;
1430                 goto out;
1431         }
1432
1433         /* The page isn't present yet, go ahead with the fault. */
1434                 
1435         swap_free(entry);
1436         if (vm_swap_full())
1437                 remove_exclusive_swap_page(page);
1438
1439         // mm->rss++;
1440         vx_rsspages_inc(mm);
1441         pte = mk_pte(page, vma->vm_page_prot);
1442         if (write_access && can_share_swap_page(page)) {
1443                 pte = maybe_mkwrite(pte_mkdirty(pte), vma);
1444                 write_access = 0;
1445         }
1446         unlock_page(page);
1447
1448         flush_icache_page(vma, page);
1449         set_pte(page_table, pte);
1450         page_add_anon_rmap(page, vma, address);
1451
1452         if (write_access) {
1453                 if (do_wp_page(mm, vma, address,
1454                                 page_table, pmd, pte) == VM_FAULT_OOM)
1455                         ret = VM_FAULT_OOM;
1456                 goto out;
1457         }
1458
1459         /* No need to invalidate - it was non-present before */
1460         update_mmu_cache(vma, address, pte);
1461         pte_unmap(page_table);
1462         spin_unlock(&mm->page_table_lock);
1463 out:
1464         return ret;
1465 }
1466
1467 /*
1468  * We are called with the MM semaphore and page_table_lock
1469  * spinlock held to protect against concurrent faults in
1470  * multithreaded programs. 
1471  */
1472 static int
1473 do_anonymous_page(struct mm_struct *mm, struct vm_area_struct *vma,
1474                 pte_t *page_table, pmd_t *pmd, int write_access,
1475                 unsigned long addr)
1476 {
1477         pte_t entry;
1478         struct page * page = ZERO_PAGE(addr);
1479
1480         if (!vx_rsspages_avail(mm, 1)) {
1481                 spin_unlock(&mm->page_table_lock);
1482                 return VM_FAULT_OOM;
1483         }
1484
1485         /* Read-only mapping of ZERO_PAGE. */
1486         entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1487
1488         /* ..except if it's a write access */
1489         if (write_access) {
1490                 /* Allocate our own private page. */
1491                 pte_unmap(page_table);
1492                 spin_unlock(&mm->page_table_lock);
1493
1494                 if (unlikely(anon_vma_prepare(vma)))
1495                         goto no_mem;
1496                 page = alloc_page_vma(GFP_HIGHUSER, vma, addr);
1497                 if (!page)
1498                         goto no_mem;
1499                 clear_user_highpage(page, addr);
1500
1501                 spin_lock(&mm->page_table_lock);
1502                 page_table = pte_offset_map(pmd, addr);
1503
1504                 if (!pte_none(*page_table)) {
1505                         pte_unmap(page_table);
1506                         page_cache_release(page);
1507                         spin_unlock(&mm->page_table_lock);
1508                         goto out;
1509                 }
1510                 // mm->rss++;
1511                 vx_rsspages_inc(mm);
1512                 entry = maybe_mkwrite(pte_mkdirty(mk_pte(page,
1513                                                          vma->vm_page_prot)),
1514                                       vma);
1515                 lru_cache_add_active(page);
1516                 mark_page_accessed(page);
1517                 page_add_anon_rmap(page, vma, addr);
1518         }
1519
1520         set_pte(page_table, entry);
1521         pte_unmap(page_table);
1522
1523         /* No need to invalidate - it was non-present before */
1524         update_mmu_cache(vma, addr, entry);
1525         spin_unlock(&mm->page_table_lock);
1526 out:
1527         return VM_FAULT_MINOR;
1528 no_mem:
1529         return VM_FAULT_OOM;
1530 }
1531
1532 /*
1533  * do_no_page() tries to create a new page mapping. It aggressively
1534  * tries to share with existing pages, but makes a separate copy if
1535  * the "write_access" parameter is true in order to avoid the next
1536  * page fault.
1537  *
1538  * As this is called only for pages that do not currently exist, we
1539  * do not need to flush old virtual caches or the TLB.
1540  *
1541  * This is called with the MM semaphore held and the page table
1542  * spinlock held. Exit with the spinlock released.
1543  */
1544 static int
1545 do_no_page(struct mm_struct *mm, struct vm_area_struct *vma,
1546         unsigned long address, int write_access, pte_t *page_table, pmd_t *pmd)
1547 {
1548         struct page * new_page;
1549         struct address_space *mapping = NULL;
1550         pte_t entry;
1551         int sequence = 0;
1552         int ret = VM_FAULT_MINOR;
1553         int anon = 0;
1554
1555         if (!vma->vm_ops || !vma->vm_ops->nopage)
1556                 return do_anonymous_page(mm, vma, page_table,
1557                                         pmd, write_access, address);
1558         pte_unmap(page_table);
1559         spin_unlock(&mm->page_table_lock);
1560
1561         if (vma->vm_file) {
1562                 mapping = vma->vm_file->f_mapping;
1563                 sequence = atomic_read(&mapping->truncate_count);
1564         }
1565         smp_rmb();  /* Prevent CPU from reordering lock-free ->nopage() */
1566 retry:
1567         new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, &ret);
1568
1569         /* no page was available -- either SIGBUS or OOM */
1570         if (new_page == NOPAGE_SIGBUS)
1571                 return VM_FAULT_SIGBUS;
1572         if (new_page == NOPAGE_OOM)
1573                 return VM_FAULT_OOM;
1574         if (!vx_rsspages_avail(mm, 1))
1575                 return VM_FAULT_OOM;
1576
1577         /*
1578          * Should we do an early C-O-W break?
1579          */
1580         if (write_access && !(vma->vm_flags & VM_SHARED)) {
1581                 struct page *page;
1582
1583                 if (unlikely(anon_vma_prepare(vma)))
1584                         goto oom;
1585                 page = alloc_page_vma(GFP_HIGHUSER, vma, address);
1586                 if (!page)
1587                         goto oom;
1588                 copy_user_highpage(page, new_page, address);
1589                 page_cache_release(new_page);
1590                 new_page = page;
1591                 anon = 1;
1592         }
1593
1594         spin_lock(&mm->page_table_lock);
1595         /*
1596          * For a file-backed vma, someone could have truncated or otherwise
1597          * invalidated this page.  If unmap_mapping_range got called,
1598          * retry getting the page.
1599          */
1600         if (mapping &&
1601               (unlikely(sequence != atomic_read(&mapping->truncate_count)))) {
1602                 sequence = atomic_read(&mapping->truncate_count);
1603                 spin_unlock(&mm->page_table_lock);
1604                 page_cache_release(new_page);
1605                 goto retry;
1606         }
1607         page_table = pte_offset_map(pmd, address);
1608
1609         /*
1610          * This silly early PAGE_DIRTY setting removes a race
1611          * due to the bad i386 page protection. But it's valid
1612          * for other architectures too.
1613          *
1614          * Note that if write_access is true, we either now have
1615          * an exclusive copy of the page, or this is a shared mapping,
1616          * so we can make it writable and dirty to avoid having to
1617          * handle that later.
1618          */
1619         /* Only go through if we didn't race with anybody else... */
1620         if (pte_none(*page_table)) {
1621                 if (!PageReserved(new_page))
1622                         // ++mm->rss;
1623                         vx_rsspages_inc(mm);
1624                 flush_icache_page(vma, new_page);
1625                 entry = mk_pte(new_page, vma->vm_page_prot);
1626                 if (write_access)
1627                         entry = maybe_mkwrite(pte_mkdirty(entry), vma);
1628                 set_pte(page_table, entry);
1629                 if (anon) {
1630                         lru_cache_add_active(new_page);
1631                         page_add_anon_rmap(new_page, vma, address);
1632                 } else
1633                         page_add_file_rmap(new_page);
1634                 pte_unmap(page_table);
1635         } else {
1636                 /* One of our sibling threads was faster, back out. */
1637                 pte_unmap(page_table);
1638                 page_cache_release(new_page);
1639                 spin_unlock(&mm->page_table_lock);
1640                 goto out;
1641         }
1642
1643         /* no need to invalidate: a not-present page shouldn't be cached */
1644         update_mmu_cache(vma, address, entry);
1645         spin_unlock(&mm->page_table_lock);
1646 out:
1647         return ret;
1648 oom:
1649         page_cache_release(new_page);
1650         ret = VM_FAULT_OOM;
1651         goto out;
1652 }
1653
1654 /*
1655  * Fault of a previously existing named mapping. Repopulate the pte
1656  * from the encoded file_pte if possible. This enables swappable
1657  * nonlinear vmas.
1658  */
1659 static int do_file_page(struct mm_struct * mm, struct vm_area_struct * vma,
1660         unsigned long address, int write_access, pte_t *pte, pmd_t *pmd)
1661 {
1662         unsigned long pgoff;
1663         int err;
1664
1665         BUG_ON(!vma->vm_ops || !vma->vm_ops->nopage);
1666         /*
1667          * Fall back to the linear mapping if the fs does not support
1668          * ->populate:
1669          */
1670         if (!vma->vm_ops || !vma->vm_ops->populate || 
1671                         (write_access && !(vma->vm_flags & VM_SHARED))) {
1672                 pte_clear(pte);
1673                 return do_no_page(mm, vma, address, write_access, pte, pmd);
1674         }
1675
1676         pgoff = pte_to_pgoff(*pte);
1677
1678         pte_unmap(pte);
1679         spin_unlock(&mm->page_table_lock);
1680
1681         err = vma->vm_ops->populate(vma, address & PAGE_MASK, PAGE_SIZE, vma->vm_page_prot, pgoff, 0);
1682         if (err == -ENOMEM)
1683                 return VM_FAULT_OOM;
1684         if (err)
1685                 return VM_FAULT_SIGBUS;
1686         return VM_FAULT_MAJOR;
1687 }
1688
1689 /*
1690  * These routines also need to handle stuff like marking pages dirty
1691  * and/or accessed for architectures that don't do it in hardware (most
1692  * RISC architectures).  The early dirtying is also good on the i386.
1693  *
1694  * There is also a hook called "update_mmu_cache()" that architectures
1695  * with external mmu caches can use to update those (ie the Sparc or
1696  * PowerPC hashed page tables that act as extended TLBs).
1697  *
1698  * Note the "page_table_lock". It is to protect against kswapd removing
1699  * pages from under us. Note that kswapd only ever _removes_ pages, never
1700  * adds them. As such, once we have noticed that the page is not present,
1701  * we can drop the lock early.
1702  *
1703  * The adding of pages is protected by the MM semaphore (which we hold),
1704  * so we don't need to worry about a page being suddenly been added into
1705  * our VM.
1706  *
1707  * We enter with the pagetable spinlock held, we are supposed to
1708  * release it when done.
1709  */
1710 static inline int handle_pte_fault(struct mm_struct *mm,
1711         struct vm_area_struct * vma, unsigned long address,
1712         int write_access, pte_t *pte, pmd_t *pmd)
1713 {
1714         pte_t entry;
1715
1716         entry = *pte;
1717         if (!pte_present(entry)) {
1718                 /*
1719                  * If it truly wasn't present, we know that kswapd
1720                  * and the PTE updates will not touch it later. So
1721                  * drop the lock.
1722                  */
1723                 if (pte_none(entry))
1724                         return do_no_page(mm, vma, address, write_access, pte, pmd);
1725                 if (pte_file(entry))
1726                         return do_file_page(mm, vma, address, write_access, pte, pmd);
1727                 return do_swap_page(mm, vma, address, pte, pmd, entry, write_access);
1728         }
1729
1730         if (write_access) {
1731                 if (!pte_write(entry))
1732                         return do_wp_page(mm, vma, address, pte, pmd, entry);
1733
1734                 entry = pte_mkdirty(entry);
1735         }
1736         entry = pte_mkyoung(entry);
1737         ptep_set_access_flags(vma, address, pte, entry, write_access);
1738         update_mmu_cache(vma, address, entry);
1739         pte_unmap(pte);
1740         spin_unlock(&mm->page_table_lock);
1741         return VM_FAULT_MINOR;
1742 }
1743
1744 /*
1745  * By the time we get here, we already hold the mm semaphore
1746  */
1747 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1748         unsigned long address, int write_access)
1749 {
1750         pgd_t *pgd;
1751         pmd_t *pmd;
1752
1753         __set_current_state(TASK_RUNNING);
1754         pgd = pgd_offset(mm, address);
1755
1756         inc_page_state(pgfault);
1757
1758         if (is_vm_hugetlb_page(vma))
1759                 return VM_FAULT_SIGBUS; /* mapping truncation does this. */
1760
1761         /*
1762          * We need the page table lock to synchronize with kswapd
1763          * and the SMP-safe atomic PTE updates.
1764          */
1765         set_delay_flag(current,PF_MEMIO);
1766         spin_lock(&mm->page_table_lock);
1767         pmd = pmd_alloc(mm, pgd, address);
1768
1769         if (pmd) {
1770                 pte_t * pte = pte_alloc_map(mm, pmd, address);
1771                 if (pte) {
1772                         int rc = handle_pte_fault(mm, vma, address, write_access, pte, pmd);
1773                         clear_delay_flag(current,PF_MEMIO);
1774                         return rc;
1775                 }
1776         }
1777         spin_unlock(&mm->page_table_lock);
1778         clear_delay_flag(current,PF_MEMIO);
1779         return VM_FAULT_OOM;
1780 }
1781
1782 /*
1783  * Allocate page middle directory.
1784  *
1785  * We've already handled the fast-path in-line, and we own the
1786  * page table lock.
1787  *
1788  * On a two-level page table, this ends up actually being entirely
1789  * optimized away.
1790  */
1791 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1792 {
1793         pmd_t *new;
1794
1795         spin_unlock(&mm->page_table_lock);
1796         new = pmd_alloc_one(mm, address);
1797         spin_lock(&mm->page_table_lock);
1798         if (!new)
1799                 return NULL;
1800
1801         /*
1802          * Because we dropped the lock, we should re-check the
1803          * entry, as somebody else could have populated it..
1804          */
1805         if (pgd_present(*pgd)) {
1806                 pmd_free(new);
1807                 goto out;
1808         }
1809         pgd_populate(mm, pgd, new);
1810 out:
1811         return pmd_offset(pgd, address);
1812 }
1813
1814 int make_pages_present(unsigned long addr, unsigned long end)
1815 {
1816         int ret, len, write;
1817         struct vm_area_struct * vma;
1818
1819         vma = find_vma(current->mm, addr);
1820         write = (vma->vm_flags & VM_WRITE) != 0;
1821         if (addr >= end)
1822                 BUG();
1823         if (end > vma->vm_end)
1824                 BUG();
1825         len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1826         ret = get_user_pages(current, current->mm, addr,
1827                         len, write, 0, NULL, NULL);
1828         if (ret < 0)
1829                 return ret;
1830         return ret == len ? 0 : -1;
1831 }
1832
1833 /* 
1834  * Map a vmalloc()-space virtual address to the physical page.
1835  */
1836 struct page * vmalloc_to_page(void * vmalloc_addr)
1837 {
1838         unsigned long addr = (unsigned long) vmalloc_addr;
1839         struct page *page = NULL;
1840         pgd_t *pgd = pgd_offset_k(addr);
1841         pmd_t *pmd;
1842         pte_t *ptep, pte;
1843   
1844         if (!pgd_none(*pgd)) {
1845                 pmd = pmd_offset(pgd, addr);
1846                 if (!pmd_none(*pmd)) {
1847                         preempt_disable();
1848                         ptep = pte_offset_map(pmd, addr);
1849                         pte = *ptep;
1850                         if (pte_present(pte))
1851                                 page = pte_page(pte);
1852                         pte_unmap(ptep);
1853                         preempt_enable();
1854                 }
1855         }
1856         return page;
1857 }
1858
1859 EXPORT_SYMBOL(vmalloc_to_page);
1860
1861 #if !defined(CONFIG_ARCH_GATE_AREA)
1862
1863 #if defined(AT_SYSINFO_EHDR)
1864 struct vm_area_struct gate_vma;
1865
1866 static int __init gate_vma_init(void)
1867 {
1868         gate_vma.vm_mm = NULL;
1869         gate_vma.vm_start = FIXADDR_USER_START;
1870         gate_vma.vm_end = FIXADDR_USER_END;
1871         gate_vma.vm_page_prot = PAGE_READONLY;
1872         gate_vma.vm_flags = 0;
1873         return 0;
1874 }
1875 __initcall(gate_vma_init);
1876 #endif
1877
1878 struct vm_area_struct *get_gate_vma(struct task_struct *tsk)
1879 {
1880 #ifdef AT_SYSINFO_EHDR
1881         return &gate_vma;
1882 #else
1883         return 0;
1884 #endif
1885 }
1886
1887 int in_gate_area(struct task_struct *task, unsigned long addr)
1888 {
1889 #ifdef AT_SYSINFO_EHDR
1890         if ((addr >= FIXADDR_USER_START) && (addr < FIXADDR_USER_END))
1891                 return 1;
1892 #endif
1893         return 0;
1894 }
1895
1896 #endif