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