2 * Lockless get_user_pages_fast for sparc, cribbed from powerpc
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
8 #include <linux/sched.h>
10 #include <linux/vmstat.h>
11 #include <linux/pagemap.h>
12 #include <linux/rwsem.h>
13 #include <asm/pgtable.h>
16 * The performance critical leaf functions are made noinline otherwise gcc
17 * inlines everything into a single function which results in too much
20 static noinline
int gup_pte_range(pmd_t pmd
, unsigned long addr
,
21 unsigned long end
, int write
, struct page
**pages
, int *nr
)
23 unsigned long mask
, result
;
26 if (tlb_type
== hypervisor
) {
27 result
= _PAGE_PRESENT_4V
|_PAGE_P_4V
;
29 result
|= _PAGE_WRITE_4V
;
31 result
= _PAGE_PRESENT_4U
|_PAGE_P_4U
;
33 result
|= _PAGE_WRITE_4U
;
35 mask
= result
| _PAGE_SPECIAL
;
37 ptep
= pte_offset_kernel(&pmd
, addr
);
39 struct page
*page
, *head
;
42 if ((pte_val(pte
) & mask
) != result
)
44 VM_BUG_ON(!pfn_valid(pte_pfn(pte
)));
46 /* The hugepage case is simplified on sparc64 because
47 * we encode the sub-page pfn offsets into the
48 * hugepage PTEs. We could optimize this in the future
49 * use page_cache_add_speculative() for the hugepage case.
52 head
= compound_head(page
);
53 if (!page_cache_get_speculative(head
))
55 if (unlikely(pte_val(pte
) != pte_val(*ptep
))) {
62 } while (ptep
++, addr
+= PAGE_SIZE
, addr
!= end
);
67 static int gup_pmd_range(pud_t pud
, unsigned long addr
, unsigned long end
,
68 int write
, struct page
**pages
, int *nr
)
73 pmdp
= pmd_offset(&pud
, addr
);
77 next
= pmd_addr_end(addr
, end
);
80 if (!gup_pte_range(pmd
, addr
, next
, write
, pages
, nr
))
82 } while (pmdp
++, addr
= next
, addr
!= end
);
87 static int gup_pud_range(pgd_t pgd
, unsigned long addr
, unsigned long end
,
88 int write
, struct page
**pages
, int *nr
)
93 pudp
= pud_offset(&pgd
, addr
);
97 next
= pud_addr_end(addr
, end
);
100 if (!gup_pmd_range(pud
, addr
, next
, write
, pages
, nr
))
102 } while (pudp
++, addr
= next
, addr
!= end
);
107 int get_user_pages_fast(unsigned long start
, int nr_pages
, int write
,
110 struct mm_struct
*mm
= current
->mm
;
111 unsigned long addr
, len
, end
;
118 len
= (unsigned long) nr_pages
<< PAGE_SHIFT
;
122 * XXX: batch / limit 'nr', to avoid large irq off latency
123 * needs some instrumenting to determine the common sizes used by
124 * important workloads (eg. DB2), and whether limiting the batch size
125 * will decrease performance.
127 * It seems like we're in the clear for the moment. Direct-IO is
128 * the main guy that batches up lots of get_user_pages, and even
129 * they are limited to 64-at-a-time which is not so many.
132 * This doesn't prevent pagetable teardown, but does prevent
133 * the pagetables from being freed on sparc.
135 * So long as we atomically load page table pointers versus teardown,
136 * we can follow the address down to the the page and take a ref on it.
140 pgdp
= pgd_offset(mm
, addr
);
144 next
= pgd_addr_end(addr
, end
);
147 if (!gup_pud_range(pgd
, addr
, next
, write
, pages
, &nr
))
149 } while (pgdp
++, addr
= next
, addr
!= end
);
153 VM_BUG_ON(nr
!= (end
- start
) >> PAGE_SHIFT
);
162 /* Try to get the remaining pages with get_user_pages */
163 start
+= nr
<< PAGE_SHIFT
;
166 down_read(&mm
->mmap_sem
);
167 ret
= get_user_pages(current
, mm
, start
,
168 (end
- start
) >> PAGE_SHIFT
, write
, 0, pages
, NULL
);
169 up_read(&mm
->mmap_sem
);
171 /* Have to be a bit careful with return values */