gup.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Lockless get_user_pages_fast for powerpc
  3. *
  4. * Copyright (C) 2008 Nick Piggin
  5. * Copyright (C) 2008 Novell Inc.
  6. */
  7. #undef DEBUG
  8. #include <linux/sched.h>
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. #include <linux/vmstat.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/rwsem.h>
  14. #include <asm/pgtable.h>
  15. #ifdef __HAVE_ARCH_PTE_SPECIAL
  16. /*
  17. * The performance critical leaf functions are made noinline otherwise gcc
  18. * inlines everything into a single function which results in too much
  19. * register pressure.
  20. */
  21. static noinline int gup_pte_range(pmd_t pmd, unsigned long addr,
  22. unsigned long end, int write, struct page **pages, int *nr)
  23. {
  24. unsigned long mask, result;
  25. pte_t *ptep;
  26. result = _PAGE_PRESENT|_PAGE_USER;
  27. if (write)
  28. result |= _PAGE_RW;
  29. mask = result | _PAGE_SPECIAL;
  30. ptep = pte_offset_kernel(&pmd, addr);
  31. do {
  32. pte_t pte = *ptep;
  33. struct page *page;
  34. if ((pte_val(pte) & mask) != result)
  35. return 0;
  36. VM_BUG_ON(!pfn_valid(pte_pfn(pte)));
  37. page = pte_page(pte);
  38. if (!page_cache_get_speculative(page))
  39. return 0;
  40. if (unlikely(pte_val(pte) != pte_val(*ptep))) {
  41. put_page(page);
  42. return 0;
  43. }
  44. pages[*nr] = page;
  45. (*nr)++;
  46. } while (ptep++, addr += PAGE_SIZE, addr != end);
  47. return 1;
  48. }
  49. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  50. int write, struct page **pages, int *nr)
  51. {
  52. unsigned long next;
  53. pmd_t *pmdp;
  54. pmdp = pmd_offset(&pud, addr);
  55. do {
  56. pmd_t pmd = *pmdp;
  57. next = pmd_addr_end(addr, end);
  58. if (pmd_none(pmd))
  59. return 0;
  60. if (is_hugepd(pmdp)) {
  61. if (!gup_hugepd((hugepd_t *)pmdp, PMD_SHIFT,
  62. addr, next, write, pages, nr))
  63. return 0;
  64. } else if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  65. return 0;
  66. } while (pmdp++, addr = next, addr != end);
  67. return 1;
  68. }
  69. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  70. int write, struct page **pages, int *nr)
  71. {
  72. unsigned long next;
  73. pud_t *pudp;
  74. pudp = pud_offset(&pgd, addr);
  75. do {
  76. pud_t pud = *pudp;
  77. next = pud_addr_end(addr, end);
  78. if (pud_none(pud))
  79. return 0;
  80. if (is_hugepd(pudp)) {
  81. if (!gup_hugepd((hugepd_t *)pudp, PUD_SHIFT,
  82. addr, next, write, pages, nr))
  83. return 0;
  84. } else if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  85. return 0;
  86. } while (pudp++, addr = next, addr != end);
  87. return 1;
  88. }
  89. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  90. struct page **pages)
  91. {
  92. struct mm_struct *mm = current->mm;
  93. unsigned long addr, len, end;
  94. unsigned long next;
  95. pgd_t *pgdp;
  96. int nr = 0;
  97. pr_devel("%s(%lx,%x,%s)\n", __func__, start, nr_pages, write ? "write" : "read");
  98. start &= PAGE_MASK;
  99. addr = start;
  100. len = (unsigned long) nr_pages << PAGE_SHIFT;
  101. end = start + len;
  102. if (unlikely(!access_ok(write ? VERIFY_WRITE : VERIFY_READ,
  103. start, len)))
  104. goto slow_irqon;
  105. pr_devel(" aligned: %lx .. %lx\n", start, end);
  106. /*
  107. * XXX: batch / limit 'nr', to avoid large irq off latency
  108. * needs some instrumenting to determine the common sizes used by
  109. * important workloads (eg. DB2), and whether limiting the batch size
  110. * will decrease performance.
  111. *
  112. * It seems like we're in the clear for the moment. Direct-IO is
  113. * the main guy that batches up lots of get_user_pages, and even
  114. * they are limited to 64-at-a-time which is not so many.
  115. */
  116. /*
  117. * This doesn't prevent pagetable teardown, but does prevent
  118. * the pagetables from being freed on powerpc.
  119. *
  120. * So long as we atomically load page table pointers versus teardown,
  121. * we can follow the address down to the the page and take a ref on it.
  122. */
  123. local_irq_disable();
  124. pgdp = pgd_offset(mm, addr);
  125. do {
  126. pgd_t pgd = *pgdp;
  127. pr_devel(" %016lx: normal pgd %p\n", addr,
  128. (void *)pgd_val(pgd));
  129. next = pgd_addr_end(addr, end);
  130. if (pgd_none(pgd))
  131. goto slow;
  132. if (is_hugepd(pgdp)) {
  133. if (!gup_hugepd((hugepd_t *)pgdp, PGDIR_SHIFT,
  134. addr, next, write, pages, &nr))
  135. goto slow;
  136. } else if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  137. goto slow;
  138. } while (pgdp++, addr = next, addr != end);
  139. local_irq_enable();
  140. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  141. return nr;
  142. {
  143. int ret;
  144. slow:
  145. local_irq_enable();
  146. slow_irqon:
  147. pr_devel(" slow path ! nr = %d\n", nr);
  148. /* Try to get the remaining pages with get_user_pages */
  149. start += nr << PAGE_SHIFT;
  150. pages += nr;
  151. down_read(&mm->mmap_sem);
  152. ret = get_user_pages(current, mm, start,
  153. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  154. up_read(&mm->mmap_sem);
  155. /* Have to be a bit careful with return values */
  156. if (nr > 0) {
  157. if (ret < 0)
  158. ret = nr;
  159. else
  160. ret += nr;
  161. }
  162. return ret;
  163. }
  164. }
  165. #endif /* __HAVE_ARCH_PTE_SPECIAL */