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> 31e408e695STheodore Ts'o #include <linux/fileattr.h> 32853ac43aSMatt Mackall #include <linux/mm.h> 3346c9a946SArnd Bergmann #include <linux/random.h> 34174cd4b1SIngo Molnar #include <linux/sched/signal.h> 35b95f1b31SPaul Gortmaker #include <linux/export.h> 36853ac43aSMatt Mackall #include <linux/swap.h> 37e2e40f2cSChristoph Hellwig #include <linux/uio.h> 38749df87bSMike Kravetz #include <linux/hugetlb.h> 39626c3920SAl Viro #include <linux/fs_parser.h> 4086a2f3f2SMiaohe Lin #include <linux/swapfile.h> 41014bb1deSNeilBrown #include "swap.h" 4295cc09d6SAndrea Arcangeli 43853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 44853ac43aSMatt Mackall 45853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 461da177e4SLinus Torvalds /* 471da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 481da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 491da177e4SLinus Torvalds * which makes it a completely usable filesystem. 501da177e4SLinus Torvalds */ 511da177e4SLinus Torvalds 5239f0247dSAndreas Gruenbacher #include <linux/xattr.h> 53a5694255SChristoph Hellwig #include <linux/exportfs.h> 541c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 55feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 561da177e4SLinus Torvalds #include <linux/mman.h> 571da177e4SLinus Torvalds #include <linux/string.h> 581da177e4SLinus Torvalds #include <linux/slab.h> 591da177e4SLinus Torvalds #include <linux/backing-dev.h> 601da177e4SLinus Torvalds #include <linux/shmem_fs.h> 611da177e4SLinus Torvalds #include <linux/writeback.h> 62bda97eabSHugh Dickins #include <linux/pagevec.h> 6341ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6483e4fa9cSHugh Dickins #include <linux/falloc.h> 65708e3508SHugh Dickins #include <linux/splice.h> 661da177e4SLinus Torvalds #include <linux/security.h> 671da177e4SLinus Torvalds #include <linux/swapops.h> 681da177e4SLinus Torvalds #include <linux/mempolicy.h> 691da177e4SLinus Torvalds #include <linux/namei.h> 70b00dc3adSHugh Dickins #include <linux/ctype.h> 71304dbdb7SLee Schermerhorn #include <linux/migrate.h> 72c1f60a5aSChristoph Lameter #include <linux/highmem.h> 73680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7492562927SMimi Zohar #include <linux/magic.h> 759183df25SDavid Herrmann #include <linux/syscalls.h> 7640e041a2SDavid Herrmann #include <linux/fcntl.h> 779183df25SDavid Herrmann #include <uapi/linux/memfd.h> 78cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 794c27fe4cSMike Rapoport #include <linux/rmap.h> 802b4db796SAmir Goldstein #include <linux/uuid.h> 81304dbdb7SLee Schermerhorn 827c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 831da177e4SLinus Torvalds 84dd56b046SMel Gorman #include "internal.h" 85dd56b046SMel Gorman 8609cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8709cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 881da177e4SLinus Torvalds 891da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 901da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 911da177e4SLinus Torvalds 9269f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9369f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9469f07ec9SHugh Dickins 951aac1400SHugh Dickins /* 96f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 979608703eSJan Kara * inode->i_private (with i_rwsem making sure that it has only one user at 98f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 991aac1400SHugh Dickins */ 1001aac1400SHugh Dickins struct shmem_falloc { 1018e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1021aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1031aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1041aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1051aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1061aac1400SHugh Dickins }; 1071aac1400SHugh Dickins 1080b5071ddSAl Viro struct shmem_options { 1090b5071ddSAl Viro unsigned long long blocks; 1100b5071ddSAl Viro unsigned long long inodes; 1110b5071ddSAl Viro struct mempolicy *mpol; 1120b5071ddSAl Viro kuid_t uid; 1130b5071ddSAl Viro kgid_t gid; 1140b5071ddSAl Viro umode_t mode; 115ea3271f7SChris Down bool full_inums; 1160b5071ddSAl Viro int huge; 1170b5071ddSAl Viro int seen; 1180b5071ddSAl Viro #define SHMEM_SEEN_BLOCKS 1 1190b5071ddSAl Viro #define SHMEM_SEEN_INODES 2 1200b5071ddSAl Viro #define SHMEM_SEEN_HUGE 4 121ea3271f7SChris Down #define SHMEM_SEEN_INUMS 8 1220b5071ddSAl Viro }; 1230b5071ddSAl Viro 124b76db735SAndrew Morton #ifdef CONFIG_TMPFS 125680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 126680d794bSakpm@linux-foundation.org { 127ca79b0c2SArun KS return totalram_pages() / 2; 128680d794bSakpm@linux-foundation.org } 129680d794bSakpm@linux-foundation.org 130680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 131680d794bSakpm@linux-foundation.org { 132ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 133ca79b0c2SArun KS 134ca79b0c2SArun KS return min(nr_pages - totalhigh_pages(), nr_pages / 2); 135680d794bSakpm@linux-foundation.org } 136b76db735SAndrew Morton #endif 137680d794bSakpm@linux-foundation.org 138da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 139da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 140c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 141c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type); 14268da9f05SHugh Dickins static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1439e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp, 144cfda0526SMike Rapoport gfp_t gfp, struct vm_area_struct *vma, 1452b740303SSouptick Joarder struct vm_fault *vmf, vm_fault_t *fault_type); 14668da9f05SHugh Dickins 147f3f0e1d2SKirill A. Shutemov int shmem_getpage(struct inode *inode, pgoff_t index, 1489e18eb29SAndres Lagar-Cavilla struct page **pagep, enum sgp_type sgp) 14968da9f05SHugh Dickins { 15068da9f05SHugh Dickins return shmem_getpage_gfp(inode, index, pagep, sgp, 151cfda0526SMike Rapoport mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 15268da9f05SHugh Dickins } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1551da177e4SLinus Torvalds { 1561da177e4SLinus Torvalds return sb->s_fs_info; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds /* 1601da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1611da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1621da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1631da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1641da177e4SLinus Torvalds */ 1651da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1661da177e4SLinus Torvalds { 1670b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 168191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1721da177e4SLinus Torvalds { 1730b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1741da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 17777142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 17877142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 17977142517SKonstantin Khlebnikov { 18077142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 18177142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 18277142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 18377142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 18477142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 18577142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 18677142517SKonstantin Khlebnikov } 18777142517SKonstantin Khlebnikov return 0; 18877142517SKonstantin Khlebnikov } 18977142517SKonstantin Khlebnikov 1901da177e4SLinus Torvalds /* 1911da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 19275edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 1931da177e4SLinus Torvalds * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1941da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1951da177e4SLinus Torvalds */ 196800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1971da177e4SLinus Torvalds { 198800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 199800d8c63SKirill A. Shutemov return 0; 200800d8c63SKirill A. Shutemov 201800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 202800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 2061da177e4SLinus Torvalds { 2070b0a0806SHugh Dickins if (flags & VM_NORESERVE) 20809cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 2110f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 2120f079694SMike Rapoport { 2130f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2140f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2150f079694SMike Rapoport 2160f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 2170f079694SMike Rapoport return false; 2180f079694SMike Rapoport 2190f079694SMike Rapoport if (sbinfo->max_blocks) { 2200f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2210f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2220f079694SMike Rapoport goto unacct; 2230f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2240f079694SMike Rapoport } 2250f079694SMike Rapoport 2260f079694SMike Rapoport return true; 2270f079694SMike Rapoport 2280f079694SMike Rapoport unacct: 2290f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2300f079694SMike Rapoport return false; 2310f079694SMike Rapoport } 2320f079694SMike Rapoport 2330f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2340f079694SMike Rapoport { 2350f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2360f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2370f079694SMike Rapoport 2380f079694SMike Rapoport if (sbinfo->max_blocks) 2390f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2400f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2410f079694SMike Rapoport } 2420f079694SMike Rapoport 243759b9775SHugh Dickins static const struct super_operations shmem_ops; 24430e6a51dSHui Su const struct address_space_operations shmem_aops; 24515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 24692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 24792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 24892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 249f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 250779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2511da177e4SLinus Torvalds 252b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 253b0506e48SMike Rapoport { 254b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 255b0506e48SMike Rapoport } 256b0506e48SMike Rapoport 2571da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 258cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2591da177e4SLinus Torvalds 260e809d5f0SChris Down /* 261e809d5f0SChris Down * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and 262e809d5f0SChris Down * produces a novel ino for the newly allocated inode. 263e809d5f0SChris Down * 264e809d5f0SChris Down * It may also be called when making a hard link to permit the space needed by 265e809d5f0SChris Down * each dentry. However, in that case, no new inode number is needed since that 266e809d5f0SChris Down * internally draws from another pool of inode numbers (currently global 267e809d5f0SChris Down * get_next_ino()). This case is indicated by passing NULL as inop. 268e809d5f0SChris Down */ 269e809d5f0SChris Down #define SHMEM_INO_BATCH 1024 270e809d5f0SChris Down static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) 2715b04c689SPavel Emelyanov { 2725b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 273e809d5f0SChris Down ino_t ino; 274e809d5f0SChris Down 275e809d5f0SChris Down if (!(sb->s_flags & SB_KERNMOUNT)) { 276bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 277bb3e96d6SByron Stanoszek if (sbinfo->max_inodes) { 2785b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 279bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 2805b04c689SPavel Emelyanov return -ENOSPC; 2815b04c689SPavel Emelyanov } 2825b04c689SPavel Emelyanov sbinfo->free_inodes--; 283bb3e96d6SByron Stanoszek } 284e809d5f0SChris Down if (inop) { 285e809d5f0SChris Down ino = sbinfo->next_ino++; 286e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 287e809d5f0SChris Down ino = sbinfo->next_ino++; 288ea3271f7SChris Down if (unlikely(!sbinfo->full_inums && 289ea3271f7SChris Down ino > UINT_MAX)) { 290e809d5f0SChris Down /* 291e809d5f0SChris Down * Emulate get_next_ino uint wraparound for 292e809d5f0SChris Down * compatibility 293e809d5f0SChris Down */ 294ea3271f7SChris Down if (IS_ENABLED(CONFIG_64BIT)) 295ea3271f7SChris Down pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", 296ea3271f7SChris Down __func__, MINOR(sb->s_dev)); 297ea3271f7SChris Down sbinfo->next_ino = 1; 298ea3271f7SChris Down ino = sbinfo->next_ino++; 2995b04c689SPavel Emelyanov } 300e809d5f0SChris Down *inop = ino; 301e809d5f0SChris Down } 302bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 303e809d5f0SChris Down } else if (inop) { 304e809d5f0SChris Down /* 305e809d5f0SChris Down * __shmem_file_setup, one of our callers, is lock-free: it 306e809d5f0SChris Down * doesn't hold stat_lock in shmem_reserve_inode since 307e809d5f0SChris Down * max_inodes is always 0, and is called from potentially 308e809d5f0SChris Down * unknown contexts. As such, use a per-cpu batched allocator 309e809d5f0SChris Down * which doesn't require the per-sb stat_lock unless we are at 310e809d5f0SChris Down * the batch boundary. 311ea3271f7SChris Down * 312ea3271f7SChris Down * We don't need to worry about inode{32,64} since SB_KERNMOUNT 313ea3271f7SChris Down * shmem mounts are not exposed to userspace, so we don't need 314ea3271f7SChris Down * to worry about things like glibc compatibility. 315e809d5f0SChris Down */ 316e809d5f0SChris Down ino_t *next_ino; 317bf11b9a8SSebastian Andrzej Siewior 318e809d5f0SChris Down next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); 319e809d5f0SChris Down ino = *next_ino; 320e809d5f0SChris Down if (unlikely(ino % SHMEM_INO_BATCH == 0)) { 321bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 322e809d5f0SChris Down ino = sbinfo->next_ino; 323e809d5f0SChris Down sbinfo->next_ino += SHMEM_INO_BATCH; 324bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 325e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 326e809d5f0SChris Down ino++; 327e809d5f0SChris Down } 328e809d5f0SChris Down *inop = ino; 329e809d5f0SChris Down *next_ino = ++ino; 330e809d5f0SChris Down put_cpu(); 331e809d5f0SChris Down } 332e809d5f0SChris Down 3335b04c689SPavel Emelyanov return 0; 3345b04c689SPavel Emelyanov } 3355b04c689SPavel Emelyanov 3365b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 3375b04c689SPavel Emelyanov { 3385b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3395b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 340bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 3415b04c689SPavel Emelyanov sbinfo->free_inodes++; 342bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3435b04c689SPavel Emelyanov } 3445b04c689SPavel Emelyanov } 3455b04c689SPavel Emelyanov 34646711810SRandy Dunlap /** 34741ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 3481da177e4SLinus Torvalds * @inode: inode to recalc 3491da177e4SLinus Torvalds * 3501da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 3511da177e4SLinus Torvalds * undirtied hole pages behind our back. 3521da177e4SLinus Torvalds * 3531da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 3541da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 3551da177e4SLinus Torvalds * 3561da177e4SLinus Torvalds * It has to be called with the spinlock held. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 3611da177e4SLinus Torvalds long freed; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 3641da177e4SLinus Torvalds if (freed > 0) { 3651da177e4SLinus Torvalds info->alloced -= freed; 36654af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 3670f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 371800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 372800d8c63SKirill A. Shutemov { 373800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3744595ef88SKirill A. Shutemov unsigned long flags; 375800d8c63SKirill A. Shutemov 3760f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 377800d8c63SKirill A. Shutemov return false; 378b1cc94abSMike Rapoport 379aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 380aaa52e34SHugh Dickins inode->i_mapping->nrpages += pages; 381aaa52e34SHugh Dickins 3824595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 383800d8c63SKirill A. Shutemov info->alloced += pages; 384800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 385800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3864595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 387800d8c63SKirill A. Shutemov 388800d8c63SKirill A. Shutemov return true; 389800d8c63SKirill A. Shutemov } 390800d8c63SKirill A. Shutemov 391800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 392800d8c63SKirill A. Shutemov { 393800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3944595ef88SKirill A. Shutemov unsigned long flags; 395800d8c63SKirill A. Shutemov 3966ffcd825SMatthew Wilcox (Oracle) /* nrpages adjustment done by __filemap_remove_folio() or caller */ 397aaa52e34SHugh Dickins 3984595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 399800d8c63SKirill A. Shutemov info->alloced -= pages; 400800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 401800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 4024595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 403800d8c63SKirill A. Shutemov 4040f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 405800d8c63SKirill A. Shutemov } 406800d8c63SKirill A. Shutemov 4077a5d0fbbSHugh Dickins /* 40862f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 4097a5d0fbbSHugh Dickins */ 41062f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 4117a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 4127a5d0fbbSHugh Dickins { 41362f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 4146dbaf22cSJohannes Weiner void *item; 4157a5d0fbbSHugh Dickins 4167a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 4176dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 41862f945b6SMatthew Wilcox item = xas_load(&xas); 4197a5d0fbbSHugh Dickins if (item != expected) 4207a5d0fbbSHugh Dickins return -ENOENT; 42162f945b6SMatthew Wilcox xas_store(&xas, replacement); 4227a5d0fbbSHugh Dickins return 0; 4237a5d0fbbSHugh Dickins } 4247a5d0fbbSHugh Dickins 4257a5d0fbbSHugh Dickins /* 426d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 427d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 428d1899228SHugh Dickins * 429d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 430d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 431d1899228SHugh Dickins */ 432d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 433d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 434d1899228SHugh Dickins { 435a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 436d1899228SHugh Dickins } 437d1899228SHugh Dickins 438d1899228SHugh Dickins /* 4395a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 4405a6e75f8SKirill A. Shutemov * 4415a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 4425a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 4435a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 4445a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 4455a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 4465a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 4475a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 4485a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 4495a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 4505a6e75f8SKirill A. Shutemov */ 4515a6e75f8SKirill A. Shutemov 4525a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 4535a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 4545a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 4555a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 4565a6e75f8SKirill A. Shutemov 4575a6e75f8SKirill A. Shutemov /* 4585a6e75f8SKirill A. Shutemov * Special values. 4595a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 4605a6e75f8SKirill A. Shutemov * 4615a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 4625a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 4635a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 4645a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 4655a6e75f8SKirill A. Shutemov * 4665a6e75f8SKirill A. Shutemov */ 4675a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 4685a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 4695a6e75f8SKirill A. Shutemov 470396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4715a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 4725a6e75f8SKirill A. Shutemov 4735e6e5a12SHugh Dickins static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER; 4745a6e75f8SKirill A. Shutemov 4755e6e5a12SHugh Dickins bool shmem_is_huge(struct vm_area_struct *vma, 4765e6e5a12SHugh Dickins struct inode *inode, pgoff_t index) 477c852023eSHugh Dickins { 478c852023eSHugh Dickins loff_t i_size; 479c852023eSHugh Dickins 480f7cd16a5SXavier Roche if (!S_ISREG(inode->i_mode)) 481f7cd16a5SXavier Roche return false; 4825e6e5a12SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 4835e6e5a12SHugh Dickins return false; 4845e6e5a12SHugh Dickins if (vma && ((vma->vm_flags & VM_NOHUGEPAGE) || 4855e6e5a12SHugh Dickins test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))) 486c852023eSHugh Dickins return false; 487c852023eSHugh Dickins if (shmem_huge == SHMEM_HUGE_FORCE) 488c852023eSHugh Dickins return true; 4895e6e5a12SHugh Dickins 4905e6e5a12SHugh Dickins switch (SHMEM_SB(inode->i_sb)->huge) { 491c852023eSHugh Dickins case SHMEM_HUGE_ALWAYS: 492c852023eSHugh Dickins return true; 493c852023eSHugh Dickins case SHMEM_HUGE_WITHIN_SIZE: 494de6ee659SLiu Yuntao index = round_up(index + 1, HPAGE_PMD_NR); 495c852023eSHugh Dickins i_size = round_up(i_size_read(inode), PAGE_SIZE); 496de6ee659SLiu Yuntao if (i_size >> PAGE_SHIFT >= index) 497c852023eSHugh Dickins return true; 498c852023eSHugh Dickins fallthrough; 499c852023eSHugh Dickins case SHMEM_HUGE_ADVISE: 5005e6e5a12SHugh Dickins if (vma && (vma->vm_flags & VM_HUGEPAGE)) 5015e6e5a12SHugh Dickins return true; 5025e6e5a12SHugh Dickins fallthrough; 503c852023eSHugh Dickins default: 504c852023eSHugh Dickins return false; 505c852023eSHugh Dickins } 506c852023eSHugh Dickins } 5075a6e75f8SKirill A. Shutemov 508e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) 5095a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 5105a6e75f8SKirill A. Shutemov { 5115a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 5125a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 5135a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 5145a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 5155a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 5165a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 5175a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 5185a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 5195a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 5205a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 5215a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 5225a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 5235a6e75f8SKirill A. Shutemov return -EINVAL; 5245a6e75f8SKirill A. Shutemov } 525e5f2249aSArnd Bergmann #endif 5265a6e75f8SKirill A. Shutemov 527e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 5285a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 5295a6e75f8SKirill A. Shutemov { 5305a6e75f8SKirill A. Shutemov switch (huge) { 5315a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 5325a6e75f8SKirill A. Shutemov return "never"; 5335a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 5345a6e75f8SKirill A. Shutemov return "always"; 5355a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 5365a6e75f8SKirill A. Shutemov return "within_size"; 5375a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 5385a6e75f8SKirill A. Shutemov return "advise"; 5395a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 5405a6e75f8SKirill A. Shutemov return "deny"; 5415a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 5425a6e75f8SKirill A. Shutemov return "force"; 5435a6e75f8SKirill A. Shutemov default: 5445a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 5455a6e75f8SKirill A. Shutemov return "bad_val"; 5465a6e75f8SKirill A. Shutemov } 5475a6e75f8SKirill A. Shutemov } 548f1f5929cSJérémy Lefaure #endif 5495a6e75f8SKirill A. Shutemov 550779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 551779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 552779750d2SKirill A. Shutemov { 553779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 554253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 555779750d2SKirill A. Shutemov struct inode *inode; 556779750d2SKirill A. Shutemov struct shmem_inode_info *info; 55705624571SMatthew Wilcox (Oracle) struct folio *folio; 558779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 55962c9827cSGang Li int split = 0; 560779750d2SKirill A. Shutemov 561779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 562779750d2SKirill A. Shutemov return SHRINK_STOP; 563779750d2SKirill A. Shutemov 564779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 565779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 566779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 567779750d2SKirill A. Shutemov 568779750d2SKirill A. Shutemov /* pin the inode */ 569779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 570779750d2SKirill A. Shutemov 571779750d2SKirill A. Shutemov /* inode is about to be evicted */ 572779750d2SKirill A. Shutemov if (!inode) { 573779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 574779750d2SKirill A. Shutemov goto next; 575779750d2SKirill A. Shutemov } 576779750d2SKirill A. Shutemov 577779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 578779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 579779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 580253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 581779750d2SKirill A. Shutemov goto next; 582779750d2SKirill A. Shutemov } 583779750d2SKirill A. Shutemov 584779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 585779750d2SKirill A. Shutemov next: 58662c9827cSGang Li sbinfo->shrinklist_len--; 587779750d2SKirill A. Shutemov if (!--batch) 588779750d2SKirill A. Shutemov break; 589779750d2SKirill A. Shutemov } 590779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 591779750d2SKirill A. Shutemov 592253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 593253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 594253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 595253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 596253fd0f0SKirill A. Shutemov iput(inode); 597253fd0f0SKirill A. Shutemov } 598253fd0f0SKirill A. Shutemov 599779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 600779750d2SKirill A. Shutemov int ret; 60105624571SMatthew Wilcox (Oracle) pgoff_t index; 602779750d2SKirill A. Shutemov 603779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 604779750d2SKirill A. Shutemov inode = &info->vfs_inode; 605779750d2SKirill A. Shutemov 606b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 60762c9827cSGang Li goto move_back; 608779750d2SKirill A. Shutemov 60905624571SMatthew Wilcox (Oracle) index = (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT; 61005624571SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, index); 61105624571SMatthew Wilcox (Oracle) if (!folio) 612779750d2SKirill A. Shutemov goto drop; 613779750d2SKirill A. Shutemov 614b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 61505624571SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) { 61605624571SMatthew Wilcox (Oracle) folio_put(folio); 617779750d2SKirill A. Shutemov goto drop; 618779750d2SKirill A. Shutemov } 619779750d2SKirill A. Shutemov 620b3cd54b2SKirill A. Shutemov /* 62162c9827cSGang Li * Move the inode on the list back to shrinklist if we failed 62262c9827cSGang Li * to lock the page at this time. 623b3cd54b2SKirill A. Shutemov * 624b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 625b3cd54b2SKirill A. Shutemov * reclaim path. 626b3cd54b2SKirill A. Shutemov */ 62705624571SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) { 62805624571SMatthew Wilcox (Oracle) folio_put(folio); 62962c9827cSGang Li goto move_back; 630b3cd54b2SKirill A. Shutemov } 631b3cd54b2SKirill A. Shutemov 632d788f5b3SMatthew Wilcox (Oracle) ret = split_folio(folio); 63305624571SMatthew Wilcox (Oracle) folio_unlock(folio); 63405624571SMatthew Wilcox (Oracle) folio_put(folio); 635779750d2SKirill A. Shutemov 63662c9827cSGang Li /* If split failed move the inode on the list back to shrinklist */ 637b3cd54b2SKirill A. Shutemov if (ret) 63862c9827cSGang Li goto move_back; 639779750d2SKirill A. Shutemov 640779750d2SKirill A. Shutemov split++; 641779750d2SKirill A. Shutemov drop: 642779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 64362c9827cSGang Li goto put; 64462c9827cSGang Li move_back: 64562c9827cSGang Li /* 64662c9827cSGang Li * Make sure the inode is either on the global list or deleted 64762c9827cSGang Li * from any local list before iput() since it could be deleted 64862c9827cSGang Li * in another thread once we put the inode (then the local list 64962c9827cSGang Li * is corrupted). 65062c9827cSGang Li */ 65162c9827cSGang Li spin_lock(&sbinfo->shrinklist_lock); 65262c9827cSGang Li list_move(&info->shrinklist, &sbinfo->shrinklist); 65362c9827cSGang Li sbinfo->shrinklist_len++; 65462c9827cSGang Li spin_unlock(&sbinfo->shrinklist_lock); 65562c9827cSGang Li put: 656779750d2SKirill A. Shutemov iput(inode); 657779750d2SKirill A. Shutemov } 658779750d2SKirill A. Shutemov 659779750d2SKirill A. Shutemov return split; 660779750d2SKirill A. Shutemov } 661779750d2SKirill A. Shutemov 662779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 663779750d2SKirill A. Shutemov struct shrink_control *sc) 664779750d2SKirill A. Shutemov { 665779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 666779750d2SKirill A. Shutemov 667779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 668779750d2SKirill A. Shutemov return SHRINK_STOP; 669779750d2SKirill A. Shutemov 670779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 671779750d2SKirill A. Shutemov } 672779750d2SKirill A. Shutemov 673779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 674779750d2SKirill A. Shutemov struct shrink_control *sc) 675779750d2SKirill A. Shutemov { 676779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 677779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 678779750d2SKirill A. Shutemov } 679396bcc52SMatthew Wilcox (Oracle) #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 6805a6e75f8SKirill A. Shutemov 6815a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 6825a6e75f8SKirill A. Shutemov 6835e6e5a12SHugh Dickins bool shmem_is_huge(struct vm_area_struct *vma, 6845e6e5a12SHugh Dickins struct inode *inode, pgoff_t index) 6855e6e5a12SHugh Dickins { 6865e6e5a12SHugh Dickins return false; 6875e6e5a12SHugh Dickins } 6885e6e5a12SHugh Dickins 689779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 690779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 691779750d2SKirill A. Shutemov { 692779750d2SKirill A. Shutemov return 0; 693779750d2SKirill A. Shutemov } 694396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 6955a6e75f8SKirill A. Shutemov 6965a6e75f8SKirill A. Shutemov /* 6972bb876b5SMatthew Wilcox (Oracle) * Like filemap_add_folio, but error if expected item has gone. 69846f65ec1SHugh Dickins */ 699b7dd44a1SMatthew Wilcox (Oracle) static int shmem_add_to_page_cache(struct folio *folio, 70046f65ec1SHugh Dickins struct address_space *mapping, 7013fea5a49SJohannes Weiner pgoff_t index, void *expected, gfp_t gfp, 7023fea5a49SJohannes Weiner struct mm_struct *charge_mm) 70346f65ec1SHugh Dickins { 704b7dd44a1SMatthew Wilcox (Oracle) XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); 705b7dd44a1SMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7063fea5a49SJohannes Weiner int error; 70746f65ec1SHugh Dickins 708b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); 709b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 710b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 711b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON(expected && folio_test_large(folio)); 71246f65ec1SHugh Dickins 713b7dd44a1SMatthew Wilcox (Oracle) folio_ref_add(folio, nr); 714b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = mapping; 715b7dd44a1SMatthew Wilcox (Oracle) folio->index = index; 71646f65ec1SHugh Dickins 717b7dd44a1SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) { 718b7dd44a1SMatthew Wilcox (Oracle) error = mem_cgroup_charge(folio, charge_mm, gfp); 7193fea5a49SJohannes Weiner if (error) { 720b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 7213fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK); 7223fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK_CHARGE); 7233fea5a49SJohannes Weiner } 7243fea5a49SJohannes Weiner goto error; 7253fea5a49SJohannes Weiner } 7264c6355b2SJohannes Weiner } 727b7dd44a1SMatthew Wilcox (Oracle) folio_throttle_swaprate(folio, gfp); 7283fea5a49SJohannes Weiner 729552446a4SMatthew Wilcox do { 730552446a4SMatthew Wilcox xas_lock_irq(&xas); 7316b24ca4aSMatthew Wilcox (Oracle) if (expected != xas_find_conflict(&xas)) { 732552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 7336b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7346b24ca4aSMatthew Wilcox (Oracle) } 7356b24ca4aSMatthew Wilcox (Oracle) if (expected && xas_find_conflict(&xas)) { 7366b24ca4aSMatthew Wilcox (Oracle) xas_set_err(&xas, -EEXIST); 7376b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7386b24ca4aSMatthew Wilcox (Oracle) } 739b7dd44a1SMatthew Wilcox (Oracle) xas_store(&xas, folio); 740552446a4SMatthew Wilcox if (xas_error(&xas)) 741552446a4SMatthew Wilcox goto unlock; 742b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 743800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 744b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr); 745552446a4SMatthew Wilcox } 746552446a4SMatthew Wilcox mapping->nrpages += nr; 747b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); 748b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, nr); 749552446a4SMatthew Wilcox unlock: 750552446a4SMatthew Wilcox xas_unlock_irq(&xas); 751552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 752552446a4SMatthew Wilcox 753552446a4SMatthew Wilcox if (xas_error(&xas)) { 7543fea5a49SJohannes Weiner error = xas_error(&xas); 7553fea5a49SJohannes Weiner goto error; 75646f65ec1SHugh Dickins } 757552446a4SMatthew Wilcox 758552446a4SMatthew Wilcox return 0; 7593fea5a49SJohannes Weiner error: 760b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = NULL; 761b7dd44a1SMatthew Wilcox (Oracle) folio_ref_sub(folio, nr); 7623fea5a49SJohannes Weiner return error; 76346f65ec1SHugh Dickins } 76446f65ec1SHugh Dickins 76546f65ec1SHugh Dickins /* 7664cd400fdSMatthew Wilcox (Oracle) * Like delete_from_page_cache, but substitutes swap for @folio. 7676922c0c7SHugh Dickins */ 7684cd400fdSMatthew Wilcox (Oracle) static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) 7696922c0c7SHugh Dickins { 7704cd400fdSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 7714cd400fdSMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7726922c0c7SHugh Dickins int error; 7736922c0c7SHugh Dickins 774b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 7754cd400fdSMatthew Wilcox (Oracle) error = shmem_replace_entry(mapping, folio->index, folio, radswap); 7764cd400fdSMatthew Wilcox (Oracle) folio->mapping = NULL; 7774cd400fdSMatthew Wilcox (Oracle) mapping->nrpages -= nr; 7784cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr); 7794cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr); 780b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 7814cd400fdSMatthew Wilcox (Oracle) folio_put(folio); 7826922c0c7SHugh Dickins BUG_ON(error); 7836922c0c7SHugh Dickins } 7846922c0c7SHugh Dickins 7856922c0c7SHugh Dickins /* 786c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 7877a5d0fbbSHugh Dickins */ 7887a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 7897a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 7907a5d0fbbSHugh Dickins { 7916dbaf22cSJohannes Weiner void *old; 7927a5d0fbbSHugh Dickins 79355f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 7946dbaf22cSJohannes Weiner if (old != radswap) 7956dbaf22cSJohannes Weiner return -ENOENT; 7967a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 7976dbaf22cSJohannes Weiner return 0; 7987a5d0fbbSHugh Dickins } 7997a5d0fbbSHugh Dickins 8007a5d0fbbSHugh Dickins /* 8016a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 80248131e03SVlastimil Babka * given offsets are swapped out. 8036a15a370SVlastimil Babka * 8049608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 8056a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 8066a15a370SVlastimil Babka */ 80748131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 80848131e03SVlastimil Babka pgoff_t start, pgoff_t end) 8096a15a370SVlastimil Babka { 8107ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 8116a15a370SVlastimil Babka struct page *page; 81248131e03SVlastimil Babka unsigned long swapped = 0; 8136a15a370SVlastimil Babka 8146a15a370SVlastimil Babka rcu_read_lock(); 8157ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 8167ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 8172cf938aaSMatthew Wilcox continue; 8183159f943SMatthew Wilcox if (xa_is_value(page)) 8196a15a370SVlastimil Babka swapped++; 8206a15a370SVlastimil Babka 8216a15a370SVlastimil Babka if (need_resched()) { 8227ae3424fSMatthew Wilcox xas_pause(&xas); 8236a15a370SVlastimil Babka cond_resched_rcu(); 8246a15a370SVlastimil Babka } 8256a15a370SVlastimil Babka } 8266a15a370SVlastimil Babka 8276a15a370SVlastimil Babka rcu_read_unlock(); 8286a15a370SVlastimil Babka 8296a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 8306a15a370SVlastimil Babka } 8316a15a370SVlastimil Babka 8326a15a370SVlastimil Babka /* 83348131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 83448131e03SVlastimil Babka * given vma is swapped out. 83548131e03SVlastimil Babka * 8369608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 83748131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 83848131e03SVlastimil Babka */ 83948131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 84048131e03SVlastimil Babka { 84148131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 84248131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 84348131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 84448131e03SVlastimil Babka unsigned long swapped; 84548131e03SVlastimil Babka 84648131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 84748131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 84848131e03SVlastimil Babka 84948131e03SVlastimil Babka /* 85048131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 85148131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 85248131e03SVlastimil Babka * already track. 85348131e03SVlastimil Babka */ 85448131e03SVlastimil Babka if (!swapped) 85548131e03SVlastimil Babka return 0; 85648131e03SVlastimil Babka 85748131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 85848131e03SVlastimil Babka return swapped << PAGE_SHIFT; 85948131e03SVlastimil Babka 86048131e03SVlastimil Babka /* Here comes the more involved part */ 86102399c88SPeter Xu return shmem_partial_swap_usage(mapping, vma->vm_pgoff, 86202399c88SPeter Xu vma->vm_pgoff + vma_pages(vma)); 86348131e03SVlastimil Babka } 86448131e03SVlastimil Babka 86548131e03SVlastimil Babka /* 86624513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 86724513264SHugh Dickins */ 86824513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 86924513264SHugh Dickins { 870105c988fSMatthew Wilcox (Oracle) struct folio_batch fbatch; 87124513264SHugh Dickins pgoff_t index = 0; 87224513264SHugh Dickins 873105c988fSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 87424513264SHugh Dickins /* 87524513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 87624513264SHugh Dickins */ 877105c988fSMatthew Wilcox (Oracle) while (!mapping_unevictable(mapping) && 878105c988fSMatthew Wilcox (Oracle) filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { 879105c988fSMatthew Wilcox (Oracle) check_move_unevictable_folios(&fbatch); 880105c988fSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 88124513264SHugh Dickins cond_resched(); 88224513264SHugh Dickins } 8837a5d0fbbSHugh Dickins } 8847a5d0fbbSHugh Dickins 885b9a8a419SMatthew Wilcox (Oracle) static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) 88671725ed1SHugh Dickins { 887b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 888b9a8a419SMatthew Wilcox (Oracle) struct page *page; 88971725ed1SHugh Dickins 890b9a8a419SMatthew Wilcox (Oracle) /* 891b9a8a419SMatthew Wilcox (Oracle) * At first avoid shmem_getpage(,,,SGP_READ): that fails 892b9a8a419SMatthew Wilcox (Oracle) * beyond i_size, and reports fallocated pages as holes. 893b9a8a419SMatthew Wilcox (Oracle) */ 894b9a8a419SMatthew Wilcox (Oracle) folio = __filemap_get_folio(inode->i_mapping, index, 895b9a8a419SMatthew Wilcox (Oracle) FGP_ENTRY | FGP_LOCK, 0); 896b9a8a419SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 897b9a8a419SMatthew Wilcox (Oracle) return folio; 898b9a8a419SMatthew Wilcox (Oracle) /* 899b9a8a419SMatthew Wilcox (Oracle) * But read a page back from swap if any of it is within i_size 900b9a8a419SMatthew Wilcox (Oracle) * (although in some cases this is just a waste of time). 901b9a8a419SMatthew Wilcox (Oracle) */ 902b9a8a419SMatthew Wilcox (Oracle) page = NULL; 903b9a8a419SMatthew Wilcox (Oracle) shmem_getpage(inode, index, &page, SGP_READ); 904b9a8a419SMatthew Wilcox (Oracle) return page ? page_folio(page) : NULL; 90571725ed1SHugh Dickins } 90671725ed1SHugh Dickins 90771725ed1SHugh Dickins /* 9087f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 9091635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 9107a5d0fbbSHugh Dickins */ 9111635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 9121635f6a7SHugh Dickins bool unfalloc) 9131da177e4SLinus Torvalds { 914285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 9151da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 91609cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 91709cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 9180e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 9197a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 920b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 921b9a8a419SMatthew Wilcox (Oracle) bool same_folio; 9227a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 923285b2c4fSHugh Dickins pgoff_t index; 924bda97eabSHugh Dickins int i; 9251da177e4SLinus Torvalds 92683e4fa9cSHugh Dickins if (lend == -1) 92783e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 928bda97eabSHugh Dickins 929d144bf62SHugh Dickins if (info->fallocend > start && info->fallocend <= end && !unfalloc) 930d144bf62SHugh Dickins info->fallocend = start; 931d144bf62SHugh Dickins 93251dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 933bda97eabSHugh Dickins index = start; 9345c211ba2SMatthew Wilcox (Oracle) while (index < end && find_lock_entries(mapping, index, end - 1, 93551dcbdacSMatthew Wilcox (Oracle) &fbatch, indices)) { 93651dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 937b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 938bda97eabSHugh Dickins 9397a5d0fbbSHugh Dickins index = indices[i]; 940bda97eabSHugh Dickins 9417b774aabSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 9421635f6a7SHugh Dickins if (unfalloc) 9431635f6a7SHugh Dickins continue; 9447a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 9457b774aabSMatthew Wilcox (Oracle) index, folio); 9467a5d0fbbSHugh Dickins continue; 9477a5d0fbbSHugh Dickins } 9487b774aabSMatthew Wilcox (Oracle) index += folio_nr_pages(folio) - 1; 9497a5d0fbbSHugh Dickins 9507b774aabSMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) 9511e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 9527b774aabSMatthew Wilcox (Oracle) folio_unlock(folio); 953bda97eabSHugh Dickins } 95451dcbdacSMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 95551dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 956bda97eabSHugh Dickins cond_resched(); 957bda97eabSHugh Dickins index++; 958bda97eabSHugh Dickins } 959bda97eabSHugh Dickins 960b9a8a419SMatthew Wilcox (Oracle) same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 961b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT); 962b9a8a419SMatthew Wilcox (Oracle) if (folio) { 963b9a8a419SMatthew Wilcox (Oracle) same_folio = lend < folio_pos(folio) + folio_size(folio); 964b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 965b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) { 966b9a8a419SMatthew Wilcox (Oracle) start = folio->index + folio_nr_pages(folio); 967b9a8a419SMatthew Wilcox (Oracle) if (same_folio) 968b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 96983e4fa9cSHugh Dickins } 970b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 971b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 972b9a8a419SMatthew Wilcox (Oracle) folio = NULL; 973bda97eabSHugh Dickins } 974b9a8a419SMatthew Wilcox (Oracle) 975b9a8a419SMatthew Wilcox (Oracle) if (!same_folio) 976b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT); 977b9a8a419SMatthew Wilcox (Oracle) if (folio) { 978b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 979b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) 980b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 981b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 982b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 983bda97eabSHugh Dickins } 984bda97eabSHugh Dickins 985bda97eabSHugh Dickins index = start; 986b1a36650SHugh Dickins while (index < end) { 987bda97eabSHugh Dickins cond_resched(); 9880cd6144aSJohannes Weiner 9890e499ed3SMatthew Wilcox (Oracle) if (!find_get_entries(mapping, index, end - 1, &fbatch, 990cf2039afSMatthew Wilcox (Oracle) indices)) { 991b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 992b1a36650SHugh Dickins if (index == start || end != -1) 993bda97eabSHugh Dickins break; 994b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 995bda97eabSHugh Dickins index = start; 996bda97eabSHugh Dickins continue; 997bda97eabSHugh Dickins } 9980e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 999b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 1000bda97eabSHugh Dickins 10017a5d0fbbSHugh Dickins index = indices[i]; 10020e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 10031635f6a7SHugh Dickins if (unfalloc) 10041635f6a7SHugh Dickins continue; 10050e499ed3SMatthew Wilcox (Oracle) if (shmem_free_swap(mapping, index, folio)) { 1006b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 1007b1a36650SHugh Dickins index--; 1008b1a36650SHugh Dickins break; 1009b1a36650SHugh Dickins } 1010b1a36650SHugh Dickins nr_swaps_freed++; 10117a5d0fbbSHugh Dickins continue; 10127a5d0fbbSHugh Dickins } 10137a5d0fbbSHugh Dickins 10140e499ed3SMatthew Wilcox (Oracle) folio_lock(folio); 1015800d8c63SKirill A. Shutemov 10160e499ed3SMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) { 10170e499ed3SMatthew Wilcox (Oracle) if (folio_mapping(folio) != mapping) { 1018b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 10190e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1020b1a36650SHugh Dickins index--; 1021b1a36650SHugh Dickins break; 10227a5d0fbbSHugh Dickins } 10230e499ed3SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_writeback(folio), 10240e499ed3SMatthew Wilcox (Oracle) folio); 10250e499ed3SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 102671725ed1SHugh Dickins } 1027b9a8a419SMatthew Wilcox (Oracle) index = folio->index + folio_nr_pages(folio) - 1; 10280e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1029bda97eabSHugh Dickins } 10300e499ed3SMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 10310e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1032bda97eabSHugh Dickins index++; 1033bda97eabSHugh Dickins } 103494c1e62dSHugh Dickins 10354595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 10367a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 10371da177e4SLinus Torvalds shmem_recalc_inode(inode); 10384595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 10391635f6a7SHugh Dickins } 10401da177e4SLinus Torvalds 10411635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 10421635f6a7SHugh Dickins { 10431635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 1044078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 10451da177e4SLinus Torvalds } 104694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 10471da177e4SLinus Torvalds 1048549c7297SChristian Brauner static int shmem_getattr(struct user_namespace *mnt_userns, 1049549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1050a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 105144a30220SYu Zhao { 1052a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 105344a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 105444a30220SYu Zhao 1055d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 10564595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 105744a30220SYu Zhao shmem_recalc_inode(inode); 10584595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1059d0424c42SHugh Dickins } 1060e408e695STheodore Ts'o if (info->fsflags & FS_APPEND_FL) 1061e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_APPEND; 1062e408e695STheodore Ts'o if (info->fsflags & FS_IMMUTABLE_FL) 1063e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_IMMUTABLE; 1064e408e695STheodore Ts'o if (info->fsflags & FS_NODUMP_FL) 1065e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_NODUMP; 1066e408e695STheodore Ts'o stat->attributes_mask |= (STATX_ATTR_APPEND | 1067e408e695STheodore Ts'o STATX_ATTR_IMMUTABLE | 1068e408e695STheodore Ts'o STATX_ATTR_NODUMP); 10690d56a451SChristian Brauner generic_fillattr(&init_user_ns, inode, stat); 107089fdcd26SYang Shi 1071a7fddc36SHugh Dickins if (shmem_is_huge(NULL, inode, 0)) 107289fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 107389fdcd26SYang Shi 1074f7cd16a5SXavier Roche if (request_mask & STATX_BTIME) { 1075f7cd16a5SXavier Roche stat->result_mask |= STATX_BTIME; 1076f7cd16a5SXavier Roche stat->btime.tv_sec = info->i_crtime.tv_sec; 1077f7cd16a5SXavier Roche stat->btime.tv_nsec = info->i_crtime.tv_nsec; 1078f7cd16a5SXavier Roche } 1079f7cd16a5SXavier Roche 108044a30220SYu Zhao return 0; 108144a30220SYu Zhao } 108244a30220SYu Zhao 1083549c7297SChristian Brauner static int shmem_setattr(struct user_namespace *mnt_userns, 1084549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 10851da177e4SLinus Torvalds { 108675c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 108740e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 10881da177e4SLinus Torvalds int error; 10891da177e4SLinus Torvalds 10902f221d6fSChristian Brauner error = setattr_prepare(&init_user_ns, dentry, attr); 1091db78b877SChristoph Hellwig if (error) 1092db78b877SChristoph Hellwig return error; 1093db78b877SChristoph Hellwig 109494c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 109594c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 109694c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10973889e6e7Snpiggin@suse.de 10989608703eSJan Kara /* protected by i_rwsem */ 109940e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 110040e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 110140e041a2SDavid Herrmann return -EPERM; 110240e041a2SDavid Herrmann 110394c1e62dSHugh Dickins if (newsize != oldsize) { 110477142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 110577142517SKonstantin Khlebnikov oldsize, newsize); 110677142517SKonstantin Khlebnikov if (error) 110777142517SKonstantin Khlebnikov return error; 110894c1e62dSHugh Dickins i_size_write(inode, newsize); 1109078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 111094c1e62dSHugh Dickins } 1111afa2db2fSJosef Bacik if (newsize <= oldsize) { 111294c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1113d0424c42SHugh Dickins if (oldsize > holebegin) 1114d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1115d0424c42SHugh Dickins holebegin, 0, 1); 1116d0424c42SHugh Dickins if (info->alloced) 1117d0424c42SHugh Dickins shmem_truncate_range(inode, 1118d0424c42SHugh Dickins newsize, (loff_t)-1); 111994c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1120d0424c42SHugh Dickins if (oldsize > holebegin) 1121d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1122d0424c42SHugh Dickins holebegin, 0, 1); 112394c1e62dSHugh Dickins } 11241da177e4SLinus Torvalds } 11251da177e4SLinus Torvalds 11262f221d6fSChristian Brauner setattr_copy(&init_user_ns, inode, attr); 1127db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1128e65ce2a5SChristian Brauner error = posix_acl_chmod(&init_user_ns, inode, inode->i_mode); 11291da177e4SLinus Torvalds return error; 11301da177e4SLinus Torvalds } 11311da177e4SLinus Torvalds 11321f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1135779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 11361da177e4SLinus Torvalds 113730e6a51dSHui Su if (shmem_mapping(inode->i_mapping)) { 11381da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 11391da177e4SLinus Torvalds inode->i_size = 0; 1140bc786390SHugh Dickins mapping_set_exiting(inode->i_mapping); 11413889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1142779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1143779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1144779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1145779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1146779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1147779750d2SKirill A. Shutemov } 1148779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1149779750d2SKirill A. Shutemov } 1150af53d3e9SHugh Dickins while (!list_empty(&info->swaplist)) { 1151af53d3e9SHugh Dickins /* Wait while shmem_unuse() is scanning this inode... */ 1152af53d3e9SHugh Dickins wait_var_event(&info->stop_eviction, 1153af53d3e9SHugh Dickins !atomic_read(&info->stop_eviction)); 1154cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1155af53d3e9SHugh Dickins /* ...but beware of the race if we peeked too early */ 1156af53d3e9SHugh Dickins if (!atomic_read(&info->stop_eviction)) 11571da177e4SLinus Torvalds list_del_init(&info->swaplist); 1158cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 11591da177e4SLinus Torvalds } 11603ed47db3SAl Viro } 1161b09e0fa4SEric Paris 116238f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11630f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11645b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1165dbd5768fSJan Kara clear_inode(inode); 11661da177e4SLinus Torvalds } 11671da177e4SLinus Torvalds 1168b56a2d8aSVineeth Remanan Pillai static int shmem_find_swap_entries(struct address_space *mapping, 1169da08e9b7SMatthew Wilcox (Oracle) pgoff_t start, struct folio_batch *fbatch, 1170da08e9b7SMatthew Wilcox (Oracle) pgoff_t *indices, unsigned int type) 1171478922e2SMatthew Wilcox { 1172b56a2d8aSVineeth Remanan Pillai XA_STATE(xas, &mapping->i_pages, start); 1173da08e9b7SMatthew Wilcox (Oracle) struct folio *folio; 117487039546SHugh Dickins swp_entry_t entry; 1175478922e2SMatthew Wilcox 1176478922e2SMatthew Wilcox rcu_read_lock(); 1177da08e9b7SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { 1178da08e9b7SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) 11795b9c98f3SMike Kravetz continue; 1180b56a2d8aSVineeth Remanan Pillai 1181da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1182478922e2SMatthew Wilcox continue; 1183b56a2d8aSVineeth Remanan Pillai 1184da08e9b7SMatthew Wilcox (Oracle) entry = radix_to_swp_entry(folio); 11856cec2b95SMiaohe Lin /* 11866cec2b95SMiaohe Lin * swapin error entries can be found in the mapping. But they're 11876cec2b95SMiaohe Lin * deliberately ignored here as we've done everything we can do. 11886cec2b95SMiaohe Lin */ 118987039546SHugh Dickins if (swp_type(entry) != type) 1190b56a2d8aSVineeth Remanan Pillai continue; 1191b56a2d8aSVineeth Remanan Pillai 1192e384200eSHugh Dickins indices[folio_batch_count(fbatch)] = xas.xa_index; 1193da08e9b7SMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio)) 1194da08e9b7SMatthew Wilcox (Oracle) break; 1195b56a2d8aSVineeth Remanan Pillai 1196b56a2d8aSVineeth Remanan Pillai if (need_resched()) { 1197e21a2955SMatthew Wilcox xas_pause(&xas); 1198478922e2SMatthew Wilcox cond_resched_rcu(); 1199478922e2SMatthew Wilcox } 1200b56a2d8aSVineeth Remanan Pillai } 1201478922e2SMatthew Wilcox rcu_read_unlock(); 1202e21a2955SMatthew Wilcox 1203da08e9b7SMatthew Wilcox (Oracle) return xas.xa_index; 1204b56a2d8aSVineeth Remanan Pillai } 1205b56a2d8aSVineeth Remanan Pillai 1206b56a2d8aSVineeth Remanan Pillai /* 1207b56a2d8aSVineeth Remanan Pillai * Move the swapped pages for an inode to page cache. Returns the count 1208b56a2d8aSVineeth Remanan Pillai * of pages swapped in, or the error in case of failure. 1209b56a2d8aSVineeth Remanan Pillai */ 1210da08e9b7SMatthew Wilcox (Oracle) static int shmem_unuse_swap_entries(struct inode *inode, 1211da08e9b7SMatthew Wilcox (Oracle) struct folio_batch *fbatch, pgoff_t *indices) 1212b56a2d8aSVineeth Remanan Pillai { 1213b56a2d8aSVineeth Remanan Pillai int i = 0; 1214b56a2d8aSVineeth Remanan Pillai int ret = 0; 1215b56a2d8aSVineeth Remanan Pillai int error = 0; 1216b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1217b56a2d8aSVineeth Remanan Pillai 1218da08e9b7SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++) { 1219da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i]; 1220b56a2d8aSVineeth Remanan Pillai 1221da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1222b56a2d8aSVineeth Remanan Pillai continue; 1223da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, indices[i], 1224da08e9b7SMatthew Wilcox (Oracle) &folio, SGP_CACHE, 1225b56a2d8aSVineeth Remanan Pillai mapping_gfp_mask(mapping), 1226b56a2d8aSVineeth Remanan Pillai NULL, NULL); 1227b56a2d8aSVineeth Remanan Pillai if (error == 0) { 1228da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1229da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1230b56a2d8aSVineeth Remanan Pillai ret++; 1231b56a2d8aSVineeth Remanan Pillai } 1232b56a2d8aSVineeth Remanan Pillai if (error == -ENOMEM) 1233b56a2d8aSVineeth Remanan Pillai break; 1234b56a2d8aSVineeth Remanan Pillai error = 0; 1235b56a2d8aSVineeth Remanan Pillai } 1236b56a2d8aSVineeth Remanan Pillai return error ? error : ret; 1237478922e2SMatthew Wilcox } 1238478922e2SMatthew Wilcox 123946f65ec1SHugh Dickins /* 124046f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 124146f65ec1SHugh Dickins */ 124210a9c496SChristoph Hellwig static int shmem_unuse_inode(struct inode *inode, unsigned int type) 12431da177e4SLinus Torvalds { 1244b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1245b56a2d8aSVineeth Remanan Pillai pgoff_t start = 0; 1246da08e9b7SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1247b56a2d8aSVineeth Remanan Pillai pgoff_t indices[PAGEVEC_SIZE]; 1248b56a2d8aSVineeth Remanan Pillai int ret = 0; 12491da177e4SLinus Torvalds 1250b56a2d8aSVineeth Remanan Pillai do { 1251da08e9b7SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1252da08e9b7SMatthew Wilcox (Oracle) shmem_find_swap_entries(mapping, start, &fbatch, indices, type); 1253da08e9b7SMatthew Wilcox (Oracle) if (folio_batch_count(&fbatch) == 0) { 1254b56a2d8aSVineeth Remanan Pillai ret = 0; 1255778dd893SHugh Dickins break; 1256b56a2d8aSVineeth Remanan Pillai } 1257b56a2d8aSVineeth Remanan Pillai 1258da08e9b7SMatthew Wilcox (Oracle) ret = shmem_unuse_swap_entries(inode, &fbatch, indices); 1259b56a2d8aSVineeth Remanan Pillai if (ret < 0) 1260b56a2d8aSVineeth Remanan Pillai break; 1261b56a2d8aSVineeth Remanan Pillai 1262da08e9b7SMatthew Wilcox (Oracle) start = indices[folio_batch_count(&fbatch) - 1]; 1263b56a2d8aSVineeth Remanan Pillai } while (true); 1264b56a2d8aSVineeth Remanan Pillai 1265b56a2d8aSVineeth Remanan Pillai return ret; 1266b56a2d8aSVineeth Remanan Pillai } 1267b56a2d8aSVineeth Remanan Pillai 1268b56a2d8aSVineeth Remanan Pillai /* 1269b56a2d8aSVineeth Remanan Pillai * Read all the shared memory data that resides in the swap 1270b56a2d8aSVineeth Remanan Pillai * device 'type' back into memory, so the swap device can be 1271b56a2d8aSVineeth Remanan Pillai * unused. 1272b56a2d8aSVineeth Remanan Pillai */ 127310a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 1274b56a2d8aSVineeth Remanan Pillai { 1275b56a2d8aSVineeth Remanan Pillai struct shmem_inode_info *info, *next; 1276b56a2d8aSVineeth Remanan Pillai int error = 0; 1277b56a2d8aSVineeth Remanan Pillai 1278b56a2d8aSVineeth Remanan Pillai if (list_empty(&shmem_swaplist)) 1279b56a2d8aSVineeth Remanan Pillai return 0; 1280b56a2d8aSVineeth Remanan Pillai 1281b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1282b56a2d8aSVineeth Remanan Pillai list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1283b56a2d8aSVineeth Remanan Pillai if (!info->swapped) { 1284b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1285b56a2d8aSVineeth Remanan Pillai continue; 1286b56a2d8aSVineeth Remanan Pillai } 1287af53d3e9SHugh Dickins /* 1288af53d3e9SHugh Dickins * Drop the swaplist mutex while searching the inode for swap; 1289af53d3e9SHugh Dickins * but before doing so, make sure shmem_evict_inode() will not 1290af53d3e9SHugh Dickins * remove placeholder inode from swaplist, nor let it be freed 1291af53d3e9SHugh Dickins * (igrab() would protect from unlink, but not from unmount). 1292af53d3e9SHugh Dickins */ 1293af53d3e9SHugh Dickins atomic_inc(&info->stop_eviction); 1294b56a2d8aSVineeth Remanan Pillai mutex_unlock(&shmem_swaplist_mutex); 1295b56a2d8aSVineeth Remanan Pillai 129610a9c496SChristoph Hellwig error = shmem_unuse_inode(&info->vfs_inode, type); 1297b56a2d8aSVineeth Remanan Pillai cond_resched(); 1298b56a2d8aSVineeth Remanan Pillai 1299b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1300b56a2d8aSVineeth Remanan Pillai next = list_next_entry(info, swaplist); 1301b56a2d8aSVineeth Remanan Pillai if (!info->swapped) 1302b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1303af53d3e9SHugh Dickins if (atomic_dec_and_test(&info->stop_eviction)) 1304af53d3e9SHugh Dickins wake_up_var(&info->stop_eviction); 1305b56a2d8aSVineeth Remanan Pillai if (error) 1306b56a2d8aSVineeth Remanan Pillai break; 13071da177e4SLinus Torvalds } 1308cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1309778dd893SHugh Dickins 1310778dd893SHugh Dickins return error; 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 13151da177e4SLinus Torvalds */ 13161da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 13171da177e4SLinus Torvalds { 1318e2e3fdc7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 13191da177e4SLinus Torvalds struct shmem_inode_info *info; 13201da177e4SLinus Torvalds struct address_space *mapping; 13211da177e4SLinus Torvalds struct inode *inode; 13226922c0c7SHugh Dickins swp_entry_t swap; 13236922c0c7SHugh Dickins pgoff_t index; 13241da177e4SLinus Torvalds 13251e6decf3SHugh Dickins /* 13261e6decf3SHugh Dickins * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or 13271e6decf3SHugh Dickins * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, 13281e6decf3SHugh Dickins * and its shmem_writeback() needs them to be split when swapping. 13291e6decf3SHugh Dickins */ 1330f530ed0eSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 13311e6decf3SHugh Dickins /* Ensure the subpages are still dirty */ 1332f530ed0eSMatthew Wilcox (Oracle) folio_test_set_dirty(folio); 13331e6decf3SHugh Dickins if (split_huge_page(page) < 0) 13341e6decf3SHugh Dickins goto redirty; 1335f530ed0eSMatthew Wilcox (Oracle) folio = page_folio(page); 1336f530ed0eSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 13371e6decf3SHugh Dickins } 13381e6decf3SHugh Dickins 1339f530ed0eSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 1340f530ed0eSMatthew Wilcox (Oracle) mapping = folio->mapping; 1341f530ed0eSMatthew Wilcox (Oracle) index = folio->index; 13421da177e4SLinus Torvalds inode = mapping->host; 13431da177e4SLinus Torvalds info = SHMEM_I(inode); 13441da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 13451da177e4SLinus Torvalds goto redirty; 1346d9fe526aSHugh Dickins if (!total_swap_pages) 13471da177e4SLinus Torvalds goto redirty; 13481da177e4SLinus Torvalds 1349d9fe526aSHugh Dickins /* 135097b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 135197b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 135297b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 135397b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 135497b713baSChristoph Hellwig * threads or sync. 1355d9fe526aSHugh Dickins */ 135648f170fbSHugh Dickins if (!wbc->for_reclaim) { 135748f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 135848f170fbSHugh Dickins goto redirty; 135948f170fbSHugh Dickins } 13601635f6a7SHugh Dickins 13611635f6a7SHugh Dickins /* 13621635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13631635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 1364f530ed0eSMatthew Wilcox (Oracle) * fallocated folio arriving here is now to initialize it and write it. 13651aac1400SHugh Dickins * 1366f530ed0eSMatthew Wilcox (Oracle) * That's okay for a folio already fallocated earlier, but if we have 13671aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 1368f530ed0eSMatthew Wilcox (Oracle) * of this folio in case we have to undo it, and (b) it may not be a 13691aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 1370f530ed0eSMatthew Wilcox (Oracle) * reactivate the folio, and let shmem_fallocate() quit when too many. 13711635f6a7SHugh Dickins */ 1372f530ed0eSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 13731aac1400SHugh Dickins if (inode->i_private) { 13741aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13751aac1400SHugh Dickins spin_lock(&inode->i_lock); 13761aac1400SHugh Dickins shmem_falloc = inode->i_private; 13771aac1400SHugh Dickins if (shmem_falloc && 13788e205f77SHugh Dickins !shmem_falloc->waitq && 13791aac1400SHugh Dickins index >= shmem_falloc->start && 13801aac1400SHugh Dickins index < shmem_falloc->next) 13811aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13821aac1400SHugh Dickins else 13831aac1400SHugh Dickins shmem_falloc = NULL; 13841aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13851aac1400SHugh Dickins if (shmem_falloc) 13861aac1400SHugh Dickins goto redirty; 13871aac1400SHugh Dickins } 1388f530ed0eSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 1389f530ed0eSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1390f530ed0eSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 13911635f6a7SHugh Dickins } 13921635f6a7SHugh Dickins 1393e2e3fdc7SMatthew Wilcox (Oracle) swap = folio_alloc_swap(folio); 139448f170fbSHugh Dickins if (!swap.val) 139548f170fbSHugh Dickins goto redirty; 1396d9fe526aSHugh Dickins 1397b1dea800SHugh Dickins /* 1398b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1399f530ed0eSMatthew Wilcox (Oracle) * if it's not already there. Do it now before the folio is 14006922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1401b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 14026922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 14036922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1404b1dea800SHugh Dickins */ 1405b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 140605bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 1407b56a2d8aSVineeth Remanan Pillai list_add(&info->swaplist, &shmem_swaplist); 1408b1dea800SHugh Dickins 1409a4c366f0SMatthew Wilcox (Oracle) if (add_to_swap_cache(folio, swap, 14103852f676SJoonsoo Kim __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, 14113852f676SJoonsoo Kim NULL) == 0) { 14124595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1413267a4c76SHugh Dickins shmem_recalc_inode(inode); 1414267a4c76SHugh Dickins info->swapped++; 14154595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1416267a4c76SHugh Dickins 1417aaa46865SHugh Dickins swap_shmem_alloc(swap); 14184cd400fdSMatthew Wilcox (Oracle) shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); 14196922c0c7SHugh Dickins 14206922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1421f530ed0eSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 1422f530ed0eSMatthew Wilcox (Oracle) swap_writepage(&folio->page, wbc); 14231da177e4SLinus Torvalds return 0; 14241da177e4SLinus Torvalds } 14251da177e4SLinus Torvalds 14266922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 14274081f744SMatthew Wilcox (Oracle) put_swap_folio(folio, swap); 14281da177e4SLinus Torvalds redirty: 1429f530ed0eSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1430d9fe526aSHugh Dickins if (wbc->for_reclaim) 1431f530ed0eSMatthew Wilcox (Oracle) return AOP_WRITEPAGE_ACTIVATE; /* Return with folio locked */ 1432f530ed0eSMatthew Wilcox (Oracle) folio_unlock(folio); 1433d9fe526aSHugh Dickins return 0; 14341da177e4SLinus Torvalds } 14351da177e4SLinus Torvalds 143675edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 143771fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1438680d794bSakpm@linux-foundation.org { 1439680d794bSakpm@linux-foundation.org char buffer[64]; 1440680d794bSakpm@linux-foundation.org 144171fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1442095f1fc4SLee Schermerhorn return; /* show nothing */ 1443095f1fc4SLee Schermerhorn 1444a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1445095f1fc4SLee Schermerhorn 1446095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1447680d794bSakpm@linux-foundation.org } 144871fe804bSLee Schermerhorn 144971fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 145071fe804bSLee Schermerhorn { 145171fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 145271fe804bSLee Schermerhorn if (sbinfo->mpol) { 1453bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 145471fe804bSLee Schermerhorn mpol = sbinfo->mpol; 145571fe804bSLee Schermerhorn mpol_get(mpol); 1456bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 145771fe804bSLee Schermerhorn } 145871fe804bSLee Schermerhorn return mpol; 145971fe804bSLee Schermerhorn } 146075edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 146175edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 146275edd345SHugh Dickins { 146375edd345SHugh Dickins } 146475edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 146575edd345SHugh Dickins { 146675edd345SHugh Dickins return NULL; 146775edd345SHugh Dickins } 146875edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 146975edd345SHugh Dickins #ifndef CONFIG_NUMA 147075edd345SHugh Dickins #define vm_policy vm_private_data 147175edd345SHugh Dickins #endif 1472680d794bSakpm@linux-foundation.org 1473800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1474800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1475800d8c63SKirill A. Shutemov { 1476800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 14772c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1478800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1479800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1480800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1481800d8c63SKirill A. Shutemov } 1482800d8c63SKirill A. Shutemov 1483800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1484800d8c63SKirill A. Shutemov { 1485800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1486800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1487800d8c63SKirill A. Shutemov } 1488800d8c63SKirill A. Shutemov 148941ffe5d5SHugh Dickins static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, 149041ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14911da177e4SLinus Torvalds { 14921da177e4SLinus Torvalds struct vm_area_struct pvma; 149318a2f371SMel Gorman struct page *page; 14948c63ca5bSWill Deacon struct vm_fault vmf = { 14958c63ca5bSWill Deacon .vma = &pvma, 14968c63ca5bSWill Deacon }; 14971da177e4SLinus Torvalds 1498800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1499e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1500800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 150118a2f371SMel Gorman 1502800d8c63SKirill A. Shutemov return page; 1503800d8c63SKirill A. Shutemov } 150418a2f371SMel Gorman 150578cc8cdcSRik van Riel /* 150678cc8cdcSRik van Riel * Make sure huge_gfp is always more limited than limit_gfp. 150778cc8cdcSRik van Riel * Some of the flags set permissions, while others set limitations. 150878cc8cdcSRik van Riel */ 150978cc8cdcSRik van Riel static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) 151078cc8cdcSRik van Riel { 151178cc8cdcSRik van Riel gfp_t allowflags = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 151278cc8cdcSRik van Riel gfp_t denyflags = __GFP_NOWARN | __GFP_NORETRY; 1513187df5ddSRik van Riel gfp_t zoneflags = limit_gfp & GFP_ZONEMASK; 1514187df5ddSRik van Riel gfp_t result = huge_gfp & ~(allowflags | GFP_ZONEMASK); 1515187df5ddSRik van Riel 1516187df5ddSRik van Riel /* Allow allocations only from the originally specified zones. */ 1517187df5ddSRik van Riel result |= zoneflags; 151878cc8cdcSRik van Riel 151978cc8cdcSRik van Riel /* 152078cc8cdcSRik van Riel * Minimize the result gfp by taking the union with the deny flags, 152178cc8cdcSRik van Riel * and the intersection of the allow flags. 152278cc8cdcSRik van Riel */ 152378cc8cdcSRik van Riel result |= (limit_gfp & denyflags); 152478cc8cdcSRik van Riel result |= (huge_gfp & limit_gfp) & allowflags; 152578cc8cdcSRik van Riel 152678cc8cdcSRik van Riel return result; 152778cc8cdcSRik van Riel } 152878cc8cdcSRik van Riel 152972827e5cSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_hugefolio(gfp_t gfp, 1530800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1531800d8c63SKirill A. Shutemov { 1532800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 15337b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 15347b8d046fSMatthew Wilcox pgoff_t hindex; 1535dfe98499SMatthew Wilcox (Oracle) struct folio *folio; 1536800d8c63SKirill A. Shutemov 15374620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 15387b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 15397b8d046fSMatthew Wilcox XA_PRESENT)) 1540800d8c63SKirill A. Shutemov return NULL; 1541800d8c63SKirill A. Shutemov 1542800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1543dfe98499SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, &pvma, 0, true); 1544800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1545dfe98499SMatthew Wilcox (Oracle) if (!folio) 1546dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 154772827e5cSMatthew Wilcox (Oracle) return folio; 154818a2f371SMel Gorman } 154918a2f371SMel Gorman 15500c023ef5SMatthew Wilcox (Oracle) static struct folio *shmem_alloc_folio(gfp_t gfp, 155118a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 155218a2f371SMel Gorman { 155318a2f371SMel Gorman struct vm_area_struct pvma; 15540c023ef5SMatthew Wilcox (Oracle) struct folio *folio; 155518a2f371SMel Gorman 1556800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 15570c023ef5SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, 0, &pvma, 0, false); 1558800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 155918a2f371SMel Gorman 15600c023ef5SMatthew Wilcox (Oracle) return folio; 156118a2f371SMel Gorman } 156218a2f371SMel Gorman 1563b1d0ec3aSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, 1564800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1565800d8c63SKirill A. Shutemov { 15660f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 156772827e5cSMatthew Wilcox (Oracle) struct folio *folio; 1568800d8c63SKirill A. Shutemov int nr; 1569800d8c63SKirill A. Shutemov int err = -ENOSPC; 1570800d8c63SKirill A. Shutemov 1571396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1572800d8c63SKirill A. Shutemov huge = false; 1573800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1574800d8c63SKirill A. Shutemov 15750f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1576800d8c63SKirill A. Shutemov goto failed; 1577800d8c63SKirill A. Shutemov 1578800d8c63SKirill A. Shutemov if (huge) 157972827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_hugefolio(gfp, info, index); 1580800d8c63SKirill A. Shutemov else 158172827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, index); 158272827e5cSMatthew Wilcox (Oracle) if (folio) { 158372827e5cSMatthew Wilcox (Oracle) __folio_set_locked(folio); 158472827e5cSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 1585b1d0ec3aSMatthew Wilcox (Oracle) return folio; 158675edd345SHugh Dickins } 158718a2f371SMel Gorman 1588800d8c63SKirill A. Shutemov err = -ENOMEM; 15890f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1590800d8c63SKirill A. Shutemov failed: 1591800d8c63SKirill A. Shutemov return ERR_PTR(err); 15921da177e4SLinus Torvalds } 159371fe804bSLee Schermerhorn 15941da177e4SLinus Torvalds /* 1595bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1596bde05d1cSHugh Dickins * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of 1597bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1598bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1599bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1600bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1601bde05d1cSHugh Dickins * 1602bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1603bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1604bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1605bde05d1cSHugh Dickins */ 1606069d849cSMatthew Wilcox (Oracle) static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) 1607bde05d1cSHugh Dickins { 1608069d849cSMatthew Wilcox (Oracle) return folio_zonenum(folio) > gfp_zone(gfp); 1609bde05d1cSHugh Dickins } 1610bde05d1cSHugh Dickins 1611bde05d1cSHugh Dickins static int shmem_replace_page(struct page **pagep, gfp_t gfp, 1612bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1613bde05d1cSHugh Dickins { 1614d21bba2bSMatthew Wilcox (Oracle) struct folio *old, *new; 1615bde05d1cSHugh Dickins struct address_space *swap_mapping; 1616c1cb20d4SYu Zhao swp_entry_t entry; 1617bde05d1cSHugh Dickins pgoff_t swap_index; 1618bde05d1cSHugh Dickins int error; 1619bde05d1cSHugh Dickins 1620907ea17eSMatthew Wilcox (Oracle) old = page_folio(*pagep); 1621907ea17eSMatthew Wilcox (Oracle) entry = folio_swap_entry(old); 1622c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1623907ea17eSMatthew Wilcox (Oracle) swap_mapping = swap_address_space(entry); 1624bde05d1cSHugh Dickins 1625bde05d1cSHugh Dickins /* 1626bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1627bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1628bde05d1cSHugh Dickins */ 1629bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1630907ea17eSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(old), old); 1631907ea17eSMatthew Wilcox (Oracle) new = shmem_alloc_folio(gfp, info, index); 1632907ea17eSMatthew Wilcox (Oracle) if (!new) 1633bde05d1cSHugh Dickins return -ENOMEM; 1634bde05d1cSHugh Dickins 1635907ea17eSMatthew Wilcox (Oracle) folio_get(new); 1636907ea17eSMatthew Wilcox (Oracle) folio_copy(new, old); 1637907ea17eSMatthew Wilcox (Oracle) flush_dcache_folio(new); 1638bde05d1cSHugh Dickins 1639907ea17eSMatthew Wilcox (Oracle) __folio_set_locked(new); 1640907ea17eSMatthew Wilcox (Oracle) __folio_set_swapbacked(new); 1641907ea17eSMatthew Wilcox (Oracle) folio_mark_uptodate(new); 1642907ea17eSMatthew Wilcox (Oracle) folio_set_swap_entry(new, entry); 1643907ea17eSMatthew Wilcox (Oracle) folio_set_swapcache(new); 1644bde05d1cSHugh Dickins 1645bde05d1cSHugh Dickins /* 1646bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1647bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1648bde05d1cSHugh Dickins */ 1649b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1650907ea17eSMatthew Wilcox (Oracle) error = shmem_replace_entry(swap_mapping, swap_index, old, new); 16510142ef6cSHugh Dickins if (!error) { 1652d21bba2bSMatthew Wilcox (Oracle) mem_cgroup_migrate(old, new); 1653907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1); 1654907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_SHMEM, 1); 1655907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1); 1656907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_SHMEM, -1); 16570142ef6cSHugh Dickins } 1658b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1659bde05d1cSHugh Dickins 16600142ef6cSHugh Dickins if (unlikely(error)) { 16610142ef6cSHugh Dickins /* 16620142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 16630142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 16640142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 16650142ef6cSHugh Dickins */ 1666907ea17eSMatthew Wilcox (Oracle) old = new; 16670142ef6cSHugh Dickins } else { 1668907ea17eSMatthew Wilcox (Oracle) folio_add_lru(new); 1669907ea17eSMatthew Wilcox (Oracle) *pagep = &new->page; 16700142ef6cSHugh Dickins } 1671bde05d1cSHugh Dickins 1672907ea17eSMatthew Wilcox (Oracle) folio_clear_swapcache(old); 1673907ea17eSMatthew Wilcox (Oracle) old->private = NULL; 1674bde05d1cSHugh Dickins 1675907ea17eSMatthew Wilcox (Oracle) folio_unlock(old); 1676907ea17eSMatthew Wilcox (Oracle) folio_put_refs(old, 2); 16770142ef6cSHugh Dickins return error; 1678bde05d1cSHugh Dickins } 1679bde05d1cSHugh Dickins 16806cec2b95SMiaohe Lin static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, 16816cec2b95SMiaohe Lin struct folio *folio, swp_entry_t swap) 16826cec2b95SMiaohe Lin { 16836cec2b95SMiaohe Lin struct address_space *mapping = inode->i_mapping; 16846cec2b95SMiaohe Lin struct shmem_inode_info *info = SHMEM_I(inode); 16856cec2b95SMiaohe Lin swp_entry_t swapin_error; 16866cec2b95SMiaohe Lin void *old; 16876cec2b95SMiaohe Lin 16886cec2b95SMiaohe Lin swapin_error = make_swapin_error_entry(&folio->page); 16896cec2b95SMiaohe Lin old = xa_cmpxchg_irq(&mapping->i_pages, index, 16906cec2b95SMiaohe Lin swp_to_radix_entry(swap), 16916cec2b95SMiaohe Lin swp_to_radix_entry(swapin_error), 0); 16926cec2b95SMiaohe Lin if (old != swp_to_radix_entry(swap)) 16936cec2b95SMiaohe Lin return; 16946cec2b95SMiaohe Lin 16956cec2b95SMiaohe Lin folio_wait_writeback(folio); 169675fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 16976cec2b95SMiaohe Lin spin_lock_irq(&info->lock); 16986cec2b95SMiaohe Lin /* 16996cec2b95SMiaohe Lin * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks won't 17006cec2b95SMiaohe Lin * be 0 when inode is released and thus trigger WARN_ON(inode->i_blocks) in 17016cec2b95SMiaohe Lin * shmem_evict_inode. 17026cec2b95SMiaohe Lin */ 17036cec2b95SMiaohe Lin info->alloced--; 17046cec2b95SMiaohe Lin info->swapped--; 17056cec2b95SMiaohe Lin shmem_recalc_inode(inode); 17066cec2b95SMiaohe Lin spin_unlock_irq(&info->lock); 17076cec2b95SMiaohe Lin swap_free(swap); 17086cec2b95SMiaohe Lin } 17096cec2b95SMiaohe Lin 1710bde05d1cSHugh Dickins /* 1711833de10fSMiaohe Lin * Swap in the folio pointed to by *foliop. 1712833de10fSMiaohe Lin * Caller has to make sure that *foliop contains a valid swapped folio. 1713833de10fSMiaohe Lin * Returns 0 and the folio in foliop if success. On failure, returns the 1714833de10fSMiaohe Lin * error code and NULL in *foliop. 17151da177e4SLinus Torvalds */ 1716da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 1717da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 1718c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 17192b740303SSouptick Joarder vm_fault_t *fault_type) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 172223f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 172304f94e3fSDan Schatzberg struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; 1724b1e1ef34SYang Shi struct page *page; 1725da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = NULL; 17261da177e4SLinus Torvalds swp_entry_t swap; 17271da177e4SLinus Torvalds int error; 17281da177e4SLinus Torvalds 1729da08e9b7SMatthew Wilcox (Oracle) VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); 1730da08e9b7SMatthew Wilcox (Oracle) swap = radix_to_swp_entry(*foliop); 1731da08e9b7SMatthew Wilcox (Oracle) *foliop = NULL; 173254af6042SHugh Dickins 17336cec2b95SMiaohe Lin if (is_swapin_error_entry(swap)) 17346cec2b95SMiaohe Lin return -EIO; 17356cec2b95SMiaohe Lin 17361da177e4SLinus Torvalds /* Look it up and read it in.. */ 1737ec560175SHuang Ying page = lookup_swap_cache(swap, NULL, 0); 173827ab7006SHugh Dickins if (!page) { 17399e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 17409e18eb29SAndres Lagar-Cavilla if (fault_type) { 174168da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 17429e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 17432262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 17449e18eb29SAndres Lagar-Cavilla } 17459e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 174641ffe5d5SHugh Dickins page = shmem_swapin(swap, gfp, info, index); 174727ab7006SHugh Dickins if (!page) { 17481da177e4SLinus Torvalds error = -ENOMEM; 174954af6042SHugh Dickins goto failed; 1750285b2c4fSHugh Dickins } 17511da177e4SLinus Torvalds } 1752da08e9b7SMatthew Wilcox (Oracle) folio = page_folio(page); 17531da177e4SLinus Torvalds 1754833de10fSMiaohe Lin /* We have to do this with folio locked to prevent races */ 1755da08e9b7SMatthew Wilcox (Oracle) folio_lock(folio); 1756da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio) || 1757da08e9b7SMatthew Wilcox (Oracle) folio_swap_entry(folio).val != swap.val || 1758d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1759c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1760d1899228SHugh Dickins goto unlock; 1761bde05d1cSHugh Dickins } 1762da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 17631da177e4SLinus Torvalds error = -EIO; 176454af6042SHugh Dickins goto failed; 176554af6042SHugh Dickins } 1766da08e9b7SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 176754af6042SHugh Dickins 17688a84802eSSteven Price /* 17698a84802eSSteven Price * Some architectures may have to restore extra metadata to the 1770da08e9b7SMatthew Wilcox (Oracle) * folio after reading from swap. 17718a84802eSSteven Price */ 1772da08e9b7SMatthew Wilcox (Oracle) arch_swap_restore(swap, folio); 17738a84802eSSteven Price 1774069d849cSMatthew Wilcox (Oracle) if (shmem_should_replace_folio(folio, gfp)) { 1775bde05d1cSHugh Dickins error = shmem_replace_page(&page, gfp, info, index); 17769dfb3b8dSMatthew Wilcox (Oracle) folio = page_folio(page); 1777bde05d1cSHugh Dickins if (error) 177854af6042SHugh Dickins goto failed; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 1781b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, index, 17823fea5a49SJohannes Weiner swp_to_radix_entry(swap), gfp, 17833fea5a49SJohannes Weiner charge_mm); 178454af6042SHugh Dickins if (error) 178554af6042SHugh Dickins goto failed; 178654af6042SHugh Dickins 17874595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 178854af6042SHugh Dickins info->swapped--; 178954af6042SHugh Dickins shmem_recalc_inode(inode); 17904595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 179127ab7006SHugh Dickins 179266d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1793da08e9b7SMatthew Wilcox (Oracle) folio_mark_accessed(folio); 179466d2f4d2SHugh Dickins 179575fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 1796da08e9b7SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 179727ab7006SHugh Dickins swap_free(swap); 179827ab7006SHugh Dickins 1799da08e9b7SMatthew Wilcox (Oracle) *foliop = folio; 1800c5bf121eSVineeth Remanan Pillai return 0; 1801c5bf121eSVineeth Remanan Pillai failed: 1802c5bf121eSVineeth Remanan Pillai if (!shmem_confirm_swap(mapping, index, swap)) 1803c5bf121eSVineeth Remanan Pillai error = -EEXIST; 18046cec2b95SMiaohe Lin if (error == -EIO) 18056cec2b95SMiaohe Lin shmem_set_folio_swapin_error(inode, index, folio, swap); 1806c5bf121eSVineeth Remanan Pillai unlock: 1807da08e9b7SMatthew Wilcox (Oracle) if (folio) { 1808da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1809da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1810c5bf121eSVineeth Remanan Pillai } 1811c5bf121eSVineeth Remanan Pillai 1812c5bf121eSVineeth Remanan Pillai return error; 1813c5bf121eSVineeth Remanan Pillai } 1814c5bf121eSVineeth Remanan Pillai 1815c5bf121eSVineeth Remanan Pillai /* 1816c5bf121eSVineeth Remanan Pillai * shmem_getpage_gfp - find page in cache, or get from swap, or allocate 1817c5bf121eSVineeth Remanan Pillai * 1818c5bf121eSVineeth Remanan Pillai * If we allocate a new one we do not mark it dirty. That's up to the 1819c5bf121eSVineeth Remanan Pillai * vm. If we swap it in we mark it dirty since we also free the swap 1820c5bf121eSVineeth Remanan Pillai * entry since a page cannot live in both the swap and page cache. 1821c5bf121eSVineeth Remanan Pillai * 1822c949b097SAxel Rasmussen * vma, vmf, and fault_type are only supplied by shmem_fault: 1823c5bf121eSVineeth Remanan Pillai * otherwise they are NULL. 1824c5bf121eSVineeth Remanan Pillai */ 1825c5bf121eSVineeth Remanan Pillai static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, 1826c5bf121eSVineeth Remanan Pillai struct page **pagep, enum sgp_type sgp, gfp_t gfp, 1827c5bf121eSVineeth Remanan Pillai struct vm_area_struct *vma, struct vm_fault *vmf, 1828c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type) 1829c5bf121eSVineeth Remanan Pillai { 1830c5bf121eSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1831c5bf121eSVineeth Remanan Pillai struct shmem_inode_info *info = SHMEM_I(inode); 1832c5bf121eSVineeth Remanan Pillai struct shmem_sb_info *sbinfo; 1833c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm; 1834b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 1835c5bf121eSVineeth Remanan Pillai pgoff_t hindex = index; 1836164cc4feSRik van Riel gfp_t huge_gfp; 1837c5bf121eSVineeth Remanan Pillai int error; 1838c5bf121eSVineeth Remanan Pillai int once = 0; 1839c5bf121eSVineeth Remanan Pillai int alloced = 0; 1840c5bf121eSVineeth Remanan Pillai 1841c5bf121eSVineeth Remanan Pillai if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 1842c5bf121eSVineeth Remanan Pillai return -EFBIG; 1843c5bf121eSVineeth Remanan Pillai repeat: 1844c5bf121eSVineeth Remanan Pillai if (sgp <= SGP_CACHE && 1845c5bf121eSVineeth Remanan Pillai ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1846c5bf121eSVineeth Remanan Pillai return -EINVAL; 1847c5bf121eSVineeth Remanan Pillai } 1848c5bf121eSVineeth Remanan Pillai 1849c5bf121eSVineeth Remanan Pillai sbinfo = SHMEM_SB(inode->i_sb); 185004f94e3fSDan Schatzberg charge_mm = vma ? vma->vm_mm : NULL; 1851c5bf121eSVineeth Remanan Pillai 1852b1d0ec3aSMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0); 1853b1d0ec3aSMatthew Wilcox (Oracle) if (folio && vma && userfaultfd_minor(vma)) { 1854b1d0ec3aSMatthew Wilcox (Oracle) if (!xa_is_value(folio)) { 1855b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1856b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1857c949b097SAxel Rasmussen } 1858c949b097SAxel Rasmussen *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); 1859c949b097SAxel Rasmussen return 0; 1860c949b097SAxel Rasmussen } 1861c949b097SAxel Rasmussen 1862b1d0ec3aSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 1863da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, index, &folio, 1864c5bf121eSVineeth Remanan Pillai sgp, gfp, vma, fault_type); 1865c5bf121eSVineeth Remanan Pillai if (error == -EEXIST) 1866c5bf121eSVineeth Remanan Pillai goto repeat; 1867c5bf121eSVineeth Remanan Pillai 1868da08e9b7SMatthew Wilcox (Oracle) *pagep = &folio->page; 1869c5bf121eSVineeth Remanan Pillai return error; 1870c5bf121eSVineeth Remanan Pillai } 1871c5bf121eSVineeth Remanan Pillai 1872b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 1873b1d0ec3aSMatthew Wilcox (Oracle) hindex = folio->index; 1874acdd9f8eSHugh Dickins if (sgp == SGP_WRITE) 1875b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 1876b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 1877acdd9f8eSHugh Dickins goto out; 1878acdd9f8eSHugh Dickins /* fallocated page */ 1879c5bf121eSVineeth Remanan Pillai if (sgp != SGP_READ) 1880c5bf121eSVineeth Remanan Pillai goto clear; 1881b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1882b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1883c5bf121eSVineeth Remanan Pillai } 1884c5bf121eSVineeth Remanan Pillai 1885c5bf121eSVineeth Remanan Pillai /* 1886acdd9f8eSHugh Dickins * SGP_READ: succeed on hole, with NULL page, letting caller zero. 1887acdd9f8eSHugh Dickins * SGP_NOALLOC: fail on hole, with NULL page, letting caller fail. 1888acdd9f8eSHugh Dickins */ 1889acdd9f8eSHugh Dickins *pagep = NULL; 1890acdd9f8eSHugh Dickins if (sgp == SGP_READ) 1891acdd9f8eSHugh Dickins return 0; 1892acdd9f8eSHugh Dickins if (sgp == SGP_NOALLOC) 1893acdd9f8eSHugh Dickins return -ENOENT; 1894acdd9f8eSHugh Dickins 1895acdd9f8eSHugh Dickins /* 1896acdd9f8eSHugh Dickins * Fast cache lookup and swap lookup did not find it: allocate. 1897c5bf121eSVineeth Remanan Pillai */ 1898c5bf121eSVineeth Remanan Pillai 1899cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1900cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1901cfda0526SMike Rapoport return 0; 1902cfda0526SMike Rapoport } 1903cfda0526SMike Rapoport 19045e6e5a12SHugh Dickins if (!shmem_is_huge(vma, inode, index)) 1905800d8c63SKirill A. Shutemov goto alloc_nohuge; 190627d80fa2SKees Cook 1907164cc4feSRik van Riel huge_gfp = vma_thp_gfp_mask(vma); 190878cc8cdcSRik van Riel huge_gfp = limit_gfp_mask(huge_gfp, gfp); 1909b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(huge_gfp, inode, index, true); 1910b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1911c5bf121eSVineeth Remanan Pillai alloc_nohuge: 1912b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(gfp, inode, index, false); 191354af6042SHugh Dickins } 1914b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1915779750d2SKirill A. Shutemov int retry = 5; 1916c5bf121eSVineeth Remanan Pillai 1917b1d0ec3aSMatthew Wilcox (Oracle) error = PTR_ERR(folio); 1918b1d0ec3aSMatthew Wilcox (Oracle) folio = NULL; 1919779750d2SKirill A. Shutemov if (error != -ENOSPC) 1920c5bf121eSVineeth Remanan Pillai goto unlock; 1921779750d2SKirill A. Shutemov /* 1922c5bf121eSVineeth Remanan Pillai * Try to reclaim some space by splitting a huge page 1923779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1924779750d2SKirill A. Shutemov */ 1925779750d2SKirill A. Shutemov while (retry--) { 1926779750d2SKirill A. Shutemov int ret; 1927c5bf121eSVineeth Remanan Pillai 1928779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1929779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1930779750d2SKirill A. Shutemov break; 1931779750d2SKirill A. Shutemov if (ret) 1932779750d2SKirill A. Shutemov goto alloc_nohuge; 1933779750d2SKirill A. Shutemov } 1934c5bf121eSVineeth Remanan Pillai goto unlock; 1935800d8c63SKirill A. Shutemov } 1936800d8c63SKirill A. Shutemov 1937b1d0ec3aSMatthew Wilcox (Oracle) hindex = round_down(index, folio_nr_pages(folio)); 1938800d8c63SKirill A. Shutemov 193966d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1940b1d0ec3aSMatthew Wilcox (Oracle) __folio_set_referenced(folio); 194166d2f4d2SHugh Dickins 1942b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, hindex, 19433fea5a49SJohannes Weiner NULL, gfp & GFP_RECLAIM_MASK, 19443fea5a49SJohannes Weiner charge_mm); 19453fea5a49SJohannes Weiner if (error) 1946800d8c63SKirill A. Shutemov goto unacct; 1947b1d0ec3aSMatthew Wilcox (Oracle) folio_add_lru(folio); 194854af6042SHugh Dickins 19494595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1950b1d0ec3aSMatthew Wilcox (Oracle) info->alloced += folio_nr_pages(folio); 1951fa020a2bSAndrew Morton inode->i_blocks += (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); 195254af6042SHugh Dickins shmem_recalc_inode(inode); 19534595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19541635f6a7SHugh Dickins alloced = true; 195554af6042SHugh Dickins 1956b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio) && 1957779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1958779750d2SKirill A. Shutemov hindex + HPAGE_PMD_NR - 1) { 1959779750d2SKirill A. Shutemov /* 1960779750d2SKirill A. Shutemov * Part of the huge page is beyond i_size: subject 1961779750d2SKirill A. Shutemov * to shrink under memory pressure. 1962779750d2SKirill A. Shutemov */ 1963779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1964d041353dSCong Wang /* 1965d041353dSCong Wang * _careful to defend against unlocked access to 1966d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1967d041353dSCong Wang */ 1968d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1969779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1970779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1971779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1972779750d2SKirill A. Shutemov } 1973779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1974779750d2SKirill A. Shutemov } 1975779750d2SKirill A. Shutemov 1976ec9516fbSHugh Dickins /* 19771635f6a7SHugh Dickins * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. 19781635f6a7SHugh Dickins */ 19791635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 19801635f6a7SHugh Dickins sgp = SGP_WRITE; 19811635f6a7SHugh Dickins clear: 19821635f6a7SHugh Dickins /* 19831635f6a7SHugh Dickins * Let SGP_WRITE caller clear ends if write does not fill page; 19841635f6a7SHugh Dickins * but SGP_FALLOC on a page fallocated earlier must initialize 19851635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1986ec9516fbSHugh Dickins */ 1987b1d0ec3aSMatthew Wilcox (Oracle) if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) { 1988b1d0ec3aSMatthew Wilcox (Oracle) long i, n = folio_nr_pages(folio); 1989800d8c63SKirill A. Shutemov 1990b1d0ec3aSMatthew Wilcox (Oracle) for (i = 0; i < n; i++) 1991b1d0ec3aSMatthew Wilcox (Oracle) clear_highpage(folio_page(folio, i)); 1992b1d0ec3aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1993b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 1994ec9516fbSHugh Dickins } 1995bde05d1cSHugh Dickins 199654af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 199775edd345SHugh Dickins if (sgp <= SGP_CACHE && 199809cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1999267a4c76SHugh Dickins if (alloced) { 2000b1d0ec3aSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 2001b1d0ec3aSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 20024595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 2003267a4c76SHugh Dickins shmem_recalc_inode(inode); 20044595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 2005267a4c76SHugh Dickins } 200654af6042SHugh Dickins error = -EINVAL; 2007267a4c76SHugh Dickins goto unlock; 2008ff36b801SShaohua Li } 200963ec1973SMatthew Wilcox (Oracle) out: 2010b1d0ec3aSMatthew Wilcox (Oracle) *pagep = folio_page(folio, index - hindex); 201154af6042SHugh Dickins return 0; 2012d00806b1SNick Piggin 2013d0217ac0SNick Piggin /* 201454af6042SHugh Dickins * Error recovery. 20151da177e4SLinus Torvalds */ 201654af6042SHugh Dickins unacct: 2017b1d0ec3aSMatthew Wilcox (Oracle) shmem_inode_unacct_blocks(inode, folio_nr_pages(folio)); 2018800d8c63SKirill A. Shutemov 2019b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 2020b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2021b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 2022800d8c63SKirill A. Shutemov goto alloc_nohuge; 2023800d8c63SKirill A. Shutemov } 2024d1899228SHugh Dickins unlock: 2025b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 2026b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2027b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 202854af6042SHugh Dickins } 202954af6042SHugh Dickins if (error == -ENOSPC && !once++) { 20304595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 203154af6042SHugh Dickins shmem_recalc_inode(inode); 20324595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 20331da177e4SLinus Torvalds goto repeat; 2034d8dc74f2SAdrian Bunk } 20357f4446eeSMatthew Wilcox if (error == -EEXIST) 203654af6042SHugh Dickins goto repeat; 203754af6042SHugh Dickins return error; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 204010d20bd2SLinus Torvalds /* 204110d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 204210d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 204310d20bd2SLinus Torvalds * target. 204410d20bd2SLinus Torvalds */ 2045ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 204610d20bd2SLinus Torvalds { 204710d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 20482055da97SIngo Molnar list_del_init(&wait->entry); 204910d20bd2SLinus Torvalds return ret; 205010d20bd2SLinus Torvalds } 205110d20bd2SLinus Torvalds 205220acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 20531da177e4SLinus Torvalds { 205411bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 2055496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 20569e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 205720acce67SSouptick Joarder int err; 205820acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 20591da177e4SLinus Torvalds 2060f00cdc6dSHugh Dickins /* 2061f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 2062f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 20639608703eSJan Kara * locks writers out with its hold on i_rwsem. So refrain from 20648e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 20658e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 20668e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 20678e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 20688e205f77SHugh Dickins * 20698e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 20708e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 20718e205f77SHugh Dickins * we just need to make racing faults a rare case. 20728e205f77SHugh Dickins * 20738e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 20749608703eSJan Kara * standard mutex or completion: but we cannot take i_rwsem in fault, 20758e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 2076f00cdc6dSHugh Dickins */ 2077f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 2078f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 2079f00cdc6dSHugh Dickins 2080f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2081f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 20828e205f77SHugh Dickins if (shmem_falloc && 20838e205f77SHugh Dickins shmem_falloc->waitq && 20848e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 20858e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 20868897c1b1SKirill A. Shutemov struct file *fpin; 20878e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 208810d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 20898e205f77SHugh Dickins 20908e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 20918897c1b1SKirill A. Shutemov fpin = maybe_unlock_mmap_for_io(vmf, NULL); 20928897c1b1SKirill A. Shutemov if (fpin) 20938e205f77SHugh Dickins ret = VM_FAULT_RETRY; 20948e205f77SHugh Dickins 20958e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 20968e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 20978e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 20988e205f77SHugh Dickins spin_unlock(&inode->i_lock); 20998e205f77SHugh Dickins schedule(); 21008e205f77SHugh Dickins 21018e205f77SHugh Dickins /* 21028e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 21038e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 21048e205f77SHugh Dickins * is usually invalid by the time we reach here, but 21058e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 21068e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 21078e205f77SHugh Dickins */ 21088e205f77SHugh Dickins spin_lock(&inode->i_lock); 21098e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 21108e205f77SHugh Dickins spin_unlock(&inode->i_lock); 21118897c1b1SKirill A. Shutemov 21128897c1b1SKirill A. Shutemov if (fpin) 21138897c1b1SKirill A. Shutemov fput(fpin); 21148e205f77SHugh Dickins return ret; 2115f00cdc6dSHugh Dickins } 21168e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2117f00cdc6dSHugh Dickins } 2118f00cdc6dSHugh Dickins 21195e6e5a12SHugh Dickins err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, SGP_CACHE, 2120cfda0526SMike Rapoport gfp, vma, vmf, &ret); 212120acce67SSouptick Joarder if (err) 212220acce67SSouptick Joarder return vmf_error(err); 212368da9f05SHugh Dickins return ret; 21241da177e4SLinus Torvalds } 21251da177e4SLinus Torvalds 2126c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2127c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2128c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2129c01d5b30SHugh Dickins { 2130c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2131c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2132c01d5b30SHugh Dickins unsigned long addr; 2133c01d5b30SHugh Dickins unsigned long offset; 2134c01d5b30SHugh Dickins unsigned long inflated_len; 2135c01d5b30SHugh Dickins unsigned long inflated_addr; 2136c01d5b30SHugh Dickins unsigned long inflated_offset; 2137c01d5b30SHugh Dickins 2138c01d5b30SHugh Dickins if (len > TASK_SIZE) 2139c01d5b30SHugh Dickins return -ENOMEM; 2140c01d5b30SHugh Dickins 2141c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2142c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2143c01d5b30SHugh Dickins 2144396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2145c01d5b30SHugh Dickins return addr; 2146c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2147c01d5b30SHugh Dickins return addr; 2148c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2149c01d5b30SHugh Dickins return addr; 2150c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2151c01d5b30SHugh Dickins return addr; 2152c01d5b30SHugh Dickins 2153c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2154c01d5b30SHugh Dickins return addr; 2155c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2156c01d5b30SHugh Dickins return addr; 2157c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2158c01d5b30SHugh Dickins return addr; 2159c01d5b30SHugh Dickins /* 2160c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2161c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 216299158997SKirill A. Shutemov * But if caller specified an address hint and we allocated area there 216399158997SKirill A. Shutemov * successfully, respect that as before. 2164c01d5b30SHugh Dickins */ 216599158997SKirill A. Shutemov if (uaddr == addr) 2166c01d5b30SHugh Dickins return addr; 2167c01d5b30SHugh Dickins 2168c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2169c01d5b30SHugh Dickins struct super_block *sb; 2170c01d5b30SHugh Dickins 2171c01d5b30SHugh Dickins if (file) { 2172c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2173c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2174c01d5b30SHugh Dickins } else { 2175c01d5b30SHugh Dickins /* 2176c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2177c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2178c01d5b30SHugh Dickins */ 2179c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2180c01d5b30SHugh Dickins return addr; 2181c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2182c01d5b30SHugh Dickins } 21833089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2184c01d5b30SHugh Dickins return addr; 2185c01d5b30SHugh Dickins } 2186c01d5b30SHugh Dickins 2187c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2188c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2189c01d5b30SHugh Dickins return addr; 2190c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2191c01d5b30SHugh Dickins return addr; 2192c01d5b30SHugh Dickins 2193c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2194c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2195c01d5b30SHugh Dickins return addr; 2196c01d5b30SHugh Dickins if (inflated_len < len) 2197c01d5b30SHugh Dickins return addr; 2198c01d5b30SHugh Dickins 219999158997SKirill A. Shutemov inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); 2200c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2201c01d5b30SHugh Dickins return addr; 2202c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2203c01d5b30SHugh Dickins return addr; 2204c01d5b30SHugh Dickins 2205c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2206c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2207c01d5b30SHugh Dickins if (inflated_offset > offset) 2208c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2209c01d5b30SHugh Dickins 2210c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2211c01d5b30SHugh Dickins return addr; 2212c01d5b30SHugh Dickins return inflated_addr; 2213c01d5b30SHugh Dickins } 2214c01d5b30SHugh Dickins 22151da177e4SLinus Torvalds #ifdef CONFIG_NUMA 221641ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 22171da177e4SLinus Torvalds { 2218496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 221941ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 22201da177e4SLinus Torvalds } 22211da177e4SLinus Torvalds 2222d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2223d8dc74f2SAdrian Bunk unsigned long addr) 22241da177e4SLinus Torvalds { 2225496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 222641ffe5d5SHugh Dickins pgoff_t index; 22271da177e4SLinus Torvalds 222841ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 222941ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 22301da177e4SLinus Torvalds } 22311da177e4SLinus Torvalds #endif 22321da177e4SLinus Torvalds 2233d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 22341da177e4SLinus Torvalds { 2235496ad9aaSAl Viro struct inode *inode = file_inode(file); 22361da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 22371da177e4SLinus Torvalds int retval = -ENOMEM; 22381da177e4SLinus Torvalds 2239ea0dfeb4SHugh Dickins /* 2240ea0dfeb4SHugh Dickins * What serializes the accesses to info->flags? 2241ea0dfeb4SHugh Dickins * ipc_lock_object() when called from shmctl_do_lock(), 2242ea0dfeb4SHugh Dickins * no serialization needed when called from shm_destroy(). 2243ea0dfeb4SHugh Dickins */ 22441da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 2245d7c9e99aSAlexey Gladkov if (!user_shm_lock(inode->i_size, ucounts)) 22461da177e4SLinus Torvalds goto out_nomem; 22471da177e4SLinus Torvalds info->flags |= VM_LOCKED; 224889e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 22491da177e4SLinus Torvalds } 2250d7c9e99aSAlexey Gladkov if (!lock && (info->flags & VM_LOCKED) && ucounts) { 2251d7c9e99aSAlexey Gladkov user_shm_unlock(inode->i_size, ucounts); 22521da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 225389e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 22541da177e4SLinus Torvalds } 22551da177e4SLinus Torvalds retval = 0; 225689e004eaSLee Schermerhorn 22571da177e4SLinus Torvalds out_nomem: 22581da177e4SLinus Torvalds return retval; 22591da177e4SLinus Torvalds } 22601da177e4SLinus Torvalds 22619b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 22621da177e4SLinus Torvalds { 2263ab3948f5SJoel Fernandes (Google) struct shmem_inode_info *info = SHMEM_I(file_inode(file)); 226422247efdSPeter Xu int ret; 2265ab3948f5SJoel Fernandes (Google) 226622247efdSPeter Xu ret = seal_check_future_write(info->seals, vma); 226722247efdSPeter Xu if (ret) 226822247efdSPeter Xu return ret; 2269ab3948f5SJoel Fernandes (Google) 227051b0bff2SCatalin Marinas /* arm64 - allow memory tagging on RAM-based files */ 227151b0bff2SCatalin Marinas vma->vm_flags |= VM_MTE_ALLOWED; 227251b0bff2SCatalin Marinas 22731da177e4SLinus Torvalds file_accessed(file); 22741da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 22751da177e4SLinus Torvalds return 0; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 2278cb241339SHugh Dickins #ifdef CONFIG_TMPFS_XATTR 2279cb241339SHugh Dickins static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 2280cb241339SHugh Dickins 2281cb241339SHugh Dickins /* 2282cb241339SHugh Dickins * chattr's fsflags are unrelated to extended attributes, 2283cb241339SHugh Dickins * but tmpfs has chosen to enable them under the same config option. 2284cb241339SHugh Dickins */ 2285cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2286e408e695STheodore Ts'o { 2287cb241339SHugh Dickins unsigned int i_flags = 0; 2288cb241339SHugh Dickins 2289cb241339SHugh Dickins if (fsflags & FS_NOATIME_FL) 2290cb241339SHugh Dickins i_flags |= S_NOATIME; 2291cb241339SHugh Dickins if (fsflags & FS_APPEND_FL) 2292cb241339SHugh Dickins i_flags |= S_APPEND; 2293cb241339SHugh Dickins if (fsflags & FS_IMMUTABLE_FL) 2294cb241339SHugh Dickins i_flags |= S_IMMUTABLE; 2295cb241339SHugh Dickins /* 2296cb241339SHugh Dickins * But FS_NODUMP_FL does not require any action in i_flags. 2297cb241339SHugh Dickins */ 2298cb241339SHugh Dickins inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE); 2299e408e695STheodore Ts'o } 2300cb241339SHugh Dickins #else 2301cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2302cb241339SHugh Dickins { 2303cb241339SHugh Dickins } 2304cb241339SHugh Dickins #define shmem_initxattrs NULL 2305cb241339SHugh Dickins #endif 2306e408e695STheodore Ts'o 2307e408e695STheodore Ts'o static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir, 230809208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 23091da177e4SLinus Torvalds { 23101da177e4SLinus Torvalds struct inode *inode; 23111da177e4SLinus Torvalds struct shmem_inode_info *info; 23121da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2313e809d5f0SChris Down ino_t ino; 23141da177e4SLinus Torvalds 2315e809d5f0SChris Down if (shmem_reserve_inode(sb, &ino)) 23161da177e4SLinus Torvalds return NULL; 23171da177e4SLinus Torvalds 23181da177e4SLinus Torvalds inode = new_inode(sb); 23191da177e4SLinus Torvalds if (inode) { 2320e809d5f0SChris Down inode->i_ino = ino; 232121cb47beSChristian Brauner inode_init_owner(&init_user_ns, inode, dir, mode); 23221da177e4SLinus Torvalds inode->i_blocks = 0; 2323078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 232446c9a946SArnd Bergmann inode->i_generation = prandom_u32(); 23251da177e4SLinus Torvalds info = SHMEM_I(inode); 23261da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 23271da177e4SLinus Torvalds spin_lock_init(&info->lock); 2328af53d3e9SHugh Dickins atomic_set(&info->stop_eviction, 0); 232940e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 23300b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2331f7cd16a5SXavier Roche info->i_crtime = inode->i_mtime; 2332e408e695STheodore Ts'o info->fsflags = (dir == NULL) ? 0 : 2333e408e695STheodore Ts'o SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; 2334cb241339SHugh Dickins if (info->fsflags) 2335cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 2336779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 23371da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 233838f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 233972c04902SAl Viro cache_no_acl(inode); 2340ff36da69SMatthew Wilcox (Oracle) mapping_set_large_folios(inode->i_mapping); 23411da177e4SLinus Torvalds 23421da177e4SLinus Torvalds switch (mode & S_IFMT) { 23431da177e4SLinus Torvalds default: 234439f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 23451da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 23461da177e4SLinus Torvalds break; 23471da177e4SLinus Torvalds case S_IFREG: 234814fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 23491da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 23501da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 235171fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 235271fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 23531da177e4SLinus Torvalds break; 23541da177e4SLinus Torvalds case S_IFDIR: 2355d8c76e6fSDave Hansen inc_nlink(inode); 23561da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 23571da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 23581da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 23591da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 23601da177e4SLinus Torvalds break; 23611da177e4SLinus Torvalds case S_IFLNK: 23621da177e4SLinus Torvalds /* 23631da177e4SLinus Torvalds * Must not load anything in the rbtree, 23641da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 23651da177e4SLinus Torvalds */ 236671fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 23671da177e4SLinus Torvalds break; 23681da177e4SLinus Torvalds } 2369b45d71fbSJoel Fernandes (Google) 2370b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 23715b04c689SPavel Emelyanov } else 23725b04c689SPavel Emelyanov shmem_free_inode(sb); 23731da177e4SLinus Torvalds return inode; 23741da177e4SLinus Torvalds } 23751da177e4SLinus Torvalds 23763460f6e5SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 23773460f6e5SAxel Rasmussen int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 23784c27fe4cSMike Rapoport pmd_t *dst_pmd, 23794c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 23804c27fe4cSMike Rapoport unsigned long dst_addr, 23814c27fe4cSMike Rapoport unsigned long src_addr, 23828ee79edfSPeter Xu bool zeropage, bool wp_copy, 23834c27fe4cSMike Rapoport struct page **pagep) 23844c27fe4cSMike Rapoport { 23854c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 23864c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 23874c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 23884c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 23894c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 23904c27fe4cSMike Rapoport void *page_kaddr; 2391b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 23924c27fe4cSMike Rapoport int ret; 23933460f6e5SAxel Rasmussen pgoff_t max_off; 23944c27fe4cSMike Rapoport 23957ed9d238SAxel Rasmussen if (!shmem_inode_acct_block(inode, 1)) { 23967ed9d238SAxel Rasmussen /* 23977ed9d238SAxel Rasmussen * We may have got a page, returned -ENOENT triggering a retry, 23987ed9d238SAxel Rasmussen * and now we find ourselves with -ENOMEM. Release the page, to 23997ed9d238SAxel Rasmussen * avoid a BUG_ON in our caller. 24007ed9d238SAxel Rasmussen */ 24017ed9d238SAxel Rasmussen if (unlikely(*pagep)) { 24027ed9d238SAxel Rasmussen put_page(*pagep); 24037ed9d238SAxel Rasmussen *pagep = NULL; 24047ed9d238SAxel Rasmussen } 24057d64ae3aSAxel Rasmussen return -ENOMEM; 24067ed9d238SAxel Rasmussen } 24074c27fe4cSMike Rapoport 2408cb658a45SAndrea Arcangeli if (!*pagep) { 24097d64ae3aSAxel Rasmussen ret = -ENOMEM; 2410*7a7256d5SMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, pgoff); 2411*7a7256d5SMatthew Wilcox (Oracle) if (!folio) 24120f079694SMike Rapoport goto out_unacct_blocks; 24134c27fe4cSMike Rapoport 24143460f6e5SAxel Rasmussen if (!zeropage) { /* COPY */ 2415*7a7256d5SMatthew Wilcox (Oracle) page_kaddr = kmap_local_folio(folio, 0); 24168d103963SMike Rapoport ret = copy_from_user(page_kaddr, 24178d103963SMike Rapoport (const void __user *)src_addr, 24184c27fe4cSMike Rapoport PAGE_SIZE); 2419*7a7256d5SMatthew Wilcox (Oracle) kunmap_local(page_kaddr); 24204c27fe4cSMike Rapoport 2421c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 24224c27fe4cSMike Rapoport if (unlikely(ret)) { 2423*7a7256d5SMatthew Wilcox (Oracle) *pagep = &folio->page; 24247d64ae3aSAxel Rasmussen ret = -ENOENT; 24254c27fe4cSMike Rapoport /* don't free the page */ 24267d64ae3aSAxel Rasmussen goto out_unacct_blocks; 24274c27fe4cSMike Rapoport } 242819b482c2SMuchun Song 2429*7a7256d5SMatthew Wilcox (Oracle) flush_dcache_folio(folio); 24303460f6e5SAxel Rasmussen } else { /* ZEROPAGE */ 2431*7a7256d5SMatthew Wilcox (Oracle) clear_user_highpage(&folio->page, dst_addr); 24328d103963SMike Rapoport } 24334c27fe4cSMike Rapoport } else { 2434*7a7256d5SMatthew Wilcox (Oracle) folio = page_folio(*pagep); 2435*7a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(folio), folio); 24364c27fe4cSMike Rapoport *pagep = NULL; 24374c27fe4cSMike Rapoport } 24384c27fe4cSMike Rapoport 2439*7a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_locked(folio)); 2440*7a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_swapbacked(folio)); 2441*7a7256d5SMatthew Wilcox (Oracle) __folio_set_locked(folio); 2442*7a7256d5SMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 2443*7a7256d5SMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 24449cc90c66SAndrea Arcangeli 2445e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2446e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 24473460f6e5SAxel Rasmussen if (unlikely(pgoff >= max_off)) 2448e2a50c1fSAndrea Arcangeli goto out_release; 2449e2a50c1fSAndrea Arcangeli 2450b7dd44a1SMatthew Wilcox (Oracle) ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, 24513fea5a49SJohannes Weiner gfp & GFP_RECLAIM_MASK, dst_mm); 24524c27fe4cSMike Rapoport if (ret) 24534c27fe4cSMike Rapoport goto out_release; 24544c27fe4cSMike Rapoport 24557d64ae3aSAxel Rasmussen ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 2456*7a7256d5SMatthew Wilcox (Oracle) &folio->page, true, wp_copy); 24577d64ae3aSAxel Rasmussen if (ret) 24587d64ae3aSAxel Rasmussen goto out_delete_from_cache; 24594c27fe4cSMike Rapoport 246094b7cc01SYang Shi spin_lock_irq(&info->lock); 24614c27fe4cSMike Rapoport info->alloced++; 24624c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 24634c27fe4cSMike Rapoport shmem_recalc_inode(inode); 246494b7cc01SYang Shi spin_unlock_irq(&info->lock); 24654c27fe4cSMike Rapoport 2466*7a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 24677d64ae3aSAxel Rasmussen return 0; 24687d64ae3aSAxel Rasmussen out_delete_from_cache: 2469*7a7256d5SMatthew Wilcox (Oracle) filemap_remove_folio(folio); 24704c27fe4cSMike Rapoport out_release: 2471*7a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 2472*7a7256d5SMatthew Wilcox (Oracle) folio_put(folio); 24734c27fe4cSMike Rapoport out_unacct_blocks: 24740f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 24757d64ae3aSAxel Rasmussen return ret; 24764c27fe4cSMike Rapoport } 24773460f6e5SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 24788d103963SMike Rapoport 24791da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 248092e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 248169f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 24821da177e4SLinus Torvalds 24831da177e4SLinus Torvalds static int 2484800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 24859d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 2486800d15a5SNick Piggin struct page **pagep, void **fsdata) 24871da177e4SLinus Torvalds { 2488800d15a5SNick Piggin struct inode *inode = mapping->host; 248940e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 249009cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 2491a7605426SYang Shi int ret = 0; 249240e041a2SDavid Herrmann 24939608703eSJan Kara /* i_rwsem is held by caller */ 2494ab3948f5SJoel Fernandes (Google) if (unlikely(info->seals & (F_SEAL_GROW | 2495ab3948f5SJoel Fernandes (Google) F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 2496ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 249740e041a2SDavid Herrmann return -EPERM; 249840e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 249940e041a2SDavid Herrmann return -EPERM; 250040e041a2SDavid Herrmann } 250140e041a2SDavid Herrmann 2502a7605426SYang Shi ret = shmem_getpage(inode, index, pagep, SGP_WRITE); 2503a7605426SYang Shi 2504a7605426SYang Shi if (ret) 2505a7605426SYang Shi return ret; 2506a7605426SYang Shi 2507a7605426SYang Shi if (PageHWPoison(*pagep)) { 2508a7605426SYang Shi unlock_page(*pagep); 2509a7605426SYang Shi put_page(*pagep); 2510a7605426SYang Shi *pagep = NULL; 2511a7605426SYang Shi return -EIO; 2512a7605426SYang Shi } 2513a7605426SYang Shi 2514a7605426SYang Shi return 0; 2515800d15a5SNick Piggin } 2516800d15a5SNick Piggin 2517800d15a5SNick Piggin static int 2518800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2519800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2520800d15a5SNick Piggin struct page *page, void *fsdata) 2521800d15a5SNick Piggin { 2522800d15a5SNick Piggin struct inode *inode = mapping->host; 2523800d15a5SNick Piggin 2524800d15a5SNick Piggin if (pos + copied > inode->i_size) 2525800d15a5SNick Piggin i_size_write(inode, pos + copied); 2526800d15a5SNick Piggin 2527ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2528800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2529800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2530800d8c63SKirill A. Shutemov int i; 2531800d8c63SKirill A. Shutemov 2532800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2533800d8c63SKirill A. Shutemov if (head + i == page) 2534800d8c63SKirill A. Shutemov continue; 2535800d8c63SKirill A. Shutemov clear_highpage(head + i); 2536800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2537800d8c63SKirill A. Shutemov } 2538800d8c63SKirill A. Shutemov } 253909cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 254009cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2541ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 254209cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2543ec9516fbSHugh Dickins } 2544800d8c63SKirill A. Shutemov SetPageUptodate(head); 2545ec9516fbSHugh Dickins } 2546d3602444SHugh Dickins set_page_dirty(page); 25476746aff7SWu Fengguang unlock_page(page); 254809cbfeafSKirill A. Shutemov put_page(page); 2549d3602444SHugh Dickins 2550800d15a5SNick Piggin return copied; 25511da177e4SLinus Torvalds } 25521da177e4SLinus Torvalds 25532ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 25541da177e4SLinus Torvalds { 25556e58e79dSAl Viro struct file *file = iocb->ki_filp; 25566e58e79dSAl Viro struct inode *inode = file_inode(file); 25571da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 255841ffe5d5SHugh Dickins pgoff_t index; 255941ffe5d5SHugh Dickins unsigned long offset; 2560f7c1d074SGeert Uytterhoeven int error = 0; 2561cb66a7a1SAl Viro ssize_t retval = 0; 25626e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2563a0ee5ec5SHugh Dickins 256409cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 256509cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds for (;;) { 25681da177e4SLinus Torvalds struct page *page = NULL; 256941ffe5d5SHugh Dickins pgoff_t end_index; 257041ffe5d5SHugh Dickins unsigned long nr, ret; 25711da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 25721da177e4SLinus Torvalds 257309cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 25741da177e4SLinus Torvalds if (index > end_index) 25751da177e4SLinus Torvalds break; 25761da177e4SLinus Torvalds if (index == end_index) { 257709cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 25781da177e4SLinus Torvalds if (nr <= offset) 25791da177e4SLinus Torvalds break; 25801da177e4SLinus Torvalds } 25811da177e4SLinus Torvalds 258256a8c8ebSHugh Dickins error = shmem_getpage(inode, index, &page, SGP_READ); 25836e58e79dSAl Viro if (error) { 25846e58e79dSAl Viro if (error == -EINVAL) 25856e58e79dSAl Viro error = 0; 25861da177e4SLinus Torvalds break; 25871da177e4SLinus Torvalds } 258875edd345SHugh Dickins if (page) { 2589d3602444SHugh Dickins unlock_page(page); 2590a7605426SYang Shi 2591a7605426SYang Shi if (PageHWPoison(page)) { 2592a7605426SYang Shi put_page(page); 2593a7605426SYang Shi error = -EIO; 2594a7605426SYang Shi break; 2595a7605426SYang Shi } 259675edd345SHugh Dickins } 25971da177e4SLinus Torvalds 25981da177e4SLinus Torvalds /* 25991da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 26009608703eSJan Kara * are called without i_rwsem protection against truncate 26011da177e4SLinus Torvalds */ 260209cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 26031da177e4SLinus Torvalds i_size = i_size_read(inode); 260409cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 26051da177e4SLinus Torvalds if (index == end_index) { 260609cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 26071da177e4SLinus Torvalds if (nr <= offset) { 26081da177e4SLinus Torvalds if (page) 260909cbfeafSKirill A. Shutemov put_page(page); 26101da177e4SLinus Torvalds break; 26111da177e4SLinus Torvalds } 26121da177e4SLinus Torvalds } 26131da177e4SLinus Torvalds nr -= offset; 26141da177e4SLinus Torvalds 26151da177e4SLinus Torvalds if (page) { 26161da177e4SLinus Torvalds /* 26171da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 26181da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 26191da177e4SLinus Torvalds * before reading the page on the kernel side. 26201da177e4SLinus Torvalds */ 26211da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 26221da177e4SLinus Torvalds flush_dcache_page(page); 26231da177e4SLinus Torvalds /* 26241da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 26251da177e4SLinus Torvalds */ 26261da177e4SLinus Torvalds if (!offset) 26271da177e4SLinus Torvalds mark_page_accessed(page); 26281da177e4SLinus Torvalds /* 26291da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 26301da177e4SLinus Torvalds * now we can copy it to user space... 26311da177e4SLinus Torvalds */ 26322ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 26331bdec44bSHugh Dickins put_page(page); 26341bdec44bSHugh Dickins 2635fcb14cb1SAl Viro } else if (user_backed_iter(to)) { 26361bdec44bSHugh Dickins /* 26371bdec44bSHugh Dickins * Copy to user tends to be so well optimized, but 26381bdec44bSHugh Dickins * clear_user() not so much, that it is noticeably 26391bdec44bSHugh Dickins * faster to copy the zero page instead of clearing. 26401bdec44bSHugh Dickins */ 26411bdec44bSHugh Dickins ret = copy_page_to_iter(ZERO_PAGE(0), offset, nr, to); 26421bdec44bSHugh Dickins } else { 26431bdec44bSHugh Dickins /* 26441bdec44bSHugh Dickins * But submitting the same page twice in a row to 26451bdec44bSHugh Dickins * splice() - or others? - can result in confusion: 26461bdec44bSHugh Dickins * so don't attempt that optimization on pipes etc. 26471bdec44bSHugh Dickins */ 26481bdec44bSHugh Dickins ret = iov_iter_zero(nr, to); 26491bdec44bSHugh Dickins } 26501bdec44bSHugh Dickins 26516e58e79dSAl Viro retval += ret; 26521da177e4SLinus Torvalds offset += ret; 265309cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 265409cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 26551da177e4SLinus Torvalds 26562ba5bbedSAl Viro if (!iov_iter_count(to)) 26571da177e4SLinus Torvalds break; 26586e58e79dSAl Viro if (ret < nr) { 26596e58e79dSAl Viro error = -EFAULT; 26606e58e79dSAl Viro break; 26616e58e79dSAl Viro } 26621da177e4SLinus Torvalds cond_resched(); 26631da177e4SLinus Torvalds } 26641da177e4SLinus Torvalds 266509cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 26666e58e79dSAl Viro file_accessed(file); 26676e58e79dSAl Viro return retval ? retval : error; 26681da177e4SLinus Torvalds } 26691da177e4SLinus Torvalds 2670965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2671220f2ac9SHugh Dickins { 2672220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2673220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2674220f2ac9SHugh Dickins 2675965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2676965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2677220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 267841139aa4SMatthew Wilcox (Oracle) if (offset < 0) 267941139aa4SMatthew Wilcox (Oracle) return -ENXIO; 268041139aa4SMatthew Wilcox (Oracle) 26815955102cSAl Viro inode_lock(inode); 26829608703eSJan Kara /* We're holding i_rwsem so we can access i_size directly */ 268341139aa4SMatthew Wilcox (Oracle) offset = mapping_seek_hole_data(mapping, offset, inode->i_size, whence); 2684387aae6fSHugh Dickins if (offset >= 0) 268546a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 26865955102cSAl Viro inode_unlock(inode); 2687220f2ac9SHugh Dickins return offset; 2688220f2ac9SHugh Dickins } 2689220f2ac9SHugh Dickins 269083e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 269183e4fa9cSHugh Dickins loff_t len) 269283e4fa9cSHugh Dickins { 2693496ad9aaSAl Viro struct inode *inode = file_inode(file); 2694e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 269540e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 26961aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2697d144bf62SHugh Dickins pgoff_t start, index, end, undo_fallocend; 2698e2d12e22SHugh Dickins int error; 269983e4fa9cSHugh Dickins 270013ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 270113ace4d0SHugh Dickins return -EOPNOTSUPP; 270213ace4d0SHugh Dickins 27035955102cSAl Viro inode_lock(inode); 270483e4fa9cSHugh Dickins 270583e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 270683e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 270783e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 270883e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 27098e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 271083e4fa9cSHugh Dickins 27119608703eSJan Kara /* protected by i_rwsem */ 2712ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 271340e041a2SDavid Herrmann error = -EPERM; 271440e041a2SDavid Herrmann goto out; 271540e041a2SDavid Herrmann } 271640e041a2SDavid Herrmann 27178e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2718aa71ecd8SChen Jun shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 2719f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2720f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2721f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2722f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2723f00cdc6dSHugh Dickins 272483e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 272583e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 272683e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 272783e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 272883e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 27298e205f77SHugh Dickins 27308e205f77SHugh Dickins spin_lock(&inode->i_lock); 27318e205f77SHugh Dickins inode->i_private = NULL; 27328e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 27332055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 27348e205f77SHugh Dickins spin_unlock(&inode->i_lock); 273583e4fa9cSHugh Dickins error = 0; 27368e205f77SHugh Dickins goto out; 273783e4fa9cSHugh Dickins } 273883e4fa9cSHugh Dickins 2739e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2740e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2741e2d12e22SHugh Dickins if (error) 2742e2d12e22SHugh Dickins goto out; 2743e2d12e22SHugh Dickins 274440e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 274540e041a2SDavid Herrmann error = -EPERM; 274640e041a2SDavid Herrmann goto out; 274740e041a2SDavid Herrmann } 274840e041a2SDavid Herrmann 274909cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 275009cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2751e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2752e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2753e2d12e22SHugh Dickins error = -ENOSPC; 2754e2d12e22SHugh Dickins goto out; 2755e2d12e22SHugh Dickins } 2756e2d12e22SHugh Dickins 27578e205f77SHugh Dickins shmem_falloc.waitq = NULL; 27581aac1400SHugh Dickins shmem_falloc.start = start; 27591aac1400SHugh Dickins shmem_falloc.next = start; 27601aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 27611aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 27621aac1400SHugh Dickins spin_lock(&inode->i_lock); 27631aac1400SHugh Dickins inode->i_private = &shmem_falloc; 27641aac1400SHugh Dickins spin_unlock(&inode->i_lock); 27651aac1400SHugh Dickins 2766d144bf62SHugh Dickins /* 2767d144bf62SHugh Dickins * info->fallocend is only relevant when huge pages might be 2768d144bf62SHugh Dickins * involved: to prevent split_huge_page() freeing fallocated 2769d144bf62SHugh Dickins * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. 2770d144bf62SHugh Dickins */ 2771d144bf62SHugh Dickins undo_fallocend = info->fallocend; 2772d144bf62SHugh Dickins if (info->fallocend < end) 2773d144bf62SHugh Dickins info->fallocend = end; 2774d144bf62SHugh Dickins 2775050dcb5cSHugh Dickins for (index = start; index < end; ) { 2776e2d12e22SHugh Dickins struct page *page; 2777e2d12e22SHugh Dickins 2778e2d12e22SHugh Dickins /* 2779e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2780e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2781e2d12e22SHugh Dickins */ 2782e2d12e22SHugh Dickins if (signal_pending(current)) 2783e2d12e22SHugh Dickins error = -EINTR; 27841aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 27851aac1400SHugh Dickins error = -ENOMEM; 2786e2d12e22SHugh Dickins else 27879e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, index, &page, SGP_FALLOC); 2788e2d12e22SHugh Dickins if (error) { 2789d144bf62SHugh Dickins info->fallocend = undo_fallocend; 27901635f6a7SHugh Dickins /* Remove the !PageUptodate pages we added */ 27917f556567SHugh Dickins if (index > start) { 27921635f6a7SHugh Dickins shmem_undo_range(inode, 279309cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2794b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 27957f556567SHugh Dickins } 27961aac1400SHugh Dickins goto undone; 2797e2d12e22SHugh Dickins } 2798e2d12e22SHugh Dickins 2799050dcb5cSHugh Dickins index++; 2800050dcb5cSHugh Dickins /* 2801050dcb5cSHugh Dickins * Here is a more important optimization than it appears: 2802050dcb5cSHugh Dickins * a second SGP_FALLOC on the same huge page will clear it, 2803050dcb5cSHugh Dickins * making it PageUptodate and un-undoable if we fail later. 2804050dcb5cSHugh Dickins */ 2805050dcb5cSHugh Dickins if (PageTransCompound(page)) { 2806050dcb5cSHugh Dickins index = round_up(index, HPAGE_PMD_NR); 2807050dcb5cSHugh Dickins /* Beware 32-bit wraparound */ 2808050dcb5cSHugh Dickins if (!index) 2809050dcb5cSHugh Dickins index--; 2810050dcb5cSHugh Dickins } 2811050dcb5cSHugh Dickins 2812e2d12e22SHugh Dickins /* 28131aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 28141aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 28151aac1400SHugh Dickins */ 28161aac1400SHugh Dickins if (!PageUptodate(page)) 2817050dcb5cSHugh Dickins shmem_falloc.nr_falloced += index - shmem_falloc.next; 2818050dcb5cSHugh Dickins shmem_falloc.next = index; 28191aac1400SHugh Dickins 28201aac1400SHugh Dickins /* 28211635f6a7SHugh Dickins * If !PageUptodate, leave it that way so that freeable pages 28221635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 28231635f6a7SHugh Dickins * But set_page_dirty so that memory pressure will swap rather 2824e2d12e22SHugh Dickins * than free the pages we are allocating (and SGP_CACHE pages 2825e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2826e2d12e22SHugh Dickins */ 2827e2d12e22SHugh Dickins set_page_dirty(page); 2828e2d12e22SHugh Dickins unlock_page(page); 282909cbfeafSKirill A. Shutemov put_page(page); 2830e2d12e22SHugh Dickins cond_resched(); 2831e2d12e22SHugh Dickins } 2832e2d12e22SHugh Dickins 2833e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2834e2d12e22SHugh Dickins i_size_write(inode, offset + len); 28351aac1400SHugh Dickins undone: 28361aac1400SHugh Dickins spin_lock(&inode->i_lock); 28371aac1400SHugh Dickins inode->i_private = NULL; 28381aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2839e2d12e22SHugh Dickins out: 284015f242bbSHugh Dickins if (!error) 284115f242bbSHugh Dickins file_modified(file); 28425955102cSAl Viro inode_unlock(inode); 284383e4fa9cSHugh Dickins return error; 284483e4fa9cSHugh Dickins } 284583e4fa9cSHugh Dickins 2846726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 28471da177e4SLinus Torvalds { 2848726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 28491da177e4SLinus Torvalds 28501da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 285109cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 28521da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 28530edd73b3SHugh Dickins if (sbinfo->max_blocks) { 28541da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 285541ffe5d5SHugh Dickins buf->f_bavail = 285641ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 285741ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 28580edd73b3SHugh Dickins } 28590edd73b3SHugh Dickins if (sbinfo->max_inodes) { 28601da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 28611da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 28621da177e4SLinus Torvalds } 28631da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 286459cda49eSAmir Goldstein 286559cda49eSAmir Goldstein buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b); 286659cda49eSAmir Goldstein 28671da177e4SLinus Torvalds return 0; 28681da177e4SLinus Torvalds } 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds /* 28711da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 28721da177e4SLinus Torvalds */ 28731da177e4SLinus Torvalds static int 2874549c7297SChristian Brauner shmem_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2875549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 28761da177e4SLinus Torvalds { 28770b0a0806SHugh Dickins struct inode *inode; 28781da177e4SLinus Torvalds int error = -ENOSPC; 28791da177e4SLinus Torvalds 2880454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 28811da177e4SLinus Torvalds if (inode) { 2882feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2883feda821eSChristoph Hellwig if (error) 2884feda821eSChristoph Hellwig goto out_iput; 28852a7dba39SEric Paris error = security_inode_init_security(inode, dir, 28869d8f13baSMimi Zohar &dentry->d_name, 28876d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2888feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2889feda821eSChristoph Hellwig goto out_iput; 289037ec43cdSMimi Zohar 2891718deb6bSAl Viro error = 0; 28921da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2893078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 28941da177e4SLinus Torvalds d_instantiate(dentry, inode); 28951da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds return error; 2898feda821eSChristoph Hellwig out_iput: 2899feda821eSChristoph Hellwig iput(inode); 2900feda821eSChristoph Hellwig return error; 29011da177e4SLinus Torvalds } 29021da177e4SLinus Torvalds 290360545d0dSAl Viro static int 2904549c7297SChristian Brauner shmem_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, 2905549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 290660545d0dSAl Viro { 290760545d0dSAl Viro struct inode *inode; 290860545d0dSAl Viro int error = -ENOSPC; 290960545d0dSAl Viro 291060545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 291160545d0dSAl Viro if (inode) { 291260545d0dSAl Viro error = security_inode_init_security(inode, dir, 291360545d0dSAl Viro NULL, 291460545d0dSAl Viro shmem_initxattrs, NULL); 2915feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2916feda821eSChristoph Hellwig goto out_iput; 2917feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2918feda821eSChristoph Hellwig if (error) 2919feda821eSChristoph Hellwig goto out_iput; 292060545d0dSAl Viro d_tmpfile(dentry, inode); 292160545d0dSAl Viro } 292260545d0dSAl Viro return error; 2923feda821eSChristoph Hellwig out_iput: 2924feda821eSChristoph Hellwig iput(inode); 2925feda821eSChristoph Hellwig return error; 292660545d0dSAl Viro } 292760545d0dSAl Viro 2928549c7297SChristian Brauner static int shmem_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2929549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 29301da177e4SLinus Torvalds { 29311da177e4SLinus Torvalds int error; 29321da177e4SLinus Torvalds 2933549c7297SChristian Brauner if ((error = shmem_mknod(&init_user_ns, dir, dentry, 2934549c7297SChristian Brauner mode | S_IFDIR, 0))) 29351da177e4SLinus Torvalds return error; 2936d8c76e6fSDave Hansen inc_nlink(dir); 29371da177e4SLinus Torvalds return 0; 29381da177e4SLinus Torvalds } 29391da177e4SLinus Torvalds 2940549c7297SChristian Brauner static int shmem_create(struct user_namespace *mnt_userns, struct inode *dir, 2941549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 29421da177e4SLinus Torvalds { 2943549c7297SChristian Brauner return shmem_mknod(&init_user_ns, 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) { 2962e809d5f0SChris Down ret = shmem_reserve_inode(inode->i_sb, NULL); 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 3001549c7297SChristian Brauner static int shmem_whiteout(struct user_namespace *mnt_userns, 3002549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry) 300346fdb794SMiklos Szeredi { 300446fdb794SMiklos Szeredi struct dentry *whiteout; 300546fdb794SMiklos Szeredi int error; 300646fdb794SMiklos Szeredi 300746fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 300846fdb794SMiklos Szeredi if (!whiteout) 300946fdb794SMiklos Szeredi return -ENOMEM; 301046fdb794SMiklos Szeredi 3011549c7297SChristian Brauner error = shmem_mknod(&init_user_ns, old_dir, whiteout, 301246fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 301346fdb794SMiklos Szeredi dput(whiteout); 301446fdb794SMiklos Szeredi if (error) 301546fdb794SMiklos Szeredi return error; 301646fdb794SMiklos Szeredi 301746fdb794SMiklos Szeredi /* 301846fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 301946fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 302046fdb794SMiklos Szeredi * 302146fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 302246fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 302346fdb794SMiklos Szeredi */ 302446fdb794SMiklos Szeredi d_rehash(whiteout); 302546fdb794SMiklos Szeredi return 0; 302646fdb794SMiklos Szeredi } 302746fdb794SMiklos Szeredi 30281da177e4SLinus Torvalds /* 30291da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 30301da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 30311da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 30321da177e4SLinus Torvalds * gets overwritten. 30331da177e4SLinus Torvalds */ 3034549c7297SChristian Brauner static int shmem_rename2(struct user_namespace *mnt_userns, 3035549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry, 3036549c7297SChristian Brauner struct inode *new_dir, struct dentry *new_dentry, 3037549c7297SChristian Brauner unsigned int flags) 30381da177e4SLinus Torvalds { 303975c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30401da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 30411da177e4SLinus Torvalds 304246fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 30433b69ff51SMiklos Szeredi return -EINVAL; 30443b69ff51SMiklos Szeredi 304537456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 30466429e463SLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 304737456771SMiklos Szeredi 30481da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 30491da177e4SLinus Torvalds return -ENOTEMPTY; 30501da177e4SLinus Torvalds 305146fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 305246fdb794SMiklos Szeredi int error; 305346fdb794SMiklos Szeredi 3054549c7297SChristian Brauner error = shmem_whiteout(&init_user_ns, old_dir, old_dentry); 305546fdb794SMiklos Szeredi if (error) 305646fdb794SMiklos Szeredi return error; 305746fdb794SMiklos Szeredi } 305846fdb794SMiklos Szeredi 305975c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 30601da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3061b928095bSMiklos Szeredi if (they_are_dirs) { 306275c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 30639a53c3a7SDave Hansen drop_nlink(old_dir); 3064b928095bSMiklos Szeredi } 30651da177e4SLinus Torvalds } else if (they_are_dirs) { 30669a53c3a7SDave Hansen drop_nlink(old_dir); 3067d8c76e6fSDave Hansen inc_nlink(new_dir); 30681da177e4SLinus Torvalds } 30691da177e4SLinus Torvalds 30701da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 30711da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 30721da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 30731da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3074078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 30751da177e4SLinus Torvalds return 0; 30761da177e4SLinus Torvalds } 30771da177e4SLinus Torvalds 3078549c7297SChristian Brauner static int shmem_symlink(struct user_namespace *mnt_userns, struct inode *dir, 3079549c7297SChristian Brauner struct dentry *dentry, const char *symname) 30801da177e4SLinus Torvalds { 30811da177e4SLinus Torvalds int error; 30821da177e4SLinus Torvalds int len; 30831da177e4SLinus Torvalds struct inode *inode; 30849276aad6SHugh Dickins struct page *page; 30851da177e4SLinus Torvalds 30861da177e4SLinus Torvalds len = strlen(symname) + 1; 308709cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 30881da177e4SLinus Torvalds return -ENAMETOOLONG; 30891da177e4SLinus Torvalds 30900825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 30910825a6f9SJoe Perches VM_NORESERVE); 30921da177e4SLinus Torvalds if (!inode) 30931da177e4SLinus Torvalds return -ENOSPC; 30941da177e4SLinus Torvalds 30959d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 30966d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3097343c3d7fSMateusz Nosek if (error && error != -EOPNOTSUPP) { 3098570bc1c2SStephen Smalley iput(inode); 3099570bc1c2SStephen Smalley return error; 3100570bc1c2SStephen Smalley } 3101570bc1c2SStephen Smalley 31021da177e4SLinus Torvalds inode->i_size = len-1; 310369f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31043ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 31053ed47db3SAl Viro if (!inode->i_link) { 310669f07ec9SHugh Dickins iput(inode); 310769f07ec9SHugh Dickins return -ENOMEM; 310869f07ec9SHugh Dickins } 310969f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 31101da177e4SLinus Torvalds } else { 3111e8ecde25SAl Viro inode_nohighmem(inode); 31129e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_WRITE); 31131da177e4SLinus Torvalds if (error) { 31141da177e4SLinus Torvalds iput(inode); 31151da177e4SLinus Torvalds return error; 31161da177e4SLinus Torvalds } 311714fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 31181da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 311921fc61c7SAl Viro memcpy(page_address(page), symname, len); 3120ec9516fbSHugh Dickins SetPageUptodate(page); 31211da177e4SLinus Torvalds set_page_dirty(page); 31226746aff7SWu Fengguang unlock_page(page); 312309cbfeafSKirill A. Shutemov put_page(page); 31241da177e4SLinus Torvalds } 31251da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3126078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 31271da177e4SLinus Torvalds d_instantiate(dentry, inode); 31281da177e4SLinus Torvalds dget(dentry); 31291da177e4SLinus Torvalds return 0; 31301da177e4SLinus Torvalds } 31311da177e4SLinus Torvalds 3132fceef393SAl Viro static void shmem_put_link(void *arg) 3133fceef393SAl Viro { 3134fceef393SAl Viro mark_page_accessed(arg); 3135fceef393SAl Viro put_page(arg); 3136fceef393SAl Viro } 3137fceef393SAl Viro 31386b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3139fceef393SAl Viro struct inode *inode, 3140fceef393SAl Viro struct delayed_call *done) 31411da177e4SLinus Torvalds { 31421da177e4SLinus Torvalds struct page *page = NULL; 31436b255391SAl Viro int error; 31446a6c9904SAl Viro if (!dentry) { 31456a6c9904SAl Viro page = find_get_page(inode->i_mapping, 0); 31466a6c9904SAl Viro if (!page) 31476b255391SAl Viro return ERR_PTR(-ECHILD); 3148a7605426SYang Shi if (PageHWPoison(page) || 3149a7605426SYang Shi !PageUptodate(page)) { 31506a6c9904SAl Viro put_page(page); 31516a6c9904SAl Viro return ERR_PTR(-ECHILD); 31526a6c9904SAl Viro } 31536a6c9904SAl Viro } else { 31549e18eb29SAndres Lagar-Cavilla error = shmem_getpage(inode, 0, &page, SGP_READ); 3155680baacbSAl Viro if (error) 3156680baacbSAl Viro return ERR_PTR(error); 3157a7605426SYang Shi if (!page) 3158a7605426SYang Shi return ERR_PTR(-ECHILD); 3159a7605426SYang Shi if (PageHWPoison(page)) { 3160a7605426SYang Shi unlock_page(page); 3161a7605426SYang Shi put_page(page); 3162a7605426SYang Shi return ERR_PTR(-ECHILD); 3163a7605426SYang Shi } 3164d3602444SHugh Dickins unlock_page(page); 31651da177e4SLinus Torvalds } 3166fceef393SAl Viro set_delayed_call(done, shmem_put_link, page); 316721fc61c7SAl Viro return page_address(page); 31681da177e4SLinus Torvalds } 31691da177e4SLinus Torvalds 3170b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3171e408e695STheodore Ts'o 3172e408e695STheodore Ts'o static int shmem_fileattr_get(struct dentry *dentry, struct fileattr *fa) 3173e408e695STheodore Ts'o { 3174e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3175e408e695STheodore Ts'o 3176e408e695STheodore Ts'o fileattr_fill_flags(fa, info->fsflags & SHMEM_FL_USER_VISIBLE); 3177e408e695STheodore Ts'o 3178e408e695STheodore Ts'o return 0; 3179e408e695STheodore Ts'o } 3180e408e695STheodore Ts'o 3181e408e695STheodore Ts'o static int shmem_fileattr_set(struct user_namespace *mnt_userns, 3182e408e695STheodore Ts'o struct dentry *dentry, struct fileattr *fa) 3183e408e695STheodore Ts'o { 3184e408e695STheodore Ts'o struct inode *inode = d_inode(dentry); 3185e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(inode); 3186e408e695STheodore Ts'o 3187e408e695STheodore Ts'o if (fileattr_has_fsx(fa)) 3188e408e695STheodore Ts'o return -EOPNOTSUPP; 3189cb241339SHugh Dickins if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) 3190cb241339SHugh Dickins return -EOPNOTSUPP; 3191e408e695STheodore Ts'o 3192e408e695STheodore Ts'o info->fsflags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | 3193e408e695STheodore Ts'o (fa->flags & SHMEM_FL_USER_MODIFIABLE); 3194e408e695STheodore Ts'o 3195cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 3196e408e695STheodore Ts'o inode->i_ctime = current_time(inode); 3197e408e695STheodore Ts'o return 0; 3198e408e695STheodore Ts'o } 3199e408e695STheodore Ts'o 3200b09e0fa4SEric Paris /* 3201b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3202b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3203b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3204b09e0fa4SEric Paris * filesystem level, though. 3205b09e0fa4SEric Paris */ 3206b09e0fa4SEric Paris 32076d9d88d0SJarkko Sakkinen /* 32086d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 32096d9d88d0SJarkko Sakkinen */ 32106d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 32116d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 32126d9d88d0SJarkko Sakkinen void *fs_info) 32136d9d88d0SJarkko Sakkinen { 32146d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 32156d9d88d0SJarkko Sakkinen const struct xattr *xattr; 321638f38657SAristeu Rozanski struct simple_xattr *new_xattr; 32176d9d88d0SJarkko Sakkinen size_t len; 32186d9d88d0SJarkko Sakkinen 32196d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 322038f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32216d9d88d0SJarkko Sakkinen if (!new_xattr) 32226d9d88d0SJarkko Sakkinen return -ENOMEM; 32236d9d88d0SJarkko Sakkinen 32246d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32256d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32266d9d88d0SJarkko Sakkinen GFP_KERNEL); 32276d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32283bef735aSChengguang Xu kvfree(new_xattr); 32296d9d88d0SJarkko Sakkinen return -ENOMEM; 32306d9d88d0SJarkko Sakkinen } 32316d9d88d0SJarkko Sakkinen 32326d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32336d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32346d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32356d9d88d0SJarkko Sakkinen xattr->name, len); 32366d9d88d0SJarkko Sakkinen 323738f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 32386d9d88d0SJarkko Sakkinen } 32396d9d88d0SJarkko Sakkinen 32406d9d88d0SJarkko Sakkinen return 0; 32416d9d88d0SJarkko Sakkinen } 32426d9d88d0SJarkko Sakkinen 3243aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3244b296821aSAl Viro struct dentry *unused, struct inode *inode, 3245b296821aSAl Viro const char *name, void *buffer, size_t size) 3246aa7c5241SAndreas Gruenbacher { 3247b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3248aa7c5241SAndreas Gruenbacher 3249aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3250aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3251aa7c5241SAndreas Gruenbacher } 3252aa7c5241SAndreas Gruenbacher 3253aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 3254e65ce2a5SChristian Brauner struct user_namespace *mnt_userns, 325559301226SAl Viro struct dentry *unused, struct inode *inode, 325659301226SAl Viro const char *name, const void *value, 325759301226SAl Viro size_t size, int flags) 3258aa7c5241SAndreas Gruenbacher { 325959301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3260aa7c5241SAndreas Gruenbacher 3261aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3262a46a2295SDaniel Xu return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL); 3263aa7c5241SAndreas Gruenbacher } 3264aa7c5241SAndreas Gruenbacher 3265aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3266aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3267aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3268aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3269aa7c5241SAndreas Gruenbacher }; 3270aa7c5241SAndreas Gruenbacher 3271aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3272aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3273aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3274aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3275aa7c5241SAndreas Gruenbacher }; 3276aa7c5241SAndreas Gruenbacher 3277b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3278b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3279feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3280feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3281b09e0fa4SEric Paris #endif 3282aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3283aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3284b09e0fa4SEric Paris NULL 3285b09e0fa4SEric Paris }; 3286b09e0fa4SEric Paris 3287b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3288b09e0fa4SEric Paris { 328975c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3290786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3291b09e0fa4SEric Paris } 3292b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3293b09e0fa4SEric Paris 329469f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 3295f7cd16a5SXavier Roche .getattr = shmem_getattr, 32966b255391SAl Viro .get_link = simple_get_link, 3297b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3298b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3299b09e0fa4SEric Paris #endif 33001da177e4SLinus Torvalds }; 33011da177e4SLinus Torvalds 330292e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 3303f7cd16a5SXavier Roche .getattr = shmem_getattr, 33046b255391SAl Viro .get_link = shmem_get_link, 3305b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3306b09e0fa4SEric Paris .listxattr = shmem_listxattr, 330739f0247dSAndreas Gruenbacher #endif 3308b09e0fa4SEric Paris }; 330939f0247dSAndreas Gruenbacher 331091828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 331191828a40SDavid M. Grimes { 331291828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 331391828a40SDavid M. Grimes } 331491828a40SDavid M. Grimes 331591828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 331691828a40SDavid M. Grimes { 331791828a40SDavid M. Grimes __u32 *fh = vfh; 331891828a40SDavid M. Grimes __u64 inum = fh[2]; 331991828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 332091828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 332191828a40SDavid M. Grimes } 332291828a40SDavid M. Grimes 332312ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 332412ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 332512ba780dSAmir Goldstein { 332612ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 332712ba780dSAmir Goldstein 332812ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 332912ba780dSAmir Goldstein } 333012ba780dSAmir Goldstein 333112ba780dSAmir Goldstein 3332480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3333480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 333491828a40SDavid M. Grimes { 333591828a40SDavid M. Grimes struct inode *inode; 3336480b116cSChristoph Hellwig struct dentry *dentry = NULL; 333735c2a7f4SHugh Dickins u64 inum; 333891828a40SDavid M. Grimes 3339480b116cSChristoph Hellwig if (fh_len < 3) 3340480b116cSChristoph Hellwig return NULL; 3341480b116cSChristoph Hellwig 334235c2a7f4SHugh Dickins inum = fid->raw[2]; 334335c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 334435c2a7f4SHugh Dickins 3345480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3346480b116cSChristoph Hellwig shmem_match, fid->raw); 334791828a40SDavid M. Grimes if (inode) { 334812ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 334991828a40SDavid M. Grimes iput(inode); 335091828a40SDavid M. Grimes } 335191828a40SDavid M. Grimes 3352480b116cSChristoph Hellwig return dentry; 335391828a40SDavid M. Grimes } 335491828a40SDavid M. Grimes 3355b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3356b0b0382bSAl Viro struct inode *parent) 335791828a40SDavid M. Grimes { 33585fe0c237SAneesh Kumar K.V if (*len < 3) { 33595fe0c237SAneesh Kumar K.V *len = 3; 336094e07a75SNamjae Jeon return FILEID_INVALID; 33615fe0c237SAneesh Kumar K.V } 336291828a40SDavid M. Grimes 33631d3382cbSAl Viro if (inode_unhashed(inode)) { 336491828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 336591828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 336691828a40SDavid M. Grimes * time, we need a lock to ensure we only try 336791828a40SDavid M. Grimes * to do it once 336891828a40SDavid M. Grimes */ 336991828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 337091828a40SDavid M. Grimes spin_lock(&lock); 33711d3382cbSAl Viro if (inode_unhashed(inode)) 337291828a40SDavid M. Grimes __insert_inode_hash(inode, 337391828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 337491828a40SDavid M. Grimes spin_unlock(&lock); 337591828a40SDavid M. Grimes } 337691828a40SDavid M. Grimes 337791828a40SDavid M. Grimes fh[0] = inode->i_generation; 337891828a40SDavid M. Grimes fh[1] = inode->i_ino; 337991828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 338091828a40SDavid M. Grimes 338191828a40SDavid M. Grimes *len = 3; 338291828a40SDavid M. Grimes return 1; 338391828a40SDavid M. Grimes } 338491828a40SDavid M. Grimes 338539655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 338691828a40SDavid M. Grimes .get_parent = shmem_get_parent, 338791828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3388480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 338991828a40SDavid M. Grimes }; 339091828a40SDavid M. Grimes 3391626c3920SAl Viro enum shmem_param { 3392626c3920SAl Viro Opt_gid, 3393626c3920SAl Viro Opt_huge, 3394626c3920SAl Viro Opt_mode, 3395626c3920SAl Viro Opt_mpol, 3396626c3920SAl Viro Opt_nr_blocks, 3397626c3920SAl Viro Opt_nr_inodes, 3398626c3920SAl Viro Opt_size, 3399626c3920SAl Viro Opt_uid, 3400ea3271f7SChris Down Opt_inode32, 3401ea3271f7SChris Down Opt_inode64, 3402626c3920SAl Viro }; 34031da177e4SLinus Torvalds 34045eede625SAl Viro static const struct constant_table shmem_param_enums_huge[] = { 34052710c957SAl Viro {"never", SHMEM_HUGE_NEVER }, 34062710c957SAl Viro {"always", SHMEM_HUGE_ALWAYS }, 34072710c957SAl Viro {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 34082710c957SAl Viro {"advise", SHMEM_HUGE_ADVISE }, 34092710c957SAl Viro {} 34102710c957SAl Viro }; 34112710c957SAl Viro 3412d7167b14SAl Viro const struct fs_parameter_spec shmem_fs_parameters[] = { 3413626c3920SAl Viro fsparam_u32 ("gid", Opt_gid), 34142710c957SAl Viro fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 3415626c3920SAl Viro fsparam_u32oct("mode", Opt_mode), 3416626c3920SAl Viro fsparam_string("mpol", Opt_mpol), 3417626c3920SAl Viro fsparam_string("nr_blocks", Opt_nr_blocks), 3418626c3920SAl Viro fsparam_string("nr_inodes", Opt_nr_inodes), 3419626c3920SAl Viro fsparam_string("size", Opt_size), 3420626c3920SAl Viro fsparam_u32 ("uid", Opt_uid), 3421ea3271f7SChris Down fsparam_flag ("inode32", Opt_inode32), 3422ea3271f7SChris Down fsparam_flag ("inode64", Opt_inode64), 3423626c3920SAl Viro {} 3424626c3920SAl Viro }; 3425626c3920SAl Viro 3426f3235626SDavid Howells static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 3427626c3920SAl Viro { 3428f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3429626c3920SAl Viro struct fs_parse_result result; 3430e04dc423SAl Viro unsigned long long size; 3431626c3920SAl Viro char *rest; 3432626c3920SAl Viro int opt; 3433626c3920SAl Viro 3434d7167b14SAl Viro opt = fs_parse(fc, shmem_fs_parameters, param, &result); 3435f3235626SDavid Howells if (opt < 0) 3436626c3920SAl Viro return opt; 3437626c3920SAl Viro 3438626c3920SAl Viro switch (opt) { 3439626c3920SAl Viro case Opt_size: 3440626c3920SAl Viro size = memparse(param->string, &rest); 3441e04dc423SAl Viro if (*rest == '%') { 3442e04dc423SAl Viro size <<= PAGE_SHIFT; 3443e04dc423SAl Viro size *= totalram_pages(); 3444e04dc423SAl Viro do_div(size, 100); 3445e04dc423SAl Viro rest++; 3446e04dc423SAl Viro } 3447e04dc423SAl Viro if (*rest) 3448626c3920SAl Viro goto bad_value; 3449e04dc423SAl Viro ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 3450e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3451626c3920SAl Viro break; 3452626c3920SAl Viro case Opt_nr_blocks: 3453626c3920SAl Viro ctx->blocks = memparse(param->string, &rest); 34540c98c8e1SZhaoLong Wang if (*rest || ctx->blocks > S64_MAX) 3455626c3920SAl Viro goto bad_value; 3456e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3457626c3920SAl Viro break; 3458626c3920SAl Viro case Opt_nr_inodes: 3459626c3920SAl Viro ctx->inodes = memparse(param->string, &rest); 3460e04dc423SAl Viro if (*rest) 3461626c3920SAl Viro goto bad_value; 3462e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_INODES; 3463626c3920SAl Viro break; 3464626c3920SAl Viro case Opt_mode: 3465626c3920SAl Viro ctx->mode = result.uint_32 & 07777; 3466626c3920SAl Viro break; 3467626c3920SAl Viro case Opt_uid: 3468626c3920SAl Viro ctx->uid = make_kuid(current_user_ns(), result.uint_32); 3469e04dc423SAl Viro if (!uid_valid(ctx->uid)) 3470626c3920SAl Viro goto bad_value; 3471626c3920SAl Viro break; 3472626c3920SAl Viro case Opt_gid: 3473626c3920SAl Viro ctx->gid = make_kgid(current_user_ns(), result.uint_32); 3474e04dc423SAl Viro if (!gid_valid(ctx->gid)) 3475626c3920SAl Viro goto bad_value; 3476626c3920SAl Viro break; 3477626c3920SAl Viro case Opt_huge: 3478626c3920SAl Viro ctx->huge = result.uint_32; 3479626c3920SAl Viro if (ctx->huge != SHMEM_HUGE_NEVER && 3480396bcc52SMatthew Wilcox (Oracle) !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 3481626c3920SAl Viro has_transparent_hugepage())) 3482626c3920SAl Viro goto unsupported_parameter; 3483e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_HUGE; 3484626c3920SAl Viro break; 3485626c3920SAl Viro case Opt_mpol: 3486626c3920SAl Viro if (IS_ENABLED(CONFIG_NUMA)) { 3487e04dc423SAl Viro mpol_put(ctx->mpol); 3488e04dc423SAl Viro ctx->mpol = NULL; 3489626c3920SAl Viro if (mpol_parse_str(param->string, &ctx->mpol)) 3490626c3920SAl Viro goto bad_value; 3491626c3920SAl Viro break; 3492626c3920SAl Viro } 3493626c3920SAl Viro goto unsupported_parameter; 3494ea3271f7SChris Down case Opt_inode32: 3495ea3271f7SChris Down ctx->full_inums = false; 3496ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3497ea3271f7SChris Down break; 3498ea3271f7SChris Down case Opt_inode64: 3499ea3271f7SChris Down if (sizeof(ino_t) < 8) { 3500ea3271f7SChris Down return invalfc(fc, 3501ea3271f7SChris Down "Cannot use inode64 with <64bit inums in kernel\n"); 3502ea3271f7SChris Down } 3503ea3271f7SChris Down ctx->full_inums = true; 3504ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3505ea3271f7SChris Down break; 3506e04dc423SAl Viro } 3507e04dc423SAl Viro return 0; 3508e04dc423SAl Viro 3509626c3920SAl Viro unsupported_parameter: 3510f35aa2bcSAl Viro return invalfc(fc, "Unsupported parameter '%s'", param->key); 3511626c3920SAl Viro bad_value: 3512f35aa2bcSAl Viro return invalfc(fc, "Bad value for '%s'", param->key); 3513e04dc423SAl Viro } 3514e04dc423SAl Viro 3515f3235626SDavid Howells static int shmem_parse_options(struct fs_context *fc, void *data) 3516e04dc423SAl Viro { 3517f3235626SDavid Howells char *options = data; 3518f3235626SDavid Howells 351933f37c64SAl Viro if (options) { 352033f37c64SAl Viro int err = security_sb_eat_lsm_opts(options, &fc->security); 352133f37c64SAl Viro if (err) 352233f37c64SAl Viro return err; 352333f37c64SAl Viro } 352433f37c64SAl Viro 3525b00dc3adSHugh Dickins while (options != NULL) { 3526626c3920SAl Viro char *this_char = options; 3527b00dc3adSHugh Dickins for (;;) { 3528b00dc3adSHugh Dickins /* 3529b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3530b00dc3adSHugh Dickins * mount options form a comma-separated list, 3531b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3532b00dc3adSHugh Dickins */ 3533b00dc3adSHugh Dickins options = strchr(options, ','); 3534b00dc3adSHugh Dickins if (options == NULL) 3535b00dc3adSHugh Dickins break; 3536b00dc3adSHugh Dickins options++; 3537b00dc3adSHugh Dickins if (!isdigit(*options)) { 3538b00dc3adSHugh Dickins options[-1] = '\0'; 3539b00dc3adSHugh Dickins break; 3540b00dc3adSHugh Dickins } 3541b00dc3adSHugh Dickins } 3542626c3920SAl Viro if (*this_char) { 3543626c3920SAl Viro char *value = strchr(this_char, '='); 3544f3235626SDavid Howells size_t len = 0; 3545626c3920SAl Viro int err; 3546626c3920SAl Viro 3547626c3920SAl Viro if (value) { 3548626c3920SAl Viro *value++ = '\0'; 3549f3235626SDavid Howells len = strlen(value); 35501da177e4SLinus Torvalds } 3551f3235626SDavid Howells err = vfs_parse_fs_string(fc, this_char, value, len); 3552f3235626SDavid Howells if (err < 0) 3553f3235626SDavid Howells return err; 35541da177e4SLinus Torvalds } 3555626c3920SAl Viro } 35561da177e4SLinus Torvalds return 0; 35571da177e4SLinus Torvalds } 35581da177e4SLinus Torvalds 3559f3235626SDavid Howells /* 3560f3235626SDavid Howells * Reconfigure a shmem filesystem. 3561f3235626SDavid Howells * 3562f3235626SDavid Howells * Note that we disallow change from limited->unlimited blocks/inodes while any 3563f3235626SDavid Howells * are in use; but we must separately disallow unlimited->limited, because in 3564f3235626SDavid Howells * that case we have no record of how much is already in use. 3565f3235626SDavid Howells */ 3566f3235626SDavid Howells static int shmem_reconfigure(struct fs_context *fc) 35671da177e4SLinus Torvalds { 3568f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3569f3235626SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 35700edd73b3SHugh Dickins unsigned long inodes; 3571bf11b9a8SSebastian Andrzej Siewior struct mempolicy *mpol = NULL; 3572f3235626SDavid Howells const char *err; 35730edd73b3SHugh Dickins 3574bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 35750edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 35760c98c8e1SZhaoLong Wang 3577f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 3578f3235626SDavid Howells if (!sbinfo->max_blocks) { 3579f3235626SDavid Howells err = "Cannot retroactively limit size"; 35800edd73b3SHugh Dickins goto out; 35810b5071ddSAl Viro } 3582f3235626SDavid Howells if (percpu_counter_compare(&sbinfo->used_blocks, 3583f3235626SDavid Howells ctx->blocks) > 0) { 3584f3235626SDavid Howells err = "Too small a size for current use"; 35850b5071ddSAl Viro goto out; 3586f3235626SDavid Howells } 3587f3235626SDavid Howells } 3588f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 3589f3235626SDavid Howells if (!sbinfo->max_inodes) { 3590f3235626SDavid Howells err = "Cannot retroactively limit inodes"; 35910b5071ddSAl Viro goto out; 35920b5071ddSAl Viro } 3593f3235626SDavid Howells if (ctx->inodes < inodes) { 3594f3235626SDavid Howells err = "Too few inodes for current use"; 3595f3235626SDavid Howells goto out; 3596f3235626SDavid Howells } 3597f3235626SDavid Howells } 35980edd73b3SHugh Dickins 3599ea3271f7SChris Down if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && 3600ea3271f7SChris Down sbinfo->next_ino > UINT_MAX) { 3601ea3271f7SChris Down err = "Current inum too high to switch to 32-bit inums"; 3602ea3271f7SChris Down goto out; 3603ea3271f7SChris Down } 3604ea3271f7SChris Down 3605f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_HUGE) 3606f3235626SDavid Howells sbinfo->huge = ctx->huge; 3607ea3271f7SChris Down if (ctx->seen & SHMEM_SEEN_INUMS) 3608ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3609f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_BLOCKS) 3610f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3611f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_INODES) { 3612f3235626SDavid Howells sbinfo->max_inodes = ctx->inodes; 3613f3235626SDavid Howells sbinfo->free_inodes = ctx->inodes - inodes; 36140b5071ddSAl Viro } 361571fe804bSLee Schermerhorn 36165f00110fSGreg Thelen /* 36175f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 36185f00110fSGreg Thelen */ 3619f3235626SDavid Howells if (ctx->mpol) { 3620bf11b9a8SSebastian Andrzej Siewior mpol = sbinfo->mpol; 3621f3235626SDavid Howells sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 3622f3235626SDavid Howells ctx->mpol = NULL; 36235f00110fSGreg Thelen } 3624bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3625bf11b9a8SSebastian Andrzej Siewior mpol_put(mpol); 3626f3235626SDavid Howells return 0; 36270edd73b3SHugh Dickins out: 3628bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3629f35aa2bcSAl Viro return invalfc(fc, "%s", err); 36301da177e4SLinus Torvalds } 3631680d794bSakpm@linux-foundation.org 363234c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3633680d794bSakpm@linux-foundation.org { 363434c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3635680d794bSakpm@linux-foundation.org 3636680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3637680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 363809cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3639680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3640680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 36410825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 364209208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 36438751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 36448751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 36458751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 36468751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 36478751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 36488751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3649ea3271f7SChris Down 3650ea3271f7SChris Down /* 3651ea3271f7SChris Down * Showing inode{64,32} might be useful even if it's the system default, 3652ea3271f7SChris Down * since then people don't have to resort to checking both here and 3653ea3271f7SChris Down * /proc/config.gz to confirm 64-bit inums were successfully applied 3654ea3271f7SChris Down * (which may not even exist if IKCONFIG_PROC isn't enabled). 3655ea3271f7SChris Down * 3656ea3271f7SChris Down * We hide it when inode64 isn't the default and we are using 32-bit 3657ea3271f7SChris Down * inodes, since that probably just means the feature isn't even under 3658ea3271f7SChris Down * consideration. 3659ea3271f7SChris Down * 3660ea3271f7SChris Down * As such: 3661ea3271f7SChris Down * 3662ea3271f7SChris Down * +-----------------+-----------------+ 3663ea3271f7SChris Down * | TMPFS_INODE64=y | TMPFS_INODE64=n | 3664ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3665ea3271f7SChris Down * | full_inums=true | show | show | 3666ea3271f7SChris Down * | full_inums=false | show | hide | 3667ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3668ea3271f7SChris Down * 3669ea3271f7SChris Down */ 3670ea3271f7SChris Down if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) 3671ea3271f7SChris Down seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); 3672396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 36735a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 36745a6e75f8SKirill A. Shutemov if (sbinfo->huge) 36755a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 36765a6e75f8SKirill A. Shutemov #endif 367771fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3678680d794bSakpm@linux-foundation.org return 0; 3679680d794bSakpm@linux-foundation.org } 36809183df25SDavid Herrmann 3681680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 36821da177e4SLinus Torvalds 36831da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 36841da177e4SLinus Torvalds { 3685602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3686602586a8SHugh Dickins 3687e809d5f0SChris Down free_percpu(sbinfo->ino_batch); 3688602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 368949cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3690602586a8SHugh Dickins kfree(sbinfo); 36911da177e4SLinus Torvalds sb->s_fs_info = NULL; 36921da177e4SLinus Torvalds } 36931da177e4SLinus Torvalds 3694f3235626SDavid Howells static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 36951da177e4SLinus Torvalds { 3696f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 36971da177e4SLinus Torvalds struct inode *inode; 36980edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3699680d794bSakpm@linux-foundation.org 3700680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3701425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3702680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3703680d794bSakpm@linux-foundation.org if (!sbinfo) 3704680d794bSakpm@linux-foundation.org return -ENOMEM; 3705680d794bSakpm@linux-foundation.org 3706680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 37071da177e4SLinus Torvalds 37080edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 37091da177e4SLinus Torvalds /* 37101da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 37111da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 37121da177e4SLinus Torvalds * but the internal instance is left unlimited. 37131da177e4SLinus Torvalds */ 37141751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3715f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 3716f3235626SDavid Howells ctx->blocks = shmem_default_max_blocks(); 3717f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_INODES)) 3718f3235626SDavid Howells ctx->inodes = shmem_default_max_inodes(); 3719ea3271f7SChris Down if (!(ctx->seen & SHMEM_SEEN_INUMS)) 3720ea3271f7SChris Down ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); 3721ca4e0519SAl Viro } else { 37221751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37231da177e4SLinus Torvalds } 372491828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 37251751e8a6SLinus Torvalds sb->s_flags |= SB_NOSEC; 37260edd73b3SHugh Dickins #else 37271751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37280edd73b3SHugh Dickins #endif 3729f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3730f3235626SDavid Howells sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; 3731e809d5f0SChris Down if (sb->s_flags & SB_KERNMOUNT) { 3732e809d5f0SChris Down sbinfo->ino_batch = alloc_percpu(ino_t); 3733e809d5f0SChris Down if (!sbinfo->ino_batch) 3734e809d5f0SChris Down goto failed; 3735e809d5f0SChris Down } 3736f3235626SDavid Howells sbinfo->uid = ctx->uid; 3737f3235626SDavid Howells sbinfo->gid = ctx->gid; 3738ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3739f3235626SDavid Howells sbinfo->mode = ctx->mode; 3740f3235626SDavid Howells sbinfo->huge = ctx->huge; 3741f3235626SDavid Howells sbinfo->mpol = ctx->mpol; 3742f3235626SDavid Howells ctx->mpol = NULL; 37431da177e4SLinus Torvalds 3744bf11b9a8SSebastian Andrzej Siewior raw_spin_lock_init(&sbinfo->stat_lock); 3745908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3746602586a8SHugh Dickins goto failed; 3747779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3748779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 37491da177e4SLinus Torvalds 3750285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 375109cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 375209cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 37531da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 37541da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3755cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3756b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 375739f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3758b09e0fa4SEric Paris #endif 3759b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 37601751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 376139f0247dSAndreas Gruenbacher #endif 37622b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 37630edd73b3SHugh Dickins 3764454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 37651da177e4SLinus Torvalds if (!inode) 37661da177e4SLinus Torvalds goto failed; 3767680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3768680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3769318ceed0SAl Viro sb->s_root = d_make_root(inode); 3770318ceed0SAl Viro if (!sb->s_root) 377148fde701SAl Viro goto failed; 37721da177e4SLinus Torvalds return 0; 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds failed: 37751da177e4SLinus Torvalds shmem_put_super(sb); 3776f2b346e4SMiaohe Lin return -ENOMEM; 37771da177e4SLinus Torvalds } 37781da177e4SLinus Torvalds 3779f3235626SDavid Howells static int shmem_get_tree(struct fs_context *fc) 3780f3235626SDavid Howells { 3781f3235626SDavid Howells return get_tree_nodev(fc, shmem_fill_super); 3782f3235626SDavid Howells } 3783f3235626SDavid Howells 3784f3235626SDavid Howells static void shmem_free_fc(struct fs_context *fc) 3785f3235626SDavid Howells { 3786f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3787f3235626SDavid Howells 3788f3235626SDavid Howells if (ctx) { 3789f3235626SDavid Howells mpol_put(ctx->mpol); 3790f3235626SDavid Howells kfree(ctx); 3791f3235626SDavid Howells } 3792f3235626SDavid Howells } 3793f3235626SDavid Howells 3794f3235626SDavid Howells static const struct fs_context_operations shmem_fs_context_ops = { 3795f3235626SDavid Howells .free = shmem_free_fc, 3796f3235626SDavid Howells .get_tree = shmem_get_tree, 3797f3235626SDavid Howells #ifdef CONFIG_TMPFS 3798f3235626SDavid Howells .parse_monolithic = shmem_parse_options, 3799f3235626SDavid Howells .parse_param = shmem_parse_one, 3800f3235626SDavid Howells .reconfigure = shmem_reconfigure, 3801f3235626SDavid Howells #endif 3802f3235626SDavid Howells }; 3803f3235626SDavid Howells 3804fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 38071da177e4SLinus Torvalds { 380841ffe5d5SHugh Dickins struct shmem_inode_info *info; 3809fd60b288SMuchun Song info = alloc_inode_sb(sb, shmem_inode_cachep, GFP_KERNEL); 381041ffe5d5SHugh Dickins if (!info) 38111da177e4SLinus Torvalds return NULL; 381241ffe5d5SHugh Dickins return &info->vfs_inode; 38131da177e4SLinus Torvalds } 38141da177e4SLinus Torvalds 381574b1da56SAl Viro static void shmem_free_in_core_inode(struct inode *inode) 3816fa0d7e3dSNick Piggin { 381784e710daSAl Viro if (S_ISLNK(inode->i_mode)) 38183ed47db3SAl Viro kfree(inode->i_link); 3819fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3820fa0d7e3dSNick Piggin } 3821fa0d7e3dSNick Piggin 38221da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 38231da177e4SLinus Torvalds { 382409208d15SAl Viro if (S_ISREG(inode->i_mode)) 38251da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 38261da177e4SLinus Torvalds } 38271da177e4SLinus Torvalds 382841ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 38291da177e4SLinus Torvalds { 383041ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 383141ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 38321da177e4SLinus Torvalds } 38331da177e4SLinus Torvalds 38349a8ec03eSweiping zhang static void shmem_init_inodecache(void) 38351da177e4SLinus Torvalds { 38361da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 38371da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 38385d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 38391da177e4SLinus Torvalds } 38401da177e4SLinus Torvalds 384141ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 38421da177e4SLinus Torvalds { 38431a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 38441da177e4SLinus Torvalds } 38451da177e4SLinus Torvalds 3846a7605426SYang Shi /* Keep the page in page cache instead of truncating it */ 3847a7605426SYang Shi static int shmem_error_remove_page(struct address_space *mapping, 3848a7605426SYang Shi struct page *page) 3849a7605426SYang Shi { 3850a7605426SYang Shi return 0; 3851a7605426SYang Shi } 3852a7605426SYang Shi 385330e6a51dSHui Su const struct address_space_operations shmem_aops = { 38541da177e4SLinus Torvalds .writepage = shmem_writepage, 385546de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 38561da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3857800d15a5SNick Piggin .write_begin = shmem_write_begin, 3858800d15a5SNick Piggin .write_end = shmem_write_end, 38591da177e4SLinus Torvalds #endif 38601c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 386154184650SMatthew Wilcox (Oracle) .migrate_folio = migrate_folio, 38621c93923cSAndrew Morton #endif 3863a7605426SYang Shi .error_remove_page = shmem_error_remove_page, 38641da177e4SLinus Torvalds }; 386530e6a51dSHui Su EXPORT_SYMBOL(shmem_aops); 38661da177e4SLinus Torvalds 386715ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 38681da177e4SLinus Torvalds .mmap = shmem_mmap, 3869c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 38701da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3871220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 38722ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 38738174202bSAl Viro .write_iter = generic_file_write_iter, 38741b061d92SChristoph Hellwig .fsync = noop_fsync, 387582c156f8SAl Viro .splice_read = generic_file_splice_read, 3876f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 387783e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 38781da177e4SLinus Torvalds #endif 38791da177e4SLinus Torvalds }; 38801da177e4SLinus Torvalds 388192e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 388244a30220SYu Zhao .getattr = shmem_getattr, 388394c1e62dSHugh Dickins .setattr = shmem_setattr, 3884b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3885b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3886feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3887e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3888e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3889b09e0fa4SEric Paris #endif 38901da177e4SLinus Torvalds }; 38911da177e4SLinus Torvalds 389292e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 38931da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3894f7cd16a5SXavier Roche .getattr = shmem_getattr, 38951da177e4SLinus Torvalds .create = shmem_create, 38961da177e4SLinus Torvalds .lookup = simple_lookup, 38971da177e4SLinus Torvalds .link = shmem_link, 38981da177e4SLinus Torvalds .unlink = shmem_unlink, 38991da177e4SLinus Torvalds .symlink = shmem_symlink, 39001da177e4SLinus Torvalds .mkdir = shmem_mkdir, 39011da177e4SLinus Torvalds .rmdir = shmem_rmdir, 39021da177e4SLinus Torvalds .mknod = shmem_mknod, 39032773bf00SMiklos Szeredi .rename = shmem_rename2, 390460545d0dSAl Viro .tmpfile = shmem_tmpfile, 39051da177e4SLinus Torvalds #endif 3906b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3907b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3908e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3909e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3910b09e0fa4SEric Paris #endif 391139f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 391294c1e62dSHugh Dickins .setattr = shmem_setattr, 3913feda821eSChristoph Hellwig .set_acl = simple_set_acl, 391439f0247dSAndreas Gruenbacher #endif 391539f0247dSAndreas Gruenbacher }; 391639f0247dSAndreas Gruenbacher 391792e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3918f7cd16a5SXavier Roche .getattr = shmem_getattr, 3919b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3920b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3921b09e0fa4SEric Paris #endif 392239f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 392394c1e62dSHugh Dickins .setattr = shmem_setattr, 3924feda821eSChristoph Hellwig .set_acl = simple_set_acl, 392539f0247dSAndreas Gruenbacher #endif 39261da177e4SLinus Torvalds }; 39271da177e4SLinus Torvalds 3928759b9775SHugh Dickins static const struct super_operations shmem_ops = { 39291da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 393074b1da56SAl Viro .free_inode = shmem_free_in_core_inode, 39311da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 39321da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 39331da177e4SLinus Torvalds .statfs = shmem_statfs, 3934680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 39351da177e4SLinus Torvalds #endif 39361f895f75SAl Viro .evict_inode = shmem_evict_inode, 39371da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 39381da177e4SLinus Torvalds .put_super = shmem_put_super, 3939396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3940779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3941779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3942779750d2SKirill A. Shutemov #endif 39431da177e4SLinus Torvalds }; 39441da177e4SLinus Torvalds 3945f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 394654cb8821SNick Piggin .fault = shmem_fault, 3947d7c17551SNing Qu .map_pages = filemap_map_pages, 39481da177e4SLinus Torvalds #ifdef CONFIG_NUMA 39491da177e4SLinus Torvalds .set_policy = shmem_set_policy, 39501da177e4SLinus Torvalds .get_policy = shmem_get_policy, 39511da177e4SLinus Torvalds #endif 39521da177e4SLinus Torvalds }; 39531da177e4SLinus Torvalds 3954f3235626SDavid Howells int shmem_init_fs_context(struct fs_context *fc) 39551da177e4SLinus Torvalds { 3956f3235626SDavid Howells struct shmem_options *ctx; 3957f3235626SDavid Howells 3958f3235626SDavid Howells ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 3959f3235626SDavid Howells if (!ctx) 3960f3235626SDavid Howells return -ENOMEM; 3961f3235626SDavid Howells 3962f3235626SDavid Howells ctx->mode = 0777 | S_ISVTX; 3963f3235626SDavid Howells ctx->uid = current_fsuid(); 3964f3235626SDavid Howells ctx->gid = current_fsgid(); 3965f3235626SDavid Howells 3966f3235626SDavid Howells fc->fs_private = ctx; 3967f3235626SDavid Howells fc->ops = &shmem_fs_context_ops; 3968f3235626SDavid Howells return 0; 39691da177e4SLinus Torvalds } 39701da177e4SLinus Torvalds 397141ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 39721da177e4SLinus Torvalds .owner = THIS_MODULE, 39731da177e4SLinus Torvalds .name = "tmpfs", 3974f3235626SDavid Howells .init_fs_context = shmem_init_fs_context, 3975f3235626SDavid Howells #ifdef CONFIG_TMPFS 3976d7167b14SAl Viro .parameters = shmem_fs_parameters, 3977f3235626SDavid Howells #endif 39781da177e4SLinus Torvalds .kill_sb = kill_litter_super, 3979ff36da69SMatthew Wilcox (Oracle) .fs_flags = FS_USERNS_MOUNT, 39801da177e4SLinus Torvalds }; 39811da177e4SLinus Torvalds 39829096bbe9SMiaohe Lin void __init shmem_init(void) 39831da177e4SLinus Torvalds { 39841da177e4SLinus Torvalds int error; 39851da177e4SLinus Torvalds 39869a8ec03eSweiping zhang shmem_init_inodecache(); 39871da177e4SLinus Torvalds 398841ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 39891da177e4SLinus Torvalds if (error) { 39901170532bSJoe Perches pr_err("Could not register tmpfs\n"); 39911da177e4SLinus Torvalds goto out2; 39921da177e4SLinus Torvalds } 399395dc112aSGreg Kroah-Hartman 3994ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 39951da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 39961da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 39971170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 39981da177e4SLinus Torvalds goto out1; 39991da177e4SLinus Torvalds } 40005a6e75f8SKirill A. Shutemov 4001396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4002435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 40035a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 40045a6e75f8SKirill A. Shutemov else 40055e6e5a12SHugh Dickins shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ 40065a6e75f8SKirill A. Shutemov #endif 40079096bbe9SMiaohe Lin return; 40081da177e4SLinus Torvalds 40091da177e4SLinus Torvalds out1: 401041ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 40111da177e4SLinus Torvalds out2: 401241ffe5d5SHugh Dickins shmem_destroy_inodecache(); 40131da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 40141da177e4SLinus Torvalds } 4015853ac43aSMatt Mackall 4016396bcc52SMatthew Wilcox (Oracle) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 40175a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 40185a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 40195a6e75f8SKirill A. Shutemov { 402026083eb6SColin Ian King static const int values[] = { 40215a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 40225a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 40235a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 40245a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 40255a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 40265a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 40275a6e75f8SKirill A. Shutemov }; 402879d4d38aSJoe Perches int len = 0; 402979d4d38aSJoe Perches int i; 40305a6e75f8SKirill A. Shutemov 403179d4d38aSJoe Perches for (i = 0; i < ARRAY_SIZE(values); i++) { 403279d4d38aSJoe Perches len += sysfs_emit_at(buf, len, 403379d4d38aSJoe Perches shmem_huge == values[i] ? "%s[%s]" : "%s%s", 403479d4d38aSJoe Perches i ? " " : "", 40355a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 40365a6e75f8SKirill A. Shutemov } 403779d4d38aSJoe Perches 403879d4d38aSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 403979d4d38aSJoe Perches 404079d4d38aSJoe Perches return len; 40415a6e75f8SKirill A. Shutemov } 40425a6e75f8SKirill A. Shutemov 40435a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 40445a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 40455a6e75f8SKirill A. Shutemov { 40465a6e75f8SKirill A. Shutemov char tmp[16]; 40475a6e75f8SKirill A. Shutemov int huge; 40485a6e75f8SKirill A. Shutemov 40495a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 40505a6e75f8SKirill A. Shutemov return -EINVAL; 40515a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 40525a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 40535a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 40545a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 40555a6e75f8SKirill A. Shutemov 40565a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 40575a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 40585a6e75f8SKirill A. Shutemov return -EINVAL; 40595a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 40605a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 40615a6e75f8SKirill A. Shutemov return -EINVAL; 40625a6e75f8SKirill A. Shutemov 40635a6e75f8SKirill A. Shutemov shmem_huge = huge; 4064435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 40655a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 40665a6e75f8SKirill A. Shutemov return count; 40675a6e75f8SKirill A. Shutemov } 40685a6e75f8SKirill A. Shutemov 40694bfa8adaSMiaohe Lin struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); 4070396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 4071f3f0e1d2SKirill A. Shutemov 4072853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4073853ac43aSMatt Mackall 4074853ac43aSMatt Mackall /* 4075853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4076853ac43aSMatt Mackall * 4077853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4078853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4079853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4080853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4081853ac43aSMatt Mackall */ 4082853ac43aSMatt Mackall 408341ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4084853ac43aSMatt Mackall .name = "tmpfs", 4085f3235626SDavid Howells .init_fs_context = ramfs_init_fs_context, 4086d7167b14SAl Viro .parameters = ramfs_fs_parameters, 4087853ac43aSMatt Mackall .kill_sb = kill_litter_super, 40882b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4089853ac43aSMatt Mackall }; 4090853ac43aSMatt Mackall 40919096bbe9SMiaohe Lin void __init shmem_init(void) 4092853ac43aSMatt Mackall { 409341ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4094853ac43aSMatt Mackall 409541ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4096853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4097853ac43aSMatt Mackall } 4098853ac43aSMatt Mackall 409910a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 4100853ac43aSMatt Mackall { 4101853ac43aSMatt Mackall return 0; 4102853ac43aSMatt Mackall } 4103853ac43aSMatt Mackall 4104d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 41053f96b79aSHugh Dickins { 41063f96b79aSHugh Dickins return 0; 41073f96b79aSHugh Dickins } 41083f96b79aSHugh Dickins 410924513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 411024513264SHugh Dickins { 411124513264SHugh Dickins } 411224513264SHugh Dickins 4113c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4114c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4115c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4116c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4117c01d5b30SHugh Dickins { 4118c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4119c01d5b30SHugh Dickins } 4120c01d5b30SHugh Dickins #endif 4121c01d5b30SHugh Dickins 412241ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 412394c1e62dSHugh Dickins { 412441ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 412594c1e62dSHugh Dickins } 412694c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 412794c1e62dSHugh Dickins 4128853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 41290b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 4130454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 41310b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 41320b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4133853ac43aSMatt Mackall 4134853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4135853ac43aSMatt Mackall 4136853ac43aSMatt Mackall /* common code */ 41371da177e4SLinus Torvalds 4138703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 4139c7277090SEric Paris unsigned long flags, unsigned int i_flags) 41401da177e4SLinus Torvalds { 41411da177e4SLinus Torvalds struct inode *inode; 414293dec2daSAl Viro struct file *res; 41431da177e4SLinus Torvalds 4144703321b6SMatthew Auld if (IS_ERR(mnt)) 4145703321b6SMatthew Auld return ERR_CAST(mnt); 41461da177e4SLinus Torvalds 4147285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 41481da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 41491da177e4SLinus Torvalds 41501da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 41511da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 41521da177e4SLinus Torvalds 415393dec2daSAl Viro inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, 415493dec2daSAl Viro flags); 4155dac2d1f6SAl Viro if (unlikely(!inode)) { 4156dac2d1f6SAl Viro shmem_unacct_size(flags, size); 4157dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 4158dac2d1f6SAl Viro } 4159c7277090SEric Paris inode->i_flags |= i_flags; 41601da177e4SLinus Torvalds inode->i_size = size; 41616d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 416226567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 416393dec2daSAl Viro if (!IS_ERR(res)) 416493dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 41654b42af81SAl Viro &shmem_file_operations); 41666b4d0b27SAl Viro if (IS_ERR(res)) 416793dec2daSAl Viro iput(inode); 41686b4d0b27SAl Viro return res; 41691da177e4SLinus Torvalds } 4170c7277090SEric Paris 4171c7277090SEric Paris /** 4172c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4173c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4174c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4175e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4176e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4177c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4178c7277090SEric Paris * @size: size to be set for the file 4179c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4180c7277090SEric Paris */ 4181c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4182c7277090SEric Paris { 4183703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 4184c7277090SEric Paris } 4185c7277090SEric Paris 4186c7277090SEric Paris /** 4187c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4188c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4189c7277090SEric Paris * @size: size to be set for the file 4190c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4191c7277090SEric Paris */ 4192c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4193c7277090SEric Paris { 4194703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 4195c7277090SEric Paris } 4196395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 41971da177e4SLinus Torvalds 419846711810SRandy Dunlap /** 4199703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 4200703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 4201703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 4202703321b6SMatthew Auld * @size: size to be set for the file 4203703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4204703321b6SMatthew Auld */ 4205703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 4206703321b6SMatthew Auld loff_t size, unsigned long flags) 4207703321b6SMatthew Auld { 4208703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 4209703321b6SMatthew Auld } 4210703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 4211703321b6SMatthew Auld 4212703321b6SMatthew Auld /** 42131da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 421445e55300SPeter Collingbourne * @vma: the vma to be mmapped is prepared by do_mmap 42151da177e4SLinus Torvalds */ 42161da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 42171da177e4SLinus Torvalds { 42181da177e4SLinus Torvalds struct file *file; 42191da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 42201da177e4SLinus Torvalds 422166fc1303SHugh Dickins /* 4222c1e8d7c6SMichel Lespinasse * Cloning a new file under mmap_lock leads to a lock ordering conflict 422366fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 422466fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 422566fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 422666fc1303SHugh Dickins */ 4227703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 42281da177e4SLinus Torvalds if (IS_ERR(file)) 42291da177e4SLinus Torvalds return PTR_ERR(file); 42301da177e4SLinus Torvalds 42311da177e4SLinus Torvalds if (vma->vm_file) 42321da177e4SLinus Torvalds fput(vma->vm_file); 42331da177e4SLinus Torvalds vma->vm_file = file; 42341da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4235f3f0e1d2SKirill A. Shutemov 42361da177e4SLinus Torvalds return 0; 42371da177e4SLinus Torvalds } 4238d9d90e5eSHugh Dickins 4239d9d90e5eSHugh Dickins /** 4240d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4241d9d90e5eSHugh Dickins * @mapping: the page's address_space 4242d9d90e5eSHugh Dickins * @index: the page index 4243d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4244d9d90e5eSHugh Dickins * 4245d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4246d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 42477e0a1265SMatthew Wilcox (Oracle) * But read_cache_page_gfp() uses the ->read_folio() method: which does not 4248d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4249d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4250d9d90e5eSHugh Dickins * 425168da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 425268da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4253d9d90e5eSHugh Dickins */ 4254d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4255d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4256d9d90e5eSHugh Dickins { 425768da9f05SHugh Dickins #ifdef CONFIG_SHMEM 425868da9f05SHugh Dickins struct inode *inode = mapping->host; 42599276aad6SHugh Dickins struct page *page; 426068da9f05SHugh Dickins int error; 426168da9f05SHugh Dickins 426230e6a51dSHui Su BUG_ON(!shmem_mapping(mapping)); 42639e18eb29SAndres Lagar-Cavilla error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, 4264cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 426568da9f05SHugh Dickins if (error) 4266a7605426SYang Shi return ERR_PTR(error); 4267a7605426SYang Shi 426868da9f05SHugh Dickins unlock_page(page); 4269a7605426SYang Shi if (PageHWPoison(page)) { 4270a7605426SYang Shi put_page(page); 4271a7605426SYang Shi return ERR_PTR(-EIO); 4272a7605426SYang Shi } 4273a7605426SYang Shi 427468da9f05SHugh Dickins return page; 427568da9f05SHugh Dickins #else 427668da9f05SHugh Dickins /* 427768da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 427868da9f05SHugh Dickins */ 4279d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 428068da9f05SHugh Dickins #endif 4281d9d90e5eSHugh Dickins } 4282d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4283