gup.c 4.2 KB

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