hash_hugepage.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright IBM Corporation, 2013
  3. * Author Aneesh Kumar K.V <[email protected]>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2.1 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. /*
  15. * PPC64 THP Support for hash based MMUs
  16. */
  17. #include <linux/mm.h>
  18. #include <asm/machdep.h>
  19. int __hash_page_thp(unsigned long ea, unsigned long access, unsigned long vsid,
  20. pmd_t *pmdp, unsigned long trap, unsigned long flags,
  21. int ssize, unsigned int psize)
  22. {
  23. unsigned int index, valid;
  24. unsigned char *hpte_slot_array;
  25. unsigned long rflags, pa, hidx;
  26. unsigned long old_pmd, new_pmd;
  27. int ret, lpsize = MMU_PAGE_16M;
  28. unsigned long vpn, hash, shift, slot;
  29. /*
  30. * atomically mark the linux large page PMD busy and dirty
  31. */
  32. do {
  33. pmd_t pmd = READ_ONCE(*pmdp);
  34. old_pmd = pmd_val(pmd);
  35. /* If PMD busy, retry the access */
  36. if (unlikely(old_pmd & H_PAGE_BUSY))
  37. return 0;
  38. /* If PMD permissions don't match, take page fault */
  39. if (unlikely(!check_pte_access(access, old_pmd)))
  40. return 1;
  41. /*
  42. * Try to lock the PTE, add ACCESSED and DIRTY if it was
  43. * a write access
  44. */
  45. new_pmd = old_pmd | H_PAGE_BUSY | _PAGE_ACCESSED;
  46. if (access & _PAGE_WRITE)
  47. new_pmd |= _PAGE_DIRTY;
  48. } while (!pmd_xchg(pmdp, __pmd(old_pmd), __pmd(new_pmd)));
  49. /*
  50. * Make sure this is thp or devmap entry
  51. */
  52. if (!(old_pmd & (H_PAGE_THP_HUGE | _PAGE_DEVMAP)))
  53. return 0;
  54. rflags = htab_convert_pte_flags(new_pmd, flags);
  55. #if 0
  56. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  57. /*
  58. * No CPU has hugepages but lacks no execute, so we
  59. * don't need to worry about that case
  60. */
  61. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  62. }
  63. #endif
  64. /*
  65. * Find the slot index details for this ea, using base page size.
  66. */
  67. shift = mmu_psize_defs[psize].shift;
  68. index = (ea & ~HPAGE_PMD_MASK) >> shift;
  69. BUG_ON(index >= PTE_FRAG_SIZE);
  70. vpn = hpt_vpn(ea, vsid, ssize);
  71. hpte_slot_array = get_hpte_slot_array(pmdp);
  72. if (psize == MMU_PAGE_4K) {
  73. /*
  74. * invalidate the old hpte entry if we have that mapped via 64K
  75. * base page size. This is because demote_segment won't flush
  76. * hash page table entries.
  77. */
  78. if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) {
  79. flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
  80. ssize, flags);
  81. /*
  82. * With THP, we also clear the slot information with
  83. * respect to all the 64K hash pte mapping the 16MB
  84. * page. They are all invalid now. This make sure we
  85. * don't find the slot valid when we fault with 4k
  86. * base page size.
  87. *
  88. */
  89. memset(hpte_slot_array, 0, PTE_FRAG_SIZE);
  90. }
  91. }
  92. valid = hpte_valid(hpte_slot_array, index);
  93. if (valid) {
  94. /* update the hpte bits */
  95. hash = hpt_hash(vpn, shift, ssize);
  96. hidx = hpte_hash_index(hpte_slot_array, index);
  97. if (hidx & _PTEIDX_SECONDARY)
  98. hash = ~hash;
  99. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  100. slot += hidx & _PTEIDX_GROUP_IX;
  101. ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
  102. psize, lpsize, ssize, flags);
  103. /*
  104. * We failed to update, try to insert a new entry.
  105. */
  106. if (ret == -1) {
  107. /*
  108. * large pte is marked busy, so we can be sure
  109. * nobody is looking at hpte_slot_array. hence we can
  110. * safely update this here.
  111. */
  112. valid = 0;
  113. hpte_slot_array[index] = 0;
  114. }
  115. }
  116. if (!valid) {
  117. unsigned long hpte_group;
  118. hash = hpt_hash(vpn, shift, ssize);
  119. /* insert new entry */
  120. pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
  121. new_pmd |= H_PAGE_HASHPTE;
  122. repeat:
  123. hpte_group = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  124. /* Insert into the hash table, primary slot */
  125. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  126. psize, lpsize, ssize);
  127. /*
  128. * Primary is full, try the secondary
  129. */
  130. if (unlikely(slot == -1)) {
  131. hpte_group = (~hash & htab_hash_mask) * HPTES_PER_GROUP;
  132. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  133. rflags,
  134. HPTE_V_SECONDARY,
  135. psize, lpsize, ssize);
  136. if (slot == -1) {
  137. if (mftb() & 0x1)
  138. hpte_group = (hash & htab_hash_mask) *
  139. HPTES_PER_GROUP;
  140. mmu_hash_ops.hpte_remove(hpte_group);
  141. goto repeat;
  142. }
  143. }
  144. /*
  145. * Hypervisor failure. Restore old pmd and return -1
  146. * similar to __hash_page_*
  147. */
  148. if (unlikely(slot == -2)) {
  149. *pmdp = __pmd(old_pmd);
  150. hash_failure_debug(ea, access, vsid, trap, ssize,
  151. psize, lpsize, old_pmd);
  152. return -1;
  153. }
  154. /*
  155. * large pte is marked busy, so we can be sure
  156. * nobody is looking at hpte_slot_array. hence we can
  157. * safely update this here.
  158. */
  159. mark_hpte_slot_valid(hpte_slot_array, index, slot);
  160. }
  161. /*
  162. * Mark the pte with H_PAGE_COMBO, if we are trying to hash it with
  163. * base page size 4k.
  164. */
  165. if (psize == MMU_PAGE_4K)
  166. new_pmd |= H_PAGE_COMBO;
  167. /*
  168. * The hpte valid is stored in the pgtable whose address is in the
  169. * second half of the PMD. Order this against clearing of the busy bit in
  170. * huge pmd.
  171. */
  172. smp_wmb();
  173. *pmdp = __pmd(new_pmd & ~H_PAGE_BUSY);
  174. return 0;
  175. }