gup.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. pages[*nr] = page;
  54. (*nr)++;
  55. } while (ptep++, addr += PAGE_SIZE, addr != end);
  56. return 1;
  57. }
  58. static int gup_pmd_range(pud_t pud, unsigned long addr, unsigned long end,
  59. int write, struct page **pages, int *nr)
  60. {
  61. unsigned long next;
  62. pmd_t *pmdp;
  63. pmdp = pmd_offset(&pud, addr);
  64. do {
  65. pmd_t pmd = *pmdp;
  66. next = pmd_addr_end(addr, end);
  67. if (pmd_none(pmd))
  68. return 0;
  69. if (!gup_pte_range(pmd, addr, next, write, pages, nr))
  70. return 0;
  71. } while (pmdp++, addr = next, addr != end);
  72. return 1;
  73. }
  74. static int gup_pud_range(pgd_t pgd, unsigned long addr, unsigned long end,
  75. int write, struct page **pages, int *nr)
  76. {
  77. unsigned long next;
  78. pud_t *pudp;
  79. pudp = pud_offset(&pgd, addr);
  80. do {
  81. pud_t pud = *pudp;
  82. next = pud_addr_end(addr, end);
  83. if (pud_none(pud))
  84. return 0;
  85. if (!gup_pmd_range(pud, addr, next, write, pages, nr))
  86. return 0;
  87. } while (pudp++, addr = next, addr != end);
  88. return 1;
  89. }
  90. int get_user_pages_fast(unsigned long start, int nr_pages, int write,
  91. struct page **pages)
  92. {
  93. struct mm_struct *mm = current->mm;
  94. unsigned long addr, len, end;
  95. unsigned long next;
  96. pgd_t *pgdp;
  97. int nr = 0;
  98. start &= PAGE_MASK;
  99. addr = start;
  100. len = (unsigned long) nr_pages << PAGE_SHIFT;
  101. end = start + len;
  102. /*
  103. * XXX: batch / limit 'nr', to avoid large irq off latency
  104. * needs some instrumenting to determine the common sizes used by
  105. * important workloads (eg. DB2), and whether limiting the batch size
  106. * will decrease performance.
  107. *
  108. * It seems like we're in the clear for the moment. Direct-IO is
  109. * the main guy that batches up lots of get_user_pages, and even
  110. * they are limited to 64-at-a-time which is not so many.
  111. */
  112. /*
  113. * This doesn't prevent pagetable teardown, but does prevent
  114. * the pagetables from being freed on sparc.
  115. *
  116. * So long as we atomically load page table pointers versus teardown,
  117. * we can follow the address down to the the page and take a ref on it.
  118. */
  119. local_irq_disable();
  120. pgdp = pgd_offset(mm, addr);
  121. do {
  122. pgd_t pgd = *pgdp;
  123. next = pgd_addr_end(addr, end);
  124. if (pgd_none(pgd))
  125. goto slow;
  126. if (!gup_pud_range(pgd, addr, next, write, pages, &nr))
  127. goto slow;
  128. } while (pgdp++, addr = next, addr != end);
  129. local_irq_enable();
  130. VM_BUG_ON(nr != (end - start) >> PAGE_SHIFT);
  131. return nr;
  132. {
  133. int ret;
  134. slow:
  135. local_irq_enable();
  136. /* Try to get the remaining pages with get_user_pages */
  137. start += nr << PAGE_SHIFT;
  138. pages += nr;
  139. down_read(&mm->mmap_sem);
  140. ret = get_user_pages(current, mm, start,
  141. (end - start) >> PAGE_SHIFT, write, 0, pages, NULL);
  142. up_read(&mm->mmap_sem);
  143. /* Have to be a bit careful with return values */
  144. if (nr > 0) {
  145. if (ret < 0)
  146. ret = nr;
  147. else
  148. ret += nr;
  149. }
  150. return ret;
  151. }
  152. }