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> 3246c9a946SArnd Bergmann #include <linux/random.h> 33174cd4b1SIngo Molnar #include <linux/sched/signal.h> 34b95f1b31SPaul Gortmaker #include <linux/export.h> 35853ac43aSMatt Mackall #include <linux/swap.h> 36e2e40f2cSChristoph Hellwig #include <linux/uio.h> 37f3f0e1d2SKirill A. Shutemov #include <linux/khugepaged.h> 38749df87bSMike Kravetz #include <linux/hugetlb.h> 39b56a2d8aSVineeth Remanan Pillai #include <linux/frontswap.h> 40626c3920SAl Viro #include <linux/fs_parser.h> 41853ac43aSMatt Mackall 4295cc09d6SAndrea Arcangeli #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ 4395cc09d6SAndrea Arcangeli 44853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 45853ac43aSMatt Mackall 46853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 471da177e4SLinus Torvalds /* 481da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 491da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 501da177e4SLinus Torvalds * which makes it a completely usable filesystem. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds 5339f0247dSAndreas Gruenbacher #include <linux/xattr.h> 54a5694255SChristoph Hellwig #include <linux/exportfs.h> 551c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 56feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 571da177e4SLinus Torvalds #include <linux/mman.h> 581da177e4SLinus Torvalds #include <linux/string.h> 591da177e4SLinus Torvalds #include <linux/slab.h> 601da177e4SLinus Torvalds #include <linux/backing-dev.h> 611da177e4SLinus Torvalds #include <linux/shmem_fs.h> 621da177e4SLinus Torvalds #include <linux/writeback.h> 631da177e4SLinus Torvalds #include <linux/blkdev.h> 64bda97eabSHugh Dickins #include <linux/pagevec.h> 6541ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6683e4fa9cSHugh Dickins #include <linux/falloc.h> 67708e3508SHugh Dickins #include <linux/splice.h> 681da177e4SLinus Torvalds #include <linux/security.h> 691da177e4SLinus Torvalds #include <linux/swapops.h> 701da177e4SLinus Torvalds #include <linux/mempolicy.h> 711da177e4SLinus Torvalds #include <linux/namei.h> 72b00dc3adSHugh Dickins #include <linux/ctype.h> 73304dbdb7SLee Schermerhorn #include <linux/migrate.h> 74c1f60a5aSChristoph Lameter #include <linux/highmem.h> 75680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7692562927SMimi Zohar #include <linux/magic.h> 779183df25SDavid Herrmann #include <linux/syscalls.h> 7840e041a2SDavid Herrmann #include <linux/fcntl.h> 799183df25SDavid Herrmann #include <uapi/linux/memfd.h> 80cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 814c27fe4cSMike Rapoport #include <linux/rmap.h> 822b4db796SAmir Goldstein #include <linux/uuid.h> 83304dbdb7SLee Schermerhorn 847c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 851da177e4SLinus Torvalds #include <asm/pgtable.h> 861da177e4SLinus Torvalds 87dd56b046SMel Gorman #include "internal.h" 88dd56b046SMel Gorman 8909cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 9009cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 931da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 941da177e4SLinus Torvalds 9569f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9669f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9769f07ec9SHugh Dickins 981aac1400SHugh Dickins /* 99f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 100f00cdc6dSHugh Dickins * inode->i_private (with i_mutex making sure that it has only one user at 101f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 1021aac1400SHugh Dickins */ 1031aac1400SHugh Dickins struct shmem_falloc { 1048e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1051aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1061aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1071aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1081aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1091aac1400SHugh Dickins }; 1101aac1400SHugh Dickins 1110b5071ddSAl Viro struct shmem_options { 1120b5071ddSAl Viro unsigned long long blocks; 1130b5071ddSAl Viro unsigned long long inodes; 1140b5071ddSAl Viro struct mempolicy *mpol; 1150b5071ddSAl Viro kuid_t uid; 1160b5071ddSAl Viro kgid_t gid; 1170b5071ddSAl Viro umode_t mode; 1180b5071ddSAl Viro int huge; 1190b5071ddSAl Viro int seen; 1200b5071ddSAl Viro #define SHMEM_SEEN_BLOCKS 1 1210b5071ddSAl Viro #define SHMEM_SEEN_INODES 2 1220b5071ddSAl Viro #define SHMEM_SEEN_HUGE 4 1230b5071ddSAl Viro }; 1240b5071ddSAl Viro 125b76db735SAndrew Morton #ifdef CONFIG_TMPFS 126680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 127680d794bSakpm@linux-foundation.org { 128ca79b0c2SArun KS return totalram_pages() / 2; 129680d794bSakpm@linux-foundation.org } 130680d794bSakpm@linux-foundation.org 131680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 132680d794bSakpm@linux-foundation.org { 133ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 134ca79b0c2SArun KS 135ca79b0c2SArun KS return min(nr_pages - totalhigh_pages(), nr_pages / 2); 136680d794bSakpm@linux-foundation.org } 137b76db735SAndrew Morton #endif 138680d794bSakpm@linux-foundation.org 139bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp); 140bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 141bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index); 142c5bf121eSVineeth Remanan Pillai static int shmem_swapin_page(struct inode *inode, pgoff_t index, 143c5bf121eSVineeth Remanan Pillai struct page **pagep, enum sgp_type sgp, 144c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 145c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type); 14668da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1479e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 148cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 1492b740303SSouptick Joarder struct vm_fault *vmf, vm_fault_t *fault_type); 15068da9f05SHugh Dickins 151f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1529e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 15368da9f05SHugh Dickins { 15468da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 155cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 15668da9f05SHugh Dickins } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds return sb->s_fs_info; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds /* 1641da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1651da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1661da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1671da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1681da177e4SLinus Torvalds */ 1691da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1701da177e4SLinus Torvalds { 1710b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 172191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1761da177e4SLinus Torvalds { 1770b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1781da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 18177142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 18277142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 18377142517SKonstantin Khlebnikov { 18477142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 18577142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 18677142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 18777142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 18877142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 18977142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 19077142517SKonstantin Khlebnikov } 19177142517SKonstantin Khlebnikov return 0; 19277142517SKonstantin Khlebnikov } 19377142517SKonstantin Khlebnikov 1941da177e4SLinus Torvalds /* 1951da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 19675edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1971da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1981da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1991da177e4SLinus Torvalds */ 200800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 2011da177e4SLinus Torvalds { 202800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 203800d8c63SKirill A. Shutemov return 0; 204800d8c63SKirill A. Shutemov 205800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 206800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 2071da177e4SLinus Torvalds } 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 2101da177e4SLinus Torvalds { 2110b0a0806SHugh Dickins if (flags & VM_NORESERVE) 21209cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2150f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 2160f079694SMike Rapoport { 2170f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2180f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2190f079694SMike Rapoport 2200f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 2210f079694SMike Rapoport return false; 2220f079694SMike Rapoport 2230f079694SMike Rapoport if (sbinfo->max_blocks) { 2240f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2250f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2260f079694SMike Rapoport goto unacct; 2270f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2280f079694SMike Rapoport } 2290f079694SMike Rapoport 2300f079694SMike Rapoport return true; 2310f079694SMike Rapoport 2320f079694SMike Rapoport unacct: 2330f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2340f079694SMike Rapoport return false; 2350f079694SMike Rapoport } 2360f079694SMike Rapoport 2370f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2380f079694SMike Rapoport { 2390f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2400f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2410f079694SMike Rapoport 2420f079694SMike Rapoport if (sbinfo->max_blocks) 2430f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2440f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2450f079694SMike Rapoport } 2460f079694SMike Rapoport 247759b9775SHugh Dickins static const struct super_operations shmem_ops; 248f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops; 24915ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 25092e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 25192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 25292e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 253f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 254779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2551da177e4SLinus Torvalds 256b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 257b0506e48SMike Rapoport { 258b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 259b0506e48SMike Rapoport } 260b0506e48SMike Rapoport 2611da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 262cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2631da177e4SLinus Torvalds 2645b04c689SPavel Emelyanov static int shmem_reserve_inode(struct super_block *sb) 2655b04c689SPavel Emelyanov { 2665b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2675b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2685b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2695b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 2705b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2715b04c689SPavel Emelyanov return -ENOSPC; 2725b04c689SPavel Emelyanov } 2735b04c689SPavel Emelyanov sbinfo->free_inodes--; 2745b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2755b04c689SPavel Emelyanov } 2765b04c689SPavel Emelyanov return 0; 2775b04c689SPavel Emelyanov } 2785b04c689SPavel Emelyanov 2795b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 2805b04c689SPavel Emelyanov { 2815b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2825b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 2835b04c689SPavel Emelyanov spin_lock(&sbinfo->stat_lock); 2845b04c689SPavel Emelyanov sbinfo->free_inodes++; 2855b04c689SPavel Emelyanov spin_unlock(&sbinfo->stat_lock); 2865b04c689SPavel Emelyanov } 2875b04c689SPavel Emelyanov } 2885b04c689SPavel Emelyanov 28946711810SRandy Dunlap /** 29041ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 2911da177e4SLinus Torvalds * @inode: inode to recalc 2921da177e4SLinus Torvalds * 2931da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 2941da177e4SLinus Torvalds * undirtied hole pages behind our back. 2951da177e4SLinus Torvalds * 2961da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 2971da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 2981da177e4SLinus Torvalds * 2991da177e4SLinus Torvalds * It has to be called with the spinlock held. 3001da177e4SLinus Torvalds */ 3011da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 3021da177e4SLinus Torvalds { 3031da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 3041da177e4SLinus Torvalds long freed; 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 3071da177e4SLinus Torvalds if (freed > 0) { 3081da177e4SLinus Torvalds info->alloced -= freed; 30954af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 3100f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 314800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 315800d8c63SKirill A. Shutemov { 316800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3174595ef88SKirill A. Shutemov unsigned long flags; 318800d8c63SKirill A. Shutemov 3190f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 320800d8c63SKirill A. Shutemov return false; 321b1cc94abSMike Rapoport 322aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 323aaa52e34SHugh Dickins inode->i_mapping->nrpages += pages; 324aaa52e34SHugh Dickins 3254595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 326800d8c63SKirill A. Shutemov info->alloced += pages; 327800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 328800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3294595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 330800d8c63SKirill A. Shutemov 331800d8c63SKirill A. Shutemov return true; 332800d8c63SKirill A. Shutemov } 333800d8c63SKirill A. Shutemov 334800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 335800d8c63SKirill A. Shutemov { 336800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3374595ef88SKirill A. Shutemov unsigned long flags; 338800d8c63SKirill A. Shutemov 339aaa52e34SHugh Dickins /* nrpages adjustment done by __delete_from_page_cache() or caller */ 340aaa52e34SHugh Dickins 3414595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 342800d8c63SKirill A. Shutemov info->alloced -= pages; 343800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 344800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3454595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 346800d8c63SKirill A. Shutemov 3470f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 348800d8c63SKirill A. Shutemov } 349800d8c63SKirill A. Shutemov 3507a5d0fbbSHugh Dickins /* 35162f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 3527a5d0fbbSHugh Dickins */ 35362f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 3547a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 3557a5d0fbbSHugh Dickins { 35662f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 3576dbaf22cSJohannes Weiner void *item; 3587a5d0fbbSHugh Dickins 3597a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 3606dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 36162f945b6SMatthew Wilcox item = xas_load(&xas); 3627a5d0fbbSHugh Dickins if (item != expected) 3637a5d0fbbSHugh Dickins return -ENOENT; 36462f945b6SMatthew Wilcox xas_store(&xas, replacement); 3657a5d0fbbSHugh Dickins return 0; 3667a5d0fbbSHugh Dickins } 3677a5d0fbbSHugh Dickins 3687a5d0fbbSHugh Dickins /* 369d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 370d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 371d1899228SHugh Dickins * 372d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 373d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 374d1899228SHugh Dickins */ 375d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 376d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 377d1899228SHugh Dickins { 378a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 379d1899228SHugh Dickins } 380d1899228SHugh Dickins 381d1899228SHugh Dickins /* 3825a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 3835a6e75f8SKirill A. Shutemov * 3845a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 3855a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 3865a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 3875a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 3885a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 3895a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 3905a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 3915a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 3925a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 3935a6e75f8SKirill A. Shutemov */ 3945a6e75f8SKirill A. Shutemov 3955a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 3965a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 3975a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 3985a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 3995a6e75f8SKirill A. Shutemov 4005a6e75f8SKirill A. Shutemov /* 4015a6e75f8SKirill A. Shutemov * Special values. 4025a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 4035a6e75f8SKirill A. Shutemov * 4045a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 4055a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 4065a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 4075a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 4085a6e75f8SKirill A. Shutemov * 4095a6e75f8SKirill A. Shutemov */ 4105a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 4115a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 4125a6e75f8SKirill A. Shutemov 413396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4145a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 4155a6e75f8SKirill A. Shutemov 4165b9c98f3SMike Kravetz static int shmem_huge __read_mostly; 4175a6e75f8SKirill A. Shutemov 418e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) 4195a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 4205a6e75f8SKirill A. Shutemov { 4215a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 4225a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 4235a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 4245a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 4255a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 4265a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 4275a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 4285a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 4295a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 4305a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 4315a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 4325a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 4335a6e75f8SKirill A. Shutemov return -EINVAL; 4345a6e75f8SKirill A. Shutemov } 435e5f2249aSArnd Bergmann #endif 4365a6e75f8SKirill A. Shutemov 437e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 4385a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 4395a6e75f8SKirill A. Shutemov { 4405a6e75f8SKirill A. Shutemov switch (huge) { 4415a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 4425a6e75f8SKirill A. Shutemov return "never"; 4435a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 4445a6e75f8SKirill A. Shutemov return "always"; 4455a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 4465a6e75f8SKirill A. Shutemov return "within_size"; 4475a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4485a6e75f8SKirill A. Shutemov return "advise"; 4495a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 4505a6e75f8SKirill A. Shutemov return "deny"; 4515a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 4525a6e75f8SKirill A. Shutemov return "force"; 4535a6e75f8SKirill A. Shutemov default: 4545a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 4555a6e75f8SKirill A. Shutemov return "bad_val"; 4565a6e75f8SKirill A. Shutemov } 4575a6e75f8SKirill A. Shutemov } 458f1f5929cSJérémy Lefaure #endif 4595a6e75f8SKirill A. Shutemov 460779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 461779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 462779750d2SKirill A. Shutemov { 463779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 464253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 465779750d2SKirill A. Shutemov struct inode *inode; 466779750d2SKirill A. Shutemov struct shmem_inode_info *info; 467779750d2SKirill A. Shutemov struct page *page; 468779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 469779750d2SKirill A. Shutemov int removed = 0, split = 0; 470779750d2SKirill A. Shutemov 471779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 472779750d2SKirill A. Shutemov return SHRINK_STOP; 473779750d2SKirill A. Shutemov 474779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 475779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 476779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 477779750d2SKirill A. Shutemov 478779750d2SKirill A. Shutemov /* pin the inode */ 479779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 480779750d2SKirill A. Shutemov 481779750d2SKirill A. Shutemov /* inode is about to be evicted */ 482779750d2SKirill A. Shutemov if (!inode) { 483779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 484779750d2SKirill A. Shutemov removed++; 485779750d2SKirill A. Shutemov goto next; 486779750d2SKirill A. Shutemov } 487779750d2SKirill A. Shutemov 488779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 489779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 490779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 491253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 492779750d2SKirill A. Shutemov removed++; 493779750d2SKirill A. Shutemov goto next; 494779750d2SKirill A. Shutemov } 495779750d2SKirill A. Shutemov 496779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 497779750d2SKirill A. Shutemov next: 498779750d2SKirill A. Shutemov if (!--batch) 499779750d2SKirill A. Shutemov break; 500779750d2SKirill A. Shutemov } 501779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 502779750d2SKirill A. Shutemov 503253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 504253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 505253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 506253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 507253fd0f0SKirill A. Shutemov iput(inode); 508253fd0f0SKirill A. Shutemov } 509253fd0f0SKirill A. Shutemov 510779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 511779750d2SKirill A. Shutemov int ret; 512779750d2SKirill A. Shutemov 513779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 514779750d2SKirill A. Shutemov inode = &info->vfs_inode; 515779750d2SKirill A. Shutemov 516b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 517b3cd54b2SKirill A. Shutemov goto leave; 518779750d2SKirill A. Shutemov 519b3cd54b2SKirill A. Shutemov page = find_get_page(inode->i_mapping, 520779750d2SKirill A. Shutemov (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); 521779750d2SKirill A. Shutemov if (!page) 522779750d2SKirill A. Shutemov goto drop; 523779750d2SKirill A. Shutemov 524b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 525779750d2SKirill A. Shutemov if (!PageTransHuge(page)) { 526779750d2SKirill A. Shutemov put_page(page); 527779750d2SKirill A. Shutemov goto drop; 528779750d2SKirill A. Shutemov } 529779750d2SKirill A. Shutemov 530b3cd54b2SKirill A. Shutemov /* 531b3cd54b2SKirill A. Shutemov * Leave the inode on the list if we failed to lock 532b3cd54b2SKirill A. Shutemov * the page at this time. 533b3cd54b2SKirill A. Shutemov * 534b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 535b3cd54b2SKirill A. Shutemov * reclaim path. 536b3cd54b2SKirill A. Shutemov */ 537b3cd54b2SKirill A. Shutemov if (!trylock_page(page)) { 538b3cd54b2SKirill A. Shutemov put_page(page); 539b3cd54b2SKirill A. Shutemov goto leave; 540b3cd54b2SKirill A. Shutemov } 541b3cd54b2SKirill A. Shutemov 542779750d2SKirill A. Shutemov ret = split_huge_page(page); 543779750d2SKirill A. Shutemov unlock_page(page); 544779750d2SKirill A. Shutemov put_page(page); 545779750d2SKirill A. Shutemov 546b3cd54b2SKirill A. Shutemov /* If split failed leave the inode on the list */ 547b3cd54b2SKirill A. Shutemov if (ret) 548b3cd54b2SKirill A. Shutemov goto leave; 549779750d2SKirill A. Shutemov 550779750d2SKirill A. Shutemov split++; 551779750d2SKirill A. Shutemov drop: 552779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 553779750d2SKirill A. Shutemov removed++; 554b3cd54b2SKirill A. Shutemov leave: 555779750d2SKirill A. Shutemov iput(inode); 556779750d2SKirill A. Shutemov } 557779750d2SKirill A. Shutemov 558779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 559779750d2SKirill A. Shutemov list_splice_tail(&list, &sbinfo->shrinklist); 560779750d2SKirill A. Shutemov sbinfo->shrinklist_len -= removed; 561779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 562779750d2SKirill A. Shutemov 563779750d2SKirill A. Shutemov return split; 564779750d2SKirill A. Shutemov } 565779750d2SKirill A. Shutemov 566779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 567779750d2SKirill A. Shutemov struct shrink_control *sc) 568779750d2SKirill A. Shutemov { 569779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 570779750d2SKirill A. Shutemov 571779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 572779750d2SKirill A. Shutemov return SHRINK_STOP; 573779750d2SKirill A. Shutemov 574779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 575779750d2SKirill A. Shutemov } 576779750d2SKirill A. Shutemov 577779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 578779750d2SKirill A. Shutemov struct shrink_control *sc) 579779750d2SKirill A. Shutemov { 580779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 581779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 582779750d2SKirill A. Shutemov } 583396bcc52SMatthew Wilcox (Oracle) #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 5845a6e75f8SKirill A. Shutemov 5855a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 5865a6e75f8SKirill A. Shutemov 587779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 588779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 589779750d2SKirill A. Shutemov { 590779750d2SKirill A. Shutemov return 0; 591779750d2SKirill A. Shutemov } 592396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 5935a6e75f8SKirill A. Shutemov 59489fdcd26SYang Shi static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) 59589fdcd26SYang Shi { 596396bcc52SMatthew Wilcox (Oracle) if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 59789fdcd26SYang Shi (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && 59889fdcd26SYang Shi shmem_huge != SHMEM_HUGE_DENY) 59989fdcd26SYang Shi return true; 60089fdcd26SYang Shi return false; 60189fdcd26SYang Shi } 60289fdcd26SYang Shi 6035a6e75f8SKirill A. Shutemov /* 60446f65ec1SHugh Dickins * Like add_to_page_cache_locked, but error if expected item has gone. 60546f65ec1SHugh Dickins */ 60646f65ec1SHugh Dickins static int shmem_add_to_page_cache(struct page *page, 60746f65ec1SHugh Dickins struct address_space *mapping, 608552446a4SMatthew Wilcox pgoff_t index, void *expected, gfp_t gfp) 60946f65ec1SHugh Dickins { 610552446a4SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page)); 611552446a4SMatthew Wilcox unsigned long i = 0; 612d8c6546bSMatthew Wilcox (Oracle) unsigned long nr = compound_nr(page); 61346f65ec1SHugh Dickins 614800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 615800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(index != round_down(index, nr), page); 616309381feSSasha Levin VM_BUG_ON_PAGE(!PageLocked(page), page); 617309381feSSasha Levin VM_BUG_ON_PAGE(!PageSwapBacked(page), page); 618800d8c63SKirill A. Shutemov VM_BUG_ON(expected && PageTransHuge(page)); 61946f65ec1SHugh Dickins 620800d8c63SKirill A. Shutemov page_ref_add(page, nr); 62146f65ec1SHugh Dickins page->mapping = mapping; 62246f65ec1SHugh Dickins page->index = index; 62346f65ec1SHugh Dickins 624552446a4SMatthew Wilcox do { 625552446a4SMatthew Wilcox void *entry; 626552446a4SMatthew Wilcox xas_lock_irq(&xas); 627552446a4SMatthew Wilcox entry = xas_find_conflict(&xas); 628552446a4SMatthew Wilcox if (entry != expected) 629552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 630552446a4SMatthew Wilcox xas_create_range(&xas); 631552446a4SMatthew Wilcox if (xas_error(&xas)) 632552446a4SMatthew Wilcox goto unlock; 633552446a4SMatthew Wilcox next: 6344101196bSMatthew Wilcox (Oracle) xas_store(&xas, page); 635552446a4SMatthew Wilcox if (++i < nr) { 636552446a4SMatthew Wilcox xas_next(&xas); 637552446a4SMatthew Wilcox goto next; 638552446a4SMatthew Wilcox } 639800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 640800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 64111fb9989SMel Gorman __inc_node_page_state(page, NR_SHMEM_THPS); 642552446a4SMatthew Wilcox } 643552446a4SMatthew Wilcox mapping->nrpages += nr; 64411fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr); 64511fb9989SMel Gorman __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr); 646552446a4SMatthew Wilcox unlock: 647552446a4SMatthew Wilcox xas_unlock_irq(&xas); 648552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 649552446a4SMatthew Wilcox 650552446a4SMatthew Wilcox if (xas_error(&xas)) { 65146f65ec1SHugh Dickins page->mapping = NULL; 652800d8c63SKirill A. Shutemov page_ref_sub(page, nr); 653552446a4SMatthew Wilcox return xas_error(&xas); 65446f65ec1SHugh Dickins } 655552446a4SMatthew Wilcox 656552446a4SMatthew Wilcox return 0; 65746f65ec1SHugh Dickins } 65846f65ec1SHugh Dickins 65946f65ec1SHugh Dickins /* 6606922c0c7SHugh Dickins * Like delete_from_page_cache, but substitutes swap for page. 6616922c0c7SHugh Dickins */ 6626922c0c7SHugh Dickins static void shmem_delete_from_page_cache(struct page *page, void *radswap) 6636922c0c7SHugh Dickins { 6646922c0c7SHugh Dickins struct address_space *mapping = page->mapping; 6656922c0c7SHugh Dickins int error; 6666922c0c7SHugh Dickins 667800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 668800d8c63SKirill A. Shutemov 669b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 67062f945b6SMatthew Wilcox error = shmem_replace_entry(mapping, page->index, page, radswap); 6716922c0c7SHugh Dickins page->mapping = NULL; 6726922c0c7SHugh Dickins mapping->nrpages--; 67311fb9989SMel Gorman __dec_node_page_state(page, NR_FILE_PAGES); 67411fb9989SMel Gorman __dec_node_page_state(page, NR_SHMEM); 675b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 67609cbfeafSKirill A. Shutemov put_page(page); 6776922c0c7SHugh Dickins BUG_ON(error); 6786922c0c7SHugh Dickins } 6796922c0c7SHugh Dickins 6806922c0c7SHugh Dickins /* 681c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 6827a5d0fbbSHugh Dickins */ 6837a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 6847a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 6857a5d0fbbSHugh Dickins { 6866dbaf22cSJohannes Weiner void *old; 6877a5d0fbbSHugh Dickins 68855f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 6896dbaf22cSJohannes Weiner if (old != radswap) 6906dbaf22cSJohannes Weiner return -ENOENT; 6917a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 6926dbaf22cSJohannes Weiner return 0; 6937a5d0fbbSHugh Dickins } 6947a5d0fbbSHugh Dickins 6957a5d0fbbSHugh Dickins /* 6966a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 69748131e03SVlastimil Babka * given offsets are swapped out. 6986a15a370SVlastimil Babka * 699b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 7006a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 7016a15a370SVlastimil Babka */ 70248131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 70348131e03SVlastimil Babka pgoff_t start, pgoff_t end) 7046a15a370SVlastimil Babka { 7057ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 7066a15a370SVlastimil Babka struct page *page; 70748131e03SVlastimil Babka unsigned long swapped = 0; 7086a15a370SVlastimil Babka 7096a15a370SVlastimil Babka rcu_read_lock(); 7107ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 7117ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 7122cf938aaSMatthew Wilcox continue; 7133159f943SMatthew Wilcox if (xa_is_value(page)) 7146a15a370SVlastimil Babka swapped++; 7156a15a370SVlastimil Babka 7166a15a370SVlastimil Babka if (need_resched()) { 7177ae3424fSMatthew Wilcox xas_pause(&xas); 7186a15a370SVlastimil Babka cond_resched_rcu(); 7196a15a370SVlastimil Babka } 7206a15a370SVlastimil Babka } 7216a15a370SVlastimil Babka 7226a15a370SVlastimil Babka rcu_read_unlock(); 7236a15a370SVlastimil Babka 7246a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 7256a15a370SVlastimil Babka } 7266a15a370SVlastimil Babka 7276a15a370SVlastimil Babka /* 72848131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 72948131e03SVlastimil Babka * given vma is swapped out. 73048131e03SVlastimil Babka * 731b93b0163SMatthew Wilcox * This is safe to call without i_mutex or the i_pages lock thanks to RCU, 73248131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 73348131e03SVlastimil Babka */ 73448131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 73548131e03SVlastimil Babka { 73648131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 73748131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 73848131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 73948131e03SVlastimil Babka unsigned long swapped; 74048131e03SVlastimil Babka 74148131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 74248131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 74348131e03SVlastimil Babka 74448131e03SVlastimil Babka /* 74548131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 74648131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 74748131e03SVlastimil Babka * already track. 74848131e03SVlastimil Babka */ 74948131e03SVlastimil Babka if (!swapped) 75048131e03SVlastimil Babka return 0; 75148131e03SVlastimil Babka 75248131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 75348131e03SVlastimil Babka return swapped << PAGE_SHIFT; 75448131e03SVlastimil Babka 75548131e03SVlastimil Babka /* Here comes the more involved part */ 75648131e03SVlastimil Babka return shmem_partial_swap_usage(mapping, 75748131e03SVlastimil Babka linear_page_index(vma, vma->vm_start), 75848131e03SVlastimil Babka linear_page_index(vma, vma->vm_end)); 75948131e03SVlastimil Babka } 76048131e03SVlastimil Babka 76148131e03SVlastimil Babka /* 76224513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 76324513264SHugh Dickins */ 76424513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 76524513264SHugh Dickins { 76624513264SHugh Dickins struct pagevec pvec; 76724513264SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 76824513264SHugh Dickins pgoff_t index = 0; 76924513264SHugh Dickins 77086679820SMel Gorman pagevec_init(&pvec); 77124513264SHugh Dickins /* 77224513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 77324513264SHugh Dickins */ 77424513264SHugh Dickins while (!mapping_unevictable(mapping)) { 77524513264SHugh Dickins /* 77624513264SHugh Dickins * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it 77724513264SHugh Dickins * has finished, if it hits a row of PAGEVEC_SIZE swap entries. 77824513264SHugh Dickins */ 7790cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 78024513264SHugh Dickins PAGEVEC_SIZE, pvec.pages, indices); 78124513264SHugh Dickins if (!pvec.nr) 78224513264SHugh Dickins break; 78324513264SHugh Dickins index = indices[pvec.nr - 1] + 1; 7840cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 78564e3d12fSKuo-Hsin Yang check_move_unevictable_pages(&pvec); 78624513264SHugh Dickins pagevec_release(&pvec); 78724513264SHugh Dickins cond_resched(); 78824513264SHugh Dickins } 7897a5d0fbbSHugh Dickins } 7907a5d0fbbSHugh Dickins 7917a5d0fbbSHugh Dickins /* 79271725ed1SHugh Dickins * Check whether a hole-punch or truncation needs to split a huge page, 79371725ed1SHugh Dickins * returning true if no split was required, or the split has been successful. 79471725ed1SHugh Dickins * 79571725ed1SHugh Dickins * Eviction (or truncation to 0 size) should never need to split a huge page; 79671725ed1SHugh Dickins * but in rare cases might do so, if shmem_undo_range() failed to trylock on 79771725ed1SHugh Dickins * head, and then succeeded to trylock on tail. 79871725ed1SHugh Dickins * 79971725ed1SHugh Dickins * A split can only succeed when there are no additional references on the 80071725ed1SHugh Dickins * huge page: so the split below relies upon find_get_entries() having stopped 80171725ed1SHugh Dickins * when it found a subpage of the huge page, without getting further references. 80271725ed1SHugh Dickins */ 80371725ed1SHugh Dickins static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end) 80471725ed1SHugh Dickins { 80571725ed1SHugh Dickins if (!PageTransCompound(page)) 80671725ed1SHugh Dickins return true; 80771725ed1SHugh Dickins 80871725ed1SHugh Dickins /* Just proceed to delete a huge page wholly within the range punched */ 80971725ed1SHugh Dickins if (PageHead(page) && 81071725ed1SHugh Dickins page->index >= start && page->index + HPAGE_PMD_NR <= end) 81171725ed1SHugh Dickins return true; 81271725ed1SHugh Dickins 81371725ed1SHugh Dickins /* Try to split huge page, so we can truly punch the hole or truncate */ 81471725ed1SHugh Dickins return split_huge_page(page) >= 0; 81571725ed1SHugh Dickins } 81671725ed1SHugh Dickins 81771725ed1SHugh Dickins /* 8187f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 8191635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 8207a5d0fbbSHugh Dickins */ 8211635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 8221635f6a7SHugh Dickins bool unfalloc) 8231da177e4SLinus Torvalds { 824285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 8251da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 82609cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 82709cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 82809cbfeafSKirill A. Shutemov unsigned int partial_start = lstart & (PAGE_SIZE - 1); 82909cbfeafSKirill A. Shutemov unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); 830bda97eabSHugh Dickins struct pagevec pvec; 8317a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 8327a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 833285b2c4fSHugh Dickins pgoff_t index; 834bda97eabSHugh Dickins int i; 8351da177e4SLinus Torvalds 83683e4fa9cSHugh Dickins if (lend == -1) 83783e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 838bda97eabSHugh Dickins 83986679820SMel Gorman pagevec_init(&pvec); 840bda97eabSHugh Dickins index = start; 84183e4fa9cSHugh Dickins while (index < end) { 8420cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 84383e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 8447a5d0fbbSHugh Dickins pvec.pages, indices); 8457a5d0fbbSHugh Dickins if (!pvec.nr) 8467a5d0fbbSHugh Dickins break; 847bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 848bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 849bda97eabSHugh Dickins 8507a5d0fbbSHugh Dickins index = indices[i]; 85183e4fa9cSHugh Dickins if (index >= end) 852bda97eabSHugh Dickins break; 853bda97eabSHugh Dickins 8543159f943SMatthew Wilcox if (xa_is_value(page)) { 8551635f6a7SHugh Dickins if (unfalloc) 8561635f6a7SHugh Dickins continue; 8577a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 8587a5d0fbbSHugh Dickins index, page); 8597a5d0fbbSHugh Dickins continue; 8607a5d0fbbSHugh Dickins } 8617a5d0fbbSHugh Dickins 862800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); 863800d8c63SKirill A. Shutemov 864bda97eabSHugh Dickins if (!trylock_page(page)) 865bda97eabSHugh Dickins continue; 866800d8c63SKirill A. Shutemov 86771725ed1SHugh Dickins if ((!unfalloc || !PageUptodate(page)) && 86871725ed1SHugh Dickins page_mapping(page) == mapping) { 869309381feSSasha Levin VM_BUG_ON_PAGE(PageWriteback(page), page); 87071725ed1SHugh Dickins if (shmem_punch_compound(page, start, end)) 871bda97eabSHugh Dickins truncate_inode_page(mapping, page); 8727a5d0fbbSHugh Dickins } 873bda97eabSHugh Dickins unlock_page(page); 874bda97eabSHugh Dickins } 8750cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 87624513264SHugh Dickins pagevec_release(&pvec); 877bda97eabSHugh Dickins cond_resched(); 878bda97eabSHugh Dickins index++; 879bda97eabSHugh Dickins } 880bda97eabSHugh Dickins 88183e4fa9cSHugh Dickins if (partial_start) { 882bda97eabSHugh Dickins struct page *page = NULL; 8839e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, start - 1, &page, SGP_READ); 884bda97eabSHugh Dickins if (page) { 88509cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 88683e4fa9cSHugh Dickins if (start > end) { 88783e4fa9cSHugh Dickins top = partial_end; 88883e4fa9cSHugh Dickins partial_end = 0; 88983e4fa9cSHugh Dickins } 89083e4fa9cSHugh Dickins zero_user_segment(page, partial_start, top); 891bda97eabSHugh Dickins set_page_dirty(page); 892bda97eabSHugh Dickins unlock_page(page); 89309cbfeafSKirill A. Shutemov put_page(page); 894bda97eabSHugh Dickins } 895bda97eabSHugh Dickins } 89683e4fa9cSHugh Dickins if (partial_end) { 89783e4fa9cSHugh Dickins struct page *page = NULL; 8989e18eb29SAndres Lagar-Cavilla shmem_getpage(inode, end, &page, SGP_READ); 89983e4fa9cSHugh Dickins if (page) { 90083e4fa9cSHugh Dickins zero_user_segment(page, 0, partial_end); 90183e4fa9cSHugh Dickins set_page_dirty(page); 90283e4fa9cSHugh Dickins unlock_page(page); 90309cbfeafSKirill A. Shutemov put_page(page); 90483e4fa9cSHugh Dickins } 90583e4fa9cSHugh Dickins } 90683e4fa9cSHugh Dickins if (start >= end) 90783e4fa9cSHugh Dickins return; 908bda97eabSHugh Dickins 909bda97eabSHugh Dickins index = start; 910b1a36650SHugh Dickins while (index < end) { 911bda97eabSHugh Dickins cond_resched(); 9120cd6144aSJohannes Weiner 9130cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 91483e4fa9cSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 9157a5d0fbbSHugh Dickins pvec.pages, indices); 9167a5d0fbbSHugh Dickins if (!pvec.nr) { 917b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 918b1a36650SHugh Dickins if (index == start || end != -1) 919bda97eabSHugh Dickins break; 920b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 921bda97eabSHugh Dickins index = start; 922bda97eabSHugh Dickins continue; 923bda97eabSHugh Dickins } 924bda97eabSHugh Dickins for (i = 0; i < pagevec_count(&pvec); i++) { 925bda97eabSHugh Dickins struct page *page = pvec.pages[i]; 926bda97eabSHugh Dickins 9277a5d0fbbSHugh Dickins index = indices[i]; 92883e4fa9cSHugh Dickins if (index >= end) 929bda97eabSHugh Dickins break; 930bda97eabSHugh Dickins 9313159f943SMatthew Wilcox if (xa_is_value(page)) { 9321635f6a7SHugh Dickins if (unfalloc) 9331635f6a7SHugh Dickins continue; 934b1a36650SHugh Dickins if (shmem_free_swap(mapping, index, page)) { 935b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 936b1a36650SHugh Dickins index--; 937b1a36650SHugh Dickins break; 938b1a36650SHugh Dickins } 939b1a36650SHugh Dickins nr_swaps_freed++; 9407a5d0fbbSHugh Dickins continue; 9417a5d0fbbSHugh Dickins } 9427a5d0fbbSHugh Dickins 943bda97eabSHugh Dickins lock_page(page); 944800d8c63SKirill A. Shutemov 9451635f6a7SHugh Dickins if (!unfalloc || !PageUptodate(page)) { 94671725ed1SHugh Dickins if (page_mapping(page) != mapping) { 947b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 948b1a36650SHugh Dickins unlock_page(page); 949b1a36650SHugh Dickins index--; 950b1a36650SHugh Dickins break; 9517a5d0fbbSHugh Dickins } 95271725ed1SHugh Dickins VM_BUG_ON_PAGE(PageWriteback(page), page); 95371725ed1SHugh Dickins if (shmem_punch_compound(page, start, end)) 95471725ed1SHugh Dickins truncate_inode_page(mapping, page); 9550783ac95SHugh Dickins else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 95671725ed1SHugh Dickins /* Wipe the page and don't get stuck */ 95771725ed1SHugh Dickins clear_highpage(page); 95871725ed1SHugh Dickins flush_dcache_page(page); 95971725ed1SHugh Dickins set_page_dirty(page); 96071725ed1SHugh Dickins if (index < 96171725ed1SHugh Dickins round_up(start, HPAGE_PMD_NR)) 96271725ed1SHugh Dickins start = index + 1; 96371725ed1SHugh Dickins } 9641635f6a7SHugh Dickins } 965bda97eabSHugh Dickins unlock_page(page); 966bda97eabSHugh Dickins } 9670cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 96824513264SHugh Dickins pagevec_release(&pvec); 969bda97eabSHugh Dickins index++; 970bda97eabSHugh Dickins } 97194c1e62dSHugh Dickins 9724595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 9737a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 9741da177e4SLinus Torvalds shmem_recalc_inode(inode); 9754595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 9761635f6a7SHugh Dickins } 9771da177e4SLinus Torvalds 9781635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 9791635f6a7SHugh Dickins { 9801635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 981078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 9821da177e4SLinus Torvalds } 98394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 9841da177e4SLinus Torvalds 985a528d35eSDavid Howells static int shmem_getattr(const struct path *path, struct kstat *stat, 986a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 98744a30220SYu Zhao { 988a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 98944a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 99089fdcd26SYang Shi struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); 99144a30220SYu Zhao 992d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 9934595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 99444a30220SYu Zhao shmem_recalc_inode(inode); 9954595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 996d0424c42SHugh Dickins } 99744a30220SYu Zhao generic_fillattr(inode, stat); 99889fdcd26SYang Shi 99989fdcd26SYang Shi if (is_huge_enabled(sb_info)) 100089fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 100189fdcd26SYang Shi 100244a30220SYu Zhao return 0; 100344a30220SYu Zhao } 100444a30220SYu Zhao 100594c1e62dSHugh Dickins static int shmem_setattr(struct dentry *dentry, struct iattr *attr) 10061da177e4SLinus Torvalds { 100775c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 100840e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 1009779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10101da177e4SLinus Torvalds int error; 10111da177e4SLinus Torvalds 101231051c85SJan Kara error = setattr_prepare(dentry, attr); 1013db78b877SChristoph Hellwig if (error) 1014db78b877SChristoph Hellwig return error; 1015db78b877SChristoph Hellwig 101694c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 101794c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 101894c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10193889e6e7Snpiggin@suse.de 102040e041a2SDavid Herrmann /* protected by i_mutex */ 102140e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 102240e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 102340e041a2SDavid Herrmann return -EPERM; 102440e041a2SDavid Herrmann 102594c1e62dSHugh Dickins if (newsize != oldsize) { 102677142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 102777142517SKonstantin Khlebnikov oldsize, newsize); 102877142517SKonstantin Khlebnikov if (error) 102977142517SKonstantin Khlebnikov return error; 103094c1e62dSHugh Dickins i_size_write(inode, newsize); 1031078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 103294c1e62dSHugh Dickins } 1033afa2db2fSJosef Bacik if (newsize <= oldsize) { 103494c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1035d0424c42SHugh Dickins if (oldsize > holebegin) 1036d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1037d0424c42SHugh Dickins holebegin, 0, 1); 1038d0424c42SHugh Dickins if (info->alloced) 1039d0424c42SHugh Dickins shmem_truncate_range(inode, 1040d0424c42SHugh Dickins newsize, (loff_t)-1); 104194c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1042d0424c42SHugh Dickins if (oldsize > holebegin) 1043d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1044d0424c42SHugh Dickins holebegin, 0, 1); 1045779750d2SKirill A. Shutemov 1046779750d2SKirill A. Shutemov /* 1047779750d2SKirill A. Shutemov * Part of the huge page can be beyond i_size: subject 1048779750d2SKirill A. Shutemov * to shrink under memory pressure. 1049779750d2SKirill A. Shutemov */ 1050396bcc52SMatthew Wilcox (Oracle) if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { 1051779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1052d041353dSCong Wang /* 1053d041353dSCong Wang * _careful to defend against unlocked access to 1054d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1055d041353dSCong Wang */ 1056d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1057779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1058779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1059779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1060779750d2SKirill A. Shutemov } 1061779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1062779750d2SKirill A. Shutemov } 106394c1e62dSHugh Dickins } 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 10666a1a90adSChristoph Hellwig setattr_copy(inode, attr); 1067db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1068feda821eSChristoph Hellwig error = posix_acl_chmod(inode, inode->i_mode); 10691da177e4SLinus Torvalds return error; 10701da177e4SLinus Torvalds } 10711da177e4SLinus Torvalds 10721f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 10731da177e4SLinus Torvalds { 10741da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1075779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 10761da177e4SLinus Torvalds 10773889e6e7Snpiggin@suse.de if (inode->i_mapping->a_ops == &shmem_aops) { 10781da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 10791da177e4SLinus Torvalds inode->i_size = 0; 10803889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1081779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1082779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1083779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1084779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1085779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1086779750d2SKirill A. Shutemov } 1087779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1088779750d2SKirill A. Shutemov } 1089af53d3e9SHugh Dickins while (!list_empty(&info->swaplist)) { 1090af53d3e9SHugh Dickins /* Wait while shmem_unuse() is scanning this inode... */ 1091af53d3e9SHugh Dickins wait_var_event(&info->stop_eviction, 1092af53d3e9SHugh Dickins !atomic_read(&info->stop_eviction)); 1093cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1094af53d3e9SHugh Dickins /* ...but beware of the race if we peeked too early */ 1095af53d3e9SHugh Dickins if (!atomic_read(&info->stop_eviction)) 10961da177e4SLinus Torvalds list_del_init(&info->swaplist); 1097cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 10981da177e4SLinus Torvalds } 10993ed47db3SAl Viro } 1100b09e0fa4SEric Paris 110138f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11020f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11035b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1104dbd5768fSJan Kara clear_inode(inode); 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 1107b56a2d8aSVineeth Remanan Pillai extern struct swap_info_struct *swap_info[]; 1108b56a2d8aSVineeth Remanan Pillai 1109b56a2d8aSVineeth Remanan Pillai static int shmem_find_swap_entries(struct address_space *mapping, 1110b56a2d8aSVineeth Remanan Pillai pgoff_t start, unsigned int nr_entries, 1111b56a2d8aSVineeth Remanan Pillai struct page **entries, pgoff_t *indices, 111287039546SHugh Dickins unsigned int type, bool frontswap) 1113478922e2SMatthew Wilcox { 1114b56a2d8aSVineeth Remanan Pillai XA_STATE(xas, &mapping->i_pages, start); 1115b56a2d8aSVineeth Remanan Pillai struct page *page; 111687039546SHugh Dickins swp_entry_t entry; 1117b56a2d8aSVineeth Remanan Pillai unsigned int ret = 0; 1118b56a2d8aSVineeth Remanan Pillai 1119b56a2d8aSVineeth Remanan Pillai if (!nr_entries) 1120b56a2d8aSVineeth Remanan Pillai return 0; 1121478922e2SMatthew Wilcox 1122478922e2SMatthew Wilcox rcu_read_lock(); 1123b56a2d8aSVineeth Remanan Pillai xas_for_each(&xas, page, ULONG_MAX) { 1124b56a2d8aSVineeth Remanan Pillai if (xas_retry(&xas, page)) 11255b9c98f3SMike Kravetz continue; 1126b56a2d8aSVineeth Remanan Pillai 1127b56a2d8aSVineeth Remanan Pillai if (!xa_is_value(page)) 1128478922e2SMatthew Wilcox continue; 1129b56a2d8aSVineeth Remanan Pillai 113087039546SHugh Dickins entry = radix_to_swp_entry(page); 113187039546SHugh Dickins if (swp_type(entry) != type) 1132b56a2d8aSVineeth Remanan Pillai continue; 113387039546SHugh Dickins if (frontswap && 113487039546SHugh Dickins !frontswap_test(swap_info[type], swp_offset(entry))) 113587039546SHugh Dickins continue; 1136b56a2d8aSVineeth Remanan Pillai 1137b56a2d8aSVineeth Remanan Pillai indices[ret] = xas.xa_index; 1138b56a2d8aSVineeth Remanan Pillai entries[ret] = page; 1139b56a2d8aSVineeth Remanan Pillai 1140b56a2d8aSVineeth Remanan Pillai if (need_resched()) { 1141e21a2955SMatthew Wilcox xas_pause(&xas); 1142478922e2SMatthew Wilcox cond_resched_rcu(); 1143478922e2SMatthew Wilcox } 1144b56a2d8aSVineeth Remanan Pillai if (++ret == nr_entries) 1145b56a2d8aSVineeth Remanan Pillai break; 1146b56a2d8aSVineeth Remanan Pillai } 1147478922e2SMatthew Wilcox rcu_read_unlock(); 1148e21a2955SMatthew Wilcox 1149b56a2d8aSVineeth Remanan Pillai return ret; 1150b56a2d8aSVineeth Remanan Pillai } 1151b56a2d8aSVineeth Remanan Pillai 1152b56a2d8aSVineeth Remanan Pillai /* 1153b56a2d8aSVineeth Remanan Pillai * Move the swapped pages for an inode to page cache. Returns the count 1154b56a2d8aSVineeth Remanan Pillai * of pages swapped in, or the error in case of failure. 1155b56a2d8aSVineeth Remanan Pillai */ 1156b56a2d8aSVineeth Remanan Pillai static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec, 1157b56a2d8aSVineeth Remanan Pillai pgoff_t *indices) 1158b56a2d8aSVineeth Remanan Pillai { 1159b56a2d8aSVineeth Remanan Pillai int i = 0; 1160b56a2d8aSVineeth Remanan Pillai int ret = 0; 1161b56a2d8aSVineeth Remanan Pillai int error = 0; 1162b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1163b56a2d8aSVineeth Remanan Pillai 1164b56a2d8aSVineeth Remanan Pillai for (i = 0; i < pvec.nr; i++) { 1165b56a2d8aSVineeth Remanan Pillai struct page *page = pvec.pages[i]; 1166b56a2d8aSVineeth Remanan Pillai 1167b56a2d8aSVineeth Remanan Pillai if (!xa_is_value(page)) 1168b56a2d8aSVineeth Remanan Pillai continue; 1169b56a2d8aSVineeth Remanan Pillai error = shmem_swapin_page(inode, indices[i], 1170b56a2d8aSVineeth Remanan Pillai &page, SGP_CACHE, 1171b56a2d8aSVineeth Remanan Pillai mapping_gfp_mask(mapping), 1172b56a2d8aSVineeth Remanan Pillai NULL, NULL); 1173b56a2d8aSVineeth Remanan Pillai if (error == 0) { 1174b56a2d8aSVineeth Remanan Pillai unlock_page(page); 1175b56a2d8aSVineeth Remanan Pillai put_page(page); 1176b56a2d8aSVineeth Remanan Pillai ret++; 1177b56a2d8aSVineeth Remanan Pillai } 1178b56a2d8aSVineeth Remanan Pillai if (error == -ENOMEM) 1179b56a2d8aSVineeth Remanan Pillai break; 1180b56a2d8aSVineeth Remanan Pillai error = 0; 1181b56a2d8aSVineeth Remanan Pillai } 1182b56a2d8aSVineeth Remanan Pillai return error ? error : ret; 1183478922e2SMatthew Wilcox } 1184478922e2SMatthew Wilcox 118546f65ec1SHugh Dickins /* 118646f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 118746f65ec1SHugh Dickins */ 1188b56a2d8aSVineeth Remanan Pillai static int shmem_unuse_inode(struct inode *inode, unsigned int type, 1189b56a2d8aSVineeth Remanan Pillai bool frontswap, unsigned long *fs_pages_to_unuse) 11901da177e4SLinus Torvalds { 1191b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1192b56a2d8aSVineeth Remanan Pillai pgoff_t start = 0; 1193b56a2d8aSVineeth Remanan Pillai struct pagevec pvec; 1194b56a2d8aSVineeth Remanan Pillai pgoff_t indices[PAGEVEC_SIZE]; 1195b56a2d8aSVineeth Remanan Pillai bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0); 1196b56a2d8aSVineeth Remanan Pillai int ret = 0; 11971da177e4SLinus Torvalds 1198b56a2d8aSVineeth Remanan Pillai pagevec_init(&pvec); 1199b56a2d8aSVineeth Remanan Pillai do { 1200b56a2d8aSVineeth Remanan Pillai unsigned int nr_entries = PAGEVEC_SIZE; 12012e0e26c7SHugh Dickins 1202b56a2d8aSVineeth Remanan Pillai if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE) 1203b56a2d8aSVineeth Remanan Pillai nr_entries = *fs_pages_to_unuse; 12042e0e26c7SHugh Dickins 1205b56a2d8aSVineeth Remanan Pillai pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries, 1206b56a2d8aSVineeth Remanan Pillai pvec.pages, indices, 120787039546SHugh Dickins type, frontswap); 1208b56a2d8aSVineeth Remanan Pillai if (pvec.nr == 0) { 1209b56a2d8aSVineeth Remanan Pillai ret = 0; 1210778dd893SHugh Dickins break; 1211b56a2d8aSVineeth Remanan Pillai } 1212b56a2d8aSVineeth Remanan Pillai 1213b56a2d8aSVineeth Remanan Pillai ret = shmem_unuse_swap_entries(inode, pvec, indices); 1214b56a2d8aSVineeth Remanan Pillai if (ret < 0) 1215b56a2d8aSVineeth Remanan Pillai break; 1216b56a2d8aSVineeth Remanan Pillai 1217b56a2d8aSVineeth Remanan Pillai if (frontswap_partial) { 1218b56a2d8aSVineeth Remanan Pillai *fs_pages_to_unuse -= ret; 1219b56a2d8aSVineeth Remanan Pillai if (*fs_pages_to_unuse == 0) { 1220b56a2d8aSVineeth Remanan Pillai ret = FRONTSWAP_PAGES_UNUSED; 1221b56a2d8aSVineeth Remanan Pillai break; 1222b56a2d8aSVineeth Remanan Pillai } 1223b56a2d8aSVineeth Remanan Pillai } 1224b56a2d8aSVineeth Remanan Pillai 1225b56a2d8aSVineeth Remanan Pillai start = indices[pvec.nr - 1]; 1226b56a2d8aSVineeth Remanan Pillai } while (true); 1227b56a2d8aSVineeth Remanan Pillai 1228b56a2d8aSVineeth Remanan Pillai return ret; 1229b56a2d8aSVineeth Remanan Pillai } 1230b56a2d8aSVineeth Remanan Pillai 1231b56a2d8aSVineeth Remanan Pillai /* 1232b56a2d8aSVineeth Remanan Pillai * Read all the shared memory data that resides in the swap 1233b56a2d8aSVineeth Remanan Pillai * device 'type' back into memory, so the swap device can be 1234b56a2d8aSVineeth Remanan Pillai * unused. 1235b56a2d8aSVineeth Remanan Pillai */ 1236b56a2d8aSVineeth Remanan Pillai int shmem_unuse(unsigned int type, bool frontswap, 1237b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 1238b56a2d8aSVineeth Remanan Pillai { 1239b56a2d8aSVineeth Remanan Pillai struct shmem_inode_info *info, *next; 1240b56a2d8aSVineeth Remanan Pillai int error = 0; 1241b56a2d8aSVineeth Remanan Pillai 1242b56a2d8aSVineeth Remanan Pillai if (list_empty(&shmem_swaplist)) 1243b56a2d8aSVineeth Remanan Pillai return 0; 1244b56a2d8aSVineeth Remanan Pillai 1245b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1246b56a2d8aSVineeth Remanan Pillai list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1247b56a2d8aSVineeth Remanan Pillai if (!info->swapped) { 1248b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1249b56a2d8aSVineeth Remanan Pillai continue; 1250b56a2d8aSVineeth Remanan Pillai } 1251af53d3e9SHugh Dickins /* 1252af53d3e9SHugh Dickins * Drop the swaplist mutex while searching the inode for swap; 1253af53d3e9SHugh Dickins * but before doing so, make sure shmem_evict_inode() will not 1254af53d3e9SHugh Dickins * remove placeholder inode from swaplist, nor let it be freed 1255af53d3e9SHugh Dickins * (igrab() would protect from unlink, but not from unmount). 1256af53d3e9SHugh Dickins */ 1257af53d3e9SHugh Dickins atomic_inc(&info->stop_eviction); 1258b56a2d8aSVineeth Remanan Pillai mutex_unlock(&shmem_swaplist_mutex); 1259b56a2d8aSVineeth Remanan Pillai 1260af53d3e9SHugh Dickins error = shmem_unuse_inode(&info->vfs_inode, type, frontswap, 1261b56a2d8aSVineeth Remanan Pillai fs_pages_to_unuse); 1262b56a2d8aSVineeth Remanan Pillai cond_resched(); 1263b56a2d8aSVineeth Remanan Pillai 1264b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1265b56a2d8aSVineeth Remanan Pillai next = list_next_entry(info, swaplist); 1266b56a2d8aSVineeth Remanan Pillai if (!info->swapped) 1267b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1268af53d3e9SHugh Dickins if (atomic_dec_and_test(&info->stop_eviction)) 1269af53d3e9SHugh Dickins wake_up_var(&info->stop_eviction); 1270b56a2d8aSVineeth Remanan Pillai if (error) 1271b56a2d8aSVineeth Remanan Pillai break; 12721da177e4SLinus Torvalds } 1273cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1274778dd893SHugh Dickins 1275778dd893SHugh Dickins return error; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds /* 12791da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 12801da177e4SLinus Torvalds */ 12811da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds struct shmem_inode_info *info; 12841da177e4SLinus Torvalds struct address_space *mapping; 12851da177e4SLinus Torvalds struct inode *inode; 12866922c0c7SHugh Dickins swp_entry_t swap; 12876922c0c7SHugh Dickins pgoff_t index; 12881da177e4SLinus Torvalds 1289800d8c63SKirill A. Shutemov VM_BUG_ON_PAGE(PageCompound(page), page); 12901da177e4SLinus Torvalds BUG_ON(!PageLocked(page)); 12911da177e4SLinus Torvalds mapping = page->mapping; 12921da177e4SLinus Torvalds index = page->index; 12931da177e4SLinus Torvalds inode = mapping->host; 12941da177e4SLinus Torvalds info = SHMEM_I(inode); 12951da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 12961da177e4SLinus Torvalds goto redirty; 1297d9fe526aSHugh Dickins if (!total_swap_pages) 12981da177e4SLinus Torvalds goto redirty; 12991da177e4SLinus Torvalds 1300d9fe526aSHugh Dickins /* 130197b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 130297b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 130397b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 130497b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 130597b713baSChristoph Hellwig * threads or sync. 1306d9fe526aSHugh Dickins */ 130748f170fbSHugh Dickins if (!wbc->for_reclaim) { 130848f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 130948f170fbSHugh Dickins goto redirty; 131048f170fbSHugh Dickins } 13111635f6a7SHugh Dickins 13121635f6a7SHugh Dickins /* 13131635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13141635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 13151635f6a7SHugh Dickins * fallocated page arriving here is now to initialize it and write it. 13161aac1400SHugh Dickins * 13171aac1400SHugh Dickins * That's okay for a page already fallocated earlier, but if we have 13181aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 13191aac1400SHugh Dickins * of this page in case we have to undo it, and (b) it may not be a 13201aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 13211aac1400SHugh Dickins * reactivate the page, and let shmem_fallocate() quit when too many. 13221635f6a7SHugh Dickins */ 13231635f6a7SHugh Dickins if (!PageUptodate(page)) { 13241aac1400SHugh Dickins if (inode->i_private) { 13251aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13261aac1400SHugh Dickins spin_lock(&inode->i_lock); 13271aac1400SHugh Dickins shmem_falloc = inode->i_private; 13281aac1400SHugh Dickins if (shmem_falloc && 13298e205f77SHugh Dickins !shmem_falloc->waitq && 13301aac1400SHugh Dickins index >= shmem_falloc->start && 13311aac1400SHugh Dickins index < shmem_falloc->next) 13321aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13331aac1400SHugh Dickins else 13341aac1400SHugh Dickins shmem_falloc = NULL; 13351aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13361aac1400SHugh Dickins if (shmem_falloc) 13371aac1400SHugh Dickins goto redirty; 13381aac1400SHugh Dickins } 13391635f6a7SHugh Dickins clear_highpage(page); 13401635f6a7SHugh Dickins flush_dcache_page(page); 13411635f6a7SHugh Dickins SetPageUptodate(page); 13421635f6a7SHugh Dickins } 13431635f6a7SHugh Dickins 134438d8b4e6SHuang Ying swap = get_swap_page(page); 134548f170fbSHugh Dickins if (!swap.val) 134648f170fbSHugh Dickins goto redirty; 1347d9fe526aSHugh Dickins 1348b1dea800SHugh Dickins /* 1349b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 13506922c0c7SHugh Dickins * if it's not already there. Do it now before the page is 13516922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1352b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13536922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13546922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1355b1dea800SHugh Dickins */ 1356b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 135705bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 1358b56a2d8aSVineeth Remanan Pillai list_add(&info->swaplist, &shmem_swaplist); 1359b1dea800SHugh Dickins 13604afab1cdSYang Shi if (add_to_swap_cache(page, swap, 13614afab1cdSYang Shi __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN) == 0) { 13624595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1363267a4c76SHugh Dickins shmem_recalc_inode(inode); 1364267a4c76SHugh Dickins info->swapped++; 13654595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1366267a4c76SHugh Dickins 1367aaa46865SHugh Dickins swap_shmem_alloc(swap); 13686922c0c7SHugh Dickins shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); 13696922c0c7SHugh Dickins 13706922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1371d9fe526aSHugh Dickins BUG_ON(page_mapped(page)); 13729fab5619SHugh Dickins swap_writepage(page, wbc); 13731da177e4SLinus Torvalds return 0; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds 13766922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 137775f6d6d2SMinchan Kim put_swap_page(page, swap); 13781da177e4SLinus Torvalds redirty: 13791da177e4SLinus Torvalds set_page_dirty(page); 1380d9fe526aSHugh Dickins if (wbc->for_reclaim) 1381d9fe526aSHugh Dickins return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ 1382d9fe526aSHugh Dickins unlock_page(page); 1383d9fe526aSHugh Dickins return 0; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 138675edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 138771fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1388680d794bSakpm@linux-foundation.org { 1389680d794bSakpm@linux-foundation.org char buffer[64]; 1390680d794bSakpm@linux-foundation.org 139171fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1392095f1fc4SLee Schermerhorn return; /* show nothing */ 1393095f1fc4SLee Schermerhorn 1394a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1395095f1fc4SLee Schermerhorn 1396095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1397680d794bSakpm@linux-foundation.org } 139871fe804bSLee Schermerhorn 139971fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 140071fe804bSLee Schermerhorn { 140171fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 140271fe804bSLee Schermerhorn if (sbinfo->mpol) { 140371fe804bSLee Schermerhorn spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 140471fe804bSLee Schermerhorn mpol = sbinfo->mpol; 140571fe804bSLee Schermerhorn mpol_get(mpol); 140671fe804bSLee Schermerhorn spin_unlock(&sbinfo->stat_lock); 140771fe804bSLee Schermerhorn } 140871fe804bSLee Schermerhorn return mpol; 140971fe804bSLee Schermerhorn } 141075edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 141175edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 141275edd345SHugh Dickins { 141375edd345SHugh Dickins } 141475edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 141575edd345SHugh Dickins { 141675edd345SHugh Dickins return NULL; 141775edd345SHugh Dickins } 141875edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 141975edd345SHugh Dickins #ifndef CONFIG_NUMA 142075edd345SHugh Dickins #define vm_policy vm_private_data 142175edd345SHugh Dickins #endif 1422680d794bSakpm@linux-foundation.org 1423800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1424800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1425800d8c63SKirill A. Shutemov { 1426800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 14272c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1428800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1429800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1430800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1431800d8c63SKirill A. Shutemov } 1432800d8c63SKirill A. Shutemov 1433800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1434800d8c63SKirill A. Shutemov { 1435800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1436800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1437800d8c63SKirill A. Shutemov } 1438800d8c63SKirill A. Shutemov 143941ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 144041ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14411da177e4SLinus Torvalds { 14421da177e4SLinus Torvalds struct vm_area_struct pvma; 144318a2f371SMel Gorman struct page *page; 1444e9e9b7ecSMinchan Kim struct vm_fault vmf; 14451da177e4SLinus Torvalds 1446800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1447e9e9b7ecSMinchan Kim vmf.vma = &pvma; 1448e9e9b7ecSMinchan Kim vmf.address = 0; 1449e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1450800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 145118a2f371SMel Gorman 1452800d8c63SKirill A. Shutemov return page; 1453800d8c63SKirill A. Shutemov } 145418a2f371SMel Gorman 1455800d8c63SKirill A. Shutemov static struct page *shmem_alloc_hugepage(gfp_t gfp, 1456800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1457800d8c63SKirill A. Shutemov { 1458800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 14597b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 14607b8d046fSMatthew Wilcox pgoff_t hindex; 1461800d8c63SKirill A. Shutemov struct page *page; 1462800d8c63SKirill A. Shutemov 14634620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 14647b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 14657b8d046fSMatthew Wilcox XA_PRESENT)) 1466800d8c63SKirill A. Shutemov return NULL; 1467800d8c63SKirill A. Shutemov 1468800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1469800d8c63SKirill A. Shutemov page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, 147019deb769SDavid Rientjes HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); 1471800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1472800d8c63SKirill A. Shutemov if (page) 1473800d8c63SKirill A. Shutemov prep_transhuge_page(page); 1474dcdf11eeSDavid Rientjes else 1475dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 147618a2f371SMel Gorman return page; 147718a2f371SMel Gorman } 147818a2f371SMel Gorman 147918a2f371SMel Gorman static struct page *shmem_alloc_page(gfp_t gfp, 148018a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 148118a2f371SMel Gorman { 148218a2f371SMel Gorman struct vm_area_struct pvma; 148318a2f371SMel Gorman struct page *page; 148418a2f371SMel Gorman 1485800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1486800d8c63SKirill A. Shutemov page = alloc_page_vma(gfp, &pvma, 0); 1487800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 148818a2f371SMel Gorman 1489800d8c63SKirill A. Shutemov return page; 1490800d8c63SKirill A. Shutemov } 1491800d8c63SKirill A. Shutemov 1492800d8c63SKirill A. Shutemov static struct page *shmem_alloc_and_acct_page(gfp_t gfp, 14930f079694SMike Rapoport struct inode *inode, 1494800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1495800d8c63SKirill A. Shutemov { 14960f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 1497800d8c63SKirill A. Shutemov struct page *page; 1498800d8c63SKirill A. Shutemov int nr; 1499800d8c63SKirill A. Shutemov int err = -ENOSPC; 1500800d8c63SKirill A. Shutemov 1501396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1502800d8c63SKirill A. Shutemov huge = false; 1503800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1504800d8c63SKirill A. Shutemov 15050f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1506800d8c63SKirill A. Shutemov goto failed; 1507800d8c63SKirill A. Shutemov 1508800d8c63SKirill A. Shutemov if (huge) 1509800d8c63SKirill A. Shutemov page = shmem_alloc_hugepage(gfp, info, index); 1510800d8c63SKirill A. Shutemov else 1511800d8c63SKirill A. Shutemov page = shmem_alloc_page(gfp, info, index); 151275edd345SHugh Dickins if (page) { 151375edd345SHugh Dickins __SetPageLocked(page); 151475edd345SHugh Dickins __SetPageSwapBacked(page); 1515800d8c63SKirill A. Shutemov return page; 151675edd345SHugh Dickins } 151718a2f371SMel Gorman 1518800d8c63SKirill A. Shutemov err = -ENOMEM; 15190f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1520800d8c63SKirill A. Shutemov failed: 1521800d8c63SKirill A. Shutemov return ERR_PTR(err); 15221da177e4SLinus Torvalds } 152371fe804bSLee Schermerhorn 15241da177e4SLinus Torvalds /* 1525bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1526bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1527bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1528bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1529bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1530bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1531bde05d1cSHugh Dickins * 1532bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1533bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1534bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1535bde05d1cSHugh Dickins */ 1536bde05d1cSHugh Dickins static bool shmem_should_replace_page(struct page *page, gfp_t gfp) 1537bde05d1cSHugh Dickins { 1538bde05d1cSHugh Dickins return page_zonenum(page) > gfp_zone(gfp); 1539bde05d1cSHugh Dickins } 1540bde05d1cSHugh Dickins 1541bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1542bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1543bde05d1cSHugh Dickins { 1544bde05d1cSHugh Dickins struct page *oldpage, *newpage; 1545bde05d1cSHugh Dickins struct address_space *swap_mapping; 1546c1cb20d4SYu Zhao swp_entry_t entry; 1547bde05d1cSHugh Dickins pgoff_t swap_index; 1548bde05d1cSHugh Dickins int error; 1549bde05d1cSHugh Dickins 1550bde05d1cSHugh Dickins oldpage = *pagep; 1551c1cb20d4SYu Zhao entry.val = page_private(oldpage); 1552c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1553bde05d1cSHugh Dickins swap_mapping = page_mapping(oldpage); 1554bde05d1cSHugh Dickins 1555bde05d1cSHugh Dickins /* 1556bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1557bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1558bde05d1cSHugh Dickins */ 1559bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1560bde05d1cSHugh Dickins newpage = shmem_alloc_page(gfp, info, index); 1561bde05d1cSHugh Dickins if (!newpage) 1562bde05d1cSHugh Dickins return -ENOMEM; 1563bde05d1cSHugh Dickins 156409cbfeafSKirill A. Shutemov get_page(newpage); 1565bde05d1cSHugh Dickins copy_highpage(newpage, oldpage); 15660142ef6cSHugh Dickins flush_dcache_page(newpage); 1567bde05d1cSHugh Dickins 15689956edf3SHugh Dickins __SetPageLocked(newpage); 15699956edf3SHugh Dickins __SetPageSwapBacked(newpage); 1570bde05d1cSHugh Dickins SetPageUptodate(newpage); 1571c1cb20d4SYu Zhao set_page_private(newpage, entry.val); 1572bde05d1cSHugh Dickins SetPageSwapCache(newpage); 1573bde05d1cSHugh Dickins 1574bde05d1cSHugh Dickins /* 1575bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1576bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1577bde05d1cSHugh Dickins */ 1578b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 157962f945b6SMatthew Wilcox error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); 15800142ef6cSHugh Dickins if (!error) { 158111fb9989SMel Gorman __inc_node_page_state(newpage, NR_FILE_PAGES); 158211fb9989SMel Gorman __dec_node_page_state(oldpage, NR_FILE_PAGES); 15830142ef6cSHugh Dickins } 1584b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1585bde05d1cSHugh Dickins 15860142ef6cSHugh Dickins if (unlikely(error)) { 15870142ef6cSHugh Dickins /* 15880142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 15890142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 15900142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 15910142ef6cSHugh Dickins */ 15920142ef6cSHugh Dickins oldpage = newpage; 15930142ef6cSHugh Dickins } else { 15946a93ca8fSJohannes Weiner mem_cgroup_migrate(oldpage, newpage); 1595bde05d1cSHugh Dickins lru_cache_add_anon(newpage); 15960142ef6cSHugh Dickins *pagep = newpage; 15970142ef6cSHugh Dickins } 1598bde05d1cSHugh Dickins 1599bde05d1cSHugh Dickins ClearPageSwapCache(oldpage); 1600bde05d1cSHugh Dickins set_page_private(oldpage, 0); 1601bde05d1cSHugh Dickins 1602bde05d1cSHugh Dickins unlock_page(oldpage); 160309cbfeafSKirill A. Shutemov put_page(oldpage); 160409cbfeafSKirill A. Shutemov put_page(oldpage); 16050142ef6cSHugh Dickins return error; 1606bde05d1cSHugh Dickins } 1607bde05d1cSHugh Dickins 1608bde05d1cSHugh Dickins /* 1609c5bf121eSVineeth Remanan Pillai * Swap in the page pointed to by *pagep. 1610c5bf121eSVineeth Remanan Pillai * Caller has to make sure that *pagep contains a valid swapped page. 1611c5bf121eSVineeth Remanan Pillai * Returns 0 and the page in pagep if success. On failure, returns the 1612c5bf121eSVineeth Remanan Pillai * the error code and NULL in *pagep. 16131da177e4SLinus Torvalds */ 1614c5bf121eSVineeth Remanan Pillai static int shmem_swapin_page(struct inode *inode, pgoff_t index, 1615c5bf121eSVineeth Remanan Pillai struct page **pagep, enum sgp_type sgp, 1616c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 16172b740303SSouptick Joarder vm_fault_t *fault_type) 16181da177e4SLinus Torvalds { 16191da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 162023f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 1621c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm; 162200501b53SJohannes Weiner struct mem_cgroup *memcg; 162327ab7006SHugh Dickins struct page *page; 16241da177e4SLinus Torvalds swp_entry_t swap; 16251da177e4SLinus Torvalds int error; 16261da177e4SLinus Torvalds 1627c5bf121eSVineeth Remanan Pillai VM_BUG_ON(!*pagep || !xa_is_value(*pagep)); 1628c5bf121eSVineeth Remanan Pillai swap = radix_to_swp_entry(*pagep); 1629c5bf121eSVineeth Remanan Pillai *pagep = NULL; 163054af6042SHugh Dickins 16311da177e4SLinus Torvalds /* Look it up and read it in.. */ 1632ec560175SHuang Ying page = lookup_swap_cache(swap, NULL, 0); 163327ab7006SHugh Dickins if (!page) { 16349e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 16359e18eb29SAndres Lagar-Cavilla if (fault_type) { 163668da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 16379e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 16382262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 16399e18eb29SAndres Lagar-Cavilla } 16409e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 164141ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 164227ab7006SHugh Dickins if (!page) { 16431da177e4SLinus Torvalds error = -ENOMEM; 164454af6042SHugh Dickins goto failed; 1645285b2c4fSHugh Dickins } 16461da177e4SLinus Torvalds } 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* We have to do this with page locked to prevent races */ 164954af6042SHugh Dickins lock_page(page); 16500142ef6cSHugh Dickins if (!PageSwapCache(page) || page_private(page) != swap.val || 1651d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1652c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1653d1899228SHugh Dickins goto unlock; 1654bde05d1cSHugh Dickins } 165527ab7006SHugh Dickins if (!PageUptodate(page)) { 16561da177e4SLinus Torvalds error = -EIO; 165754af6042SHugh Dickins goto failed; 165854af6042SHugh Dickins } 165954af6042SHugh Dickins wait_on_page_writeback(page); 166054af6042SHugh Dickins 1661bde05d1cSHugh Dickins if (shmem_should_replace_page(page, gfp)) { 1662bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 1663bde05d1cSHugh Dickins if (error) 166454af6042SHugh Dickins goto failed; 16651da177e4SLinus Torvalds } 16661da177e4SLinus Torvalds 16672cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1668f627c2f5SKirill A. Shutemov false); 1669d1899228SHugh Dickins if (!error) { 167054af6042SHugh Dickins error = shmem_add_to_page_cache(page, mapping, index, 1671552446a4SMatthew Wilcox swp_to_radix_entry(swap), gfp); 1672215c02bcSHugh Dickins /* 1673215c02bcSHugh Dickins * We already confirmed swap under page lock, and make 1674215c02bcSHugh Dickins * no memory allocation here, so usually no possibility 1675215c02bcSHugh Dickins * of error; but free_swap_and_cache() only trylocks a 1676215c02bcSHugh Dickins * page, so it is just possible that the entry has been 1677215c02bcSHugh Dickins * truncated or holepunched since swap was confirmed. 1678215c02bcSHugh Dickins * shmem_undo_range() will have done some of the 1679215c02bcSHugh Dickins * unaccounting, now delete_from_swap_cache() will do 168093aa7d95SVladimir Davydov * the rest. 1681215c02bcSHugh Dickins */ 168200501b53SJohannes Weiner if (error) { 1683f627c2f5SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, false); 1684215c02bcSHugh Dickins delete_from_swap_cache(page); 1685d1899228SHugh Dickins } 168600501b53SJohannes Weiner } 168754af6042SHugh Dickins if (error) 168854af6042SHugh Dickins goto failed; 168954af6042SHugh Dickins 1690f627c2f5SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, true, false); 169100501b53SJohannes Weiner 16924595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 169354af6042SHugh Dickins info->swapped--; 169454af6042SHugh Dickins shmem_recalc_inode(inode); 16954595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 169627ab7006SHugh Dickins 169766d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 169866d2f4d2SHugh Dickins mark_page_accessed(page); 169966d2f4d2SHugh Dickins 170027ab7006SHugh Dickins delete_from_swap_cache(page); 170127ab7006SHugh Dickins set_page_dirty(page); 170227ab7006SHugh Dickins swap_free(swap); 170327ab7006SHugh Dickins 1704c5bf121eSVineeth Remanan Pillai *pagep = page; 1705c5bf121eSVineeth Remanan Pillai return 0; 1706c5bf121eSVineeth Remanan Pillai failed: 1707c5bf121eSVineeth Remanan Pillai if (!shmem_confirm_swap(mapping, index, swap)) 1708c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1709c5bf121eSVineeth Remanan Pillai unlock: 1710c5bf121eSVineeth Remanan Pillai if (page) { 1711c5bf121eSVineeth Remanan Pillai unlock_page(page); 1712c5bf121eSVineeth Remanan Pillai put_page(page); 1713c5bf121eSVineeth Remanan Pillai } 1714c5bf121eSVineeth Remanan Pillai 1715c5bf121eSVineeth Remanan Pillai return error; 1716c5bf121eSVineeth Remanan Pillai } 1717c5bf121eSVineeth Remanan Pillai 1718c5bf121eSVineeth Remanan Pillai /* 1719c5bf121eSVineeth Remanan Pillai * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 1720c5bf121eSVineeth Remanan Pillai * 1721c5bf121eSVineeth Remanan Pillai * If we allocate a new one we do not mark it dirty. That's up to the 1722c5bf121eSVineeth Remanan Pillai * vm. If we swap it in we mark it dirty since we also free the swap 1723c5bf121eSVineeth Remanan Pillai * entry since a page cannot live in both the swap and page cache. 1724c5bf121eSVineeth Remanan Pillai * 172528eb3c80SMiles Chen * vmf and fault_type are only supplied by shmem_fault: 1726c5bf121eSVineeth Remanan Pillai * otherwise they are NULL. 1727c5bf121eSVineeth Remanan Pillai */ 1728c5bf121eSVineeth Remanan Pillai static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1729c5bf121eSVineeth Remanan Pillai struct page **pagep, enum sgp_type sgp, gfp_t gfp, 1730c5bf121eSVineeth Remanan Pillai struct vm_area_struct *vma, struct vm_fault *vmf, 1731c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type) 1732c5bf121eSVineeth Remanan Pillai { 1733c5bf121eSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1734c5bf121eSVineeth Remanan Pillai struct shmem_inode_info *info = SHMEM_I(inode); 1735c5bf121eSVineeth Remanan Pillai struct shmem_sb_info *sbinfo; 1736c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm; 1737c5bf121eSVineeth Remanan Pillai struct mem_cgroup *memcg; 1738c5bf121eSVineeth Remanan Pillai struct page *page; 1739c5bf121eSVineeth Remanan Pillai enum sgp_type sgp_huge = sgp; 1740c5bf121eSVineeth Remanan Pillai pgoff_t hindex = index; 1741c5bf121eSVineeth Remanan Pillai int error; 1742c5bf121eSVineeth Remanan Pillai int once = 0; 1743c5bf121eSVineeth Remanan Pillai int alloced = 0; 1744c5bf121eSVineeth Remanan Pillai 1745c5bf121eSVineeth Remanan Pillai if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 1746c5bf121eSVineeth Remanan Pillai return -EFBIG; 1747c5bf121eSVineeth Remanan Pillai if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) 1748c5bf121eSVineeth Remanan Pillai sgp = SGP_CACHE; 1749c5bf121eSVineeth Remanan Pillai repeat: 1750c5bf121eSVineeth Remanan Pillai if (sgp <= SGP_CACHE && 1751c5bf121eSVineeth Remanan Pillai ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1752c5bf121eSVineeth Remanan Pillai return -EINVAL; 1753c5bf121eSVineeth Remanan Pillai } 1754c5bf121eSVineeth Remanan Pillai 1755c5bf121eSVineeth Remanan Pillai sbinfo = SHMEM_SB(inode->i_sb); 1756c5bf121eSVineeth Remanan Pillai charge_mm = vma ? vma->vm_mm : current->mm; 1757c5bf121eSVineeth Remanan Pillai 1758c5bf121eSVineeth Remanan Pillai page = find_lock_entry(mapping, index); 1759c5bf121eSVineeth Remanan Pillai if (xa_is_value(page)) { 1760c5bf121eSVineeth Remanan Pillai error = shmem_swapin_page(inode, index, &page, 1761c5bf121eSVineeth Remanan Pillai sgp, gfp, vma, fault_type); 1762c5bf121eSVineeth Remanan Pillai if (error == -EEXIST) 1763c5bf121eSVineeth Remanan Pillai goto repeat; 1764c5bf121eSVineeth Remanan Pillai 1765c5bf121eSVineeth Remanan Pillai *pagep = page; 1766c5bf121eSVineeth Remanan Pillai return error; 1767c5bf121eSVineeth Remanan Pillai } 1768c5bf121eSVineeth Remanan Pillai 1769c5bf121eSVineeth Remanan Pillai if (page && sgp == SGP_WRITE) 1770c5bf121eSVineeth Remanan Pillai mark_page_accessed(page); 1771c5bf121eSVineeth Remanan Pillai 1772c5bf121eSVineeth Remanan Pillai /* fallocated page? */ 1773c5bf121eSVineeth Remanan Pillai if (page && !PageUptodate(page)) { 1774c5bf121eSVineeth Remanan Pillai if (sgp != SGP_READ) 1775c5bf121eSVineeth Remanan Pillai goto clear; 1776c5bf121eSVineeth Remanan Pillai unlock_page(page); 1777c5bf121eSVineeth Remanan Pillai put_page(page); 1778c5bf121eSVineeth Remanan Pillai page = NULL; 1779c5bf121eSVineeth Remanan Pillai } 1780c5bf121eSVineeth Remanan Pillai if (page || sgp == SGP_READ) { 1781c5bf121eSVineeth Remanan Pillai *pagep = page; 1782c5bf121eSVineeth Remanan Pillai return 0; 1783c5bf121eSVineeth Remanan Pillai } 1784c5bf121eSVineeth Remanan Pillai 1785c5bf121eSVineeth Remanan Pillai /* 1786c5bf121eSVineeth Remanan Pillai * Fast cache lookup did not find it: 1787c5bf121eSVineeth Remanan Pillai * bring it back from swap or allocate. 1788c5bf121eSVineeth Remanan Pillai */ 1789c5bf121eSVineeth Remanan Pillai 1790cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1791cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1792cfda0526SMike Rapoport return 0; 1793cfda0526SMike Rapoport } 1794cfda0526SMike Rapoport 1795800d8c63SKirill A. Shutemov /* shmem_symlink() */ 1796800d8c63SKirill A. Shutemov if (mapping->a_ops != &shmem_aops) 1797800d8c63SKirill A. Shutemov goto alloc_nohuge; 1798657e3038SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) 1799800d8c63SKirill A. Shutemov goto alloc_nohuge; 1800800d8c63SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 1801800d8c63SKirill A. Shutemov goto alloc_huge; 1802800d8c63SKirill A. Shutemov switch (sbinfo->huge) { 1803800d8c63SKirill A. Shutemov case SHMEM_HUGE_NEVER: 1804800d8c63SKirill A. Shutemov goto alloc_nohuge; 180527d80fa2SKees Cook case SHMEM_HUGE_WITHIN_SIZE: { 180627d80fa2SKees Cook loff_t i_size; 180727d80fa2SKees Cook pgoff_t off; 180827d80fa2SKees Cook 1809800d8c63SKirill A. Shutemov off = round_up(index, HPAGE_PMD_NR); 1810800d8c63SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 1811800d8c63SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 1812800d8c63SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 1813800d8c63SKirill A. Shutemov goto alloc_huge; 181427d80fa2SKees Cook 181527d80fa2SKees Cook fallthrough; 181627d80fa2SKees Cook } 1817800d8c63SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 1818657e3038SKirill A. Shutemov if (sgp_huge == SGP_HUGE) 1819657e3038SKirill A. Shutemov goto alloc_huge; 1820657e3038SKirill A. Shutemov /* TODO: implement fadvise() hints */ 1821800d8c63SKirill A. Shutemov goto alloc_nohuge; 182259a16eadSHugh Dickins } 18231da177e4SLinus Torvalds 1824800d8c63SKirill A. Shutemov alloc_huge: 18250f079694SMike Rapoport page = shmem_alloc_and_acct_page(gfp, inode, index, true); 1826800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1827c5bf121eSVineeth Remanan Pillai alloc_nohuge: 1828c5bf121eSVineeth Remanan Pillai page = shmem_alloc_and_acct_page(gfp, inode, 1829800d8c63SKirill A. Shutemov index, false); 183054af6042SHugh Dickins } 1831800d8c63SKirill A. Shutemov if (IS_ERR(page)) { 1832779750d2SKirill A. Shutemov int retry = 5; 1833c5bf121eSVineeth Remanan Pillai 1834800d8c63SKirill A. Shutemov error = PTR_ERR(page); 1835800d8c63SKirill A. Shutemov page = NULL; 1836779750d2SKirill A. Shutemov if (error != -ENOSPC) 1837c5bf121eSVineeth Remanan Pillai goto unlock; 1838779750d2SKirill A. Shutemov /* 1839c5bf121eSVineeth Remanan Pillai * Try to reclaim some space by splitting a huge page 1840779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1841779750d2SKirill A. Shutemov */ 1842779750d2SKirill A. Shutemov while (retry--) { 1843779750d2SKirill A. Shutemov int ret; 1844c5bf121eSVineeth Remanan Pillai 1845779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1846779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1847779750d2SKirill A. Shutemov break; 1848779750d2SKirill A. Shutemov if (ret) 1849779750d2SKirill A. Shutemov goto alloc_nohuge; 1850779750d2SKirill A. Shutemov } 1851c5bf121eSVineeth Remanan Pillai goto unlock; 1852800d8c63SKirill A. Shutemov } 1853800d8c63SKirill A. Shutemov 1854800d8c63SKirill A. Shutemov if (PageTransHuge(page)) 1855800d8c63SKirill A. Shutemov hindex = round_down(index, HPAGE_PMD_NR); 1856800d8c63SKirill A. Shutemov else 1857800d8c63SKirill A. Shutemov hindex = index; 1858800d8c63SKirill A. Shutemov 185966d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1860eb39d618SHugh Dickins __SetPageReferenced(page); 186166d2f4d2SHugh Dickins 18622cf85583STejun Heo error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg, 1863800d8c63SKirill A. Shutemov PageTransHuge(page)); 1864dcdf11eeSDavid Rientjes if (error) { 186585b9f46eSDavid Rientjes if (PageTransHuge(page)) { 1866dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 186785b9f46eSDavid Rientjes count_vm_event(THP_FILE_FALLBACK_CHARGE); 186885b9f46eSDavid Rientjes } 1869800d8c63SKirill A. Shutemov goto unacct; 1870dcdf11eeSDavid Rientjes } 1871800d8c63SKirill A. Shutemov error = shmem_add_to_page_cache(page, mapping, hindex, 1872552446a4SMatthew Wilcox NULL, gfp & GFP_RECLAIM_MASK); 1873b065b432SHugh Dickins if (error) { 1874800d8c63SKirill A. Shutemov mem_cgroup_cancel_charge(page, memcg, 1875800d8c63SKirill A. Shutemov PageTransHuge(page)); 1876800d8c63SKirill A. Shutemov goto unacct; 1877b065b432SHugh Dickins } 1878800d8c63SKirill A. Shutemov mem_cgroup_commit_charge(page, memcg, false, 1879800d8c63SKirill A. Shutemov PageTransHuge(page)); 188054af6042SHugh Dickins lru_cache_add_anon(page); 188154af6042SHugh Dickins 18824595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1883d8c6546bSMatthew Wilcox (Oracle) info->alloced += compound_nr(page); 1884800d8c63SKirill A. Shutemov inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); 188554af6042SHugh Dickins shmem_recalc_inode(inode); 18864595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 18871635f6a7SHugh Dickins alloced = true; 188854af6042SHugh Dickins 1889779750d2SKirill A. Shutemov if (PageTransHuge(page) && 1890779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1891779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1892779750d2SKirill A. Shutemov /* 1893779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1894779750d2SKirill A. Shutemov * to shrink under memory pressure. 1895779750d2SKirill A. Shutemov */ 1896779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1897d041353dSCong Wang /* 1898d041353dSCong Wang * _careful to defend against unlocked access to 1899d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1900d041353dSCong Wang */ 1901d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1902779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1903779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1904779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1905779750d2SKirill A. Shutemov } 1906779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1907779750d2SKirill A. Shutemov } 1908779750d2SKirill A. Shutemov 1909ec9516fbSHugh Dickins /* 19101635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 19111635f6a7SHugh Dickins */ 19121635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 19131635f6a7SHugh Dickins sgp = SGP_WRITE; 19141635f6a7SHugh Dickins clear: 19151635f6a7SHugh Dickins /* 19161635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 19171635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 19181635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1919ec9516fbSHugh Dickins */ 1920800d8c63SKirill A. Shutemov if (sgp != SGP_WRITE && !PageUptodate(page)) { 1921800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 1922800d8c63SKirill A. Shutemov int i; 1923800d8c63SKirill A. Shutemov 1924d8c6546bSMatthew Wilcox (Oracle) for (i = 0; i < compound_nr(head); i++) { 1925800d8c63SKirill A. Shutemov clear_highpage(head + i); 1926800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 1927800d8c63SKirill A. Shutemov } 1928800d8c63SKirill A. Shutemov SetPageUptodate(head); 1929ec9516fbSHugh Dickins } 1930bde05d1cSHugh Dickins 193154af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 193275edd345SHugh Dickins if (sgp <= SGP_CACHE && 193309cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1934267a4c76SHugh Dickins if (alloced) { 1935267a4c76SHugh Dickins ClearPageDirty(page); 1936267a4c76SHugh Dickins delete_from_page_cache(page); 19374595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1938267a4c76SHugh Dickins shmem_recalc_inode(inode); 19394595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1940267a4c76SHugh Dickins } 194154af6042SHugh Dickins error = -EINVAL; 1942267a4c76SHugh Dickins goto unlock; 1943ff36b801SShaohua Li } 1944800d8c63SKirill A. Shutemov *pagep = page + index - hindex; 194554af6042SHugh Dickins return 0; 1946d00806b1SNick Piggin 1947d0217ac0SNick Piggin /* 194854af6042SHugh Dickins * Error recovery. 19491da177e4SLinus Torvalds */ 195054af6042SHugh Dickins unacct: 1951d8c6546bSMatthew Wilcox (Oracle) shmem_inode_unacct_blocks(inode, compound_nr(page)); 1952800d8c63SKirill A. Shutemov 1953800d8c63SKirill A. Shutemov if (PageTransHuge(page)) { 1954800d8c63SKirill A. Shutemov unlock_page(page); 1955800d8c63SKirill A. Shutemov put_page(page); 1956800d8c63SKirill A. Shutemov goto alloc_nohuge; 1957800d8c63SKirill A. Shutemov } 1958d1899228SHugh Dickins unlock: 195927ab7006SHugh Dickins if (page) { 196054af6042SHugh Dickins unlock_page(page); 196109cbfeafSKirill A. Shutemov put_page(page); 196254af6042SHugh Dickins } 196354af6042SHugh Dickins if (error == -ENOSPC && !once++) { 19644595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 196554af6042SHugh Dickins shmem_recalc_inode(inode); 19664595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19671da177e4SLinus Torvalds goto repeat; 1968d8dc74f2SAdrian Bunk } 19697f4446eeSMatthew Wilcox if (error == -EEXIST) 197054af6042SHugh Dickins goto repeat; 197154af6042SHugh Dickins return error; 19721da177e4SLinus Torvalds } 19731da177e4SLinus Torvalds 197410d20bd2SLinus Torvalds /* 197510d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 197610d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 197710d20bd2SLinus Torvalds * target. 197810d20bd2SLinus Torvalds */ 1979ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 198010d20bd2SLinus Torvalds { 198110d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 19822055da97SIngo Molnar list_del_init(&wait->entry); 198310d20bd2SLinus Torvalds return ret; 198410d20bd2SLinus Torvalds } 198510d20bd2SLinus Torvalds 198620acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 19871da177e4SLinus Torvalds { 198811bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 1989496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 19909e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 1991657e3038SKirill A. Shutemov enum sgp_type sgp; 199220acce67SSouptick Joarder int err; 199320acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 19941da177e4SLinus Torvalds 1995f00cdc6dSHugh Dickins /* 1996f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 1997f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 1998f00cdc6dSHugh Dickins * locks writers out with its hold on i_mutex. So refrain from 19998e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 20008e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 20018e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 20028e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 20038e205f77SHugh Dickins * 20048e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 20058e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 20068e205f77SHugh Dickins * we just need to make racing faults a rare case. 20078e205f77SHugh Dickins * 20088e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 20098e205f77SHugh Dickins * standard mutex or completion: but we cannot take i_mutex in fault, 20108e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 2011f00cdc6dSHugh Dickins */ 2012f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 2013f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 2014f00cdc6dSHugh Dickins 2015f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2016f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 20178e205f77SHugh Dickins if (shmem_falloc && 20188e205f77SHugh Dickins shmem_falloc->waitq && 20198e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 20208e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 20218897c1b1SKirill A. Shutemov struct file *fpin; 20228e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 202310d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 20248e205f77SHugh Dickins 20258e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 20268897c1b1SKirill A. Shutemov fpin = maybe_unlock_mmap_for_io(vmf, NULL); 20278897c1b1SKirill A. Shutemov if (fpin) 20288e205f77SHugh Dickins ret = VM_FAULT_RETRY; 20298e205f77SHugh Dickins 20308e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 20318e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 20328e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 20338e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20348e205f77SHugh Dickins schedule(); 20358e205f77SHugh Dickins 20368e205f77SHugh Dickins /* 20378e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 20388e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 20398e205f77SHugh Dickins * is usually invalid by the time we reach here, but 20408e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 20418e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 20428e205f77SHugh Dickins */ 20438e205f77SHugh Dickins spin_lock(&inode->i_lock); 20448e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 20458e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20468897c1b1SKirill A. Shutemov 20478897c1b1SKirill A. Shutemov if (fpin) 20488897c1b1SKirill A. Shutemov fput(fpin); 20498e205f77SHugh Dickins return ret; 2050f00cdc6dSHugh Dickins } 20518e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2052f00cdc6dSHugh Dickins } 2053f00cdc6dSHugh Dickins 2054657e3038SKirill A. Shutemov sgp = SGP_CACHE; 205518600332SMichal Hocko 205618600332SMichal Hocko if ((vma->vm_flags & VM_NOHUGEPAGE) || 205718600332SMichal Hocko test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 2058657e3038SKirill A. Shutemov sgp = SGP_NOHUGE; 205918600332SMichal Hocko else if (vma->vm_flags & VM_HUGEPAGE) 206018600332SMichal Hocko sgp = SGP_HUGE; 2061657e3038SKirill A. Shutemov 206220acce67SSouptick Joarder err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, 2063cfda0526SMike Rapoport gfp, vma, vmf, &ret); 206420acce67SSouptick Joarder if (err) 206520acce67SSouptick Joarder return vmf_error(err); 206668da9f05SHugh Dickins return ret; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2070c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2071c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2072c01d5b30SHugh Dickins { 2073c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2074c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2075c01d5b30SHugh Dickins unsigned long addr; 2076c01d5b30SHugh Dickins unsigned long offset; 2077c01d5b30SHugh Dickins unsigned long inflated_len; 2078c01d5b30SHugh Dickins unsigned long inflated_addr; 2079c01d5b30SHugh Dickins unsigned long inflated_offset; 2080c01d5b30SHugh Dickins 2081c01d5b30SHugh Dickins if (len > TASK_SIZE) 2082c01d5b30SHugh Dickins return -ENOMEM; 2083c01d5b30SHugh Dickins 2084c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2085c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2086c01d5b30SHugh Dickins 2087396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2088c01d5b30SHugh Dickins return addr; 2089c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2090c01d5b30SHugh Dickins return addr; 2091c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2092c01d5b30SHugh Dickins return addr; 2093c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2094c01d5b30SHugh Dickins return addr; 2095c01d5b30SHugh Dickins 2096c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2097c01d5b30SHugh Dickins return addr; 2098c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2099c01d5b30SHugh Dickins return addr; 2100c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2101c01d5b30SHugh Dickins return addr; 2102c01d5b30SHugh Dickins /* 2103c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2104c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 210599158997SKirill A. Shutemov * But if caller specified an address hint and we allocated area there 210699158997SKirill A. Shutemov * successfully, respect that as before. 2107c01d5b30SHugh Dickins */ 210899158997SKirill A. Shutemov if (uaddr == addr) 2109c01d5b30SHugh Dickins return addr; 2110c01d5b30SHugh Dickins 2111c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2112c01d5b30SHugh Dickins struct super_block *sb; 2113c01d5b30SHugh Dickins 2114c01d5b30SHugh Dickins if (file) { 2115c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2116c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2117c01d5b30SHugh Dickins } else { 2118c01d5b30SHugh Dickins /* 2119c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2120c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2121c01d5b30SHugh Dickins */ 2122c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2123c01d5b30SHugh Dickins return addr; 2124c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2125c01d5b30SHugh Dickins } 21263089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2127c01d5b30SHugh Dickins return addr; 2128c01d5b30SHugh Dickins } 2129c01d5b30SHugh Dickins 2130c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2131c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2132c01d5b30SHugh Dickins return addr; 2133c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2134c01d5b30SHugh Dickins return addr; 2135c01d5b30SHugh Dickins 2136c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2137c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2138c01d5b30SHugh Dickins return addr; 2139c01d5b30SHugh Dickins if (inflated_len < len) 2140c01d5b30SHugh Dickins return addr; 2141c01d5b30SHugh Dickins 214299158997SKirill A. Shutemov inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); 2143c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2144c01d5b30SHugh Dickins return addr; 2145c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2146c01d5b30SHugh Dickins return addr; 2147c01d5b30SHugh Dickins 2148c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2149c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2150c01d5b30SHugh Dickins if (inflated_offset > offset) 2151c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2152c01d5b30SHugh Dickins 2153c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2154c01d5b30SHugh Dickins return addr; 2155c01d5b30SHugh Dickins return inflated_addr; 2156c01d5b30SHugh Dickins } 2157c01d5b30SHugh Dickins 21581da177e4SLinus Torvalds #ifdef CONFIG_NUMA 215941ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 21601da177e4SLinus Torvalds { 2161496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 216241ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds 2165d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2166d8dc74f2SAdrian Bunk unsigned long addr) 21671da177e4SLinus Torvalds { 2168496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 216941ffe5d5SHugh Dickins pgoff_t index; 21701da177e4SLinus Torvalds 217141ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 217241ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 21731da177e4SLinus Torvalds } 21741da177e4SLinus Torvalds #endif 21751da177e4SLinus Torvalds 21761da177e4SLinus Torvalds int shmem_lock(struct file *file, int lock, struct user_struct *user) 21771da177e4SLinus Torvalds { 2178496ad9aaSAl Viro struct inode *inode = file_inode(file); 21791da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 21801da177e4SLinus Torvalds int retval = -ENOMEM; 21811da177e4SLinus Torvalds 2182*ea0dfeb4SHugh Dickins /* 2183*ea0dfeb4SHugh Dickins * What serializes the accesses to info->flags? 2184*ea0dfeb4SHugh Dickins * ipc_lock_object() when called from shmctl_do_lock(), 2185*ea0dfeb4SHugh Dickins * no serialization needed when called from shm_destroy(). 2186*ea0dfeb4SHugh Dickins */ 21871da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 21881da177e4SLinus Torvalds if (!user_shm_lock(inode->i_size, user)) 21891da177e4SLinus Torvalds goto out_nomem; 21901da177e4SLinus Torvalds info->flags |= VM_LOCKED; 219189e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 21921da177e4SLinus Torvalds } 21931da177e4SLinus Torvalds if (!lock && (info->flags & VM_LOCKED) && user) { 21941da177e4SLinus Torvalds user_shm_unlock(inode->i_size, user); 21951da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 219689e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds retval = 0; 219989e004eaSLee Schermerhorn 22001da177e4SLinus Torvalds out_nomem: 22011da177e4SLinus Torvalds return retval; 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds 22049b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 22051da177e4SLinus Torvalds { 2206ab3948f5SJoel Fernandes (Google) struct shmem_inode_info *info = SHMEM_I(file_inode(file)); 2207ab3948f5SJoel Fernandes (Google) 2208ab3948f5SJoel Fernandes (Google) if (info->seals & F_SEAL_FUTURE_WRITE) { 2209ab3948f5SJoel Fernandes (Google) /* 2210ab3948f5SJoel Fernandes (Google) * New PROT_WRITE and MAP_SHARED mmaps are not allowed when 2211ab3948f5SJoel Fernandes (Google) * "future write" seal active. 2212ab3948f5SJoel Fernandes (Google) */ 2213ab3948f5SJoel Fernandes (Google) if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE)) 2214ab3948f5SJoel Fernandes (Google) return -EPERM; 2215ab3948f5SJoel Fernandes (Google) 2216ab3948f5SJoel Fernandes (Google) /* 221705d35110SNicolas Geoffray * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as 221805d35110SNicolas Geoffray * MAP_SHARED and read-only, take care to not allow mprotect to 221905d35110SNicolas Geoffray * revert protections on such mappings. Do this only for shared 222005d35110SNicolas Geoffray * mappings. For private mappings, don't need to mask 222105d35110SNicolas Geoffray * VM_MAYWRITE as we still want them to be COW-writable. 2222ab3948f5SJoel Fernandes (Google) */ 222305d35110SNicolas Geoffray if (vma->vm_flags & VM_SHARED) 2224ab3948f5SJoel Fernandes (Google) vma->vm_flags &= ~(VM_MAYWRITE); 2225ab3948f5SJoel Fernandes (Google) } 2226ab3948f5SJoel Fernandes (Google) 22271da177e4SLinus Torvalds file_accessed(file); 22281da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 2229396bcc52SMatthew Wilcox (Oracle) if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 2230f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 2231f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 2232f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 2233f3f0e1d2SKirill A. Shutemov } 22341da177e4SLinus Torvalds return 0; 22351da177e4SLinus Torvalds } 22361da177e4SLinus Torvalds 2237454abafeSDmitry Monakhov static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, 223809208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 22391da177e4SLinus Torvalds { 22401da177e4SLinus Torvalds struct inode *inode; 22411da177e4SLinus Torvalds struct shmem_inode_info *info; 22421da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 22431da177e4SLinus Torvalds 22445b04c689SPavel Emelyanov if (shmem_reserve_inode(sb)) 22451da177e4SLinus Torvalds return NULL; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds inode = new_inode(sb); 22481da177e4SLinus Torvalds if (inode) { 224985fe4025SChristoph Hellwig inode->i_ino = get_next_ino(); 2250454abafeSDmitry Monakhov inode_init_owner(inode, dir, mode); 22511da177e4SLinus Torvalds inode->i_blocks = 0; 2252078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 225346c9a946SArnd Bergmann inode->i_generation = prandom_u32(); 22541da177e4SLinus Torvalds info = SHMEM_I(inode); 22551da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 22561da177e4SLinus Torvalds spin_lock_init(&info->lock); 2257af53d3e9SHugh Dickins atomic_set(&info->stop_eviction, 0); 225840e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 22590b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2260779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 22611da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 226238f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 226372c04902SAl Viro cache_no_acl(inode); 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds switch (mode & S_IFMT) { 22661da177e4SLinus Torvalds default: 226739f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 22681da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 22691da177e4SLinus Torvalds break; 22701da177e4SLinus Torvalds case S_IFREG: 227114fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 22721da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 22731da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 227471fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 227571fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 22761da177e4SLinus Torvalds break; 22771da177e4SLinus Torvalds case S_IFDIR: 2278d8c76e6fSDave Hansen inc_nlink(inode); 22791da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 22801da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 22811da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 22821da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 22831da177e4SLinus Torvalds break; 22841da177e4SLinus Torvalds case S_IFLNK: 22851da177e4SLinus Torvalds /* 22861da177e4SLinus Torvalds * Must not load anything in the rbtree, 22871da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 22881da177e4SLinus Torvalds */ 228971fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 22901da177e4SLinus Torvalds break; 22911da177e4SLinus Torvalds } 2292b45d71fbSJoel Fernandes (Google) 2293b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 22945b04c689SPavel Emelyanov } else 22955b04c689SPavel Emelyanov shmem_free_inode(sb); 22961da177e4SLinus Torvalds return inode; 22971da177e4SLinus Torvalds } 22981da177e4SLinus Torvalds 22990cd6144aSJohannes Weiner bool shmem_mapping(struct address_space *mapping) 23000cd6144aSJohannes Weiner { 2301f8005451SHugh Dickins return mapping->a_ops == &shmem_aops; 23020cd6144aSJohannes Weiner } 23030cd6144aSJohannes Weiner 23048d103963SMike Rapoport static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 23054c27fe4cSMike Rapoport pmd_t *dst_pmd, 23064c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 23074c27fe4cSMike Rapoport unsigned long dst_addr, 23084c27fe4cSMike Rapoport unsigned long src_addr, 23098d103963SMike Rapoport bool zeropage, 23104c27fe4cSMike Rapoport struct page **pagep) 23114c27fe4cSMike Rapoport { 23124c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 23134c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 23144c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 23154c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 23164c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 23174c27fe4cSMike Rapoport struct mem_cgroup *memcg; 23184c27fe4cSMike Rapoport spinlock_t *ptl; 23194c27fe4cSMike Rapoport void *page_kaddr; 23204c27fe4cSMike Rapoport struct page *page; 23214c27fe4cSMike Rapoport pte_t _dst_pte, *dst_pte; 23224c27fe4cSMike Rapoport int ret; 2323e2a50c1fSAndrea Arcangeli pgoff_t offset, max_off; 23244c27fe4cSMike Rapoport 23254c27fe4cSMike Rapoport ret = -ENOMEM; 23260f079694SMike Rapoport if (!shmem_inode_acct_block(inode, 1)) 23274c27fe4cSMike Rapoport goto out; 23284c27fe4cSMike Rapoport 2329cb658a45SAndrea Arcangeli if (!*pagep) { 23304c27fe4cSMike Rapoport page = shmem_alloc_page(gfp, info, pgoff); 23314c27fe4cSMike Rapoport if (!page) 23320f079694SMike Rapoport goto out_unacct_blocks; 23334c27fe4cSMike Rapoport 23348d103963SMike Rapoport if (!zeropage) { /* mcopy_atomic */ 23354c27fe4cSMike Rapoport page_kaddr = kmap_atomic(page); 23368d103963SMike Rapoport ret = copy_from_user(page_kaddr, 23378d103963SMike Rapoport (const void __user *)src_addr, 23384c27fe4cSMike Rapoport PAGE_SIZE); 23394c27fe4cSMike Rapoport kunmap_atomic(page_kaddr); 23404c27fe4cSMike Rapoport 23414c27fe4cSMike Rapoport /* fallback to copy_from_user outside mmap_sem */ 23424c27fe4cSMike Rapoport if (unlikely(ret)) { 23434c27fe4cSMike Rapoport *pagep = page; 23440f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 23454c27fe4cSMike Rapoport /* don't free the page */ 23469e368259SAndrea Arcangeli return -ENOENT; 23474c27fe4cSMike Rapoport } 23488d103963SMike Rapoport } else { /* mfill_zeropage_atomic */ 23498d103963SMike Rapoport clear_highpage(page); 23508d103963SMike Rapoport } 23514c27fe4cSMike Rapoport } else { 23524c27fe4cSMike Rapoport page = *pagep; 23534c27fe4cSMike Rapoport *pagep = NULL; 23544c27fe4cSMike Rapoport } 23554c27fe4cSMike Rapoport 23569cc90c66SAndrea Arcangeli VM_BUG_ON(PageLocked(page) || PageSwapBacked(page)); 23579cc90c66SAndrea Arcangeli __SetPageLocked(page); 23589cc90c66SAndrea Arcangeli __SetPageSwapBacked(page); 2359a425d358SAndrea Arcangeli __SetPageUptodate(page); 23609cc90c66SAndrea Arcangeli 2361e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2362e2a50c1fSAndrea Arcangeli offset = linear_page_index(dst_vma, dst_addr); 2363e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 2364e2a50c1fSAndrea Arcangeli if (unlikely(offset >= max_off)) 2365e2a50c1fSAndrea Arcangeli goto out_release; 2366e2a50c1fSAndrea Arcangeli 23672cf85583STejun Heo ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false); 23684c27fe4cSMike Rapoport if (ret) 23694c27fe4cSMike Rapoport goto out_release; 23704c27fe4cSMike Rapoport 2371552446a4SMatthew Wilcox ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL, 2372552446a4SMatthew Wilcox gfp & GFP_RECLAIM_MASK); 23734c27fe4cSMike Rapoport if (ret) 23744c27fe4cSMike Rapoport goto out_release_uncharge; 23754c27fe4cSMike Rapoport 23764c27fe4cSMike Rapoport mem_cgroup_commit_charge(page, memcg, false, false); 23774c27fe4cSMike Rapoport 23784c27fe4cSMike Rapoport _dst_pte = mk_pte(page, dst_vma->vm_page_prot); 23794c27fe4cSMike Rapoport if (dst_vma->vm_flags & VM_WRITE) 23804c27fe4cSMike Rapoport _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte)); 2381dcf7fe9dSAndrea Arcangeli else { 2382dcf7fe9dSAndrea Arcangeli /* 2383dcf7fe9dSAndrea Arcangeli * We don't set the pte dirty if the vma has no 2384dcf7fe9dSAndrea Arcangeli * VM_WRITE permission, so mark the page dirty or it 2385dcf7fe9dSAndrea Arcangeli * could be freed from under us. We could do it 2386dcf7fe9dSAndrea Arcangeli * unconditionally before unlock_page(), but doing it 2387dcf7fe9dSAndrea Arcangeli * only if VM_WRITE is not set is faster. 2388dcf7fe9dSAndrea Arcangeli */ 2389dcf7fe9dSAndrea Arcangeli set_page_dirty(page); 2390dcf7fe9dSAndrea Arcangeli } 23914c27fe4cSMike Rapoport 23924c27fe4cSMike Rapoport dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl); 2393e2a50c1fSAndrea Arcangeli 2394e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2395e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 2396e2a50c1fSAndrea Arcangeli if (unlikely(offset >= max_off)) 2397e2a50c1fSAndrea Arcangeli goto out_release_uncharge_unlock; 2398e2a50c1fSAndrea Arcangeli 2399e2a50c1fSAndrea Arcangeli ret = -EEXIST; 24004c27fe4cSMike Rapoport if (!pte_none(*dst_pte)) 24014c27fe4cSMike Rapoport goto out_release_uncharge_unlock; 24024c27fe4cSMike Rapoport 24034c27fe4cSMike Rapoport lru_cache_add_anon(page); 24044c27fe4cSMike Rapoport 24054c27fe4cSMike Rapoport spin_lock(&info->lock); 24064c27fe4cSMike Rapoport info->alloced++; 24074c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 24084c27fe4cSMike Rapoport shmem_recalc_inode(inode); 24094c27fe4cSMike Rapoport spin_unlock(&info->lock); 24104c27fe4cSMike Rapoport 24114c27fe4cSMike Rapoport inc_mm_counter(dst_mm, mm_counter_file(page)); 24124c27fe4cSMike Rapoport page_add_file_rmap(page, false); 24134c27fe4cSMike Rapoport set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte); 24144c27fe4cSMike Rapoport 24154c27fe4cSMike Rapoport /* No need to invalidate - it was non-present before */ 24164c27fe4cSMike Rapoport update_mmu_cache(dst_vma, dst_addr, dst_pte); 24174c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 2418e2a50c1fSAndrea Arcangeli unlock_page(page); 24194c27fe4cSMike Rapoport ret = 0; 24204c27fe4cSMike Rapoport out: 24214c27fe4cSMike Rapoport return ret; 24224c27fe4cSMike Rapoport out_release_uncharge_unlock: 24234c27fe4cSMike Rapoport pte_unmap_unlock(dst_pte, ptl); 2424dcf7fe9dSAndrea Arcangeli ClearPageDirty(page); 2425e2a50c1fSAndrea Arcangeli delete_from_page_cache(page); 24264c27fe4cSMike Rapoport out_release_uncharge: 24274c27fe4cSMike Rapoport mem_cgroup_cancel_charge(page, memcg, false); 24284c27fe4cSMike Rapoport out_release: 24299cc90c66SAndrea Arcangeli unlock_page(page); 24304c27fe4cSMike Rapoport put_page(page); 24314c27fe4cSMike Rapoport out_unacct_blocks: 24320f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 24334c27fe4cSMike Rapoport goto out; 24344c27fe4cSMike Rapoport } 24354c27fe4cSMike Rapoport 24368d103963SMike Rapoport int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm, 24378d103963SMike Rapoport pmd_t *dst_pmd, 24388d103963SMike Rapoport struct vm_area_struct *dst_vma, 24398d103963SMike Rapoport unsigned long dst_addr, 24408d103963SMike Rapoport unsigned long src_addr, 24418d103963SMike Rapoport struct page **pagep) 24428d103963SMike Rapoport { 24438d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 24448d103963SMike Rapoport dst_addr, src_addr, false, pagep); 24458d103963SMike Rapoport } 24468d103963SMike Rapoport 24478d103963SMike Rapoport int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm, 24488d103963SMike Rapoport pmd_t *dst_pmd, 24498d103963SMike Rapoport struct vm_area_struct *dst_vma, 24508d103963SMike Rapoport unsigned long dst_addr) 24518d103963SMike Rapoport { 24528d103963SMike Rapoport struct page *page = NULL; 24538d103963SMike Rapoport 24548d103963SMike Rapoport return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma, 24558d103963SMike Rapoport dst_addr, 0, true, &page); 24568d103963SMike Rapoport } 24578d103963SMike Rapoport 24581da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 245992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 246069f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 24611da177e4SLinus Torvalds 24626d9d88d0SJarkko Sakkinen #ifdef CONFIG_TMPFS_XATTR 24636d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 24646d9d88d0SJarkko Sakkinen #else 24656d9d88d0SJarkko Sakkinen #define shmem_initxattrs NULL 24666d9d88d0SJarkko Sakkinen #endif 24676d9d88d0SJarkko Sakkinen 24681da177e4SLinus Torvalds static int 2469800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 2470800d15a5SNick Piggin loff_t pos, unsigned len, unsigned flags, 2471800d15a5SNick Piggin struct page **pagep, void **fsdata) 24721da177e4SLinus Torvalds { 2473800d15a5SNick Piggin struct inode *inode = mapping->host; 247440e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 247509cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 247640e041a2SDavid Herrmann 247740e041a2SDavid Herrmann /* i_mutex is held by caller */ 2478ab3948f5SJoel Fernandes (Google) if (unlikely(info->seals & (F_SEAL_GROW | 2479ab3948f5SJoel Fernandes (Google) F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 2480ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 248140e041a2SDavid Herrmann return -EPERM; 248240e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 248340e041a2SDavid Herrmann return -EPERM; 248440e041a2SDavid Herrmann } 248540e041a2SDavid Herrmann 24869e18eb29SAndres Lagar-Cavilla return shmem_getpage(inode, index, pagep, SGP_WRITE); 2487800d15a5SNick Piggin } 2488800d15a5SNick Piggin 2489800d15a5SNick Piggin static int 2490800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2491800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2492800d15a5SNick Piggin struct page *page, void *fsdata) 2493800d15a5SNick Piggin { 2494800d15a5SNick Piggin struct inode *inode = mapping->host; 2495800d15a5SNick Piggin 2496800d15a5SNick Piggin if (pos + copied > inode->i_size) 2497800d15a5SNick Piggin i_size_write(inode, pos + copied); 2498800d15a5SNick Piggin 2499ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2500800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2501800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2502800d8c63SKirill A. Shutemov int i; 2503800d8c63SKirill A. Shutemov 2504800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2505800d8c63SKirill A. Shutemov if (head + i == page) 2506800d8c63SKirill A. Shutemov continue; 2507800d8c63SKirill A. Shutemov clear_highpage(head + i); 2508800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2509800d8c63SKirill A. Shutemov } 2510800d8c63SKirill A. Shutemov } 251109cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 251209cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2513ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 251409cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2515ec9516fbSHugh Dickins } 2516800d8c63SKirill A. Shutemov SetPageUptodate(head); 2517ec9516fbSHugh Dickins } 2518d3602444SHugh Dickins set_page_dirty(page); 25196746aff7SWu Fengguang unlock_page(page); 252009cbfeafSKirill A. Shutemov put_page(page); 2521d3602444SHugh Dickins 2522800d15a5SNick Piggin return copied; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25252ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 25261da177e4SLinus Torvalds { 25276e58e79dSAl Viro struct file *file = iocb->ki_filp; 25286e58e79dSAl Viro struct inode *inode = file_inode(file); 25291da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 253041ffe5d5SHugh Dickins pgoff_t index; 253141ffe5d5SHugh Dickins unsigned long offset; 2532a0ee5ec5SHugh Dickins enum sgp_type sgp = SGP_READ; 2533f7c1d074SGeert Uytterhoeven int error = 0; 2534cb66a7a1SAl Viro ssize_t retval = 0; 25356e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2536a0ee5ec5SHugh Dickins 2537a0ee5ec5SHugh Dickins /* 2538a0ee5ec5SHugh Dickins * Might this read be for a stacking filesystem? Then when reading 2539a0ee5ec5SHugh Dickins * holes of a sparse file, we actually need to allocate those pages, 2540a0ee5ec5SHugh Dickins * and even mark them dirty, so it cannot exceed the max_blocks limit. 2541a0ee5ec5SHugh Dickins */ 2542777eda2cSAl Viro if (!iter_is_iovec(to)) 254375edd345SHugh Dickins sgp = SGP_CACHE; 25441da177e4SLinus Torvalds 254509cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 254609cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 25471da177e4SLinus Torvalds 25481da177e4SLinus Torvalds for (;;) { 25491da177e4SLinus Torvalds struct page *page = NULL; 255041ffe5d5SHugh Dickins pgoff_t end_index; 255141ffe5d5SHugh Dickins unsigned long nr, ret; 25521da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 25531da177e4SLinus Torvalds 255409cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 25551da177e4SLinus Torvalds if (index > end_index) 25561da177e4SLinus Torvalds break; 25571da177e4SLinus Torvalds if (index == end_index) { 255809cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 25591da177e4SLinus Torvalds if (nr <= offset) 25601da177e4SLinus Torvalds break; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds 25639e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, sgp); 25646e58e79dSAl Viro if (error) { 25656e58e79dSAl Viro if (error == -EINVAL) 25666e58e79dSAl Viro error = 0; 25671da177e4SLinus Torvalds break; 25681da177e4SLinus Torvalds } 256975edd345SHugh Dickins if (page) { 257075edd345SHugh Dickins if (sgp == SGP_CACHE) 257175edd345SHugh Dickins set_page_dirty(page); 2572d3602444SHugh Dickins unlock_page(page); 257375edd345SHugh Dickins } 25741da177e4SLinus Torvalds 25751da177e4SLinus Torvalds /* 25761da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 25771b1dcc1bSJes Sorensen * are called without i_mutex protection against truncate 25781da177e4SLinus Torvalds */ 257909cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 25801da177e4SLinus Torvalds i_size = i_size_read(inode); 258109cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 25821da177e4SLinus Torvalds if (index == end_index) { 258309cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 25841da177e4SLinus Torvalds if (nr <= offset) { 25851da177e4SLinus Torvalds if (page) 258609cbfeafSKirill A. Shutemov put_page(page); 25871da177e4SLinus Torvalds break; 25881da177e4SLinus Torvalds } 25891da177e4SLinus Torvalds } 25901da177e4SLinus Torvalds nr -= offset; 25911da177e4SLinus Torvalds 25921da177e4SLinus Torvalds if (page) { 25931da177e4SLinus Torvalds /* 25941da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 25951da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 25961da177e4SLinus Torvalds * before reading the page on the kernel side. 25971da177e4SLinus Torvalds */ 25981da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 25991da177e4SLinus Torvalds flush_dcache_page(page); 26001da177e4SLinus Torvalds /* 26011da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 26021da177e4SLinus Torvalds */ 26031da177e4SLinus Torvalds if (!offset) 26041da177e4SLinus Torvalds mark_page_accessed(page); 2605b5810039SNick Piggin } else { 26061da177e4SLinus Torvalds page = ZERO_PAGE(0); 260709cbfeafSKirill A. Shutemov get_page(page); 2608b5810039SNick Piggin } 26091da177e4SLinus Torvalds 26101da177e4SLinus Torvalds /* 26111da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 26121da177e4SLinus Torvalds * now we can copy it to user space... 26131da177e4SLinus Torvalds */ 26142ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 26156e58e79dSAl Viro retval += ret; 26161da177e4SLinus Torvalds offset += ret; 261709cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 261809cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 26191da177e4SLinus Torvalds 262009cbfeafSKirill A. Shutemov put_page(page); 26212ba5bbedSAl Viro if (!iov_iter_count(to)) 26221da177e4SLinus Torvalds break; 26236e58e79dSAl Viro if (ret < nr) { 26246e58e79dSAl Viro error = -EFAULT; 26256e58e79dSAl Viro break; 26266e58e79dSAl Viro } 26271da177e4SLinus Torvalds cond_resched(); 26281da177e4SLinus Torvalds } 26291da177e4SLinus Torvalds 263009cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 26316e58e79dSAl Viro file_accessed(file); 26326e58e79dSAl Viro return retval ? retval : error; 26331da177e4SLinus Torvalds } 26341da177e4SLinus Torvalds 2635220f2ac9SHugh Dickins /* 26367f4446eeSMatthew Wilcox * llseek SEEK_DATA or SEEK_HOLE through the page cache. 2637220f2ac9SHugh Dickins */ 2638220f2ac9SHugh Dickins static pgoff_t shmem_seek_hole_data(struct address_space *mapping, 2639965c8e59SAndrew Morton pgoff_t index, pgoff_t end, int whence) 2640220f2ac9SHugh Dickins { 2641220f2ac9SHugh Dickins struct page *page; 2642220f2ac9SHugh Dickins struct pagevec pvec; 2643220f2ac9SHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 2644220f2ac9SHugh Dickins bool done = false; 2645220f2ac9SHugh Dickins int i; 2646220f2ac9SHugh Dickins 264786679820SMel Gorman pagevec_init(&pvec); 2648220f2ac9SHugh Dickins pvec.nr = 1; /* start small: we may be there already */ 2649220f2ac9SHugh Dickins while (!done) { 26500cd6144aSJohannes Weiner pvec.nr = find_get_entries(mapping, index, 2651220f2ac9SHugh Dickins pvec.nr, pvec.pages, indices); 2652220f2ac9SHugh Dickins if (!pvec.nr) { 2653965c8e59SAndrew Morton if (whence == SEEK_DATA) 2654220f2ac9SHugh Dickins index = end; 2655220f2ac9SHugh Dickins break; 2656220f2ac9SHugh Dickins } 2657220f2ac9SHugh Dickins for (i = 0; i < pvec.nr; i++, index++) { 2658220f2ac9SHugh Dickins if (index < indices[i]) { 2659965c8e59SAndrew Morton if (whence == SEEK_HOLE) { 2660220f2ac9SHugh Dickins done = true; 2661220f2ac9SHugh Dickins break; 2662220f2ac9SHugh Dickins } 2663220f2ac9SHugh Dickins index = indices[i]; 2664220f2ac9SHugh Dickins } 2665220f2ac9SHugh Dickins page = pvec.pages[i]; 26663159f943SMatthew Wilcox if (page && !xa_is_value(page)) { 2667220f2ac9SHugh Dickins if (!PageUptodate(page)) 2668220f2ac9SHugh Dickins page = NULL; 2669220f2ac9SHugh Dickins } 2670220f2ac9SHugh Dickins if (index >= end || 2671965c8e59SAndrew Morton (page && whence == SEEK_DATA) || 2672965c8e59SAndrew Morton (!page && whence == SEEK_HOLE)) { 2673220f2ac9SHugh Dickins done = true; 2674220f2ac9SHugh Dickins break; 2675220f2ac9SHugh Dickins } 2676220f2ac9SHugh Dickins } 26770cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 2678220f2ac9SHugh Dickins pagevec_release(&pvec); 2679220f2ac9SHugh Dickins pvec.nr = PAGEVEC_SIZE; 2680220f2ac9SHugh Dickins cond_resched(); 2681220f2ac9SHugh Dickins } 2682220f2ac9SHugh Dickins return index; 2683220f2ac9SHugh Dickins } 2684220f2ac9SHugh Dickins 2685965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2686220f2ac9SHugh Dickins { 2687220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2688220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2689220f2ac9SHugh Dickins pgoff_t start, end; 2690220f2ac9SHugh Dickins loff_t new_offset; 2691220f2ac9SHugh Dickins 2692965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2693965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2694220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 26955955102cSAl Viro inode_lock(inode); 2696220f2ac9SHugh Dickins /* We're holding i_mutex so we can access i_size directly */ 2697220f2ac9SHugh Dickins 26981a413646SYufen Yu if (offset < 0 || offset >= inode->i_size) 2699220f2ac9SHugh Dickins offset = -ENXIO; 2700220f2ac9SHugh Dickins else { 270109cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 270209cbfeafSKirill A. Shutemov end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; 2703965c8e59SAndrew Morton new_offset = shmem_seek_hole_data(mapping, start, end, whence); 270409cbfeafSKirill A. Shutemov new_offset <<= PAGE_SHIFT; 2705220f2ac9SHugh Dickins if (new_offset > offset) { 2706220f2ac9SHugh Dickins if (new_offset < inode->i_size) 2707220f2ac9SHugh Dickins offset = new_offset; 2708965c8e59SAndrew Morton else if (whence == SEEK_DATA) 2709220f2ac9SHugh Dickins offset = -ENXIO; 2710220f2ac9SHugh Dickins else 2711220f2ac9SHugh Dickins offset = inode->i_size; 2712220f2ac9SHugh Dickins } 2713220f2ac9SHugh Dickins } 2714220f2ac9SHugh Dickins 2715387aae6fSHugh Dickins if (offset >= 0) 271646a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 27175955102cSAl Viro inode_unlock(inode); 2718220f2ac9SHugh Dickins return offset; 2719220f2ac9SHugh Dickins } 2720220f2ac9SHugh Dickins 272183e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 272283e4fa9cSHugh Dickins loff_t len) 272383e4fa9cSHugh Dickins { 2724496ad9aaSAl Viro struct inode *inode = file_inode(file); 2725e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 272640e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 27271aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2728e2d12e22SHugh Dickins pgoff_t start, index, end; 2729e2d12e22SHugh Dickins int error; 273083e4fa9cSHugh Dickins 273113ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 273213ace4d0SHugh Dickins return -EOPNOTSUPP; 273313ace4d0SHugh Dickins 27345955102cSAl Viro inode_lock(inode); 273583e4fa9cSHugh Dickins 273683e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 273783e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 273883e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 273983e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 27408e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 274183e4fa9cSHugh Dickins 274240e041a2SDavid Herrmann /* protected by i_mutex */ 2743ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 274440e041a2SDavid Herrmann error = -EPERM; 274540e041a2SDavid Herrmann goto out; 274640e041a2SDavid Herrmann } 274740e041a2SDavid Herrmann 27488e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2749aa71ecd8SChen Jun shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 2750f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2751f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2752f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2753f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2754f00cdc6dSHugh Dickins 275583e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 275683e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 275783e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 275883e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 275983e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 27608e205f77SHugh Dickins 27618e205f77SHugh Dickins spin_lock(&inode->i_lock); 27628e205f77SHugh Dickins inode->i_private = NULL; 27638e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 27642055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 27658e205f77SHugh Dickins spin_unlock(&inode->i_lock); 276683e4fa9cSHugh Dickins error = 0; 27678e205f77SHugh Dickins goto out; 276883e4fa9cSHugh Dickins } 276983e4fa9cSHugh Dickins 2770e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2771e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2772e2d12e22SHugh Dickins if (error) 2773e2d12e22SHugh Dickins goto out; 2774e2d12e22SHugh Dickins 277540e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 277640e041a2SDavid Herrmann error = -EPERM; 277740e041a2SDavid Herrmann goto out; 277840e041a2SDavid Herrmann } 277940e041a2SDavid Herrmann 278009cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 278109cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2782e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2783e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2784e2d12e22SHugh Dickins error = -ENOSPC; 2785e2d12e22SHugh Dickins goto out; 2786e2d12e22SHugh Dickins } 2787e2d12e22SHugh Dickins 27888e205f77SHugh Dickins shmem_falloc.waitq = NULL; 27891aac1400SHugh Dickins shmem_falloc.start = start; 27901aac1400SHugh Dickins shmem_falloc.next = start; 27911aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 27921aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 27931aac1400SHugh Dickins spin_lock(&inode->i_lock); 27941aac1400SHugh Dickins inode->i_private = &shmem_falloc; 27951aac1400SHugh Dickins spin_unlock(&inode->i_lock); 27961aac1400SHugh Dickins 2797e2d12e22SHugh Dickins for (index = start; index < end; index++) { 2798e2d12e22SHugh Dickins struct page *page; 2799e2d12e22SHugh Dickins 2800e2d12e22SHugh Dickins /* 2801e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2802e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2803e2d12e22SHugh Dickins */ 2804e2d12e22SHugh Dickins if (signal_pending(current)) 2805e2d12e22SHugh Dickins error = -EINTR; 28061aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 28071aac1400SHugh Dickins error = -ENOMEM; 2808e2d12e22SHugh Dickins else 28099e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2810e2d12e22SHugh Dickins if (error) { 28111635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 28127f556567SHugh Dickins if (index > start) { 28131635f6a7SHugh Dickins shmem_undo_range(inode, 281409cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2815b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 28167f556567SHugh Dickins } 28171aac1400SHugh Dickins goto undone; 2818e2d12e22SHugh Dickins } 2819e2d12e22SHugh Dickins 2820e2d12e22SHugh Dickins /* 28211aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 28221aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 28231aac1400SHugh Dickins */ 28241aac1400SHugh Dickins shmem_falloc.next++; 28251aac1400SHugh Dickins if (!PageUptodate(page)) 28261aac1400SHugh Dickins shmem_falloc.nr_falloced++; 28271aac1400SHugh Dickins 28281aac1400SHugh Dickins /* 28291635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 28301635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 28311635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2832e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2833e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2834e2d12e22SHugh Dickins */ 2835e2d12e22SHugh Dickins set_page_dirty(page); 2836e2d12e22SHugh Dickins unlock_page(page); 283709cbfeafSKirill A. Shutemov put_page(page); 2838e2d12e22SHugh Dickins cond_resched(); 2839e2d12e22SHugh Dickins } 2840e2d12e22SHugh Dickins 2841e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2842e2d12e22SHugh Dickins i_size_write(inode, offset + len); 2843078cd827SDeepa Dinamani inode->i_ctime = current_time(inode); 28441aac1400SHugh Dickins undone: 28451aac1400SHugh Dickins spin_lock(&inode->i_lock); 28461aac1400SHugh Dickins inode->i_private = NULL; 28471aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2848e2d12e22SHugh Dickins out: 28495955102cSAl Viro inode_unlock(inode); 285083e4fa9cSHugh Dickins return error; 285183e4fa9cSHugh Dickins } 285283e4fa9cSHugh Dickins 2853726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 28541da177e4SLinus Torvalds { 2855726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 28561da177e4SLinus Torvalds 28571da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 285809cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 28591da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 28600edd73b3SHugh Dickins if (sbinfo->max_blocks) { 28611da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 286241ffe5d5SHugh Dickins buf->f_bavail = 286341ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 286441ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 28650edd73b3SHugh Dickins } 28660edd73b3SHugh Dickins if (sbinfo->max_inodes) { 28671da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 28681da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 28691da177e4SLinus Torvalds } 28701da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 28711da177e4SLinus Torvalds return 0; 28721da177e4SLinus Torvalds } 28731da177e4SLinus Torvalds 28741da177e4SLinus Torvalds /* 28751da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 28761da177e4SLinus Torvalds */ 28771da177e4SLinus Torvalds static int 28781a67aafbSAl Viro shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28791da177e4SLinus Torvalds { 28800b0a0806SHugh Dickins struct inode *inode; 28811da177e4SLinus Torvalds int error = -ENOSPC; 28821da177e4SLinus Torvalds 2883454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 28841da177e4SLinus Torvalds if (inode) { 2885feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2886feda821eSChristoph Hellwig if (error) 2887feda821eSChristoph Hellwig goto out_iput; 28882a7dba39SEric Paris error = security_inode_init_security(inode, dir, 28899d8f13baSMimi Zohar &dentry->d_name, 28906d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2891feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2892feda821eSChristoph Hellwig goto out_iput; 289337ec43cdSMimi Zohar 2894718deb6bSAl Viro error = 0; 28951da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2896078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 28971da177e4SLinus Torvalds d_instantiate(dentry, inode); 28981da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 28991da177e4SLinus Torvalds } 29001da177e4SLinus Torvalds return error; 2901feda821eSChristoph Hellwig out_iput: 2902feda821eSChristoph Hellwig iput(inode); 2903feda821eSChristoph Hellwig return error; 29041da177e4SLinus Torvalds } 29051da177e4SLinus Torvalds 290660545d0dSAl Viro static int 290760545d0dSAl Viro shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) 290860545d0dSAl Viro { 290960545d0dSAl Viro struct inode *inode; 291060545d0dSAl Viro int error = -ENOSPC; 291160545d0dSAl Viro 291260545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 291360545d0dSAl Viro if (inode) { 291460545d0dSAl Viro error = security_inode_init_security(inode, dir, 291560545d0dSAl Viro NULL, 291660545d0dSAl Viro shmem_initxattrs, NULL); 2917feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2918feda821eSChristoph Hellwig goto out_iput; 2919feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2920feda821eSChristoph Hellwig if (error) 2921feda821eSChristoph Hellwig goto out_iput; 292260545d0dSAl Viro d_tmpfile(dentry, inode); 292360545d0dSAl Viro } 292460545d0dSAl Viro return error; 2925feda821eSChristoph Hellwig out_iput: 2926feda821eSChristoph Hellwig iput(inode); 2927feda821eSChristoph Hellwig return error; 292860545d0dSAl Viro } 292960545d0dSAl Viro 293018bb1db3SAl Viro static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 29311da177e4SLinus Torvalds { 29321da177e4SLinus Torvalds int error; 29331da177e4SLinus Torvalds 29341da177e4SLinus Torvalds if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) 29351da177e4SLinus Torvalds return error; 2936d8c76e6fSDave Hansen inc_nlink(dir); 29371da177e4SLinus Torvalds return 0; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 29404acdaf27SAl Viro static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2941ebfc3b49SAl Viro bool excl) 29421da177e4SLinus Torvalds { 29431da177e4SLinus Torvalds return shmem_mknod(dir, dentry, mode | S_IFREG, 0); 29441da177e4SLinus Torvalds } 29451da177e4SLinus Torvalds 29461da177e4SLinus Torvalds /* 29471da177e4SLinus Torvalds * Link a file.. 29481da177e4SLinus Torvalds */ 29491da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 29501da177e4SLinus Torvalds { 295175c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 295229b00e60SDarrick J. Wong int ret = 0; 29531da177e4SLinus Torvalds 29541da177e4SLinus Torvalds /* 29551da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 29561da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 29571da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 29581062af92SDarrick J. Wong * But if an O_TMPFILE file is linked into the tmpfs, the 29591062af92SDarrick J. Wong * first link must skip that, to get the accounting right. 29601da177e4SLinus Torvalds */ 29611062af92SDarrick J. Wong if (inode->i_nlink) { 29625b04c689SPavel Emelyanov ret = shmem_reserve_inode(inode->i_sb); 29635b04c689SPavel Emelyanov if (ret) 29645b04c689SPavel Emelyanov goto out; 29651062af92SDarrick J. Wong } 29661da177e4SLinus Torvalds 29671da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2968078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 2969d8c76e6fSDave Hansen inc_nlink(inode); 29707de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 29711da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 29721da177e4SLinus Torvalds d_instantiate(dentry, inode); 29735b04c689SPavel Emelyanov out: 29745b04c689SPavel Emelyanov return ret; 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds 29771da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 29781da177e4SLinus Torvalds { 297975c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 29801da177e4SLinus Torvalds 29815b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 29825b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 2985078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 29869a53c3a7SDave Hansen drop_nlink(inode); 29871da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 29881da177e4SLinus Torvalds return 0; 29891da177e4SLinus Torvalds } 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 29921da177e4SLinus Torvalds { 29931da177e4SLinus Torvalds if (!simple_empty(dentry)) 29941da177e4SLinus Torvalds return -ENOTEMPTY; 29951da177e4SLinus Torvalds 299675c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 29979a53c3a7SDave Hansen drop_nlink(dir); 29981da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 29991da177e4SLinus Torvalds } 30001da177e4SLinus Torvalds 300137456771SMiklos Szeredi static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) 300237456771SMiklos Szeredi { 3003e36cb0b8SDavid Howells bool old_is_dir = d_is_dir(old_dentry); 3004e36cb0b8SDavid Howells bool new_is_dir = d_is_dir(new_dentry); 300537456771SMiklos Szeredi 300637456771SMiklos Szeredi if (old_dir != new_dir && old_is_dir != new_is_dir) { 300737456771SMiklos Szeredi if (old_is_dir) { 300837456771SMiklos Szeredi drop_nlink(old_dir); 300937456771SMiklos Szeredi inc_nlink(new_dir); 301037456771SMiklos Szeredi } else { 301137456771SMiklos Szeredi drop_nlink(new_dir); 301237456771SMiklos Szeredi inc_nlink(old_dir); 301337456771SMiklos Szeredi } 301437456771SMiklos Szeredi } 301537456771SMiklos Szeredi old_dir->i_ctime = old_dir->i_mtime = 301637456771SMiklos Szeredi new_dir->i_ctime = new_dir->i_mtime = 301775c3cfa8SDavid Howells d_inode(old_dentry)->i_ctime = 3018078cd827SDeepa Dinamani d_inode(new_dentry)->i_ctime = current_time(old_dir); 301937456771SMiklos Szeredi 302037456771SMiklos Szeredi return 0; 302137456771SMiklos Szeredi } 302237456771SMiklos Szeredi 302346fdb794SMiklos Szeredi static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) 302446fdb794SMiklos Szeredi { 302546fdb794SMiklos Szeredi struct dentry *whiteout; 302646fdb794SMiklos Szeredi int error; 302746fdb794SMiklos Szeredi 302846fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 302946fdb794SMiklos Szeredi if (!whiteout) 303046fdb794SMiklos Szeredi return -ENOMEM; 303146fdb794SMiklos Szeredi 303246fdb794SMiklos Szeredi error = shmem_mknod(old_dir, whiteout, 303346fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 303446fdb794SMiklos Szeredi dput(whiteout); 303546fdb794SMiklos Szeredi if (error) 303646fdb794SMiklos Szeredi return error; 303746fdb794SMiklos Szeredi 303846fdb794SMiklos Szeredi /* 303946fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 304046fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 304146fdb794SMiklos Szeredi * 304246fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 304346fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 304446fdb794SMiklos Szeredi */ 304546fdb794SMiklos Szeredi d_rehash(whiteout); 304646fdb794SMiklos Szeredi return 0; 304746fdb794SMiklos Szeredi } 304846fdb794SMiklos Szeredi 30491da177e4SLinus Torvalds /* 30501da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 30511da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 30521da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 30531da177e4SLinus Torvalds * gets overwritten. 30541da177e4SLinus Torvalds */ 30553b69ff51SMiklos Szeredi static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) 30561da177e4SLinus Torvalds { 305775c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30581da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 30591da177e4SLinus Torvalds 306046fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 30613b69ff51SMiklos Szeredi return -EINVAL; 30623b69ff51SMiklos Szeredi 306337456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 306437456771SMiklos Szeredi return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); 306537456771SMiklos Szeredi 30661da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 30671da177e4SLinus Torvalds return -ENOTEMPTY; 30681da177e4SLinus Torvalds 306946fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 307046fdb794SMiklos Szeredi int error; 307146fdb794SMiklos Szeredi 307246fdb794SMiklos Szeredi error = shmem_whiteout(old_dir, old_dentry); 307346fdb794SMiklos Szeredi if (error) 307446fdb794SMiklos Szeredi return error; 307546fdb794SMiklos Szeredi } 307646fdb794SMiklos Szeredi 307775c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 30781da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3079b928095bSMiklos Szeredi if (they_are_dirs) { 308075c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 30819a53c3a7SDave Hansen drop_nlink(old_dir); 3082b928095bSMiklos Szeredi } 30831da177e4SLinus Torvalds } else if (they_are_dirs) { 30849a53c3a7SDave Hansen drop_nlink(old_dir); 3085d8c76e6fSDave Hansen inc_nlink(new_dir); 30861da177e4SLinus Torvalds } 30871da177e4SLinus Torvalds 30881da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 30891da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 30901da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 30911da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3092078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 30931da177e4SLinus Torvalds return 0; 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) 30971da177e4SLinus Torvalds { 30981da177e4SLinus Torvalds int error; 30991da177e4SLinus Torvalds int len; 31001da177e4SLinus Torvalds struct inode *inode; 31019276aad6SHugh Dickins struct page *page; 31021da177e4SLinus Torvalds 31031da177e4SLinus Torvalds len = strlen(symname) + 1; 310409cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 31051da177e4SLinus Torvalds return -ENAMETOOLONG; 31061da177e4SLinus Torvalds 31070825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 31080825a6f9SJoe Perches VM_NORESERVE); 31091da177e4SLinus Torvalds if (!inode) 31101da177e4SLinus Torvalds return -ENOSPC; 31111da177e4SLinus Torvalds 31129d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 31136d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3114343c3d7fSMateusz Nosek if (error && error != -EOPNOTSUPP) { 3115570bc1c2SStephen Smalley iput(inode); 3116570bc1c2SStephen Smalley return error; 3117570bc1c2SStephen Smalley } 3118570bc1c2SStephen Smalley 31191da177e4SLinus Torvalds inode->i_size = len-1; 312069f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31213ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 31223ed47db3SAl Viro if (!inode->i_link) { 312369f07ec9SHugh Dickins iput(inode); 312469f07ec9SHugh Dickins return -ENOMEM; 312569f07ec9SHugh Dickins } 312669f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 31271da177e4SLinus Torvalds } else { 3128e8ecde25SAl Viro inode_nohighmem(inode); 31299e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 31301da177e4SLinus Torvalds if (error) { 31311da177e4SLinus Torvalds iput(inode); 31321da177e4SLinus Torvalds return error; 31331da177e4SLinus Torvalds } 313414fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 31351da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 313621fc61c7SAl Viro memcpy(page_address(page), symname, len); 3137ec9516fbSHugh Dickins SetPageUptodate(page); 31381da177e4SLinus Torvalds set_page_dirty(page); 31396746aff7SWu Fengguang unlock_page(page); 314009cbfeafSKirill A. Shutemov put_page(page); 31411da177e4SLinus Torvalds } 31421da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3143078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 31441da177e4SLinus Torvalds d_instantiate(dentry, inode); 31451da177e4SLinus Torvalds dget(dentry); 31461da177e4SLinus Torvalds return 0; 31471da177e4SLinus Torvalds } 31481da177e4SLinus Torvalds 3149fceef393SAl Viro static void shmem_put_link(void *arg) 3150fceef393SAl Viro { 3151fceef393SAl Viro mark_page_accessed(arg); 3152fceef393SAl Viro put_page(arg); 3153fceef393SAl Viro } 3154fceef393SAl Viro 31556b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3156fceef393SAl Viro struct inode *inode, 3157fceef393SAl Viro struct delayed_call *done) 31581da177e4SLinus Torvalds { 31591da177e4SLinus Torvalds struct page *page = NULL; 31606b255391SAl Viro int error; 31616a6c9904SAl Viro if (!dentry) { 31626a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 31636a6c9904SAl Viro if (!page) 31646b255391SAl Viro return ERR_PTR(-ECHILD); 31656a6c9904SAl Viro if (!PageUptodate(page)) { 31666a6c9904SAl Viro put_page(page); 31676a6c9904SAl Viro return ERR_PTR(-ECHILD); 31686a6c9904SAl Viro } 31696a6c9904SAl Viro } else { 31709e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3171680baacbSAl Viro if (error) 3172680baacbSAl Viro return ERR_PTR(error); 3173d3602444SHugh Dickins unlock_page(page); 31741da177e4SLinus Torvalds } 3175fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 317621fc61c7SAl Viro return page_address(page); 31771da177e4SLinus Torvalds } 31781da177e4SLinus Torvalds 3179b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3180b09e0fa4SEric Paris /* 3181b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3182b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3183b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3184b09e0fa4SEric Paris * filesystem level, though. 3185b09e0fa4SEric Paris */ 3186b09e0fa4SEric Paris 31876d9d88d0SJarkko Sakkinen /* 31886d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 31896d9d88d0SJarkko Sakkinen */ 31906d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 31916d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 31926d9d88d0SJarkko Sakkinen void *fs_info) 31936d9d88d0SJarkko Sakkinen { 31946d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 31956d9d88d0SJarkko Sakkinen const struct xattr *xattr; 319638f38657SAristeu Rozanski struct simple_xattr *new_xattr; 31976d9d88d0SJarkko Sakkinen size_t len; 31986d9d88d0SJarkko Sakkinen 31996d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 320038f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32016d9d88d0SJarkko Sakkinen if (!new_xattr) 32026d9d88d0SJarkko Sakkinen return -ENOMEM; 32036d9d88d0SJarkko Sakkinen 32046d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32056d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32066d9d88d0SJarkko Sakkinen GFP_KERNEL); 32076d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32086d9d88d0SJarkko Sakkinen kfree(new_xattr); 32096d9d88d0SJarkko Sakkinen return -ENOMEM; 32106d9d88d0SJarkko Sakkinen } 32116d9d88d0SJarkko Sakkinen 32126d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32136d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32146d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32156d9d88d0SJarkko Sakkinen xattr->name, len); 32166d9d88d0SJarkko Sakkinen 321738f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 32186d9d88d0SJarkko Sakkinen } 32196d9d88d0SJarkko Sakkinen 32206d9d88d0SJarkko Sakkinen return 0; 32216d9d88d0SJarkko Sakkinen } 32226d9d88d0SJarkko Sakkinen 3223aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3224b296821aSAl Viro struct dentry *unused, struct inode *inode, 3225b296821aSAl Viro const char *name, void *buffer, size_t size) 3226aa7c5241SAndreas Gruenbacher { 3227b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3228aa7c5241SAndreas Gruenbacher 3229aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3230aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3231aa7c5241SAndreas Gruenbacher } 3232aa7c5241SAndreas Gruenbacher 3233aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 323459301226SAl Viro struct dentry *unused, struct inode *inode, 323559301226SAl Viro const char *name, const void *value, 323659301226SAl Viro size_t size, int flags) 3237aa7c5241SAndreas Gruenbacher { 323859301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3239aa7c5241SAndreas Gruenbacher 3240aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3241a46a2295SDaniel Xu return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL); 3242aa7c5241SAndreas Gruenbacher } 3243aa7c5241SAndreas Gruenbacher 3244aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3245aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3246aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3247aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3248aa7c5241SAndreas Gruenbacher }; 3249aa7c5241SAndreas Gruenbacher 3250aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3251aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3252aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3253aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3254aa7c5241SAndreas Gruenbacher }; 3255aa7c5241SAndreas Gruenbacher 3256b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3257b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3258feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3259feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3260b09e0fa4SEric Paris #endif 3261aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3262aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3263b09e0fa4SEric Paris NULL 3264b09e0fa4SEric Paris }; 3265b09e0fa4SEric Paris 3266b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3267b09e0fa4SEric Paris { 326875c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3269786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3270b09e0fa4SEric Paris } 3271b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3272b09e0fa4SEric Paris 327369f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 32746b255391SAl Viro .get_link = simple_get_link, 3275b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3276b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3277b09e0fa4SEric Paris #endif 32781da177e4SLinus Torvalds }; 32791da177e4SLinus Torvalds 328092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 32816b255391SAl Viro .get_link = shmem_get_link, 3282b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3283b09e0fa4SEric Paris .listxattr = shmem_listxattr, 328439f0247dSAndreas Gruenbacher #endif 3285b09e0fa4SEric Paris }; 328639f0247dSAndreas Gruenbacher 328791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 328891828a40SDavid M. Grimes { 328991828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 329091828a40SDavid M. Grimes } 329191828a40SDavid M. Grimes 329291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 329391828a40SDavid M. Grimes { 329491828a40SDavid M. Grimes __u32 *fh = vfh; 329591828a40SDavid M. Grimes __u64 inum = fh[2]; 329691828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 329791828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 329891828a40SDavid M. Grimes } 329991828a40SDavid M. Grimes 330012ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 330112ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 330212ba780dSAmir Goldstein { 330312ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 330412ba780dSAmir Goldstein 330512ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 330612ba780dSAmir Goldstein } 330712ba780dSAmir Goldstein 330812ba780dSAmir Goldstein 3309480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3310480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 331191828a40SDavid M. Grimes { 331291828a40SDavid M. Grimes struct inode *inode; 3313480b116cSChristoph Hellwig struct dentry *dentry = NULL; 331435c2a7f4SHugh Dickins u64 inum; 331591828a40SDavid M. Grimes 3316480b116cSChristoph Hellwig if (fh_len < 3) 3317480b116cSChristoph Hellwig return NULL; 3318480b116cSChristoph Hellwig 331935c2a7f4SHugh Dickins inum = fid->raw[2]; 332035c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 332135c2a7f4SHugh Dickins 3322480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3323480b116cSChristoph Hellwig shmem_match, fid->raw); 332491828a40SDavid M. Grimes if (inode) { 332512ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 332691828a40SDavid M. Grimes iput(inode); 332791828a40SDavid M. Grimes } 332891828a40SDavid M. Grimes 3329480b116cSChristoph Hellwig return dentry; 333091828a40SDavid M. Grimes } 333191828a40SDavid M. Grimes 3332b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3333b0b0382bSAl Viro struct inode *parent) 333491828a40SDavid M. Grimes { 33355fe0c237SAneesh Kumar K.V if (*len < 3) { 33365fe0c237SAneesh Kumar K.V *len = 3; 333794e07a75SNamjae Jeon return FILEID_INVALID; 33385fe0c237SAneesh Kumar K.V } 333991828a40SDavid M. Grimes 33401d3382cbSAl Viro if (inode_unhashed(inode)) { 334191828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 334291828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 334391828a40SDavid M. Grimes * time, we need a lock to ensure we only try 334491828a40SDavid M. Grimes * to do it once 334591828a40SDavid M. Grimes */ 334691828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 334791828a40SDavid M. Grimes spin_lock(&lock); 33481d3382cbSAl Viro if (inode_unhashed(inode)) 334991828a40SDavid M. Grimes __insert_inode_hash(inode, 335091828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 335191828a40SDavid M. Grimes spin_unlock(&lock); 335291828a40SDavid M. Grimes } 335391828a40SDavid M. Grimes 335491828a40SDavid M. Grimes fh[0] = inode->i_generation; 335591828a40SDavid M. Grimes fh[1] = inode->i_ino; 335691828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 335791828a40SDavid M. Grimes 335891828a40SDavid M. Grimes *len = 3; 335991828a40SDavid M. Grimes return 1; 336091828a40SDavid M. Grimes } 336191828a40SDavid M. Grimes 336239655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 336391828a40SDavid M. Grimes .get_parent = shmem_get_parent, 336491828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3365480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 336691828a40SDavid M. Grimes }; 336791828a40SDavid M. Grimes 3368626c3920SAl Viro enum shmem_param { 3369626c3920SAl Viro Opt_gid, 3370626c3920SAl Viro Opt_huge, 3371626c3920SAl Viro Opt_mode, 3372626c3920SAl Viro Opt_mpol, 3373626c3920SAl Viro Opt_nr_blocks, 3374626c3920SAl Viro Opt_nr_inodes, 3375626c3920SAl Viro Opt_size, 3376626c3920SAl Viro Opt_uid, 3377626c3920SAl Viro }; 33781da177e4SLinus Torvalds 33795eede625SAl Viro static const struct constant_table shmem_param_enums_huge[] = { 33802710c957SAl Viro {"never", SHMEM_HUGE_NEVER }, 33812710c957SAl Viro {"always", SHMEM_HUGE_ALWAYS }, 33822710c957SAl Viro {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 33832710c957SAl Viro {"advise", SHMEM_HUGE_ADVISE }, 33842710c957SAl Viro {} 33852710c957SAl Viro }; 33862710c957SAl Viro 3387d7167b14SAl Viro const struct fs_parameter_spec shmem_fs_parameters[] = { 3388626c3920SAl Viro fsparam_u32 ("gid", Opt_gid), 33892710c957SAl Viro fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 3390626c3920SAl Viro fsparam_u32oct("mode", Opt_mode), 3391626c3920SAl Viro fsparam_string("mpol", Opt_mpol), 3392626c3920SAl Viro fsparam_string("nr_blocks", Opt_nr_blocks), 3393626c3920SAl Viro fsparam_string("nr_inodes", Opt_nr_inodes), 3394626c3920SAl Viro fsparam_string("size", Opt_size), 3395626c3920SAl Viro fsparam_u32 ("uid", Opt_uid), 3396626c3920SAl Viro {} 3397626c3920SAl Viro }; 3398626c3920SAl Viro 3399f3235626SDavid Howells static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 3400626c3920SAl Viro { 3401f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3402626c3920SAl Viro struct fs_parse_result result; 3403e04dc423SAl Viro unsigned long long size; 3404626c3920SAl Viro char *rest; 3405626c3920SAl Viro int opt; 3406626c3920SAl Viro 3407d7167b14SAl Viro opt = fs_parse(fc, shmem_fs_parameters, param, &result); 3408f3235626SDavid Howells if (opt < 0) 3409626c3920SAl Viro return opt; 3410626c3920SAl Viro 3411626c3920SAl Viro switch (opt) { 3412626c3920SAl Viro case Opt_size: 3413626c3920SAl Viro size = memparse(param->string, &rest); 3414e04dc423SAl Viro if (*rest == '%') { 3415e04dc423SAl Viro size <<= PAGE_SHIFT; 3416e04dc423SAl Viro size *= totalram_pages(); 3417e04dc423SAl Viro do_div(size, 100); 3418e04dc423SAl Viro rest++; 3419e04dc423SAl Viro } 3420e04dc423SAl Viro if (*rest) 3421626c3920SAl Viro goto bad_value; 3422e04dc423SAl Viro ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 3423e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3424626c3920SAl Viro break; 3425626c3920SAl Viro case Opt_nr_blocks: 3426626c3920SAl Viro ctx->blocks = memparse(param->string, &rest); 3427e04dc423SAl Viro if (*rest) 3428626c3920SAl Viro goto bad_value; 3429e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3430626c3920SAl Viro break; 3431626c3920SAl Viro case Opt_nr_inodes: 3432626c3920SAl Viro ctx->inodes = memparse(param->string, &rest); 3433e04dc423SAl Viro if (*rest) 3434626c3920SAl Viro goto bad_value; 3435e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_INODES; 3436626c3920SAl Viro break; 3437626c3920SAl Viro case Opt_mode: 3438626c3920SAl Viro ctx->mode = result.uint_32 & 07777; 3439626c3920SAl Viro break; 3440626c3920SAl Viro case Opt_uid: 3441626c3920SAl Viro ctx->uid = make_kuid(current_user_ns(), result.uint_32); 3442e04dc423SAl Viro if (!uid_valid(ctx->uid)) 3443626c3920SAl Viro goto bad_value; 3444626c3920SAl Viro break; 3445626c3920SAl Viro case Opt_gid: 3446626c3920SAl Viro ctx->gid = make_kgid(current_user_ns(), result.uint_32); 3447e04dc423SAl Viro if (!gid_valid(ctx->gid)) 3448626c3920SAl Viro goto bad_value; 3449626c3920SAl Viro break; 3450626c3920SAl Viro case Opt_huge: 3451626c3920SAl Viro ctx->huge = result.uint_32; 3452626c3920SAl Viro if (ctx->huge != SHMEM_HUGE_NEVER && 3453396bcc52SMatthew Wilcox (Oracle) !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 3454626c3920SAl Viro has_transparent_hugepage())) 3455626c3920SAl Viro goto unsupported_parameter; 3456e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_HUGE; 3457626c3920SAl Viro break; 3458626c3920SAl Viro case Opt_mpol: 3459626c3920SAl Viro if (IS_ENABLED(CONFIG_NUMA)) { 3460e04dc423SAl Viro mpol_put(ctx->mpol); 3461e04dc423SAl Viro ctx->mpol = NULL; 3462626c3920SAl Viro if (mpol_parse_str(param->string, &ctx->mpol)) 3463626c3920SAl Viro goto bad_value; 3464626c3920SAl Viro break; 3465626c3920SAl Viro } 3466626c3920SAl Viro goto unsupported_parameter; 3467e04dc423SAl Viro } 3468e04dc423SAl Viro return 0; 3469e04dc423SAl Viro 3470626c3920SAl Viro unsupported_parameter: 3471f35aa2bcSAl Viro return invalfc(fc, "Unsupported parameter '%s'", param->key); 3472626c3920SAl Viro bad_value: 3473f35aa2bcSAl Viro return invalfc(fc, "Bad value for '%s'", param->key); 3474e04dc423SAl Viro } 3475e04dc423SAl Viro 3476f3235626SDavid Howells static int shmem_parse_options(struct fs_context *fc, void *data) 3477e04dc423SAl Viro { 3478f3235626SDavid Howells char *options = data; 3479f3235626SDavid Howells 348033f37c64SAl Viro if (options) { 348133f37c64SAl Viro int err = security_sb_eat_lsm_opts(options, &fc->security); 348233f37c64SAl Viro if (err) 348333f37c64SAl Viro return err; 348433f37c64SAl Viro } 348533f37c64SAl Viro 3486b00dc3adSHugh Dickins while (options != NULL) { 3487626c3920SAl Viro char *this_char = options; 3488b00dc3adSHugh Dickins for (;;) { 3489b00dc3adSHugh Dickins /* 3490b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3491b00dc3adSHugh Dickins * mount options form a comma-separated list, 3492b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3493b00dc3adSHugh Dickins */ 3494b00dc3adSHugh Dickins options = strchr(options, ','); 3495b00dc3adSHugh Dickins if (options == NULL) 3496b00dc3adSHugh Dickins break; 3497b00dc3adSHugh Dickins options++; 3498b00dc3adSHugh Dickins if (!isdigit(*options)) { 3499b00dc3adSHugh Dickins options[-1] = '\0'; 3500b00dc3adSHugh Dickins break; 3501b00dc3adSHugh Dickins } 3502b00dc3adSHugh Dickins } 3503626c3920SAl Viro if (*this_char) { 3504626c3920SAl Viro char *value = strchr(this_char,'='); 3505f3235626SDavid Howells size_t len = 0; 3506626c3920SAl Viro int err; 3507626c3920SAl Viro 3508626c3920SAl Viro if (value) { 3509626c3920SAl Viro *value++ = '\0'; 3510f3235626SDavid Howells len = strlen(value); 35111da177e4SLinus Torvalds } 3512f3235626SDavid Howells err = vfs_parse_fs_string(fc, this_char, value, len); 3513f3235626SDavid Howells if (err < 0) 3514f3235626SDavid Howells return err; 35151da177e4SLinus Torvalds } 3516626c3920SAl Viro } 35171da177e4SLinus Torvalds return 0; 35181da177e4SLinus Torvalds } 35191da177e4SLinus Torvalds 3520f3235626SDavid Howells /* 3521f3235626SDavid Howells * Reconfigure a shmem filesystem. 3522f3235626SDavid Howells * 3523f3235626SDavid Howells * Note that we disallow change from limited->unlimited blocks/inodes while any 3524f3235626SDavid Howells * are in use; but we must separately disallow unlimited->limited, because in 3525f3235626SDavid Howells * that case we have no record of how much is already in use. 3526f3235626SDavid Howells */ 3527f3235626SDavid Howells static int shmem_reconfigure(struct fs_context *fc) 35281da177e4SLinus Torvalds { 3529f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3530f3235626SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 35310edd73b3SHugh Dickins unsigned long inodes; 3532f3235626SDavid Howells const char *err; 35330edd73b3SHugh Dickins 35340edd73b3SHugh Dickins spin_lock(&sbinfo->stat_lock); 35350edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 3536f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 3537f3235626SDavid Howells if (!sbinfo->max_blocks) { 3538f3235626SDavid Howells err = "Cannot retroactively limit size"; 35390edd73b3SHugh Dickins goto out; 35400b5071ddSAl Viro } 3541f3235626SDavid Howells if (percpu_counter_compare(&sbinfo->used_blocks, 3542f3235626SDavid Howells ctx->blocks) > 0) { 3543f3235626SDavid Howells err = "Too small a size for current use"; 35440b5071ddSAl Viro goto out; 3545f3235626SDavid Howells } 3546f3235626SDavid Howells } 3547f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 3548f3235626SDavid Howells if (!sbinfo->max_inodes) { 3549f3235626SDavid Howells err = "Cannot retroactively limit inodes"; 35500b5071ddSAl Viro goto out; 35510b5071ddSAl Viro } 3552f3235626SDavid Howells if (ctx->inodes < inodes) { 3553f3235626SDavid Howells err = "Too few inodes for current use"; 3554f3235626SDavid Howells goto out; 3555f3235626SDavid Howells } 3556f3235626SDavid Howells } 35570edd73b3SHugh Dickins 3558f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_HUGE) 3559f3235626SDavid Howells sbinfo->huge = ctx->huge; 3560f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_BLOCKS) 3561f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3562f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_INODES) { 3563f3235626SDavid Howells sbinfo->max_inodes = ctx->inodes; 3564f3235626SDavid Howells sbinfo->free_inodes = ctx->inodes - inodes; 35650b5071ddSAl Viro } 356671fe804bSLee Schermerhorn 35675f00110fSGreg Thelen /* 35685f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 35695f00110fSGreg Thelen */ 3570f3235626SDavid Howells if (ctx->mpol) { 357171fe804bSLee Schermerhorn mpol_put(sbinfo->mpol); 3572f3235626SDavid Howells sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 3573f3235626SDavid Howells ctx->mpol = NULL; 35745f00110fSGreg Thelen } 3575f3235626SDavid Howells spin_unlock(&sbinfo->stat_lock); 3576f3235626SDavid Howells return 0; 35770edd73b3SHugh Dickins out: 35780edd73b3SHugh Dickins spin_unlock(&sbinfo->stat_lock); 3579f35aa2bcSAl Viro return invalfc(fc, "%s", err); 35801da177e4SLinus Torvalds } 3581680d794bSakpm@linux-foundation.org 358234c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3583680d794bSakpm@linux-foundation.org { 358434c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3585680d794bSakpm@linux-foundation.org 3586680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3587680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 358809cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3589680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3590680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 35910825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 359209208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 35938751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 35948751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 35958751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 35968751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 35978751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 35988751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3599396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 36005a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 36015a6e75f8SKirill A. Shutemov if (sbinfo->huge) 36025a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 36035a6e75f8SKirill A. Shutemov #endif 360471fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3605680d794bSakpm@linux-foundation.org return 0; 3606680d794bSakpm@linux-foundation.org } 36079183df25SDavid Herrmann 3608680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 36111da177e4SLinus Torvalds { 3612602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3613602586a8SHugh Dickins 3614602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 361549cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3616602586a8SHugh Dickins kfree(sbinfo); 36171da177e4SLinus Torvalds sb->s_fs_info = NULL; 36181da177e4SLinus Torvalds } 36191da177e4SLinus Torvalds 3620f3235626SDavid Howells static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 36211da177e4SLinus Torvalds { 3622f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 36231da177e4SLinus Torvalds struct inode *inode; 36240edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3625680d794bSakpm@linux-foundation.org int err = -ENOMEM; 3626680d794bSakpm@linux-foundation.org 3627680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3628425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3629680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3630680d794bSakpm@linux-foundation.org if (!sbinfo) 3631680d794bSakpm@linux-foundation.org return -ENOMEM; 3632680d794bSakpm@linux-foundation.org 3633680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 36341da177e4SLinus Torvalds 36350edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 36361da177e4SLinus Torvalds /* 36371da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 36381da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 36391da177e4SLinus Torvalds * but the internal instance is left unlimited. 36401da177e4SLinus Torvalds */ 36411751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3642f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 3643f3235626SDavid Howells ctx->blocks = shmem_default_max_blocks(); 3644f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_INODES)) 3645f3235626SDavid Howells ctx->inodes = shmem_default_max_inodes(); 3646ca4e0519SAl Viro } else { 36471751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 36481da177e4SLinus Torvalds } 364991828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 36501751e8a6SLinus Torvalds sb->s_flags |= SB_NOSEC; 36510edd73b3SHugh Dickins #else 36521751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 36530edd73b3SHugh Dickins #endif 3654f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3655f3235626SDavid Howells sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; 3656f3235626SDavid Howells sbinfo->uid = ctx->uid; 3657f3235626SDavid Howells sbinfo->gid = ctx->gid; 3658f3235626SDavid Howells sbinfo->mode = ctx->mode; 3659f3235626SDavid Howells sbinfo->huge = ctx->huge; 3660f3235626SDavid Howells sbinfo->mpol = ctx->mpol; 3661f3235626SDavid Howells ctx->mpol = NULL; 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds spin_lock_init(&sbinfo->stat_lock); 3664908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3665602586a8SHugh Dickins goto failed; 3666779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3667779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 36681da177e4SLinus Torvalds 3669285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 367009cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 367109cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 36721da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 36731da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3674cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3675b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 367639f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3677b09e0fa4SEric Paris #endif 3678b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 36791751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 368039f0247dSAndreas Gruenbacher #endif 36812b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 36820edd73b3SHugh Dickins 3683454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 36841da177e4SLinus Torvalds if (!inode) 36851da177e4SLinus Torvalds goto failed; 3686680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3687680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3688318ceed0SAl Viro sb->s_root = d_make_root(inode); 3689318ceed0SAl Viro if (!sb->s_root) 369048fde701SAl Viro goto failed; 36911da177e4SLinus Torvalds return 0; 36921da177e4SLinus Torvalds 36931da177e4SLinus Torvalds failed: 36941da177e4SLinus Torvalds shmem_put_super(sb); 36951da177e4SLinus Torvalds return err; 36961da177e4SLinus Torvalds } 36971da177e4SLinus Torvalds 3698f3235626SDavid Howells static int shmem_get_tree(struct fs_context *fc) 3699f3235626SDavid Howells { 3700f3235626SDavid Howells return get_tree_nodev(fc, shmem_fill_super); 3701f3235626SDavid Howells } 3702f3235626SDavid Howells 3703f3235626SDavid Howells static void shmem_free_fc(struct fs_context *fc) 3704f3235626SDavid Howells { 3705f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3706f3235626SDavid Howells 3707f3235626SDavid Howells if (ctx) { 3708f3235626SDavid Howells mpol_put(ctx->mpol); 3709f3235626SDavid Howells kfree(ctx); 3710f3235626SDavid Howells } 3711f3235626SDavid Howells } 3712f3235626SDavid Howells 3713f3235626SDavid Howells static const struct fs_context_operations shmem_fs_context_ops = { 3714f3235626SDavid Howells .free = shmem_free_fc, 3715f3235626SDavid Howells .get_tree = shmem_get_tree, 3716f3235626SDavid Howells #ifdef CONFIG_TMPFS 3717f3235626SDavid Howells .parse_monolithic = shmem_parse_options, 3718f3235626SDavid Howells .parse_param = shmem_parse_one, 3719f3235626SDavid Howells .reconfigure = shmem_reconfigure, 3720f3235626SDavid Howells #endif 3721f3235626SDavid Howells }; 3722f3235626SDavid Howells 3723fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 37241da177e4SLinus Torvalds 37251da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 37261da177e4SLinus Torvalds { 372741ffe5d5SHugh Dickins struct shmem_inode_info *info; 372841ffe5d5SHugh Dickins info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); 372941ffe5d5SHugh Dickins if (!info) 37301da177e4SLinus Torvalds return NULL; 373141ffe5d5SHugh Dickins return &info->vfs_inode; 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 373474b1da56SAl Viro static void shmem_free_in_core_inode(struct inode *inode) 3735fa0d7e3dSNick Piggin { 373684e710daSAl Viro if (S_ISLNK(inode->i_mode)) 37373ed47db3SAl Viro kfree(inode->i_link); 3738fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3739fa0d7e3dSNick Piggin } 3740fa0d7e3dSNick Piggin 37411da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 37421da177e4SLinus Torvalds { 374309208d15SAl Viro if (S_ISREG(inode->i_mode)) 37441da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 374741ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 37481da177e4SLinus Torvalds { 374941ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 375041ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 37511da177e4SLinus Torvalds } 37521da177e4SLinus Torvalds 37539a8ec03eSweiping zhang static void shmem_init_inodecache(void) 37541da177e4SLinus Torvalds { 37551da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 37561da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 37575d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 37581da177e4SLinus Torvalds } 37591da177e4SLinus Torvalds 376041ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 37611da177e4SLinus Torvalds { 37621a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 37631da177e4SLinus Torvalds } 37641da177e4SLinus Torvalds 3765f5e54d6eSChristoph Hellwig static const struct address_space_operations shmem_aops = { 37661da177e4SLinus Torvalds .writepage = shmem_writepage, 376776719325SKen Chen .set_page_dirty = __set_page_dirty_no_writeback, 37681da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3769800d15a5SNick Piggin .write_begin = shmem_write_begin, 3770800d15a5SNick Piggin .write_end = shmem_write_end, 37711da177e4SLinus Torvalds #endif 37721c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 3773304dbdb7SLee Schermerhorn .migratepage = migrate_page, 37741c93923cSAndrew Morton #endif 3775aa261f54SAndi Kleen .error_remove_page = generic_error_remove_page, 37761da177e4SLinus Torvalds }; 37771da177e4SLinus Torvalds 377815ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 37791da177e4SLinus Torvalds .mmap = shmem_mmap, 3780c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 37811da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3782220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 37832ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 37848174202bSAl Viro .write_iter = generic_file_write_iter, 37851b061d92SChristoph Hellwig .fsync = noop_fsync, 378682c156f8SAl Viro .splice_read = generic_file_splice_read, 3787f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 378883e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 37891da177e4SLinus Torvalds #endif 37901da177e4SLinus Torvalds }; 37911da177e4SLinus Torvalds 379292e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 379344a30220SYu Zhao .getattr = shmem_getattr, 379494c1e62dSHugh Dickins .setattr = shmem_setattr, 3795b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3796b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3797feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3798b09e0fa4SEric Paris #endif 37991da177e4SLinus Torvalds }; 38001da177e4SLinus Torvalds 380192e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 38021da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38031da177e4SLinus Torvalds .create = shmem_create, 38041da177e4SLinus Torvalds .lookup = simple_lookup, 38051da177e4SLinus Torvalds .link = shmem_link, 38061da177e4SLinus Torvalds .unlink = shmem_unlink, 38071da177e4SLinus Torvalds .symlink = shmem_symlink, 38081da177e4SLinus Torvalds .mkdir = shmem_mkdir, 38091da177e4SLinus Torvalds .rmdir = shmem_rmdir, 38101da177e4SLinus Torvalds .mknod = shmem_mknod, 38112773bf00SMiklos Szeredi .rename = shmem_rename2, 381260545d0dSAl Viro .tmpfile = shmem_tmpfile, 38131da177e4SLinus Torvalds #endif 3814b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3815b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3816b09e0fa4SEric Paris #endif 381739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 381894c1e62dSHugh Dickins .setattr = shmem_setattr, 3819feda821eSChristoph Hellwig .set_acl = simple_set_acl, 382039f0247dSAndreas Gruenbacher #endif 382139f0247dSAndreas Gruenbacher }; 382239f0247dSAndreas Gruenbacher 382392e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3824b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3825b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3826b09e0fa4SEric Paris #endif 382739f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 382894c1e62dSHugh Dickins .setattr = shmem_setattr, 3829feda821eSChristoph Hellwig .set_acl = simple_set_acl, 383039f0247dSAndreas Gruenbacher #endif 38311da177e4SLinus Torvalds }; 38321da177e4SLinus Torvalds 3833759b9775SHugh Dickins static const struct super_operations shmem_ops = { 38341da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 383574b1da56SAl Viro .free_inode = shmem_free_in_core_inode, 38361da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 38371da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 38381da177e4SLinus Torvalds .statfs = shmem_statfs, 3839680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 38401da177e4SLinus Torvalds #endif 38411f895f75SAl Viro .evict_inode = shmem_evict_inode, 38421da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 38431da177e4SLinus Torvalds .put_super = shmem_put_super, 3844396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3845779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3846779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3847779750d2SKirill A. Shutemov #endif 38481da177e4SLinus Torvalds }; 38491da177e4SLinus Torvalds 3850f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 385154cb8821SNick Piggin .fault = shmem_fault, 3852d7c17551SNing Qu .map_pages = filemap_map_pages, 38531da177e4SLinus Torvalds #ifdef CONFIG_NUMA 38541da177e4SLinus Torvalds .set_policy = shmem_set_policy, 38551da177e4SLinus Torvalds .get_policy = shmem_get_policy, 38561da177e4SLinus Torvalds #endif 38571da177e4SLinus Torvalds }; 38581da177e4SLinus Torvalds 3859f3235626SDavid Howells int shmem_init_fs_context(struct fs_context *fc) 38601da177e4SLinus Torvalds { 3861f3235626SDavid Howells struct shmem_options *ctx; 3862f3235626SDavid Howells 3863f3235626SDavid Howells ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 3864f3235626SDavid Howells if (!ctx) 3865f3235626SDavid Howells return -ENOMEM; 3866f3235626SDavid Howells 3867f3235626SDavid Howells ctx->mode = 0777 | S_ISVTX; 3868f3235626SDavid Howells ctx->uid = current_fsuid(); 3869f3235626SDavid Howells ctx->gid = current_fsgid(); 3870f3235626SDavid Howells 3871f3235626SDavid Howells fc->fs_private = ctx; 3872f3235626SDavid Howells fc->ops = &shmem_fs_context_ops; 3873f3235626SDavid Howells return 0; 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 387641ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 38771da177e4SLinus Torvalds .owner = THIS_MODULE, 38781da177e4SLinus Torvalds .name = "tmpfs", 3879f3235626SDavid Howells .init_fs_context = shmem_init_fs_context, 3880f3235626SDavid Howells #ifdef CONFIG_TMPFS 3881d7167b14SAl Viro .parameters = shmem_fs_parameters, 3882f3235626SDavid Howells #endif 38831da177e4SLinus Torvalds .kill_sb = kill_litter_super, 38842b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 38851da177e4SLinus Torvalds }; 38861da177e4SLinus Torvalds 388741ffe5d5SHugh Dickins int __init shmem_init(void) 38881da177e4SLinus Torvalds { 38891da177e4SLinus Torvalds int error; 38901da177e4SLinus Torvalds 38919a8ec03eSweiping zhang shmem_init_inodecache(); 38921da177e4SLinus Torvalds 389341ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 38941da177e4SLinus Torvalds if (error) { 38951170532bSJoe Perches pr_err("Could not register tmpfs\n"); 38961da177e4SLinus Torvalds goto out2; 38971da177e4SLinus Torvalds } 389895dc112aSGreg Kroah-Hartman 3899ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 39001da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 39011da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 39021170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 39031da177e4SLinus Torvalds goto out1; 39041da177e4SLinus Torvalds } 39055a6e75f8SKirill A. Shutemov 3906396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3907435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 39085a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 39095a6e75f8SKirill A. Shutemov else 39105a6e75f8SKirill A. Shutemov shmem_huge = 0; /* just in case it was patched */ 39115a6e75f8SKirill A. Shutemov #endif 39121da177e4SLinus Torvalds return 0; 39131da177e4SLinus Torvalds 39141da177e4SLinus Torvalds out1: 391541ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 39161da177e4SLinus Torvalds out2: 391741ffe5d5SHugh Dickins shmem_destroy_inodecache(); 39181da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 39191da177e4SLinus Torvalds return error; 39201da177e4SLinus Torvalds } 3921853ac43aSMatt Mackall 3922396bcc52SMatthew Wilcox (Oracle) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 39235a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 39245a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 39255a6e75f8SKirill A. Shutemov { 392626083eb6SColin Ian King static const int values[] = { 39275a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 39285a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 39295a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 39305a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 39315a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 39325a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 39335a6e75f8SKirill A. Shutemov }; 39345a6e75f8SKirill A. Shutemov int i, count; 39355a6e75f8SKirill A. Shutemov 39365a6e75f8SKirill A. Shutemov for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { 39375a6e75f8SKirill A. Shutemov const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; 39385a6e75f8SKirill A. Shutemov 39395a6e75f8SKirill A. Shutemov count += sprintf(buf + count, fmt, 39405a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 39415a6e75f8SKirill A. Shutemov } 39425a6e75f8SKirill A. Shutemov buf[count - 1] = '\n'; 39435a6e75f8SKirill A. Shutemov return count; 39445a6e75f8SKirill A. Shutemov } 39455a6e75f8SKirill A. Shutemov 39465a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 39475a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 39485a6e75f8SKirill A. Shutemov { 39495a6e75f8SKirill A. Shutemov char tmp[16]; 39505a6e75f8SKirill A. Shutemov int huge; 39515a6e75f8SKirill A. Shutemov 39525a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 39535a6e75f8SKirill A. Shutemov return -EINVAL; 39545a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 39555a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 39565a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 39575a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 39585a6e75f8SKirill A. Shutemov 39595a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 39605a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 39615a6e75f8SKirill A. Shutemov return -EINVAL; 39625a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 39635a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 39645a6e75f8SKirill A. Shutemov return -EINVAL; 39655a6e75f8SKirill A. Shutemov 39665a6e75f8SKirill A. Shutemov shmem_huge = huge; 3967435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 39685a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 39695a6e75f8SKirill A. Shutemov return count; 39705a6e75f8SKirill A. Shutemov } 39715a6e75f8SKirill A. Shutemov 39725a6e75f8SKirill A. Shutemov struct kobj_attribute shmem_enabled_attr = 39735a6e75f8SKirill A. Shutemov __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); 3974396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 3975f3f0e1d2SKirill A. Shutemov 3976396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3977f3f0e1d2SKirill A. Shutemov bool shmem_huge_enabled(struct vm_area_struct *vma) 3978f3f0e1d2SKirill A. Shutemov { 3979f3f0e1d2SKirill A. Shutemov struct inode *inode = file_inode(vma->vm_file); 3980f3f0e1d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3981f3f0e1d2SKirill A. Shutemov loff_t i_size; 3982f3f0e1d2SKirill A. Shutemov pgoff_t off; 3983f3f0e1d2SKirill A. Shutemov 3984c0630669SYang Shi if ((vma->vm_flags & VM_NOHUGEPAGE) || 3985c0630669SYang Shi test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) 3986c0630669SYang Shi return false; 3987f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_FORCE) 3988f3f0e1d2SKirill A. Shutemov return true; 3989f3f0e1d2SKirill A. Shutemov if (shmem_huge == SHMEM_HUGE_DENY) 3990f3f0e1d2SKirill A. Shutemov return false; 3991f3f0e1d2SKirill A. Shutemov switch (sbinfo->huge) { 3992f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_NEVER: 3993f3f0e1d2SKirill A. Shutemov return false; 3994f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 3995f3f0e1d2SKirill A. Shutemov return true; 3996f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 3997f3f0e1d2SKirill A. Shutemov off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); 3998f3f0e1d2SKirill A. Shutemov i_size = round_up(i_size_read(inode), PAGE_SIZE); 3999f3f0e1d2SKirill A. Shutemov if (i_size >= HPAGE_PMD_SIZE && 4000f3f0e1d2SKirill A. Shutemov i_size >> PAGE_SHIFT >= off) 4001f3f0e1d2SKirill A. Shutemov return true; 4002e4a9bc58SJoe Perches fallthrough; 4003f3f0e1d2SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 4004f3f0e1d2SKirill A. Shutemov /* TODO: implement fadvise() hints */ 4005f3f0e1d2SKirill A. Shutemov return (vma->vm_flags & VM_HUGEPAGE); 4006f3f0e1d2SKirill A. Shutemov default: 4007f3f0e1d2SKirill A. Shutemov VM_BUG_ON(1); 4008f3f0e1d2SKirill A. Shutemov return false; 4009f3f0e1d2SKirill A. Shutemov } 4010f3f0e1d2SKirill A. Shutemov } 4011396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 40125a6e75f8SKirill A. Shutemov 4013853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4014853ac43aSMatt Mackall 4015853ac43aSMatt Mackall /* 4016853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4017853ac43aSMatt Mackall * 4018853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4019853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4020853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4021853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4022853ac43aSMatt Mackall */ 4023853ac43aSMatt Mackall 402441ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4025853ac43aSMatt Mackall .name = "tmpfs", 4026f3235626SDavid Howells .init_fs_context = ramfs_init_fs_context, 4027d7167b14SAl Viro .parameters = ramfs_fs_parameters, 4028853ac43aSMatt Mackall .kill_sb = kill_litter_super, 40292b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4030853ac43aSMatt Mackall }; 4031853ac43aSMatt Mackall 403241ffe5d5SHugh Dickins int __init shmem_init(void) 4033853ac43aSMatt Mackall { 403441ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4035853ac43aSMatt Mackall 403641ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4037853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4038853ac43aSMatt Mackall 4039853ac43aSMatt Mackall return 0; 4040853ac43aSMatt Mackall } 4041853ac43aSMatt Mackall 4042b56a2d8aSVineeth Remanan Pillai int shmem_unuse(unsigned int type, bool frontswap, 4043b56a2d8aSVineeth Remanan Pillai unsigned long *fs_pages_to_unuse) 4044853ac43aSMatt Mackall { 4045853ac43aSMatt Mackall return 0; 4046853ac43aSMatt Mackall } 4047853ac43aSMatt Mackall 40483f96b79aSHugh Dickins int shmem_lock(struct file *file, int lock, struct user_struct *user) 40493f96b79aSHugh Dickins { 40503f96b79aSHugh Dickins return 0; 40513f96b79aSHugh Dickins } 40523f96b79aSHugh Dickins 405324513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 405424513264SHugh Dickins { 405524513264SHugh Dickins } 405624513264SHugh Dickins 4057c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4058c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4059c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4060c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4061c01d5b30SHugh Dickins { 4062c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4063c01d5b30SHugh Dickins } 4064c01d5b30SHugh Dickins #endif 4065c01d5b30SHugh Dickins 406641ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 406794c1e62dSHugh Dickins { 406841ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 406994c1e62dSHugh Dickins } 407094c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 407194c1e62dSHugh Dickins 4072853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 40730b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 4074454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 40750b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 40760b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4077853ac43aSMatt Mackall 4078853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4079853ac43aSMatt Mackall 4080853ac43aSMatt Mackall /* common code */ 40811da177e4SLinus Torvalds 4082703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 4083c7277090SEric Paris unsigned long flags, unsigned int i_flags) 40841da177e4SLinus Torvalds { 40851da177e4SLinus Torvalds struct inode *inode; 408693dec2daSAl Viro struct file *res; 40871da177e4SLinus Torvalds 4088703321b6SMatthew Auld if (IS_ERR(mnt)) 4089703321b6SMatthew Auld return ERR_CAST(mnt); 40901da177e4SLinus Torvalds 4091285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 40921da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 40931da177e4SLinus Torvalds 40941da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 40951da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 40961da177e4SLinus Torvalds 409793dec2daSAl Viro inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, 409893dec2daSAl Viro flags); 4099dac2d1f6SAl Viro if (unlikely(!inode)) { 4100dac2d1f6SAl Viro shmem_unacct_size(flags, size); 4101dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 4102dac2d1f6SAl Viro } 4103c7277090SEric Paris inode->i_flags |= i_flags; 41041da177e4SLinus Torvalds inode->i_size = size; 41056d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 410626567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 410793dec2daSAl Viro if (!IS_ERR(res)) 410893dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 41094b42af81SAl Viro &shmem_file_operations); 41106b4d0b27SAl Viro if (IS_ERR(res)) 411193dec2daSAl Viro iput(inode); 41126b4d0b27SAl Viro return res; 41131da177e4SLinus Torvalds } 4114c7277090SEric Paris 4115c7277090SEric Paris /** 4116c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4117c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4118c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4119e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4120e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4121c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4122c7277090SEric Paris * @size: size to be set for the file 4123c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4124c7277090SEric Paris */ 4125c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4126c7277090SEric Paris { 4127703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 4128c7277090SEric Paris } 4129c7277090SEric Paris 4130c7277090SEric Paris /** 4131c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4132c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4133c7277090SEric Paris * @size: size to be set for the file 4134c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4135c7277090SEric Paris */ 4136c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4137c7277090SEric Paris { 4138703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 4139c7277090SEric Paris } 4140395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 41411da177e4SLinus Torvalds 414246711810SRandy Dunlap /** 4143703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 4144703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 4145703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 4146703321b6SMatthew Auld * @size: size to be set for the file 4147703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4148703321b6SMatthew Auld */ 4149703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 4150703321b6SMatthew Auld loff_t size, unsigned long flags) 4151703321b6SMatthew Auld { 4152703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 4153703321b6SMatthew Auld } 4154703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 4155703321b6SMatthew Auld 4156703321b6SMatthew Auld /** 41571da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 41581da177e4SLinus Torvalds * @vma: the vma to be mmapped is prepared by do_mmap_pgoff 41591da177e4SLinus Torvalds */ 41601da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 41611da177e4SLinus Torvalds { 41621da177e4SLinus Torvalds struct file *file; 41631da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 41641da177e4SLinus Torvalds 416566fc1303SHugh Dickins /* 416666fc1303SHugh Dickins * Cloning a new file under mmap_sem leads to a lock ordering conflict 416766fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 416866fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 416966fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 417066fc1303SHugh Dickins */ 4171703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 41721da177e4SLinus Torvalds if (IS_ERR(file)) 41731da177e4SLinus Torvalds return PTR_ERR(file); 41741da177e4SLinus Torvalds 41751da177e4SLinus Torvalds if (vma->vm_file) 41761da177e4SLinus Torvalds fput(vma->vm_file); 41771da177e4SLinus Torvalds vma->vm_file = file; 41781da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4179f3f0e1d2SKirill A. Shutemov 4180396bcc52SMatthew Wilcox (Oracle) if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 4181f3f0e1d2SKirill A. Shutemov ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < 4182f3f0e1d2SKirill A. Shutemov (vma->vm_end & HPAGE_PMD_MASK)) { 4183f3f0e1d2SKirill A. Shutemov khugepaged_enter(vma, vma->vm_flags); 4184f3f0e1d2SKirill A. Shutemov } 4185f3f0e1d2SKirill A. Shutemov 41861da177e4SLinus Torvalds return 0; 41871da177e4SLinus Torvalds } 4188d9d90e5eSHugh Dickins 4189d9d90e5eSHugh Dickins /** 4190d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4191d9d90e5eSHugh Dickins * @mapping: the page's address_space 4192d9d90e5eSHugh Dickins * @index: the page index 4193d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4194d9d90e5eSHugh Dickins * 4195d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4196d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 4197d9d90e5eSHugh Dickins * But read_cache_page_gfp() uses the ->readpage() method: which does not 4198d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4199d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4200d9d90e5eSHugh Dickins * 420168da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 420268da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4203d9d90e5eSHugh Dickins */ 4204d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4205d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4206d9d90e5eSHugh Dickins { 420768da9f05SHugh Dickins #ifdef CONFIG_SHMEM 420868da9f05SHugh Dickins struct inode *inode = mapping->host; 42099276aad6SHugh Dickins struct page *page; 421068da9f05SHugh Dickins int error; 421168da9f05SHugh Dickins 421268da9f05SHugh Dickins BUG_ON(mapping->a_ops != &shmem_aops); 42139e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4214cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 421568da9f05SHugh Dickins if (error) 421668da9f05SHugh Dickins page = ERR_PTR(error); 421768da9f05SHugh Dickins else 421868da9f05SHugh Dickins unlock_page(page); 421968da9f05SHugh Dickins return page; 422068da9f05SHugh Dickins #else 422168da9f05SHugh Dickins /* 422268da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 422368da9f05SHugh Dickins */ 4224d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 422568da9f05SHugh Dickins #endif 4226d9d90e5eSHugh Dickins } 4227d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4228