2 * address space "slices" (meta-segments) support
4 * Copyright (C) 2007 Benjamin Herrenschmidt, IBM Corporation.
6 * Based on hugetlb implementation
8 * Copyright (C) 2003 David Gibson, IBM Corporation.
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.
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.
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
27 #include <linux/kernel.h>
29 #include <linux/pagemap.h>
30 #include <linux/err.h>
31 #include <linux/spinlock.h>
32 #include <linux/export.h>
33 #include <linux/hugetlb.h>
34 #include <linux/security.h>
37 #include <asm/copro.h>
38 #include <asm/hugetlb.h>
40 static DEFINE_SPINLOCK(slice_convert_lock);
42 * One bit per slice. We have lower slices which cover 256MB segments
43 * upto 4G range. That gets us 16 low slices. For the rest we track slices
48 DECLARE_BITMAP(high_slices, SLICE_NUM_HIGH);
54 static void slice_print_mask(const char *label, struct slice_mask mask)
58 pr_devel("%s low_slice: %*pbl\n", label, (int)SLICE_NUM_LOW, &mask.low_slices);
59 pr_devel("%s high_slice: %*pbl\n", label, (int)SLICE_NUM_HIGH, mask.high_slices);
62 #define slice_dbg(fmt...) do { if (_slice_debug) pr_devel(fmt); } while (0)
66 static void slice_print_mask(const char *label, struct slice_mask mask) {}
67 #define slice_dbg(fmt...)
71 static void slice_range_to_mask(unsigned long start, unsigned long len,
72 struct slice_mask *ret)
74 unsigned long end = start + len - 1;
78 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
80 if (start < SLICE_LOW_TOP) {
81 unsigned long mend = min(end,
82 (unsigned long)(SLICE_LOW_TOP - 1));
84 ret->low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
85 - (1u << GET_LOW_SLICE_INDEX(start));
88 if ((start + len) > SLICE_LOW_TOP) {
89 unsigned long start_index = GET_HIGH_SLICE_INDEX(start);
90 unsigned long align_end = ALIGN(end, (1UL << SLICE_HIGH_SHIFT));
91 unsigned long count = GET_HIGH_SLICE_INDEX(align_end) - start_index;
93 bitmap_set(ret->high_slices, start_index, count);
97 static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
100 struct vm_area_struct *vma;
102 if ((mm->context.addr_limit - len) < addr)
104 vma = find_vma(mm, addr);
105 return (!vma || (addr + len) <= vm_start_gap(vma));
108 static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
110 return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
111 1ul << SLICE_LOW_SHIFT);
114 static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
116 unsigned long start = slice << SLICE_HIGH_SHIFT;
117 unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
120 /* Hack, so that each addresses is controlled by exactly one
121 * of the high or low area bitmaps, the first high area starts
124 start = SLICE_LOW_TOP;
127 return !slice_area_is_free(mm, start, end - start);
130 static void slice_mask_for_free(struct mm_struct *mm, struct slice_mask *ret)
136 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
138 for (i = 0; i < SLICE_NUM_LOW; i++)
139 if (!slice_low_has_vma(mm, i))
140 ret->low_slices |= 1u << i;
142 if (mm->context.addr_limit <= SLICE_LOW_TOP)
145 for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++)
146 if (!slice_high_has_vma(mm, i))
147 __set_bit(i, ret->high_slices);
150 static void slice_mask_for_size(struct mm_struct *mm, int psize, struct slice_mask *ret)
152 unsigned char *hpsizes;
153 int index, mask_index;
159 bitmap_zero(ret->high_slices, SLICE_NUM_HIGH);
161 lpsizes = mm->context.low_slices_psize;
162 for (i = 0; i < SLICE_NUM_LOW; i++)
163 if (((lpsizes >> (i * 4)) & 0xf) == psize)
164 ret->low_slices |= 1u << i;
166 hpsizes = mm->context.high_slices_psize;
167 for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++) {
168 mask_index = i & 0x1;
170 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
171 __set_bit(i, ret->high_slices);
175 static int slice_check_fit(struct mm_struct *mm,
176 struct slice_mask mask, struct slice_mask available)
178 DECLARE_BITMAP(result, SLICE_NUM_HIGH);
179 unsigned long slice_count = GET_HIGH_SLICE_INDEX(mm->context.addr_limit);
182 return (mask.low_slices & available.low_slices) ==
185 bitmap_and(result, mask.high_slices,
186 available.high_slices, slice_count);
188 return (mask.low_slices & available.low_slices) == mask.low_slices &&
189 bitmap_equal(result, mask.high_slices, slice_count);
192 static void slice_flush_segments(void *parm)
195 struct mm_struct *mm = parm;
198 if (mm != current->active_mm)
201 copy_mm_to_paca(current->active_mm);
203 local_irq_save(flags);
204 slb_flush_and_rebolt();
205 local_irq_restore(flags);
209 static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
211 int index, mask_index;
212 /* Write the new slice psize bits */
213 unsigned char *hpsizes;
215 unsigned long i, flags;
217 slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
218 slice_print_mask(" mask", mask);
220 /* We need to use a spinlock here to protect against
221 * concurrent 64k -> 4k demotion ...
223 spin_lock_irqsave(&slice_convert_lock, flags);
225 lpsizes = mm->context.low_slices_psize;
226 for (i = 0; i < SLICE_NUM_LOW; i++)
227 if (mask.low_slices & (1u << i))
228 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
229 (((unsigned long)psize) << (i * 4));
231 /* Assign the value back */
232 mm->context.low_slices_psize = lpsizes;
234 hpsizes = mm->context.high_slices_psize;
235 for (i = 0; i < GET_HIGH_SLICE_INDEX(mm->context.addr_limit); i++) {
236 mask_index = i & 0x1;
238 if (test_bit(i, mask.high_slices))
239 hpsizes[index] = (hpsizes[index] &
240 ~(0xf << (mask_index * 4))) |
241 (((unsigned long)psize) << (mask_index * 4));
244 slice_dbg(" lsps=%lx, hsps=%lx\n",
245 (unsigned long)mm->context.low_slices_psize,
246 (unsigned long)mm->context.high_slices_psize);
248 spin_unlock_irqrestore(&slice_convert_lock, flags);
250 copro_flush_all_slbs(mm);
254 * Compute which slice addr is part of;
255 * set *boundary_addr to the start or end boundary of that slice
256 * (depending on 'end' parameter);
257 * return boolean indicating if the slice is marked as available in the
258 * 'available' slice_mark.
260 static bool slice_scan_available(unsigned long addr,
261 struct slice_mask available,
263 unsigned long *boundary_addr)
266 if (addr < SLICE_LOW_TOP) {
267 slice = GET_LOW_SLICE_INDEX(addr);
268 *boundary_addr = (slice + end) << SLICE_LOW_SHIFT;
269 return !!(available.low_slices & (1u << slice));
271 slice = GET_HIGH_SLICE_INDEX(addr);
272 *boundary_addr = (slice + end) ?
273 ((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP;
274 return !!test_bit(slice, available.high_slices);
278 static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
280 struct slice_mask available,
281 int psize, unsigned long high_limit)
283 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
284 unsigned long addr, found, next_end;
285 struct vm_unmapped_area_info info;
289 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
290 info.align_offset = 0;
292 addr = TASK_UNMAPPED_BASE;
294 * Check till the allow max value for this mmap request
296 while (addr < high_limit) {
297 info.low_limit = addr;
298 if (!slice_scan_available(addr, available, 1, &addr))
303 * At this point [info.low_limit; addr) covers
304 * available slices only and ends at a slice boundary.
305 * Check if we need to reduce the range, or if we can
306 * extend it to cover the next available slice.
308 if (addr >= high_limit)
310 else if (slice_scan_available(addr, available, 1, &next_end)) {
314 info.high_limit = addr;
316 found = vm_unmapped_area(&info);
317 if (!(found & ~PAGE_MASK))
324 static unsigned long slice_find_area_topdown(struct mm_struct *mm,
326 struct slice_mask available,
327 int psize, unsigned long high_limit)
329 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
330 unsigned long addr, found, prev;
331 struct vm_unmapped_area_info info;
332 unsigned long min_addr = max(PAGE_SIZE, mmap_min_addr);
334 info.flags = VM_UNMAPPED_AREA_TOPDOWN;
336 info.align_mask = PAGE_MASK & ((1ul << pshift) - 1);
337 info.align_offset = 0;
339 addr = mm->mmap_base;
341 * If we are trying to allocate above DEFAULT_MAP_WINDOW
342 * Add the different to the mmap_base.
343 * Only for that request for which high_limit is above
344 * DEFAULT_MAP_WINDOW we should apply this.
346 if (high_limit > DEFAULT_MAP_WINDOW)
347 addr += mm->context.addr_limit - DEFAULT_MAP_WINDOW;
349 while (addr > min_addr) {
350 info.high_limit = addr;
351 if (!slice_scan_available(addr - 1, available, 0, &addr))
356 * At this point [addr; info.high_limit) covers
357 * available slices only and starts at a slice boundary.
358 * Check if we need to reduce the range, or if we can
359 * extend it to cover the previous available slice.
363 else if (slice_scan_available(addr - 1, available, 0, &prev)) {
367 info.low_limit = addr;
369 found = vm_unmapped_area(&info);
370 if (!(found & ~PAGE_MASK))
375 * A failed mmap() very likely causes application failure,
376 * so fall back to the bottom-up function here. This scenario
377 * can happen with large stack limits and large mmap()
380 return slice_find_area_bottomup(mm, len, available, psize, high_limit);
384 static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
385 struct slice_mask mask, int psize,
386 int topdown, unsigned long high_limit)
389 return slice_find_area_topdown(mm, len, mask, psize, high_limit);
391 return slice_find_area_bottomup(mm, len, mask, psize, high_limit);
394 static inline void slice_or_mask(struct slice_mask *dst, struct slice_mask *src)
396 dst->low_slices |= src->low_slices;
399 bitmap_or(dst->high_slices, dst->high_slices, src->high_slices,
403 static inline void slice_andnot_mask(struct slice_mask *dst, struct slice_mask *src)
405 dst->low_slices &= ~src->low_slices;
409 bitmap_andnot(dst->high_slices, dst->high_slices, src->high_slices,
413 #ifdef CONFIG_PPC_64K_PAGES
414 #define MMU_PAGE_BASE MMU_PAGE_64K
416 #define MMU_PAGE_BASE MMU_PAGE_4K
419 unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
420 unsigned long flags, unsigned int psize,
423 struct slice_mask mask;
424 struct slice_mask good_mask;
425 struct slice_mask potential_mask;
426 struct slice_mask compat_mask;
427 int fixed = (flags & MAP_FIXED);
428 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
429 unsigned long page_size = 1UL << pshift;
430 struct mm_struct *mm = current->mm;
431 unsigned long newaddr;
432 unsigned long high_limit;
434 high_limit = DEFAULT_MAP_WINDOW;
435 if (addr >= high_limit || (fixed && (addr + len > high_limit)))
436 high_limit = TASK_SIZE;
438 if (len > high_limit)
440 if (len & (page_size - 1))
443 if (addr & (page_size - 1))
445 if (addr > high_limit - len)
449 if (high_limit > mm->context.addr_limit) {
450 mm->context.addr_limit = high_limit;
451 on_each_cpu(slice_flush_segments, mm, 1);
455 * init different masks
459 /* silence stupid warning */;
460 potential_mask.low_slices = 0;
462 compat_mask.low_slices = 0;
464 if (SLICE_NUM_HIGH) {
465 bitmap_zero(mask.high_slices, SLICE_NUM_HIGH);
466 bitmap_zero(potential_mask.high_slices, SLICE_NUM_HIGH);
467 bitmap_zero(compat_mask.high_slices, SLICE_NUM_HIGH);
471 BUG_ON(mm->task_size == 0);
472 BUG_ON(mm->context.addr_limit == 0);
473 VM_BUG_ON(radix_enabled());
475 slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
476 slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d\n",
477 addr, len, flags, topdown);
479 /* If hint, make sure it matches our alignment restrictions */
480 if (!fixed && addr) {
481 addr = _ALIGN_UP(addr, page_size);
482 slice_dbg(" aligned addr=%lx\n", addr);
483 /* Ignore hint if it's too large or overlaps a VMA */
484 if (addr > high_limit - len || addr < mmap_min_addr ||
485 !slice_area_is_free(mm, addr, len))
489 /* First make up a "good" mask of slices that have the right size
492 slice_mask_for_size(mm, psize, &good_mask);
493 slice_print_mask(" good_mask", good_mask);
496 * Here "good" means slices that are already the right page size,
497 * "compat" means slices that have a compatible page size (i.e.
498 * 4k in a 64k pagesize kernel), and "free" means slices without
502 * check if fits in good | compat => OK
503 * check if fits in good | compat | free => convert free
506 * check if hint fits in good => OK
507 * check if hint fits in good | free => convert free
509 * search in good, found => OK
510 * search in good | free, found => convert free
511 * search in good | compat | free, found => convert free.
514 #ifdef CONFIG_PPC_64K_PAGES
515 /* If we support combo pages, we can allow 64k pages in 4k slices */
516 if (psize == MMU_PAGE_64K) {
517 slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
519 slice_or_mask(&good_mask, &compat_mask);
523 /* First check hint if it's valid or if we have MAP_FIXED */
524 if (addr != 0 || fixed) {
525 /* Build a mask for the requested range */
526 slice_range_to_mask(addr, len, &mask);
527 slice_print_mask(" mask", mask);
529 /* Check if we fit in the good mask. If we do, we just return,
532 if (slice_check_fit(mm, mask, good_mask)) {
533 slice_dbg(" fits good !\n");
537 /* Now let's see if we can find something in the existing
538 * slices for that size
540 newaddr = slice_find_area(mm, len, good_mask,
541 psize, topdown, high_limit);
542 if (newaddr != -ENOMEM) {
543 /* Found within the good mask, we don't have to setup,
544 * we thus return directly
546 slice_dbg(" found area at 0x%lx\n", newaddr);
551 /* We don't fit in the good mask, check what other slices are
552 * empty and thus can be converted
554 slice_mask_for_free(mm, &potential_mask);
555 slice_or_mask(&potential_mask, &good_mask);
556 slice_print_mask(" potential", potential_mask);
558 if ((addr != 0 || fixed) && slice_check_fit(mm, mask, potential_mask)) {
559 slice_dbg(" fits potential !\n");
563 /* If we have MAP_FIXED and failed the above steps, then error out */
567 slice_dbg(" search...\n");
569 /* If we had a hint that didn't work out, see if we can fit
570 * anywhere in the good area.
573 addr = slice_find_area(mm, len, good_mask,
574 psize, topdown, high_limit);
575 if (addr != -ENOMEM) {
576 slice_dbg(" found area at 0x%lx\n", addr);
581 /* Now let's see if we can find something in the existing slices
582 * for that size plus free slices
584 addr = slice_find_area(mm, len, potential_mask,
585 psize, topdown, high_limit);
587 #ifdef CONFIG_PPC_64K_PAGES
588 if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
589 /* retry the search with 4k-page slices included */
590 slice_or_mask(&potential_mask, &compat_mask);
591 addr = slice_find_area(mm, len, potential_mask,
592 psize, topdown, high_limit);
599 slice_range_to_mask(addr, len, &mask);
600 slice_dbg(" found potential area at 0x%lx\n", addr);
601 slice_print_mask(" mask", mask);
604 slice_andnot_mask(&mask, &good_mask);
605 slice_andnot_mask(&mask, &compat_mask);
606 if (mask.low_slices ||
608 !bitmap_empty(mask.high_slices, SLICE_NUM_HIGH))) {
609 slice_convert(mm, mask, psize);
610 if (psize > MMU_PAGE_BASE)
611 on_each_cpu(slice_flush_segments, mm, 1);
616 EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
618 unsigned long arch_get_unmapped_area(struct file *filp,
624 return slice_get_unmapped_area(addr, len, flags,
625 current->mm->context.user_psize, 0);
628 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
629 const unsigned long addr0,
630 const unsigned long len,
631 const unsigned long pgoff,
632 const unsigned long flags)
634 return slice_get_unmapped_area(addr0, len, flags,
635 current->mm->context.user_psize, 1);
638 unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
640 unsigned char *hpsizes;
641 int index, mask_index;
644 * Radix doesn't use slice, but can get enabled along with MMU_SLICE
646 if (radix_enabled()) {
647 #ifdef CONFIG_PPC_64K_PAGES
653 if (addr < SLICE_LOW_TOP) {
655 lpsizes = mm->context.low_slices_psize;
656 index = GET_LOW_SLICE_INDEX(addr);
657 return (lpsizes >> (index * 4)) & 0xf;
659 hpsizes = mm->context.high_slices_psize;
660 index = GET_HIGH_SLICE_INDEX(addr);
661 mask_index = index & 0x1;
662 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
664 EXPORT_SYMBOL_GPL(get_slice_psize);
667 * This is called by hash_page when it needs to do a lazy conversion of
668 * an address space from real 64K pages to combo 4K pages (typically
669 * when hitting a non cacheable mapping on a processor or hypervisor
670 * that won't allow them for 64K pages).
672 * This is also called in init_new_context() to change back the user
673 * psize from whatever the parent context had it set to
674 * N.B. This may be called before mm->context.id has been set.
676 * This function will only change the content of the {low,high)_slice_psize
677 * masks, it will not flush SLBs as this shall be handled lazily by the
680 void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
682 int index, mask_index;
683 unsigned char *hpsizes;
684 unsigned long flags, lpsizes;
685 unsigned int old_psize;
688 slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
690 VM_BUG_ON(radix_enabled());
691 spin_lock_irqsave(&slice_convert_lock, flags);
693 old_psize = mm->context.user_psize;
694 slice_dbg(" old_psize=%d\n", old_psize);
695 if (old_psize == psize)
698 mm->context.user_psize = psize;
701 lpsizes = mm->context.low_slices_psize;
702 for (i = 0; i < SLICE_NUM_LOW; i++)
703 if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
704 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
705 (((unsigned long)psize) << (i * 4));
706 /* Assign the value back */
707 mm->context.low_slices_psize = lpsizes;
709 hpsizes = mm->context.high_slices_psize;
710 for (i = 0; i < SLICE_NUM_HIGH; i++) {
711 mask_index = i & 0x1;
713 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
714 hpsizes[index] = (hpsizes[index] &
715 ~(0xf << (mask_index * 4))) |
716 (((unsigned long)psize) << (mask_index * 4));
722 slice_dbg(" lsps=%lx, hsps=%lx\n",
723 (unsigned long)mm->context.low_slices_psize,
724 (unsigned long)mm->context.high_slices_psize);
727 spin_unlock_irqrestore(&slice_convert_lock, flags);
730 void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
731 unsigned long len, unsigned int psize)
733 struct slice_mask mask;
735 VM_BUG_ON(radix_enabled());
737 slice_range_to_mask(start, len, &mask);
738 slice_convert(mm, mask, psize);
741 #ifdef CONFIG_HUGETLB_PAGE
743 * is_hugepage_only_range() is used by generic code to verify whether
744 * a normal mmap mapping (non hugetlbfs) is valid on a given area.
746 * until the generic code provides a more generic hook and/or starts
747 * calling arch get_unmapped_area for MAP_FIXED (which our implementation
748 * here knows how to deal with), we hijack it to keep standard mappings
751 * because of that generic code limitation, MAP_FIXED mapping cannot
752 * "convert" back a slice with no VMAs to the standard page size, only
753 * get_unmapped_area() can. It would be possible to fix it here but I
754 * prefer working on fixing the generic code instead.
756 * WARNING: This will not work if hugetlbfs isn't enabled since the
757 * generic code will redefine that function as 0 in that. This is ok
758 * for now as we only use slices with hugetlbfs enabled. This should
759 * be fixed as the generic code gets fixed.
761 int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
764 struct slice_mask mask, available;
765 unsigned int psize = mm->context.user_psize;
770 slice_range_to_mask(addr, len, &mask);
771 slice_mask_for_size(mm, psize, &available);
772 #ifdef CONFIG_PPC_64K_PAGES
773 /* We need to account for 4k slices too */
774 if (psize == MMU_PAGE_64K) {
775 struct slice_mask compat_mask;
776 slice_mask_for_size(mm, MMU_PAGE_4K, &compat_mask);
777 slice_or_mask(&available, &compat_mask);
781 #if 0 /* too verbose */
782 slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
784 slice_print_mask(" mask", mask);
785 slice_print_mask(" available", available);
787 return !slice_check_fit(mm, mask, available);