blob: a0dbe62f8042e751508d7eb0eee392089cb5a787 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Dickins6922c0c2011-08-03 16:21:25 -07009 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
Hugh Dickins0edd73b2005-06-21 17:15:04 -070011 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 Mackall853ac432009-01-06 14:40:20 -080018 * tiny-shmem:
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * This file is released under the GPL.
22 */
23
Matt Mackall853ac432009-01-06 14:40:20 -080024#include <linux/fs.h>
25#include <linux/init.h>
26#include <linux/vfs.h>
27#include <linux/mount.h>
Andrew Morton250297e2013-04-29 15:06:12 -070028#include <linux/ramfs.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070029#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080030#include <linux/file.h>
31#include <linux/mm.h>
Arnd Bergmann46c9a942018-08-17 15:45:09 -070032#include <linux/random.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010033#include <linux/sched/signal.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040034#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080035#include <linux/swap.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080036#include <linux/uio.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070037#include <linux/khugepaged.h>
Mike Kravetz749df872017-09-06 16:24:16 -070038#include <linux/hugetlb.h>
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -080039#include <linux/frontswap.h>
Al Viro626c3922019-09-08 20:28:06 -040040#include <linux/fs_parser.h>
Matt Mackall853ac432009-01-06 14:40:20 -080041
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080042#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
43
Matt Mackall853ac432009-01-06 14:40:20 -080044static struct vfsmount *shm_mnt;
45
46#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * This virtual memory filesystem is heavily based on the ramfs. It
49 * extends ramfs by the ability to use swap and honor resource limits
50 * which makes it a completely usable filesystem.
51 */
52
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070053#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070054#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010055#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080056#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/string.h>
59#include <linux/slab.h>
60#include <linux/backing-dev.h>
61#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070064#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070065#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070066#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070067#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/security.h>
69#include <linux/swapops.h>
70#include <linux/mempolicy.h>
71#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000072#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070073#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070074#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080075#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040076#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070077#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070078#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070079#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080080#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080081#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030082#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070083
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080084#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Mel Gormandd56b042015-11-06 16:28:43 -080086#include "internal.h"
87
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
89#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Pretend that each entry is of this size in directory's i_size */
92#define BOGO_DIRENT_SIZE 20
93
Hugh Dickins69f07ec2011-08-03 16:21:26 -070094/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
95#define SHORT_SYMLINK_LEN 128
96
Hugh Dickins1aac1402012-05-29 15:06:42 -070097/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070098 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
99 * inode->i_private (with i_mutex making sure that it has only one user at
100 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700101 */
102struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700103 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700104 pgoff_t start; /* start of range currently being fallocated */
105 pgoff_t next; /* the next page offset to be fallocated */
106 pgoff_t nr_falloced; /* how many new pages have been fallocated */
107 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
108};
109
Al Viro0b5071d2019-09-08 18:49:18 -0400110struct shmem_options {
111 unsigned long long blocks;
112 unsigned long long inodes;
113 struct mempolicy *mpol;
114 kuid_t uid;
115 kgid_t gid;
116 umode_t mode;
117 int huge;
118 int seen;
119#define SHMEM_SEEN_BLOCKS 1
120#define SHMEM_SEEN_INODES 2
121#define SHMEM_SEEN_HUGE 4
122};
123
Andrew Mortonb76db732008-02-08 04:21:49 -0800124#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800125static unsigned long shmem_default_max_blocks(void)
126{
Arun KSca79b0c2018-12-28 00:34:29 -0800127 return totalram_pages() / 2;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800128}
129
130static unsigned long shmem_default_max_inodes(void)
131{
Arun KSca79b0c2018-12-28 00:34:29 -0800132 unsigned long nr_pages = totalram_pages();
133
134 return min(nr_pages - totalhigh_pages(), nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800135}
Andrew Mortonb76db732008-02-08 04:21:49 -0800136#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800137
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700138static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
139static int shmem_replace_page(struct page **pagep, gfp_t gfp,
140 struct shmem_inode_info *info, pgoff_t index);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -0800141static int shmem_swapin_page(struct inode *inode, pgoff_t index,
142 struct page **pagep, enum sgp_type sgp,
143 gfp_t gfp, struct vm_area_struct *vma,
144 vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700145static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700146 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800147 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700148 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700149
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700150int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700151 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700152{
153 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800154 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
158{
159 return sb->s_fs_info;
160}
161
162/*
163 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
164 * for shared memory and for shared anonymous (/dev/zero) mappings
165 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
166 * consistent with the pre-accounting of private mappings ...
167 */
168static inline int shmem_acct_size(unsigned long flags, loff_t size)
169{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000170 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000171 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static inline void shmem_unacct_size(unsigned long flags, loff_t size)
175{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000176 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 vm_unacct_memory(VM_ACCT(size));
178}
179
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700180static inline int shmem_reacct_size(unsigned long flags,
181 loff_t oldsize, loff_t newsize)
182{
183 if (!(flags & VM_NORESERVE)) {
184 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
185 return security_vm_enough_memory_mm(current->mm,
186 VM_ACCT(newsize) - VM_ACCT(oldsize));
187 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
188 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
189 }
190 return 0;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700195 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
197 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
198 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700199static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700201 if (!(flags & VM_NORESERVE))
202 return 0;
203
204 return security_vm_enough_memory_mm(current->mm,
205 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static inline void shmem_unacct_blocks(unsigned long flags, long pages)
209{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000210 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300211 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Mike Rapoport0f079692017-09-06 16:22:59 -0700214static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
215{
216 struct shmem_inode_info *info = SHMEM_I(inode);
217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218
219 if (shmem_acct_block(info->flags, pages))
220 return false;
221
222 if (sbinfo->max_blocks) {
223 if (percpu_counter_compare(&sbinfo->used_blocks,
224 sbinfo->max_blocks - pages) > 0)
225 goto unacct;
226 percpu_counter_add(&sbinfo->used_blocks, pages);
227 }
228
229 return true;
230
231unacct:
232 shmem_unacct_blocks(info->flags, pages);
233 return false;
234}
235
236static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
237{
238 struct shmem_inode_info *info = SHMEM_I(inode);
239 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
240
241 if (sbinfo->max_blocks)
242 percpu_counter_sub(&sbinfo->used_blocks, pages);
243 shmem_unacct_blocks(info->flags, pages);
244}
245
Hugh Dickins759b9772007-03-05 00:30:28 -0800246static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700247static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800248static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800249static const struct inode_operations shmem_inode_operations;
250static const struct inode_operations shmem_dir_inode_operations;
251static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400252static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700253static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Mike Rapoportb0506e42017-02-22 15:43:28 -0800255bool vma_is_shmem(struct vm_area_struct *vma)
256{
257 return vma->vm_ops == &shmem_vm_ops;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800261static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800263static int shmem_reserve_inode(struct super_block *sb)
264{
265 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
266 if (sbinfo->max_inodes) {
267 spin_lock(&sbinfo->stat_lock);
268 if (!sbinfo->free_inodes) {
269 spin_unlock(&sbinfo->stat_lock);
270 return -ENOSPC;
271 }
272 sbinfo->free_inodes--;
273 spin_unlock(&sbinfo->stat_lock);
274 }
275 return 0;
276}
277
278static void shmem_free_inode(struct super_block *sb)
279{
280 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
281 if (sbinfo->max_inodes) {
282 spin_lock(&sbinfo->stat_lock);
283 sbinfo->free_inodes++;
284 spin_unlock(&sbinfo->stat_lock);
285 }
286}
287
Randy Dunlap46711812008-03-19 17:00:41 -0700288/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700289 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * @inode: inode to recalc
291 *
292 * We have to calculate the free blocks since the mm can drop
293 * undirtied hole pages behind our back.
294 *
295 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
296 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
297 *
298 * It has to be called with the spinlock held.
299 */
300static void shmem_recalc_inode(struct inode *inode)
301{
302 struct shmem_inode_info *info = SHMEM_I(inode);
303 long freed;
304
305 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
306 if (freed > 0) {
307 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700308 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700309 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311}
312
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700313bool shmem_charge(struct inode *inode, long pages)
314{
315 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700316 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700317
Mike Rapoport0f079692017-09-06 16:22:59 -0700318 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700319 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700320
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800321 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
322 inode->i_mapping->nrpages += pages;
323
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700324 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700325 info->alloced += pages;
326 inode->i_blocks += pages * BLOCKS_PER_PAGE;
327 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700328 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700329
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700330 return true;
331}
332
333void shmem_uncharge(struct inode *inode, long pages)
334{
335 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700336 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700337
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800338 /* nrpages adjustment done by __delete_from_page_cache() or caller */
339
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700340 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700341 info->alloced -= pages;
342 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
343 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700344 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700345
Mike Rapoport0f079692017-09-06 16:22:59 -0700346 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700347}
348
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700349/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500350 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700351 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500352static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700353 pgoff_t index, void *expected, void *replacement)
354{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500355 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700356 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700357
358 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700359 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500360 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700361 if (item != expected)
362 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500363 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700364 return 0;
365}
366
367/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700368 * Sometimes, before we decide whether to proceed or to fail, we must check
369 * that an entry was not already brought back from swap by a racing thread.
370 *
371 * Checking page is not enough: by the time a SwapCache page is locked, it
372 * might be reused, and again be SwapCache, using the same swap as before.
373 */
374static bool shmem_confirm_swap(struct address_space *mapping,
375 pgoff_t index, swp_entry_t swap)
376{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500377 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700378}
379
380/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700381 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
382 *
383 * SHMEM_HUGE_NEVER:
384 * disables huge pages for the mount;
385 * SHMEM_HUGE_ALWAYS:
386 * enables huge pages for the mount;
387 * SHMEM_HUGE_WITHIN_SIZE:
388 * only allocate huge pages if the page will be fully within i_size,
389 * also respect fadvise()/madvise() hints;
390 * SHMEM_HUGE_ADVISE:
391 * only allocate huge pages if requested with fadvise()/madvise();
392 */
393
394#define SHMEM_HUGE_NEVER 0
395#define SHMEM_HUGE_ALWAYS 1
396#define SHMEM_HUGE_WITHIN_SIZE 2
397#define SHMEM_HUGE_ADVISE 3
398
399/*
400 * Special values.
401 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
402 *
403 * SHMEM_HUGE_DENY:
404 * disables huge on shm_mnt and all mounts, for emergency use;
405 * SHMEM_HUGE_FORCE:
406 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
407 *
408 */
409#define SHMEM_HUGE_DENY (-1)
410#define SHMEM_HUGE_FORCE (-2)
411
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700412#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700413/* ifdef here to avoid bloating shmem.o when not necessary */
414
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700415static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700416
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700417#if defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700418static int shmem_parse_huge(const char *str)
419{
420 if (!strcmp(str, "never"))
421 return SHMEM_HUGE_NEVER;
422 if (!strcmp(str, "always"))
423 return SHMEM_HUGE_ALWAYS;
424 if (!strcmp(str, "within_size"))
425 return SHMEM_HUGE_WITHIN_SIZE;
426 if (!strcmp(str, "advise"))
427 return SHMEM_HUGE_ADVISE;
428 if (!strcmp(str, "deny"))
429 return SHMEM_HUGE_DENY;
430 if (!strcmp(str, "force"))
431 return SHMEM_HUGE_FORCE;
432 return -EINVAL;
433}
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700434#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700435
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700436#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700437static const char *shmem_format_huge(int huge)
438{
439 switch (huge) {
440 case SHMEM_HUGE_NEVER:
441 return "never";
442 case SHMEM_HUGE_ALWAYS:
443 return "always";
444 case SHMEM_HUGE_WITHIN_SIZE:
445 return "within_size";
446 case SHMEM_HUGE_ADVISE:
447 return "advise";
448 case SHMEM_HUGE_DENY:
449 return "deny";
450 case SHMEM_HUGE_FORCE:
451 return "force";
452 default:
453 VM_BUG_ON(1);
454 return "bad_val";
455 }
456}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800457#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700458
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700459static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
460 struct shrink_control *sc, unsigned long nr_to_split)
461{
462 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800463 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700464 struct inode *inode;
465 struct shmem_inode_info *info;
466 struct page *page;
467 unsigned long batch = sc ? sc->nr_to_scan : 128;
468 int removed = 0, split = 0;
469
470 if (list_empty(&sbinfo->shrinklist))
471 return SHRINK_STOP;
472
473 spin_lock(&sbinfo->shrinklist_lock);
474 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
475 info = list_entry(pos, struct shmem_inode_info, shrinklist);
476
477 /* pin the inode */
478 inode = igrab(&info->vfs_inode);
479
480 /* inode is about to be evicted */
481 if (!inode) {
482 list_del_init(&info->shrinklist);
483 removed++;
484 goto next;
485 }
486
487 /* Check if there's anything to gain */
488 if (round_up(inode->i_size, PAGE_SIZE) ==
489 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800490 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700491 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700492 goto next;
493 }
494
495 list_move(&info->shrinklist, &list);
496next:
497 if (!--batch)
498 break;
499 }
500 spin_unlock(&sbinfo->shrinklist_lock);
501
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800502 list_for_each_safe(pos, next, &to_remove) {
503 info = list_entry(pos, struct shmem_inode_info, shrinklist);
504 inode = &info->vfs_inode;
505 list_del_init(&info->shrinklist);
506 iput(inode);
507 }
508
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700509 list_for_each_safe(pos, next, &list) {
510 int ret;
511
512 info = list_entry(pos, struct shmem_inode_info, shrinklist);
513 inode = &info->vfs_inode;
514
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700515 if (nr_to_split && split >= nr_to_split)
516 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700517
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700518 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700519 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
520 if (!page)
521 goto drop;
522
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700523 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700524 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700525 put_page(page);
526 goto drop;
527 }
528
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700529 /*
530 * Leave the inode on the list if we failed to lock
531 * the page at this time.
532 *
533 * Waiting for the lock may lead to deadlock in the
534 * reclaim path.
535 */
536 if (!trylock_page(page)) {
537 put_page(page);
538 goto leave;
539 }
540
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700541 ret = split_huge_page(page);
542 unlock_page(page);
543 put_page(page);
544
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700545 /* If split failed leave the inode on the list */
546 if (ret)
547 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700548
549 split++;
550drop:
551 list_del_init(&info->shrinklist);
552 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700553leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700554 iput(inode);
555 }
556
557 spin_lock(&sbinfo->shrinklist_lock);
558 list_splice_tail(&list, &sbinfo->shrinklist);
559 sbinfo->shrinklist_len -= removed;
560 spin_unlock(&sbinfo->shrinklist_lock);
561
562 return split;
563}
564
565static long shmem_unused_huge_scan(struct super_block *sb,
566 struct shrink_control *sc)
567{
568 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
569
570 if (!READ_ONCE(sbinfo->shrinklist_len))
571 return SHRINK_STOP;
572
573 return shmem_unused_huge_shrink(sbinfo, sc, 0);
574}
575
576static long shmem_unused_huge_count(struct super_block *sb,
577 struct shrink_control *sc)
578{
579 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
580 return READ_ONCE(sbinfo->shrinklist_len);
581}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700582#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700583
584#define shmem_huge SHMEM_HUGE_DENY
585
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700586static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
587 struct shrink_control *sc, unsigned long nr_to_split)
588{
589 return 0;
590}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700591#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700592
Yang Shi89fdcd22018-06-07 17:06:59 -0700593static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
594{
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700595 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Yang Shi89fdcd22018-06-07 17:06:59 -0700596 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
597 shmem_huge != SHMEM_HUGE_DENY)
598 return true;
599 return false;
600}
601
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700602/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700603 * Like add_to_page_cache_locked, but error if expected item has gone.
604 */
605static int shmem_add_to_page_cache(struct page *page,
606 struct address_space *mapping,
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700607 pgoff_t index, void *expected, gfp_t gfp,
608 struct mm_struct *charge_mm)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700609{
Matthew Wilcox552446a2017-12-01 13:25:14 -0500610 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
611 unsigned long i = 0;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700612 unsigned long nr = compound_nr(page);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700613 int error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700614
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700615 VM_BUG_ON_PAGE(PageTail(page), page);
616 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800617 VM_BUG_ON_PAGE(!PageLocked(page), page);
618 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700619 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700620
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700621 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700622 page->mapping = mapping;
623 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700624
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700625 if (!PageSwapCache(page)) {
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -0700626 error = mem_cgroup_charge(page, charge_mm, gfp);
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700627 if (error) {
628 if (PageTransHuge(page)) {
629 count_vm_event(THP_FILE_FALLBACK);
630 count_vm_event(THP_FILE_FALLBACK_CHARGE);
631 }
632 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700633 }
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700634 }
635 cgroup_throttle_swaprate(page, gfp);
636
Matthew Wilcox552446a2017-12-01 13:25:14 -0500637 do {
638 void *entry;
639 xas_lock_irq(&xas);
640 entry = xas_find_conflict(&xas);
641 if (entry != expected)
642 xas_set_err(&xas, -EEXIST);
643 xas_create_range(&xas);
644 if (xas_error(&xas))
645 goto unlock;
646next:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700647 xas_store(&xas, page);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500648 if (++i < nr) {
649 xas_next(&xas);
650 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700651 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500652 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700653 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700654 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500655 }
656 mapping->nrpages += nr;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700657 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
658 __mod_lruvec_page_state(page, NR_SHMEM, nr);
Matthew Wilcox552446a2017-12-01 13:25:14 -0500659unlock:
660 xas_unlock_irq(&xas);
661 } while (xas_nomem(&xas, gfp));
662
663 if (xas_error(&xas)) {
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700664 error = xas_error(&xas);
665 goto error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700666 }
Matthew Wilcox552446a2017-12-01 13:25:14 -0500667
668 return 0;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700669error:
670 page->mapping = NULL;
671 page_ref_sub(page, nr);
672 return error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700673}
674
675/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700676 * Like delete_from_page_cache, but substitutes swap for page.
677 */
678static void shmem_delete_from_page_cache(struct page *page, void *radswap)
679{
680 struct address_space *mapping = page->mapping;
681 int error;
682
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700683 VM_BUG_ON_PAGE(PageCompound(page), page);
684
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700685 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500686 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700687 page->mapping = NULL;
688 mapping->nrpages--;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700689 __dec_lruvec_page_state(page, NR_FILE_PAGES);
690 __dec_lruvec_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700691 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300692 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700693 BUG_ON(error);
694}
695
696/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500697 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700698 */
699static int shmem_free_swap(struct address_space *mapping,
700 pgoff_t index, void *radswap)
701{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700702 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700703
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500704 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700705 if (old != radswap)
706 return -ENOENT;
707 free_swap_and_cache(radix_to_swp_entry(radswap));
708 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700709}
710
711/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800712 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800713 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800714 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700715 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800716 * as long as the inode doesn't go away and racy results are not a problem.
717 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800718unsigned long shmem_partial_swap_usage(struct address_space *mapping,
719 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800720{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500721 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800722 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800723 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800724
725 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500726 xas_for_each(&xas, page, end - 1) {
727 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700728 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400729 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800730 swapped++;
731
732 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500733 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800734 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800735 }
736 }
737
738 rcu_read_unlock();
739
740 return swapped << PAGE_SHIFT;
741}
742
743/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800744 * Determine (in bytes) how many of the shmem object's pages mapped by the
745 * given vma is swapped out.
746 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700747 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800748 * as long as the inode doesn't go away and racy results are not a problem.
749 */
750unsigned long shmem_swap_usage(struct vm_area_struct *vma)
751{
752 struct inode *inode = file_inode(vma->vm_file);
753 struct shmem_inode_info *info = SHMEM_I(inode);
754 struct address_space *mapping = inode->i_mapping;
755 unsigned long swapped;
756
757 /* Be careful as we don't hold info->lock */
758 swapped = READ_ONCE(info->swapped);
759
760 /*
761 * The easier cases are when the shmem object has nothing in swap, or
762 * the vma maps it whole. Then we can simply use the stats that we
763 * already track.
764 */
765 if (!swapped)
766 return 0;
767
768 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
769 return swapped << PAGE_SHIFT;
770
771 /* Here comes the more involved part */
772 return shmem_partial_swap_usage(mapping,
773 linear_page_index(vma, vma->vm_start),
774 linear_page_index(vma, vma->vm_end));
775}
776
777/*
Hugh Dickins24513262012-01-20 14:34:21 -0800778 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
779 */
780void shmem_unlock_mapping(struct address_space *mapping)
781{
782 struct pagevec pvec;
783 pgoff_t indices[PAGEVEC_SIZE];
784 pgoff_t index = 0;
785
Mel Gorman86679822017-11-15 17:37:52 -0800786 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800787 /*
788 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
789 */
790 while (!mapping_unevictable(mapping)) {
791 /*
792 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
793 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
794 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700795 pvec.nr = find_get_entries(mapping, index,
796 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800797 if (!pvec.nr)
798 break;
799 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700800 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000801 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800802 pagevec_release(&pvec);
803 cond_resched();
804 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700805}
806
807/*
Hugh Dickins71725ed2020-04-06 20:07:57 -0700808 * Check whether a hole-punch or truncation needs to split a huge page,
809 * returning true if no split was required, or the split has been successful.
810 *
811 * Eviction (or truncation to 0 size) should never need to split a huge page;
812 * but in rare cases might do so, if shmem_undo_range() failed to trylock on
813 * head, and then succeeded to trylock on tail.
814 *
815 * A split can only succeed when there are no additional references on the
816 * huge page: so the split below relies upon find_get_entries() having stopped
817 * when it found a subpage of the huge page, without getting further references.
818 */
819static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end)
820{
821 if (!PageTransCompound(page))
822 return true;
823
824 /* Just proceed to delete a huge page wholly within the range punched */
825 if (PageHead(page) &&
826 page->index >= start && page->index + HPAGE_PMD_NR <= end)
827 return true;
828
829 /* Try to split huge page, so we can truly punch the hole or truncate */
830 return split_huge_page(page) >= 0;
831}
832
833/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500834 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700835 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700836 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700837static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
838 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700840 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300842 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
843 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
844 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
845 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700846 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700847 pgoff_t indices[PAGEVEC_SIZE];
848 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700849 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700850 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700852 if (lend == -1)
853 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700854
Mel Gorman86679822017-11-15 17:37:52 -0800855 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700856 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700857 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700858 pvec.nr = find_get_entries(mapping, index,
859 min(end - index, (pgoff_t)PAGEVEC_SIZE),
860 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700861 if (!pvec.nr)
862 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700863 for (i = 0; i < pagevec_count(&pvec); i++) {
864 struct page *page = pvec.pages[i];
865
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700866 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700867 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700868 break;
869
Matthew Wilcox3159f942017-11-03 13:30:42 -0400870 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700871 if (unfalloc)
872 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700873 nr_swaps_freed += !shmem_free_swap(mapping,
874 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700875 continue;
876 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700877
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700878 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
879
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700880 if (!trylock_page(page))
881 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700882
Hugh Dickins71725ed2020-04-06 20:07:57 -0700883 if ((!unfalloc || !PageUptodate(page)) &&
884 page_mapping(page) == mapping) {
885 VM_BUG_ON_PAGE(PageWriteback(page), page);
886 if (shmem_punch_compound(page, start, end))
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700887 truncate_inode_page(mapping, page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700888 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700889 unlock_page(page);
890 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700891 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800892 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700893 cond_resched();
894 index++;
895 }
896
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700897 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700898 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700899 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700900 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300901 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700902 if (start > end) {
903 top = partial_end;
904 partial_end = 0;
905 }
906 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700907 set_page_dirty(page);
908 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300909 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700910 }
911 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700912 if (partial_end) {
913 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700914 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700915 if (page) {
916 zero_user_segment(page, 0, partial_end);
917 set_page_dirty(page);
918 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300919 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700920 }
921 }
922 if (start >= end)
923 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700924
925 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700926 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700927 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700928
929 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700930 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700931 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700932 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700933 /* If all gone or hole-punch or unfalloc, we're done */
934 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700935 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700936 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700937 index = start;
938 continue;
939 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700940 for (i = 0; i < pagevec_count(&pvec); i++) {
941 struct page *page = pvec.pages[i];
942
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700943 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700944 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700945 break;
946
Matthew Wilcox3159f942017-11-03 13:30:42 -0400947 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700948 if (unfalloc)
949 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700950 if (shmem_free_swap(mapping, index, page)) {
951 /* Swap was replaced by page: retry */
952 index--;
953 break;
954 }
955 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700956 continue;
957 }
958
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700959 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700960
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700961 if (!unfalloc || !PageUptodate(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -0700962 if (page_mapping(page) != mapping) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700963 /* Page was replaced by swap: retry */
964 unlock_page(page);
965 index--;
966 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700967 }
Hugh Dickins71725ed2020-04-06 20:07:57 -0700968 VM_BUG_ON_PAGE(PageWriteback(page), page);
969 if (shmem_punch_compound(page, start, end))
970 truncate_inode_page(mapping, page);
Hugh Dickins0783ac92020-04-20 18:14:07 -0700971 else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -0700972 /* Wipe the page and don't get stuck */
973 clear_highpage(page);
974 flush_dcache_page(page);
975 set_page_dirty(page);
976 if (index <
977 round_up(start, HPAGE_PMD_NR))
978 start = index + 1;
979 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700980 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700981 unlock_page(page);
982 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700983 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800984 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700985 index++;
986 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700987
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700988 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700989 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700991 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700992}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700994void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
995{
996 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700997 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700999EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
David Howellsa528d352017-01-31 16:46:22 +00001001static int shmem_getattr(const struct path *path, struct kstat *stat,
1002 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -07001003{
David Howellsa528d352017-01-31 16:46:22 +00001004 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001005 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001006 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001007
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001008 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001009 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001010 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001011 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001012 }
Yu Zhao44a30222015-09-08 15:03:33 -07001013 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001014
1015 if (is_huge_enabled(sb_info))
1016 stat->blksize = HPAGE_PMD_SIZE;
1017
Yu Zhao44a30222015-09-08 15:03:33 -07001018 return 0;
1019}
1020
Hugh Dickins94c1e622011-06-27 16:18:03 -07001021static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
David Howells75c3cfa2015-03-17 22:26:12 +00001023 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001024 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001025 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int error;
1027
Jan Kara31051c82016-05-26 16:55:18 +02001028 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001029 if (error)
1030 return error;
1031
Hugh Dickins94c1e622011-06-27 16:18:03 -07001032 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1033 loff_t oldsize = inode->i_size;
1034 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001035
David Herrmann40e041a2014-08-08 14:25:27 -07001036 /* protected by i_mutex */
1037 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1038 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1039 return -EPERM;
1040
Hugh Dickins94c1e622011-06-27 16:18:03 -07001041 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001042 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1043 oldsize, newsize);
1044 if (error)
1045 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001046 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001047 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001048 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001049 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001050 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001051 if (oldsize > holebegin)
1052 unmap_mapping_range(inode->i_mapping,
1053 holebegin, 0, 1);
1054 if (info->alloced)
1055 shmem_truncate_range(inode,
1056 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001057 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001058 if (oldsize > holebegin)
1059 unmap_mapping_range(inode->i_mapping,
1060 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001061
1062 /*
1063 * Part of the huge page can be beyond i_size: subject
1064 * to shrink under memory pressure.
1065 */
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001066 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001067 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001068 /*
1069 * _careful to defend against unlocked access to
1070 * ->shrink_list in shmem_unused_huge_shrink()
1071 */
1072 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001073 list_add_tail(&info->shrinklist,
1074 &sbinfo->shrinklist);
1075 sbinfo->shrinklist_len++;
1076 }
1077 spin_unlock(&sbinfo->shrinklist_lock);
1078 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001082 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001083 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001084 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return error;
1086}
1087
Al Viro1f895f72010-06-05 19:10:41 -04001088static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001091 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001093 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 shmem_unacct_size(info->flags, inode->i_size);
1095 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001096 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001097 if (!list_empty(&info->shrinklist)) {
1098 spin_lock(&sbinfo->shrinklist_lock);
1099 if (!list_empty(&info->shrinklist)) {
1100 list_del_init(&info->shrinklist);
1101 sbinfo->shrinklist_len--;
1102 }
1103 spin_unlock(&sbinfo->shrinklist_lock);
1104 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001105 while (!list_empty(&info->swaplist)) {
1106 /* Wait while shmem_unuse() is scanning this inode... */
1107 wait_var_event(&info->stop_eviction,
1108 !atomic_read(&info->stop_eviction));
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001109 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001110 /* ...but beware of the race if we peeked too early */
1111 if (!atomic_read(&info->stop_eviction))
1112 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001113 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 }
Al Viro3ed47db2016-01-22 18:08:52 -05001115 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001116
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001117 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001118 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001119 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001120 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001123extern struct swap_info_struct *swap_info[];
1124
1125static int shmem_find_swap_entries(struct address_space *mapping,
1126 pgoff_t start, unsigned int nr_entries,
1127 struct page **entries, pgoff_t *indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001128 unsigned int type, bool frontswap)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001129{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001130 XA_STATE(xas, &mapping->i_pages, start);
1131 struct page *page;
Hugh Dickins87039542019-04-18 17:49:58 -07001132 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001133 unsigned int ret = 0;
1134
1135 if (!nr_entries)
1136 return 0;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001137
1138 rcu_read_lock();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001139 xas_for_each(&xas, page, ULONG_MAX) {
1140 if (xas_retry(&xas, page))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001141 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001142
1143 if (!xa_is_value(page))
1144 continue;
1145
Hugh Dickins87039542019-04-18 17:49:58 -07001146 entry = radix_to_swp_entry(page);
1147 if (swp_type(entry) != type)
1148 continue;
1149 if (frontswap &&
1150 !frontswap_test(swap_info[type], swp_offset(entry)))
1151 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001152
1153 indices[ret] = xas.xa_index;
1154 entries[ret] = page;
1155
1156 if (need_resched()) {
1157 xas_pause(&xas);
1158 cond_resched_rcu();
1159 }
1160 if (++ret == nr_entries)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001161 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001162 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001163 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001164
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001165 return ret;
1166}
1167
1168/*
1169 * Move the swapped pages for an inode to page cache. Returns the count
1170 * of pages swapped in, or the error in case of failure.
1171 */
1172static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
1173 pgoff_t *indices)
1174{
1175 int i = 0;
1176 int ret = 0;
1177 int error = 0;
1178 struct address_space *mapping = inode->i_mapping;
1179
1180 for (i = 0; i < pvec.nr; i++) {
1181 struct page *page = pvec.pages[i];
1182
1183 if (!xa_is_value(page))
1184 continue;
1185 error = shmem_swapin_page(inode, indices[i],
1186 &page, SGP_CACHE,
1187 mapping_gfp_mask(mapping),
1188 NULL, NULL);
1189 if (error == 0) {
1190 unlock_page(page);
1191 put_page(page);
1192 ret++;
1193 }
1194 if (error == -ENOMEM)
1195 break;
1196 error = 0;
1197 }
1198 return error ? error : ret;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001199}
1200
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001201/*
1202 * If swap found in inode, free it and move page from swapcache to filecache.
1203 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001204static int shmem_unuse_inode(struct inode *inode, unsigned int type,
1205 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001207 struct address_space *mapping = inode->i_mapping;
1208 pgoff_t start = 0;
1209 struct pagevec pvec;
1210 pgoff_t indices[PAGEVEC_SIZE];
1211 bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
1212 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001214 pagevec_init(&pvec);
1215 do {
1216 unsigned int nr_entries = PAGEVEC_SIZE;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001217
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001218 if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
1219 nr_entries = *fs_pages_to_unuse;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001220
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001221 pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
1222 pvec.pages, indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001223 type, frontswap);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001224 if (pvec.nr == 0) {
1225 ret = 0;
1226 break;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001227 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001228
1229 ret = shmem_unuse_swap_entries(inode, pvec, indices);
1230 if (ret < 0)
1231 break;
1232
1233 if (frontswap_partial) {
1234 *fs_pages_to_unuse -= ret;
1235 if (*fs_pages_to_unuse == 0) {
1236 ret = FRONTSWAP_PAGES_UNUSED;
1237 break;
1238 }
1239 }
1240
1241 start = indices[pvec.nr - 1];
1242 } while (true);
1243
1244 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245}
1246
1247/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001248 * Read all the shared memory data that resides in the swap
1249 * device 'type' back into memory, so the swap device can be
1250 * unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001252int shmem_unuse(unsigned int type, bool frontswap,
1253 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001255 struct shmem_inode_info *info, *next;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001256 int error = 0;
1257
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001258 if (list_empty(&shmem_swaplist))
1259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001261 mutex_lock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001262 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) {
1263 if (!info->swapped) {
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001264 list_del_init(&info->swaplist);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001265 continue;
1266 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001267 /*
1268 * Drop the swaplist mutex while searching the inode for swap;
1269 * but before doing so, make sure shmem_evict_inode() will not
1270 * remove placeholder inode from swaplist, nor let it be freed
1271 * (igrab() would protect from unlink, but not from unmount).
1272 */
1273 atomic_inc(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001274 mutex_unlock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001275
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001276 error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001277 fs_pages_to_unuse);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001278 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001279
1280 mutex_lock(&shmem_swaplist_mutex);
1281 next = list_next_entry(info, swaplist);
1282 if (!info->swapped)
1283 list_del_init(&info->swaplist);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001284 if (atomic_dec_and_test(&info->stop_eviction))
1285 wake_up_var(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001286 if (error)
Hugh Dickins778dd892011-05-11 15:13:37 -07001287 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001289 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001290
Hugh Dickins778dd892011-05-11 15:13:37 -07001291 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
1294/*
1295 * Move the page from the page cache to the swap cache.
1296 */
1297static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1298{
1299 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001302 swp_entry_t swap;
1303 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001305 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 mapping = page->mapping;
1308 index = page->index;
1309 inode = mapping->host;
1310 info = SHMEM_I(inode);
1311 if (info->flags & VM_LOCKED)
1312 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001313 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 goto redirty;
1315
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001316 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001317 * Our capabilities prevent regular writeback or sync from ever calling
1318 * shmem_writepage; but a stacking filesystem might use ->writepage of
1319 * its underlying filesystem, in which case tmpfs should write out to
1320 * swap only in response to memory pressure, and not for the writeback
1321 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001322 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001323 if (!wbc->for_reclaim) {
1324 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1325 goto redirty;
1326 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001327
1328 /*
1329 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1330 * value into swapfile.c, the only way we can correctly account for a
1331 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001332 *
1333 * That's okay for a page already fallocated earlier, but if we have
1334 * not yet completed the fallocation, then (a) we want to keep track
1335 * of this page in case we have to undo it, and (b) it may not be a
1336 * good idea to continue anyway, once we're pushing into swap. So
1337 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001338 */
1339 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001340 if (inode->i_private) {
1341 struct shmem_falloc *shmem_falloc;
1342 spin_lock(&inode->i_lock);
1343 shmem_falloc = inode->i_private;
1344 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001345 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001346 index >= shmem_falloc->start &&
1347 index < shmem_falloc->next)
1348 shmem_falloc->nr_unswapped++;
1349 else
1350 shmem_falloc = NULL;
1351 spin_unlock(&inode->i_lock);
1352 if (shmem_falloc)
1353 goto redirty;
1354 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001355 clear_highpage(page);
1356 flush_dcache_page(page);
1357 SetPageUptodate(page);
1358 }
1359
Huang Ying38d8b4e2017-07-06 15:37:18 -07001360 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001361 if (!swap.val)
1362 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001363
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001364 /*
1365 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001366 * if it's not already there. Do it now before the page is
1367 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001368 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001369 * we've incremented swapped, because shmem_unuse_inode() will
1370 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001371 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001372 mutex_lock(&shmem_swaplist_mutex);
1373 if (list_empty(&info->swaplist))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001374 list_add(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001375
Yang Shi4afab1c2019-11-30 17:58:07 -08001376 if (add_to_swap_cache(page, swap,
1377 __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001378 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001379 shmem_recalc_inode(inode);
1380 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001381 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001382
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001383 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001384 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1385
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001386 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001387 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001388 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return 0;
1390 }
1391
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001392 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001393 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394redirty:
1395 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001396 if (wbc->for_reclaim)
1397 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1398 unlock_page(page);
1399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400}
1401
Hugh Dickins75edd342016-05-19 17:12:44 -07001402#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001403static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001404{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001405 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001406
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001407 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001408 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001409
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001410 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001411
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001412 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001413}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001414
1415static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1416{
1417 struct mempolicy *mpol = NULL;
1418 if (sbinfo->mpol) {
1419 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1420 mpol = sbinfo->mpol;
1421 mpol_get(mpol);
1422 spin_unlock(&sbinfo->stat_lock);
1423 }
1424 return mpol;
1425}
Hugh Dickins75edd342016-05-19 17:12:44 -07001426#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1427static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1428{
1429}
1430static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1431{
1432 return NULL;
1433}
1434#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1435#ifndef CONFIG_NUMA
1436#define vm_policy vm_private_data
1437#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001438
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001439static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1440 struct shmem_inode_info *info, pgoff_t index)
1441{
1442 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001443 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001444 /* Bias interleave by inode number to distribute better across nodes */
1445 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001446 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1447}
1448
1449static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1450{
1451 /* Drop reference taken by mpol_shared_policy_lookup() */
1452 mpol_cond_put(vma->vm_policy);
1453}
1454
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001455static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1456 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001459 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001460 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001462 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001463 vmf.vma = &pvma;
1464 vmf.address = 0;
1465 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001466 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001467
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001468 return page;
1469}
Mel Gorman18a2f372012-12-05 14:01:41 -08001470
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001471static struct page *shmem_alloc_hugepage(gfp_t gfp,
1472 struct shmem_inode_info *info, pgoff_t index)
1473{
1474 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001475 struct address_space *mapping = info->vfs_inode.i_mapping;
1476 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001477 struct page *page;
1478
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001479 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001480 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1481 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001482 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001483
1484 shmem_pseudo_vma_init(&pvma, info, hindex);
1485 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes19deb762019-09-04 12:54:20 -07001486 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001487 shmem_pseudo_vma_destroy(&pvma);
1488 if (page)
1489 prep_transhuge_page(page);
David Rientjesdcdf11e2020-04-06 20:04:25 -07001490 else
1491 count_vm_event(THP_FILE_FALLBACK);
Mel Gorman18a2f372012-12-05 14:01:41 -08001492 return page;
1493}
1494
1495static struct page *shmem_alloc_page(gfp_t gfp,
1496 struct shmem_inode_info *info, pgoff_t index)
1497{
1498 struct vm_area_struct pvma;
1499 struct page *page;
1500
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001501 shmem_pseudo_vma_init(&pvma, info, index);
1502 page = alloc_page_vma(gfp, &pvma, 0);
1503 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001504
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001505 return page;
1506}
1507
1508static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001509 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001510 pgoff_t index, bool huge)
1511{
Mike Rapoport0f079692017-09-06 16:22:59 -07001512 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001513 struct page *page;
1514 int nr;
1515 int err = -ENOSPC;
1516
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001517 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001518 huge = false;
1519 nr = huge ? HPAGE_PMD_NR : 1;
1520
Mike Rapoport0f079692017-09-06 16:22:59 -07001521 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001522 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001523
1524 if (huge)
1525 page = shmem_alloc_hugepage(gfp, info, index);
1526 else
1527 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001528 if (page) {
1529 __SetPageLocked(page);
1530 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001531 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001532 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001533
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001534 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001535 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001536failed:
1537 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001541 * When a page is moved from swapcache to shmem filecache (either by the
1542 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1543 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1544 * ignorance of the mapping it belongs to. If that mapping has special
1545 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1546 * we may need to copy to a suitable page before moving to filecache.
1547 *
1548 * In a future release, this may well be extended to respect cpuset and
1549 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1550 * but for now it is a simple matter of zone.
1551 */
1552static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1553{
1554 return page_zonenum(page) > gfp_zone(gfp);
1555}
1556
1557static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1558 struct shmem_inode_info *info, pgoff_t index)
1559{
1560 struct page *oldpage, *newpage;
1561 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001562 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001563 pgoff_t swap_index;
1564 int error;
1565
1566 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001567 entry.val = page_private(oldpage);
1568 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001569 swap_mapping = page_mapping(oldpage);
1570
1571 /*
1572 * We have arrived here because our zones are constrained, so don't
1573 * limit chance of success by further cpuset and node constraints.
1574 */
1575 gfp &= ~GFP_CONSTRAINT_MASK;
1576 newpage = shmem_alloc_page(gfp, info, index);
1577 if (!newpage)
1578 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001579
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001580 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001581 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001582 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001583
Hugh Dickins9956edf2016-11-10 10:46:11 -08001584 __SetPageLocked(newpage);
1585 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001586 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001587 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001588 SetPageSwapCache(newpage);
1589
1590 /*
1591 * Our caller will very soon move newpage out of swapcache, but it's
1592 * a nice clean interface for us to replace oldpage by newpage there.
1593 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001594 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001595 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001596 if (!error) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07001597 mem_cgroup_migrate(oldpage, newpage);
1598 __inc_lruvec_page_state(newpage, NR_FILE_PAGES);
1599 __dec_lruvec_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001600 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001601 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001602
Hugh Dickins0142ef62012-06-07 14:21:09 -07001603 if (unlikely(error)) {
1604 /*
1605 * Is this possible? I think not, now that our callers check
1606 * both PageSwapCache and page_private after getting page lock;
1607 * but be defensive. Reverse old to newpage for clear and free.
1608 */
1609 oldpage = newpage;
1610 } else {
Johannes Weiner6058eae2020-06-03 16:02:40 -07001611 lru_cache_add(newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001612 *pagep = newpage;
1613 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001614
1615 ClearPageSwapCache(oldpage);
1616 set_page_private(oldpage, 0);
1617
1618 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001619 put_page(oldpage);
1620 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001621 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001622}
1623
1624/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001625 * Swap in the page pointed to by *pagep.
1626 * Caller has to make sure that *pagep contains a valid swapped page.
1627 * Returns 0 and the page in pagep if success. On failure, returns the
1628 * the error code and NULL in *pagep.
1629 */
1630static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1631 struct page **pagep, enum sgp_type sgp,
1632 gfp_t gfp, struct vm_area_struct *vma,
1633 vm_fault_t *fault_type)
1634{
1635 struct address_space *mapping = inode->i_mapping;
1636 struct shmem_inode_info *info = SHMEM_I(inode);
1637 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001638 struct page *page;
1639 swp_entry_t swap;
1640 int error;
1641
1642 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1643 swap = radix_to_swp_entry(*pagep);
1644 *pagep = NULL;
1645
1646 /* Look it up and read it in.. */
1647 page = lookup_swap_cache(swap, NULL, 0);
1648 if (!page) {
1649 /* Or update major stats only when swapin succeeds?? */
1650 if (fault_type) {
1651 *fault_type |= VM_FAULT_MAJOR;
1652 count_vm_event(PGMAJFAULT);
1653 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1654 }
1655 /* Here we actually start the io */
1656 page = shmem_swapin(swap, gfp, info, index);
1657 if (!page) {
1658 error = -ENOMEM;
1659 goto failed;
1660 }
1661 }
1662
1663 /* We have to do this with page locked to prevent races */
1664 lock_page(page);
1665 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1666 !shmem_confirm_swap(mapping, index, swap)) {
1667 error = -EEXIST;
1668 goto unlock;
1669 }
1670 if (!PageUptodate(page)) {
1671 error = -EIO;
1672 goto failed;
1673 }
1674 wait_on_page_writeback(page);
1675
1676 if (shmem_should_replace_page(page, gfp)) {
1677 error = shmem_replace_page(&page, gfp, info, index);
1678 if (error)
1679 goto failed;
1680 }
1681
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001682 error = shmem_add_to_page_cache(page, mapping, index,
1683 swp_to_radix_entry(swap), gfp,
1684 charge_mm);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001685 if (error)
1686 goto failed;
1687
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001688 spin_lock_irq(&info->lock);
1689 info->swapped--;
1690 shmem_recalc_inode(inode);
1691 spin_unlock_irq(&info->lock);
1692
1693 if (sgp == SGP_WRITE)
1694 mark_page_accessed(page);
1695
1696 delete_from_swap_cache(page);
1697 set_page_dirty(page);
1698 swap_free(swap);
1699
1700 *pagep = page;
1701 return 0;
1702failed:
1703 if (!shmem_confirm_swap(mapping, index, swap))
1704 error = -EEXIST;
1705unlock:
1706 if (page) {
1707 unlock_page(page);
1708 put_page(page);
1709 }
1710
1711 return error;
1712}
1713
1714/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001715 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 *
1717 * If we allocate a new one we do not mark it dirty. That's up to the
1718 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001719 * entry since a page cannot live in both the swap and page cache.
1720 *
Miles Chen28eb3c802019-09-23 15:39:34 -07001721 * vmf and fault_type are only supplied by shmem_fault:
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001722 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001724static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001725 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001726 struct vm_area_struct *vma, struct vm_fault *vmf,
1727 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
1729 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001730 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001732 struct mm_struct *charge_mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001733 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001734 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001735 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001737 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001738 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001740 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001742 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1743 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001745 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001746 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001747 return -EINVAL;
1748 }
1749
1750 sbinfo = SHMEM_SB(inode->i_sb);
1751 charge_mm = vma ? vma->vm_mm : current->mm;
1752
1753 page = find_lock_entry(mapping, index);
1754 if (xa_is_value(page)) {
1755 error = shmem_swapin_page(inode, index, &page,
1756 sgp, gfp, vma, fault_type);
1757 if (error == -EEXIST)
1758 goto repeat;
1759
1760 *pagep = page;
1761 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001762 }
1763
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001764 if (page && sgp == SGP_WRITE)
1765 mark_page_accessed(page);
1766
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001767 /* fallocated page? */
1768 if (page && !PageUptodate(page)) {
1769 if (sgp != SGP_READ)
1770 goto clear;
1771 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001772 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001773 page = NULL;
1774 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001775 if (page || sgp == SGP_READ) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001776 *pagep = page;
1777 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001781 * Fast cache lookup did not find it:
1782 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001784
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001785 if (vma && userfaultfd_missing(vma)) {
1786 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1787 return 0;
1788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001790 /* shmem_symlink() */
1791 if (mapping->a_ops != &shmem_aops)
1792 goto alloc_nohuge;
1793 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1794 goto alloc_nohuge;
1795 if (shmem_huge == SHMEM_HUGE_FORCE)
1796 goto alloc_huge;
1797 switch (sbinfo->huge) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001798 case SHMEM_HUGE_NEVER:
1799 goto alloc_nohuge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001800 case SHMEM_HUGE_WITHIN_SIZE: {
1801 loff_t i_size;
1802 pgoff_t off;
1803
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001804 off = round_up(index, HPAGE_PMD_NR);
1805 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1806 if (i_size >= HPAGE_PMD_SIZE &&
1807 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001808 goto alloc_huge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001809
1810 fallthrough;
1811 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001812 case SHMEM_HUGE_ADVISE:
1813 if (sgp_huge == SGP_HUGE)
1814 goto alloc_huge;
1815 /* TODO: implement fadvise() hints */
1816 goto alloc_nohuge;
1817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001819alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001820 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1821 if (IS_ERR(page)) {
1822alloc_nohuge:
1823 page = shmem_alloc_and_acct_page(gfp, inode,
1824 index, false);
1825 }
1826 if (IS_ERR(page)) {
1827 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001828
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001829 error = PTR_ERR(page);
1830 page = NULL;
1831 if (error != -ENOSPC)
1832 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001833 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001834 * Try to reclaim some space by splitting a huge page
1835 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001836 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001837 while (retry--) {
1838 int ret;
1839
1840 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1841 if (ret == SHRINK_STOP)
1842 break;
1843 if (ret)
1844 goto alloc_nohuge;
1845 }
1846 goto unlock;
1847 }
1848
1849 if (PageTransHuge(page))
1850 hindex = round_down(index, HPAGE_PMD_NR);
1851 else
1852 hindex = index;
1853
1854 if (sgp == SGP_WRITE)
1855 __SetPageReferenced(page);
1856
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001857 error = shmem_add_to_page_cache(page, mapping, hindex,
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001858 NULL, gfp & GFP_RECLAIM_MASK,
1859 charge_mm);
1860 if (error)
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001861 goto unacct;
Johannes Weiner6058eae2020-06-03 16:02:40 -07001862 lru_cache_add(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001863
1864 spin_lock_irq(&info->lock);
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001865 info->alloced += compound_nr(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001866 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1867 shmem_recalc_inode(inode);
1868 spin_unlock_irq(&info->lock);
1869 alloced = true;
1870
1871 if (PageTransHuge(page) &&
1872 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1873 hindex + HPAGE_PMD_NR - 1) {
1874 /*
1875 * Part of the huge page is beyond i_size: subject
1876 * to shrink under memory pressure.
1877 */
1878 spin_lock(&sbinfo->shrinklist_lock);
1879 /*
1880 * _careful to defend against unlocked access to
1881 * ->shrink_list in shmem_unused_huge_shrink()
1882 */
1883 if (list_empty_careful(&info->shrinklist)) {
1884 list_add_tail(&info->shrinklist,
1885 &sbinfo->shrinklist);
1886 sbinfo->shrinklist_len++;
1887 }
1888 spin_unlock(&sbinfo->shrinklist_lock);
1889 }
1890
1891 /*
1892 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1893 */
1894 if (sgp == SGP_FALLOC)
1895 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001896clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001897 /*
1898 * Let SGP_WRITE caller clear ends if write does not fill page;
1899 * but SGP_FALLOC on a page fallocated earlier must initialize
1900 * it now, lest undo on failure cancel our earlier guarantee.
1901 */
1902 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1903 struct page *head = compound_head(page);
1904 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001905
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001906 for (i = 0; i < compound_nr(head); i++) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001907 clear_highpage(head + i);
1908 flush_dcache_page(head + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001909 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001910 SetPageUptodate(head);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001911 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001912
Hugh Dickins54af60422011-08-03 16:21:24 -07001913 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001914 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001915 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001916 if (alloced) {
1917 ClearPageDirty(page);
1918 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001919 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001920 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001921 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001922 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001923 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001924 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001925 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001926 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001927 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001928
Nick Piggind0217ac2007-07-19 01:47:03 -07001929 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001930 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001932unacct:
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001933 shmem_inode_unacct_blocks(inode, compound_nr(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001934
1935 if (PageTransHuge(page)) {
1936 unlock_page(page);
1937 put_page(page);
1938 goto alloc_nohuge;
1939 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001940unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001941 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001942 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001943 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001944 }
1945 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001946 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001947 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001948 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001949 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001950 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001951 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07001952 goto repeat;
1953 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954}
1955
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001956/*
1957 * This is like autoremove_wake_function, but it removes the wait queue
1958 * entry unconditionally - even if something else had already woken the
1959 * target.
1960 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001961static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001962{
1963 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001964 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001965 return ret;
1966}
1967
Souptick Joarder20acce62018-06-07 17:09:17 -07001968static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Dave Jiang11bac802017-02-24 14:56:41 -08001970 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001971 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001972 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001973 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001974 int err;
1975 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001977 /*
1978 * Trinity finds that probing a hole which tmpfs is punching can
1979 * prevent the hole-punch from ever completing: which in turn
1980 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001981 * faulting pages into the hole while it's being punched. Although
1982 * shmem_undo_range() does remove the additions, it may be unable to
1983 * keep up, as each new page needs its own unmap_mapping_range() call,
1984 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1985 *
1986 * It does not matter if we sometimes reach this check just before the
1987 * hole-punch begins, so that one fault then races with the punch:
1988 * we just need to make racing faults a rare case.
1989 *
1990 * The implementation below would be much simpler if we just used a
1991 * standard mutex or completion: but we cannot take i_mutex in fault,
1992 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001993 */
1994 if (unlikely(inode->i_private)) {
1995 struct shmem_falloc *shmem_falloc;
1996
1997 spin_lock(&inode->i_lock);
1998 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001999 if (shmem_falloc &&
2000 shmem_falloc->waitq &&
2001 vmf->pgoff >= shmem_falloc->start &&
2002 vmf->pgoff < shmem_falloc->next) {
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002003 struct file *fpin;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002004 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002005 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002006
2007 ret = VM_FAULT_NOPAGE;
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002008 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2009 if (fpin)
Hugh Dickins8e205f72014-07-23 14:00:10 -07002010 ret = VM_FAULT_RETRY;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002011
2012 shmem_falloc_waitq = shmem_falloc->waitq;
2013 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2014 TASK_UNINTERRUPTIBLE);
2015 spin_unlock(&inode->i_lock);
2016 schedule();
2017
2018 /*
2019 * shmem_falloc_waitq points into the shmem_fallocate()
2020 * stack of the hole-punching task: shmem_falloc_waitq
2021 * is usually invalid by the time we reach here, but
2022 * finish_wait() does not dereference it in that case;
2023 * though i_lock needed lest racing with wake_up_all().
2024 */
2025 spin_lock(&inode->i_lock);
2026 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2027 spin_unlock(&inode->i_lock);
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002028
2029 if (fpin)
2030 fput(fpin);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002031 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002032 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002033 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002034 }
2035
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002036 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002037
2038 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2039 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002040 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002041 else if (vma->vm_flags & VM_HUGEPAGE)
2042 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002043
Souptick Joarder20acce62018-06-07 17:09:17 -07002044 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002045 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002046 if (err)
2047 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002051unsigned long shmem_get_unmapped_area(struct file *file,
2052 unsigned long uaddr, unsigned long len,
2053 unsigned long pgoff, unsigned long flags)
2054{
2055 unsigned long (*get_area)(struct file *,
2056 unsigned long, unsigned long, unsigned long, unsigned long);
2057 unsigned long addr;
2058 unsigned long offset;
2059 unsigned long inflated_len;
2060 unsigned long inflated_addr;
2061 unsigned long inflated_offset;
2062
2063 if (len > TASK_SIZE)
2064 return -ENOMEM;
2065
2066 get_area = current->mm->get_unmapped_area;
2067 addr = get_area(file, uaddr, len, pgoff, flags);
2068
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002069 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002070 return addr;
2071 if (IS_ERR_VALUE(addr))
2072 return addr;
2073 if (addr & ~PAGE_MASK)
2074 return addr;
2075 if (addr > TASK_SIZE - len)
2076 return addr;
2077
2078 if (shmem_huge == SHMEM_HUGE_DENY)
2079 return addr;
2080 if (len < HPAGE_PMD_SIZE)
2081 return addr;
2082 if (flags & MAP_FIXED)
2083 return addr;
2084 /*
2085 * Our priority is to support MAP_SHARED mapped hugely;
2086 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002087 * But if caller specified an address hint and we allocated area there
2088 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002089 */
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002090 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002091 return addr;
2092
2093 if (shmem_huge != SHMEM_HUGE_FORCE) {
2094 struct super_block *sb;
2095
2096 if (file) {
2097 VM_BUG_ON(file->f_op != &shmem_file_operations);
2098 sb = file_inode(file)->i_sb;
2099 } else {
2100 /*
2101 * Called directly from mm/mmap.c, or drivers/char/mem.c
2102 * for "/dev/zero", to create a shared anonymous object.
2103 */
2104 if (IS_ERR(shm_mnt))
2105 return addr;
2106 sb = shm_mnt->mnt_sb;
2107 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002108 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002109 return addr;
2110 }
2111
2112 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2113 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2114 return addr;
2115 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2116 return addr;
2117
2118 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2119 if (inflated_len > TASK_SIZE)
2120 return addr;
2121 if (inflated_len < len)
2122 return addr;
2123
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002124 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002125 if (IS_ERR_VALUE(inflated_addr))
2126 return addr;
2127 if (inflated_addr & ~PAGE_MASK)
2128 return addr;
2129
2130 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2131 inflated_addr += offset - inflated_offset;
2132 if (inflated_offset > offset)
2133 inflated_addr += HPAGE_PMD_SIZE;
2134
2135 if (inflated_addr > TASK_SIZE - len)
2136 return addr;
2137 return inflated_addr;
2138}
2139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002141static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Al Viro496ad9a2013-01-23 17:07:38 -05002143 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002144 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002147static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2148 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Al Viro496ad9a2013-01-23 17:07:38 -05002150 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002151 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002153 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2154 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155}
2156#endif
2157
2158int shmem_lock(struct file *file, int lock, struct user_struct *user)
2159{
Al Viro496ad9a2013-01-23 17:07:38 -05002160 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct shmem_inode_info *info = SHMEM_I(inode);
2162 int retval = -ENOMEM;
2163
Hugh Dickinsea0dfeb2020-04-20 18:14:14 -07002164 /*
2165 * What serializes the accesses to info->flags?
2166 * ipc_lock_object() when called from shmctl_do_lock(),
2167 * no serialization needed when called from shm_destroy().
2168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (lock && !(info->flags & VM_LOCKED)) {
2170 if (!user_shm_lock(inode->i_size, user))
2171 goto out_nomem;
2172 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002173 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 if (!lock && (info->flags & VM_LOCKED) && user) {
2176 user_shm_unlock(inode->i_size, user);
2177 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002178 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return retval;
2184}
2185
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002186static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002188 struct shmem_inode_info *info = SHMEM_I(file_inode(file));
2189
2190 if (info->seals & F_SEAL_FUTURE_WRITE) {
2191 /*
2192 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
2193 * "future write" seal active.
2194 */
2195 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
2196 return -EPERM;
2197
2198 /*
Nicolas Geoffray05d351102019-11-30 17:53:28 -08002199 * Since an F_SEAL_FUTURE_WRITE sealed memfd can be mapped as
2200 * MAP_SHARED and read-only, take care to not allow mprotect to
2201 * revert protections on such mappings. Do this only for shared
2202 * mappings. For private mappings, don't need to mask
2203 * VM_MAYWRITE as we still want them to be COW-writable.
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002204 */
Nicolas Geoffray05d351102019-11-30 17:53:28 -08002205 if (vma->vm_flags & VM_SHARED)
2206 vma->vm_flags &= ~(VM_MAYWRITE);
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002207 }
2208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 file_accessed(file);
2210 vma->vm_ops = &shmem_vm_ops;
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002211 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002212 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2213 (vma->vm_end & HPAGE_PMD_MASK)) {
2214 khugepaged_enter(vma, vma->vm_flags);
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 return 0;
2217}
2218
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002219static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002220 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
2222 struct inode *inode;
2223 struct shmem_inode_info *info;
2224 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2225
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002226 if (shmem_reserve_inode(sb))
2227 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
2229 inode = new_inode(sb);
2230 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002231 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002232 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002234 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002235 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 info = SHMEM_I(inode);
2237 memset(info, 0, (char *)inode - (char *)info);
2238 spin_lock_init(&info->lock);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002239 atomic_set(&info->stop_eviction, 0);
David Herrmann40e041a2014-08-08 14:25:27 -07002240 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002241 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002242 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002244 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002245 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 switch (mode & S_IFMT) {
2248 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002249 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 init_special_inode(inode, mode, dev);
2251 break;
2252 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002253 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 inode->i_op = &shmem_inode_operations;
2255 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002256 mpol_shared_policy_init(&info->policy,
2257 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 break;
2259 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002260 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /* Some things misbehave if size == 0 on a directory */
2262 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2263 inode->i_op = &shmem_dir_inode_operations;
2264 inode->i_fop = &simple_dir_operations;
2265 break;
2266 case S_IFLNK:
2267 /*
2268 * Must not load anything in the rbtree,
2269 * mpol_free_shared_policy will not be called.
2270 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002271 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 break;
2273 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002274
2275 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002276 } else
2277 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return inode;
2279}
2280
Johannes Weiner0cd61442014-04-03 14:47:46 -07002281bool shmem_mapping(struct address_space *mapping)
2282{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002283 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002284}
2285
Mike Rapoport8d103962017-09-06 16:23:02 -07002286static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2287 pmd_t *dst_pmd,
2288 struct vm_area_struct *dst_vma,
2289 unsigned long dst_addr,
2290 unsigned long src_addr,
2291 bool zeropage,
2292 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002293{
2294 struct inode *inode = file_inode(dst_vma->vm_file);
2295 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002296 struct address_space *mapping = inode->i_mapping;
2297 gfp_t gfp = mapping_gfp_mask(mapping);
2298 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002299 spinlock_t *ptl;
2300 void *page_kaddr;
2301 struct page *page;
2302 pte_t _dst_pte, *dst_pte;
2303 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002304 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002305
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002306 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002307 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002308 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002309
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002310 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002311 page = shmem_alloc_page(gfp, info, pgoff);
2312 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002313 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002314
Mike Rapoport8d103962017-09-06 16:23:02 -07002315 if (!zeropage) { /* mcopy_atomic */
2316 page_kaddr = kmap_atomic(page);
2317 ret = copy_from_user(page_kaddr,
2318 (const void __user *)src_addr,
2319 PAGE_SIZE);
2320 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002321
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002322 /* fallback to copy_from_user outside mmap_lock */
Mike Rapoport8d103962017-09-06 16:23:02 -07002323 if (unlikely(ret)) {
2324 *pagep = page;
2325 shmem_inode_unacct_blocks(inode, 1);
2326 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002327 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002328 }
2329 } else { /* mfill_zeropage_atomic */
2330 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002331 }
2332 } else {
2333 page = *pagep;
2334 *pagep = NULL;
2335 }
2336
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002337 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2338 __SetPageLocked(page);
2339 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002340 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002341
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002342 ret = -EFAULT;
2343 offset = linear_page_index(dst_vma, dst_addr);
2344 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2345 if (unlikely(offset >= max_off))
2346 goto out_release;
2347
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002348 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2349 gfp & GFP_RECLAIM_MASK, dst_mm);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002350 if (ret)
2351 goto out_release;
2352
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002353 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2354 if (dst_vma->vm_flags & VM_WRITE)
2355 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002356 else {
2357 /*
2358 * We don't set the pte dirty if the vma has no
2359 * VM_WRITE permission, so mark the page dirty or it
2360 * could be freed from under us. We could do it
2361 * unconditionally before unlock_page(), but doing it
2362 * only if VM_WRITE is not set is faster.
2363 */
2364 set_page_dirty(page);
2365 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002366
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002367 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002368
2369 ret = -EFAULT;
2370 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2371 if (unlikely(offset >= max_off))
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002372 goto out_release_unlock;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002373
2374 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002375 if (!pte_none(*dst_pte))
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002376 goto out_release_unlock;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002377
Johannes Weiner6058eae2020-06-03 16:02:40 -07002378 lru_cache_add(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002379
Yang Shi94b7cc02020-04-20 18:14:17 -07002380 spin_lock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002381 info->alloced++;
2382 inode->i_blocks += BLOCKS_PER_PAGE;
2383 shmem_recalc_inode(inode);
Yang Shi94b7cc02020-04-20 18:14:17 -07002384 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002385
2386 inc_mm_counter(dst_mm, mm_counter_file(page));
2387 page_add_file_rmap(page, false);
2388 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2389
2390 /* No need to invalidate - it was non-present before */
2391 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002392 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002393 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002394 ret = 0;
2395out:
2396 return ret;
Johannes Weiner3fea5a42020-06-03 16:01:41 -07002397out_release_unlock:
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002398 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002399 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002400 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002401out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002402 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002403 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002404out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002405 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002406 goto out;
2407}
2408
Mike Rapoport8d103962017-09-06 16:23:02 -07002409int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2410 pmd_t *dst_pmd,
2411 struct vm_area_struct *dst_vma,
2412 unsigned long dst_addr,
2413 unsigned long src_addr,
2414 struct page **pagep)
2415{
2416 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2417 dst_addr, src_addr, false, pagep);
2418}
2419
2420int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2421 pmd_t *dst_pmd,
2422 struct vm_area_struct *dst_vma,
2423 unsigned long dst_addr)
2424{
2425 struct page *page = NULL;
2426
2427 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2428 dst_addr, 0, true, &page);
2429}
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002432static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002433static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002435#ifdef CONFIG_TMPFS_XATTR
2436static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2437#else
2438#define shmem_initxattrs NULL
2439#endif
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002442shmem_write_begin(struct file *file, struct address_space *mapping,
2443 loff_t pos, unsigned len, unsigned flags,
2444 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445{
Nick Piggin800d15a2007-10-16 01:25:03 -07002446 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002447 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002448 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002449
2450 /* i_mutex is held by caller */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002451 if (unlikely(info->seals & (F_SEAL_GROW |
2452 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2453 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
David Herrmann40e041a2014-08-08 14:25:27 -07002454 return -EPERM;
2455 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2456 return -EPERM;
2457 }
2458
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002459 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002460}
2461
2462static int
2463shmem_write_end(struct file *file, struct address_space *mapping,
2464 loff_t pos, unsigned len, unsigned copied,
2465 struct page *page, void *fsdata)
2466{
2467 struct inode *inode = mapping->host;
2468
Hugh Dickinsd3602442008-02-04 22:28:44 -08002469 if (pos + copied > inode->i_size)
2470 i_size_write(inode, pos + copied);
2471
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002472 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002473 struct page *head = compound_head(page);
2474 if (PageTransCompound(page)) {
2475 int i;
2476
2477 for (i = 0; i < HPAGE_PMD_NR; i++) {
2478 if (head + i == page)
2479 continue;
2480 clear_highpage(head + i);
2481 flush_dcache_page(head + i);
2482 }
2483 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002484 if (copied < PAGE_SIZE) {
2485 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002486 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002487 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002488 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002489 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002490 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002491 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002492 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002493 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002494
Nick Piggin800d15a2007-10-16 01:25:03 -07002495 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496}
2497
Al Viro2ba5bbe2014-04-02 20:00:02 -04002498static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Al Viro6e58e792014-02-03 17:07:03 -05002500 struct file *file = iocb->ki_filp;
2501 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002503 pgoff_t index;
2504 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002505 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002506 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002507 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002508 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002509
2510 /*
2511 * Might this read be for a stacking filesystem? Then when reading
2512 * holes of a sparse file, we actually need to allocate those pages,
2513 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2514 */
Al Viro777eda22014-12-17 04:46:46 -05002515 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002516 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002518 index = *ppos >> PAGE_SHIFT;
2519 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 for (;;) {
2522 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002523 pgoff_t end_index;
2524 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 loff_t i_size = i_size_read(inode);
2526
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002527 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (index > end_index)
2529 break;
2530 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002531 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (nr <= offset)
2533 break;
2534 }
2535
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002536 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002537 if (error) {
2538 if (error == -EINVAL)
2539 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 break;
2541 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002542 if (page) {
2543 if (sgp == SGP_CACHE)
2544 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002545 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
2548 /*
2549 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002550 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002552 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002554 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002556 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (nr <= offset) {
2558 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002559 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 break;
2561 }
2562 }
2563 nr -= offset;
2564
2565 if (page) {
2566 /*
2567 * If users can be writing to this page using arbitrary
2568 * virtual addresses, take care about potential aliasing
2569 * before reading the page on the kernel side.
2570 */
2571 if (mapping_writably_mapped(mapping))
2572 flush_dcache_page(page);
2573 /*
2574 * Mark the page accessed if we read the beginning.
2575 */
2576 if (!offset)
2577 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002578 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002580 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
2583 /*
2584 * Ok, we have the page, and it's up-to-date, so
2585 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002587 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002588 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002590 index += offset >> PAGE_SHIFT;
2591 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002593 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002594 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 break;
Al Viro6e58e792014-02-03 17:07:03 -05002596 if (ret < nr) {
2597 error = -EFAULT;
2598 break;
2599 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 cond_resched();
2601 }
2602
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002603 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002604 file_accessed(file);
2605 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002608/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002609 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002610 */
2611static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002612 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002613{
2614 struct page *page;
2615 struct pagevec pvec;
2616 pgoff_t indices[PAGEVEC_SIZE];
2617 bool done = false;
2618 int i;
2619
Mel Gorman86679822017-11-15 17:37:52 -08002620 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002621 pvec.nr = 1; /* start small: we may be there already */
2622 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002623 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002624 pvec.nr, pvec.pages, indices);
2625 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002626 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002627 index = end;
2628 break;
2629 }
2630 for (i = 0; i < pvec.nr; i++, index++) {
2631 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002632 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002633 done = true;
2634 break;
2635 }
2636 index = indices[i];
2637 }
2638 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002639 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002640 if (!PageUptodate(page))
2641 page = NULL;
2642 }
2643 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002644 (page && whence == SEEK_DATA) ||
2645 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002646 done = true;
2647 break;
2648 }
2649 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002650 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002651 pagevec_release(&pvec);
2652 pvec.nr = PAGEVEC_SIZE;
2653 cond_resched();
2654 }
2655 return index;
2656}
2657
Andrew Morton965c8e52012-12-17 15:59:39 -08002658static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002659{
2660 struct address_space *mapping = file->f_mapping;
2661 struct inode *inode = mapping->host;
2662 pgoff_t start, end;
2663 loff_t new_offset;
2664
Andrew Morton965c8e52012-12-17 15:59:39 -08002665 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2666 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002667 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002668 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002669 /* We're holding i_mutex so we can access i_size directly */
2670
Yufen Yu1a413642018-11-16 15:08:39 -08002671 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002672 offset = -ENXIO;
2673 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002674 start = offset >> PAGE_SHIFT;
2675 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002676 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002677 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002678 if (new_offset > offset) {
2679 if (new_offset < inode->i_size)
2680 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002681 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002682 offset = -ENXIO;
2683 else
2684 offset = inode->i_size;
2685 }
2686 }
2687
Hugh Dickins387aae62013-08-04 11:30:25 -07002688 if (offset >= 0)
2689 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002690 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002691 return offset;
2692}
2693
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002694static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2695 loff_t len)
2696{
Al Viro496ad9a2013-01-23 17:07:38 -05002697 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002698 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002699 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002700 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002701 pgoff_t start, index, end;
2702 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002703
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002704 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2705 return -EOPNOTSUPP;
2706
Al Viro59551022016-01-22 15:40:57 -05002707 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002708
2709 if (mode & FALLOC_FL_PUNCH_HOLE) {
2710 struct address_space *mapping = file->f_mapping;
2711 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2712 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002713 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002714
David Herrmann40e041a2014-08-08 14:25:27 -07002715 /* protected by i_mutex */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002716 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
David Herrmann40e041a2014-08-08 14:25:27 -07002717 error = -EPERM;
2718 goto out;
2719 }
2720
Hugh Dickins8e205f72014-07-23 14:00:10 -07002721 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Junaa71ecd2019-11-30 17:58:11 -08002722 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002723 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2724 spin_lock(&inode->i_lock);
2725 inode->i_private = &shmem_falloc;
2726 spin_unlock(&inode->i_lock);
2727
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002728 if ((u64)unmap_end > (u64)unmap_start)
2729 unmap_mapping_range(mapping, unmap_start,
2730 1 + unmap_end - unmap_start, 0);
2731 shmem_truncate_range(inode, offset, offset + len - 1);
2732 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002733
2734 spin_lock(&inode->i_lock);
2735 inode->i_private = NULL;
2736 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002737 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002738 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002739 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002740 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002741 }
2742
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002743 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2744 error = inode_newsize_ok(inode, offset + len);
2745 if (error)
2746 goto out;
2747
David Herrmann40e041a2014-08-08 14:25:27 -07002748 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2749 error = -EPERM;
2750 goto out;
2751 }
2752
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002753 start = offset >> PAGE_SHIFT;
2754 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002755 /* Try to avoid a swapstorm if len is impossible to satisfy */
2756 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2757 error = -ENOSPC;
2758 goto out;
2759 }
2760
Hugh Dickins8e205f72014-07-23 14:00:10 -07002761 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002762 shmem_falloc.start = start;
2763 shmem_falloc.next = start;
2764 shmem_falloc.nr_falloced = 0;
2765 shmem_falloc.nr_unswapped = 0;
2766 spin_lock(&inode->i_lock);
2767 inode->i_private = &shmem_falloc;
2768 spin_unlock(&inode->i_lock);
2769
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002770 for (index = start; index < end; index++) {
2771 struct page *page;
2772
2773 /*
2774 * Good, the fallocate(2) manpage permits EINTR: we may have
2775 * been interrupted because we are using up too much memory.
2776 */
2777 if (signal_pending(current))
2778 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002779 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2780 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002781 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002782 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002783 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002784 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002785 if (index > start) {
2786 shmem_undo_range(inode,
2787 (loff_t)start << PAGE_SHIFT,
2788 ((loff_t)index << PAGE_SHIFT) - 1, true);
2789 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002790 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002791 }
2792
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002793 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002794 * Inform shmem_writepage() how far we have reached.
2795 * No need for lock or barrier: we have the page lock.
2796 */
2797 shmem_falloc.next++;
2798 if (!PageUptodate(page))
2799 shmem_falloc.nr_falloced++;
2800
2801 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002802 * If !PageUptodate, leave it that way so that freeable pages
2803 * can be recognized if we need to rollback on error later.
2804 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002805 * than free the pages we are allocating (and SGP_CACHE pages
2806 * might still be clean: we now need to mark those dirty too).
2807 */
2808 set_page_dirty(page);
2809 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002810 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002811 cond_resched();
2812 }
2813
2814 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2815 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002816 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002817undone:
2818 spin_lock(&inode->i_lock);
2819 inode->i_private = NULL;
2820 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002821out:
Al Viro59551022016-01-22 15:40:57 -05002822 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002823 return error;
2824}
2825
David Howells726c3342006-06-23 02:02:58 -07002826static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
David Howells726c3342006-06-23 02:02:58 -07002828 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002831 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002833 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002835 buf->f_bavail =
2836 buf->f_bfree = sbinfo->max_blocks -
2837 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002838 }
2839 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 buf->f_files = sbinfo->max_inodes;
2841 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
2843 /* else leave those fields 0 like simple_statfs */
2844 return 0;
2845}
2846
2847/*
2848 * File creation. Allocate an inode, and we're done..
2849 */
2850static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002851shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002853 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int error = -ENOSPC;
2855
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002856 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002858 error = simple_acl_create(dir, inode);
2859 if (error)
2860 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002861 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002862 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002863 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002864 if (error && error != -EOPNOTSUPP)
2865 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002866
Al Viro718deb62009-12-16 19:35:36 -05002867 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002869 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 d_instantiate(dentry, inode);
2871 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
2873 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002874out_iput:
2875 iput(inode);
2876 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
Al Viro60545d02013-06-07 01:20:27 -04002879static int
2880shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2881{
2882 struct inode *inode;
2883 int error = -ENOSPC;
2884
2885 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2886 if (inode) {
2887 error = security_inode_init_security(inode, dir,
2888 NULL,
2889 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002890 if (error && error != -EOPNOTSUPP)
2891 goto out_iput;
2892 error = simple_acl_create(dir, inode);
2893 if (error)
2894 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002895 d_tmpfile(dentry, inode);
2896 }
2897 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002898out_iput:
2899 iput(inode);
2900 return error;
Al Viro60545d02013-06-07 01:20:27 -04002901}
2902
Al Viro18bb1db2011-07-26 01:41:39 -04002903static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 int error;
2906
2907 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2908 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002909 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return 0;
2911}
2912
Al Viro4acdaf22011-07-26 01:42:34 -04002913static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002914 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2917}
2918
2919/*
2920 * Link a file..
2921 */
2922static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2923{
David Howells75c3cfa2015-03-17 22:26:12 +00002924 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e62019-02-22 22:35:32 -08002925 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 /*
2928 * No ordinary (disk based) filesystem counts links as inodes;
2929 * but each new link needs a new dentry, pinning lowmem, and
2930 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002931 * But if an O_TMPFILE file is linked into the tmpfs, the
2932 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002934 if (inode->i_nlink) {
2935 ret = shmem_reserve_inode(inode->i_sb);
2936 if (ret)
2937 goto out;
2938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002941 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002942 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002943 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 dget(dentry); /* Extra pinning count for the created dentry */
2945 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002946out:
2947 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948}
2949
2950static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2951{
David Howells75c3cfa2015-03-17 22:26:12 +00002952 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002954 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2955 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
2957 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002958 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002959 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 dput(dentry); /* Undo the count from "create" - this does all the work */
2961 return 0;
2962}
2963
2964static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2965{
2966 if (!simple_empty(dentry))
2967 return -ENOTEMPTY;
2968
David Howells75c3cfa2015-03-17 22:26:12 +00002969 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002970 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return shmem_unlink(dir, dentry);
2972}
2973
Miklos Szeredi37456772014-07-23 15:15:34 +02002974static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2975{
David Howellse36cb0b2015-01-29 12:02:35 +00002976 bool old_is_dir = d_is_dir(old_dentry);
2977 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002978
2979 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2980 if (old_is_dir) {
2981 drop_nlink(old_dir);
2982 inc_nlink(new_dir);
2983 } else {
2984 drop_nlink(new_dir);
2985 inc_nlink(old_dir);
2986 }
2987 }
2988 old_dir->i_ctime = old_dir->i_mtime =
2989 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002990 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002991 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002992
2993 return 0;
2994}
2995
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002996static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2997{
2998 struct dentry *whiteout;
2999 int error;
3000
3001 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3002 if (!whiteout)
3003 return -ENOMEM;
3004
3005 error = shmem_mknod(old_dir, whiteout,
3006 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3007 dput(whiteout);
3008 if (error)
3009 return error;
3010
3011 /*
3012 * Cheat and hash the whiteout while the old dentry is still in
3013 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3014 *
3015 * d_lookup() will consistently find one of them at this point,
3016 * not sure which one, but that isn't even important.
3017 */
3018 d_rehash(whiteout);
3019 return 0;
3020}
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022/*
3023 * The VFS layer already does all the dentry stuff for rename,
3024 * we just have to decrement the usage count for the target if
3025 * it exists so that the VFS layer correctly free's it when it
3026 * gets overwritten.
3027 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003028static int shmem_rename2(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029{
David Howells75c3cfa2015-03-17 22:26:12 +00003030 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 int they_are_dirs = S_ISDIR(inode->i_mode);
3032
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003033 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003034 return -EINVAL;
3035
Miklos Szeredi37456772014-07-23 15:15:34 +02003036 if (flags & RENAME_EXCHANGE)
3037 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (!simple_empty(new_dentry))
3040 return -ENOTEMPTY;
3041
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003042 if (flags & RENAME_WHITEOUT) {
3043 int error;
3044
3045 error = shmem_whiteout(old_dir, old_dentry);
3046 if (error)
3047 return error;
3048 }
3049
David Howells75c3cfa2015-03-17 22:26:12 +00003050 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003052 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003053 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003054 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003057 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003058 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
3060
3061 old_dir->i_size -= BOGO_DIRENT_SIZE;
3062 new_dir->i_size += BOGO_DIRENT_SIZE;
3063 old_dir->i_ctime = old_dir->i_mtime =
3064 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003065 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 return 0;
3067}
3068
3069static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3070{
3071 int error;
3072 int len;
3073 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003074 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003077 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return -ENAMETOOLONG;
3079
Joe Perches0825a6f2018-06-14 15:27:58 -07003080 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3081 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 if (!inode)
3083 return -ENOSPC;
3084
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003085 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003086 shmem_initxattrs, NULL);
Mateusz Nosek343c3d72020-04-06 20:07:54 -07003087 if (error && error != -EOPNOTSUPP) {
3088 iput(inode);
3089 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003090 }
3091
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003093 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003094 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3095 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003096 iput(inode);
3097 return -ENOMEM;
3098 }
3099 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003101 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003102 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (error) {
3104 iput(inode);
3105 return error;
3106 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003107 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003109 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003110 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003112 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003113 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003116 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 d_instantiate(dentry, inode);
3118 dget(dentry);
3119 return 0;
3120}
3121
Al Virofceef392015-12-29 15:58:39 -05003122static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Al Virofceef392015-12-29 15:58:39 -05003124 mark_page_accessed(arg);
3125 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
Al Viro6b255392015-11-17 10:20:54 -05003128static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003129 struct inode *inode,
3130 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003133 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003134 if (!dentry) {
3135 page = find_get_page(inode->i_mapping, 0);
3136 if (!page)
3137 return ERR_PTR(-ECHILD);
3138 if (!PageUptodate(page)) {
3139 put_page(page);
3140 return ERR_PTR(-ECHILD);
3141 }
3142 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003143 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003144 if (error)
3145 return ERR_PTR(error);
3146 unlock_page(page);
3147 }
Al Virofceef392015-12-29 15:58:39 -05003148 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003149 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Eric Parisb09e0fa2011-05-24 17:12:39 -07003152#ifdef CONFIG_TMPFS_XATTR
3153/*
3154 * Superblocks without xattr inode operations may get some security.* xattr
3155 * support from the LSM "for free". As soon as we have any other xattrs
3156 * like ACLs, we also need to implement the security.* handlers at
3157 * filesystem level, though.
3158 */
3159
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003160/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003161 * Callback for security_inode_init_security() for acquiring xattrs.
3162 */
3163static int shmem_initxattrs(struct inode *inode,
3164 const struct xattr *xattr_array,
3165 void *fs_info)
3166{
3167 struct shmem_inode_info *info = SHMEM_I(inode);
3168 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003169 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003170 size_t len;
3171
3172 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003173 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003174 if (!new_xattr)
3175 return -ENOMEM;
3176
3177 len = strlen(xattr->name) + 1;
3178 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3179 GFP_KERNEL);
3180 if (!new_xattr->name) {
3181 kfree(new_xattr);
3182 return -ENOMEM;
3183 }
3184
3185 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3186 XATTR_SECURITY_PREFIX_LEN);
3187 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3188 xattr->name, len);
3189
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003190 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003191 }
3192
3193 return 0;
3194}
3195
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003196static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003197 struct dentry *unused, struct inode *inode,
3198 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003199{
Al Virob2968212016-04-10 20:48:24 -04003200 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003201
3202 name = xattr_full_name(handler, name);
3203 return simple_xattr_get(&info->xattrs, name, buffer, size);
3204}
3205
3206static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003207 struct dentry *unused, struct inode *inode,
3208 const char *name, const void *value,
3209 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003210{
Al Viro59301222016-05-27 10:19:30 -04003211 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003212
3213 name = xattr_full_name(handler, name);
Daniel Xua46a2292020-03-12 13:03:15 -07003214 return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003215}
3216
3217static const struct xattr_handler shmem_security_xattr_handler = {
3218 .prefix = XATTR_SECURITY_PREFIX,
3219 .get = shmem_xattr_handler_get,
3220 .set = shmem_xattr_handler_set,
3221};
3222
3223static const struct xattr_handler shmem_trusted_xattr_handler = {
3224 .prefix = XATTR_TRUSTED_PREFIX,
3225 .get = shmem_xattr_handler_get,
3226 .set = shmem_xattr_handler_set,
3227};
3228
Eric Parisb09e0fa2011-05-24 17:12:39 -07003229static const struct xattr_handler *shmem_xattr_handlers[] = {
3230#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003231 &posix_acl_access_xattr_handler,
3232 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003233#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003234 &shmem_security_xattr_handler,
3235 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003236 NULL
3237};
3238
Eric Parisb09e0fa2011-05-24 17:12:39 -07003239static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3240{
David Howells75c3cfa2015-03-17 22:26:12 +00003241 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003242 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003243}
3244#endif /* CONFIG_TMPFS_XATTR */
3245
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003246static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003247 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003248#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003249 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251};
3252
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003253static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003254 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003255#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003256 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003257#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003258};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003259
David M. Grimes91828a42006-10-17 00:09:45 -07003260static struct dentry *shmem_get_parent(struct dentry *child)
3261{
3262 return ERR_PTR(-ESTALE);
3263}
3264
3265static int shmem_match(struct inode *ino, void *vfh)
3266{
3267 __u32 *fh = vfh;
3268 __u64 inum = fh[2];
3269 inum = (inum << 32) | fh[1];
3270 return ino->i_ino == inum && fh[0] == ino->i_generation;
3271}
3272
Amir Goldstein12ba7802018-06-07 17:07:15 -07003273/* Find any alias of inode, but prefer a hashed alias */
3274static struct dentry *shmem_find_alias(struct inode *inode)
3275{
3276 struct dentry *alias = d_find_alias(inode);
3277
3278 return alias ?: d_find_any_alias(inode);
3279}
3280
3281
Christoph Hellwig480b1162007-10-21 16:42:13 -07003282static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3283 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003284{
David M. Grimes91828a42006-10-17 00:09:45 -07003285 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003286 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003287 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003288
Christoph Hellwig480b1162007-10-21 16:42:13 -07003289 if (fh_len < 3)
3290 return NULL;
3291
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003292 inum = fid->raw[2];
3293 inum = (inum << 32) | fid->raw[1];
3294
Christoph Hellwig480b1162007-10-21 16:42:13 -07003295 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3296 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003297 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003298 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003299 iput(inode);
3300 }
3301
Christoph Hellwig480b1162007-10-21 16:42:13 -07003302 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003303}
3304
Al Virob0b03822012-04-02 14:34:06 -04003305static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3306 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003307{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303308 if (*len < 3) {
3309 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003310 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303311 }
David M. Grimes91828a42006-10-17 00:09:45 -07003312
Al Viro1d3382cb2010-10-23 15:19:20 -04003313 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003314 /* Unfortunately insert_inode_hash is not idempotent,
3315 * so as we hash inodes here rather than at creation
3316 * time, we need a lock to ensure we only try
3317 * to do it once
3318 */
3319 static DEFINE_SPINLOCK(lock);
3320 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003321 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003322 __insert_inode_hash(inode,
3323 inode->i_ino + inode->i_generation);
3324 spin_unlock(&lock);
3325 }
3326
3327 fh[0] = inode->i_generation;
3328 fh[1] = inode->i_ino;
3329 fh[2] = ((__u64)inode->i_ino) >> 32;
3330
3331 *len = 3;
3332 return 1;
3333}
3334
Christoph Hellwig39655162007-10-21 16:42:17 -07003335static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003336 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003337 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003338 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003339};
3340
Al Viro626c3922019-09-08 20:28:06 -04003341enum shmem_param {
3342 Opt_gid,
3343 Opt_huge,
3344 Opt_mode,
3345 Opt_mpol,
3346 Opt_nr_blocks,
3347 Opt_nr_inodes,
3348 Opt_size,
3349 Opt_uid,
3350};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351
Al Viro5eede622019-12-16 13:33:32 -05003352static const struct constant_table shmem_param_enums_huge[] = {
Al Viro2710c957a2019-09-06 22:12:08 -04003353 {"never", SHMEM_HUGE_NEVER },
3354 {"always", SHMEM_HUGE_ALWAYS },
3355 {"within_size", SHMEM_HUGE_WITHIN_SIZE },
3356 {"advise", SHMEM_HUGE_ADVISE },
Al Viro2710c957a2019-09-06 22:12:08 -04003357 {}
3358};
3359
Al Virod7167b12019-09-07 07:23:15 -04003360const struct fs_parameter_spec shmem_fs_parameters[] = {
Al Viro626c3922019-09-08 20:28:06 -04003361 fsparam_u32 ("gid", Opt_gid),
Al Viro2710c957a2019-09-06 22:12:08 -04003362 fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge),
Al Viro626c3922019-09-08 20:28:06 -04003363 fsparam_u32oct("mode", Opt_mode),
3364 fsparam_string("mpol", Opt_mpol),
3365 fsparam_string("nr_blocks", Opt_nr_blocks),
3366 fsparam_string("nr_inodes", Opt_nr_inodes),
3367 fsparam_string("size", Opt_size),
3368 fsparam_u32 ("uid", Opt_uid),
3369 {}
3370};
3371
David Howellsf3235622019-03-25 16:38:31 +00003372static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
Al Viro626c3922019-09-08 20:28:06 -04003373{
David Howellsf3235622019-03-25 16:38:31 +00003374 struct shmem_options *ctx = fc->fs_private;
Al Viro626c3922019-09-08 20:28:06 -04003375 struct fs_parse_result result;
3376 unsigned long long size;
3377 char *rest;
3378 int opt;
3379
Al Virod7167b12019-09-07 07:23:15 -04003380 opt = fs_parse(fc, shmem_fs_parameters, param, &result);
David Howellsf3235622019-03-25 16:38:31 +00003381 if (opt < 0)
Al Viro626c3922019-09-08 20:28:06 -04003382 return opt;
Al Viro626c3922019-09-08 20:28:06 -04003383
3384 switch (opt) {
3385 case Opt_size:
3386 size = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003387 if (*rest == '%') {
3388 size <<= PAGE_SHIFT;
3389 size *= totalram_pages();
3390 do_div(size, 100);
3391 rest++;
3392 }
3393 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003394 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003395 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE);
3396 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003397 break;
3398 case Opt_nr_blocks:
3399 ctx->blocks = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003400 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003401 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003402 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003403 break;
3404 case Opt_nr_inodes:
3405 ctx->inodes = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003406 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003407 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003408 ctx->seen |= SHMEM_SEEN_INODES;
Al Viro626c3922019-09-08 20:28:06 -04003409 break;
3410 case Opt_mode:
3411 ctx->mode = result.uint_32 & 07777;
3412 break;
3413 case Opt_uid:
3414 ctx->uid = make_kuid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003415 if (!uid_valid(ctx->uid))
Al Viro626c3922019-09-08 20:28:06 -04003416 goto bad_value;
3417 break;
3418 case Opt_gid:
3419 ctx->gid = make_kgid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003420 if (!gid_valid(ctx->gid))
Al Viro626c3922019-09-08 20:28:06 -04003421 goto bad_value;
3422 break;
3423 case Opt_huge:
3424 ctx->huge = result.uint_32;
3425 if (ctx->huge != SHMEM_HUGE_NEVER &&
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003426 !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Al Viro626c3922019-09-08 20:28:06 -04003427 has_transparent_hugepage()))
3428 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003429 ctx->seen |= SHMEM_SEEN_HUGE;
Al Viro626c3922019-09-08 20:28:06 -04003430 break;
3431 case Opt_mpol:
3432 if (IS_ENABLED(CONFIG_NUMA)) {
3433 mpol_put(ctx->mpol);
3434 ctx->mpol = NULL;
3435 if (mpol_parse_str(param->string, &ctx->mpol))
3436 goto bad_value;
3437 break;
3438 }
3439 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003440 }
3441 return 0;
3442
Al Viro626c3922019-09-08 20:28:06 -04003443unsupported_parameter:
Al Virof35aa2b2019-12-21 21:35:55 -05003444 return invalfc(fc, "Unsupported parameter '%s'", param->key);
Al Viro626c3922019-09-08 20:28:06 -04003445bad_value:
Al Virof35aa2b2019-12-21 21:35:55 -05003446 return invalfc(fc, "Bad value for '%s'", param->key);
Al Viroe04dc422019-09-08 19:20:12 -04003447}
3448
David Howellsf3235622019-03-25 16:38:31 +00003449static int shmem_parse_options(struct fs_context *fc, void *data)
Al Viroe04dc422019-09-08 19:20:12 -04003450{
David Howellsf3235622019-03-25 16:38:31 +00003451 char *options = data;
3452
Al Viro33f37c62019-10-09 22:48:01 -04003453 if (options) {
3454 int err = security_sb_eat_lsm_opts(options, &fc->security);
3455 if (err)
3456 return err;
3457 }
3458
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003459 while (options != NULL) {
Al Viro626c3922019-09-08 20:28:06 -04003460 char *this_char = options;
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003461 for (;;) {
3462 /*
3463 * NUL-terminate this option: unfortunately,
3464 * mount options form a comma-separated list,
3465 * but mpol's nodelist may also contain commas.
3466 */
3467 options = strchr(options, ',');
3468 if (options == NULL)
3469 break;
3470 options++;
3471 if (!isdigit(*options)) {
3472 options[-1] = '\0';
3473 break;
3474 }
3475 }
Al Viro626c3922019-09-08 20:28:06 -04003476 if (*this_char) {
3477 char *value = strchr(this_char,'=');
David Howellsf3235622019-03-25 16:38:31 +00003478 size_t len = 0;
Al Viro626c3922019-09-08 20:28:06 -04003479 int err;
3480
3481 if (value) {
3482 *value++ = '\0';
David Howellsf3235622019-03-25 16:38:31 +00003483 len = strlen(value);
Al Viro626c3922019-09-08 20:28:06 -04003484 }
David Howellsf3235622019-03-25 16:38:31 +00003485 err = vfs_parse_fs_string(fc, this_char, value, len);
3486 if (err < 0)
3487 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 }
3490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491}
3492
David Howellsf3235622019-03-25 16:38:31 +00003493/*
3494 * Reconfigure a shmem filesystem.
3495 *
3496 * Note that we disallow change from limited->unlimited blocks/inodes while any
3497 * are in use; but we must separately disallow unlimited->limited, because in
3498 * that case we have no record of how much is already in use.
3499 */
3500static int shmem_reconfigure(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501{
David Howellsf3235622019-03-25 16:38:31 +00003502 struct shmem_options *ctx = fc->fs_private;
3503 struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003504 unsigned long inodes;
David Howellsf3235622019-03-25 16:38:31 +00003505 const char *err;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003506
3507 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003508 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
David Howellsf3235622019-03-25 16:38:31 +00003509 if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) {
3510 if (!sbinfo->max_blocks) {
3511 err = "Cannot retroactively limit size";
Al Viro0b5071d2019-09-08 18:49:18 -04003512 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003513 }
Al Viro0b5071d2019-09-08 18:49:18 -04003514 if (percpu_counter_compare(&sbinfo->used_blocks,
David Howellsf3235622019-03-25 16:38:31 +00003515 ctx->blocks) > 0) {
3516 err = "Too small a size for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003517 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003518 }
Al Viro0b5071d2019-09-08 18:49:18 -04003519 }
David Howellsf3235622019-03-25 16:38:31 +00003520 if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) {
3521 if (!sbinfo->max_inodes) {
3522 err = "Cannot retroactively limit inodes";
Al Viro0b5071d2019-09-08 18:49:18 -04003523 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003524 }
3525 if (ctx->inodes < inodes) {
3526 err = "Too few inodes for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003527 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003528 }
Al Viro0b5071d2019-09-08 18:49:18 -04003529 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003530
David Howellsf3235622019-03-25 16:38:31 +00003531 if (ctx->seen & SHMEM_SEEN_HUGE)
3532 sbinfo->huge = ctx->huge;
3533 if (ctx->seen & SHMEM_SEEN_BLOCKS)
3534 sbinfo->max_blocks = ctx->blocks;
3535 if (ctx->seen & SHMEM_SEEN_INODES) {
3536 sbinfo->max_inodes = ctx->inodes;
3537 sbinfo->free_inodes = ctx->inodes - inodes;
Al Viro0b5071d2019-09-08 18:49:18 -04003538 }
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003539
Greg Thelen5f001102013-02-22 16:36:01 -08003540 /*
3541 * Preserve previous mempolicy unless mpol remount option was specified.
3542 */
David Howellsf3235622019-03-25 16:38:31 +00003543 if (ctx->mpol) {
Greg Thelen5f001102013-02-22 16:36:01 -08003544 mpol_put(sbinfo->mpol);
David Howellsf3235622019-03-25 16:38:31 +00003545 sbinfo->mpol = ctx->mpol; /* transfers initial ref */
3546 ctx->mpol = NULL;
Greg Thelen5f001102013-02-22 16:36:01 -08003547 }
David Howellsf3235622019-03-25 16:38:31 +00003548 spin_unlock(&sbinfo->stat_lock);
3549 return 0;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003550out:
3551 spin_unlock(&sbinfo->stat_lock);
Al Virof35aa2b2019-12-21 21:35:55 -05003552 return invalfc(fc, "%s", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003554
Al Viro34c80b12011-12-08 21:32:45 -05003555static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003556{
Al Viro34c80b12011-12-08 21:32:45 -05003557 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003558
3559 if (sbinfo->max_blocks != shmem_default_max_blocks())
3560 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003561 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003562 if (sbinfo->max_inodes != shmem_default_max_inodes())
3563 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003564 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003565 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003566 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3567 seq_printf(seq, ",uid=%u",
3568 from_kuid_munged(&init_user_ns, sbinfo->uid));
3569 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3570 seq_printf(seq, ",gid=%u",
3571 from_kgid_munged(&init_user_ns, sbinfo->gid));
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003572#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003573 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3574 if (sbinfo->huge)
3575 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3576#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003577 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003578 return 0;
3579}
David Herrmann9183df22014-08-08 14:25:29 -07003580
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003581#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583static void shmem_put_super(struct super_block *sb)
3584{
Hugh Dickins602586a2010-08-17 15:23:56 -07003585 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3586
3587 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003588 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003589 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 sb->s_fs_info = NULL;
3591}
3592
David Howellsf3235622019-03-25 16:38:31 +00003593static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
David Howellsf3235622019-03-25 16:38:31 +00003595 struct shmem_options *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003597 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003598 int err = -ENOMEM;
3599
3600 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003601 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003602 L1_CACHE_BYTES), GFP_KERNEL);
3603 if (!sbinfo)
3604 return -ENOMEM;
3605
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003606 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003608#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 /*
3610 * Per default we only allow half of the physical ram per
3611 * tmpfs instance, limiting inodes to one per page of lowmem;
3612 * but the internal instance is left unlimited.
3613 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003614 if (!(sb->s_flags & SB_KERNMOUNT)) {
David Howellsf3235622019-03-25 16:38:31 +00003615 if (!(ctx->seen & SHMEM_SEEN_BLOCKS))
3616 ctx->blocks = shmem_default_max_blocks();
3617 if (!(ctx->seen & SHMEM_SEEN_INODES))
3618 ctx->inodes = shmem_default_max_inodes();
Al Viroca4e0512013-08-31 12:57:10 -04003619 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003620 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 }
David M. Grimes91828a42006-10-17 00:09:45 -07003622 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003623 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003625 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626#endif
David Howellsf3235622019-03-25 16:38:31 +00003627 sbinfo->max_blocks = ctx->blocks;
3628 sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
3629 sbinfo->uid = ctx->uid;
3630 sbinfo->gid = ctx->gid;
3631 sbinfo->mode = ctx->mode;
3632 sbinfo->huge = ctx->huge;
3633 sbinfo->mpol = ctx->mpol;
3634 ctx->mpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003636 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003637 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003638 goto failed;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003639 spin_lock_init(&sbinfo->shrinklist_lock);
3640 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003641
Hugh Dickins285b2c42011-08-03 16:21:20 -07003642 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003643 sb->s_blocksize = PAGE_SIZE;
3644 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 sb->s_magic = TMPFS_MAGIC;
3646 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003647 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003648#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003649 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003650#endif
3651#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003652 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003653#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003654 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003655
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003656 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (!inode)
3658 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003659 inode->i_uid = sbinfo->uid;
3660 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003661 sb->s_root = d_make_root(inode);
3662 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003663 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return 0;
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666failed:
3667 shmem_put_super(sb);
3668 return err;
3669}
3670
David Howellsf3235622019-03-25 16:38:31 +00003671static int shmem_get_tree(struct fs_context *fc)
3672{
3673 return get_tree_nodev(fc, shmem_fill_super);
3674}
3675
3676static void shmem_free_fc(struct fs_context *fc)
3677{
3678 struct shmem_options *ctx = fc->fs_private;
3679
3680 if (ctx) {
3681 mpol_put(ctx->mpol);
3682 kfree(ctx);
3683 }
3684}
3685
3686static const struct fs_context_operations shmem_fs_context_ops = {
3687 .free = shmem_free_fc,
3688 .get_tree = shmem_get_tree,
3689#ifdef CONFIG_TMPFS
3690 .parse_monolithic = shmem_parse_options,
3691 .parse_param = shmem_parse_one,
3692 .reconfigure = shmem_reconfigure,
3693#endif
3694};
3695
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003696static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
3698static struct inode *shmem_alloc_inode(struct super_block *sb)
3699{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003700 struct shmem_inode_info *info;
3701 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3702 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003704 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
Al Viro74b1da52019-04-15 23:19:05 -04003707static void shmem_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003708{
Al Viro84e710d2016-04-15 00:58:55 -04003709 if (S_ISLNK(inode->i_mode))
3710 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003711 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3712}
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714static void shmem_destroy_inode(struct inode *inode)
3715{
Al Viro09208d12011-07-26 03:15:03 -04003716 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718}
3719
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003720static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003722 struct shmem_inode_info *info = foo;
3723 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003726static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727{
3728 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3729 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003730 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003733static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003735 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003738static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003740 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003742 .write_begin = shmem_write_begin,
3743 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003745#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003746 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003747#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003748 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749};
3750
Helge Deller15ad7cd2006-12-06 20:40:36 -08003751static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003753 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003755 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003756 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003757 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003758 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003759 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003760 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003761 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762#endif
3763};
3764
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003765static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003766 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003767 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003768#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003769 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003770 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003771#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772};
3773
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003774static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775#ifdef CONFIG_TMPFS
3776 .create = shmem_create,
3777 .lookup = simple_lookup,
3778 .link = shmem_link,
3779 .unlink = shmem_unlink,
3780 .symlink = shmem_symlink,
3781 .mkdir = shmem_mkdir,
3782 .rmdir = shmem_rmdir,
3783 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003784 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003785 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003787#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003788 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003789#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003790#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003791 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003792 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003793#endif
3794};
3795
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003796static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003797#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003798 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003799#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003800#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003801 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003802 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804};
3805
Hugh Dickins759b9772007-03-05 00:30:28 -08003806static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 .alloc_inode = shmem_alloc_inode,
Al Viro74b1da52019-04-15 23:19:05 -04003808 .free_inode = shmem_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 .destroy_inode = shmem_destroy_inode,
3810#ifdef CONFIG_TMPFS
3811 .statfs = shmem_statfs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003812 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813#endif
Al Viro1f895f72010-06-05 19:10:41 -04003814 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 .drop_inode = generic_delete_inode,
3816 .put_super = shmem_put_super,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003817#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003818 .nr_cached_objects = shmem_unused_huge_count,
3819 .free_cached_objects = shmem_unused_huge_scan,
3820#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821};
3822
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003823static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003824 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003825 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826#ifdef CONFIG_NUMA
3827 .set_policy = shmem_set_policy,
3828 .get_policy = shmem_get_policy,
3829#endif
3830};
3831
David Howellsf3235622019-03-25 16:38:31 +00003832int shmem_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833{
David Howellsf3235622019-03-25 16:38:31 +00003834 struct shmem_options *ctx;
3835
3836 ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL);
3837 if (!ctx)
3838 return -ENOMEM;
3839
3840 ctx->mode = 0777 | S_ISVTX;
3841 ctx->uid = current_fsuid();
3842 ctx->gid = current_fsgid();
3843
3844 fc->fs_private = ctx;
3845 fc->ops = &shmem_fs_context_ops;
3846 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003849static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 .owner = THIS_MODULE,
3851 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00003852 .init_fs_context = shmem_init_fs_context,
3853#ifdef CONFIG_TMPFS
Al Virod7167b12019-09-07 07:23:15 -04003854 .parameters = shmem_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +00003855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003857 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003860int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
3862 int error;
3863
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003864 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003866 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003868 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 goto out2;
3870 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003871
Al Viroca4e0512013-08-31 12:57:10 -04003872 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 if (IS_ERR(shm_mnt)) {
3874 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003875 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 goto out1;
3877 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003878
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003879#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003880 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003881 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3882 else
3883 shmem_huge = 0; /* just in case it was patched */
3884#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return 0;
3886
3887out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003888 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003890 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 shm_mnt = ERR_PTR(error);
3892 return error;
3893}
Matt Mackall853ac432009-01-06 14:40:20 -08003894
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003895#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003896static ssize_t shmem_enabled_show(struct kobject *kobj,
3897 struct kobj_attribute *attr, char *buf)
3898{
Colin Ian King26083eb62019-11-30 17:58:04 -08003899 static const int values[] = {
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003900 SHMEM_HUGE_ALWAYS,
3901 SHMEM_HUGE_WITHIN_SIZE,
3902 SHMEM_HUGE_ADVISE,
3903 SHMEM_HUGE_NEVER,
3904 SHMEM_HUGE_DENY,
3905 SHMEM_HUGE_FORCE,
3906 };
3907 int i, count;
3908
3909 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3910 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3911
3912 count += sprintf(buf + count, fmt,
3913 shmem_format_huge(values[i]));
3914 }
3915 buf[count - 1] = '\n';
3916 return count;
3917}
3918
3919static ssize_t shmem_enabled_store(struct kobject *kobj,
3920 struct kobj_attribute *attr, const char *buf, size_t count)
3921{
3922 char tmp[16];
3923 int huge;
3924
3925 if (count + 1 > sizeof(tmp))
3926 return -EINVAL;
3927 memcpy(tmp, buf, count);
3928 tmp[count] = '\0';
3929 if (count && tmp[count - 1] == '\n')
3930 tmp[count - 1] = '\0';
3931
3932 huge = shmem_parse_huge(tmp);
3933 if (huge == -EINVAL)
3934 return -EINVAL;
3935 if (!has_transparent_hugepage() &&
3936 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3937 return -EINVAL;
3938
3939 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003940 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003941 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3942 return count;
3943}
3944
3945struct kobj_attribute shmem_enabled_attr =
3946 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003947#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003948
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003949#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003950bool shmem_huge_enabled(struct vm_area_struct *vma)
3951{
3952 struct inode *inode = file_inode(vma->vm_file);
3953 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3954 loff_t i_size;
3955 pgoff_t off;
3956
Yang Shic0630662019-07-18 15:57:27 -07003957 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
3958 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
3959 return false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003960 if (shmem_huge == SHMEM_HUGE_FORCE)
3961 return true;
3962 if (shmem_huge == SHMEM_HUGE_DENY)
3963 return false;
3964 switch (sbinfo->huge) {
3965 case SHMEM_HUGE_NEVER:
3966 return false;
3967 case SHMEM_HUGE_ALWAYS:
3968 return true;
3969 case SHMEM_HUGE_WITHIN_SIZE:
3970 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3971 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3972 if (i_size >= HPAGE_PMD_SIZE &&
3973 i_size >> PAGE_SHIFT >= off)
3974 return true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07003975 fallthrough;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003976 case SHMEM_HUGE_ADVISE:
3977 /* TODO: implement fadvise() hints */
3978 return (vma->vm_flags & VM_HUGEPAGE);
3979 default:
3980 VM_BUG_ON(1);
3981 return false;
3982 }
3983}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003984#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003985
Matt Mackall853ac432009-01-06 14:40:20 -08003986#else /* !CONFIG_SHMEM */
3987
3988/*
3989 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3990 *
3991 * This is intended for small system where the benefits of the full
3992 * shmem code (swap-backed and resource-limited) are outweighed by
3993 * their complexity. On systems without swap this code should be
3994 * effectively equivalent, but much lighter weight.
3995 */
3996
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003997static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003998 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00003999 .init_fs_context = ramfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04004000 .parameters = ramfs_fs_parameters,
Matt Mackall853ac432009-01-06 14:40:20 -08004001 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004002 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08004003};
4004
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004005int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08004006{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004007 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08004008
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004009 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08004010 BUG_ON(IS_ERR(shm_mnt));
4011
4012 return 0;
4013}
4014
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08004015int shmem_unuse(unsigned int type, bool frontswap,
4016 unsigned long *fs_pages_to_unuse)
Matt Mackall853ac432009-01-06 14:40:20 -08004017{
4018 return 0;
4019}
4020
Hugh Dickins3f96b792009-09-21 17:03:37 -07004021int shmem_lock(struct file *file, int lock, struct user_struct *user)
4022{
4023 return 0;
4024}
4025
Hugh Dickins24513262012-01-20 14:34:21 -08004026void shmem_unlock_mapping(struct address_space *mapping)
4027{
4028}
4029
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07004030#ifdef CONFIG_MMU
4031unsigned long shmem_get_unmapped_area(struct file *file,
4032 unsigned long addr, unsigned long len,
4033 unsigned long pgoff, unsigned long flags)
4034{
4035 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4036}
4037#endif
4038
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004039void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07004040{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004041 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07004042}
4043EXPORT_SYMBOL_GPL(shmem_truncate_range);
4044
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004045#define shmem_vm_ops generic_file_vm_ops
4046#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03004047#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004048#define shmem_acct_size(flags, size) 0
4049#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08004050
4051#endif /* CONFIG_SHMEM */
4052
4053/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054
Matthew Auld703321b2017-10-06 23:18:13 +01004055static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00004056 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04004059 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Matthew Auld703321b2017-10-06 23:18:13 +01004061 if (IS_ERR(mnt))
4062 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063
Hugh Dickins285b2c42011-08-03 16:21:20 -07004064 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return ERR_PTR(-EINVAL);
4066
4067 if (shmem_acct_size(flags, size))
4068 return ERR_PTR(-ENOMEM);
4069
Al Viro93dec2d2018-07-08 23:02:03 -04004070 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
4071 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04004072 if (unlikely(!inode)) {
4073 shmem_unacct_size(flags, size);
4074 return ERR_PTR(-ENOSPC);
4075 }
Eric Parisc7277092013-12-02 11:24:19 +00004076 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004078 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004079 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04004080 if (!IS_ERR(res))
4081 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
4082 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05004083 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04004084 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05004085 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
Eric Parisc7277092013-12-02 11:24:19 +00004087
4088/**
4089 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4090 * kernel internal. There will be NO LSM permission checks against the
4091 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004092 * higher layer. The users are the big_key and shm implementations. LSM
4093 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004094 * @name: name for dentry (to be seen in /proc/<pid>/maps
4095 * @size: size to be set for the file
4096 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4097 */
4098struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4099{
Matthew Auld703321b2017-10-06 23:18:13 +01004100 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00004101}
4102
4103/**
4104 * shmem_file_setup - get an unlinked file living in tmpfs
4105 * @name: name for dentry (to be seen in /proc/<pid>/maps
4106 * @size: size to be set for the file
4107 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4108 */
4109struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4110{
Matthew Auld703321b2017-10-06 23:18:13 +01004111 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00004112}
Keith Packard395e0dd2008-06-20 00:08:06 -07004113EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Randy Dunlap46711812008-03-19 17:00:41 -07004115/**
Matthew Auld703321b2017-10-06 23:18:13 +01004116 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4117 * @mnt: the tmpfs mount where the file will be created
4118 * @name: name for dentry (to be seen in /proc/<pid>/maps
4119 * @size: size to be set for the file
4120 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4121 */
4122struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4123 loff_t size, unsigned long flags)
4124{
4125 return __shmem_file_setup(mnt, name, size, flags, 0);
4126}
4127EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4128
4129/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4132 */
4133int shmem_zero_setup(struct vm_area_struct *vma)
4134{
4135 struct file *file;
4136 loff_t size = vma->vm_end - vma->vm_start;
4137
Hugh Dickins66fc1302015-06-14 09:48:09 -07004138 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004139 * Cloning a new file under mmap_lock leads to a lock ordering conflict
Hugh Dickins66fc1302015-06-14 09:48:09 -07004140 * between XFS directory reading and selinux: since this file is only
4141 * accessible to the user through its mapping, use S_PRIVATE flag to
4142 * bypass file security, in the same way as shmem_kernel_file_setup().
4143 */
Matthew Auld703321b2017-10-06 23:18:13 +01004144 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (IS_ERR(file))
4146 return PTR_ERR(file);
4147
4148 if (vma->vm_file)
4149 fput(vma->vm_file);
4150 vma->vm_file = file;
4151 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004152
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004153 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004154 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4155 (vma->vm_end & HPAGE_PMD_MASK)) {
4156 khugepaged_enter(vma, vma->vm_flags);
4157 }
4158
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return 0;
4160}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004161
4162/**
4163 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4164 * @mapping: the page's address_space
4165 * @index: the page index
4166 * @gfp: the page allocator flags to use if allocating
4167 *
4168 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4169 * with any new page allocations done using the specified allocation flags.
4170 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4171 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4172 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4173 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004174 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4175 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004176 */
4177struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4178 pgoff_t index, gfp_t gfp)
4179{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004180#ifdef CONFIG_SHMEM
4181 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004182 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004183 int error;
4184
4185 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004186 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004187 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004188 if (error)
4189 page = ERR_PTR(error);
4190 else
4191 unlock_page(page);
4192 return page;
4193#else
4194 /*
4195 * The tiny !SHMEM case uses ramfs without swap
4196 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004197 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004198#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004199}
4200EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);