ia64/xen-unstable

view linux-2.4.26-xen-sparse/mm/memory.c @ 1527:a815a43920c0

bitkeeper revision 1.994 (40d6ed9ePUmxTwjKFv1vprN2-xFpmQ)

Install mkernel odules with 'make install'
author iap10@labyrinth.cl.cam.ac.uk
date Mon Jun 21 14:15:58 2004 +0000 (2004-06-21)
parents f3123052268f
children 0e1219bec03c cbee10dcdd93
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 defined(CONFIG_XEN_PRIVILEGED_GUEST)
322 if (pte_io(pte)) {
323 queue_l1_entry_update(ptep, 0);
324 continue;
325 }
326 #endif
327 if (VALID_PAGE(page) && !PageReserved(page))
328 freed ++;
329 /* This will eventually call __free_pte on the pte. */
330 tlb_remove_page(tlb, ptep, address + offset);
331 } else {
332 free_swap_and_cache(pte_to_swp_entry(pte));
333 pte_clear(ptep);
334 }
335 }
337 return freed;
338 }
340 static inline int zap_pmd_range(mmu_gather_t *tlb, pgd_t * dir, unsigned long address, unsigned long size)
341 {
342 pmd_t * pmd;
343 unsigned long end;
344 int freed;
346 if (pgd_none(*dir))
347 return 0;
348 if (pgd_bad(*dir)) {
349 pgd_ERROR(*dir);
350 pgd_clear(dir);
351 return 0;
352 }
353 pmd = pmd_offset(dir, address);
354 end = address + size;
355 if (end > ((address + PGDIR_SIZE) & PGDIR_MASK))
356 end = ((address + PGDIR_SIZE) & PGDIR_MASK);
357 freed = 0;
358 do {
359 freed += zap_pte_range(tlb, pmd, address, end - address);
360 address = (address + PMD_SIZE) & PMD_MASK;
361 pmd++;
362 } while (address < end);
363 return freed;
364 }
366 /*
367 * remove user pages in a given range.
368 */
369 void zap_page_range(struct mm_struct *mm, unsigned long address, unsigned long size)
370 {
371 mmu_gather_t *tlb;
372 pgd_t * dir;
373 unsigned long start = address, end = address + size;
374 int freed = 0;
376 dir = pgd_offset(mm, address);
378 /*
379 * This is a long-lived spinlock. That's fine.
380 * There's no contention, because the page table
381 * lock only protects against kswapd anyway, and
382 * even if kswapd happened to be looking at this
383 * process we _want_ it to get stuck.
384 */
385 if (address >= end)
386 BUG();
387 spin_lock(&mm->page_table_lock);
388 flush_cache_range(mm, address, end);
389 tlb = tlb_gather_mmu(mm);
391 do {
392 freed += zap_pmd_range(tlb, dir, address, end - address);
393 address = (address + PGDIR_SIZE) & PGDIR_MASK;
394 dir++;
395 } while (address && (address < end));
397 /* this will flush any remaining tlb entries */
398 tlb_finish_mmu(tlb, start, end);
400 /*
401 * Update rss for the mm_struct (not necessarily current->mm)
402 * Notice that rss is an unsigned long.
403 */
404 if (mm->rss > freed)
405 mm->rss -= freed;
406 else
407 mm->rss = 0;
408 spin_unlock(&mm->page_table_lock);
409 }
411 /*
412 * Do a quick page-table lookup for a single page.
413 */
414 static struct page * follow_page(struct mm_struct *mm, unsigned long address, int write)
415 {
416 pgd_t *pgd;
417 pmd_t *pmd;
418 pte_t *ptep, pte;
420 pgd = pgd_offset(mm, address);
421 if (pgd_none(*pgd) || pgd_bad(*pgd))
422 goto out;
424 pmd = pmd_offset(pgd, address);
425 if (pmd_none(*pmd) || pmd_bad(*pmd))
426 goto out;
428 ptep = pte_offset(pmd, address);
429 if (!ptep)
430 goto out;
432 pte = *ptep;
433 if (pte_present(pte)) {
434 if (!write ||
435 (pte_write(pte) && pte_dirty(pte)))
436 return pte_page(pte);
437 }
439 out:
440 return 0;
441 }
443 /*
444 * Given a physical address, is there a useful struct page pointing to
445 * it? This may become more complex in the future if we start dealing
446 * with IO-aperture pages in kiobufs.
447 */
449 static inline struct page * get_page_map(struct page *page)
450 {
451 if (!VALID_PAGE(page))
452 return 0;
453 return page;
454 }
456 /*
457 * Please read Documentation/cachetlb.txt before using this function,
458 * accessing foreign memory spaces can cause cache coherency problems.
459 *
460 * Accessing a VM_IO area is even more dangerous, therefore the function
461 * fails if pages is != NULL and a VM_IO area is found.
462 */
463 int get_user_pages(struct task_struct *tsk, struct mm_struct *mm, unsigned long start,
464 int len, int write, int force, struct page **pages, struct vm_area_struct **vmas)
465 {
466 int i;
467 unsigned int flags;
469 /*
470 * Require read or write permissions.
471 * If 'force' is set, we only require the "MAY" flags.
472 */
473 flags = write ? (VM_WRITE | VM_MAYWRITE) : (VM_READ | VM_MAYREAD);
474 flags &= force ? (VM_MAYREAD | VM_MAYWRITE) : (VM_READ | VM_WRITE);
475 i = 0;
477 do {
478 struct vm_area_struct * vma;
480 vma = find_extend_vma(mm, start);
482 if ( !vma || (pages && vma->vm_flags & VM_IO) || !(flags & vma->vm_flags) )
483 return i ? : -EFAULT;
485 spin_lock(&mm->page_table_lock);
486 do {
487 struct page *map;
488 while (!(map = follow_page(mm, start, write))) {
489 spin_unlock(&mm->page_table_lock);
490 switch (handle_mm_fault(mm, vma, start, write)) {
491 case 1:
492 tsk->min_flt++;
493 break;
494 case 2:
495 tsk->maj_flt++;
496 break;
497 case 0:
498 if (i) return i;
499 return -EFAULT;
500 default:
501 if (i) return i;
502 return -ENOMEM;
503 }
504 spin_lock(&mm->page_table_lock);
505 }
506 if (pages) {
507 pages[i] = get_page_map(map);
508 /* FIXME: call the correct function,
509 * depending on the type of the found page
510 */
511 if (!pages[i])
512 goto bad_page;
513 page_cache_get(pages[i]);
514 }
515 if (vmas)
516 vmas[i] = vma;
517 i++;
518 start += PAGE_SIZE;
519 len--;
520 } while(len && start < vma->vm_end);
521 spin_unlock(&mm->page_table_lock);
522 } while(len);
523 out:
524 return i;
526 /*
527 * We found an invalid page in the VMA. Release all we have
528 * so far and fail.
529 */
530 bad_page:
531 spin_unlock(&mm->page_table_lock);
532 while (i--)
533 page_cache_release(pages[i]);
534 i = -EFAULT;
535 goto out;
536 }
538 EXPORT_SYMBOL(get_user_pages);
540 /*
541 * Force in an entire range of pages from the current process's user VA,
542 * and pin them in physical memory.
543 */
544 #define dprintk(x...)
546 int map_user_kiobuf(int rw, struct kiobuf *iobuf, unsigned long va, size_t len)
547 {
548 int pgcount, err;
549 struct mm_struct * mm;
551 /* Make sure the iobuf is not already mapped somewhere. */
552 if (iobuf->nr_pages)
553 return -EINVAL;
555 mm = current->mm;
556 dprintk ("map_user_kiobuf: begin\n");
558 pgcount = (va + len + PAGE_SIZE - 1)/PAGE_SIZE - va/PAGE_SIZE;
559 /* mapping 0 bytes is not permitted */
560 if (!pgcount) BUG();
561 err = expand_kiobuf(iobuf, pgcount);
562 if (err)
563 return err;
565 iobuf->locked = 0;
566 iobuf->offset = va & (PAGE_SIZE-1);
567 iobuf->length = len;
569 /* Try to fault in all of the necessary pages */
570 down_read(&mm->mmap_sem);
571 /* rw==READ means read from disk, write into memory area */
572 err = get_user_pages(current, mm, va, pgcount,
573 (rw==READ), 0, iobuf->maplist, NULL);
574 up_read(&mm->mmap_sem);
575 if (err < 0) {
576 unmap_kiobuf(iobuf);
577 dprintk ("map_user_kiobuf: end %d\n", err);
578 return err;
579 }
580 iobuf->nr_pages = err;
581 while (pgcount--) {
582 /* FIXME: flush superflous for rw==READ,
583 * probably wrong function for rw==WRITE
584 */
585 flush_dcache_page(iobuf->maplist[pgcount]);
586 }
587 dprintk ("map_user_kiobuf: end OK\n");
588 return 0;
589 }
591 /*
592 * Mark all of the pages in a kiobuf as dirty
593 *
594 * We need to be able to deal with short reads from disk: if an IO error
595 * occurs, the number of bytes read into memory may be less than the
596 * size of the kiobuf, so we have to stop marking pages dirty once the
597 * requested byte count has been reached.
598 *
599 * Must be called from process context - set_page_dirty() takes VFS locks.
600 */
602 void mark_dirty_kiobuf(struct kiobuf *iobuf, int bytes)
603 {
604 int index, offset, remaining;
605 struct page *page;
607 index = iobuf->offset >> PAGE_SHIFT;
608 offset = iobuf->offset & ~PAGE_MASK;
609 remaining = bytes;
610 if (remaining > iobuf->length)
611 remaining = iobuf->length;
613 while (remaining > 0 && index < iobuf->nr_pages) {
614 page = iobuf->maplist[index];
616 if (!PageReserved(page))
617 set_page_dirty(page);
619 remaining -= (PAGE_SIZE - offset);
620 offset = 0;
621 index++;
622 }
623 }
625 /*
626 * Unmap all of the pages referenced by a kiobuf. We release the pages,
627 * and unlock them if they were locked.
628 */
630 void unmap_kiobuf (struct kiobuf *iobuf)
631 {
632 int i;
633 struct page *map;
635 for (i = 0; i < iobuf->nr_pages; i++) {
636 map = iobuf->maplist[i];
637 if (map) {
638 if (iobuf->locked)
639 UnlockPage(map);
640 /* FIXME: cache flush missing for rw==READ
641 * FIXME: call the correct reference counting function
642 */
643 page_cache_release(map);
644 }
645 }
647 iobuf->nr_pages = 0;
648 iobuf->locked = 0;
649 }
652 /*
653 * Lock down all of the pages of a kiovec for IO.
654 *
655 * If any page is mapped twice in the kiovec, we return the error -EINVAL.
656 *
657 * The optional wait parameter causes the lock call to block until all
658 * pages can be locked if set. If wait==0, the lock operation is
659 * aborted if any locked pages are found and -EAGAIN is returned.
660 */
662 int lock_kiovec(int nr, struct kiobuf *iovec[], int wait)
663 {
664 struct kiobuf *iobuf;
665 int i, j;
666 struct page *page, **ppage;
667 int doublepage = 0;
668 int repeat = 0;
670 repeat:
672 for (i = 0; i < nr; i++) {
673 iobuf = iovec[i];
675 if (iobuf->locked)
676 continue;
678 ppage = iobuf->maplist;
679 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
680 page = *ppage;
681 if (!page)
682 continue;
684 if (TryLockPage(page)) {
685 while (j--) {
686 struct page *tmp = *--ppage;
687 if (tmp)
688 UnlockPage(tmp);
689 }
690 goto retry;
691 }
692 }
693 iobuf->locked = 1;
694 }
696 return 0;
698 retry:
700 /*
701 * We couldn't lock one of the pages. Undo the locking so far,
702 * wait on the page we got to, and try again.
703 */
705 unlock_kiovec(nr, iovec);
706 if (!wait)
707 return -EAGAIN;
709 /*
710 * Did the release also unlock the page we got stuck on?
711 */
712 if (!PageLocked(page)) {
713 /*
714 * If so, we may well have the page mapped twice
715 * in the IO address range. Bad news. Of
716 * course, it _might_ just be a coincidence,
717 * but if it happens more than once, chances
718 * are we have a double-mapped page.
719 */
720 if (++doublepage >= 3)
721 return -EINVAL;
723 /* Try again... */
724 wait_on_page(page);
725 }
727 if (++repeat < 16)
728 goto repeat;
729 return -EAGAIN;
730 }
732 /*
733 * Unlock all of the pages of a kiovec after IO.
734 */
736 int unlock_kiovec(int nr, struct kiobuf *iovec[])
737 {
738 struct kiobuf *iobuf;
739 int i, j;
740 struct page *page, **ppage;
742 for (i = 0; i < nr; i++) {
743 iobuf = iovec[i];
745 if (!iobuf->locked)
746 continue;
747 iobuf->locked = 0;
749 ppage = iobuf->maplist;
750 for (j = 0; j < iobuf->nr_pages; ppage++, j++) {
751 page = *ppage;
752 if (!page)
753 continue;
754 UnlockPage(page);
755 }
756 }
757 return 0;
758 }
760 static inline void zeromap_pte_range(pte_t * pte, unsigned long address,
761 unsigned long size, pgprot_t prot)
762 {
763 unsigned long end;
765 address &= ~PMD_MASK;
766 end = address + size;
767 if (end > PMD_SIZE)
768 end = PMD_SIZE;
769 do {
770 pte_t zero_pte = pte_wrprotect(mk_pte(ZERO_PAGE(address), prot));
771 pte_t oldpage = ptep_get_and_clear(pte);
772 set_pte(pte, zero_pte);
773 forget_pte(oldpage);
774 address += PAGE_SIZE;
775 pte++;
776 } while (address && (address < end));
777 }
779 static inline int zeromap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address,
780 unsigned long size, pgprot_t prot)
781 {
782 unsigned long end;
784 address &= ~PGDIR_MASK;
785 end = address + size;
786 if (end > PGDIR_SIZE)
787 end = PGDIR_SIZE;
788 do {
789 pte_t * pte = pte_alloc(mm, pmd, address);
790 if (!pte)
791 return -ENOMEM;
792 zeromap_pte_range(pte, address, end - address, prot);
793 address = (address + PMD_SIZE) & PMD_MASK;
794 pmd++;
795 } while (address && (address < end));
796 return 0;
797 }
799 int zeromap_page_range(unsigned long address, unsigned long size, pgprot_t prot)
800 {
801 int error = 0;
802 pgd_t * dir;
803 unsigned long beg = address;
804 unsigned long end = address + size;
805 struct mm_struct *mm = current->mm;
807 dir = pgd_offset(mm, address);
808 flush_cache_range(mm, beg, end);
809 if (address >= end)
810 BUG();
812 spin_lock(&mm->page_table_lock);
813 do {
814 pmd_t *pmd = pmd_alloc(mm, dir, address);
815 error = -ENOMEM;
816 if (!pmd)
817 break;
818 error = zeromap_pmd_range(mm, pmd, address, end - address, prot);
819 if (error)
820 break;
821 address = (address + PGDIR_SIZE) & PGDIR_MASK;
822 dir++;
823 } while (address && (address < end));
824 spin_unlock(&mm->page_table_lock);
825 flush_tlb_range(mm, beg, end);
826 return error;
827 }
829 /*
830 * maps a range of physical memory into the requested pages. the old
831 * mappings are removed. any references to nonexistent pages results
832 * in null mappings (currently treated as "copy-on-access")
833 */
834 static inline void remap_pte_range(pte_t * pte, unsigned long address, unsigned long size,
835 unsigned long phys_addr, pgprot_t prot)
836 {
837 unsigned long end;
839 address &= ~PMD_MASK;
840 end = address + size;
841 if (end > PMD_SIZE)
842 end = PMD_SIZE;
843 do {
844 struct page *page;
845 pte_t oldpage;
846 oldpage = ptep_get_and_clear(pte);
848 page = virt_to_page(__va(phys_addr));
849 if ((!VALID_PAGE(page)) || PageReserved(page))
850 set_pte(pte, mk_pte_phys(phys_addr, prot));
851 forget_pte(oldpage);
852 address += PAGE_SIZE;
853 phys_addr += PAGE_SIZE;
854 pte++;
855 } while (address && (address < end));
856 }
858 static inline int remap_pmd_range(struct mm_struct *mm, pmd_t * pmd, unsigned long address, unsigned long size,
859 unsigned long phys_addr, pgprot_t prot)
860 {
861 unsigned long end;
863 address &= ~PGDIR_MASK;
864 end = address + size;
865 if (end > PGDIR_SIZE)
866 end = PGDIR_SIZE;
867 phys_addr -= address;
868 do {
869 pte_t * pte = pte_alloc(mm, pmd, address);
870 if (!pte)
871 return -ENOMEM;
872 remap_pte_range(pte, address, end - address, address + phys_addr, prot);
873 address = (address + PMD_SIZE) & PMD_MASK;
874 pmd++;
875 } while (address && (address < end));
876 return 0;
877 }
879 /* Note: this is only safe if the mm semaphore is held when called. */
880 int remap_page_range(unsigned long from, unsigned long phys_addr, unsigned long size, pgprot_t prot)
881 {
882 int error = 0;
883 pgd_t * dir;
884 unsigned long beg = from;
885 unsigned long end = from + size;
886 struct mm_struct *mm = current->mm;
888 phys_addr -= from;
889 dir = pgd_offset(mm, from);
890 flush_cache_range(mm, beg, end);
891 if (from >= end)
892 BUG();
894 spin_lock(&mm->page_table_lock);
895 do {
896 pmd_t *pmd = pmd_alloc(mm, dir, from);
897 error = -ENOMEM;
898 if (!pmd)
899 break;
900 error = remap_pmd_range(mm, pmd, from, end - from, phys_addr + from, prot);
901 if (error)
902 break;
903 from = (from + PGDIR_SIZE) & PGDIR_MASK;
904 dir++;
905 } while (from && (from < end));
906 spin_unlock(&mm->page_table_lock);
907 flush_tlb_range(mm, beg, end);
908 return error;
909 }
911 /*
912 * Establish a new mapping:
913 * - flush the old one
914 * - update the page tables
915 * - inform the TLB about the new one
916 *
917 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
918 */
919 static inline void establish_pte(struct vm_area_struct * vma, unsigned long address, pte_t *page_table, pte_t entry)
920 {
921 #ifdef CONFIG_XEN
922 if ( likely(vma->vm_mm == current->mm) ) {
923 XEN_flush_page_update_queue();
924 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, entry, UVMF_INVLPG);
925 } else {
926 set_pte(page_table, entry);
927 flush_tlb_page(vma, address);
928 }
929 #else
930 set_pte(page_table, entry);
931 flush_tlb_page(vma, address);
932 #endif
933 update_mmu_cache(vma, address, entry);
934 }
936 /*
937 * We hold the mm semaphore for reading and vma->vm_mm->page_table_lock
938 */
939 static inline void break_cow(struct vm_area_struct * vma, struct page * new_page, unsigned long address,
940 pte_t *page_table)
941 {
942 flush_page_to_ram(new_page);
943 flush_cache_page(vma, address);
944 establish_pte(vma, address, page_table, pte_mkwrite(pte_mkdirty(mk_pte(new_page, vma->vm_page_prot))));
945 }
947 /*
948 * This routine handles present pages, when users try to write
949 * to a shared page. It is done by copying the page to a new address
950 * and decrementing the shared-page counter for the old page.
951 *
952 * Goto-purists beware: the only reason for goto's here is that it results
953 * in better assembly code.. The "default" path will see no jumps at all.
954 *
955 * Note that this routine assumes that the protection checks have been
956 * done by the caller (the low-level page fault routine in most cases).
957 * Thus we can safely just mark it writable once we've done any necessary
958 * COW.
959 *
960 * We also mark the page dirty at this point even though the page will
961 * change only once the write actually happens. This avoids a few races,
962 * and potentially makes it more efficient.
963 *
964 * We hold the mm semaphore and the page_table_lock on entry and exit
965 * with the page_table_lock released.
966 */
967 static int do_wp_page(struct mm_struct *mm, struct vm_area_struct * vma,
968 unsigned long address, pte_t *page_table, pte_t pte)
969 {
970 struct page *old_page, *new_page;
972 old_page = pte_page(pte);
973 if (!VALID_PAGE(old_page))
974 goto bad_wp_page;
976 if (!TryLockPage(old_page)) {
977 int reuse = can_share_swap_page(old_page);
978 unlock_page(old_page);
979 if (reuse) {
980 flush_cache_page(vma, address);
981 establish_pte(vma, address, page_table, pte_mkyoung(pte_mkdirty(pte_mkwrite(pte))));
982 spin_unlock(&mm->page_table_lock);
983 return 1; /* Minor fault */
984 }
985 }
987 /*
988 * Ok, we need to copy. Oh, well..
989 */
990 page_cache_get(old_page);
991 spin_unlock(&mm->page_table_lock);
993 new_page = alloc_page(GFP_HIGHUSER);
994 if (!new_page)
995 goto no_mem;
996 copy_cow_page(old_page,new_page,address);
998 /*
999 * Re-check the pte - we dropped the lock
1000 */
1001 spin_lock(&mm->page_table_lock);
1002 if (pte_same(*page_table, pte)) {
1003 if (PageReserved(old_page))
1004 ++mm->rss;
1005 break_cow(vma, new_page, address, page_table);
1006 lru_cache_add(new_page);
1008 /* Free the old page.. */
1009 new_page = old_page;
1011 spin_unlock(&mm->page_table_lock);
1012 page_cache_release(new_page);
1013 page_cache_release(old_page);
1014 return 1; /* Minor fault */
1016 bad_wp_page:
1017 spin_unlock(&mm->page_table_lock);
1018 printk("do_wp_page: bogus page at address %08lx (page 0x%lx)\n",address,(unsigned long)old_page);
1019 return -1;
1020 no_mem:
1021 page_cache_release(old_page);
1022 return -1;
1025 static void vmtruncate_list(struct vm_area_struct *mpnt, unsigned long pgoff)
1027 do {
1028 struct mm_struct *mm = mpnt->vm_mm;
1029 unsigned long start = mpnt->vm_start;
1030 unsigned long end = mpnt->vm_end;
1031 unsigned long len = end - start;
1032 unsigned long diff;
1034 /* mapping wholly truncated? */
1035 if (mpnt->vm_pgoff >= pgoff) {
1036 zap_page_range(mm, start, len);
1037 continue;
1040 /* mapping wholly unaffected? */
1041 len = len >> PAGE_SHIFT;
1042 diff = pgoff - mpnt->vm_pgoff;
1043 if (diff >= len)
1044 continue;
1046 /* Ok, partially affected.. */
1047 start += diff << PAGE_SHIFT;
1048 len = (len - diff) << PAGE_SHIFT;
1049 zap_page_range(mm, start, len);
1050 } while ((mpnt = mpnt->vm_next_share) != NULL);
1053 /*
1054 * Handle all mappings that got truncated by a "truncate()"
1055 * system call.
1057 * NOTE! We have to be ready to update the memory sharing
1058 * between the file and the memory map for a potential last
1059 * incomplete page. Ugly, but necessary.
1060 */
1061 int vmtruncate(struct inode * inode, loff_t offset)
1063 unsigned long pgoff;
1064 struct address_space *mapping = inode->i_mapping;
1065 unsigned long limit;
1067 if (inode->i_size < offset)
1068 goto do_expand;
1069 inode->i_size = offset;
1070 spin_lock(&mapping->i_shared_lock);
1071 if (!mapping->i_mmap && !mapping->i_mmap_shared)
1072 goto out_unlock;
1074 pgoff = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
1075 if (mapping->i_mmap != NULL)
1076 vmtruncate_list(mapping->i_mmap, pgoff);
1077 if (mapping->i_mmap_shared != NULL)
1078 vmtruncate_list(mapping->i_mmap_shared, pgoff);
1080 out_unlock:
1081 spin_unlock(&mapping->i_shared_lock);
1082 truncate_inode_pages(mapping, offset);
1083 goto out_truncate;
1085 do_expand:
1086 limit = current->rlim[RLIMIT_FSIZE].rlim_cur;
1087 if (limit != RLIM_INFINITY && offset > limit)
1088 goto out_sig;
1089 if (offset > inode->i_sb->s_maxbytes)
1090 goto out;
1091 inode->i_size = offset;
1093 out_truncate:
1094 if (inode->i_op && inode->i_op->truncate) {
1095 lock_kernel();
1096 inode->i_op->truncate(inode);
1097 unlock_kernel();
1099 return 0;
1100 out_sig:
1101 send_sig(SIGXFSZ, current, 0);
1102 out:
1103 return -EFBIG;
1106 /*
1107 * Primitive swap readahead code. We simply read an aligned block of
1108 * (1 << page_cluster) entries in the swap area. This method is chosen
1109 * because it doesn't cost us any seek time. We also make sure to queue
1110 * the 'original' request together with the readahead ones...
1111 */
1112 void swapin_readahead(swp_entry_t entry)
1114 int i, num;
1115 struct page *new_page;
1116 unsigned long offset;
1118 /*
1119 * Get the number of handles we should do readahead io to.
1120 */
1121 num = valid_swaphandles(entry, &offset);
1122 for (i = 0; i < num; offset++, i++) {
1123 /* Ok, do the async read-ahead now */
1124 new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset));
1125 if (!new_page)
1126 break;
1127 page_cache_release(new_page);
1129 return;
1132 /*
1133 * We hold the mm semaphore and the page_table_lock on entry and
1134 * should release the pagetable lock on exit..
1135 */
1136 static int do_swap_page(struct mm_struct * mm,
1137 struct vm_area_struct * vma, unsigned long address,
1138 pte_t * page_table, pte_t orig_pte, int write_access)
1140 struct page *page;
1141 swp_entry_t entry = pte_to_swp_entry(orig_pte);
1142 pte_t pte;
1143 int ret = 1;
1145 spin_unlock(&mm->page_table_lock);
1146 page = lookup_swap_cache(entry);
1147 if (!page) {
1148 swapin_readahead(entry);
1149 page = read_swap_cache_async(entry);
1150 if (!page) {
1151 /*
1152 * Back out if somebody else faulted in this pte while
1153 * we released the page table lock.
1154 */
1155 int retval;
1156 spin_lock(&mm->page_table_lock);
1157 retval = pte_same(*page_table, orig_pte) ? -1 : 1;
1158 spin_unlock(&mm->page_table_lock);
1159 return retval;
1162 /* Had to read the page from swap area: Major fault */
1163 ret = 2;
1166 mark_page_accessed(page);
1168 lock_page(page);
1170 /*
1171 * Back out if somebody else faulted in this pte while we
1172 * released the page table lock.
1173 */
1174 spin_lock(&mm->page_table_lock);
1175 if (!pte_same(*page_table, orig_pte)) {
1176 spin_unlock(&mm->page_table_lock);
1177 unlock_page(page);
1178 page_cache_release(page);
1179 return 1;
1182 /* The page isn't present yet, go ahead with the fault. */
1184 swap_free(entry);
1185 if (vm_swap_full())
1186 remove_exclusive_swap_page(page);
1188 mm->rss++;
1189 pte = mk_pte(page, vma->vm_page_prot);
1190 if (write_access && can_share_swap_page(page))
1191 pte = pte_mkdirty(pte_mkwrite(pte));
1192 unlock_page(page);
1194 flush_page_to_ram(page);
1195 flush_icache_page(vma, page);
1196 #ifdef CONFIG_XEN
1197 if ( likely(vma->vm_mm == current->mm) ) {
1198 XEN_flush_page_update_queue();
1199 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, pte, 0);
1200 } else {
1201 set_pte(page_table, pte);
1202 XEN_flush_page_update_queue();
1204 #else
1205 set_pte(page_table, pte);
1206 #endif
1208 /* No need to invalidate - it was non-present before */
1209 update_mmu_cache(vma, address, pte);
1210 spin_unlock(&mm->page_table_lock);
1211 return ret;
1214 /*
1215 * We are called with the MM semaphore and page_table_lock
1216 * spinlock held to protect against concurrent faults in
1217 * multithreaded programs.
1218 */
1219 static int do_anonymous_page(struct mm_struct * mm, struct vm_area_struct * vma, pte_t *page_table, int write_access, unsigned long addr)
1221 pte_t entry;
1223 /* Read-only mapping of ZERO_PAGE. */
1224 entry = pte_wrprotect(mk_pte(ZERO_PAGE(addr), vma->vm_page_prot));
1226 /* ..except if it's a write access */
1227 if (write_access) {
1228 struct page *page;
1230 /* Allocate our own private page. */
1231 spin_unlock(&mm->page_table_lock);
1233 page = alloc_page(GFP_HIGHUSER);
1234 if (!page)
1235 goto no_mem;
1236 clear_user_highpage(page, addr);
1238 spin_lock(&mm->page_table_lock);
1239 if (!pte_none(*page_table)) {
1240 page_cache_release(page);
1241 spin_unlock(&mm->page_table_lock);
1242 return 1;
1244 mm->rss++;
1245 flush_page_to_ram(page);
1246 entry = pte_mkwrite(pte_mkdirty(mk_pte(page, vma->vm_page_prot)));
1247 lru_cache_add(page);
1248 mark_page_accessed(page);
1251 #ifdef CONFIG_XEN
1252 if ( likely(vma->vm_mm == current->mm) ) {
1253 XEN_flush_page_update_queue();
1254 HYPERVISOR_update_va_mapping(addr>>PAGE_SHIFT, entry, 0);
1255 } else {
1256 set_pte(page_table, entry);
1257 XEN_flush_page_update_queue();
1259 #else
1260 set_pte(page_table, entry);
1261 #endif
1263 /* No need to invalidate - it was non-present before */
1264 update_mmu_cache(vma, addr, entry);
1265 spin_unlock(&mm->page_table_lock);
1266 return 1; /* Minor fault */
1268 no_mem:
1269 return -1;
1272 /*
1273 * do_no_page() tries to create a new page mapping. It aggressively
1274 * tries to share with existing pages, but makes a separate copy if
1275 * the "write_access" parameter is true in order to avoid the next
1276 * page fault.
1278 * As this is called only for pages that do not currently exist, we
1279 * do not need to flush old virtual caches or the TLB.
1281 * This is called with the MM semaphore held and the page table
1282 * spinlock held. Exit with the spinlock released.
1283 */
1284 static int do_no_page(struct mm_struct * mm, struct vm_area_struct * vma,
1285 unsigned long address, int write_access, pte_t *page_table)
1287 struct page * new_page;
1288 pte_t entry;
1290 if (!vma->vm_ops || !vma->vm_ops->nopage)
1291 return do_anonymous_page(mm, vma, page_table, write_access, address);
1292 spin_unlock(&mm->page_table_lock);
1294 new_page = vma->vm_ops->nopage(vma, address & PAGE_MASK, 0);
1296 if (new_page == NULL) /* no page was available -- SIGBUS */
1297 return 0;
1298 if (new_page == NOPAGE_OOM)
1299 return -1;
1301 /*
1302 * Should we do an early C-O-W break?
1303 */
1304 if (write_access && !(vma->vm_flags & VM_SHARED)) {
1305 struct page * page = alloc_page(GFP_HIGHUSER);
1306 if (!page) {
1307 page_cache_release(new_page);
1308 return -1;
1310 copy_user_highpage(page, new_page, address);
1311 page_cache_release(new_page);
1312 lru_cache_add(page);
1313 new_page = page;
1316 spin_lock(&mm->page_table_lock);
1317 /*
1318 * This silly early PAGE_DIRTY setting removes a race
1319 * due to the bad i386 page protection. But it's valid
1320 * for other architectures too.
1322 * Note that if write_access is true, we either now have
1323 * an exclusive copy of the page, or this is a shared mapping,
1324 * so we can make it writable and dirty to avoid having to
1325 * handle that later.
1326 */
1327 /* Only go through if we didn't race with anybody else... */
1328 if (pte_none(*page_table)) {
1329 if (!PageReserved(new_page))
1330 ++mm->rss;
1331 flush_page_to_ram(new_page);
1332 flush_icache_page(vma, new_page);
1333 entry = mk_pte(new_page, vma->vm_page_prot);
1334 if (write_access)
1335 entry = pte_mkwrite(pte_mkdirty(entry));
1336 #ifdef CONFIG_XEN
1337 if ( likely(vma->vm_mm == current->mm) ) {
1338 XEN_flush_page_update_queue();
1339 HYPERVISOR_update_va_mapping(address>>PAGE_SHIFT, entry, 0);
1340 } else {
1341 set_pte(page_table, entry);
1342 XEN_flush_page_update_queue();
1344 #else
1345 set_pte(page_table, entry);
1346 #endif
1347 } else {
1348 /* One of our sibling threads was faster, back out. */
1349 page_cache_release(new_page);
1350 spin_unlock(&mm->page_table_lock);
1351 return 1;
1354 /* no need to invalidate: a not-present page shouldn't be cached */
1355 update_mmu_cache(vma, address, entry);
1356 spin_unlock(&mm->page_table_lock);
1357 return 2; /* Major fault */
1360 /*
1361 * These routines also need to handle stuff like marking pages dirty
1362 * and/or accessed for architectures that don't do it in hardware (most
1363 * RISC architectures). The early dirtying is also good on the i386.
1365 * There is also a hook called "update_mmu_cache()" that architectures
1366 * with external mmu caches can use to update those (ie the Sparc or
1367 * PowerPC hashed page tables that act as extended TLBs).
1369 * Note the "page_table_lock". It is to protect against kswapd removing
1370 * pages from under us. Note that kswapd only ever _removes_ pages, never
1371 * adds them. As such, once we have noticed that the page is not present,
1372 * we can drop the lock early.
1374 * The adding of pages is protected by the MM semaphore (which we hold),
1375 * so we don't need to worry about a page being suddenly been added into
1376 * our VM.
1378 * We enter with the pagetable spinlock held, we are supposed to
1379 * release it when done.
1380 */
1381 static inline int handle_pte_fault(struct mm_struct *mm,
1382 struct vm_area_struct * vma, unsigned long address,
1383 int write_access, pte_t * pte)
1385 pte_t entry;
1387 entry = *pte;
1388 if (!pte_present(entry)) {
1389 /*
1390 * If it truly wasn't present, we know that kswapd
1391 * and the PTE updates will not touch it later. So
1392 * drop the lock.
1393 */
1394 if (pte_none(entry))
1395 return do_no_page(mm, vma, address, write_access, pte);
1396 return do_swap_page(mm, vma, address, pte, entry, write_access);
1399 if (write_access) {
1400 if (!pte_write(entry))
1401 return do_wp_page(mm, vma, address, pte, entry);
1403 entry = pte_mkdirty(entry);
1405 entry = pte_mkyoung(entry);
1406 establish_pte(vma, address, pte, entry);
1407 spin_unlock(&mm->page_table_lock);
1408 return 1;
1411 /*
1412 * By the time we get here, we already hold the mm semaphore
1413 */
1414 int handle_mm_fault(struct mm_struct *mm, struct vm_area_struct * vma,
1415 unsigned long address, int write_access)
1417 pgd_t *pgd;
1418 pmd_t *pmd;
1420 current->state = TASK_RUNNING;
1421 pgd = pgd_offset(mm, address);
1423 /*
1424 * We need the page table lock to synchronize with kswapd
1425 * and the SMP-safe atomic PTE updates.
1426 */
1427 spin_lock(&mm->page_table_lock);
1428 pmd = pmd_alloc(mm, pgd, address);
1430 if (pmd) {
1431 pte_t * pte = pte_alloc(mm, pmd, address);
1432 if (pte)
1433 return handle_pte_fault(mm, vma, address, write_access, pte);
1435 spin_unlock(&mm->page_table_lock);
1436 return -1;
1439 /*
1440 * Allocate page middle directory.
1442 * We've already handled the fast-path in-line, and we own the
1443 * page table lock.
1445 * On a two-level page table, this ends up actually being entirely
1446 * optimized away.
1447 */
1448 pmd_t *__pmd_alloc(struct mm_struct *mm, pgd_t *pgd, unsigned long address)
1450 pmd_t *new;
1452 /* "fast" allocation can happen without dropping the lock.. */
1453 new = pmd_alloc_one_fast(mm, address);
1454 if (!new) {
1455 spin_unlock(&mm->page_table_lock);
1456 new = pmd_alloc_one(mm, address);
1457 spin_lock(&mm->page_table_lock);
1458 if (!new)
1459 return NULL;
1461 /*
1462 * Because we dropped the lock, we should re-check the
1463 * entry, as somebody else could have populated it..
1464 */
1465 if (!pgd_none(*pgd)) {
1466 pmd_free(new);
1467 check_pgt_cache();
1468 goto out;
1471 pgd_populate(mm, pgd, new);
1472 out:
1473 return pmd_offset(pgd, address);
1476 /*
1477 * Allocate the page table directory.
1479 * We've already handled the fast-path in-line, and we own the
1480 * page table lock.
1481 */
1482 pte_t *pte_alloc(struct mm_struct *mm, pmd_t *pmd, unsigned long address)
1484 if (pmd_none(*pmd)) {
1485 pte_t *new;
1487 /* "fast" allocation can happen without dropping the lock.. */
1488 new = pte_alloc_one_fast(mm, address);
1489 if (!new) {
1490 XEN_flush_page_update_queue();
1491 spin_unlock(&mm->page_table_lock);
1492 new = pte_alloc_one(mm, address);
1493 spin_lock(&mm->page_table_lock);
1494 if (!new)
1495 return NULL;
1497 /*
1498 * Because we dropped the lock, we should re-check the
1499 * entry, as somebody else could have populated it..
1500 */
1501 if (!pmd_none(*pmd)) {
1502 pte_free(new);
1503 check_pgt_cache();
1504 goto out;
1507 pmd_populate(mm, pmd, new);
1509 out:
1510 return pte_offset(pmd, address);
1513 int make_pages_present(unsigned long addr, unsigned long end)
1515 int ret, len, write;
1516 struct vm_area_struct * vma;
1518 vma = find_vma(current->mm, addr);
1519 write = (vma->vm_flags & VM_WRITE) != 0;
1520 if (addr >= end)
1521 BUG();
1522 if (end > vma->vm_end)
1523 BUG();
1524 len = (end+PAGE_SIZE-1)/PAGE_SIZE-addr/PAGE_SIZE;
1525 ret = get_user_pages(current, current->mm, addr,
1526 len, write, 0, NULL, NULL);
1527 return ret == len ? 0 : -1;
1530 struct page * vmalloc_to_page(void * vmalloc_addr)
1532 unsigned long addr = (unsigned long) vmalloc_addr;
1533 struct page *page = NULL;
1534 pmd_t *pmd;
1535 pte_t *pte;
1536 pgd_t *pgd;
1538 pgd = pgd_offset_k(addr);
1539 if (!pgd_none(*pgd)) {
1540 pmd = pmd_offset(pgd, addr);
1541 if (!pmd_none(*pmd)) {
1542 pte = pte_offset(pmd, addr);
1543 if (pte_present(*pte)) {
1544 page = pte_page(*pte);
1548 return page;