slice.c 23 KB

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