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> 4136f05cabSJeff Layton #include <linux/iversion.h> 42014bb1deSNeilBrown #include "swap.h" 4395cc09d6SAndrea Arcangeli 44853ac43aSMatt Mackall static struct vfsmount *shm_mnt; 45853ac43aSMatt Mackall 46853ac43aSMatt Mackall #ifdef CONFIG_SHMEM 471da177e4SLinus Torvalds /* 481da177e4SLinus Torvalds * This virtual memory filesystem is heavily based on the ramfs. It 491da177e4SLinus Torvalds * extends ramfs by the ability to use swap and honor resource limits 501da177e4SLinus Torvalds * which makes it a completely usable filesystem. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds 5339f0247dSAndreas Gruenbacher #include <linux/xattr.h> 54a5694255SChristoph Hellwig #include <linux/exportfs.h> 551c7c474cSChristoph Hellwig #include <linux/posix_acl.h> 56feda821eSChristoph Hellwig #include <linux/posix_acl_xattr.h> 571da177e4SLinus Torvalds #include <linux/mman.h> 581da177e4SLinus Torvalds #include <linux/string.h> 591da177e4SLinus Torvalds #include <linux/slab.h> 601da177e4SLinus Torvalds #include <linux/backing-dev.h> 611da177e4SLinus Torvalds #include <linux/shmem_fs.h> 621da177e4SLinus Torvalds #include <linux/writeback.h> 63bda97eabSHugh Dickins #include <linux/pagevec.h> 6441ffe5d5SHugh Dickins #include <linux/percpu_counter.h> 6583e4fa9cSHugh Dickins #include <linux/falloc.h> 66708e3508SHugh Dickins #include <linux/splice.h> 671da177e4SLinus Torvalds #include <linux/security.h> 681da177e4SLinus Torvalds #include <linux/swapops.h> 691da177e4SLinus Torvalds #include <linux/mempolicy.h> 701da177e4SLinus Torvalds #include <linux/namei.h> 71b00dc3adSHugh Dickins #include <linux/ctype.h> 72304dbdb7SLee Schermerhorn #include <linux/migrate.h> 73c1f60a5aSChristoph Lameter #include <linux/highmem.h> 74680d794bSakpm@linux-foundation.org #include <linux/seq_file.h> 7592562927SMimi Zohar #include <linux/magic.h> 769183df25SDavid Herrmann #include <linux/syscalls.h> 7740e041a2SDavid Herrmann #include <linux/fcntl.h> 789183df25SDavid Herrmann #include <uapi/linux/memfd.h> 79cfda0526SMike Rapoport #include <linux/userfaultfd_k.h> 804c27fe4cSMike Rapoport #include <linux/rmap.h> 812b4db796SAmir Goldstein #include <linux/uuid.h> 82304dbdb7SLee Schermerhorn 837c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 841da177e4SLinus Torvalds 85dd56b046SMel Gorman #include "internal.h" 86dd56b046SMel Gorman 8709cbfeafSKirill A. Shutemov #define BLOCKS_PER_PAGE (PAGE_SIZE/512) 8809cbfeafSKirill A. Shutemov #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* Pretend that each entry is of this size in directory's i_size */ 911da177e4SLinus Torvalds #define BOGO_DIRENT_SIZE 20 921da177e4SLinus Torvalds 9369f07ec9SHugh Dickins /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 9469f07ec9SHugh Dickins #define SHORT_SYMLINK_LEN 128 9569f07ec9SHugh Dickins 961aac1400SHugh Dickins /* 97f00cdc6dSHugh Dickins * shmem_fallocate communicates with shmem_fault or shmem_writepage via 989608703eSJan Kara * inode->i_private (with i_rwsem making sure that it has only one user at 99f00cdc6dSHugh Dickins * a time): we would prefer not to enlarge the shmem inode just for that. 1001aac1400SHugh Dickins */ 1011aac1400SHugh Dickins struct shmem_falloc { 1028e205f77SHugh Dickins wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 1031aac1400SHugh Dickins pgoff_t start; /* start of range currently being fallocated */ 1041aac1400SHugh Dickins pgoff_t next; /* the next page offset to be fallocated */ 1051aac1400SHugh Dickins pgoff_t nr_falloced; /* how many new pages have been fallocated */ 1061aac1400SHugh Dickins pgoff_t nr_unswapped; /* how often writepage refused to swap out */ 1071aac1400SHugh Dickins }; 1081aac1400SHugh Dickins 1090b5071ddSAl Viro struct shmem_options { 1100b5071ddSAl Viro unsigned long long blocks; 1110b5071ddSAl Viro unsigned long long inodes; 1120b5071ddSAl Viro struct mempolicy *mpol; 1130b5071ddSAl Viro kuid_t uid; 1140b5071ddSAl Viro kgid_t gid; 1150b5071ddSAl Viro umode_t mode; 116ea3271f7SChris Down bool full_inums; 1170b5071ddSAl Viro int huge; 1180b5071ddSAl Viro int seen; 1190b5071ddSAl Viro #define SHMEM_SEEN_BLOCKS 1 1200b5071ddSAl Viro #define SHMEM_SEEN_INODES 2 1210b5071ddSAl Viro #define SHMEM_SEEN_HUGE 4 122ea3271f7SChris Down #define SHMEM_SEEN_INUMS 8 1230b5071ddSAl Viro }; 1240b5071ddSAl Viro 125b76db735SAndrew Morton #ifdef CONFIG_TMPFS 126680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_blocks(void) 127680d794bSakpm@linux-foundation.org { 128ca79b0c2SArun KS return totalram_pages() / 2; 129680d794bSakpm@linux-foundation.org } 130680d794bSakpm@linux-foundation.org 131680d794bSakpm@linux-foundation.org static unsigned long shmem_default_max_inodes(void) 132680d794bSakpm@linux-foundation.org { 133ca79b0c2SArun KS unsigned long nr_pages = totalram_pages(); 134ca79b0c2SArun KS 135ca79b0c2SArun KS return min(nr_pages - totalhigh_pages(), nr_pages / 2); 136680d794bSakpm@linux-foundation.org } 137b76db735SAndrew Morton #endif 138680d794bSakpm@linux-foundation.org 139da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 140da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 141c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 142c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type); 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 1451da177e4SLinus Torvalds { 1461da177e4SLinus Torvalds return sb->s_fs_info; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* 1501da177e4SLinus Torvalds * shmem_file_setup pre-accounts the whole fixed size of a VM object, 1511da177e4SLinus Torvalds * for shared memory and for shared anonymous (/dev/zero) mappings 1521da177e4SLinus Torvalds * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 1531da177e4SLinus Torvalds * consistent with the pre-accounting of private mappings ... 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds static inline int shmem_acct_size(unsigned long flags, loff_t size) 1561da177e4SLinus Torvalds { 1570b0a0806SHugh Dickins return (flags & VM_NORESERVE) ? 158191c5424SAl Viro 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds static inline void shmem_unacct_size(unsigned long flags, loff_t size) 1621da177e4SLinus Torvalds { 1630b0a0806SHugh Dickins if (!(flags & VM_NORESERVE)) 1641da177e4SLinus Torvalds vm_unacct_memory(VM_ACCT(size)); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 16777142517SKonstantin Khlebnikov static inline int shmem_reacct_size(unsigned long flags, 16877142517SKonstantin Khlebnikov loff_t oldsize, loff_t newsize) 16977142517SKonstantin Khlebnikov { 17077142517SKonstantin Khlebnikov if (!(flags & VM_NORESERVE)) { 17177142517SKonstantin Khlebnikov if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 17277142517SKonstantin Khlebnikov return security_vm_enough_memory_mm(current->mm, 17377142517SKonstantin Khlebnikov VM_ACCT(newsize) - VM_ACCT(oldsize)); 17477142517SKonstantin Khlebnikov else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 17577142517SKonstantin Khlebnikov vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 17677142517SKonstantin Khlebnikov } 17777142517SKonstantin Khlebnikov return 0; 17877142517SKonstantin Khlebnikov } 17977142517SKonstantin Khlebnikov 1801da177e4SLinus Torvalds /* 1811da177e4SLinus Torvalds * ... whereas tmpfs objects are accounted incrementally as 18275edd345SHugh Dickins * pages are allocated, in order to allow large sparse files. 183923e2f0eSMatthew Wilcox (Oracle) * shmem_get_folio reports shmem_acct_block failure as -ENOSPC not -ENOMEM, 1841da177e4SLinus Torvalds * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 1851da177e4SLinus Torvalds */ 186800d8c63SKirill A. Shutemov static inline int shmem_acct_block(unsigned long flags, long pages) 1871da177e4SLinus Torvalds { 188800d8c63SKirill A. Shutemov if (!(flags & VM_NORESERVE)) 189800d8c63SKirill A. Shutemov return 0; 190800d8c63SKirill A. Shutemov 191800d8c63SKirill A. Shutemov return security_vm_enough_memory_mm(current->mm, 192800d8c63SKirill A. Shutemov pages * VM_ACCT(PAGE_SIZE)); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static inline void shmem_unacct_blocks(unsigned long flags, long pages) 1961da177e4SLinus Torvalds { 1970b0a0806SHugh Dickins if (flags & VM_NORESERVE) 19809cbfeafSKirill A. Shutemov vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2010f079694SMike Rapoport static inline bool shmem_inode_acct_block(struct inode *inode, long pages) 2020f079694SMike Rapoport { 2030f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2040f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2050f079694SMike Rapoport 2060f079694SMike Rapoport if (shmem_acct_block(info->flags, pages)) 2070f079694SMike Rapoport return false; 2080f079694SMike Rapoport 2090f079694SMike Rapoport if (sbinfo->max_blocks) { 2100f079694SMike Rapoport if (percpu_counter_compare(&sbinfo->used_blocks, 2110f079694SMike Rapoport sbinfo->max_blocks - pages) > 0) 2120f079694SMike Rapoport goto unacct; 2130f079694SMike Rapoport percpu_counter_add(&sbinfo->used_blocks, pages); 2140f079694SMike Rapoport } 2150f079694SMike Rapoport 2160f079694SMike Rapoport return true; 2170f079694SMike Rapoport 2180f079694SMike Rapoport unacct: 2190f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2200f079694SMike Rapoport return false; 2210f079694SMike Rapoport } 2220f079694SMike Rapoport 2230f079694SMike Rapoport static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) 2240f079694SMike Rapoport { 2250f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 2260f079694SMike Rapoport struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2270f079694SMike Rapoport 2280f079694SMike Rapoport if (sbinfo->max_blocks) 2290f079694SMike Rapoport percpu_counter_sub(&sbinfo->used_blocks, pages); 2300f079694SMike Rapoport shmem_unacct_blocks(info->flags, pages); 2310f079694SMike Rapoport } 2320f079694SMike Rapoport 233759b9775SHugh Dickins static const struct super_operations shmem_ops; 23430e6a51dSHui Su const struct address_space_operations shmem_aops; 23515ad7cdcSHelge Deller static const struct file_operations shmem_file_operations; 23692e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations; 23792e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations; 23892e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations; 239f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops; 240779750d2SKirill A. Shutemov static struct file_system_type shmem_fs_type; 2411da177e4SLinus Torvalds 242b0506e48SMike Rapoport bool vma_is_shmem(struct vm_area_struct *vma) 243b0506e48SMike Rapoport { 244b0506e48SMike Rapoport return vma->vm_ops == &shmem_vm_ops; 245b0506e48SMike Rapoport } 246b0506e48SMike Rapoport 2471da177e4SLinus Torvalds static LIST_HEAD(shmem_swaplist); 248cb5f7b9aSHugh Dickins static DEFINE_MUTEX(shmem_swaplist_mutex); 2491da177e4SLinus Torvalds 250e809d5f0SChris Down /* 251e809d5f0SChris Down * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and 252e809d5f0SChris Down * produces a novel ino for the newly allocated inode. 253e809d5f0SChris Down * 254e809d5f0SChris Down * It may also be called when making a hard link to permit the space needed by 255e809d5f0SChris Down * each dentry. However, in that case, no new inode number is needed since that 256e809d5f0SChris Down * internally draws from another pool of inode numbers (currently global 257e809d5f0SChris Down * get_next_ino()). This case is indicated by passing NULL as inop. 258e809d5f0SChris Down */ 259e809d5f0SChris Down #define SHMEM_INO_BATCH 1024 260e809d5f0SChris Down static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) 2615b04c689SPavel Emelyanov { 2625b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 263e809d5f0SChris Down ino_t ino; 264e809d5f0SChris Down 265e809d5f0SChris Down if (!(sb->s_flags & SB_KERNMOUNT)) { 266bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 267bb3e96d6SByron Stanoszek if (sbinfo->max_inodes) { 2685b04c689SPavel Emelyanov if (!sbinfo->free_inodes) { 269bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 2705b04c689SPavel Emelyanov return -ENOSPC; 2715b04c689SPavel Emelyanov } 2725b04c689SPavel Emelyanov sbinfo->free_inodes--; 273bb3e96d6SByron Stanoszek } 274e809d5f0SChris Down if (inop) { 275e809d5f0SChris Down ino = sbinfo->next_ino++; 276e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 277e809d5f0SChris Down ino = sbinfo->next_ino++; 278ea3271f7SChris Down if (unlikely(!sbinfo->full_inums && 279ea3271f7SChris Down ino > UINT_MAX)) { 280e809d5f0SChris Down /* 281e809d5f0SChris Down * Emulate get_next_ino uint wraparound for 282e809d5f0SChris Down * compatibility 283e809d5f0SChris Down */ 284ea3271f7SChris Down if (IS_ENABLED(CONFIG_64BIT)) 285ea3271f7SChris Down pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", 286ea3271f7SChris Down __func__, MINOR(sb->s_dev)); 287ea3271f7SChris Down sbinfo->next_ino = 1; 288ea3271f7SChris Down ino = sbinfo->next_ino++; 2895b04c689SPavel Emelyanov } 290e809d5f0SChris Down *inop = ino; 291e809d5f0SChris Down } 292bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 293e809d5f0SChris Down } else if (inop) { 294e809d5f0SChris Down /* 295e809d5f0SChris Down * __shmem_file_setup, one of our callers, is lock-free: it 296e809d5f0SChris Down * doesn't hold stat_lock in shmem_reserve_inode since 297e809d5f0SChris Down * max_inodes is always 0, and is called from potentially 298e809d5f0SChris Down * unknown contexts. As such, use a per-cpu batched allocator 299e809d5f0SChris Down * which doesn't require the per-sb stat_lock unless we are at 300e809d5f0SChris Down * the batch boundary. 301ea3271f7SChris Down * 302ea3271f7SChris Down * We don't need to worry about inode{32,64} since SB_KERNMOUNT 303ea3271f7SChris Down * shmem mounts are not exposed to userspace, so we don't need 304ea3271f7SChris Down * to worry about things like glibc compatibility. 305e809d5f0SChris Down */ 306e809d5f0SChris Down ino_t *next_ino; 307bf11b9a8SSebastian Andrzej Siewior 308e809d5f0SChris Down next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); 309e809d5f0SChris Down ino = *next_ino; 310e809d5f0SChris Down if (unlikely(ino % SHMEM_INO_BATCH == 0)) { 311bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 312e809d5f0SChris Down ino = sbinfo->next_ino; 313e809d5f0SChris Down sbinfo->next_ino += SHMEM_INO_BATCH; 314bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 315e809d5f0SChris Down if (unlikely(is_zero_ino(ino))) 316e809d5f0SChris Down ino++; 317e809d5f0SChris Down } 318e809d5f0SChris Down *inop = ino; 319e809d5f0SChris Down *next_ino = ++ino; 320e809d5f0SChris Down put_cpu(); 321e809d5f0SChris Down } 322e809d5f0SChris Down 3235b04c689SPavel Emelyanov return 0; 3245b04c689SPavel Emelyanov } 3255b04c689SPavel Emelyanov 3265b04c689SPavel Emelyanov static void shmem_free_inode(struct super_block *sb) 3275b04c689SPavel Emelyanov { 3285b04c689SPavel Emelyanov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3295b04c689SPavel Emelyanov if (sbinfo->max_inodes) { 330bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 3315b04c689SPavel Emelyanov sbinfo->free_inodes++; 332bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3335b04c689SPavel Emelyanov } 3345b04c689SPavel Emelyanov } 3355b04c689SPavel Emelyanov 33646711810SRandy Dunlap /** 33741ffe5d5SHugh Dickins * shmem_recalc_inode - recalculate the block usage of an inode 3381da177e4SLinus Torvalds * @inode: inode to recalc 3391da177e4SLinus Torvalds * 3401da177e4SLinus Torvalds * We have to calculate the free blocks since the mm can drop 3411da177e4SLinus Torvalds * undirtied hole pages behind our back. 3421da177e4SLinus Torvalds * 3431da177e4SLinus Torvalds * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 3441da177e4SLinus Torvalds * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 3451da177e4SLinus Torvalds * 3461da177e4SLinus Torvalds * It has to be called with the spinlock held. 3471da177e4SLinus Torvalds */ 3481da177e4SLinus Torvalds static void shmem_recalc_inode(struct inode *inode) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 3511da177e4SLinus Torvalds long freed; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds freed = info->alloced - info->swapped - inode->i_mapping->nrpages; 3541da177e4SLinus Torvalds if (freed > 0) { 3551da177e4SLinus Torvalds info->alloced -= freed; 35654af6042SHugh Dickins inode->i_blocks -= freed * BLOCKS_PER_PAGE; 3570f079694SMike Rapoport shmem_inode_unacct_blocks(inode, freed); 3581da177e4SLinus Torvalds } 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 361800d8c63SKirill A. Shutemov bool shmem_charge(struct inode *inode, long pages) 362800d8c63SKirill A. Shutemov { 363800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3644595ef88SKirill A. Shutemov unsigned long flags; 365800d8c63SKirill A. Shutemov 3660f079694SMike Rapoport if (!shmem_inode_acct_block(inode, pages)) 367800d8c63SKirill A. Shutemov return false; 368b1cc94abSMike Rapoport 369aaa52e34SHugh Dickins /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 370aaa52e34SHugh Dickins inode->i_mapping->nrpages += pages; 371aaa52e34SHugh Dickins 3724595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 373800d8c63SKirill A. Shutemov info->alloced += pages; 374800d8c63SKirill A. Shutemov inode->i_blocks += pages * BLOCKS_PER_PAGE; 375800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3764595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 377800d8c63SKirill A. Shutemov 378800d8c63SKirill A. Shutemov return true; 379800d8c63SKirill A. Shutemov } 380800d8c63SKirill A. Shutemov 381800d8c63SKirill A. Shutemov void shmem_uncharge(struct inode *inode, long pages) 382800d8c63SKirill A. Shutemov { 383800d8c63SKirill A. Shutemov struct shmem_inode_info *info = SHMEM_I(inode); 3844595ef88SKirill A. Shutemov unsigned long flags; 385800d8c63SKirill A. Shutemov 3866ffcd825SMatthew Wilcox (Oracle) /* nrpages adjustment done by __filemap_remove_folio() or caller */ 387aaa52e34SHugh Dickins 3884595ef88SKirill A. Shutemov spin_lock_irqsave(&info->lock, flags); 389800d8c63SKirill A. Shutemov info->alloced -= pages; 390800d8c63SKirill A. Shutemov inode->i_blocks -= pages * BLOCKS_PER_PAGE; 391800d8c63SKirill A. Shutemov shmem_recalc_inode(inode); 3924595ef88SKirill A. Shutemov spin_unlock_irqrestore(&info->lock, flags); 393800d8c63SKirill A. Shutemov 3940f079694SMike Rapoport shmem_inode_unacct_blocks(inode, pages); 395800d8c63SKirill A. Shutemov } 396800d8c63SKirill A. Shutemov 3977a5d0fbbSHugh Dickins /* 39862f945b6SMatthew Wilcox * Replace item expected in xarray by a new item, while holding xa_lock. 3997a5d0fbbSHugh Dickins */ 40062f945b6SMatthew Wilcox static int shmem_replace_entry(struct address_space *mapping, 4017a5d0fbbSHugh Dickins pgoff_t index, void *expected, void *replacement) 4027a5d0fbbSHugh Dickins { 40362f945b6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 4046dbaf22cSJohannes Weiner void *item; 4057a5d0fbbSHugh Dickins 4067a5d0fbbSHugh Dickins VM_BUG_ON(!expected); 4076dbaf22cSJohannes Weiner VM_BUG_ON(!replacement); 40862f945b6SMatthew Wilcox item = xas_load(&xas); 4097a5d0fbbSHugh Dickins if (item != expected) 4107a5d0fbbSHugh Dickins return -ENOENT; 41162f945b6SMatthew Wilcox xas_store(&xas, replacement); 4127a5d0fbbSHugh Dickins return 0; 4137a5d0fbbSHugh Dickins } 4147a5d0fbbSHugh Dickins 4157a5d0fbbSHugh Dickins /* 416d1899228SHugh Dickins * Sometimes, before we decide whether to proceed or to fail, we must check 417d1899228SHugh Dickins * that an entry was not already brought back from swap by a racing thread. 418d1899228SHugh Dickins * 419d1899228SHugh Dickins * Checking page is not enough: by the time a SwapCache page is locked, it 420d1899228SHugh Dickins * might be reused, and again be SwapCache, using the same swap as before. 421d1899228SHugh Dickins */ 422d1899228SHugh Dickins static bool shmem_confirm_swap(struct address_space *mapping, 423d1899228SHugh Dickins pgoff_t index, swp_entry_t swap) 424d1899228SHugh Dickins { 425a12831bfSMatthew Wilcox return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 426d1899228SHugh Dickins } 427d1899228SHugh Dickins 428d1899228SHugh Dickins /* 4295a6e75f8SKirill A. Shutemov * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 4305a6e75f8SKirill A. Shutemov * 4315a6e75f8SKirill A. Shutemov * SHMEM_HUGE_NEVER: 4325a6e75f8SKirill A. Shutemov * disables huge pages for the mount; 4335a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ALWAYS: 4345a6e75f8SKirill A. Shutemov * enables huge pages for the mount; 4355a6e75f8SKirill A. Shutemov * SHMEM_HUGE_WITHIN_SIZE: 4365a6e75f8SKirill A. Shutemov * only allocate huge pages if the page will be fully within i_size, 4375a6e75f8SKirill A. Shutemov * also respect fadvise()/madvise() hints; 4385a6e75f8SKirill A. Shutemov * SHMEM_HUGE_ADVISE: 4395a6e75f8SKirill A. Shutemov * only allocate huge pages if requested with fadvise()/madvise(); 4405a6e75f8SKirill A. Shutemov */ 4415a6e75f8SKirill A. Shutemov 4425a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_NEVER 0 4435a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ALWAYS 1 4445a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_WITHIN_SIZE 2 4455a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_ADVISE 3 4465a6e75f8SKirill A. Shutemov 4475a6e75f8SKirill A. Shutemov /* 4485a6e75f8SKirill A. Shutemov * Special values. 4495a6e75f8SKirill A. Shutemov * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 4505a6e75f8SKirill A. Shutemov * 4515a6e75f8SKirill A. Shutemov * SHMEM_HUGE_DENY: 4525a6e75f8SKirill A. Shutemov * disables huge on shm_mnt and all mounts, for emergency use; 4535a6e75f8SKirill A. Shutemov * SHMEM_HUGE_FORCE: 4545a6e75f8SKirill A. Shutemov * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 4555a6e75f8SKirill A. Shutemov * 4565a6e75f8SKirill A. Shutemov */ 4575a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_DENY (-1) 4585a6e75f8SKirill A. Shutemov #define SHMEM_HUGE_FORCE (-2) 4595a6e75f8SKirill A. Shutemov 460396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4615a6e75f8SKirill A. Shutemov /* ifdef here to avoid bloating shmem.o when not necessary */ 4625a6e75f8SKirill A. Shutemov 4635e6e5a12SHugh Dickins static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER; 4645a6e75f8SKirill A. Shutemov 4657c6c6cc4SZach O'Keefe bool shmem_is_huge(struct vm_area_struct *vma, struct inode *inode, 4667c6c6cc4SZach O'Keefe pgoff_t index, bool shmem_huge_force) 467c852023eSHugh Dickins { 468c852023eSHugh Dickins loff_t i_size; 469c852023eSHugh Dickins 470f7cd16a5SXavier Roche if (!S_ISREG(inode->i_mode)) 471f7cd16a5SXavier Roche return false; 4725e6e5a12SHugh Dickins if (vma && ((vma->vm_flags & VM_NOHUGEPAGE) || 4735e6e5a12SHugh Dickins test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))) 474c852023eSHugh Dickins return false; 4757c6c6cc4SZach O'Keefe if (shmem_huge_force) 4767c6c6cc4SZach O'Keefe return true; 477c852023eSHugh Dickins if (shmem_huge == SHMEM_HUGE_FORCE) 478c852023eSHugh Dickins return true; 4797c6c6cc4SZach O'Keefe if (shmem_huge == SHMEM_HUGE_DENY) 4807c6c6cc4SZach O'Keefe return false; 4815e6e5a12SHugh Dickins 4825e6e5a12SHugh Dickins switch (SHMEM_SB(inode->i_sb)->huge) { 483c852023eSHugh Dickins case SHMEM_HUGE_ALWAYS: 484c852023eSHugh Dickins return true; 485c852023eSHugh Dickins case SHMEM_HUGE_WITHIN_SIZE: 486de6ee659SLiu Yuntao index = round_up(index + 1, HPAGE_PMD_NR); 487c852023eSHugh Dickins i_size = round_up(i_size_read(inode), PAGE_SIZE); 488de6ee659SLiu Yuntao if (i_size >> PAGE_SHIFT >= index) 489c852023eSHugh Dickins return true; 490c852023eSHugh Dickins fallthrough; 491c852023eSHugh Dickins case SHMEM_HUGE_ADVISE: 4925e6e5a12SHugh Dickins if (vma && (vma->vm_flags & VM_HUGEPAGE)) 4935e6e5a12SHugh Dickins return true; 4945e6e5a12SHugh Dickins fallthrough; 495c852023eSHugh Dickins default: 496c852023eSHugh Dickins return false; 497c852023eSHugh Dickins } 498c852023eSHugh Dickins } 4995a6e75f8SKirill A. Shutemov 500e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) 5015a6e75f8SKirill A. Shutemov static int shmem_parse_huge(const char *str) 5025a6e75f8SKirill A. Shutemov { 5035a6e75f8SKirill A. Shutemov if (!strcmp(str, "never")) 5045a6e75f8SKirill A. Shutemov return SHMEM_HUGE_NEVER; 5055a6e75f8SKirill A. Shutemov if (!strcmp(str, "always")) 5065a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ALWAYS; 5075a6e75f8SKirill A. Shutemov if (!strcmp(str, "within_size")) 5085a6e75f8SKirill A. Shutemov return SHMEM_HUGE_WITHIN_SIZE; 5095a6e75f8SKirill A. Shutemov if (!strcmp(str, "advise")) 5105a6e75f8SKirill A. Shutemov return SHMEM_HUGE_ADVISE; 5115a6e75f8SKirill A. Shutemov if (!strcmp(str, "deny")) 5125a6e75f8SKirill A. Shutemov return SHMEM_HUGE_DENY; 5135a6e75f8SKirill A. Shutemov if (!strcmp(str, "force")) 5145a6e75f8SKirill A. Shutemov return SHMEM_HUGE_FORCE; 5155a6e75f8SKirill A. Shutemov return -EINVAL; 5165a6e75f8SKirill A. Shutemov } 517e5f2249aSArnd Bergmann #endif 5185a6e75f8SKirill A. Shutemov 519e5f2249aSArnd Bergmann #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 5205a6e75f8SKirill A. Shutemov static const char *shmem_format_huge(int huge) 5215a6e75f8SKirill A. Shutemov { 5225a6e75f8SKirill A. Shutemov switch (huge) { 5235a6e75f8SKirill A. Shutemov case SHMEM_HUGE_NEVER: 5245a6e75f8SKirill A. Shutemov return "never"; 5255a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ALWAYS: 5265a6e75f8SKirill A. Shutemov return "always"; 5275a6e75f8SKirill A. Shutemov case SHMEM_HUGE_WITHIN_SIZE: 5285a6e75f8SKirill A. Shutemov return "within_size"; 5295a6e75f8SKirill A. Shutemov case SHMEM_HUGE_ADVISE: 5305a6e75f8SKirill A. Shutemov return "advise"; 5315a6e75f8SKirill A. Shutemov case SHMEM_HUGE_DENY: 5325a6e75f8SKirill A. Shutemov return "deny"; 5335a6e75f8SKirill A. Shutemov case SHMEM_HUGE_FORCE: 5345a6e75f8SKirill A. Shutemov return "force"; 5355a6e75f8SKirill A. Shutemov default: 5365a6e75f8SKirill A. Shutemov VM_BUG_ON(1); 5375a6e75f8SKirill A. Shutemov return "bad_val"; 5385a6e75f8SKirill A. Shutemov } 5395a6e75f8SKirill A. Shutemov } 540f1f5929cSJérémy Lefaure #endif 5415a6e75f8SKirill A. Shutemov 542779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 543779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 544779750d2SKirill A. Shutemov { 545779750d2SKirill A. Shutemov LIST_HEAD(list), *pos, *next; 546253fd0f0SKirill A. Shutemov LIST_HEAD(to_remove); 547779750d2SKirill A. Shutemov struct inode *inode; 548779750d2SKirill A. Shutemov struct shmem_inode_info *info; 54905624571SMatthew Wilcox (Oracle) struct folio *folio; 550779750d2SKirill A. Shutemov unsigned long batch = sc ? sc->nr_to_scan : 128; 55162c9827cSGang Li int split = 0; 552779750d2SKirill A. Shutemov 553779750d2SKirill A. Shutemov if (list_empty(&sbinfo->shrinklist)) 554779750d2SKirill A. Shutemov return SHRINK_STOP; 555779750d2SKirill A. Shutemov 556779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 557779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &sbinfo->shrinklist) { 558779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 559779750d2SKirill A. Shutemov 560779750d2SKirill A. Shutemov /* pin the inode */ 561779750d2SKirill A. Shutemov inode = igrab(&info->vfs_inode); 562779750d2SKirill A. Shutemov 563779750d2SKirill A. Shutemov /* inode is about to be evicted */ 564779750d2SKirill A. Shutemov if (!inode) { 565779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 566779750d2SKirill A. Shutemov goto next; 567779750d2SKirill A. Shutemov } 568779750d2SKirill A. Shutemov 569779750d2SKirill A. Shutemov /* Check if there's anything to gain */ 570779750d2SKirill A. Shutemov if (round_up(inode->i_size, PAGE_SIZE) == 571779750d2SKirill A. Shutemov round_up(inode->i_size, HPAGE_PMD_SIZE)) { 572253fd0f0SKirill A. Shutemov list_move(&info->shrinklist, &to_remove); 573779750d2SKirill A. Shutemov goto next; 574779750d2SKirill A. Shutemov } 575779750d2SKirill A. Shutemov 576779750d2SKirill A. Shutemov list_move(&info->shrinklist, &list); 577779750d2SKirill A. Shutemov next: 57862c9827cSGang Li sbinfo->shrinklist_len--; 579779750d2SKirill A. Shutemov if (!--batch) 580779750d2SKirill A. Shutemov break; 581779750d2SKirill A. Shutemov } 582779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 583779750d2SKirill A. Shutemov 584253fd0f0SKirill A. Shutemov list_for_each_safe(pos, next, &to_remove) { 585253fd0f0SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 586253fd0f0SKirill A. Shutemov inode = &info->vfs_inode; 587253fd0f0SKirill A. Shutemov list_del_init(&info->shrinklist); 588253fd0f0SKirill A. Shutemov iput(inode); 589253fd0f0SKirill A. Shutemov } 590253fd0f0SKirill A. Shutemov 591779750d2SKirill A. Shutemov list_for_each_safe(pos, next, &list) { 592779750d2SKirill A. Shutemov int ret; 59305624571SMatthew Wilcox (Oracle) pgoff_t index; 594779750d2SKirill A. Shutemov 595779750d2SKirill A. Shutemov info = list_entry(pos, struct shmem_inode_info, shrinklist); 596779750d2SKirill A. Shutemov inode = &info->vfs_inode; 597779750d2SKirill A. Shutemov 598b3cd54b2SKirill A. Shutemov if (nr_to_split && split >= nr_to_split) 59962c9827cSGang Li goto move_back; 600779750d2SKirill A. Shutemov 60105624571SMatthew Wilcox (Oracle) index = (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT; 60205624571SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, index); 60305624571SMatthew Wilcox (Oracle) if (!folio) 604779750d2SKirill A. Shutemov goto drop; 605779750d2SKirill A. Shutemov 606b3cd54b2SKirill A. Shutemov /* No huge page at the end of the file: nothing to split */ 60705624571SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) { 60805624571SMatthew Wilcox (Oracle) folio_put(folio); 609779750d2SKirill A. Shutemov goto drop; 610779750d2SKirill A. Shutemov } 611779750d2SKirill A. Shutemov 612b3cd54b2SKirill A. Shutemov /* 61362c9827cSGang Li * Move the inode on the list back to shrinklist if we failed 61462c9827cSGang Li * to lock the page at this time. 615b3cd54b2SKirill A. Shutemov * 616b3cd54b2SKirill A. Shutemov * Waiting for the lock may lead to deadlock in the 617b3cd54b2SKirill A. Shutemov * reclaim path. 618b3cd54b2SKirill A. Shutemov */ 61905624571SMatthew Wilcox (Oracle) if (!folio_trylock(folio)) { 62005624571SMatthew Wilcox (Oracle) folio_put(folio); 62162c9827cSGang Li goto move_back; 622b3cd54b2SKirill A. Shutemov } 623b3cd54b2SKirill A. Shutemov 624d788f5b3SMatthew Wilcox (Oracle) ret = split_folio(folio); 62505624571SMatthew Wilcox (Oracle) folio_unlock(folio); 62605624571SMatthew Wilcox (Oracle) folio_put(folio); 627779750d2SKirill A. Shutemov 62862c9827cSGang Li /* If split failed move the inode on the list back to shrinklist */ 629b3cd54b2SKirill A. Shutemov if (ret) 63062c9827cSGang Li goto move_back; 631779750d2SKirill A. Shutemov 632779750d2SKirill A. Shutemov split++; 633779750d2SKirill A. Shutemov drop: 634779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 63562c9827cSGang Li goto put; 63662c9827cSGang Li move_back: 63762c9827cSGang Li /* 63862c9827cSGang Li * Make sure the inode is either on the global list or deleted 63962c9827cSGang Li * from any local list before iput() since it could be deleted 64062c9827cSGang Li * in another thread once we put the inode (then the local list 64162c9827cSGang Li * is corrupted). 64262c9827cSGang Li */ 64362c9827cSGang Li spin_lock(&sbinfo->shrinklist_lock); 64462c9827cSGang Li list_move(&info->shrinklist, &sbinfo->shrinklist); 64562c9827cSGang Li sbinfo->shrinklist_len++; 64662c9827cSGang Li spin_unlock(&sbinfo->shrinklist_lock); 64762c9827cSGang Li put: 648779750d2SKirill A. Shutemov iput(inode); 649779750d2SKirill A. Shutemov } 650779750d2SKirill A. Shutemov 651779750d2SKirill A. Shutemov return split; 652779750d2SKirill A. Shutemov } 653779750d2SKirill A. Shutemov 654779750d2SKirill A. Shutemov static long shmem_unused_huge_scan(struct super_block *sb, 655779750d2SKirill A. Shutemov struct shrink_control *sc) 656779750d2SKirill A. Shutemov { 657779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 658779750d2SKirill A. Shutemov 659779750d2SKirill A. Shutemov if (!READ_ONCE(sbinfo->shrinklist_len)) 660779750d2SKirill A. Shutemov return SHRINK_STOP; 661779750d2SKirill A. Shutemov 662779750d2SKirill A. Shutemov return shmem_unused_huge_shrink(sbinfo, sc, 0); 663779750d2SKirill A. Shutemov } 664779750d2SKirill A. Shutemov 665779750d2SKirill A. Shutemov static long shmem_unused_huge_count(struct super_block *sb, 666779750d2SKirill A. Shutemov struct shrink_control *sc) 667779750d2SKirill A. Shutemov { 668779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 669779750d2SKirill A. Shutemov return READ_ONCE(sbinfo->shrinklist_len); 670779750d2SKirill A. Shutemov } 671396bcc52SMatthew Wilcox (Oracle) #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 6725a6e75f8SKirill A. Shutemov 6735a6e75f8SKirill A. Shutemov #define shmem_huge SHMEM_HUGE_DENY 6745a6e75f8SKirill A. Shutemov 6757c6c6cc4SZach O'Keefe bool shmem_is_huge(struct vm_area_struct *vma, struct inode *inode, 6767c6c6cc4SZach O'Keefe pgoff_t index, bool shmem_huge_force) 6775e6e5a12SHugh Dickins { 6785e6e5a12SHugh Dickins return false; 6795e6e5a12SHugh Dickins } 6805e6e5a12SHugh Dickins 681779750d2SKirill A. Shutemov static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 682779750d2SKirill A. Shutemov struct shrink_control *sc, unsigned long nr_to_split) 683779750d2SKirill A. Shutemov { 684779750d2SKirill A. Shutemov return 0; 685779750d2SKirill A. Shutemov } 686396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 6875a6e75f8SKirill A. Shutemov 6885a6e75f8SKirill A. Shutemov /* 6892bb876b5SMatthew Wilcox (Oracle) * Like filemap_add_folio, but error if expected item has gone. 69046f65ec1SHugh Dickins */ 691b7dd44a1SMatthew Wilcox (Oracle) static int shmem_add_to_page_cache(struct folio *folio, 69246f65ec1SHugh Dickins struct address_space *mapping, 6933fea5a49SJohannes Weiner pgoff_t index, void *expected, gfp_t gfp, 6943fea5a49SJohannes Weiner struct mm_struct *charge_mm) 69546f65ec1SHugh Dickins { 696b7dd44a1SMatthew Wilcox (Oracle) XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); 697b7dd44a1SMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 6983fea5a49SJohannes Weiner int error; 69946f65ec1SHugh Dickins 700b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); 701b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 702b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 703b7dd44a1SMatthew Wilcox (Oracle) VM_BUG_ON(expected && folio_test_large(folio)); 70446f65ec1SHugh Dickins 705b7dd44a1SMatthew Wilcox (Oracle) folio_ref_add(folio, nr); 706b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = mapping; 707b7dd44a1SMatthew Wilcox (Oracle) folio->index = index; 70846f65ec1SHugh Dickins 709b7dd44a1SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio)) { 710b7dd44a1SMatthew Wilcox (Oracle) error = mem_cgroup_charge(folio, charge_mm, gfp); 7113fea5a49SJohannes Weiner if (error) { 712b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 7133fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK); 7143fea5a49SJohannes Weiner count_vm_event(THP_FILE_FALLBACK_CHARGE); 7153fea5a49SJohannes Weiner } 7163fea5a49SJohannes Weiner goto error; 7173fea5a49SJohannes Weiner } 7184c6355b2SJohannes Weiner } 719b7dd44a1SMatthew Wilcox (Oracle) folio_throttle_swaprate(folio, gfp); 7203fea5a49SJohannes Weiner 721552446a4SMatthew Wilcox do { 722552446a4SMatthew Wilcox xas_lock_irq(&xas); 7236b24ca4aSMatthew Wilcox (Oracle) if (expected != xas_find_conflict(&xas)) { 724552446a4SMatthew Wilcox xas_set_err(&xas, -EEXIST); 7256b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7266b24ca4aSMatthew Wilcox (Oracle) } 7276b24ca4aSMatthew Wilcox (Oracle) if (expected && xas_find_conflict(&xas)) { 7286b24ca4aSMatthew Wilcox (Oracle) xas_set_err(&xas, -EEXIST); 7296b24ca4aSMatthew Wilcox (Oracle) goto unlock; 7306b24ca4aSMatthew Wilcox (Oracle) } 731b7dd44a1SMatthew Wilcox (Oracle) xas_store(&xas, folio); 732552446a4SMatthew Wilcox if (xas_error(&xas)) 733552446a4SMatthew Wilcox goto unlock; 734b7dd44a1SMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio)) { 735800d8c63SKirill A. Shutemov count_vm_event(THP_FILE_ALLOC); 736b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr); 737552446a4SMatthew Wilcox } 738552446a4SMatthew Wilcox mapping->nrpages += nr; 739b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr); 740b7dd44a1SMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, nr); 741552446a4SMatthew Wilcox unlock: 742552446a4SMatthew Wilcox xas_unlock_irq(&xas); 743552446a4SMatthew Wilcox } while (xas_nomem(&xas, gfp)); 744552446a4SMatthew Wilcox 745552446a4SMatthew Wilcox if (xas_error(&xas)) { 7463fea5a49SJohannes Weiner error = xas_error(&xas); 7473fea5a49SJohannes Weiner goto error; 74846f65ec1SHugh Dickins } 749552446a4SMatthew Wilcox 750552446a4SMatthew Wilcox return 0; 7513fea5a49SJohannes Weiner error: 752b7dd44a1SMatthew Wilcox (Oracle) folio->mapping = NULL; 753b7dd44a1SMatthew Wilcox (Oracle) folio_ref_sub(folio, nr); 7543fea5a49SJohannes Weiner return error; 75546f65ec1SHugh Dickins } 75646f65ec1SHugh Dickins 75746f65ec1SHugh Dickins /* 7584cd400fdSMatthew Wilcox (Oracle) * Like delete_from_page_cache, but substitutes swap for @folio. 7596922c0c7SHugh Dickins */ 7604cd400fdSMatthew Wilcox (Oracle) static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) 7616922c0c7SHugh Dickins { 7624cd400fdSMatthew Wilcox (Oracle) struct address_space *mapping = folio->mapping; 7634cd400fdSMatthew Wilcox (Oracle) long nr = folio_nr_pages(folio); 7646922c0c7SHugh Dickins int error; 7656922c0c7SHugh Dickins 766b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 7674cd400fdSMatthew Wilcox (Oracle) error = shmem_replace_entry(mapping, folio->index, folio, radswap); 7684cd400fdSMatthew Wilcox (Oracle) folio->mapping = NULL; 7694cd400fdSMatthew Wilcox (Oracle) mapping->nrpages -= nr; 7704cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, -nr); 7714cd400fdSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(folio, NR_SHMEM, -nr); 772b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 7734cd400fdSMatthew Wilcox (Oracle) folio_put(folio); 7746922c0c7SHugh Dickins BUG_ON(error); 7756922c0c7SHugh Dickins } 7766922c0c7SHugh Dickins 7776922c0c7SHugh Dickins /* 778c121d3bbSMatthew Wilcox * Remove swap entry from page cache, free the swap and its page cache. 7797a5d0fbbSHugh Dickins */ 7807a5d0fbbSHugh Dickins static int shmem_free_swap(struct address_space *mapping, 7817a5d0fbbSHugh Dickins pgoff_t index, void *radswap) 7827a5d0fbbSHugh Dickins { 7836dbaf22cSJohannes Weiner void *old; 7847a5d0fbbSHugh Dickins 78555f3f7eaSMatthew Wilcox old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 7866dbaf22cSJohannes Weiner if (old != radswap) 7876dbaf22cSJohannes Weiner return -ENOENT; 7887a5d0fbbSHugh Dickins free_swap_and_cache(radix_to_swp_entry(radswap)); 7896dbaf22cSJohannes Weiner return 0; 7907a5d0fbbSHugh Dickins } 7917a5d0fbbSHugh Dickins 7927a5d0fbbSHugh Dickins /* 7936a15a370SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 79448131e03SVlastimil Babka * given offsets are swapped out. 7956a15a370SVlastimil Babka * 7969608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 7976a15a370SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 7986a15a370SVlastimil Babka */ 79948131e03SVlastimil Babka unsigned long shmem_partial_swap_usage(struct address_space *mapping, 80048131e03SVlastimil Babka pgoff_t start, pgoff_t end) 8016a15a370SVlastimil Babka { 8027ae3424fSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, start); 8036a15a370SVlastimil Babka struct page *page; 80448131e03SVlastimil Babka unsigned long swapped = 0; 8056a15a370SVlastimil Babka 8066a15a370SVlastimil Babka rcu_read_lock(); 8077ae3424fSMatthew Wilcox xas_for_each(&xas, page, end - 1) { 8087ae3424fSMatthew Wilcox if (xas_retry(&xas, page)) 8092cf938aaSMatthew Wilcox continue; 8103159f943SMatthew Wilcox if (xa_is_value(page)) 8116a15a370SVlastimil Babka swapped++; 8126a15a370SVlastimil Babka 8136a15a370SVlastimil Babka if (need_resched()) { 8147ae3424fSMatthew Wilcox xas_pause(&xas); 8156a15a370SVlastimil Babka cond_resched_rcu(); 8166a15a370SVlastimil Babka } 8176a15a370SVlastimil Babka } 8186a15a370SVlastimil Babka 8196a15a370SVlastimil Babka rcu_read_unlock(); 8206a15a370SVlastimil Babka 8216a15a370SVlastimil Babka return swapped << PAGE_SHIFT; 8226a15a370SVlastimil Babka } 8236a15a370SVlastimil Babka 8246a15a370SVlastimil Babka /* 82548131e03SVlastimil Babka * Determine (in bytes) how many of the shmem object's pages mapped by the 82648131e03SVlastimil Babka * given vma is swapped out. 82748131e03SVlastimil Babka * 8289608703eSJan Kara * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 82948131e03SVlastimil Babka * as long as the inode doesn't go away and racy results are not a problem. 83048131e03SVlastimil Babka */ 83148131e03SVlastimil Babka unsigned long shmem_swap_usage(struct vm_area_struct *vma) 83248131e03SVlastimil Babka { 83348131e03SVlastimil Babka struct inode *inode = file_inode(vma->vm_file); 83448131e03SVlastimil Babka struct shmem_inode_info *info = SHMEM_I(inode); 83548131e03SVlastimil Babka struct address_space *mapping = inode->i_mapping; 83648131e03SVlastimil Babka unsigned long swapped; 83748131e03SVlastimil Babka 83848131e03SVlastimil Babka /* Be careful as we don't hold info->lock */ 83948131e03SVlastimil Babka swapped = READ_ONCE(info->swapped); 84048131e03SVlastimil Babka 84148131e03SVlastimil Babka /* 84248131e03SVlastimil Babka * The easier cases are when the shmem object has nothing in swap, or 84348131e03SVlastimil Babka * the vma maps it whole. Then we can simply use the stats that we 84448131e03SVlastimil Babka * already track. 84548131e03SVlastimil Babka */ 84648131e03SVlastimil Babka if (!swapped) 84748131e03SVlastimil Babka return 0; 84848131e03SVlastimil Babka 84948131e03SVlastimil Babka if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 85048131e03SVlastimil Babka return swapped << PAGE_SHIFT; 85148131e03SVlastimil Babka 85248131e03SVlastimil Babka /* Here comes the more involved part */ 85302399c88SPeter Xu return shmem_partial_swap_usage(mapping, vma->vm_pgoff, 85402399c88SPeter Xu vma->vm_pgoff + vma_pages(vma)); 85548131e03SVlastimil Babka } 85648131e03SVlastimil Babka 85748131e03SVlastimil Babka /* 85824513264SHugh Dickins * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 85924513264SHugh Dickins */ 86024513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 86124513264SHugh Dickins { 862105c988fSMatthew Wilcox (Oracle) struct folio_batch fbatch; 86324513264SHugh Dickins pgoff_t index = 0; 86424513264SHugh Dickins 865105c988fSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 86624513264SHugh Dickins /* 86724513264SHugh Dickins * Minor point, but we might as well stop if someone else SHM_LOCKs it. 86824513264SHugh Dickins */ 869105c988fSMatthew Wilcox (Oracle) while (!mapping_unevictable(mapping) && 870105c988fSMatthew Wilcox (Oracle) filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { 871105c988fSMatthew Wilcox (Oracle) check_move_unevictable_folios(&fbatch); 872105c988fSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 87324513264SHugh Dickins cond_resched(); 87424513264SHugh Dickins } 8757a5d0fbbSHugh Dickins } 8767a5d0fbbSHugh Dickins 877b9a8a419SMatthew Wilcox (Oracle) static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) 87871725ed1SHugh Dickins { 879b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 88071725ed1SHugh Dickins 881b9a8a419SMatthew Wilcox (Oracle) /* 882a7f5862cSMatthew Wilcox (Oracle) * At first avoid shmem_get_folio(,,,SGP_READ): that fails 883b9a8a419SMatthew Wilcox (Oracle) * beyond i_size, and reports fallocated pages as holes. 884b9a8a419SMatthew Wilcox (Oracle) */ 885b9a8a419SMatthew Wilcox (Oracle) folio = __filemap_get_folio(inode->i_mapping, index, 886b9a8a419SMatthew Wilcox (Oracle) FGP_ENTRY | FGP_LOCK, 0); 887b9a8a419SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 888b9a8a419SMatthew Wilcox (Oracle) return folio; 889b9a8a419SMatthew Wilcox (Oracle) /* 890b9a8a419SMatthew Wilcox (Oracle) * But read a page back from swap if any of it is within i_size 891b9a8a419SMatthew Wilcox (Oracle) * (although in some cases this is just a waste of time). 892b9a8a419SMatthew Wilcox (Oracle) */ 893a7f5862cSMatthew Wilcox (Oracle) folio = NULL; 894a7f5862cSMatthew Wilcox (Oracle) shmem_get_folio(inode, index, &folio, SGP_READ); 895a7f5862cSMatthew Wilcox (Oracle) return folio; 89671725ed1SHugh Dickins } 89771725ed1SHugh Dickins 89871725ed1SHugh Dickins /* 8997f4446eeSMatthew Wilcox * Remove range of pages and swap entries from page cache, and free them. 9001635f6a7SHugh Dickins * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 9017a5d0fbbSHugh Dickins */ 9021635f6a7SHugh Dickins static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 9031635f6a7SHugh Dickins bool unfalloc) 9041da177e4SLinus Torvalds { 905285b2c4fSHugh Dickins struct address_space *mapping = inode->i_mapping; 9061da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 90709cbfeafSKirill A. Shutemov pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 90809cbfeafSKirill A. Shutemov pgoff_t end = (lend + 1) >> PAGE_SHIFT; 9090e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 9107a5d0fbbSHugh Dickins pgoff_t indices[PAGEVEC_SIZE]; 911b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 912b9a8a419SMatthew Wilcox (Oracle) bool same_folio; 9137a5d0fbbSHugh Dickins long nr_swaps_freed = 0; 914285b2c4fSHugh Dickins pgoff_t index; 915bda97eabSHugh Dickins int i; 9161da177e4SLinus Torvalds 91783e4fa9cSHugh Dickins if (lend == -1) 91883e4fa9cSHugh Dickins end = -1; /* unsigned, so actually very big */ 919bda97eabSHugh Dickins 920d144bf62SHugh Dickins if (info->fallocend > start && info->fallocend <= end && !unfalloc) 921d144bf62SHugh Dickins info->fallocend = start; 922d144bf62SHugh Dickins 92351dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 924bda97eabSHugh Dickins index = start; 9253392ca12SVishal Moola (Oracle) while (index < end && find_lock_entries(mapping, &index, end - 1, 92651dcbdacSMatthew Wilcox (Oracle) &fbatch, indices)) { 92751dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 928b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 929bda97eabSHugh Dickins 9307b774aabSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 9311635f6a7SHugh Dickins if (unfalloc) 9321635f6a7SHugh Dickins continue; 9337a5d0fbbSHugh Dickins nr_swaps_freed += !shmem_free_swap(mapping, 9343392ca12SVishal Moola (Oracle) indices[i], folio); 9357a5d0fbbSHugh Dickins continue; 9367a5d0fbbSHugh Dickins } 9377a5d0fbbSHugh Dickins 9387b774aabSMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) 9391e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 9407b774aabSMatthew Wilcox (Oracle) folio_unlock(folio); 941bda97eabSHugh Dickins } 94251dcbdacSMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 94351dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 944bda97eabSHugh Dickins cond_resched(); 945bda97eabSHugh Dickins } 946bda97eabSHugh Dickins 947b9a8a419SMatthew Wilcox (Oracle) same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 948b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT); 949b9a8a419SMatthew Wilcox (Oracle) if (folio) { 950b9a8a419SMatthew Wilcox (Oracle) same_folio = lend < folio_pos(folio) + folio_size(folio); 951b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 952b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) { 953b9a8a419SMatthew Wilcox (Oracle) start = folio->index + folio_nr_pages(folio); 954b9a8a419SMatthew Wilcox (Oracle) if (same_folio) 955b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 95683e4fa9cSHugh Dickins } 957b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 958b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 959b9a8a419SMatthew Wilcox (Oracle) folio = NULL; 960bda97eabSHugh Dickins } 961b9a8a419SMatthew Wilcox (Oracle) 962b9a8a419SMatthew Wilcox (Oracle) if (!same_folio) 963b9a8a419SMatthew Wilcox (Oracle) folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT); 964b9a8a419SMatthew Wilcox (Oracle) if (folio) { 965b9a8a419SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 966b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) 967b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 968b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 969b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 970bda97eabSHugh Dickins } 971bda97eabSHugh Dickins 972bda97eabSHugh Dickins index = start; 973b1a36650SHugh Dickins while (index < end) { 974bda97eabSHugh Dickins cond_resched(); 9750cd6144aSJohannes Weiner 976*9fb6beeaSVishal Moola (Oracle) if (!find_get_entries(mapping, &index, end - 1, &fbatch, 977cf2039afSMatthew Wilcox (Oracle) indices)) { 978b1a36650SHugh Dickins /* If all gone or hole-punch or unfalloc, we're done */ 979b1a36650SHugh Dickins if (index == start || end != -1) 980bda97eabSHugh Dickins break; 981b1a36650SHugh Dickins /* But if truncating, restart to make sure all gone */ 982bda97eabSHugh Dickins index = start; 983bda97eabSHugh Dickins continue; 984bda97eabSHugh Dickins } 9850e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 986b9a8a419SMatthew Wilcox (Oracle) folio = fbatch.folios[i]; 987bda97eabSHugh Dickins 9880e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 9891635f6a7SHugh Dickins if (unfalloc) 9901635f6a7SHugh Dickins continue; 991*9fb6beeaSVishal Moola (Oracle) if (shmem_free_swap(mapping, indices[i], folio)) { 992b1a36650SHugh Dickins /* Swap was replaced by page: retry */ 993*9fb6beeaSVishal Moola (Oracle) index = indices[i]; 994b1a36650SHugh Dickins break; 995b1a36650SHugh Dickins } 996b1a36650SHugh Dickins nr_swaps_freed++; 9977a5d0fbbSHugh Dickins continue; 9987a5d0fbbSHugh Dickins } 9997a5d0fbbSHugh Dickins 10000e499ed3SMatthew Wilcox (Oracle) folio_lock(folio); 1001800d8c63SKirill A. Shutemov 10020e499ed3SMatthew Wilcox (Oracle) if (!unfalloc || !folio_test_uptodate(folio)) { 10030e499ed3SMatthew Wilcox (Oracle) if (folio_mapping(folio) != mapping) { 1004b1a36650SHugh Dickins /* Page was replaced by swap: retry */ 10050e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1006*9fb6beeaSVishal Moola (Oracle) index = indices[i]; 1007b1a36650SHugh Dickins break; 10087a5d0fbbSHugh Dickins } 10090e499ed3SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_writeback(folio), 10100e499ed3SMatthew Wilcox (Oracle) folio); 10110e499ed3SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 101271725ed1SHugh Dickins } 10130e499ed3SMatthew Wilcox (Oracle) folio_unlock(folio); 1014bda97eabSHugh Dickins } 10150e499ed3SMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 10160e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 1017bda97eabSHugh Dickins } 101894c1e62dSHugh Dickins 10194595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 10207a5d0fbbSHugh Dickins info->swapped -= nr_swaps_freed; 10211da177e4SLinus Torvalds shmem_recalc_inode(inode); 10224595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 10231635f6a7SHugh Dickins } 10241da177e4SLinus Torvalds 10251635f6a7SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 10261635f6a7SHugh Dickins { 10271635f6a7SHugh Dickins shmem_undo_range(inode, lstart, lend, false); 1028078cd827SDeepa Dinamani inode->i_ctime = inode->i_mtime = current_time(inode); 102936f05cabSJeff Layton inode_inc_iversion(inode); 10301da177e4SLinus Torvalds } 103194c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 10321da177e4SLinus Torvalds 1033549c7297SChristian Brauner static int shmem_getattr(struct user_namespace *mnt_userns, 1034549c7297SChristian Brauner const struct path *path, struct kstat *stat, 1035a528d35eSDavid Howells u32 request_mask, unsigned int query_flags) 103644a30220SYu Zhao { 1037a528d35eSDavid Howells struct inode *inode = path->dentry->d_inode; 103844a30220SYu Zhao struct shmem_inode_info *info = SHMEM_I(inode); 103944a30220SYu Zhao 1040d0424c42SHugh Dickins if (info->alloced - info->swapped != inode->i_mapping->nrpages) { 10414595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 104244a30220SYu Zhao shmem_recalc_inode(inode); 10434595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1044d0424c42SHugh Dickins } 1045e408e695STheodore Ts'o if (info->fsflags & FS_APPEND_FL) 1046e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_APPEND; 1047e408e695STheodore Ts'o if (info->fsflags & FS_IMMUTABLE_FL) 1048e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_IMMUTABLE; 1049e408e695STheodore Ts'o if (info->fsflags & FS_NODUMP_FL) 1050e408e695STheodore Ts'o stat->attributes |= STATX_ATTR_NODUMP; 1051e408e695STheodore Ts'o stat->attributes_mask |= (STATX_ATTR_APPEND | 1052e408e695STheodore Ts'o STATX_ATTR_IMMUTABLE | 1053e408e695STheodore Ts'o STATX_ATTR_NODUMP); 10540d56a451SChristian Brauner generic_fillattr(&init_user_ns, inode, stat); 105589fdcd26SYang Shi 10567c6c6cc4SZach O'Keefe if (shmem_is_huge(NULL, inode, 0, false)) 105789fdcd26SYang Shi stat->blksize = HPAGE_PMD_SIZE; 105889fdcd26SYang Shi 1059f7cd16a5SXavier Roche if (request_mask & STATX_BTIME) { 1060f7cd16a5SXavier Roche stat->result_mask |= STATX_BTIME; 1061f7cd16a5SXavier Roche stat->btime.tv_sec = info->i_crtime.tv_sec; 1062f7cd16a5SXavier Roche stat->btime.tv_nsec = info->i_crtime.tv_nsec; 1063f7cd16a5SXavier Roche } 1064f7cd16a5SXavier Roche 106544a30220SYu Zhao return 0; 106644a30220SYu Zhao } 106744a30220SYu Zhao 1068549c7297SChristian Brauner static int shmem_setattr(struct user_namespace *mnt_userns, 1069549c7297SChristian Brauner struct dentry *dentry, struct iattr *attr) 10701da177e4SLinus Torvalds { 107175c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 107240e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 10731da177e4SLinus Torvalds int error; 107436f05cabSJeff Layton bool update_mtime = false; 107536f05cabSJeff Layton bool update_ctime = true; 10761da177e4SLinus Torvalds 10772f221d6fSChristian Brauner error = setattr_prepare(&init_user_ns, dentry, attr); 1078db78b877SChristoph Hellwig if (error) 1079db78b877SChristoph Hellwig return error; 1080db78b877SChristoph Hellwig 108194c1e62dSHugh Dickins if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 108294c1e62dSHugh Dickins loff_t oldsize = inode->i_size; 108394c1e62dSHugh Dickins loff_t newsize = attr->ia_size; 10843889e6e7Snpiggin@suse.de 10859608703eSJan Kara /* protected by i_rwsem */ 108640e041a2SDavid Herrmann if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 108740e041a2SDavid Herrmann (newsize > oldsize && (info->seals & F_SEAL_GROW))) 108840e041a2SDavid Herrmann return -EPERM; 108940e041a2SDavid Herrmann 109094c1e62dSHugh Dickins if (newsize != oldsize) { 109177142517SKonstantin Khlebnikov error = shmem_reacct_size(SHMEM_I(inode)->flags, 109277142517SKonstantin Khlebnikov oldsize, newsize); 109377142517SKonstantin Khlebnikov if (error) 109477142517SKonstantin Khlebnikov return error; 109594c1e62dSHugh Dickins i_size_write(inode, newsize); 109636f05cabSJeff Layton update_mtime = true; 109736f05cabSJeff Layton } else { 109836f05cabSJeff Layton update_ctime = false; 109994c1e62dSHugh Dickins } 1100afa2db2fSJosef Bacik if (newsize <= oldsize) { 110194c1e62dSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 1102d0424c42SHugh Dickins if (oldsize > holebegin) 1103d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1104d0424c42SHugh Dickins holebegin, 0, 1); 1105d0424c42SHugh Dickins if (info->alloced) 1106d0424c42SHugh Dickins shmem_truncate_range(inode, 1107d0424c42SHugh Dickins newsize, (loff_t)-1); 110894c1e62dSHugh Dickins /* unmap again to remove racily COWed private pages */ 1109d0424c42SHugh Dickins if (oldsize > holebegin) 1110d0424c42SHugh Dickins unmap_mapping_range(inode->i_mapping, 1111d0424c42SHugh Dickins holebegin, 0, 1); 111294c1e62dSHugh Dickins } 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds 11152f221d6fSChristian Brauner setattr_copy(&init_user_ns, inode, attr); 1116db78b877SChristoph Hellwig if (attr->ia_valid & ATTR_MODE) 1117e65ce2a5SChristian Brauner error = posix_acl_chmod(&init_user_ns, inode, inode->i_mode); 111836f05cabSJeff Layton if (!error && update_ctime) { 111936f05cabSJeff Layton inode->i_ctime = current_time(inode); 112036f05cabSJeff Layton if (update_mtime) 112136f05cabSJeff Layton inode->i_mtime = inode->i_ctime; 112236f05cabSJeff Layton inode_inc_iversion(inode); 112336f05cabSJeff Layton } 11241da177e4SLinus Torvalds return error; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11271f895f75SAl Viro static void shmem_evict_inode(struct inode *inode) 11281da177e4SLinus Torvalds { 11291da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 1130779750d2SKirill A. Shutemov struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 11311da177e4SLinus Torvalds 113230e6a51dSHui Su if (shmem_mapping(inode->i_mapping)) { 11331da177e4SLinus Torvalds shmem_unacct_size(info->flags, inode->i_size); 11341da177e4SLinus Torvalds inode->i_size = 0; 1135bc786390SHugh Dickins mapping_set_exiting(inode->i_mapping); 11363889e6e7Snpiggin@suse.de shmem_truncate_range(inode, 0, (loff_t)-1); 1137779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1138779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1139779750d2SKirill A. Shutemov if (!list_empty(&info->shrinklist)) { 1140779750d2SKirill A. Shutemov list_del_init(&info->shrinklist); 1141779750d2SKirill A. Shutemov sbinfo->shrinklist_len--; 1142779750d2SKirill A. Shutemov } 1143779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1144779750d2SKirill A. Shutemov } 1145af53d3e9SHugh Dickins while (!list_empty(&info->swaplist)) { 1146af53d3e9SHugh Dickins /* Wait while shmem_unuse() is scanning this inode... */ 1147af53d3e9SHugh Dickins wait_var_event(&info->stop_eviction, 1148af53d3e9SHugh Dickins !atomic_read(&info->stop_eviction)); 1149cb5f7b9aSHugh Dickins mutex_lock(&shmem_swaplist_mutex); 1150af53d3e9SHugh Dickins /* ...but beware of the race if we peeked too early */ 1151af53d3e9SHugh Dickins if (!atomic_read(&info->stop_eviction)) 11521da177e4SLinus Torvalds list_del_init(&info->swaplist); 1153cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 11541da177e4SLinus Torvalds } 11553ed47db3SAl Viro } 1156b09e0fa4SEric Paris 115738f38657SAristeu Rozanski simple_xattrs_free(&info->xattrs); 11580f3c42f5SHugh Dickins WARN_ON(inode->i_blocks); 11595b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 1160dbd5768fSJan Kara clear_inode(inode); 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 1163b56a2d8aSVineeth Remanan Pillai static int shmem_find_swap_entries(struct address_space *mapping, 1164da08e9b7SMatthew Wilcox (Oracle) pgoff_t start, struct folio_batch *fbatch, 1165da08e9b7SMatthew Wilcox (Oracle) pgoff_t *indices, unsigned int type) 1166478922e2SMatthew Wilcox { 1167b56a2d8aSVineeth Remanan Pillai XA_STATE(xas, &mapping->i_pages, start); 1168da08e9b7SMatthew Wilcox (Oracle) struct folio *folio; 116987039546SHugh Dickins swp_entry_t entry; 1170478922e2SMatthew Wilcox 1171478922e2SMatthew Wilcox rcu_read_lock(); 1172da08e9b7SMatthew Wilcox (Oracle) xas_for_each(&xas, folio, ULONG_MAX) { 1173da08e9b7SMatthew Wilcox (Oracle) if (xas_retry(&xas, folio)) 11745b9c98f3SMike Kravetz continue; 1175b56a2d8aSVineeth Remanan Pillai 1176da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1177478922e2SMatthew Wilcox continue; 1178b56a2d8aSVineeth Remanan Pillai 1179da08e9b7SMatthew Wilcox (Oracle) entry = radix_to_swp_entry(folio); 11806cec2b95SMiaohe Lin /* 11816cec2b95SMiaohe Lin * swapin error entries can be found in the mapping. But they're 11826cec2b95SMiaohe Lin * deliberately ignored here as we've done everything we can do. 11836cec2b95SMiaohe Lin */ 118487039546SHugh Dickins if (swp_type(entry) != type) 1185b56a2d8aSVineeth Remanan Pillai continue; 1186b56a2d8aSVineeth Remanan Pillai 1187e384200eSHugh Dickins indices[folio_batch_count(fbatch)] = xas.xa_index; 1188da08e9b7SMatthew Wilcox (Oracle) if (!folio_batch_add(fbatch, folio)) 1189da08e9b7SMatthew Wilcox (Oracle) break; 1190b56a2d8aSVineeth Remanan Pillai 1191b56a2d8aSVineeth Remanan Pillai if (need_resched()) { 1192e21a2955SMatthew Wilcox xas_pause(&xas); 1193478922e2SMatthew Wilcox cond_resched_rcu(); 1194478922e2SMatthew Wilcox } 1195b56a2d8aSVineeth Remanan Pillai } 1196478922e2SMatthew Wilcox rcu_read_unlock(); 1197e21a2955SMatthew Wilcox 1198da08e9b7SMatthew Wilcox (Oracle) return xas.xa_index; 1199b56a2d8aSVineeth Remanan Pillai } 1200b56a2d8aSVineeth Remanan Pillai 1201b56a2d8aSVineeth Remanan Pillai /* 1202b56a2d8aSVineeth Remanan Pillai * Move the swapped pages for an inode to page cache. Returns the count 1203b56a2d8aSVineeth Remanan Pillai * of pages swapped in, or the error in case of failure. 1204b56a2d8aSVineeth Remanan Pillai */ 1205da08e9b7SMatthew Wilcox (Oracle) static int shmem_unuse_swap_entries(struct inode *inode, 1206da08e9b7SMatthew Wilcox (Oracle) struct folio_batch *fbatch, pgoff_t *indices) 1207b56a2d8aSVineeth Remanan Pillai { 1208b56a2d8aSVineeth Remanan Pillai int i = 0; 1209b56a2d8aSVineeth Remanan Pillai int ret = 0; 1210b56a2d8aSVineeth Remanan Pillai int error = 0; 1211b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1212b56a2d8aSVineeth Remanan Pillai 1213da08e9b7SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(fbatch); i++) { 1214da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i]; 1215b56a2d8aSVineeth Remanan Pillai 1216da08e9b7SMatthew Wilcox (Oracle) if (!xa_is_value(folio)) 1217b56a2d8aSVineeth Remanan Pillai continue; 1218da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, indices[i], 1219da08e9b7SMatthew Wilcox (Oracle) &folio, SGP_CACHE, 1220b56a2d8aSVineeth Remanan Pillai mapping_gfp_mask(mapping), 1221b56a2d8aSVineeth Remanan Pillai NULL, NULL); 1222b56a2d8aSVineeth Remanan Pillai if (error == 0) { 1223da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1224da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1225b56a2d8aSVineeth Remanan Pillai ret++; 1226b56a2d8aSVineeth Remanan Pillai } 1227b56a2d8aSVineeth Remanan Pillai if (error == -ENOMEM) 1228b56a2d8aSVineeth Remanan Pillai break; 1229b56a2d8aSVineeth Remanan Pillai error = 0; 1230b56a2d8aSVineeth Remanan Pillai } 1231b56a2d8aSVineeth Remanan Pillai return error ? error : ret; 1232478922e2SMatthew Wilcox } 1233478922e2SMatthew Wilcox 123446f65ec1SHugh Dickins /* 123546f65ec1SHugh Dickins * If swap found in inode, free it and move page from swapcache to filecache. 123646f65ec1SHugh Dickins */ 123710a9c496SChristoph Hellwig static int shmem_unuse_inode(struct inode *inode, unsigned int type) 12381da177e4SLinus Torvalds { 1239b56a2d8aSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1240b56a2d8aSVineeth Remanan Pillai pgoff_t start = 0; 1241da08e9b7SMatthew Wilcox (Oracle) struct folio_batch fbatch; 1242b56a2d8aSVineeth Remanan Pillai pgoff_t indices[PAGEVEC_SIZE]; 1243b56a2d8aSVineeth Remanan Pillai int ret = 0; 12441da177e4SLinus Torvalds 1245b56a2d8aSVineeth Remanan Pillai do { 1246da08e9b7SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 1247da08e9b7SMatthew Wilcox (Oracle) shmem_find_swap_entries(mapping, start, &fbatch, indices, type); 1248da08e9b7SMatthew Wilcox (Oracle) if (folio_batch_count(&fbatch) == 0) { 1249b56a2d8aSVineeth Remanan Pillai ret = 0; 1250778dd893SHugh Dickins break; 1251b56a2d8aSVineeth Remanan Pillai } 1252b56a2d8aSVineeth Remanan Pillai 1253da08e9b7SMatthew Wilcox (Oracle) ret = shmem_unuse_swap_entries(inode, &fbatch, indices); 1254b56a2d8aSVineeth Remanan Pillai if (ret < 0) 1255b56a2d8aSVineeth Remanan Pillai break; 1256b56a2d8aSVineeth Remanan Pillai 1257da08e9b7SMatthew Wilcox (Oracle) start = indices[folio_batch_count(&fbatch) - 1]; 1258b56a2d8aSVineeth Remanan Pillai } while (true); 1259b56a2d8aSVineeth Remanan Pillai 1260b56a2d8aSVineeth Remanan Pillai return ret; 1261b56a2d8aSVineeth Remanan Pillai } 1262b56a2d8aSVineeth Remanan Pillai 1263b56a2d8aSVineeth Remanan Pillai /* 1264b56a2d8aSVineeth Remanan Pillai * Read all the shared memory data that resides in the swap 1265b56a2d8aSVineeth Remanan Pillai * device 'type' back into memory, so the swap device can be 1266b56a2d8aSVineeth Remanan Pillai * unused. 1267b56a2d8aSVineeth Remanan Pillai */ 126810a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 1269b56a2d8aSVineeth Remanan Pillai { 1270b56a2d8aSVineeth Remanan Pillai struct shmem_inode_info *info, *next; 1271b56a2d8aSVineeth Remanan Pillai int error = 0; 1272b56a2d8aSVineeth Remanan Pillai 1273b56a2d8aSVineeth Remanan Pillai if (list_empty(&shmem_swaplist)) 1274b56a2d8aSVineeth Remanan Pillai return 0; 1275b56a2d8aSVineeth Remanan Pillai 1276b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1277b56a2d8aSVineeth Remanan Pillai list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1278b56a2d8aSVineeth Remanan Pillai if (!info->swapped) { 1279b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1280b56a2d8aSVineeth Remanan Pillai continue; 1281b56a2d8aSVineeth Remanan Pillai } 1282af53d3e9SHugh Dickins /* 1283af53d3e9SHugh Dickins * Drop the swaplist mutex while searching the inode for swap; 1284af53d3e9SHugh Dickins * but before doing so, make sure shmem_evict_inode() will not 1285af53d3e9SHugh Dickins * remove placeholder inode from swaplist, nor let it be freed 1286af53d3e9SHugh Dickins * (igrab() would protect from unlink, but not from unmount). 1287af53d3e9SHugh Dickins */ 1288af53d3e9SHugh Dickins atomic_inc(&info->stop_eviction); 1289b56a2d8aSVineeth Remanan Pillai mutex_unlock(&shmem_swaplist_mutex); 1290b56a2d8aSVineeth Remanan Pillai 129110a9c496SChristoph Hellwig error = shmem_unuse_inode(&info->vfs_inode, type); 1292b56a2d8aSVineeth Remanan Pillai cond_resched(); 1293b56a2d8aSVineeth Remanan Pillai 1294b56a2d8aSVineeth Remanan Pillai mutex_lock(&shmem_swaplist_mutex); 1295b56a2d8aSVineeth Remanan Pillai next = list_next_entry(info, swaplist); 1296b56a2d8aSVineeth Remanan Pillai if (!info->swapped) 1297b56a2d8aSVineeth Remanan Pillai list_del_init(&info->swaplist); 1298af53d3e9SHugh Dickins if (atomic_dec_and_test(&info->stop_eviction)) 1299af53d3e9SHugh Dickins wake_up_var(&info->stop_eviction); 1300b56a2d8aSVineeth Remanan Pillai if (error) 1301b56a2d8aSVineeth Remanan Pillai break; 13021da177e4SLinus Torvalds } 1303cb5f7b9aSHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1304778dd893SHugh Dickins 1305778dd893SHugh Dickins return error; 13061da177e4SLinus Torvalds } 13071da177e4SLinus Torvalds 13081da177e4SLinus Torvalds /* 13091da177e4SLinus Torvalds * Move the page from the page cache to the swap cache. 13101da177e4SLinus Torvalds */ 13111da177e4SLinus Torvalds static int shmem_writepage(struct page *page, struct writeback_control *wbc) 13121da177e4SLinus Torvalds { 1313e2e3fdc7SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 13141da177e4SLinus Torvalds struct shmem_inode_info *info; 13151da177e4SLinus Torvalds struct address_space *mapping; 13161da177e4SLinus Torvalds struct inode *inode; 13176922c0c7SHugh Dickins swp_entry_t swap; 13186922c0c7SHugh Dickins pgoff_t index; 13191da177e4SLinus Torvalds 13201e6decf3SHugh Dickins /* 13211e6decf3SHugh Dickins * If /sys/kernel/mm/transparent_hugepage/shmem_enabled is "always" or 13221e6decf3SHugh Dickins * "force", drivers/gpu/drm/i915/gem/i915_gem_shmem.c gets huge pages, 13231e6decf3SHugh Dickins * and its shmem_writeback() needs them to be split when swapping. 13241e6decf3SHugh Dickins */ 1325f530ed0eSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 13261e6decf3SHugh Dickins /* Ensure the subpages are still dirty */ 1327f530ed0eSMatthew Wilcox (Oracle) folio_test_set_dirty(folio); 13281e6decf3SHugh Dickins if (split_huge_page(page) < 0) 13291e6decf3SHugh Dickins goto redirty; 1330f530ed0eSMatthew Wilcox (Oracle) folio = page_folio(page); 1331f530ed0eSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 13321e6decf3SHugh Dickins } 13331e6decf3SHugh Dickins 1334f530ed0eSMatthew Wilcox (Oracle) BUG_ON(!folio_test_locked(folio)); 1335f530ed0eSMatthew Wilcox (Oracle) mapping = folio->mapping; 1336f530ed0eSMatthew Wilcox (Oracle) index = folio->index; 13371da177e4SLinus Torvalds inode = mapping->host; 13381da177e4SLinus Torvalds info = SHMEM_I(inode); 13391da177e4SLinus Torvalds if (info->flags & VM_LOCKED) 13401da177e4SLinus Torvalds goto redirty; 1341d9fe526aSHugh Dickins if (!total_swap_pages) 13421da177e4SLinus Torvalds goto redirty; 13431da177e4SLinus Torvalds 1344d9fe526aSHugh Dickins /* 134597b713baSChristoph Hellwig * Our capabilities prevent regular writeback or sync from ever calling 134697b713baSChristoph Hellwig * shmem_writepage; but a stacking filesystem might use ->writepage of 134797b713baSChristoph Hellwig * its underlying filesystem, in which case tmpfs should write out to 134897b713baSChristoph Hellwig * swap only in response to memory pressure, and not for the writeback 134997b713baSChristoph Hellwig * threads or sync. 1350d9fe526aSHugh Dickins */ 135148f170fbSHugh Dickins if (!wbc->for_reclaim) { 135248f170fbSHugh Dickins WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ 135348f170fbSHugh Dickins goto redirty; 135448f170fbSHugh Dickins } 13551635f6a7SHugh Dickins 13561635f6a7SHugh Dickins /* 13571635f6a7SHugh Dickins * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 13581635f6a7SHugh Dickins * value into swapfile.c, the only way we can correctly account for a 1359f530ed0eSMatthew Wilcox (Oracle) * fallocated folio arriving here is now to initialize it and write it. 13601aac1400SHugh Dickins * 1361f530ed0eSMatthew Wilcox (Oracle) * That's okay for a folio already fallocated earlier, but if we have 13621aac1400SHugh Dickins * not yet completed the fallocation, then (a) we want to keep track 1363f530ed0eSMatthew Wilcox (Oracle) * of this folio in case we have to undo it, and (b) it may not be a 13641aac1400SHugh Dickins * good idea to continue anyway, once we're pushing into swap. So 1365f530ed0eSMatthew Wilcox (Oracle) * reactivate the folio, and let shmem_fallocate() quit when too many. 13661635f6a7SHugh Dickins */ 1367f530ed0eSMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 13681aac1400SHugh Dickins if (inode->i_private) { 13691aac1400SHugh Dickins struct shmem_falloc *shmem_falloc; 13701aac1400SHugh Dickins spin_lock(&inode->i_lock); 13711aac1400SHugh Dickins shmem_falloc = inode->i_private; 13721aac1400SHugh Dickins if (shmem_falloc && 13738e205f77SHugh Dickins !shmem_falloc->waitq && 13741aac1400SHugh Dickins index >= shmem_falloc->start && 13751aac1400SHugh Dickins index < shmem_falloc->next) 13761aac1400SHugh Dickins shmem_falloc->nr_unswapped++; 13771aac1400SHugh Dickins else 13781aac1400SHugh Dickins shmem_falloc = NULL; 13791aac1400SHugh Dickins spin_unlock(&inode->i_lock); 13801aac1400SHugh Dickins if (shmem_falloc) 13811aac1400SHugh Dickins goto redirty; 13821aac1400SHugh Dickins } 1383f530ed0eSMatthew Wilcox (Oracle) folio_zero_range(folio, 0, folio_size(folio)); 1384f530ed0eSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1385f530ed0eSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 13861635f6a7SHugh Dickins } 13871635f6a7SHugh Dickins 1388e2e3fdc7SMatthew Wilcox (Oracle) swap = folio_alloc_swap(folio); 138948f170fbSHugh Dickins if (!swap.val) 139048f170fbSHugh Dickins goto redirty; 1391d9fe526aSHugh Dickins 1392b1dea800SHugh Dickins /* 1393b1dea800SHugh Dickins * Add inode to shmem_unuse()'s list of swapped-out inodes, 1394f530ed0eSMatthew Wilcox (Oracle) * if it's not already there. Do it now before the folio is 13956922c0c7SHugh Dickins * moved to swap cache, when its pagelock no longer protects 1396b1dea800SHugh Dickins * the inode from eviction. But don't unlock the mutex until 13976922c0c7SHugh Dickins * we've incremented swapped, because shmem_unuse_inode() will 13986922c0c7SHugh Dickins * prune a !swapped inode from the swaplist under this mutex. 1399b1dea800SHugh Dickins */ 1400b1dea800SHugh Dickins mutex_lock(&shmem_swaplist_mutex); 140105bf86b4SHugh Dickins if (list_empty(&info->swaplist)) 1402b56a2d8aSVineeth Remanan Pillai list_add(&info->swaplist, &shmem_swaplist); 1403b1dea800SHugh Dickins 1404a4c366f0SMatthew Wilcox (Oracle) if (add_to_swap_cache(folio, swap, 14053852f676SJoonsoo Kim __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, 14063852f676SJoonsoo Kim NULL) == 0) { 14074595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1408267a4c76SHugh Dickins shmem_recalc_inode(inode); 1409267a4c76SHugh Dickins info->swapped++; 14104595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1411267a4c76SHugh Dickins 1412aaa46865SHugh Dickins swap_shmem_alloc(swap); 14134cd400fdSMatthew Wilcox (Oracle) shmem_delete_from_page_cache(folio, swp_to_radix_entry(swap)); 14146922c0c7SHugh Dickins 14156922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 1416f530ed0eSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 1417f530ed0eSMatthew Wilcox (Oracle) swap_writepage(&folio->page, wbc); 14181da177e4SLinus Torvalds return 0; 14191da177e4SLinus Torvalds } 14201da177e4SLinus Torvalds 14216922c0c7SHugh Dickins mutex_unlock(&shmem_swaplist_mutex); 14224081f744SMatthew Wilcox (Oracle) put_swap_folio(folio, swap); 14231da177e4SLinus Torvalds redirty: 1424f530ed0eSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1425d9fe526aSHugh Dickins if (wbc->for_reclaim) 1426f530ed0eSMatthew Wilcox (Oracle) return AOP_WRITEPAGE_ACTIVATE; /* Return with folio locked */ 1427f530ed0eSMatthew Wilcox (Oracle) folio_unlock(folio); 1428d9fe526aSHugh Dickins return 0; 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 143175edd345SHugh Dickins #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 143271fe804bSLee Schermerhorn static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1433680d794bSakpm@linux-foundation.org { 1434680d794bSakpm@linux-foundation.org char buffer[64]; 1435680d794bSakpm@linux-foundation.org 143671fe804bSLee Schermerhorn if (!mpol || mpol->mode == MPOL_DEFAULT) 1437095f1fc4SLee Schermerhorn return; /* show nothing */ 1438095f1fc4SLee Schermerhorn 1439a7a88b23SHugh Dickins mpol_to_str(buffer, sizeof(buffer), mpol); 1440095f1fc4SLee Schermerhorn 1441095f1fc4SLee Schermerhorn seq_printf(seq, ",mpol=%s", buffer); 1442680d794bSakpm@linux-foundation.org } 144371fe804bSLee Schermerhorn 144471fe804bSLee Schermerhorn static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 144571fe804bSLee Schermerhorn { 144671fe804bSLee Schermerhorn struct mempolicy *mpol = NULL; 144771fe804bSLee Schermerhorn if (sbinfo->mpol) { 1448bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 144971fe804bSLee Schermerhorn mpol = sbinfo->mpol; 145071fe804bSLee Schermerhorn mpol_get(mpol); 1451bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 145271fe804bSLee Schermerhorn } 145371fe804bSLee Schermerhorn return mpol; 145471fe804bSLee Schermerhorn } 145575edd345SHugh Dickins #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 145675edd345SHugh Dickins static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 145775edd345SHugh Dickins { 145875edd345SHugh Dickins } 145975edd345SHugh Dickins static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 146075edd345SHugh Dickins { 146175edd345SHugh Dickins return NULL; 146275edd345SHugh Dickins } 146375edd345SHugh Dickins #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 146475edd345SHugh Dickins #ifndef CONFIG_NUMA 146575edd345SHugh Dickins #define vm_policy vm_private_data 146675edd345SHugh Dickins #endif 1467680d794bSakpm@linux-foundation.org 1468800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_init(struct vm_area_struct *vma, 1469800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1470800d8c63SKirill A. Shutemov { 1471800d8c63SKirill A. Shutemov /* Create a pseudo vma that just contains the policy */ 14722c4541e2SKirill A. Shutemov vma_init(vma, NULL); 1473800d8c63SKirill A. Shutemov /* Bias interleave by inode number to distribute better across nodes */ 1474800d8c63SKirill A. Shutemov vma->vm_pgoff = index + info->vfs_inode.i_ino; 1475800d8c63SKirill A. Shutemov vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); 1476800d8c63SKirill A. Shutemov } 1477800d8c63SKirill A. Shutemov 1478800d8c63SKirill A. Shutemov static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) 1479800d8c63SKirill A. Shutemov { 1480800d8c63SKirill A. Shutemov /* Drop reference taken by mpol_shared_policy_lookup() */ 1481800d8c63SKirill A. Shutemov mpol_cond_put(vma->vm_policy); 1482800d8c63SKirill A. Shutemov } 1483800d8c63SKirill A. Shutemov 14845739a81cSMatthew Wilcox (Oracle) static struct folio *shmem_swapin(swp_entry_t swap, gfp_t gfp, 148541ffe5d5SHugh Dickins struct shmem_inode_info *info, pgoff_t index) 14861da177e4SLinus Torvalds { 14871da177e4SLinus Torvalds struct vm_area_struct pvma; 148818a2f371SMel Gorman struct page *page; 14898c63ca5bSWill Deacon struct vm_fault vmf = { 14908c63ca5bSWill Deacon .vma = &pvma, 14918c63ca5bSWill Deacon }; 14921da177e4SLinus Torvalds 1493800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 1494e9e9b7ecSMinchan Kim page = swap_cluster_readahead(swap, gfp, &vmf); 1495800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 149618a2f371SMel Gorman 14975739a81cSMatthew Wilcox (Oracle) if (!page) 14985739a81cSMatthew Wilcox (Oracle) return NULL; 14995739a81cSMatthew Wilcox (Oracle) return page_folio(page); 1500800d8c63SKirill A. Shutemov } 150118a2f371SMel Gorman 150278cc8cdcSRik van Riel /* 150378cc8cdcSRik van Riel * Make sure huge_gfp is always more limited than limit_gfp. 150478cc8cdcSRik van Riel * Some of the flags set permissions, while others set limitations. 150578cc8cdcSRik van Riel */ 150678cc8cdcSRik van Riel static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) 150778cc8cdcSRik van Riel { 150878cc8cdcSRik van Riel gfp_t allowflags = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 150978cc8cdcSRik van Riel gfp_t denyflags = __GFP_NOWARN | __GFP_NORETRY; 1510187df5ddSRik van Riel gfp_t zoneflags = limit_gfp & GFP_ZONEMASK; 1511187df5ddSRik van Riel gfp_t result = huge_gfp & ~(allowflags | GFP_ZONEMASK); 1512187df5ddSRik van Riel 1513187df5ddSRik van Riel /* Allow allocations only from the originally specified zones. */ 1514187df5ddSRik van Riel result |= zoneflags; 151578cc8cdcSRik van Riel 151678cc8cdcSRik van Riel /* 151778cc8cdcSRik van Riel * Minimize the result gfp by taking the union with the deny flags, 151878cc8cdcSRik van Riel * and the intersection of the allow flags. 151978cc8cdcSRik van Riel */ 152078cc8cdcSRik van Riel result |= (limit_gfp & denyflags); 152178cc8cdcSRik van Riel result |= (huge_gfp & limit_gfp) & allowflags; 152278cc8cdcSRik van Riel 152378cc8cdcSRik van Riel return result; 152478cc8cdcSRik van Riel } 152578cc8cdcSRik van Riel 152672827e5cSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_hugefolio(gfp_t gfp, 1527800d8c63SKirill A. Shutemov struct shmem_inode_info *info, pgoff_t index) 1528800d8c63SKirill A. Shutemov { 1529800d8c63SKirill A. Shutemov struct vm_area_struct pvma; 15307b8d046fSMatthew Wilcox struct address_space *mapping = info->vfs_inode.i_mapping; 15317b8d046fSMatthew Wilcox pgoff_t hindex; 1532dfe98499SMatthew Wilcox (Oracle) struct folio *folio; 1533800d8c63SKirill A. Shutemov 15344620a06eSGeert Uytterhoeven hindex = round_down(index, HPAGE_PMD_NR); 15357b8d046fSMatthew Wilcox if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, 15367b8d046fSMatthew Wilcox XA_PRESENT)) 1537800d8c63SKirill A. Shutemov return NULL; 1538800d8c63SKirill A. Shutemov 1539800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, hindex); 1540dfe98499SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, HPAGE_PMD_ORDER, &pvma, 0, true); 1541800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 1542dfe98499SMatthew Wilcox (Oracle) if (!folio) 1543dcdf11eeSDavid Rientjes count_vm_event(THP_FILE_FALLBACK); 154472827e5cSMatthew Wilcox (Oracle) return folio; 154518a2f371SMel Gorman } 154618a2f371SMel Gorman 15470c023ef5SMatthew Wilcox (Oracle) static struct folio *shmem_alloc_folio(gfp_t gfp, 154818a2f371SMel Gorman struct shmem_inode_info *info, pgoff_t index) 154918a2f371SMel Gorman { 155018a2f371SMel Gorman struct vm_area_struct pvma; 15510c023ef5SMatthew Wilcox (Oracle) struct folio *folio; 155218a2f371SMel Gorman 1553800d8c63SKirill A. Shutemov shmem_pseudo_vma_init(&pvma, info, index); 15540c023ef5SMatthew Wilcox (Oracle) folio = vma_alloc_folio(gfp, 0, &pvma, 0, false); 1555800d8c63SKirill A. Shutemov shmem_pseudo_vma_destroy(&pvma); 155618a2f371SMel Gorman 15570c023ef5SMatthew Wilcox (Oracle) return folio; 155818a2f371SMel Gorman } 155918a2f371SMel Gorman 1560b1d0ec3aSMatthew Wilcox (Oracle) static struct folio *shmem_alloc_and_acct_folio(gfp_t gfp, struct inode *inode, 1561800d8c63SKirill A. Shutemov pgoff_t index, bool huge) 1562800d8c63SKirill A. Shutemov { 15630f079694SMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 156472827e5cSMatthew Wilcox (Oracle) struct folio *folio; 1565800d8c63SKirill A. Shutemov int nr; 1566800d8c63SKirill A. Shutemov int err = -ENOSPC; 1567800d8c63SKirill A. Shutemov 1568396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1569800d8c63SKirill A. Shutemov huge = false; 1570800d8c63SKirill A. Shutemov nr = huge ? HPAGE_PMD_NR : 1; 1571800d8c63SKirill A. Shutemov 15720f079694SMike Rapoport if (!shmem_inode_acct_block(inode, nr)) 1573800d8c63SKirill A. Shutemov goto failed; 1574800d8c63SKirill A. Shutemov 1575800d8c63SKirill A. Shutemov if (huge) 157672827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_hugefolio(gfp, info, index); 1577800d8c63SKirill A. Shutemov else 157872827e5cSMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, index); 157972827e5cSMatthew Wilcox (Oracle) if (folio) { 158072827e5cSMatthew Wilcox (Oracle) __folio_set_locked(folio); 158172827e5cSMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 1582b1d0ec3aSMatthew Wilcox (Oracle) return folio; 158375edd345SHugh Dickins } 158418a2f371SMel Gorman 1585800d8c63SKirill A. Shutemov err = -ENOMEM; 15860f079694SMike Rapoport shmem_inode_unacct_blocks(inode, nr); 1587800d8c63SKirill A. Shutemov failed: 1588800d8c63SKirill A. Shutemov return ERR_PTR(err); 15891da177e4SLinus Torvalds } 159071fe804bSLee Schermerhorn 15911da177e4SLinus Torvalds /* 1592bde05d1cSHugh Dickins * When a page is moved from swapcache to shmem filecache (either by the 1593fc26babbSMatthew Wilcox (Oracle) * usual swapin of shmem_get_folio_gfp(), or by the less common swapoff of 1594bde05d1cSHugh Dickins * shmem_unuse_inode()), it may have been read in earlier from swap, in 1595bde05d1cSHugh Dickins * ignorance of the mapping it belongs to. If that mapping has special 1596bde05d1cSHugh Dickins * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 1597bde05d1cSHugh Dickins * we may need to copy to a suitable page before moving to filecache. 1598bde05d1cSHugh Dickins * 1599bde05d1cSHugh Dickins * In a future release, this may well be extended to respect cpuset and 1600bde05d1cSHugh Dickins * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 1601bde05d1cSHugh Dickins * but for now it is a simple matter of zone. 1602bde05d1cSHugh Dickins */ 1603069d849cSMatthew Wilcox (Oracle) static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) 1604bde05d1cSHugh Dickins { 1605069d849cSMatthew Wilcox (Oracle) return folio_zonenum(folio) > gfp_zone(gfp); 1606bde05d1cSHugh Dickins } 1607bde05d1cSHugh Dickins 16080d698e25SMatthew Wilcox (Oracle) static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, 1609bde05d1cSHugh Dickins struct shmem_inode_info *info, pgoff_t index) 1610bde05d1cSHugh Dickins { 1611d21bba2bSMatthew Wilcox (Oracle) struct folio *old, *new; 1612bde05d1cSHugh Dickins struct address_space *swap_mapping; 1613c1cb20d4SYu Zhao swp_entry_t entry; 1614bde05d1cSHugh Dickins pgoff_t swap_index; 1615bde05d1cSHugh Dickins int error; 1616bde05d1cSHugh Dickins 16170d698e25SMatthew Wilcox (Oracle) old = *foliop; 1618907ea17eSMatthew Wilcox (Oracle) entry = folio_swap_entry(old); 1619c1cb20d4SYu Zhao swap_index = swp_offset(entry); 1620907ea17eSMatthew Wilcox (Oracle) swap_mapping = swap_address_space(entry); 1621bde05d1cSHugh Dickins 1622bde05d1cSHugh Dickins /* 1623bde05d1cSHugh Dickins * We have arrived here because our zones are constrained, so don't 1624bde05d1cSHugh Dickins * limit chance of success by further cpuset and node constraints. 1625bde05d1cSHugh Dickins */ 1626bde05d1cSHugh Dickins gfp &= ~GFP_CONSTRAINT_MASK; 1627907ea17eSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(old), old); 1628907ea17eSMatthew Wilcox (Oracle) new = shmem_alloc_folio(gfp, info, index); 1629907ea17eSMatthew Wilcox (Oracle) if (!new) 1630bde05d1cSHugh Dickins return -ENOMEM; 1631bde05d1cSHugh Dickins 1632907ea17eSMatthew Wilcox (Oracle) folio_get(new); 1633907ea17eSMatthew Wilcox (Oracle) folio_copy(new, old); 1634907ea17eSMatthew Wilcox (Oracle) flush_dcache_folio(new); 1635bde05d1cSHugh Dickins 1636907ea17eSMatthew Wilcox (Oracle) __folio_set_locked(new); 1637907ea17eSMatthew Wilcox (Oracle) __folio_set_swapbacked(new); 1638907ea17eSMatthew Wilcox (Oracle) folio_mark_uptodate(new); 1639907ea17eSMatthew Wilcox (Oracle) folio_set_swap_entry(new, entry); 1640907ea17eSMatthew Wilcox (Oracle) folio_set_swapcache(new); 1641bde05d1cSHugh Dickins 1642bde05d1cSHugh Dickins /* 1643bde05d1cSHugh Dickins * Our caller will very soon move newpage out of swapcache, but it's 1644bde05d1cSHugh Dickins * a nice clean interface for us to replace oldpage by newpage there. 1645bde05d1cSHugh Dickins */ 1646b93b0163SMatthew Wilcox xa_lock_irq(&swap_mapping->i_pages); 1647907ea17eSMatthew Wilcox (Oracle) error = shmem_replace_entry(swap_mapping, swap_index, old, new); 16480142ef6cSHugh Dickins if (!error) { 1649d21bba2bSMatthew Wilcox (Oracle) mem_cgroup_migrate(old, new); 1650907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_FILE_PAGES, 1); 1651907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(new, NR_SHMEM, 1); 1652907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_FILE_PAGES, -1); 1653907ea17eSMatthew Wilcox (Oracle) __lruvec_stat_mod_folio(old, NR_SHMEM, -1); 16540142ef6cSHugh Dickins } 1655b93b0163SMatthew Wilcox xa_unlock_irq(&swap_mapping->i_pages); 1656bde05d1cSHugh Dickins 16570142ef6cSHugh Dickins if (unlikely(error)) { 16580142ef6cSHugh Dickins /* 16590142ef6cSHugh Dickins * Is this possible? I think not, now that our callers check 16600142ef6cSHugh Dickins * both PageSwapCache and page_private after getting page lock; 16610142ef6cSHugh Dickins * but be defensive. Reverse old to newpage for clear and free. 16620142ef6cSHugh Dickins */ 1663907ea17eSMatthew Wilcox (Oracle) old = new; 16640142ef6cSHugh Dickins } else { 1665907ea17eSMatthew Wilcox (Oracle) folio_add_lru(new); 16660d698e25SMatthew Wilcox (Oracle) *foliop = new; 16670142ef6cSHugh Dickins } 1668bde05d1cSHugh Dickins 1669907ea17eSMatthew Wilcox (Oracle) folio_clear_swapcache(old); 1670907ea17eSMatthew Wilcox (Oracle) old->private = NULL; 1671bde05d1cSHugh Dickins 1672907ea17eSMatthew Wilcox (Oracle) folio_unlock(old); 1673907ea17eSMatthew Wilcox (Oracle) folio_put_refs(old, 2); 16740142ef6cSHugh Dickins return error; 1675bde05d1cSHugh Dickins } 1676bde05d1cSHugh Dickins 16776cec2b95SMiaohe Lin static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, 16786cec2b95SMiaohe Lin struct folio *folio, swp_entry_t swap) 16796cec2b95SMiaohe Lin { 16806cec2b95SMiaohe Lin struct address_space *mapping = inode->i_mapping; 16816cec2b95SMiaohe Lin struct shmem_inode_info *info = SHMEM_I(inode); 16826cec2b95SMiaohe Lin swp_entry_t swapin_error; 16836cec2b95SMiaohe Lin void *old; 16846cec2b95SMiaohe Lin 16856cec2b95SMiaohe Lin swapin_error = make_swapin_error_entry(&folio->page); 16866cec2b95SMiaohe Lin old = xa_cmpxchg_irq(&mapping->i_pages, index, 16876cec2b95SMiaohe Lin swp_to_radix_entry(swap), 16886cec2b95SMiaohe Lin swp_to_radix_entry(swapin_error), 0); 16896cec2b95SMiaohe Lin if (old != swp_to_radix_entry(swap)) 16906cec2b95SMiaohe Lin return; 16916cec2b95SMiaohe Lin 16926cec2b95SMiaohe Lin folio_wait_writeback(folio); 169375fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 16946cec2b95SMiaohe Lin spin_lock_irq(&info->lock); 16956cec2b95SMiaohe Lin /* 16966cec2b95SMiaohe Lin * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks won't 16976cec2b95SMiaohe Lin * be 0 when inode is released and thus trigger WARN_ON(inode->i_blocks) in 16986cec2b95SMiaohe Lin * shmem_evict_inode. 16996cec2b95SMiaohe Lin */ 17006cec2b95SMiaohe Lin info->alloced--; 17016cec2b95SMiaohe Lin info->swapped--; 17026cec2b95SMiaohe Lin shmem_recalc_inode(inode); 17036cec2b95SMiaohe Lin spin_unlock_irq(&info->lock); 17046cec2b95SMiaohe Lin swap_free(swap); 17056cec2b95SMiaohe Lin } 17066cec2b95SMiaohe Lin 1707bde05d1cSHugh Dickins /* 1708833de10fSMiaohe Lin * Swap in the folio pointed to by *foliop. 1709833de10fSMiaohe Lin * Caller has to make sure that *foliop contains a valid swapped folio. 1710833de10fSMiaohe Lin * Returns 0 and the folio in foliop if success. On failure, returns the 1711833de10fSMiaohe Lin * error code and NULL in *foliop. 17121da177e4SLinus Torvalds */ 1713da08e9b7SMatthew Wilcox (Oracle) static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 1714da08e9b7SMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, 1715c5bf121eSVineeth Remanan Pillai gfp_t gfp, struct vm_area_struct *vma, 17162b740303SSouptick Joarder vm_fault_t *fault_type) 17171da177e4SLinus Torvalds { 17181da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 171923f919d4SArnd Bergmann struct shmem_inode_info *info = SHMEM_I(inode); 172004f94e3fSDan Schatzberg struct mm_struct *charge_mm = vma ? vma->vm_mm : NULL; 1721da08e9b7SMatthew Wilcox (Oracle) struct folio *folio = NULL; 17221da177e4SLinus Torvalds swp_entry_t swap; 17231da177e4SLinus Torvalds int error; 17241da177e4SLinus Torvalds 1725da08e9b7SMatthew Wilcox (Oracle) VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); 1726da08e9b7SMatthew Wilcox (Oracle) swap = radix_to_swp_entry(*foliop); 1727da08e9b7SMatthew Wilcox (Oracle) *foliop = NULL; 172854af6042SHugh Dickins 17296cec2b95SMiaohe Lin if (is_swapin_error_entry(swap)) 17306cec2b95SMiaohe Lin return -EIO; 17316cec2b95SMiaohe Lin 17321da177e4SLinus Torvalds /* Look it up and read it in.. */ 17335739a81cSMatthew Wilcox (Oracle) folio = swap_cache_get_folio(swap, NULL, 0); 17345739a81cSMatthew Wilcox (Oracle) if (!folio) { 17359e18eb29SAndres Lagar-Cavilla /* Or update major stats only when swapin succeeds?? */ 17369e18eb29SAndres Lagar-Cavilla if (fault_type) { 173768da9f05SHugh Dickins *fault_type |= VM_FAULT_MAJOR; 17389e18eb29SAndres Lagar-Cavilla count_vm_event(PGMAJFAULT); 17392262185cSRoman Gushchin count_memcg_event_mm(charge_mm, PGMAJFAULT); 17409e18eb29SAndres Lagar-Cavilla } 17419e18eb29SAndres Lagar-Cavilla /* Here we actually start the io */ 17425739a81cSMatthew Wilcox (Oracle) folio = shmem_swapin(swap, gfp, info, index); 17435739a81cSMatthew Wilcox (Oracle) if (!folio) { 17441da177e4SLinus Torvalds error = -ENOMEM; 174554af6042SHugh Dickins goto failed; 1746285b2c4fSHugh Dickins } 17471da177e4SLinus Torvalds } 17481da177e4SLinus Torvalds 1749833de10fSMiaohe Lin /* We have to do this with folio locked to prevent races */ 1750da08e9b7SMatthew Wilcox (Oracle) folio_lock(folio); 1751da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_swapcache(folio) || 1752da08e9b7SMatthew Wilcox (Oracle) folio_swap_entry(folio).val != swap.val || 1753d1899228SHugh Dickins !shmem_confirm_swap(mapping, index, swap)) { 1754c5bf121eSVineeth Remanan Pillai error = -EEXIST; 1755d1899228SHugh Dickins goto unlock; 1756bde05d1cSHugh Dickins } 1757da08e9b7SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) { 17581da177e4SLinus Torvalds error = -EIO; 175954af6042SHugh Dickins goto failed; 176054af6042SHugh Dickins } 1761da08e9b7SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 176254af6042SHugh Dickins 17638a84802eSSteven Price /* 17648a84802eSSteven Price * Some architectures may have to restore extra metadata to the 1765da08e9b7SMatthew Wilcox (Oracle) * folio after reading from swap. 17668a84802eSSteven Price */ 1767da08e9b7SMatthew Wilcox (Oracle) arch_swap_restore(swap, folio); 17688a84802eSSteven Price 1769069d849cSMatthew Wilcox (Oracle) if (shmem_should_replace_folio(folio, gfp)) { 17700d698e25SMatthew Wilcox (Oracle) error = shmem_replace_folio(&folio, gfp, info, index); 1771bde05d1cSHugh Dickins if (error) 177254af6042SHugh Dickins goto failed; 17731da177e4SLinus Torvalds } 17741da177e4SLinus Torvalds 1775b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, index, 17763fea5a49SJohannes Weiner swp_to_radix_entry(swap), gfp, 17773fea5a49SJohannes Weiner charge_mm); 177854af6042SHugh Dickins if (error) 177954af6042SHugh Dickins goto failed; 178054af6042SHugh Dickins 17814595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 178254af6042SHugh Dickins info->swapped--; 178354af6042SHugh Dickins shmem_recalc_inode(inode); 17844595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 178527ab7006SHugh Dickins 178666d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1787da08e9b7SMatthew Wilcox (Oracle) folio_mark_accessed(folio); 178866d2f4d2SHugh Dickins 178975fa68a5SMatthew Wilcox (Oracle) delete_from_swap_cache(folio); 1790da08e9b7SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 179127ab7006SHugh Dickins swap_free(swap); 179227ab7006SHugh Dickins 1793da08e9b7SMatthew Wilcox (Oracle) *foliop = folio; 1794c5bf121eSVineeth Remanan Pillai return 0; 1795c5bf121eSVineeth Remanan Pillai failed: 1796c5bf121eSVineeth Remanan Pillai if (!shmem_confirm_swap(mapping, index, swap)) 1797c5bf121eSVineeth Remanan Pillai error = -EEXIST; 17986cec2b95SMiaohe Lin if (error == -EIO) 17996cec2b95SMiaohe Lin shmem_set_folio_swapin_error(inode, index, folio, swap); 1800c5bf121eSVineeth Remanan Pillai unlock: 1801da08e9b7SMatthew Wilcox (Oracle) if (folio) { 1802da08e9b7SMatthew Wilcox (Oracle) folio_unlock(folio); 1803da08e9b7SMatthew Wilcox (Oracle) folio_put(folio); 1804c5bf121eSVineeth Remanan Pillai } 1805c5bf121eSVineeth Remanan Pillai 1806c5bf121eSVineeth Remanan Pillai return error; 1807c5bf121eSVineeth Remanan Pillai } 1808c5bf121eSVineeth Remanan Pillai 1809c5bf121eSVineeth Remanan Pillai /* 1810fc26babbSMatthew Wilcox (Oracle) * shmem_get_folio_gfp - find page in cache, or get from swap, or allocate 1811c5bf121eSVineeth Remanan Pillai * 1812c5bf121eSVineeth Remanan Pillai * If we allocate a new one we do not mark it dirty. That's up to the 1813c5bf121eSVineeth Remanan Pillai * vm. If we swap it in we mark it dirty since we also free the swap 1814c5bf121eSVineeth Remanan Pillai * entry since a page cannot live in both the swap and page cache. 1815c5bf121eSVineeth Remanan Pillai * 1816c949b097SAxel Rasmussen * vma, vmf, and fault_type are only supplied by shmem_fault: 1817c5bf121eSVineeth Remanan Pillai * otherwise they are NULL. 1818c5bf121eSVineeth Remanan Pillai */ 1819fc26babbSMatthew Wilcox (Oracle) static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, 1820fc26babbSMatthew Wilcox (Oracle) struct folio **foliop, enum sgp_type sgp, gfp_t gfp, 1821c5bf121eSVineeth Remanan Pillai struct vm_area_struct *vma, struct vm_fault *vmf, 1822c5bf121eSVineeth Remanan Pillai vm_fault_t *fault_type) 1823c5bf121eSVineeth Remanan Pillai { 1824c5bf121eSVineeth Remanan Pillai struct address_space *mapping = inode->i_mapping; 1825c5bf121eSVineeth Remanan Pillai struct shmem_inode_info *info = SHMEM_I(inode); 1826c5bf121eSVineeth Remanan Pillai struct shmem_sb_info *sbinfo; 1827c5bf121eSVineeth Remanan Pillai struct mm_struct *charge_mm; 1828b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 1829c5bf121eSVineeth Remanan Pillai pgoff_t hindex = index; 1830164cc4feSRik van Riel gfp_t huge_gfp; 1831c5bf121eSVineeth Remanan Pillai int error; 1832c5bf121eSVineeth Remanan Pillai int once = 0; 1833c5bf121eSVineeth Remanan Pillai int alloced = 0; 1834c5bf121eSVineeth Remanan Pillai 1835c5bf121eSVineeth Remanan Pillai if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 1836c5bf121eSVineeth Remanan Pillai return -EFBIG; 1837c5bf121eSVineeth Remanan Pillai repeat: 1838c5bf121eSVineeth Remanan Pillai if (sgp <= SGP_CACHE && 1839c5bf121eSVineeth Remanan Pillai ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1840c5bf121eSVineeth Remanan Pillai return -EINVAL; 1841c5bf121eSVineeth Remanan Pillai } 1842c5bf121eSVineeth Remanan Pillai 1843c5bf121eSVineeth Remanan Pillai sbinfo = SHMEM_SB(inode->i_sb); 184404f94e3fSDan Schatzberg charge_mm = vma ? vma->vm_mm : NULL; 1845c5bf121eSVineeth Remanan Pillai 1846b1d0ec3aSMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, index, FGP_ENTRY | FGP_LOCK, 0); 1847b1d0ec3aSMatthew Wilcox (Oracle) if (folio && vma && userfaultfd_minor(vma)) { 1848b1d0ec3aSMatthew Wilcox (Oracle) if (!xa_is_value(folio)) { 1849b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1850b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1851c949b097SAxel Rasmussen } 1852c949b097SAxel Rasmussen *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); 1853c949b097SAxel Rasmussen return 0; 1854c949b097SAxel Rasmussen } 1855c949b097SAxel Rasmussen 1856b1d0ec3aSMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 1857da08e9b7SMatthew Wilcox (Oracle) error = shmem_swapin_folio(inode, index, &folio, 1858c5bf121eSVineeth Remanan Pillai sgp, gfp, vma, fault_type); 1859c5bf121eSVineeth Remanan Pillai if (error == -EEXIST) 1860c5bf121eSVineeth Remanan Pillai goto repeat; 1861c5bf121eSVineeth Remanan Pillai 1862fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 1863c5bf121eSVineeth Remanan Pillai return error; 1864c5bf121eSVineeth Remanan Pillai } 1865c5bf121eSVineeth Remanan Pillai 1866b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 1867b1d0ec3aSMatthew Wilcox (Oracle) hindex = folio->index; 1868acdd9f8eSHugh Dickins if (sgp == SGP_WRITE) 1869b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 1870b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 1871acdd9f8eSHugh Dickins goto out; 1872fc26babbSMatthew Wilcox (Oracle) /* fallocated folio */ 1873c5bf121eSVineeth Remanan Pillai if (sgp != SGP_READ) 1874c5bf121eSVineeth Remanan Pillai goto clear; 1875b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 1876b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 1877c5bf121eSVineeth Remanan Pillai } 1878c5bf121eSVineeth Remanan Pillai 1879c5bf121eSVineeth Remanan Pillai /* 1880fc26babbSMatthew Wilcox (Oracle) * SGP_READ: succeed on hole, with NULL folio, letting caller zero. 1881fc26babbSMatthew Wilcox (Oracle) * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. 1882acdd9f8eSHugh Dickins */ 1883fc26babbSMatthew Wilcox (Oracle) *foliop = NULL; 1884acdd9f8eSHugh Dickins if (sgp == SGP_READ) 1885acdd9f8eSHugh Dickins return 0; 1886acdd9f8eSHugh Dickins if (sgp == SGP_NOALLOC) 1887acdd9f8eSHugh Dickins return -ENOENT; 1888acdd9f8eSHugh Dickins 1889acdd9f8eSHugh Dickins /* 1890acdd9f8eSHugh Dickins * Fast cache lookup and swap lookup did not find it: allocate. 1891c5bf121eSVineeth Remanan Pillai */ 1892c5bf121eSVineeth Remanan Pillai 1893cfda0526SMike Rapoport if (vma && userfaultfd_missing(vma)) { 1894cfda0526SMike Rapoport *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 1895cfda0526SMike Rapoport return 0; 1896cfda0526SMike Rapoport } 1897cfda0526SMike Rapoport 18987c6c6cc4SZach O'Keefe if (!shmem_is_huge(vma, inode, index, false)) 1899800d8c63SKirill A. Shutemov goto alloc_nohuge; 190027d80fa2SKees Cook 1901164cc4feSRik van Riel huge_gfp = vma_thp_gfp_mask(vma); 190278cc8cdcSRik van Riel huge_gfp = limit_gfp_mask(huge_gfp, gfp); 1903b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(huge_gfp, inode, index, true); 1904b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1905c5bf121eSVineeth Remanan Pillai alloc_nohuge: 1906b1d0ec3aSMatthew Wilcox (Oracle) folio = shmem_alloc_and_acct_folio(gfp, inode, index, false); 190754af6042SHugh Dickins } 1908b1d0ec3aSMatthew Wilcox (Oracle) if (IS_ERR(folio)) { 1909779750d2SKirill A. Shutemov int retry = 5; 1910c5bf121eSVineeth Remanan Pillai 1911b1d0ec3aSMatthew Wilcox (Oracle) error = PTR_ERR(folio); 1912b1d0ec3aSMatthew Wilcox (Oracle) folio = NULL; 1913779750d2SKirill A. Shutemov if (error != -ENOSPC) 1914c5bf121eSVineeth Remanan Pillai goto unlock; 1915779750d2SKirill A. Shutemov /* 1916fc26babbSMatthew Wilcox (Oracle) * Try to reclaim some space by splitting a large folio 1917779750d2SKirill A. Shutemov * beyond i_size on the filesystem. 1918779750d2SKirill A. Shutemov */ 1919779750d2SKirill A. Shutemov while (retry--) { 1920779750d2SKirill A. Shutemov int ret; 1921c5bf121eSVineeth Remanan Pillai 1922779750d2SKirill A. Shutemov ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); 1923779750d2SKirill A. Shutemov if (ret == SHRINK_STOP) 1924779750d2SKirill A. Shutemov break; 1925779750d2SKirill A. Shutemov if (ret) 1926779750d2SKirill A. Shutemov goto alloc_nohuge; 1927779750d2SKirill A. Shutemov } 1928c5bf121eSVineeth Remanan Pillai goto unlock; 1929800d8c63SKirill A. Shutemov } 1930800d8c63SKirill A. Shutemov 1931b1d0ec3aSMatthew Wilcox (Oracle) hindex = round_down(index, folio_nr_pages(folio)); 1932800d8c63SKirill A. Shutemov 193366d2f4d2SHugh Dickins if (sgp == SGP_WRITE) 1934b1d0ec3aSMatthew Wilcox (Oracle) __folio_set_referenced(folio); 193566d2f4d2SHugh Dickins 1936b7dd44a1SMatthew Wilcox (Oracle) error = shmem_add_to_page_cache(folio, mapping, hindex, 19373fea5a49SJohannes Weiner NULL, gfp & GFP_RECLAIM_MASK, 19383fea5a49SJohannes Weiner charge_mm); 19393fea5a49SJohannes Weiner if (error) 1940800d8c63SKirill A. Shutemov goto unacct; 1941b1d0ec3aSMatthew Wilcox (Oracle) folio_add_lru(folio); 194254af6042SHugh Dickins 19434595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1944b1d0ec3aSMatthew Wilcox (Oracle) info->alloced += folio_nr_pages(folio); 1945fa020a2bSAndrew Morton inode->i_blocks += (blkcnt_t)BLOCKS_PER_PAGE << folio_order(folio); 194654af6042SHugh Dickins shmem_recalc_inode(inode); 19474595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 19481635f6a7SHugh Dickins alloced = true; 194954af6042SHugh Dickins 1950b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_pmd_mappable(folio) && 1951779750d2SKirill A. Shutemov DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 1952fc26babbSMatthew Wilcox (Oracle) folio_next_index(folio) - 1) { 1953779750d2SKirill A. Shutemov /* 1954fc26babbSMatthew Wilcox (Oracle) * Part of the large folio is beyond i_size: subject 1955779750d2SKirill A. Shutemov * to shrink under memory pressure. 1956779750d2SKirill A. Shutemov */ 1957779750d2SKirill A. Shutemov spin_lock(&sbinfo->shrinklist_lock); 1958d041353dSCong Wang /* 1959d041353dSCong Wang * _careful to defend against unlocked access to 1960d041353dSCong Wang * ->shrink_list in shmem_unused_huge_shrink() 1961d041353dSCong Wang */ 1962d041353dSCong Wang if (list_empty_careful(&info->shrinklist)) { 1963779750d2SKirill A. Shutemov list_add_tail(&info->shrinklist, 1964779750d2SKirill A. Shutemov &sbinfo->shrinklist); 1965779750d2SKirill A. Shutemov sbinfo->shrinklist_len++; 1966779750d2SKirill A. Shutemov } 1967779750d2SKirill A. Shutemov spin_unlock(&sbinfo->shrinklist_lock); 1968779750d2SKirill A. Shutemov } 1969779750d2SKirill A. Shutemov 1970ec9516fbSHugh Dickins /* 1971fc26babbSMatthew Wilcox (Oracle) * Let SGP_FALLOC use the SGP_WRITE optimization on a new folio. 19721635f6a7SHugh Dickins */ 19731635f6a7SHugh Dickins if (sgp == SGP_FALLOC) 19741635f6a7SHugh Dickins sgp = SGP_WRITE; 19751635f6a7SHugh Dickins clear: 19761635f6a7SHugh Dickins /* 1977fc26babbSMatthew Wilcox (Oracle) * Let SGP_WRITE caller clear ends if write does not fill folio; 1978fc26babbSMatthew Wilcox (Oracle) * but SGP_FALLOC on a folio fallocated earlier must initialize 19791635f6a7SHugh Dickins * it now, lest undo on failure cancel our earlier guarantee. 1980ec9516fbSHugh Dickins */ 1981b1d0ec3aSMatthew Wilcox (Oracle) if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) { 1982b1d0ec3aSMatthew Wilcox (Oracle) long i, n = folio_nr_pages(folio); 1983800d8c63SKirill A. Shutemov 1984b1d0ec3aSMatthew Wilcox (Oracle) for (i = 0; i < n; i++) 1985b1d0ec3aSMatthew Wilcox (Oracle) clear_highpage(folio_page(folio, i)); 1986b1d0ec3aSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 1987b1d0ec3aSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 1988ec9516fbSHugh Dickins } 1989bde05d1cSHugh Dickins 199054af6042SHugh Dickins /* Perhaps the file has been truncated since we checked */ 199175edd345SHugh Dickins if (sgp <= SGP_CACHE && 199209cbfeafSKirill A. Shutemov ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 1993267a4c76SHugh Dickins if (alloced) { 1994b1d0ec3aSMatthew Wilcox (Oracle) folio_clear_dirty(folio); 1995b1d0ec3aSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 19964595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 1997267a4c76SHugh Dickins shmem_recalc_inode(inode); 19984595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 1999267a4c76SHugh Dickins } 200054af6042SHugh Dickins error = -EINVAL; 2001267a4c76SHugh Dickins goto unlock; 2002ff36b801SShaohua Li } 200363ec1973SMatthew Wilcox (Oracle) out: 2004fc26babbSMatthew Wilcox (Oracle) *foliop = folio; 200554af6042SHugh Dickins return 0; 2006d00806b1SNick Piggin 2007d0217ac0SNick Piggin /* 200854af6042SHugh Dickins * Error recovery. 20091da177e4SLinus Torvalds */ 201054af6042SHugh Dickins unacct: 2011b1d0ec3aSMatthew Wilcox (Oracle) shmem_inode_unacct_blocks(inode, folio_nr_pages(folio)); 2012800d8c63SKirill A. Shutemov 2013b1d0ec3aSMatthew Wilcox (Oracle) if (folio_test_large(folio)) { 2014b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2015b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 2016800d8c63SKirill A. Shutemov goto alloc_nohuge; 2017800d8c63SKirill A. Shutemov } 2018d1899228SHugh Dickins unlock: 2019b1d0ec3aSMatthew Wilcox (Oracle) if (folio) { 2020b1d0ec3aSMatthew Wilcox (Oracle) folio_unlock(folio); 2021b1d0ec3aSMatthew Wilcox (Oracle) folio_put(folio); 202254af6042SHugh Dickins } 202354af6042SHugh Dickins if (error == -ENOSPC && !once++) { 20244595ef88SKirill A. Shutemov spin_lock_irq(&info->lock); 202554af6042SHugh Dickins shmem_recalc_inode(inode); 20264595ef88SKirill A. Shutemov spin_unlock_irq(&info->lock); 20271da177e4SLinus Torvalds goto repeat; 2028d8dc74f2SAdrian Bunk } 20297f4446eeSMatthew Wilcox if (error == -EEXIST) 203054af6042SHugh Dickins goto repeat; 203154af6042SHugh Dickins return error; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20344e1fc793SMatthew Wilcox (Oracle) int shmem_get_folio(struct inode *inode, pgoff_t index, struct folio **foliop, 20354e1fc793SMatthew Wilcox (Oracle) enum sgp_type sgp) 20364e1fc793SMatthew Wilcox (Oracle) { 20374e1fc793SMatthew Wilcox (Oracle) return shmem_get_folio_gfp(inode, index, foliop, sgp, 20384e1fc793SMatthew Wilcox (Oracle) mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); 20394e1fc793SMatthew Wilcox (Oracle) } 20404e1fc793SMatthew Wilcox (Oracle) 204110d20bd2SLinus Torvalds /* 204210d20bd2SLinus Torvalds * This is like autoremove_wake_function, but it removes the wait queue 204310d20bd2SLinus Torvalds * entry unconditionally - even if something else had already woken the 204410d20bd2SLinus Torvalds * target. 204510d20bd2SLinus Torvalds */ 2046ac6424b9SIngo Molnar static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) 204710d20bd2SLinus Torvalds { 204810d20bd2SLinus Torvalds int ret = default_wake_function(wait, mode, sync, key); 20492055da97SIngo Molnar list_del_init(&wait->entry); 205010d20bd2SLinus Torvalds return ret; 205110d20bd2SLinus Torvalds } 205210d20bd2SLinus Torvalds 205320acce67SSouptick Joarder static vm_fault_t shmem_fault(struct vm_fault *vmf) 20541da177e4SLinus Torvalds { 205511bac800SDave Jiang struct vm_area_struct *vma = vmf->vma; 2056496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 20579e18eb29SAndres Lagar-Cavilla gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 205868a54100SMatthew Wilcox (Oracle) struct folio *folio = NULL; 205920acce67SSouptick Joarder int err; 206020acce67SSouptick Joarder vm_fault_t ret = VM_FAULT_LOCKED; 20611da177e4SLinus Torvalds 2062f00cdc6dSHugh Dickins /* 2063f00cdc6dSHugh Dickins * Trinity finds that probing a hole which tmpfs is punching can 2064f00cdc6dSHugh Dickins * prevent the hole-punch from ever completing: which in turn 20659608703eSJan Kara * locks writers out with its hold on i_rwsem. So refrain from 20668e205f77SHugh Dickins * faulting pages into the hole while it's being punched. Although 20678e205f77SHugh Dickins * shmem_undo_range() does remove the additions, it may be unable to 20688e205f77SHugh Dickins * keep up, as each new page needs its own unmap_mapping_range() call, 20698e205f77SHugh Dickins * and the i_mmap tree grows ever slower to scan if new vmas are added. 20708e205f77SHugh Dickins * 20718e205f77SHugh Dickins * It does not matter if we sometimes reach this check just before the 20728e205f77SHugh Dickins * hole-punch begins, so that one fault then races with the punch: 20738e205f77SHugh Dickins * we just need to make racing faults a rare case. 20748e205f77SHugh Dickins * 20758e205f77SHugh Dickins * The implementation below would be much simpler if we just used a 20769608703eSJan Kara * standard mutex or completion: but we cannot take i_rwsem in fault, 20778e205f77SHugh Dickins * and bloating every shmem inode for this unlikely case would be sad. 2078f00cdc6dSHugh Dickins */ 2079f00cdc6dSHugh Dickins if (unlikely(inode->i_private)) { 2080f00cdc6dSHugh Dickins struct shmem_falloc *shmem_falloc; 2081f00cdc6dSHugh Dickins 2082f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2083f00cdc6dSHugh Dickins shmem_falloc = inode->i_private; 20848e205f77SHugh Dickins if (shmem_falloc && 20858e205f77SHugh Dickins shmem_falloc->waitq && 20868e205f77SHugh Dickins vmf->pgoff >= shmem_falloc->start && 20878e205f77SHugh Dickins vmf->pgoff < shmem_falloc->next) { 20888897c1b1SKirill A. Shutemov struct file *fpin; 20898e205f77SHugh Dickins wait_queue_head_t *shmem_falloc_waitq; 209010d20bd2SLinus Torvalds DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 20918e205f77SHugh Dickins 20928e205f77SHugh Dickins ret = VM_FAULT_NOPAGE; 20938897c1b1SKirill A. Shutemov fpin = maybe_unlock_mmap_for_io(vmf, NULL); 20948897c1b1SKirill A. Shutemov if (fpin) 20958e205f77SHugh Dickins ret = VM_FAULT_RETRY; 20968e205f77SHugh Dickins 20978e205f77SHugh Dickins shmem_falloc_waitq = shmem_falloc->waitq; 20988e205f77SHugh Dickins prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 20998e205f77SHugh Dickins TASK_UNINTERRUPTIBLE); 21008e205f77SHugh Dickins spin_unlock(&inode->i_lock); 21018e205f77SHugh Dickins schedule(); 21028e205f77SHugh Dickins 21038e205f77SHugh Dickins /* 21048e205f77SHugh Dickins * shmem_falloc_waitq points into the shmem_fallocate() 21058e205f77SHugh Dickins * stack of the hole-punching task: shmem_falloc_waitq 21068e205f77SHugh Dickins * is usually invalid by the time we reach here, but 21078e205f77SHugh Dickins * finish_wait() does not dereference it in that case; 21088e205f77SHugh Dickins * though i_lock needed lest racing with wake_up_all(). 21098e205f77SHugh Dickins */ 21108e205f77SHugh Dickins spin_lock(&inode->i_lock); 21118e205f77SHugh Dickins finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 21128e205f77SHugh Dickins spin_unlock(&inode->i_lock); 21138897c1b1SKirill A. Shutemov 21148897c1b1SKirill A. Shutemov if (fpin) 21158897c1b1SKirill A. Shutemov fput(fpin); 21168e205f77SHugh Dickins return ret; 2117f00cdc6dSHugh Dickins } 21188e205f77SHugh Dickins spin_unlock(&inode->i_lock); 2119f00cdc6dSHugh Dickins } 2120f00cdc6dSHugh Dickins 212168a54100SMatthew Wilcox (Oracle) err = shmem_get_folio_gfp(inode, vmf->pgoff, &folio, SGP_CACHE, 2122cfda0526SMike Rapoport gfp, vma, vmf, &ret); 212320acce67SSouptick Joarder if (err) 212420acce67SSouptick Joarder return vmf_error(err); 212568a54100SMatthew Wilcox (Oracle) if (folio) 212668a54100SMatthew Wilcox (Oracle) vmf->page = folio_file_page(folio, vmf->pgoff); 212768da9f05SHugh Dickins return ret; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 2130c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 2131c01d5b30SHugh Dickins unsigned long uaddr, unsigned long len, 2132c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 2133c01d5b30SHugh Dickins { 2134c01d5b30SHugh Dickins unsigned long (*get_area)(struct file *, 2135c01d5b30SHugh Dickins unsigned long, unsigned long, unsigned long, unsigned long); 2136c01d5b30SHugh Dickins unsigned long addr; 2137c01d5b30SHugh Dickins unsigned long offset; 2138c01d5b30SHugh Dickins unsigned long inflated_len; 2139c01d5b30SHugh Dickins unsigned long inflated_addr; 2140c01d5b30SHugh Dickins unsigned long inflated_offset; 2141c01d5b30SHugh Dickins 2142c01d5b30SHugh Dickins if (len > TASK_SIZE) 2143c01d5b30SHugh Dickins return -ENOMEM; 2144c01d5b30SHugh Dickins 2145c01d5b30SHugh Dickins get_area = current->mm->get_unmapped_area; 2146c01d5b30SHugh Dickins addr = get_area(file, uaddr, len, pgoff, flags); 2147c01d5b30SHugh Dickins 2148396bcc52SMatthew Wilcox (Oracle) if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2149c01d5b30SHugh Dickins return addr; 2150c01d5b30SHugh Dickins if (IS_ERR_VALUE(addr)) 2151c01d5b30SHugh Dickins return addr; 2152c01d5b30SHugh Dickins if (addr & ~PAGE_MASK) 2153c01d5b30SHugh Dickins return addr; 2154c01d5b30SHugh Dickins if (addr > TASK_SIZE - len) 2155c01d5b30SHugh Dickins return addr; 2156c01d5b30SHugh Dickins 2157c01d5b30SHugh Dickins if (shmem_huge == SHMEM_HUGE_DENY) 2158c01d5b30SHugh Dickins return addr; 2159c01d5b30SHugh Dickins if (len < HPAGE_PMD_SIZE) 2160c01d5b30SHugh Dickins return addr; 2161c01d5b30SHugh Dickins if (flags & MAP_FIXED) 2162c01d5b30SHugh Dickins return addr; 2163c01d5b30SHugh Dickins /* 2164c01d5b30SHugh Dickins * Our priority is to support MAP_SHARED mapped hugely; 2165c01d5b30SHugh Dickins * and support MAP_PRIVATE mapped hugely too, until it is COWed. 216699158997SKirill A. Shutemov * But if caller specified an address hint and we allocated area there 216799158997SKirill A. Shutemov * successfully, respect that as before. 2168c01d5b30SHugh Dickins */ 216999158997SKirill A. Shutemov if (uaddr == addr) 2170c01d5b30SHugh Dickins return addr; 2171c01d5b30SHugh Dickins 2172c01d5b30SHugh Dickins if (shmem_huge != SHMEM_HUGE_FORCE) { 2173c01d5b30SHugh Dickins struct super_block *sb; 2174c01d5b30SHugh Dickins 2175c01d5b30SHugh Dickins if (file) { 2176c01d5b30SHugh Dickins VM_BUG_ON(file->f_op != &shmem_file_operations); 2177c01d5b30SHugh Dickins sb = file_inode(file)->i_sb; 2178c01d5b30SHugh Dickins } else { 2179c01d5b30SHugh Dickins /* 2180c01d5b30SHugh Dickins * Called directly from mm/mmap.c, or drivers/char/mem.c 2181c01d5b30SHugh Dickins * for "/dev/zero", to create a shared anonymous object. 2182c01d5b30SHugh Dickins */ 2183c01d5b30SHugh Dickins if (IS_ERR(shm_mnt)) 2184c01d5b30SHugh Dickins return addr; 2185c01d5b30SHugh Dickins sb = shm_mnt->mnt_sb; 2186c01d5b30SHugh Dickins } 21873089bf61SToshi Kani if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) 2188c01d5b30SHugh Dickins return addr; 2189c01d5b30SHugh Dickins } 2190c01d5b30SHugh Dickins 2191c01d5b30SHugh Dickins offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); 2192c01d5b30SHugh Dickins if (offset && offset + len < 2 * HPAGE_PMD_SIZE) 2193c01d5b30SHugh Dickins return addr; 2194c01d5b30SHugh Dickins if ((addr & (HPAGE_PMD_SIZE-1)) == offset) 2195c01d5b30SHugh Dickins return addr; 2196c01d5b30SHugh Dickins 2197c01d5b30SHugh Dickins inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; 2198c01d5b30SHugh Dickins if (inflated_len > TASK_SIZE) 2199c01d5b30SHugh Dickins return addr; 2200c01d5b30SHugh Dickins if (inflated_len < len) 2201c01d5b30SHugh Dickins return addr; 2202c01d5b30SHugh Dickins 220399158997SKirill A. Shutemov inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); 2204c01d5b30SHugh Dickins if (IS_ERR_VALUE(inflated_addr)) 2205c01d5b30SHugh Dickins return addr; 2206c01d5b30SHugh Dickins if (inflated_addr & ~PAGE_MASK) 2207c01d5b30SHugh Dickins return addr; 2208c01d5b30SHugh Dickins 2209c01d5b30SHugh Dickins inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); 2210c01d5b30SHugh Dickins inflated_addr += offset - inflated_offset; 2211c01d5b30SHugh Dickins if (inflated_offset > offset) 2212c01d5b30SHugh Dickins inflated_addr += HPAGE_PMD_SIZE; 2213c01d5b30SHugh Dickins 2214c01d5b30SHugh Dickins if (inflated_addr > TASK_SIZE - len) 2215c01d5b30SHugh Dickins return addr; 2216c01d5b30SHugh Dickins return inflated_addr; 2217c01d5b30SHugh Dickins } 2218c01d5b30SHugh Dickins 22191da177e4SLinus Torvalds #ifdef CONFIG_NUMA 222041ffe5d5SHugh Dickins static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 22211da177e4SLinus Torvalds { 2222496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 222341ffe5d5SHugh Dickins return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 22241da177e4SLinus Torvalds } 22251da177e4SLinus Torvalds 2226d8dc74f2SAdrian Bunk static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2227d8dc74f2SAdrian Bunk unsigned long addr) 22281da177e4SLinus Torvalds { 2229496ad9aaSAl Viro struct inode *inode = file_inode(vma->vm_file); 223041ffe5d5SHugh Dickins pgoff_t index; 22311da177e4SLinus Torvalds 223241ffe5d5SHugh Dickins index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 223341ffe5d5SHugh Dickins return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 22341da177e4SLinus Torvalds } 22351da177e4SLinus Torvalds #endif 22361da177e4SLinus Torvalds 2237d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 22381da177e4SLinus Torvalds { 2239496ad9aaSAl Viro struct inode *inode = file_inode(file); 22401da177e4SLinus Torvalds struct shmem_inode_info *info = SHMEM_I(inode); 22411da177e4SLinus Torvalds int retval = -ENOMEM; 22421da177e4SLinus Torvalds 2243ea0dfeb4SHugh Dickins /* 2244ea0dfeb4SHugh Dickins * What serializes the accesses to info->flags? 2245ea0dfeb4SHugh Dickins * ipc_lock_object() when called from shmctl_do_lock(), 2246ea0dfeb4SHugh Dickins * no serialization needed when called from shm_destroy(). 2247ea0dfeb4SHugh Dickins */ 22481da177e4SLinus Torvalds if (lock && !(info->flags & VM_LOCKED)) { 2249d7c9e99aSAlexey Gladkov if (!user_shm_lock(inode->i_size, ucounts)) 22501da177e4SLinus Torvalds goto out_nomem; 22511da177e4SLinus Torvalds info->flags |= VM_LOCKED; 225289e004eaSLee Schermerhorn mapping_set_unevictable(file->f_mapping); 22531da177e4SLinus Torvalds } 2254d7c9e99aSAlexey Gladkov if (!lock && (info->flags & VM_LOCKED) && ucounts) { 2255d7c9e99aSAlexey Gladkov user_shm_unlock(inode->i_size, ucounts); 22561da177e4SLinus Torvalds info->flags &= ~VM_LOCKED; 225789e004eaSLee Schermerhorn mapping_clear_unevictable(file->f_mapping); 22581da177e4SLinus Torvalds } 22591da177e4SLinus Torvalds retval = 0; 226089e004eaSLee Schermerhorn 22611da177e4SLinus Torvalds out_nomem: 22621da177e4SLinus Torvalds return retval; 22631da177e4SLinus Torvalds } 22641da177e4SLinus Torvalds 22659b83a6a8SAdrian Bunk static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 22661da177e4SLinus Torvalds { 2267ab3948f5SJoel Fernandes (Google) struct shmem_inode_info *info = SHMEM_I(file_inode(file)); 226822247efdSPeter Xu int ret; 2269ab3948f5SJoel Fernandes (Google) 227022247efdSPeter Xu ret = seal_check_future_write(info->seals, vma); 227122247efdSPeter Xu if (ret) 227222247efdSPeter Xu return ret; 2273ab3948f5SJoel Fernandes (Google) 227451b0bff2SCatalin Marinas /* arm64 - allow memory tagging on RAM-based files */ 227551b0bff2SCatalin Marinas vma->vm_flags |= VM_MTE_ALLOWED; 227651b0bff2SCatalin Marinas 22771da177e4SLinus Torvalds file_accessed(file); 22781da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 22791da177e4SLinus Torvalds return 0; 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds 2282cb241339SHugh Dickins #ifdef CONFIG_TMPFS_XATTR 2283cb241339SHugh Dickins static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 2284cb241339SHugh Dickins 2285cb241339SHugh Dickins /* 2286cb241339SHugh Dickins * chattr's fsflags are unrelated to extended attributes, 2287cb241339SHugh Dickins * but tmpfs has chosen to enable them under the same config option. 2288cb241339SHugh Dickins */ 2289cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2290e408e695STheodore Ts'o { 2291cb241339SHugh Dickins unsigned int i_flags = 0; 2292cb241339SHugh Dickins 2293cb241339SHugh Dickins if (fsflags & FS_NOATIME_FL) 2294cb241339SHugh Dickins i_flags |= S_NOATIME; 2295cb241339SHugh Dickins if (fsflags & FS_APPEND_FL) 2296cb241339SHugh Dickins i_flags |= S_APPEND; 2297cb241339SHugh Dickins if (fsflags & FS_IMMUTABLE_FL) 2298cb241339SHugh Dickins i_flags |= S_IMMUTABLE; 2299cb241339SHugh Dickins /* 2300cb241339SHugh Dickins * But FS_NODUMP_FL does not require any action in i_flags. 2301cb241339SHugh Dickins */ 2302cb241339SHugh Dickins inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE); 2303e408e695STheodore Ts'o } 2304cb241339SHugh Dickins #else 2305cb241339SHugh Dickins static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) 2306cb241339SHugh Dickins { 2307cb241339SHugh Dickins } 2308cb241339SHugh Dickins #define shmem_initxattrs NULL 2309cb241339SHugh Dickins #endif 2310e408e695STheodore Ts'o 2311e408e695STheodore Ts'o static struct inode *shmem_get_inode(struct super_block *sb, struct inode *dir, 231209208d15SAl Viro umode_t mode, dev_t dev, unsigned long flags) 23131da177e4SLinus Torvalds { 23141da177e4SLinus Torvalds struct inode *inode; 23151da177e4SLinus Torvalds struct shmem_inode_info *info; 23161da177e4SLinus Torvalds struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 2317e809d5f0SChris Down ino_t ino; 23181da177e4SLinus Torvalds 2319e809d5f0SChris Down if (shmem_reserve_inode(sb, &ino)) 23201da177e4SLinus Torvalds return NULL; 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds inode = new_inode(sb); 23231da177e4SLinus Torvalds if (inode) { 2324e809d5f0SChris Down inode->i_ino = ino; 232521cb47beSChristian Brauner inode_init_owner(&init_user_ns, inode, dir, mode); 23261da177e4SLinus Torvalds inode->i_blocks = 0; 2327078cd827SDeepa Dinamani inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); 2328a251c17aSJason A. Donenfeld inode->i_generation = get_random_u32(); 23291da177e4SLinus Torvalds info = SHMEM_I(inode); 23301da177e4SLinus Torvalds memset(info, 0, (char *)inode - (char *)info); 23311da177e4SLinus Torvalds spin_lock_init(&info->lock); 2332af53d3e9SHugh Dickins atomic_set(&info->stop_eviction, 0); 233340e041a2SDavid Herrmann info->seals = F_SEAL_SEAL; 23340b0a0806SHugh Dickins info->flags = flags & VM_NORESERVE; 2335f7cd16a5SXavier Roche info->i_crtime = inode->i_mtime; 2336e408e695STheodore Ts'o info->fsflags = (dir == NULL) ? 0 : 2337e408e695STheodore Ts'o SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; 2338cb241339SHugh Dickins if (info->fsflags) 2339cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 2340779750d2SKirill A. Shutemov INIT_LIST_HEAD(&info->shrinklist); 23411da177e4SLinus Torvalds INIT_LIST_HEAD(&info->swaplist); 234238f38657SAristeu Rozanski simple_xattrs_init(&info->xattrs); 234372c04902SAl Viro cache_no_acl(inode); 2344ff36da69SMatthew Wilcox (Oracle) mapping_set_large_folios(inode->i_mapping); 23451da177e4SLinus Torvalds 23461da177e4SLinus Torvalds switch (mode & S_IFMT) { 23471da177e4SLinus Torvalds default: 234839f0247dSAndreas Gruenbacher inode->i_op = &shmem_special_inode_operations; 23491da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 23501da177e4SLinus Torvalds break; 23511da177e4SLinus Torvalds case S_IFREG: 235214fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 23531da177e4SLinus Torvalds inode->i_op = &shmem_inode_operations; 23541da177e4SLinus Torvalds inode->i_fop = &shmem_file_operations; 235571fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, 235671fe804bSLee Schermerhorn shmem_get_sbmpol(sbinfo)); 23571da177e4SLinus Torvalds break; 23581da177e4SLinus Torvalds case S_IFDIR: 2359d8c76e6fSDave Hansen inc_nlink(inode); 23601da177e4SLinus Torvalds /* Some things misbehave if size == 0 on a directory */ 23611da177e4SLinus Torvalds inode->i_size = 2 * BOGO_DIRENT_SIZE; 23621da177e4SLinus Torvalds inode->i_op = &shmem_dir_inode_operations; 23631da177e4SLinus Torvalds inode->i_fop = &simple_dir_operations; 23641da177e4SLinus Torvalds break; 23651da177e4SLinus Torvalds case S_IFLNK: 23661da177e4SLinus Torvalds /* 23671da177e4SLinus Torvalds * Must not load anything in the rbtree, 23681da177e4SLinus Torvalds * mpol_free_shared_policy will not be called. 23691da177e4SLinus Torvalds */ 237071fe804bSLee Schermerhorn mpol_shared_policy_init(&info->policy, NULL); 23711da177e4SLinus Torvalds break; 23721da177e4SLinus Torvalds } 2373b45d71fbSJoel Fernandes (Google) 2374b45d71fbSJoel Fernandes (Google) lockdep_annotate_inode_mutex_key(inode); 23755b04c689SPavel Emelyanov } else 23765b04c689SPavel Emelyanov shmem_free_inode(sb); 23771da177e4SLinus Torvalds return inode; 23781da177e4SLinus Torvalds } 23791da177e4SLinus Torvalds 23803460f6e5SAxel Rasmussen #ifdef CONFIG_USERFAULTFD 23813460f6e5SAxel Rasmussen int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, 23824c27fe4cSMike Rapoport pmd_t *dst_pmd, 23834c27fe4cSMike Rapoport struct vm_area_struct *dst_vma, 23844c27fe4cSMike Rapoport unsigned long dst_addr, 23854c27fe4cSMike Rapoport unsigned long src_addr, 23868ee79edfSPeter Xu bool zeropage, bool wp_copy, 23874c27fe4cSMike Rapoport struct page **pagep) 23884c27fe4cSMike Rapoport { 23894c27fe4cSMike Rapoport struct inode *inode = file_inode(dst_vma->vm_file); 23904c27fe4cSMike Rapoport struct shmem_inode_info *info = SHMEM_I(inode); 23914c27fe4cSMike Rapoport struct address_space *mapping = inode->i_mapping; 23924c27fe4cSMike Rapoport gfp_t gfp = mapping_gfp_mask(mapping); 23934c27fe4cSMike Rapoport pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 23944c27fe4cSMike Rapoport void *page_kaddr; 2395b7dd44a1SMatthew Wilcox (Oracle) struct folio *folio; 23964c27fe4cSMike Rapoport int ret; 23973460f6e5SAxel Rasmussen pgoff_t max_off; 23984c27fe4cSMike Rapoport 23997ed9d238SAxel Rasmussen if (!shmem_inode_acct_block(inode, 1)) { 24007ed9d238SAxel Rasmussen /* 24017ed9d238SAxel Rasmussen * We may have got a page, returned -ENOENT triggering a retry, 24027ed9d238SAxel Rasmussen * and now we find ourselves with -ENOMEM. Release the page, to 24037ed9d238SAxel Rasmussen * avoid a BUG_ON in our caller. 24047ed9d238SAxel Rasmussen */ 24057ed9d238SAxel Rasmussen if (unlikely(*pagep)) { 24067ed9d238SAxel Rasmussen put_page(*pagep); 24077ed9d238SAxel Rasmussen *pagep = NULL; 24087ed9d238SAxel Rasmussen } 24097d64ae3aSAxel Rasmussen return -ENOMEM; 24107ed9d238SAxel Rasmussen } 24114c27fe4cSMike Rapoport 2412cb658a45SAndrea Arcangeli if (!*pagep) { 24137d64ae3aSAxel Rasmussen ret = -ENOMEM; 24147a7256d5SMatthew Wilcox (Oracle) folio = shmem_alloc_folio(gfp, info, pgoff); 24157a7256d5SMatthew Wilcox (Oracle) if (!folio) 24160f079694SMike Rapoport goto out_unacct_blocks; 24174c27fe4cSMike Rapoport 24183460f6e5SAxel Rasmussen if (!zeropage) { /* COPY */ 24197a7256d5SMatthew Wilcox (Oracle) page_kaddr = kmap_local_folio(folio, 0); 24205dc21f0cSIra Weiny /* 24215dc21f0cSIra Weiny * The read mmap_lock is held here. Despite the 24225dc21f0cSIra Weiny * mmap_lock being read recursive a deadlock is still 24235dc21f0cSIra Weiny * possible if a writer has taken a lock. For example: 24245dc21f0cSIra Weiny * 24255dc21f0cSIra Weiny * process A thread 1 takes read lock on own mmap_lock 24265dc21f0cSIra Weiny * process A thread 2 calls mmap, blocks taking write lock 24275dc21f0cSIra Weiny * process B thread 1 takes page fault, read lock on own mmap lock 24285dc21f0cSIra Weiny * process B thread 2 calls mmap, blocks taking write lock 24295dc21f0cSIra Weiny * process A thread 1 blocks taking read lock on process B 24305dc21f0cSIra Weiny * process B thread 1 blocks taking read lock on process A 24315dc21f0cSIra Weiny * 24325dc21f0cSIra Weiny * Disable page faults to prevent potential deadlock 24335dc21f0cSIra Weiny * and retry the copy outside the mmap_lock. 24345dc21f0cSIra Weiny */ 24355dc21f0cSIra Weiny pagefault_disable(); 24368d103963SMike Rapoport ret = copy_from_user(page_kaddr, 24378d103963SMike Rapoport (const void __user *)src_addr, 24384c27fe4cSMike Rapoport PAGE_SIZE); 24395dc21f0cSIra Weiny pagefault_enable(); 24407a7256d5SMatthew Wilcox (Oracle) kunmap_local(page_kaddr); 24414c27fe4cSMike Rapoport 2442c1e8d7c6SMichel Lespinasse /* fallback to copy_from_user outside mmap_lock */ 24434c27fe4cSMike Rapoport if (unlikely(ret)) { 24447a7256d5SMatthew Wilcox (Oracle) *pagep = &folio->page; 24457d64ae3aSAxel Rasmussen ret = -ENOENT; 24464c27fe4cSMike Rapoport /* don't free the page */ 24477d64ae3aSAxel Rasmussen goto out_unacct_blocks; 24484c27fe4cSMike Rapoport } 244919b482c2SMuchun Song 24507a7256d5SMatthew Wilcox (Oracle) flush_dcache_folio(folio); 24513460f6e5SAxel Rasmussen } else { /* ZEROPAGE */ 24527a7256d5SMatthew Wilcox (Oracle) clear_user_highpage(&folio->page, dst_addr); 24538d103963SMike Rapoport } 24544c27fe4cSMike Rapoport } else { 24557a7256d5SMatthew Wilcox (Oracle) folio = page_folio(*pagep); 24567a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(folio_test_large(folio), folio); 24574c27fe4cSMike Rapoport *pagep = NULL; 24584c27fe4cSMike Rapoport } 24594c27fe4cSMike Rapoport 24607a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_locked(folio)); 24617a7256d5SMatthew Wilcox (Oracle) VM_BUG_ON(folio_test_swapbacked(folio)); 24627a7256d5SMatthew Wilcox (Oracle) __folio_set_locked(folio); 24637a7256d5SMatthew Wilcox (Oracle) __folio_set_swapbacked(folio); 24647a7256d5SMatthew Wilcox (Oracle) __folio_mark_uptodate(folio); 24659cc90c66SAndrea Arcangeli 2466e2a50c1fSAndrea Arcangeli ret = -EFAULT; 2467e2a50c1fSAndrea Arcangeli max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 24683460f6e5SAxel Rasmussen if (unlikely(pgoff >= max_off)) 2469e2a50c1fSAndrea Arcangeli goto out_release; 2470e2a50c1fSAndrea Arcangeli 2471b7dd44a1SMatthew Wilcox (Oracle) ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, 24723fea5a49SJohannes Weiner gfp & GFP_RECLAIM_MASK, dst_mm); 24734c27fe4cSMike Rapoport if (ret) 24744c27fe4cSMike Rapoport goto out_release; 24754c27fe4cSMike Rapoport 24767d64ae3aSAxel Rasmussen ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, 24777a7256d5SMatthew Wilcox (Oracle) &folio->page, true, wp_copy); 24787d64ae3aSAxel Rasmussen if (ret) 24797d64ae3aSAxel Rasmussen goto out_delete_from_cache; 24804c27fe4cSMike Rapoport 248194b7cc01SYang Shi spin_lock_irq(&info->lock); 24824c27fe4cSMike Rapoport info->alloced++; 24834c27fe4cSMike Rapoport inode->i_blocks += BLOCKS_PER_PAGE; 24844c27fe4cSMike Rapoport shmem_recalc_inode(inode); 248594b7cc01SYang Shi spin_unlock_irq(&info->lock); 24864c27fe4cSMike Rapoport 24877a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 24887d64ae3aSAxel Rasmussen return 0; 24897d64ae3aSAxel Rasmussen out_delete_from_cache: 24907a7256d5SMatthew Wilcox (Oracle) filemap_remove_folio(folio); 24914c27fe4cSMike Rapoport out_release: 24927a7256d5SMatthew Wilcox (Oracle) folio_unlock(folio); 24937a7256d5SMatthew Wilcox (Oracle) folio_put(folio); 24944c27fe4cSMike Rapoport out_unacct_blocks: 24950f079694SMike Rapoport shmem_inode_unacct_blocks(inode, 1); 24967d64ae3aSAxel Rasmussen return ret; 24974c27fe4cSMike Rapoport } 24983460f6e5SAxel Rasmussen #endif /* CONFIG_USERFAULTFD */ 24998d103963SMike Rapoport 25001da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 250192e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations; 250269f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations; 25031da177e4SLinus Torvalds 25041da177e4SLinus Torvalds static int 2505800d15a5SNick Piggin shmem_write_begin(struct file *file, struct address_space *mapping, 25069d6b0cd7SMatthew Wilcox (Oracle) loff_t pos, unsigned len, 2507800d15a5SNick Piggin struct page **pagep, void **fsdata) 25081da177e4SLinus Torvalds { 2509800d15a5SNick Piggin struct inode *inode = mapping->host; 251040e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 251109cbfeafSKirill A. Shutemov pgoff_t index = pos >> PAGE_SHIFT; 2512eff1f906SMatthew Wilcox (Oracle) struct folio *folio; 2513a7605426SYang Shi int ret = 0; 251440e041a2SDavid Herrmann 25159608703eSJan Kara /* i_rwsem is held by caller */ 2516ab3948f5SJoel Fernandes (Google) if (unlikely(info->seals & (F_SEAL_GROW | 2517ab3948f5SJoel Fernandes (Google) F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 2518ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 251940e041a2SDavid Herrmann return -EPERM; 252040e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 252140e041a2SDavid Herrmann return -EPERM; 252240e041a2SDavid Herrmann } 252340e041a2SDavid Herrmann 2524eff1f906SMatthew Wilcox (Oracle) ret = shmem_get_folio(inode, index, &folio, SGP_WRITE); 2525a7605426SYang Shi 2526a7605426SYang Shi if (ret) 2527a7605426SYang Shi return ret; 2528a7605426SYang Shi 2529eff1f906SMatthew Wilcox (Oracle) *pagep = folio_file_page(folio, index); 2530a7605426SYang Shi if (PageHWPoison(*pagep)) { 2531eff1f906SMatthew Wilcox (Oracle) folio_unlock(folio); 2532eff1f906SMatthew Wilcox (Oracle) folio_put(folio); 2533a7605426SYang Shi *pagep = NULL; 2534a7605426SYang Shi return -EIO; 2535a7605426SYang Shi } 2536a7605426SYang Shi 2537a7605426SYang Shi return 0; 2538800d15a5SNick Piggin } 2539800d15a5SNick Piggin 2540800d15a5SNick Piggin static int 2541800d15a5SNick Piggin shmem_write_end(struct file *file, struct address_space *mapping, 2542800d15a5SNick Piggin loff_t pos, unsigned len, unsigned copied, 2543800d15a5SNick Piggin struct page *page, void *fsdata) 2544800d15a5SNick Piggin { 2545800d15a5SNick Piggin struct inode *inode = mapping->host; 2546800d15a5SNick Piggin 2547800d15a5SNick Piggin if (pos + copied > inode->i_size) 2548800d15a5SNick Piggin i_size_write(inode, pos + copied); 2549800d15a5SNick Piggin 2550ec9516fbSHugh Dickins if (!PageUptodate(page)) { 2551800d8c63SKirill A. Shutemov struct page *head = compound_head(page); 2552800d8c63SKirill A. Shutemov if (PageTransCompound(page)) { 2553800d8c63SKirill A. Shutemov int i; 2554800d8c63SKirill A. Shutemov 2555800d8c63SKirill A. Shutemov for (i = 0; i < HPAGE_PMD_NR; i++) { 2556800d8c63SKirill A. Shutemov if (head + i == page) 2557800d8c63SKirill A. Shutemov continue; 2558800d8c63SKirill A. Shutemov clear_highpage(head + i); 2559800d8c63SKirill A. Shutemov flush_dcache_page(head + i); 2560800d8c63SKirill A. Shutemov } 2561800d8c63SKirill A. Shutemov } 256209cbfeafSKirill A. Shutemov if (copied < PAGE_SIZE) { 256309cbfeafSKirill A. Shutemov unsigned from = pos & (PAGE_SIZE - 1); 2564ec9516fbSHugh Dickins zero_user_segments(page, 0, from, 256509cbfeafSKirill A. Shutemov from + copied, PAGE_SIZE); 2566ec9516fbSHugh Dickins } 2567800d8c63SKirill A. Shutemov SetPageUptodate(head); 2568ec9516fbSHugh Dickins } 2569d3602444SHugh Dickins set_page_dirty(page); 25706746aff7SWu Fengguang unlock_page(page); 257109cbfeafSKirill A. Shutemov put_page(page); 2572d3602444SHugh Dickins 2573800d15a5SNick Piggin return copied; 25741da177e4SLinus Torvalds } 25751da177e4SLinus Torvalds 25762ba5bbedSAl Viro static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 25771da177e4SLinus Torvalds { 25786e58e79dSAl Viro struct file *file = iocb->ki_filp; 25796e58e79dSAl Viro struct inode *inode = file_inode(file); 25801da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 258141ffe5d5SHugh Dickins pgoff_t index; 258241ffe5d5SHugh Dickins unsigned long offset; 2583f7c1d074SGeert Uytterhoeven int error = 0; 2584cb66a7a1SAl Viro ssize_t retval = 0; 25856e58e79dSAl Viro loff_t *ppos = &iocb->ki_pos; 2586a0ee5ec5SHugh Dickins 258709cbfeafSKirill A. Shutemov index = *ppos >> PAGE_SHIFT; 258809cbfeafSKirill A. Shutemov offset = *ppos & ~PAGE_MASK; 25891da177e4SLinus Torvalds 25901da177e4SLinus Torvalds for (;;) { 25914601e2fcSMatthew Wilcox (Oracle) struct folio *folio = NULL; 25921da177e4SLinus Torvalds struct page *page = NULL; 259341ffe5d5SHugh Dickins pgoff_t end_index; 259441ffe5d5SHugh Dickins unsigned long nr, ret; 25951da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 25961da177e4SLinus Torvalds 259709cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 25981da177e4SLinus Torvalds if (index > end_index) 25991da177e4SLinus Torvalds break; 26001da177e4SLinus Torvalds if (index == end_index) { 260109cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 26021da177e4SLinus Torvalds if (nr <= offset) 26031da177e4SLinus Torvalds break; 26041da177e4SLinus Torvalds } 26051da177e4SLinus Torvalds 26064601e2fcSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, SGP_READ); 26076e58e79dSAl Viro if (error) { 26086e58e79dSAl Viro if (error == -EINVAL) 26096e58e79dSAl Viro error = 0; 26101da177e4SLinus Torvalds break; 26111da177e4SLinus Torvalds } 26124601e2fcSMatthew Wilcox (Oracle) if (folio) { 26134601e2fcSMatthew Wilcox (Oracle) folio_unlock(folio); 2614a7605426SYang Shi 26154601e2fcSMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 2616a7605426SYang Shi if (PageHWPoison(page)) { 26174601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 2618a7605426SYang Shi error = -EIO; 2619a7605426SYang Shi break; 2620a7605426SYang Shi } 262175edd345SHugh Dickins } 26221da177e4SLinus Torvalds 26231da177e4SLinus Torvalds /* 26241da177e4SLinus Torvalds * We must evaluate after, since reads (unlike writes) 26259608703eSJan Kara * are called without i_rwsem protection against truncate 26261da177e4SLinus Torvalds */ 262709cbfeafSKirill A. Shutemov nr = PAGE_SIZE; 26281da177e4SLinus Torvalds i_size = i_size_read(inode); 262909cbfeafSKirill A. Shutemov end_index = i_size >> PAGE_SHIFT; 26301da177e4SLinus Torvalds if (index == end_index) { 263109cbfeafSKirill A. Shutemov nr = i_size & ~PAGE_MASK; 26321da177e4SLinus Torvalds if (nr <= offset) { 26334601e2fcSMatthew Wilcox (Oracle) if (folio) 26344601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 26351da177e4SLinus Torvalds break; 26361da177e4SLinus Torvalds } 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds nr -= offset; 26391da177e4SLinus Torvalds 26404601e2fcSMatthew Wilcox (Oracle) if (folio) { 26411da177e4SLinus Torvalds /* 26421da177e4SLinus Torvalds * If users can be writing to this page using arbitrary 26431da177e4SLinus Torvalds * virtual addresses, take care about potential aliasing 26441da177e4SLinus Torvalds * before reading the page on the kernel side. 26451da177e4SLinus Torvalds */ 26461da177e4SLinus Torvalds if (mapping_writably_mapped(mapping)) 26471da177e4SLinus Torvalds flush_dcache_page(page); 26481da177e4SLinus Torvalds /* 26491da177e4SLinus Torvalds * Mark the page accessed if we read the beginning. 26501da177e4SLinus Torvalds */ 26511da177e4SLinus Torvalds if (!offset) 26524601e2fcSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 26531da177e4SLinus Torvalds /* 26541da177e4SLinus Torvalds * Ok, we have the page, and it's up-to-date, so 26551da177e4SLinus Torvalds * now we can copy it to user space... 26561da177e4SLinus Torvalds */ 26572ba5bbedSAl Viro ret = copy_page_to_iter(page, offset, nr, to); 26584601e2fcSMatthew Wilcox (Oracle) folio_put(folio); 26591bdec44bSHugh Dickins 2660fcb14cb1SAl Viro } else if (user_backed_iter(to)) { 26611bdec44bSHugh Dickins /* 26621bdec44bSHugh Dickins * Copy to user tends to be so well optimized, but 26631bdec44bSHugh Dickins * clear_user() not so much, that it is noticeably 26641bdec44bSHugh Dickins * faster to copy the zero page instead of clearing. 26651bdec44bSHugh Dickins */ 26661bdec44bSHugh Dickins ret = copy_page_to_iter(ZERO_PAGE(0), offset, nr, to); 26671bdec44bSHugh Dickins } else { 26681bdec44bSHugh Dickins /* 26691bdec44bSHugh Dickins * But submitting the same page twice in a row to 26701bdec44bSHugh Dickins * splice() - or others? - can result in confusion: 26711bdec44bSHugh Dickins * so don't attempt that optimization on pipes etc. 26721bdec44bSHugh Dickins */ 26731bdec44bSHugh Dickins ret = iov_iter_zero(nr, to); 26741bdec44bSHugh Dickins } 26751bdec44bSHugh Dickins 26766e58e79dSAl Viro retval += ret; 26771da177e4SLinus Torvalds offset += ret; 267809cbfeafSKirill A. Shutemov index += offset >> PAGE_SHIFT; 267909cbfeafSKirill A. Shutemov offset &= ~PAGE_MASK; 26801da177e4SLinus Torvalds 26812ba5bbedSAl Viro if (!iov_iter_count(to)) 26821da177e4SLinus Torvalds break; 26836e58e79dSAl Viro if (ret < nr) { 26846e58e79dSAl Viro error = -EFAULT; 26856e58e79dSAl Viro break; 26866e58e79dSAl Viro } 26871da177e4SLinus Torvalds cond_resched(); 26881da177e4SLinus Torvalds } 26891da177e4SLinus Torvalds 269009cbfeafSKirill A. Shutemov *ppos = ((loff_t) index << PAGE_SHIFT) + offset; 26916e58e79dSAl Viro file_accessed(file); 26926e58e79dSAl Viro return retval ? retval : error; 26931da177e4SLinus Torvalds } 26941da177e4SLinus Torvalds 2695965c8e59SAndrew Morton static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 2696220f2ac9SHugh Dickins { 2697220f2ac9SHugh Dickins struct address_space *mapping = file->f_mapping; 2698220f2ac9SHugh Dickins struct inode *inode = mapping->host; 2699220f2ac9SHugh Dickins 2700965c8e59SAndrew Morton if (whence != SEEK_DATA && whence != SEEK_HOLE) 2701965c8e59SAndrew Morton return generic_file_llseek_size(file, offset, whence, 2702220f2ac9SHugh Dickins MAX_LFS_FILESIZE, i_size_read(inode)); 270341139aa4SMatthew Wilcox (Oracle) if (offset < 0) 270441139aa4SMatthew Wilcox (Oracle) return -ENXIO; 270541139aa4SMatthew Wilcox (Oracle) 27065955102cSAl Viro inode_lock(inode); 27079608703eSJan Kara /* We're holding i_rwsem so we can access i_size directly */ 270841139aa4SMatthew Wilcox (Oracle) offset = mapping_seek_hole_data(mapping, offset, inode->i_size, whence); 2709387aae6fSHugh Dickins if (offset >= 0) 271046a1c2c7SJie Liu offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 27115955102cSAl Viro inode_unlock(inode); 2712220f2ac9SHugh Dickins return offset; 2713220f2ac9SHugh Dickins } 2714220f2ac9SHugh Dickins 271583e4fa9cSHugh Dickins static long shmem_fallocate(struct file *file, int mode, loff_t offset, 271683e4fa9cSHugh Dickins loff_t len) 271783e4fa9cSHugh Dickins { 2718496ad9aaSAl Viro struct inode *inode = file_inode(file); 2719e2d12e22SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 272040e041a2SDavid Herrmann struct shmem_inode_info *info = SHMEM_I(inode); 27211aac1400SHugh Dickins struct shmem_falloc shmem_falloc; 2722d144bf62SHugh Dickins pgoff_t start, index, end, undo_fallocend; 2723e2d12e22SHugh Dickins int error; 272483e4fa9cSHugh Dickins 272513ace4d0SHugh Dickins if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 272613ace4d0SHugh Dickins return -EOPNOTSUPP; 272713ace4d0SHugh Dickins 27285955102cSAl Viro inode_lock(inode); 272983e4fa9cSHugh Dickins 273083e4fa9cSHugh Dickins if (mode & FALLOC_FL_PUNCH_HOLE) { 273183e4fa9cSHugh Dickins struct address_space *mapping = file->f_mapping; 273283e4fa9cSHugh Dickins loff_t unmap_start = round_up(offset, PAGE_SIZE); 273383e4fa9cSHugh Dickins loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 27348e205f77SHugh Dickins DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 273583e4fa9cSHugh Dickins 27369608703eSJan Kara /* protected by i_rwsem */ 2737ab3948f5SJoel Fernandes (Google) if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 273840e041a2SDavid Herrmann error = -EPERM; 273940e041a2SDavid Herrmann goto out; 274040e041a2SDavid Herrmann } 274140e041a2SDavid Herrmann 27428e205f77SHugh Dickins shmem_falloc.waitq = &shmem_falloc_waitq; 2743aa71ecd8SChen Jun shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 2744f00cdc6dSHugh Dickins shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 2745f00cdc6dSHugh Dickins spin_lock(&inode->i_lock); 2746f00cdc6dSHugh Dickins inode->i_private = &shmem_falloc; 2747f00cdc6dSHugh Dickins spin_unlock(&inode->i_lock); 2748f00cdc6dSHugh Dickins 274983e4fa9cSHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 275083e4fa9cSHugh Dickins unmap_mapping_range(mapping, unmap_start, 275183e4fa9cSHugh Dickins 1 + unmap_end - unmap_start, 0); 275283e4fa9cSHugh Dickins shmem_truncate_range(inode, offset, offset + len - 1); 275383e4fa9cSHugh Dickins /* No need to unmap again: hole-punching leaves COWed pages */ 27548e205f77SHugh Dickins 27558e205f77SHugh Dickins spin_lock(&inode->i_lock); 27568e205f77SHugh Dickins inode->i_private = NULL; 27578e205f77SHugh Dickins wake_up_all(&shmem_falloc_waitq); 27582055da97SIngo Molnar WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 27598e205f77SHugh Dickins spin_unlock(&inode->i_lock); 276083e4fa9cSHugh Dickins error = 0; 27618e205f77SHugh Dickins goto out; 276283e4fa9cSHugh Dickins } 276383e4fa9cSHugh Dickins 2764e2d12e22SHugh Dickins /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 2765e2d12e22SHugh Dickins error = inode_newsize_ok(inode, offset + len); 2766e2d12e22SHugh Dickins if (error) 2767e2d12e22SHugh Dickins goto out; 2768e2d12e22SHugh Dickins 276940e041a2SDavid Herrmann if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 277040e041a2SDavid Herrmann error = -EPERM; 277140e041a2SDavid Herrmann goto out; 277240e041a2SDavid Herrmann } 277340e041a2SDavid Herrmann 277409cbfeafSKirill A. Shutemov start = offset >> PAGE_SHIFT; 277509cbfeafSKirill A. Shutemov end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 2776e2d12e22SHugh Dickins /* Try to avoid a swapstorm if len is impossible to satisfy */ 2777e2d12e22SHugh Dickins if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 2778e2d12e22SHugh Dickins error = -ENOSPC; 2779e2d12e22SHugh Dickins goto out; 2780e2d12e22SHugh Dickins } 2781e2d12e22SHugh Dickins 27828e205f77SHugh Dickins shmem_falloc.waitq = NULL; 27831aac1400SHugh Dickins shmem_falloc.start = start; 27841aac1400SHugh Dickins shmem_falloc.next = start; 27851aac1400SHugh Dickins shmem_falloc.nr_falloced = 0; 27861aac1400SHugh Dickins shmem_falloc.nr_unswapped = 0; 27871aac1400SHugh Dickins spin_lock(&inode->i_lock); 27881aac1400SHugh Dickins inode->i_private = &shmem_falloc; 27891aac1400SHugh Dickins spin_unlock(&inode->i_lock); 27901aac1400SHugh Dickins 2791d144bf62SHugh Dickins /* 2792d144bf62SHugh Dickins * info->fallocend is only relevant when huge pages might be 2793d144bf62SHugh Dickins * involved: to prevent split_huge_page() freeing fallocated 2794d144bf62SHugh Dickins * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. 2795d144bf62SHugh Dickins */ 2796d144bf62SHugh Dickins undo_fallocend = info->fallocend; 2797d144bf62SHugh Dickins if (info->fallocend < end) 2798d144bf62SHugh Dickins info->fallocend = end; 2799d144bf62SHugh Dickins 2800050dcb5cSHugh Dickins for (index = start; index < end; ) { 2801b0802b22SMatthew Wilcox (Oracle) struct folio *folio; 2802e2d12e22SHugh Dickins 2803e2d12e22SHugh Dickins /* 2804e2d12e22SHugh Dickins * Good, the fallocate(2) manpage permits EINTR: we may have 2805e2d12e22SHugh Dickins * been interrupted because we are using up too much memory. 2806e2d12e22SHugh Dickins */ 2807e2d12e22SHugh Dickins if (signal_pending(current)) 2808e2d12e22SHugh Dickins error = -EINTR; 28091aac1400SHugh Dickins else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 28101aac1400SHugh Dickins error = -ENOMEM; 2811e2d12e22SHugh Dickins else 2812b0802b22SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, index, &folio, 2813b0802b22SMatthew Wilcox (Oracle) SGP_FALLOC); 2814e2d12e22SHugh Dickins if (error) { 2815d144bf62SHugh Dickins info->fallocend = undo_fallocend; 2816b0802b22SMatthew Wilcox (Oracle) /* Remove the !uptodate folios we added */ 28177f556567SHugh Dickins if (index > start) { 28181635f6a7SHugh Dickins shmem_undo_range(inode, 281909cbfeafSKirill A. Shutemov (loff_t)start << PAGE_SHIFT, 2820b9b4bb26SAnthony Romano ((loff_t)index << PAGE_SHIFT) - 1, true); 28217f556567SHugh Dickins } 28221aac1400SHugh Dickins goto undone; 2823e2d12e22SHugh Dickins } 2824e2d12e22SHugh Dickins 2825050dcb5cSHugh Dickins /* 2826050dcb5cSHugh Dickins * Here is a more important optimization than it appears: 2827b0802b22SMatthew Wilcox (Oracle) * a second SGP_FALLOC on the same large folio will clear it, 2828b0802b22SMatthew Wilcox (Oracle) * making it uptodate and un-undoable if we fail later. 2829050dcb5cSHugh Dickins */ 2830b0802b22SMatthew Wilcox (Oracle) index = folio_next_index(folio); 2831050dcb5cSHugh Dickins /* Beware 32-bit wraparound */ 2832050dcb5cSHugh Dickins if (!index) 2833050dcb5cSHugh Dickins index--; 2834050dcb5cSHugh Dickins 2835e2d12e22SHugh Dickins /* 28361aac1400SHugh Dickins * Inform shmem_writepage() how far we have reached. 28371aac1400SHugh Dickins * No need for lock or barrier: we have the page lock. 28381aac1400SHugh Dickins */ 2839b0802b22SMatthew Wilcox (Oracle) if (!folio_test_uptodate(folio)) 2840050dcb5cSHugh Dickins shmem_falloc.nr_falloced += index - shmem_falloc.next; 2841050dcb5cSHugh Dickins shmem_falloc.next = index; 28421aac1400SHugh Dickins 28431aac1400SHugh Dickins /* 2844b0802b22SMatthew Wilcox (Oracle) * If !uptodate, leave it that way so that freeable folios 28451635f6a7SHugh Dickins * can be recognized if we need to rollback on error later. 2846b0802b22SMatthew Wilcox (Oracle) * But mark it dirty so that memory pressure will swap rather 2847b0802b22SMatthew Wilcox (Oracle) * than free the folios we are allocating (and SGP_CACHE folios 2848e2d12e22SHugh Dickins * might still be clean: we now need to mark those dirty too). 2849e2d12e22SHugh Dickins */ 2850b0802b22SMatthew Wilcox (Oracle) folio_mark_dirty(folio); 2851b0802b22SMatthew Wilcox (Oracle) folio_unlock(folio); 2852b0802b22SMatthew Wilcox (Oracle) folio_put(folio); 2853e2d12e22SHugh Dickins cond_resched(); 2854e2d12e22SHugh Dickins } 2855e2d12e22SHugh Dickins 2856e2d12e22SHugh Dickins if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 2857e2d12e22SHugh Dickins i_size_write(inode, offset + len); 28581aac1400SHugh Dickins undone: 28591aac1400SHugh Dickins spin_lock(&inode->i_lock); 28601aac1400SHugh Dickins inode->i_private = NULL; 28611aac1400SHugh Dickins spin_unlock(&inode->i_lock); 2862e2d12e22SHugh Dickins out: 286315f242bbSHugh Dickins if (!error) 286415f242bbSHugh Dickins file_modified(file); 28655955102cSAl Viro inode_unlock(inode); 286683e4fa9cSHugh Dickins return error; 286783e4fa9cSHugh Dickins } 286883e4fa9cSHugh Dickins 2869726c3342SDavid Howells static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 28701da177e4SLinus Torvalds { 2871726c3342SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 28721da177e4SLinus Torvalds 28731da177e4SLinus Torvalds buf->f_type = TMPFS_MAGIC; 287409cbfeafSKirill A. Shutemov buf->f_bsize = PAGE_SIZE; 28751da177e4SLinus Torvalds buf->f_namelen = NAME_MAX; 28760edd73b3SHugh Dickins if (sbinfo->max_blocks) { 28771da177e4SLinus Torvalds buf->f_blocks = sbinfo->max_blocks; 287841ffe5d5SHugh Dickins buf->f_bavail = 287941ffe5d5SHugh Dickins buf->f_bfree = sbinfo->max_blocks - 288041ffe5d5SHugh Dickins percpu_counter_sum(&sbinfo->used_blocks); 28810edd73b3SHugh Dickins } 28820edd73b3SHugh Dickins if (sbinfo->max_inodes) { 28831da177e4SLinus Torvalds buf->f_files = sbinfo->max_inodes; 28841da177e4SLinus Torvalds buf->f_ffree = sbinfo->free_inodes; 28851da177e4SLinus Torvalds } 28861da177e4SLinus Torvalds /* else leave those fields 0 like simple_statfs */ 288759cda49eSAmir Goldstein 288859cda49eSAmir Goldstein buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b); 288959cda49eSAmir Goldstein 28901da177e4SLinus Torvalds return 0; 28911da177e4SLinus Torvalds } 28921da177e4SLinus Torvalds 28931da177e4SLinus Torvalds /* 28941da177e4SLinus Torvalds * File creation. Allocate an inode, and we're done.. 28951da177e4SLinus Torvalds */ 28961da177e4SLinus Torvalds static int 2897549c7297SChristian Brauner shmem_mknod(struct user_namespace *mnt_userns, struct inode *dir, 2898549c7297SChristian Brauner struct dentry *dentry, umode_t mode, dev_t dev) 28991da177e4SLinus Torvalds { 29000b0a0806SHugh Dickins struct inode *inode; 29011da177e4SLinus Torvalds int error = -ENOSPC; 29021da177e4SLinus Torvalds 2903454abafeSDmitry Monakhov inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); 29041da177e4SLinus Torvalds if (inode) { 2905feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2906feda821eSChristoph Hellwig if (error) 2907feda821eSChristoph Hellwig goto out_iput; 29082a7dba39SEric Paris error = security_inode_init_security(inode, dir, 29099d8f13baSMimi Zohar &dentry->d_name, 29106d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 2911feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2912feda821eSChristoph Hellwig goto out_iput; 291337ec43cdSMimi Zohar 2914718deb6bSAl Viro error = 0; 29151da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2916078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 291736f05cabSJeff Layton inode_inc_iversion(dir); 29181da177e4SLinus Torvalds d_instantiate(dentry, inode); 29191da177e4SLinus Torvalds dget(dentry); /* Extra count - pin the dentry in core */ 29201da177e4SLinus Torvalds } 29211da177e4SLinus Torvalds return error; 2922feda821eSChristoph Hellwig out_iput: 2923feda821eSChristoph Hellwig iput(inode); 2924feda821eSChristoph Hellwig return error; 29251da177e4SLinus Torvalds } 29261da177e4SLinus Torvalds 292760545d0dSAl Viro static int 2928549c7297SChristian Brauner shmem_tmpfile(struct user_namespace *mnt_userns, struct inode *dir, 2929863f144fSMiklos Szeredi struct file *file, umode_t mode) 293060545d0dSAl Viro { 293160545d0dSAl Viro struct inode *inode; 293260545d0dSAl Viro int error = -ENOSPC; 293360545d0dSAl Viro 293460545d0dSAl Viro inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); 293560545d0dSAl Viro if (inode) { 293660545d0dSAl Viro error = security_inode_init_security(inode, dir, 293760545d0dSAl Viro NULL, 293860545d0dSAl Viro shmem_initxattrs, NULL); 2939feda821eSChristoph Hellwig if (error && error != -EOPNOTSUPP) 2940feda821eSChristoph Hellwig goto out_iput; 2941feda821eSChristoph Hellwig error = simple_acl_create(dir, inode); 2942feda821eSChristoph Hellwig if (error) 2943feda821eSChristoph Hellwig goto out_iput; 2944863f144fSMiklos Szeredi d_tmpfile(file, inode); 294560545d0dSAl Viro } 2946863f144fSMiklos Szeredi return finish_open_simple(file, error); 2947feda821eSChristoph Hellwig out_iput: 2948feda821eSChristoph Hellwig iput(inode); 2949feda821eSChristoph Hellwig return error; 295060545d0dSAl Viro } 295160545d0dSAl Viro 2952549c7297SChristian Brauner static int shmem_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 2953549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 29541da177e4SLinus Torvalds { 29551da177e4SLinus Torvalds int error; 29561da177e4SLinus Torvalds 2957549c7297SChristian Brauner if ((error = shmem_mknod(&init_user_ns, dir, dentry, 2958549c7297SChristian Brauner mode | S_IFDIR, 0))) 29591da177e4SLinus Torvalds return error; 2960d8c76e6fSDave Hansen inc_nlink(dir); 29611da177e4SLinus Torvalds return 0; 29621da177e4SLinus Torvalds } 29631da177e4SLinus Torvalds 2964549c7297SChristian Brauner static int shmem_create(struct user_namespace *mnt_userns, struct inode *dir, 2965549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 29661da177e4SLinus Torvalds { 2967549c7297SChristian Brauner return shmem_mknod(&init_user_ns, dir, dentry, mode | S_IFREG, 0); 29681da177e4SLinus Torvalds } 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds /* 29711da177e4SLinus Torvalds * Link a file.. 29721da177e4SLinus Torvalds */ 29731da177e4SLinus Torvalds static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) 29741da177e4SLinus Torvalds { 297575c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 297629b00e60SDarrick J. Wong int ret = 0; 29771da177e4SLinus Torvalds 29781da177e4SLinus Torvalds /* 29791da177e4SLinus Torvalds * No ordinary (disk based) filesystem counts links as inodes; 29801da177e4SLinus Torvalds * but each new link needs a new dentry, pinning lowmem, and 29811da177e4SLinus Torvalds * tmpfs dentries cannot be pruned until they are unlinked. 29821062af92SDarrick J. Wong * But if an O_TMPFILE file is linked into the tmpfs, the 29831062af92SDarrick J. Wong * first link must skip that, to get the accounting right. 29841da177e4SLinus Torvalds */ 29851062af92SDarrick J. Wong if (inode->i_nlink) { 2986e809d5f0SChris Down ret = shmem_reserve_inode(inode->i_sb, NULL); 29875b04c689SPavel Emelyanov if (ret) 29885b04c689SPavel Emelyanov goto out; 29891062af92SDarrick J. Wong } 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 2992078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 299336f05cabSJeff Layton inode_inc_iversion(dir); 2994d8c76e6fSDave Hansen inc_nlink(inode); 29957de9c6eeSAl Viro ihold(inode); /* New dentry reference */ 29961da177e4SLinus Torvalds dget(dentry); /* Extra pinning count for the created dentry */ 29971da177e4SLinus Torvalds d_instantiate(dentry, inode); 29985b04c689SPavel Emelyanov out: 29995b04c689SPavel Emelyanov return ret; 30001da177e4SLinus Torvalds } 30011da177e4SLinus Torvalds 30021da177e4SLinus Torvalds static int shmem_unlink(struct inode *dir, struct dentry *dentry) 30031da177e4SLinus Torvalds { 300475c3cfa8SDavid Howells struct inode *inode = d_inode(dentry); 30051da177e4SLinus Torvalds 30065b04c689SPavel Emelyanov if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 30075b04c689SPavel Emelyanov shmem_free_inode(inode->i_sb); 30081da177e4SLinus Torvalds 30091da177e4SLinus Torvalds dir->i_size -= BOGO_DIRENT_SIZE; 3010078cd827SDeepa Dinamani inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); 301136f05cabSJeff Layton inode_inc_iversion(dir); 30129a53c3a7SDave Hansen drop_nlink(inode); 30131da177e4SLinus Torvalds dput(dentry); /* Undo the count from "create" - this does all the work */ 30141da177e4SLinus Torvalds return 0; 30151da177e4SLinus Torvalds } 30161da177e4SLinus Torvalds 30171da177e4SLinus Torvalds static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 30181da177e4SLinus Torvalds { 30191da177e4SLinus Torvalds if (!simple_empty(dentry)) 30201da177e4SLinus Torvalds return -ENOTEMPTY; 30211da177e4SLinus Torvalds 302275c3cfa8SDavid Howells drop_nlink(d_inode(dentry)); 30239a53c3a7SDave Hansen drop_nlink(dir); 30241da177e4SLinus Torvalds return shmem_unlink(dir, dentry); 30251da177e4SLinus Torvalds } 30261da177e4SLinus Torvalds 3027549c7297SChristian Brauner static int shmem_whiteout(struct user_namespace *mnt_userns, 3028549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry) 302946fdb794SMiklos Szeredi { 303046fdb794SMiklos Szeredi struct dentry *whiteout; 303146fdb794SMiklos Szeredi int error; 303246fdb794SMiklos Szeredi 303346fdb794SMiklos Szeredi whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 303446fdb794SMiklos Szeredi if (!whiteout) 303546fdb794SMiklos Szeredi return -ENOMEM; 303646fdb794SMiklos Szeredi 3037549c7297SChristian Brauner error = shmem_mknod(&init_user_ns, old_dir, whiteout, 303846fdb794SMiklos Szeredi S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 303946fdb794SMiklos Szeredi dput(whiteout); 304046fdb794SMiklos Szeredi if (error) 304146fdb794SMiklos Szeredi return error; 304246fdb794SMiklos Szeredi 304346fdb794SMiklos Szeredi /* 304446fdb794SMiklos Szeredi * Cheat and hash the whiteout while the old dentry is still in 304546fdb794SMiklos Szeredi * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 304646fdb794SMiklos Szeredi * 304746fdb794SMiklos Szeredi * d_lookup() will consistently find one of them at this point, 304846fdb794SMiklos Szeredi * not sure which one, but that isn't even important. 304946fdb794SMiklos Szeredi */ 305046fdb794SMiklos Szeredi d_rehash(whiteout); 305146fdb794SMiklos Szeredi return 0; 305246fdb794SMiklos Szeredi } 305346fdb794SMiklos Szeredi 30541da177e4SLinus Torvalds /* 30551da177e4SLinus Torvalds * The VFS layer already does all the dentry stuff for rename, 30561da177e4SLinus Torvalds * we just have to decrement the usage count for the target if 30571da177e4SLinus Torvalds * it exists so that the VFS layer correctly free's it when it 30581da177e4SLinus Torvalds * gets overwritten. 30591da177e4SLinus Torvalds */ 3060549c7297SChristian Brauner static int shmem_rename2(struct user_namespace *mnt_userns, 3061549c7297SChristian Brauner struct inode *old_dir, struct dentry *old_dentry, 3062549c7297SChristian Brauner struct inode *new_dir, struct dentry *new_dentry, 3063549c7297SChristian Brauner unsigned int flags) 30641da177e4SLinus Torvalds { 306575c3cfa8SDavid Howells struct inode *inode = d_inode(old_dentry); 30661da177e4SLinus Torvalds int they_are_dirs = S_ISDIR(inode->i_mode); 30671da177e4SLinus Torvalds 306846fdb794SMiklos Szeredi if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 30693b69ff51SMiklos Szeredi return -EINVAL; 30703b69ff51SMiklos Szeredi 307137456771SMiklos Szeredi if (flags & RENAME_EXCHANGE) 30726429e463SLorenz Bauer return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); 307337456771SMiklos Szeredi 30741da177e4SLinus Torvalds if (!simple_empty(new_dentry)) 30751da177e4SLinus Torvalds return -ENOTEMPTY; 30761da177e4SLinus Torvalds 307746fdb794SMiklos Szeredi if (flags & RENAME_WHITEOUT) { 307846fdb794SMiklos Szeredi int error; 307946fdb794SMiklos Szeredi 3080549c7297SChristian Brauner error = shmem_whiteout(&init_user_ns, old_dir, old_dentry); 308146fdb794SMiklos Szeredi if (error) 308246fdb794SMiklos Szeredi return error; 308346fdb794SMiklos Szeredi } 308446fdb794SMiklos Szeredi 308575c3cfa8SDavid Howells if (d_really_is_positive(new_dentry)) { 30861da177e4SLinus Torvalds (void) shmem_unlink(new_dir, new_dentry); 3087b928095bSMiklos Szeredi if (they_are_dirs) { 308875c3cfa8SDavid Howells drop_nlink(d_inode(new_dentry)); 30899a53c3a7SDave Hansen drop_nlink(old_dir); 3090b928095bSMiklos Szeredi } 30911da177e4SLinus Torvalds } else if (they_are_dirs) { 30929a53c3a7SDave Hansen drop_nlink(old_dir); 3093d8c76e6fSDave Hansen inc_nlink(new_dir); 30941da177e4SLinus Torvalds } 30951da177e4SLinus Torvalds 30961da177e4SLinus Torvalds old_dir->i_size -= BOGO_DIRENT_SIZE; 30971da177e4SLinus Torvalds new_dir->i_size += BOGO_DIRENT_SIZE; 30981da177e4SLinus Torvalds old_dir->i_ctime = old_dir->i_mtime = 30991da177e4SLinus Torvalds new_dir->i_ctime = new_dir->i_mtime = 3100078cd827SDeepa Dinamani inode->i_ctime = current_time(old_dir); 310136f05cabSJeff Layton inode_inc_iversion(old_dir); 310236f05cabSJeff Layton inode_inc_iversion(new_dir); 31031da177e4SLinus Torvalds return 0; 31041da177e4SLinus Torvalds } 31051da177e4SLinus Torvalds 3106549c7297SChristian Brauner static int shmem_symlink(struct user_namespace *mnt_userns, struct inode *dir, 3107549c7297SChristian Brauner struct dentry *dentry, const char *symname) 31081da177e4SLinus Torvalds { 31091da177e4SLinus Torvalds int error; 31101da177e4SLinus Torvalds int len; 31111da177e4SLinus Torvalds struct inode *inode; 31127ad0414bSMatthew Wilcox (Oracle) struct folio *folio; 31131da177e4SLinus Torvalds 31141da177e4SLinus Torvalds len = strlen(symname) + 1; 311509cbfeafSKirill A. Shutemov if (len > PAGE_SIZE) 31161da177e4SLinus Torvalds return -ENAMETOOLONG; 31171da177e4SLinus Torvalds 31180825a6f9SJoe Perches inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, 31190825a6f9SJoe Perches VM_NORESERVE); 31201da177e4SLinus Torvalds if (!inode) 31211da177e4SLinus Torvalds return -ENOSPC; 31221da177e4SLinus Torvalds 31239d8f13baSMimi Zohar error = security_inode_init_security(inode, dir, &dentry->d_name, 31246d9d88d0SJarkko Sakkinen shmem_initxattrs, NULL); 3125343c3d7fSMateusz Nosek if (error && error != -EOPNOTSUPP) { 3126570bc1c2SStephen Smalley iput(inode); 3127570bc1c2SStephen Smalley return error; 3128570bc1c2SStephen Smalley } 3129570bc1c2SStephen Smalley 31301da177e4SLinus Torvalds inode->i_size = len-1; 313169f07ec9SHugh Dickins if (len <= SHORT_SYMLINK_LEN) { 31323ed47db3SAl Viro inode->i_link = kmemdup(symname, len, GFP_KERNEL); 31333ed47db3SAl Viro if (!inode->i_link) { 313469f07ec9SHugh Dickins iput(inode); 313569f07ec9SHugh Dickins return -ENOMEM; 313669f07ec9SHugh Dickins } 313769f07ec9SHugh Dickins inode->i_op = &shmem_short_symlink_operations; 31381da177e4SLinus Torvalds } else { 3139e8ecde25SAl Viro inode_nohighmem(inode); 31407ad0414bSMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_WRITE); 31411da177e4SLinus Torvalds if (error) { 31421da177e4SLinus Torvalds iput(inode); 31431da177e4SLinus Torvalds return error; 31441da177e4SLinus Torvalds } 314514fcc23fSHugh Dickins inode->i_mapping->a_ops = &shmem_aops; 31461da177e4SLinus Torvalds inode->i_op = &shmem_symlink_inode_operations; 31477ad0414bSMatthew Wilcox (Oracle) memcpy(folio_address(folio), symname, len); 31487ad0414bSMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 31497ad0414bSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 31507ad0414bSMatthew Wilcox (Oracle) folio_unlock(folio); 31517ad0414bSMatthew Wilcox (Oracle) folio_put(folio); 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds dir->i_size += BOGO_DIRENT_SIZE; 3154078cd827SDeepa Dinamani dir->i_ctime = dir->i_mtime = current_time(dir); 315536f05cabSJeff Layton inode_inc_iversion(dir); 31561da177e4SLinus Torvalds d_instantiate(dentry, inode); 31571da177e4SLinus Torvalds dget(dentry); 31581da177e4SLinus Torvalds return 0; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 3161fceef393SAl Viro static void shmem_put_link(void *arg) 3162fceef393SAl Viro { 3163e4b57722SMatthew Wilcox (Oracle) folio_mark_accessed(arg); 3164e4b57722SMatthew Wilcox (Oracle) folio_put(arg); 3165fceef393SAl Viro } 3166fceef393SAl Viro 31676b255391SAl Viro static const char *shmem_get_link(struct dentry *dentry, 3168fceef393SAl Viro struct inode *inode, 3169fceef393SAl Viro struct delayed_call *done) 31701da177e4SLinus Torvalds { 3171e4b57722SMatthew Wilcox (Oracle) struct folio *folio = NULL; 31726b255391SAl Viro int error; 3173e4b57722SMatthew Wilcox (Oracle) 31746a6c9904SAl Viro if (!dentry) { 3175e4b57722SMatthew Wilcox (Oracle) folio = filemap_get_folio(inode->i_mapping, 0); 3176e4b57722SMatthew Wilcox (Oracle) if (!folio) 31776b255391SAl Viro return ERR_PTR(-ECHILD); 31787459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0)) || 3179e4b57722SMatthew Wilcox (Oracle) !folio_test_uptodate(folio)) { 3180e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 31816a6c9904SAl Viro return ERR_PTR(-ECHILD); 31826a6c9904SAl Viro } 31836a6c9904SAl Viro } else { 3184e4b57722SMatthew Wilcox (Oracle) error = shmem_get_folio(inode, 0, &folio, SGP_READ); 3185680baacbSAl Viro if (error) 3186680baacbSAl Viro return ERR_PTR(error); 3187e4b57722SMatthew Wilcox (Oracle) if (!folio) 3188a7605426SYang Shi return ERR_PTR(-ECHILD); 31897459c149SMatthew Wilcox (Oracle) if (PageHWPoison(folio_page(folio, 0))) { 3190e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 3191e4b57722SMatthew Wilcox (Oracle) folio_put(folio); 3192a7605426SYang Shi return ERR_PTR(-ECHILD); 3193a7605426SYang Shi } 3194e4b57722SMatthew Wilcox (Oracle) folio_unlock(folio); 31951da177e4SLinus Torvalds } 3196e4b57722SMatthew Wilcox (Oracle) set_delayed_call(done, shmem_put_link, folio); 3197e4b57722SMatthew Wilcox (Oracle) return folio_address(folio); 31981da177e4SLinus Torvalds } 31991da177e4SLinus Torvalds 3200b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3201e408e695STheodore Ts'o 3202e408e695STheodore Ts'o static int shmem_fileattr_get(struct dentry *dentry, struct fileattr *fa) 3203e408e695STheodore Ts'o { 3204e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3205e408e695STheodore Ts'o 3206e408e695STheodore Ts'o fileattr_fill_flags(fa, info->fsflags & SHMEM_FL_USER_VISIBLE); 3207e408e695STheodore Ts'o 3208e408e695STheodore Ts'o return 0; 3209e408e695STheodore Ts'o } 3210e408e695STheodore Ts'o 3211e408e695STheodore Ts'o static int shmem_fileattr_set(struct user_namespace *mnt_userns, 3212e408e695STheodore Ts'o struct dentry *dentry, struct fileattr *fa) 3213e408e695STheodore Ts'o { 3214e408e695STheodore Ts'o struct inode *inode = d_inode(dentry); 3215e408e695STheodore Ts'o struct shmem_inode_info *info = SHMEM_I(inode); 3216e408e695STheodore Ts'o 3217e408e695STheodore Ts'o if (fileattr_has_fsx(fa)) 3218e408e695STheodore Ts'o return -EOPNOTSUPP; 3219cb241339SHugh Dickins if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) 3220cb241339SHugh Dickins return -EOPNOTSUPP; 3221e408e695STheodore Ts'o 3222e408e695STheodore Ts'o info->fsflags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | 3223e408e695STheodore Ts'o (fa->flags & SHMEM_FL_USER_MODIFIABLE); 3224e408e695STheodore Ts'o 3225cb241339SHugh Dickins shmem_set_inode_flags(inode, info->fsflags); 3226e408e695STheodore Ts'o inode->i_ctime = current_time(inode); 322736f05cabSJeff Layton inode_inc_iversion(inode); 3228e408e695STheodore Ts'o return 0; 3229e408e695STheodore Ts'o } 3230e408e695STheodore Ts'o 3231b09e0fa4SEric Paris /* 3232b09e0fa4SEric Paris * Superblocks without xattr inode operations may get some security.* xattr 3233b09e0fa4SEric Paris * support from the LSM "for free". As soon as we have any other xattrs 3234b09e0fa4SEric Paris * like ACLs, we also need to implement the security.* handlers at 3235b09e0fa4SEric Paris * filesystem level, though. 3236b09e0fa4SEric Paris */ 3237b09e0fa4SEric Paris 32386d9d88d0SJarkko Sakkinen /* 32396d9d88d0SJarkko Sakkinen * Callback for security_inode_init_security() for acquiring xattrs. 32406d9d88d0SJarkko Sakkinen */ 32416d9d88d0SJarkko Sakkinen static int shmem_initxattrs(struct inode *inode, 32426d9d88d0SJarkko Sakkinen const struct xattr *xattr_array, 32436d9d88d0SJarkko Sakkinen void *fs_info) 32446d9d88d0SJarkko Sakkinen { 32456d9d88d0SJarkko Sakkinen struct shmem_inode_info *info = SHMEM_I(inode); 32466d9d88d0SJarkko Sakkinen const struct xattr *xattr; 324738f38657SAristeu Rozanski struct simple_xattr *new_xattr; 32486d9d88d0SJarkko Sakkinen size_t len; 32496d9d88d0SJarkko Sakkinen 32506d9d88d0SJarkko Sakkinen for (xattr = xattr_array; xattr->name != NULL; xattr++) { 325138f38657SAristeu Rozanski new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 32526d9d88d0SJarkko Sakkinen if (!new_xattr) 32536d9d88d0SJarkko Sakkinen return -ENOMEM; 32546d9d88d0SJarkko Sakkinen 32556d9d88d0SJarkko Sakkinen len = strlen(xattr->name) + 1; 32566d9d88d0SJarkko Sakkinen new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 32576d9d88d0SJarkko Sakkinen GFP_KERNEL); 32586d9d88d0SJarkko Sakkinen if (!new_xattr->name) { 32593bef735aSChengguang Xu kvfree(new_xattr); 32606d9d88d0SJarkko Sakkinen return -ENOMEM; 32616d9d88d0SJarkko Sakkinen } 32626d9d88d0SJarkko Sakkinen 32636d9d88d0SJarkko Sakkinen memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 32646d9d88d0SJarkko Sakkinen XATTR_SECURITY_PREFIX_LEN); 32656d9d88d0SJarkko Sakkinen memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 32666d9d88d0SJarkko Sakkinen xattr->name, len); 32676d9d88d0SJarkko Sakkinen 326838f38657SAristeu Rozanski simple_xattr_list_add(&info->xattrs, new_xattr); 32696d9d88d0SJarkko Sakkinen } 32706d9d88d0SJarkko Sakkinen 32716d9d88d0SJarkko Sakkinen return 0; 32726d9d88d0SJarkko Sakkinen } 32736d9d88d0SJarkko Sakkinen 3274aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_get(const struct xattr_handler *handler, 3275b296821aSAl Viro struct dentry *unused, struct inode *inode, 3276b296821aSAl Viro const char *name, void *buffer, size_t size) 3277aa7c5241SAndreas Gruenbacher { 3278b296821aSAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 3279aa7c5241SAndreas Gruenbacher 3280aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 3281aa7c5241SAndreas Gruenbacher return simple_xattr_get(&info->xattrs, name, buffer, size); 3282aa7c5241SAndreas Gruenbacher } 3283aa7c5241SAndreas Gruenbacher 3284aa7c5241SAndreas Gruenbacher static int shmem_xattr_handler_set(const struct xattr_handler *handler, 3285e65ce2a5SChristian Brauner struct user_namespace *mnt_userns, 328659301226SAl Viro struct dentry *unused, struct inode *inode, 328759301226SAl Viro const char *name, const void *value, 328859301226SAl Viro size_t size, int flags) 3289aa7c5241SAndreas Gruenbacher { 329059301226SAl Viro struct shmem_inode_info *info = SHMEM_I(inode); 329136f05cabSJeff Layton int err; 3292aa7c5241SAndreas Gruenbacher 3293aa7c5241SAndreas Gruenbacher name = xattr_full_name(handler, name); 329436f05cabSJeff Layton err = simple_xattr_set(&info->xattrs, name, value, size, flags, NULL); 329536f05cabSJeff Layton if (!err) { 329636f05cabSJeff Layton inode->i_ctime = current_time(inode); 329736f05cabSJeff Layton inode_inc_iversion(inode); 329836f05cabSJeff Layton } 329936f05cabSJeff Layton return err; 3300aa7c5241SAndreas Gruenbacher } 3301aa7c5241SAndreas Gruenbacher 3302aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_security_xattr_handler = { 3303aa7c5241SAndreas Gruenbacher .prefix = XATTR_SECURITY_PREFIX, 3304aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3305aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3306aa7c5241SAndreas Gruenbacher }; 3307aa7c5241SAndreas Gruenbacher 3308aa7c5241SAndreas Gruenbacher static const struct xattr_handler shmem_trusted_xattr_handler = { 3309aa7c5241SAndreas Gruenbacher .prefix = XATTR_TRUSTED_PREFIX, 3310aa7c5241SAndreas Gruenbacher .get = shmem_xattr_handler_get, 3311aa7c5241SAndreas Gruenbacher .set = shmem_xattr_handler_set, 3312aa7c5241SAndreas Gruenbacher }; 3313aa7c5241SAndreas Gruenbacher 3314b09e0fa4SEric Paris static const struct xattr_handler *shmem_xattr_handlers[] = { 3315b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 3316feda821eSChristoph Hellwig &posix_acl_access_xattr_handler, 3317feda821eSChristoph Hellwig &posix_acl_default_xattr_handler, 3318b09e0fa4SEric Paris #endif 3319aa7c5241SAndreas Gruenbacher &shmem_security_xattr_handler, 3320aa7c5241SAndreas Gruenbacher &shmem_trusted_xattr_handler, 3321b09e0fa4SEric Paris NULL 3322b09e0fa4SEric Paris }; 3323b09e0fa4SEric Paris 3324b09e0fa4SEric Paris static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 3325b09e0fa4SEric Paris { 332675c3cfa8SDavid Howells struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 3327786534b9SAndreas Gruenbacher return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 3328b09e0fa4SEric Paris } 3329b09e0fa4SEric Paris #endif /* CONFIG_TMPFS_XATTR */ 3330b09e0fa4SEric Paris 333169f07ec9SHugh Dickins static const struct inode_operations shmem_short_symlink_operations = { 3332f7cd16a5SXavier Roche .getattr = shmem_getattr, 33336b255391SAl Viro .get_link = simple_get_link, 3334b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3335b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3336b09e0fa4SEric Paris #endif 33371da177e4SLinus Torvalds }; 33381da177e4SLinus Torvalds 333992e1d5beSArjan van de Ven static const struct inode_operations shmem_symlink_inode_operations = { 3340f7cd16a5SXavier Roche .getattr = shmem_getattr, 33416b255391SAl Viro .get_link = shmem_get_link, 3342b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3343b09e0fa4SEric Paris .listxattr = shmem_listxattr, 334439f0247dSAndreas Gruenbacher #endif 3345b09e0fa4SEric Paris }; 334639f0247dSAndreas Gruenbacher 334791828a40SDavid M. Grimes static struct dentry *shmem_get_parent(struct dentry *child) 334891828a40SDavid M. Grimes { 334991828a40SDavid M. Grimes return ERR_PTR(-ESTALE); 335091828a40SDavid M. Grimes } 335191828a40SDavid M. Grimes 335291828a40SDavid M. Grimes static int shmem_match(struct inode *ino, void *vfh) 335391828a40SDavid M. Grimes { 335491828a40SDavid M. Grimes __u32 *fh = vfh; 335591828a40SDavid M. Grimes __u64 inum = fh[2]; 335691828a40SDavid M. Grimes inum = (inum << 32) | fh[1]; 335791828a40SDavid M. Grimes return ino->i_ino == inum && fh[0] == ino->i_generation; 335891828a40SDavid M. Grimes } 335991828a40SDavid M. Grimes 336012ba780dSAmir Goldstein /* Find any alias of inode, but prefer a hashed alias */ 336112ba780dSAmir Goldstein static struct dentry *shmem_find_alias(struct inode *inode) 336212ba780dSAmir Goldstein { 336312ba780dSAmir Goldstein struct dentry *alias = d_find_alias(inode); 336412ba780dSAmir Goldstein 336512ba780dSAmir Goldstein return alias ?: d_find_any_alias(inode); 336612ba780dSAmir Goldstein } 336712ba780dSAmir Goldstein 336812ba780dSAmir Goldstein 3369480b116cSChristoph Hellwig static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 3370480b116cSChristoph Hellwig struct fid *fid, int fh_len, int fh_type) 337191828a40SDavid M. Grimes { 337291828a40SDavid M. Grimes struct inode *inode; 3373480b116cSChristoph Hellwig struct dentry *dentry = NULL; 337435c2a7f4SHugh Dickins u64 inum; 337591828a40SDavid M. Grimes 3376480b116cSChristoph Hellwig if (fh_len < 3) 3377480b116cSChristoph Hellwig return NULL; 3378480b116cSChristoph Hellwig 337935c2a7f4SHugh Dickins inum = fid->raw[2]; 338035c2a7f4SHugh Dickins inum = (inum << 32) | fid->raw[1]; 338135c2a7f4SHugh Dickins 3382480b116cSChristoph Hellwig inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 3383480b116cSChristoph Hellwig shmem_match, fid->raw); 338491828a40SDavid M. Grimes if (inode) { 338512ba780dSAmir Goldstein dentry = shmem_find_alias(inode); 338691828a40SDavid M. Grimes iput(inode); 338791828a40SDavid M. Grimes } 338891828a40SDavid M. Grimes 3389480b116cSChristoph Hellwig return dentry; 339091828a40SDavid M. Grimes } 339191828a40SDavid M. Grimes 3392b0b0382bSAl Viro static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 3393b0b0382bSAl Viro struct inode *parent) 339491828a40SDavid M. Grimes { 33955fe0c237SAneesh Kumar K.V if (*len < 3) { 33965fe0c237SAneesh Kumar K.V *len = 3; 339794e07a75SNamjae Jeon return FILEID_INVALID; 33985fe0c237SAneesh Kumar K.V } 339991828a40SDavid M. Grimes 34001d3382cbSAl Viro if (inode_unhashed(inode)) { 340191828a40SDavid M. Grimes /* Unfortunately insert_inode_hash is not idempotent, 340291828a40SDavid M. Grimes * so as we hash inodes here rather than at creation 340391828a40SDavid M. Grimes * time, we need a lock to ensure we only try 340491828a40SDavid M. Grimes * to do it once 340591828a40SDavid M. Grimes */ 340691828a40SDavid M. Grimes static DEFINE_SPINLOCK(lock); 340791828a40SDavid M. Grimes spin_lock(&lock); 34081d3382cbSAl Viro if (inode_unhashed(inode)) 340991828a40SDavid M. Grimes __insert_inode_hash(inode, 341091828a40SDavid M. Grimes inode->i_ino + inode->i_generation); 341191828a40SDavid M. Grimes spin_unlock(&lock); 341291828a40SDavid M. Grimes } 341391828a40SDavid M. Grimes 341491828a40SDavid M. Grimes fh[0] = inode->i_generation; 341591828a40SDavid M. Grimes fh[1] = inode->i_ino; 341691828a40SDavid M. Grimes fh[2] = ((__u64)inode->i_ino) >> 32; 341791828a40SDavid M. Grimes 341891828a40SDavid M. Grimes *len = 3; 341991828a40SDavid M. Grimes return 1; 342091828a40SDavid M. Grimes } 342191828a40SDavid M. Grimes 342239655164SChristoph Hellwig static const struct export_operations shmem_export_ops = { 342391828a40SDavid M. Grimes .get_parent = shmem_get_parent, 342491828a40SDavid M. Grimes .encode_fh = shmem_encode_fh, 3425480b116cSChristoph Hellwig .fh_to_dentry = shmem_fh_to_dentry, 342691828a40SDavid M. Grimes }; 342791828a40SDavid M. Grimes 3428626c3920SAl Viro enum shmem_param { 3429626c3920SAl Viro Opt_gid, 3430626c3920SAl Viro Opt_huge, 3431626c3920SAl Viro Opt_mode, 3432626c3920SAl Viro Opt_mpol, 3433626c3920SAl Viro Opt_nr_blocks, 3434626c3920SAl Viro Opt_nr_inodes, 3435626c3920SAl Viro Opt_size, 3436626c3920SAl Viro Opt_uid, 3437ea3271f7SChris Down Opt_inode32, 3438ea3271f7SChris Down Opt_inode64, 3439626c3920SAl Viro }; 34401da177e4SLinus Torvalds 34415eede625SAl Viro static const struct constant_table shmem_param_enums_huge[] = { 34422710c957SAl Viro {"never", SHMEM_HUGE_NEVER }, 34432710c957SAl Viro {"always", SHMEM_HUGE_ALWAYS }, 34442710c957SAl Viro {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 34452710c957SAl Viro {"advise", SHMEM_HUGE_ADVISE }, 34462710c957SAl Viro {} 34472710c957SAl Viro }; 34482710c957SAl Viro 3449d7167b14SAl Viro const struct fs_parameter_spec shmem_fs_parameters[] = { 3450626c3920SAl Viro fsparam_u32 ("gid", Opt_gid), 34512710c957SAl Viro fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 3452626c3920SAl Viro fsparam_u32oct("mode", Opt_mode), 3453626c3920SAl Viro fsparam_string("mpol", Opt_mpol), 3454626c3920SAl Viro fsparam_string("nr_blocks", Opt_nr_blocks), 3455626c3920SAl Viro fsparam_string("nr_inodes", Opt_nr_inodes), 3456626c3920SAl Viro fsparam_string("size", Opt_size), 3457626c3920SAl Viro fsparam_u32 ("uid", Opt_uid), 3458ea3271f7SChris Down fsparam_flag ("inode32", Opt_inode32), 3459ea3271f7SChris Down fsparam_flag ("inode64", Opt_inode64), 3460626c3920SAl Viro {} 3461626c3920SAl Viro }; 3462626c3920SAl Viro 3463f3235626SDavid Howells static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 3464626c3920SAl Viro { 3465f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3466626c3920SAl Viro struct fs_parse_result result; 3467e04dc423SAl Viro unsigned long long size; 3468626c3920SAl Viro char *rest; 3469626c3920SAl Viro int opt; 3470626c3920SAl Viro 3471d7167b14SAl Viro opt = fs_parse(fc, shmem_fs_parameters, param, &result); 3472f3235626SDavid Howells if (opt < 0) 3473626c3920SAl Viro return opt; 3474626c3920SAl Viro 3475626c3920SAl Viro switch (opt) { 3476626c3920SAl Viro case Opt_size: 3477626c3920SAl Viro size = memparse(param->string, &rest); 3478e04dc423SAl Viro if (*rest == '%') { 3479e04dc423SAl Viro size <<= PAGE_SHIFT; 3480e04dc423SAl Viro size *= totalram_pages(); 3481e04dc423SAl Viro do_div(size, 100); 3482e04dc423SAl Viro rest++; 3483e04dc423SAl Viro } 3484e04dc423SAl Viro if (*rest) 3485626c3920SAl Viro goto bad_value; 3486e04dc423SAl Viro ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 3487e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3488626c3920SAl Viro break; 3489626c3920SAl Viro case Opt_nr_blocks: 3490626c3920SAl Viro ctx->blocks = memparse(param->string, &rest); 34910c98c8e1SZhaoLong Wang if (*rest || ctx->blocks > S64_MAX) 3492626c3920SAl Viro goto bad_value; 3493e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_BLOCKS; 3494626c3920SAl Viro break; 3495626c3920SAl Viro case Opt_nr_inodes: 3496626c3920SAl Viro ctx->inodes = memparse(param->string, &rest); 3497e04dc423SAl Viro if (*rest) 3498626c3920SAl Viro goto bad_value; 3499e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_INODES; 3500626c3920SAl Viro break; 3501626c3920SAl Viro case Opt_mode: 3502626c3920SAl Viro ctx->mode = result.uint_32 & 07777; 3503626c3920SAl Viro break; 3504626c3920SAl Viro case Opt_uid: 3505626c3920SAl Viro ctx->uid = make_kuid(current_user_ns(), result.uint_32); 3506e04dc423SAl Viro if (!uid_valid(ctx->uid)) 3507626c3920SAl Viro goto bad_value; 3508626c3920SAl Viro break; 3509626c3920SAl Viro case Opt_gid: 3510626c3920SAl Viro ctx->gid = make_kgid(current_user_ns(), result.uint_32); 3511e04dc423SAl Viro if (!gid_valid(ctx->gid)) 3512626c3920SAl Viro goto bad_value; 3513626c3920SAl Viro break; 3514626c3920SAl Viro case Opt_huge: 3515626c3920SAl Viro ctx->huge = result.uint_32; 3516626c3920SAl Viro if (ctx->huge != SHMEM_HUGE_NEVER && 3517396bcc52SMatthew Wilcox (Oracle) !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 3518626c3920SAl Viro has_transparent_hugepage())) 3519626c3920SAl Viro goto unsupported_parameter; 3520e04dc423SAl Viro ctx->seen |= SHMEM_SEEN_HUGE; 3521626c3920SAl Viro break; 3522626c3920SAl Viro case Opt_mpol: 3523626c3920SAl Viro if (IS_ENABLED(CONFIG_NUMA)) { 3524e04dc423SAl Viro mpol_put(ctx->mpol); 3525e04dc423SAl Viro ctx->mpol = NULL; 3526626c3920SAl Viro if (mpol_parse_str(param->string, &ctx->mpol)) 3527626c3920SAl Viro goto bad_value; 3528626c3920SAl Viro break; 3529626c3920SAl Viro } 3530626c3920SAl Viro goto unsupported_parameter; 3531ea3271f7SChris Down case Opt_inode32: 3532ea3271f7SChris Down ctx->full_inums = false; 3533ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3534ea3271f7SChris Down break; 3535ea3271f7SChris Down case Opt_inode64: 3536ea3271f7SChris Down if (sizeof(ino_t) < 8) { 3537ea3271f7SChris Down return invalfc(fc, 3538ea3271f7SChris Down "Cannot use inode64 with <64bit inums in kernel\n"); 3539ea3271f7SChris Down } 3540ea3271f7SChris Down ctx->full_inums = true; 3541ea3271f7SChris Down ctx->seen |= SHMEM_SEEN_INUMS; 3542ea3271f7SChris Down break; 3543e04dc423SAl Viro } 3544e04dc423SAl Viro return 0; 3545e04dc423SAl Viro 3546626c3920SAl Viro unsupported_parameter: 3547f35aa2bcSAl Viro return invalfc(fc, "Unsupported parameter '%s'", param->key); 3548626c3920SAl Viro bad_value: 3549f35aa2bcSAl Viro return invalfc(fc, "Bad value for '%s'", param->key); 3550e04dc423SAl Viro } 3551e04dc423SAl Viro 3552f3235626SDavid Howells static int shmem_parse_options(struct fs_context *fc, void *data) 3553e04dc423SAl Viro { 3554f3235626SDavid Howells char *options = data; 3555f3235626SDavid Howells 355633f37c64SAl Viro if (options) { 355733f37c64SAl Viro int err = security_sb_eat_lsm_opts(options, &fc->security); 355833f37c64SAl Viro if (err) 355933f37c64SAl Viro return err; 356033f37c64SAl Viro } 356133f37c64SAl Viro 3562b00dc3adSHugh Dickins while (options != NULL) { 3563626c3920SAl Viro char *this_char = options; 3564b00dc3adSHugh Dickins for (;;) { 3565b00dc3adSHugh Dickins /* 3566b00dc3adSHugh Dickins * NUL-terminate this option: unfortunately, 3567b00dc3adSHugh Dickins * mount options form a comma-separated list, 3568b00dc3adSHugh Dickins * but mpol's nodelist may also contain commas. 3569b00dc3adSHugh Dickins */ 3570b00dc3adSHugh Dickins options = strchr(options, ','); 3571b00dc3adSHugh Dickins if (options == NULL) 3572b00dc3adSHugh Dickins break; 3573b00dc3adSHugh Dickins options++; 3574b00dc3adSHugh Dickins if (!isdigit(*options)) { 3575b00dc3adSHugh Dickins options[-1] = '\0'; 3576b00dc3adSHugh Dickins break; 3577b00dc3adSHugh Dickins } 3578b00dc3adSHugh Dickins } 3579626c3920SAl Viro if (*this_char) { 3580626c3920SAl Viro char *value = strchr(this_char, '='); 3581f3235626SDavid Howells size_t len = 0; 3582626c3920SAl Viro int err; 3583626c3920SAl Viro 3584626c3920SAl Viro if (value) { 3585626c3920SAl Viro *value++ = '\0'; 3586f3235626SDavid Howells len = strlen(value); 35871da177e4SLinus Torvalds } 3588f3235626SDavid Howells err = vfs_parse_fs_string(fc, this_char, value, len); 3589f3235626SDavid Howells if (err < 0) 3590f3235626SDavid Howells return err; 35911da177e4SLinus Torvalds } 3592626c3920SAl Viro } 35931da177e4SLinus Torvalds return 0; 35941da177e4SLinus Torvalds } 35951da177e4SLinus Torvalds 3596f3235626SDavid Howells /* 3597f3235626SDavid Howells * Reconfigure a shmem filesystem. 3598f3235626SDavid Howells * 3599f3235626SDavid Howells * Note that we disallow change from limited->unlimited blocks/inodes while any 3600f3235626SDavid Howells * are in use; but we must separately disallow unlimited->limited, because in 3601f3235626SDavid Howells * that case we have no record of how much is already in use. 3602f3235626SDavid Howells */ 3603f3235626SDavid Howells static int shmem_reconfigure(struct fs_context *fc) 36041da177e4SLinus Torvalds { 3605f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3606f3235626SDavid Howells struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 36070edd73b3SHugh Dickins unsigned long inodes; 3608bf11b9a8SSebastian Andrzej Siewior struct mempolicy *mpol = NULL; 3609f3235626SDavid Howells const char *err; 36100edd73b3SHugh Dickins 3611bf11b9a8SSebastian Andrzej Siewior raw_spin_lock(&sbinfo->stat_lock); 36120edd73b3SHugh Dickins inodes = sbinfo->max_inodes - sbinfo->free_inodes; 36130c98c8e1SZhaoLong Wang 3614f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 3615f3235626SDavid Howells if (!sbinfo->max_blocks) { 3616f3235626SDavid Howells err = "Cannot retroactively limit size"; 36170edd73b3SHugh Dickins goto out; 36180b5071ddSAl Viro } 3619f3235626SDavid Howells if (percpu_counter_compare(&sbinfo->used_blocks, 3620f3235626SDavid Howells ctx->blocks) > 0) { 3621f3235626SDavid Howells err = "Too small a size for current use"; 36220b5071ddSAl Viro goto out; 3623f3235626SDavid Howells } 3624f3235626SDavid Howells } 3625f3235626SDavid Howells if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 3626f3235626SDavid Howells if (!sbinfo->max_inodes) { 3627f3235626SDavid Howells err = "Cannot retroactively limit inodes"; 36280b5071ddSAl Viro goto out; 36290b5071ddSAl Viro } 3630f3235626SDavid Howells if (ctx->inodes < inodes) { 3631f3235626SDavid Howells err = "Too few inodes for current use"; 3632f3235626SDavid Howells goto out; 3633f3235626SDavid Howells } 3634f3235626SDavid Howells } 36350edd73b3SHugh Dickins 3636ea3271f7SChris Down if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && 3637ea3271f7SChris Down sbinfo->next_ino > UINT_MAX) { 3638ea3271f7SChris Down err = "Current inum too high to switch to 32-bit inums"; 3639ea3271f7SChris Down goto out; 3640ea3271f7SChris Down } 3641ea3271f7SChris Down 3642f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_HUGE) 3643f3235626SDavid Howells sbinfo->huge = ctx->huge; 3644ea3271f7SChris Down if (ctx->seen & SHMEM_SEEN_INUMS) 3645ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3646f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_BLOCKS) 3647f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3648f3235626SDavid Howells if (ctx->seen & SHMEM_SEEN_INODES) { 3649f3235626SDavid Howells sbinfo->max_inodes = ctx->inodes; 3650f3235626SDavid Howells sbinfo->free_inodes = ctx->inodes - inodes; 36510b5071ddSAl Viro } 365271fe804bSLee Schermerhorn 36535f00110fSGreg Thelen /* 36545f00110fSGreg Thelen * Preserve previous mempolicy unless mpol remount option was specified. 36555f00110fSGreg Thelen */ 3656f3235626SDavid Howells if (ctx->mpol) { 3657bf11b9a8SSebastian Andrzej Siewior mpol = sbinfo->mpol; 3658f3235626SDavid Howells sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 3659f3235626SDavid Howells ctx->mpol = NULL; 36605f00110fSGreg Thelen } 3661bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3662bf11b9a8SSebastian Andrzej Siewior mpol_put(mpol); 3663f3235626SDavid Howells return 0; 36640edd73b3SHugh Dickins out: 3665bf11b9a8SSebastian Andrzej Siewior raw_spin_unlock(&sbinfo->stat_lock); 3666f35aa2bcSAl Viro return invalfc(fc, "%s", err); 36671da177e4SLinus Torvalds } 3668680d794bSakpm@linux-foundation.org 366934c80b1dSAl Viro static int shmem_show_options(struct seq_file *seq, struct dentry *root) 3670680d794bSakpm@linux-foundation.org { 367134c80b1dSAl Viro struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 3672680d794bSakpm@linux-foundation.org 3673680d794bSakpm@linux-foundation.org if (sbinfo->max_blocks != shmem_default_max_blocks()) 3674680d794bSakpm@linux-foundation.org seq_printf(seq, ",size=%luk", 367509cbfeafSKirill A. Shutemov sbinfo->max_blocks << (PAGE_SHIFT - 10)); 3676680d794bSakpm@linux-foundation.org if (sbinfo->max_inodes != shmem_default_max_inodes()) 3677680d794bSakpm@linux-foundation.org seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 36780825a6f9SJoe Perches if (sbinfo->mode != (0777 | S_ISVTX)) 367909208d15SAl Viro seq_printf(seq, ",mode=%03ho", sbinfo->mode); 36808751e039SEric W. Biederman if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 36818751e039SEric W. Biederman seq_printf(seq, ",uid=%u", 36828751e039SEric W. Biederman from_kuid_munged(&init_user_ns, sbinfo->uid)); 36838751e039SEric W. Biederman if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 36848751e039SEric W. Biederman seq_printf(seq, ",gid=%u", 36858751e039SEric W. Biederman from_kgid_munged(&init_user_ns, sbinfo->gid)); 3686ea3271f7SChris Down 3687ea3271f7SChris Down /* 3688ea3271f7SChris Down * Showing inode{64,32} might be useful even if it's the system default, 3689ea3271f7SChris Down * since then people don't have to resort to checking both here and 3690ea3271f7SChris Down * /proc/config.gz to confirm 64-bit inums were successfully applied 3691ea3271f7SChris Down * (which may not even exist if IKCONFIG_PROC isn't enabled). 3692ea3271f7SChris Down * 3693ea3271f7SChris Down * We hide it when inode64 isn't the default and we are using 32-bit 3694ea3271f7SChris Down * inodes, since that probably just means the feature isn't even under 3695ea3271f7SChris Down * consideration. 3696ea3271f7SChris Down * 3697ea3271f7SChris Down * As such: 3698ea3271f7SChris Down * 3699ea3271f7SChris Down * +-----------------+-----------------+ 3700ea3271f7SChris Down * | TMPFS_INODE64=y | TMPFS_INODE64=n | 3701ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3702ea3271f7SChris Down * | full_inums=true | show | show | 3703ea3271f7SChris Down * | full_inums=false | show | hide | 3704ea3271f7SChris Down * +------------------+-----------------+-----------------+ 3705ea3271f7SChris Down * 3706ea3271f7SChris Down */ 3707ea3271f7SChris Down if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) 3708ea3271f7SChris Down seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); 3709396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 37105a6e75f8SKirill A. Shutemov /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 37115a6e75f8SKirill A. Shutemov if (sbinfo->huge) 37125a6e75f8SKirill A. Shutemov seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 37135a6e75f8SKirill A. Shutemov #endif 371471fe804bSLee Schermerhorn shmem_show_mpol(seq, sbinfo->mpol); 3715680d794bSakpm@linux-foundation.org return 0; 3716680d794bSakpm@linux-foundation.org } 37179183df25SDavid Herrmann 3718680d794bSakpm@linux-foundation.org #endif /* CONFIG_TMPFS */ 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds static void shmem_put_super(struct super_block *sb) 37211da177e4SLinus Torvalds { 3722602586a8SHugh Dickins struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3723602586a8SHugh Dickins 3724e809d5f0SChris Down free_percpu(sbinfo->ino_batch); 3725602586a8SHugh Dickins percpu_counter_destroy(&sbinfo->used_blocks); 372649cd0a5cSGreg Thelen mpol_put(sbinfo->mpol); 3727602586a8SHugh Dickins kfree(sbinfo); 37281da177e4SLinus Torvalds sb->s_fs_info = NULL; 37291da177e4SLinus Torvalds } 37301da177e4SLinus Torvalds 3731f3235626SDavid Howells static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 37321da177e4SLinus Torvalds { 3733f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 37341da177e4SLinus Torvalds struct inode *inode; 37350edd73b3SHugh Dickins struct shmem_sb_info *sbinfo; 3736680d794bSakpm@linux-foundation.org 3737680d794bSakpm@linux-foundation.org /* Round up to L1_CACHE_BYTES to resist false sharing */ 3738425fbf04SPekka Enberg sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 3739680d794bSakpm@linux-foundation.org L1_CACHE_BYTES), GFP_KERNEL); 3740680d794bSakpm@linux-foundation.org if (!sbinfo) 3741680d794bSakpm@linux-foundation.org return -ENOMEM; 3742680d794bSakpm@linux-foundation.org 3743680d794bSakpm@linux-foundation.org sb->s_fs_info = sbinfo; 37441da177e4SLinus Torvalds 37450edd73b3SHugh Dickins #ifdef CONFIG_TMPFS 37461da177e4SLinus Torvalds /* 37471da177e4SLinus Torvalds * Per default we only allow half of the physical ram per 37481da177e4SLinus Torvalds * tmpfs instance, limiting inodes to one per page of lowmem; 37491da177e4SLinus Torvalds * but the internal instance is left unlimited. 37501da177e4SLinus Torvalds */ 37511751e8a6SLinus Torvalds if (!(sb->s_flags & SB_KERNMOUNT)) { 3752f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 3753f3235626SDavid Howells ctx->blocks = shmem_default_max_blocks(); 3754f3235626SDavid Howells if (!(ctx->seen & SHMEM_SEEN_INODES)) 3755f3235626SDavid Howells ctx->inodes = shmem_default_max_inodes(); 3756ea3271f7SChris Down if (!(ctx->seen & SHMEM_SEEN_INUMS)) 3757ea3271f7SChris Down ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); 3758ca4e0519SAl Viro } else { 37591751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37601da177e4SLinus Torvalds } 376191828a40SDavid M. Grimes sb->s_export_op = &shmem_export_ops; 376236f05cabSJeff Layton sb->s_flags |= SB_NOSEC | SB_I_VERSION; 37630edd73b3SHugh Dickins #else 37641751e8a6SLinus Torvalds sb->s_flags |= SB_NOUSER; 37650edd73b3SHugh Dickins #endif 3766f3235626SDavid Howells sbinfo->max_blocks = ctx->blocks; 3767f3235626SDavid Howells sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; 3768e809d5f0SChris Down if (sb->s_flags & SB_KERNMOUNT) { 3769e809d5f0SChris Down sbinfo->ino_batch = alloc_percpu(ino_t); 3770e809d5f0SChris Down if (!sbinfo->ino_batch) 3771e809d5f0SChris Down goto failed; 3772e809d5f0SChris Down } 3773f3235626SDavid Howells sbinfo->uid = ctx->uid; 3774f3235626SDavid Howells sbinfo->gid = ctx->gid; 3775ea3271f7SChris Down sbinfo->full_inums = ctx->full_inums; 3776f3235626SDavid Howells sbinfo->mode = ctx->mode; 3777f3235626SDavid Howells sbinfo->huge = ctx->huge; 3778f3235626SDavid Howells sbinfo->mpol = ctx->mpol; 3779f3235626SDavid Howells ctx->mpol = NULL; 37801da177e4SLinus Torvalds 3781bf11b9a8SSebastian Andrzej Siewior raw_spin_lock_init(&sbinfo->stat_lock); 3782908c7f19STejun Heo if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 3783602586a8SHugh Dickins goto failed; 3784779750d2SKirill A. Shutemov spin_lock_init(&sbinfo->shrinklist_lock); 3785779750d2SKirill A. Shutemov INIT_LIST_HEAD(&sbinfo->shrinklist); 37861da177e4SLinus Torvalds 3787285b2c4fSHugh Dickins sb->s_maxbytes = MAX_LFS_FILESIZE; 378809cbfeafSKirill A. Shutemov sb->s_blocksize = PAGE_SIZE; 378909cbfeafSKirill A. Shutemov sb->s_blocksize_bits = PAGE_SHIFT; 37901da177e4SLinus Torvalds sb->s_magic = TMPFS_MAGIC; 37911da177e4SLinus Torvalds sb->s_op = &shmem_ops; 3792cfd95a9cSRobin H. Johnson sb->s_time_gran = 1; 3793b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 379439f0247dSAndreas Gruenbacher sb->s_xattr = shmem_xattr_handlers; 3795b09e0fa4SEric Paris #endif 3796b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_POSIX_ACL 37971751e8a6SLinus Torvalds sb->s_flags |= SB_POSIXACL; 379839f0247dSAndreas Gruenbacher #endif 37992b4db796SAmir Goldstein uuid_gen(&sb->s_uuid); 38000edd73b3SHugh Dickins 3801454abafeSDmitry Monakhov inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 38021da177e4SLinus Torvalds if (!inode) 38031da177e4SLinus Torvalds goto failed; 3804680d794bSakpm@linux-foundation.org inode->i_uid = sbinfo->uid; 3805680d794bSakpm@linux-foundation.org inode->i_gid = sbinfo->gid; 3806318ceed0SAl Viro sb->s_root = d_make_root(inode); 3807318ceed0SAl Viro if (!sb->s_root) 380848fde701SAl Viro goto failed; 38091da177e4SLinus Torvalds return 0; 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds failed: 38121da177e4SLinus Torvalds shmem_put_super(sb); 3813f2b346e4SMiaohe Lin return -ENOMEM; 38141da177e4SLinus Torvalds } 38151da177e4SLinus Torvalds 3816f3235626SDavid Howells static int shmem_get_tree(struct fs_context *fc) 3817f3235626SDavid Howells { 3818f3235626SDavid Howells return get_tree_nodev(fc, shmem_fill_super); 3819f3235626SDavid Howells } 3820f3235626SDavid Howells 3821f3235626SDavid Howells static void shmem_free_fc(struct fs_context *fc) 3822f3235626SDavid Howells { 3823f3235626SDavid Howells struct shmem_options *ctx = fc->fs_private; 3824f3235626SDavid Howells 3825f3235626SDavid Howells if (ctx) { 3826f3235626SDavid Howells mpol_put(ctx->mpol); 3827f3235626SDavid Howells kfree(ctx); 3828f3235626SDavid Howells } 3829f3235626SDavid Howells } 3830f3235626SDavid Howells 3831f3235626SDavid Howells static const struct fs_context_operations shmem_fs_context_ops = { 3832f3235626SDavid Howells .free = shmem_free_fc, 3833f3235626SDavid Howells .get_tree = shmem_get_tree, 3834f3235626SDavid Howells #ifdef CONFIG_TMPFS 3835f3235626SDavid Howells .parse_monolithic = shmem_parse_options, 3836f3235626SDavid Howells .parse_param = shmem_parse_one, 3837f3235626SDavid Howells .reconfigure = shmem_reconfigure, 3838f3235626SDavid Howells #endif 3839f3235626SDavid Howells }; 3840f3235626SDavid Howells 3841fcc234f8SPekka Enberg static struct kmem_cache *shmem_inode_cachep; 38421da177e4SLinus Torvalds 38431da177e4SLinus Torvalds static struct inode *shmem_alloc_inode(struct super_block *sb) 38441da177e4SLinus Torvalds { 384541ffe5d5SHugh Dickins struct shmem_inode_info *info; 3846fd60b288SMuchun Song info = alloc_inode_sb(sb, shmem_inode_cachep, GFP_KERNEL); 384741ffe5d5SHugh Dickins if (!info) 38481da177e4SLinus Torvalds return NULL; 384941ffe5d5SHugh Dickins return &info->vfs_inode; 38501da177e4SLinus Torvalds } 38511da177e4SLinus Torvalds 385274b1da56SAl Viro static void shmem_free_in_core_inode(struct inode *inode) 3853fa0d7e3dSNick Piggin { 385484e710daSAl Viro if (S_ISLNK(inode->i_mode)) 38553ed47db3SAl Viro kfree(inode->i_link); 3856fa0d7e3dSNick Piggin kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 3857fa0d7e3dSNick Piggin } 3858fa0d7e3dSNick Piggin 38591da177e4SLinus Torvalds static void shmem_destroy_inode(struct inode *inode) 38601da177e4SLinus Torvalds { 386109208d15SAl Viro if (S_ISREG(inode->i_mode)) 38621da177e4SLinus Torvalds mpol_free_shared_policy(&SHMEM_I(inode)->policy); 38631da177e4SLinus Torvalds } 38641da177e4SLinus Torvalds 386541ffe5d5SHugh Dickins static void shmem_init_inode(void *foo) 38661da177e4SLinus Torvalds { 386741ffe5d5SHugh Dickins struct shmem_inode_info *info = foo; 386841ffe5d5SHugh Dickins inode_init_once(&info->vfs_inode); 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds 38719a8ec03eSweiping zhang static void shmem_init_inodecache(void) 38721da177e4SLinus Torvalds { 38731da177e4SLinus Torvalds shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 38741da177e4SLinus Torvalds sizeof(struct shmem_inode_info), 38755d097056SVladimir Davydov 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 38761da177e4SLinus Torvalds } 38771da177e4SLinus Torvalds 387841ffe5d5SHugh Dickins static void shmem_destroy_inodecache(void) 38791da177e4SLinus Torvalds { 38801a1d92c1SAlexey Dobriyan kmem_cache_destroy(shmem_inode_cachep); 38811da177e4SLinus Torvalds } 38821da177e4SLinus Torvalds 3883a7605426SYang Shi /* Keep the page in page cache instead of truncating it */ 3884a7605426SYang Shi static int shmem_error_remove_page(struct address_space *mapping, 3885a7605426SYang Shi struct page *page) 3886a7605426SYang Shi { 3887a7605426SYang Shi return 0; 3888a7605426SYang Shi } 3889a7605426SYang Shi 389030e6a51dSHui Su const struct address_space_operations shmem_aops = { 38911da177e4SLinus Torvalds .writepage = shmem_writepage, 389246de8b97SMatthew Wilcox (Oracle) .dirty_folio = noop_dirty_folio, 38931da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3894800d15a5SNick Piggin .write_begin = shmem_write_begin, 3895800d15a5SNick Piggin .write_end = shmem_write_end, 38961da177e4SLinus Torvalds #endif 38971c93923cSAndrew Morton #ifdef CONFIG_MIGRATION 389854184650SMatthew Wilcox (Oracle) .migrate_folio = migrate_folio, 38991c93923cSAndrew Morton #endif 3900a7605426SYang Shi .error_remove_page = shmem_error_remove_page, 39011da177e4SLinus Torvalds }; 390230e6a51dSHui Su EXPORT_SYMBOL(shmem_aops); 39031da177e4SLinus Torvalds 390415ad7cdcSHelge Deller static const struct file_operations shmem_file_operations = { 39051da177e4SLinus Torvalds .mmap = shmem_mmap, 3906c01d5b30SHugh Dickins .get_unmapped_area = shmem_get_unmapped_area, 39071da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3908220f2ac9SHugh Dickins .llseek = shmem_file_llseek, 39092ba5bbedSAl Viro .read_iter = shmem_file_read_iter, 39108174202bSAl Viro .write_iter = generic_file_write_iter, 39111b061d92SChristoph Hellwig .fsync = noop_fsync, 391282c156f8SAl Viro .splice_read = generic_file_splice_read, 3913f6cb85d0SAl Viro .splice_write = iter_file_splice_write, 391483e4fa9cSHugh Dickins .fallocate = shmem_fallocate, 39151da177e4SLinus Torvalds #endif 39161da177e4SLinus Torvalds }; 39171da177e4SLinus Torvalds 391892e1d5beSArjan van de Ven static const struct inode_operations shmem_inode_operations = { 391944a30220SYu Zhao .getattr = shmem_getattr, 392094c1e62dSHugh Dickins .setattr = shmem_setattr, 3921b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3922b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3923feda821eSChristoph Hellwig .set_acl = simple_set_acl, 3924e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3925e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3926b09e0fa4SEric Paris #endif 39271da177e4SLinus Torvalds }; 39281da177e4SLinus Torvalds 392992e1d5beSArjan van de Ven static const struct inode_operations shmem_dir_inode_operations = { 39301da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 3931f7cd16a5SXavier Roche .getattr = shmem_getattr, 39321da177e4SLinus Torvalds .create = shmem_create, 39331da177e4SLinus Torvalds .lookup = simple_lookup, 39341da177e4SLinus Torvalds .link = shmem_link, 39351da177e4SLinus Torvalds .unlink = shmem_unlink, 39361da177e4SLinus Torvalds .symlink = shmem_symlink, 39371da177e4SLinus Torvalds .mkdir = shmem_mkdir, 39381da177e4SLinus Torvalds .rmdir = shmem_rmdir, 39391da177e4SLinus Torvalds .mknod = shmem_mknod, 39402773bf00SMiklos Szeredi .rename = shmem_rename2, 394160545d0dSAl Viro .tmpfile = shmem_tmpfile, 39421da177e4SLinus Torvalds #endif 3943b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3944b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3945e408e695STheodore Ts'o .fileattr_get = shmem_fileattr_get, 3946e408e695STheodore Ts'o .fileattr_set = shmem_fileattr_set, 3947b09e0fa4SEric Paris #endif 394839f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 394994c1e62dSHugh Dickins .setattr = shmem_setattr, 3950feda821eSChristoph Hellwig .set_acl = simple_set_acl, 395139f0247dSAndreas Gruenbacher #endif 395239f0247dSAndreas Gruenbacher }; 395339f0247dSAndreas Gruenbacher 395492e1d5beSArjan van de Ven static const struct inode_operations shmem_special_inode_operations = { 3955f7cd16a5SXavier Roche .getattr = shmem_getattr, 3956b09e0fa4SEric Paris #ifdef CONFIG_TMPFS_XATTR 3957b09e0fa4SEric Paris .listxattr = shmem_listxattr, 3958b09e0fa4SEric Paris #endif 395939f0247dSAndreas Gruenbacher #ifdef CONFIG_TMPFS_POSIX_ACL 396094c1e62dSHugh Dickins .setattr = shmem_setattr, 3961feda821eSChristoph Hellwig .set_acl = simple_set_acl, 396239f0247dSAndreas Gruenbacher #endif 39631da177e4SLinus Torvalds }; 39641da177e4SLinus Torvalds 3965759b9775SHugh Dickins static const struct super_operations shmem_ops = { 39661da177e4SLinus Torvalds .alloc_inode = shmem_alloc_inode, 396774b1da56SAl Viro .free_inode = shmem_free_in_core_inode, 39681da177e4SLinus Torvalds .destroy_inode = shmem_destroy_inode, 39691da177e4SLinus Torvalds #ifdef CONFIG_TMPFS 39701da177e4SLinus Torvalds .statfs = shmem_statfs, 3971680d794bSakpm@linux-foundation.org .show_options = shmem_show_options, 39721da177e4SLinus Torvalds #endif 39731f895f75SAl Viro .evict_inode = shmem_evict_inode, 39741da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 39751da177e4SLinus Torvalds .put_super = shmem_put_super, 3976396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 3977779750d2SKirill A. Shutemov .nr_cached_objects = shmem_unused_huge_count, 3978779750d2SKirill A. Shutemov .free_cached_objects = shmem_unused_huge_scan, 3979779750d2SKirill A. Shutemov #endif 39801da177e4SLinus Torvalds }; 39811da177e4SLinus Torvalds 3982f0f37e2fSAlexey Dobriyan static const struct vm_operations_struct shmem_vm_ops = { 398354cb8821SNick Piggin .fault = shmem_fault, 3984d7c17551SNing Qu .map_pages = filemap_map_pages, 39851da177e4SLinus Torvalds #ifdef CONFIG_NUMA 39861da177e4SLinus Torvalds .set_policy = shmem_set_policy, 39871da177e4SLinus Torvalds .get_policy = shmem_get_policy, 39881da177e4SLinus Torvalds #endif 39891da177e4SLinus Torvalds }; 39901da177e4SLinus Torvalds 3991f3235626SDavid Howells int shmem_init_fs_context(struct fs_context *fc) 39921da177e4SLinus Torvalds { 3993f3235626SDavid Howells struct shmem_options *ctx; 3994f3235626SDavid Howells 3995f3235626SDavid Howells ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 3996f3235626SDavid Howells if (!ctx) 3997f3235626SDavid Howells return -ENOMEM; 3998f3235626SDavid Howells 3999f3235626SDavid Howells ctx->mode = 0777 | S_ISVTX; 4000f3235626SDavid Howells ctx->uid = current_fsuid(); 4001f3235626SDavid Howells ctx->gid = current_fsgid(); 4002f3235626SDavid Howells 4003f3235626SDavid Howells fc->fs_private = ctx; 4004f3235626SDavid Howells fc->ops = &shmem_fs_context_ops; 4005f3235626SDavid Howells return 0; 40061da177e4SLinus Torvalds } 40071da177e4SLinus Torvalds 400841ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 40091da177e4SLinus Torvalds .owner = THIS_MODULE, 40101da177e4SLinus Torvalds .name = "tmpfs", 4011f3235626SDavid Howells .init_fs_context = shmem_init_fs_context, 4012f3235626SDavid Howells #ifdef CONFIG_TMPFS 4013d7167b14SAl Viro .parameters = shmem_fs_parameters, 4014f3235626SDavid Howells #endif 40151da177e4SLinus Torvalds .kill_sb = kill_litter_super, 4016ff36da69SMatthew Wilcox (Oracle) .fs_flags = FS_USERNS_MOUNT, 40171da177e4SLinus Torvalds }; 40181da177e4SLinus Torvalds 40199096bbe9SMiaohe Lin void __init shmem_init(void) 40201da177e4SLinus Torvalds { 40211da177e4SLinus Torvalds int error; 40221da177e4SLinus Torvalds 40239a8ec03eSweiping zhang shmem_init_inodecache(); 40241da177e4SLinus Torvalds 402541ffe5d5SHugh Dickins error = register_filesystem(&shmem_fs_type); 40261da177e4SLinus Torvalds if (error) { 40271170532bSJoe Perches pr_err("Could not register tmpfs\n"); 40281da177e4SLinus Torvalds goto out2; 40291da177e4SLinus Torvalds } 403095dc112aSGreg Kroah-Hartman 4031ca4e0519SAl Viro shm_mnt = kern_mount(&shmem_fs_type); 40321da177e4SLinus Torvalds if (IS_ERR(shm_mnt)) { 40331da177e4SLinus Torvalds error = PTR_ERR(shm_mnt); 40341170532bSJoe Perches pr_err("Could not kern_mount tmpfs\n"); 40351da177e4SLinus Torvalds goto out1; 40361da177e4SLinus Torvalds } 40375a6e75f8SKirill A. Shutemov 4038396bcc52SMatthew Wilcox (Oracle) #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4039435c0b87SKirill A. Shutemov if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 40405a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 40415a6e75f8SKirill A. Shutemov else 40425e6e5a12SHugh Dickins shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ 40435a6e75f8SKirill A. Shutemov #endif 40449096bbe9SMiaohe Lin return; 40451da177e4SLinus Torvalds 40461da177e4SLinus Torvalds out1: 404741ffe5d5SHugh Dickins unregister_filesystem(&shmem_fs_type); 40481da177e4SLinus Torvalds out2: 404941ffe5d5SHugh Dickins shmem_destroy_inodecache(); 40501da177e4SLinus Torvalds shm_mnt = ERR_PTR(error); 40511da177e4SLinus Torvalds } 4052853ac43aSMatt Mackall 4053396bcc52SMatthew Wilcox (Oracle) #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 40545a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_show(struct kobject *kobj, 40555a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, char *buf) 40565a6e75f8SKirill A. Shutemov { 405726083eb6SColin Ian King static const int values[] = { 40585a6e75f8SKirill A. Shutemov SHMEM_HUGE_ALWAYS, 40595a6e75f8SKirill A. Shutemov SHMEM_HUGE_WITHIN_SIZE, 40605a6e75f8SKirill A. Shutemov SHMEM_HUGE_ADVISE, 40615a6e75f8SKirill A. Shutemov SHMEM_HUGE_NEVER, 40625a6e75f8SKirill A. Shutemov SHMEM_HUGE_DENY, 40635a6e75f8SKirill A. Shutemov SHMEM_HUGE_FORCE, 40645a6e75f8SKirill A. Shutemov }; 406579d4d38aSJoe Perches int len = 0; 406679d4d38aSJoe Perches int i; 40675a6e75f8SKirill A. Shutemov 406879d4d38aSJoe Perches for (i = 0; i < ARRAY_SIZE(values); i++) { 406979d4d38aSJoe Perches len += sysfs_emit_at(buf, len, 407079d4d38aSJoe Perches shmem_huge == values[i] ? "%s[%s]" : "%s%s", 407179d4d38aSJoe Perches i ? " " : "", 40725a6e75f8SKirill A. Shutemov shmem_format_huge(values[i])); 40735a6e75f8SKirill A. Shutemov } 407479d4d38aSJoe Perches 407579d4d38aSJoe Perches len += sysfs_emit_at(buf, len, "\n"); 407679d4d38aSJoe Perches 407779d4d38aSJoe Perches return len; 40785a6e75f8SKirill A. Shutemov } 40795a6e75f8SKirill A. Shutemov 40805a6e75f8SKirill A. Shutemov static ssize_t shmem_enabled_store(struct kobject *kobj, 40815a6e75f8SKirill A. Shutemov struct kobj_attribute *attr, const char *buf, size_t count) 40825a6e75f8SKirill A. Shutemov { 40835a6e75f8SKirill A. Shutemov char tmp[16]; 40845a6e75f8SKirill A. Shutemov int huge; 40855a6e75f8SKirill A. Shutemov 40865a6e75f8SKirill A. Shutemov if (count + 1 > sizeof(tmp)) 40875a6e75f8SKirill A. Shutemov return -EINVAL; 40885a6e75f8SKirill A. Shutemov memcpy(tmp, buf, count); 40895a6e75f8SKirill A. Shutemov tmp[count] = '\0'; 40905a6e75f8SKirill A. Shutemov if (count && tmp[count - 1] == '\n') 40915a6e75f8SKirill A. Shutemov tmp[count - 1] = '\0'; 40925a6e75f8SKirill A. Shutemov 40935a6e75f8SKirill A. Shutemov huge = shmem_parse_huge(tmp); 40945a6e75f8SKirill A. Shutemov if (huge == -EINVAL) 40955a6e75f8SKirill A. Shutemov return -EINVAL; 40965a6e75f8SKirill A. Shutemov if (!has_transparent_hugepage() && 40975a6e75f8SKirill A. Shutemov huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 40985a6e75f8SKirill A. Shutemov return -EINVAL; 40995a6e75f8SKirill A. Shutemov 41005a6e75f8SKirill A. Shutemov shmem_huge = huge; 4101435c0b87SKirill A. Shutemov if (shmem_huge > SHMEM_HUGE_DENY) 41025a6e75f8SKirill A. Shutemov SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 41035a6e75f8SKirill A. Shutemov return count; 41045a6e75f8SKirill A. Shutemov } 41055a6e75f8SKirill A. Shutemov 41064bfa8adaSMiaohe Lin struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); 4107396bcc52SMatthew Wilcox (Oracle) #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 4108f3f0e1d2SKirill A. Shutemov 4109853ac43aSMatt Mackall #else /* !CONFIG_SHMEM */ 4110853ac43aSMatt Mackall 4111853ac43aSMatt Mackall /* 4112853ac43aSMatt Mackall * tiny-shmem: simple shmemfs and tmpfs using ramfs code 4113853ac43aSMatt Mackall * 4114853ac43aSMatt Mackall * This is intended for small system where the benefits of the full 4115853ac43aSMatt Mackall * shmem code (swap-backed and resource-limited) are outweighed by 4116853ac43aSMatt Mackall * their complexity. On systems without swap this code should be 4117853ac43aSMatt Mackall * effectively equivalent, but much lighter weight. 4118853ac43aSMatt Mackall */ 4119853ac43aSMatt Mackall 412041ffe5d5SHugh Dickins static struct file_system_type shmem_fs_type = { 4121853ac43aSMatt Mackall .name = "tmpfs", 4122f3235626SDavid Howells .init_fs_context = ramfs_init_fs_context, 4123d7167b14SAl Viro .parameters = ramfs_fs_parameters, 4124853ac43aSMatt Mackall .kill_sb = kill_litter_super, 41252b8576cbSEric W. Biederman .fs_flags = FS_USERNS_MOUNT, 4126853ac43aSMatt Mackall }; 4127853ac43aSMatt Mackall 41289096bbe9SMiaohe Lin void __init shmem_init(void) 4129853ac43aSMatt Mackall { 413041ffe5d5SHugh Dickins BUG_ON(register_filesystem(&shmem_fs_type) != 0); 4131853ac43aSMatt Mackall 413241ffe5d5SHugh Dickins shm_mnt = kern_mount(&shmem_fs_type); 4133853ac43aSMatt Mackall BUG_ON(IS_ERR(shm_mnt)); 4134853ac43aSMatt Mackall } 4135853ac43aSMatt Mackall 413610a9c496SChristoph Hellwig int shmem_unuse(unsigned int type) 4137853ac43aSMatt Mackall { 4138853ac43aSMatt Mackall return 0; 4139853ac43aSMatt Mackall } 4140853ac43aSMatt Mackall 4141d7c9e99aSAlexey Gladkov int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 41423f96b79aSHugh Dickins { 41433f96b79aSHugh Dickins return 0; 41443f96b79aSHugh Dickins } 41453f96b79aSHugh Dickins 414624513264SHugh Dickins void shmem_unlock_mapping(struct address_space *mapping) 414724513264SHugh Dickins { 414824513264SHugh Dickins } 414924513264SHugh Dickins 4150c01d5b30SHugh Dickins #ifdef CONFIG_MMU 4151c01d5b30SHugh Dickins unsigned long shmem_get_unmapped_area(struct file *file, 4152c01d5b30SHugh Dickins unsigned long addr, unsigned long len, 4153c01d5b30SHugh Dickins unsigned long pgoff, unsigned long flags) 4154c01d5b30SHugh Dickins { 4155c01d5b30SHugh Dickins return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); 4156c01d5b30SHugh Dickins } 4157c01d5b30SHugh Dickins #endif 4158c01d5b30SHugh Dickins 415941ffe5d5SHugh Dickins void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 416094c1e62dSHugh Dickins { 416141ffe5d5SHugh Dickins truncate_inode_pages_range(inode->i_mapping, lstart, lend); 416294c1e62dSHugh Dickins } 416394c1e62dSHugh Dickins EXPORT_SYMBOL_GPL(shmem_truncate_range); 416494c1e62dSHugh Dickins 4165853ac43aSMatt Mackall #define shmem_vm_ops generic_file_vm_ops 41660b0a0806SHugh Dickins #define shmem_file_operations ramfs_file_operations 4167454abafeSDmitry Monakhov #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) 41680b0a0806SHugh Dickins #define shmem_acct_size(flags, size) 0 41690b0a0806SHugh Dickins #define shmem_unacct_size(flags, size) do {} while (0) 4170853ac43aSMatt Mackall 4171853ac43aSMatt Mackall #endif /* CONFIG_SHMEM */ 4172853ac43aSMatt Mackall 4173853ac43aSMatt Mackall /* common code */ 41741da177e4SLinus Torvalds 4175703321b6SMatthew Auld static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, 4176c7277090SEric Paris unsigned long flags, unsigned int i_flags) 41771da177e4SLinus Torvalds { 41781da177e4SLinus Torvalds struct inode *inode; 417993dec2daSAl Viro struct file *res; 41801da177e4SLinus Torvalds 4181703321b6SMatthew Auld if (IS_ERR(mnt)) 4182703321b6SMatthew Auld return ERR_CAST(mnt); 41831da177e4SLinus Torvalds 4184285b2c4fSHugh Dickins if (size < 0 || size > MAX_LFS_FILESIZE) 41851da177e4SLinus Torvalds return ERR_PTR(-EINVAL); 41861da177e4SLinus Torvalds 41871da177e4SLinus Torvalds if (shmem_acct_size(flags, size)) 41881da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 41891da177e4SLinus Torvalds 419093dec2daSAl Viro inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, 419193dec2daSAl Viro flags); 4192dac2d1f6SAl Viro if (unlikely(!inode)) { 4193dac2d1f6SAl Viro shmem_unacct_size(flags, size); 4194dac2d1f6SAl Viro return ERR_PTR(-ENOSPC); 4195dac2d1f6SAl Viro } 4196c7277090SEric Paris inode->i_flags |= i_flags; 41971da177e4SLinus Torvalds inode->i_size = size; 41986d6b77f1SMiklos Szeredi clear_nlink(inode); /* It is unlinked */ 419926567cdbSAl Viro res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 420093dec2daSAl Viro if (!IS_ERR(res)) 420193dec2daSAl Viro res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 42024b42af81SAl Viro &shmem_file_operations); 42036b4d0b27SAl Viro if (IS_ERR(res)) 420493dec2daSAl Viro iput(inode); 42056b4d0b27SAl Viro return res; 42061da177e4SLinus Torvalds } 4207c7277090SEric Paris 4208c7277090SEric Paris /** 4209c7277090SEric Paris * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 4210c7277090SEric Paris * kernel internal. There will be NO LSM permission checks against the 4211c7277090SEric Paris * underlying inode. So users of this interface must do LSM checks at a 4212e1832f29SStephen Smalley * higher layer. The users are the big_key and shm implementations. LSM 4213e1832f29SStephen Smalley * checks are provided at the key or shm level rather than the inode. 4214c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4215c7277090SEric Paris * @size: size to be set for the file 4216c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4217c7277090SEric Paris */ 4218c7277090SEric Paris struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 4219c7277090SEric Paris { 4220703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 4221c7277090SEric Paris } 4222c7277090SEric Paris 4223c7277090SEric Paris /** 4224c7277090SEric Paris * shmem_file_setup - get an unlinked file living in tmpfs 4225c7277090SEric Paris * @name: name for dentry (to be seen in /proc/<pid>/maps 4226c7277090SEric Paris * @size: size to be set for the file 4227c7277090SEric Paris * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4228c7277090SEric Paris */ 4229c7277090SEric Paris struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 4230c7277090SEric Paris { 4231703321b6SMatthew Auld return __shmem_file_setup(shm_mnt, name, size, flags, 0); 4232c7277090SEric Paris } 4233395e0ddcSKeith Packard EXPORT_SYMBOL_GPL(shmem_file_setup); 42341da177e4SLinus Torvalds 423546711810SRandy Dunlap /** 4236703321b6SMatthew Auld * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 4237703321b6SMatthew Auld * @mnt: the tmpfs mount where the file will be created 4238703321b6SMatthew Auld * @name: name for dentry (to be seen in /proc/<pid>/maps 4239703321b6SMatthew Auld * @size: size to be set for the file 4240703321b6SMatthew Auld * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 4241703321b6SMatthew Auld */ 4242703321b6SMatthew Auld struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 4243703321b6SMatthew Auld loff_t size, unsigned long flags) 4244703321b6SMatthew Auld { 4245703321b6SMatthew Auld return __shmem_file_setup(mnt, name, size, flags, 0); 4246703321b6SMatthew Auld } 4247703321b6SMatthew Auld EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 4248703321b6SMatthew Auld 4249703321b6SMatthew Auld /** 42501da177e4SLinus Torvalds * shmem_zero_setup - setup a shared anonymous mapping 425145e55300SPeter Collingbourne * @vma: the vma to be mmapped is prepared by do_mmap 42521da177e4SLinus Torvalds */ 42531da177e4SLinus Torvalds int shmem_zero_setup(struct vm_area_struct *vma) 42541da177e4SLinus Torvalds { 42551da177e4SLinus Torvalds struct file *file; 42561da177e4SLinus Torvalds loff_t size = vma->vm_end - vma->vm_start; 42571da177e4SLinus Torvalds 425866fc1303SHugh Dickins /* 4259c1e8d7c6SMichel Lespinasse * Cloning a new file under mmap_lock leads to a lock ordering conflict 426066fc1303SHugh Dickins * between XFS directory reading and selinux: since this file is only 426166fc1303SHugh Dickins * accessible to the user through its mapping, use S_PRIVATE flag to 426266fc1303SHugh Dickins * bypass file security, in the same way as shmem_kernel_file_setup(). 426366fc1303SHugh Dickins */ 4264703321b6SMatthew Auld file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 42651da177e4SLinus Torvalds if (IS_ERR(file)) 42661da177e4SLinus Torvalds return PTR_ERR(file); 42671da177e4SLinus Torvalds 42681da177e4SLinus Torvalds if (vma->vm_file) 42691da177e4SLinus Torvalds fput(vma->vm_file); 42701da177e4SLinus Torvalds vma->vm_file = file; 42711da177e4SLinus Torvalds vma->vm_ops = &shmem_vm_ops; 4272f3f0e1d2SKirill A. Shutemov 42731da177e4SLinus Torvalds return 0; 42741da177e4SLinus Torvalds } 4275d9d90e5eSHugh Dickins 4276d9d90e5eSHugh Dickins /** 4277d9d90e5eSHugh Dickins * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. 4278d9d90e5eSHugh Dickins * @mapping: the page's address_space 4279d9d90e5eSHugh Dickins * @index: the page index 4280d9d90e5eSHugh Dickins * @gfp: the page allocator flags to use if allocating 4281d9d90e5eSHugh Dickins * 4282d9d90e5eSHugh Dickins * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 4283d9d90e5eSHugh Dickins * with any new page allocations done using the specified allocation flags. 42847e0a1265SMatthew Wilcox (Oracle) * But read_cache_page_gfp() uses the ->read_folio() method: which does not 4285d9d90e5eSHugh Dickins * suit tmpfs, since it may have pages in swapcache, and needs to find those 4286d9d90e5eSHugh Dickins * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 4287d9d90e5eSHugh Dickins * 428868da9f05SHugh Dickins * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 428968da9f05SHugh Dickins * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 4290d9d90e5eSHugh Dickins */ 4291d9d90e5eSHugh Dickins struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 4292d9d90e5eSHugh Dickins pgoff_t index, gfp_t gfp) 4293d9d90e5eSHugh Dickins { 429468da9f05SHugh Dickins #ifdef CONFIG_SHMEM 429568da9f05SHugh Dickins struct inode *inode = mapping->host; 4296a3a9c397SMatthew Wilcox (Oracle) struct folio *folio; 42979276aad6SHugh Dickins struct page *page; 429868da9f05SHugh Dickins int error; 429968da9f05SHugh Dickins 430030e6a51dSHui Su BUG_ON(!shmem_mapping(mapping)); 4301a3a9c397SMatthew Wilcox (Oracle) error = shmem_get_folio_gfp(inode, index, &folio, SGP_CACHE, 4302cfda0526SMike Rapoport gfp, NULL, NULL, NULL); 430368da9f05SHugh Dickins if (error) 4304a7605426SYang Shi return ERR_PTR(error); 4305a7605426SYang Shi 4306a3a9c397SMatthew Wilcox (Oracle) folio_unlock(folio); 4307a3a9c397SMatthew Wilcox (Oracle) page = folio_file_page(folio, index); 4308a7605426SYang Shi if (PageHWPoison(page)) { 4309a3a9c397SMatthew Wilcox (Oracle) folio_put(folio); 4310a7605426SYang Shi return ERR_PTR(-EIO); 4311a7605426SYang Shi } 4312a7605426SYang Shi 431368da9f05SHugh Dickins return page; 431468da9f05SHugh Dickins #else 431568da9f05SHugh Dickins /* 431668da9f05SHugh Dickins * The tiny !SHMEM case uses ramfs without swap 431768da9f05SHugh Dickins */ 4318d9d90e5eSHugh Dickins return read_cache_page_gfp(mapping, index, gfp); 431968da9f05SHugh Dickins #endif 4320d9d90e5eSHugh Dickins } 4321d9d90e5eSHugh Dickins EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 4322