sparc: gup_pte_range() support THP based tail recounting
[linux-2.6/btrfs-unstable.git] / arch / sparc / mm / gup.c
blobafcebac144fb7b5f5cf2b4318fff8234d64a7cb1
1 /*
2 * Lockless get_user_pages_fast for sparc, cribbed from powerpc
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
6 */
8 #include <linux/sched.h>
9 #include <linux/mm.h>
10 #include <linux/vmstat.h>
11 #include <linux/pagemap.h>
12 #include <linux/rwsem.h>
13 #include <asm/pgtable.h>
15 static inline void get_huge_page_tail(struct page *page)
18 * __split_huge_page_refcount() cannot run
19 * from under us.
21 VM_BUG_ON(page_mapcount(page) < 0);
22 VM_BUG_ON(atomic_read(&page->_count) != 0);
23 atomic_inc(&page->_mapcount);
27 * The performance critical leaf functions are made noinline otherwise gcc
28 * inlines everything into a single function which results in too much
29 * register pressure.
31 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
32 unsigned long end, int write, struct page **pages, int *nr)
34 unsigned long mask, result;
35 pte_t *ptep;
37 if (tlb_type == hypervisor) {
38 result = _PAGE_PRESENT_4V|_PAGE_P_4V;
39 if (write)
40 result |= _PAGE_WRITE_4V;
41 } else {
42 result = _PAGE_PRESENT_4U|_PAGE_P_4U;
43 if (write)
44 result |= _PAGE_WRITE_4U;
46 mask = result | _PAGE_SPECIAL;
48 ptep = pte_offset_kernel(&pmd, addr);
49 do {
50 struct page *page, *head;
51 pte_t pte = *ptep;
53 if ((pte_val(pte) & mask) != result)
54 return 0;
55 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
57 /* The hugepage case is simplified on sparc64 because
58 * we encode the sub-page pfn offsets into the
59 * hugepage PTEs. We could optimize this in the future
60 * use page_cache_add_speculative() for the hugepage case.
62 page = pte_page(pte);
63 head = compound_head(page);
64 if (!page_cache_get_speculative(head))
65 return 0;
66 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
67 put_page(head);
68 return 0;
70 if (head != page)
71 get_huge_page_tail(page);
73 pages[*nr] = page;
74 (*nr)++;
75 } while (ptep++, addr += PAGE_SIZE, addr != end);
77 return 1;
80 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
81 int write, struct page **pages, int *nr)
83 unsigned long next;
84 pmd_t *pmdp;
86 pmdp = pmd_offset(&pud, addr);
87 do {
88 pmd_t pmd = *pmdp;
90 next = pmd_addr_end(addr, end);
91 if (pmd_none(pmd))
92 return 0;
93 if (!gup_pte_range(pmd, addr, next, write, pages, nr))
94 return 0;
95 } while (pmdp++, addr = next, addr != end);
97 return 1;
100 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
101 int write, struct page **pages, int *nr)
103 unsigned long next;
104 pud_t *pudp;
106 pudp = pud_offset(&pgd, addr);
107 do {
108 pud_t pud = *pudp;
110 next = pud_addr_end(addr, end);
111 if (pud_none(pud))
112 return 0;
113 if (!gup_pmd_range(pud, addr, next, write, pages, nr))
114 return 0;
115 } while (pudp++, addr = next, addr != end);
117 return 1;
120 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
121 struct page **pages)
123 struct mm_struct *mm = current->mm;
124 unsigned long addr, len, end;
125 unsigned long next;
126 pgd_t *pgdp;
127 int nr = 0;
129 start &= PAGE_MASK;
130 addr = start;
131 len = (unsigned long) nr_pages << PAGE_SHIFT;
132 end = start + len;
135 * XXX: batch / limit 'nr', to avoid large irq off latency
136 * needs some instrumenting to determine the common sizes used by
137 * important workloads (eg. DB2), and whether limiting the batch size
138 * will decrease performance.
140 * It seems like we're in the clear for the moment. Direct-IO is
141 * the main guy that batches up lots of get_user_pages, and even
142 * they are limited to 64-at-a-time which is not so many.
145 * This doesn't prevent pagetable teardown, but does prevent
146 * the pagetables from being freed on sparc.
148 * So long as we atomically load page table pointers versus teardown,
149 * we can follow the address down to the the page and take a ref on it.
151 local_irq_disable();
153 pgdp = pgd_offset(mm, addr);
154 do {
155 pgd_t pgd = *pgdp;
157 next = pgd_addr_end(addr, end);
158 if (pgd_none(pgd))
159 goto slow;
160 if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
161 goto slow;
162 } while (pgdp++, addr = next, addr != end);
164 local_irq_enable();
166 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
167 return nr;
170 int ret;
172 slow:
173 local_irq_enable();
175 /* Try to get the remaining pages with get_user_pages */
176 start += nr << PAGE_SHIFT;
177 pages += nr;
179 down_read(&mm->mmap_sem);
180 ret = get_user_pages(current, mm, start,
181 (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
182 up_read(&mm->mmap_sem);
184 /* Have to be a bit careful with return values */
185 if (nr > 0) {
186 if (ret < 0)
187 ret = nr;
188 else
189 ret += nr;
192 return ret;