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