11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Resizable virtual memory filesystem for Linux. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Linus Torvalds. 51da177e4SLinus Torvalds * 2000 Transmeta Corp. 61da177e4SLinus Torvalds * 2000-2001 Christoph Rohland 71da177e4SLinus Torvalds * 2000-2001 SAP AG 81da177e4SLinus Torvalds * 2002 Red Hat Inc. 96922c0c7SHugh Dickins * Copyright (C) 2002-2011 Hugh Dickins. 106922c0c7SHugh Dickins * Copyright (C) 2011 Google Inc. 110edd73b3SHugh Dickins * Copyright (C) 2002-2005 VERITAS Software Corporation. 121da177e4SLinus Torvalds * Copyright (C) 2004 Andi Kleen, SuSE Labs 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * Extended attribute support for tmpfs: 151da177e4SLinus Torvalds * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 161da177e4SLinus Torvalds * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 171da177e4SLinus Torvalds * 18853ac43aSMatt Mackall * tiny-shmem: 19853ac43aSMatt Mackall * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20853ac43aSMatt Mackall * 211da177e4SLinus Torvalds * This file is released under the GPL. 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 24853ac43aSMatt Mackall #include <linux/fs.h> 25853ac43aSMatt Mackall #include <linux/init.h> 26853ac43aSMatt Mackall #include <linux/vfs.h> 27853ac43aSMatt Mackall #include <linux/mount.h> 28250297edSAndrew Morton #include <linux/ramfs.h> 29caefba17SHugh Dickins #include <linux/pagemap.h> 30853ac43aSMatt Mackall #include <linux/file.h> 31853ac43aSMatt Mackall #include <linux/mm.h> 32b95f1b31SPaul Gortmaker #include <linux/export.h> 33853ac43aSMatt Mackall #include <linux/swap.h> 34e2e40f2cSChristoph Hellwig #include <linux/uio.h> 35f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 36853ac43aSMatt Mackall 3795cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ 3895cc09d6SAndrea Arcangeli 39853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 40853ac43aSMatt Mackall 41853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 421da177e4SLinus Torvalds /* 431da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 441da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 451da177e4SLinus Torvalds * which makes it a completely usable filesystem. 461da177e4SLinus Torvalds */ 471da177e4SLinus Torvalds 4839f0247dSAndreas Gruenbacher #include <linux/xattr.h> 49a5694255SChristoph Hellwig #include <linux/exportfs.h> 501c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 51feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 521da177e4SLinus Torvalds #include <linux/mman.h> 531da177e4SLinus Torvalds #include <linux/string.h> 541da177e4SLinus Torvalds #include <linux/slab.h> 551da177e4SLinus Torvalds #include <linux/backing-dev.h> 561da177e4SLinus Torvalds #include <linux/shmem_fs.h> 571da177e4SLinus Torvalds #include <linux/writeback.h> 581da177e4SLinus Torvalds #include <linux/blkdev.h> 59bda97eabSHugh Dickins #include <linux/pagevec.h> 6041ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6183e4fa9cSHugh Dickins #include <linux/falloc.h> 62708e3508SHugh Dickins #include <linux/splice.h> 631da177e4SLinus Torvalds #include <linux/security.h> 641da177e4SLinus Torvalds #include <linux/swapops.h> 651da177e4SLinus Torvalds #include <linux/mempolicy.h> 661da177e4SLinus Torvalds #include <linux/namei.h> 67b00dc3adSHugh Dickins #include <linux/ctype.h> 68304dbdb7SLee Schermerhorn #include <linux/migrate.h> 69c1f60a5aSChristoph Lameter #include <linux/highmem.h> 70680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7192562927SMimi Zohar #include <linux/magic.h> 729183df25SDavid Herrmann #include <linux/syscalls.h> 7340e041a2SDavid Herrmann #include <linux/fcntl.h> 749183df25SDavid Herrmann #include <uapi/linux/memfd.h> 75cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 764c27fe4cSMike Rapoport #include <linux/rmap.h> 77304dbdb7SLee Schermerhorn 787c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 791da177e4SLinus Torvalds #include <asm/pgtable.h> 801da177e4SLinus Torvalds 81dd56b046SMel Gorman #include "internal.h" 82dd56b046SMel Gorman 8309cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8409cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 871da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 881da177e4SLinus Torvalds 8969f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9069f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9169f07ec9SHugh Dickins 921aac1400SHugh Dickins /* 93f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 94f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 95f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 961aac1400SHugh Dickins */ 971aac1400SHugh Dickins struct shmem_falloc { 988e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 991aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1001aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1011aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1021aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1031aac1400SHugh Dickins }; 1041aac1400SHugh Dickins 105b76db735SAndrew Morton #ifdef CONFIG_TMPFS 106680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 107680d794bSakpm@linux-foundation.org { 108680d794bSakpm@linux-foundation.org return totalram_pages / 2; 109680d794bSakpm@linux-foundation.org } 110680d794bSakpm@linux-foundation.org 111680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 112680d794bSakpm@linux-foundation.org { 113680d794bSakpm@linux-foundation.org return min(totalram_pages - totalhigh_pages, totalram_pages / 2); 114680d794bSakpm@linux-foundation.org } 115b76db735SAndrew Morton #endif 116680d794bSakpm@linux-foundation.org 117bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 118bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 119bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 12068da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1219e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 122cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 123cfda0526SMike Rapoport struct vm_fault *vmf, int *fault_type); 12468da9f05SHugh Dickins 125f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1269e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 12768da9f05SHugh Dickins { 12868da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 129cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 13068da9f05SHugh Dickins } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1331da177e4SLinus Torvalds { 1341da177e4SLinus Torvalds return sb->s_fs_info; 1351da177e4SLinus Torvalds } 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* 1381da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1391da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1401da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1411da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1421da177e4SLinus Torvalds */ 1431da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1441da177e4SLinus Torvalds { 1450b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 146191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1501da177e4SLinus Torvalds { 1510b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1521da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 15577142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 15677142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 15777142517SKonstantin Khlebnikov { 15877142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 15977142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 16077142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 16177142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 16277142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 16377142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 16477142517SKonstantin Khlebnikov } 16577142517SKonstantin Khlebnikov return 0; 16677142517SKonstantin Khlebnikov } 16777142517SKonstantin Khlebnikov 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 17075edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1711da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1721da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1731da177e4SLinus Torvalds */ 174800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1751da177e4SLinus Torvalds { 176800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 177800d8c63SKirill A. Shutemov return 0; 178800d8c63SKirill A. Shutemov 179800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 180800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1811da177e4SLinus Torvalds } 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1841da177e4SLinus Torvalds { 1850b0a0806SHugh Dickins if (flags & VM_NORESERVE) 18609cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189759b9775SHugh Dickins static const struct super_operations shmem_ops; 190f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 19115ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 19292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 19392e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 19492e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 195f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 196779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 1971da177e4SLinus Torvalds 198b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 199b0506e48SMike Rapoport { 200b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 201b0506e48SMike Rapoport } 202b0506e48SMike Rapoport 2031da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 204cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2051da177e4SLinus Torvalds 2065b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2075b04c689SPavel Emelyanov { 2085b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2095b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2105b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2115b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2125b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2135b04c689SPavel Emelyanov return -ENOSPC; 2145b04c689SPavel Emelyanov } 2155b04c689SPavel Emelyanov sbinfo->free_inodes--; 2165b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2175b04c689SPavel Emelyanov } 2185b04c689SPavel Emelyanov return 0; 2195b04c689SPavel Emelyanov } 2205b04c689SPavel Emelyanov 2215b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2225b04c689SPavel Emelyanov { 2235b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2245b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2255b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2265b04c689SPavel Emelyanov sbinfo->free_inodes++; 2275b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2285b04c689SPavel Emelyanov } 2295b04c689SPavel Emelyanov } 2305b04c689SPavel Emelyanov 23146711810SRandy Dunlap /** 23241ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2331da177e4SLinus Torvalds * @inode: inode to recalc 2341da177e4SLinus Torvalds * 2351da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2361da177e4SLinus Torvalds * undirtied hole pages behind our back. 2371da177e4SLinus Torvalds * 2381da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2391da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2401da177e4SLinus Torvalds * 2411da177e4SLinus Torvalds * It has to be called with the spinlock held. 2421da177e4SLinus Torvalds */ 2431da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 2461da177e4SLinus Torvalds long freed; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 2491da177e4SLinus Torvalds if (freed > 0) { 25054af6042SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 25154af6042SHugh Dickins if (sbinfo->max_blocks) 25254af6042SHugh Dickins percpu_counter_add(&sbinfo->used_blocks, -freed); 2531da177e4SLinus Torvalds info->alloced -= freed; 25454af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 2551da177e4SLinus Torvalds shmem_unacct_blocks(info->flags, freed); 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 259800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 260800d8c63SKirill A. Shutemov { 261800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 262800d8c63SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2634595ef88SKirill A. Shutemov unsigned long flags; 264800d8c63SKirill A. Shutemov 265800d8c63SKirill A. Shutemov if (shmem_acct_block(info->flags, pages)) 266800d8c63SKirill A. Shutemov return false; 2674595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 268800d8c63SKirill A. Shutemov info->alloced += pages; 269800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 270800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 2714595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 272800d8c63SKirill A. Shutemov inode->i_mapping->nrpages += pages; 273800d8c63SKirill A. Shutemov 274800d8c63SKirill A. Shutemov if (!sbinfo->max_blocks) 275800d8c63SKirill A. Shutemov return true; 276800d8c63SKirill A. Shutemov if (percpu_counter_compare(&sbinfo->used_blocks, 277800d8c63SKirill A. Shutemov sbinfo->max_blocks - pages) > 0) { 278800d8c63SKirill A. Shutemov inode->i_mapping->nrpages -= pages; 2794595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 280800d8c63SKirill A. Shutemov info->alloced -= pages; 281800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 2824595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 28371664665SHugh Dickins shmem_unacct_blocks(info->flags, pages); 284800d8c63SKirill A. Shutemov return false; 285800d8c63SKirill A. Shutemov } 286800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, pages); 287800d8c63SKirill A. Shutemov return true; 288800d8c63SKirill A. Shutemov } 289800d8c63SKirill A. Shutemov 290800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 291800d8c63SKirill A. Shutemov { 292800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 293800d8c63SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2944595ef88SKirill A. Shutemov unsigned long flags; 295800d8c63SKirill A. Shutemov 2964595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 297800d8c63SKirill A. Shutemov info->alloced -= pages; 298800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 299800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3004595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 301800d8c63SKirill A. Shutemov 302800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 303800d8c63SKirill A. Shutemov percpu_counter_sub(&sbinfo->used_blocks, pages); 30471664665SHugh Dickins shmem_unacct_blocks(info->flags, pages); 305800d8c63SKirill A. Shutemov } 306800d8c63SKirill A. Shutemov 3077a5d0fbbSHugh Dickins /* 3087a5d0fbbSHugh Dickins * Replace item expected in radix tree by a new item, while holding tree lock. 3097a5d0fbbSHugh Dickins */ 3107a5d0fbbSHugh Dickins static int shmem_radix_tree_replace(struct address_space *mapping, 3117a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3127a5d0fbbSHugh Dickins { 313f7942430SJohannes Weiner struct radix_tree_node *node; 3147a5d0fbbSHugh Dickins void **pslot; 3156dbaf22cSJohannes Weiner void *item; 3167a5d0fbbSHugh Dickins 3177a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3186dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 319f7942430SJohannes Weiner item = __radix_tree_lookup(&mapping->page_tree, index, &node, &pslot); 320f7942430SJohannes Weiner if (!item) 3216dbaf22cSJohannes Weiner return -ENOENT; 3227a5d0fbbSHugh Dickins if (item != expected) 3237a5d0fbbSHugh Dickins return -ENOENT; 3244d693d08SJohannes Weiner __radix_tree_replace(&mapping->page_tree, node, pslot, 3254d693d08SJohannes Weiner replacement, NULL, NULL); 3267a5d0fbbSHugh Dickins return 0; 3277a5d0fbbSHugh Dickins } 3287a5d0fbbSHugh Dickins 3297a5d0fbbSHugh Dickins /* 330d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 331d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 332d1899228SHugh Dickins * 333d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 334d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 335d1899228SHugh Dickins */ 336d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 337d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 338d1899228SHugh Dickins { 339d1899228SHugh Dickins void *item; 340d1899228SHugh Dickins 341d1899228SHugh Dickins rcu_read_lock(); 342d1899228SHugh Dickins item = radix_tree_lookup(&mapping->page_tree, index); 343d1899228SHugh Dickins rcu_read_unlock(); 344d1899228SHugh Dickins return item == swp_to_radix_entry(swap); 345d1899228SHugh Dickins } 346d1899228SHugh Dickins 347d1899228SHugh Dickins /* 3485a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3495a6e75f8SKirill A. Shutemov * 3505a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3515a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3525a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3535a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3545a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3555a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3565a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3575a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3585a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3595a6e75f8SKirill A. Shutemov */ 3605a6e75f8SKirill A. Shutemov 3615a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3625a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3635a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3645a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3655a6e75f8SKirill A. Shutemov 3665a6e75f8SKirill A. Shutemov /* 3675a6e75f8SKirill A. Shutemov * Special values. 3685a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 3695a6e75f8SKirill A. Shutemov * 3705a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 3715a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 3725a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 3735a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 3745a6e75f8SKirill A. Shutemov * 3755a6e75f8SKirill A. Shutemov */ 3765a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 3775a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 3785a6e75f8SKirill A. Shutemov 379e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3805a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 3815a6e75f8SKirill A. Shutemov 3825a6e75f8SKirill A. Shutemov int shmem_huge __read_mostly; 3835a6e75f8SKirill A. Shutemov 384f1f5929cSJérémy Lefaure #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 3855a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 3865a6e75f8SKirill A. Shutemov { 3875a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 3885a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 3895a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 3905a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 3915a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 3925a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 3935a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 3945a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 3955a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 3965a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 3975a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 3985a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 3995a6e75f8SKirill A. Shutemov return -EINVAL; 4005a6e75f8SKirill A. Shutemov } 4015a6e75f8SKirill A. Shutemov 4025a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 4035a6e75f8SKirill A. Shutemov { 4045a6e75f8SKirill A. Shutemov switch (huge) { 4055a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4065a6e75f8SKirill A. Shutemov return "never"; 4075a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4085a6e75f8SKirill A. Shutemov return "always"; 4095a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4105a6e75f8SKirill A. Shutemov return "within_size"; 4115a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4125a6e75f8SKirill A. Shutemov return "advise"; 4135a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4145a6e75f8SKirill A. Shutemov return "deny"; 4155a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4165a6e75f8SKirill A. Shutemov return "force"; 4175a6e75f8SKirill A. Shutemov default: 4185a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4195a6e75f8SKirill A. Shutemov return "bad_val"; 4205a6e75f8SKirill A. Shutemov } 4215a6e75f8SKirill A. Shutemov } 422f1f5929cSJérémy Lefaure #endif 4235a6e75f8SKirill A. Shutemov 424779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 425779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 426779750d2SKirill A. Shutemov { 427779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 428253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 429779750d2SKirill A. Shutemov struct inode *inode; 430779750d2SKirill A. Shutemov struct shmem_inode_info *info; 431779750d2SKirill A. Shutemov struct page *page; 432779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 433779750d2SKirill A. Shutemov int removed = 0, split = 0; 434779750d2SKirill A. Shutemov 435779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 436779750d2SKirill A. Shutemov return SHRINK_STOP; 437779750d2SKirill A. Shutemov 438779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 439779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 440779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 441779750d2SKirill A. Shutemov 442779750d2SKirill A. Shutemov /* pin the inode */ 443779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 444779750d2SKirill A. Shutemov 445779750d2SKirill A. Shutemov /* inode is about to be evicted */ 446779750d2SKirill A. Shutemov if (!inode) { 447779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 448779750d2SKirill A. Shutemov removed++; 449779750d2SKirill A. Shutemov goto next; 450779750d2SKirill A. Shutemov } 451779750d2SKirill A. Shutemov 452779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 453779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 454779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 455253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 456779750d2SKirill A. Shutemov removed++; 457779750d2SKirill A. Shutemov goto next; 458779750d2SKirill A. Shutemov } 459779750d2SKirill A. Shutemov 460779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 461779750d2SKirill A. Shutemov next: 462779750d2SKirill A. Shutemov if (!--batch) 463779750d2SKirill A. Shutemov break; 464779750d2SKirill A. Shutemov } 465779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 466779750d2SKirill A. Shutemov 467253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 468253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 469253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 470253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 471253fd0f0SKirill A. Shutemov iput(inode); 472253fd0f0SKirill A. Shutemov } 473253fd0f0SKirill A. Shutemov 474779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 475779750d2SKirill A. Shutemov int ret; 476779750d2SKirill A. Shutemov 477779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 478779750d2SKirill A. Shutemov inode = &info->vfs_inode; 479779750d2SKirill A. Shutemov 480779750d2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) { 481779750d2SKirill A. Shutemov iput(inode); 482779750d2SKirill A. Shutemov continue; 483779750d2SKirill A. Shutemov } 484779750d2SKirill A. Shutemov 485779750d2SKirill A. Shutemov page = find_lock_page(inode->i_mapping, 486779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 487779750d2SKirill A. Shutemov if (!page) 488779750d2SKirill A. Shutemov goto drop; 489779750d2SKirill A. Shutemov 490779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 491779750d2SKirill A. Shutemov unlock_page(page); 492779750d2SKirill A. Shutemov put_page(page); 493779750d2SKirill A. Shutemov goto drop; 494779750d2SKirill A. Shutemov } 495779750d2SKirill A. Shutemov 496779750d2SKirill A. Shutemov ret = split_huge_page(page); 497779750d2SKirill A. Shutemov unlock_page(page); 498779750d2SKirill A. Shutemov put_page(page); 499779750d2SKirill A. Shutemov 500779750d2SKirill A. Shutemov if (ret) { 501779750d2SKirill A. Shutemov /* split failed: leave it on the list */ 502779750d2SKirill A. Shutemov iput(inode); 503779750d2SKirill A. Shutemov continue; 504779750d2SKirill A. Shutemov } 505779750d2SKirill A. Shutemov 506779750d2SKirill A. Shutemov split++; 507779750d2SKirill A. Shutemov drop: 508779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 509779750d2SKirill A. Shutemov removed++; 510779750d2SKirill A. Shutemov iput(inode); 511779750d2SKirill A. Shutemov } 512779750d2SKirill A. Shutemov 513779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 514779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 515779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 516779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 517779750d2SKirill A. Shutemov 518779750d2SKirill A. Shutemov return split; 519779750d2SKirill A. Shutemov } 520779750d2SKirill A. Shutemov 521779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 522779750d2SKirill A. Shutemov struct shrink_control *sc) 523779750d2SKirill A. Shutemov { 524779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 525779750d2SKirill A. Shutemov 526779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 527779750d2SKirill A. Shutemov return SHRINK_STOP; 528779750d2SKirill A. Shutemov 529779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 530779750d2SKirill A. Shutemov } 531779750d2SKirill A. Shutemov 532779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 533779750d2SKirill A. Shutemov struct shrink_control *sc) 534779750d2SKirill A. Shutemov { 535779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 536779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 537779750d2SKirill A. Shutemov } 538e496cf3dSKirill A. Shutemov #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5395a6e75f8SKirill A. Shutemov 5405a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5415a6e75f8SKirill A. Shutemov 542779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 543779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 544779750d2SKirill A. Shutemov { 545779750d2SKirill A. Shutemov return 0; 546779750d2SKirill A. Shutemov } 547e496cf3dSKirill A. Shutemov #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 5485a6e75f8SKirill A. Shutemov 5495a6e75f8SKirill A. Shutemov /* 55046f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 55146f65ec1SHugh Dickins */ 55246f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 55346f65ec1SHugh Dickins struct address_space *mapping, 554fed400a1SWang Sheng-Hui pgoff_t index, void *expected) 55546f65ec1SHugh Dickins { 556800d8c63SKirill A. Shutemov int error, nr = hpage_nr_pages(page); 55746f65ec1SHugh Dickins 558800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 559800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(index != round_down(index, nr), page); 560309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 561309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 562800d8c63SKirill A. Shutemov VM_BUG_ON(expected && PageTransHuge(page)); 56346f65ec1SHugh Dickins 564800d8c63SKirill A. Shutemov page_ref_add(page, nr); 56546f65ec1SHugh Dickins page->mapping = mapping; 56646f65ec1SHugh Dickins page->index = index; 56746f65ec1SHugh Dickins 56846f65ec1SHugh Dickins spin_lock_irq(&mapping->tree_lock); 569800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 570800d8c63SKirill A. Shutemov void __rcu **results; 571800d8c63SKirill A. Shutemov pgoff_t idx; 572800d8c63SKirill A. Shutemov int i; 573800d8c63SKirill A. Shutemov 574800d8c63SKirill A. Shutemov error = 0; 575800d8c63SKirill A. Shutemov if (radix_tree_gang_lookup_slot(&mapping->page_tree, 576800d8c63SKirill A. Shutemov &results, &idx, index, 1) && 577800d8c63SKirill A. Shutemov idx < index + HPAGE_PMD_NR) { 578800d8c63SKirill A. Shutemov error = -EEXIST; 579800d8c63SKirill A. Shutemov } 580800d8c63SKirill A. Shutemov 581800d8c63SKirill A. Shutemov if (!error) { 582800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 583800d8c63SKirill A. Shutemov error = radix_tree_insert(&mapping->page_tree, 584800d8c63SKirill A. Shutemov index + i, page + i); 585800d8c63SKirill A. Shutemov VM_BUG_ON(error); 586800d8c63SKirill A. Shutemov } 587800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 588800d8c63SKirill A. Shutemov } 589800d8c63SKirill A. Shutemov } else if (!expected) { 590b065b432SHugh Dickins error = radix_tree_insert(&mapping->page_tree, index, page); 591800d8c63SKirill A. Shutemov } else { 592b065b432SHugh Dickins error = shmem_radix_tree_replace(mapping, index, expected, 593b065b432SHugh Dickins page); 594800d8c63SKirill A. Shutemov } 595800d8c63SKirill A. Shutemov 59646f65ec1SHugh Dickins if (!error) { 597800d8c63SKirill A. Shutemov mapping->nrpages += nr; 598800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 59911fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 60011fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 60111fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 60246f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 60346f65ec1SHugh Dickins } else { 60446f65ec1SHugh Dickins page->mapping = NULL; 60546f65ec1SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 606800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 60746f65ec1SHugh Dickins } 60846f65ec1SHugh Dickins return error; 60946f65ec1SHugh Dickins } 61046f65ec1SHugh Dickins 61146f65ec1SHugh Dickins /* 6126922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 6136922c0c7SHugh Dickins */ 6146922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 6156922c0c7SHugh Dickins { 6166922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 6176922c0c7SHugh Dickins int error; 6186922c0c7SHugh Dickins 619800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 620800d8c63SKirill A. Shutemov 6216922c0c7SHugh Dickins spin_lock_irq(&mapping->tree_lock); 6226922c0c7SHugh Dickins error = shmem_radix_tree_replace(mapping, page->index, page, radswap); 6236922c0c7SHugh Dickins page->mapping = NULL; 6246922c0c7SHugh Dickins mapping->nrpages--; 62511fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 62611fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 6276922c0c7SHugh Dickins spin_unlock_irq(&mapping->tree_lock); 62809cbfeafSKirill A. Shutemov put_page(page); 6296922c0c7SHugh Dickins BUG_ON(error); 6306922c0c7SHugh Dickins } 6316922c0c7SHugh Dickins 6326922c0c7SHugh Dickins /* 6337a5d0fbbSHugh Dickins * Remove swap entry from radix tree, free the swap and its page cache. 6347a5d0fbbSHugh Dickins */ 6357a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6367a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6377a5d0fbbSHugh Dickins { 6386dbaf22cSJohannes Weiner void *old; 6397a5d0fbbSHugh Dickins 6407a5d0fbbSHugh Dickins spin_lock_irq(&mapping->tree_lock); 6416dbaf22cSJohannes Weiner old = radix_tree_delete_item(&mapping->page_tree, index, radswap); 6427a5d0fbbSHugh Dickins spin_unlock_irq(&mapping->tree_lock); 6436dbaf22cSJohannes Weiner if (old != radswap) 6446dbaf22cSJohannes Weiner return -ENOENT; 6457a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6466dbaf22cSJohannes Weiner return 0; 6477a5d0fbbSHugh Dickins } 6487a5d0fbbSHugh Dickins 6497a5d0fbbSHugh Dickins /* 6506a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 65148131e03SVlastimil Babka * given offsets are swapped out. 6526a15a370SVlastimil Babka * 6536a15a370SVlastimil Babka * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU, 6546a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 6556a15a370SVlastimil Babka */ 65648131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 65748131e03SVlastimil Babka pgoff_t start, pgoff_t end) 6586a15a370SVlastimil Babka { 6596a15a370SVlastimil Babka struct radix_tree_iter iter; 6606a15a370SVlastimil Babka void **slot; 6616a15a370SVlastimil Babka struct page *page; 66248131e03SVlastimil Babka unsigned long swapped = 0; 6636a15a370SVlastimil Babka 6646a15a370SVlastimil Babka rcu_read_lock(); 6656a15a370SVlastimil Babka 6666a15a370SVlastimil Babka radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 6676a15a370SVlastimil Babka if (iter.index >= end) 6686a15a370SVlastimil Babka break; 6696a15a370SVlastimil Babka 6706a15a370SVlastimil Babka page = radix_tree_deref_slot(slot); 6716a15a370SVlastimil Babka 6722cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 6732cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 6742cf938aaSMatthew Wilcox continue; 6752cf938aaSMatthew Wilcox } 6766a15a370SVlastimil Babka 6776a15a370SVlastimil Babka if (radix_tree_exceptional_entry(page)) 6786a15a370SVlastimil Babka swapped++; 6796a15a370SVlastimil Babka 6806a15a370SVlastimil Babka if (need_resched()) { 681148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 6826a15a370SVlastimil Babka cond_resched_rcu(); 6836a15a370SVlastimil Babka } 6846a15a370SVlastimil Babka } 6856a15a370SVlastimil Babka 6866a15a370SVlastimil Babka rcu_read_unlock(); 6876a15a370SVlastimil Babka 6886a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 6896a15a370SVlastimil Babka } 6906a15a370SVlastimil Babka 6916a15a370SVlastimil Babka /* 69248131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 69348131e03SVlastimil Babka * given vma is swapped out. 69448131e03SVlastimil Babka * 69548131e03SVlastimil Babka * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU, 69648131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 69748131e03SVlastimil Babka */ 69848131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 69948131e03SVlastimil Babka { 70048131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 70148131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 70248131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 70348131e03SVlastimil Babka unsigned long swapped; 70448131e03SVlastimil Babka 70548131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 70648131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 70748131e03SVlastimil Babka 70848131e03SVlastimil Babka /* 70948131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 71048131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 71148131e03SVlastimil Babka * already track. 71248131e03SVlastimil Babka */ 71348131e03SVlastimil Babka if (!swapped) 71448131e03SVlastimil Babka return 0; 71548131e03SVlastimil Babka 71648131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 71748131e03SVlastimil Babka return swapped << PAGE_SHIFT; 71848131e03SVlastimil Babka 71948131e03SVlastimil Babka /* Here comes the more involved part */ 72048131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 72148131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 72248131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 72348131e03SVlastimil Babka } 72448131e03SVlastimil Babka 72548131e03SVlastimil Babka /* 72624513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 72724513264SHugh Dickins */ 72824513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 72924513264SHugh Dickins { 73024513264SHugh Dickins struct pagevec pvec; 73124513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 73224513264SHugh Dickins pgoff_t index = 0; 73324513264SHugh Dickins 73424513264SHugh Dickins pagevec_init(&pvec, 0); 73524513264SHugh Dickins /* 73624513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 73724513264SHugh Dickins */ 73824513264SHugh Dickins while (!mapping_unevictable(mapping)) { 73924513264SHugh Dickins /* 74024513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 74124513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 74224513264SHugh Dickins */ 7430cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 74424513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 74524513264SHugh Dickins if (!pvec.nr) 74624513264SHugh Dickins break; 74724513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7480cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 74924513264SHugh Dickins check_move_unevictable_pages(pvec.pages, pvec.nr); 75024513264SHugh Dickins pagevec_release(&pvec); 75124513264SHugh Dickins cond_resched(); 75224513264SHugh Dickins } 7537a5d0fbbSHugh Dickins } 7547a5d0fbbSHugh Dickins 7557a5d0fbbSHugh Dickins /* 7567a5d0fbbSHugh Dickins * Remove range of pages and swap entries from radix tree, and free them. 7571635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 7587a5d0fbbSHugh Dickins */ 7591635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 7601635f6a7SHugh Dickins bool unfalloc) 7611da177e4SLinus Torvalds { 762285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 7631da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 76409cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 76509cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 76609cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 76709cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 768bda97eabSHugh Dickins struct pagevec pvec; 7697a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 7707a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 771285b2c4fSHugh Dickins pgoff_t index; 772bda97eabSHugh Dickins int i; 7731da177e4SLinus Torvalds 77483e4fa9cSHugh Dickins if (lend == -1) 77583e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 776bda97eabSHugh Dickins 777bda97eabSHugh Dickins pagevec_init(&pvec, 0); 778bda97eabSHugh Dickins index = start; 77983e4fa9cSHugh Dickins while (index < end) { 7800cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 78183e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 7827a5d0fbbSHugh Dickins pvec.pages, indices); 7837a5d0fbbSHugh Dickins if (!pvec.nr) 7847a5d0fbbSHugh Dickins break; 785bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 786bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 787bda97eabSHugh Dickins 7887a5d0fbbSHugh Dickins index = indices[i]; 78983e4fa9cSHugh Dickins if (index >= end) 790bda97eabSHugh Dickins break; 791bda97eabSHugh Dickins 7927a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 7931635f6a7SHugh Dickins if (unfalloc) 7941635f6a7SHugh Dickins continue; 7957a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 7967a5d0fbbSHugh Dickins index, page); 7977a5d0fbbSHugh Dickins continue; 7987a5d0fbbSHugh Dickins } 7997a5d0fbbSHugh Dickins 800800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 801800d8c63SKirill A. Shutemov 802bda97eabSHugh Dickins if (!trylock_page(page)) 803bda97eabSHugh Dickins continue; 804800d8c63SKirill A. Shutemov 805800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 806800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 807800d8c63SKirill A. Shutemov clear_highpage(page); 808800d8c63SKirill A. Shutemov unlock_page(page); 809800d8c63SKirill A. Shutemov continue; 810800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 811800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 812800d8c63SKirill A. Shutemov /* 813800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 814800d8c63SKirill A. Shutemov * zero out the page 815800d8c63SKirill A. Shutemov */ 816800d8c63SKirill A. Shutemov clear_highpage(page); 817800d8c63SKirill A. Shutemov unlock_page(page); 818800d8c63SKirill A. Shutemov continue; 819800d8c63SKirill A. Shutemov } 820800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 821800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 822800d8c63SKirill A. Shutemov } 823800d8c63SKirill A. Shutemov 8241635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 825800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 826800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 827309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 828bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8297a5d0fbbSHugh Dickins } 8301635f6a7SHugh Dickins } 831bda97eabSHugh Dickins unlock_page(page); 832bda97eabSHugh Dickins } 8330cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 83424513264SHugh Dickins pagevec_release(&pvec); 835bda97eabSHugh Dickins cond_resched(); 836bda97eabSHugh Dickins index++; 837bda97eabSHugh Dickins } 838bda97eabSHugh Dickins 83983e4fa9cSHugh Dickins if (partial_start) { 840bda97eabSHugh Dickins struct page *page = NULL; 8419e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 842bda97eabSHugh Dickins if (page) { 84309cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 84483e4fa9cSHugh Dickins if (start > end) { 84583e4fa9cSHugh Dickins top = partial_end; 84683e4fa9cSHugh Dickins partial_end = 0; 84783e4fa9cSHugh Dickins } 84883e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 849bda97eabSHugh Dickins set_page_dirty(page); 850bda97eabSHugh Dickins unlock_page(page); 85109cbfeafSKirill A. Shutemov put_page(page); 852bda97eabSHugh Dickins } 853bda97eabSHugh Dickins } 85483e4fa9cSHugh Dickins if (partial_end) { 85583e4fa9cSHugh Dickins struct page *page = NULL; 8569e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 85783e4fa9cSHugh Dickins if (page) { 85883e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 85983e4fa9cSHugh Dickins set_page_dirty(page); 86083e4fa9cSHugh Dickins unlock_page(page); 86109cbfeafSKirill A. Shutemov put_page(page); 86283e4fa9cSHugh Dickins } 86383e4fa9cSHugh Dickins } 86483e4fa9cSHugh Dickins if (start >= end) 86583e4fa9cSHugh Dickins return; 866bda97eabSHugh Dickins 867bda97eabSHugh Dickins index = start; 868b1a36650SHugh Dickins while (index < end) { 869bda97eabSHugh Dickins cond_resched(); 8700cd6144aSJohannes Weiner 8710cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 87283e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8737a5d0fbbSHugh Dickins pvec.pages, indices); 8747a5d0fbbSHugh Dickins if (!pvec.nr) { 875b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 876b1a36650SHugh Dickins if (index == start || end != -1) 877bda97eabSHugh Dickins break; 878b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 879bda97eabSHugh Dickins index = start; 880bda97eabSHugh Dickins continue; 881bda97eabSHugh Dickins } 882bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 883bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 884bda97eabSHugh Dickins 8857a5d0fbbSHugh Dickins index = indices[i]; 88683e4fa9cSHugh Dickins if (index >= end) 887bda97eabSHugh Dickins break; 888bda97eabSHugh Dickins 8897a5d0fbbSHugh Dickins if (radix_tree_exceptional_entry(page)) { 8901635f6a7SHugh Dickins if (unfalloc) 8911635f6a7SHugh Dickins continue; 892b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 893b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 894b1a36650SHugh Dickins index--; 895b1a36650SHugh Dickins break; 896b1a36650SHugh Dickins } 897b1a36650SHugh Dickins nr_swaps_freed++; 8987a5d0fbbSHugh Dickins continue; 8997a5d0fbbSHugh Dickins } 9007a5d0fbbSHugh Dickins 901bda97eabSHugh Dickins lock_page(page); 902800d8c63SKirill A. Shutemov 903800d8c63SKirill A. Shutemov if (PageTransTail(page)) { 904800d8c63SKirill A. Shutemov /* Middle of THP: zero out the page */ 905800d8c63SKirill A. Shutemov clear_highpage(page); 906800d8c63SKirill A. Shutemov unlock_page(page); 907800d8c63SKirill A. Shutemov /* 908800d8c63SKirill A. Shutemov * Partial thp truncate due 'start' in middle 909800d8c63SKirill A. Shutemov * of THP: don't need to look on these pages 910800d8c63SKirill A. Shutemov * again on !pvec.nr restart. 911800d8c63SKirill A. Shutemov */ 912800d8c63SKirill A. Shutemov if (index != round_down(end, HPAGE_PMD_NR)) 913800d8c63SKirill A. Shutemov start++; 914800d8c63SKirill A. Shutemov continue; 915800d8c63SKirill A. Shutemov } else if (PageTransHuge(page)) { 916800d8c63SKirill A. Shutemov if (index == round_down(end, HPAGE_PMD_NR)) { 917800d8c63SKirill A. Shutemov /* 918800d8c63SKirill A. Shutemov * Range ends in the middle of THP: 919800d8c63SKirill A. Shutemov * zero out the page 920800d8c63SKirill A. Shutemov */ 921800d8c63SKirill A. Shutemov clear_highpage(page); 922800d8c63SKirill A. Shutemov unlock_page(page); 923800d8c63SKirill A. Shutemov continue; 924800d8c63SKirill A. Shutemov } 925800d8c63SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 926800d8c63SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 927800d8c63SKirill A. Shutemov } 928800d8c63SKirill A. Shutemov 9291635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 930800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 931800d8c63SKirill A. Shutemov if (page_mapping(page) == mapping) { 932309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 933bda97eabSHugh Dickins truncate_inode_page(mapping, page); 934b1a36650SHugh Dickins } else { 935b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 936b1a36650SHugh Dickins unlock_page(page); 937b1a36650SHugh Dickins index--; 938b1a36650SHugh Dickins break; 9397a5d0fbbSHugh Dickins } 9401635f6a7SHugh Dickins } 941bda97eabSHugh Dickins unlock_page(page); 942bda97eabSHugh Dickins } 9430cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 94424513264SHugh Dickins pagevec_release(&pvec); 945bda97eabSHugh Dickins index++; 946bda97eabSHugh Dickins } 94794c1e62dSHugh Dickins 9484595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9497a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9501da177e4SLinus Torvalds shmem_recalc_inode(inode); 9514595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9521635f6a7SHugh Dickins } 9531da177e4SLinus Torvalds 9541635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9551635f6a7SHugh Dickins { 9561635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 957078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 9581da177e4SLinus Torvalds } 95994c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9601da177e4SLinus Torvalds 96144a30220SYu Zhao static int shmem_getattr(struct vfsmount *mnt, struct dentry *dentry, 96244a30220SYu Zhao struct kstat *stat) 96344a30220SYu Zhao { 96444a30220SYu Zhao struct inode *inode = dentry->d_inode; 96544a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 96644a30220SYu Zhao 967d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 9684595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 96944a30220SYu Zhao shmem_recalc_inode(inode); 9704595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 971d0424c42SHugh Dickins } 97244a30220SYu Zhao generic_fillattr(inode, stat); 97344a30220SYu Zhao return 0; 97444a30220SYu Zhao } 97544a30220SYu Zhao 97694c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 9771da177e4SLinus Torvalds { 97875c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 97940e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 980779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 9811da177e4SLinus Torvalds int error; 9821da177e4SLinus Torvalds 98331051c85SJan Kara error = setattr_prepare(dentry, attr); 984db78b877SChristoph Hellwig if (error) 985db78b877SChristoph Hellwig return error; 986db78b877SChristoph Hellwig 98794c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 98894c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 98994c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 9903889e6e7Snpiggin@suse.de 99140e041a2SDavid Herrmann /* protected by i_mutex */ 99240e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 99340e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 99440e041a2SDavid Herrmann return -EPERM; 99540e041a2SDavid Herrmann 99694c1e62dSHugh Dickins if (newsize != oldsize) { 99777142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 99877142517SKonstantin Khlebnikov oldsize, newsize); 99977142517SKonstantin Khlebnikov if (error) 100077142517SKonstantin Khlebnikov return error; 100194c1e62dSHugh Dickins i_size_write(inode, newsize); 1002078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 100394c1e62dSHugh Dickins } 1004afa2db2fSJosef Bacik if (newsize <= oldsize) { 100594c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1006d0424c42SHugh Dickins if (oldsize > holebegin) 1007d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1008d0424c42SHugh Dickins holebegin, 0, 1); 1009d0424c42SHugh Dickins if (info->alloced) 1010d0424c42SHugh Dickins shmem_truncate_range(inode, 1011d0424c42SHugh Dickins newsize, (loff_t)-1); 101294c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1013d0424c42SHugh Dickins if (oldsize > holebegin) 1014d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1015d0424c42SHugh Dickins holebegin, 0, 1); 1016779750d2SKirill A. Shutemov 1017779750d2SKirill A. Shutemov /* 1018779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 1019779750d2SKirill A. Shutemov * to shrink under memory pressure. 1020779750d2SKirill A. Shutemov */ 1021779750d2SKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) { 1022779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1023779750d2SKirill A. Shutemov if (list_empty(&info->shrinklist)) { 1024779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1025779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1026779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1027779750d2SKirill A. Shutemov } 1028779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1029779750d2SKirill A. Shutemov } 103094c1e62dSHugh Dickins } 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 10336a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1034db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1035feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10361da177e4SLinus Torvalds return error; 10371da177e4SLinus Torvalds } 10381da177e4SLinus Torvalds 10391f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10401da177e4SLinus Torvalds { 10411da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1042779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10431da177e4SLinus Torvalds 10443889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10451da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10461da177e4SLinus Torvalds inode->i_size = 0; 10473889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1048779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1049779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1050779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1051779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1052779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1053779750d2SKirill A. Shutemov } 1054779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1055779750d2SKirill A. Shutemov } 10561da177e4SLinus Torvalds if (!list_empty(&info->swaplist)) { 1057cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 10581da177e4SLinus Torvalds list_del_init(&info->swaplist); 1059cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 10601da177e4SLinus Torvalds } 10613ed47db3SAl Viro } 1062b09e0fa4SEric Paris 106338f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 10640f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 10655b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1066dbd5768fSJan Kara clear_inode(inode); 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds 1069478922e2SMatthew Wilcox static unsigned long find_swap_entry(struct radix_tree_root *root, void *item) 1070478922e2SMatthew Wilcox { 1071478922e2SMatthew Wilcox struct radix_tree_iter iter; 1072478922e2SMatthew Wilcox void **slot; 1073478922e2SMatthew Wilcox unsigned long found = -1; 1074478922e2SMatthew Wilcox unsigned int checked = 0; 1075478922e2SMatthew Wilcox 1076478922e2SMatthew Wilcox rcu_read_lock(); 1077478922e2SMatthew Wilcox radix_tree_for_each_slot(slot, root, &iter, 0) { 1078478922e2SMatthew Wilcox if (*slot == item) { 1079478922e2SMatthew Wilcox found = iter.index; 1080478922e2SMatthew Wilcox break; 1081478922e2SMatthew Wilcox } 1082478922e2SMatthew Wilcox checked++; 1083478922e2SMatthew Wilcox if ((checked % 4096) != 0) 1084478922e2SMatthew Wilcox continue; 1085478922e2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 1086478922e2SMatthew Wilcox cond_resched_rcu(); 1087478922e2SMatthew Wilcox } 1088478922e2SMatthew Wilcox 1089478922e2SMatthew Wilcox rcu_read_unlock(); 1090478922e2SMatthew Wilcox return found; 1091478922e2SMatthew Wilcox } 1092478922e2SMatthew Wilcox 109346f65ec1SHugh Dickins /* 109446f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 109546f65ec1SHugh Dickins */ 109641ffe5d5SHugh Dickins static int shmem_unuse_inode(struct shmem_inode_info *info, 1097bde05d1cSHugh Dickins swp_entry_t swap, struct page **pagep) 10981da177e4SLinus Torvalds { 1099285b2c4fSHugh Dickins struct address_space *mapping = info->vfs_inode.i_mapping; 110046f65ec1SHugh Dickins void *radswap; 110141ffe5d5SHugh Dickins pgoff_t index; 1102bde05d1cSHugh Dickins gfp_t gfp; 1103bde05d1cSHugh Dickins int error = 0; 11041da177e4SLinus Torvalds 110546f65ec1SHugh Dickins radswap = swp_to_radix_entry(swap); 1106478922e2SMatthew Wilcox index = find_swap_entry(&mapping->page_tree, radswap); 110746f65ec1SHugh Dickins if (index == -1) 110800501b53SJohannes Weiner return -EAGAIN; /* tell shmem_unuse we found nothing */ 11092e0e26c7SHugh Dickins 11101b1b32f2SHugh Dickins /* 11111b1b32f2SHugh Dickins * Move _head_ to start search for next from here. 11121f895f75SAl Viro * But be careful: shmem_evict_inode checks list_empty without taking 11131b1b32f2SHugh Dickins * mutex, and there's an instant in list_move_tail when info->swaplist 1114285b2c4fSHugh Dickins * would appear empty, if it were the only one on shmem_swaplist. 11151b1b32f2SHugh Dickins */ 11161b1b32f2SHugh Dickins if (shmem_swaplist.next != &info->swaplist) 11172e0e26c7SHugh Dickins list_move_tail(&shmem_swaplist, &info->swaplist); 11182e0e26c7SHugh Dickins 1119bde05d1cSHugh Dickins gfp = mapping_gfp_mask(mapping); 1120bde05d1cSHugh Dickins if (shmem_should_replace_page(*pagep, gfp)) { 1121bde05d1cSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1122bde05d1cSHugh Dickins error = shmem_replace_page(pagep, gfp, info, index); 1123bde05d1cSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1124bde05d1cSHugh Dickins /* 1125bde05d1cSHugh Dickins * We needed to drop mutex to make that restrictive page 11260142ef6cSHugh Dickins * allocation, but the inode might have been freed while we 11270142ef6cSHugh Dickins * dropped it: although a racing shmem_evict_inode() cannot 11280142ef6cSHugh Dickins * complete without emptying the radix_tree, our page lock 11290142ef6cSHugh Dickins * on this swapcache page is not enough to prevent that - 11300142ef6cSHugh Dickins * free_swap_and_cache() of our swap entry will only 11310142ef6cSHugh Dickins * trylock_page(), removing swap from radix_tree whatever. 11320142ef6cSHugh Dickins * 11330142ef6cSHugh Dickins * We must not proceed to shmem_add_to_page_cache() if the 11340142ef6cSHugh Dickins * inode has been freed, but of course we cannot rely on 11350142ef6cSHugh Dickins * inode or mapping or info to check that. However, we can 11360142ef6cSHugh Dickins * safely check if our swap entry is still in use (and here 11370142ef6cSHugh Dickins * it can't have got reused for another page): if it's still 11380142ef6cSHugh Dickins * in use, then the inode cannot have been freed yet, and we 11390142ef6cSHugh Dickins * can safely proceed (if it's no longer in use, that tells 11400142ef6cSHugh Dickins * nothing about the inode, but we don't need to unuse swap). 1141bde05d1cSHugh Dickins */ 1142bde05d1cSHugh Dickins if (!page_swapcount(*pagep)) 1143bde05d1cSHugh Dickins error = -ENOENT; 1144bde05d1cSHugh Dickins } 1145bde05d1cSHugh Dickins 1146d13d1443SKAMEZAWA Hiroyuki /* 1147778dd893SHugh Dickins * We rely on shmem_swaplist_mutex, not only to protect the swaplist, 1148778dd893SHugh Dickins * but also to hold up shmem_evict_inode(): so inode cannot be freed 1149778dd893SHugh Dickins * beneath us (pagelock doesn't help until the page is in pagecache). 1150d13d1443SKAMEZAWA Hiroyuki */ 1151bde05d1cSHugh Dickins if (!error) 1152bde05d1cSHugh Dickins error = shmem_add_to_page_cache(*pagep, mapping, index, 1153fed400a1SWang Sheng-Hui radswap); 115448f170fbSHugh Dickins if (error != -ENOMEM) { 115546f65ec1SHugh Dickins /* 115646f65ec1SHugh Dickins * Truncation and eviction use free_swap_and_cache(), which 115746f65ec1SHugh Dickins * only does trylock page: if we raced, best clean up here. 115846f65ec1SHugh Dickins */ 1159bde05d1cSHugh Dickins delete_from_swap_cache(*pagep); 1160bde05d1cSHugh Dickins set_page_dirty(*pagep); 116146f65ec1SHugh Dickins if (!error) { 11624595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1163285b2c4fSHugh Dickins info->swapped--; 11644595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 116541ffe5d5SHugh Dickins swap_free(swap); 116646f65ec1SHugh Dickins } 11671da177e4SLinus Torvalds } 11682e0e26c7SHugh Dickins return error; 11691da177e4SLinus Torvalds } 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* 117246f65ec1SHugh Dickins * Search through swapped inodes to find and replace swap by page. 11731da177e4SLinus Torvalds */ 117441ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 11751da177e4SLinus Torvalds { 117641ffe5d5SHugh Dickins struct list_head *this, *next; 11771da177e4SLinus Torvalds struct shmem_inode_info *info; 117800501b53SJohannes Weiner struct mem_cgroup *memcg; 1179bde05d1cSHugh Dickins int error = 0; 1180bde05d1cSHugh Dickins 1181bde05d1cSHugh Dickins /* 1182bde05d1cSHugh Dickins * There's a faint possibility that swap page was replaced before 11830142ef6cSHugh Dickins * caller locked it: caller will come back later with the right page. 1184bde05d1cSHugh Dickins */ 11850142ef6cSHugh Dickins if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val)) 1186bde05d1cSHugh Dickins goto out; 1187778dd893SHugh Dickins 1188778dd893SHugh Dickins /* 1189778dd893SHugh Dickins * Charge page using GFP_KERNEL while we can wait, before taking 1190778dd893SHugh Dickins * the shmem_swaplist_mutex which might hold up shmem_writepage(). 1191778dd893SHugh Dickins * Charged back to the user (not to caller) when swap account is used. 1192778dd893SHugh Dickins */ 1193f627c2f5SKirill A. Shutemov error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg, 1194f627c2f5SKirill A. Shutemov false); 1195778dd893SHugh Dickins if (error) 1196778dd893SHugh Dickins goto out; 119746f65ec1SHugh Dickins /* No radix_tree_preload: swap entry keeps a place for page in tree */ 119800501b53SJohannes Weiner error = -EAGAIN; 11991da177e4SLinus Torvalds 1200cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 120141ffe5d5SHugh Dickins list_for_each_safe(this, next, &shmem_swaplist) { 120241ffe5d5SHugh Dickins info = list_entry(this, struct shmem_inode_info, swaplist); 1203285b2c4fSHugh Dickins if (info->swapped) 120400501b53SJohannes Weiner error = shmem_unuse_inode(info, swap, &page); 12056922c0c7SHugh Dickins else 12066922c0c7SHugh Dickins list_del_init(&info->swaplist); 1207cb5f7b9aSHugh Dickins cond_resched(); 120800501b53SJohannes Weiner if (error != -EAGAIN) 1209778dd893SHugh Dickins break; 121000501b53SJohannes Weiner /* found nothing in this: move on to search the next */ 12111da177e4SLinus Torvalds } 1212cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1213778dd893SHugh Dickins 121400501b53SJohannes Weiner if (error) { 121500501b53SJohannes Weiner if (error != -ENOMEM) 121600501b53SJohannes Weiner error = 0; 1217f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 121800501b53SJohannes Weiner } else 1219f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 1220778dd893SHugh Dickins out: 1221aaa46865SHugh Dickins unlock_page(page); 122209cbfeafSKirill A. Shutemov put_page(page); 1223778dd893SHugh Dickins return error; 12241da177e4SLinus Torvalds } 12251da177e4SLinus Torvalds 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 12281da177e4SLinus Torvalds */ 12291da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds struct shmem_inode_info *info; 12321da177e4SLinus Torvalds struct address_space *mapping; 12331da177e4SLinus Torvalds struct inode *inode; 12346922c0c7SHugh Dickins swp_entry_t swap; 12356922c0c7SHugh Dickins pgoff_t index; 12361da177e4SLinus Torvalds 1237800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 12381da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 12391da177e4SLinus Torvalds mapping = page->mapping; 12401da177e4SLinus Torvalds index = page->index; 12411da177e4SLinus Torvalds inode = mapping->host; 12421da177e4SLinus Torvalds info = SHMEM_I(inode); 12431da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 12441da177e4SLinus Torvalds goto redirty; 1245d9fe526aSHugh Dickins if (!total_swap_pages) 12461da177e4SLinus Torvalds goto redirty; 12471da177e4SLinus Torvalds 1248d9fe526aSHugh Dickins /* 124997b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 125097b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 125197b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 125297b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 125397b713baSChristoph Hellwig * threads or sync. 1254d9fe526aSHugh Dickins */ 125548f170fbSHugh Dickins if (!wbc->for_reclaim) { 125648f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 125748f170fbSHugh Dickins goto redirty; 125848f170fbSHugh Dickins } 12591635f6a7SHugh Dickins 12601635f6a7SHugh Dickins /* 12611635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 12621635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 12631635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 12641aac1400SHugh Dickins * 12651aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 12661aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 12671aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 12681aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 12691aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 12701635f6a7SHugh Dickins */ 12711635f6a7SHugh Dickins if (!PageUptodate(page)) { 12721aac1400SHugh Dickins if (inode->i_private) { 12731aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 12741aac1400SHugh Dickins spin_lock(&inode->i_lock); 12751aac1400SHugh Dickins shmem_falloc = inode->i_private; 12761aac1400SHugh Dickins if (shmem_falloc && 12778e205f77SHugh Dickins !shmem_falloc->waitq && 12781aac1400SHugh Dickins index >= shmem_falloc->start && 12791aac1400SHugh Dickins index < shmem_falloc->next) 12801aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 12811aac1400SHugh Dickins else 12821aac1400SHugh Dickins shmem_falloc = NULL; 12831aac1400SHugh Dickins spin_unlock(&inode->i_lock); 12841aac1400SHugh Dickins if (shmem_falloc) 12851aac1400SHugh Dickins goto redirty; 12861aac1400SHugh Dickins } 12871635f6a7SHugh Dickins clear_highpage(page); 12881635f6a7SHugh Dickins flush_dcache_page(page); 12891635f6a7SHugh Dickins SetPageUptodate(page); 12901635f6a7SHugh Dickins } 12911635f6a7SHugh Dickins 1292d9fe526aSHugh Dickins swap = get_swap_page(); 129348f170fbSHugh Dickins if (!swap.val) 129448f170fbSHugh Dickins goto redirty; 1295d9fe526aSHugh Dickins 129637e84351SVladimir Davydov if (mem_cgroup_try_charge_swap(page, swap)) 129737e84351SVladimir Davydov goto free_swap; 129837e84351SVladimir Davydov 1299b1dea800SHugh Dickins /* 1300b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 13016922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 13026922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1303b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13046922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13056922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1306b1dea800SHugh Dickins */ 1307b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 130805bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 130905bf86b4SHugh Dickins list_add_tail(&info->swaplist, &shmem_swaplist); 1310b1dea800SHugh Dickins 131148f170fbSHugh Dickins if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) { 13124595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1313267a4c76SHugh Dickins shmem_recalc_inode(inode); 1314267a4c76SHugh Dickins info->swapped++; 13154595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1316267a4c76SHugh Dickins 1317aaa46865SHugh Dickins swap_shmem_alloc(swap); 13186922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 13196922c0c7SHugh Dickins 13206922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1321d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 13229fab5619SHugh Dickins swap_writepage(page, wbc); 13231da177e4SLinus Torvalds return 0; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13266922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 132737e84351SVladimir Davydov free_swap: 13280a31bc97SJohannes Weiner swapcache_free(swap); 13291da177e4SLinus Torvalds redirty: 13301da177e4SLinus Torvalds set_page_dirty(page); 1331d9fe526aSHugh Dickins if (wbc->for_reclaim) 1332d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1333d9fe526aSHugh Dickins unlock_page(page); 1334d9fe526aSHugh Dickins return 0; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 133775edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 133871fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1339680d794bSakpm@linux-foundation.org { 1340680d794bSakpm@linux-foundation.org char buffer[64]; 1341680d794bSakpm@linux-foundation.org 134271fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1343095f1fc4SLee Schermerhorn return; /* show nothing */ 1344095f1fc4SLee Schermerhorn 1345a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1346095f1fc4SLee Schermerhorn 1347095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1348680d794bSakpm@linux-foundation.org } 134971fe804bSLee Schermerhorn 135071fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 135171fe804bSLee Schermerhorn { 135271fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 135371fe804bSLee Schermerhorn if (sbinfo->mpol) { 135471fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 135571fe804bSLee Schermerhorn mpol = sbinfo->mpol; 135671fe804bSLee Schermerhorn mpol_get(mpol); 135771fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 135871fe804bSLee Schermerhorn } 135971fe804bSLee Schermerhorn return mpol; 136071fe804bSLee Schermerhorn } 136175edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 136275edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 136375edd345SHugh Dickins { 136475edd345SHugh Dickins } 136575edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 136675edd345SHugh Dickins { 136775edd345SHugh Dickins return NULL; 136875edd345SHugh Dickins } 136975edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 137075edd345SHugh Dickins #ifndef CONFIG_NUMA 137175edd345SHugh Dickins #define vm_policy vm_private_data 137275edd345SHugh Dickins #endif 1373680d794bSakpm@linux-foundation.org 1374800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1375800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1376800d8c63SKirill A. Shutemov { 1377800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 1378800d8c63SKirill A. Shutemov vma->vm_start = 0; 1379800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1380800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1381800d8c63SKirill A. Shutemov vma->vm_ops = NULL; 1382800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1383800d8c63SKirill A. Shutemov } 1384800d8c63SKirill A. Shutemov 1385800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1386800d8c63SKirill A. Shutemov { 1387800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1388800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1389800d8c63SKirill A. Shutemov } 1390800d8c63SKirill A. Shutemov 139141ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 139241ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct vm_area_struct pvma; 139518a2f371SMel Gorman struct page *page; 13961da177e4SLinus Torvalds 1397800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 139818a2f371SMel Gorman page = swapin_readahead(swap, gfp, &pvma, 0); 1399800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 140018a2f371SMel Gorman 1401800d8c63SKirill A. Shutemov return page; 1402800d8c63SKirill A. Shutemov } 140318a2f371SMel Gorman 1404800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1405800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1406800d8c63SKirill A. Shutemov { 1407800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 1408800d8c63SKirill A. Shutemov struct inode *inode = &info->vfs_inode; 1409800d8c63SKirill A. Shutemov struct address_space *mapping = inode->i_mapping; 14104620a06eSGeert Uytterhoeven pgoff_t idx, hindex; 1411800d8c63SKirill A. Shutemov void __rcu **results; 1412800d8c63SKirill A. Shutemov struct page *page; 1413800d8c63SKirill A. Shutemov 1414e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1415800d8c63SKirill A. Shutemov return NULL; 1416800d8c63SKirill A. Shutemov 14174620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 1418800d8c63SKirill A. Shutemov rcu_read_lock(); 1419800d8c63SKirill A. Shutemov if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx, 1420800d8c63SKirill A. Shutemov hindex, 1) && idx < hindex + HPAGE_PMD_NR) { 1421800d8c63SKirill A. Shutemov rcu_read_unlock(); 1422800d8c63SKirill A. Shutemov return NULL; 1423800d8c63SKirill A. Shutemov } 1424800d8c63SKirill A. Shutemov rcu_read_unlock(); 1425800d8c63SKirill A. Shutemov 1426800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1427800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 1428800d8c63SKirill A. Shutemov HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1429800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1430800d8c63SKirill A. Shutemov if (page) 1431800d8c63SKirill A. Shutemov prep_transhuge_page(page); 143218a2f371SMel Gorman return page; 143318a2f371SMel Gorman } 143418a2f371SMel Gorman 143518a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 143618a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 143718a2f371SMel Gorman { 143818a2f371SMel Gorman struct vm_area_struct pvma; 143918a2f371SMel Gorman struct page *page; 144018a2f371SMel Gorman 1441800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1442800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1443800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 144418a2f371SMel Gorman 1445800d8c63SKirill A. Shutemov return page; 1446800d8c63SKirill A. Shutemov } 1447800d8c63SKirill A. Shutemov 1448800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 1449800d8c63SKirill A. Shutemov struct shmem_inode_info *info, struct shmem_sb_info *sbinfo, 1450800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1451800d8c63SKirill A. Shutemov { 1452800d8c63SKirill A. Shutemov struct page *page; 1453800d8c63SKirill A. Shutemov int nr; 1454800d8c63SKirill A. Shutemov int err = -ENOSPC; 1455800d8c63SKirill A. Shutemov 1456e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 1457800d8c63SKirill A. Shutemov huge = false; 1458800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1459800d8c63SKirill A. Shutemov 1460800d8c63SKirill A. Shutemov if (shmem_acct_block(info->flags, nr)) 1461800d8c63SKirill A. Shutemov goto failed; 1462800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) { 1463800d8c63SKirill A. Shutemov if (percpu_counter_compare(&sbinfo->used_blocks, 1464800d8c63SKirill A. Shutemov sbinfo->max_blocks - nr) > 0) 1465800d8c63SKirill A. Shutemov goto unacct; 1466800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, nr); 1467800d8c63SKirill A. Shutemov } 1468800d8c63SKirill A. Shutemov 1469800d8c63SKirill A. Shutemov if (huge) 1470800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1471800d8c63SKirill A. Shutemov else 1472800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 147375edd345SHugh Dickins if (page) { 147475edd345SHugh Dickins __SetPageLocked(page); 147575edd345SHugh Dickins __SetPageSwapBacked(page); 1476800d8c63SKirill A. Shutemov return page; 147775edd345SHugh Dickins } 147818a2f371SMel Gorman 1479800d8c63SKirill A. Shutemov err = -ENOMEM; 1480800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 1481800d8c63SKirill A. Shutemov percpu_counter_add(&sbinfo->used_blocks, -nr); 1482800d8c63SKirill A. Shutemov unacct: 1483800d8c63SKirill A. Shutemov shmem_unacct_blocks(info->flags, nr); 1484800d8c63SKirill A. Shutemov failed: 1485800d8c63SKirill A. Shutemov return ERR_PTR(err); 14861da177e4SLinus Torvalds } 148771fe804bSLee Schermerhorn 14881da177e4SLinus Torvalds /* 1489bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1490bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1491bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1492bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1493bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1494bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1495bde05d1cSHugh Dickins * 1496bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1497bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1498bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1499bde05d1cSHugh Dickins */ 1500bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1501bde05d1cSHugh Dickins { 1502bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1503bde05d1cSHugh Dickins } 1504bde05d1cSHugh Dickins 1505bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1506bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1507bde05d1cSHugh Dickins { 1508bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1509bde05d1cSHugh Dickins struct address_space *swap_mapping; 1510bde05d1cSHugh Dickins pgoff_t swap_index; 1511bde05d1cSHugh Dickins int error; 1512bde05d1cSHugh Dickins 1513bde05d1cSHugh Dickins oldpage = *pagep; 1514bde05d1cSHugh Dickins swap_index = page_private(oldpage); 1515bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1516bde05d1cSHugh Dickins 1517bde05d1cSHugh Dickins /* 1518bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1519bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1520bde05d1cSHugh Dickins */ 1521bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1522bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1523bde05d1cSHugh Dickins if (!newpage) 1524bde05d1cSHugh Dickins return -ENOMEM; 1525bde05d1cSHugh Dickins 152609cbfeafSKirill A. Shutemov get_page(newpage); 1527bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 15280142ef6cSHugh Dickins flush_dcache_page(newpage); 1529bde05d1cSHugh Dickins 15309956edf3SHugh Dickins __SetPageLocked(newpage); 15319956edf3SHugh Dickins __SetPageSwapBacked(newpage); 1532bde05d1cSHugh Dickins SetPageUptodate(newpage); 1533bde05d1cSHugh Dickins set_page_private(newpage, swap_index); 1534bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1535bde05d1cSHugh Dickins 1536bde05d1cSHugh Dickins /* 1537bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1538bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1539bde05d1cSHugh Dickins */ 1540bde05d1cSHugh Dickins spin_lock_irq(&swap_mapping->tree_lock); 1541bde05d1cSHugh Dickins error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage, 1542bde05d1cSHugh Dickins newpage); 15430142ef6cSHugh Dickins if (!error) { 154411fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 154511fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 15460142ef6cSHugh Dickins } 1547bde05d1cSHugh Dickins spin_unlock_irq(&swap_mapping->tree_lock); 1548bde05d1cSHugh Dickins 15490142ef6cSHugh Dickins if (unlikely(error)) { 15500142ef6cSHugh Dickins /* 15510142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15520142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15530142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15540142ef6cSHugh Dickins */ 15550142ef6cSHugh Dickins oldpage = newpage; 15560142ef6cSHugh Dickins } else { 15576a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1558bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 15590142ef6cSHugh Dickins *pagep = newpage; 15600142ef6cSHugh Dickins } 1561bde05d1cSHugh Dickins 1562bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1563bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1564bde05d1cSHugh Dickins 1565bde05d1cSHugh Dickins unlock_page(oldpage); 156609cbfeafSKirill A. Shutemov put_page(oldpage); 156709cbfeafSKirill A. Shutemov put_page(oldpage); 15680142ef6cSHugh Dickins return error; 1569bde05d1cSHugh Dickins } 1570bde05d1cSHugh Dickins 1571bde05d1cSHugh Dickins /* 157268da9f05SHugh Dickins * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 15731da177e4SLinus Torvalds * 15741da177e4SLinus Torvalds * If we allocate a new one we do not mark it dirty. That's up to the 15751da177e4SLinus Torvalds * vm. If we swap it in we mark it dirty since we also free the swap 15769e18eb29SAndres Lagar-Cavilla * entry since a page cannot live in both the swap and page cache. 15779e18eb29SAndres Lagar-Cavilla * 15789e18eb29SAndres Lagar-Cavilla * fault_mm and fault_type are only supplied by shmem_fault: 15799e18eb29SAndres Lagar-Cavilla * otherwise they are NULL. 15801da177e4SLinus Torvalds */ 158141ffe5d5SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 15829e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, gfp_t gfp, 1583cfda0526SMike Rapoport struct vm_area_struct *vma, struct vm_fault *vmf, int *fault_type) 15841da177e4SLinus Torvalds { 15851da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 158623f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 15871da177e4SLinus Torvalds struct shmem_sb_info *sbinfo; 15889e18eb29SAndres Lagar-Cavilla struct mm_struct *charge_mm; 158900501b53SJohannes Weiner struct mem_cgroup *memcg; 159027ab7006SHugh Dickins struct page *page; 15911da177e4SLinus Torvalds swp_entry_t swap; 1592657e3038SKirill A. Shutemov enum sgp_type sgp_huge = sgp; 1593800d8c63SKirill A. Shutemov pgoff_t hindex = index; 15941da177e4SLinus Torvalds int error; 159554af6042SHugh Dickins int once = 0; 15961635f6a7SHugh Dickins int alloced = 0; 15971da177e4SLinus Torvalds 159809cbfeafSKirill A. Shutemov if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 15991da177e4SLinus Torvalds return -EFBIG; 1600657e3038SKirill A. Shutemov if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1601657e3038SKirill A. Shutemov sgp = SGP_CACHE; 16021da177e4SLinus Torvalds repeat: 160354af6042SHugh Dickins swap.val = 0; 16040cd6144aSJohannes Weiner page = find_lock_entry(mapping, index); 160554af6042SHugh Dickins if (radix_tree_exceptional_entry(page)) { 160654af6042SHugh Dickins swap = radix_to_swp_entry(page); 160754af6042SHugh Dickins page = NULL; 160854af6042SHugh Dickins } 160954af6042SHugh Dickins 161075edd345SHugh Dickins if (sgp <= SGP_CACHE && 161109cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 161254af6042SHugh Dickins error = -EINVAL; 1613267a4c76SHugh Dickins goto unlock; 161454af6042SHugh Dickins } 161554af6042SHugh Dickins 161666d2f4d2SHugh Dickins if (page && sgp == SGP_WRITE) 161766d2f4d2SHugh Dickins mark_page_accessed(page); 161866d2f4d2SHugh Dickins 16191635f6a7SHugh Dickins /* fallocated page? */ 16201635f6a7SHugh Dickins if (page && !PageUptodate(page)) { 16211635f6a7SHugh Dickins if (sgp != SGP_READ) 16221635f6a7SHugh Dickins goto clear; 16231635f6a7SHugh Dickins unlock_page(page); 162409cbfeafSKirill A. Shutemov put_page(page); 16251635f6a7SHugh Dickins page = NULL; 16261635f6a7SHugh Dickins } 162754af6042SHugh Dickins if (page || (sgp == SGP_READ && !swap.val)) { 162854af6042SHugh Dickins *pagep = page; 162954af6042SHugh Dickins return 0; 163027ab7006SHugh Dickins } 163127ab7006SHugh Dickins 1632b409f9fcSHugh Dickins /* 163354af6042SHugh Dickins * Fast cache lookup did not find it: 163454af6042SHugh Dickins * bring it back from swap or allocate. 1635b409f9fcSHugh Dickins */ 163654af6042SHugh Dickins sbinfo = SHMEM_SB(inode->i_sb); 1637cfda0526SMike Rapoport charge_mm = vma ? vma->vm_mm : current->mm; 163827ab7006SHugh Dickins 16391da177e4SLinus Torvalds if (swap.val) { 16401da177e4SLinus Torvalds /* Look it up and read it in.. */ 164127ab7006SHugh Dickins page = lookup_swap_cache(swap); 164227ab7006SHugh Dickins if (!page) { 16439e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 16449e18eb29SAndres Lagar-Cavilla if (fault_type) { 164568da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16469e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 1647cfda0526SMike Rapoport mem_cgroup_count_vm_event(charge_mm, 1648cfda0526SMike Rapoport PGMAJFAULT); 16499e18eb29SAndres Lagar-Cavilla } 16509e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 165141ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 165227ab7006SHugh Dickins if (!page) { 16531da177e4SLinus Torvalds error = -ENOMEM; 165454af6042SHugh Dickins goto failed; 1655285b2c4fSHugh Dickins } 16561da177e4SLinus Torvalds } 16571da177e4SLinus Torvalds 16581da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 165954af6042SHugh Dickins lock_page(page); 16600142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1661d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1662bde05d1cSHugh Dickins error = -EEXIST; /* try again */ 1663d1899228SHugh Dickins goto unlock; 1664bde05d1cSHugh Dickins } 166527ab7006SHugh Dickins if (!PageUptodate(page)) { 16661da177e4SLinus Torvalds error = -EIO; 166754af6042SHugh Dickins goto failed; 166854af6042SHugh Dickins } 166954af6042SHugh Dickins wait_on_page_writeback(page); 167054af6042SHugh Dickins 1671bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1672bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1673bde05d1cSHugh Dickins if (error) 167454af6042SHugh Dickins goto failed; 16751da177e4SLinus Torvalds } 16761da177e4SLinus Torvalds 16779e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1678f627c2f5SKirill A. Shutemov false); 1679d1899228SHugh Dickins if (!error) { 168054af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1681fed400a1SWang Sheng-Hui swp_to_radix_entry(swap)); 1682215c02bcSHugh Dickins /* 1683215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1684215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1685215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1686215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1687215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1688215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1689215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 169093aa7d95SVladimir Davydov * the rest. 1691215c02bcSHugh Dickins * Reset swap.val? No, leave it so "failed" goes back to 1692215c02bcSHugh Dickins * "repeat": reading a hole and writing should succeed. 1693215c02bcSHugh Dickins */ 169400501b53SJohannes Weiner if (error) { 1695f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1696215c02bcSHugh Dickins delete_from_swap_cache(page); 1697d1899228SHugh Dickins } 169800501b53SJohannes Weiner } 169954af6042SHugh Dickins if (error) 170054af6042SHugh Dickins goto failed; 170154af6042SHugh Dickins 1702f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 170300501b53SJohannes Weiner 17044595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 170554af6042SHugh Dickins info->swapped--; 170654af6042SHugh Dickins shmem_recalc_inode(inode); 17074595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 170827ab7006SHugh Dickins 170966d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 171066d2f4d2SHugh Dickins mark_page_accessed(page); 171166d2f4d2SHugh Dickins 171227ab7006SHugh Dickins delete_from_swap_cache(page); 171327ab7006SHugh Dickins set_page_dirty(page); 171427ab7006SHugh Dickins swap_free(swap); 171527ab7006SHugh Dickins 171654af6042SHugh Dickins } else { 1717cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1718cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1719cfda0526SMike Rapoport return 0; 1720cfda0526SMike Rapoport } 1721cfda0526SMike Rapoport 1722800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1723800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1724800d8c63SKirill A. Shutemov goto alloc_nohuge; 1725657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1726800d8c63SKirill A. Shutemov goto alloc_nohuge; 1727800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1728800d8c63SKirill A. Shutemov goto alloc_huge; 1729800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1730800d8c63SKirill A. Shutemov loff_t i_size; 1731800d8c63SKirill A. Shutemov pgoff_t off; 1732800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1733800d8c63SKirill A. Shutemov goto alloc_nohuge; 1734800d8c63SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 1735800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1736800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1737800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1738800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1739800d8c63SKirill A. Shutemov goto alloc_huge; 1740800d8c63SKirill A. Shutemov /* fallthrough */ 1741800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1742657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1743657e3038SKirill A. Shutemov goto alloc_huge; 1744657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1745800d8c63SKirill A. Shutemov goto alloc_nohuge; 174659a16eadSHugh Dickins } 17471da177e4SLinus Torvalds 1748800d8c63SKirill A. Shutemov alloc_huge: 1749800d8c63SKirill A. Shutemov page = shmem_alloc_and_acct_page(gfp, info, sbinfo, 1750800d8c63SKirill A. Shutemov index, true); 1751800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1752800d8c63SKirill A. Shutemov alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, info, sbinfo, 1753800d8c63SKirill A. Shutemov index, false); 175454af6042SHugh Dickins } 1755800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1756779750d2SKirill A. Shutemov int retry = 5; 1757800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1758800d8c63SKirill A. Shutemov page = NULL; 1759779750d2SKirill A. Shutemov if (error != -ENOSPC) 1760779750d2SKirill A. Shutemov goto failed; 1761779750d2SKirill A. Shutemov /* 1762779750d2SKirill A. Shutemov * Try to reclaim some spece by splitting a huge page 1763779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1764779750d2SKirill A. Shutemov */ 1765779750d2SKirill A. Shutemov while (retry--) { 1766779750d2SKirill A. Shutemov int ret; 1767779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1768779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1769779750d2SKirill A. Shutemov break; 1770779750d2SKirill A. Shutemov if (ret) 1771779750d2SKirill A. Shutemov goto alloc_nohuge; 1772779750d2SKirill A. Shutemov } 1773800d8c63SKirill A. Shutemov goto failed; 1774800d8c63SKirill A. Shutemov } 1775800d8c63SKirill A. Shutemov 1776800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1777800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1778800d8c63SKirill A. Shutemov else 1779800d8c63SKirill A. Shutemov hindex = index; 1780800d8c63SKirill A. Shutemov 178166d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1782eb39d618SHugh Dickins __SetPageReferenced(page); 178366d2f4d2SHugh Dickins 17849e18eb29SAndres Lagar-Cavilla error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg, 1785800d8c63SKirill A. Shutemov PageTransHuge(page)); 178654af6042SHugh Dickins if (error) 1787800d8c63SKirill A. Shutemov goto unacct; 1788800d8c63SKirill A. Shutemov error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK, 1789800d8c63SKirill A. Shutemov compound_order(page)); 1790b065b432SHugh Dickins if (!error) { 1791800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1792fed400a1SWang Sheng-Hui NULL); 1793b065b432SHugh Dickins radix_tree_preload_end(); 1794b065b432SHugh Dickins } 1795b065b432SHugh Dickins if (error) { 1796800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1797800d8c63SKirill A. Shutemov PageTransHuge(page)); 1798800d8c63SKirill A. Shutemov goto unacct; 1799b065b432SHugh Dickins } 1800800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1801800d8c63SKirill A. Shutemov PageTransHuge(page)); 180254af6042SHugh Dickins lru_cache_add_anon(page); 180354af6042SHugh Dickins 18044595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1805800d8c63SKirill A. Shutemov info->alloced += 1 << compound_order(page); 1806800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 180754af6042SHugh Dickins shmem_recalc_inode(inode); 18084595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18091635f6a7SHugh Dickins alloced = true; 181054af6042SHugh Dickins 1811779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1812779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1813779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1814779750d2SKirill A. Shutemov /* 1815779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1816779750d2SKirill A. Shutemov * to shrink under memory pressure. 1817779750d2SKirill A. Shutemov */ 1818779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1819779750d2SKirill A. Shutemov if (list_empty(&info->shrinklist)) { 1820779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1821779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1822779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1823779750d2SKirill A. Shutemov } 1824779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1825779750d2SKirill A. Shutemov } 1826779750d2SKirill A. Shutemov 1827ec9516fbSHugh Dickins /* 18281635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 18291635f6a7SHugh Dickins */ 18301635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 18311635f6a7SHugh Dickins sgp = SGP_WRITE; 18321635f6a7SHugh Dickins clear: 18331635f6a7SHugh Dickins /* 18341635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 18351635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 18361635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1837ec9516fbSHugh Dickins */ 1838800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1839800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1840800d8c63SKirill A. Shutemov int i; 1841800d8c63SKirill A. Shutemov 1842800d8c63SKirill A. Shutemov for (i = 0; i < (1 << compound_order(head)); i++) { 1843800d8c63SKirill A. Shutemov clear_highpage(head + i); 1844800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1845800d8c63SKirill A. Shutemov } 1846800d8c63SKirill A. Shutemov SetPageUptodate(head); 1847ec9516fbSHugh Dickins } 18481da177e4SLinus Torvalds } 1849bde05d1cSHugh Dickins 185054af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 185175edd345SHugh Dickins if (sgp <= SGP_CACHE && 185209cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1853267a4c76SHugh Dickins if (alloced) { 1854267a4c76SHugh Dickins ClearPageDirty(page); 1855267a4c76SHugh Dickins delete_from_page_cache(page); 18564595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1857267a4c76SHugh Dickins shmem_recalc_inode(inode); 18584595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1859267a4c76SHugh Dickins } 186054af6042SHugh Dickins error = -EINVAL; 1861267a4c76SHugh Dickins goto unlock; 1862ff36b801SShaohua Li } 1863800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 186454af6042SHugh Dickins return 0; 1865d00806b1SNick Piggin 1866d0217ac0SNick Piggin /* 186754af6042SHugh Dickins * Error recovery. 18681da177e4SLinus Torvalds */ 186954af6042SHugh Dickins unacct: 1870800d8c63SKirill A. Shutemov if (sbinfo->max_blocks) 1871800d8c63SKirill A. Shutemov percpu_counter_sub(&sbinfo->used_blocks, 1872800d8c63SKirill A. Shutemov 1 << compound_order(page)); 1873800d8c63SKirill A. Shutemov shmem_unacct_blocks(info->flags, 1 << compound_order(page)); 1874800d8c63SKirill A. Shutemov 1875800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1876800d8c63SKirill A. Shutemov unlock_page(page); 1877800d8c63SKirill A. Shutemov put_page(page); 1878800d8c63SKirill A. Shutemov goto alloc_nohuge; 1879800d8c63SKirill A. Shutemov } 188054af6042SHugh Dickins failed: 1881267a4c76SHugh Dickins if (swap.val && !shmem_confirm_swap(mapping, index, swap)) 188254af6042SHugh Dickins error = -EEXIST; 1883d1899228SHugh Dickins unlock: 188427ab7006SHugh Dickins if (page) { 188554af6042SHugh Dickins unlock_page(page); 188609cbfeafSKirill A. Shutemov put_page(page); 188754af6042SHugh Dickins } 188854af6042SHugh Dickins if (error == -ENOSPC && !once++) { 18894595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 189054af6042SHugh Dickins shmem_recalc_inode(inode); 18914595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18921da177e4SLinus Torvalds goto repeat; 1893d8dc74f2SAdrian Bunk } 1894d1899228SHugh Dickins if (error == -EEXIST) /* from above or from radix_tree_insert */ 189554af6042SHugh Dickins goto repeat; 189654af6042SHugh Dickins return error; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 189910d20bd2SLinus Torvalds /* 190010d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 190110d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 190210d20bd2SLinus Torvalds * target. 190310d20bd2SLinus Torvalds */ 190410d20bd2SLinus Torvalds static int synchronous_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key) 190510d20bd2SLinus Torvalds { 190610d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 190710d20bd2SLinus Torvalds list_del_init(&wait->task_list); 190810d20bd2SLinus Torvalds return ret; 190910d20bd2SLinus Torvalds } 191010d20bd2SLinus Torvalds 191111bac800SDave Jiang static int shmem_fault(struct vm_fault *vmf) 19121da177e4SLinus Torvalds { 191311bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1914496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 19159e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1916657e3038SKirill A. Shutemov enum sgp_type sgp; 19171da177e4SLinus Torvalds int error; 191868da9f05SHugh Dickins int ret = VM_FAULT_LOCKED; 19191da177e4SLinus Torvalds 1920f00cdc6dSHugh Dickins /* 1921f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1922f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1923f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 19248e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 19258e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 19268e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 19278e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 19288e205f77SHugh Dickins * 19298e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 19308e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 19318e205f77SHugh Dickins * we just need to make racing faults a rare case. 19328e205f77SHugh Dickins * 19338e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 19348e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 19358e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 1936f00cdc6dSHugh Dickins */ 1937f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 1938f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 1939f00cdc6dSHugh Dickins 1940f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 1941f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 19428e205f77SHugh Dickins if (shmem_falloc && 19438e205f77SHugh Dickins shmem_falloc->waitq && 19448e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 19458e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 19468e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 194710d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 19488e205f77SHugh Dickins 19498e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 1950f00cdc6dSHugh Dickins if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) && 1951f00cdc6dSHugh Dickins !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) { 19528e205f77SHugh Dickins /* It's polite to up mmap_sem if we can */ 1953f00cdc6dSHugh Dickins up_read(&vma->vm_mm->mmap_sem); 19548e205f77SHugh Dickins ret = VM_FAULT_RETRY; 1955f00cdc6dSHugh Dickins } 19568e205f77SHugh Dickins 19578e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 19588e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 19598e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 19608e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19618e205f77SHugh Dickins schedule(); 19628e205f77SHugh Dickins 19638e205f77SHugh Dickins /* 19648e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 19658e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 19668e205f77SHugh Dickins * is usually invalid by the time we reach here, but 19678e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 19688e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 19698e205f77SHugh Dickins */ 19708e205f77SHugh Dickins spin_lock(&inode->i_lock); 19718e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 19728e205f77SHugh Dickins spin_unlock(&inode->i_lock); 19738e205f77SHugh Dickins return ret; 1974f00cdc6dSHugh Dickins } 19758e205f77SHugh Dickins spin_unlock(&inode->i_lock); 1976f00cdc6dSHugh Dickins } 1977f00cdc6dSHugh Dickins 1978657e3038SKirill A. Shutemov sgp = SGP_CACHE; 1979657e3038SKirill A. Shutemov if (vma->vm_flags & VM_HUGEPAGE) 1980657e3038SKirill A. Shutemov sgp = SGP_HUGE; 1981657e3038SKirill A. Shutemov else if (vma->vm_flags & VM_NOHUGEPAGE) 1982657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 1983657e3038SKirill A. Shutemov 1984657e3038SKirill A. Shutemov error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 1985cfda0526SMike Rapoport gfp, vma, vmf, &ret); 19861da177e4SLinus Torvalds if (error) 19871da177e4SLinus Torvalds return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS); 198868da9f05SHugh Dickins return ret; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds 1991c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 1992c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 1993c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 1994c01d5b30SHugh Dickins { 1995c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 1996c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 1997c01d5b30SHugh Dickins unsigned long addr; 1998c01d5b30SHugh Dickins unsigned long offset; 1999c01d5b30SHugh Dickins unsigned long inflated_len; 2000c01d5b30SHugh Dickins unsigned long inflated_addr; 2001c01d5b30SHugh Dickins unsigned long inflated_offset; 2002c01d5b30SHugh Dickins 2003c01d5b30SHugh Dickins if (len > TASK_SIZE) 2004c01d5b30SHugh Dickins return -ENOMEM; 2005c01d5b30SHugh Dickins 2006c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2007c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2008c01d5b30SHugh Dickins 2009e496cf3dSKirill A. Shutemov if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) 2010c01d5b30SHugh Dickins return addr; 2011c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2012c01d5b30SHugh Dickins return addr; 2013c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2014c01d5b30SHugh Dickins return addr; 2015c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2016c01d5b30SHugh Dickins return addr; 2017c01d5b30SHugh Dickins 2018c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2019c01d5b30SHugh Dickins return addr; 2020c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2021c01d5b30SHugh Dickins return addr; 2022c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2023c01d5b30SHugh Dickins return addr; 2024c01d5b30SHugh Dickins /* 2025c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2026c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 2027c01d5b30SHugh Dickins * But if caller specified an address hint, respect that as before. 2028c01d5b30SHugh Dickins */ 2029c01d5b30SHugh Dickins if (uaddr) 2030c01d5b30SHugh Dickins return addr; 2031c01d5b30SHugh Dickins 2032c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2033c01d5b30SHugh Dickins struct super_block *sb; 2034c01d5b30SHugh Dickins 2035c01d5b30SHugh Dickins if (file) { 2036c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2037c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2038c01d5b30SHugh Dickins } else { 2039c01d5b30SHugh Dickins /* 2040c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2041c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2042c01d5b30SHugh Dickins */ 2043c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2044c01d5b30SHugh Dickins return addr; 2045c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2046c01d5b30SHugh Dickins } 20473089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2048c01d5b30SHugh Dickins return addr; 2049c01d5b30SHugh Dickins } 2050c01d5b30SHugh Dickins 2051c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2052c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2053c01d5b30SHugh Dickins return addr; 2054c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2055c01d5b30SHugh Dickins return addr; 2056c01d5b30SHugh Dickins 2057c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2058c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2059c01d5b30SHugh Dickins return addr; 2060c01d5b30SHugh Dickins if (inflated_len < len) 2061c01d5b30SHugh Dickins return addr; 2062c01d5b30SHugh Dickins 2063c01d5b30SHugh Dickins inflated_addr = get_area(NULL, 0, inflated_len, 0, flags); 2064c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2065c01d5b30SHugh Dickins return addr; 2066c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2067c01d5b30SHugh Dickins return addr; 2068c01d5b30SHugh Dickins 2069c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2070c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2071c01d5b30SHugh Dickins if (inflated_offset > offset) 2072c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2073c01d5b30SHugh Dickins 2074c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2075c01d5b30SHugh Dickins return addr; 2076c01d5b30SHugh Dickins return inflated_addr; 2077c01d5b30SHugh Dickins } 2078c01d5b30SHugh Dickins 20791da177e4SLinus Torvalds #ifdef CONFIG_NUMA 208041ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 20811da177e4SLinus Torvalds { 2082496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 208341ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds 2086d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2087d8dc74f2SAdrian Bunk unsigned long addr) 20881da177e4SLinus Torvalds { 2089496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 209041ffe5d5SHugh Dickins pgoff_t index; 20911da177e4SLinus Torvalds 209241ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 209341ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds #endif 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 20981da177e4SLinus Torvalds { 2099496ad9aaSAl Viro struct inode *inode = file_inode(file); 21001da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 21011da177e4SLinus Torvalds int retval = -ENOMEM; 21021da177e4SLinus Torvalds 21034595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 21041da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 21051da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 21061da177e4SLinus Torvalds goto out_nomem; 21071da177e4SLinus Torvalds info->flags |= VM_LOCKED; 210889e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 21091da177e4SLinus Torvalds } 21101da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 21111da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 21121da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 211389e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds retval = 0; 211689e004eaSLee Schermerhorn 21171da177e4SLinus Torvalds out_nomem: 21184595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 21191da177e4SLinus Torvalds return retval; 21201da177e4SLinus Torvalds } 21211da177e4SLinus Torvalds 21229b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 21231da177e4SLinus Torvalds { 21241da177e4SLinus Torvalds file_accessed(file); 21251da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2126e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 2127f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2128f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2129f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2130f3f0e1d2SKirill A. Shutemov } 21311da177e4SLinus Torvalds return 0; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds 2134454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 213509208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 21361da177e4SLinus Torvalds { 21371da177e4SLinus Torvalds struct inode *inode; 21381da177e4SLinus Torvalds struct shmem_inode_info *info; 21391da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 21401da177e4SLinus Torvalds 21415b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 21421da177e4SLinus Torvalds return NULL; 21431da177e4SLinus Torvalds 21441da177e4SLinus Torvalds inode = new_inode(sb); 21451da177e4SLinus Torvalds if (inode) { 214685fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2147454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 21481da177e4SLinus Torvalds inode->i_blocks = 0; 2149078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 215091828a40SDavid M. Grimes inode->i_generation = get_seconds(); 21511da177e4SLinus Torvalds info = SHMEM_I(inode); 21521da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 21531da177e4SLinus Torvalds spin_lock_init(&info->lock); 215440e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 21550b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2156779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 21571da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 215838f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 215972c04902SAl Viro cache_no_acl(inode); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds switch (mode & S_IFMT) { 21621da177e4SLinus Torvalds default: 216339f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 21641da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 21651da177e4SLinus Torvalds break; 21661da177e4SLinus Torvalds case S_IFREG: 216714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 21681da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 21691da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 217071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 217171fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 21721da177e4SLinus Torvalds break; 21731da177e4SLinus Torvalds case S_IFDIR: 2174d8c76e6fSDave Hansen inc_nlink(inode); 21751da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 21761da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 21771da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 21781da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 21791da177e4SLinus Torvalds break; 21801da177e4SLinus Torvalds case S_IFLNK: 21811da177e4SLinus Torvalds /* 21821da177e4SLinus Torvalds * Must not load anything in the rbtree, 21831da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 21841da177e4SLinus Torvalds */ 218571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 21861da177e4SLinus Torvalds break; 21871da177e4SLinus Torvalds } 21885b04c689SPavel Emelyanov } else 21895b04c689SPavel Emelyanov shmem_free_inode(sb); 21901da177e4SLinus Torvalds return inode; 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 21930cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 21940cd6144aSJohannes Weiner { 2195f8005451SHugh Dickins return mapping->a_ops == &shmem_aops; 21960cd6144aSJohannes Weiner } 21970cd6144aSJohannes Weiner 21984c27fe4cSMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, 21994c27fe4cSMike Rapoport pmd_t *dst_pmd, 22004c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 22014c27fe4cSMike Rapoport unsigned long dst_addr, 22024c27fe4cSMike Rapoport unsigned long src_addr, 22034c27fe4cSMike Rapoport struct page **pagep) 22044c27fe4cSMike Rapoport { 22054c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 22064c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 22074c27fe4cSMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 22084c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 22094c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 22104c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 22114c27fe4cSMike Rapoport struct mem_cgroup *memcg; 22124c27fe4cSMike Rapoport spinlock_t *ptl; 22134c27fe4cSMike Rapoport void *page_kaddr; 22144c27fe4cSMike Rapoport struct page *page; 22154c27fe4cSMike Rapoport pte_t _dst_pte, *dst_pte; 22164c27fe4cSMike Rapoport int ret; 22174c27fe4cSMike Rapoport 22184c27fe4cSMike Rapoport ret = -ENOMEM; 22194c27fe4cSMike Rapoport if (shmem_acct_block(info->flags, 1)) 22204c27fe4cSMike Rapoport goto out; 22214c27fe4cSMike Rapoport if (sbinfo->max_blocks) { 22224c27fe4cSMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 22234c27fe4cSMike Rapoport sbinfo->max_blocks) >= 0) 22244c27fe4cSMike Rapoport goto out_unacct_blocks; 22254c27fe4cSMike Rapoport percpu_counter_inc(&sbinfo->used_blocks); 22264c27fe4cSMike Rapoport } 22274c27fe4cSMike Rapoport 2228cb658a45SAndrea Arcangeli if (!*pagep) { 22294c27fe4cSMike Rapoport page = shmem_alloc_page(gfp, info, pgoff); 22304c27fe4cSMike Rapoport if (!page) 22314c27fe4cSMike Rapoport goto out_dec_used_blocks; 22324c27fe4cSMike Rapoport 22334c27fe4cSMike Rapoport page_kaddr = kmap_atomic(page); 22344c27fe4cSMike Rapoport ret = copy_from_user(page_kaddr, (const void __user *)src_addr, 22354c27fe4cSMike Rapoport PAGE_SIZE); 22364c27fe4cSMike Rapoport kunmap_atomic(page_kaddr); 22374c27fe4cSMike Rapoport 22384c27fe4cSMike Rapoport /* fallback to copy_from_user outside mmap_sem */ 22394c27fe4cSMike Rapoport if (unlikely(ret)) { 22404c27fe4cSMike Rapoport *pagep = page; 2241cb658a45SAndrea Arcangeli if (sbinfo->max_blocks) 2242cb658a45SAndrea Arcangeli percpu_counter_add(&sbinfo->used_blocks, -1); 2243cb658a45SAndrea Arcangeli shmem_unacct_blocks(info->flags, 1); 22444c27fe4cSMike Rapoport /* don't free the page */ 22454c27fe4cSMike Rapoport return -EFAULT; 22464c27fe4cSMike Rapoport } 22474c27fe4cSMike Rapoport } else { 22484c27fe4cSMike Rapoport page = *pagep; 22494c27fe4cSMike Rapoport *pagep = NULL; 22504c27fe4cSMike Rapoport } 22514c27fe4cSMike Rapoport 22529cc90c66SAndrea Arcangeli VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); 22539cc90c66SAndrea Arcangeli __SetPageLocked(page); 22549cc90c66SAndrea Arcangeli __SetPageSwapBacked(page); 2255a425d358SAndrea Arcangeli __SetPageUptodate(page); 22569cc90c66SAndrea Arcangeli 22574c27fe4cSMike Rapoport ret = mem_cgroup_try_charge(page, dst_mm, gfp, &memcg, false); 22584c27fe4cSMike Rapoport if (ret) 22594c27fe4cSMike Rapoport goto out_release; 22604c27fe4cSMike Rapoport 22614c27fe4cSMike Rapoport ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK); 22624c27fe4cSMike Rapoport if (!ret) { 22634c27fe4cSMike Rapoport ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL); 22644c27fe4cSMike Rapoport radix_tree_preload_end(); 22654c27fe4cSMike Rapoport } 22664c27fe4cSMike Rapoport if (ret) 22674c27fe4cSMike Rapoport goto out_release_uncharge; 22684c27fe4cSMike Rapoport 22694c27fe4cSMike Rapoport mem_cgroup_commit_charge(page, memcg, false, false); 22704c27fe4cSMike Rapoport 22714c27fe4cSMike Rapoport _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 22724c27fe4cSMike Rapoport if (dst_vma->vm_flags & VM_WRITE) 22734c27fe4cSMike Rapoport _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); 22744c27fe4cSMike Rapoport 22754c27fe4cSMike Rapoport ret = -EEXIST; 22764c27fe4cSMike Rapoport dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 22774c27fe4cSMike Rapoport if (!pte_none(*dst_pte)) 22784c27fe4cSMike Rapoport goto out_release_uncharge_unlock; 22794c27fe4cSMike Rapoport 22804c27fe4cSMike Rapoport lru_cache_add_anon(page); 22814c27fe4cSMike Rapoport 22824c27fe4cSMike Rapoport spin_lock(&info->lock); 22834c27fe4cSMike Rapoport info->alloced++; 22844c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 22854c27fe4cSMike Rapoport shmem_recalc_inode(inode); 22864c27fe4cSMike Rapoport spin_unlock(&info->lock); 22874c27fe4cSMike Rapoport 22884c27fe4cSMike Rapoport inc_mm_counter(dst_mm, mm_counter_file(page)); 22894c27fe4cSMike Rapoport page_add_file_rmap(page, false); 22904c27fe4cSMike Rapoport set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 22914c27fe4cSMike Rapoport 22924c27fe4cSMike Rapoport /* No need to invalidate - it was non-present before */ 22934c27fe4cSMike Rapoport update_mmu_cache(dst_vma, dst_addr, dst_pte); 22944c27fe4cSMike Rapoport unlock_page(page); 22954c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 22964c27fe4cSMike Rapoport ret = 0; 22974c27fe4cSMike Rapoport out: 22984c27fe4cSMike Rapoport return ret; 22994c27fe4cSMike Rapoport out_release_uncharge_unlock: 23004c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 23014c27fe4cSMike Rapoport out_release_uncharge: 23024c27fe4cSMike Rapoport mem_cgroup_cancel_charge(page, memcg, false); 23034c27fe4cSMike Rapoport out_release: 23049cc90c66SAndrea Arcangeli unlock_page(page); 23054c27fe4cSMike Rapoport put_page(page); 23064c27fe4cSMike Rapoport out_dec_used_blocks: 23074c27fe4cSMike Rapoport if (sbinfo->max_blocks) 23084c27fe4cSMike Rapoport percpu_counter_add(&sbinfo->used_blocks, -1); 23094c27fe4cSMike Rapoport out_unacct_blocks: 23104c27fe4cSMike Rapoport shmem_unacct_blocks(info->flags, 1); 23114c27fe4cSMike Rapoport goto out; 23124c27fe4cSMike Rapoport } 23134c27fe4cSMike Rapoport 23141da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 231592e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 231669f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 23171da177e4SLinus Torvalds 23186d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 23196d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 23206d9d88d0SJarkko Sakkinen #else 23216d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 23226d9d88d0SJarkko Sakkinen #endif 23236d9d88d0SJarkko Sakkinen 23241da177e4SLinus Torvalds static int 2325800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2326800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2327800d15a5SNick Piggin struct page **pagep, void **fsdata) 23281da177e4SLinus Torvalds { 2329800d15a5SNick Piggin struct inode *inode = mapping->host; 233040e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 233109cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 233240e041a2SDavid Herrmann 233340e041a2SDavid Herrmann /* i_mutex is held by caller */ 2334*3f472cc9SSteven Rostedt (VMware) if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) { 233540e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) 233640e041a2SDavid Herrmann return -EPERM; 233740e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 233840e041a2SDavid Herrmann return -EPERM; 233940e041a2SDavid Herrmann } 234040e041a2SDavid Herrmann 23419e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2342800d15a5SNick Piggin } 2343800d15a5SNick Piggin 2344800d15a5SNick Piggin static int 2345800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2346800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2347800d15a5SNick Piggin struct page *page, void *fsdata) 2348800d15a5SNick Piggin { 2349800d15a5SNick Piggin struct inode *inode = mapping->host; 2350800d15a5SNick Piggin 2351800d15a5SNick Piggin if (pos + copied > inode->i_size) 2352800d15a5SNick Piggin i_size_write(inode, pos + copied); 2353800d15a5SNick Piggin 2354ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2355800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2356800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2357800d8c63SKirill A. Shutemov int i; 2358800d8c63SKirill A. Shutemov 2359800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2360800d8c63SKirill A. Shutemov if (head + i == page) 2361800d8c63SKirill A. Shutemov continue; 2362800d8c63SKirill A. Shutemov clear_highpage(head + i); 2363800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2364800d8c63SKirill A. Shutemov } 2365800d8c63SKirill A. Shutemov } 236609cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 236709cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2368ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 236909cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2370ec9516fbSHugh Dickins } 2371800d8c63SKirill A. Shutemov SetPageUptodate(head); 2372ec9516fbSHugh Dickins } 2373d3602444SHugh Dickins set_page_dirty(page); 23746746aff7SWu Fengguang unlock_page(page); 237509cbfeafSKirill A. Shutemov put_page(page); 2376d3602444SHugh Dickins 2377800d15a5SNick Piggin return copied; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 23802ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 23811da177e4SLinus Torvalds { 23826e58e79dSAl Viro struct file *file = iocb->ki_filp; 23836e58e79dSAl Viro struct inode *inode = file_inode(file); 23841da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 238541ffe5d5SHugh Dickins pgoff_t index; 238641ffe5d5SHugh Dickins unsigned long offset; 2387a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2388f7c1d074SGeert Uytterhoeven int error = 0; 2389cb66a7a1SAl Viro ssize_t retval = 0; 23906e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2391a0ee5ec5SHugh Dickins 2392a0ee5ec5SHugh Dickins /* 2393a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2394a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2395a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2396a0ee5ec5SHugh Dickins */ 2397777eda2cSAl Viro if (!iter_is_iovec(to)) 239875edd345SHugh Dickins sgp = SGP_CACHE; 23991da177e4SLinus Torvalds 240009cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 240109cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 24021da177e4SLinus Torvalds 24031da177e4SLinus Torvalds for (;;) { 24041da177e4SLinus Torvalds struct page *page = NULL; 240541ffe5d5SHugh Dickins pgoff_t end_index; 240641ffe5d5SHugh Dickins unsigned long nr, ret; 24071da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 24081da177e4SLinus Torvalds 240909cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24101da177e4SLinus Torvalds if (index > end_index) 24111da177e4SLinus Torvalds break; 24121da177e4SLinus Torvalds if (index == end_index) { 241309cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24141da177e4SLinus Torvalds if (nr <= offset) 24151da177e4SLinus Torvalds break; 24161da177e4SLinus Torvalds } 24171da177e4SLinus Torvalds 24189e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 24196e58e79dSAl Viro if (error) { 24206e58e79dSAl Viro if (error == -EINVAL) 24216e58e79dSAl Viro error = 0; 24221da177e4SLinus Torvalds break; 24231da177e4SLinus Torvalds } 242475edd345SHugh Dickins if (page) { 242575edd345SHugh Dickins if (sgp == SGP_CACHE) 242675edd345SHugh Dickins set_page_dirty(page); 2427d3602444SHugh Dickins unlock_page(page); 242875edd345SHugh Dickins } 24291da177e4SLinus Torvalds 24301da177e4SLinus Torvalds /* 24311da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 24321b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 24331da177e4SLinus Torvalds */ 243409cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 24351da177e4SLinus Torvalds i_size = i_size_read(inode); 243609cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 24371da177e4SLinus Torvalds if (index == end_index) { 243809cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 24391da177e4SLinus Torvalds if (nr <= offset) { 24401da177e4SLinus Torvalds if (page) 244109cbfeafSKirill A. Shutemov put_page(page); 24421da177e4SLinus Torvalds break; 24431da177e4SLinus Torvalds } 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds nr -= offset; 24461da177e4SLinus Torvalds 24471da177e4SLinus Torvalds if (page) { 24481da177e4SLinus Torvalds /* 24491da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 24501da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 24511da177e4SLinus Torvalds * before reading the page on the kernel side. 24521da177e4SLinus Torvalds */ 24531da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 24541da177e4SLinus Torvalds flush_dcache_page(page); 24551da177e4SLinus Torvalds /* 24561da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 24571da177e4SLinus Torvalds */ 24581da177e4SLinus Torvalds if (!offset) 24591da177e4SLinus Torvalds mark_page_accessed(page); 2460b5810039SNick Piggin } else { 24611da177e4SLinus Torvalds page = ZERO_PAGE(0); 246209cbfeafSKirill A. Shutemov get_page(page); 2463b5810039SNick Piggin } 24641da177e4SLinus Torvalds 24651da177e4SLinus Torvalds /* 24661da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 24671da177e4SLinus Torvalds * now we can copy it to user space... 24681da177e4SLinus Torvalds */ 24692ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 24706e58e79dSAl Viro retval += ret; 24711da177e4SLinus Torvalds offset += ret; 247209cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 247309cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 24741da177e4SLinus Torvalds 247509cbfeafSKirill A. Shutemov put_page(page); 24762ba5bbedSAl Viro if (!iov_iter_count(to)) 24771da177e4SLinus Torvalds break; 24786e58e79dSAl Viro if (ret < nr) { 24796e58e79dSAl Viro error = -EFAULT; 24806e58e79dSAl Viro break; 24816e58e79dSAl Viro } 24821da177e4SLinus Torvalds cond_resched(); 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 248509cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 24866e58e79dSAl Viro file_accessed(file); 24876e58e79dSAl Viro return retval ? retval : error; 24881da177e4SLinus Torvalds } 24891da177e4SLinus Torvalds 2490220f2ac9SHugh Dickins /* 2491220f2ac9SHugh Dickins * llseek SEEK_DATA or SEEK_HOLE through the radix_tree. 2492220f2ac9SHugh Dickins */ 2493220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2494965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2495220f2ac9SHugh Dickins { 2496220f2ac9SHugh Dickins struct page *page; 2497220f2ac9SHugh Dickins struct pagevec pvec; 2498220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2499220f2ac9SHugh Dickins bool done = false; 2500220f2ac9SHugh Dickins int i; 2501220f2ac9SHugh Dickins 2502220f2ac9SHugh Dickins pagevec_init(&pvec, 0); 2503220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2504220f2ac9SHugh Dickins while (!done) { 25050cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2506220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2507220f2ac9SHugh Dickins if (!pvec.nr) { 2508965c8e59SAndrew Morton if (whence == SEEK_DATA) 2509220f2ac9SHugh Dickins index = end; 2510220f2ac9SHugh Dickins break; 2511220f2ac9SHugh Dickins } 2512220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2513220f2ac9SHugh Dickins if (index < indices[i]) { 2514965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2515220f2ac9SHugh Dickins done = true; 2516220f2ac9SHugh Dickins break; 2517220f2ac9SHugh Dickins } 2518220f2ac9SHugh Dickins index = indices[i]; 2519220f2ac9SHugh Dickins } 2520220f2ac9SHugh Dickins page = pvec.pages[i]; 2521220f2ac9SHugh Dickins if (page && !radix_tree_exceptional_entry(page)) { 2522220f2ac9SHugh Dickins if (!PageUptodate(page)) 2523220f2ac9SHugh Dickins page = NULL; 2524220f2ac9SHugh Dickins } 2525220f2ac9SHugh Dickins if (index >= end || 2526965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2527965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2528220f2ac9SHugh Dickins done = true; 2529220f2ac9SHugh Dickins break; 2530220f2ac9SHugh Dickins } 2531220f2ac9SHugh Dickins } 25320cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2533220f2ac9SHugh Dickins pagevec_release(&pvec); 2534220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2535220f2ac9SHugh Dickins cond_resched(); 2536220f2ac9SHugh Dickins } 2537220f2ac9SHugh Dickins return index; 2538220f2ac9SHugh Dickins } 2539220f2ac9SHugh Dickins 2540965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2541220f2ac9SHugh Dickins { 2542220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2543220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2544220f2ac9SHugh Dickins pgoff_t start, end; 2545220f2ac9SHugh Dickins loff_t new_offset; 2546220f2ac9SHugh Dickins 2547965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2548965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2549220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 25505955102cSAl Viro inode_lock(inode); 2551220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2552220f2ac9SHugh Dickins 2553220f2ac9SHugh Dickins if (offset < 0) 2554220f2ac9SHugh Dickins offset = -EINVAL; 2555220f2ac9SHugh Dickins else if (offset >= inode->i_size) 2556220f2ac9SHugh Dickins offset = -ENXIO; 2557220f2ac9SHugh Dickins else { 255809cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 255909cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2560965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 256109cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2562220f2ac9SHugh Dickins if (new_offset > offset) { 2563220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2564220f2ac9SHugh Dickins offset = new_offset; 2565965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2566220f2ac9SHugh Dickins offset = -ENXIO; 2567220f2ac9SHugh Dickins else 2568220f2ac9SHugh Dickins offset = inode->i_size; 2569220f2ac9SHugh Dickins } 2570220f2ac9SHugh Dickins } 2571220f2ac9SHugh Dickins 2572387aae6fSHugh Dickins if (offset >= 0) 257346a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 25745955102cSAl Viro inode_unlock(inode); 2575220f2ac9SHugh Dickins return offset; 2576220f2ac9SHugh Dickins } 2577220f2ac9SHugh Dickins 257805f65b5cSDavid Herrmann /* 257905f65b5cSDavid Herrmann * We need a tag: a new tag would expand every radix_tree_node by 8 bytes, 258005f65b5cSDavid Herrmann * so reuse a tag which we firmly believe is never set or cleared on shmem. 258105f65b5cSDavid Herrmann */ 258205f65b5cSDavid Herrmann #define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE 258305f65b5cSDavid Herrmann #define LAST_SCAN 4 /* about 150ms max */ 258405f65b5cSDavid Herrmann 258505f65b5cSDavid Herrmann static void shmem_tag_pins(struct address_space *mapping) 258605f65b5cSDavid Herrmann { 258705f65b5cSDavid Herrmann struct radix_tree_iter iter; 258805f65b5cSDavid Herrmann void **slot; 258905f65b5cSDavid Herrmann pgoff_t start; 259005f65b5cSDavid Herrmann struct page *page; 259105f65b5cSDavid Herrmann 259205f65b5cSDavid Herrmann lru_add_drain(); 259305f65b5cSDavid Herrmann start = 0; 259405f65b5cSDavid Herrmann rcu_read_lock(); 259505f65b5cSDavid Herrmann 259605f65b5cSDavid Herrmann radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) { 259705f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 259805f65b5cSDavid Herrmann if (!page || radix_tree_exception(page)) { 25992cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 26002cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 26012cf938aaSMatthew Wilcox continue; 26022cf938aaSMatthew Wilcox } 260305f65b5cSDavid Herrmann } else if (page_count(page) - page_mapcount(page) > 1) { 260405f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 260505f65b5cSDavid Herrmann radix_tree_tag_set(&mapping->page_tree, iter.index, 260605f65b5cSDavid Herrmann SHMEM_TAG_PINNED); 260705f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 260805f65b5cSDavid Herrmann } 260905f65b5cSDavid Herrmann 261005f65b5cSDavid Herrmann if (need_resched()) { 2611148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 261205f65b5cSDavid Herrmann cond_resched_rcu(); 261305f65b5cSDavid Herrmann } 261405f65b5cSDavid Herrmann } 261505f65b5cSDavid Herrmann rcu_read_unlock(); 261605f65b5cSDavid Herrmann } 261705f65b5cSDavid Herrmann 261805f65b5cSDavid Herrmann /* 261905f65b5cSDavid Herrmann * Setting SEAL_WRITE requires us to verify there's no pending writer. However, 262005f65b5cSDavid Herrmann * via get_user_pages(), drivers might have some pending I/O without any active 262105f65b5cSDavid Herrmann * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages 262205f65b5cSDavid Herrmann * and see whether it has an elevated ref-count. If so, we tag them and wait for 262305f65b5cSDavid Herrmann * them to be dropped. 262405f65b5cSDavid Herrmann * The caller must guarantee that no new user will acquire writable references 262505f65b5cSDavid Herrmann * to those pages to avoid races. 262605f65b5cSDavid Herrmann */ 262740e041a2SDavid Herrmann static int shmem_wait_for_pins(struct address_space *mapping) 262840e041a2SDavid Herrmann { 262905f65b5cSDavid Herrmann struct radix_tree_iter iter; 263005f65b5cSDavid Herrmann void **slot; 263105f65b5cSDavid Herrmann pgoff_t start; 263205f65b5cSDavid Herrmann struct page *page; 263305f65b5cSDavid Herrmann int error, scan; 263405f65b5cSDavid Herrmann 263505f65b5cSDavid Herrmann shmem_tag_pins(mapping); 263605f65b5cSDavid Herrmann 263705f65b5cSDavid Herrmann error = 0; 263805f65b5cSDavid Herrmann for (scan = 0; scan <= LAST_SCAN; scan++) { 263905f65b5cSDavid Herrmann if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED)) 264005f65b5cSDavid Herrmann break; 264105f65b5cSDavid Herrmann 264205f65b5cSDavid Herrmann if (!scan) 264305f65b5cSDavid Herrmann lru_add_drain_all(); 264405f65b5cSDavid Herrmann else if (schedule_timeout_killable((HZ << scan) / 200)) 264505f65b5cSDavid Herrmann scan = LAST_SCAN; 264605f65b5cSDavid Herrmann 264705f65b5cSDavid Herrmann start = 0; 264805f65b5cSDavid Herrmann rcu_read_lock(); 264905f65b5cSDavid Herrmann radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter, 265005f65b5cSDavid Herrmann start, SHMEM_TAG_PINNED) { 265105f65b5cSDavid Herrmann 265205f65b5cSDavid Herrmann page = radix_tree_deref_slot(slot); 265305f65b5cSDavid Herrmann if (radix_tree_exception(page)) { 26542cf938aaSMatthew Wilcox if (radix_tree_deref_retry(page)) { 26552cf938aaSMatthew Wilcox slot = radix_tree_iter_retry(&iter); 26562cf938aaSMatthew Wilcox continue; 26572cf938aaSMatthew Wilcox } 265805f65b5cSDavid Herrmann 265905f65b5cSDavid Herrmann page = NULL; 266005f65b5cSDavid Herrmann } 266105f65b5cSDavid Herrmann 266205f65b5cSDavid Herrmann if (page && 266305f65b5cSDavid Herrmann page_count(page) - page_mapcount(page) != 1) { 266405f65b5cSDavid Herrmann if (scan < LAST_SCAN) 266505f65b5cSDavid Herrmann goto continue_resched; 266605f65b5cSDavid Herrmann 266705f65b5cSDavid Herrmann /* 266805f65b5cSDavid Herrmann * On the last scan, we clean up all those tags 266905f65b5cSDavid Herrmann * we inserted; but make a note that we still 267005f65b5cSDavid Herrmann * found pages pinned. 267105f65b5cSDavid Herrmann */ 267205f65b5cSDavid Herrmann error = -EBUSY; 267305f65b5cSDavid Herrmann } 267405f65b5cSDavid Herrmann 267505f65b5cSDavid Herrmann spin_lock_irq(&mapping->tree_lock); 267605f65b5cSDavid Herrmann radix_tree_tag_clear(&mapping->page_tree, 267705f65b5cSDavid Herrmann iter.index, SHMEM_TAG_PINNED); 267805f65b5cSDavid Herrmann spin_unlock_irq(&mapping->tree_lock); 267905f65b5cSDavid Herrmann continue_resched: 268005f65b5cSDavid Herrmann if (need_resched()) { 2681148deab2SMatthew Wilcox slot = radix_tree_iter_resume(slot, &iter); 268205f65b5cSDavid Herrmann cond_resched_rcu(); 268305f65b5cSDavid Herrmann } 268405f65b5cSDavid Herrmann } 268505f65b5cSDavid Herrmann rcu_read_unlock(); 268605f65b5cSDavid Herrmann } 268705f65b5cSDavid Herrmann 268805f65b5cSDavid Herrmann return error; 268940e041a2SDavid Herrmann } 269040e041a2SDavid Herrmann 269140e041a2SDavid Herrmann #define F_ALL_SEALS (F_SEAL_SEAL | \ 269240e041a2SDavid Herrmann F_SEAL_SHRINK | \ 269340e041a2SDavid Herrmann F_SEAL_GROW | \ 269440e041a2SDavid Herrmann F_SEAL_WRITE) 269540e041a2SDavid Herrmann 269640e041a2SDavid Herrmann int shmem_add_seals(struct file *file, unsigned int seals) 269740e041a2SDavid Herrmann { 269840e041a2SDavid Herrmann struct inode *inode = file_inode(file); 269940e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 270040e041a2SDavid Herrmann int error; 270140e041a2SDavid Herrmann 270240e041a2SDavid Herrmann /* 270340e041a2SDavid Herrmann * SEALING 270440e041a2SDavid Herrmann * Sealing allows multiple parties to share a shmem-file but restrict 270540e041a2SDavid Herrmann * access to a specific subset of file operations. Seals can only be 270640e041a2SDavid Herrmann * added, but never removed. This way, mutually untrusted parties can 270740e041a2SDavid Herrmann * share common memory regions with a well-defined policy. A malicious 270840e041a2SDavid Herrmann * peer can thus never perform unwanted operations on a shared object. 270940e041a2SDavid Herrmann * 271040e041a2SDavid Herrmann * Seals are only supported on special shmem-files and always affect 271140e041a2SDavid Herrmann * the whole underlying inode. Once a seal is set, it may prevent some 271240e041a2SDavid Herrmann * kinds of access to the file. Currently, the following seals are 271340e041a2SDavid Herrmann * defined: 271440e041a2SDavid Herrmann * SEAL_SEAL: Prevent further seals from being set on this file 271540e041a2SDavid Herrmann * SEAL_SHRINK: Prevent the file from shrinking 271640e041a2SDavid Herrmann * SEAL_GROW: Prevent the file from growing 271740e041a2SDavid Herrmann * SEAL_WRITE: Prevent write access to the file 271840e041a2SDavid Herrmann * 271940e041a2SDavid Herrmann * As we don't require any trust relationship between two parties, we 272040e041a2SDavid Herrmann * must prevent seals from being removed. Therefore, sealing a file 272140e041a2SDavid Herrmann * only adds a given set of seals to the file, it never touches 272240e041a2SDavid Herrmann * existing seals. Furthermore, the "setting seals"-operation can be 272340e041a2SDavid Herrmann * sealed itself, which basically prevents any further seal from being 272440e041a2SDavid Herrmann * added. 272540e041a2SDavid Herrmann * 272640e041a2SDavid Herrmann * Semantics of sealing are only defined on volatile files. Only 272740e041a2SDavid Herrmann * anonymous shmem files support sealing. More importantly, seals are 272840e041a2SDavid Herrmann * never written to disk. Therefore, there's no plan to support it on 272940e041a2SDavid Herrmann * other file types. 273040e041a2SDavid Herrmann */ 273140e041a2SDavid Herrmann 273240e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 273340e041a2SDavid Herrmann return -EINVAL; 273440e041a2SDavid Herrmann if (!(file->f_mode & FMODE_WRITE)) 273540e041a2SDavid Herrmann return -EPERM; 273640e041a2SDavid Herrmann if (seals & ~(unsigned int)F_ALL_SEALS) 273740e041a2SDavid Herrmann return -EINVAL; 273840e041a2SDavid Herrmann 27395955102cSAl Viro inode_lock(inode); 274040e041a2SDavid Herrmann 274140e041a2SDavid Herrmann if (info->seals & F_SEAL_SEAL) { 274240e041a2SDavid Herrmann error = -EPERM; 274340e041a2SDavid Herrmann goto unlock; 274440e041a2SDavid Herrmann } 274540e041a2SDavid Herrmann 274640e041a2SDavid Herrmann if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) { 274740e041a2SDavid Herrmann error = mapping_deny_writable(file->f_mapping); 274840e041a2SDavid Herrmann if (error) 274940e041a2SDavid Herrmann goto unlock; 275040e041a2SDavid Herrmann 275140e041a2SDavid Herrmann error = shmem_wait_for_pins(file->f_mapping); 275240e041a2SDavid Herrmann if (error) { 275340e041a2SDavid Herrmann mapping_allow_writable(file->f_mapping); 275440e041a2SDavid Herrmann goto unlock; 275540e041a2SDavid Herrmann } 275640e041a2SDavid Herrmann } 275740e041a2SDavid Herrmann 275840e041a2SDavid Herrmann info->seals |= seals; 275940e041a2SDavid Herrmann error = 0; 276040e041a2SDavid Herrmann 276140e041a2SDavid Herrmann unlock: 27625955102cSAl Viro inode_unlock(inode); 276340e041a2SDavid Herrmann return error; 276440e041a2SDavid Herrmann } 276540e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_add_seals); 276640e041a2SDavid Herrmann 276740e041a2SDavid Herrmann int shmem_get_seals(struct file *file) 276840e041a2SDavid Herrmann { 276940e041a2SDavid Herrmann if (file->f_op != &shmem_file_operations) 277040e041a2SDavid Herrmann return -EINVAL; 277140e041a2SDavid Herrmann 277240e041a2SDavid Herrmann return SHMEM_I(file_inode(file))->seals; 277340e041a2SDavid Herrmann } 277440e041a2SDavid Herrmann EXPORT_SYMBOL_GPL(shmem_get_seals); 277540e041a2SDavid Herrmann 277640e041a2SDavid Herrmann long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg) 277740e041a2SDavid Herrmann { 277840e041a2SDavid Herrmann long error; 277940e041a2SDavid Herrmann 278040e041a2SDavid Herrmann switch (cmd) { 278140e041a2SDavid Herrmann case F_ADD_SEALS: 278240e041a2SDavid Herrmann /* disallow upper 32bit */ 278340e041a2SDavid Herrmann if (arg > UINT_MAX) 278440e041a2SDavid Herrmann return -EINVAL; 278540e041a2SDavid Herrmann 278640e041a2SDavid Herrmann error = shmem_add_seals(file, arg); 278740e041a2SDavid Herrmann break; 278840e041a2SDavid Herrmann case F_GET_SEALS: 278940e041a2SDavid Herrmann error = shmem_get_seals(file); 279040e041a2SDavid Herrmann break; 279140e041a2SDavid Herrmann default: 279240e041a2SDavid Herrmann error = -EINVAL; 279340e041a2SDavid Herrmann break; 279440e041a2SDavid Herrmann } 279540e041a2SDavid Herrmann 279640e041a2SDavid Herrmann return error; 279740e041a2SDavid Herrmann } 279840e041a2SDavid Herrmann 279983e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 280083e4fa9cSHugh Dickins loff_t len) 280183e4fa9cSHugh Dickins { 2802496ad9aaSAl Viro struct inode *inode = file_inode(file); 2803e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 280440e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 28051aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2806e2d12e22SHugh Dickins pgoff_t start, index, end; 2807e2d12e22SHugh Dickins int error; 280883e4fa9cSHugh Dickins 280913ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 281013ace4d0SHugh Dickins return -EOPNOTSUPP; 281113ace4d0SHugh Dickins 28125955102cSAl Viro inode_lock(inode); 281383e4fa9cSHugh Dickins 281483e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 281583e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 281683e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 281783e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 28188e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 281983e4fa9cSHugh Dickins 282040e041a2SDavid Herrmann /* protected by i_mutex */ 282140e041a2SDavid Herrmann if (info->seals & F_SEAL_WRITE) { 282240e041a2SDavid Herrmann error = -EPERM; 282340e041a2SDavid Herrmann goto out; 282440e041a2SDavid Herrmann } 282540e041a2SDavid Herrmann 28268e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2827f00cdc6dSHugh Dickins shmem_falloc.start = unmap_start >> PAGE_SHIFT; 2828f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2829f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2830f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2831f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2832f00cdc6dSHugh Dickins 283383e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 283483e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 283583e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 283683e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 283783e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 28388e205f77SHugh Dickins 28398e205f77SHugh Dickins spin_lock(&inode->i_lock); 28408e205f77SHugh Dickins inode->i_private = NULL; 28418e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 284210d20bd2SLinus Torvalds WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.task_list)); 28438e205f77SHugh Dickins spin_unlock(&inode->i_lock); 284483e4fa9cSHugh Dickins error = 0; 28458e205f77SHugh Dickins goto out; 284683e4fa9cSHugh Dickins } 284783e4fa9cSHugh Dickins 2848e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2849e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2850e2d12e22SHugh Dickins if (error) 2851e2d12e22SHugh Dickins goto out; 2852e2d12e22SHugh Dickins 285340e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 285440e041a2SDavid Herrmann error = -EPERM; 285540e041a2SDavid Herrmann goto out; 285640e041a2SDavid Herrmann } 285740e041a2SDavid Herrmann 285809cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 285909cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2860e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2861e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2862e2d12e22SHugh Dickins error = -ENOSPC; 2863e2d12e22SHugh Dickins goto out; 2864e2d12e22SHugh Dickins } 2865e2d12e22SHugh Dickins 28668e205f77SHugh Dickins shmem_falloc.waitq = NULL; 28671aac1400SHugh Dickins shmem_falloc.start = start; 28681aac1400SHugh Dickins shmem_falloc.next = start; 28691aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 28701aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 28711aac1400SHugh Dickins spin_lock(&inode->i_lock); 28721aac1400SHugh Dickins inode->i_private = &shmem_falloc; 28731aac1400SHugh Dickins spin_unlock(&inode->i_lock); 28741aac1400SHugh Dickins 2875e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2876e2d12e22SHugh Dickins struct page *page; 2877e2d12e22SHugh Dickins 2878e2d12e22SHugh Dickins /* 2879e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2880e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2881e2d12e22SHugh Dickins */ 2882e2d12e22SHugh Dickins if (signal_pending(current)) 2883e2d12e22SHugh Dickins error = -EINTR; 28841aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 28851aac1400SHugh Dickins error = -ENOMEM; 2886e2d12e22SHugh Dickins else 28879e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2888e2d12e22SHugh Dickins if (error) { 28891635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 28907f556567SHugh Dickins if (index > start) { 28911635f6a7SHugh Dickins shmem_undo_range(inode, 289209cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2893b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 28947f556567SHugh Dickins } 28951aac1400SHugh Dickins goto undone; 2896e2d12e22SHugh Dickins } 2897e2d12e22SHugh Dickins 2898e2d12e22SHugh Dickins /* 28991aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 29001aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 29011aac1400SHugh Dickins */ 29021aac1400SHugh Dickins shmem_falloc.next++; 29031aac1400SHugh Dickins if (!PageUptodate(page)) 29041aac1400SHugh Dickins shmem_falloc.nr_falloced++; 29051aac1400SHugh Dickins 29061aac1400SHugh Dickins /* 29071635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 29081635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 29091635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2910e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2911e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2912e2d12e22SHugh Dickins */ 2913e2d12e22SHugh Dickins set_page_dirty(page); 2914e2d12e22SHugh Dickins unlock_page(page); 291509cbfeafSKirill A. Shutemov put_page(page); 2916e2d12e22SHugh Dickins cond_resched(); 2917e2d12e22SHugh Dickins } 2918e2d12e22SHugh Dickins 2919e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2920e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2921078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 29221aac1400SHugh Dickins undone: 29231aac1400SHugh Dickins spin_lock(&inode->i_lock); 29241aac1400SHugh Dickins inode->i_private = NULL; 29251aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2926e2d12e22SHugh Dickins out: 29275955102cSAl Viro inode_unlock(inode); 292883e4fa9cSHugh Dickins return error; 292983e4fa9cSHugh Dickins } 293083e4fa9cSHugh Dickins 2931726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 29321da177e4SLinus Torvalds { 2933726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 293609cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 29371da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 29380edd73b3SHugh Dickins if (sbinfo->max_blocks) { 29391da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 294041ffe5d5SHugh Dickins buf->f_bavail = 294141ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 294241ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 29430edd73b3SHugh Dickins } 29440edd73b3SHugh Dickins if (sbinfo->max_inodes) { 29451da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 29461da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 29471da177e4SLinus Torvalds } 29481da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 29491da177e4SLinus Torvalds return 0; 29501da177e4SLinus Torvalds } 29511da177e4SLinus Torvalds 29521da177e4SLinus Torvalds /* 29531da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 29541da177e4SLinus Torvalds */ 29551da177e4SLinus Torvalds static int 29561a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 29571da177e4SLinus Torvalds { 29580b0a0806SHugh Dickins struct inode *inode; 29591da177e4SLinus Torvalds int error = -ENOSPC; 29601da177e4SLinus Torvalds 2961454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 29621da177e4SLinus Torvalds if (inode) { 2963feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2964feda821eSChristoph Hellwig if (error) 2965feda821eSChristoph Hellwig goto out_iput; 29662a7dba39SEric Paris error = security_inode_init_security(inode, dir, 29679d8f13baSMimi Zohar &dentry->d_name, 29686d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2969feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2970feda821eSChristoph Hellwig goto out_iput; 297137ec43cdSMimi Zohar 2972718deb6bSAl Viro error = 0; 29731da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2974078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 29751da177e4SLinus Torvalds d_instantiate(dentry, inode); 29761da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 29771da177e4SLinus Torvalds } 29781da177e4SLinus Torvalds return error; 2979feda821eSChristoph Hellwig out_iput: 2980feda821eSChristoph Hellwig iput(inode); 2981feda821eSChristoph Hellwig return error; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 298460545d0dSAl Viro static int 298560545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 298660545d0dSAl Viro { 298760545d0dSAl Viro struct inode *inode; 298860545d0dSAl Viro int error = -ENOSPC; 298960545d0dSAl Viro 299060545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 299160545d0dSAl Viro if (inode) { 299260545d0dSAl Viro error = security_inode_init_security(inode, dir, 299360545d0dSAl Viro NULL, 299460545d0dSAl Viro shmem_initxattrs, NULL); 2995feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2996feda821eSChristoph Hellwig goto out_iput; 2997feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2998feda821eSChristoph Hellwig if (error) 2999feda821eSChristoph Hellwig goto out_iput; 300060545d0dSAl Viro d_tmpfile(dentry, inode); 300160545d0dSAl Viro } 300260545d0dSAl Viro return error; 3003feda821eSChristoph Hellwig out_iput: 3004feda821eSChristoph Hellwig iput(inode); 3005feda821eSChristoph Hellwig return error; 300660545d0dSAl Viro } 300760545d0dSAl Viro 300818bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 30091da177e4SLinus Torvalds { 30101da177e4SLinus Torvalds int error; 30111da177e4SLinus Torvalds 30121da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 30131da177e4SLinus Torvalds return error; 3014d8c76e6fSDave Hansen inc_nlink(dir); 30151da177e4SLinus Torvalds return 0; 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 30184acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 3019ebfc3b49SAl Viro bool excl) 30201da177e4SLinus Torvalds { 30211da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 30221da177e4SLinus Torvalds } 30231da177e4SLinus Torvalds 30241da177e4SLinus Torvalds /* 30251da177e4SLinus Torvalds * Link a file.. 30261da177e4SLinus Torvalds */ 30271da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 30281da177e4SLinus Torvalds { 302975c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30305b04c689SPavel Emelyanov int ret; 30311da177e4SLinus Torvalds 30321da177e4SLinus Torvalds /* 30331da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 30341da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 30351da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 30361da177e4SLinus Torvalds */ 30375b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 30385b04c689SPavel Emelyanov if (ret) 30395b04c689SPavel Emelyanov goto out; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3042078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 3043d8c76e6fSDave Hansen inc_nlink(inode); 30447de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 30451da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 30461da177e4SLinus Torvalds d_instantiate(dentry, inode); 30475b04c689SPavel Emelyanov out: 30485b04c689SPavel Emelyanov return ret; 30491da177e4SLinus Torvalds } 30501da177e4SLinus Torvalds 30511da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 30521da177e4SLinus Torvalds { 305375c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 30541da177e4SLinus Torvalds 30555b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 30565b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 30571da177e4SLinus Torvalds 30581da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 3059078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 30609a53c3a7SDave Hansen drop_nlink(inode); 30611da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 30621da177e4SLinus Torvalds return 0; 30631da177e4SLinus Torvalds } 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 30661da177e4SLinus Torvalds { 30671da177e4SLinus Torvalds if (!simple_empty(dentry)) 30681da177e4SLinus Torvalds return -ENOTEMPTY; 30691da177e4SLinus Torvalds 307075c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 30719a53c3a7SDave Hansen drop_nlink(dir); 30721da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds 307537456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 307637456771SMiklos Szeredi { 3077e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 3078e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 307937456771SMiklos Szeredi 308037456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 308137456771SMiklos Szeredi if (old_is_dir) { 308237456771SMiklos Szeredi drop_nlink(old_dir); 308337456771SMiklos Szeredi inc_nlink(new_dir); 308437456771SMiklos Szeredi } else { 308537456771SMiklos Szeredi drop_nlink(new_dir); 308637456771SMiklos Szeredi inc_nlink(old_dir); 308737456771SMiklos Szeredi } 308837456771SMiklos Szeredi } 308937456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 309037456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 309175c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 3092078cd827SDeepa Dinamani d_inode(new_dentry)->i_ctime = current_time(old_dir); 309337456771SMiklos Szeredi 309437456771SMiklos Szeredi return 0; 309537456771SMiklos Szeredi } 309637456771SMiklos Szeredi 309746fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 309846fdb794SMiklos Szeredi { 309946fdb794SMiklos Szeredi struct dentry *whiteout; 310046fdb794SMiklos Szeredi int error; 310146fdb794SMiklos Szeredi 310246fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 310346fdb794SMiklos Szeredi if (!whiteout) 310446fdb794SMiklos Szeredi return -ENOMEM; 310546fdb794SMiklos Szeredi 310646fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 310746fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 310846fdb794SMiklos Szeredi dput(whiteout); 310946fdb794SMiklos Szeredi if (error) 311046fdb794SMiklos Szeredi return error; 311146fdb794SMiklos Szeredi 311246fdb794SMiklos Szeredi /* 311346fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 311446fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 311546fdb794SMiklos Szeredi * 311646fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 311746fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 311846fdb794SMiklos Szeredi */ 311946fdb794SMiklos Szeredi d_rehash(whiteout); 312046fdb794SMiklos Szeredi return 0; 312146fdb794SMiklos Szeredi } 312246fdb794SMiklos Szeredi 31231da177e4SLinus Torvalds /* 31241da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 31251da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 31261da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 31271da177e4SLinus Torvalds * gets overwritten. 31281da177e4SLinus Torvalds */ 31293b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 31301da177e4SLinus Torvalds { 313175c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 31321da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 31331da177e4SLinus Torvalds 313446fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 31353b69ff51SMiklos Szeredi return -EINVAL; 31363b69ff51SMiklos Szeredi 313737456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 313837456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 313937456771SMiklos Szeredi 31401da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 31411da177e4SLinus Torvalds return -ENOTEMPTY; 31421da177e4SLinus Torvalds 314346fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 314446fdb794SMiklos Szeredi int error; 314546fdb794SMiklos Szeredi 314646fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 314746fdb794SMiklos Szeredi if (error) 314846fdb794SMiklos Szeredi return error; 314946fdb794SMiklos Szeredi } 315046fdb794SMiklos Szeredi 315175c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 31521da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3153b928095bSMiklos Szeredi if (they_are_dirs) { 315475c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 31559a53c3a7SDave Hansen drop_nlink(old_dir); 3156b928095bSMiklos Szeredi } 31571da177e4SLinus Torvalds } else if (they_are_dirs) { 31589a53c3a7SDave Hansen drop_nlink(old_dir); 3159d8c76e6fSDave Hansen inc_nlink(new_dir); 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 31631da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 31641da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 31651da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3166078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 31671da177e4SLinus Torvalds return 0; 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds 31701da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds int error; 31731da177e4SLinus Torvalds int len; 31741da177e4SLinus Torvalds struct inode *inode; 31759276aad6SHugh Dickins struct page *page; 31761da177e4SLinus Torvalds struct shmem_inode_info *info; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds len = strlen(symname) + 1; 317909cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 31801da177e4SLinus Torvalds return -ENAMETOOLONG; 31811da177e4SLinus Torvalds 3182454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE); 31831da177e4SLinus Torvalds if (!inode) 31841da177e4SLinus Torvalds return -ENOSPC; 31851da177e4SLinus Torvalds 31869d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 31876d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3188570bc1c2SStephen Smalley if (error) { 3189570bc1c2SStephen Smalley if (error != -EOPNOTSUPP) { 3190570bc1c2SStephen Smalley iput(inode); 3191570bc1c2SStephen Smalley return error; 3192570bc1c2SStephen Smalley } 3193570bc1c2SStephen Smalley error = 0; 3194570bc1c2SStephen Smalley } 3195570bc1c2SStephen Smalley 31961da177e4SLinus Torvalds info = SHMEM_I(inode); 31971da177e4SLinus Torvalds inode->i_size = len-1; 319869f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31993ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 32003ed47db3SAl Viro if (!inode->i_link) { 320169f07ec9SHugh Dickins iput(inode); 320269f07ec9SHugh Dickins return -ENOMEM; 320369f07ec9SHugh Dickins } 320469f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 32051da177e4SLinus Torvalds } else { 3206e8ecde25SAl Viro inode_nohighmem(inode); 32079e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 32081da177e4SLinus Torvalds if (error) { 32091da177e4SLinus Torvalds iput(inode); 32101da177e4SLinus Torvalds return error; 32111da177e4SLinus Torvalds } 321214fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 32131da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 321421fc61c7SAl Viro memcpy(page_address(page), symname, len); 3215ec9516fbSHugh Dickins SetPageUptodate(page); 32161da177e4SLinus Torvalds set_page_dirty(page); 32176746aff7SWu Fengguang unlock_page(page); 321809cbfeafSKirill A. Shutemov put_page(page); 32191da177e4SLinus Torvalds } 32201da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3221078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 32221da177e4SLinus Torvalds d_instantiate(dentry, inode); 32231da177e4SLinus Torvalds dget(dentry); 32241da177e4SLinus Torvalds return 0; 32251da177e4SLinus Torvalds } 32261da177e4SLinus Torvalds 3227fceef393SAl Viro static void shmem_put_link(void *arg) 3228fceef393SAl Viro { 3229fceef393SAl Viro mark_page_accessed(arg); 3230fceef393SAl Viro put_page(arg); 3231fceef393SAl Viro } 3232fceef393SAl Viro 32336b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3234fceef393SAl Viro struct inode *inode, 3235fceef393SAl Viro struct delayed_call *done) 32361da177e4SLinus Torvalds { 32371da177e4SLinus Torvalds struct page *page = NULL; 32386b255391SAl Viro int error; 32396a6c9904SAl Viro if (!dentry) { 32406a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 32416a6c9904SAl Viro if (!page) 32426b255391SAl Viro return ERR_PTR(-ECHILD); 32436a6c9904SAl Viro if (!PageUptodate(page)) { 32446a6c9904SAl Viro put_page(page); 32456a6c9904SAl Viro return ERR_PTR(-ECHILD); 32466a6c9904SAl Viro } 32476a6c9904SAl Viro } else { 32489e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3249680baacbSAl Viro if (error) 3250680baacbSAl Viro return ERR_PTR(error); 3251d3602444SHugh Dickins unlock_page(page); 32521da177e4SLinus Torvalds } 3253fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 325421fc61c7SAl Viro return page_address(page); 32551da177e4SLinus Torvalds } 32561da177e4SLinus Torvalds 3257b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3258b09e0fa4SEric Paris /* 3259b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3260b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3261b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3262b09e0fa4SEric Paris * filesystem level, though. 3263b09e0fa4SEric Paris */ 3264b09e0fa4SEric Paris 32656d9d88d0SJarkko Sakkinen /* 32666d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 32676d9d88d0SJarkko Sakkinen */ 32686d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 32696d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 32706d9d88d0SJarkko Sakkinen void *fs_info) 32716d9d88d0SJarkko Sakkinen { 32726d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 32736d9d88d0SJarkko Sakkinen const struct xattr *xattr; 327438f38657SAristeu Rozanski struct simple_xattr *new_xattr; 32756d9d88d0SJarkko Sakkinen size_t len; 32766d9d88d0SJarkko Sakkinen 32776d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 327838f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32796d9d88d0SJarkko Sakkinen if (!new_xattr) 32806d9d88d0SJarkko Sakkinen return -ENOMEM; 32816d9d88d0SJarkko Sakkinen 32826d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32836d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32846d9d88d0SJarkko Sakkinen GFP_KERNEL); 32856d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32866d9d88d0SJarkko Sakkinen kfree(new_xattr); 32876d9d88d0SJarkko Sakkinen return -ENOMEM; 32886d9d88d0SJarkko Sakkinen } 32896d9d88d0SJarkko Sakkinen 32906d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32916d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32926d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32936d9d88d0SJarkko Sakkinen xattr->name, len); 32946d9d88d0SJarkko Sakkinen 329538f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 32966d9d88d0SJarkko Sakkinen } 32976d9d88d0SJarkko Sakkinen 32986d9d88d0SJarkko Sakkinen return 0; 32996d9d88d0SJarkko Sakkinen } 33006d9d88d0SJarkko Sakkinen 3301aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3302b296821aSAl Viro struct dentry *unused, struct inode *inode, 3303b296821aSAl Viro const char *name, void *buffer, size_t size) 3304aa7c5241SAndreas Gruenbacher { 3305b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3306aa7c5241SAndreas Gruenbacher 3307aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3308aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3309aa7c5241SAndreas Gruenbacher } 3310aa7c5241SAndreas Gruenbacher 3311aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 331259301226SAl Viro struct dentry *unused, struct inode *inode, 331359301226SAl Viro const char *name, const void *value, 331459301226SAl Viro size_t size, int flags) 3315aa7c5241SAndreas Gruenbacher { 331659301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3317aa7c5241SAndreas Gruenbacher 3318aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3319aa7c5241SAndreas Gruenbacher return simple_xattr_set(&info->xattrs, name, value, size, flags); 3320aa7c5241SAndreas Gruenbacher } 3321aa7c5241SAndreas Gruenbacher 3322aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3323aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3324aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3325aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3326aa7c5241SAndreas Gruenbacher }; 3327aa7c5241SAndreas Gruenbacher 3328aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3329aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3330aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3331aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3332aa7c5241SAndreas Gruenbacher }; 3333aa7c5241SAndreas Gruenbacher 3334b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3335b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3336feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3337feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3338b09e0fa4SEric Paris #endif 3339aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3340aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3341b09e0fa4SEric Paris NULL 3342b09e0fa4SEric Paris }; 3343b09e0fa4SEric Paris 3344b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3345b09e0fa4SEric Paris { 334675c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3347786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3348b09e0fa4SEric Paris } 3349b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3350b09e0fa4SEric Paris 335169f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 33526b255391SAl Viro .get_link = simple_get_link, 3353b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3354b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3355b09e0fa4SEric Paris #endif 33561da177e4SLinus Torvalds }; 33571da177e4SLinus Torvalds 335892e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 33596b255391SAl Viro .get_link = shmem_get_link, 3360b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3361b09e0fa4SEric Paris .listxattr = shmem_listxattr, 336239f0247dSAndreas Gruenbacher #endif 3363b09e0fa4SEric Paris }; 336439f0247dSAndreas Gruenbacher 336591828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 336691828a40SDavid M. Grimes { 336791828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 336891828a40SDavid M. Grimes } 336991828a40SDavid M. Grimes 337091828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 337191828a40SDavid M. Grimes { 337291828a40SDavid M. Grimes __u32 *fh = vfh; 337391828a40SDavid M. Grimes __u64 inum = fh[2]; 337491828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 337591828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 337691828a40SDavid M. Grimes } 337791828a40SDavid M. Grimes 3378480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3379480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 338091828a40SDavid M. Grimes { 338191828a40SDavid M. Grimes struct inode *inode; 3382480b116cSChristoph Hellwig struct dentry *dentry = NULL; 338335c2a7f4SHugh Dickins u64 inum; 338491828a40SDavid M. Grimes 3385480b116cSChristoph Hellwig if (fh_len < 3) 3386480b116cSChristoph Hellwig return NULL; 3387480b116cSChristoph Hellwig 338835c2a7f4SHugh Dickins inum = fid->raw[2]; 338935c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 339035c2a7f4SHugh Dickins 3391480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3392480b116cSChristoph Hellwig shmem_match, fid->raw); 339391828a40SDavid M. Grimes if (inode) { 3394480b116cSChristoph Hellwig dentry = d_find_alias(inode); 339591828a40SDavid M. Grimes iput(inode); 339691828a40SDavid M. Grimes } 339791828a40SDavid M. Grimes 3398480b116cSChristoph Hellwig return dentry; 339991828a40SDavid M. Grimes } 340091828a40SDavid M. Grimes 3401b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3402b0b0382bSAl Viro struct inode *parent) 340391828a40SDavid M. Grimes { 34045fe0c237SAneesh Kumar K.V if (*len < 3) { 34055fe0c237SAneesh Kumar K.V *len = 3; 340694e07a75SNamjae Jeon return FILEID_INVALID; 34075fe0c237SAneesh Kumar K.V } 340891828a40SDavid M. Grimes 34091d3382cbSAl Viro if (inode_unhashed(inode)) { 341091828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 341191828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 341291828a40SDavid M. Grimes * time, we need a lock to ensure we only try 341391828a40SDavid M. Grimes * to do it once 341491828a40SDavid M. Grimes */ 341591828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 341691828a40SDavid M. Grimes spin_lock(&lock); 34171d3382cbSAl Viro if (inode_unhashed(inode)) 341891828a40SDavid M. Grimes __insert_inode_hash(inode, 341991828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 342091828a40SDavid M. Grimes spin_unlock(&lock); 342191828a40SDavid M. Grimes } 342291828a40SDavid M. Grimes 342391828a40SDavid M. Grimes fh[0] = inode->i_generation; 342491828a40SDavid M. Grimes fh[1] = inode->i_ino; 342591828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 342691828a40SDavid M. Grimes 342791828a40SDavid M. Grimes *len = 3; 342891828a40SDavid M. Grimes return 1; 342991828a40SDavid M. Grimes } 343091828a40SDavid M. Grimes 343139655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 343291828a40SDavid M. Grimes .get_parent = shmem_get_parent, 343391828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3434480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 343591828a40SDavid M. Grimes }; 343691828a40SDavid M. Grimes 3437680d794bSakpm@linux-foundation.org static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo, 3438680d794bSakpm@linux-foundation.org bool remount) 34391da177e4SLinus Torvalds { 34401da177e4SLinus Torvalds char *this_char, *value, *rest; 344149cd0a5cSGreg Thelen struct mempolicy *mpol = NULL; 34428751e039SEric W. Biederman uid_t uid; 34438751e039SEric W. Biederman gid_t gid; 34441da177e4SLinus Torvalds 3445b00dc3adSHugh Dickins while (options != NULL) { 3446b00dc3adSHugh Dickins this_char = options; 3447b00dc3adSHugh Dickins for (;;) { 3448b00dc3adSHugh Dickins /* 3449b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3450b00dc3adSHugh Dickins * mount options form a comma-separated list, 3451b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3452b00dc3adSHugh Dickins */ 3453b00dc3adSHugh Dickins options = strchr(options, ','); 3454b00dc3adSHugh Dickins if (options == NULL) 3455b00dc3adSHugh Dickins break; 3456b00dc3adSHugh Dickins options++; 3457b00dc3adSHugh Dickins if (!isdigit(*options)) { 3458b00dc3adSHugh Dickins options[-1] = '\0'; 3459b00dc3adSHugh Dickins break; 3460b00dc3adSHugh Dickins } 3461b00dc3adSHugh Dickins } 34621da177e4SLinus Torvalds if (!*this_char) 34631da177e4SLinus Torvalds continue; 34641da177e4SLinus Torvalds if ((value = strchr(this_char,'=')) != NULL) { 34651da177e4SLinus Torvalds *value++ = 0; 34661da177e4SLinus Torvalds } else { 34671170532bSJoe Perches pr_err("tmpfs: No value for mount option '%s'\n", 34681da177e4SLinus Torvalds this_char); 346949cd0a5cSGreg Thelen goto error; 34701da177e4SLinus Torvalds } 34711da177e4SLinus Torvalds 34721da177e4SLinus Torvalds if (!strcmp(this_char,"size")) { 34731da177e4SLinus Torvalds unsigned long long size; 34741da177e4SLinus Torvalds size = memparse(value,&rest); 34751da177e4SLinus Torvalds if (*rest == '%') { 34761da177e4SLinus Torvalds size <<= PAGE_SHIFT; 34771da177e4SLinus Torvalds size *= totalram_pages; 34781da177e4SLinus Torvalds do_div(size, 100); 34791da177e4SLinus Torvalds rest++; 34801da177e4SLinus Torvalds } 34811da177e4SLinus Torvalds if (*rest) 34821da177e4SLinus Torvalds goto bad_val; 3483680d794bSakpm@linux-foundation.org sbinfo->max_blocks = 348409cbfeafSKirill A. Shutemov DIV_ROUND_UP(size, PAGE_SIZE); 34851da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_blocks")) { 3486680d794bSakpm@linux-foundation.org sbinfo->max_blocks = memparse(value, &rest); 34871da177e4SLinus Torvalds if (*rest) 34881da177e4SLinus Torvalds goto bad_val; 34891da177e4SLinus Torvalds } else if (!strcmp(this_char,"nr_inodes")) { 3490680d794bSakpm@linux-foundation.org sbinfo->max_inodes = memparse(value, &rest); 34911da177e4SLinus Torvalds if (*rest) 34921da177e4SLinus Torvalds goto bad_val; 34931da177e4SLinus Torvalds } else if (!strcmp(this_char,"mode")) { 3494680d794bSakpm@linux-foundation.org if (remount) 34951da177e4SLinus Torvalds continue; 3496680d794bSakpm@linux-foundation.org sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777; 34971da177e4SLinus Torvalds if (*rest) 34981da177e4SLinus Torvalds goto bad_val; 34991da177e4SLinus Torvalds } else if (!strcmp(this_char,"uid")) { 3500680d794bSakpm@linux-foundation.org if (remount) 35011da177e4SLinus Torvalds continue; 35028751e039SEric W. Biederman uid = simple_strtoul(value, &rest, 0); 35031da177e4SLinus Torvalds if (*rest) 35041da177e4SLinus Torvalds goto bad_val; 35058751e039SEric W. Biederman sbinfo->uid = make_kuid(current_user_ns(), uid); 35068751e039SEric W. Biederman if (!uid_valid(sbinfo->uid)) 35078751e039SEric W. Biederman goto bad_val; 35081da177e4SLinus Torvalds } else if (!strcmp(this_char,"gid")) { 3509680d794bSakpm@linux-foundation.org if (remount) 35101da177e4SLinus Torvalds continue; 35118751e039SEric W. Biederman gid = simple_strtoul(value, &rest, 0); 35121da177e4SLinus Torvalds if (*rest) 35131da177e4SLinus Torvalds goto bad_val; 35148751e039SEric W. Biederman sbinfo->gid = make_kgid(current_user_ns(), gid); 35158751e039SEric W. Biederman if (!gid_valid(sbinfo->gid)) 35168751e039SEric W. Biederman goto bad_val; 3517e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 35185a6e75f8SKirill A. Shutemov } else if (!strcmp(this_char, "huge")) { 35195a6e75f8SKirill A. Shutemov int huge; 35205a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(value); 35215a6e75f8SKirill A. Shutemov if (huge < 0) 35225a6e75f8SKirill A. Shutemov goto bad_val; 35235a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 35245a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER) 35255a6e75f8SKirill A. Shutemov goto bad_val; 35265a6e75f8SKirill A. Shutemov sbinfo->huge = huge; 35275a6e75f8SKirill A. Shutemov #endif 35285a6e75f8SKirill A. Shutemov #ifdef CONFIG_NUMA 35297339ff83SRobin Holt } else if (!strcmp(this_char,"mpol")) { 353049cd0a5cSGreg Thelen mpol_put(mpol); 353149cd0a5cSGreg Thelen mpol = NULL; 353249cd0a5cSGreg Thelen if (mpol_parse_str(value, &mpol)) 35337339ff83SRobin Holt goto bad_val; 35345a6e75f8SKirill A. Shutemov #endif 35351da177e4SLinus Torvalds } else { 35361170532bSJoe Perches pr_err("tmpfs: Bad mount option %s\n", this_char); 353749cd0a5cSGreg Thelen goto error; 35381da177e4SLinus Torvalds } 35391da177e4SLinus Torvalds } 354049cd0a5cSGreg Thelen sbinfo->mpol = mpol; 35411da177e4SLinus Torvalds return 0; 35421da177e4SLinus Torvalds 35431da177e4SLinus Torvalds bad_val: 35441170532bSJoe Perches pr_err("tmpfs: Bad value '%s' for mount option '%s'\n", 35451da177e4SLinus Torvalds value, this_char); 354649cd0a5cSGreg Thelen error: 354749cd0a5cSGreg Thelen mpol_put(mpol); 35481da177e4SLinus Torvalds return 1; 35491da177e4SLinus Torvalds 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds static int shmem_remount_fs(struct super_block *sb, int *flags, char *data) 35531da177e4SLinus Torvalds { 35541da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3555680d794bSakpm@linux-foundation.org struct shmem_sb_info config = *sbinfo; 35560edd73b3SHugh Dickins unsigned long inodes; 35570edd73b3SHugh Dickins int error = -EINVAL; 35581da177e4SLinus Torvalds 35595f00110fSGreg Thelen config.mpol = NULL; 3560680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, &config, true)) 35610edd73b3SHugh Dickins return error; 35620edd73b3SHugh Dickins 35630edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 35640edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 35657e496299STim Chen if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0) 35660edd73b3SHugh Dickins goto out; 3567680d794bSakpm@linux-foundation.org if (config.max_inodes < inodes) 35680edd73b3SHugh Dickins goto out; 35690edd73b3SHugh Dickins /* 357054af6042SHugh Dickins * Those tests disallow limited->unlimited while any are in use; 35710edd73b3SHugh Dickins * but we must separately disallow unlimited->limited, because 35720edd73b3SHugh Dickins * in that case we have no record of how much is already in use. 35730edd73b3SHugh Dickins */ 3574680d794bSakpm@linux-foundation.org if (config.max_blocks && !sbinfo->max_blocks) 35750edd73b3SHugh Dickins goto out; 3576680d794bSakpm@linux-foundation.org if (config.max_inodes && !sbinfo->max_inodes) 35770edd73b3SHugh Dickins goto out; 35780edd73b3SHugh Dickins 35790edd73b3SHugh Dickins error = 0; 35805a6e75f8SKirill A. Shutemov sbinfo->huge = config.huge; 3581680d794bSakpm@linux-foundation.org sbinfo->max_blocks = config.max_blocks; 3582680d794bSakpm@linux-foundation.org sbinfo->max_inodes = config.max_inodes; 3583680d794bSakpm@linux-foundation.org sbinfo->free_inodes = config.max_inodes - inodes; 358471fe804bSLee Schermerhorn 35855f00110fSGreg Thelen /* 35865f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 35875f00110fSGreg Thelen */ 35885f00110fSGreg Thelen if (config.mpol) { 358971fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 359071fe804bSLee Schermerhorn sbinfo->mpol = config.mpol; /* transfers initial ref */ 35915f00110fSGreg Thelen } 35920edd73b3SHugh Dickins out: 35930edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 35940edd73b3SHugh Dickins return error; 35951da177e4SLinus Torvalds } 3596680d794bSakpm@linux-foundation.org 359734c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3598680d794bSakpm@linux-foundation.org { 359934c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3600680d794bSakpm@linux-foundation.org 3601680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3602680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 360309cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3604680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3605680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 3606680d794bSakpm@linux-foundation.org if (sbinfo->mode != (S_IRWXUGO | S_ISVTX)) 360709208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 36088751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 36098751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 36108751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 36118751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 36128751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 36138751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3614e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 36155a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 36165a6e75f8SKirill A. Shutemov if (sbinfo->huge) 36175a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 36185a6e75f8SKirill A. Shutemov #endif 361971fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3620680d794bSakpm@linux-foundation.org return 0; 3621680d794bSakpm@linux-foundation.org } 36229183df25SDavid Herrmann 36239183df25SDavid Herrmann #define MFD_NAME_PREFIX "memfd:" 36249183df25SDavid Herrmann #define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1) 36259183df25SDavid Herrmann #define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN) 36269183df25SDavid Herrmann 36279183df25SDavid Herrmann #define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING) 36289183df25SDavid Herrmann 36299183df25SDavid Herrmann SYSCALL_DEFINE2(memfd_create, 36309183df25SDavid Herrmann const char __user *, uname, 36319183df25SDavid Herrmann unsigned int, flags) 36329183df25SDavid Herrmann { 36339183df25SDavid Herrmann struct shmem_inode_info *info; 36349183df25SDavid Herrmann struct file *file; 36359183df25SDavid Herrmann int fd, error; 36369183df25SDavid Herrmann char *name; 36379183df25SDavid Herrmann long len; 36389183df25SDavid Herrmann 36399183df25SDavid Herrmann if (flags & ~(unsigned int)MFD_ALL_FLAGS) 36409183df25SDavid Herrmann return -EINVAL; 36419183df25SDavid Herrmann 36429183df25SDavid Herrmann /* length includes terminating zero */ 36439183df25SDavid Herrmann len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1); 36449183df25SDavid Herrmann if (len <= 0) 36459183df25SDavid Herrmann return -EFAULT; 36469183df25SDavid Herrmann if (len > MFD_NAME_MAX_LEN + 1) 36479183df25SDavid Herrmann return -EINVAL; 36489183df25SDavid Herrmann 36499183df25SDavid Herrmann name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_TEMPORARY); 36509183df25SDavid Herrmann if (!name) 36519183df25SDavid Herrmann return -ENOMEM; 36529183df25SDavid Herrmann 36539183df25SDavid Herrmann strcpy(name, MFD_NAME_PREFIX); 36549183df25SDavid Herrmann if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) { 36559183df25SDavid Herrmann error = -EFAULT; 36569183df25SDavid Herrmann goto err_name; 36579183df25SDavid Herrmann } 36589183df25SDavid Herrmann 36599183df25SDavid Herrmann /* terminating-zero may have changed after strnlen_user() returned */ 36609183df25SDavid Herrmann if (name[len + MFD_NAME_PREFIX_LEN - 1]) { 36619183df25SDavid Herrmann error = -EFAULT; 36629183df25SDavid Herrmann goto err_name; 36639183df25SDavid Herrmann } 36649183df25SDavid Herrmann 36659183df25SDavid Herrmann fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0); 36669183df25SDavid Herrmann if (fd < 0) { 36679183df25SDavid Herrmann error = fd; 36689183df25SDavid Herrmann goto err_name; 36699183df25SDavid Herrmann } 36709183df25SDavid Herrmann 36719183df25SDavid Herrmann file = shmem_file_setup(name, 0, VM_NORESERVE); 36729183df25SDavid Herrmann if (IS_ERR(file)) { 36739183df25SDavid Herrmann error = PTR_ERR(file); 36749183df25SDavid Herrmann goto err_fd; 36759183df25SDavid Herrmann } 36769183df25SDavid Herrmann info = SHMEM_I(file_inode(file)); 36779183df25SDavid Herrmann file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE; 36789183df25SDavid Herrmann file->f_flags |= O_RDWR | O_LARGEFILE; 36799183df25SDavid Herrmann if (flags & MFD_ALLOW_SEALING) 36809183df25SDavid Herrmann info->seals &= ~F_SEAL_SEAL; 36819183df25SDavid Herrmann 36829183df25SDavid Herrmann fd_install(fd, file); 36839183df25SDavid Herrmann kfree(name); 36849183df25SDavid Herrmann return fd; 36859183df25SDavid Herrmann 36869183df25SDavid Herrmann err_fd: 36879183df25SDavid Herrmann put_unused_fd(fd); 36889183df25SDavid Herrmann err_name: 36899183df25SDavid Herrmann kfree(name); 36909183df25SDavid Herrmann return error; 36919183df25SDavid Herrmann } 36929183df25SDavid Herrmann 3693680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 36941da177e4SLinus Torvalds 36951da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 36961da177e4SLinus Torvalds { 3697602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3698602586a8SHugh Dickins 3699602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 370049cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3701602586a8SHugh Dickins kfree(sbinfo); 37021da177e4SLinus Torvalds sb->s_fs_info = NULL; 37031da177e4SLinus Torvalds } 37041da177e4SLinus Torvalds 37052b2af54aSKay Sievers int shmem_fill_super(struct super_block *sb, void *data, int silent) 37061da177e4SLinus Torvalds { 37071da177e4SLinus Torvalds struct inode *inode; 37080edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3709680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3710680d794bSakpm@linux-foundation.org 3711680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3712425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3713680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3714680d794bSakpm@linux-foundation.org if (!sbinfo) 3715680d794bSakpm@linux-foundation.org return -ENOMEM; 3716680d794bSakpm@linux-foundation.org 3717680d794bSakpm@linux-foundation.org sbinfo->mode = S_IRWXUGO | S_ISVTX; 371876aac0e9SDavid Howells sbinfo->uid = current_fsuid(); 371976aac0e9SDavid Howells sbinfo->gid = current_fsgid(); 3720680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 37211da177e4SLinus Torvalds 37220edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 37231da177e4SLinus Torvalds /* 37241da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 37251da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 37261da177e4SLinus Torvalds * but the internal instance is left unlimited. 37271da177e4SLinus Torvalds */ 3728ca4e0519SAl Viro if (!(sb->s_flags & MS_KERNMOUNT)) { 3729680d794bSakpm@linux-foundation.org sbinfo->max_blocks = shmem_default_max_blocks(); 3730680d794bSakpm@linux-foundation.org sbinfo->max_inodes = shmem_default_max_inodes(); 3731680d794bSakpm@linux-foundation.org if (shmem_parse_options(data, sbinfo, false)) { 3732680d794bSakpm@linux-foundation.org err = -EINVAL; 3733680d794bSakpm@linux-foundation.org goto failed; 3734680d794bSakpm@linux-foundation.org } 3735ca4e0519SAl Viro } else { 3736ca4e0519SAl Viro sb->s_flags |= MS_NOUSER; 37371da177e4SLinus Torvalds } 373891828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 37392f6e38f3SHugh Dickins sb->s_flags |= MS_NOSEC; 37400edd73b3SHugh Dickins #else 37410edd73b3SHugh Dickins sb->s_flags |= MS_NOUSER; 37420edd73b3SHugh Dickins #endif 37431da177e4SLinus Torvalds 37441da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3745908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3746602586a8SHugh Dickins goto failed; 3747680d794bSakpm@linux-foundation.org sbinfo->free_inodes = sbinfo->max_inodes; 3748779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3749779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 37501da177e4SLinus Torvalds 3751285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 375209cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 375309cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 37541da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 37551da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3756cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3757b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 375839f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3759b09e0fa4SEric Paris #endif 3760b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 376139f0247dSAndreas Gruenbacher sb->s_flags |= MS_POSIXACL; 376239f0247dSAndreas Gruenbacher #endif 37630edd73b3SHugh Dickins 3764454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 37651da177e4SLinus Torvalds if (!inode) 37661da177e4SLinus Torvalds goto failed; 3767680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3768680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3769318ceed0SAl Viro sb->s_root = d_make_root(inode); 3770318ceed0SAl Viro if (!sb->s_root) 377148fde701SAl Viro goto failed; 37721da177e4SLinus Torvalds return 0; 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds failed: 37751da177e4SLinus Torvalds shmem_put_super(sb); 37761da177e4SLinus Torvalds return err; 37771da177e4SLinus Torvalds } 37781da177e4SLinus Torvalds 3779fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 37801da177e4SLinus Torvalds 37811da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 37821da177e4SLinus Torvalds { 378341ffe5d5SHugh Dickins struct shmem_inode_info *info; 378441ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 378541ffe5d5SHugh Dickins if (!info) 37861da177e4SLinus Torvalds return NULL; 378741ffe5d5SHugh Dickins return &info->vfs_inode; 37881da177e4SLinus Torvalds } 37891da177e4SLinus Torvalds 379041ffe5d5SHugh Dickins static void shmem_destroy_callback(struct rcu_head *head) 3791fa0d7e3dSNick Piggin { 3792fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 379384e710daSAl Viro if (S_ISLNK(inode->i_mode)) 37943ed47db3SAl Viro kfree(inode->i_link); 3795fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3796fa0d7e3dSNick Piggin } 3797fa0d7e3dSNick Piggin 37981da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 37991da177e4SLinus Torvalds { 380009208d15SAl Viro if (S_ISREG(inode->i_mode)) 38011da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 380241ffe5d5SHugh Dickins call_rcu(&inode->i_rcu, shmem_destroy_callback); 38031da177e4SLinus Torvalds } 38041da177e4SLinus Torvalds 380541ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 38061da177e4SLinus Torvalds { 380741ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 380841ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 38091da177e4SLinus Torvalds } 38101da177e4SLinus Torvalds 381141ffe5d5SHugh Dickins static int shmem_init_inodecache(void) 38121da177e4SLinus Torvalds { 38131da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 38141da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 38155d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 38161da177e4SLinus Torvalds return 0; 38171da177e4SLinus Torvalds } 38181da177e4SLinus Torvalds 381941ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 38201da177e4SLinus Torvalds { 38211a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 38221da177e4SLinus Torvalds } 38231da177e4SLinus Torvalds 3824f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 38251da177e4SLinus Torvalds .writepage = shmem_writepage, 382676719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 38271da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3828800d15a5SNick Piggin .write_begin = shmem_write_begin, 3829800d15a5SNick Piggin .write_end = shmem_write_end, 38301da177e4SLinus Torvalds #endif 38311c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3832304dbdb7SLee Schermerhorn .migratepage = migrate_page, 38331c93923cSAndrew Morton #endif 3834aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 38351da177e4SLinus Torvalds }; 38361da177e4SLinus Torvalds 383715ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 38381da177e4SLinus Torvalds .mmap = shmem_mmap, 3839c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 38401da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3841220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 38422ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 38438174202bSAl Viro .write_iter = generic_file_write_iter, 38441b061d92SChristoph Hellwig .fsync = noop_fsync, 384582c156f8SAl Viro .splice_read = generic_file_splice_read, 3846f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 384783e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 38481da177e4SLinus Torvalds #endif 38491da177e4SLinus Torvalds }; 38501da177e4SLinus Torvalds 385192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 385244a30220SYu Zhao .getattr = shmem_getattr, 385394c1e62dSHugh Dickins .setattr = shmem_setattr, 3854b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3855b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3856feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3857b09e0fa4SEric Paris #endif 38581da177e4SLinus Torvalds }; 38591da177e4SLinus Torvalds 386092e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 38611da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38621da177e4SLinus Torvalds .create = shmem_create, 38631da177e4SLinus Torvalds .lookup = simple_lookup, 38641da177e4SLinus Torvalds .link = shmem_link, 38651da177e4SLinus Torvalds .unlink = shmem_unlink, 38661da177e4SLinus Torvalds .symlink = shmem_symlink, 38671da177e4SLinus Torvalds .mkdir = shmem_mkdir, 38681da177e4SLinus Torvalds .rmdir = shmem_rmdir, 38691da177e4SLinus Torvalds .mknod = shmem_mknod, 38702773bf00SMiklos Szeredi .rename = shmem_rename2, 387160545d0dSAl Viro .tmpfile = shmem_tmpfile, 38721da177e4SLinus Torvalds #endif 3873b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3874b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3875b09e0fa4SEric Paris #endif 387639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 387794c1e62dSHugh Dickins .setattr = shmem_setattr, 3878feda821eSChristoph Hellwig .set_acl = simple_set_acl, 387939f0247dSAndreas Gruenbacher #endif 388039f0247dSAndreas Gruenbacher }; 388139f0247dSAndreas Gruenbacher 388292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3883b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3884b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3885b09e0fa4SEric Paris #endif 388639f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 388794c1e62dSHugh Dickins .setattr = shmem_setattr, 3888feda821eSChristoph Hellwig .set_acl = simple_set_acl, 388939f0247dSAndreas Gruenbacher #endif 38901da177e4SLinus Torvalds }; 38911da177e4SLinus Torvalds 3892759b9775SHugh Dickins static const struct super_operations shmem_ops = { 38931da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 38941da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 38951da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38961da177e4SLinus Torvalds .statfs = shmem_statfs, 38971da177e4SLinus Torvalds .remount_fs = shmem_remount_fs, 3898680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 38991da177e4SLinus Torvalds #endif 39001f895f75SAl Viro .evict_inode = shmem_evict_inode, 39011da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 39021da177e4SLinus Torvalds .put_super = shmem_put_super, 3903779750d2SKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 3904779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3905779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3906779750d2SKirill A. Shutemov #endif 39071da177e4SLinus Torvalds }; 39081da177e4SLinus Torvalds 3909f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 391054cb8821SNick Piggin .fault = shmem_fault, 3911d7c17551SNing Qu .map_pages = filemap_map_pages, 39121da177e4SLinus Torvalds #ifdef CONFIG_NUMA 39131da177e4SLinus Torvalds .set_policy = shmem_set_policy, 39141da177e4SLinus Torvalds .get_policy = shmem_get_policy, 39151da177e4SLinus Torvalds #endif 39161da177e4SLinus Torvalds }; 39171da177e4SLinus Torvalds 39183c26ff6eSAl Viro static struct dentry *shmem_mount(struct file_system_type *fs_type, 39193c26ff6eSAl Viro int flags, const char *dev_name, void *data) 39201da177e4SLinus Torvalds { 39213c26ff6eSAl Viro return mount_nodev(fs_type, flags, data, shmem_fill_super); 39221da177e4SLinus Torvalds } 39231da177e4SLinus Torvalds 392441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 39251da177e4SLinus Torvalds .owner = THIS_MODULE, 39261da177e4SLinus Torvalds .name = "tmpfs", 39273c26ff6eSAl Viro .mount = shmem_mount, 39281da177e4SLinus Torvalds .kill_sb = kill_litter_super, 39292b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 39301da177e4SLinus Torvalds }; 39311da177e4SLinus Torvalds 393241ffe5d5SHugh Dickins int __init shmem_init(void) 39331da177e4SLinus Torvalds { 39341da177e4SLinus Torvalds int error; 39351da177e4SLinus Torvalds 393616203a7aSRob Landley /* If rootfs called this, don't re-init */ 393716203a7aSRob Landley if (shmem_inode_cachep) 393816203a7aSRob Landley return 0; 393916203a7aSRob Landley 394041ffe5d5SHugh Dickins error = shmem_init_inodecache(); 39411da177e4SLinus Torvalds if (error) 39421da177e4SLinus Torvalds goto out3; 39431da177e4SLinus Torvalds 394441ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 39451da177e4SLinus Torvalds if (error) { 39461170532bSJoe Perches pr_err("Could not register tmpfs\n"); 39471da177e4SLinus Torvalds goto out2; 39481da177e4SLinus Torvalds } 394995dc112aSGreg Kroah-Hartman 3950ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 39511da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 39521da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 39531170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 39541da177e4SLinus Torvalds goto out1; 39551da177e4SLinus Torvalds } 39565a6e75f8SKirill A. Shutemov 3957e496cf3dSKirill A. Shutemov #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 39585a6e75f8SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge < SHMEM_HUGE_DENY) 39595a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 39605a6e75f8SKirill A. Shutemov else 39615a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 39625a6e75f8SKirill A. Shutemov #endif 39631da177e4SLinus Torvalds return 0; 39641da177e4SLinus Torvalds 39651da177e4SLinus Torvalds out1: 396641ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 39671da177e4SLinus Torvalds out2: 396841ffe5d5SHugh Dickins shmem_destroy_inodecache(); 39691da177e4SLinus Torvalds out3: 39701da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 39711da177e4SLinus Torvalds return error; 39721da177e4SLinus Torvalds } 3973853ac43aSMatt Mackall 3974e496cf3dSKirill A. Shutemov #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS) 39755a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 39765a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 39775a6e75f8SKirill A. Shutemov { 39785a6e75f8SKirill A. Shutemov int values[] = { 39795a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 39805a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 39815a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 39825a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 39835a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 39845a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 39855a6e75f8SKirill A. Shutemov }; 39865a6e75f8SKirill A. Shutemov int i, count; 39875a6e75f8SKirill A. Shutemov 39885a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 39895a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 39905a6e75f8SKirill A. Shutemov 39915a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 39925a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 39935a6e75f8SKirill A. Shutemov } 39945a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 39955a6e75f8SKirill A. Shutemov return count; 39965a6e75f8SKirill A. Shutemov } 39975a6e75f8SKirill A. Shutemov 39985a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 39995a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 40005a6e75f8SKirill A. Shutemov { 40015a6e75f8SKirill A. Shutemov char tmp[16]; 40025a6e75f8SKirill A. Shutemov int huge; 40035a6e75f8SKirill A. Shutemov 40045a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 40055a6e75f8SKirill A. Shutemov return -EINVAL; 40065a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 40075a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 40085a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 40095a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 40105a6e75f8SKirill A. Shutemov 40115a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 40125a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 40135a6e75f8SKirill A. Shutemov return -EINVAL; 40145a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 40155a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 40165a6e75f8SKirill A. Shutemov return -EINVAL; 40175a6e75f8SKirill A. Shutemov 40185a6e75f8SKirill A. Shutemov shmem_huge = huge; 40195a6e75f8SKirill A. Shutemov if (shmem_huge < SHMEM_HUGE_DENY) 40205a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 40215a6e75f8SKirill A. Shutemov return count; 40225a6e75f8SKirill A. Shutemov } 40235a6e75f8SKirill A. Shutemov 40245a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 40255a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 40263b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */ 4027f3f0e1d2SKirill A. Shutemov 40283b33719cSArnd Bergmann #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE 4029f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 4030f3f0e1d2SKirill A. Shutemov { 4031f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 4032f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 4033f3f0e1d2SKirill A. Shutemov loff_t i_size; 4034f3f0e1d2SKirill A. Shutemov pgoff_t off; 4035f3f0e1d2SKirill A. Shutemov 4036f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 4037f3f0e1d2SKirill A. Shutemov return true; 4038f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 4039f3f0e1d2SKirill A. Shutemov return false; 4040f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 4041f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4042f3f0e1d2SKirill A. Shutemov return false; 4043f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4044f3f0e1d2SKirill A. Shutemov return true; 4045f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4046f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 4047f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 4048f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 4049f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 4050f3f0e1d2SKirill A. Shutemov return true; 4051f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4052f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 4053f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 4054f3f0e1d2SKirill A. Shutemov default: 4055f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 4056f3f0e1d2SKirill A. Shutemov return false; 4057f3f0e1d2SKirill A. Shutemov } 4058f3f0e1d2SKirill A. Shutemov } 40593b33719cSArnd Bergmann #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */ 40605a6e75f8SKirill A. Shutemov 4061853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4062853ac43aSMatt Mackall 4063853ac43aSMatt Mackall /* 4064853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4065853ac43aSMatt Mackall * 4066853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4067853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4068853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4069853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4070853ac43aSMatt Mackall */ 4071853ac43aSMatt Mackall 407241ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4073853ac43aSMatt Mackall .name = "tmpfs", 40743c26ff6eSAl Viro .mount = ramfs_mount, 4075853ac43aSMatt Mackall .kill_sb = kill_litter_super, 40762b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4077853ac43aSMatt Mackall }; 4078853ac43aSMatt Mackall 407941ffe5d5SHugh Dickins int __init shmem_init(void) 4080853ac43aSMatt Mackall { 408141ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4082853ac43aSMatt Mackall 408341ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4084853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4085853ac43aSMatt Mackall 4086853ac43aSMatt Mackall return 0; 4087853ac43aSMatt Mackall } 4088853ac43aSMatt Mackall 408941ffe5d5SHugh Dickins int shmem_unuse(swp_entry_t swap, struct page *page) 4090853ac43aSMatt Mackall { 4091853ac43aSMatt Mackall return 0; 4092853ac43aSMatt Mackall } 4093853ac43aSMatt Mackall 40943f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 40953f96b79aSHugh Dickins { 40963f96b79aSHugh Dickins return 0; 40973f96b79aSHugh Dickins } 40983f96b79aSHugh Dickins 409924513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 410024513264SHugh Dickins { 410124513264SHugh Dickins } 410224513264SHugh Dickins 4103c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4104c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4105c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4106c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4107c01d5b30SHugh Dickins { 4108c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4109c01d5b30SHugh Dickins } 4110c01d5b30SHugh Dickins #endif 4111c01d5b30SHugh Dickins 411241ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 411394c1e62dSHugh Dickins { 411441ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 411594c1e62dSHugh Dickins } 411694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 411794c1e62dSHugh Dickins 4118853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 41190b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 4120454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 41210b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 41220b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4123853ac43aSMatt Mackall 4124853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4125853ac43aSMatt Mackall 4126853ac43aSMatt Mackall /* common code */ 41271da177e4SLinus Torvalds 412819938e35SRasmus Villemoes static const struct dentry_operations anon_ops = { 4129118b2302SAl Viro .d_dname = simple_dname 41303451538aSAl Viro }; 41313451538aSAl Viro 4132c7277090SEric Paris static struct file *__shmem_file_setup(const char *name, loff_t size, 4133c7277090SEric Paris unsigned long flags, unsigned int i_flags) 41341da177e4SLinus Torvalds { 41356b4d0b27SAl Viro struct file *res; 41361da177e4SLinus Torvalds struct inode *inode; 41372c48b9c4SAl Viro struct path path; 41383451538aSAl Viro struct super_block *sb; 41391da177e4SLinus Torvalds struct qstr this; 41401da177e4SLinus Torvalds 41411da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) 41426b4d0b27SAl Viro return ERR_CAST(shm_mnt); 41431da177e4SLinus Torvalds 4144285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 41451da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 41461da177e4SLinus Torvalds 41471da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 41481da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 41491da177e4SLinus Torvalds 41506b4d0b27SAl Viro res = ERR_PTR(-ENOMEM); 41511da177e4SLinus Torvalds this.name = name; 41521da177e4SLinus Torvalds this.len = strlen(name); 41531da177e4SLinus Torvalds this.hash = 0; /* will go */ 41543451538aSAl Viro sb = shm_mnt->mnt_sb; 415566ee4b88SKonstantin Khlebnikov path.mnt = mntget(shm_mnt); 41563451538aSAl Viro path.dentry = d_alloc_pseudo(sb, &this); 41572c48b9c4SAl Viro if (!path.dentry) 41581da177e4SLinus Torvalds goto put_memory; 41593451538aSAl Viro d_set_d_op(path.dentry, &anon_ops); 41601da177e4SLinus Torvalds 41616b4d0b27SAl Viro res = ERR_PTR(-ENOSPC); 41623451538aSAl Viro inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags); 41631da177e4SLinus Torvalds if (!inode) 416466ee4b88SKonstantin Khlebnikov goto put_memory; 41651da177e4SLinus Torvalds 4166c7277090SEric Paris inode->i_flags |= i_flags; 41672c48b9c4SAl Viro d_instantiate(path.dentry, inode); 41681da177e4SLinus Torvalds inode->i_size = size; 41696d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 417026567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 417126567cdbSAl Viro if (IS_ERR(res)) 417266ee4b88SKonstantin Khlebnikov goto put_path; 41734b42af81SAl Viro 41746b4d0b27SAl Viro res = alloc_file(&path, FMODE_WRITE | FMODE_READ, 41754b42af81SAl Viro &shmem_file_operations); 41766b4d0b27SAl Viro if (IS_ERR(res)) 417766ee4b88SKonstantin Khlebnikov goto put_path; 41784b42af81SAl Viro 41796b4d0b27SAl Viro return res; 41801da177e4SLinus Torvalds 41811da177e4SLinus Torvalds put_memory: 41821da177e4SLinus Torvalds shmem_unacct_size(flags, size); 418366ee4b88SKonstantin Khlebnikov put_path: 418466ee4b88SKonstantin Khlebnikov path_put(&path); 41856b4d0b27SAl Viro return res; 41861da177e4SLinus Torvalds } 4187c7277090SEric Paris 4188c7277090SEric Paris /** 4189c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4190c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4191c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4192e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4193e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4194c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4195c7277090SEric Paris * @size: size to be set for the file 4196c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4197c7277090SEric Paris */ 4198c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4199c7277090SEric Paris { 4200c7277090SEric Paris return __shmem_file_setup(name, size, flags, S_PRIVATE); 4201c7277090SEric Paris } 4202c7277090SEric Paris 4203c7277090SEric Paris /** 4204c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4205c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4206c7277090SEric Paris * @size: size to be set for the file 4207c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4208c7277090SEric Paris */ 4209c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4210c7277090SEric Paris { 4211c7277090SEric Paris return __shmem_file_setup(name, size, flags, 0); 4212c7277090SEric Paris } 4213395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 42141da177e4SLinus Torvalds 421546711810SRandy Dunlap /** 42161da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 42171da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 42181da177e4SLinus Torvalds */ 42191da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 42201da177e4SLinus Torvalds { 42211da177e4SLinus Torvalds struct file *file; 42221da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 42231da177e4SLinus Torvalds 422466fc1303SHugh Dickins /* 422566fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 422666fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 422766fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 422866fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 422966fc1303SHugh Dickins */ 423066fc1303SHugh Dickins file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE); 42311da177e4SLinus Torvalds if (IS_ERR(file)) 42321da177e4SLinus Torvalds return PTR_ERR(file); 42331da177e4SLinus Torvalds 42341da177e4SLinus Torvalds if (vma->vm_file) 42351da177e4SLinus Torvalds fput(vma->vm_file); 42361da177e4SLinus Torvalds vma->vm_file = file; 42371da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4238f3f0e1d2SKirill A. Shutemov 4239e496cf3dSKirill A. Shutemov if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && 4240f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 4241f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 4242f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 4243f3f0e1d2SKirill A. Shutemov } 4244f3f0e1d2SKirill A. Shutemov 42451da177e4SLinus Torvalds return 0; 42461da177e4SLinus Torvalds } 4247d9d90e5eSHugh Dickins 4248d9d90e5eSHugh Dickins /** 4249d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4250d9d90e5eSHugh Dickins * @mapping: the page's address_space 4251d9d90e5eSHugh Dickins * @index: the page index 4252d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4253d9d90e5eSHugh Dickins * 4254d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4255d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4256d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4257d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4258d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4259d9d90e5eSHugh Dickins * 426068da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 426168da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4262d9d90e5eSHugh Dickins */ 4263d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4264d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4265d9d90e5eSHugh Dickins { 426668da9f05SHugh Dickins #ifdef CONFIG_SHMEM 426768da9f05SHugh Dickins struct inode *inode = mapping->host; 42689276aad6SHugh Dickins struct page *page; 426968da9f05SHugh Dickins int error; 427068da9f05SHugh Dickins 427168da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 42729e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4273cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 427468da9f05SHugh Dickins if (error) 427568da9f05SHugh Dickins page = ERR_PTR(error); 427668da9f05SHugh Dickins else 427768da9f05SHugh Dickins unlock_page(page); 427868da9f05SHugh Dickins return page; 427968da9f05SHugh Dickins #else 428068da9f05SHugh Dickins /* 428168da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 428268da9f05SHugh Dickins */ 4283d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 428468da9f05SHugh Dickins #endif 4285d9d90e5eSHugh Dickins } 4286d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4287