11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * hugetlbpage-backed filesystem. Based on ramfs. 31da177e4SLinus Torvalds * 46d49e352SNadia Yvette Chambers * Nadia Yvette Chambers, 2002 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * Copyright (C) 2002 Linus Torvalds. 73e89e1c5SPaul Gortmaker * License: GPL 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 109b857d26SAndrew Morton #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 119b857d26SAndrew Morton 121da177e4SLinus Torvalds #include <linux/thread_info.h> 131da177e4SLinus Torvalds #include <asm/current.h> 1470c3547eSMike Kravetz #include <linux/falloc.h> 151da177e4SLinus Torvalds #include <linux/fs.h> 161da177e4SLinus Torvalds #include <linux/mount.h> 171da177e4SLinus Torvalds #include <linux/file.h> 18e73a75faSRandy Dunlap #include <linux/kernel.h> 191da177e4SLinus Torvalds #include <linux/writeback.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/highmem.h> 221da177e4SLinus Torvalds #include <linux/init.h> 231da177e4SLinus Torvalds #include <linux/string.h> 2416f7e0feSRandy Dunlap #include <linux/capability.h> 25e73a75faSRandy Dunlap #include <linux/ctype.h> 261da177e4SLinus Torvalds #include <linux/backing-dev.h> 271da177e4SLinus Torvalds #include <linux/hugetlb.h> 281da177e4SLinus Torvalds #include <linux/pagevec.h> 2932021982SDavid Howells #include <linux/fs_parser.h> 30036e0856SBenjamin Herrenschmidt #include <linux/mman.h> 311da177e4SLinus Torvalds #include <linux/slab.h> 321da177e4SLinus Torvalds #include <linux/dnotify.h> 331da177e4SLinus Torvalds #include <linux/statfs.h> 341da177e4SLinus Torvalds #include <linux/security.h> 351fd7317dSNick Black #include <linux/magic.h> 36290408d4SNaoya Horiguchi #include <linux/migrate.h> 3734d0640eSAl Viro #include <linux/uio.h> 381da177e4SLinus Torvalds 397c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 4088590253SShijie Hu #include <linux/sched/mm.h> 411da177e4SLinus Torvalds 42f5e54d6eSChristoph Hellwig static const struct address_space_operations hugetlbfs_aops; 434b6f5d20SArjan van de Ven const struct file_operations hugetlbfs_file_operations; 4492e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_dir_inode_operations; 4592e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_inode_operations; 461da177e4SLinus Torvalds 4732021982SDavid Howells enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT }; 4832021982SDavid Howells 4932021982SDavid Howells struct hugetlbfs_fs_context { 504a25220dSDavid Howells struct hstate *hstate; 5132021982SDavid Howells unsigned long long max_size_opt; 5232021982SDavid Howells unsigned long long min_size_opt; 534a25220dSDavid Howells long max_hpages; 544a25220dSDavid Howells long nr_inodes; 554a25220dSDavid Howells long min_hpages; 5632021982SDavid Howells enum hugetlbfs_size_type max_val_type; 5732021982SDavid Howells enum hugetlbfs_size_type min_val_type; 58a0eb3a05SEric W. Biederman kuid_t uid; 59a0eb3a05SEric W. Biederman kgid_t gid; 60a1d776eeSDavid Gibson umode_t mode; 61a1d776eeSDavid Gibson }; 62a1d776eeSDavid Gibson 631da177e4SLinus Torvalds int sysctl_hugetlb_shm_group; 641da177e4SLinus Torvalds 6532021982SDavid Howells enum hugetlb_param { 6632021982SDavid Howells Opt_gid, 6732021982SDavid Howells Opt_min_size, 6832021982SDavid Howells Opt_mode, 6932021982SDavid Howells Opt_nr_inodes, 7032021982SDavid Howells Opt_pagesize, 7132021982SDavid Howells Opt_size, 7232021982SDavid Howells Opt_uid, 73e73a75faSRandy Dunlap }; 74e73a75faSRandy Dunlap 75d7167b14SAl Viro static const struct fs_parameter_spec hugetlb_fs_parameters[] = { 7632021982SDavid Howells fsparam_u32 ("gid", Opt_gid), 7732021982SDavid Howells fsparam_string("min_size", Opt_min_size), 78e0f7e2b2SMike Kravetz fsparam_u32oct("mode", Opt_mode), 7932021982SDavid Howells fsparam_string("nr_inodes", Opt_nr_inodes), 8032021982SDavid Howells fsparam_string("pagesize", Opt_pagesize), 8132021982SDavid Howells fsparam_string("size", Opt_size), 8232021982SDavid Howells fsparam_u32 ("uid", Opt_uid), 8332021982SDavid Howells {} 8432021982SDavid Howells }; 8532021982SDavid Howells 8670c3547eSMike Kravetz #ifdef CONFIG_NUMA 8770c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma, 8870c3547eSMike Kravetz struct inode *inode, pgoff_t index) 8970c3547eSMike Kravetz { 9070c3547eSMike Kravetz vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy, 9170c3547eSMike Kravetz index); 9270c3547eSMike Kravetz } 9370c3547eSMike Kravetz 9470c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma) 9570c3547eSMike Kravetz { 9670c3547eSMike Kravetz mpol_cond_put(vma->vm_policy); 9770c3547eSMike Kravetz } 9870c3547eSMike Kravetz #else 9970c3547eSMike Kravetz static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma, 10070c3547eSMike Kravetz struct inode *inode, pgoff_t index) 10170c3547eSMike Kravetz { 10270c3547eSMike Kravetz } 10370c3547eSMike Kravetz 10470c3547eSMike Kravetz static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma) 10570c3547eSMike Kravetz { 10670c3547eSMike Kravetz } 10770c3547eSMike Kravetz #endif 10870c3547eSMike Kravetz 10963489f8eSMike Kravetz /* 11063489f8eSMike Kravetz * Mask used when checking the page offset value passed in via system 11163489f8eSMike Kravetz * calls. This value will be converted to a loff_t which is signed. 11263489f8eSMike Kravetz * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the 11363489f8eSMike Kravetz * value. The extra bit (- 1 in the shift value) is to take the sign 11463489f8eSMike Kravetz * bit into account. 11563489f8eSMike Kravetz */ 11663489f8eSMike Kravetz #define PGOFF_LOFFT_MAX \ 11763489f8eSMike Kravetz (((1UL << (PAGE_SHIFT + 1)) - 1) << (BITS_PER_LONG - (PAGE_SHIFT + 1))) 11863489f8eSMike Kravetz 1191da177e4SLinus Torvalds static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) 1201da177e4SLinus Torvalds { 121496ad9aaSAl Viro struct inode *inode = file_inode(file); 12222247efdSPeter Xu struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); 1231da177e4SLinus Torvalds loff_t len, vma_len; 1241da177e4SLinus Torvalds int ret; 125a5516438SAndi Kleen struct hstate *h = hstate_file(file); 1261da177e4SLinus Torvalds 12768589bc3SHugh Dickins /* 128dec4ad86SDavid Gibson * vma address alignment (but not the pgoff alignment) has 129dec4ad86SDavid Gibson * already been checked by prepare_hugepage_range. If you add 130dec4ad86SDavid Gibson * any error returns here, do so after setting VM_HUGETLB, so 131dec4ad86SDavid Gibson * is_vm_hugetlb_page tests below unmap_region go the right 13245e55300SPeter Collingbourne * way when do_mmap unwinds (may be important on powerpc 133dec4ad86SDavid Gibson * and ia64). 13468589bc3SHugh Dickins */ 135a2fce914SNaoya Horiguchi vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND; 13668589bc3SHugh Dickins vma->vm_ops = &hugetlb_vm_ops; 1371da177e4SLinus Torvalds 13822247efdSPeter Xu ret = seal_check_future_write(info->seals, vma); 13922247efdSPeter Xu if (ret) 14022247efdSPeter Xu return ret; 14122247efdSPeter Xu 142045c7a3fSMike Kravetz /* 14363489f8eSMike Kravetz * page based offset in vm_pgoff could be sufficiently large to 1445df63c2aSMike Kravetz * overflow a loff_t when converted to byte offset. This can 1455df63c2aSMike Kravetz * only happen on architectures where sizeof(loff_t) == 1465df63c2aSMike Kravetz * sizeof(unsigned long). So, only check in those instances. 147045c7a3fSMike Kravetz */ 1485df63c2aSMike Kravetz if (sizeof(unsigned long) == sizeof(loff_t)) { 14963489f8eSMike Kravetz if (vma->vm_pgoff & PGOFF_LOFFT_MAX) 150045c7a3fSMike Kravetz return -EINVAL; 1515df63c2aSMike Kravetz } 152045c7a3fSMike Kravetz 15363489f8eSMike Kravetz /* must be huge page aligned */ 1542b37c35eSBecky Bruce if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT)) 155dec4ad86SDavid Gibson return -EINVAL; 156dec4ad86SDavid Gibson 1571da177e4SLinus Torvalds vma_len = (loff_t)(vma->vm_end - vma->vm_start); 158045c7a3fSMike Kravetz len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT); 159045c7a3fSMike Kravetz /* check for overflow */ 160045c7a3fSMike Kravetz if (len < vma_len) 161045c7a3fSMike Kravetz return -EINVAL; 1621da177e4SLinus Torvalds 1635955102cSAl Viro inode_lock(inode); 1641da177e4SLinus Torvalds file_accessed(file); 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds ret = -ENOMEM; 16733b8f84aSMike Kravetz if (!hugetlb_reserve_pages(inode, 168a5516438SAndi Kleen vma->vm_pgoff >> huge_page_order(h), 1695a6fe125SMel Gorman len >> huge_page_shift(h), vma, 1705a6fe125SMel Gorman vma->vm_flags)) 171b45b5bd6SDavid Gibson goto out; 172b45b5bd6SDavid Gibson 1734c887265SAdam Litke ret = 0; 174b6174df5SZhang, Yanmin if (vma->vm_flags & VM_WRITE && inode->i_size < len) 175045c7a3fSMike Kravetz i_size_write(inode, len); 1761da177e4SLinus Torvalds out: 1775955102cSAl Viro inode_unlock(inode); 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds return ret; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* 1833e4e28c5SMichel Lespinasse * Called under mmap_write_lock(mm). 1841da177e4SLinus Torvalds */ 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds static unsigned long 18788590253SShijie Hu hugetlb_get_unmapped_area_bottomup(struct file *file, unsigned long addr, 18888590253SShijie Hu unsigned long len, unsigned long pgoff, unsigned long flags) 18988590253SShijie Hu { 19088590253SShijie Hu struct hstate *h = hstate_file(file); 19188590253SShijie Hu struct vm_unmapped_area_info info; 19288590253SShijie Hu 19388590253SShijie Hu info.flags = 0; 19488590253SShijie Hu info.length = len; 19588590253SShijie Hu info.low_limit = current->mm->mmap_base; 1962cb4de08SChristophe Leroy info.high_limit = arch_get_mmap_end(addr, len, flags); 19788590253SShijie Hu info.align_mask = PAGE_MASK & ~huge_page_mask(h); 19888590253SShijie Hu info.align_offset = 0; 19988590253SShijie Hu return vm_unmapped_area(&info); 20088590253SShijie Hu } 20188590253SShijie Hu 20288590253SShijie Hu static unsigned long 20388590253SShijie Hu hugetlb_get_unmapped_area_topdown(struct file *file, unsigned long addr, 20488590253SShijie Hu unsigned long len, unsigned long pgoff, unsigned long flags) 20588590253SShijie Hu { 20688590253SShijie Hu struct hstate *h = hstate_file(file); 20788590253SShijie Hu struct vm_unmapped_area_info info; 20888590253SShijie Hu 20988590253SShijie Hu info.flags = VM_UNMAPPED_AREA_TOPDOWN; 21088590253SShijie Hu info.length = len; 21188590253SShijie Hu info.low_limit = max(PAGE_SIZE, mmap_min_addr); 2125f24d5a5SChristophe Leroy info.high_limit = arch_get_mmap_base(addr, current->mm->mmap_base); 21388590253SShijie Hu info.align_mask = PAGE_MASK & ~huge_page_mask(h); 21488590253SShijie Hu info.align_offset = 0; 21588590253SShijie Hu addr = vm_unmapped_area(&info); 21688590253SShijie Hu 21788590253SShijie Hu /* 21888590253SShijie Hu * A failed mmap() very likely causes application failure, 21988590253SShijie Hu * so fall back to the bottom-up function here. This scenario 22088590253SShijie Hu * can happen with large stack limits and large mmap() 22188590253SShijie Hu * allocations. 22288590253SShijie Hu */ 22388590253SShijie Hu if (unlikely(offset_in_page(addr))) { 22488590253SShijie Hu VM_BUG_ON(addr != -ENOMEM); 22588590253SShijie Hu info.flags = 0; 22688590253SShijie Hu info.low_limit = current->mm->mmap_base; 2272cb4de08SChristophe Leroy info.high_limit = arch_get_mmap_end(addr, len, flags); 22888590253SShijie Hu addr = vm_unmapped_area(&info); 22988590253SShijie Hu } 23088590253SShijie Hu 23188590253SShijie Hu return addr; 23288590253SShijie Hu } 23388590253SShijie Hu 2344b439e25SChristophe Leroy unsigned long 2354b439e25SChristophe Leroy generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, 2364b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 2374b439e25SChristophe Leroy unsigned long flags) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds struct mm_struct *mm = current->mm; 2401da177e4SLinus Torvalds struct vm_area_struct *vma; 241a5516438SAndi Kleen struct hstate *h = hstate_file(file); 2422cb4de08SChristophe Leroy const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); 2431da177e4SLinus Torvalds 244a5516438SAndi Kleen if (len & ~huge_page_mask(h)) 2451da177e4SLinus Torvalds return -EINVAL; 2461da177e4SLinus Torvalds if (len > TASK_SIZE) 2471da177e4SLinus Torvalds return -ENOMEM; 2481da177e4SLinus Torvalds 249036e0856SBenjamin Herrenschmidt if (flags & MAP_FIXED) { 250a5516438SAndi Kleen if (prepare_hugepage_range(file, addr, len)) 251036e0856SBenjamin Herrenschmidt return -EINVAL; 252036e0856SBenjamin Herrenschmidt return addr; 253036e0856SBenjamin Herrenschmidt } 254036e0856SBenjamin Herrenschmidt 2551da177e4SLinus Torvalds if (addr) { 256a5516438SAndi Kleen addr = ALIGN(addr, huge_page_size(h)); 2571da177e4SLinus Torvalds vma = find_vma(mm, addr); 2585f24d5a5SChristophe Leroy if (mmap_end - len >= addr && 2591be7107fSHugh Dickins (!vma || addr + len <= vm_start_gap(vma))) 2601da177e4SLinus Torvalds return addr; 2611da177e4SLinus Torvalds } 2621da177e4SLinus Torvalds 26388590253SShijie Hu /* 26488590253SShijie Hu * Use mm->get_unmapped_area value as a hint to use topdown routine. 26588590253SShijie Hu * If architectures have special needs, they should define their own 26688590253SShijie Hu * version of hugetlb_get_unmapped_area. 26788590253SShijie Hu */ 26888590253SShijie Hu if (mm->get_unmapped_area == arch_get_unmapped_area_topdown) 26988590253SShijie Hu return hugetlb_get_unmapped_area_topdown(file, addr, len, 27088590253SShijie Hu pgoff, flags); 27188590253SShijie Hu return hugetlb_get_unmapped_area_bottomup(file, addr, len, 27288590253SShijie Hu pgoff, flags); 2731da177e4SLinus Torvalds } 2744b439e25SChristophe Leroy 2754b439e25SChristophe Leroy #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA 2764b439e25SChristophe Leroy static unsigned long 2774b439e25SChristophe Leroy hugetlb_get_unmapped_area(struct file *file, unsigned long addr, 2784b439e25SChristophe Leroy unsigned long len, unsigned long pgoff, 2794b439e25SChristophe Leroy unsigned long flags) 2804b439e25SChristophe Leroy { 2814b439e25SChristophe Leroy return generic_hugetlb_get_unmapped_area(file, addr, len, pgoff, flags); 2824b439e25SChristophe Leroy } 2831da177e4SLinus Torvalds #endif 2841da177e4SLinus Torvalds 285e63e1e5aSBadari Pulavarty /* 286e63e1e5aSBadari Pulavarty * Support for read() - Find the page attached to f_mapping and copy out the 287445c8098SMiaohe Lin * data. This provides functionality similar to filemap_read(). 288e63e1e5aSBadari Pulavarty */ 28934d0640eSAl Viro static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to) 290e63e1e5aSBadari Pulavarty { 29134d0640eSAl Viro struct file *file = iocb->ki_filp; 29234d0640eSAl Viro struct hstate *h = hstate_file(file); 29334d0640eSAl Viro struct address_space *mapping = file->f_mapping; 294e63e1e5aSBadari Pulavarty struct inode *inode = mapping->host; 29534d0640eSAl Viro unsigned long index = iocb->ki_pos >> huge_page_shift(h); 29634d0640eSAl Viro unsigned long offset = iocb->ki_pos & ~huge_page_mask(h); 297e63e1e5aSBadari Pulavarty unsigned long end_index; 298e63e1e5aSBadari Pulavarty loff_t isize; 299e63e1e5aSBadari Pulavarty ssize_t retval = 0; 300e63e1e5aSBadari Pulavarty 30134d0640eSAl Viro while (iov_iter_count(to)) { 302e63e1e5aSBadari Pulavarty struct page *page; 30334d0640eSAl Viro size_t nr, copied; 304e63e1e5aSBadari Pulavarty 305e63e1e5aSBadari Pulavarty /* nr is the maximum number of bytes to copy from this page */ 306a5516438SAndi Kleen nr = huge_page_size(h); 307a05b0855SAneesh Kumar K.V isize = i_size_read(inode); 308a05b0855SAneesh Kumar K.V if (!isize) 30934d0640eSAl Viro break; 310a05b0855SAneesh Kumar K.V end_index = (isize - 1) >> huge_page_shift(h); 311e63e1e5aSBadari Pulavarty if (index > end_index) 31234d0640eSAl Viro break; 31334d0640eSAl Viro if (index == end_index) { 314a5516438SAndi Kleen nr = ((isize - 1) & ~huge_page_mask(h)) + 1; 315a05b0855SAneesh Kumar K.V if (nr <= offset) 31634d0640eSAl Viro break; 317e63e1e5aSBadari Pulavarty } 318e63e1e5aSBadari Pulavarty nr = nr - offset; 319e63e1e5aSBadari Pulavarty 320e63e1e5aSBadari Pulavarty /* Find the page */ 321a05b0855SAneesh Kumar K.V page = find_lock_page(mapping, index); 322e63e1e5aSBadari Pulavarty if (unlikely(page == NULL)) { 323e63e1e5aSBadari Pulavarty /* 324e63e1e5aSBadari Pulavarty * We have a HOLE, zero out the user-buffer for the 325e63e1e5aSBadari Pulavarty * length of the hole or request. 326e63e1e5aSBadari Pulavarty */ 32734d0640eSAl Viro copied = iov_iter_zero(nr, to); 328e63e1e5aSBadari Pulavarty } else { 329a05b0855SAneesh Kumar K.V unlock_page(page); 330a05b0855SAneesh Kumar K.V 331e63e1e5aSBadari Pulavarty /* 332e63e1e5aSBadari Pulavarty * We have the page, copy it to user space buffer. 333e63e1e5aSBadari Pulavarty */ 334c7d57ab1SAl Viro copied = copy_page_to_iter(page, offset, nr, to); 33509cbfeafSKirill A. Shutemov put_page(page); 336e63e1e5aSBadari Pulavarty } 33734d0640eSAl Viro offset += copied; 33834d0640eSAl Viro retval += copied; 33934d0640eSAl Viro if (copied != nr && iov_iter_count(to)) { 34034d0640eSAl Viro if (!retval) 34134d0640eSAl Viro retval = -EFAULT; 342e63e1e5aSBadari Pulavarty break; 343e63e1e5aSBadari Pulavarty } 34434d0640eSAl Viro index += offset >> huge_page_shift(h); 34534d0640eSAl Viro offset &= ~huge_page_mask(h); 34634d0640eSAl Viro } 34734d0640eSAl Viro iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset; 348e63e1e5aSBadari Pulavarty return retval; 349e63e1e5aSBadari Pulavarty } 350e63e1e5aSBadari Pulavarty 351800d15a5SNick Piggin static int hugetlbfs_write_begin(struct file *file, 352800d15a5SNick Piggin struct address_space *mapping, 3539d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 354800d15a5SNick Piggin struct page **pagep, void **fsdata) 3551da177e4SLinus Torvalds { 3561da177e4SLinus Torvalds return -EINVAL; 3571da177e4SLinus Torvalds } 3581da177e4SLinus Torvalds 359800d15a5SNick Piggin static int hugetlbfs_write_end(struct file *file, struct address_space *mapping, 360800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 361800d15a5SNick Piggin struct page *page, void *fsdata) 3621da177e4SLinus Torvalds { 363800d15a5SNick Piggin BUG(); 3641da177e4SLinus Torvalds return -EINVAL; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3677e1813d4SMike Kravetz static void hugetlb_delete_from_page_cache(struct page *page) 3681da177e4SLinus Torvalds { 369b9ea2515SKonstantin Khlebnikov ClearPageDirty(page); 3701da177e4SLinus Torvalds ClearPageUptodate(page); 371bd65cb86SMinchan Kim delete_from_page_cache(page); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 374378397ccSMike Kravetz /* 375378397ccSMike Kravetz * Called with i_mmap_rwsem held for inode based vma maps. This makes 376378397ccSMike Kravetz * sure vma (and vm_mm) will not go away. We also hold the hugetlb fault 377378397ccSMike Kravetz * mutex for the page in the mapping. So, we can not race with page being 378378397ccSMike Kravetz * faulted into the vma. 379378397ccSMike Kravetz */ 380378397ccSMike Kravetz static bool hugetlb_vma_maps_page(struct vm_area_struct *vma, 381378397ccSMike Kravetz unsigned long addr, struct page *page) 382378397ccSMike Kravetz { 383378397ccSMike Kravetz pte_t *ptep, pte; 384378397ccSMike Kravetz 385378397ccSMike Kravetz ptep = huge_pte_offset(vma->vm_mm, addr, 386378397ccSMike Kravetz huge_page_size(hstate_vma(vma))); 387378397ccSMike Kravetz 388378397ccSMike Kravetz if (!ptep) 389378397ccSMike Kravetz return false; 390378397ccSMike Kravetz 391378397ccSMike Kravetz pte = huge_ptep_get(ptep); 392378397ccSMike Kravetz if (huge_pte_none(pte) || !pte_present(pte)) 393378397ccSMike Kravetz return false; 394378397ccSMike Kravetz 395378397ccSMike Kravetz if (pte_page(pte) == page) 396378397ccSMike Kravetz return true; 397378397ccSMike Kravetz 398378397ccSMike Kravetz return false; 399378397ccSMike Kravetz } 400378397ccSMike Kravetz 401378397ccSMike Kravetz /* 402378397ccSMike Kravetz * Can vma_offset_start/vma_offset_end overflow on 32-bit arches? 403378397ccSMike Kravetz * No, because the interval tree returns us only those vmas 404378397ccSMike Kravetz * which overlap the truncated area starting at pgoff, 405378397ccSMike Kravetz * and no vma on a 32-bit arch can span beyond the 4GB. 406378397ccSMike Kravetz */ 407378397ccSMike Kravetz static unsigned long vma_offset_start(struct vm_area_struct *vma, pgoff_t start) 408378397ccSMike Kravetz { 409378397ccSMike Kravetz if (vma->vm_pgoff < start) 410378397ccSMike Kravetz return (start - vma->vm_pgoff) << PAGE_SHIFT; 411378397ccSMike Kravetz else 412378397ccSMike Kravetz return 0; 413378397ccSMike Kravetz } 414378397ccSMike Kravetz 415378397ccSMike Kravetz static unsigned long vma_offset_end(struct vm_area_struct *vma, pgoff_t end) 416378397ccSMike Kravetz { 417378397ccSMike Kravetz unsigned long t_end; 418378397ccSMike Kravetz 419378397ccSMike Kravetz if (!end) 420378397ccSMike Kravetz return vma->vm_end; 421378397ccSMike Kravetz 422378397ccSMike Kravetz t_end = ((end - vma->vm_pgoff) << PAGE_SHIFT) + vma->vm_start; 423378397ccSMike Kravetz if (t_end > vma->vm_end) 424378397ccSMike Kravetz t_end = vma->vm_end; 425378397ccSMike Kravetz return t_end; 426378397ccSMike Kravetz } 427378397ccSMike Kravetz 428378397ccSMike Kravetz /* 429378397ccSMike Kravetz * Called with hugetlb fault mutex held. Therefore, no more mappings to 430378397ccSMike Kravetz * this folio can be created while executing the routine. 431378397ccSMike Kravetz */ 432378397ccSMike Kravetz static void hugetlb_unmap_file_folio(struct hstate *h, 433378397ccSMike Kravetz struct address_space *mapping, 434378397ccSMike Kravetz struct folio *folio, pgoff_t index) 435378397ccSMike Kravetz { 436378397ccSMike Kravetz struct rb_root_cached *root = &mapping->i_mmap; 437*40549ba8SMike Kravetz struct hugetlb_vma_lock *vma_lock; 438378397ccSMike Kravetz struct page *page = &folio->page; 439378397ccSMike Kravetz struct vm_area_struct *vma; 440378397ccSMike Kravetz unsigned long v_start; 441378397ccSMike Kravetz unsigned long v_end; 442378397ccSMike Kravetz pgoff_t start, end; 443378397ccSMike Kravetz 444378397ccSMike Kravetz start = index * pages_per_huge_page(h); 445378397ccSMike Kravetz end = (index + 1) * pages_per_huge_page(h); 446378397ccSMike Kravetz 447378397ccSMike Kravetz i_mmap_lock_write(mapping); 448*40549ba8SMike Kravetz retry: 449*40549ba8SMike Kravetz vma_lock = NULL; 450378397ccSMike Kravetz vma_interval_tree_foreach(vma, root, start, end - 1) { 451378397ccSMike Kravetz v_start = vma_offset_start(vma, start); 452378397ccSMike Kravetz v_end = vma_offset_end(vma, end); 453378397ccSMike Kravetz 454378397ccSMike Kravetz if (!hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page)) 455378397ccSMike Kravetz continue; 456378397ccSMike Kravetz 457*40549ba8SMike Kravetz if (!hugetlb_vma_trylock_write(vma)) { 458*40549ba8SMike Kravetz vma_lock = vma->vm_private_data; 459*40549ba8SMike Kravetz /* 460*40549ba8SMike Kravetz * If we can not get vma lock, we need to drop 461*40549ba8SMike Kravetz * immap_sema and take locks in order. First, 462*40549ba8SMike Kravetz * take a ref on the vma_lock structure so that 463*40549ba8SMike Kravetz * we can be guaranteed it will not go away when 464*40549ba8SMike Kravetz * dropping immap_sema. 465*40549ba8SMike Kravetz */ 466*40549ba8SMike Kravetz kref_get(&vma_lock->refs); 467*40549ba8SMike Kravetz break; 468*40549ba8SMike Kravetz } 469*40549ba8SMike Kravetz 470378397ccSMike Kravetz unmap_hugepage_range(vma, vma->vm_start + v_start, v_end, 471378397ccSMike Kravetz NULL, ZAP_FLAG_DROP_MARKER); 472*40549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 473378397ccSMike Kravetz } 474378397ccSMike Kravetz 475378397ccSMike Kravetz i_mmap_unlock_write(mapping); 476*40549ba8SMike Kravetz 477*40549ba8SMike Kravetz if (vma_lock) { 478*40549ba8SMike Kravetz /* 479*40549ba8SMike Kravetz * Wait on vma_lock. We know it is still valid as we have 480*40549ba8SMike Kravetz * a reference. We must 'open code' vma locking as we do 481*40549ba8SMike Kravetz * not know if vma_lock is still attached to vma. 482*40549ba8SMike Kravetz */ 483*40549ba8SMike Kravetz down_write(&vma_lock->rw_sema); 484*40549ba8SMike Kravetz i_mmap_lock_write(mapping); 485*40549ba8SMike Kravetz 486*40549ba8SMike Kravetz vma = vma_lock->vma; 487*40549ba8SMike Kravetz if (!vma) { 488*40549ba8SMike Kravetz /* 489*40549ba8SMike Kravetz * If lock is no longer attached to vma, then just 490*40549ba8SMike Kravetz * unlock, drop our reference and retry looking for 491*40549ba8SMike Kravetz * other vmas. 492*40549ba8SMike Kravetz */ 493*40549ba8SMike Kravetz up_write(&vma_lock->rw_sema); 494*40549ba8SMike Kravetz kref_put(&vma_lock->refs, hugetlb_vma_lock_release); 495*40549ba8SMike Kravetz goto retry; 496*40549ba8SMike Kravetz } 497*40549ba8SMike Kravetz 498*40549ba8SMike Kravetz /* 499*40549ba8SMike Kravetz * vma_lock is still attached to vma. Check to see if vma 500*40549ba8SMike Kravetz * still maps page and if so, unmap. 501*40549ba8SMike Kravetz */ 502*40549ba8SMike Kravetz v_start = vma_offset_start(vma, start); 503*40549ba8SMike Kravetz v_end = vma_offset_end(vma, end); 504*40549ba8SMike Kravetz if (hugetlb_vma_maps_page(vma, vma->vm_start + v_start, page)) 505*40549ba8SMike Kravetz unmap_hugepage_range(vma, vma->vm_start + v_start, 506*40549ba8SMike Kravetz v_end, NULL, 507*40549ba8SMike Kravetz ZAP_FLAG_DROP_MARKER); 508*40549ba8SMike Kravetz 509*40549ba8SMike Kravetz kref_put(&vma_lock->refs, hugetlb_vma_lock_release); 510*40549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 511*40549ba8SMike Kravetz 512*40549ba8SMike Kravetz goto retry; 513*40549ba8SMike Kravetz } 514378397ccSMike Kravetz } 515378397ccSMike Kravetz 5164aae8d1cSMike Kravetz static void 51705e90bd0SPeter Xu hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end, 51805e90bd0SPeter Xu zap_flags_t zap_flags) 5194aae8d1cSMike Kravetz { 5204aae8d1cSMike Kravetz struct vm_area_struct *vma; 5214aae8d1cSMike Kravetz 5224aae8d1cSMike Kravetz /* 523d6aba4c8SSean Christopherson * end == 0 indicates that the entire range after start should be 524d6aba4c8SSean Christopherson * unmapped. Note, end is exclusive, whereas the interval tree takes 525d6aba4c8SSean Christopherson * an inclusive "last". 5264aae8d1cSMike Kravetz */ 527d6aba4c8SSean Christopherson vma_interval_tree_foreach(vma, root, start, end ? end - 1 : ULONG_MAX) { 528378397ccSMike Kravetz unsigned long v_start; 5294aae8d1cSMike Kravetz unsigned long v_end; 5304aae8d1cSMike Kravetz 531*40549ba8SMike Kravetz if (!hugetlb_vma_trylock_write(vma)) 532*40549ba8SMike Kravetz continue; 533*40549ba8SMike Kravetz 534378397ccSMike Kravetz v_start = vma_offset_start(vma, start); 535378397ccSMike Kravetz v_end = vma_offset_end(vma, end); 5364aae8d1cSMike Kravetz 537378397ccSMike Kravetz unmap_hugepage_range(vma, vma->vm_start + v_start, v_end, 53805e90bd0SPeter Xu NULL, zap_flags); 539*40549ba8SMike Kravetz 540*40549ba8SMike Kravetz /* 541*40549ba8SMike Kravetz * Note that vma lock only exists for shared/non-private 542*40549ba8SMike Kravetz * vmas. Therefore, lock is not held when calling 543*40549ba8SMike Kravetz * unmap_hugepage_range for private vmas. 544*40549ba8SMike Kravetz */ 545*40549ba8SMike Kravetz hugetlb_vma_unlock_write(vma); 5464aae8d1cSMike Kravetz } 5474aae8d1cSMike Kravetz } 548b5cec28dSMike Kravetz 549b5cec28dSMike Kravetz /* 550c8627228SMike Kravetz * Called with hugetlb fault mutex held. 551c8627228SMike Kravetz * Returns true if page was actually removed, false otherwise. 552c8627228SMike Kravetz */ 553c8627228SMike Kravetz static bool remove_inode_single_folio(struct hstate *h, struct inode *inode, 554c8627228SMike Kravetz struct address_space *mapping, 555c8627228SMike Kravetz struct folio *folio, pgoff_t index, 556c8627228SMike Kravetz bool truncate_op) 557c8627228SMike Kravetz { 558c8627228SMike Kravetz bool ret = false; 559c8627228SMike Kravetz 560c8627228SMike Kravetz /* 561c8627228SMike Kravetz * If folio is mapped, it was faulted in after being 562c8627228SMike Kravetz * unmapped in caller. Unmap (again) while holding 563c8627228SMike Kravetz * the fault mutex. The mutex will prevent faults 564c8627228SMike Kravetz * until we finish removing the folio. 565c8627228SMike Kravetz */ 566378397ccSMike Kravetz if (unlikely(folio_mapped(folio))) 567378397ccSMike Kravetz hugetlb_unmap_file_folio(h, mapping, folio, index); 568c8627228SMike Kravetz 569c8627228SMike Kravetz folio_lock(folio); 570c8627228SMike Kravetz /* 571c8627228SMike Kravetz * After locking page, make sure mapping is the same. 572c8627228SMike Kravetz * We could have raced with page fault populate and 573c8627228SMike Kravetz * backout code. 574c8627228SMike Kravetz */ 575c8627228SMike Kravetz if (folio_mapping(folio) == mapping) { 576c8627228SMike Kravetz /* 577c8627228SMike Kravetz * We must remove the folio from page cache before removing 578c8627228SMike Kravetz * the region/ reserve map (hugetlb_unreserve_pages). In 579c8627228SMike Kravetz * rare out of memory conditions, removal of the region/reserve 580c8627228SMike Kravetz * map could fail. Correspondingly, the subpool and global 581c8627228SMike Kravetz * reserve usage count can need to be adjusted. 582c8627228SMike Kravetz */ 583c8627228SMike Kravetz VM_BUG_ON(HPageRestoreReserve(&folio->page)); 584c8627228SMike Kravetz hugetlb_delete_from_page_cache(&folio->page); 585c8627228SMike Kravetz ret = true; 586c8627228SMike Kravetz if (!truncate_op) { 587c8627228SMike Kravetz if (unlikely(hugetlb_unreserve_pages(inode, index, 588c8627228SMike Kravetz index + 1, 1))) 589c8627228SMike Kravetz hugetlb_fix_reserve_counts(inode); 590c8627228SMike Kravetz } 591c8627228SMike Kravetz } 592c8627228SMike Kravetz 593c8627228SMike Kravetz folio_unlock(folio); 594c8627228SMike Kravetz return ret; 595c8627228SMike Kravetz } 596c8627228SMike Kravetz 597c8627228SMike Kravetz /* 598b5cec28dSMike Kravetz * remove_inode_hugepages handles two distinct cases: truncation and hole 599b5cec28dSMike Kravetz * punch. There are subtle differences in operation for each case. 6004aae8d1cSMike Kravetz * 601b5cec28dSMike Kravetz * truncation is indicated by end of range being LLONG_MAX 602b5cec28dSMike Kravetz * In this case, we first scan the range and release found pages. 6031935ebd3SMiaohe Lin * After releasing pages, hugetlb_unreserve_pages cleans up region/reserve 604c8627228SMike Kravetz * maps and global counts. Page faults can race with truncation. 605c8627228SMike Kravetz * During faults, hugetlb_no_page() checks i_size before page allocation, 606c8627228SMike Kravetz * and again after obtaining page table lock. It will 'back out' 607c8627228SMike Kravetz * allocations in the truncated range. 608b5cec28dSMike Kravetz * hole punch is indicated if end is not LLONG_MAX 609b5cec28dSMike Kravetz * In the hole punch case we scan the range and release found pages. 6101935ebd3SMiaohe Lin * Only when releasing a page is the associated region/reserve map 6111935ebd3SMiaohe Lin * deleted. The region/reserve map for ranges without associated 612e7c58097SMike Kravetz * pages are not modified. Page faults can race with hole punch. 613e7c58097SMike Kravetz * This is indicated if we find a mapped page. 614b5cec28dSMike Kravetz * Note: If the passed end of range value is beyond the end of file, but 615b5cec28dSMike Kravetz * not LLONG_MAX this routine still performs a hole punch operation. 616b5cec28dSMike Kravetz */ 617b5cec28dSMike Kravetz static void remove_inode_hugepages(struct inode *inode, loff_t lstart, 618b5cec28dSMike Kravetz loff_t lend) 6191da177e4SLinus Torvalds { 620a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 621b45b5bd6SDavid Gibson struct address_space *mapping = &inode->i_data; 622a5516438SAndi Kleen const pgoff_t start = lstart >> huge_page_shift(h); 623b5cec28dSMike Kravetz const pgoff_t end = lend >> huge_page_shift(h); 6241508062eSMatthew Wilcox (Oracle) struct folio_batch fbatch; 625d72dc8a2SJan Kara pgoff_t next, index; 626a43a8c39SChen, Kenneth W int i, freed = 0; 627b5cec28dSMike Kravetz bool truncate_op = (lend == LLONG_MAX); 6281da177e4SLinus Torvalds 6291508062eSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 6301da177e4SLinus Torvalds next = start; 6311508062eSMatthew Wilcox (Oracle) while (filemap_get_folios(mapping, &next, end - 1, &fbatch)) { 6321508062eSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); ++i) { 6331508062eSMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 634d4241a04SMiaohe Lin u32 hash = 0; 635b5cec28dSMike Kravetz 6361508062eSMatthew Wilcox (Oracle) index = folio->index; 637d4241a04SMiaohe Lin hash = hugetlb_fault_mutex_hash(mapping, index); 638e7c58097SMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 639e7c58097SMike Kravetz 640b5cec28dSMike Kravetz /* 641c8627228SMike Kravetz * Remove folio that was part of folio_batch. 642b5cec28dSMike Kravetz */ 643c8627228SMike Kravetz if (remove_inode_single_folio(h, inode, mapping, folio, 644c8627228SMike Kravetz index, truncate_op)) 645b5cec28dSMike Kravetz freed++; 646b5cec28dSMike Kravetz 647e7c58097SMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 6481da177e4SLinus Torvalds } 6491508062eSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 6501817889eSMike Kravetz cond_resched(); 6511da177e4SLinus Torvalds } 652b5cec28dSMike Kravetz 653b5cec28dSMike Kravetz if (truncate_op) 654b5cec28dSMike Kravetz (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed); 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds 6572bbbda30SAl Viro static void hugetlbfs_evict_inode(struct inode *inode) 6581da177e4SLinus Torvalds { 6599119a41eSJoonsoo Kim struct resv_map *resv_map; 6609119a41eSJoonsoo Kim 661b5cec28dSMike Kravetz remove_inode_hugepages(inode, 0, LLONG_MAX); 662f27a5136SMike Kravetz 663f27a5136SMike Kravetz /* 664f27a5136SMike Kravetz * Get the resv_map from the address space embedded in the inode. 665f27a5136SMike Kravetz * This is the address space which points to any resv_map allocated 666f27a5136SMike Kravetz * at inode creation time. If this is a device special inode, 667f27a5136SMike Kravetz * i_mapping may not point to the original address space. 668f27a5136SMike Kravetz */ 669f27a5136SMike Kravetz resv_map = (struct resv_map *)(&inode->i_data)->private_data; 670f27a5136SMike Kravetz /* Only regular and link inodes have associated reserve maps */ 6719119a41eSJoonsoo Kim if (resv_map) 6729119a41eSJoonsoo Kim resv_map_release(&resv_map->refs); 673dbd5768fSJan Kara clear_inode(inode); 674149f4211SChristoph Hellwig } 675149f4211SChristoph Hellwig 676e5d319deSMiaohe Lin static void hugetlb_vmtruncate(struct inode *inode, loff_t offset) 6771da177e4SLinus Torvalds { 678856fc295SHugh Dickins pgoff_t pgoff; 6791da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 680a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 6811da177e4SLinus Torvalds 682a5516438SAndi Kleen BUG_ON(offset & ~huge_page_mask(h)); 683856fc295SHugh Dickins pgoff = offset >> PAGE_SHIFT; 6841da177e4SLinus Torvalds 68587bf91d3SMike Kravetz i_size_write(inode, offset); 686188a3972SMike Kravetz i_mmap_lock_write(mapping); 687f808c13fSDavidlohr Bueso if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)) 68805e90bd0SPeter Xu hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0, 68905e90bd0SPeter Xu ZAP_FLAG_DROP_MARKER); 690c86aa7bbSMike Kravetz i_mmap_unlock_write(mapping); 691e7c58097SMike Kravetz remove_inode_hugepages(inode, offset, LLONG_MAX); 6921da177e4SLinus Torvalds } 6931da177e4SLinus Torvalds 69468d32527SMike Kravetz static void hugetlbfs_zero_partial_page(struct hstate *h, 69568d32527SMike Kravetz struct address_space *mapping, 69668d32527SMike Kravetz loff_t start, 69768d32527SMike Kravetz loff_t end) 69868d32527SMike Kravetz { 69968d32527SMike Kravetz pgoff_t idx = start >> huge_page_shift(h); 70068d32527SMike Kravetz struct folio *folio; 70168d32527SMike Kravetz 70268d32527SMike Kravetz folio = filemap_lock_folio(mapping, idx); 70368d32527SMike Kravetz if (!folio) 70468d32527SMike Kravetz return; 70568d32527SMike Kravetz 70668d32527SMike Kravetz start = start & ~huge_page_mask(h); 70768d32527SMike Kravetz end = end & ~huge_page_mask(h); 70868d32527SMike Kravetz if (!end) 70968d32527SMike Kravetz end = huge_page_size(h); 71068d32527SMike Kravetz 71168d32527SMike Kravetz folio_zero_segment(folio, (size_t)start, (size_t)end); 71268d32527SMike Kravetz 71368d32527SMike Kravetz folio_unlock(folio); 71468d32527SMike Kravetz folio_put(folio); 71568d32527SMike Kravetz } 71668d32527SMike Kravetz 71770c3547eSMike Kravetz static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len) 71870c3547eSMike Kravetz { 71968d32527SMike Kravetz struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); 72068d32527SMike Kravetz struct address_space *mapping = inode->i_mapping; 72170c3547eSMike Kravetz struct hstate *h = hstate_inode(inode); 72270c3547eSMike Kravetz loff_t hpage_size = huge_page_size(h); 72370c3547eSMike Kravetz loff_t hole_start, hole_end; 72470c3547eSMike Kravetz 72570c3547eSMike Kravetz /* 72668d32527SMike Kravetz * hole_start and hole_end indicate the full pages within the hole. 72770c3547eSMike Kravetz */ 72870c3547eSMike Kravetz hole_start = round_up(offset, hpage_size); 72970c3547eSMike Kravetz hole_end = round_down(offset + len, hpage_size); 73070c3547eSMike Kravetz 7315955102cSAl Viro inode_lock(inode); 732ff62a342SMarc-André Lureau 733398c0da7SMiaohe Lin /* protected by i_rwsem */ 734ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 735ff62a342SMarc-André Lureau inode_unlock(inode); 736ff62a342SMarc-André Lureau return -EPERM; 737ff62a342SMarc-André Lureau } 738ff62a342SMarc-André Lureau 73970c3547eSMike Kravetz i_mmap_lock_write(mapping); 74068d32527SMike Kravetz 74168d32527SMike Kravetz /* If range starts before first full page, zero partial page. */ 74268d32527SMike Kravetz if (offset < hole_start) 74368d32527SMike Kravetz hugetlbfs_zero_partial_page(h, mapping, 74468d32527SMike Kravetz offset, min(offset + len, hole_start)); 74568d32527SMike Kravetz 74668d32527SMike Kravetz /* Unmap users of full pages in the hole. */ 74768d32527SMike Kravetz if (hole_end > hole_start) { 748f808c13fSDavidlohr Bueso if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root)) 74970c3547eSMike Kravetz hugetlb_vmdelete_list(&mapping->i_mmap, 75070c3547eSMike Kravetz hole_start >> PAGE_SHIFT, 75105e90bd0SPeter Xu hole_end >> PAGE_SHIFT, 0); 75270c3547eSMike Kravetz } 75370c3547eSMike Kravetz 75468d32527SMike Kravetz /* If range extends beyond last full page, zero partial page. */ 75568d32527SMike Kravetz if ((offset + len) > hole_end && (offset + len) > hole_start) 75668d32527SMike Kravetz hugetlbfs_zero_partial_page(h, mapping, 75768d32527SMike Kravetz hole_end, offset + len); 75868d32527SMike Kravetz 75968d32527SMike Kravetz i_mmap_unlock_write(mapping); 76068d32527SMike Kravetz 76168d32527SMike Kravetz /* Remove full pages from the file. */ 76268d32527SMike Kravetz if (hole_end > hole_start) 76368d32527SMike Kravetz remove_inode_hugepages(inode, hole_start, hole_end); 76468d32527SMike Kravetz 76568d32527SMike Kravetz inode_unlock(inode); 76668d32527SMike Kravetz 76770c3547eSMike Kravetz return 0; 76870c3547eSMike Kravetz } 76970c3547eSMike Kravetz 77070c3547eSMike Kravetz static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset, 77170c3547eSMike Kravetz loff_t len) 77270c3547eSMike Kravetz { 77370c3547eSMike Kravetz struct inode *inode = file_inode(file); 774ff62a342SMarc-André Lureau struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); 77570c3547eSMike Kravetz struct address_space *mapping = inode->i_mapping; 77670c3547eSMike Kravetz struct hstate *h = hstate_inode(inode); 77770c3547eSMike Kravetz struct vm_area_struct pseudo_vma; 77870c3547eSMike Kravetz struct mm_struct *mm = current->mm; 77970c3547eSMike Kravetz loff_t hpage_size = huge_page_size(h); 78070c3547eSMike Kravetz unsigned long hpage_shift = huge_page_shift(h); 78170c3547eSMike Kravetz pgoff_t start, index, end; 78270c3547eSMike Kravetz int error; 78370c3547eSMike Kravetz u32 hash; 78470c3547eSMike Kravetz 78570c3547eSMike Kravetz if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 78670c3547eSMike Kravetz return -EOPNOTSUPP; 78770c3547eSMike Kravetz 78870c3547eSMike Kravetz if (mode & FALLOC_FL_PUNCH_HOLE) 78970c3547eSMike Kravetz return hugetlbfs_punch_hole(inode, offset, len); 79070c3547eSMike Kravetz 79170c3547eSMike Kravetz /* 79270c3547eSMike Kravetz * Default preallocate case. 79370c3547eSMike Kravetz * For this range, start is rounded down and end is rounded up 79470c3547eSMike Kravetz * as well as being converted to page offsets. 79570c3547eSMike Kravetz */ 79670c3547eSMike Kravetz start = offset >> hpage_shift; 79770c3547eSMike Kravetz end = (offset + len + hpage_size - 1) >> hpage_shift; 79870c3547eSMike Kravetz 7995955102cSAl Viro inode_lock(inode); 80070c3547eSMike Kravetz 80170c3547eSMike Kravetz /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 80270c3547eSMike Kravetz error = inode_newsize_ok(inode, offset + len); 80370c3547eSMike Kravetz if (error) 80470c3547eSMike Kravetz goto out; 80570c3547eSMike Kravetz 806ff62a342SMarc-André Lureau if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 807ff62a342SMarc-André Lureau error = -EPERM; 808ff62a342SMarc-André Lureau goto out; 809ff62a342SMarc-André Lureau } 810ff62a342SMarc-André Lureau 81170c3547eSMike Kravetz /* 81270c3547eSMike Kravetz * Initialize a pseudo vma as this is required by the huge page 81370c3547eSMike Kravetz * allocation routines. If NUMA is configured, use page index 81470c3547eSMike Kravetz * as input to create an allocation policy. 81570c3547eSMike Kravetz */ 8162c4541e2SKirill A. Shutemov vma_init(&pseudo_vma, mm); 81770c3547eSMike Kravetz pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED); 81870c3547eSMike Kravetz pseudo_vma.vm_file = file; 81970c3547eSMike Kravetz 82070c3547eSMike Kravetz for (index = start; index < end; index++) { 82170c3547eSMike Kravetz /* 82270c3547eSMike Kravetz * This is supposed to be the vaddr where the page is being 82370c3547eSMike Kravetz * faulted in, but we have no vaddr here. 82470c3547eSMike Kravetz */ 82570c3547eSMike Kravetz struct page *page; 82670c3547eSMike Kravetz unsigned long addr; 82770c3547eSMike Kravetz 82870c3547eSMike Kravetz cond_resched(); 82970c3547eSMike Kravetz 83070c3547eSMike Kravetz /* 83170c3547eSMike Kravetz * fallocate(2) manpage permits EINTR; we may have been 83270c3547eSMike Kravetz * interrupted because we are using up too much memory. 83370c3547eSMike Kravetz */ 83470c3547eSMike Kravetz if (signal_pending(current)) { 83570c3547eSMike Kravetz error = -EINTR; 83670c3547eSMike Kravetz break; 83770c3547eSMike Kravetz } 83870c3547eSMike Kravetz 83970c3547eSMike Kravetz /* Set numa allocation policy based on index */ 84070c3547eSMike Kravetz hugetlb_set_vma_policy(&pseudo_vma, inode, index); 84170c3547eSMike Kravetz 84270c3547eSMike Kravetz /* addr is the offset within the file (zero based) */ 84370c3547eSMike Kravetz addr = index * hpage_size; 84470c3547eSMike Kravetz 845188a3972SMike Kravetz /* mutex taken here, fault path and hole punch */ 846188b04a7SWei Yang hash = hugetlb_fault_mutex_hash(mapping, index); 84770c3547eSMike Kravetz mutex_lock(&hugetlb_fault_mutex_table[hash]); 84870c3547eSMike Kravetz 84970c3547eSMike Kravetz /* See if already present in mapping to avoid alloc/free */ 85070c3547eSMike Kravetz page = find_get_page(mapping, index); 85170c3547eSMike Kravetz if (page) { 85270c3547eSMike Kravetz put_page(page); 85370c3547eSMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 85470c3547eSMike Kravetz hugetlb_drop_vma_policy(&pseudo_vma); 85570c3547eSMike Kravetz continue; 85670c3547eSMike Kravetz } 85770c3547eSMike Kravetz 85888ce3fefSMiaohe Lin /* 85988ce3fefSMiaohe Lin * Allocate page without setting the avoid_reserve argument. 86088ce3fefSMiaohe Lin * There certainly are no reserves associated with the 86188ce3fefSMiaohe Lin * pseudo_vma. However, there could be shared mappings with 86288ce3fefSMiaohe Lin * reserves for the file at the inode level. If we fallocate 86388ce3fefSMiaohe Lin * pages in these areas, we need to consume the reserves 86488ce3fefSMiaohe Lin * to keep reservation accounting consistent. 86588ce3fefSMiaohe Lin */ 86688ce3fefSMiaohe Lin page = alloc_huge_page(&pseudo_vma, addr, 0); 86770c3547eSMike Kravetz hugetlb_drop_vma_policy(&pseudo_vma); 86870c3547eSMike Kravetz if (IS_ERR(page)) { 86970c3547eSMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 87070c3547eSMike Kravetz error = PTR_ERR(page); 87170c3547eSMike Kravetz goto out; 87270c3547eSMike Kravetz } 87370c3547eSMike Kravetz clear_huge_page(page, addr, pages_per_huge_page(h)); 87470c3547eSMike Kravetz __SetPageUptodate(page); 8757e1813d4SMike Kravetz error = hugetlb_add_to_page_cache(page, mapping, index); 87670c3547eSMike Kravetz if (unlikely(error)) { 877846be085SMike Kravetz restore_reserve_on_error(h, &pseudo_vma, addr, page); 87870c3547eSMike Kravetz put_page(page); 87970c3547eSMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 88070c3547eSMike Kravetz goto out; 88170c3547eSMike Kravetz } 88270c3547eSMike Kravetz 88370c3547eSMike Kravetz mutex_unlock(&hugetlb_fault_mutex_table[hash]); 88470c3547eSMike Kravetz 8858f251a3dSMike Kravetz SetHPageMigratable(page); 88670c3547eSMike Kravetz /* 8877e1813d4SMike Kravetz * unlock_page because locked by hugetlb_add_to_page_cache() 888585fc0d2SMuchun Song * put_page() due to reference from alloc_huge_page() 88970c3547eSMike Kravetz */ 89070c3547eSMike Kravetz unlock_page(page); 89172639e6dSNadav Amit put_page(page); 89270c3547eSMike Kravetz } 89370c3547eSMike Kravetz 89470c3547eSMike Kravetz if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 89570c3547eSMike Kravetz i_size_write(inode, offset + len); 896078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 89770c3547eSMike Kravetz out: 8985955102cSAl Viro inode_unlock(inode); 89970c3547eSMike Kravetz return error; 90070c3547eSMike Kravetz } 90170c3547eSMike Kravetz 902549c7297SChristian Brauner static int hugetlbfs_setattr(struct user_namespace *mnt_userns, 903549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 9041da177e4SLinus Torvalds { 9052b0143b5SDavid Howells struct inode *inode = d_inode(dentry); 906a5516438SAndi Kleen struct hstate *h = hstate_inode(inode); 9071da177e4SLinus Torvalds int error; 9081da177e4SLinus Torvalds unsigned int ia_valid = attr->ia_valid; 909ff62a342SMarc-André Lureau struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); 9101da177e4SLinus Torvalds 9112f221d6fSChristian Brauner error = setattr_prepare(&init_user_ns, dentry, attr); 9121da177e4SLinus Torvalds if (error) 9131025774cSChristoph Hellwig return error; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds if (ia_valid & ATTR_SIZE) { 916ff62a342SMarc-André Lureau loff_t oldsize = inode->i_size; 917ff62a342SMarc-André Lureau loff_t newsize = attr->ia_size; 918ff62a342SMarc-André Lureau 919ff62a342SMarc-André Lureau if (newsize & ~huge_page_mask(h)) 9201025774cSChristoph Hellwig return -EINVAL; 921398c0da7SMiaohe Lin /* protected by i_rwsem */ 922ff62a342SMarc-André Lureau if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 923ff62a342SMarc-André Lureau (newsize > oldsize && (info->seals & F_SEAL_GROW))) 924ff62a342SMarc-André Lureau return -EPERM; 925e5d319deSMiaohe Lin hugetlb_vmtruncate(inode, newsize); 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9282f221d6fSChristian Brauner setattr_copy(&init_user_ns, inode, attr); 9291025774cSChristoph Hellwig mark_inode_dirty(inode); 9301025774cSChristoph Hellwig return 0; 9311025774cSChristoph Hellwig } 9321025774cSChristoph Hellwig 9337d54fa64SAl Viro static struct inode *hugetlbfs_get_root(struct super_block *sb, 93432021982SDavid Howells struct hugetlbfs_fs_context *ctx) 9351da177e4SLinus Torvalds { 9361da177e4SLinus Torvalds struct inode *inode; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds inode = new_inode(sb); 9391da177e4SLinus Torvalds if (inode) { 94085fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 94132021982SDavid Howells inode->i_mode = S_IFDIR | ctx->mode; 94232021982SDavid Howells inode->i_uid = ctx->uid; 94332021982SDavid Howells inode->i_gid = ctx->gid; 944078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9457d54fa64SAl Viro inode->i_op = &hugetlbfs_dir_inode_operations; 9467d54fa64SAl Viro inode->i_fop = &simple_dir_operations; 9477d54fa64SAl Viro /* directory inodes start off with i_nlink == 2 (for "." entry) */ 9487d54fa64SAl Viro inc_nlink(inode); 94965ed7601SAneesh Kumar K.V lockdep_annotate_inode_mutex_key(inode); 9507d54fa64SAl Viro } 9517d54fa64SAl Viro return inode; 9527d54fa64SAl Viro } 9537d54fa64SAl Viro 954b610ded7SMichal Hocko /* 955c8c06efaSDavidlohr Bueso * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never 956b610ded7SMichal Hocko * be taken from reclaim -- unlike regular filesystems. This needs an 95788f306b6SKirill A. Shutemov * annotation because huge_pmd_share() does an allocation under hugetlb's 958c8c06efaSDavidlohr Bueso * i_mmap_rwsem. 959b610ded7SMichal Hocko */ 960c8c06efaSDavidlohr Bueso static struct lock_class_key hugetlbfs_i_mmap_rwsem_key; 961b610ded7SMichal Hocko 9627d54fa64SAl Viro static struct inode *hugetlbfs_get_inode(struct super_block *sb, 9637d54fa64SAl Viro struct inode *dir, 96418df2252SAl Viro umode_t mode, dev_t dev) 9657d54fa64SAl Viro { 9667d54fa64SAl Viro struct inode *inode; 96758b6e5e8SMike Kravetz struct resv_map *resv_map = NULL; 9689119a41eSJoonsoo Kim 96958b6e5e8SMike Kravetz /* 97058b6e5e8SMike Kravetz * Reserve maps are only needed for inodes that can have associated 97158b6e5e8SMike Kravetz * page allocations. 97258b6e5e8SMike Kravetz */ 97358b6e5e8SMike Kravetz if (S_ISREG(mode) || S_ISLNK(mode)) { 9749119a41eSJoonsoo Kim resv_map = resv_map_alloc(); 9759119a41eSJoonsoo Kim if (!resv_map) 9769119a41eSJoonsoo Kim return NULL; 97758b6e5e8SMike Kravetz } 9787d54fa64SAl Viro 9797d54fa64SAl Viro inode = new_inode(sb); 9807d54fa64SAl Viro if (inode) { 981ff62a342SMarc-André Lureau struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode); 982ff62a342SMarc-André Lureau 9837d54fa64SAl Viro inode->i_ino = get_next_ino(); 98421cb47beSChristian Brauner inode_init_owner(&init_user_ns, inode, dir, mode); 985c8c06efaSDavidlohr Bueso lockdep_set_class(&inode->i_mapping->i_mmap_rwsem, 986c8c06efaSDavidlohr Bueso &hugetlbfs_i_mmap_rwsem_key); 9871da177e4SLinus Torvalds inode->i_mapping->a_ops = &hugetlbfs_aops; 988078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 9899119a41eSJoonsoo Kim inode->i_mapping->private_data = resv_map; 990ff62a342SMarc-André Lureau info->seals = F_SEAL_SEAL; 9911da177e4SLinus Torvalds switch (mode & S_IFMT) { 9921da177e4SLinus Torvalds default: 9931da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 9941da177e4SLinus Torvalds break; 9951da177e4SLinus Torvalds case S_IFREG: 9961da177e4SLinus Torvalds inode->i_op = &hugetlbfs_inode_operations; 9971da177e4SLinus Torvalds inode->i_fop = &hugetlbfs_file_operations; 9981da177e4SLinus Torvalds break; 9991da177e4SLinus Torvalds case S_IFDIR: 10001da177e4SLinus Torvalds inode->i_op = &hugetlbfs_dir_inode_operations; 10011da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 10021da177e4SLinus Torvalds 10031da177e4SLinus Torvalds /* directory inodes start off with i_nlink == 2 (for "." entry) */ 1004d8c76e6fSDave Hansen inc_nlink(inode); 10051da177e4SLinus Torvalds break; 10061da177e4SLinus Torvalds case S_IFLNK: 10071da177e4SLinus Torvalds inode->i_op = &page_symlink_inode_operations; 100821fc61c7SAl Viro inode_nohighmem(inode); 10091da177e4SLinus Torvalds break; 10101da177e4SLinus Torvalds } 1011e096d0c7SJosh Boyer lockdep_annotate_inode_mutex_key(inode); 101258b6e5e8SMike Kravetz } else { 101358b6e5e8SMike Kravetz if (resv_map) 10149119a41eSJoonsoo Kim kref_put(&resv_map->refs, resv_map_release); 101558b6e5e8SMike Kravetz } 10169119a41eSJoonsoo Kim 10171da177e4SLinus Torvalds return inode; 10181da177e4SLinus Torvalds } 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds /* 10211da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 10221da177e4SLinus Torvalds */ 10231ab5b82fSPiotr Sarna static int do_hugetlbfs_mknod(struct inode *dir, 10241ab5b82fSPiotr Sarna struct dentry *dentry, 10251ab5b82fSPiotr Sarna umode_t mode, 10261ab5b82fSPiotr Sarna dev_t dev, 10271ab5b82fSPiotr Sarna bool tmpfile) 10281da177e4SLinus Torvalds { 10291da177e4SLinus Torvalds struct inode *inode; 10301da177e4SLinus Torvalds int error = -ENOSPC; 10311da177e4SLinus Torvalds 10327d54fa64SAl Viro inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev); 10331da177e4SLinus Torvalds if (inode) { 1034078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 10351ab5b82fSPiotr Sarna if (tmpfile) { 10361ab5b82fSPiotr Sarna d_tmpfile(dentry, inode); 10371ab5b82fSPiotr Sarna } else { 10381da177e4SLinus Torvalds d_instantiate(dentry, inode); 10391da177e4SLinus Torvalds dget(dentry);/* Extra count - pin the dentry in core */ 10401ab5b82fSPiotr Sarna } 10411da177e4SLinus Torvalds error = 0; 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds return error; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 1046549c7297SChristian Brauner static int hugetlbfs_mknod(struct user_namespace *mnt_userns, struct inode *dir, 10471ab5b82fSPiotr Sarna struct dentry *dentry, umode_t mode, dev_t dev) 10481ab5b82fSPiotr Sarna { 10491ab5b82fSPiotr Sarna return do_hugetlbfs_mknod(dir, dentry, mode, dev, false); 10501ab5b82fSPiotr Sarna } 10511ab5b82fSPiotr Sarna 1052549c7297SChristian Brauner static int hugetlbfs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 1053549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 10541da177e4SLinus Torvalds { 1055549c7297SChristian Brauner int retval = hugetlbfs_mknod(&init_user_ns, dir, dentry, 1056549c7297SChristian Brauner mode | S_IFDIR, 0); 10571da177e4SLinus Torvalds if (!retval) 1058d8c76e6fSDave Hansen inc_nlink(dir); 10591da177e4SLinus Torvalds return retval; 10601da177e4SLinus Torvalds } 10611da177e4SLinus Torvalds 1062549c7297SChristian Brauner static int hugetlbfs_create(struct user_namespace *mnt_userns, 1063549c7297SChristian Brauner struct inode *dir, struct dentry *dentry, 1064549c7297SChristian Brauner umode_t mode, bool excl) 10651da177e4SLinus Torvalds { 1066549c7297SChristian Brauner return hugetlbfs_mknod(&init_user_ns, dir, dentry, mode | S_IFREG, 0); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 1069549c7297SChristian Brauner static int hugetlbfs_tmpfile(struct user_namespace *mnt_userns, 1070549c7297SChristian Brauner struct inode *dir, struct dentry *dentry, 1071549c7297SChristian Brauner umode_t mode) 10721ab5b82fSPiotr Sarna { 10731ab5b82fSPiotr Sarna return do_hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0, true); 10741ab5b82fSPiotr Sarna } 10751ab5b82fSPiotr Sarna 1076549c7297SChristian Brauner static int hugetlbfs_symlink(struct user_namespace *mnt_userns, 1077549c7297SChristian Brauner struct inode *dir, struct dentry *dentry, 1078549c7297SChristian Brauner const char *symname) 10791da177e4SLinus Torvalds { 10801da177e4SLinus Torvalds struct inode *inode; 10811da177e4SLinus Torvalds int error = -ENOSPC; 10821da177e4SLinus Torvalds 10837d54fa64SAl Viro inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0); 10841da177e4SLinus Torvalds if (inode) { 10851da177e4SLinus Torvalds int l = strlen(symname)+1; 10861da177e4SLinus Torvalds error = page_symlink(inode, symname, l); 10871da177e4SLinus Torvalds if (!error) { 10881da177e4SLinus Torvalds d_instantiate(dentry, inode); 10891da177e4SLinus Torvalds dget(dentry); 10901da177e4SLinus Torvalds } else 10911da177e4SLinus Torvalds iput(inode); 10921da177e4SLinus Torvalds } 1093078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds return error; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 1098b890ec2aSMatthew Wilcox (Oracle) #ifdef CONFIG_MIGRATION 1099b890ec2aSMatthew Wilcox (Oracle) static int hugetlbfs_migrate_folio(struct address_space *mapping, 1100b890ec2aSMatthew Wilcox (Oracle) struct folio *dst, struct folio *src, 1101a6bc32b8SMel Gorman enum migrate_mode mode) 1102290408d4SNaoya Horiguchi { 1103290408d4SNaoya Horiguchi int rc; 1104290408d4SNaoya Horiguchi 1105b890ec2aSMatthew Wilcox (Oracle) rc = migrate_huge_page_move_mapping(mapping, dst, src); 110678bd5209SRafael Aquini if (rc != MIGRATEPAGE_SUCCESS) 1107290408d4SNaoya Horiguchi return rc; 1108cb6acd01SMike Kravetz 1109b890ec2aSMatthew Wilcox (Oracle) if (hugetlb_page_subpool(&src->page)) { 1110b890ec2aSMatthew Wilcox (Oracle) hugetlb_set_page_subpool(&dst->page, 1111b890ec2aSMatthew Wilcox (Oracle) hugetlb_page_subpool(&src->page)); 1112b890ec2aSMatthew Wilcox (Oracle) hugetlb_set_page_subpool(&src->page, NULL); 1113cb6acd01SMike Kravetz } 1114cb6acd01SMike Kravetz 11152916ecc0SJérôme Glisse if (mode != MIGRATE_SYNC_NO_COPY) 1116b890ec2aSMatthew Wilcox (Oracle) folio_migrate_copy(dst, src); 11172916ecc0SJérôme Glisse else 1118b890ec2aSMatthew Wilcox (Oracle) folio_migrate_flags(dst, src); 1119290408d4SNaoya Horiguchi 112078bd5209SRafael Aquini return MIGRATEPAGE_SUCCESS; 1121290408d4SNaoya Horiguchi } 1122b890ec2aSMatthew Wilcox (Oracle) #else 1123b890ec2aSMatthew Wilcox (Oracle) #define hugetlbfs_migrate_folio NULL 1124b890ec2aSMatthew Wilcox (Oracle) #endif 1125290408d4SNaoya Horiguchi 112678bb9203SNaoya Horiguchi static int hugetlbfs_error_remove_page(struct address_space *mapping, 112778bb9203SNaoya Horiguchi struct page *page) 112878bb9203SNaoya Horiguchi { 112978bb9203SNaoya Horiguchi struct inode *inode = mapping->host; 1130ab615a5bSMike Kravetz pgoff_t index = page->index; 113178bb9203SNaoya Horiguchi 11327e1813d4SMike Kravetz hugetlb_delete_from_page_cache(page); 1133ab615a5bSMike Kravetz if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1))) 113478bb9203SNaoya Horiguchi hugetlb_fix_reserve_counts(inode); 1135ab615a5bSMike Kravetz 113678bb9203SNaoya Horiguchi return 0; 113778bb9203SNaoya Horiguchi } 113878bb9203SNaoya Horiguchi 11394a25220dSDavid Howells /* 11404a25220dSDavid Howells * Display the mount options in /proc/mounts. 11414a25220dSDavid Howells */ 11424a25220dSDavid Howells static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root) 11434a25220dSDavid Howells { 11444a25220dSDavid Howells struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb); 11454a25220dSDavid Howells struct hugepage_subpool *spool = sbinfo->spool; 11464a25220dSDavid Howells unsigned long hpage_size = huge_page_size(sbinfo->hstate); 11474a25220dSDavid Howells unsigned hpage_shift = huge_page_shift(sbinfo->hstate); 11484a25220dSDavid Howells char mod; 11494a25220dSDavid Howells 11504a25220dSDavid Howells if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 11514a25220dSDavid Howells seq_printf(m, ",uid=%u", 11524a25220dSDavid Howells from_kuid_munged(&init_user_ns, sbinfo->uid)); 11534a25220dSDavid Howells if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 11544a25220dSDavid Howells seq_printf(m, ",gid=%u", 11554a25220dSDavid Howells from_kgid_munged(&init_user_ns, sbinfo->gid)); 11564a25220dSDavid Howells if (sbinfo->mode != 0755) 11574a25220dSDavid Howells seq_printf(m, ",mode=%o", sbinfo->mode); 11584a25220dSDavid Howells if (sbinfo->max_inodes != -1) 11594a25220dSDavid Howells seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes); 11604a25220dSDavid Howells 11614a25220dSDavid Howells hpage_size /= 1024; 11624a25220dSDavid Howells mod = 'K'; 11634a25220dSDavid Howells if (hpage_size >= 1024) { 11644a25220dSDavid Howells hpage_size /= 1024; 11654a25220dSDavid Howells mod = 'M'; 11664a25220dSDavid Howells } 11674a25220dSDavid Howells seq_printf(m, ",pagesize=%lu%c", hpage_size, mod); 11684a25220dSDavid Howells if (spool) { 11694a25220dSDavid Howells if (spool->max_hpages != -1) 11704a25220dSDavid Howells seq_printf(m, ",size=%llu", 11714a25220dSDavid Howells (unsigned long long)spool->max_hpages << hpage_shift); 11724a25220dSDavid Howells if (spool->min_hpages != -1) 11734a25220dSDavid Howells seq_printf(m, ",min_size=%llu", 11744a25220dSDavid Howells (unsigned long long)spool->min_hpages << hpage_shift); 11754a25220dSDavid Howells } 11764a25220dSDavid Howells return 0; 11774a25220dSDavid Howells } 11784a25220dSDavid Howells 1179726c3342SDavid Howells static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf) 11801da177e4SLinus Torvalds { 1181726c3342SDavid Howells struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb); 11822b0143b5SDavid Howells struct hstate *h = hstate_inode(d_inode(dentry)); 11831da177e4SLinus Torvalds 11841da177e4SLinus Torvalds buf->f_type = HUGETLBFS_MAGIC; 1185a5516438SAndi Kleen buf->f_bsize = huge_page_size(h); 11861da177e4SLinus Torvalds if (sbinfo) { 11871da177e4SLinus Torvalds spin_lock(&sbinfo->stat_lock); 118811680763SMiaohe Lin /* If no limits set, just report 0 or -1 for max/free/used 118974a8a65cSDavid Gibson * blocks, like simple_statfs() */ 119090481622SDavid Gibson if (sbinfo->spool) { 119190481622SDavid Gibson long free_pages; 119290481622SDavid Gibson 11934b25f030SMina Almasry spin_lock_irq(&sbinfo->spool->lock); 119490481622SDavid Gibson buf->f_blocks = sbinfo->spool->max_hpages; 119590481622SDavid Gibson free_pages = sbinfo->spool->max_hpages 119690481622SDavid Gibson - sbinfo->spool->used_hpages; 119790481622SDavid Gibson buf->f_bavail = buf->f_bfree = free_pages; 11984b25f030SMina Almasry spin_unlock_irq(&sbinfo->spool->lock); 11991da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 12001da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 120174a8a65cSDavid Gibson } 12021da177e4SLinus Torvalds spin_unlock(&sbinfo->stat_lock); 12031da177e4SLinus Torvalds } 12041da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 12051da177e4SLinus Torvalds return 0; 12061da177e4SLinus Torvalds } 12071da177e4SLinus Torvalds 12081da177e4SLinus Torvalds static void hugetlbfs_put_super(struct super_block *sb) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb); 12111da177e4SLinus Torvalds 12121da177e4SLinus Torvalds if (sbi) { 12131da177e4SLinus Torvalds sb->s_fs_info = NULL; 121490481622SDavid Gibson 121590481622SDavid Gibson if (sbi->spool) 121690481622SDavid Gibson hugepage_put_subpool(sbi->spool); 121790481622SDavid Gibson 12181da177e4SLinus Torvalds kfree(sbi); 12191da177e4SLinus Torvalds } 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 122296527980SChristoph Hellwig static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo) 122396527980SChristoph Hellwig { 122496527980SChristoph Hellwig if (sbinfo->free_inodes >= 0) { 122596527980SChristoph Hellwig spin_lock(&sbinfo->stat_lock); 122696527980SChristoph Hellwig if (unlikely(!sbinfo->free_inodes)) { 122796527980SChristoph Hellwig spin_unlock(&sbinfo->stat_lock); 122896527980SChristoph Hellwig return 0; 122996527980SChristoph Hellwig } 123096527980SChristoph Hellwig sbinfo->free_inodes--; 123196527980SChristoph Hellwig spin_unlock(&sbinfo->stat_lock); 123296527980SChristoph Hellwig } 123396527980SChristoph Hellwig 123496527980SChristoph Hellwig return 1; 123596527980SChristoph Hellwig } 123696527980SChristoph Hellwig 123796527980SChristoph Hellwig static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo) 123896527980SChristoph Hellwig { 123996527980SChristoph Hellwig if (sbinfo->free_inodes >= 0) { 124096527980SChristoph Hellwig spin_lock(&sbinfo->stat_lock); 124196527980SChristoph Hellwig sbinfo->free_inodes++; 124296527980SChristoph Hellwig spin_unlock(&sbinfo->stat_lock); 124396527980SChristoph Hellwig } 124496527980SChristoph Hellwig } 124596527980SChristoph Hellwig 124696527980SChristoph Hellwig 1247e18b890bSChristoph Lameter static struct kmem_cache *hugetlbfs_inode_cachep; 12481da177e4SLinus Torvalds 12491da177e4SLinus Torvalds static struct inode *hugetlbfs_alloc_inode(struct super_block *sb) 12501da177e4SLinus Torvalds { 125196527980SChristoph Hellwig struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb); 12521da177e4SLinus Torvalds struct hugetlbfs_inode_info *p; 12531da177e4SLinus Torvalds 125496527980SChristoph Hellwig if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo))) 12551da177e4SLinus Torvalds return NULL; 1256fd60b288SMuchun Song p = alloc_inode_sb(sb, hugetlbfs_inode_cachep, GFP_KERNEL); 125796527980SChristoph Hellwig if (unlikely(!p)) { 125896527980SChristoph Hellwig hugetlbfs_inc_free_inodes(sbinfo); 125996527980SChristoph Hellwig return NULL; 12601da177e4SLinus Torvalds } 12614742a35dSMike Kravetz 12624742a35dSMike Kravetz /* 12634742a35dSMike Kravetz * Any time after allocation, hugetlbfs_destroy_inode can be called 12644742a35dSMike Kravetz * for the inode. mpol_free_shared_policy is unconditionally called 12654742a35dSMike Kravetz * as part of hugetlbfs_destroy_inode. So, initialize policy here 12664742a35dSMike Kravetz * in case of a quick call to destroy. 12674742a35dSMike Kravetz * 12684742a35dSMike Kravetz * Note that the policy is initialized even if we are creating a 12694742a35dSMike Kravetz * private inode. This simplifies hugetlbfs_destroy_inode. 12704742a35dSMike Kravetz */ 12714742a35dSMike Kravetz mpol_shared_policy_init(&p->policy, NULL); 12724742a35dSMike Kravetz 127396527980SChristoph Hellwig return &p->vfs_inode; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 1276b62de322SAl Viro static void hugetlbfs_free_inode(struct inode *inode) 1277fa0d7e3dSNick Piggin { 1278fa0d7e3dSNick Piggin kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode)); 1279fa0d7e3dSNick Piggin } 1280fa0d7e3dSNick Piggin 12811da177e4SLinus Torvalds static void hugetlbfs_destroy_inode(struct inode *inode) 12821da177e4SLinus Torvalds { 128396527980SChristoph Hellwig hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb)); 12841da177e4SLinus Torvalds mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy); 12851da177e4SLinus Torvalds } 12861da177e4SLinus Torvalds 1287f5e54d6eSChristoph Hellwig static const struct address_space_operations hugetlbfs_aops = { 1288800d15a5SNick Piggin .write_begin = hugetlbfs_write_begin, 1289800d15a5SNick Piggin .write_end = hugetlbfs_write_end, 129046de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 1291b890ec2aSMatthew Wilcox (Oracle) .migrate_folio = hugetlbfs_migrate_folio, 129278bb9203SNaoya Horiguchi .error_remove_page = hugetlbfs_error_remove_page, 12931da177e4SLinus Torvalds }; 12941da177e4SLinus Torvalds 129596527980SChristoph Hellwig 129651cc5068SAlexey Dobriyan static void init_once(void *foo) 129796527980SChristoph Hellwig { 129896527980SChristoph Hellwig struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo; 129996527980SChristoph Hellwig 130096527980SChristoph Hellwig inode_init_once(&ei->vfs_inode); 130196527980SChristoph Hellwig } 130296527980SChristoph Hellwig 13034b6f5d20SArjan van de Ven const struct file_operations hugetlbfs_file_operations = { 130434d0640eSAl Viro .read_iter = hugetlbfs_read_iter, 13051da177e4SLinus Torvalds .mmap = hugetlbfs_file_mmap, 13061b061d92SChristoph Hellwig .fsync = noop_fsync, 13071da177e4SLinus Torvalds .get_unmapped_area = hugetlb_get_unmapped_area, 13086038f373SArnd Bergmann .llseek = default_llseek, 130970c3547eSMike Kravetz .fallocate = hugetlbfs_fallocate, 13101da177e4SLinus Torvalds }; 13111da177e4SLinus Torvalds 131292e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_dir_inode_operations = { 13131da177e4SLinus Torvalds .create = hugetlbfs_create, 13141da177e4SLinus Torvalds .lookup = simple_lookup, 13151da177e4SLinus Torvalds .link = simple_link, 13161da177e4SLinus Torvalds .unlink = simple_unlink, 13171da177e4SLinus Torvalds .symlink = hugetlbfs_symlink, 13181da177e4SLinus Torvalds .mkdir = hugetlbfs_mkdir, 13191da177e4SLinus Torvalds .rmdir = simple_rmdir, 13201da177e4SLinus Torvalds .mknod = hugetlbfs_mknod, 13211da177e4SLinus Torvalds .rename = simple_rename, 13221da177e4SLinus Torvalds .setattr = hugetlbfs_setattr, 13231ab5b82fSPiotr Sarna .tmpfile = hugetlbfs_tmpfile, 13241da177e4SLinus Torvalds }; 13251da177e4SLinus Torvalds 132692e1d5beSArjan van de Ven static const struct inode_operations hugetlbfs_inode_operations = { 13271da177e4SLinus Torvalds .setattr = hugetlbfs_setattr, 13281da177e4SLinus Torvalds }; 13291da177e4SLinus Torvalds 1330ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hugetlbfs_ops = { 13311da177e4SLinus Torvalds .alloc_inode = hugetlbfs_alloc_inode, 1332b62de322SAl Viro .free_inode = hugetlbfs_free_inode, 13331da177e4SLinus Torvalds .destroy_inode = hugetlbfs_destroy_inode, 13342bbbda30SAl Viro .evict_inode = hugetlbfs_evict_inode, 13351da177e4SLinus Torvalds .statfs = hugetlbfs_statfs, 13361da177e4SLinus Torvalds .put_super = hugetlbfs_put_super, 13374a25220dSDavid Howells .show_options = hugetlbfs_show_options, 13381da177e4SLinus Torvalds }; 13391da177e4SLinus Torvalds 13407ca02d0aSMike Kravetz /* 13417ca02d0aSMike Kravetz * Convert size option passed from command line to number of huge pages 13427ca02d0aSMike Kravetz * in the pool specified by hstate. Size option could be in bytes 13437ca02d0aSMike Kravetz * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT). 13447ca02d0aSMike Kravetz */ 13454a25220dSDavid Howells static long 13467ca02d0aSMike Kravetz hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt, 13474a25220dSDavid Howells enum hugetlbfs_size_type val_type) 13487ca02d0aSMike Kravetz { 13497ca02d0aSMike Kravetz if (val_type == NO_SIZE) 13507ca02d0aSMike Kravetz return -1; 13517ca02d0aSMike Kravetz 13527ca02d0aSMike Kravetz if (val_type == SIZE_PERCENT) { 13537ca02d0aSMike Kravetz size_opt <<= huge_page_shift(h); 13547ca02d0aSMike Kravetz size_opt *= h->max_huge_pages; 13557ca02d0aSMike Kravetz do_div(size_opt, 100); 13567ca02d0aSMike Kravetz } 13577ca02d0aSMike Kravetz 13587ca02d0aSMike Kravetz size_opt >>= huge_page_shift(h); 13597ca02d0aSMike Kravetz return size_opt; 13607ca02d0aSMike Kravetz } 13617ca02d0aSMike Kravetz 136232021982SDavid Howells /* 136332021982SDavid Howells * Parse one mount parameter. 136432021982SDavid Howells */ 136532021982SDavid Howells static int hugetlbfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 13661da177e4SLinus Torvalds { 136732021982SDavid Howells struct hugetlbfs_fs_context *ctx = fc->fs_private; 136832021982SDavid Howells struct fs_parse_result result; 136932021982SDavid Howells char *rest; 137032021982SDavid Howells unsigned long ps; 137132021982SDavid Howells int opt; 13721da177e4SLinus Torvalds 1373d7167b14SAl Viro opt = fs_parse(fc, hugetlb_fs_parameters, param, &result); 137432021982SDavid Howells if (opt < 0) 137532021982SDavid Howells return opt; 137632021982SDavid Howells 137732021982SDavid Howells switch (opt) { 137832021982SDavid Howells case Opt_uid: 137932021982SDavid Howells ctx->uid = make_kuid(current_user_ns(), result.uint_32); 138032021982SDavid Howells if (!uid_valid(ctx->uid)) 138132021982SDavid Howells goto bad_val; 13821da177e4SLinus Torvalds return 0; 13831da177e4SLinus Torvalds 1384e73a75faSRandy Dunlap case Opt_gid: 138532021982SDavid Howells ctx->gid = make_kgid(current_user_ns(), result.uint_32); 138632021982SDavid Howells if (!gid_valid(ctx->gid)) 1387e73a75faSRandy Dunlap goto bad_val; 138832021982SDavid Howells return 0; 1389e73a75faSRandy Dunlap 1390e73a75faSRandy Dunlap case Opt_mode: 139132021982SDavid Howells ctx->mode = result.uint_32 & 01777U; 139232021982SDavid Howells return 0; 1393e73a75faSRandy Dunlap 139432021982SDavid Howells case Opt_size: 1395e73a75faSRandy Dunlap /* memparse() will accept a K/M/G without a digit */ 139632021982SDavid Howells if (!isdigit(param->string[0])) 1397e73a75faSRandy Dunlap goto bad_val; 139832021982SDavid Howells ctx->max_size_opt = memparse(param->string, &rest); 139932021982SDavid Howells ctx->max_val_type = SIZE_STD; 1400a137e1ccSAndi Kleen if (*rest == '%') 140132021982SDavid Howells ctx->max_val_type = SIZE_PERCENT; 140232021982SDavid Howells return 0; 14031da177e4SLinus Torvalds 1404e73a75faSRandy Dunlap case Opt_nr_inodes: 1405e73a75faSRandy Dunlap /* memparse() will accept a K/M/G without a digit */ 140632021982SDavid Howells if (!isdigit(param->string[0])) 1407e73a75faSRandy Dunlap goto bad_val; 140832021982SDavid Howells ctx->nr_inodes = memparse(param->string, &rest); 140932021982SDavid Howells return 0; 1410e73a75faSRandy Dunlap 141132021982SDavid Howells case Opt_pagesize: 141232021982SDavid Howells ps = memparse(param->string, &rest); 141332021982SDavid Howells ctx->hstate = size_to_hstate(ps); 141432021982SDavid Howells if (!ctx->hstate) { 1415d0036517SMiaohe Lin pr_err("Unsupported page size %lu MB\n", ps / SZ_1M); 1416a137e1ccSAndi Kleen return -EINVAL; 1417a137e1ccSAndi Kleen } 141832021982SDavid Howells return 0; 1419a137e1ccSAndi Kleen 142032021982SDavid Howells case Opt_min_size: 14217ca02d0aSMike Kravetz /* memparse() will accept a K/M/G without a digit */ 142232021982SDavid Howells if (!isdigit(param->string[0])) 14237ca02d0aSMike Kravetz goto bad_val; 142432021982SDavid Howells ctx->min_size_opt = memparse(param->string, &rest); 142532021982SDavid Howells ctx->min_val_type = SIZE_STD; 14267ca02d0aSMike Kravetz if (*rest == '%') 142732021982SDavid Howells ctx->min_val_type = SIZE_PERCENT; 142832021982SDavid Howells return 0; 14297ca02d0aSMike Kravetz 1430e73a75faSRandy Dunlap default: 1431b4c07bceSLee Schermerhorn return -EINVAL; 1432e73a75faSRandy Dunlap } 143332021982SDavid Howells 143432021982SDavid Howells bad_val: 1435b5db30cfSAl Viro return invalfc(fc, "Bad value '%s' for mount option '%s'\n", 143632021982SDavid Howells param->string, param->key); 14371da177e4SLinus Torvalds } 1438a137e1ccSAndi Kleen 14397ca02d0aSMike Kravetz /* 144032021982SDavid Howells * Validate the parsed options. 144132021982SDavid Howells */ 144232021982SDavid Howells static int hugetlbfs_validate(struct fs_context *fc) 144332021982SDavid Howells { 144432021982SDavid Howells struct hugetlbfs_fs_context *ctx = fc->fs_private; 144532021982SDavid Howells 144632021982SDavid Howells /* 14477ca02d0aSMike Kravetz * Use huge page pool size (in hstate) to convert the size 14487ca02d0aSMike Kravetz * options to number of huge pages. If NO_SIZE, -1 is returned. 14497ca02d0aSMike Kravetz */ 145032021982SDavid Howells ctx->max_hpages = hugetlbfs_size_to_hpages(ctx->hstate, 145132021982SDavid Howells ctx->max_size_opt, 145232021982SDavid Howells ctx->max_val_type); 145332021982SDavid Howells ctx->min_hpages = hugetlbfs_size_to_hpages(ctx->hstate, 145432021982SDavid Howells ctx->min_size_opt, 145532021982SDavid Howells ctx->min_val_type); 14567ca02d0aSMike Kravetz 14577ca02d0aSMike Kravetz /* 14587ca02d0aSMike Kravetz * If max_size was specified, then min_size must be smaller 14597ca02d0aSMike Kravetz */ 146032021982SDavid Howells if (ctx->max_val_type > NO_SIZE && 146132021982SDavid Howells ctx->min_hpages > ctx->max_hpages) { 146232021982SDavid Howells pr_err("Minimum size can not be greater than maximum size\n"); 14637ca02d0aSMike Kravetz return -EINVAL; 1464a137e1ccSAndi Kleen } 1465a137e1ccSAndi Kleen 14661da177e4SLinus Torvalds return 0; 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14691da177e4SLinus Torvalds static int 147032021982SDavid Howells hugetlbfs_fill_super(struct super_block *sb, struct fs_context *fc) 14711da177e4SLinus Torvalds { 147232021982SDavid Howells struct hugetlbfs_fs_context *ctx = fc->fs_private; 14731da177e4SLinus Torvalds struct hugetlbfs_sb_info *sbinfo; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL); 14761da177e4SLinus Torvalds if (!sbinfo) 14771da177e4SLinus Torvalds return -ENOMEM; 14781da177e4SLinus Torvalds sb->s_fs_info = sbinfo; 14791da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 148032021982SDavid Howells sbinfo->hstate = ctx->hstate; 148132021982SDavid Howells sbinfo->max_inodes = ctx->nr_inodes; 148232021982SDavid Howells sbinfo->free_inodes = ctx->nr_inodes; 148390481622SDavid Gibson sbinfo->spool = NULL; 148432021982SDavid Howells sbinfo->uid = ctx->uid; 148532021982SDavid Howells sbinfo->gid = ctx->gid; 148632021982SDavid Howells sbinfo->mode = ctx->mode; 14874a25220dSDavid Howells 14887ca02d0aSMike Kravetz /* 14897ca02d0aSMike Kravetz * Allocate and initialize subpool if maximum or minimum size is 14901935ebd3SMiaohe Lin * specified. Any needed reservations (for minimum size) are taken 1491445c8098SMiaohe Lin * when the subpool is created. 14927ca02d0aSMike Kravetz */ 149332021982SDavid Howells if (ctx->max_hpages != -1 || ctx->min_hpages != -1) { 149432021982SDavid Howells sbinfo->spool = hugepage_new_subpool(ctx->hstate, 149532021982SDavid Howells ctx->max_hpages, 149632021982SDavid Howells ctx->min_hpages); 149790481622SDavid Gibson if (!sbinfo->spool) 149890481622SDavid Gibson goto out_free; 149990481622SDavid Gibson } 15001da177e4SLinus Torvalds sb->s_maxbytes = MAX_LFS_FILESIZE; 150132021982SDavid Howells sb->s_blocksize = huge_page_size(ctx->hstate); 150232021982SDavid Howells sb->s_blocksize_bits = huge_page_shift(ctx->hstate); 15031da177e4SLinus Torvalds sb->s_magic = HUGETLBFS_MAGIC; 15041da177e4SLinus Torvalds sb->s_op = &hugetlbfs_ops; 15051da177e4SLinus Torvalds sb->s_time_gran = 1; 150615568299SMike Kravetz 150715568299SMike Kravetz /* 150815568299SMike Kravetz * Due to the special and limited functionality of hugetlbfs, it does 150915568299SMike Kravetz * not work well as a stacking filesystem. 151015568299SMike Kravetz */ 151115568299SMike Kravetz sb->s_stack_depth = FILESYSTEM_MAX_STACK_DEPTH; 151232021982SDavid Howells sb->s_root = d_make_root(hugetlbfs_get_root(sb, ctx)); 151348fde701SAl Viro if (!sb->s_root) 15141da177e4SLinus Torvalds goto out_free; 15151da177e4SLinus Torvalds return 0; 15161da177e4SLinus Torvalds out_free: 151790481622SDavid Gibson kfree(sbinfo->spool); 15181da177e4SLinus Torvalds kfree(sbinfo); 15191da177e4SLinus Torvalds return -ENOMEM; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 152232021982SDavid Howells static int hugetlbfs_get_tree(struct fs_context *fc) 15231da177e4SLinus Torvalds { 152432021982SDavid Howells int err = hugetlbfs_validate(fc); 152532021982SDavid Howells if (err) 152632021982SDavid Howells return err; 15272ac295d4SAl Viro return get_tree_nodev(fc, hugetlbfs_fill_super); 152832021982SDavid Howells } 152932021982SDavid Howells 153032021982SDavid Howells static void hugetlbfs_fs_context_free(struct fs_context *fc) 153132021982SDavid Howells { 153232021982SDavid Howells kfree(fc->fs_private); 153332021982SDavid Howells } 153432021982SDavid Howells 153532021982SDavid Howells static const struct fs_context_operations hugetlbfs_fs_context_ops = { 153632021982SDavid Howells .free = hugetlbfs_fs_context_free, 153732021982SDavid Howells .parse_param = hugetlbfs_parse_param, 153832021982SDavid Howells .get_tree = hugetlbfs_get_tree, 153932021982SDavid Howells }; 154032021982SDavid Howells 154132021982SDavid Howells static int hugetlbfs_init_fs_context(struct fs_context *fc) 154232021982SDavid Howells { 154332021982SDavid Howells struct hugetlbfs_fs_context *ctx; 154432021982SDavid Howells 154532021982SDavid Howells ctx = kzalloc(sizeof(struct hugetlbfs_fs_context), GFP_KERNEL); 154632021982SDavid Howells if (!ctx) 154732021982SDavid Howells return -ENOMEM; 154832021982SDavid Howells 154932021982SDavid Howells ctx->max_hpages = -1; /* No limit on size by default */ 155032021982SDavid Howells ctx->nr_inodes = -1; /* No limit on number of inodes by default */ 155132021982SDavid Howells ctx->uid = current_fsuid(); 155232021982SDavid Howells ctx->gid = current_fsgid(); 155332021982SDavid Howells ctx->mode = 0755; 155432021982SDavid Howells ctx->hstate = &default_hstate; 155532021982SDavid Howells ctx->min_hpages = -1; /* No default minimum size */ 155632021982SDavid Howells ctx->max_val_type = NO_SIZE; 155732021982SDavid Howells ctx->min_val_type = NO_SIZE; 155832021982SDavid Howells fc->fs_private = ctx; 155932021982SDavid Howells fc->ops = &hugetlbfs_fs_context_ops; 156032021982SDavid Howells return 0; 15611da177e4SLinus Torvalds } 15621da177e4SLinus Torvalds 15631da177e4SLinus Torvalds static struct file_system_type hugetlbfs_fs_type = { 15641da177e4SLinus Torvalds .name = "hugetlbfs", 156532021982SDavid Howells .init_fs_context = hugetlbfs_init_fs_context, 1566d7167b14SAl Viro .parameters = hugetlb_fs_parameters, 15671da177e4SLinus Torvalds .kill_sb = kill_litter_super, 15681da177e4SLinus Torvalds }; 15691da177e4SLinus Torvalds 157042d7395fSAndi Kleen static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE]; 15711da177e4SLinus Torvalds 1572ef1ff6b8SFrom: Mel Gorman static int can_do_hugetlb_shm(void) 15731da177e4SLinus Torvalds { 1574a0eb3a05SEric W. Biederman kgid_t shm_group; 1575a0eb3a05SEric W. Biederman shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group); 1576a0eb3a05SEric W. Biederman return capable(CAP_IPC_LOCK) || in_group_p(shm_group); 15771da177e4SLinus Torvalds } 15781da177e4SLinus Torvalds 157942d7395fSAndi Kleen static int get_hstate_idx(int page_size_log) 158042d7395fSAndi Kleen { 1581af73e4d9SNaoya Horiguchi struct hstate *h = hstate_sizelog(page_size_log); 158242d7395fSAndi Kleen 158342d7395fSAndi Kleen if (!h) 158442d7395fSAndi Kleen return -1; 158504adbc3fSMiaohe Lin return hstate_index(h); 158642d7395fSAndi Kleen } 158742d7395fSAndi Kleen 1588af73e4d9SNaoya Horiguchi /* 1589af73e4d9SNaoya Horiguchi * Note that size should be aligned to proper hugepage size in caller side, 1590af73e4d9SNaoya Horiguchi * otherwise hugetlb_reserve_pages reserves one less hugepages than intended. 1591af73e4d9SNaoya Horiguchi */ 1592af73e4d9SNaoya Horiguchi struct file *hugetlb_file_setup(const char *name, size_t size, 159383c1fd76Szhangyiru vm_flags_t acctflag, int creat_flags, 159483c1fd76Szhangyiru int page_size_log) 15951da177e4SLinus Torvalds { 15961da177e4SLinus Torvalds struct inode *inode; 1597e68375c8SAl Viro struct vfsmount *mnt; 159842d7395fSAndi Kleen int hstate_idx; 1599e68375c8SAl Viro struct file *file; 160042d7395fSAndi Kleen 160142d7395fSAndi Kleen hstate_idx = get_hstate_idx(page_size_log); 160242d7395fSAndi Kleen if (hstate_idx < 0) 160342d7395fSAndi Kleen return ERR_PTR(-ENODEV); 16041da177e4SLinus Torvalds 1605e68375c8SAl Viro mnt = hugetlbfs_vfsmount[hstate_idx]; 1606e68375c8SAl Viro if (!mnt) 16075bc98594SAkinobu Mita return ERR_PTR(-ENOENT); 16085bc98594SAkinobu Mita 1609ef1ff6b8SFrom: Mel Gorman if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) { 161083c1fd76Szhangyiru struct ucounts *ucounts = current_ucounts(); 161183c1fd76Szhangyiru 161283c1fd76Szhangyiru if (user_shm_lock(size, ucounts)) { 161383c1fd76Szhangyiru pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is obsolete\n", 161421a3c273SDavid Rientjes current->comm, current->pid); 161583c1fd76Szhangyiru user_shm_unlock(size, ucounts); 16162584e517SRavikiran G Thirumalai } 161783c1fd76Szhangyiru return ERR_PTR(-EPERM); 1618353d5c30SHugh Dickins } 16191da177e4SLinus Torvalds 162039b65252SAnatol Pomozov file = ERR_PTR(-ENOSPC); 1621e68375c8SAl Viro inode = hugetlbfs_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0); 16221da177e4SLinus Torvalds if (!inode) 1623e68375c8SAl Viro goto out; 1624e1832f29SStephen Smalley if (creat_flags == HUGETLB_SHMFS_INODE) 1625e1832f29SStephen Smalley inode->i_flags |= S_PRIVATE; 16261da177e4SLinus Torvalds 16271da177e4SLinus Torvalds inode->i_size = size; 16286d6b77f1SMiklos Szeredi clear_nlink(inode); 1629ce8d2cdfSDave Hansen 163033b8f84aSMike Kravetz if (!hugetlb_reserve_pages(inode, 0, 1631e68375c8SAl Viro size >> huge_page_shift(hstate_inode(inode)), NULL, 1632e68375c8SAl Viro acctflag)) 1633e68375c8SAl Viro file = ERR_PTR(-ENOMEM); 1634e68375c8SAl Viro else 1635e68375c8SAl Viro file = alloc_file_pseudo(inode, mnt, name, O_RDWR, 1636ce8d2cdfSDave Hansen &hugetlbfs_file_operations); 1637e68375c8SAl Viro if (!IS_ERR(file)) 16381da177e4SLinus Torvalds return file; 16391da177e4SLinus Torvalds 1640b45b5bd6SDavid Gibson iput(inode); 1641e68375c8SAl Viro out: 164239b65252SAnatol Pomozov return file; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds 164532021982SDavid Howells static struct vfsmount *__init mount_one_hugetlbfs(struct hstate *h) 164632021982SDavid Howells { 164732021982SDavid Howells struct fs_context *fc; 164832021982SDavid Howells struct vfsmount *mnt; 164932021982SDavid Howells 165032021982SDavid Howells fc = fs_context_for_mount(&hugetlbfs_fs_type, SB_KERNMOUNT); 165132021982SDavid Howells if (IS_ERR(fc)) { 165232021982SDavid Howells mnt = ERR_CAST(fc); 165332021982SDavid Howells } else { 165432021982SDavid Howells struct hugetlbfs_fs_context *ctx = fc->fs_private; 165532021982SDavid Howells ctx->hstate = h; 165632021982SDavid Howells mnt = fc_mount(fc); 165732021982SDavid Howells put_fs_context(fc); 165832021982SDavid Howells } 165932021982SDavid Howells if (IS_ERR(mnt)) 1660a25fddceSMiaohe Lin pr_err("Cannot mount internal hugetlbfs for page size %luK", 1661d0036517SMiaohe Lin huge_page_size(h) / SZ_1K); 166232021982SDavid Howells return mnt; 166332021982SDavid Howells } 166432021982SDavid Howells 16651da177e4SLinus Torvalds static int __init init_hugetlbfs_fs(void) 16661da177e4SLinus Torvalds { 166732021982SDavid Howells struct vfsmount *mnt; 166842d7395fSAndi Kleen struct hstate *h; 16691da177e4SLinus Torvalds int error; 167042d7395fSAndi Kleen int i; 16711da177e4SLinus Torvalds 1672457c1b27SNishanth Aravamudan if (!hugepages_supported()) { 16739b857d26SAndrew Morton pr_info("disabling because there are no supported hugepage sizes\n"); 1674457c1b27SNishanth Aravamudan return -ENOTSUPP; 1675457c1b27SNishanth Aravamudan } 1676457c1b27SNishanth Aravamudan 1677d1d5e05fSHillf Danton error = -ENOMEM; 16781da177e4SLinus Torvalds hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache", 16791da177e4SLinus Torvalds sizeof(struct hugetlbfs_inode_info), 16805d097056SVladimir Davydov 0, SLAB_ACCOUNT, init_once); 16811da177e4SLinus Torvalds if (hugetlbfs_inode_cachep == NULL) 16828fc312b3SMike Kravetz goto out; 16831da177e4SLinus Torvalds 16841da177e4SLinus Torvalds error = register_filesystem(&hugetlbfs_fs_type); 16851da177e4SLinus Torvalds if (error) 16868fc312b3SMike Kravetz goto out_free; 16871da177e4SLinus Torvalds 16888fc312b3SMike Kravetz /* default hstate mount is required */ 16893b2275a8SMiaohe Lin mnt = mount_one_hugetlbfs(&default_hstate); 16908fc312b3SMike Kravetz if (IS_ERR(mnt)) { 16918fc312b3SMike Kravetz error = PTR_ERR(mnt); 16928fc312b3SMike Kravetz goto out_unreg; 16938fc312b3SMike Kravetz } 16948fc312b3SMike Kravetz hugetlbfs_vfsmount[default_hstate_idx] = mnt; 16958fc312b3SMike Kravetz 16968fc312b3SMike Kravetz /* other hstates are optional */ 169742d7395fSAndi Kleen i = 0; 169842d7395fSAndi Kleen for_each_hstate(h) { 169915f0ec94SJan Stancek if (i == default_hstate_idx) { 170015f0ec94SJan Stancek i++; 17018fc312b3SMike Kravetz continue; 170215f0ec94SJan Stancek } 17038fc312b3SMike Kravetz 170432021982SDavid Howells mnt = mount_one_hugetlbfs(h); 17058fc312b3SMike Kravetz if (IS_ERR(mnt)) 17068fc312b3SMike Kravetz hugetlbfs_vfsmount[i] = NULL; 17078fc312b3SMike Kravetz else 170832021982SDavid Howells hugetlbfs_vfsmount[i] = mnt; 170942d7395fSAndi Kleen i++; 171042d7395fSAndi Kleen } 171132021982SDavid Howells 171242d7395fSAndi Kleen return 0; 17131da177e4SLinus Torvalds 17148fc312b3SMike Kravetz out_unreg: 17158fc312b3SMike Kravetz (void)unregister_filesystem(&hugetlbfs_fs_type); 17168fc312b3SMike Kravetz out_free: 17171da177e4SLinus Torvalds kmem_cache_destroy(hugetlbfs_inode_cachep); 17188fc312b3SMike Kravetz out: 17191da177e4SLinus Torvalds return error; 17201da177e4SLinus Torvalds } 17213e89e1c5SPaul Gortmaker fs_initcall(init_hugetlbfs_fs) 1722