Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Resizable virtual memory filesystem for Linux. |
| 3 | * |
| 4 | * Copyright (C) 2000 Linus Torvalds. |
| 5 | * 2000 Transmeta Corp. |
| 6 | * 2000-2001 Christoph Rohland |
| 7 | * 2000-2001 SAP AG |
| 8 | * 2002 Red Hat Inc. |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 9 | * Copyright (C) 2002-2011 Hugh Dickins. |
| 10 | * Copyright (C) 2011 Google Inc. |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 11 | * Copyright (C) 2002-2005 VERITAS Software Corporation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Copyright (C) 2004 Andi Kleen, SuSE Labs |
| 13 | * |
| 14 | * Extended attribute support for tmpfs: |
| 15 | * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> |
| 16 | * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> |
| 17 | * |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 18 | * tiny-shmem: |
| 19 | * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> |
| 20 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * This file is released under the GPL. |
| 22 | */ |
| 23 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 24 | #include <linux/fs.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/vfs.h> |
| 27 | #include <linux/mount.h> |
Andrew Morton | 250297e | 2013-04-29 15:06:12 -0700 | [diff] [blame] | 28 | #include <linux/ramfs.h> |
Hugh Dickins | caefba1 | 2009-04-13 14:40:12 -0700 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 30 | #include <linux/file.h> |
| 31 | #include <linux/mm.h> |
Arnd Bergmann | 46c9a94 | 2018-08-17 15:45:09 -0700 | [diff] [blame] | 32 | #include <linux/random.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 33 | #include <linux/sched/signal.h> |
Paul Gortmaker | b95f1b31 | 2011-10-16 02:01:52 -0400 | [diff] [blame] | 34 | #include <linux/export.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 35 | #include <linux/swap.h> |
Christoph Hellwig | e2e40f2 | 2015-02-22 08:58:50 -0800 | [diff] [blame] | 36 | #include <linux/uio.h> |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 37 | #include <linux/khugepaged.h> |
Mike Kravetz | 749df87 | 2017-09-06 16:24:16 -0700 | [diff] [blame] | 38 | #include <linux/hugetlb.h> |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 39 | #include <linux/frontswap.h> |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 40 | #include <linux/fs_parser.h> |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 41 | #include <linux/mm_inline.h> |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 42 | |
Andrea Arcangeli | 95cc09d | 2017-02-22 15:43:31 -0800 | [diff] [blame] | 43 | #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */ |
| 44 | |
Charan Teja Reddy | 25c7eb4 | 2021-07-09 21:59:12 +0530 | [diff] [blame] | 45 | #include "internal.h" |
| 46 | |
Charan Teja Reddy | 964220d | 2021-07-06 19:04:50 +0530 | [diff] [blame] | 47 | #undef CREATE_TRACE_POINTS |
| 48 | #include <trace/hooks/shmem_fs.h> |
| 49 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 50 | static struct vfsmount *shm_mnt; |
| 51 | |
| 52 | #ifdef CONFIG_SHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | /* |
| 54 | * This virtual memory filesystem is heavily based on the ramfs. It |
| 55 | * extends ramfs by the ability to use swap and honor resource limits |
| 56 | * which makes it a completely usable filesystem. |
| 57 | */ |
| 58 | |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 59 | #include <linux/xattr.h> |
Christoph Hellwig | a569425 | 2007-07-17 04:04:28 -0700 | [diff] [blame] | 60 | #include <linux/exportfs.h> |
Christoph Hellwig | 1c7c474 | 2009-11-03 16:44:44 +0100 | [diff] [blame] | 61 | #include <linux/posix_acl.h> |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 62 | #include <linux/posix_acl_xattr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <linux/string.h> |
| 65 | #include <linux/slab.h> |
| 66 | #include <linux/backing-dev.h> |
| 67 | #include <linux/shmem_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #include <linux/writeback.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | #include <linux/blkdev.h> |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 70 | #include <linux/pagevec.h> |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 71 | #include <linux/percpu_counter.h> |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 72 | #include <linux/falloc.h> |
Hugh Dickins | 708e350 | 2011-07-25 17:12:32 -0700 | [diff] [blame] | 73 | #include <linux/splice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #include <linux/security.h> |
| 75 | #include <linux/swapops.h> |
| 76 | #include <linux/mempolicy.h> |
| 77 | #include <linux/namei.h> |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 78 | #include <linux/ctype.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 79 | #include <linux/migrate.h> |
Christoph Lameter | c1f60a5 | 2006-09-25 23:31:11 -0700 | [diff] [blame] | 80 | #include <linux/highmem.h> |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 81 | #include <linux/seq_file.h> |
Mimi Zohar | 9256292 | 2008-10-07 14:00:12 -0400 | [diff] [blame] | 82 | #include <linux/magic.h> |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 83 | #include <linux/syscalls.h> |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 84 | #include <linux/fcntl.h> |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 85 | #include <uapi/linux/memfd.h> |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 86 | #include <linux/userfaultfd_k.h> |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 87 | #include <linux/rmap.h> |
Amir Goldstein | 2b4db79 | 2017-05-18 15:29:33 +0300 | [diff] [blame] | 88 | #include <linux/uuid.h> |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 89 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 90 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Mel Gorman | dd56b04 | 2015-11-06 16:28:43 -0800 | [diff] [blame] | 92 | #include "internal.h" |
| 93 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 94 | #define BLOCKS_PER_PAGE (PAGE_SIZE/512) |
| 95 | #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* Pretend that each entry is of this size in directory's i_size */ |
| 98 | #define BOGO_DIRENT_SIZE 20 |
| 99 | |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 100 | /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ |
| 101 | #define SHORT_SYMLINK_LEN 128 |
| 102 | |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 103 | /* |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 104 | * shmem_fallocate communicates with shmem_fault or shmem_writepage via |
| 105 | * inode->i_private (with i_mutex making sure that it has only one user at |
| 106 | * a time): we would prefer not to enlarge the shmem inode just for that. |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 107 | */ |
| 108 | struct shmem_falloc { |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 109 | wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 110 | pgoff_t start; /* start of range currently being fallocated */ |
| 111 | pgoff_t next; /* the next page offset to be fallocated */ |
| 112 | pgoff_t nr_falloced; /* how many new pages have been fallocated */ |
| 113 | pgoff_t nr_unswapped; /* how often writepage refused to swap out */ |
| 114 | }; |
| 115 | |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 116 | struct shmem_options { |
| 117 | unsigned long long blocks; |
| 118 | unsigned long long inodes; |
| 119 | struct mempolicy *mpol; |
| 120 | kuid_t uid; |
| 121 | kgid_t gid; |
| 122 | umode_t mode; |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 123 | bool full_inums; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 124 | int huge; |
| 125 | int seen; |
| 126 | #define SHMEM_SEEN_BLOCKS 1 |
| 127 | #define SHMEM_SEEN_INODES 2 |
| 128 | #define SHMEM_SEEN_HUGE 4 |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 129 | #define SHMEM_SEEN_INUMS 8 |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 130 | }; |
| 131 | |
Andrew Morton | b76db73 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 132 | #ifdef CONFIG_TMPFS |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 133 | static unsigned long shmem_default_max_blocks(void) |
| 134 | { |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 135 | return totalram_pages() / 2; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static unsigned long shmem_default_max_inodes(void) |
| 139 | { |
Arun KS | ca79b0c | 2018-12-28 00:34:29 -0800 | [diff] [blame] | 140 | unsigned long nr_pages = totalram_pages(); |
| 141 | |
| 142 | return min(nr_pages - totalhigh_pages(), nr_pages / 2); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 143 | } |
Andrew Morton | b76db73 | 2008-02-08 04:21:49 -0800 | [diff] [blame] | 144 | #endif |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 145 | |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 146 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp); |
| 147 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, |
| 148 | struct shmem_inode_info *info, pgoff_t index); |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 149 | static int shmem_swapin_page(struct inode *inode, pgoff_t index, |
| 150 | struct page **pagep, enum sgp_type sgp, |
| 151 | gfp_t gfp, struct vm_area_struct *vma, |
| 152 | vm_fault_t *fault_type); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 153 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 154 | struct page **pagep, enum sgp_type sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 155 | gfp_t gfp, struct vm_area_struct *vma, |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 156 | struct vm_fault *vmf, vm_fault_t *fault_type); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 157 | |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 158 | int shmem_getpage(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 159 | struct page **pagep, enum sgp_type sgp) |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 160 | { |
| 161 | return shmem_getpage_gfp(inode, index, pagep, sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 162 | mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) |
| 166 | { |
| 167 | return sb->s_fs_info; |
| 168 | } |
| 169 | |
| 170 | /* |
| 171 | * shmem_file_setup pre-accounts the whole fixed size of a VM object, |
| 172 | * for shared memory and for shared anonymous (/dev/zero) mappings |
| 173 | * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), |
| 174 | * consistent with the pre-accounting of private mappings ... |
| 175 | */ |
| 176 | static inline int shmem_acct_size(unsigned long flags, loff_t size) |
| 177 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 178 | return (flags & VM_NORESERVE) ? |
Al Viro | 191c542 | 2012-02-13 03:58:52 +0000 | [diff] [blame] | 179 | 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static inline void shmem_unacct_size(unsigned long flags, loff_t size) |
| 183 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 184 | if (!(flags & VM_NORESERVE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | vm_unacct_memory(VM_ACCT(size)); |
| 186 | } |
| 187 | |
Konstantin Khlebnikov | 7714251 | 2014-08-06 16:06:34 -0700 | [diff] [blame] | 188 | static inline int shmem_reacct_size(unsigned long flags, |
| 189 | loff_t oldsize, loff_t newsize) |
| 190 | { |
| 191 | if (!(flags & VM_NORESERVE)) { |
| 192 | if (VM_ACCT(newsize) > VM_ACCT(oldsize)) |
| 193 | return security_vm_enough_memory_mm(current->mm, |
| 194 | VM_ACCT(newsize) - VM_ACCT(oldsize)); |
| 195 | else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) |
| 196 | vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* |
| 202 | * ... whereas tmpfs objects are accounted incrementally as |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 203 | * pages are allocated, in order to allow large sparse files. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM, |
| 205 | * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. |
| 206 | */ |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 207 | static inline int shmem_acct_block(unsigned long flags, long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 209 | if (!(flags & VM_NORESERVE)) |
| 210 | return 0; |
| 211 | |
| 212 | return security_vm_enough_memory_mm(current->mm, |
| 213 | pages * VM_ACCT(PAGE_SIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | static inline void shmem_unacct_blocks(unsigned long flags, long pages) |
| 217 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 218 | if (flags & VM_NORESERVE) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 219 | vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 222 | static inline bool shmem_inode_acct_block(struct inode *inode, long pages) |
| 223 | { |
| 224 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 225 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 226 | |
| 227 | if (shmem_acct_block(info->flags, pages)) |
| 228 | return false; |
| 229 | |
| 230 | if (sbinfo->max_blocks) { |
| 231 | if (percpu_counter_compare(&sbinfo->used_blocks, |
| 232 | sbinfo->max_blocks - pages) > 0) |
| 233 | goto unacct; |
| 234 | percpu_counter_add(&sbinfo->used_blocks, pages); |
| 235 | } |
| 236 | |
| 237 | return true; |
| 238 | |
| 239 | unacct: |
| 240 | shmem_unacct_blocks(info->flags, pages); |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages) |
| 245 | { |
| 246 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 247 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 248 | |
| 249 | if (sbinfo->max_blocks) |
| 250 | percpu_counter_sub(&sbinfo->used_blocks, pages); |
| 251 | shmem_unacct_blocks(info->flags, pages); |
| 252 | } |
| 253 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 254 | static const struct super_operations shmem_ops; |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 255 | static const struct address_space_operations shmem_aops; |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 256 | static const struct file_operations shmem_file_operations; |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 257 | static const struct inode_operations shmem_inode_operations; |
| 258 | static const struct inode_operations shmem_dir_inode_operations; |
| 259 | static const struct inode_operations shmem_special_inode_operations; |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 260 | static const struct vm_operations_struct shmem_vm_ops; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 261 | static struct file_system_type shmem_fs_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Mike Rapoport | b0506e4 | 2017-02-22 15:43:28 -0800 | [diff] [blame] | 263 | bool vma_is_shmem(struct vm_area_struct *vma) |
| 264 | { |
| 265 | return vma->vm_ops == &shmem_vm_ops; |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | static LIST_HEAD(shmem_swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 269 | static DEFINE_MUTEX(shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 271 | /* |
| 272 | * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and |
| 273 | * produces a novel ino for the newly allocated inode. |
| 274 | * |
| 275 | * It may also be called when making a hard link to permit the space needed by |
| 276 | * each dentry. However, in that case, no new inode number is needed since that |
| 277 | * internally draws from another pool of inode numbers (currently global |
| 278 | * get_next_ino()). This case is indicated by passing NULL as inop. |
| 279 | */ |
| 280 | #define SHMEM_INO_BATCH 1024 |
| 281 | static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 282 | { |
| 283 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 284 | ino_t ino; |
| 285 | |
| 286 | if (!(sb->s_flags & SB_KERNMOUNT)) { |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 287 | spin_lock(&sbinfo->stat_lock); |
Byron Stanoszek | bb3e96d | 2020-09-18 21:20:18 -0700 | [diff] [blame] | 288 | if (sbinfo->max_inodes) { |
| 289 | if (!sbinfo->free_inodes) { |
| 290 | spin_unlock(&sbinfo->stat_lock); |
| 291 | return -ENOSPC; |
| 292 | } |
| 293 | sbinfo->free_inodes--; |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 294 | } |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 295 | if (inop) { |
| 296 | ino = sbinfo->next_ino++; |
| 297 | if (unlikely(is_zero_ino(ino))) |
| 298 | ino = sbinfo->next_ino++; |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 299 | if (unlikely(!sbinfo->full_inums && |
| 300 | ino > UINT_MAX)) { |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 301 | /* |
| 302 | * Emulate get_next_ino uint wraparound for |
| 303 | * compatibility |
| 304 | */ |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 305 | if (IS_ENABLED(CONFIG_64BIT)) |
| 306 | pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", |
| 307 | __func__, MINOR(sb->s_dev)); |
| 308 | sbinfo->next_ino = 1; |
| 309 | ino = sbinfo->next_ino++; |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 310 | } |
| 311 | *inop = ino; |
| 312 | } |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 313 | spin_unlock(&sbinfo->stat_lock); |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 314 | } else if (inop) { |
| 315 | /* |
| 316 | * __shmem_file_setup, one of our callers, is lock-free: it |
| 317 | * doesn't hold stat_lock in shmem_reserve_inode since |
| 318 | * max_inodes is always 0, and is called from potentially |
| 319 | * unknown contexts. As such, use a per-cpu batched allocator |
| 320 | * which doesn't require the per-sb stat_lock unless we are at |
| 321 | * the batch boundary. |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 322 | * |
| 323 | * We don't need to worry about inode{32,64} since SB_KERNMOUNT |
| 324 | * shmem mounts are not exposed to userspace, so we don't need |
| 325 | * to worry about things like glibc compatibility. |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 326 | */ |
| 327 | ino_t *next_ino; |
| 328 | next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); |
| 329 | ino = *next_ino; |
| 330 | if (unlikely(ino % SHMEM_INO_BATCH == 0)) { |
| 331 | spin_lock(&sbinfo->stat_lock); |
| 332 | ino = sbinfo->next_ino; |
| 333 | sbinfo->next_ino += SHMEM_INO_BATCH; |
| 334 | spin_unlock(&sbinfo->stat_lock); |
| 335 | if (unlikely(is_zero_ino(ino))) |
| 336 | ino++; |
| 337 | } |
| 338 | *inop = ino; |
| 339 | *next_ino = ++ino; |
| 340 | put_cpu(); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 341 | } |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 342 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static void shmem_free_inode(struct super_block *sb) |
| 347 | { |
| 348 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 349 | if (sbinfo->max_inodes) { |
| 350 | spin_lock(&sbinfo->stat_lock); |
| 351 | sbinfo->free_inodes++; |
| 352 | spin_unlock(&sbinfo->stat_lock); |
| 353 | } |
| 354 | } |
| 355 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 356 | /** |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 357 | * shmem_recalc_inode - recalculate the block usage of an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | * @inode: inode to recalc |
| 359 | * |
| 360 | * We have to calculate the free blocks since the mm can drop |
| 361 | * undirtied hole pages behind our back. |
| 362 | * |
| 363 | * But normally info->alloced == inode->i_mapping->nrpages + info->swapped |
| 364 | * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) |
| 365 | * |
| 366 | * It has to be called with the spinlock held. |
| 367 | */ |
| 368 | static void shmem_recalc_inode(struct inode *inode) |
| 369 | { |
| 370 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 371 | long freed; |
| 372 | |
| 373 | freed = info->alloced - info->swapped - inode->i_mapping->nrpages; |
| 374 | if (freed > 0) { |
| 375 | info->alloced -= freed; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 376 | inode->i_blocks -= freed * BLOCKS_PER_PAGE; |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 377 | shmem_inode_unacct_blocks(inode, freed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 381 | bool shmem_charge(struct inode *inode, long pages) |
| 382 | { |
| 383 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 384 | unsigned long flags; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 385 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 386 | if (!shmem_inode_acct_block(inode, pages)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 387 | return false; |
Mike Rapoport | b1cc94a | 2017-09-06 16:22:56 -0700 | [diff] [blame] | 388 | |
Hugh Dickins | aaa52e3 | 2018-11-30 14:10:29 -0800 | [diff] [blame] | 389 | /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ |
| 390 | inode->i_mapping->nrpages += pages; |
| 391 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 392 | spin_lock_irqsave(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 393 | info->alloced += pages; |
| 394 | inode->i_blocks += pages * BLOCKS_PER_PAGE; |
| 395 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 396 | spin_unlock_irqrestore(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 397 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 398 | return true; |
| 399 | } |
| 400 | |
| 401 | void shmem_uncharge(struct inode *inode, long pages) |
| 402 | { |
| 403 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 404 | unsigned long flags; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 405 | |
Hugh Dickins | aaa52e3 | 2018-11-30 14:10:29 -0800 | [diff] [blame] | 406 | /* nrpages adjustment done by __delete_from_page_cache() or caller */ |
| 407 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 408 | spin_lock_irqsave(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 409 | info->alloced -= pages; |
| 410 | inode->i_blocks -= pages * BLOCKS_PER_PAGE; |
| 411 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 412 | spin_unlock_irqrestore(&info->lock, flags); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 413 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 414 | shmem_inode_unacct_blocks(inode, pages); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 417 | /* |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 418 | * Replace item expected in xarray by a new item, while holding xa_lock. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 419 | */ |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 420 | static int shmem_replace_entry(struct address_space *mapping, |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 421 | pgoff_t index, void *expected, void *replacement) |
| 422 | { |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 423 | XA_STATE(xas, &mapping->i_pages, index); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 424 | void *item; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 425 | |
| 426 | VM_BUG_ON(!expected); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 427 | VM_BUG_ON(!replacement); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 428 | item = xas_load(&xas); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 429 | if (item != expected) |
| 430 | return -ENOENT; |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 431 | xas_store(&xas, replacement); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 436 | * Sometimes, before we decide whether to proceed or to fail, we must check |
| 437 | * that an entry was not already brought back from swap by a racing thread. |
| 438 | * |
| 439 | * Checking page is not enough: by the time a SwapCache page is locked, it |
| 440 | * might be reused, and again be SwapCache, using the same swap as before. |
| 441 | */ |
| 442 | static bool shmem_confirm_swap(struct address_space *mapping, |
| 443 | pgoff_t index, swp_entry_t swap) |
| 444 | { |
Matthew Wilcox | a12831b | 2017-11-22 08:34:58 -0500 | [diff] [blame] | 445 | return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 449 | * Definitions for "huge tmpfs": tmpfs mounted with the huge= option |
| 450 | * |
| 451 | * SHMEM_HUGE_NEVER: |
| 452 | * disables huge pages for the mount; |
| 453 | * SHMEM_HUGE_ALWAYS: |
| 454 | * enables huge pages for the mount; |
| 455 | * SHMEM_HUGE_WITHIN_SIZE: |
| 456 | * only allocate huge pages if the page will be fully within i_size, |
| 457 | * also respect fadvise()/madvise() hints; |
| 458 | * SHMEM_HUGE_ADVISE: |
| 459 | * only allocate huge pages if requested with fadvise()/madvise(); |
| 460 | */ |
| 461 | |
| 462 | #define SHMEM_HUGE_NEVER 0 |
| 463 | #define SHMEM_HUGE_ALWAYS 1 |
| 464 | #define SHMEM_HUGE_WITHIN_SIZE 2 |
| 465 | #define SHMEM_HUGE_ADVISE 3 |
| 466 | |
| 467 | /* |
| 468 | * Special values. |
| 469 | * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: |
| 470 | * |
| 471 | * SHMEM_HUGE_DENY: |
| 472 | * disables huge on shm_mnt and all mounts, for emergency use; |
| 473 | * SHMEM_HUGE_FORCE: |
| 474 | * enables huge on shm_mnt and all mounts, w/o needing option, for testing; |
| 475 | * |
| 476 | */ |
| 477 | #define SHMEM_HUGE_DENY (-1) |
| 478 | #define SHMEM_HUGE_FORCE (-2) |
| 479 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 480 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 481 | /* ifdef here to avoid bloating shmem.o when not necessary */ |
| 482 | |
Mike Kravetz | 5b9c98f | 2018-06-07 17:05:53 -0700 | [diff] [blame] | 483 | static int shmem_huge __read_mostly; |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 484 | |
Arnd Bergmann | e5f2249 | 2019-07-16 16:25:51 -0700 | [diff] [blame] | 485 | #if defined(CONFIG_SYSFS) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 486 | static int shmem_parse_huge(const char *str) |
| 487 | { |
| 488 | if (!strcmp(str, "never")) |
| 489 | return SHMEM_HUGE_NEVER; |
| 490 | if (!strcmp(str, "always")) |
| 491 | return SHMEM_HUGE_ALWAYS; |
| 492 | if (!strcmp(str, "within_size")) |
| 493 | return SHMEM_HUGE_WITHIN_SIZE; |
| 494 | if (!strcmp(str, "advise")) |
| 495 | return SHMEM_HUGE_ADVISE; |
| 496 | if (!strcmp(str, "deny")) |
| 497 | return SHMEM_HUGE_DENY; |
| 498 | if (!strcmp(str, "force")) |
| 499 | return SHMEM_HUGE_FORCE; |
| 500 | return -EINVAL; |
| 501 | } |
Arnd Bergmann | e5f2249 | 2019-07-16 16:25:51 -0700 | [diff] [blame] | 502 | #endif |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 503 | |
Arnd Bergmann | e5f2249 | 2019-07-16 16:25:51 -0700 | [diff] [blame] | 504 | #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 505 | static const char *shmem_format_huge(int huge) |
| 506 | { |
| 507 | switch (huge) { |
| 508 | case SHMEM_HUGE_NEVER: |
| 509 | return "never"; |
| 510 | case SHMEM_HUGE_ALWAYS: |
| 511 | return "always"; |
| 512 | case SHMEM_HUGE_WITHIN_SIZE: |
| 513 | return "within_size"; |
| 514 | case SHMEM_HUGE_ADVISE: |
| 515 | return "advise"; |
| 516 | case SHMEM_HUGE_DENY: |
| 517 | return "deny"; |
| 518 | case SHMEM_HUGE_FORCE: |
| 519 | return "force"; |
| 520 | default: |
| 521 | VM_BUG_ON(1); |
| 522 | return "bad_val"; |
| 523 | } |
| 524 | } |
Jérémy Lefaure | f1f5929 | 2016-12-12 16:43:23 -0800 | [diff] [blame] | 525 | #endif |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 526 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 527 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
| 528 | struct shrink_control *sc, unsigned long nr_to_split) |
| 529 | { |
| 530 | LIST_HEAD(list), *pos, *next; |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 531 | LIST_HEAD(to_remove); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 532 | struct inode *inode; |
| 533 | struct shmem_inode_info *info; |
| 534 | struct page *page; |
| 535 | unsigned long batch = sc ? sc->nr_to_scan : 128; |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 536 | int split = 0; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 537 | |
| 538 | if (list_empty(&sbinfo->shrinklist)) |
| 539 | return SHRINK_STOP; |
| 540 | |
| 541 | spin_lock(&sbinfo->shrinklist_lock); |
| 542 | list_for_each_safe(pos, next, &sbinfo->shrinklist) { |
| 543 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 544 | |
| 545 | /* pin the inode */ |
| 546 | inode = igrab(&info->vfs_inode); |
| 547 | |
| 548 | /* inode is about to be evicted */ |
| 549 | if (!inode) { |
| 550 | list_del_init(&info->shrinklist); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 551 | goto next; |
| 552 | } |
| 553 | |
| 554 | /* Check if there's anything to gain */ |
| 555 | if (round_up(inode->i_size, PAGE_SIZE) == |
| 556 | round_up(inode->i_size, HPAGE_PMD_SIZE)) { |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 557 | list_move(&info->shrinklist, &to_remove); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 558 | goto next; |
| 559 | } |
| 560 | |
| 561 | list_move(&info->shrinklist, &list); |
| 562 | next: |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 563 | sbinfo->shrinklist_len--; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 564 | if (!--batch) |
| 565 | break; |
| 566 | } |
| 567 | spin_unlock(&sbinfo->shrinklist_lock); |
| 568 | |
Kirill A. Shutemov | 253fd0f | 2017-02-03 13:13:15 -0800 | [diff] [blame] | 569 | list_for_each_safe(pos, next, &to_remove) { |
| 570 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 571 | inode = &info->vfs_inode; |
| 572 | list_del_init(&info->shrinklist); |
| 573 | iput(inode); |
| 574 | } |
| 575 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 576 | list_for_each_safe(pos, next, &list) { |
| 577 | int ret; |
| 578 | |
| 579 | info = list_entry(pos, struct shmem_inode_info, shrinklist); |
| 580 | inode = &info->vfs_inode; |
| 581 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 582 | if (nr_to_split && split >= nr_to_split) |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 583 | goto move_back; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 584 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 585 | page = find_get_page(inode->i_mapping, |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 586 | (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT); |
| 587 | if (!page) |
| 588 | goto drop; |
| 589 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 590 | /* No huge page at the end of the file: nothing to split */ |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 591 | if (!PageTransHuge(page)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 592 | put_page(page); |
| 593 | goto drop; |
| 594 | } |
| 595 | |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 596 | /* |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 597 | * Move the inode on the list back to shrinklist if we failed |
| 598 | * to lock the page at this time. |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 599 | * |
| 600 | * Waiting for the lock may lead to deadlock in the |
| 601 | * reclaim path. |
| 602 | */ |
| 603 | if (!trylock_page(page)) { |
| 604 | put_page(page); |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 605 | goto move_back; |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 608 | ret = split_huge_page(page); |
| 609 | unlock_page(page); |
| 610 | put_page(page); |
| 611 | |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 612 | /* If split failed move the inode on the list back to shrinklist */ |
Kirill A. Shutemov | b3cd54b | 2018-03-22 16:17:35 -0700 | [diff] [blame] | 613 | if (ret) |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 614 | goto move_back; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 615 | |
| 616 | split++; |
| 617 | drop: |
| 618 | list_del_init(&info->shrinklist); |
Gang Li | 7b9fa91 | 2022-01-14 14:05:23 -0800 | [diff] [blame] | 619 | goto put; |
| 620 | move_back: |
| 621 | /* |
| 622 | * Make sure the inode is either on the global list or deleted |
| 623 | * from any local list before iput() since it could be deleted |
| 624 | * in another thread once we put the inode (then the local list |
| 625 | * is corrupted). |
| 626 | */ |
| 627 | spin_lock(&sbinfo->shrinklist_lock); |
| 628 | list_move(&info->shrinklist, &sbinfo->shrinklist); |
| 629 | sbinfo->shrinklist_len++; |
| 630 | spin_unlock(&sbinfo->shrinklist_lock); |
| 631 | put: |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 632 | iput(inode); |
| 633 | } |
| 634 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 635 | return split; |
| 636 | } |
| 637 | |
| 638 | static long shmem_unused_huge_scan(struct super_block *sb, |
| 639 | struct shrink_control *sc) |
| 640 | { |
| 641 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 642 | |
| 643 | if (!READ_ONCE(sbinfo->shrinklist_len)) |
| 644 | return SHRINK_STOP; |
| 645 | |
| 646 | return shmem_unused_huge_shrink(sbinfo, sc, 0); |
| 647 | } |
| 648 | |
| 649 | static long shmem_unused_huge_count(struct super_block *sb, |
| 650 | struct shrink_control *sc) |
| 651 | { |
| 652 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 653 | return READ_ONCE(sbinfo->shrinklist_len); |
| 654 | } |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 655 | #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 656 | |
| 657 | #define shmem_huge SHMEM_HUGE_DENY |
| 658 | |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 659 | static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, |
| 660 | struct shrink_control *sc, unsigned long nr_to_split) |
| 661 | { |
| 662 | return 0; |
| 663 | } |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 664 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 665 | |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 666 | static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo) |
| 667 | { |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 668 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 669 | (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) && |
| 670 | shmem_huge != SHMEM_HUGE_DENY) |
| 671 | return true; |
| 672 | return false; |
| 673 | } |
| 674 | |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 675 | /* |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 676 | * Like add_to_page_cache_locked, but error if expected item has gone. |
| 677 | */ |
| 678 | static int shmem_add_to_page_cache(struct page *page, |
| 679 | struct address_space *mapping, |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 680 | pgoff_t index, void *expected, gfp_t gfp, |
| 681 | struct mm_struct *charge_mm) |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 682 | { |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 683 | XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page)); |
| 684 | unsigned long i = 0; |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 685 | unsigned long nr = compound_nr(page); |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 686 | int error; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 687 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 688 | VM_BUG_ON_PAGE(PageTail(page), page); |
| 689 | VM_BUG_ON_PAGE(index != round_down(index, nr), page); |
Sasha Levin | 309381fea | 2014-01-23 15:52:54 -0800 | [diff] [blame] | 690 | VM_BUG_ON_PAGE(!PageLocked(page), page); |
| 691 | VM_BUG_ON_PAGE(!PageSwapBacked(page), page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 692 | VM_BUG_ON(expected && PageTransHuge(page)); |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 693 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 694 | page_ref_add(page, nr); |
Hugh Dickins | b065b43 | 2012-07-11 14:02:48 -0700 | [diff] [blame] | 695 | page->mapping = mapping; |
| 696 | page->index = index; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 697 | |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 698 | if (!PageSwapCache(page)) { |
Johannes Weiner | d9eb1ea | 2020-06-03 16:02:24 -0700 | [diff] [blame] | 699 | error = mem_cgroup_charge(page, charge_mm, gfp); |
Johannes Weiner | 4c6355b | 2020-06-03 16:02:17 -0700 | [diff] [blame] | 700 | if (error) { |
| 701 | if (PageTransHuge(page)) { |
| 702 | count_vm_event(THP_FILE_FALLBACK); |
| 703 | count_vm_event(THP_FILE_FALLBACK_CHARGE); |
| 704 | } |
| 705 | goto error; |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 706 | } |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 707 | } |
| 708 | cgroup_throttle_swaprate(page, gfp); |
| 709 | |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 710 | do { |
| 711 | void *entry; |
| 712 | xas_lock_irq(&xas); |
| 713 | entry = xas_find_conflict(&xas); |
| 714 | if (entry != expected) |
| 715 | xas_set_err(&xas, -EEXIST); |
| 716 | xas_create_range(&xas); |
| 717 | if (xas_error(&xas)) |
| 718 | goto unlock; |
| 719 | next: |
Matthew Wilcox (Oracle) | 4101196 | 2019-09-23 15:34:52 -0700 | [diff] [blame] | 720 | xas_store(&xas, page); |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 721 | if (++i < nr) { |
| 722 | xas_next(&xas); |
| 723 | goto next; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 724 | } |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 725 | if (PageTransHuge(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 726 | count_vm_event(THP_FILE_ALLOC); |
Mel Gorman | 11fb998 | 2016-07-28 15:46:20 -0700 | [diff] [blame] | 727 | __inc_node_page_state(page, NR_SHMEM_THPS); |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 728 | } |
| 729 | mapping->nrpages += nr; |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 730 | __mod_lruvec_page_state(page, NR_FILE_PAGES, nr); |
| 731 | __mod_lruvec_page_state(page, NR_SHMEM, nr); |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 732 | unlock: |
| 733 | xas_unlock_irq(&xas); |
| 734 | } while (xas_nomem(&xas, gfp)); |
| 735 | |
| 736 | if (xas_error(&xas)) { |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 737 | error = xas_error(&xas); |
| 738 | goto error; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 739 | } |
Matthew Wilcox | 552446a4 | 2017-12-01 13:25:14 -0500 | [diff] [blame] | 740 | |
| 741 | return 0; |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 742 | error: |
| 743 | page->mapping = NULL; |
| 744 | page_ref_sub(page, nr); |
| 745 | return error; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | /* |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 749 | * Like delete_from_page_cache, but substitutes swap for page. |
| 750 | */ |
| 751 | static void shmem_delete_from_page_cache(struct page *page, void *radswap) |
| 752 | { |
| 753 | struct address_space *mapping = page->mapping; |
| 754 | int error; |
| 755 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 756 | VM_BUG_ON_PAGE(PageCompound(page), page); |
| 757 | |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 758 | xa_lock_irq(&mapping->i_pages); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 759 | error = shmem_replace_entry(mapping, page->index, page, radswap); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 760 | page->mapping = NULL; |
| 761 | mapping->nrpages--; |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 762 | __dec_lruvec_page_state(page, NR_FILE_PAGES); |
| 763 | __dec_lruvec_page_state(page, NR_SHMEM); |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 764 | xa_unlock_irq(&mapping->i_pages); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 765 | put_page(page); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 766 | BUG_ON(error); |
| 767 | } |
| 768 | |
| 769 | /* |
Matthew Wilcox | c121d3b | 2017-12-04 03:13:54 -0500 | [diff] [blame] | 770 | * Remove swap entry from page cache, free the swap and its page cache. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 771 | */ |
| 772 | static int shmem_free_swap(struct address_space *mapping, |
| 773 | pgoff_t index, void *radswap) |
| 774 | { |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 775 | void *old; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 776 | |
Matthew Wilcox | 55f3f7e | 2018-11-26 16:08:43 -0500 | [diff] [blame] | 777 | old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); |
Johannes Weiner | 6dbaf22 | 2014-04-03 14:47:41 -0700 | [diff] [blame] | 778 | if (old != radswap) |
| 779 | return -ENOENT; |
| 780 | free_swap_and_cache(radix_to_swp_entry(radswap)); |
| 781 | return 0; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | /* |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 785 | * Determine (in bytes) how many of the shmem object's pages mapped by the |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 786 | * given offsets are swapped out. |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 787 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 788 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 789 | * as long as the inode doesn't go away and racy results are not a problem. |
| 790 | */ |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 791 | unsigned long shmem_partial_swap_usage(struct address_space *mapping, |
| 792 | pgoff_t start, pgoff_t end) |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 793 | { |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 794 | XA_STATE(xas, &mapping->i_pages, start); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 795 | struct page *page; |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 796 | unsigned long swapped = 0; |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 797 | |
| 798 | rcu_read_lock(); |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 799 | xas_for_each(&xas, page, end - 1) { |
| 800 | if (xas_retry(&xas, page)) |
Matthew Wilcox | 2cf938a | 2016-03-17 14:22:03 -0700 | [diff] [blame] | 801 | continue; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 802 | if (xa_is_value(page)) |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 803 | swapped++; |
| 804 | |
| 805 | if (need_resched()) { |
Matthew Wilcox | 7ae3424 | 2017-12-04 03:28:00 -0500 | [diff] [blame] | 806 | xas_pause(&xas); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 807 | cond_resched_rcu(); |
Vlastimil Babka | 6a15a37 | 2016-01-14 15:19:20 -0800 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
| 811 | rcu_read_unlock(); |
| 812 | |
| 813 | return swapped << PAGE_SHIFT; |
| 814 | } |
| 815 | |
| 816 | /* |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 817 | * Determine (in bytes) how many of the shmem object's pages mapped by the |
| 818 | * given vma is swapped out. |
| 819 | * |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 820 | * This is safe to call without i_mutex or the i_pages lock thanks to RCU, |
Vlastimil Babka | 48131e0 | 2016-01-14 15:19:23 -0800 | [diff] [blame] | 821 | * as long as the inode doesn't go away and racy results are not a problem. |
| 822 | */ |
| 823 | unsigned long shmem_swap_usage(struct vm_area_struct *vma) |
| 824 | { |
| 825 | struct inode *inode = file_inode(vma->vm_file); |
| 826 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 827 | struct address_space *mapping = inode->i_mapping; |
| 828 | unsigned long swapped; |
| 829 | |
| 830 | /* Be careful as we don't hold info->lock */ |
| 831 | swapped = READ_ONCE(info->swapped); |
| 832 | |
| 833 | /* |
| 834 | * The easier cases are when the shmem object has nothing in swap, or |
| 835 | * the vma maps it whole. Then we can simply use the stats that we |
| 836 | * already track. |
| 837 | */ |
| 838 | if (!swapped) |
| 839 | return 0; |
| 840 | |
| 841 | if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) |
| 842 | return swapped << PAGE_SHIFT; |
| 843 | |
| 844 | /* Here comes the more involved part */ |
| 845 | return shmem_partial_swap_usage(mapping, |
| 846 | linear_page_index(vma, vma->vm_start), |
| 847 | linear_page_index(vma, vma->vm_end)); |
| 848 | } |
| 849 | |
| 850 | /* |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 851 | * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. |
| 852 | */ |
| 853 | void shmem_unlock_mapping(struct address_space *mapping) |
| 854 | { |
| 855 | struct pagevec pvec; |
| 856 | pgoff_t indices[PAGEVEC_SIZE]; |
| 857 | pgoff_t index = 0; |
| 858 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 859 | pagevec_init(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 860 | /* |
| 861 | * Minor point, but we might as well stop if someone else SHM_LOCKs it. |
| 862 | */ |
| 863 | while (!mapping_unevictable(mapping)) { |
| 864 | /* |
| 865 | * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it |
| 866 | * has finished, if it hits a row of PAGEVEC_SIZE swap entries. |
| 867 | */ |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 868 | pvec.nr = find_get_entries(mapping, index, |
| 869 | PAGEVEC_SIZE, pvec.pages, indices); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 870 | if (!pvec.nr) |
| 871 | break; |
| 872 | index = indices[pvec.nr - 1] + 1; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 873 | pagevec_remove_exceptionals(&pvec); |
Kuo-Hsin Yang | 64e3d12 | 2018-11-06 13:23:24 +0000 | [diff] [blame] | 874 | check_move_unevictable_pages(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 875 | pagevec_release(&pvec); |
| 876 | cond_resched(); |
| 877 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /* |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 881 | * Check whether a hole-punch or truncation needs to split a huge page, |
| 882 | * returning true if no split was required, or the split has been successful. |
| 883 | * |
| 884 | * Eviction (or truncation to 0 size) should never need to split a huge page; |
| 885 | * but in rare cases might do so, if shmem_undo_range() failed to trylock on |
| 886 | * head, and then succeeded to trylock on tail. |
| 887 | * |
| 888 | * A split can only succeed when there are no additional references on the |
| 889 | * huge page: so the split below relies upon find_get_entries() having stopped |
| 890 | * when it found a subpage of the huge page, without getting further references. |
| 891 | */ |
| 892 | static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end) |
| 893 | { |
| 894 | if (!PageTransCompound(page)) |
| 895 | return true; |
| 896 | |
| 897 | /* Just proceed to delete a huge page wholly within the range punched */ |
| 898 | if (PageHead(page) && |
| 899 | page->index >= start && page->index + HPAGE_PMD_NR <= end) |
| 900 | return true; |
| 901 | |
| 902 | /* Try to split huge page, so we can truly punch the hole or truncate */ |
| 903 | return split_huge_page(page) >= 0; |
| 904 | } |
| 905 | |
| 906 | /* |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 907 | * Remove range of pages and swap entries from page cache, and free them. |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 908 | * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 909 | */ |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 910 | static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, |
| 911 | bool unfalloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | { |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 913 | struct address_space *mapping = inode->i_mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 915 | pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 916 | pgoff_t end = (lend + 1) >> PAGE_SHIFT; |
| 917 | unsigned int partial_start = lstart & (PAGE_SIZE - 1); |
| 918 | unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 919 | struct pagevec pvec; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 920 | pgoff_t indices[PAGEVEC_SIZE]; |
| 921 | long nr_swaps_freed = 0; |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 922 | pgoff_t index; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 923 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 925 | if (lend == -1) |
| 926 | end = -1; /* unsigned, so actually very big */ |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 927 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 928 | pagevec_init(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 929 | index = start; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 930 | while (index < end) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 931 | pvec.nr = find_get_entries(mapping, index, |
| 932 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
| 933 | pvec.pages, indices); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 934 | if (!pvec.nr) |
| 935 | break; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 936 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 937 | struct page *page = pvec.pages[i]; |
| 938 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 939 | index = indices[i]; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 940 | if (index >= end) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 941 | break; |
| 942 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 943 | if (xa_is_value(page)) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 944 | if (unfalloc) |
| 945 | continue; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 946 | nr_swaps_freed += !shmem_free_swap(mapping, |
| 947 | index, page); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 948 | continue; |
| 949 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 950 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 951 | VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page); |
| 952 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 953 | if (!trylock_page(page)) |
| 954 | continue; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 955 | |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 956 | if ((!unfalloc || !PageUptodate(page)) && |
| 957 | page_mapping(page) == mapping) { |
| 958 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
| 959 | if (shmem_punch_compound(page, start, end)) |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 960 | truncate_inode_page(mapping, page); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 961 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 962 | unlock_page(page); |
| 963 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 964 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 965 | pagevec_release(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 966 | cond_resched(); |
| 967 | index++; |
| 968 | } |
| 969 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 970 | if (partial_start) { |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 971 | struct page *page = NULL; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 972 | shmem_getpage(inode, start - 1, &page, SGP_READ); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 973 | if (page) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 974 | unsigned int top = PAGE_SIZE; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 975 | if (start > end) { |
| 976 | top = partial_end; |
| 977 | partial_end = 0; |
| 978 | } |
| 979 | zero_user_segment(page, partial_start, top); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 980 | set_page_dirty(page); |
| 981 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 982 | put_page(page); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 983 | } |
| 984 | } |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 985 | if (partial_end) { |
| 986 | struct page *page = NULL; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 987 | shmem_getpage(inode, end, &page, SGP_READ); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 988 | if (page) { |
| 989 | zero_user_segment(page, 0, partial_end); |
| 990 | set_page_dirty(page); |
| 991 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 992 | put_page(page); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 993 | } |
| 994 | } |
| 995 | if (start >= end) |
| 996 | return; |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 997 | |
| 998 | index = start; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 999 | while (index < end) { |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1000 | cond_resched(); |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1001 | |
| 1002 | pvec.nr = find_get_entries(mapping, index, |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 1003 | min(end - index, (pgoff_t)PAGEVEC_SIZE), |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1004 | pvec.pages, indices); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 1005 | if (!pvec.nr) { |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 1006 | /* If all gone or hole-punch or unfalloc, we're done */ |
| 1007 | if (index == start || end != -1) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1008 | break; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 1009 | /* But if truncating, restart to make sure all gone */ |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1010 | index = start; |
| 1011 | continue; |
| 1012 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1013 | for (i = 0; i < pagevec_count(&pvec); i++) { |
| 1014 | struct page *page = pvec.pages[i]; |
| 1015 | |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 1016 | index = indices[i]; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 1017 | if (index >= end) |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1018 | break; |
| 1019 | |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 1020 | if (xa_is_value(page)) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1021 | if (unfalloc) |
| 1022 | continue; |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 1023 | if (shmem_free_swap(mapping, index, page)) { |
| 1024 | /* Swap was replaced by page: retry */ |
| 1025 | index--; |
| 1026 | break; |
| 1027 | } |
| 1028 | nr_swaps_freed++; |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 1029 | continue; |
| 1030 | } |
| 1031 | |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1032 | lock_page(page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1033 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1034 | if (!unfalloc || !PageUptodate(page)) { |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 1035 | if (page_mapping(page) != mapping) { |
Hugh Dickins | b1a3665 | 2014-07-23 14:00:13 -0700 | [diff] [blame] | 1036 | /* Page was replaced by swap: retry */ |
| 1037 | unlock_page(page); |
| 1038 | index--; |
| 1039 | break; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1040 | } |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 1041 | VM_BUG_ON_PAGE(PageWriteback(page), page); |
| 1042 | if (shmem_punch_compound(page, start, end)) |
| 1043 | truncate_inode_page(mapping, page); |
Hugh Dickins | 0783ac95b | 2020-04-20 18:14:07 -0700 | [diff] [blame] | 1044 | else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { |
Hugh Dickins | 71725ed | 2020-04-06 20:07:57 -0700 | [diff] [blame] | 1045 | /* Wipe the page and don't get stuck */ |
| 1046 | clear_highpage(page); |
| 1047 | flush_dcache_page(page); |
| 1048 | set_page_dirty(page); |
| 1049 | if (index < |
| 1050 | round_up(start, HPAGE_PMD_NR)) |
| 1051 | start = index + 1; |
| 1052 | } |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 1053 | } |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1054 | unlock_page(page); |
| 1055 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 1056 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 1057 | pagevec_release(&pvec); |
Hugh Dickins | bda97ea | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1058 | index++; |
| 1059 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1060 | |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1061 | spin_lock_irq(&info->lock); |
Hugh Dickins | 7a5d0fb | 2011-08-03 16:21:22 -0700 | [diff] [blame] | 1062 | info->swapped -= nr_swaps_freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1063 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1064 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1067 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
| 1068 | { |
| 1069 | shmem_undo_range(inode, lstart, lend, false); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1070 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1072 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1074 | static int shmem_getattr(const struct path *path, struct kstat *stat, |
| 1075 | u32 request_mask, unsigned int query_flags) |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 1076 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 1077 | struct inode *inode = path->dentry->d_inode; |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 1078 | struct shmem_inode_info *info = SHMEM_I(inode); |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 1079 | struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb); |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 1080 | |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1081 | if (info->alloced - info->swapped != inode->i_mapping->nrpages) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1082 | spin_lock_irq(&info->lock); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1083 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1084 | spin_unlock_irq(&info->lock); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1085 | } |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 1086 | generic_fillattr(inode, stat); |
Yang Shi | 89fdcd2 | 2018-06-07 17:06:59 -0700 | [diff] [blame] | 1087 | |
| 1088 | if (is_huge_enabled(sb_info)) |
| 1089 | stat->blksize = HPAGE_PMD_SIZE; |
| 1090 | |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 1091 | return 0; |
| 1092 | } |
| 1093 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1094 | static int shmem_setattr(struct dentry *dentry, struct iattr *attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 1096 | struct inode *inode = d_inode(dentry); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 1097 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1098 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | int error; |
| 1100 | |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 1101 | error = setattr_prepare(dentry, attr); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1102 | if (error) |
| 1103 | return error; |
| 1104 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1105 | if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { |
| 1106 | loff_t oldsize = inode->i_size; |
| 1107 | loff_t newsize = attr->ia_size; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1108 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 1109 | /* protected by i_mutex */ |
| 1110 | if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || |
| 1111 | (newsize > oldsize && (info->seals & F_SEAL_GROW))) |
| 1112 | return -EPERM; |
| 1113 | |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1114 | if (newsize != oldsize) { |
Konstantin Khlebnikov | 7714251 | 2014-08-06 16:06:34 -0700 | [diff] [blame] | 1115 | error = shmem_reacct_size(SHMEM_I(inode)->flags, |
| 1116 | oldsize, newsize); |
| 1117 | if (error) |
| 1118 | return error; |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1119 | i_size_write(inode, newsize); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 1120 | inode->i_ctime = inode->i_mtime = current_time(inode); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1121 | } |
Josef Bacik | afa2db2 | 2015-06-24 16:58:45 -0700 | [diff] [blame] | 1122 | if (newsize <= oldsize) { |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1123 | loff_t holebegin = round_up(newsize, PAGE_SIZE); |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1124 | if (oldsize > holebegin) |
| 1125 | unmap_mapping_range(inode->i_mapping, |
| 1126 | holebegin, 0, 1); |
| 1127 | if (info->alloced) |
| 1128 | shmem_truncate_range(inode, |
| 1129 | newsize, (loff_t)-1); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1130 | /* unmap again to remove racily COWed private pages */ |
Hugh Dickins | d0424c4 | 2015-11-05 18:50:34 -0800 | [diff] [blame] | 1131 | if (oldsize > holebegin) |
| 1132 | unmap_mapping_range(inode->i_mapping, |
| 1133 | holebegin, 0, 1); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1134 | |
| 1135 | /* |
| 1136 | * Part of the huge page can be beyond i_size: subject |
| 1137 | * to shrink under memory pressure. |
| 1138 | */ |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 1139 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1140 | spin_lock(&sbinfo->shrinklist_lock); |
Cong Wang | d041353 | 2017-08-10 15:24:24 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * _careful to defend against unlocked access to |
| 1143 | * ->shrink_list in shmem_unused_huge_shrink() |
| 1144 | */ |
| 1145 | if (list_empty_careful(&info->shrinklist)) { |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1146 | list_add_tail(&info->shrinklist, |
| 1147 | &sbinfo->shrinklist); |
| 1148 | sbinfo->shrinklist_len++; |
| 1149 | } |
| 1150 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1151 | } |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 1152 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | } |
| 1154 | |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1155 | setattr_copy(inode, attr); |
Christoph Hellwig | db78b87 | 2010-06-04 11:30:03 +0200 | [diff] [blame] | 1156 | if (attr->ia_valid & ATTR_MODE) |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 1157 | error = posix_acl_chmod(inode, inode->i_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | return error; |
| 1159 | } |
| 1160 | |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 1161 | static void shmem_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1164 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1165 | |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1166 | if (inode->i_mapping->a_ops == &shmem_aops) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | shmem_unacct_size(info->flags, inode->i_size); |
| 1168 | inode->i_size = 0; |
npiggin@suse.de | 3889e6e | 2010-05-27 01:05:36 +1000 | [diff] [blame] | 1169 | shmem_truncate_range(inode, 0, (loff_t)-1); |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 1170 | if (!list_empty(&info->shrinklist)) { |
| 1171 | spin_lock(&sbinfo->shrinklist_lock); |
| 1172 | if (!list_empty(&info->shrinklist)) { |
| 1173 | list_del_init(&info->shrinklist); |
| 1174 | sbinfo->shrinklist_len--; |
| 1175 | } |
| 1176 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1177 | } |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 1178 | while (!list_empty(&info->swaplist)) { |
| 1179 | /* Wait while shmem_unuse() is scanning this inode... */ |
| 1180 | wait_var_event(&info->stop_eviction, |
| 1181 | !atomic_read(&info->stop_eviction)); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1182 | mutex_lock(&shmem_swaplist_mutex); |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 1183 | /* ...but beware of the race if we peeked too early */ |
| 1184 | if (!atomic_read(&info->stop_eviction)) |
| 1185 | list_del_init(&info->swaplist); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1186 | mutex_unlock(&shmem_swaplist_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
Al Viro | 3ed47db | 2016-01-22 18:08:52 -0500 | [diff] [blame] | 1188 | } |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 1189 | |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 1190 | simple_xattrs_free(&info->xattrs); |
Hugh Dickins | 0f3c42f | 2012-11-16 14:15:04 -0800 | [diff] [blame] | 1191 | WARN_ON(inode->i_blocks); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 1192 | shmem_free_inode(inode->i_sb); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 1193 | clear_inode(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | } |
| 1195 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1196 | extern struct swap_info_struct *swap_info[]; |
| 1197 | |
| 1198 | static int shmem_find_swap_entries(struct address_space *mapping, |
| 1199 | pgoff_t start, unsigned int nr_entries, |
| 1200 | struct page **entries, pgoff_t *indices, |
Hugh Dickins | 8703954 | 2019-04-18 17:49:58 -0700 | [diff] [blame] | 1201 | unsigned int type, bool frontswap) |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1202 | { |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1203 | XA_STATE(xas, &mapping->i_pages, start); |
| 1204 | struct page *page; |
Hugh Dickins | 8703954 | 2019-04-18 17:49:58 -0700 | [diff] [blame] | 1205 | swp_entry_t entry; |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1206 | unsigned int ret = 0; |
| 1207 | |
| 1208 | if (!nr_entries) |
| 1209 | return 0; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1210 | |
| 1211 | rcu_read_lock(); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1212 | xas_for_each(&xas, page, ULONG_MAX) { |
| 1213 | if (xas_retry(&xas, page)) |
Mike Kravetz | 5b9c98f | 2018-06-07 17:05:53 -0700 | [diff] [blame] | 1214 | continue; |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1215 | |
| 1216 | if (!xa_is_value(page)) |
| 1217 | continue; |
| 1218 | |
Hugh Dickins | 8703954 | 2019-04-18 17:49:58 -0700 | [diff] [blame] | 1219 | entry = radix_to_swp_entry(page); |
| 1220 | if (swp_type(entry) != type) |
| 1221 | continue; |
| 1222 | if (frontswap && |
| 1223 | !frontswap_test(swap_info[type], swp_offset(entry))) |
| 1224 | continue; |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1225 | |
| 1226 | indices[ret] = xas.xa_index; |
| 1227 | entries[ret] = page; |
| 1228 | |
| 1229 | if (need_resched()) { |
| 1230 | xas_pause(&xas); |
| 1231 | cond_resched_rcu(); |
| 1232 | } |
| 1233 | if (++ret == nr_entries) |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1234 | break; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1235 | } |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1236 | rcu_read_unlock(); |
Matthew Wilcox | e21a295 | 2017-11-22 08:36:00 -0500 | [diff] [blame] | 1237 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1238 | return ret; |
| 1239 | } |
| 1240 | |
| 1241 | /* |
| 1242 | * Move the swapped pages for an inode to page cache. Returns the count |
| 1243 | * of pages swapped in, or the error in case of failure. |
| 1244 | */ |
| 1245 | static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec, |
| 1246 | pgoff_t *indices) |
| 1247 | { |
| 1248 | int i = 0; |
| 1249 | int ret = 0; |
| 1250 | int error = 0; |
| 1251 | struct address_space *mapping = inode->i_mapping; |
| 1252 | |
| 1253 | for (i = 0; i < pvec.nr; i++) { |
| 1254 | struct page *page = pvec.pages[i]; |
| 1255 | |
| 1256 | if (!xa_is_value(page)) |
| 1257 | continue; |
| 1258 | error = shmem_swapin_page(inode, indices[i], |
| 1259 | &page, SGP_CACHE, |
| 1260 | mapping_gfp_mask(mapping), |
| 1261 | NULL, NULL); |
| 1262 | if (error == 0) { |
| 1263 | unlock_page(page); |
| 1264 | put_page(page); |
| 1265 | ret++; |
| 1266 | } |
| 1267 | if (error == -ENOMEM) |
| 1268 | break; |
| 1269 | error = 0; |
| 1270 | } |
| 1271 | return error ? error : ret; |
Matthew Wilcox | 478922e | 2016-12-14 15:08:52 -0800 | [diff] [blame] | 1272 | } |
| 1273 | |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1274 | /* |
| 1275 | * If swap found in inode, free it and move page from swapcache to filecache. |
| 1276 | */ |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1277 | static int shmem_unuse_inode(struct inode *inode, unsigned int type, |
| 1278 | bool frontswap, unsigned long *fs_pages_to_unuse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | { |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1280 | struct address_space *mapping = inode->i_mapping; |
| 1281 | pgoff_t start = 0; |
| 1282 | struct pagevec pvec; |
| 1283 | pgoff_t indices[PAGEVEC_SIZE]; |
| 1284 | bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0); |
| 1285 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1287 | pagevec_init(&pvec); |
| 1288 | do { |
| 1289 | unsigned int nr_entries = PAGEVEC_SIZE; |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1290 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1291 | if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE) |
| 1292 | nr_entries = *fs_pages_to_unuse; |
Hugh Dickins | 2e0e26c | 2008-02-04 22:28:53 -0800 | [diff] [blame] | 1293 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1294 | pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries, |
| 1295 | pvec.pages, indices, |
Hugh Dickins | 8703954 | 2019-04-18 17:49:58 -0700 | [diff] [blame] | 1296 | type, frontswap); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1297 | if (pvec.nr == 0) { |
| 1298 | ret = 0; |
| 1299 | break; |
Hugh Dickins | 46f65ec | 2011-08-03 16:21:23 -0700 | [diff] [blame] | 1300 | } |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1301 | |
| 1302 | ret = shmem_unuse_swap_entries(inode, pvec, indices); |
| 1303 | if (ret < 0) |
| 1304 | break; |
| 1305 | |
| 1306 | if (frontswap_partial) { |
| 1307 | *fs_pages_to_unuse -= ret; |
| 1308 | if (*fs_pages_to_unuse == 0) { |
| 1309 | ret = FRONTSWAP_PAGES_UNUSED; |
| 1310 | break; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | start = indices[pvec.nr - 1]; |
| 1315 | } while (true); |
| 1316 | |
| 1317 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /* |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1321 | * Read all the shared memory data that resides in the swap |
| 1322 | * device 'type' back into memory, so the swap device can be |
| 1323 | * unused. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | */ |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1325 | int shmem_unuse(unsigned int type, bool frontswap, |
| 1326 | unsigned long *fs_pages_to_unuse) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1328 | struct shmem_inode_info *info, *next; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1329 | int error = 0; |
| 1330 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1331 | if (list_empty(&shmem_swaplist)) |
| 1332 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1334 | mutex_lock(&shmem_swaplist_mutex); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1335 | list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { |
| 1336 | if (!info->swapped) { |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1337 | list_del_init(&info->swaplist); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1338 | continue; |
| 1339 | } |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * Drop the swaplist mutex while searching the inode for swap; |
| 1342 | * but before doing so, make sure shmem_evict_inode() will not |
| 1343 | * remove placeholder inode from swaplist, nor let it be freed |
| 1344 | * (igrab() would protect from unlink, but not from unmount). |
| 1345 | */ |
| 1346 | atomic_inc(&info->stop_eviction); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1347 | mutex_unlock(&shmem_swaplist_mutex); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1348 | |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 1349 | error = shmem_unuse_inode(&info->vfs_inode, type, frontswap, |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1350 | fs_pages_to_unuse); |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1351 | cond_resched(); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1352 | |
| 1353 | mutex_lock(&shmem_swaplist_mutex); |
| 1354 | next = list_next_entry(info, swaplist); |
| 1355 | if (!info->swapped) |
| 1356 | list_del_init(&info->swaplist); |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 1357 | if (atomic_dec_and_test(&info->stop_eviction)) |
| 1358 | wake_up_var(&info->stop_eviction); |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1359 | if (error) |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1360 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | } |
Hugh Dickins | cb5f7b9 | 2008-02-04 22:28:52 -0800 | [diff] [blame] | 1362 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1363 | |
Hugh Dickins | 778dd89 | 2011-05-11 15:13:37 -0700 | [diff] [blame] | 1364 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /* |
| 1368 | * Move the page from the page cache to the swap cache. |
| 1369 | */ |
| 1370 | static int shmem_writepage(struct page *page, struct writeback_control *wbc) |
| 1371 | { |
| 1372 | struct shmem_inode_info *info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | struct address_space *mapping; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | struct inode *inode; |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1375 | swp_entry_t swap; |
| 1376 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1378 | VM_BUG_ON_PAGE(PageCompound(page), page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | BUG_ON(!PageLocked(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | mapping = page->mapping; |
| 1381 | index = page->index; |
| 1382 | inode = mapping->host; |
| 1383 | info = SHMEM_I(inode); |
| 1384 | if (info->flags & VM_LOCKED) |
| 1385 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1386 | if (!total_swap_pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | goto redirty; |
| 1388 | |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1389 | /* |
Christoph Hellwig | 97b713b | 2015-01-14 10:42:31 +0100 | [diff] [blame] | 1390 | * Our capabilities prevent regular writeback or sync from ever calling |
| 1391 | * shmem_writepage; but a stacking filesystem might use ->writepage of |
| 1392 | * its underlying filesystem, in which case tmpfs should write out to |
| 1393 | * swap only in response to memory pressure, and not for the writeback |
| 1394 | * threads or sync. |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1395 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1396 | if (!wbc->for_reclaim) { |
| 1397 | WARN_ON_ONCE(1); /* Still happens? Tell us about it! */ |
| 1398 | goto redirty; |
| 1399 | } |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1400 | |
| 1401 | /* |
| 1402 | * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC |
| 1403 | * value into swapfile.c, the only way we can correctly account for a |
| 1404 | * fallocated page arriving here is now to initialize it and write it. |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1405 | * |
| 1406 | * That's okay for a page already fallocated earlier, but if we have |
| 1407 | * not yet completed the fallocation, then (a) we want to keep track |
| 1408 | * of this page in case we have to undo it, and (b) it may not be a |
| 1409 | * good idea to continue anyway, once we're pushing into swap. So |
| 1410 | * reactivate the page, and let shmem_fallocate() quit when too many. |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1411 | */ |
| 1412 | if (!PageUptodate(page)) { |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1413 | if (inode->i_private) { |
| 1414 | struct shmem_falloc *shmem_falloc; |
| 1415 | spin_lock(&inode->i_lock); |
| 1416 | shmem_falloc = inode->i_private; |
| 1417 | if (shmem_falloc && |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 1418 | !shmem_falloc->waitq && |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1419 | index >= shmem_falloc->start && |
| 1420 | index < shmem_falloc->next) |
| 1421 | shmem_falloc->nr_unswapped++; |
| 1422 | else |
| 1423 | shmem_falloc = NULL; |
| 1424 | spin_unlock(&inode->i_lock); |
| 1425 | if (shmem_falloc) |
| 1426 | goto redirty; |
| 1427 | } |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1428 | clear_highpage(page); |
| 1429 | flush_dcache_page(page); |
| 1430 | SetPageUptodate(page); |
| 1431 | } |
| 1432 | |
Huang Ying | 38d8b4e | 2017-07-06 15:37:18 -0700 | [diff] [blame] | 1433 | swap = get_swap_page(page); |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1434 | if (!swap.val) |
| 1435 | goto redirty; |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1436 | |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1437 | /* |
| 1438 | * Add inode to shmem_unuse()'s list of swapped-out inodes, |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1439 | * if it's not already there. Do it now before the page is |
| 1440 | * moved to swap cache, when its pagelock no longer protects |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1441 | * the inode from eviction. But don't unlock the mutex until |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1442 | * we've incremented swapped, because shmem_unuse_inode() will |
| 1443 | * prune a !swapped inode from the swaplist under this mutex. |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1444 | */ |
Hugh Dickins | 48f170f | 2011-07-25 17:12:37 -0700 | [diff] [blame] | 1445 | mutex_lock(&shmem_swaplist_mutex); |
| 1446 | if (list_empty(&info->swaplist)) |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 1447 | list_add(&info->swaplist, &shmem_swaplist); |
Hugh Dickins | b1dea80 | 2011-05-11 15:13:36 -0700 | [diff] [blame] | 1448 | |
Yang Shi | 4afab1c | 2019-11-30 17:58:07 -0800 | [diff] [blame] | 1449 | if (add_to_swap_cache(page, swap, |
Joonsoo Kim | 3852f67 | 2020-08-11 18:30:47 -0700 | [diff] [blame] | 1450 | __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN, |
| 1451 | NULL) == 0) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1452 | spin_lock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1453 | shmem_recalc_inode(inode); |
| 1454 | info->swapped++; |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 1455 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 1456 | |
Hugh Dickins | aaa4686 | 2009-12-14 17:58:47 -0800 | [diff] [blame] | 1457 | swap_shmem_alloc(swap); |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1458 | shmem_delete_from_page_cache(page, swp_to_radix_entry(swap)); |
| 1459 | |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1460 | mutex_unlock(&shmem_swaplist_mutex); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1461 | BUG_ON(page_mapped(page)); |
Hugh Dickins | 9fab561 | 2009-03-31 15:23:33 -0700 | [diff] [blame] | 1462 | swap_writepage(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | return 0; |
| 1464 | } |
| 1465 | |
Hugh Dickins | 6922c0c | 2011-08-03 16:21:25 -0700 | [diff] [blame] | 1466 | mutex_unlock(&shmem_swaplist_mutex); |
Minchan Kim | 75f6d6d | 2017-07-06 15:37:21 -0700 | [diff] [blame] | 1467 | put_swap_page(page, swap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | redirty: |
| 1469 | set_page_dirty(page); |
Hugh Dickins | d9fe526 | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 1470 | if (wbc->for_reclaim) |
| 1471 | return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */ |
| 1472 | unlock_page(page); |
| 1473 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1476 | #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1477 | static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1478 | { |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1479 | char buffer[64]; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1480 | |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1481 | if (!mpol || mpol->mode == MPOL_DEFAULT) |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1482 | return; /* show nothing */ |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1483 | |
Hugh Dickins | a7a88b2 | 2013-01-02 02:04:23 -0800 | [diff] [blame] | 1484 | mpol_to_str(buffer, sizeof(buffer), mpol); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1485 | |
Lee Schermerhorn | 095f1fc | 2008-04-28 02:13:23 -0700 | [diff] [blame] | 1486 | seq_printf(seq, ",mpol=%s", buffer); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1487 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1488 | |
| 1489 | static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 1490 | { |
| 1491 | struct mempolicy *mpol = NULL; |
| 1492 | if (sbinfo->mpol) { |
| 1493 | spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ |
| 1494 | mpol = sbinfo->mpol; |
| 1495 | mpol_get(mpol); |
| 1496 | spin_unlock(&sbinfo->stat_lock); |
| 1497 | } |
| 1498 | return mpol; |
| 1499 | } |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1500 | #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ |
| 1501 | static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) |
| 1502 | { |
| 1503 | } |
| 1504 | static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) |
| 1505 | { |
| 1506 | return NULL; |
| 1507 | } |
| 1508 | #endif /* CONFIG_NUMA && CONFIG_TMPFS */ |
| 1509 | #ifndef CONFIG_NUMA |
| 1510 | #define vm_policy vm_private_data |
| 1511 | #endif |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 1512 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1513 | static void shmem_pseudo_vma_init(struct vm_area_struct *vma, |
| 1514 | struct shmem_inode_info *info, pgoff_t index) |
| 1515 | { |
| 1516 | /* Create a pseudo vma that just contains the policy */ |
Kirill A. Shutemov | 2c4541e | 2018-07-26 16:37:30 -0700 | [diff] [blame] | 1517 | vma_init(vma, NULL); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1518 | /* Bias interleave by inode number to distribute better across nodes */ |
| 1519 | vma->vm_pgoff = index + info->vfs_inode.i_ino; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1520 | vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index); |
| 1521 | } |
| 1522 | |
| 1523 | static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma) |
| 1524 | { |
| 1525 | /* Drop reference taken by mpol_shared_policy_lookup() */ |
| 1526 | mpol_cond_put(vma->vm_policy); |
| 1527 | } |
| 1528 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1529 | static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp, |
| 1530 | struct shmem_inode_info *info, pgoff_t index) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | struct vm_area_struct pvma; |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1533 | struct page *page; |
Will Deacon | cab48b2 | 2021-01-14 15:42:14 +0000 | [diff] [blame] | 1534 | struct vm_fault vmf = { |
| 1535 | .vma = &pvma, |
| 1536 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1537 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1538 | shmem_pseudo_vma_init(&pvma, info, index); |
Minchan Kim | e9e9b7e | 2018-04-05 16:23:42 -0700 | [diff] [blame] | 1539 | page = swap_cluster_readahead(swap, gfp, &vmf); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1540 | shmem_pseudo_vma_destroy(&pvma); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1541 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1542 | return page; |
| 1543 | } |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1544 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1545 | static struct page *shmem_alloc_hugepage(gfp_t gfp, |
| 1546 | struct shmem_inode_info *info, pgoff_t index) |
| 1547 | { |
| 1548 | struct vm_area_struct pvma; |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 1549 | struct address_space *mapping = info->vfs_inode.i_mapping; |
| 1550 | pgoff_t hindex; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1551 | struct page *page; |
| 1552 | |
Geert Uytterhoeven | 4620a06 | 2016-08-03 19:58:19 +0200 | [diff] [blame] | 1553 | hindex = round_down(index, HPAGE_PMD_NR); |
Matthew Wilcox | 7b8d046 | 2017-12-01 22:13:06 -0500 | [diff] [blame] | 1554 | if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1, |
| 1555 | XA_PRESENT)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1556 | return NULL; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1557 | |
| 1558 | shmem_pseudo_vma_init(&pvma, info, hindex); |
| 1559 | page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN, |
David Rientjes | 19deb76 | 2019-09-04 12:54:20 -0700 | [diff] [blame] | 1560 | HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1561 | shmem_pseudo_vma_destroy(&pvma); |
| 1562 | if (page) |
| 1563 | prep_transhuge_page(page); |
David Rientjes | dcdf11e | 2020-04-06 20:04:25 -0700 | [diff] [blame] | 1564 | else |
| 1565 | count_vm_event(THP_FILE_FALLBACK); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1566 | return page; |
| 1567 | } |
| 1568 | |
| 1569 | static struct page *shmem_alloc_page(gfp_t gfp, |
| 1570 | struct shmem_inode_info *info, pgoff_t index) |
| 1571 | { |
| 1572 | struct vm_area_struct pvma; |
Charan Teja Reddy | 964220d | 2021-07-06 19:04:50 +0530 | [diff] [blame] | 1573 | struct page *page = NULL; |
| 1574 | |
| 1575 | trace_android_vh_shmem_alloc_page(&page); |
| 1576 | if (page) |
| 1577 | return page; |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1578 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1579 | shmem_pseudo_vma_init(&pvma, info, index); |
| 1580 | page = alloc_page_vma(gfp, &pvma, 0); |
| 1581 | shmem_pseudo_vma_destroy(&pvma); |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1582 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1583 | return page; |
| 1584 | } |
| 1585 | |
| 1586 | static struct page *shmem_alloc_and_acct_page(gfp_t gfp, |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1587 | struct inode *inode, |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1588 | pgoff_t index, bool huge) |
| 1589 | { |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1590 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1591 | struct page *page; |
| 1592 | int nr; |
| 1593 | int err = -ENOSPC; |
| 1594 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 1595 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1596 | huge = false; |
| 1597 | nr = huge ? HPAGE_PMD_NR : 1; |
| 1598 | |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1599 | if (!shmem_inode_acct_block(inode, nr)) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1600 | goto failed; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1601 | |
| 1602 | if (huge) |
| 1603 | page = shmem_alloc_hugepage(gfp, info, index); |
| 1604 | else |
| 1605 | page = shmem_alloc_page(gfp, info, index); |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1606 | if (page) { |
| 1607 | __SetPageLocked(page); |
| 1608 | __SetPageSwapBacked(page); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1609 | return page; |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1610 | } |
Mel Gorman | 18a2f37 | 2012-12-05 14:01:41 -0800 | [diff] [blame] | 1611 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1612 | err = -ENOMEM; |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 1613 | shmem_inode_unacct_blocks(inode, nr); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1614 | failed: |
| 1615 | return ERR_PTR(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 1617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | /* |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1619 | * When a page is moved from swapcache to shmem filecache (either by the |
| 1620 | * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of |
| 1621 | * shmem_unuse_inode()), it may have been read in earlier from swap, in |
| 1622 | * ignorance of the mapping it belongs to. If that mapping has special |
| 1623 | * constraints (like the gma500 GEM driver, which requires RAM below 4GB), |
| 1624 | * we may need to copy to a suitable page before moving to filecache. |
| 1625 | * |
| 1626 | * In a future release, this may well be extended to respect cpuset and |
| 1627 | * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); |
| 1628 | * but for now it is a simple matter of zone. |
| 1629 | */ |
| 1630 | static bool shmem_should_replace_page(struct page *page, gfp_t gfp) |
| 1631 | { |
| 1632 | return page_zonenum(page) > gfp_zone(gfp); |
| 1633 | } |
| 1634 | |
| 1635 | static int shmem_replace_page(struct page **pagep, gfp_t gfp, |
| 1636 | struct shmem_inode_info *info, pgoff_t index) |
| 1637 | { |
| 1638 | struct page *oldpage, *newpage; |
| 1639 | struct address_space *swap_mapping; |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame] | 1640 | swp_entry_t entry; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1641 | pgoff_t swap_index; |
| 1642 | int error; |
| 1643 | |
| 1644 | oldpage = *pagep; |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame] | 1645 | entry.val = page_private(oldpage); |
| 1646 | swap_index = swp_offset(entry); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1647 | swap_mapping = page_mapping(oldpage); |
| 1648 | |
| 1649 | /* |
| 1650 | * We have arrived here because our zones are constrained, so don't |
| 1651 | * limit chance of success by further cpuset and node constraints. |
| 1652 | */ |
| 1653 | gfp &= ~GFP_CONSTRAINT_MASK; |
| 1654 | newpage = shmem_alloc_page(gfp, info, index); |
| 1655 | if (!newpage) |
| 1656 | return -ENOMEM; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1657 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1658 | get_page(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1659 | copy_highpage(newpage, oldpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1660 | flush_dcache_page(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1661 | |
Hugh Dickins | 9956edf | 2016-11-10 10:46:11 -0800 | [diff] [blame] | 1662 | __SetPageLocked(newpage); |
| 1663 | __SetPageSwapBacked(newpage); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1664 | SetPageUptodate(newpage); |
Yu Zhao | c1cb20d | 2018-11-30 14:09:03 -0800 | [diff] [blame] | 1665 | set_page_private(newpage, entry.val); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1666 | SetPageSwapCache(newpage); |
| 1667 | |
| 1668 | /* |
| 1669 | * Our caller will very soon move newpage out of swapcache, but it's |
| 1670 | * a nice clean interface for us to replace oldpage by newpage there. |
| 1671 | */ |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1672 | xa_lock_irq(&swap_mapping->i_pages); |
Matthew Wilcox | 62f945b | 2017-11-17 10:22:37 -0500 | [diff] [blame] | 1673 | error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1674 | if (!error) { |
Johannes Weiner | 0d1c207 | 2020-06-03 16:01:54 -0700 | [diff] [blame] | 1675 | mem_cgroup_migrate(oldpage, newpage); |
| 1676 | __inc_lruvec_page_state(newpage, NR_FILE_PAGES); |
| 1677 | __dec_lruvec_page_state(oldpage, NR_FILE_PAGES); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1678 | } |
Matthew Wilcox | b93b016 | 2018-04-10 16:36:56 -0700 | [diff] [blame] | 1679 | xa_unlock_irq(&swap_mapping->i_pages); |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1680 | |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1681 | if (unlikely(error)) { |
| 1682 | /* |
| 1683 | * Is this possible? I think not, now that our callers check |
| 1684 | * both PageSwapCache and page_private after getting page lock; |
| 1685 | * but be defensive. Reverse old to newpage for clear and free. |
| 1686 | */ |
| 1687 | oldpage = newpage; |
| 1688 | } else { |
Johannes Weiner | 6058eae | 2020-06-03 16:02:40 -0700 | [diff] [blame] | 1689 | lru_cache_add(newpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1690 | *pagep = newpage; |
| 1691 | } |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1692 | |
| 1693 | ClearPageSwapCache(oldpage); |
| 1694 | set_page_private(oldpage, 0); |
| 1695 | |
| 1696 | unlock_page(oldpage); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1697 | put_page(oldpage); |
| 1698 | put_page(oldpage); |
Hugh Dickins | 0142ef6 | 2012-06-07 14:21:09 -0700 | [diff] [blame] | 1699 | return error; |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | /* |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1703 | * Swap in the page pointed to by *pagep. |
| 1704 | * Caller has to make sure that *pagep contains a valid swapped page. |
| 1705 | * Returns 0 and the page in pagep if success. On failure, returns the |
Randy Dunlap | af44c12 | 2020-08-11 18:33:17 -0700 | [diff] [blame] | 1706 | * error code and NULL in *pagep. |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1707 | */ |
| 1708 | static int shmem_swapin_page(struct inode *inode, pgoff_t index, |
| 1709 | struct page **pagep, enum sgp_type sgp, |
| 1710 | gfp_t gfp, struct vm_area_struct *vma, |
| 1711 | vm_fault_t *fault_type) |
| 1712 | { |
| 1713 | struct address_space *mapping = inode->i_mapping; |
| 1714 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 1715 | struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm; |
Greg Kroah-Hartman | 7d7d0e8 | 2021-07-22 15:46:08 +0200 | [diff] [blame] | 1716 | struct page *page; |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1717 | swp_entry_t swap; |
| 1718 | int error; |
| 1719 | |
| 1720 | VM_BUG_ON(!*pagep || !xa_is_value(*pagep)); |
| 1721 | swap = radix_to_swp_entry(*pagep); |
| 1722 | *pagep = NULL; |
| 1723 | |
| 1724 | /* Look it up and read it in.. */ |
| 1725 | page = lookup_swap_cache(swap, NULL, 0); |
| 1726 | if (!page) { |
| 1727 | /* Or update major stats only when swapin succeeds?? */ |
| 1728 | if (fault_type) { |
| 1729 | *fault_type |= VM_FAULT_MAJOR; |
| 1730 | count_vm_event(PGMAJFAULT); |
| 1731 | count_memcg_event_mm(charge_mm, PGMAJFAULT); |
| 1732 | } |
| 1733 | /* Here we actually start the io */ |
| 1734 | page = shmem_swapin(swap, gfp, info, index); |
| 1735 | if (!page) { |
| 1736 | error = -ENOMEM; |
| 1737 | goto failed; |
| 1738 | } |
| 1739 | } |
| 1740 | |
| 1741 | /* We have to do this with page locked to prevent races */ |
| 1742 | lock_page(page); |
| 1743 | if (!PageSwapCache(page) || page_private(page) != swap.val || |
| 1744 | !shmem_confirm_swap(mapping, index, swap)) { |
| 1745 | error = -EEXIST; |
| 1746 | goto unlock; |
| 1747 | } |
| 1748 | if (!PageUptodate(page)) { |
| 1749 | error = -EIO; |
| 1750 | goto failed; |
| 1751 | } |
| 1752 | wait_on_page_writeback(page); |
| 1753 | |
Steven Price | 8a84802 | 2020-05-13 16:37:49 +0100 | [diff] [blame] | 1754 | /* |
| 1755 | * Some architectures may have to restore extra metadata to the |
| 1756 | * physical page after reading from swap. |
| 1757 | */ |
| 1758 | arch_swap_restore(swap, page); |
| 1759 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1760 | if (shmem_should_replace_page(page, gfp)) { |
| 1761 | error = shmem_replace_page(&page, gfp, info, index); |
| 1762 | if (error) |
| 1763 | goto failed; |
| 1764 | } |
| 1765 | |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 1766 | error = shmem_add_to_page_cache(page, mapping, index, |
| 1767 | swp_to_radix_entry(swap), gfp, |
| 1768 | charge_mm); |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1769 | if (error) |
| 1770 | goto failed; |
| 1771 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1772 | spin_lock_irq(&info->lock); |
| 1773 | info->swapped--; |
| 1774 | shmem_recalc_inode(inode); |
| 1775 | spin_unlock_irq(&info->lock); |
| 1776 | |
| 1777 | if (sgp == SGP_WRITE) |
| 1778 | mark_page_accessed(page); |
| 1779 | |
| 1780 | delete_from_swap_cache(page); |
| 1781 | set_page_dirty(page); |
| 1782 | swap_free(swap); |
| 1783 | |
| 1784 | *pagep = page; |
| 1785 | return 0; |
| 1786 | failed: |
| 1787 | if (!shmem_confirm_swap(mapping, index, swap)) |
| 1788 | error = -EEXIST; |
| 1789 | unlock: |
| 1790 | if (page) { |
| 1791 | unlock_page(page); |
| 1792 | put_page(page); |
| 1793 | } |
| 1794 | |
| 1795 | return error; |
| 1796 | } |
| 1797 | |
| 1798 | /* |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 1799 | * shmem_getpage_gfp - find page in cache, or get from swap, or allocate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | * |
| 1801 | * If we allocate a new one we do not mark it dirty. That's up to the |
| 1802 | * vm. If we swap it in we mark it dirty since we also free the swap |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1803 | * entry since a page cannot live in both the swap and page cache. |
| 1804 | * |
Axel Rasmussen | 0c959cd | 2021-05-11 15:05:58 +1000 | [diff] [blame] | 1805 | * vma, vmf, and fault_type are only supplied by shmem_fault: |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 1806 | * otherwise they are NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | */ |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 1808 | static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1809 | struct page **pagep, enum sgp_type sgp, gfp_t gfp, |
Souptick Joarder | 2b74030 | 2018-08-23 17:01:36 -0700 | [diff] [blame] | 1810 | struct vm_area_struct *vma, struct vm_fault *vmf, |
| 1811 | vm_fault_t *fault_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | { |
| 1813 | struct address_space *mapping = inode->i_mapping; |
Arnd Bergmann | 23f919d | 2016-12-12 16:42:28 -0800 | [diff] [blame] | 1814 | struct shmem_inode_info *info = SHMEM_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | struct shmem_sb_info *sbinfo; |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 1816 | struct mm_struct *charge_mm; |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1817 | struct page *page; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1818 | enum sgp_type sgp_huge = sgp; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1819 | pgoff_t hindex = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | int error; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1821 | int once = 0; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1822 | int alloced = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1824 | if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | return -EFBIG; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 1826 | if (sgp == SGP_NOHUGE || sgp == SGP_HUGE) |
| 1827 | sgp = SGP_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | repeat: |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 1829 | if (sgp <= SGP_CACHE && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1830 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1831 | return -EINVAL; |
| 1832 | } |
| 1833 | |
| 1834 | sbinfo = SHMEM_SB(inode->i_sb); |
| 1835 | charge_mm = vma ? vma->vm_mm : current->mm; |
| 1836 | |
| 1837 | page = find_lock_entry(mapping, index); |
Axel Rasmussen | 0c959cd | 2021-05-11 15:05:58 +1000 | [diff] [blame] | 1838 | |
| 1839 | if (page && vma && userfaultfd_minor(vma)) { |
| 1840 | if (!xa_is_value(page)) { |
| 1841 | unlock_page(page); |
| 1842 | put_page(page); |
| 1843 | } |
| 1844 | *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1848 | if (xa_is_value(page)) { |
| 1849 | error = shmem_swapin_page(inode, index, &page, |
| 1850 | sgp, gfp, vma, fault_type); |
| 1851 | if (error == -EEXIST) |
| 1852 | goto repeat; |
| 1853 | |
| 1854 | *pagep = page; |
| 1855 | return error; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1856 | } |
| 1857 | |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 1858 | if (page) |
| 1859 | hindex = page->index; |
Hugh Dickins | 66d2f4d | 2014-07-02 15:22:38 -0700 | [diff] [blame] | 1860 | if (page && sgp == SGP_WRITE) |
| 1861 | mark_page_accessed(page); |
| 1862 | |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1863 | /* fallocated page? */ |
| 1864 | if (page && !PageUptodate(page)) { |
| 1865 | if (sgp != SGP_READ) |
| 1866 | goto clear; |
| 1867 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 1868 | put_page(page); |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1869 | page = NULL; |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 1870 | hindex = index; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1871 | } |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 1872 | if (page || sgp == SGP_READ) |
| 1873 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | |
| 1875 | /* |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 1876 | * Fast cache lookup did not find it: |
| 1877 | * bring it back from swap or allocate. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | */ |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 1879 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1880 | if (vma && userfaultfd_missing(vma)) { |
| 1881 | *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); |
| 1882 | return 0; |
| 1883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1885 | /* shmem_symlink() */ |
| 1886 | if (mapping->a_ops != &shmem_aops) |
| 1887 | goto alloc_nohuge; |
| 1888 | if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE) |
| 1889 | goto alloc_nohuge; |
| 1890 | if (shmem_huge == SHMEM_HUGE_FORCE) |
| 1891 | goto alloc_huge; |
| 1892 | switch (sbinfo->huge) { |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1893 | case SHMEM_HUGE_NEVER: |
| 1894 | goto alloc_nohuge; |
Kees Cook | 27d80fa2 | 2020-04-06 20:07:51 -0700 | [diff] [blame] | 1895 | case SHMEM_HUGE_WITHIN_SIZE: { |
| 1896 | loff_t i_size; |
| 1897 | pgoff_t off; |
| 1898 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1899 | off = round_up(index, HPAGE_PMD_NR); |
| 1900 | i_size = round_up(i_size_read(inode), PAGE_SIZE); |
| 1901 | if (i_size >= HPAGE_PMD_SIZE && |
| 1902 | i_size >> PAGE_SHIFT >= off) |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1903 | goto alloc_huge; |
Kees Cook | 27d80fa2 | 2020-04-06 20:07:51 -0700 | [diff] [blame] | 1904 | |
| 1905 | fallthrough; |
| 1906 | } |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1907 | case SHMEM_HUGE_ADVISE: |
| 1908 | if (sgp_huge == SGP_HUGE) |
| 1909 | goto alloc_huge; |
| 1910 | /* TODO: implement fadvise() hints */ |
| 1911 | goto alloc_nohuge; |
| 1912 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1914 | alloc_huge: |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1915 | page = shmem_alloc_and_acct_page(gfp, inode, index, true); |
| 1916 | if (IS_ERR(page)) { |
| 1917 | alloc_nohuge: |
| 1918 | page = shmem_alloc_and_acct_page(gfp, inode, |
| 1919 | index, false); |
| 1920 | } |
| 1921 | if (IS_ERR(page)) { |
| 1922 | int retry = 5; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1923 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1924 | error = PTR_ERR(page); |
| 1925 | page = NULL; |
| 1926 | if (error != -ENOSPC) |
| 1927 | goto unlock; |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 1928 | /* |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1929 | * Try to reclaim some space by splitting a huge page |
| 1930 | * beyond i_size on the filesystem. |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1931 | */ |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1932 | while (retry--) { |
| 1933 | int ret; |
| 1934 | |
| 1935 | ret = shmem_unused_huge_shrink(sbinfo, NULL, 1); |
| 1936 | if (ret == SHRINK_STOP) |
| 1937 | break; |
| 1938 | if (ret) |
| 1939 | goto alloc_nohuge; |
| 1940 | } |
| 1941 | goto unlock; |
| 1942 | } |
| 1943 | |
| 1944 | if (PageTransHuge(page)) |
| 1945 | hindex = round_down(index, HPAGE_PMD_NR); |
| 1946 | else |
| 1947 | hindex = index; |
| 1948 | |
| 1949 | if (sgp == SGP_WRITE) |
| 1950 | __SetPageReferenced(page); |
| 1951 | |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1952 | error = shmem_add_to_page_cache(page, mapping, hindex, |
Johannes Weiner | 3fea5a4 | 2020-06-03 16:01:41 -0700 | [diff] [blame] | 1953 | NULL, gfp & GFP_RECLAIM_MASK, |
| 1954 | charge_mm); |
| 1955 | if (error) |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1956 | goto unacct; |
Johannes Weiner | 6058eae | 2020-06-03 16:02:40 -0700 | [diff] [blame] | 1957 | lru_cache_add(page); |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1958 | |
| 1959 | spin_lock_irq(&info->lock); |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 1960 | info->alloced += compound_nr(page); |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1961 | inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page); |
| 1962 | shmem_recalc_inode(inode); |
| 1963 | spin_unlock_irq(&info->lock); |
| 1964 | alloced = true; |
| 1965 | |
| 1966 | if (PageTransHuge(page) && |
| 1967 | DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < |
| 1968 | hindex + HPAGE_PMD_NR - 1) { |
| 1969 | /* |
| 1970 | * Part of the huge page is beyond i_size: subject |
| 1971 | * to shrink under memory pressure. |
| 1972 | */ |
| 1973 | spin_lock(&sbinfo->shrinklist_lock); |
| 1974 | /* |
| 1975 | * _careful to defend against unlocked access to |
| 1976 | * ->shrink_list in shmem_unused_huge_shrink() |
| 1977 | */ |
| 1978 | if (list_empty_careful(&info->shrinklist)) { |
| 1979 | list_add_tail(&info->shrinklist, |
| 1980 | &sbinfo->shrinklist); |
| 1981 | sbinfo->shrinklist_len++; |
| 1982 | } |
| 1983 | spin_unlock(&sbinfo->shrinklist_lock); |
| 1984 | } |
| 1985 | |
| 1986 | /* |
| 1987 | * Let SGP_FALLOC use the SGP_WRITE optimization on a new page. |
| 1988 | */ |
| 1989 | if (sgp == SGP_FALLOC) |
| 1990 | sgp = SGP_WRITE; |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 1991 | clear: |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1992 | /* |
| 1993 | * Let SGP_WRITE caller clear ends if write does not fill page; |
| 1994 | * but SGP_FALLOC on a page fallocated earlier must initialize |
| 1995 | * it now, lest undo on failure cancel our earlier guarantee. |
| 1996 | */ |
| 1997 | if (sgp != SGP_WRITE && !PageUptodate(page)) { |
Vineeth Remanan Pillai | c5bf121 | 2019-03-05 15:46:58 -0800 | [diff] [blame] | 1998 | int i; |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 1999 | |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 2000 | for (i = 0; i < compound_nr(page); i++) { |
| 2001 | clear_highpage(page + i); |
| 2002 | flush_dcache_page(page + i); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2003 | } |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 2004 | SetPageUptodate(page); |
Hugh Dickins | 59a16ea | 2011-05-11 15:13:38 -0700 | [diff] [blame] | 2005 | } |
Hugh Dickins | bde05d1 | 2012-05-29 15:06:38 -0700 | [diff] [blame] | 2006 | |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2007 | /* Perhaps the file has been truncated since we checked */ |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2008 | if (sgp <= SGP_CACHE && |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2009 | ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 2010 | if (alloced) { |
| 2011 | ClearPageDirty(page); |
| 2012 | delete_from_page_cache(page); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2013 | spin_lock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 2014 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2015 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 2016 | } |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2017 | error = -EINVAL; |
Hugh Dickins | 267a4c7 | 2015-12-11 13:40:55 -0800 | [diff] [blame] | 2018 | goto unlock; |
Shaohua Li | ff36b801 | 2010-08-09 17:19:06 -0700 | [diff] [blame] | 2019 | } |
Matthew Wilcox (Oracle) | 63ec197 | 2020-10-13 16:51:38 -0700 | [diff] [blame] | 2020 | out: |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2021 | *pagep = page + index - hindex; |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2022 | return 0; |
Nick Piggin | d00806b | 2007-07-19 01:46:57 -0700 | [diff] [blame] | 2023 | |
Nick Piggin | d0217ac | 2007-07-19 01:47:03 -0700 | [diff] [blame] | 2024 | /* |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2025 | * Error recovery. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | */ |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2027 | unacct: |
Matthew Wilcox (Oracle) | d8c6546 | 2019-09-23 15:34:30 -0700 | [diff] [blame] | 2028 | shmem_inode_unacct_blocks(inode, compound_nr(page)); |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2029 | |
| 2030 | if (PageTransHuge(page)) { |
| 2031 | unlock_page(page); |
| 2032 | put_page(page); |
| 2033 | goto alloc_nohuge; |
| 2034 | } |
Hugh Dickins | d189922 | 2012-07-11 14:02:47 -0700 | [diff] [blame] | 2035 | unlock: |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 2036 | if (page) { |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2037 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2038 | put_page(page); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2039 | } |
| 2040 | if (error == -ENOSPC && !once++) { |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2041 | spin_lock_irq(&info->lock); |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2042 | shmem_recalc_inode(inode); |
Kirill A. Shutemov | 4595ef8 | 2016-07-26 15:26:29 -0700 | [diff] [blame] | 2043 | spin_unlock_irq(&info->lock); |
Hugh Dickins | 27ab700 | 2011-07-25 17:12:36 -0700 | [diff] [blame] | 2044 | goto repeat; |
Josef "Jeff" Sipek | d3ac7f8 | 2006-12-08 02:36:44 -0800 | [diff] [blame] | 2045 | } |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 2046 | if (error == -EEXIST) |
Hugh Dickins | 54af6042 | 2011-08-03 16:21:24 -0700 | [diff] [blame] | 2047 | goto repeat; |
| 2048 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
| 2050 | |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 2051 | /* |
| 2052 | * This is like autoremove_wake_function, but it removes the wait queue |
| 2053 | * entry unconditionally - even if something else had already woken the |
| 2054 | * target. |
| 2055 | */ |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 2056 | static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key) |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 2057 | { |
| 2058 | int ret = default_wake_function(wait, mode, sync, key); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 2059 | list_del_init(&wait->entry); |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 2060 | return ret; |
| 2061 | } |
| 2062 | |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 2063 | static vm_fault_t shmem_fault(struct vm_fault *vmf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 2065 | struct vm_area_struct *vma = vmf->vma; |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2066 | struct inode *inode = file_inode(vma->vm_file); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2067 | gfp_t gfp = mapping_gfp_mask(inode->i_mapping); |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 2068 | enum sgp_type sgp; |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 2069 | int err; |
| 2070 | vm_fault_t ret = VM_FAULT_LOCKED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2071 | |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2072 | /* |
| 2073 | * Trinity finds that probing a hole which tmpfs is punching can |
| 2074 | * prevent the hole-punch from ever completing: which in turn |
| 2075 | * locks writers out with its hold on i_mutex. So refrain from |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2076 | * faulting pages into the hole while it's being punched. Although |
| 2077 | * shmem_undo_range() does remove the additions, it may be unable to |
| 2078 | * keep up, as each new page needs its own unmap_mapping_range() call, |
| 2079 | * and the i_mmap tree grows ever slower to scan if new vmas are added. |
| 2080 | * |
| 2081 | * It does not matter if we sometimes reach this check just before the |
| 2082 | * hole-punch begins, so that one fault then races with the punch: |
| 2083 | * we just need to make racing faults a rare case. |
| 2084 | * |
| 2085 | * The implementation below would be much simpler if we just used a |
| 2086 | * standard mutex or completion: but we cannot take i_mutex in fault, |
| 2087 | * and bloating every shmem inode for this unlikely case would be sad. |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2088 | */ |
| 2089 | if (unlikely(inode->i_private)) { |
| 2090 | struct shmem_falloc *shmem_falloc; |
| 2091 | |
| 2092 | spin_lock(&inode->i_lock); |
| 2093 | shmem_falloc = inode->i_private; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2094 | if (shmem_falloc && |
| 2095 | shmem_falloc->waitq && |
| 2096 | vmf->pgoff >= shmem_falloc->start && |
| 2097 | vmf->pgoff < shmem_falloc->next) { |
Kirill A. Shutemov | 8897c1b | 2019-11-30 17:50:26 -0800 | [diff] [blame] | 2098 | struct file *fpin; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2099 | wait_queue_head_t *shmem_falloc_waitq; |
Linus Torvalds | 10d20bd | 2016-12-05 12:10:29 -0800 | [diff] [blame] | 2100 | DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2101 | |
| 2102 | ret = VM_FAULT_NOPAGE; |
Kirill A. Shutemov | 8897c1b | 2019-11-30 17:50:26 -0800 | [diff] [blame] | 2103 | fpin = maybe_unlock_mmap_for_io(vmf, NULL); |
| 2104 | if (fpin) |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2105 | ret = VM_FAULT_RETRY; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2106 | |
| 2107 | shmem_falloc_waitq = shmem_falloc->waitq; |
| 2108 | prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, |
| 2109 | TASK_UNINTERRUPTIBLE); |
| 2110 | spin_unlock(&inode->i_lock); |
| 2111 | schedule(); |
| 2112 | |
| 2113 | /* |
| 2114 | * shmem_falloc_waitq points into the shmem_fallocate() |
| 2115 | * stack of the hole-punching task: shmem_falloc_waitq |
| 2116 | * is usually invalid by the time we reach here, but |
| 2117 | * finish_wait() does not dereference it in that case; |
| 2118 | * though i_lock needed lest racing with wake_up_all(). |
| 2119 | */ |
| 2120 | spin_lock(&inode->i_lock); |
| 2121 | finish_wait(shmem_falloc_waitq, &shmem_fault_wait); |
| 2122 | spin_unlock(&inode->i_lock); |
Kirill A. Shutemov | 8897c1b | 2019-11-30 17:50:26 -0800 | [diff] [blame] | 2123 | |
| 2124 | if (fpin) |
| 2125 | fput(fpin); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2126 | return ret; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2127 | } |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2128 | spin_unlock(&inode->i_lock); |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2129 | } |
| 2130 | |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 2131 | sgp = SGP_CACHE; |
Michal Hocko | 1860033 | 2017-07-10 15:48:02 -0700 | [diff] [blame] | 2132 | |
| 2133 | if ((vma->vm_flags & VM_NOHUGEPAGE) || |
| 2134 | test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags)) |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 2135 | sgp = SGP_NOHUGE; |
Michal Hocko | 1860033 | 2017-07-10 15:48:02 -0700 | [diff] [blame] | 2136 | else if (vma->vm_flags & VM_HUGEPAGE) |
| 2137 | sgp = SGP_HUGE; |
Kirill A. Shutemov | 657e303 | 2016-07-26 15:26:21 -0700 | [diff] [blame] | 2138 | |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 2139 | err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 2140 | gfp, vma, vmf, &ret); |
Souptick Joarder | 20acce6 | 2018-06-07 17:09:17 -0700 | [diff] [blame] | 2141 | if (err) |
| 2142 | return vmf_error(err); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 2143 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2146 | unsigned long shmem_get_unmapped_area(struct file *file, |
| 2147 | unsigned long uaddr, unsigned long len, |
| 2148 | unsigned long pgoff, unsigned long flags) |
| 2149 | { |
| 2150 | unsigned long (*get_area)(struct file *, |
| 2151 | unsigned long, unsigned long, unsigned long, unsigned long); |
| 2152 | unsigned long addr; |
| 2153 | unsigned long offset; |
| 2154 | unsigned long inflated_len; |
| 2155 | unsigned long inflated_addr; |
| 2156 | unsigned long inflated_offset; |
| 2157 | |
| 2158 | if (len > TASK_SIZE) |
| 2159 | return -ENOMEM; |
| 2160 | |
| 2161 | get_area = current->mm->get_unmapped_area; |
| 2162 | addr = get_area(file, uaddr, len, pgoff, flags); |
| 2163 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 2164 | if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2165 | return addr; |
| 2166 | if (IS_ERR_VALUE(addr)) |
| 2167 | return addr; |
| 2168 | if (addr & ~PAGE_MASK) |
| 2169 | return addr; |
| 2170 | if (addr > TASK_SIZE - len) |
| 2171 | return addr; |
| 2172 | |
| 2173 | if (shmem_huge == SHMEM_HUGE_DENY) |
| 2174 | return addr; |
| 2175 | if (len < HPAGE_PMD_SIZE) |
| 2176 | return addr; |
| 2177 | if (flags & MAP_FIXED) |
| 2178 | return addr; |
| 2179 | /* |
| 2180 | * Our priority is to support MAP_SHARED mapped hugely; |
| 2181 | * and support MAP_PRIVATE mapped hugely too, until it is COWed. |
Kirill A. Shutemov | 9915899 | 2020-01-13 16:29:13 -0800 | [diff] [blame] | 2182 | * But if caller specified an address hint and we allocated area there |
| 2183 | * successfully, respect that as before. |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2184 | */ |
Kirill A. Shutemov | 9915899 | 2020-01-13 16:29:13 -0800 | [diff] [blame] | 2185 | if (uaddr == addr) |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2186 | return addr; |
| 2187 | |
| 2188 | if (shmem_huge != SHMEM_HUGE_FORCE) { |
| 2189 | struct super_block *sb; |
| 2190 | |
| 2191 | if (file) { |
| 2192 | VM_BUG_ON(file->f_op != &shmem_file_operations); |
| 2193 | sb = file_inode(file)->i_sb; |
| 2194 | } else { |
| 2195 | /* |
| 2196 | * Called directly from mm/mmap.c, or drivers/char/mem.c |
| 2197 | * for "/dev/zero", to create a shared anonymous object. |
| 2198 | */ |
| 2199 | if (IS_ERR(shm_mnt)) |
| 2200 | return addr; |
| 2201 | sb = shm_mnt->mnt_sb; |
| 2202 | } |
Toshi Kani | 3089bf6 | 2016-09-23 20:21:56 -0700 | [diff] [blame] | 2203 | if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER) |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2204 | return addr; |
| 2205 | } |
| 2206 | |
| 2207 | offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1); |
| 2208 | if (offset && offset + len < 2 * HPAGE_PMD_SIZE) |
| 2209 | return addr; |
| 2210 | if ((addr & (HPAGE_PMD_SIZE-1)) == offset) |
| 2211 | return addr; |
| 2212 | |
| 2213 | inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE; |
| 2214 | if (inflated_len > TASK_SIZE) |
| 2215 | return addr; |
| 2216 | if (inflated_len < len) |
| 2217 | return addr; |
| 2218 | |
Kirill A. Shutemov | 9915899 | 2020-01-13 16:29:13 -0800 | [diff] [blame] | 2219 | inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags); |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 2220 | if (IS_ERR_VALUE(inflated_addr)) |
| 2221 | return addr; |
| 2222 | if (inflated_addr & ~PAGE_MASK) |
| 2223 | return addr; |
| 2224 | |
| 2225 | inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1); |
| 2226 | inflated_addr += offset - inflated_offset; |
| 2227 | if (inflated_offset > offset) |
| 2228 | inflated_addr += HPAGE_PMD_SIZE; |
| 2229 | |
| 2230 | if (inflated_addr > TASK_SIZE - len) |
| 2231 | return addr; |
| 2232 | return inflated_addr; |
| 2233 | } |
| 2234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | #ifdef CONFIG_NUMA |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2236 | static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2238 | struct inode *inode = file_inode(vma->vm_file); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2239 | return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
Adrian Bunk | d8dc74f | 2007-10-16 01:26:26 -0700 | [diff] [blame] | 2242 | static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, |
| 2243 | unsigned long addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2245 | struct inode *inode = file_inode(vma->vm_file); |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2246 | pgoff_t index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2247 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2248 | index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; |
| 2249 | return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2250 | } |
| 2251 | #endif |
| 2252 | |
| 2253 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 2254 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2255 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 2257 | int retval = -ENOMEM; |
| 2258 | |
Hugh Dickins | ea0dfeb | 2020-04-20 18:14:14 -0700 | [diff] [blame] | 2259 | /* |
| 2260 | * What serializes the accesses to info->flags? |
| 2261 | * ipc_lock_object() when called from shmctl_do_lock(), |
| 2262 | * no serialization needed when called from shm_destroy(). |
| 2263 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | if (lock && !(info->flags & VM_LOCKED)) { |
| 2265 | if (!user_shm_lock(inode->i_size, user)) |
| 2266 | goto out_nomem; |
| 2267 | info->flags |= VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2268 | mapping_set_unevictable(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2269 | } |
| 2270 | if (!lock && (info->flags & VM_LOCKED) && user) { |
| 2271 | user_shm_unlock(inode->i_size, user); |
| 2272 | info->flags &= ~VM_LOCKED; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2273 | mapping_clear_unevictable(file->f_mapping); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | } |
| 2275 | retval = 0; |
Lee Schermerhorn | 89e004ea | 2008-10-18 20:26:43 -0700 | [diff] [blame] | 2276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | out_nomem: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2278 | return retval; |
| 2279 | } |
| 2280 | |
Adrian Bunk | 9b83a6a | 2007-02-28 20:11:03 -0800 | [diff] [blame] | 2281 | static int shmem_mmap(struct file *file, struct vm_area_struct *vma) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | { |
Joel Fernandes (Google) | ab3948f | 2019-03-05 15:47:54 -0800 | [diff] [blame] | 2283 | struct shmem_inode_info *info = SHMEM_I(file_inode(file)); |
Peter Xu | 0148686 | 2021-05-14 17:27:04 -0700 | [diff] [blame] | 2284 | int ret; |
Joel Fernandes (Google) | ab3948f | 2019-03-05 15:47:54 -0800 | [diff] [blame] | 2285 | |
Peter Xu | 0148686 | 2021-05-14 17:27:04 -0700 | [diff] [blame] | 2286 | ret = seal_check_future_write(info->seals, vma); |
| 2287 | if (ret) |
| 2288 | return ret; |
Joel Fernandes (Google) | ab3948f | 2019-03-05 15:47:54 -0800 | [diff] [blame] | 2289 | |
Catalin Marinas | 51b0bff | 2019-11-29 12:45:08 +0000 | [diff] [blame] | 2290 | /* arm64 - allow memory tagging on RAM-based files */ |
| 2291 | vma->vm_flags |= VM_MTE_ALLOWED; |
| 2292 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | file_accessed(file); |
| 2294 | vma->vm_ops = &shmem_vm_ops; |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 2295 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 2296 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
| 2297 | (vma->vm_end & HPAGE_PMD_MASK)) { |
| 2298 | khugepaged_enter(vma, vma->vm_flags); |
| 2299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | return 0; |
| 2301 | } |
| 2302 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2303 | static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir, |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 2304 | umode_t mode, dev_t dev, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | { |
| 2306 | struct inode *inode; |
| 2307 | struct shmem_inode_info *info; |
| 2308 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 2309 | ino_t ino; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 2311 | if (shmem_reserve_inode(sb, &ino)) |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2312 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | |
| 2314 | inode = new_inode(sb); |
| 2315 | if (inode) { |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 2316 | inode->i_ino = ino; |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2317 | inode_init_owner(inode, dir, mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | inode->i_blocks = 0; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2319 | inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode); |
Arnd Bergmann | 46c9a94 | 2018-08-17 15:45:09 -0700 | [diff] [blame] | 2320 | inode->i_generation = prandom_u32(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | info = SHMEM_I(inode); |
| 2322 | memset(info, 0, (char *)inode - (char *)info); |
| 2323 | spin_lock_init(&info->lock); |
Hugh Dickins | af53d3e | 2019-04-18 17:50:13 -0700 | [diff] [blame] | 2324 | atomic_set(&info->stop_eviction, 0); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2325 | info->seals = F_SEAL_SEAL; |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2326 | info->flags = flags & VM_NORESERVE; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 2327 | INIT_LIST_HEAD(&info->shrinklist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | INIT_LIST_HEAD(&info->swaplist); |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 2329 | simple_xattrs_init(&info->xattrs); |
Al Viro | 72c0490 | 2009-06-24 16:58:48 -0400 | [diff] [blame] | 2330 | cache_no_acl(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | |
| 2332 | switch (mode & S_IFMT) { |
| 2333 | default: |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 2334 | inode->i_op = &shmem_special_inode_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | init_special_inode(inode, mode, dev); |
| 2336 | break; |
| 2337 | case S_IFREG: |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 2338 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | inode->i_op = &shmem_inode_operations; |
| 2340 | inode->i_fop = &shmem_file_operations; |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 2341 | mpol_shared_policy_init(&info->policy, |
| 2342 | shmem_get_sbmpol(sbinfo)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2343 | break; |
| 2344 | case S_IFDIR: |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2345 | inc_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | /* Some things misbehave if size == 0 on a directory */ |
| 2347 | inode->i_size = 2 * BOGO_DIRENT_SIZE; |
| 2348 | inode->i_op = &shmem_dir_inode_operations; |
| 2349 | inode->i_fop = &simple_dir_operations; |
| 2350 | break; |
| 2351 | case S_IFLNK: |
| 2352 | /* |
| 2353 | * Must not load anything in the rbtree, |
| 2354 | * mpol_free_shared_policy will not be called. |
| 2355 | */ |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 2356 | mpol_shared_policy_init(&info->policy, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | break; |
| 2358 | } |
Joel Fernandes (Google) | b45d71f | 2018-09-20 12:22:39 -0700 | [diff] [blame] | 2359 | |
| 2360 | lockdep_annotate_inode_mutex_key(inode); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2361 | } else |
| 2362 | shmem_free_inode(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | return inode; |
| 2364 | } |
| 2365 | |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2366 | bool shmem_mapping(struct address_space *mapping) |
| 2367 | { |
Hugh Dickins | f800545 | 2017-02-22 15:41:42 -0800 | [diff] [blame] | 2368 | return mapping->a_ops == &shmem_aops; |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2371 | #ifdef CONFIG_USERFAULTFD |
| 2372 | int shmem_mfill_atomic_pte(struct mm_struct *dst_mm, |
| 2373 | pmd_t *dst_pmd, |
| 2374 | struct vm_area_struct *dst_vma, |
| 2375 | unsigned long dst_addr, |
| 2376 | unsigned long src_addr, |
| 2377 | bool zeropage, |
| 2378 | struct page **pagep) |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2379 | { |
| 2380 | struct inode *inode = file_inode(dst_vma->vm_file); |
| 2381 | struct shmem_inode_info *info = SHMEM_I(inode); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2382 | struct address_space *mapping = inode->i_mapping; |
| 2383 | gfp_t gfp = mapping_gfp_mask(mapping); |
| 2384 | pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2385 | void *page_kaddr; |
| 2386 | struct page *page; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2387 | int ret; |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2388 | pgoff_t max_off; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2389 | |
Axel Rasmussen | 140cfd9 | 2021-05-14 17:27:19 -0700 | [diff] [blame] | 2390 | if (!shmem_inode_acct_block(inode, 1)) { |
| 2391 | /* |
| 2392 | * We may have got a page, returned -ENOENT triggering a retry, |
| 2393 | * and now we find ourselves with -ENOMEM. Release the page, to |
| 2394 | * avoid a BUG_ON in our caller. |
| 2395 | */ |
| 2396 | if (unlikely(*pagep)) { |
| 2397 | put_page(*pagep); |
| 2398 | *pagep = NULL; |
| 2399 | } |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2400 | return -ENOMEM; |
Axel Rasmussen | 140cfd9 | 2021-05-14 17:27:19 -0700 | [diff] [blame] | 2401 | } |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2402 | |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 2403 | if (!*pagep) { |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2404 | ret = -ENOMEM; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2405 | page = shmem_alloc_page(gfp, info, pgoff); |
| 2406 | if (!page) |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 2407 | goto out_unacct_blocks; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2408 | |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2409 | if (!zeropage) { /* COPY */ |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2410 | page_kaddr = kmap_atomic(page); |
| 2411 | ret = copy_from_user(page_kaddr, |
| 2412 | (const void __user *)src_addr, |
| 2413 | PAGE_SIZE); |
| 2414 | kunmap_atomic(page_kaddr); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2415 | |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 2416 | /* fallback to copy_from_user outside mmap_lock */ |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2417 | if (unlikely(ret)) { |
| 2418 | *pagep = page; |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2419 | ret = -ENOENT; |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2420 | /* don't free the page */ |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2421 | goto out_unacct_blocks; |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2422 | } |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2423 | } else { /* ZEROPAGE */ |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2424 | clear_highpage(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2425 | } |
| 2426 | } else { |
| 2427 | page = *pagep; |
| 2428 | *pagep = NULL; |
| 2429 | } |
| 2430 | |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2431 | VM_BUG_ON(PageLocked(page)); |
| 2432 | VM_BUG_ON(PageSwapBacked(page)); |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 2433 | __SetPageLocked(page); |
| 2434 | __SetPageSwapBacked(page); |
| 2435 | __SetPageUptodate(page); |
Andrea Arcangeli | 9cc90c6 | 2017-02-22 15:43:49 -0800 | [diff] [blame] | 2436 | |
Andrea Arcangeli | e2a50c1 | 2018-11-30 14:09:37 -0800 | [diff] [blame] | 2437 | ret = -EFAULT; |
Andrea Arcangeli | e2a50c1 | 2018-11-30 14:09:37 -0800 | [diff] [blame] | 2438 | max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2439 | if (unlikely(pgoff >= max_off)) |
Andrea Arcangeli | e2a50c1 | 2018-11-30 14:09:37 -0800 | [diff] [blame] | 2440 | goto out_release; |
| 2441 | |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 2442 | ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL, |
| 2443 | gfp & GFP_RECLAIM_MASK, dst_mm); |
| 2444 | if (ret) |
| 2445 | goto out_release; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2446 | |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2447 | ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr, |
| 2448 | page, true, false); |
| 2449 | if (ret) |
| 2450 | goto out_delete_from_cache; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2451 | |
Lokesh Gidra | 84330a5 | 2021-05-13 05:11:10 -0700 | [diff] [blame] | 2452 | spin_lock_irq(&info->lock); |
| 2453 | info->alloced++; |
| 2454 | inode->i_blocks += BLOCKS_PER_PAGE; |
| 2455 | shmem_recalc_inode(inode); |
| 2456 | spin_unlock_irq(&info->lock); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2457 | |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2458 | SetPageDirty(page); |
Andrea Arcangeli | e2a50c1 | 2018-11-30 14:09:37 -0800 | [diff] [blame] | 2459 | unlock_page(page); |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2460 | return 0; |
| 2461 | out_delete_from_cache: |
Andrea Arcangeli | e2a50c1 | 2018-11-30 14:09:37 -0800 | [diff] [blame] | 2462 | delete_from_page_cache(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2463 | out_release: |
Andrea Arcangeli | 9cc90c6 | 2017-02-22 15:43:49 -0800 | [diff] [blame] | 2464 | unlock_page(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2465 | put_page(page); |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2466 | out_unacct_blocks: |
Mike Rapoport | 0f07969 | 2017-09-06 16:22:59 -0700 | [diff] [blame] | 2467 | shmem_inode_unacct_blocks(inode, 1); |
Axel Rasmussen | 0b161cf | 2021-05-11 15:05:59 +1000 | [diff] [blame] | 2468 | return ret; |
Mike Rapoport | 4c27fe4 | 2017-02-22 15:43:25 -0800 | [diff] [blame] | 2469 | } |
Axel Rasmussen | 5f6dc07 | 2021-05-11 15:05:57 +1000 | [diff] [blame] | 2470 | #endif /* CONFIG_USERFAULTFD */ |
Mike Rapoport | 8d10396 | 2017-09-06 16:23:02 -0700 | [diff] [blame] | 2471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | #ifdef CONFIG_TMPFS |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 2473 | static const struct inode_operations shmem_symlink_inode_operations; |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 2474 | static const struct inode_operations shmem_short_symlink_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 2476 | #ifdef CONFIG_TMPFS_XATTR |
| 2477 | static int shmem_initxattrs(struct inode *, const struct xattr *, void *); |
| 2478 | #else |
| 2479 | #define shmem_initxattrs NULL |
| 2480 | #endif |
| 2481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | static int |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2483 | shmem_write_begin(struct file *file, struct address_space *mapping, |
| 2484 | loff_t pos, unsigned len, unsigned flags, |
| 2485 | struct page **pagep, void **fsdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | { |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2487 | struct inode *inode = mapping->host; |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2488 | struct shmem_inode_info *info = SHMEM_I(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2489 | pgoff_t index = pos >> PAGE_SHIFT; |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2490 | |
| 2491 | /* i_mutex is held by caller */ |
Joel Fernandes (Google) | ab3948f | 2019-03-05 15:47:54 -0800 | [diff] [blame] | 2492 | if (unlikely(info->seals & (F_SEAL_GROW | |
| 2493 | F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { |
| 2494 | if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2495 | return -EPERM; |
| 2496 | if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) |
| 2497 | return -EPERM; |
| 2498 | } |
| 2499 | |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2500 | return shmem_getpage(inode, index, pagep, SGP_WRITE); |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | static int |
| 2504 | shmem_write_end(struct file *file, struct address_space *mapping, |
| 2505 | loff_t pos, unsigned len, unsigned copied, |
| 2506 | struct page *page, void *fsdata) |
| 2507 | { |
| 2508 | struct inode *inode = mapping->host; |
| 2509 | |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2510 | if (pos + copied > inode->i_size) |
| 2511 | i_size_write(inode, pos + copied); |
| 2512 | |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2513 | if (!PageUptodate(page)) { |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2514 | struct page *head = compound_head(page); |
| 2515 | if (PageTransCompound(page)) { |
| 2516 | int i; |
| 2517 | |
| 2518 | for (i = 0; i < HPAGE_PMD_NR; i++) { |
| 2519 | if (head + i == page) |
| 2520 | continue; |
| 2521 | clear_highpage(head + i); |
| 2522 | flush_dcache_page(head + i); |
| 2523 | } |
| 2524 | } |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2525 | if (copied < PAGE_SIZE) { |
| 2526 | unsigned from = pos & (PAGE_SIZE - 1); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2527 | zero_user_segments(page, 0, from, |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2528 | from + copied, PAGE_SIZE); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2529 | } |
Kirill A. Shutemov | 800d8c6 | 2016-07-26 15:26:18 -0700 | [diff] [blame] | 2530 | SetPageUptodate(head); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 2531 | } |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2532 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2533 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2534 | put_page(page); |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2535 | |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 2536 | return copied; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | } |
| 2538 | |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2539 | static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | { |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2541 | struct file *file = iocb->ki_filp; |
| 2542 | struct inode *inode = file_inode(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | struct address_space *mapping = inode->i_mapping; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2544 | pgoff_t index; |
| 2545 | unsigned long offset; |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 2546 | enum sgp_type sgp = SGP_READ; |
Geert Uytterhoeven | f7c1d07 | 2014-04-13 20:46:22 +0200 | [diff] [blame] | 2547 | int error = 0; |
Al Viro | cb66a7a | 2014-03-04 15:24:06 -0500 | [diff] [blame] | 2548 | ssize_t retval = 0; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2549 | loff_t *ppos = &iocb->ki_pos; |
Hugh Dickins | a0ee5ec | 2008-02-04 22:28:51 -0800 | [diff] [blame] | 2550 | |
| 2551 | /* |
| 2552 | * Might this read be for a stacking filesystem? Then when reading |
| 2553 | * holes of a sparse file, we actually need to allocate those pages, |
| 2554 | * and even mark them dirty, so it cannot exceed the max_blocks limit. |
| 2555 | */ |
Al Viro | 777eda2 | 2014-12-17 04:46:46 -0500 | [diff] [blame] | 2556 | if (!iter_is_iovec(to)) |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2557 | sgp = SGP_CACHE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2559 | index = *ppos >> PAGE_SHIFT; |
| 2560 | offset = *ppos & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | |
| 2562 | for (;;) { |
| 2563 | struct page *page = NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2564 | pgoff_t end_index; |
| 2565 | unsigned long nr, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | loff_t i_size = i_size_read(inode); |
| 2567 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2568 | end_index = i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | if (index > end_index) |
| 2570 | break; |
| 2571 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2572 | nr = i_size & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2573 | if (nr <= offset) |
| 2574 | break; |
| 2575 | } |
| 2576 | |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2577 | error = shmem_getpage(inode, index, &page, sgp); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2578 | if (error) { |
| 2579 | if (error == -EINVAL) |
| 2580 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | break; |
| 2582 | } |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2583 | if (page) { |
| 2584 | if (sgp == SGP_CACHE) |
| 2585 | set_page_dirty(page); |
Hugh Dickins | d360244 | 2008-02-04 22:28:44 -0800 | [diff] [blame] | 2586 | unlock_page(page); |
Hugh Dickins | 75edd34 | 2016-05-19 17:12:44 -0700 | [diff] [blame] | 2587 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | |
| 2589 | /* |
| 2590 | * We must evaluate after, since reads (unlike writes) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2591 | * are called without i_mutex protection against truncate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | */ |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2593 | nr = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | i_size = i_size_read(inode); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2595 | end_index = i_size >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | if (index == end_index) { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2597 | nr = i_size & ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | if (nr <= offset) { |
| 2599 | if (page) |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2600 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | break; |
| 2602 | } |
| 2603 | } |
| 2604 | nr -= offset; |
| 2605 | |
| 2606 | if (page) { |
| 2607 | /* |
| 2608 | * If users can be writing to this page using arbitrary |
| 2609 | * virtual addresses, take care about potential aliasing |
| 2610 | * before reading the page on the kernel side. |
| 2611 | */ |
| 2612 | if (mapping_writably_mapped(mapping)) |
| 2613 | flush_dcache_page(page); |
| 2614 | /* |
| 2615 | * Mark the page accessed if we read the beginning. |
| 2616 | */ |
| 2617 | if (!offset) |
| 2618 | mark_page_accessed(page); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 2619 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | page = ZERO_PAGE(0); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2621 | get_page(page); |
Nick Piggin | b581003 | 2005-10-29 18:16:12 -0700 | [diff] [blame] | 2622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | |
| 2624 | /* |
| 2625 | * Ok, we have the page, and it's up-to-date, so |
| 2626 | * now we can copy it to user space... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | */ |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2628 | ret = copy_page_to_iter(page, offset, nr, to); |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2629 | retval += ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2630 | offset += ret; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2631 | index += offset >> PAGE_SHIFT; |
| 2632 | offset &= ~PAGE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2634 | put_page(page); |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 2635 | if (!iov_iter_count(to)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | break; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2637 | if (ret < nr) { |
| 2638 | error = -EFAULT; |
| 2639 | break; |
| 2640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | cond_resched(); |
| 2642 | } |
| 2643 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2644 | *ppos = ((loff_t) index << PAGE_SHIFT) + offset; |
Al Viro | 6e58e79 | 2014-02-03 17:07:03 -0500 | [diff] [blame] | 2645 | file_accessed(file); |
| 2646 | return retval ? retval : error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | } |
| 2648 | |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2649 | /* |
Matthew Wilcox | 7f4446e | 2017-12-04 03:31:13 -0500 | [diff] [blame] | 2650 | * llseek SEEK_DATA or SEEK_HOLE through the page cache. |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2651 | */ |
| 2652 | static pgoff_t shmem_seek_hole_data(struct address_space *mapping, |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2653 | pgoff_t index, pgoff_t end, int whence) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2654 | { |
| 2655 | struct page *page; |
| 2656 | struct pagevec pvec; |
| 2657 | pgoff_t indices[PAGEVEC_SIZE]; |
| 2658 | bool done = false; |
| 2659 | int i; |
| 2660 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 2661 | pagevec_init(&pvec); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2662 | pvec.nr = 1; /* start small: we may be there already */ |
| 2663 | while (!done) { |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2664 | pvec.nr = find_get_entries(mapping, index, |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2665 | pvec.nr, pvec.pages, indices); |
| 2666 | if (!pvec.nr) { |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2667 | if (whence == SEEK_DATA) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2668 | index = end; |
| 2669 | break; |
| 2670 | } |
| 2671 | for (i = 0; i < pvec.nr; i++, index++) { |
| 2672 | if (index < indices[i]) { |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2673 | if (whence == SEEK_HOLE) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2674 | done = true; |
| 2675 | break; |
| 2676 | } |
| 2677 | index = indices[i]; |
| 2678 | } |
| 2679 | page = pvec.pages[i]; |
Matthew Wilcox | 3159f94 | 2017-11-03 13:30:42 -0400 | [diff] [blame] | 2680 | if (page && !xa_is_value(page)) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2681 | if (!PageUptodate(page)) |
| 2682 | page = NULL; |
| 2683 | } |
| 2684 | if (index >= end || |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2685 | (page && whence == SEEK_DATA) || |
| 2686 | (!page && whence == SEEK_HOLE)) { |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2687 | done = true; |
| 2688 | break; |
| 2689 | } |
| 2690 | } |
Johannes Weiner | 0cd6144 | 2014-04-03 14:47:46 -0700 | [diff] [blame] | 2691 | pagevec_remove_exceptionals(&pvec); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2692 | pagevec_release(&pvec); |
| 2693 | pvec.nr = PAGEVEC_SIZE; |
| 2694 | cond_resched(); |
| 2695 | } |
| 2696 | return index; |
| 2697 | } |
| 2698 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2699 | static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2700 | { |
| 2701 | struct address_space *mapping = file->f_mapping; |
| 2702 | struct inode *inode = mapping->host; |
| 2703 | pgoff_t start, end; |
| 2704 | loff_t new_offset; |
| 2705 | |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2706 | if (whence != SEEK_DATA && whence != SEEK_HOLE) |
| 2707 | return generic_file_llseek_size(file, offset, whence, |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2708 | MAX_LFS_FILESIZE, i_size_read(inode)); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2709 | inode_lock(inode); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2710 | /* We're holding i_mutex so we can access i_size directly */ |
| 2711 | |
Yufen Yu | 1a41364 | 2018-11-16 15:08:39 -0800 | [diff] [blame] | 2712 | if (offset < 0 || offset >= inode->i_size) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2713 | offset = -ENXIO; |
| 2714 | else { |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2715 | start = offset >> PAGE_SHIFT; |
| 2716 | end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2717 | new_offset = shmem_seek_hole_data(mapping, start, end, whence); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2718 | new_offset <<= PAGE_SHIFT; |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2719 | if (new_offset > offset) { |
| 2720 | if (new_offset < inode->i_size) |
| 2721 | offset = new_offset; |
Andrew Morton | 965c8e5 | 2012-12-17 15:59:39 -0800 | [diff] [blame] | 2722 | else if (whence == SEEK_DATA) |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2723 | offset = -ENXIO; |
| 2724 | else |
| 2725 | offset = inode->i_size; |
| 2726 | } |
| 2727 | } |
| 2728 | |
Hugh Dickins | 387aae6 | 2013-08-04 11:30:25 -0700 | [diff] [blame] | 2729 | if (offset >= 0) |
| 2730 | offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2731 | inode_unlock(inode); |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 2732 | return offset; |
| 2733 | } |
| 2734 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2735 | static long shmem_fallocate(struct file *file, int mode, loff_t offset, |
| 2736 | loff_t len) |
| 2737 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 2738 | struct inode *inode = file_inode(file); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2739 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2740 | struct shmem_inode_info *info = SHMEM_I(inode); |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2741 | struct shmem_falloc shmem_falloc; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2742 | pgoff_t start, index, end; |
| 2743 | int error; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2744 | |
Hugh Dickins | 13ace4d | 2014-06-23 13:22:03 -0700 | [diff] [blame] | 2745 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
| 2746 | return -EOPNOTSUPP; |
| 2747 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2748 | inode_lock(inode); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2749 | |
| 2750 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
| 2751 | struct address_space *mapping = file->f_mapping; |
| 2752 | loff_t unmap_start = round_up(offset, PAGE_SIZE); |
| 2753 | loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2754 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2755 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2756 | /* protected by i_mutex */ |
Joel Fernandes (Google) | ab3948f | 2019-03-05 15:47:54 -0800 | [diff] [blame] | 2757 | if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2758 | error = -EPERM; |
| 2759 | goto out; |
| 2760 | } |
| 2761 | |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2762 | shmem_falloc.waitq = &shmem_falloc_waitq; |
Chen Jun | aa71ecd | 2019-11-30 17:58:11 -0800 | [diff] [blame] | 2763 | shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; |
Hugh Dickins | f00cdc6 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2764 | shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; |
| 2765 | spin_lock(&inode->i_lock); |
| 2766 | inode->i_private = &shmem_falloc; |
| 2767 | spin_unlock(&inode->i_lock); |
| 2768 | |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2769 | if ((u64)unmap_end > (u64)unmap_start) |
| 2770 | unmap_mapping_range(mapping, unmap_start, |
| 2771 | 1 + unmap_end - unmap_start, 0); |
| 2772 | shmem_truncate_range(inode, offset, offset + len - 1); |
| 2773 | /* No need to unmap again: hole-punching leaves COWed pages */ |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2774 | |
| 2775 | spin_lock(&inode->i_lock); |
| 2776 | inode->i_private = NULL; |
| 2777 | wake_up_all(&shmem_falloc_waitq); |
Ingo Molnar | 2055da9 | 2017-06-20 12:06:46 +0200 | [diff] [blame] | 2778 | WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2779 | spin_unlock(&inode->i_lock); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2780 | error = 0; |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2781 | goto out; |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2784 | /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ |
| 2785 | error = inode_newsize_ok(inode, offset + len); |
| 2786 | if (error) |
| 2787 | goto out; |
| 2788 | |
David Herrmann | 40e041a | 2014-08-08 14:25:27 -0700 | [diff] [blame] | 2789 | if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { |
| 2790 | error = -EPERM; |
| 2791 | goto out; |
| 2792 | } |
| 2793 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2794 | start = offset >> PAGE_SHIFT; |
| 2795 | end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2796 | /* Try to avoid a swapstorm if len is impossible to satisfy */ |
| 2797 | if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { |
| 2798 | error = -ENOSPC; |
| 2799 | goto out; |
| 2800 | } |
| 2801 | |
Hugh Dickins | 8e205f7 | 2014-07-23 14:00:10 -0700 | [diff] [blame] | 2802 | shmem_falloc.waitq = NULL; |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2803 | shmem_falloc.start = start; |
| 2804 | shmem_falloc.next = start; |
| 2805 | shmem_falloc.nr_falloced = 0; |
| 2806 | shmem_falloc.nr_unswapped = 0; |
| 2807 | spin_lock(&inode->i_lock); |
| 2808 | inode->i_private = &shmem_falloc; |
| 2809 | spin_unlock(&inode->i_lock); |
| 2810 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2811 | for (index = start; index < end; index++) { |
| 2812 | struct page *page; |
| 2813 | |
| 2814 | /* |
| 2815 | * Good, the fallocate(2) manpage permits EINTR: we may have |
| 2816 | * been interrupted because we are using up too much memory. |
| 2817 | */ |
| 2818 | if (signal_pending(current)) |
| 2819 | error = -EINTR; |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2820 | else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) |
| 2821 | error = -ENOMEM; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2822 | else |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 2823 | error = shmem_getpage(inode, index, &page, SGP_FALLOC); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2824 | if (error) { |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2825 | /* Remove the !PageUptodate pages we added */ |
Hugh Dickins | 7f55656 | 2016-07-10 16:46:32 -0700 | [diff] [blame] | 2826 | if (index > start) { |
| 2827 | shmem_undo_range(inode, |
| 2828 | (loff_t)start << PAGE_SHIFT, |
| 2829 | ((loff_t)index << PAGE_SHIFT) - 1, true); |
| 2830 | } |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2831 | goto undone; |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2832 | } |
| 2833 | |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2834 | /* |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2835 | * Inform shmem_writepage() how far we have reached. |
| 2836 | * No need for lock or barrier: we have the page lock. |
| 2837 | */ |
| 2838 | shmem_falloc.next++; |
| 2839 | if (!PageUptodate(page)) |
| 2840 | shmem_falloc.nr_falloced++; |
| 2841 | |
| 2842 | /* |
Hugh Dickins | 1635f6a | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2843 | * If !PageUptodate, leave it that way so that freeable pages |
| 2844 | * can be recognized if we need to rollback on error later. |
| 2845 | * But set_page_dirty so that memory pressure will swap rather |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2846 | * than free the pages we are allocating (and SGP_CACHE pages |
| 2847 | * might still be clean: we now need to mark those dirty too). |
| 2848 | */ |
| 2849 | set_page_dirty(page); |
| 2850 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2851 | put_page(page); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2852 | cond_resched(); |
| 2853 | } |
| 2854 | |
| 2855 | if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) |
| 2856 | i_size_write(inode, offset + len); |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2857 | inode->i_ctime = current_time(inode); |
Hugh Dickins | 1aac140 | 2012-05-29 15:06:42 -0700 | [diff] [blame] | 2858 | undone: |
| 2859 | spin_lock(&inode->i_lock); |
| 2860 | inode->i_private = NULL; |
| 2861 | spin_unlock(&inode->i_lock); |
Hugh Dickins | e2d12e2 | 2012-05-29 15:06:41 -0700 | [diff] [blame] | 2862 | out: |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 2863 | inode_unlock(inode); |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 2864 | return error; |
| 2865 | } |
| 2866 | |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2867 | static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2868 | { |
David Howells | 726c334 | 2006-06-23 02:02:58 -0700 | [diff] [blame] | 2869 | struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | |
| 2871 | buf->f_type = TMPFS_MAGIC; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 2872 | buf->f_bsize = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | buf->f_namelen = NAME_MAX; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2874 | if (sbinfo->max_blocks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | buf->f_blocks = sbinfo->max_blocks; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 2876 | buf->f_bavail = |
| 2877 | buf->f_bfree = sbinfo->max_blocks - |
| 2878 | percpu_counter_sum(&sbinfo->used_blocks); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 2879 | } |
| 2880 | if (sbinfo->max_inodes) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | buf->f_files = sbinfo->max_inodes; |
| 2882 | buf->f_ffree = sbinfo->free_inodes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | } |
| 2884 | /* else leave those fields 0 like simple_statfs */ |
| 2885 | return 0; |
| 2886 | } |
| 2887 | |
| 2888 | /* |
| 2889 | * File creation. Allocate an inode, and we're done.. |
| 2890 | */ |
| 2891 | static int |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2892 | shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | { |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 2894 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2895 | int error = -ENOSPC; |
| 2896 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 2897 | inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | if (inode) { |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2899 | error = simple_acl_create(dir, inode); |
| 2900 | if (error) |
| 2901 | goto out_iput; |
Eric Paris | 2a7dba3 | 2011-02-01 11:05:39 -0500 | [diff] [blame] | 2902 | error = security_inode_init_security(inode, dir, |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 2903 | &dentry->d_name, |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 2904 | shmem_initxattrs, NULL); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2905 | if (error && error != -EOPNOTSUPP) |
| 2906 | goto out_iput; |
Mimi Zohar | 37ec43c | 2013-04-14 09:21:47 -0400 | [diff] [blame] | 2907 | |
Al Viro | 718deb6 | 2009-12-16 19:35:36 -0500 | [diff] [blame] | 2908 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2910 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | d_instantiate(dentry, inode); |
| 2912 | dget(dentry); /* Extra count - pin the dentry in core */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2913 | } |
| 2914 | return error; |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2915 | out_iput: |
| 2916 | iput(inode); |
| 2917 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2920 | static int |
| 2921 | shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 2922 | { |
| 2923 | struct inode *inode; |
| 2924 | int error = -ENOSPC; |
| 2925 | |
| 2926 | inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE); |
| 2927 | if (inode) { |
| 2928 | error = security_inode_init_security(inode, dir, |
| 2929 | NULL, |
| 2930 | shmem_initxattrs, NULL); |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2931 | if (error && error != -EOPNOTSUPP) |
| 2932 | goto out_iput; |
| 2933 | error = simple_acl_create(dir, inode); |
| 2934 | if (error) |
| 2935 | goto out_iput; |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2936 | d_tmpfile(dentry, inode); |
| 2937 | } |
| 2938 | return error; |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 2939 | out_iput: |
| 2940 | iput(inode); |
| 2941 | return error; |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 2942 | } |
| 2943 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 2944 | static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | { |
| 2946 | int error; |
| 2947 | |
| 2948 | if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0))) |
| 2949 | return error; |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2950 | inc_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | return 0; |
| 2952 | } |
| 2953 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2954 | static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 2955 | bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | { |
| 2957 | return shmem_mknod(dir, dentry, mode | S_IFREG, 0); |
| 2958 | } |
| 2959 | |
| 2960 | /* |
| 2961 | * Link a file.. |
| 2962 | */ |
| 2963 | static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 2964 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2965 | struct inode *inode = d_inode(old_dentry); |
Darrick J. Wong | 29b00e60 | 2019-02-22 22:35:32 -0800 | [diff] [blame] | 2966 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | |
| 2968 | /* |
| 2969 | * No ordinary (disk based) filesystem counts links as inodes; |
| 2970 | * but each new link needs a new dentry, pinning lowmem, and |
| 2971 | * tmpfs dentries cannot be pruned until they are unlinked. |
Darrick J. Wong | 1062af9 | 2019-02-21 08:48:09 -0800 | [diff] [blame] | 2972 | * But if an O_TMPFILE file is linked into the tmpfs, the |
| 2973 | * first link must skip that, to get the accounting right. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | */ |
Darrick J. Wong | 1062af9 | 2019-02-21 08:48:09 -0800 | [diff] [blame] | 2975 | if (inode->i_nlink) { |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 2976 | ret = shmem_reserve_inode(inode->i_sb, NULL); |
Darrick J. Wong | 1062af9 | 2019-02-21 08:48:09 -0800 | [diff] [blame] | 2977 | if (ret) |
| 2978 | goto out; |
| 2979 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | |
| 2981 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2982 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 2983 | inc_nlink(inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 2984 | ihold(inode); /* New dentry reference */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | dget(dentry); /* Extra pinning count for the created dentry */ |
| 2986 | d_instantiate(dentry, inode); |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2987 | out: |
| 2988 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | static int shmem_unlink(struct inode *dir, struct dentry *dentry) |
| 2992 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 2993 | struct inode *inode = d_inode(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2994 | |
Pavel Emelyanov | 5b04c68 | 2008-02-04 22:28:47 -0800 | [diff] [blame] | 2995 | if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) |
| 2996 | shmem_free_inode(inode->i_sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | |
| 2998 | dir->i_size -= BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 2999 | inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 3000 | drop_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | dput(dentry); /* Undo the count from "create" - this does all the work */ |
| 3002 | return 0; |
| 3003 | } |
| 3004 | |
| 3005 | static int shmem_rmdir(struct inode *dir, struct dentry *dentry) |
| 3006 | { |
| 3007 | if (!simple_empty(dentry)) |
| 3008 | return -ENOTEMPTY; |
| 3009 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3010 | drop_nlink(d_inode(dentry)); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 3011 | drop_nlink(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | return shmem_unlink(dir, dentry); |
| 3013 | } |
| 3014 | |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 3015 | static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry) |
| 3016 | { |
David Howells | e36cb0b | 2015-01-29 12:02:35 +0000 | [diff] [blame] | 3017 | bool old_is_dir = d_is_dir(old_dentry); |
| 3018 | bool new_is_dir = d_is_dir(new_dentry); |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 3019 | |
| 3020 | if (old_dir != new_dir && old_is_dir != new_is_dir) { |
| 3021 | if (old_is_dir) { |
| 3022 | drop_nlink(old_dir); |
| 3023 | inc_nlink(new_dir); |
| 3024 | } else { |
| 3025 | drop_nlink(new_dir); |
| 3026 | inc_nlink(old_dir); |
| 3027 | } |
| 3028 | } |
| 3029 | old_dir->i_ctime = old_dir->i_mtime = |
| 3030 | new_dir->i_ctime = new_dir->i_mtime = |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3031 | d_inode(old_dentry)->i_ctime = |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 3032 | d_inode(new_dentry)->i_ctime = current_time(old_dir); |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 3033 | |
| 3034 | return 0; |
| 3035 | } |
| 3036 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3037 | static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry) |
| 3038 | { |
| 3039 | struct dentry *whiteout; |
| 3040 | int error; |
| 3041 | |
| 3042 | whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); |
| 3043 | if (!whiteout) |
| 3044 | return -ENOMEM; |
| 3045 | |
| 3046 | error = shmem_mknod(old_dir, whiteout, |
| 3047 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); |
| 3048 | dput(whiteout); |
| 3049 | if (error) |
| 3050 | return error; |
| 3051 | |
| 3052 | /* |
| 3053 | * Cheat and hash the whiteout while the old dentry is still in |
| 3054 | * place, instead of playing games with FS_RENAME_DOES_D_MOVE. |
| 3055 | * |
| 3056 | * d_lookup() will consistently find one of them at this point, |
| 3057 | * not sure which one, but that isn't even important. |
| 3058 | */ |
| 3059 | d_rehash(whiteout); |
| 3060 | return 0; |
| 3061 | } |
| 3062 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | /* |
| 3064 | * The VFS layer already does all the dentry stuff for rename, |
| 3065 | * we just have to decrement the usage count for the target if |
| 3066 | * it exists so that the VFS layer correctly free's it when it |
| 3067 | * gets overwritten. |
| 3068 | */ |
Miklos Szeredi | 3b69ff5 | 2014-07-23 15:15:33 +0200 | [diff] [blame] | 3069 | static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3071 | struct inode *inode = d_inode(old_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3072 | int they_are_dirs = S_ISDIR(inode->i_mode); |
| 3073 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3074 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
Miklos Szeredi | 3b69ff5 | 2014-07-23 15:15:33 +0200 | [diff] [blame] | 3075 | return -EINVAL; |
| 3076 | |
Miklos Szeredi | 3745677 | 2014-07-23 15:15:34 +0200 | [diff] [blame] | 3077 | if (flags & RENAME_EXCHANGE) |
| 3078 | return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry); |
| 3079 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3080 | if (!simple_empty(new_dentry)) |
| 3081 | return -ENOTEMPTY; |
| 3082 | |
Miklos Szeredi | 46fdb79 | 2014-10-24 00:14:37 +0200 | [diff] [blame] | 3083 | if (flags & RENAME_WHITEOUT) { |
| 3084 | int error; |
| 3085 | |
| 3086 | error = shmem_whiteout(old_dir, old_dentry); |
| 3087 | if (error) |
| 3088 | return error; |
| 3089 | } |
| 3090 | |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3091 | if (d_really_is_positive(new_dentry)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | (void) shmem_unlink(new_dir, new_dentry); |
Miklos Szeredi | b928095 | 2014-09-24 17:56:17 +0200 | [diff] [blame] | 3093 | if (they_are_dirs) { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3094 | drop_nlink(d_inode(new_dentry)); |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 3095 | drop_nlink(old_dir); |
Miklos Szeredi | b928095 | 2014-09-24 17:56:17 +0200 | [diff] [blame] | 3096 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | } else if (they_are_dirs) { |
Dave Hansen | 9a53c3a | 2006-09-30 23:29:03 -0700 | [diff] [blame] | 3098 | drop_nlink(old_dir); |
Dave Hansen | d8c76e6 | 2006-09-30 23:29:04 -0700 | [diff] [blame] | 3099 | inc_nlink(new_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | old_dir->i_size -= BOGO_DIRENT_SIZE; |
| 3103 | new_dir->i_size += BOGO_DIRENT_SIZE; |
| 3104 | old_dir->i_ctime = old_dir->i_mtime = |
| 3105 | new_dir->i_ctime = new_dir->i_mtime = |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 3106 | inode->i_ctime = current_time(old_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | return 0; |
| 3108 | } |
| 3109 | |
| 3110 | static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 3111 | { |
| 3112 | int error; |
| 3113 | int len; |
| 3114 | struct inode *inode; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 3115 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | |
| 3117 | len = strlen(symname) + 1; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3118 | if (len > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3119 | return -ENAMETOOLONG; |
| 3120 | |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3121 | inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0, |
| 3122 | VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | if (!inode) |
| 3124 | return -ENOSPC; |
| 3125 | |
Mimi Zohar | 9d8f13b | 2011-06-06 15:29:25 -0400 | [diff] [blame] | 3126 | error = security_inode_init_security(inode, dir, &dentry->d_name, |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3127 | shmem_initxattrs, NULL); |
Mateusz Nosek | 343c3d7 | 2020-04-06 20:07:54 -0700 | [diff] [blame] | 3128 | if (error && error != -EOPNOTSUPP) { |
| 3129 | iput(inode); |
| 3130 | return error; |
Stephen Smalley | 570bc1c | 2005-09-09 13:01:43 -0700 | [diff] [blame] | 3131 | } |
| 3132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | inode->i_size = len-1; |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 3134 | if (len <= SHORT_SYMLINK_LEN) { |
Al Viro | 3ed47db | 2016-01-22 18:08:52 -0500 | [diff] [blame] | 3135 | inode->i_link = kmemdup(symname, len, GFP_KERNEL); |
| 3136 | if (!inode->i_link) { |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 3137 | iput(inode); |
| 3138 | return -ENOMEM; |
| 3139 | } |
| 3140 | inode->i_op = &shmem_short_symlink_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | } else { |
Al Viro | e8ecde2 | 2016-01-14 17:52:59 -0500 | [diff] [blame] | 3142 | inode_nohighmem(inode); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 3143 | error = shmem_getpage(inode, 0, &page, SGP_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | if (error) { |
| 3145 | iput(inode); |
| 3146 | return error; |
| 3147 | } |
Hugh Dickins | 14fcc23 | 2008-07-28 15:46:19 -0700 | [diff] [blame] | 3148 | inode->i_mapping->a_ops = &shmem_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3149 | inode->i_op = &shmem_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3150 | memcpy(page_address(page), symname, len); |
Hugh Dickins | ec9516f | 2012-05-29 15:06:39 -0700 | [diff] [blame] | 3151 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | set_page_dirty(page); |
Wu Fengguang | 6746aff | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 3153 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3154 | put_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | dir->i_size += BOGO_DIRENT_SIZE; |
Deepa Dinamani | 078cd82 | 2016-09-14 07:48:04 -0700 | [diff] [blame] | 3157 | dir->i_ctime = dir->i_mtime = current_time(dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3158 | d_instantiate(dentry, inode); |
| 3159 | dget(dentry); |
| 3160 | return 0; |
| 3161 | } |
| 3162 | |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3163 | static void shmem_put_link(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | { |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3165 | mark_page_accessed(arg); |
| 3166 | put_page(arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | } |
| 3168 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3169 | static const char *shmem_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3170 | struct inode *inode, |
| 3171 | struct delayed_call *done) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3173 | struct page *page = NULL; |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3174 | int error; |
Al Viro | 6a6c990 | 2015-11-17 10:54:32 -0500 | [diff] [blame] | 3175 | if (!dentry) { |
| 3176 | page = find_get_page(inode->i_mapping, 0); |
| 3177 | if (!page) |
| 3178 | return ERR_PTR(-ECHILD); |
| 3179 | if (!PageUptodate(page)) { |
| 3180 | put_page(page); |
| 3181 | return ERR_PTR(-ECHILD); |
| 3182 | } |
| 3183 | } else { |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 3184 | error = shmem_getpage(inode, 0, &page, SGP_READ); |
Al Viro | 6a6c990 | 2015-11-17 10:54:32 -0500 | [diff] [blame] | 3185 | if (error) |
| 3186 | return ERR_PTR(error); |
| 3187 | unlock_page(page); |
| 3188 | } |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 3189 | set_delayed_call(done, shmem_put_link, page); |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 3190 | return page_address(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3193 | #ifdef CONFIG_TMPFS_XATTR |
| 3194 | /* |
| 3195 | * Superblocks without xattr inode operations may get some security.* xattr |
| 3196 | * support from the LSM "for free". As soon as we have any other xattrs |
| 3197 | * like ACLs, we also need to implement the security.* handlers at |
| 3198 | * filesystem level, though. |
| 3199 | */ |
| 3200 | |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3201 | /* |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3202 | * Callback for security_inode_init_security() for acquiring xattrs. |
| 3203 | */ |
| 3204 | static int shmem_initxattrs(struct inode *inode, |
| 3205 | const struct xattr *xattr_array, |
| 3206 | void *fs_info) |
| 3207 | { |
| 3208 | struct shmem_inode_info *info = SHMEM_I(inode); |
| 3209 | const struct xattr *xattr; |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3210 | struct simple_xattr *new_xattr; |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3211 | size_t len; |
| 3212 | |
| 3213 | for (xattr = xattr_array; xattr->name != NULL; xattr++) { |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3214 | new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3215 | if (!new_xattr) |
| 3216 | return -ENOMEM; |
| 3217 | |
| 3218 | len = strlen(xattr->name) + 1; |
| 3219 | new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, |
| 3220 | GFP_KERNEL); |
| 3221 | if (!new_xattr->name) { |
Chengguang Xu | 3bef735 | 2020-07-23 21:15:14 -0700 | [diff] [blame] | 3222 | kvfree(new_xattr); |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3223 | return -ENOMEM; |
| 3224 | } |
| 3225 | |
| 3226 | memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, |
| 3227 | XATTR_SECURITY_PREFIX_LEN); |
| 3228 | memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, |
| 3229 | xattr->name, len); |
| 3230 | |
Aristeu Rozanski | 38f3865 | 2012-08-23 16:53:28 -0400 | [diff] [blame] | 3231 | simple_xattr_list_add(&info->xattrs, new_xattr); |
Jarkko Sakkinen | 6d9d88d | 2012-03-21 16:34:05 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3237 | static int shmem_xattr_handler_get(const struct xattr_handler *handler, |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 3238 | struct dentry *unused, struct inode *inode, |
Mark Salyzyn | 3484eba | 2019-11-04 08:57:10 -0800 | [diff] [blame] | 3239 | const char *name, void *buffer, size_t size, |
| 3240 | int flags) |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3241 | { |
Al Viro | b296821 | 2016-04-10 20:48:24 -0400 | [diff] [blame] | 3242 | struct shmem_inode_info *info = SHMEM_I(inode); |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3243 | |
| 3244 | name = xattr_full_name(handler, name); |
| 3245 | return simple_xattr_get(&info->xattrs, name, buffer, size); |
| 3246 | } |
| 3247 | |
| 3248 | static int shmem_xattr_handler_set(const struct xattr_handler *handler, |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 3249 | struct dentry *unused, struct inode *inode, |
| 3250 | const char *name, const void *value, |
| 3251 | size_t size, int flags) |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3252 | { |
Al Viro | 5930122 | 2016-05-27 10:19:30 -0400 | [diff] [blame] | 3253 | struct shmem_inode_info *info = SHMEM_I(inode); |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3254 | |
| 3255 | name = xattr_full_name(handler, name); |
Daniel Xu | a46a229 | 2020-03-12 13:03:15 -0700 | [diff] [blame] | 3256 | return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL); |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | static const struct xattr_handler shmem_security_xattr_handler = { |
| 3260 | .prefix = XATTR_SECURITY_PREFIX, |
| 3261 | .get = shmem_xattr_handler_get, |
| 3262 | .set = shmem_xattr_handler_set, |
| 3263 | }; |
| 3264 | |
| 3265 | static const struct xattr_handler shmem_trusted_xattr_handler = { |
| 3266 | .prefix = XATTR_TRUSTED_PREFIX, |
| 3267 | .get = shmem_xattr_handler_get, |
| 3268 | .set = shmem_xattr_handler_set, |
| 3269 | }; |
| 3270 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3271 | static const struct xattr_handler *shmem_xattr_handlers[] = { |
| 3272 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3273 | &posix_acl_access_xattr_handler, |
| 3274 | &posix_acl_default_xattr_handler, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3275 | #endif |
Andreas Gruenbacher | aa7c524 | 2015-12-02 14:44:38 +0100 | [diff] [blame] | 3276 | &shmem_security_xattr_handler, |
| 3277 | &shmem_trusted_xattr_handler, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3278 | NULL |
| 3279 | }; |
| 3280 | |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3281 | static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) |
| 3282 | { |
David Howells | 75c3cfa | 2015-03-17 22:26:12 +0000 | [diff] [blame] | 3283 | struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); |
Andreas Gruenbacher | 786534b | 2015-12-02 14:44:39 +0100 | [diff] [blame] | 3284 | return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3285 | } |
| 3286 | #endif /* CONFIG_TMPFS_XATTR */ |
| 3287 | |
Hugh Dickins | 69f07ec | 2011-08-03 16:21:26 -0700 | [diff] [blame] | 3288 | static const struct inode_operations shmem_short_symlink_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3289 | .get_link = simple_get_link, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3290 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3291 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3292 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3293 | }; |
| 3294 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3295 | static const struct inode_operations shmem_symlink_inode_operations = { |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 3296 | .get_link = shmem_get_link, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3297 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3298 | .listxattr = shmem_listxattr, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3299 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3300 | }; |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3301 | |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3302 | static struct dentry *shmem_get_parent(struct dentry *child) |
| 3303 | { |
| 3304 | return ERR_PTR(-ESTALE); |
| 3305 | } |
| 3306 | |
| 3307 | static int shmem_match(struct inode *ino, void *vfh) |
| 3308 | { |
| 3309 | __u32 *fh = vfh; |
| 3310 | __u64 inum = fh[2]; |
| 3311 | inum = (inum << 32) | fh[1]; |
| 3312 | return ino->i_ino == inum && fh[0] == ino->i_generation; |
| 3313 | } |
| 3314 | |
Amir Goldstein | 12ba780 | 2018-06-07 17:07:15 -0700 | [diff] [blame] | 3315 | /* Find any alias of inode, but prefer a hashed alias */ |
| 3316 | static struct dentry *shmem_find_alias(struct inode *inode) |
| 3317 | { |
| 3318 | struct dentry *alias = d_find_alias(inode); |
| 3319 | |
| 3320 | return alias ?: d_find_any_alias(inode); |
| 3321 | } |
| 3322 | |
| 3323 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3324 | static struct dentry *shmem_fh_to_dentry(struct super_block *sb, |
| 3325 | struct fid *fid, int fh_len, int fh_type) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3326 | { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3327 | struct inode *inode; |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3328 | struct dentry *dentry = NULL; |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 3329 | u64 inum; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3330 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3331 | if (fh_len < 3) |
| 3332 | return NULL; |
| 3333 | |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 3334 | inum = fid->raw[2]; |
| 3335 | inum = (inum << 32) | fid->raw[1]; |
| 3336 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3337 | inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), |
| 3338 | shmem_match, fid->raw); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3339 | if (inode) { |
Amir Goldstein | 12ba780 | 2018-06-07 17:07:15 -0700 | [diff] [blame] | 3340 | dentry = shmem_find_alias(inode); |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3341 | iput(inode); |
| 3342 | } |
| 3343 | |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3344 | return dentry; |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3345 | } |
| 3346 | |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 3347 | static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, |
| 3348 | struct inode *parent) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3349 | { |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 3350 | if (*len < 3) { |
| 3351 | *len = 3; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 3352 | return FILEID_INVALID; |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 3353 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3354 | |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3355 | if (inode_unhashed(inode)) { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3356 | /* Unfortunately insert_inode_hash is not idempotent, |
| 3357 | * so as we hash inodes here rather than at creation |
| 3358 | * time, we need a lock to ensure we only try |
| 3359 | * to do it once |
| 3360 | */ |
| 3361 | static DEFINE_SPINLOCK(lock); |
| 3362 | spin_lock(&lock); |
Al Viro | 1d3382cb | 2010-10-23 15:19:20 -0400 | [diff] [blame] | 3363 | if (inode_unhashed(inode)) |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3364 | __insert_inode_hash(inode, |
| 3365 | inode->i_ino + inode->i_generation); |
| 3366 | spin_unlock(&lock); |
| 3367 | } |
| 3368 | |
| 3369 | fh[0] = inode->i_generation; |
| 3370 | fh[1] = inode->i_ino; |
| 3371 | fh[2] = ((__u64)inode->i_ino) >> 32; |
| 3372 | |
| 3373 | *len = 3; |
| 3374 | return 1; |
| 3375 | } |
| 3376 | |
Christoph Hellwig | 3965516 | 2007-10-21 16:42:17 -0700 | [diff] [blame] | 3377 | static const struct export_operations shmem_export_ops = { |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3378 | .get_parent = shmem_get_parent, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3379 | .encode_fh = shmem_encode_fh, |
Christoph Hellwig | 480b116 | 2007-10-21 16:42:13 -0700 | [diff] [blame] | 3380 | .fh_to_dentry = shmem_fh_to_dentry, |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3381 | }; |
| 3382 | |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3383 | enum shmem_param { |
| 3384 | Opt_gid, |
| 3385 | Opt_huge, |
| 3386 | Opt_mode, |
| 3387 | Opt_mpol, |
| 3388 | Opt_nr_blocks, |
| 3389 | Opt_nr_inodes, |
| 3390 | Opt_size, |
| 3391 | Opt_uid, |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3392 | Opt_inode32, |
| 3393 | Opt_inode64, |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3394 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | |
Al Viro | 5eede62 | 2019-12-16 13:33:32 -0500 | [diff] [blame] | 3396 | static const struct constant_table shmem_param_enums_huge[] = { |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 3397 | {"never", SHMEM_HUGE_NEVER }, |
| 3398 | {"always", SHMEM_HUGE_ALWAYS }, |
| 3399 | {"within_size", SHMEM_HUGE_WITHIN_SIZE }, |
| 3400 | {"advise", SHMEM_HUGE_ADVISE }, |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 3401 | {} |
| 3402 | }; |
| 3403 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 3404 | const struct fs_parameter_spec shmem_fs_parameters[] = { |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3405 | fsparam_u32 ("gid", Opt_gid), |
Al Viro | 2710c957a | 2019-09-06 22:12:08 -0400 | [diff] [blame] | 3406 | fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3407 | fsparam_u32oct("mode", Opt_mode), |
| 3408 | fsparam_string("mpol", Opt_mpol), |
| 3409 | fsparam_string("nr_blocks", Opt_nr_blocks), |
| 3410 | fsparam_string("nr_inodes", Opt_nr_inodes), |
| 3411 | fsparam_string("size", Opt_size), |
| 3412 | fsparam_u32 ("uid", Opt_uid), |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3413 | fsparam_flag ("inode32", Opt_inode32), |
| 3414 | fsparam_flag ("inode64", Opt_inode64), |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3415 | {} |
| 3416 | }; |
| 3417 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3418 | static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3419 | { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3420 | struct shmem_options *ctx = fc->fs_private; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3421 | struct fs_parse_result result; |
| 3422 | unsigned long long size; |
| 3423 | char *rest; |
| 3424 | int opt; |
| 3425 | |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 3426 | opt = fs_parse(fc, shmem_fs_parameters, param, &result); |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3427 | if (opt < 0) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3428 | return opt; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3429 | |
| 3430 | switch (opt) { |
| 3431 | case Opt_size: |
| 3432 | size = memparse(param->string, &rest); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3433 | if (*rest == '%') { |
| 3434 | size <<= PAGE_SHIFT; |
| 3435 | size *= totalram_pages(); |
| 3436 | do_div(size, 100); |
| 3437 | rest++; |
| 3438 | } |
| 3439 | if (*rest) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3440 | goto bad_value; |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3441 | ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); |
| 3442 | ctx->seen |= SHMEM_SEEN_BLOCKS; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3443 | break; |
| 3444 | case Opt_nr_blocks: |
| 3445 | ctx->blocks = memparse(param->string, &rest); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3446 | if (*rest) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3447 | goto bad_value; |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3448 | ctx->seen |= SHMEM_SEEN_BLOCKS; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3449 | break; |
| 3450 | case Opt_nr_inodes: |
| 3451 | ctx->inodes = memparse(param->string, &rest); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3452 | if (*rest) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3453 | goto bad_value; |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3454 | ctx->seen |= SHMEM_SEEN_INODES; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3455 | break; |
| 3456 | case Opt_mode: |
| 3457 | ctx->mode = result.uint_32 & 07777; |
| 3458 | break; |
| 3459 | case Opt_uid: |
| 3460 | ctx->uid = make_kuid(current_user_ns(), result.uint_32); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3461 | if (!uid_valid(ctx->uid)) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3462 | goto bad_value; |
| 3463 | break; |
| 3464 | case Opt_gid: |
| 3465 | ctx->gid = make_kgid(current_user_ns(), result.uint_32); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3466 | if (!gid_valid(ctx->gid)) |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3467 | goto bad_value; |
| 3468 | break; |
| 3469 | case Opt_huge: |
| 3470 | ctx->huge = result.uint_32; |
| 3471 | if (ctx->huge != SHMEM_HUGE_NEVER && |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3472 | !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3473 | has_transparent_hugepage())) |
| 3474 | goto unsupported_parameter; |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3475 | ctx->seen |= SHMEM_SEEN_HUGE; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3476 | break; |
| 3477 | case Opt_mpol: |
| 3478 | if (IS_ENABLED(CONFIG_NUMA)) { |
| 3479 | mpol_put(ctx->mpol); |
| 3480 | ctx->mpol = NULL; |
| 3481 | if (mpol_parse_str(param->string, &ctx->mpol)) |
| 3482 | goto bad_value; |
| 3483 | break; |
| 3484 | } |
| 3485 | goto unsupported_parameter; |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3486 | case Opt_inode32: |
| 3487 | ctx->full_inums = false; |
| 3488 | ctx->seen |= SHMEM_SEEN_INUMS; |
| 3489 | break; |
| 3490 | case Opt_inode64: |
| 3491 | if (sizeof(ino_t) < 8) { |
| 3492 | return invalfc(fc, |
| 3493 | "Cannot use inode64 with <64bit inums in kernel\n"); |
| 3494 | } |
| 3495 | ctx->full_inums = true; |
| 3496 | ctx->seen |= SHMEM_SEEN_INUMS; |
| 3497 | break; |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3498 | } |
| 3499 | return 0; |
| 3500 | |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3501 | unsupported_parameter: |
Al Viro | f35aa2b | 2019-12-21 21:35:55 -0500 | [diff] [blame] | 3502 | return invalfc(fc, "Unsupported parameter '%s'", param->key); |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3503 | bad_value: |
Al Viro | f35aa2b | 2019-12-21 21:35:55 -0500 | [diff] [blame] | 3504 | return invalfc(fc, "Bad value for '%s'", param->key); |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3505 | } |
| 3506 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3507 | static int shmem_parse_options(struct fs_context *fc, void *data) |
Al Viro | e04dc42 | 2019-09-08 19:20:12 -0400 | [diff] [blame] | 3508 | { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3509 | char *options = data; |
| 3510 | |
Al Viro | 33f37c6 | 2019-10-09 22:48:01 -0400 | [diff] [blame] | 3511 | if (options) { |
| 3512 | int err = security_sb_eat_lsm_opts(options, &fc->security); |
| 3513 | if (err) |
| 3514 | return err; |
| 3515 | } |
| 3516 | |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 3517 | while (options != NULL) { |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3518 | char *this_char = options; |
Hugh Dickins | b00dc3a | 2006-02-21 23:49:47 +0000 | [diff] [blame] | 3519 | for (;;) { |
| 3520 | /* |
| 3521 | * NUL-terminate this option: unfortunately, |
| 3522 | * mount options form a comma-separated list, |
| 3523 | * but mpol's nodelist may also contain commas. |
| 3524 | */ |
| 3525 | options = strchr(options, ','); |
| 3526 | if (options == NULL) |
| 3527 | break; |
| 3528 | options++; |
| 3529 | if (!isdigit(*options)) { |
| 3530 | options[-1] = '\0'; |
| 3531 | break; |
| 3532 | } |
| 3533 | } |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3534 | if (*this_char) { |
| 3535 | char *value = strchr(this_char,'='); |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3536 | size_t len = 0; |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3537 | int err; |
| 3538 | |
| 3539 | if (value) { |
| 3540 | *value++ = '\0'; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3541 | len = strlen(value); |
Al Viro | 626c392 | 2019-09-08 20:28:06 -0400 | [diff] [blame] | 3542 | } |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3543 | err = vfs_parse_fs_string(fc, this_char, value, len); |
| 3544 | if (err < 0) |
| 3545 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3546 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | } |
| 3548 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | } |
| 3550 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3551 | /* |
| 3552 | * Reconfigure a shmem filesystem. |
| 3553 | * |
| 3554 | * Note that we disallow change from limited->unlimited blocks/inodes while any |
| 3555 | * are in use; but we must separately disallow unlimited->limited, because in |
| 3556 | * that case we have no record of how much is already in use. |
| 3557 | */ |
| 3558 | static int shmem_reconfigure(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3560 | struct shmem_options *ctx = fc->fs_private; |
| 3561 | struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3562 | unsigned long inodes; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3563 | const char *err; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3564 | |
| 3565 | spin_lock(&sbinfo->stat_lock); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3566 | inodes = sbinfo->max_inodes - sbinfo->free_inodes; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3567 | if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { |
| 3568 | if (!sbinfo->max_blocks) { |
| 3569 | err = "Cannot retroactively limit size"; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3570 | goto out; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3571 | } |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3572 | if (percpu_counter_compare(&sbinfo->used_blocks, |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3573 | ctx->blocks) > 0) { |
| 3574 | err = "Too small a size for current use"; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3575 | goto out; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3576 | } |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3577 | } |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3578 | if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { |
| 3579 | if (!sbinfo->max_inodes) { |
| 3580 | err = "Cannot retroactively limit inodes"; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3581 | goto out; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3582 | } |
| 3583 | if (ctx->inodes < inodes) { |
| 3584 | err = "Too few inodes for current use"; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3585 | goto out; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3586 | } |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3587 | } |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3588 | |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3589 | if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && |
| 3590 | sbinfo->next_ino > UINT_MAX) { |
| 3591 | err = "Current inum too high to switch to 32-bit inums"; |
| 3592 | goto out; |
| 3593 | } |
| 3594 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3595 | if (ctx->seen & SHMEM_SEEN_HUGE) |
| 3596 | sbinfo->huge = ctx->huge; |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3597 | if (ctx->seen & SHMEM_SEEN_INUMS) |
| 3598 | sbinfo->full_inums = ctx->full_inums; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3599 | if (ctx->seen & SHMEM_SEEN_BLOCKS) |
| 3600 | sbinfo->max_blocks = ctx->blocks; |
| 3601 | if (ctx->seen & SHMEM_SEEN_INODES) { |
| 3602 | sbinfo->max_inodes = ctx->inodes; |
| 3603 | sbinfo->free_inodes = ctx->inodes - inodes; |
Al Viro | 0b5071d | 2019-09-08 18:49:18 -0400 | [diff] [blame] | 3604 | } |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 3605 | |
Greg Thelen | 5f00110 | 2013-02-22 16:36:01 -0800 | [diff] [blame] | 3606 | /* |
| 3607 | * Preserve previous mempolicy unless mpol remount option was specified. |
| 3608 | */ |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3609 | if (ctx->mpol) { |
Greg Thelen | 5f00110 | 2013-02-22 16:36:01 -0800 | [diff] [blame] | 3610 | mpol_put(sbinfo->mpol); |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3611 | sbinfo->mpol = ctx->mpol; /* transfers initial ref */ |
| 3612 | ctx->mpol = NULL; |
Greg Thelen | 5f00110 | 2013-02-22 16:36:01 -0800 | [diff] [blame] | 3613 | } |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3614 | spin_unlock(&sbinfo->stat_lock); |
| 3615 | return 0; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3616 | out: |
| 3617 | spin_unlock(&sbinfo->stat_lock); |
Al Viro | f35aa2b | 2019-12-21 21:35:55 -0500 | [diff] [blame] | 3618 | return invalfc(fc, "%s", err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | } |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3620 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 3621 | static int shmem_show_options(struct seq_file *seq, struct dentry *root) |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3622 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 3623 | struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3624 | |
| 3625 | if (sbinfo->max_blocks != shmem_default_max_blocks()) |
| 3626 | seq_printf(seq, ",size=%luk", |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3627 | sbinfo->max_blocks << (PAGE_SHIFT - 10)); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3628 | if (sbinfo->max_inodes != shmem_default_max_inodes()) |
| 3629 | seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 3630 | if (sbinfo->mode != (0777 | S_ISVTX)) |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 3631 | seq_printf(seq, ",mode=%03ho", sbinfo->mode); |
Eric W. Biederman | 8751e03 | 2012-02-07 16:46:12 -0800 | [diff] [blame] | 3632 | if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) |
| 3633 | seq_printf(seq, ",uid=%u", |
| 3634 | from_kuid_munged(&init_user_ns, sbinfo->uid)); |
| 3635 | if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) |
| 3636 | seq_printf(seq, ",gid=%u", |
| 3637 | from_kgid_munged(&init_user_ns, sbinfo->gid)); |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3638 | |
| 3639 | /* |
| 3640 | * Showing inode{64,32} might be useful even if it's the system default, |
| 3641 | * since then people don't have to resort to checking both here and |
| 3642 | * /proc/config.gz to confirm 64-bit inums were successfully applied |
| 3643 | * (which may not even exist if IKCONFIG_PROC isn't enabled). |
| 3644 | * |
| 3645 | * We hide it when inode64 isn't the default and we are using 32-bit |
| 3646 | * inodes, since that probably just means the feature isn't even under |
| 3647 | * consideration. |
| 3648 | * |
| 3649 | * As such: |
| 3650 | * |
| 3651 | * +-----------------+-----------------+ |
| 3652 | * | TMPFS_INODE64=y | TMPFS_INODE64=n | |
| 3653 | * +------------------+-----------------+-----------------+ |
| 3654 | * | full_inums=true | show | show | |
| 3655 | * | full_inums=false | show | hide | |
| 3656 | * +------------------+-----------------+-----------------+ |
| 3657 | * |
| 3658 | */ |
| 3659 | if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) |
| 3660 | seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3661 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3662 | /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ |
| 3663 | if (sbinfo->huge) |
| 3664 | seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); |
| 3665 | #endif |
Lee Schermerhorn | 71fe804 | 2008-04-28 02:13:26 -0700 | [diff] [blame] | 3666 | shmem_show_mpol(seq, sbinfo->mpol); |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3667 | return 0; |
| 3668 | } |
David Herrmann | 9183df2 | 2014-08-08 14:25:29 -0700 | [diff] [blame] | 3669 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3670 | #endif /* CONFIG_TMPFS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3671 | |
| 3672 | static void shmem_put_super(struct super_block *sb) |
| 3673 | { |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3674 | struct shmem_sb_info *sbinfo = SHMEM_SB(sb); |
| 3675 | |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 3676 | free_percpu(sbinfo->ino_batch); |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3677 | percpu_counter_destroy(&sbinfo->used_blocks); |
Greg Thelen | 49cd0a5 | 2013-02-22 16:36:02 -0800 | [diff] [blame] | 3678 | mpol_put(sbinfo->mpol); |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3679 | kfree(sbinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3680 | sb->s_fs_info = NULL; |
| 3681 | } |
| 3682 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3683 | static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3685 | struct shmem_options *ctx = fc->fs_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3686 | struct inode *inode; |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3687 | struct shmem_sb_info *sbinfo; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3688 | int err = -ENOMEM; |
| 3689 | |
| 3690 | /* Round up to L1_CACHE_BYTES to resist false sharing */ |
Pekka Enberg | 425fbf0 | 2009-09-21 17:03:50 -0700 | [diff] [blame] | 3691 | sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3692 | L1_CACHE_BYTES), GFP_KERNEL); |
| 3693 | if (!sbinfo) |
| 3694 | return -ENOMEM; |
| 3695 | |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3696 | sb->s_fs_info = sbinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3698 | #ifdef CONFIG_TMPFS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3699 | /* |
| 3700 | * Per default we only allow half of the physical ram per |
| 3701 | * tmpfs instance, limiting inodes to one per page of lowmem; |
| 3702 | * but the internal instance is left unlimited. |
| 3703 | */ |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3704 | if (!(sb->s_flags & SB_KERNMOUNT)) { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3705 | if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) |
| 3706 | ctx->blocks = shmem_default_max_blocks(); |
| 3707 | if (!(ctx->seen & SHMEM_SEEN_INODES)) |
| 3708 | ctx->inodes = shmem_default_max_inodes(); |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3709 | if (!(ctx->seen & SHMEM_SEEN_INUMS)) |
| 3710 | ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); |
Al Viro | ca4e051 | 2013-08-31 12:57:10 -0400 | [diff] [blame] | 3711 | } else { |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3712 | sb->s_flags |= SB_NOUSER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | } |
David M. Grimes | 91828a4 | 2006-10-17 00:09:45 -0700 | [diff] [blame] | 3714 | sb->s_export_op = &shmem_export_ops; |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3715 | sb->s_flags |= SB_NOSEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | #else |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3717 | sb->s_flags |= SB_NOUSER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3718 | #endif |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3719 | sbinfo->max_blocks = ctx->blocks; |
| 3720 | sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes; |
Chris Down | e809d5f | 2020-08-06 23:20:20 -0700 | [diff] [blame] | 3721 | if (sb->s_flags & SB_KERNMOUNT) { |
| 3722 | sbinfo->ino_batch = alloc_percpu(ino_t); |
| 3723 | if (!sbinfo->ino_batch) |
| 3724 | goto failed; |
| 3725 | } |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3726 | sbinfo->uid = ctx->uid; |
| 3727 | sbinfo->gid = ctx->gid; |
Chris Down | ea3271f | 2020-08-06 23:20:25 -0700 | [diff] [blame] | 3728 | sbinfo->full_inums = ctx->full_inums; |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3729 | sbinfo->mode = ctx->mode; |
| 3730 | sbinfo->huge = ctx->huge; |
| 3731 | sbinfo->mpol = ctx->mpol; |
| 3732 | ctx->mpol = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3734 | spin_lock_init(&sbinfo->stat_lock); |
Tejun Heo | 908c7f1 | 2014-09-08 09:51:29 +0900 | [diff] [blame] | 3735 | if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) |
Hugh Dickins | 602586a | 2010-08-17 15:23:56 -0700 | [diff] [blame] | 3736 | goto failed; |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 3737 | spin_lock_init(&sbinfo->shrinklist_lock); |
| 3738 | INIT_LIST_HEAD(&sbinfo->shrinklist); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3739 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 3740 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 3741 | sb->s_blocksize = PAGE_SIZE; |
| 3742 | sb->s_blocksize_bits = PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3743 | sb->s_magic = TMPFS_MAGIC; |
| 3744 | sb->s_op = &shmem_ops; |
Robin H. Johnson | cfd95a9 | 2006-06-12 21:50:25 +0100 | [diff] [blame] | 3745 | sb->s_time_gran = 1; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3746 | #ifdef CONFIG_TMPFS_XATTR |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3747 | sb->s_xattr = shmem_xattr_handlers; |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3748 | #endif |
| 3749 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 3750 | sb->s_flags |= SB_POSIXACL; |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3751 | #endif |
Amir Goldstein | 2b4db79 | 2017-05-18 15:29:33 +0300 | [diff] [blame] | 3752 | uuid_gen(&sb->s_uuid); |
Hugh Dickins | 0edd73b | 2005-06-21 17:15:04 -0700 | [diff] [blame] | 3753 | |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 3754 | inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3755 | if (!inode) |
| 3756 | goto failed; |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3757 | inode->i_uid = sbinfo->uid; |
| 3758 | inode->i_gid = sbinfo->gid; |
Al Viro | 318ceed | 2012-02-12 22:08:01 -0500 | [diff] [blame] | 3759 | sb->s_root = d_make_root(inode); |
| 3760 | if (!sb->s_root) |
Al Viro | 48fde70 | 2012-01-08 22:15:13 -0500 | [diff] [blame] | 3761 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3762 | return 0; |
| 3763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | failed: |
| 3765 | shmem_put_super(sb); |
| 3766 | return err; |
| 3767 | } |
| 3768 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3769 | static int shmem_get_tree(struct fs_context *fc) |
| 3770 | { |
| 3771 | return get_tree_nodev(fc, shmem_fill_super); |
| 3772 | } |
| 3773 | |
| 3774 | static void shmem_free_fc(struct fs_context *fc) |
| 3775 | { |
| 3776 | struct shmem_options *ctx = fc->fs_private; |
| 3777 | |
| 3778 | if (ctx) { |
| 3779 | mpol_put(ctx->mpol); |
| 3780 | kfree(ctx); |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | static const struct fs_context_operations shmem_fs_context_ops = { |
| 3785 | .free = shmem_free_fc, |
| 3786 | .get_tree = shmem_get_tree, |
| 3787 | #ifdef CONFIG_TMPFS |
| 3788 | .parse_monolithic = shmem_parse_options, |
| 3789 | .parse_param = shmem_parse_one, |
| 3790 | .reconfigure = shmem_reconfigure, |
| 3791 | #endif |
| 3792 | }; |
| 3793 | |
Pekka Enberg | fcc234f | 2006-03-22 00:08:13 -0800 | [diff] [blame] | 3794 | static struct kmem_cache *shmem_inode_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3795 | |
| 3796 | static struct inode *shmem_alloc_inode(struct super_block *sb) |
| 3797 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3798 | struct shmem_inode_info *info; |
| 3799 | info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL); |
| 3800 | if (!info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | return NULL; |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3802 | return &info->vfs_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3803 | } |
| 3804 | |
Al Viro | 74b1da5 | 2019-04-15 23:19:05 -0400 | [diff] [blame] | 3805 | static void shmem_free_in_core_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 3806 | { |
Al Viro | 84e710d | 2016-04-15 00:58:55 -0400 | [diff] [blame] | 3807 | if (S_ISLNK(inode->i_mode)) |
| 3808 | kfree(inode->i_link); |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 3809 | kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); |
| 3810 | } |
| 3811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3812 | static void shmem_destroy_inode(struct inode *inode) |
| 3813 | { |
Al Viro | 09208d1 | 2011-07-26 03:15:03 -0400 | [diff] [blame] | 3814 | if (S_ISREG(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3815 | mpol_free_shared_policy(&SHMEM_I(inode)->policy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | } |
| 3817 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3818 | static void shmem_init_inode(void *foo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3819 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3820 | struct shmem_inode_info *info = foo; |
| 3821 | inode_init_once(&info->vfs_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
weiping zhang | 9a8ec03e | 2017-11-15 17:38:18 -0800 | [diff] [blame] | 3824 | static void shmem_init_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3825 | { |
| 3826 | shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", |
| 3827 | sizeof(struct shmem_inode_info), |
Vladimir Davydov | 5d09705 | 2016-01-14 15:18:21 -0800 | [diff] [blame] | 3828 | 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3831 | static void shmem_destroy_inodecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | { |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 3833 | kmem_cache_destroy(shmem_inode_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3834 | } |
| 3835 | |
Christoph Hellwig | f5e54d6 | 2006-06-28 04:26:44 -0700 | [diff] [blame] | 3836 | static const struct address_space_operations shmem_aops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | .writepage = shmem_writepage, |
Ken Chen | 7671932 | 2007-02-10 01:43:15 -0800 | [diff] [blame] | 3838 | .set_page_dirty = __set_page_dirty_no_writeback, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | #ifdef CONFIG_TMPFS |
Nick Piggin | 800d15a | 2007-10-16 01:25:03 -0700 | [diff] [blame] | 3840 | .write_begin = shmem_write_begin, |
| 3841 | .write_end = shmem_write_end, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3842 | #endif |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 3843 | #ifdef CONFIG_MIGRATION |
Lee Schermerhorn | 304dbdb | 2006-04-22 02:35:48 -0700 | [diff] [blame] | 3844 | .migratepage = migrate_page, |
Andrew Morton | 1c93923 | 2014-10-09 15:27:59 -0700 | [diff] [blame] | 3845 | #endif |
Andi Kleen | aa261f5 | 2009-09-16 11:50:16 +0200 | [diff] [blame] | 3846 | .error_remove_page = generic_error_remove_page, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | }; |
| 3848 | |
Helge Deller | 15ad7cd | 2006-12-06 20:40:36 -0800 | [diff] [blame] | 3849 | static const struct file_operations shmem_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3850 | .mmap = shmem_mmap, |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 3851 | .get_unmapped_area = shmem_get_unmapped_area, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3852 | #ifdef CONFIG_TMPFS |
Hugh Dickins | 220f2ac | 2012-12-12 13:52:21 -0800 | [diff] [blame] | 3853 | .llseek = shmem_file_llseek, |
Al Viro | 2ba5bbe | 2014-04-02 20:00:02 -0400 | [diff] [blame] | 3854 | .read_iter = shmem_file_read_iter, |
Al Viro | 8174202 | 2014-04-03 03:17:43 -0400 | [diff] [blame] | 3855 | .write_iter = generic_file_write_iter, |
Christoph Hellwig | 1b061d9 | 2010-05-26 17:53:41 +0200 | [diff] [blame] | 3856 | .fsync = noop_fsync, |
Al Viro | 82c156f | 2016-09-22 23:35:42 -0400 | [diff] [blame] | 3857 | .splice_read = generic_file_splice_read, |
Al Viro | f6cb85d | 2014-04-05 04:38:56 -0400 | [diff] [blame] | 3858 | .splice_write = iter_file_splice_write, |
Hugh Dickins | 83e4fa9 | 2012-05-29 15:06:40 -0700 | [diff] [blame] | 3859 | .fallocate = shmem_fallocate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3860 | #endif |
| 3861 | }; |
| 3862 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3863 | static const struct inode_operations shmem_inode_operations = { |
Yu Zhao | 44a3022 | 2015-09-08 15:03:33 -0700 | [diff] [blame] | 3864 | .getattr = shmem_getattr, |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3865 | .setattr = shmem_setattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3866 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3867 | .listxattr = shmem_listxattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3868 | .set_acl = simple_set_acl, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3869 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | }; |
| 3871 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3872 | static const struct inode_operations shmem_dir_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3873 | #ifdef CONFIG_TMPFS |
| 3874 | .create = shmem_create, |
| 3875 | .lookup = simple_lookup, |
| 3876 | .link = shmem_link, |
| 3877 | .unlink = shmem_unlink, |
| 3878 | .symlink = shmem_symlink, |
| 3879 | .mkdir = shmem_mkdir, |
| 3880 | .rmdir = shmem_rmdir, |
| 3881 | .mknod = shmem_mknod, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 3882 | .rename = shmem_rename2, |
Al Viro | 60545d0 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3883 | .tmpfile = shmem_tmpfile, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3884 | #endif |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3885 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3886 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3887 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3888 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3889 | .setattr = shmem_setattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3890 | .set_acl = simple_set_acl, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3891 | #endif |
| 3892 | }; |
| 3893 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 3894 | static const struct inode_operations shmem_special_inode_operations = { |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3895 | #ifdef CONFIG_TMPFS_XATTR |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3896 | .listxattr = shmem_listxattr, |
Eric Paris | b09e0fa | 2011-05-24 17:12:39 -0700 | [diff] [blame] | 3897 | #endif |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3898 | #ifdef CONFIG_TMPFS_POSIX_ACL |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 3899 | .setattr = shmem_setattr, |
Christoph Hellwig | feda821 | 2013-12-20 05:16:54 -0800 | [diff] [blame] | 3900 | .set_acl = simple_set_acl, |
Andreas Gruenbacher | 39f0247 | 2006-09-29 02:01:35 -0700 | [diff] [blame] | 3901 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | }; |
| 3903 | |
Hugh Dickins | 759b977 | 2007-03-05 00:30:28 -0800 | [diff] [blame] | 3904 | static const struct super_operations shmem_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3905 | .alloc_inode = shmem_alloc_inode, |
Al Viro | 74b1da5 | 2019-04-15 23:19:05 -0400 | [diff] [blame] | 3906 | .free_inode = shmem_free_in_core_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3907 | .destroy_inode = shmem_destroy_inode, |
| 3908 | #ifdef CONFIG_TMPFS |
| 3909 | .statfs = shmem_statfs, |
akpm@linux-foundation.org | 680d794 | 2008-02-08 04:21:48 -0800 | [diff] [blame] | 3910 | .show_options = shmem_show_options, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | #endif |
Al Viro | 1f895f7 | 2010-06-05 19:10:41 -0400 | [diff] [blame] | 3912 | .evict_inode = shmem_evict_inode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | .drop_inode = generic_delete_inode, |
| 3914 | .put_super = shmem_put_super, |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3915 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | 779750d | 2016-07-26 15:26:38 -0700 | [diff] [blame] | 3916 | .nr_cached_objects = shmem_unused_huge_count, |
| 3917 | .free_cached_objects = shmem_unused_huge_scan, |
| 3918 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | }; |
| 3920 | |
Alexey Dobriyan | f0f37e2f | 2009-09-27 22:29:37 +0400 | [diff] [blame] | 3921 | static const struct vm_operations_struct shmem_vm_ops = { |
Nick Piggin | 54cb882 | 2007-07-19 01:46:59 -0700 | [diff] [blame] | 3922 | .fault = shmem_fault, |
Ning Qu | d7c1755 | 2014-04-07 15:37:24 -0700 | [diff] [blame] | 3923 | .map_pages = filemap_map_pages, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | #ifdef CONFIG_NUMA |
| 3925 | .set_policy = shmem_set_policy, |
| 3926 | .get_policy = shmem_get_policy, |
| 3927 | #endif |
Vinayak Menon | 35eacb5 | 2021-03-18 15:20:17 +0530 | [diff] [blame] | 3928 | #ifdef CONFIG_SPECULATIVE_PAGE_FAULT |
| 3929 | .allow_speculation = filemap_allow_speculation, |
| 3930 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | }; |
| 3932 | |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3933 | int shmem_init_fs_context(struct fs_context *fc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3934 | { |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3935 | struct shmem_options *ctx; |
| 3936 | |
| 3937 | ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); |
| 3938 | if (!ctx) |
| 3939 | return -ENOMEM; |
| 3940 | |
| 3941 | ctx->mode = 0777 | S_ISVTX; |
| 3942 | ctx->uid = current_fsuid(); |
| 3943 | ctx->gid = current_fsgid(); |
| 3944 | |
| 3945 | fc->fs_private = ctx; |
| 3946 | fc->ops = &shmem_fs_context_ops; |
| 3947 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | } |
| 3949 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3950 | static struct file_system_type shmem_fs_type = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | .owner = THIS_MODULE, |
| 3952 | .name = "tmpfs", |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3953 | .init_fs_context = shmem_init_fs_context, |
| 3954 | #ifdef CONFIG_TMPFS |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 3955 | .parameters = shmem_fs_parameters, |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 3956 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3957 | .kill_sb = kill_litter_super, |
Matthew Wilcox (Oracle) | 01c7026 | 2020-10-15 20:06:00 -0700 | [diff] [blame] | 3958 | .fs_flags = FS_USERNS_MOUNT | FS_THP_SUPPORT, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3959 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3961 | int __init shmem_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3962 | { |
| 3963 | int error; |
| 3964 | |
weiping zhang | 9a8ec03e | 2017-11-15 17:38:18 -0800 | [diff] [blame] | 3965 | shmem_init_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3967 | error = register_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | if (error) { |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3969 | pr_err("Could not register tmpfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3970 | goto out2; |
| 3971 | } |
Greg Kroah-Hartman | 95dc112 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 3972 | |
Al Viro | ca4e051 | 2013-08-31 12:57:10 -0400 | [diff] [blame] | 3973 | shm_mnt = kern_mount(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | if (IS_ERR(shm_mnt)) { |
| 3975 | error = PTR_ERR(shm_mnt); |
Joe Perches | 1170532 | 2016-03-17 14:19:50 -0700 | [diff] [blame] | 3976 | pr_err("Could not kern_mount tmpfs\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | goto out1; |
| 3978 | } |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3979 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3980 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | 435c0b87 | 2017-08-25 15:55:33 -0700 | [diff] [blame] | 3981 | if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3982 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
| 3983 | else |
| 3984 | shmem_huge = 0; /* just in case it was patched */ |
| 3985 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3986 | return 0; |
| 3987 | |
| 3988 | out1: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3989 | unregister_filesystem(&shmem_fs_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3990 | out2: |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 3991 | shmem_destroy_inodecache(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3992 | shm_mnt = ERR_PTR(error); |
| 3993 | return error; |
| 3994 | } |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 3995 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 3996 | #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 3997 | static ssize_t shmem_enabled_show(struct kobject *kobj, |
| 3998 | struct kobj_attribute *attr, char *buf) |
| 3999 | { |
Colin Ian King | 26083eb6 | 2019-11-30 17:58:04 -0800 | [diff] [blame] | 4000 | static const int values[] = { |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 4001 | SHMEM_HUGE_ALWAYS, |
| 4002 | SHMEM_HUGE_WITHIN_SIZE, |
| 4003 | SHMEM_HUGE_ADVISE, |
| 4004 | SHMEM_HUGE_NEVER, |
| 4005 | SHMEM_HUGE_DENY, |
| 4006 | SHMEM_HUGE_FORCE, |
| 4007 | }; |
| 4008 | int i, count; |
| 4009 | |
| 4010 | for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) { |
| 4011 | const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s "; |
| 4012 | |
| 4013 | count += sprintf(buf + count, fmt, |
| 4014 | shmem_format_huge(values[i])); |
| 4015 | } |
| 4016 | buf[count - 1] = '\n'; |
| 4017 | return count; |
| 4018 | } |
| 4019 | |
| 4020 | static ssize_t shmem_enabled_store(struct kobject *kobj, |
| 4021 | struct kobj_attribute *attr, const char *buf, size_t count) |
| 4022 | { |
| 4023 | char tmp[16]; |
| 4024 | int huge; |
| 4025 | |
| 4026 | if (count + 1 > sizeof(tmp)) |
| 4027 | return -EINVAL; |
| 4028 | memcpy(tmp, buf, count); |
| 4029 | tmp[count] = '\0'; |
| 4030 | if (count && tmp[count - 1] == '\n') |
| 4031 | tmp[count - 1] = '\0'; |
| 4032 | |
| 4033 | huge = shmem_parse_huge(tmp); |
| 4034 | if (huge == -EINVAL) |
| 4035 | return -EINVAL; |
| 4036 | if (!has_transparent_hugepage() && |
| 4037 | huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) |
| 4038 | return -EINVAL; |
| 4039 | |
| 4040 | shmem_huge = huge; |
Kirill A. Shutemov | 435c0b87 | 2017-08-25 15:55:33 -0700 | [diff] [blame] | 4041 | if (shmem_huge > SHMEM_HUGE_DENY) |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 4042 | SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; |
| 4043 | return count; |
| 4044 | } |
| 4045 | |
| 4046 | struct kobj_attribute shmem_enabled_attr = |
| 4047 | __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store); |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 4048 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4049 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 4050 | #ifdef CONFIG_TRANSPARENT_HUGEPAGE |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4051 | bool shmem_huge_enabled(struct vm_area_struct *vma) |
| 4052 | { |
| 4053 | struct inode *inode = file_inode(vma->vm_file); |
| 4054 | struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); |
| 4055 | loff_t i_size; |
| 4056 | pgoff_t off; |
| 4057 | |
Miaohe Lin | b655973 | 2021-06-30 18:47:50 -0700 | [diff] [blame] | 4058 | if (!transhuge_vma_enabled(vma, vma->vm_flags)) |
Yang Shi | c063066 | 2019-07-18 15:57:27 -0700 | [diff] [blame] | 4059 | return false; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4060 | if (shmem_huge == SHMEM_HUGE_FORCE) |
| 4061 | return true; |
| 4062 | if (shmem_huge == SHMEM_HUGE_DENY) |
| 4063 | return false; |
| 4064 | switch (sbinfo->huge) { |
| 4065 | case SHMEM_HUGE_NEVER: |
| 4066 | return false; |
| 4067 | case SHMEM_HUGE_ALWAYS: |
| 4068 | return true; |
| 4069 | case SHMEM_HUGE_WITHIN_SIZE: |
| 4070 | off = round_up(vma->vm_pgoff, HPAGE_PMD_NR); |
| 4071 | i_size = round_up(i_size_read(inode), PAGE_SIZE); |
| 4072 | if (i_size >= HPAGE_PMD_SIZE && |
| 4073 | i_size >> PAGE_SHIFT >= off) |
| 4074 | return true; |
Joe Perches | e4a9bc5 | 2020-04-06 20:08:39 -0700 | [diff] [blame] | 4075 | fallthrough; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4076 | case SHMEM_HUGE_ADVISE: |
| 4077 | /* TODO: implement fadvise() hints */ |
| 4078 | return (vma->vm_flags & VM_HUGEPAGE); |
| 4079 | default: |
| 4080 | VM_BUG_ON(1); |
| 4081 | return false; |
| 4082 | } |
| 4083 | } |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 4084 | #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ |
Kirill A. Shutemov | 5a6e75f | 2016-07-26 15:26:13 -0700 | [diff] [blame] | 4085 | |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4086 | #else /* !CONFIG_SHMEM */ |
| 4087 | |
| 4088 | /* |
| 4089 | * tiny-shmem: simple shmemfs and tmpfs using ramfs code |
| 4090 | * |
| 4091 | * This is intended for small system where the benefits of the full |
| 4092 | * shmem code (swap-backed and resource-limited) are outweighed by |
| 4093 | * their complexity. On systems without swap this code should be |
| 4094 | * effectively equivalent, but much lighter weight. |
| 4095 | */ |
| 4096 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4097 | static struct file_system_type shmem_fs_type = { |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4098 | .name = "tmpfs", |
David Howells | f323562 | 2019-03-25 16:38:31 +0000 | [diff] [blame] | 4099 | .init_fs_context = ramfs_init_fs_context, |
Al Viro | d7167b1 | 2019-09-07 07:23:15 -0400 | [diff] [blame] | 4100 | .parameters = ramfs_fs_parameters, |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4101 | .kill_sb = kill_litter_super, |
Eric W. Biederman | 2b8576c | 2013-01-25 16:32:10 -0800 | [diff] [blame] | 4102 | .fs_flags = FS_USERNS_MOUNT, |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4103 | }; |
| 4104 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4105 | int __init shmem_init(void) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4106 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4107 | BUG_ON(register_filesystem(&shmem_fs_type) != 0); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4108 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4109 | shm_mnt = kern_mount(&shmem_fs_type); |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4110 | BUG_ON(IS_ERR(shm_mnt)); |
| 4111 | |
| 4112 | return 0; |
| 4113 | } |
| 4114 | |
Vineeth Remanan Pillai | b56a2d8 | 2019-03-05 15:47:03 -0800 | [diff] [blame] | 4115 | int shmem_unuse(unsigned int type, bool frontswap, |
| 4116 | unsigned long *fs_pages_to_unuse) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4117 | { |
| 4118 | return 0; |
| 4119 | } |
| 4120 | |
Hugh Dickins | 3f96b79 | 2009-09-21 17:03:37 -0700 | [diff] [blame] | 4121 | int shmem_lock(struct file *file, int lock, struct user_struct *user) |
| 4122 | { |
| 4123 | return 0; |
| 4124 | } |
| 4125 | |
Hugh Dickins | 2451326 | 2012-01-20 14:34:21 -0800 | [diff] [blame] | 4126 | void shmem_unlock_mapping(struct address_space *mapping) |
| 4127 | { |
| 4128 | } |
| 4129 | |
Hugh Dickins | c01d5b3 | 2016-07-26 15:26:15 -0700 | [diff] [blame] | 4130 | #ifdef CONFIG_MMU |
| 4131 | unsigned long shmem_get_unmapped_area(struct file *file, |
| 4132 | unsigned long addr, unsigned long len, |
| 4133 | unsigned long pgoff, unsigned long flags) |
| 4134 | { |
| 4135 | return current->mm->get_unmapped_area(file, addr, len, pgoff, flags); |
| 4136 | } |
| 4137 | #endif |
| 4138 | |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4139 | void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 4140 | { |
Hugh Dickins | 41ffe5d | 2011-08-03 16:21:21 -0700 | [diff] [blame] | 4141 | truncate_inode_pages_range(inode->i_mapping, lstart, lend); |
Hugh Dickins | 94c1e62 | 2011-06-27 16:18:03 -0700 | [diff] [blame] | 4142 | } |
| 4143 | EXPORT_SYMBOL_GPL(shmem_truncate_range); |
| 4144 | |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 4145 | #define shmem_vm_ops generic_file_vm_ops |
| 4146 | #define shmem_file_operations ramfs_file_operations |
Dmitry Monakhov | 454abaf | 2010-03-04 17:32:18 +0300 | [diff] [blame] | 4147 | #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev) |
Hugh Dickins | 0b0a080 | 2009-02-24 20:51:52 +0000 | [diff] [blame] | 4148 | #define shmem_acct_size(flags, size) 0 |
| 4149 | #define shmem_unacct_size(flags, size) do {} while (0) |
Matt Mackall | 853ac43 | 2009-01-06 14:40:20 -0800 | [diff] [blame] | 4150 | |
| 4151 | #endif /* CONFIG_SHMEM */ |
| 4152 | |
| 4153 | /* common code */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4154 | |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4155 | static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size, |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4156 | unsigned long flags, unsigned int i_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4158 | struct inode *inode; |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 4159 | struct file *res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4161 | if (IS_ERR(mnt)) |
| 4162 | return ERR_CAST(mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | |
Hugh Dickins | 285b2c4 | 2011-08-03 16:21:20 -0700 | [diff] [blame] | 4164 | if (size < 0 || size > MAX_LFS_FILESIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | return ERR_PTR(-EINVAL); |
| 4166 | |
| 4167 | if (shmem_acct_size(flags, size)) |
| 4168 | return ERR_PTR(-ENOMEM); |
| 4169 | |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 4170 | inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0, |
| 4171 | flags); |
Al Viro | dac2d1f | 2018-06-09 09:27:41 -0400 | [diff] [blame] | 4172 | if (unlikely(!inode)) { |
| 4173 | shmem_unacct_size(flags, size); |
| 4174 | return ERR_PTR(-ENOSPC); |
| 4175 | } |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4176 | inode->i_flags |= i_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4177 | inode->i_size = size; |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 4178 | clear_nlink(inode); /* It is unlinked */ |
Al Viro | 26567cd | 2013-03-01 20:22:53 -0500 | [diff] [blame] | 4179 | res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 4180 | if (!IS_ERR(res)) |
| 4181 | res = alloc_file_pseudo(inode, mnt, name, O_RDWR, |
| 4182 | &shmem_file_operations); |
Al Viro | 26567cd | 2013-03-01 20:22:53 -0500 | [diff] [blame] | 4183 | if (IS_ERR(res)) |
Al Viro | 93dec2d | 2018-07-08 23:02:03 -0400 | [diff] [blame] | 4184 | iput(inode); |
Al Viro | 6b4d0b2 | 2013-02-14 21:37:26 -0500 | [diff] [blame] | 4185 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | } |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4187 | |
| 4188 | /** |
| 4189 | * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be |
| 4190 | * kernel internal. There will be NO LSM permission checks against the |
| 4191 | * underlying inode. So users of this interface must do LSM checks at a |
Stephen Smalley | e1832f2 | 2015-08-06 15:46:55 -0700 | [diff] [blame] | 4192 | * higher layer. The users are the big_key and shm implementations. LSM |
| 4193 | * checks are provided at the key or shm level rather than the inode. |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4194 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 4195 | * @size: size to be set for the file |
| 4196 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 4197 | */ |
| 4198 | struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) |
| 4199 | { |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4200 | return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
| 4203 | /** |
| 4204 | * shmem_file_setup - get an unlinked file living in tmpfs |
| 4205 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 4206 | * @size: size to be set for the file |
| 4207 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 4208 | */ |
| 4209 | struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) |
| 4210 | { |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4211 | return __shmem_file_setup(shm_mnt, name, size, flags, 0); |
Eric Paris | c727709 | 2013-12-02 11:24:19 +0000 | [diff] [blame] | 4212 | } |
Keith Packard | 395e0dd | 2008-06-20 00:08:06 -0700 | [diff] [blame] | 4213 | EXPORT_SYMBOL_GPL(shmem_file_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4214 | |
Randy Dunlap | 4671181 | 2008-03-19 17:00:41 -0700 | [diff] [blame] | 4215 | /** |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4216 | * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs |
| 4217 | * @mnt: the tmpfs mount where the file will be created |
| 4218 | * @name: name for dentry (to be seen in /proc/<pid>/maps |
| 4219 | * @size: size to be set for the file |
| 4220 | * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size |
| 4221 | */ |
| 4222 | struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, |
| 4223 | loff_t size, unsigned long flags) |
| 4224 | { |
| 4225 | return __shmem_file_setup(mnt, name, size, flags, 0); |
| 4226 | } |
| 4227 | EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); |
| 4228 | |
| 4229 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | * shmem_zero_setup - setup a shared anonymous mapping |
Peter Collingbourne | 45e5530 | 2020-08-06 23:23:37 -0700 | [diff] [blame] | 4231 | * @vma: the vma to be mmapped is prepared by do_mmap |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | */ |
| 4233 | int shmem_zero_setup(struct vm_area_struct *vma) |
| 4234 | { |
| 4235 | struct file *file; |
| 4236 | loff_t size = vma->vm_end - vma->vm_start; |
| 4237 | |
Hugh Dickins | 66fc130 | 2015-06-14 09:48:09 -0700 | [diff] [blame] | 4238 | /* |
Michel Lespinasse | c1e8d7c | 2020-06-08 21:33:54 -0700 | [diff] [blame] | 4239 | * Cloning a new file under mmap_lock leads to a lock ordering conflict |
Hugh Dickins | 66fc130 | 2015-06-14 09:48:09 -0700 | [diff] [blame] | 4240 | * between XFS directory reading and selinux: since this file is only |
| 4241 | * accessible to the user through its mapping, use S_PRIVATE flag to |
| 4242 | * bypass file security, in the same way as shmem_kernel_file_setup(). |
| 4243 | */ |
Matthew Auld | 703321b | 2017-10-06 23:18:13 +0100 | [diff] [blame] | 4244 | file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4245 | if (IS_ERR(file)) |
| 4246 | return PTR_ERR(file); |
| 4247 | |
| 4248 | if (vma->vm_file) |
| 4249 | fput(vma->vm_file); |
| 4250 | vma->vm_file = file; |
| 4251 | vma->vm_ops = &shmem_vm_ops; |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4252 | |
Matthew Wilcox (Oracle) | 396bcc5 | 2020-04-06 20:04:35 -0700 | [diff] [blame] | 4253 | if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && |
Kirill A. Shutemov | f3f0e1d | 2016-07-26 15:26:32 -0700 | [diff] [blame] | 4254 | ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) < |
| 4255 | (vma->vm_end & HPAGE_PMD_MASK)) { |
| 4256 | khugepaged_enter(vma, vma->vm_flags); |
| 4257 | } |
| 4258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | return 0; |
| 4260 | } |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 4261 | |
| 4262 | /** |
| 4263 | * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags. |
| 4264 | * @mapping: the page's address_space |
| 4265 | * @index: the page index |
| 4266 | * @gfp: the page allocator flags to use if allocating |
| 4267 | * |
| 4268 | * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", |
| 4269 | * with any new page allocations done using the specified allocation flags. |
| 4270 | * But read_cache_page_gfp() uses the ->readpage() method: which does not |
| 4271 | * suit tmpfs, since it may have pages in swapcache, and needs to find those |
| 4272 | * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. |
| 4273 | * |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4274 | * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in |
| 4275 | * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 4276 | */ |
| 4277 | struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, |
| 4278 | pgoff_t index, gfp_t gfp) |
| 4279 | { |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4280 | #ifdef CONFIG_SHMEM |
| 4281 | struct inode *inode = mapping->host; |
Hugh Dickins | 9276aad | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4282 | struct page *page; |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4283 | int error; |
| 4284 | |
| 4285 | BUG_ON(mapping->a_ops != &shmem_aops); |
Andres Lagar-Cavilla | 9e18eb2 | 2016-05-19 17:12:47 -0700 | [diff] [blame] | 4286 | error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE, |
Mike Rapoport | cfda052 | 2017-02-22 15:43:37 -0800 | [diff] [blame] | 4287 | gfp, NULL, NULL, NULL); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4288 | if (error) |
| 4289 | page = ERR_PTR(error); |
| 4290 | else |
| 4291 | unlock_page(page); |
| 4292 | return page; |
| 4293 | #else |
| 4294 | /* |
| 4295 | * The tiny !SHMEM case uses ramfs without swap |
| 4296 | */ |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 4297 | return read_cache_page_gfp(mapping, index, gfp); |
Hugh Dickins | 68da9f0 | 2011-07-25 17:12:34 -0700 | [diff] [blame] | 4298 | #endif |
Hugh Dickins | d9d90e5 | 2011-06-27 16:18:04 -0700 | [diff] [blame] | 4299 | } |
| 4300 | EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); |
Vinayak Menon | 9975da5 | 2021-03-18 16:49:18 +0530 | [diff] [blame] | 4301 | |
Charan Teja Reddy | 8011eb2 | 2021-06-25 12:30:50 +0530 | [diff] [blame] | 4302 | void shmem_mark_page_lazyfree(struct page *page, bool tail) |
Vinayak Menon | 9975da5 | 2021-03-18 16:49:18 +0530 | [diff] [blame] | 4303 | { |
Charan Teja Reddy | 8011eb2 | 2021-06-25 12:30:50 +0530 | [diff] [blame] | 4304 | mark_page_lazyfree_movetail(page, tail); |
Vinayak Menon | 9975da5 | 2021-03-18 16:49:18 +0530 | [diff] [blame] | 4305 | } |
| 4306 | EXPORT_SYMBOL_GPL(shmem_mark_page_lazyfree); |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4307 | |
| 4308 | int reclaim_shmem_address_space(struct address_space *mapping) |
| 4309 | { |
Suren Baghdasaryan | 404df47 | 2022-03-24 11:00:05 -0700 | [diff] [blame] | 4310 | #ifdef CONFIG_SHMEM |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4311 | pgoff_t start = 0; |
| 4312 | struct page *page; |
| 4313 | LIST_HEAD(page_list); |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4314 | XA_STATE(xas, &mapping->i_pages, start); |
| 4315 | |
| 4316 | if (!shmem_mapping(mapping)) |
| 4317 | return -EINVAL; |
| 4318 | |
| 4319 | lru_add_drain(); |
| 4320 | |
| 4321 | rcu_read_lock(); |
| 4322 | xas_for_each(&xas, page, ULONG_MAX) { |
| 4323 | if (xas_retry(&xas, page)) |
| 4324 | continue; |
| 4325 | if (xa_is_value(page)) |
| 4326 | continue; |
| 4327 | if (isolate_lru_page(page)) |
| 4328 | continue; |
| 4329 | |
| 4330 | list_add(&page->lru, &page_list); |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4331 | |
| 4332 | if (need_resched()) { |
| 4333 | xas_pause(&xas); |
| 4334 | cond_resched_rcu(); |
| 4335 | } |
| 4336 | } |
| 4337 | rcu_read_unlock(); |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4338 | |
Charan Teja Reddy | 10027474 | 2022-04-07 15:17:24 +0530 | [diff] [blame] | 4339 | return reclaim_pages(&page_list); |
Suren Baghdasaryan | 404df47 | 2022-03-24 11:00:05 -0700 | [diff] [blame] | 4340 | #else |
| 4341 | return 0; |
| 4342 | #endif |
Charan Teja Reddy | daeabfe | 2021-06-28 16:00:03 +0530 | [diff] [blame] | 4343 | } |
| 4344 | EXPORT_SYMBOL_GPL(reclaim_shmem_address_space); |