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