hugepage-hash64.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright IBM Corporation, 2013
  3. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  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. rflags = htab_convert_pte_flags(new_pmd);
  50. #if 0
  51. if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE)) {
  52. /*
  53. * No CPU has hugepages but lacks no execute, so we
  54. * don't need to worry about that case
  55. */
  56. rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);
  57. }
  58. #endif
  59. /*
  60. * Find the slot index details for this ea, using base page size.
  61. */
  62. shift = mmu_psize_defs[psize].shift;
  63. index = (ea & ~HPAGE_PMD_MASK) >> shift;
  64. BUG_ON(index >= PTE_FRAG_SIZE);
  65. vpn = hpt_vpn(ea, vsid, ssize);
  66. hpte_slot_array = get_hpte_slot_array(pmdp);
  67. if (psize == MMU_PAGE_4K) {
  68. /*
  69. * invalidate the old hpte entry if we have that mapped via 64K
  70. * base page size. This is because demote_segment won't flush
  71. * hash page table entries.
  72. */
  73. if ((old_pmd & H_PAGE_HASHPTE) && !(old_pmd & H_PAGE_COMBO)) {
  74. flush_hash_hugepage(vsid, ea, pmdp, MMU_PAGE_64K,
  75. ssize, flags);
  76. /*
  77. * With THP, we also clear the slot information with
  78. * respect to all the 64K hash pte mapping the 16MB
  79. * page. They are all invalid now. This make sure we
  80. * don't find the slot valid when we fault with 4k
  81. * base page size.
  82. *
  83. */
  84. memset(hpte_slot_array, 0, PTE_FRAG_SIZE);
  85. }
  86. }
  87. valid = hpte_valid(hpte_slot_array, index);
  88. if (valid) {
  89. /* update the hpte bits */
  90. hash = hpt_hash(vpn, shift, ssize);
  91. hidx = hpte_hash_index(hpte_slot_array, index);
  92. if (hidx & _PTEIDX_SECONDARY)
  93. hash = ~hash;
  94. slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;
  95. slot += hidx & _PTEIDX_GROUP_IX;
  96. ret = mmu_hash_ops.hpte_updatepp(slot, rflags, vpn,
  97. psize, lpsize, ssize, flags);
  98. /*
  99. * We failed to update, try to insert a new entry.
  100. */
  101. if (ret == -1) {
  102. /*
  103. * large pte is marked busy, so we can be sure
  104. * nobody is looking at hpte_slot_array. hence we can
  105. * safely update this here.
  106. */
  107. valid = 0;
  108. hpte_slot_array[index] = 0;
  109. }
  110. }
  111. if (!valid) {
  112. unsigned long hpte_group;
  113. hash = hpt_hash(vpn, shift, ssize);
  114. /* insert new entry */
  115. pa = pmd_pfn(__pmd(old_pmd)) << PAGE_SHIFT;
  116. new_pmd |= H_PAGE_HASHPTE;
  117. repeat:
  118. hpte_group = ((hash & htab_hash_mask) * HPTES_PER_GROUP) & ~0x7UL;
  119. /* Insert into the hash table, primary slot */
  120. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa, rflags, 0,
  121. psize, lpsize, ssize);
  122. /*
  123. * Primary is full, try the secondary
  124. */
  125. if (unlikely(slot == -1)) {
  126. hpte_group = ((~hash & htab_hash_mask) *
  127. HPTES_PER_GROUP) & ~0x7UL;
  128. slot = mmu_hash_ops.hpte_insert(hpte_group, vpn, pa,
  129. rflags,
  130. HPTE_V_SECONDARY,
  131. psize, lpsize, ssize);
  132. if (slot == -1) {
  133. if (mftb() & 0x1)
  134. hpte_group = ((hash & htab_hash_mask) *
  135. HPTES_PER_GROUP) & ~0x7UL;
  136. mmu_hash_ops.hpte_remove(hpte_group);
  137. goto repeat;
  138. }
  139. }
  140. /*
  141. * Hypervisor failure. Restore old pmd and return -1
  142. * similar to __hash_page_*
  143. */
  144. if (unlikely(slot == -2)) {
  145. *pmdp = __pmd(old_pmd);
  146. hash_failure_debug(ea, access, vsid, trap, ssize,
  147. psize, lpsize, old_pmd);
  148. return -1;
  149. }
  150. /*
  151. * large pte is marked busy, so we can be sure
  152. * nobody is looking at hpte_slot_array. hence we can
  153. * safely update this here.
  154. */
  155. mark_hpte_slot_valid(hpte_slot_array, index, slot);
  156. }
  157. /*
  158. * Mark the pte with H_PAGE_COMBO, if we are trying to hash it with
  159. * base page size 4k.
  160. */
  161. if (psize == MMU_PAGE_4K)
  162. new_pmd |= H_PAGE_COMBO;
  163. /*
  164. * The hpte valid is stored in the pgtable whose address is in the
  165. * second half of the PMD. Order this against clearing of the busy bit in
  166. * huge pmd.
  167. */
  168. smp_wmb();
  169. *pmdp = __pmd(new_pmd & ~H_PAGE_BUSY);
  170. return 0;
  171. }