hugetlbpage.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * IBM System z Huge TLB Page Support for Kernel.
  3. *
  4. * Copyright IBM Corp. 2007,2016
  5. * Author(s): Gerald Schaefer <gerald.schaefer@de.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "hugetlb"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/mm.h>
  10. #include <linux/hugetlb.h>
  11. /*
  12. * If the bit selected by single-bit bitmask "a" is set within "x", move
  13. * it to the position indicated by single-bit bitmask "b".
  14. */
  15. #define move_set_bit(x, a, b) (((x) & (a)) >> ilog2(a) << ilog2(b))
  16. static inline unsigned long __pte_to_rste(pte_t pte)
  17. {
  18. unsigned long rste;
  19. /*
  20. * Convert encoding pte bits pmd / pud bits
  21. * lIR.uswrdy.p dy..R...I...wr
  22. * empty 010.000000.0 -> 00..0...1...00
  23. * prot-none, clean, old 111.000000.1 -> 00..1...1...00
  24. * prot-none, clean, young 111.000001.1 -> 01..1...1...00
  25. * prot-none, dirty, old 111.000010.1 -> 10..1...1...00
  26. * prot-none, dirty, young 111.000011.1 -> 11..1...1...00
  27. * read-only, clean, old 111.000100.1 -> 00..1...1...01
  28. * read-only, clean, young 101.000101.1 -> 01..1...0...01
  29. * read-only, dirty, old 111.000110.1 -> 10..1...1...01
  30. * read-only, dirty, young 101.000111.1 -> 11..1...0...01
  31. * read-write, clean, old 111.001100.1 -> 00..1...1...11
  32. * read-write, clean, young 101.001101.1 -> 01..1...0...11
  33. * read-write, dirty, old 110.001110.1 -> 10..0...1...11
  34. * read-write, dirty, young 100.001111.1 -> 11..0...0...11
  35. * HW-bits: R read-only, I invalid
  36. * SW-bits: p present, y young, d dirty, r read, w write, s special,
  37. * u unused, l large
  38. */
  39. if (pte_present(pte)) {
  40. rste = pte_val(pte) & PAGE_MASK;
  41. rste |= move_set_bit(pte_val(pte), _PAGE_READ,
  42. _SEGMENT_ENTRY_READ);
  43. rste |= move_set_bit(pte_val(pte), _PAGE_WRITE,
  44. _SEGMENT_ENTRY_WRITE);
  45. rste |= move_set_bit(pte_val(pte), _PAGE_INVALID,
  46. _SEGMENT_ENTRY_INVALID);
  47. rste |= move_set_bit(pte_val(pte), _PAGE_PROTECT,
  48. _SEGMENT_ENTRY_PROTECT);
  49. rste |= move_set_bit(pte_val(pte), _PAGE_DIRTY,
  50. _SEGMENT_ENTRY_DIRTY);
  51. rste |= move_set_bit(pte_val(pte), _PAGE_YOUNG,
  52. _SEGMENT_ENTRY_YOUNG);
  53. #ifdef CONFIG_MEM_SOFT_DIRTY
  54. rste |= move_set_bit(pte_val(pte), _PAGE_SOFT_DIRTY,
  55. _SEGMENT_ENTRY_SOFT_DIRTY);
  56. #endif
  57. } else
  58. rste = _SEGMENT_ENTRY_INVALID;
  59. return rste;
  60. }
  61. static inline pte_t __rste_to_pte(unsigned long rste)
  62. {
  63. int present;
  64. pte_t pte;
  65. if ((rste & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  66. present = pud_present(__pud(rste));
  67. else
  68. present = pmd_present(__pmd(rste));
  69. /*
  70. * Convert encoding pmd / pud bits pte bits
  71. * dy..R...I...wr lIR.uswrdy.p
  72. * empty 00..0...1...00 -> 010.000000.0
  73. * prot-none, clean, old 00..1...1...00 -> 111.000000.1
  74. * prot-none, clean, young 01..1...1...00 -> 111.000001.1
  75. * prot-none, dirty, old 10..1...1...00 -> 111.000010.1
  76. * prot-none, dirty, young 11..1...1...00 -> 111.000011.1
  77. * read-only, clean, old 00..1...1...01 -> 111.000100.1
  78. * read-only, clean, young 01..1...0...01 -> 101.000101.1
  79. * read-only, dirty, old 10..1...1...01 -> 111.000110.1
  80. * read-only, dirty, young 11..1...0...01 -> 101.000111.1
  81. * read-write, clean, old 00..1...1...11 -> 111.001100.1
  82. * read-write, clean, young 01..1...0...11 -> 101.001101.1
  83. * read-write, dirty, old 10..0...1...11 -> 110.001110.1
  84. * read-write, dirty, young 11..0...0...11 -> 100.001111.1
  85. * HW-bits: R read-only, I invalid
  86. * SW-bits: p present, y young, d dirty, r read, w write, s special,
  87. * u unused, l large
  88. */
  89. if (present) {
  90. pte_val(pte) = rste & _SEGMENT_ENTRY_ORIGIN_LARGE;
  91. pte_val(pte) |= _PAGE_LARGE | _PAGE_PRESENT;
  92. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_READ,
  93. _PAGE_READ);
  94. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_WRITE,
  95. _PAGE_WRITE);
  96. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_INVALID,
  97. _PAGE_INVALID);
  98. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_PROTECT,
  99. _PAGE_PROTECT);
  100. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_DIRTY,
  101. _PAGE_DIRTY);
  102. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_YOUNG,
  103. _PAGE_YOUNG);
  104. #ifdef CONFIG_MEM_SOFT_DIRTY
  105. pte_val(pte) |= move_set_bit(rste, _SEGMENT_ENTRY_SOFT_DIRTY,
  106. _PAGE_DIRTY);
  107. #endif
  108. } else
  109. pte_val(pte) = _PAGE_INVALID;
  110. return pte;
  111. }
  112. void set_huge_pte_at(struct mm_struct *mm, unsigned long addr,
  113. pte_t *ptep, pte_t pte)
  114. {
  115. unsigned long rste = __pte_to_rste(pte);
  116. /* Set correct table type for 2G hugepages */
  117. if ((pte_val(*ptep) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  118. rste |= _REGION_ENTRY_TYPE_R3 | _REGION3_ENTRY_LARGE;
  119. else
  120. rste |= _SEGMENT_ENTRY_LARGE;
  121. pte_val(*ptep) = rste;
  122. }
  123. pte_t huge_ptep_get(pte_t *ptep)
  124. {
  125. return __rste_to_pte(pte_val(*ptep));
  126. }
  127. pte_t huge_ptep_get_and_clear(struct mm_struct *mm,
  128. unsigned long addr, pte_t *ptep)
  129. {
  130. pte_t pte = huge_ptep_get(ptep);
  131. pmd_t *pmdp = (pmd_t *) ptep;
  132. pud_t *pudp = (pud_t *) ptep;
  133. if ((pte_val(*ptep) & _REGION_ENTRY_TYPE_MASK) == _REGION_ENTRY_TYPE_R3)
  134. pudp_xchg_direct(mm, addr, pudp, __pud(_REGION3_ENTRY_EMPTY));
  135. else
  136. pmdp_xchg_direct(mm, addr, pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
  137. return pte;
  138. }
  139. pte_t *huge_pte_alloc(struct mm_struct *mm,
  140. unsigned long addr, unsigned long sz)
  141. {
  142. pgd_t *pgdp;
  143. pud_t *pudp;
  144. pmd_t *pmdp = NULL;
  145. pgdp = pgd_offset(mm, addr);
  146. pudp = pud_alloc(mm, pgdp, addr);
  147. if (pudp) {
  148. if (sz == PUD_SIZE)
  149. return (pte_t *) pudp;
  150. else if (sz == PMD_SIZE)
  151. pmdp = pmd_alloc(mm, pudp, addr);
  152. }
  153. return (pte_t *) pmdp;
  154. }
  155. pte_t *huge_pte_offset(struct mm_struct *mm, unsigned long addr)
  156. {
  157. pgd_t *pgdp;
  158. pud_t *pudp;
  159. pmd_t *pmdp = NULL;
  160. pgdp = pgd_offset(mm, addr);
  161. if (pgd_present(*pgdp)) {
  162. pudp = pud_offset(pgdp, addr);
  163. if (pud_present(*pudp)) {
  164. if (pud_large(*pudp))
  165. return (pte_t *) pudp;
  166. pmdp = pmd_offset(pudp, addr);
  167. }
  168. }
  169. return (pte_t *) pmdp;
  170. }
  171. int pmd_huge(pmd_t pmd)
  172. {
  173. return pmd_large(pmd);
  174. }
  175. int pud_huge(pud_t pud)
  176. {
  177. return pud_large(pud);
  178. }
  179. struct page *
  180. follow_huge_pud(struct mm_struct *mm, unsigned long address,
  181. pud_t *pud, int flags)
  182. {
  183. if (flags & FOLL_GET)
  184. return NULL;
  185. return pud_page(*pud) + ((address & ~PUD_MASK) >> PAGE_SHIFT);
  186. }
  187. static __init int setup_hugepagesz(char *opt)
  188. {
  189. unsigned long size;
  190. char *string = opt;
  191. size = memparse(opt, &opt);
  192. if (MACHINE_HAS_EDAT1 && size == PMD_SIZE) {
  193. hugetlb_add_hstate(PMD_SHIFT - PAGE_SHIFT);
  194. } else if (MACHINE_HAS_EDAT2 && size == PUD_SIZE) {
  195. hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
  196. } else {
  197. hugetlb_bad_size();
  198. pr_err("hugepagesz= specifies an unsupported page size %s\n",
  199. string);
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. __setup("hugepagesz=", setup_hugepagesz);