slice.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * address space "slices" (meta-segments) support
  3. *
  4. * Copyright (C) 2007 Benjamin Herrenschmidt, IBM Corporation.
  5. *
  6. * Based on hugetlb implementation
  7. *
  8. * Copyright (C) 2003 David Gibson, IBM Corporation.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #undef DEBUG
  25. #include <linux/kernel.h>
  26. #include <linux/mm.h>
  27. #include <linux/pagemap.h>
  28. #include <linux/err.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/export.h>
  31. #include <linux/hugetlb.h>
  32. #include <asm/mman.h>
  33. #include <asm/mmu.h>
  34. #include <asm/copro.h>
  35. #include <asm/hugetlb.h>
  36. /* some sanity checks */
  37. #if (H_PGTABLE_RANGE >> 43) > SLICE_MASK_SIZE
  38. #error H_PGTABLE_RANGE exceeds slice_mask high_slices size
  39. #endif
  40. static DEFINE_SPINLOCK(slice_convert_lock);
  41. #ifdef DEBUG
  42. int _slice_debug = 1;
  43. static void slice_print_mask(const char *label, struct slice_mask mask)
  44. {
  45. char *p, buf[16 + 3 + 64 + 1];
  46. int i;
  47. if (!_slice_debug)
  48. return;
  49. p = buf;
  50. for (i = 0; i < SLICE_NUM_LOW; i++)
  51. *(p++) = (mask.low_slices & (1 << i)) ? '1' : '0';
  52. *(p++) = ' ';
  53. *(p++) = '-';
  54. *(p++) = ' ';
  55. for (i = 0; i < SLICE_NUM_HIGH; i++)
  56. *(p++) = (mask.high_slices & (1ul << i)) ? '1' : '0';
  57. *(p++) = 0;
  58. printk(KERN_DEBUG "%s:%s\n", label, buf);
  59. }
  60. #define slice_dbg(fmt...) do { if (_slice_debug) pr_debug(fmt); } while(0)
  61. #else
  62. static void slice_print_mask(const char *label, struct slice_mask mask) {}
  63. #define slice_dbg(fmt...)
  64. #endif
  65. static struct slice_mask slice_range_to_mask(unsigned long start,
  66. unsigned long len)
  67. {
  68. unsigned long end = start + len - 1;
  69. struct slice_mask ret = { 0, 0 };
  70. if (start < SLICE_LOW_TOP) {
  71. unsigned long mend = min(end, SLICE_LOW_TOP);
  72. unsigned long mstart = min(start, SLICE_LOW_TOP);
  73. ret.low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
  74. - (1u << GET_LOW_SLICE_INDEX(mstart));
  75. }
  76. if ((start + len) > SLICE_LOW_TOP)
  77. ret.high_slices = (1ul << (GET_HIGH_SLICE_INDEX(end) + 1))
  78. - (1ul << GET_HIGH_SLICE_INDEX(start));
  79. return ret;
  80. }
  81. static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
  82. unsigned long len)
  83. {
  84. struct vm_area_struct *vma;
  85. if ((mm->task_size - len) < addr)
  86. return 0;
  87. vma = find_vma(mm, addr);
  88. return (!vma || (addr + len) <= vm_start_gap(vma));
  89. }
  90. static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
  91. {
  92. return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
  93. 1ul << SLICE_LOW_SHIFT);
  94. }
  95. static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
  96. {
  97. unsigned long start = slice << SLICE_HIGH_SHIFT;
  98. unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
  99. /* Hack, so that each addresses is controlled by exactly one
  100. * of the high or low area bitmaps, the first high area starts
  101. * at 4GB, not 0 */
  102. if (start == 0)
  103. start = SLICE_LOW_TOP;
  104. return !slice_area_is_free(mm, start, end - start);
  105. }
  106. static struct slice_mask slice_mask_for_free(struct mm_struct *mm)
  107. {
  108. struct slice_mask ret = { 0, 0 };
  109. unsigned long i;
  110. for (i = 0; i < SLICE_NUM_LOW; i++)
  111. if (!slice_low_has_vma(mm, i))
  112. ret.low_slices |= 1u << i;
  113. if (mm->task_size <= SLICE_LOW_TOP)
  114. return ret;
  115. for (i = 0; i < SLICE_NUM_HIGH; i++)
  116. if (!slice_high_has_vma(mm, i))
  117. ret.high_slices |= 1ul << i;
  118. return ret;
  119. }
  120. static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize)
  121. {
  122. unsigned char *hpsizes;
  123. int index, mask_index;
  124. struct slice_mask ret = { 0, 0 };
  125. unsigned long i;
  126. u64 lpsizes;
  127. lpsizes = mm->context.low_slices_psize;
  128. for (i = 0; i < SLICE_NUM_LOW; i++)
  129. if (((lpsizes >> (i * 4)) & 0xf) == psize)
  130. ret.low_slices |= 1u << i;
  131. hpsizes = mm->context.high_slices_psize;
  132. for (i = 0; i < SLICE_NUM_HIGH; i++) {
  133. mask_index = i & 0x1;
  134. index = i >> 1;
  135. if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
  136. ret.high_slices |= 1ul << i;
  137. }
  138. return ret;
  139. }
  140. static int slice_check_fit(struct slice_mask mask, struct slice_mask available)
  141. {
  142. return (mask.low_slices & available.low_slices) == mask.low_slices &&
  143. (mask.high_slices & available.high_slices) == mask.high_slices;
  144. }
  145. static void slice_flush_segments(void *parm)
  146. {
  147. struct mm_struct *mm = parm;
  148. unsigned long flags;
  149. if (mm != current->active_mm)
  150. return;
  151. copy_mm_to_paca(&current->active_mm->context);
  152. local_irq_save(flags);
  153. slb_flush_and_rebolt();
  154. local_irq_restore(flags);
  155. }
  156. static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
  157. {
  158. int index, mask_index;
  159. /* Write the new slice psize bits */
  160. unsigned char *hpsizes;
  161. u64 lpsizes;
  162. unsigned long i, flags;
  163. slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
  164. slice_print_mask(" mask", mask);
  165. /* We need to use a spinlock here to protect against
  166. * concurrent 64k -> 4k demotion ...
  167. */
  168. spin_lock_irqsave(&slice_convert_lock, flags);
  169. lpsizes = mm->context.low_slices_psize;
  170. for (i = 0; i < SLICE_NUM_LOW; i++)
  171. if (mask.low_slices & (1u << i))
  172. lpsizes = (lpsizes & ~(0xful << (i * 4))) |
  173. (((unsigned long)psize) << (i * 4));
  174. /* Assign the value back */
  175. mm->context.low_slices_psize = lpsizes;
  176. hpsizes = mm->context.high_slices_psize;
  177. for (i = 0; i < SLICE_NUM_HIGH; i++) {
  178. mask_index = i & 0x1;
  179. index = i >> 1;
  180. if (mask.high_slices & (1ul << i))
  181. hpsizes[index] = (hpsizes[index] &
  182. ~(0xf << (mask_index * 4))) |
  183. (((unsigned long)psize) << (mask_index * 4));
  184. }
  185. slice_dbg(" lsps=%lx, hsps=%lx\n",
  186. mm->context.low_slices_psize,
  187. mm->context.high_slices_psize);
  188. spin_unlock_irqrestore(&slice_convert_lock, flags);
  189. copro_flush_all_slbs(mm);
  190. }
  191. /*
  192. * Compute which slice addr is part of;
  193. * set *boundary_addr to the start or end boundary of that slice
  194. * (depending on 'end' parameter);
  195. * return boolean indicating if the slice is marked as available in the
  196. * 'available' slice_mark.
  197. */
  198. static bool slice_scan_available(unsigned long addr,
  199. struct slice_mask available,
  200. int end,
  201. unsigned long *boundary_addr)
  202. {
  203. unsigned long slice;
  204. if (addr < SLICE_LOW_TOP) {
  205. slice = GET_LOW_SLICE_INDEX(addr);
  206. *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
  207. return !!(available.low_slices & (1u << slice));
  208. } else {
  209. slice = GET_HIGH_SLICE_INDEX(addr);
  210. *boundary_addr = (slice + end) ?
  211. ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
  212. return !!(available.high_slices & (1ul << slice));
  213. }
  214. }
  215. static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
  216. unsigned long len,
  217. struct slice_mask available,
  218. int psize)
  219. {
  220. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  221. unsigned long addr, found, next_end;
  222. struct vm_unmapped_area_info info;
  223. info.flags = 0;
  224. info.length = len;
  225. info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
  226. info.align_offset = 0;
  227. addr = TASK_UNMAPPED_BASE;
  228. while (addr < TASK_SIZE) {
  229. info.low_limit = addr;
  230. if (!slice_scan_available(addr, available, 1, &addr))
  231. continue;
  232. next_slice:
  233. /*
  234. * At this point [info.low_limit; addr) covers
  235. * available slices only and ends at a slice boundary.
  236. * Check if we need to reduce the range, or if we can
  237. * extend it to cover the next available slice.
  238. */
  239. if (addr >= TASK_SIZE)
  240. addr = TASK_SIZE;
  241. else if (slice_scan_available(addr, available, 1, &next_end)) {
  242. addr = next_end;
  243. goto next_slice;
  244. }
  245. info.high_limit = addr;
  246. found = vm_unmapped_area(&info);
  247. if (!(found & ~PAGE_MASK))
  248. return found;
  249. }
  250. return -ENOMEM;
  251. }
  252. static unsigned long slice_find_area_topdown(struct mm_struct *mm,
  253. unsigned long len,
  254. struct slice_mask available,
  255. int psize)
  256. {
  257. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  258. unsigned long addr, found, prev;
  259. struct vm_unmapped_area_info info;
  260. info.flags = VM_UNMAPPED_AREA_TOPDOWN;
  261. info.length = len;
  262. info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
  263. info.align_offset = 0;
  264. addr = mm->mmap_base;
  265. while (addr > PAGE_SIZE) {
  266. info.high_limit = addr;
  267. if (!slice_scan_available(addr - 1, available, 0, &addr))
  268. continue;
  269. prev_slice:
  270. /*
  271. * At this point [addr; info.high_limit) covers
  272. * available slices only and starts at a slice boundary.
  273. * Check if we need to reduce the range, or if we can
  274. * extend it to cover the previous available slice.
  275. */
  276. if (addr < PAGE_SIZE)
  277. addr = PAGE_SIZE;
  278. else if (slice_scan_available(addr - 1, available, 0, &prev)) {
  279. addr = prev;
  280. goto prev_slice;
  281. }
  282. info.low_limit = addr;
  283. found = vm_unmapped_area(&info);
  284. if (!(found & ~PAGE_MASK))
  285. return found;
  286. }
  287. /*
  288. * A failed mmap() very likely causes application failure,
  289. * so fall back to the bottom-up function here. This scenario
  290. * can happen with large stack limits and large mmap()
  291. * allocations.
  292. */
  293. return slice_find_area_bottomup(mm, len, available, psize);
  294. }
  295. static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
  296. struct slice_mask mask, int psize,
  297. int topdown)
  298. {
  299. if (topdown)
  300. return slice_find_area_topdown(mm, len, mask, psize);
  301. else
  302. return slice_find_area_bottomup(mm, len, mask, psize);
  303. }
  304. #define or_mask(dst, src) do { \
  305. (dst).low_slices |= (src).low_slices; \
  306. (dst).high_slices |= (src).high_slices; \
  307. } while (0)
  308. #define andnot_mask(dst, src) do { \
  309. (dst).low_slices &= ~(src).low_slices; \
  310. (dst).high_slices &= ~(src).high_slices; \
  311. } while (0)
  312. #ifdef CONFIG_PPC_64K_PAGES
  313. #define MMU_PAGE_BASE MMU_PAGE_64K
  314. #else
  315. #define MMU_PAGE_BASE MMU_PAGE_4K
  316. #endif
  317. unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
  318. unsigned long flags, unsigned int psize,
  319. int topdown)
  320. {
  321. struct slice_mask mask = {0, 0};
  322. struct slice_mask good_mask;
  323. struct slice_mask potential_mask = {0,0} /* silence stupid warning */;
  324. struct slice_mask compat_mask = {0, 0};
  325. int fixed = (flags & MAP_FIXED);
  326. int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
  327. struct mm_struct *mm = current->mm;
  328. unsigned long newaddr;
  329. /* Sanity checks */
  330. BUG_ON(mm->task_size == 0);
  331. VM_BUG_ON(radix_enabled());
  332. slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
  333. slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
  334. addr, len, flags, topdown);
  335. if (len > mm->task_size)
  336. return -ENOMEM;
  337. if (len & ((1ul << pshift) - 1))
  338. return -EINVAL;
  339. if (fixed && (addr & ((1ul << pshift) - 1)))
  340. return -EINVAL;
  341. if (fixed && addr > (mm->task_size - len))
  342. return -ENOMEM;
  343. /* If hint, make sure it matches our alignment restrictions */
  344. if (!fixed && addr) {
  345. addr = _ALIGN_UP(addr, 1ul << pshift);
  346. slice_dbg(" aligned addr=%lx\n", addr);
  347. /* Ignore hint if it's too large or overlaps a VMA */
  348. if (addr > mm->task_size - len ||
  349. !slice_area_is_free(mm, addr, len))
  350. addr = 0;
  351. }
  352. /* First make up a "good" mask of slices that have the right size
  353. * already
  354. */
  355. good_mask = slice_mask_for_size(mm, psize);
  356. slice_print_mask(" good_mask", good_mask);
  357. /*
  358. * Here "good" means slices that are already the right page size,
  359. * "compat" means slices that have a compatible page size (i.e.
  360. * 4k in a 64k pagesize kernel), and "free" means slices without
  361. * any VMAs.
  362. *
  363. * If MAP_FIXED:
  364. * check if fits in good | compat => OK
  365. * check if fits in good | compat | free => convert free
  366. * else bad
  367. * If have hint:
  368. * check if hint fits in good => OK
  369. * check if hint fits in good | free => convert free
  370. * Otherwise:
  371. * search in good, found => OK
  372. * search in good | free, found => convert free
  373. * search in good | compat | free, found => convert free.
  374. */
  375. #ifdef CONFIG_PPC_64K_PAGES
  376. /* If we support combo pages, we can allow 64k pages in 4k slices */
  377. if (psize == MMU_PAGE_64K) {
  378. compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
  379. if (fixed)
  380. or_mask(good_mask, compat_mask);
  381. }
  382. #endif
  383. /* First check hint if it's valid or if we have MAP_FIXED */
  384. if (addr != 0 || fixed) {
  385. /* Build a mask for the requested range */
  386. mask = slice_range_to_mask(addr, len);
  387. slice_print_mask(" mask", mask);
  388. /* Check if we fit in the good mask. If we do, we just return,
  389. * nothing else to do
  390. */
  391. if (slice_check_fit(mask, good_mask)) {
  392. slice_dbg(" fits good !\n");
  393. return addr;
  394. }
  395. } else {
  396. /* Now let's see if we can find something in the existing
  397. * slices for that size
  398. */
  399. newaddr = slice_find_area(mm, len, good_mask, psize, topdown);
  400. if (newaddr != -ENOMEM) {
  401. /* Found within the good mask, we don't have to setup,
  402. * we thus return directly
  403. */
  404. slice_dbg(" found area at 0x%lx\n", newaddr);
  405. return newaddr;
  406. }
  407. }
  408. /* We don't fit in the good mask, check what other slices are
  409. * empty and thus can be converted
  410. */
  411. potential_mask = slice_mask_for_free(mm);
  412. or_mask(potential_mask, good_mask);
  413. slice_print_mask(" potential", potential_mask);
  414. if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) {
  415. slice_dbg(" fits potential !\n");
  416. goto convert;
  417. }
  418. /* If we have MAP_FIXED and failed the above steps, then error out */
  419. if (fixed)
  420. return -EBUSY;
  421. slice_dbg(" search...\n");
  422. /* If we had a hint that didn't work out, see if we can fit
  423. * anywhere in the good area.
  424. */
  425. if (addr) {
  426. addr = slice_find_area(mm, len, good_mask, psize, topdown);
  427. if (addr != -ENOMEM) {
  428. slice_dbg(" found area at 0x%lx\n", addr);
  429. return addr;
  430. }
  431. }
  432. /* Now let's see if we can find something in the existing slices
  433. * for that size plus free slices
  434. */
  435. addr = slice_find_area(mm, len, potential_mask, psize, topdown);
  436. #ifdef CONFIG_PPC_64K_PAGES
  437. if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
  438. /* retry the search with 4k-page slices included */
  439. or_mask(potential_mask, compat_mask);
  440. addr = slice_find_area(mm, len, potential_mask, psize,
  441. topdown);
  442. }
  443. #endif
  444. if (addr == -ENOMEM)
  445. return -ENOMEM;
  446. mask = slice_range_to_mask(addr, len);
  447. slice_dbg(" found potential area at 0x%lx\n", addr);
  448. slice_print_mask(" mask", mask);
  449. convert:
  450. andnot_mask(mask, good_mask);
  451. andnot_mask(mask, compat_mask);
  452. if (mask.low_slices || mask.high_slices) {
  453. slice_convert(mm, mask, psize);
  454. if (psize > MMU_PAGE_BASE)
  455. on_each_cpu(slice_flush_segments, mm, 1);
  456. }
  457. return addr;
  458. }
  459. EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
  460. unsigned long arch_get_unmapped_area(struct file *filp,
  461. unsigned long addr,
  462. unsigned long len,
  463. unsigned long pgoff,
  464. unsigned long flags)
  465. {
  466. return slice_get_unmapped_area(addr, len, flags,
  467. current->mm->context.user_psize, 0);
  468. }
  469. unsigned long arch_get_unmapped_area_topdown(struct file *filp,
  470. const unsigned long addr0,
  471. const unsigned long len,
  472. const unsigned long pgoff,
  473. const unsigned long flags)
  474. {
  475. return slice_get_unmapped_area(addr0, len, flags,
  476. current->mm->context.user_psize, 1);
  477. }
  478. unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
  479. {
  480. unsigned char *hpsizes;
  481. int index, mask_index;
  482. /*
  483. * Radix doesn't use slice, but can get enabled along with MMU_SLICE
  484. */
  485. if (radix_enabled()) {
  486. #ifdef CONFIG_PPC_64K_PAGES
  487. return MMU_PAGE_64K;
  488. #else
  489. return MMU_PAGE_4K;
  490. #endif
  491. }
  492. if (addr < SLICE_LOW_TOP) {
  493. u64 lpsizes;
  494. lpsizes = mm->context.low_slices_psize;
  495. index = GET_LOW_SLICE_INDEX(addr);
  496. return (lpsizes >> (index * 4)) & 0xf;
  497. }
  498. hpsizes = mm->context.high_slices_psize;
  499. index = GET_HIGH_SLICE_INDEX(addr);
  500. mask_index = index & 0x1;
  501. return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
  502. }
  503. EXPORT_SYMBOL_GPL(get_slice_psize);
  504. /*
  505. * This is called by hash_page when it needs to do a lazy conversion of
  506. * an address space from real 64K pages to combo 4K pages (typically
  507. * when hitting a non cacheable mapping on a processor or hypervisor
  508. * that won't allow them for 64K pages).
  509. *
  510. * This is also called in init_new_context() to change back the user
  511. * psize from whatever the parent context had it set to
  512. * N.B. This may be called before mm->context.id has been set.
  513. *
  514. * This function will only change the content of the {low,high)_slice_psize
  515. * masks, it will not flush SLBs as this shall be handled lazily by the
  516. * caller.
  517. */
  518. void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
  519. {
  520. int index, mask_index;
  521. unsigned char *hpsizes;
  522. unsigned long flags, lpsizes;
  523. unsigned int old_psize;
  524. int i;
  525. slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
  526. VM_BUG_ON(radix_enabled());
  527. spin_lock_irqsave(&slice_convert_lock, flags);
  528. old_psize = mm->context.user_psize;
  529. slice_dbg(" old_psize=%d\n", old_psize);
  530. if (old_psize == psize)
  531. goto bail;
  532. mm->context.user_psize = psize;
  533. wmb();
  534. lpsizes = mm->context.low_slices_psize;
  535. for (i = 0; i < SLICE_NUM_LOW; i++)
  536. if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
  537. lpsizes = (lpsizes & ~(0xful << (i * 4))) |
  538. (((unsigned long)psize) << (i * 4));
  539. /* Assign the value back */
  540. mm->context.low_slices_psize = lpsizes;
  541. hpsizes = mm->context.high_slices_psize;
  542. for (i = 0; i < SLICE_NUM_HIGH; i++) {
  543. mask_index = i & 0x1;
  544. index = i >> 1;
  545. if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
  546. hpsizes[index] = (hpsizes[index] &
  547. ~(0xf << (mask_index * 4))) |
  548. (((unsigned long)psize) << (mask_index * 4));
  549. }
  550. slice_dbg(" lsps=%lx, hsps=%lx\n",
  551. mm->context.low_slices_psize,
  552. mm->context.high_slices_psize);
  553. bail:
  554. spin_unlock_irqrestore(&slice_convert_lock, flags);
  555. }
  556. void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
  557. unsigned long len, unsigned int psize)
  558. {
  559. struct slice_mask mask = slice_range_to_mask(start, len);
  560. VM_BUG_ON(radix_enabled());
  561. slice_convert(mm, mask, psize);
  562. }
  563. #ifdef CONFIG_HUGETLB_PAGE
  564. /*
  565. * is_hugepage_only_range() is used by generic code to verify whether
  566. * a normal mmap mapping (non hugetlbfs) is valid on a given area.
  567. *
  568. * until the generic code provides a more generic hook and/or starts
  569. * calling arch get_unmapped_area for MAP_FIXED (which our implementation
  570. * here knows how to deal with), we hijack it to keep standard mappings
  571. * away from us.
  572. *
  573. * because of that generic code limitation, MAP_FIXED mapping cannot
  574. * "convert" back a slice with no VMAs to the standard page size, only
  575. * get_unmapped_area() can. It would be possible to fix it here but I
  576. * prefer working on fixing the generic code instead.
  577. *
  578. * WARNING: This will not work if hugetlbfs isn't enabled since the
  579. * generic code will redefine that function as 0 in that. This is ok
  580. * for now as we only use slices with hugetlbfs enabled. This should
  581. * be fixed as the generic code gets fixed.
  582. */
  583. int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
  584. unsigned long len)
  585. {
  586. struct slice_mask mask, available;
  587. unsigned int psize = mm->context.user_psize;
  588. if (radix_enabled())
  589. return 0;
  590. mask = slice_range_to_mask(addr, len);
  591. available = slice_mask_for_size(mm, psize);
  592. #ifdef CONFIG_PPC_64K_PAGES
  593. /* We need to account for 4k slices too */
  594. if (psize == MMU_PAGE_64K) {
  595. struct slice_mask compat_mask;
  596. compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
  597. or_mask(available, compat_mask);
  598. }
  599. #endif
  600. #if 0 /* too verbose */
  601. slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
  602. mm, addr, len);
  603. slice_print_mask(" mask", mask);
  604. slice_print_mask(" available", available);
  605. #endif
  606. return !slice_check_fit(mask, available);
  607. }
  608. #endif