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>
37 static DEFINE_SPINLOCK(slice_convert_lock);
43 static void slice_print_mask(const char *label, struct slice_mask mask)
45 char *p, buf[16 + 3 + 64 + 1];
51 for (i = 0; i < SLICE_NUM_LOW; i++)
52 *(p++) = (mask.low_slices & (1 << i)) ? '1' : '0';
56 for (i = 0; i < SLICE_NUM_HIGH; i++)
57 *(p++) = (mask.high_slices & (1ul << i)) ? '1' : '0';
60 printk(KERN_DEBUG "%s:%s\n", label, buf);
63 #define slice_dbg(fmt...) do { if (_slice_debug) pr_debug(fmt); } while(0)
67 static void slice_print_mask(const char *label, struct slice_mask mask) {}
68 #define slice_dbg(fmt...)
72 static struct slice_mask slice_range_to_mask(unsigned long start,
75 unsigned long end = start + len - 1;
76 struct slice_mask ret = { 0, 0 };
78 if (start < SLICE_LOW_TOP) {
79 unsigned long mend = min(end, SLICE_LOW_TOP);
80 unsigned long mstart = min(start, SLICE_LOW_TOP);
82 ret.low_slices = (1u << (GET_LOW_SLICE_INDEX(mend) + 1))
83 - (1u << GET_LOW_SLICE_INDEX(mstart));
86 if ((start + len) > SLICE_LOW_TOP)
87 ret.high_slices = (1ul << (GET_HIGH_SLICE_INDEX(end) + 1))
88 - (1ul << GET_HIGH_SLICE_INDEX(start));
93 static int slice_area_is_free(struct mm_struct *mm, unsigned long addr,
96 struct vm_area_struct *vma;
98 if ((mm->task_size - len) < addr)
100 vma = find_vma(mm, addr);
101 return (!vma || (addr + len) <= vma->vm_start);
104 static int slice_low_has_vma(struct mm_struct *mm, unsigned long slice)
106 return !slice_area_is_free(mm, slice << SLICE_LOW_SHIFT,
107 1ul << SLICE_LOW_SHIFT);
110 static int slice_high_has_vma(struct mm_struct *mm, unsigned long slice)
112 unsigned long start = slice << SLICE_HIGH_SHIFT;
113 unsigned long end = start + (1ul << SLICE_HIGH_SHIFT);
115 /* Hack, so that each addresses is controlled by exactly one
116 * of the high or low area bitmaps, the first high area starts
119 start = SLICE_LOW_TOP;
121 return !slice_area_is_free(mm, start, end - start);
124 static struct slice_mask slice_mask_for_free(struct mm_struct *mm)
126 struct slice_mask ret = { 0, 0 };
129 for (i = 0; i < SLICE_NUM_LOW; i++)
130 if (!slice_low_has_vma(mm, i))
131 ret.low_slices |= 1u << i;
133 if (mm->task_size <= SLICE_LOW_TOP)
136 for (i = 0; i < SLICE_NUM_HIGH; i++)
137 if (!slice_high_has_vma(mm, i))
138 ret.high_slices |= 1ul << i;
143 static struct slice_mask slice_mask_for_size(struct mm_struct *mm, int psize)
145 unsigned char *hpsizes;
146 int index, mask_index;
147 struct slice_mask ret = { 0, 0 };
151 lpsizes = mm->context.low_slices_psize;
152 for (i = 0; i < SLICE_NUM_LOW; i++)
153 if (((lpsizes >> (i * 4)) & 0xf) == psize)
154 ret.low_slices |= 1u << i;
156 hpsizes = mm->context.high_slices_psize;
157 for (i = 0; i < SLICE_NUM_HIGH; i++) {
158 mask_index = i & 0x1;
160 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == psize)
161 ret.high_slices |= 1ul << i;
167 static int slice_check_fit(struct slice_mask mask, struct slice_mask available)
169 return (mask.low_slices & available.low_slices) == mask.low_slices &&
170 (mask.high_slices & available.high_slices) == mask.high_slices;
173 static void slice_flush_segments(void *parm)
175 struct mm_struct *mm = parm;
178 if (mm != current->active_mm)
181 /* update the paca copy of the context struct */
182 get_paca()->context = current->active_mm->context;
184 local_irq_save(flags);
185 slb_flush_and_rebolt();
186 local_irq_restore(flags);
189 static void slice_convert(struct mm_struct *mm, struct slice_mask mask, int psize)
191 int index, mask_index;
192 /* Write the new slice psize bits */
193 unsigned char *hpsizes;
195 unsigned long i, flags;
197 slice_dbg("slice_convert(mm=%p, psize=%d)\n", mm, psize);
198 slice_print_mask(" mask", mask);
200 /* We need to use a spinlock here to protect against
201 * concurrent 64k -> 4k demotion ...
203 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 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
209 (((unsigned long)psize) << (i * 4));
211 /* Assign the value back */
212 mm->context.low_slices_psize = lpsizes;
214 hpsizes = mm->context.high_slices_psize;
215 for (i = 0; i < SLICE_NUM_HIGH; i++) {
216 mask_index = i & 0x1;
218 if (mask.high_slices & (1ul << i))
219 hpsizes[index] = (hpsizes[index] &
220 ~(0xf << (mask_index * 4))) |
221 (((unsigned long)psize) << (mask_index * 4));
224 slice_dbg(" lsps=%lx, hsps=%lx\n",
225 mm->context.low_slices_psize,
226 mm->context.high_slices_psize);
228 spin_unlock_irqrestore(&slice_convert_lock, flags);
230 #ifdef CONFIG_SPU_BASE
231 spu_flush_all_slbs(mm);
235 static unsigned long slice_find_area_bottomup(struct mm_struct *mm,
237 struct slice_mask available,
238 int psize, int use_cache)
240 struct vm_area_struct *vma;
241 unsigned long start_addr, addr;
242 struct slice_mask mask;
243 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
246 if (len <= mm->cached_hole_size) {
247 start_addr = addr = TASK_UNMAPPED_BASE;
248 mm->cached_hole_size = 0;
250 start_addr = addr = mm->free_area_cache;
252 start_addr = addr = TASK_UNMAPPED_BASE;
256 addr = _ALIGN_UP(addr, 1ul << pshift);
257 if ((TASK_SIZE - len) < addr)
259 vma = find_vma(mm, addr);
260 BUG_ON(vma && (addr >= vma->vm_end));
262 mask = slice_range_to_mask(addr, len);
263 if (!slice_check_fit(mask, available)) {
264 if (addr < SLICE_LOW_TOP)
265 addr = _ALIGN_UP(addr + 1, 1ul << SLICE_LOW_SHIFT);
267 addr = _ALIGN_UP(addr + 1, 1ul << SLICE_HIGH_SHIFT);
270 if (!vma || addr + len <= vma->vm_start) {
272 * Remember the place where we stopped the search:
275 mm->free_area_cache = addr + len;
278 if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
279 mm->cached_hole_size = vma->vm_start - addr;
283 /* Make sure we didn't miss any holes */
284 if (use_cache && start_addr != TASK_UNMAPPED_BASE) {
285 start_addr = addr = TASK_UNMAPPED_BASE;
286 mm->cached_hole_size = 0;
292 static unsigned long slice_find_area_topdown(struct mm_struct *mm,
294 struct slice_mask available,
295 int psize, int use_cache)
297 struct vm_area_struct *vma;
299 struct slice_mask mask;
300 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
302 /* check if free_area_cache is useful for us */
304 if (len <= mm->cached_hole_size) {
305 mm->cached_hole_size = 0;
306 mm->free_area_cache = mm->mmap_base;
309 /* either no address requested or can't fit in requested
312 addr = mm->free_area_cache;
314 /* make sure it can fit in the remaining address space */
316 addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
317 mask = slice_range_to_mask(addr, len);
318 if (slice_check_fit(mask, available) &&
319 slice_area_is_free(mm, addr, len))
320 /* remember the address as a hint for
323 return (mm->free_area_cache = addr);
327 addr = mm->mmap_base;
329 /* Go down by chunk size */
330 addr = _ALIGN_DOWN(addr - len, 1ul << pshift);
332 /* Check for hit with different page size */
333 mask = slice_range_to_mask(addr, len);
334 if (!slice_check_fit(mask, available)) {
335 if (addr < SLICE_LOW_TOP)
336 addr = _ALIGN_DOWN(addr, 1ul << SLICE_LOW_SHIFT);
337 else if (addr < (1ul << SLICE_HIGH_SHIFT))
338 addr = SLICE_LOW_TOP;
340 addr = _ALIGN_DOWN(addr, 1ul << SLICE_HIGH_SHIFT);
345 * Lookup failure means no vma is above this address,
346 * else if new region fits below vma->vm_start,
347 * return with success:
349 vma = find_vma(mm, addr);
350 if (!vma || (addr + len) <= vma->vm_start) {
351 /* remember the address as a hint for next time */
353 mm->free_area_cache = addr;
357 /* remember the largest hole we saw so far */
358 if (use_cache && (addr + mm->cached_hole_size) < vma->vm_start)
359 mm->cached_hole_size = vma->vm_start - addr;
361 /* try just below the current vma->vm_start */
362 addr = vma->vm_start;
366 * A failed mmap() very likely causes application failure,
367 * so fall back to the bottom-up function here. This scenario
368 * can happen with large stack limits and large mmap()
371 addr = slice_find_area_bottomup(mm, len, available, psize, 0);
374 * Restore the topdown base:
377 mm->free_area_cache = mm->mmap_base;
378 mm->cached_hole_size = ~0UL;
385 static unsigned long slice_find_area(struct mm_struct *mm, unsigned long len,
386 struct slice_mask mask, int psize,
387 int topdown, int use_cache)
390 return slice_find_area_topdown(mm, len, mask, psize, use_cache);
392 return slice_find_area_bottomup(mm, len, mask, psize, use_cache);
395 #define or_mask(dst, src) do { \
396 (dst).low_slices |= (src).low_slices; \
397 (dst).high_slices |= (src).high_slices; \
400 #define andnot_mask(dst, src) do { \
401 (dst).low_slices &= ~(src).low_slices; \
402 (dst).high_slices &= ~(src).high_slices; \
405 #ifdef CONFIG_PPC_64K_PAGES
406 #define MMU_PAGE_BASE MMU_PAGE_64K
408 #define MMU_PAGE_BASE MMU_PAGE_4K
411 unsigned long slice_get_unmapped_area(unsigned long addr, unsigned long len,
412 unsigned long flags, unsigned int psize,
413 int topdown, int use_cache)
415 struct slice_mask mask = {0, 0};
416 struct slice_mask good_mask;
417 struct slice_mask potential_mask = {0,0} /* silence stupid warning */;
418 struct slice_mask compat_mask = {0, 0};
419 int fixed = (flags & MAP_FIXED);
420 int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT);
421 struct mm_struct *mm = current->mm;
422 unsigned long newaddr;
425 BUG_ON(mm->task_size == 0);
427 slice_dbg("slice_get_unmapped_area(mm=%p, psize=%d...\n", mm, psize);
428 slice_dbg(" addr=%lx, len=%lx, flags=%lx, topdown=%d, use_cache=%d\n",
429 addr, len, flags, topdown, use_cache);
431 if (len > mm->task_size)
433 if (len & ((1ul << pshift) - 1))
435 if (fixed && (addr & ((1ul << pshift) - 1)))
437 if (fixed && addr > (mm->task_size - len))
440 /* If hint, make sure it matches our alignment restrictions */
441 if (!fixed && addr) {
442 addr = _ALIGN_UP(addr, 1ul << pshift);
443 slice_dbg(" aligned addr=%lx\n", addr);
444 /* Ignore hint if it's too large or overlaps a VMA */
445 if (addr > mm->task_size - len ||
446 !slice_area_is_free(mm, addr, len))
450 /* First make up a "good" mask of slices that have the right size
453 good_mask = slice_mask_for_size(mm, psize);
454 slice_print_mask(" good_mask", good_mask);
457 * Here "good" means slices that are already the right page size,
458 * "compat" means slices that have a compatible page size (i.e.
459 * 4k in a 64k pagesize kernel), and "free" means slices without
463 * check if fits in good | compat => OK
464 * check if fits in good | compat | free => convert free
467 * check if hint fits in good => OK
468 * check if hint fits in good | free => convert free
470 * search in good, found => OK
471 * search in good | free, found => convert free
472 * search in good | compat | free, found => convert free.
475 #ifdef CONFIG_PPC_64K_PAGES
476 /* If we support combo pages, we can allow 64k pages in 4k slices */
477 if (psize == MMU_PAGE_64K) {
478 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
480 or_mask(good_mask, compat_mask);
484 /* First check hint if it's valid or if we have MAP_FIXED */
485 if (addr != 0 || fixed) {
486 /* Build a mask for the requested range */
487 mask = slice_range_to_mask(addr, len);
488 slice_print_mask(" mask", mask);
490 /* Check if we fit in the good mask. If we do, we just return,
493 if (slice_check_fit(mask, good_mask)) {
494 slice_dbg(" fits good !\n");
498 /* Now let's see if we can find something in the existing
499 * slices for that size
501 newaddr = slice_find_area(mm, len, good_mask, psize, topdown,
503 if (newaddr != -ENOMEM) {
504 /* Found within the good mask, we don't have to setup,
505 * we thus return directly
507 slice_dbg(" found area at 0x%lx\n", newaddr);
512 /* We don't fit in the good mask, check what other slices are
513 * empty and thus can be converted
515 potential_mask = slice_mask_for_free(mm);
516 or_mask(potential_mask, good_mask);
517 slice_print_mask(" potential", potential_mask);
519 if ((addr != 0 || fixed) && slice_check_fit(mask, potential_mask)) {
520 slice_dbg(" fits potential !\n");
524 /* If we have MAP_FIXED and failed the above steps, then error out */
528 slice_dbg(" search...\n");
530 /* If we had a hint that didn't work out, see if we can fit
531 * anywhere in the good area.
534 addr = slice_find_area(mm, len, good_mask, psize, topdown,
536 if (addr != -ENOMEM) {
537 slice_dbg(" found area at 0x%lx\n", addr);
542 /* Now let's see if we can find something in the existing slices
543 * for that size plus free slices
545 addr = slice_find_area(mm, len, potential_mask, psize, topdown,
548 #ifdef CONFIG_PPC_64K_PAGES
549 if (addr == -ENOMEM && psize == MMU_PAGE_64K) {
550 /* retry the search with 4k-page slices included */
551 or_mask(potential_mask, compat_mask);
552 addr = slice_find_area(mm, len, potential_mask, psize,
560 mask = slice_range_to_mask(addr, len);
561 slice_dbg(" found potential area at 0x%lx\n", addr);
562 slice_print_mask(" mask", mask);
565 andnot_mask(mask, good_mask);
566 andnot_mask(mask, compat_mask);
567 if (mask.low_slices || mask.high_slices) {
568 slice_convert(mm, mask, psize);
569 if (psize > MMU_PAGE_BASE)
570 on_each_cpu(slice_flush_segments, mm, 1);
575 EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
577 unsigned long arch_get_unmapped_area(struct file *filp,
583 return slice_get_unmapped_area(addr, len, flags,
584 current->mm->context.user_psize,
588 unsigned long arch_get_unmapped_area_topdown(struct file *filp,
589 const unsigned long addr0,
590 const unsigned long len,
591 const unsigned long pgoff,
592 const unsigned long flags)
594 return slice_get_unmapped_area(addr0, len, flags,
595 current->mm->context.user_psize,
599 unsigned int get_slice_psize(struct mm_struct *mm, unsigned long addr)
601 unsigned char *hpsizes;
602 int index, mask_index;
604 if (addr < SLICE_LOW_TOP) {
606 lpsizes = mm->context.low_slices_psize;
607 index = GET_LOW_SLICE_INDEX(addr);
608 return (lpsizes >> (index * 4)) & 0xf;
610 hpsizes = mm->context.high_slices_psize;
611 index = GET_HIGH_SLICE_INDEX(addr);
612 mask_index = index & 0x1;
613 return (hpsizes[index >> 1] >> (mask_index * 4)) & 0xf;
615 EXPORT_SYMBOL_GPL(get_slice_psize);
618 * This is called by hash_page when it needs to do a lazy conversion of
619 * an address space from real 64K pages to combo 4K pages (typically
620 * when hitting a non cacheable mapping on a processor or hypervisor
621 * that won't allow them for 64K pages).
623 * This is also called in init_new_context() to change back the user
624 * psize from whatever the parent context had it set to
625 * N.B. This may be called before mm->context.id has been set.
627 * This function will only change the content of the {low,high)_slice_psize
628 * masks, it will not flush SLBs as this shall be handled lazily by the
631 void slice_set_user_psize(struct mm_struct *mm, unsigned int psize)
633 int index, mask_index;
634 unsigned char *hpsizes;
635 unsigned long flags, lpsizes;
636 unsigned int old_psize;
639 slice_dbg("slice_set_user_psize(mm=%p, psize=%d)\n", mm, psize);
641 spin_lock_irqsave(&slice_convert_lock, flags);
643 old_psize = mm->context.user_psize;
644 slice_dbg(" old_psize=%d\n", old_psize);
645 if (old_psize == psize)
648 mm->context.user_psize = psize;
651 lpsizes = mm->context.low_slices_psize;
652 for (i = 0; i < SLICE_NUM_LOW; i++)
653 if (((lpsizes >> (i * 4)) & 0xf) == old_psize)
654 lpsizes = (lpsizes & ~(0xful << (i * 4))) |
655 (((unsigned long)psize) << (i * 4));
656 /* Assign the value back */
657 mm->context.low_slices_psize = lpsizes;
659 hpsizes = mm->context.high_slices_psize;
660 for (i = 0; i < SLICE_NUM_HIGH; i++) {
661 mask_index = i & 0x1;
663 if (((hpsizes[index] >> (mask_index * 4)) & 0xf) == old_psize)
664 hpsizes[index] = (hpsizes[index] &
665 ~(0xf << (mask_index * 4))) |
666 (((unsigned long)psize) << (mask_index * 4));
672 slice_dbg(" lsps=%lx, hsps=%lx\n",
673 mm->context.low_slices_psize,
674 mm->context.high_slices_psize);
677 spin_unlock_irqrestore(&slice_convert_lock, flags);
680 void slice_set_psize(struct mm_struct *mm, unsigned long address,
683 unsigned char *hpsizes;
684 unsigned long i, flags;
687 spin_lock_irqsave(&slice_convert_lock, flags);
688 if (address < SLICE_LOW_TOP) {
689 i = GET_LOW_SLICE_INDEX(address);
690 lpsizes = &mm->context.low_slices_psize;
691 *lpsizes = (*lpsizes & ~(0xful << (i * 4))) |
692 ((unsigned long) psize << (i * 4));
694 int index, mask_index;
695 i = GET_HIGH_SLICE_INDEX(address);
696 hpsizes = mm->context.high_slices_psize;
697 mask_index = i & 0x1;
699 hpsizes[index] = (hpsizes[index] &
700 ~(0xf << (mask_index * 4))) |
701 (((unsigned long)psize) << (mask_index * 4));
704 spin_unlock_irqrestore(&slice_convert_lock, flags);
706 #ifdef CONFIG_SPU_BASE
707 spu_flush_all_slbs(mm);
711 void slice_set_range_psize(struct mm_struct *mm, unsigned long start,
712 unsigned long len, unsigned int psize)
714 struct slice_mask mask = slice_range_to_mask(start, len);
716 slice_convert(mm, mask, psize);
720 * is_hugepage_only_range() is used by generic code to verify wether
721 * a normal mmap mapping (non hugetlbfs) is valid on a given area.
723 * until the generic code provides a more generic hook and/or starts
724 * calling arch get_unmapped_area for MAP_FIXED (which our implementation
725 * here knows how to deal with), we hijack it to keep standard mappings
728 * because of that generic code limitation, MAP_FIXED mapping cannot
729 * "convert" back a slice with no VMAs to the standard page size, only
730 * get_unmapped_area() can. It would be possible to fix it here but I
731 * prefer working on fixing the generic code instead.
733 * WARNING: This will not work if hugetlbfs isn't enabled since the
734 * generic code will redefine that function as 0 in that. This is ok
735 * for now as we only use slices with hugetlbfs enabled. This should
736 * be fixed as the generic code gets fixed.
738 int is_hugepage_only_range(struct mm_struct *mm, unsigned long addr,
741 struct slice_mask mask, available;
742 unsigned int psize = mm->context.user_psize;
744 mask = slice_range_to_mask(addr, len);
745 available = slice_mask_for_size(mm, psize);
746 #ifdef CONFIG_PPC_64K_PAGES
747 /* We need to account for 4k slices too */
748 if (psize == MMU_PAGE_64K) {
749 struct slice_mask compat_mask;
750 compat_mask = slice_mask_for_size(mm, MMU_PAGE_4K);
751 or_mask(available, compat_mask);
755 #if 0 /* too verbose */
756 slice_dbg("is_hugepage_only_range(mm=%p, addr=%lx, len=%lx)\n",
758 slice_print_mask(" mask", mask);
759 slice_print_mask(" available", available);
761 return !slice_check_fit(mask, available);