slice.c 22 KB

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