// SPDX-License-Identifier: GPL-2.0-or-later /* * address space "slices" (meta-segments) support * * Copyright (C) 2007 Benjamin Herrenschmidt, IBM Corporation. * * Based on hugetlb implementation * * Copyright (C) 2003 David Gibson, IBM Corporation.
*/
/* Hack, so that each addresses is controlled by exactly one * of the high or low area bitmaps, the first high area starts
* at 4GB, not 0 */ if (start == 0)
start = (unsignedlong)SLICE_LOW_TOP;
return !slice_area_is_free(mm, start, end - start);
}
hpsizes = mm_ctx_high_slices(&mm->context); for (i = 0; i < GET_HIGH_SLICE_INDEX(mm_ctx_slb_addr_limit(&mm->context)); i++) { if (!test_bit(i, mask->high_slices)) continue;
/* * Compute which slice addr is part of; * set *boundary_addr to the start or end boundary of that slice * (depending on 'end' parameter); * return boolean indicating if the slice is marked as available in the * 'available' slice_mark.
*/ staticbool slice_scan_available(unsignedlong addr, conststruct slice_mask *available, int end, unsignedlong *boundary_addr)
{ unsignedlong slice; if (slice_addr_is_low(addr)) {
slice = GET_LOW_SLICE_INDEX(addr);
*boundary_addr = (slice + end) << SLICE_LOW_SHIFT; return !!(available->low_slices & (1u << slice));
} else {
slice = GET_HIGH_SLICE_INDEX(addr);
*boundary_addr = (slice + end) ?
((slice + end) << SLICE_HIGH_SHIFT) : SLICE_LOW_TOP; return !!test_bit(slice, available->high_slices);
}
}
staticunsignedlong slice_find_area_bottomup(struct mm_struct *mm, unsignedlong addr, unsignedlong len, conststruct slice_mask *available, int psize, unsignedlong high_limit)
{ int pshift = max_t(int, mmu_psize_defs[psize].shift, PAGE_SHIFT); unsignedlong found, next_end; struct vm_unmapped_area_info info = {
.length = len,
.align_mask = PAGE_MASK & ((1ul << pshift) - 1),
}; /* * Check till the allow max value for this mmap request
*/ while (addr < high_limit) {
info.low_limit = addr; if (!slice_scan_available(addr, available, 1, &addr)) continue;
next_slice: /* * At this point [info.low_limit; addr) covers * available slices only and ends at a slice boundary. * Check if we need to reduce the range, or if we can * extend it to cover the next available slice.
*/ if (addr >= high_limit)
addr = high_limit; elseif (slice_scan_available(addr, available, 1, &next_end)) {
addr = next_end; goto next_slice;
}
info.high_limit = addr;
found = vm_unmapped_area(&info); if (!(found & ~PAGE_MASK)) return found;
}
/* * If we are trying to allocate above DEFAULT_MAP_WINDOW * Add the different to the mmap_base. * Only for that request for which high_limit is above * DEFAULT_MAP_WINDOW we should apply this.
*/ if (high_limit > DEFAULT_MAP_WINDOW)
addr += mm_ctx_slb_addr_limit(&mm->context) - DEFAULT_MAP_WINDOW;
while (addr > min_addr) {
info.high_limit = addr; if (!slice_scan_available(addr - 1, available, 0, &addr)) continue;
prev_slice: /* * At this point [addr; info.high_limit) covers * available slices only and starts at a slice boundary. * Check if we need to reduce the range, or if we can * extend it to cover the previous available slice.
*/ if (addr < min_addr)
addr = min_addr; elseif (slice_scan_available(addr - 1, available, 0, &prev)) {
addr = prev; goto prev_slice;
}
info.low_limit = addr;
found = vm_unmapped_area(&info); if (!(found & ~PAGE_MASK)) return found;
}
/* * A failed mmap() very likely causes application failure, * so fall back to the bottom-up function here. This scenario * can happen with large stack limits and large mmap() * allocations.
*/ return slice_find_area_bottomup(mm, TASK_UNMAPPED_BASE, len, available, psize, high_limit);
}
high_limit = DEFAULT_MAP_WINDOW; if (addr >= high_limit || (fixed && (addr + len > high_limit)))
high_limit = TASK_SIZE;
if (len > high_limit) return -ENOMEM; if (len & (page_size - 1)) return -EINVAL; if (fixed) { if (addr & (page_size - 1)) return -EINVAL; if (addr > high_limit - len) return -ENOMEM;
}
if (high_limit > mm_ctx_slb_addr_limit(&mm->context)) { /* * Increasing the slb_addr_limit does not require * slice mask cache to be recalculated because it should * be already initialised beyond the old address limit.
*/
mm_ctx_set_slb_addr_limit(&mm->context, high_limit);
/* If hint, make sure it matches our alignment restrictions */ if (!fixed && addr) {
addr = ALIGN(addr, page_size);
slice_dbg(" aligned addr=%lx\n", addr); /* Ignore hint if it's too large or overlaps a VMA */ if (addr > high_limit - len || addr < mmap_min_addr ||
!slice_area_is_free(mm, addr, len))
addr = 0;
}
/* First make up a "good" mask of slices that have the right size * already
*/
maskp = slice_mask_for_size(&mm->context, psize);
/* * Here "good" means slices that are already the right page size, * "compat" means slices that have a compatible page size (i.e. * 4k in a 64k pagesize kernel), and "free" means slices without * any VMAs. * * If MAP_FIXED: * check if fits in good | compat => OK * check if fits in good | compat | free => convert free * else bad * If have hint: * check if hint fits in good => OK * check if hint fits in good | free => convert free * Otherwise: * search in good, found => OK * search in good | free, found => convert free * search in good | compat | free, found => convert free.
*/
/* * If we support combo pages, we can allow 64k pages in 4k slices * The mask copies could be avoided in most cases here if we had * a pointer to good mask for the next code to use.
*/ if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && psize == MMU_PAGE_64K) {
compat_maskp = slice_mask_for_size(&mm->context, MMU_PAGE_4K); if (fixed)
slice_or_mask(&good_mask, maskp, compat_maskp); else
slice_copy_mask(&good_mask, maskp);
} else {
slice_copy_mask(&good_mask, maskp);
}
slice_print_mask(" good_mask", &good_mask); if (compat_maskp)
slice_print_mask(" compat_mask", compat_maskp);
/* First check hint if it's valid or if we have MAP_FIXED */ if (addr != 0 || fixed) { /* Check if we fit in the good mask. If we do, we just return, * nothing else to do
*/ if (slice_check_range_fits(mm, &good_mask, addr, len)) {
slice_dbg(" fits good !\n");
newaddr = addr; goto return_addr;
}
} else { /* Now let's see if we can find something in the existing * slices for that size
*/
newaddr = slice_find_area(mm, len, &good_mask,
psize, topdown, high_limit); if (newaddr != -ENOMEM) { /* Found within the good mask, we don't have to setup, * we thus return directly
*/
slice_dbg(" found area at 0x%lx\n", newaddr); goto return_addr;
}
} /* * We don't fit in the good mask, check what other slices are * empty and thus can be converted
*/
slice_mask_for_free(mm, &potential_mask, high_limit);
slice_or_mask(&potential_mask, &potential_mask, &good_mask);
slice_print_mask(" potential", &potential_mask);
/* If we have MAP_FIXED and failed the above steps, then error out */ if (fixed) return -EBUSY;
slice_dbg(" search...\n");
/* If we had a hint that didn't work out, see if we can fit * anywhere in the good area.
*/ if (addr) {
newaddr = slice_find_area(mm, len, &good_mask,
psize, topdown, high_limit); if (newaddr != -ENOMEM) {
slice_dbg(" found area at 0x%lx\n", newaddr); goto return_addr;
}
}
/* Now let's see if we can find something in the existing slices * for that size plus free slices
*/
newaddr = slice_find_area(mm, len, &potential_mask,
psize, topdown, high_limit);
if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && newaddr == -ENOMEM &&
psize == MMU_PAGE_64K) { /* retry the search with 4k-page slices included */
slice_or_mask(&potential_mask, &potential_mask, compat_maskp);
newaddr = slice_find_area(mm, len, &potential_mask,
psize, topdown, high_limit);
}
if (newaddr == -ENOMEM) return -ENOMEM;
slice_range_to_mask(newaddr, len, &potential_mask);
slice_dbg(" found potential area at 0x%lx\n", newaddr);
slice_print_mask(" mask", &potential_mask);
convert: /* * Try to allocate the context before we do slice convert * so that we handle the context allocation failure gracefully.
*/ if (need_extra_context(mm, newaddr)) { if (alloc_extended_context(mm, newaddr) < 0) return -ENOMEM;
}
slice_andnot_mask(&potential_mask, &potential_mask, &good_mask); if (compat_maskp && !fixed)
slice_andnot_mask(&potential_mask, &potential_mask, compat_maskp); if (potential_mask.low_slices ||
(SLICE_NUM_HIGH &&
!bitmap_empty(potential_mask.high_slices, SLICE_NUM_HIGH))) {
slice_convert(mm, &potential_mask, psize); if (psize > MMU_PAGE_BASE)
on_each_cpu(slice_flush_segments, mm, 1);
} return newaddr;
return_addr: if (need_extra_context(mm, newaddr)) { if (alloc_extended_context(mm, newaddr) < 0) return -ENOMEM;
} return newaddr;
}
EXPORT_SYMBOL_GPL(slice_get_unmapped_area);
/* * In the case of exec, use the default limit. In the * case of fork it is just inherited from the mm being * duplicated.
*/
mm_ctx_set_slb_addr_limit(&mm->context, SLB_ADDR_LIMIT_DEFAULT);
mm_ctx_set_user_psize(&mm->context, psize);
/* * Set all slice psizes to the default.
*/
lpsizes = mm_ctx_low_slices(&mm->context);
memset(lpsizes, (psize << 4) | psize, SLICE_NUM_LOW >> 1);
#ifdef CONFIG_HUGETLB_PAGE /* * is_hugepage_only_range() is used by generic code to verify whether * a normal mmap mapping (non hugetlbfs) is valid on a given area. * * until the generic code provides a more generic hook and/or starts * calling arch get_unmapped_area for MAP_FIXED (which our implementation * here knows how to deal with), we hijack it to keep standard mappings * away from us. * * because of that generic code limitation, MAP_FIXED mapping cannot * "convert" back a slice with no VMAs to the standard page size, only * get_unmapped_area() can. It would be possible to fix it here but I * prefer working on fixing the generic code instead. * * WARNING: This will not work if hugetlbfs isn't enabled since the * generic code will redefine that function as 0 in that. This is ok * for now as we only use slices with hugetlbfs enabled. This should * be fixed as the generic code gets fixed.
*/ int slice_is_hugepage_only_range(struct mm_struct *mm, unsignedlong addr, unsignedlong len)
{ conststruct slice_mask *maskp; unsignedint psize = mm_ctx_user_psize(&mm->context);
VM_BUG_ON(radix_enabled());
maskp = slice_mask_for_size(&mm->context, psize);
/* We need to account for 4k slices too */ if (IS_ENABLED(CONFIG_PPC_64K_PAGES) && psize == MMU_PAGE_64K) { conststruct slice_mask *compat_maskp; struct slice_mask available;
unsignedlong vma_mmu_pagesize(struct vm_area_struct *vma)
{ /* With radix we don't use slice, so derive it from vma*/ if (radix_enabled()) return vma_kernel_pagesize(vma);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.