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