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