2 * Lockless get_user_pages_fast for powerpc
4 * Copyright (C) 2008 Nick Piggin
5 * Copyright (C) 2008 Novell Inc.
9 #include <linux/sched.h>
11 #include <linux/hugetlb.h>
12 #include <linux/vmstat.h>
13 #include <linux/pagemap.h>
14 #include <linux/rwsem.h>
15 #include <asm/pgtable.h>
17 #ifdef __HAVE_ARCH_PTE_SPECIAL
19 static inline void get_huge_page_tail(struct page *page)
22 * __split_huge_page_refcount() cannot run
25 VM_BUG_ON(page_mapcount(page) < 0);
26 VM_BUG_ON(atomic_read(&page->_count) != 0);
27 atomic_inc(&page->_mapcount);
31 * The performance critical leaf functions are made noinline otherwise gcc
32 * inlines everything into a single function which results in too much
35 static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
36 unsigned long end, int write, struct page **pages, int *nr)
38 unsigned long mask, result;
41 result = _PAGE_PRESENT|_PAGE_USER;
44 mask = result | _PAGE_SPECIAL;
46 ptep = pte_offset_kernel(&pmd, addr);
51 if ((pte_val(pte) & mask) != result)
53 VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
55 if (!page_cache_get_speculative(page))
57 if (unlikely(pte_val(pte) != pte_val(*ptep))) {
62 get_huge_page_tail(page);
66 } while (ptep++, addr += PAGE_SIZE, addr != end);
71 static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
72 int write, struct page **pages, int *nr)
77 pmdp = pmd_offset(&pud, addr);
81 next = pmd_addr_end(addr, end);
84 if (is_hugepd(pmdp)) {
85 if (!gup_hugepd((hugepd_t *)pmdp, PMD_SHIFT,
86 addr, next, write, pages, nr))
88 } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
90 } while (pmdp++, addr = next, addr != end);
95 static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
96 int write, struct page **pages, int *nr)
101 pudp = pud_offset(&pgd, addr);
105 next = pud_addr_end(addr, end);
108 if (is_hugepd(pudp)) {
109 if (!gup_hugepd((hugepd_t *)pudp, PUD_SHIFT,
110 addr, next, write, pages, nr))
112 } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
114 } while (pudp++, addr = next, addr != end);
119 int get_user_pages_fast(unsigned long start, int nr_pages, int write,
122 struct mm_struct *mm = current->mm;
123 unsigned long addr, len, end;
128 pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
132 len = (unsigned long) nr_pages << PAGE_SHIFT;
135 if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
139 pr_devel(" aligned: %lx .. %lx\n", start, end);
142 * XXX: batch / limit 'nr', to avoid large irq off latency
143 * needs some instrumenting to determine the common sizes used by
144 * important workloads (eg. DB2), and whether limiting the batch size
145 * will decrease performance.
147 * It seems like we're in the clear for the moment. Direct-IO is
148 * the main guy that batches up lots of get_user_pages, and even
149 * they are limited to 64-at-a-time which is not so many.
152 * This doesn't prevent pagetable teardown, but does prevent
153 * the pagetables from being freed on powerpc.
155 * So long as we atomically load page table pointers versus teardown,
156 * we can follow the address down to the the page and take a ref on it.
160 pgdp = pgd_offset(mm, addr);
164 pr_devel(" %016lx: normal pgd %p\n", addr,
165 (void *)pgd_val(pgd));
166 next = pgd_addr_end(addr, end);
169 if (is_hugepd(pgdp)) {
170 if (!gup_hugepd((hugepd_t *)pgdp, PGDIR_SHIFT,
171 addr, next, write, pages, &nr))
173 } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
175 } while (pgdp++, addr = next, addr != end);
179 VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
188 pr_devel(" slow path ! nr = %d\n", nr);
190 /* Try to get the remaining pages with get_user_pages */
191 start += nr << PAGE_SHIFT;
194 down_read(&mm->mmap_sem);
195 ret = get_user_pages(current, mm, start,
196 (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
197 up_read(&mm->mmap_sem);
199 /* Have to be a bit careful with return values */
211 #endif /* __HAVE_ARCH_PTE_SPECIAL */