ia64/xen-unstable

view linux-2.4.29-xen-sparse/mm/memory.c @ 3602:9a9c5a491401

bitkeeper revision 1.1159.235.1 (42000d3dwcPyT8aY4VIPYGCfCAJuQQ)

More x86/64. Status: traps.c now included in the build, but actual building
of IDT doesn't happen, and we need some sort of entry.S. More page-table
building required so that arch_init_memory() can work. And there is something
odd with MP-table parsing; I currently suspect that __init sections are
causing problems.
Signed-off-by: keir.fraser@cl.cam.ac.uk
author kaf24@viper.(none)
date Tue Feb 01 23:14:05 2005 +0000 (2005-02-01)
parents 610068179f96
children f504382b179f 9f7935ea4606 f5f2757b3aa2
line source
1 /*
2 * linux/mm/memory.c
3 *
4 * Copyright (C) 1991, 1992, 1993, 1994 Linus Torvalds
5 */
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 */
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 */
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 */
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 */
39 #include <linux/mm.h>
40 #include <linux/mman.h>
41 #include <linux/swap.h>
42 #include <linux/smp_lock.h>
43 #include <linux/swapctl.h>
44 #include <linux/iobuf.h>
45 #include <linux/highmem.h>
46 #include <linux/pagemap.h>
47 #include <linux/module.h>
49 #include <asm/pgalloc.h>
50 #include <asm/uaccess.h>
51 #include <asm/tlb.h>
53 unsigned long max_mapnr;
54 unsigned long num_physpages;
55 unsigned long num_mappedpages;
56 void * high_memory;
57 struct page *highmem_start_page;
59 /*
60 * We special-case the C-O-W ZERO_PAGE, because it's such
61 * a common occurrence (no need to read the page to know
62 * that it's zero - better for the cache and memory subsystem).
63 */
64 static inline void copy_cow_page(struct page * from, struct page * to, unsigned long address)
65 {
66 if (from == ZERO_PAGE(address)) {
67 clear_user_highpage(to, address);
68 return;
69 }
70 copy_user_highpage(to, from, address);
71 }
73 mem_map_t * mem_map;
75 /*
76 * Called by TLB shootdown
77 */
78 void __free_pte(pte_t pte)
79 {
80 struct page *page = pte_page(pte);
81 if ((!VALID_PAGE(page)) || PageReserved(page))
82 return;
83 if (pte_dirty(pte))
84 set_page_dirty(page);
85 free_page_and_swap_cache(page);
86 }
89 /*
90 * Note: this doesn't free the actual pages themselves. That
91 * has been handled earlier when unmapping all the memory regions.
92 */
93 static inline void free_one_pmd(pmd_t * dir)
94 {
95 pte_t * pte;
97 if (pmd_none(*dir))
98 return;
99 if (pmd_bad(*dir)) {
100 pmd_ERROR(*dir);
101 pmd_clear(dir);
102 return;
103 }
104 pte = pte_offset(dir, 0);
105 pmd_clear(dir);
106 pte_free(pte);
107 }
109 static inline void free_one_pgd(pgd_t * dir)
110 {
111 int j;
112 pmd_t * pmd;
114 if (pgd_none(*dir))
115 return;
116 if (pgd_bad(*dir)) {
117 pgd_ERROR(*dir);
118 pgd_clear(dir);
119 return;
120 }
121 pmd = pmd_offset(dir, 0);
122 pgd_clear(dir);
123 for (j = 0; j < PTRS_PER_PMD ; j++) {
124 prefetchw(pmd+j+(PREFETCH_STRIDE/16));
125 free_one_pmd(pmd+j);
126 }
127 pmd_free(pmd);
128 }
130 /* Low and high watermarks for page table cache.
131 The system should try to have pgt_water[0] <= cache elements <= pgt_water[1]
132 */
133 int pgt_cache_water[2] = { 25, 50 };
135 /* Returns the number of pages freed */
136 int check_pgt_cache(void)
137 {
138 return do_check_pgt_cache(pgt_cache_water[0], pgt_cache_water[1]);
139 }
142 /*
143 * This function clears all user-level page tables of a process - this
144 * is needed by execve(), so that old pages aren't in the way.
145 */
146 void clear_page_tables(struct mm_struct *mm, unsigned long first, int nr)
147 {
148 pgd_t * page_dir = mm->pgd;
150 spin_lock(&mm->page_table_lock);
151 page_dir += first;
152 do {
153 free_one_pgd(page_dir);
154 page_dir++;
155 } while (--nr);
156 XEN_flush_page_update_queue();
157 spin_unlock(&mm->page_table_lock);
159 /* keep the page table cache within bounds */
160 check_pgt_cache();
161 }
163 #define PTE_TABLE_MASK ((PTRS_PER_PTE-1) * sizeof(pte_t))
164 #define PMD_TABLE_MASK ((PTRS_PER_PMD-1) * sizeof(pmd_t))
166 /*
167 * copy one vm_area from one task to the other. Assumes the page tables
168 * already present in the new task to be cleared in the whole range
169 * covered by this vma.
170 *
171 * 08Jan98 Merged into one routine from several inline routines to reduce
172 * variable count and make things faster. -jj
173 *
174 * dst->page_table_lock is held on entry and exit,
175 * but may be dropped within pmd_alloc() and pte_alloc().
176 */
177 int copy_page_range(struct mm_struct *dst, struct mm_struct *src,
178 struct vm_area_struct *vma)
179 {
180 pgd_t * src_pgd, * dst_pgd;
181 unsigned long address = vma->vm_start;
182 unsigned long end = vma->vm_end;
183 unsigned long cow = (vma->vm_flags & (VM_SHARED | VM_MAYWRITE)) == VM_MAYWRITE;
185 src_pgd = pgd_offset(src, address)-1;
186 dst_pgd = pgd_offset(dst, address)-1;
188 for (;;) {
189 pmd_t * src_pmd, * dst_pmd;
191 src_pgd++; dst_pgd++;
193 /* copy_pmd_range */
195 if (pgd_none(*src_pgd))
196 goto skip_copy_pmd_range;
197 if (pgd_bad(*src_pgd)) {
198 pgd_ERROR(*src_pgd);
199 pgd_clear(src_pgd);
200 skip_copy_pmd_range: address = (address + PGDIR_SIZE) & PGDIR_MASK;
201 if (!address || (address >= end))
202 goto out;
203 continue;
204 }
206 src_pmd = pmd_offset(src_pgd, address);
207 dst_pmd = pmd_alloc(dst, dst_pgd, address);
208 if (!dst_pmd)
209 goto nomem;
211 do {
212 pte_t * src_pte, * dst_pte;
214 /* copy_pte_range */
216 if (pmd_none(*src_pmd))
217 goto skip_copy_pte_range;
218 if (pmd_bad(*src_pmd)) {
219 pmd_ERROR(*src_pmd);
220 pmd_clear(src_pmd);
221 skip_copy_pte_range: address = (address + PMD_SIZE) & PMD_MASK;
222 if (address >= end)
223 goto out;
224 goto cont_copy_pmd_range;
225 }
227 src_pte = pte_offset(src_pmd, address);
228 dst_pte = pte_alloc(dst, dst_pmd, address);
229 if (!dst_pte)
230 goto nomem;
232 spin_lock(&src->page_table_lock);
233 do {
234 pte_t pte = *src_pte;
235 struct page *ptepage;
237 /* copy_one_pte */
239 if (pte_none(pte))
240 goto cont_copy_pte_range_noset;
241 if (!pte_present(pte)) {
242 swap_duplicate(pte_to_swp_entry(pte));
243 goto cont_copy_pte_range;
244 }
245 ptepage = pte_page(pte);
246 if ((!VALID_PAGE(ptepage)) ||
247 PageReserved(ptepage))
248 goto cont_copy_pte_range;
250 /* If it's a COW mapping, write protect it both in the parent and the child */
251 if (cow && pte_write(pte)) {
252 /* XEN modification: modified ordering here to avoid RaW hazard. */
253 pte = *src_pte;
254 pte = pte_wrprotect(pte);
255 ptep_set_wrprotect(src_pte);
256 }
258 /* If it's a shared mapping, mark it clean in the child */
259 if (vma->vm_flags & VM_SHARED)
260 pte = pte_mkclean(pte);
261 pte = pte_mkold(pte);
262 get_page(ptepage);
263 dst->rss++;
265 cont_copy_pte_range: set_pte(dst_pte, pte);
266 cont_copy_pte_range_noset: address += PAGE_SIZE;
267 if (address >= end)
268 goto out_unlock;
269 src_pte++;
270 dst_pte++;
271 } while ((unsigned long)src_pte & PTE_TABLE_MASK);
272 spin_unlock(&src->page_table_lock);
274 cont_copy_pmd_range: src_pmd++;
275 dst_pmd++;
276 } while ((unsigned long)src_pmd & PMD_TABLE_MASK);
277 }
278 out_unlock:
279 spin_unlock(&src->page_table_lock);
280 out:
281 return 0;
282 nomem:
283 return -ENOMEM;
284 }
286 /*
287 * Return indicates whether a page was freed so caller can adjust rss
288 */
289 static inline void forget_pte(pte_t page)
290 {
291 if (!pte_none(page)) {
292 printk("forget_pte: old mapping existed!\n");
293 BUG();
294 }
295 }
297 static inline int zap_pte_range(mmu_gather_t *tlb, pmd_t * pmd, unsigned long address, unsigned long size)
298 {
299 unsigned long offset;
300 pte_t * ptep;
301 int freed = 0;
303 if (pmd_none(*pmd))
304 return 0;
305 if (pmd_bad(*pmd)) {
306 pmd_ERROR(*pmd);
307 pmd_clear(pmd);
308 return 0;
309 }
310 ptep = pte_offset(pmd, address);
311 offset = address & ~PMD_MASK;
312 if (offset + size > PMD_SIZE)
313 size = PMD_SIZE - offset;
314 size &= PAGE_MASK;
315 for (offset=0; offset < size; ptep++, offset += PAGE_SIZE) {
316 pte_t pte = *ptep;
317 if (pte_none(pte))
318 continue;
319 if (pte_present(pte)) {
320 struct page *page = pte_page(pte);
321 if (VALID_PAGE(page) && !PageReserved(page))
322 freed ++;
323 /* This will eventually call __free_pte on the pte. */
324 tlb_remove_page(tlb, ptep, address + offset);
325 } else {
326 free_swap_and_cache(pte_to_swp_entry(pte));
327 pte_clear(ptep);
328 }
329 }
331 return freed;
332 }
334 static inline int zap_pmd_range(mmu_gather_t *tlb, pgd_t * dir, unsigned long address, unsigned long size)
335 {
336 pmd_t * pmd;
337 unsigned long end;
338 int freed;
340 if (pgd_none(*dir))
341 return 0;
342 if (pgd_bad(*dir)) {
343 pgd_ERROR(*dir);
344 pgd_clear(dir);
345 return 0;
346 }
347 pmd = pmd_offset(dir, address);
348 end = address + size;
349 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
350 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
351 freed = 0;
352 do {
353 freed += zap_pte_range(tlb, pmd, address, end - address);
354 address = (address + PMD_SIZE) & PMD_MASK;
355 pmd++;
356 } while (address < end);
357 return freed;
358 }
360 /*
361 * remove user pages in a given range.
362 */
363 void zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size)
364 {
365 mmu_gather_t *tlb;
366 pgd_t * dir;
367 unsigned long start = address, end = address + size;
368 int freed = 0;
370 dir = pgd_offset(mm, address);
372 /*
373 * This is a long-lived spinlock. That's fine.
374 * There's no contention, because the page table
375 * lock only protects against kswapd anyway, and
376 * even if kswapd happened to be looking at this
377 * process we _want_ it to get stuck.
378 */
379 if (address >= end)
380 BUG();
381 spin_lock(&mm->page_table_lock);
382 flush_cache_range(mm, address, end);
383 tlb = tlb_gather_mmu(mm);
385 do {
386 freed += zap_pmd_range(tlb, dir, address, end - address);
387 address = (address + PGDIR_SIZE) & PGDIR_MASK;
388 dir++;
389 } while (address && (address < end));
391 /* this will flush any remaining tlb entries */
392 tlb_finish_mmu(tlb, start, end);
394 /*
395 * Update rss for the mm_struct (not necessarily current->mm)
396 * Notice that rss is an unsigned long.
397 */
398 if (mm->rss > freed)
399 mm->rss -= freed;
400 else
401 mm->rss = 0;
402 spin_unlock(&mm->page_table_lock);
403 }
405 /*
406 * Do a quick page-table lookup for a single page.
407 */
408 static struct page * follow_page(struct mm_struct *mm, unsigned long address, int write)
409 {
410 pgd_t *pgd;
411 pmd_t *pmd;
412 pte_t *ptep, pte;
414 pgd = pgd_offset(mm, address);
415 if (pgd_none(*pgd) || pgd_bad(*pgd))
416 goto out;
418 pmd = pmd_offset(pgd, address);
419 if (pmd_none(*pmd) || pmd_bad(*pmd))
420 goto out;
422 ptep = pte_offset(pmd, address);
423 if (!ptep)
424 goto out;
426 pte = *ptep;
427 if (pte_present(pte)) {
428 if (!write ||
429 (pte_write(pte) && pte_dirty(pte)))
430 return pte_page(pte);
431 }
433 out:
434 return 0;
435 }
437 /*
438 * Given a physical address, is there a useful struct page pointing to
439 * it? This may become more complex in the future if we start dealing
440 * with IO-aperture pages in kiobufs.
441 */
443 static inline struct page * get_page_map(struct page *page)
444 {
445 if (!VALID_PAGE(page))
446 return 0;
447 return page;
448 }
450 /*
451 * Please read Documentation/cachetlb.txt before using this function,
452 * accessing foreign memory spaces can cause cache coherency problems.
453 *
454 * Accessing a VM_IO area is even more dangerous, therefore the function
455 * fails if pages is != NULL and a VM_IO area is found.
456 */
457 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
458 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas)
459 {
460 int i;
461 unsigned int flags;
463 /*
464 * Require read or write permissions.
465 * If 'force' is set, we only require the "MAY" flags.
466 */
467 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
468 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
469 i = 0;
471 do {
472 struct vm_area_struct * vma;
474 vma = find_extend_vma(mm, start);
476 if ( !vma || (pages && vma->vm_flags & VM_IO) || !(flags & vma->vm_flags) )
477 return i ? : -EFAULT;
479 spin_lock(&mm->page_table_lock);
480 do {
481 struct page *map;
482 while (!(map = follow_page(mm, start, write))) {
483 spin_unlock(&mm->page_table_lock);
484 switch (handle_mm_fault(mm, vma, start, write)) {
485 case 1:
486 tsk->min_flt++;
487 break;
488 case 2:
489 tsk->maj_flt++;
490 break;
491 case 0:
492 if (i) return i;
493 return -EFAULT;
494 default:
495 if (i) return i;
496 return -ENOMEM;
497 }
498 spin_lock(&mm->page_table_lock);
499 }
500 if (pages) {
501 pages[i] = get_page_map(map);
502 /* FIXME: call the correct function,
503 * depending on the type of the found page
504 */
505 if (!pages[i])
506 goto bad_page;
507 page_cache_get(pages[i]);
508 }
509 if (vmas)
510 vmas[i] = vma;
511 i++;
512 start += PAGE_SIZE;
513 len--;
514 } while(len && start < vma->vm_end);
515 spin_unlock(&mm->page_table_lock);
516 } while(len);
517 out:
518 return i;
520 /*
521 * We found an invalid page in the VMA. Release all we have
522 * so far and fail.
523 */
524 bad_page:
525 spin_unlock(&mm->page_table_lock);
526 while (i--)
527 page_cache_release(pages[i]);
528 i = -EFAULT;
529 goto out;
530 }
532 EXPORT_SYMBOL(get_user_pages);
534 /*
535 * Force in an entire range of pages from the current process's user VA,
536 * and pin them in physical memory.
537 */
538 #define dprintk(x...)
540 int map_user_kiobuf(int rw, struct kiobuf *iobuf, unsigned long va, size_t len)
541 {
542 int pgcount, err;
543 struct mm_struct * mm;
545 /* Make sure the iobuf is not already mapped somewhere. */
546 if (iobuf->nr_pages)
547 return -EINVAL;
549 mm = current->mm;
550 dprintk ("map_user_kiobuf: begin\n");
552 pgcount = (va + len + PAGE_SIZE - 1)/PAGE_SIZE - va/PAGE_SIZE;
553 /* mapping 0 bytes is not permitted */
554 if (!pgcount) BUG();
555 err = expand_kiobuf(iobuf, pgcount);
556 if (err)
557 return err;
559 iobuf->locked = 0;
560 iobuf->offset = va & (PAGE_SIZE-1);
561 iobuf->length = len;
563 /* Try to fault in all of the necessary pages */
564 down_read(&mm->mmap_sem);
565 /* rw==READ means read from disk, write into memory area */
566 err = get_user_pages(current, mm, va, pgcount,
567 (rw==READ), 0, iobuf->maplist, NULL);
568 up_read(&mm->mmap_sem);
569 if (err < 0) {
570 unmap_kiobuf(iobuf);
571 dprintk ("map_user_kiobuf: end %d\n", err);
572 return err;
573 }
574 iobuf->nr_pages = err;
575 while (pgcount--) {
576 /* FIXME: flush superflous for rw==READ,
577 * probably wrong function for rw==WRITE
578 */
579 flush_dcache_page(iobuf->maplist[pgcount]);
580 }
581 dprintk ("map_user_kiobuf: end OK\n");
582 return 0;
583 }
585 /*
586 * Mark all of the pages in a kiobuf as dirty
587 *
588 * We need to be able to deal with short reads from disk: if an IO error
589 * occurs, the number of bytes read into memory may be less than the
590 * size of the kiobuf, so we have to stop marking pages dirty once the
591 * requested byte count has been reached.
592 *
593 * Must be called from process context - set_page_dirty() takes VFS locks.
594 */
596 void mark_dirty_kiobuf(struct kiobuf *iobuf, int bytes)
597 {
598 int index, offset, remaining;
599 struct page *page;
601 index = iobuf->offset >> PAGE_SHIFT;
602 offset = iobuf->offset & ~PAGE_MASK;
603 remaining = bytes;
604 if (remaining > iobuf->length)
605 remaining = iobuf->length;
607 while (remaining > 0 && index < iobuf->nr_pages) {
608 page = iobuf->maplist[index];
610 if (!PageReserved(page))
611 set_page_dirty(page);
613 remaining -= (PAGE_SIZE - offset);
614 offset = 0;
615 index++;
616 }
617 }
619 /*
620 * Unmap all of the pages referenced by a kiobuf. We release the pages,
621 * and unlock them if they were locked.
622 */
624 void unmap_kiobuf (struct kiobuf *iobuf)
625 {
626 int i;
627 struct page *map;
629 for (i = 0; i < iobuf->nr_pages; i++) {
630 map = iobuf->maplist[i];
631 if (map) {
632 if (iobuf->locked)
633 UnlockPage(map);
634 /* FIXME: cache flush missing for rw==READ
635 * FIXME: call the correct reference counting function
636 */
637 page_cache_release(map);
638 }
639 }
641 iobuf->nr_pages = 0;
642 iobuf->locked = 0;
643 }
646 /*
647 * Lock down all of the pages of a kiovec for IO.
648 *
649 * If any page is mapped twice in the kiovec, we return the error -EINVAL.
650 *
651 * The optional wait parameter causes the lock call to block until all
652 * pages can be locked if set. If wait==0, the lock operation is
653 * aborted if any locked pages are found and -EAGAIN is returned.
654 */
656 int lock_kiovec(int nr, struct kiobuf *iovec[], int wait)
657 {
658 struct kiobuf *iobuf;
659 int i, j;
660 struct page *page, **ppage;
661 int doublepage = 0;
662 int repeat = 0;
664 repeat:
666 for (i = 0; i < nr; i++) {
667 iobuf = iovec[i];
669 if (iobuf->locked)
670 continue;
672 ppage = iobuf->maplist;
673 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
674 page = *ppage;
675 if (!page)
676 continue;
678 if (TryLockPage(page)) {
679 while (j--) {
680 struct page *tmp = *--ppage;
681 if (tmp)
682 UnlockPage(tmp);
683 }
684 goto retry;
685 }
686 }
687 iobuf->locked = 1;
688 }
690 return 0;
692 retry:
694 /*
695 * We couldn't lock one of the pages. Undo the locking so far,
696 * wait on the page we got to, and try again.
697 */
699 unlock_kiovec(nr, iovec);
700 if (!wait)
701 return -EAGAIN;
703 /*
704 * Did the release also unlock the page we got stuck on?
705 */
706 if (!PageLocked(page)) {
707 /*
708 * If so, we may well have the page mapped twice
709 * in the IO address range. Bad news. Of
710 * course, it _might_ just be a coincidence,
711 * but if it happens more than once, chances
712 * are we have a double-mapped page.
713 */
714 if (++doublepage >= 3)
715 return -EINVAL;
717 /* Try again... */
718 wait_on_page(page);
719 }
721 if (++repeat < 16)
722 goto repeat;
723 return -EAGAIN;
724 }
726 /*
727 * Unlock all of the pages of a kiovec after IO.
728 */
730 int unlock_kiovec(int nr, struct kiobuf *iovec[])
731 {
732 struct kiobuf *iobuf;
733 int i, j;
734 struct page *page, **ppage;
736 for (i = 0; i < nr; i++) {
737 iobuf = iovec[i];
739 if (!iobuf->locked)
740 continue;
741 iobuf->locked = 0;
743 ppage = iobuf->maplist;
744 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
745 page = *ppage;
746 if (!page)
747 continue;
748 UnlockPage(page);
749 }
750 }
751 return 0;
752 }
754 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
755 unsigned long size, pgprot_t prot)
756 {
757 unsigned long end;
759 address &= ~PMD_MASK;
760 end = address + size;
761 if (end > PMD_SIZE)
762 end = PMD_SIZE;
763 do {
764 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
765 pte_t oldpage = ptep_get_and_clear(pte);
766 set_pte(pte, zero_pte);
767 forget_pte(oldpage);
768 address += PAGE_SIZE;
769 pte++;
770 } while (address && (address < end));
771 }
773 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
774 unsigned long size, pgprot_t prot)
775 {
776 unsigned long end;
778 address &= ~PGDIR_MASK;
779 end = address + size;
780 if (end > PGDIR_SIZE)
781 end = PGDIR_SIZE;
782 do {
783 pte_t * pte = pte_alloc(mm, pmd, address);
784 if (!pte)
785 return -ENOMEM;
786 zeromap_pte_range(pte, address, end - address, prot);
787 address = (address + PMD_SIZE) & PMD_MASK;
788 pmd++;
789 } while (address && (address < end));
790 return 0;
791 }
793 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
794 {
795 int error = 0;
796 pgd_t * dir;
797 unsigned long beg = address;
798 unsigned long end = address + size;
799 struct mm_struct *mm = current->mm;
801 dir = pgd_offset(mm, address);
802 flush_cache_range(mm, beg, end);
803 if (address >= end)
804 BUG();
806 spin_lock(&mm->page_table_lock);
807 do {
808 pmd_t *pmd = pmd_alloc(mm, dir, address);
809 error = -ENOMEM;
810 if (!pmd)
811 break;
812 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
813 if (error)
814 break;
815 address = (address + PGDIR_SIZE) & PGDIR_MASK;
816 dir++;
817 } while (address && (address < end));
818 spin_unlock(&mm->page_table_lock);
819 flush_tlb_range(mm, beg, end);
820 return error;
821 }
823 /*
824 * maps a range of physical memory into the requested pages. the old
825 * mappings are removed. any references to nonexistent pages results
826 * in null mappings (currently treated as "copy-on-access")
827 */
828 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
829 unsigned long phys_addr, pgprot_t prot)
830 {
831 unsigned long end;
833 address &= ~PMD_MASK;
834 end = address + size;
835 if (end > PMD_SIZE)
836 end = PMD_SIZE;
837 do {
838 struct page *page;
839 pte_t oldpage;
840 oldpage = ptep_get_and_clear(pte);
842 page = virt_to_page(__va(phys_addr));
843 if ((!VALID_PAGE(page)) || PageReserved(page))
844 set_pte(pte, mk_pte_phys(phys_addr, prot));
845 forget_pte(oldpage);
846 address += PAGE_SIZE;
847 phys_addr += PAGE_SIZE;
848 pte++;
849 } while (address && (address < end));
850 }
852 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
853 unsigned long phys_addr, pgprot_t prot)
854 {
855 unsigned long end;
857 address &= ~PGDIR_MASK;
858 end = address + size;
859 if (end > PGDIR_SIZE)
860 end = PGDIR_SIZE;
861 phys_addr -= address;
862 do {
863 pte_t * pte = pte_alloc(mm, pmd, address);
864 if (!pte)
865 return -ENOMEM;
866 remap_pte_range(pte, address, end - address, address + phys_addr, prot);
867 address = (address + PMD_SIZE) & PMD_MASK;
868 pmd++;
869 } while (address && (address < end));
870 return 0;
871 }
873 /* Note: this is only safe if the mm semaphore is held when called. */
874 int remap_page_range(unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
875 {
876 int error = 0;
877 pgd_t * dir;
878 unsigned long beg = from;
879 unsigned long end = from + size;
880 struct mm_struct *mm = current->mm;
882 phys_addr -= from;
883 dir = pgd_offset(mm, from);
884 flush_cache_range(mm, beg, end);
885 if (from >= end)
886 BUG();
888 spin_lock(&mm->page_table_lock);
889 do {
890 pmd_t *pmd = pmd_alloc(mm, dir, from);
891 error = -ENOMEM;
892 if (!pmd)
893 break;
894 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
895 if (error)
896 break;
897 from = (from + PGDIR_SIZE) & PGDIR_MASK;
898 dir++;
899 } while (from && (from < end));
900 spin_unlock(&mm->page_table_lock);
901 flush_tlb_range(mm, beg, end);
902 return error;
903 }
905 /*
906 * Establish a new mapping:
907 * - flush the old one
908 * - update the page tables
909 * - inform the TLB about the new one
910 *
911 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
912 */
913 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
914 {
915 #ifdef CONFIG_XEN
916 if ( likely(vma->vm_mm == current->mm) ) {
917 XEN_flush_page_update_queue();
918 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, entry, UVMF_INVLPG);
919 } else {
920 set_pte(page_table, entry);
921 flush_tlb_page(vma, address);
922 }
923 #else
924 set_pte(page_table, entry);
925 flush_tlb_page(vma, address);
926 #endif
927 update_mmu_cache(vma, address, entry);
928 }
930 /*
931 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
932 */
933 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
934 pte_t *page_table)
935 {
936 flush_page_to_ram(new_page);
937 flush_cache_page(vma, address);
938 establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
939 }
941 /*
942 * This routine handles present pages, when users try to write
943 * to a shared page. It is done by copying the page to a new address
944 * and decrementing the shared-page counter for the old page.
945 *
946 * Goto-purists beware: the only reason for goto's here is that it results
947 * in better assembly code.. The "default" path will see no jumps at all.
948 *
949 * Note that this routine assumes that the protection checks have been
950 * done by the caller (the low-level page fault routine in most cases).
951 * Thus we can safely just mark it writable once we've done any necessary
952 * COW.
953 *
954 * We also mark the page dirty at this point even though the page will
955 * change only once the write actually happens. This avoids a few races,
956 * and potentially makes it more efficient.
957 *
958 * We hold the mm semaphore and the page_table_lock on entry and exit
959 * with the page_table_lock released.
960 */
961 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
962 unsigned long address, pte_t *page_table, pte_t pte)
963 {
964 struct page *old_page, *new_page;
966 old_page = pte_page(pte);
967 if (!VALID_PAGE(old_page))
968 goto bad_wp_page;
970 if (!TryLockPage(old_page)) {
971 int reuse = can_share_swap_page(old_page);
972 unlock_page(old_page);
973 if (reuse) {
974 flush_cache_page(vma, address);
975 establish_pte(vma, address, page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
976 spin_unlock(&mm->page_table_lock);
977 return 1; /* Minor fault */
978 }
979 }
981 /*
982 * Ok, we need to copy. Oh, well..
983 */
984 page_cache_get(old_page);
985 spin_unlock(&mm->page_table_lock);
987 new_page = alloc_page(GFP_HIGHUSER);
988 if (!new_page)
989 goto no_mem;
990 copy_cow_page(old_page,new_page,address);
992 /*
993 * Re-check the pte - we dropped the lock
994 */
995 spin_lock(&mm->page_table_lock);
996 if (pte_same(*page_table, pte)) {
997 if (PageReserved(old_page))
998 ++mm->rss;
999 break_cow(vma, new_page, address, page_table);
1000 if (vm_anon_lru)
1001 lru_cache_add(new_page);
1003 /* Free the old page.. */
1004 new_page = old_page;
1006 spin_unlock(&mm->page_table_lock);
1007 page_cache_release(new_page);
1008 page_cache_release(old_page);
1009 return 1; /* Minor fault */
1011 bad_wp_page:
1012 spin_unlock(&mm->page_table_lock);
1013 printk("do_wp_page: bogus page at address %08lx (page 0x%lx)\n",address,(unsigned long)old_page);
1014 return -1;
1015 no_mem:
1016 page_cache_release(old_page);
1017 return -1;
1020 static void vmtruncate_list(struct vm_area_struct *mpnt, unsigned long pgoff)
1022 do {
1023 struct mm_struct *mm = mpnt->vm_mm;
1024 unsigned long start = mpnt->vm_start;
1025 unsigned long end = mpnt->vm_end;
1026 unsigned long len = end - start;
1027 unsigned long diff;
1029 /* mapping wholly truncated? */
1030 if (mpnt->vm_pgoff >= pgoff) {
1031 zap_page_range(mm, start, len);
1032 continue;
1035 /* mapping wholly unaffected? */
1036 len = len >> PAGE_SHIFT;
1037 diff = pgoff - mpnt->vm_pgoff;
1038 if (diff >= len)
1039 continue;
1041 /* Ok, partially affected.. */
1042 start += diff << PAGE_SHIFT;
1043 len = (len - diff) << PAGE_SHIFT;
1044 zap_page_range(mm, start, len);
1045 } while ((mpnt = mpnt->vm_next_share) != NULL);
1048 /*
1049 * Handle all mappings that got truncated by a "truncate()"
1050 * system call.
1052 * NOTE! We have to be ready to update the memory sharing
1053 * between the file and the memory map for a potential last
1054 * incomplete page. Ugly, but necessary.
1055 */
1056 int vmtruncate(struct inode * inode, loff_t offset)
1058 unsigned long pgoff;
1059 struct address_space *mapping = inode->i_mapping;
1060 unsigned long limit;
1062 if (inode->i_size < offset)
1063 goto do_expand;
1064 inode->i_size = offset;
1065 spin_lock(&mapping->i_shared_lock);
1066 if (!mapping->i_mmap && !mapping->i_mmap_shared)
1067 goto out_unlock;
1069 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1070 if (mapping->i_mmap != NULL)
1071 vmtruncate_list(mapping->i_mmap, pgoff);
1072 if (mapping->i_mmap_shared != NULL)
1073 vmtruncate_list(mapping->i_mmap_shared, pgoff);
1075 out_unlock:
1076 spin_unlock(&mapping->i_shared_lock);
1077 truncate_inode_pages(mapping, offset);
1078 goto out_truncate;
1080 do_expand:
1081 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1082 if (limit != RLIM_INFINITY && offset > limit)
1083 goto out_sig;
1084 if (offset > inode->i_sb->s_maxbytes)
1085 goto out;
1086 inode->i_size = offset;
1088 out_truncate:
1089 if (inode->i_op && inode->i_op->truncate) {
1090 lock_kernel();
1091 inode->i_op->truncate(inode);
1092 unlock_kernel();
1094 return 0;
1095 out_sig:
1096 send_sig(SIGXFSZ, current, 0);
1097 out:
1098 return -EFBIG;
1101 /*
1102 * Primitive swap readahead code. We simply read an aligned block of
1103 * (1 << page_cluster) entries in the swap area. This method is chosen
1104 * because it doesn't cost us any seek time. We also make sure to queue
1105 * the 'original' request together with the readahead ones...
1106 */
1107 void swapin_readahead(swp_entry_t entry)
1109 int i, num;
1110 struct page *new_page;
1111 unsigned long offset;
1113 /*
1114 * Get the number of handles we should do readahead io to.
1115 */
1116 num = valid_swaphandles(entry, &offset);
1117 for (i = 0; i < num; offset++, i++) {
1118 /* Ok, do the async read-ahead now */
1119 new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset));
1120 if (!new_page)
1121 break;
1122 page_cache_release(new_page);
1124 return;
1127 /*
1128 * We hold the mm semaphore and the page_table_lock on entry and
1129 * should release the pagetable lock on exit..
1130 */
1131 static int do_swap_page(struct mm_struct * mm,
1132 struct vm_area_struct * vma, unsigned long address,
1133 pte_t * page_table, pte_t orig_pte, int write_access)
1135 struct page *page;
1136 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1137 pte_t pte;
1138 int ret = 1;
1140 spin_unlock(&mm->page_table_lock);
1141 page = lookup_swap_cache(entry);
1142 if (!page) {
1143 swapin_readahead(entry);
1144 page = read_swap_cache_async(entry);
1145 if (!page) {
1146 /*
1147 * Back out if somebody else faulted in this pte while
1148 * we released the page table lock.
1149 */
1150 int retval;
1151 spin_lock(&mm->page_table_lock);
1152 retval = pte_same(*page_table, orig_pte) ? -1 : 1;
1153 spin_unlock(&mm->page_table_lock);
1154 return retval;
1157 /* Had to read the page from swap area: Major fault */
1158 ret = 2;
1161 mark_page_accessed(page);
1163 lock_page(page);
1165 /*
1166 * Back out if somebody else faulted in this pte while we
1167 * released the page table lock.
1168 */
1169 spin_lock(&mm->page_table_lock);
1170 if (!pte_same(*page_table, orig_pte)) {
1171 spin_unlock(&mm->page_table_lock);
1172 unlock_page(page);
1173 page_cache_release(page);
1174 return 1;
1177 /* The page isn't present yet, go ahead with the fault. */
1179 swap_free(entry);
1180 if (vm_swap_full())
1181 remove_exclusive_swap_page(page);
1183 mm->rss++;
1184 pte = mk_pte(page, vma->vm_page_prot);
1185 if (write_access && can_share_swap_page(page))
1186 pte = pte_mkdirty(pte_mkwrite(pte));
1187 unlock_page(page);
1189 flush_page_to_ram(page);
1190 flush_icache_page(vma, page);
1191 #ifdef CONFIG_XEN
1192 if ( likely(vma->vm_mm == current->mm) ) {
1193 XEN_flush_page_update_queue();
1194 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, pte, 0);
1195 } else {
1196 set_pte(page_table, pte);
1197 XEN_flush_page_update_queue();
1199 #else
1200 set_pte(page_table, pte);
1201 #endif
1203 /* No need to invalidate - it was non-present before */
1204 update_mmu_cache(vma, address, pte);
1205 spin_unlock(&mm->page_table_lock);
1206 return ret;
1209 /*
1210 * We are called with the MM semaphore and page_table_lock
1211 * spinlock held to protect against concurrent faults in
1212 * multithreaded programs.
1213 */
1214 static int do_anonymous_page(struct mm_struct * mm, struct vm_area_struct * vma, pte_t *page_table, int write_access, unsigned long addr)
1216 pte_t entry;
1218 /* Read-only mapping of ZERO_PAGE. */
1219 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1221 /* ..except if it's a write access */
1222 if (write_access) {
1223 struct page *page;
1225 /* Allocate our own private page. */
1226 spin_unlock(&mm->page_table_lock);
1228 page = alloc_page(GFP_HIGHUSER);
1229 if (!page)
1230 goto no_mem;
1231 clear_user_highpage(page, addr);
1233 spin_lock(&mm->page_table_lock);
1234 if (!pte_none(*page_table)) {
1235 page_cache_release(page);
1236 spin_unlock(&mm->page_table_lock);
1237 return 1;
1239 mm->rss++;
1240 flush_page_to_ram(page);
1241 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1242 if (vm_anon_lru)
1243 lru_cache_add(page);
1244 mark_page_accessed(page);
1247 #ifdef CONFIG_XEN
1248 if ( likely(vma->vm_mm == current->mm) ) {
1249 XEN_flush_page_update_queue();
1250 HYPERVISOR_update_va_mapping(addr>>PAGE_SHIFT, entry, 0);
1251 } else {
1252 set_pte(page_table, entry);
1253 XEN_flush_page_update_queue();
1255 #else
1256 set_pte(page_table, entry);
1257 #endif
1259 /* No need to invalidate - it was non-present before */
1260 update_mmu_cache(vma, addr, entry);
1261 spin_unlock(&mm->page_table_lock);
1262 return 1; /* Minor fault */
1264 no_mem:
1265 return -1;
1268 /*
1269 * do_no_page() tries to create a new page mapping. It aggressively
1270 * tries to share with existing pages, but makes a separate copy if
1271 * the "write_access" parameter is true in order to avoid the next
1272 * page fault.
1274 * As this is called only for pages that do not currently exist, we
1275 * do not need to flush old virtual caches or the TLB.
1277 * This is called with the MM semaphore held and the page table
1278 * spinlock held. Exit with the spinlock released.
1279 */
1280 static int do_no_page(struct mm_struct * mm, struct vm_area_struct * vma,
1281 unsigned long address, int write_access, pte_t *page_table)
1283 struct page * new_page;
1284 pte_t entry;
1286 if (!vma->vm_ops || !vma->vm_ops->nopage)
1287 return do_anonymous_page(mm, vma, page_table, write_access, address);
1288 spin_unlock(&mm->page_table_lock);
1290 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1292 if (new_page == NULL) /* no page was available -- SIGBUS */
1293 return 0;
1294 if (new_page == NOPAGE_OOM)
1295 return -1;
1297 /*
1298 * Should we do an early C-O-W break?
1299 */
1300 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1301 struct page * page = alloc_page(GFP_HIGHUSER);
1302 if (!page) {
1303 page_cache_release(new_page);
1304 return -1;
1306 copy_user_highpage(page, new_page, address);
1307 page_cache_release(new_page);
1308 if (vm_anon_lru)
1309 lru_cache_add(page);
1310 new_page = page;
1313 spin_lock(&mm->page_table_lock);
1314 /*
1315 * This silly early PAGE_DIRTY setting removes a race
1316 * due to the bad i386 page protection. But it's valid
1317 * for other architectures too.
1319 * Note that if write_access is true, we either now have
1320 * an exclusive copy of the page, or this is a shared mapping,
1321 * so we can make it writable and dirty to avoid having to
1322 * handle that later.
1323 */
1324 /* Only go through if we didn't race with anybody else... */
1325 if (pte_none(*page_table)) {
1326 if (!PageReserved(new_page))
1327 ++mm->rss;
1328 flush_page_to_ram(new_page);
1329 flush_icache_page(vma, new_page);
1330 entry = mk_pte(new_page, vma->vm_page_prot);
1331 if (write_access)
1332 entry = pte_mkwrite(pte_mkdirty(entry));
1333 #ifdef CONFIG_XEN
1334 if ( likely(vma->vm_mm == current->mm) ) {
1335 XEN_flush_page_update_queue();
1336 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, entry, 0);
1337 } else {
1338 set_pte(page_table, entry);
1339 XEN_flush_page_update_queue();
1341 #else
1342 set_pte(page_table, entry);
1343 #endif
1344 } else {
1345 /* One of our sibling threads was faster, back out. */
1346 page_cache_release(new_page);
1347 spin_unlock(&mm->page_table_lock);
1348 return 1;
1351 /* no need to invalidate: a not-present page shouldn't be cached */
1352 update_mmu_cache(vma, address, entry);
1353 spin_unlock(&mm->page_table_lock);
1354 return 2; /* Major fault */
1357 /*
1358 * These routines also need to handle stuff like marking pages dirty
1359 * and/or accessed for architectures that don't do it in hardware (most
1360 * RISC architectures). The early dirtying is also good on the i386.
1362 * There is also a hook called "update_mmu_cache()" that architectures
1363 * with external mmu caches can use to update those (ie the Sparc or
1364 * PowerPC hashed page tables that act as extended TLBs).
1366 * Note the "page_table_lock". It is to protect against kswapd removing
1367 * pages from under us. Note that kswapd only ever _removes_ pages, never
1368 * adds them. As such, once we have noticed that the page is not present,
1369 * we can drop the lock early.
1371 * The adding of pages is protected by the MM semaphore (which we hold),
1372 * so we don't need to worry about a page being suddenly been added into
1373 * our VM.
1375 * We enter with the pagetable spinlock held, we are supposed to
1376 * release it when done.
1377 */
1378 static inline int handle_pte_fault(struct mm_struct *mm,
1379 struct vm_area_struct * vma, unsigned long address,
1380 int write_access, pte_t * pte)
1382 pte_t entry;
1384 entry = *pte;
1385 if (!pte_present(entry)) {
1386 /*
1387 * If it truly wasn't present, we know that kswapd
1388 * and the PTE updates will not touch it later. So
1389 * drop the lock.
1390 */
1391 if (pte_none(entry))
1392 return do_no_page(mm, vma, address, write_access, pte);
1393 return do_swap_page(mm, vma, address, pte, entry, write_access);
1396 if (write_access) {
1397 if (!pte_write(entry))
1398 return do_wp_page(mm, vma, address, pte, entry);
1400 entry = pte_mkdirty(entry);
1402 entry = pte_mkyoung(entry);
1403 establish_pte(vma, address, pte, entry);
1404 spin_unlock(&mm->page_table_lock);
1405 return 1;
1408 /*
1409 * By the time we get here, we already hold the mm semaphore
1410 */
1411 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1412 unsigned long address, int write_access)
1414 pgd_t *pgd;
1415 pmd_t *pmd;
1417 current->state = TASK_RUNNING;
1418 pgd = pgd_offset(mm, address);
1420 /*
1421 * We need the page table lock to synchronize with kswapd
1422 * and the SMP-safe atomic PTE updates.
1423 */
1424 spin_lock(&mm->page_table_lock);
1425 pmd = pmd_alloc(mm, pgd, address);
1427 if (pmd) {
1428 pte_t * pte = pte_alloc(mm, pmd, address);
1429 if (pte)
1430 return handle_pte_fault(mm, vma, address, write_access, pte);
1432 spin_unlock(&mm->page_table_lock);
1433 return -1;
1436 /*
1437 * Allocate page middle directory.
1439 * We've already handled the fast-path in-line, and we own the
1440 * page table lock.
1442 * On a two-level page table, this ends up actually being entirely
1443 * optimized away.
1444 */
1445 pmd_t fastcall *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1447 pmd_t *new;
1449 /* "fast" allocation can happen without dropping the lock.. */
1450 new = pmd_alloc_one_fast(mm, address);
1451 if (!new) {
1452 spin_unlock(&mm->page_table_lock);
1453 new = pmd_alloc_one(mm, address);
1454 spin_lock(&mm->page_table_lock);
1455 if (!new)
1456 return NULL;
1458 /*
1459 * Because we dropped the lock, we should re-check the
1460 * entry, as somebody else could have populated it..
1461 */
1462 if (!pgd_none(*pgd)) {
1463 pmd_free(new);
1464 check_pgt_cache();
1465 goto out;
1468 pgd_populate(mm, pgd, new);
1469 out:
1470 return pmd_offset(pgd, address);
1473 /*
1474 * Allocate the page table directory.
1476 * We've already handled the fast-path in-line, and we own the
1477 * page table lock.
1478 */
1479 pte_t fastcall *pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1481 if (pmd_none(*pmd)) {
1482 pte_t *new;
1484 /* "fast" allocation can happen without dropping the lock.. */
1485 new = pte_alloc_one_fast(mm, address);
1486 if (!new) {
1487 XEN_flush_page_update_queue();
1488 spin_unlock(&mm->page_table_lock);
1489 new = pte_alloc_one(mm, address);
1490 spin_lock(&mm->page_table_lock);
1491 if (!new)
1492 return NULL;
1494 /*
1495 * Because we dropped the lock, we should re-check the
1496 * entry, as somebody else could have populated it..
1497 */
1498 if (!pmd_none(*pmd)) {
1499 pte_free(new);
1500 check_pgt_cache();
1501 goto out;
1504 pmd_populate(mm, pmd, new);
1506 out:
1507 return pte_offset(pmd, address);
1510 int make_pages_present(unsigned long addr, unsigned long end)
1512 int ret, len, write;
1513 struct vm_area_struct * vma;
1515 vma = find_vma(current->mm, addr);
1516 write = (vma->vm_flags & VM_WRITE) != 0;
1517 if (addr >= end)
1518 BUG();
1519 if (end > vma->vm_end)
1520 BUG();
1521 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1522 ret = get_user_pages(current, current->mm, addr,
1523 len, write, 0, NULL, NULL);
1524 return ret == len ? 0 : -1;
1527 struct page * vmalloc_to_page(void * vmalloc_addr)
1529 unsigned long addr = (unsigned long) vmalloc_addr;
1530 struct page *page = NULL;
1531 pmd_t *pmd;
1532 pte_t *pte;
1533 pgd_t *pgd;
1535 pgd = pgd_offset_k(addr);
1536 if (!pgd_none(*pgd)) {
1537 pmd = pmd_offset(pgd, addr);
1538 if (!pmd_none(*pmd)) {
1539 pte = pte_offset(pmd, addr);
1540 if (pte_present(*pte)) {
1541 page = pte_page(*pte);
1545 return page;