slice.c 23 KB

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