Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * IA-32 Huge TLB Page Support for Kernel.
0004  *
0005  * Copyright (C) 2002, Rohit Seth <rohit.seth@intel.com>
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/fs.h>
0010 #include <linux/mm.h>
0011 #include <linux/sched/mm.h>
0012 #include <linux/hugetlb.h>
0013 #include <linux/pagemap.h>
0014 #include <linux/err.h>
0015 #include <linux/sysctl.h>
0016 #include <linux/compat.h>
0017 #include <asm/mman.h>
0018 #include <asm/tlb.h>
0019 #include <asm/tlbflush.h>
0020 #include <asm/elf.h>
0021 
0022 /*
0023  * pmd_huge() returns 1 if @pmd is hugetlb related entry, that is normal
0024  * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
0025  * Otherwise, returns 0.
0026  */
0027 int pmd_huge(pmd_t pmd)
0028 {
0029     return !pmd_none(pmd) &&
0030         (pmd_val(pmd) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
0031 }
0032 
0033 /*
0034  * pud_huge() returns 1 if @pud is hugetlb related entry, that is normal
0035  * hugetlb entry or non-present (migration or hwpoisoned) hugetlb entry.
0036  * Otherwise, returns 0.
0037  */
0038 int pud_huge(pud_t pud)
0039 {
0040     return !pud_none(pud) &&
0041         (pud_val(pud) & (_PAGE_PRESENT|_PAGE_PSE)) != _PAGE_PRESENT;
0042 }
0043 
0044 #ifdef CONFIG_HUGETLB_PAGE
0045 static unsigned long hugetlb_get_unmapped_area_bottomup(struct file *file,
0046         unsigned long addr, unsigned long len,
0047         unsigned long pgoff, unsigned long flags)
0048 {
0049     struct hstate *h = hstate_file(file);
0050     struct vm_unmapped_area_info info;
0051 
0052     info.flags = 0;
0053     info.length = len;
0054     info.low_limit = get_mmap_base(1);
0055 
0056     /*
0057      * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
0058      * in the full address space.
0059      */
0060     info.high_limit = in_32bit_syscall() ?
0061         task_size_32bit() : task_size_64bit(addr > DEFAULT_MAP_WINDOW);
0062 
0063     info.align_mask = PAGE_MASK & ~huge_page_mask(h);
0064     info.align_offset = 0;
0065     return vm_unmapped_area(&info);
0066 }
0067 
0068 static unsigned long hugetlb_get_unmapped_area_topdown(struct file *file,
0069         unsigned long addr, unsigned long len,
0070         unsigned long pgoff, unsigned long flags)
0071 {
0072     struct hstate *h = hstate_file(file);
0073     struct vm_unmapped_area_info info;
0074 
0075     info.flags = VM_UNMAPPED_AREA_TOPDOWN;
0076     info.length = len;
0077     info.low_limit = PAGE_SIZE;
0078     info.high_limit = get_mmap_base(0);
0079 
0080     /*
0081      * If hint address is above DEFAULT_MAP_WINDOW, look for unmapped area
0082      * in the full address space.
0083      */
0084     if (addr > DEFAULT_MAP_WINDOW && !in_32bit_syscall())
0085         info.high_limit += TASK_SIZE_MAX - DEFAULT_MAP_WINDOW;
0086 
0087     info.align_mask = PAGE_MASK & ~huge_page_mask(h);
0088     info.align_offset = 0;
0089     addr = vm_unmapped_area(&info);
0090 
0091     /*
0092      * A failed mmap() very likely causes application failure,
0093      * so fall back to the bottom-up function here. This scenario
0094      * can happen with large stack limits and large mmap()
0095      * allocations.
0096      */
0097     if (addr & ~PAGE_MASK) {
0098         VM_BUG_ON(addr != -ENOMEM);
0099         info.flags = 0;
0100         info.low_limit = TASK_UNMAPPED_BASE;
0101         info.high_limit = TASK_SIZE_LOW;
0102         addr = vm_unmapped_area(&info);
0103     }
0104 
0105     return addr;
0106 }
0107 
0108 unsigned long
0109 hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
0110         unsigned long len, unsigned long pgoff, unsigned long flags)
0111 {
0112     struct hstate *h = hstate_file(file);
0113     struct mm_struct *mm = current->mm;
0114     struct vm_area_struct *vma;
0115 
0116     if (len & ~huge_page_mask(h))
0117         return -EINVAL;
0118 
0119     if (len > TASK_SIZE)
0120         return -ENOMEM;
0121 
0122     /* No address checking. See comment at mmap_address_hint_valid() */
0123     if (flags & MAP_FIXED) {
0124         if (prepare_hugepage_range(file, addr, len))
0125             return -EINVAL;
0126         return addr;
0127     }
0128 
0129     if (addr) {
0130         addr &= huge_page_mask(h);
0131         if (!mmap_address_hint_valid(addr, len))
0132             goto get_unmapped_area;
0133 
0134         vma = find_vma(mm, addr);
0135         if (!vma || addr + len <= vm_start_gap(vma))
0136             return addr;
0137     }
0138 
0139 get_unmapped_area:
0140     if (mm->get_unmapped_area == arch_get_unmapped_area)
0141         return hugetlb_get_unmapped_area_bottomup(file, addr, len,
0142                 pgoff, flags);
0143     else
0144         return hugetlb_get_unmapped_area_topdown(file, addr, len,
0145                 pgoff, flags);
0146 }
0147 #endif /* CONFIG_HUGETLB_PAGE */
0148 
0149 #ifdef CONFIG_X86_64
0150 bool __init arch_hugetlb_valid_size(unsigned long size)
0151 {
0152     if (size == PMD_SIZE)
0153         return true;
0154     else if (size == PUD_SIZE && boot_cpu_has(X86_FEATURE_GBPAGES))
0155         return true;
0156     else
0157         return false;
0158 }
0159 
0160 #ifdef CONFIG_CONTIG_ALLOC
0161 static __init int gigantic_pages_init(void)
0162 {
0163     /* With compaction or CMA we can allocate gigantic pages at runtime */
0164     if (boot_cpu_has(X86_FEATURE_GBPAGES))
0165         hugetlb_add_hstate(PUD_SHIFT - PAGE_SHIFT);
0166     return 0;
0167 }
0168 arch_initcall(gigantic_pages_init);
0169 #endif
0170 #endif