blob: 0ed98d632eaa8f9f23ffc62e9bdb55e0408496d9 [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>
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +053041#include <linux/mm_inline.h>
Matt Mackall853ac432009-01-06 14:40:20 -080042
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080043#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
44
Charan Teja Reddy25c7eb42021-07-09 21:59:12 +053045#include "internal.h"
46
Charan Teja Reddy964220d2021-07-06 19:04:50 +053047#undef CREATE_TRACE_POINTS
48#include <trace/hooks/shmem_fs.h>
49
Matt Mackall853ac432009-01-06 14:40:20 -080050static struct vfsmount *shm_mnt;
51
52#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
54 * This virtual memory filesystem is heavily based on the ramfs. It
55 * extends ramfs by the ability to use swap and honor resource limits
56 * which makes it a completely usable filesystem.
57 */
58
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070059#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070060#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010061#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080062#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <linux/string.h>
65#include <linux/slab.h>
66#include <linux/backing-dev.h>
67#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070070#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070071#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070072#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070073#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#include <linux/security.h>
75#include <linux/swapops.h>
76#include <linux/mempolicy.h>
77#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000078#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070079#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070080#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080081#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040082#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070083#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070084#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070085#include <uapi/linux/memfd.h>
Lokesh Gidra84330a52021-05-13 05:11:10 -070086#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080087#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030088#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070089
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080090#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Mel Gormandd56b042015-11-06 16:28:43 -080092#include "internal.h"
93
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030094#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
95#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/* Pretend that each entry is of this size in directory's i_size */
98#define BOGO_DIRENT_SIZE 20
99
Hugh Dickins69f07ec2011-08-03 16:21:26 -0700100/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
101#define SHORT_SYMLINK_LEN 128
102
Hugh Dickins1aac1402012-05-29 15:06:42 -0700103/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -0700104 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
105 * inode->i_private (with i_mutex making sure that it has only one user at
106 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700107 */
108struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700109 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700110 pgoff_t start; /* start of range currently being fallocated */
111 pgoff_t next; /* the next page offset to be fallocated */
112 pgoff_t nr_falloced; /* how many new pages have been fallocated */
113 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
114};
115
Al Viro0b5071d2019-09-08 18:49:18 -0400116struct shmem_options {
117 unsigned long long blocks;
118 unsigned long long inodes;
119 struct mempolicy *mpol;
120 kuid_t uid;
121 kgid_t gid;
122 umode_t mode;
Chris Downea3271f2020-08-06 23:20:25 -0700123 bool full_inums;
Al Viro0b5071d2019-09-08 18:49:18 -0400124 int huge;
125 int seen;
126#define SHMEM_SEEN_BLOCKS 1
127#define SHMEM_SEEN_INODES 2
128#define SHMEM_SEEN_HUGE 4
Chris Downea3271f2020-08-06 23:20:25 -0700129#define SHMEM_SEEN_INUMS 8
Al Viro0b5071d2019-09-08 18:49:18 -0400130};
131
Andrew Mortonb76db732008-02-08 04:21:49 -0800132#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800133static unsigned long shmem_default_max_blocks(void)
134{
Arun KSca79b0c2018-12-28 00:34:29 -0800135 return totalram_pages() / 2;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800136}
137
138static unsigned long shmem_default_max_inodes(void)
139{
Arun KSca79b0c2018-12-28 00:34:29 -0800140 unsigned long nr_pages = totalram_pages();
141
142 return min(nr_pages - totalhigh_pages(), nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800143}
Andrew Mortonb76db732008-02-08 04:21:49 -0800144#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800145
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700146static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
147static int shmem_replace_page(struct page **pagep, gfp_t gfp,
148 struct shmem_inode_info *info, pgoff_t index);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -0800149static int shmem_swapin_page(struct inode *inode, pgoff_t index,
150 struct page **pagep, enum sgp_type sgp,
151 gfp_t gfp, struct vm_area_struct *vma,
152 vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700153static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700154 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800155 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700156 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700157
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700158int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700159 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700160{
161 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800162 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
166{
167 return sb->s_fs_info;
168}
169
170/*
171 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
172 * for shared memory and for shared anonymous (/dev/zero) mappings
173 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
174 * consistent with the pre-accounting of private mappings ...
175 */
176static inline int shmem_acct_size(unsigned long flags, loff_t size)
177{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000178 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000179 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182static inline void shmem_unacct_size(unsigned long flags, loff_t size)
183{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000184 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 vm_unacct_memory(VM_ACCT(size));
186}
187
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700188static inline int shmem_reacct_size(unsigned long flags,
189 loff_t oldsize, loff_t newsize)
190{
191 if (!(flags & VM_NORESERVE)) {
192 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
193 return security_vm_enough_memory_mm(current->mm,
194 VM_ACCT(newsize) - VM_ACCT(oldsize));
195 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
196 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
197 }
198 return 0;
199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700203 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
205 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
206 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700207static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700209 if (!(flags & VM_NORESERVE))
210 return 0;
211
212 return security_vm_enough_memory_mm(current->mm,
213 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
216static inline void shmem_unacct_blocks(unsigned long flags, long pages)
217{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000218 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Mike Rapoport0f079692017-09-06 16:22:59 -0700222static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
223{
224 struct shmem_inode_info *info = SHMEM_I(inode);
225 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
226
227 if (shmem_acct_block(info->flags, pages))
228 return false;
229
230 if (sbinfo->max_blocks) {
231 if (percpu_counter_compare(&sbinfo->used_blocks,
232 sbinfo->max_blocks - pages) > 0)
233 goto unacct;
234 percpu_counter_add(&sbinfo->used_blocks, pages);
235 }
236
237 return true;
238
239unacct:
240 shmem_unacct_blocks(info->flags, pages);
241 return false;
242}
243
244static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
245{
246 struct shmem_inode_info *info = SHMEM_I(inode);
247 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
248
249 if (sbinfo->max_blocks)
250 percpu_counter_sub(&sbinfo->used_blocks, pages);
251 shmem_unacct_blocks(info->flags, pages);
252}
253
Hugh Dickins759b9772007-03-05 00:30:28 -0800254static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700255static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800256static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800257static const struct inode_operations shmem_inode_operations;
258static const struct inode_operations shmem_dir_inode_operations;
259static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400260static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700261static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Mike Rapoportb0506e42017-02-22 15:43:28 -0800263bool vma_is_shmem(struct vm_area_struct *vma)
264{
265 return vma->vm_ops == &shmem_vm_ops;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800269static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Chris Downe809d5f2020-08-06 23:20:20 -0700271/*
272 * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and
273 * produces a novel ino for the newly allocated inode.
274 *
275 * It may also be called when making a hard link to permit the space needed by
276 * each dentry. However, in that case, no new inode number is needed since that
277 * internally draws from another pool of inode numbers (currently global
278 * get_next_ino()). This case is indicated by passing NULL as inop.
279 */
280#define SHMEM_INO_BATCH 1024
281static int shmem_reserve_inode(struct super_block *sb, ino_t *inop)
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800282{
283 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Chris Downe809d5f2020-08-06 23:20:20 -0700284 ino_t ino;
285
286 if (!(sb->s_flags & SB_KERNMOUNT)) {
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800287 spin_lock(&sbinfo->stat_lock);
Byron Stanoszekbb3e96d2020-09-18 21:20:18 -0700288 if (sbinfo->max_inodes) {
289 if (!sbinfo->free_inodes) {
290 spin_unlock(&sbinfo->stat_lock);
291 return -ENOSPC;
292 }
293 sbinfo->free_inodes--;
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800294 }
Chris Downe809d5f2020-08-06 23:20:20 -0700295 if (inop) {
296 ino = sbinfo->next_ino++;
297 if (unlikely(is_zero_ino(ino)))
298 ino = sbinfo->next_ino++;
Chris Downea3271f2020-08-06 23:20:25 -0700299 if (unlikely(!sbinfo->full_inums &&
300 ino > UINT_MAX)) {
Chris Downe809d5f2020-08-06 23:20:20 -0700301 /*
302 * Emulate get_next_ino uint wraparound for
303 * compatibility
304 */
Chris Downea3271f2020-08-06 23:20:25 -0700305 if (IS_ENABLED(CONFIG_64BIT))
306 pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n",
307 __func__, MINOR(sb->s_dev));
308 sbinfo->next_ino = 1;
309 ino = sbinfo->next_ino++;
Chris Downe809d5f2020-08-06 23:20:20 -0700310 }
311 *inop = ino;
312 }
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800313 spin_unlock(&sbinfo->stat_lock);
Chris Downe809d5f2020-08-06 23:20:20 -0700314 } else if (inop) {
315 /*
316 * __shmem_file_setup, one of our callers, is lock-free: it
317 * doesn't hold stat_lock in shmem_reserve_inode since
318 * max_inodes is always 0, and is called from potentially
319 * unknown contexts. As such, use a per-cpu batched allocator
320 * which doesn't require the per-sb stat_lock unless we are at
321 * the batch boundary.
Chris Downea3271f2020-08-06 23:20:25 -0700322 *
323 * We don't need to worry about inode{32,64} since SB_KERNMOUNT
324 * shmem mounts are not exposed to userspace, so we don't need
325 * to worry about things like glibc compatibility.
Chris Downe809d5f2020-08-06 23:20:20 -0700326 */
327 ino_t *next_ino;
328 next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu());
329 ino = *next_ino;
330 if (unlikely(ino % SHMEM_INO_BATCH == 0)) {
331 spin_lock(&sbinfo->stat_lock);
332 ino = sbinfo->next_ino;
333 sbinfo->next_ino += SHMEM_INO_BATCH;
334 spin_unlock(&sbinfo->stat_lock);
335 if (unlikely(is_zero_ino(ino)))
336 ino++;
337 }
338 *inop = ino;
339 *next_ino = ++ino;
340 put_cpu();
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800341 }
Chris Downe809d5f2020-08-06 23:20:20 -0700342
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800343 return 0;
344}
345
346static void shmem_free_inode(struct super_block *sb)
347{
348 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
349 if (sbinfo->max_inodes) {
350 spin_lock(&sbinfo->stat_lock);
351 sbinfo->free_inodes++;
352 spin_unlock(&sbinfo->stat_lock);
353 }
354}
355
Randy Dunlap46711812008-03-19 17:00:41 -0700356/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700357 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * @inode: inode to recalc
359 *
360 * We have to calculate the free blocks since the mm can drop
361 * undirtied hole pages behind our back.
362 *
363 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
364 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
365 *
366 * It has to be called with the spinlock held.
367 */
368static void shmem_recalc_inode(struct inode *inode)
369{
370 struct shmem_inode_info *info = SHMEM_I(inode);
371 long freed;
372
373 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
374 if (freed > 0) {
375 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700376 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700377 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379}
380
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700381bool shmem_charge(struct inode *inode, long pages)
382{
383 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700384 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700385
Mike Rapoport0f079692017-09-06 16:22:59 -0700386 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700387 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700388
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800389 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
390 inode->i_mapping->nrpages += pages;
391
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700392 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700393 info->alloced += pages;
394 inode->i_blocks += pages * BLOCKS_PER_PAGE;
395 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700396 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700397
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700398 return true;
399}
400
401void shmem_uncharge(struct inode *inode, long pages)
402{
403 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700404 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700405
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800406 /* nrpages adjustment done by __delete_from_page_cache() or caller */
407
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700408 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700409 info->alloced -= pages;
410 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
411 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700412 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700413
Mike Rapoport0f079692017-09-06 16:22:59 -0700414 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700415}
416
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700417/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500418 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700419 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500420static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700421 pgoff_t index, void *expected, void *replacement)
422{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500423 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700424 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700425
426 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700427 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500428 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700429 if (item != expected)
430 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500431 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700432 return 0;
433}
434
435/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700436 * Sometimes, before we decide whether to proceed or to fail, we must check
437 * that an entry was not already brought back from swap by a racing thread.
438 *
439 * Checking page is not enough: by the time a SwapCache page is locked, it
440 * might be reused, and again be SwapCache, using the same swap as before.
441 */
442static bool shmem_confirm_swap(struct address_space *mapping,
443 pgoff_t index, swp_entry_t swap)
444{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500445 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700446}
447
448/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700449 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
450 *
451 * SHMEM_HUGE_NEVER:
452 * disables huge pages for the mount;
453 * SHMEM_HUGE_ALWAYS:
454 * enables huge pages for the mount;
455 * SHMEM_HUGE_WITHIN_SIZE:
456 * only allocate huge pages if the page will be fully within i_size,
457 * also respect fadvise()/madvise() hints;
458 * SHMEM_HUGE_ADVISE:
459 * only allocate huge pages if requested with fadvise()/madvise();
460 */
461
462#define SHMEM_HUGE_NEVER 0
463#define SHMEM_HUGE_ALWAYS 1
464#define SHMEM_HUGE_WITHIN_SIZE 2
465#define SHMEM_HUGE_ADVISE 3
466
467/*
468 * Special values.
469 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
470 *
471 * SHMEM_HUGE_DENY:
472 * disables huge on shm_mnt and all mounts, for emergency use;
473 * SHMEM_HUGE_FORCE:
474 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
475 *
476 */
477#define SHMEM_HUGE_DENY (-1)
478#define SHMEM_HUGE_FORCE (-2)
479
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700480#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700481/* ifdef here to avoid bloating shmem.o when not necessary */
482
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700483static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700484
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700485#if defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700486static int shmem_parse_huge(const char *str)
487{
488 if (!strcmp(str, "never"))
489 return SHMEM_HUGE_NEVER;
490 if (!strcmp(str, "always"))
491 return SHMEM_HUGE_ALWAYS;
492 if (!strcmp(str, "within_size"))
493 return SHMEM_HUGE_WITHIN_SIZE;
494 if (!strcmp(str, "advise"))
495 return SHMEM_HUGE_ADVISE;
496 if (!strcmp(str, "deny"))
497 return SHMEM_HUGE_DENY;
498 if (!strcmp(str, "force"))
499 return SHMEM_HUGE_FORCE;
500 return -EINVAL;
501}
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700502#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700503
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700504#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700505static const char *shmem_format_huge(int huge)
506{
507 switch (huge) {
508 case SHMEM_HUGE_NEVER:
509 return "never";
510 case SHMEM_HUGE_ALWAYS:
511 return "always";
512 case SHMEM_HUGE_WITHIN_SIZE:
513 return "within_size";
514 case SHMEM_HUGE_ADVISE:
515 return "advise";
516 case SHMEM_HUGE_DENY:
517 return "deny";
518 case SHMEM_HUGE_FORCE:
519 return "force";
520 default:
521 VM_BUG_ON(1);
522 return "bad_val";
523 }
524}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800525#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700526
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700527static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
528 struct shrink_control *sc, unsigned long nr_to_split)
529{
530 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800531 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700532 struct inode *inode;
533 struct shmem_inode_info *info;
534 struct page *page;
535 unsigned long batch = sc ? sc->nr_to_scan : 128;
Gang Li7b9fa912022-01-14 14:05:23 -0800536 int split = 0;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700537
538 if (list_empty(&sbinfo->shrinklist))
539 return SHRINK_STOP;
540
541 spin_lock(&sbinfo->shrinklist_lock);
542 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
543 info = list_entry(pos, struct shmem_inode_info, shrinklist);
544
545 /* pin the inode */
546 inode = igrab(&info->vfs_inode);
547
548 /* inode is about to be evicted */
549 if (!inode) {
550 list_del_init(&info->shrinklist);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700551 goto next;
552 }
553
554 /* Check if there's anything to gain */
555 if (round_up(inode->i_size, PAGE_SIZE) ==
556 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800557 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700558 goto next;
559 }
560
561 list_move(&info->shrinklist, &list);
562next:
Gang Li7b9fa912022-01-14 14:05:23 -0800563 sbinfo->shrinklist_len--;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700564 if (!--batch)
565 break;
566 }
567 spin_unlock(&sbinfo->shrinklist_lock);
568
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800569 list_for_each_safe(pos, next, &to_remove) {
570 info = list_entry(pos, struct shmem_inode_info, shrinklist);
571 inode = &info->vfs_inode;
572 list_del_init(&info->shrinklist);
573 iput(inode);
574 }
575
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700576 list_for_each_safe(pos, next, &list) {
577 int ret;
578
579 info = list_entry(pos, struct shmem_inode_info, shrinklist);
580 inode = &info->vfs_inode;
581
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700582 if (nr_to_split && split >= nr_to_split)
Gang Li7b9fa912022-01-14 14:05:23 -0800583 goto move_back;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700584
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700585 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700586 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
587 if (!page)
588 goto drop;
589
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700590 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700591 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700592 put_page(page);
593 goto drop;
594 }
595
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700596 /*
Gang Li7b9fa912022-01-14 14:05:23 -0800597 * Move the inode on the list back to shrinklist if we failed
598 * to lock the page at this time.
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700599 *
600 * Waiting for the lock may lead to deadlock in the
601 * reclaim path.
602 */
603 if (!trylock_page(page)) {
604 put_page(page);
Gang Li7b9fa912022-01-14 14:05:23 -0800605 goto move_back;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700606 }
607
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700608 ret = split_huge_page(page);
609 unlock_page(page);
610 put_page(page);
611
Gang Li7b9fa912022-01-14 14:05:23 -0800612 /* If split failed move the inode on the list back to shrinklist */
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700613 if (ret)
Gang Li7b9fa912022-01-14 14:05:23 -0800614 goto move_back;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700615
616 split++;
617drop:
618 list_del_init(&info->shrinklist);
Gang Li7b9fa912022-01-14 14:05:23 -0800619 goto put;
620move_back:
621 /*
622 * Make sure the inode is either on the global list or deleted
623 * from any local list before iput() since it could be deleted
624 * in another thread once we put the inode (then the local list
625 * is corrupted).
626 */
627 spin_lock(&sbinfo->shrinklist_lock);
628 list_move(&info->shrinklist, &sbinfo->shrinklist);
629 sbinfo->shrinklist_len++;
630 spin_unlock(&sbinfo->shrinklist_lock);
631put:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700632 iput(inode);
633 }
634
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700635 return split;
636}
637
638static long shmem_unused_huge_scan(struct super_block *sb,
639 struct shrink_control *sc)
640{
641 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
642
643 if (!READ_ONCE(sbinfo->shrinklist_len))
644 return SHRINK_STOP;
645
646 return shmem_unused_huge_shrink(sbinfo, sc, 0);
647}
648
649static long shmem_unused_huge_count(struct super_block *sb,
650 struct shrink_control *sc)
651{
652 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
653 return READ_ONCE(sbinfo->shrinklist_len);
654}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700655#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700656
657#define shmem_huge SHMEM_HUGE_DENY
658
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700659static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
660 struct shrink_control *sc, unsigned long nr_to_split)
661{
662 return 0;
663}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700664#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700665
Yang Shi89fdcd22018-06-07 17:06:59 -0700666static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
667{
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700668 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Yang Shi89fdcd22018-06-07 17:06:59 -0700669 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
670 shmem_huge != SHMEM_HUGE_DENY)
671 return true;
672 return false;
673}
674
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700675/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700676 * Like add_to_page_cache_locked, but error if expected item has gone.
677 */
678static int shmem_add_to_page_cache(struct page *page,
679 struct address_space *mapping,
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700680 pgoff_t index, void *expected, gfp_t gfp,
681 struct mm_struct *charge_mm)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700682{
Matthew Wilcox552446a42017-12-01 13:25:14 -0500683 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
684 unsigned long i = 0;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700685 unsigned long nr = compound_nr(page);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700686 int error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700687
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700688 VM_BUG_ON_PAGE(PageTail(page), page);
689 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800690 VM_BUG_ON_PAGE(!PageLocked(page), page);
691 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700692 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700693
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700694 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700695 page->mapping = mapping;
696 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700697
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700698 if (!PageSwapCache(page)) {
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -0700699 error = mem_cgroup_charge(page, charge_mm, gfp);
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700700 if (error) {
701 if (PageTransHuge(page)) {
702 count_vm_event(THP_FILE_FALLBACK);
703 count_vm_event(THP_FILE_FALLBACK_CHARGE);
704 }
705 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700706 }
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700707 }
708 cgroup_throttle_swaprate(page, gfp);
709
Matthew Wilcox552446a42017-12-01 13:25:14 -0500710 do {
711 void *entry;
712 xas_lock_irq(&xas);
713 entry = xas_find_conflict(&xas);
714 if (entry != expected)
715 xas_set_err(&xas, -EEXIST);
716 xas_create_range(&xas);
717 if (xas_error(&xas))
718 goto unlock;
719next:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700720 xas_store(&xas, page);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500721 if (++i < nr) {
722 xas_next(&xas);
723 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700724 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500725 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700726 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700727 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500728 }
729 mapping->nrpages += nr;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700730 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
731 __mod_lruvec_page_state(page, NR_SHMEM, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500732unlock:
733 xas_unlock_irq(&xas);
734 } while (xas_nomem(&xas, gfp));
735
736 if (xas_error(&xas)) {
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700737 error = xas_error(&xas);
738 goto error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700739 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500740
741 return 0;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700742error:
743 page->mapping = NULL;
744 page_ref_sub(page, nr);
745 return error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700746}
747
748/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700749 * Like delete_from_page_cache, but substitutes swap for page.
750 */
751static void shmem_delete_from_page_cache(struct page *page, void *radswap)
752{
753 struct address_space *mapping = page->mapping;
754 int error;
755
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700756 VM_BUG_ON_PAGE(PageCompound(page), page);
757
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700758 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500759 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700760 page->mapping = NULL;
761 mapping->nrpages--;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700762 __dec_lruvec_page_state(page, NR_FILE_PAGES);
763 __dec_lruvec_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700764 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300765 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700766 BUG_ON(error);
767}
768
769/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500770 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700771 */
772static int shmem_free_swap(struct address_space *mapping,
773 pgoff_t index, void *radswap)
774{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700775 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700776
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500777 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700778 if (old != radswap)
779 return -ENOENT;
780 free_swap_and_cache(radix_to_swp_entry(radswap));
781 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700782}
783
784/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800785 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800786 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800787 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700788 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800789 * as long as the inode doesn't go away and racy results are not a problem.
790 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800791unsigned long shmem_partial_swap_usage(struct address_space *mapping,
792 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800793{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500794 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800795 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800796 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800797
798 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500799 xas_for_each(&xas, page, end - 1) {
800 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700801 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400802 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800803 swapped++;
804
805 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500806 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800807 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800808 }
809 }
810
811 rcu_read_unlock();
812
813 return swapped << PAGE_SHIFT;
814}
815
816/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800817 * Determine (in bytes) how many of the shmem object's pages mapped by the
818 * given vma is swapped out.
819 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700820 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800821 * as long as the inode doesn't go away and racy results are not a problem.
822 */
823unsigned long shmem_swap_usage(struct vm_area_struct *vma)
824{
825 struct inode *inode = file_inode(vma->vm_file);
826 struct shmem_inode_info *info = SHMEM_I(inode);
827 struct address_space *mapping = inode->i_mapping;
828 unsigned long swapped;
829
830 /* Be careful as we don't hold info->lock */
831 swapped = READ_ONCE(info->swapped);
832
833 /*
834 * The easier cases are when the shmem object has nothing in swap, or
835 * the vma maps it whole. Then we can simply use the stats that we
836 * already track.
837 */
838 if (!swapped)
839 return 0;
840
841 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
842 return swapped << PAGE_SHIFT;
843
844 /* Here comes the more involved part */
845 return shmem_partial_swap_usage(mapping,
846 linear_page_index(vma, vma->vm_start),
847 linear_page_index(vma, vma->vm_end));
848}
849
850/*
Hugh Dickins24513262012-01-20 14:34:21 -0800851 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
852 */
853void shmem_unlock_mapping(struct address_space *mapping)
854{
855 struct pagevec pvec;
856 pgoff_t indices[PAGEVEC_SIZE];
857 pgoff_t index = 0;
858
Mel Gorman86679822017-11-15 17:37:52 -0800859 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800860 /*
861 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
862 */
863 while (!mapping_unevictable(mapping)) {
864 /*
865 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
866 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
867 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700868 pvec.nr = find_get_entries(mapping, index,
869 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800870 if (!pvec.nr)
871 break;
872 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700873 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000874 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800875 pagevec_release(&pvec);
876 cond_resched();
877 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700878}
879
880/*
Hugh Dickins71725ed2020-04-06 20:07:57 -0700881 * Check whether a hole-punch or truncation needs to split a huge page,
882 * returning true if no split was required, or the split has been successful.
883 *
884 * Eviction (or truncation to 0 size) should never need to split a huge page;
885 * but in rare cases might do so, if shmem_undo_range() failed to trylock on
886 * head, and then succeeded to trylock on tail.
887 *
888 * A split can only succeed when there are no additional references on the
889 * huge page: so the split below relies upon find_get_entries() having stopped
890 * when it found a subpage of the huge page, without getting further references.
891 */
892static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end)
893{
894 if (!PageTransCompound(page))
895 return true;
896
897 /* Just proceed to delete a huge page wholly within the range punched */
898 if (PageHead(page) &&
899 page->index >= start && page->index + HPAGE_PMD_NR <= end)
900 return true;
901
902 /* Try to split huge page, so we can truly punch the hole or truncate */
903 return split_huge_page(page) >= 0;
904}
905
906/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500907 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700908 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700909 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700910static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
911 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700913 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300915 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
916 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
917 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
918 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700919 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700920 pgoff_t indices[PAGEVEC_SIZE];
921 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700922 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700923 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700925 if (lend == -1)
926 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700927
Mel Gorman86679822017-11-15 17:37:52 -0800928 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700929 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700930 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700931 pvec.nr = find_get_entries(mapping, index,
932 min(end - index, (pgoff_t)PAGEVEC_SIZE),
933 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700934 if (!pvec.nr)
935 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700936 for (i = 0; i < pagevec_count(&pvec); i++) {
937 struct page *page = pvec.pages[i];
938
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700939 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700940 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700941 break;
942
Matthew Wilcox3159f942017-11-03 13:30:42 -0400943 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700944 if (unfalloc)
945 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700946 nr_swaps_freed += !shmem_free_swap(mapping,
947 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700948 continue;
949 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700950
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700951 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
952
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700953 if (!trylock_page(page))
954 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700955
Hugh Dickins71725ed2020-04-06 20:07:57 -0700956 if ((!unfalloc || !PageUptodate(page)) &&
957 page_mapping(page) == mapping) {
958 VM_BUG_ON_PAGE(PageWriteback(page), page);
959 if (shmem_punch_compound(page, start, end))
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700960 truncate_inode_page(mapping, page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700961 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700962 unlock_page(page);
963 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700964 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800965 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700966 cond_resched();
967 index++;
968 }
969
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700970 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700971 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700972 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700973 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300974 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700975 if (start > end) {
976 top = partial_end;
977 partial_end = 0;
978 }
979 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700980 set_page_dirty(page);
981 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300982 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700983 }
984 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700985 if (partial_end) {
986 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700987 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700988 if (page) {
989 zero_user_segment(page, 0, partial_end);
990 set_page_dirty(page);
991 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300992 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700993 }
994 }
995 if (start >= end)
996 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700997
998 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700999 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001000 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -07001001
1002 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07001003 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -07001004 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001005 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001006 /* If all gone or hole-punch or unfalloc, we're done */
1007 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001008 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001009 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001010 index = start;
1011 continue;
1012 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001013 for (i = 0; i < pagevec_count(&pvec); i++) {
1014 struct page *page = pvec.pages[i];
1015
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001016 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -07001017 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001018 break;
1019
Matthew Wilcox3159f942017-11-03 13:30:42 -04001020 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001021 if (unfalloc)
1022 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001023 if (shmem_free_swap(mapping, index, page)) {
1024 /* Swap was replaced by page: retry */
1025 index--;
1026 break;
1027 }
1028 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001029 continue;
1030 }
1031
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001032 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001033
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001034 if (!unfalloc || !PageUptodate(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001035 if (page_mapping(page) != mapping) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001036 /* Page was replaced by swap: retry */
1037 unlock_page(page);
1038 index--;
1039 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001040 }
Hugh Dickins71725ed2020-04-06 20:07:57 -07001041 VM_BUG_ON_PAGE(PageWriteback(page), page);
1042 if (shmem_punch_compound(page, start, end))
1043 truncate_inode_page(mapping, page);
Hugh Dickins0783ac95b2020-04-20 18:14:07 -07001044 else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001045 /* Wipe the page and don't get stuck */
1046 clear_highpage(page);
1047 flush_dcache_page(page);
1048 set_page_dirty(page);
1049 if (index <
1050 round_up(start, HPAGE_PMD_NR))
1051 start = index + 1;
1052 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001053 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001054 unlock_page(page);
1055 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001056 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -08001057 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001058 index++;
1059 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001060
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001061 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001062 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001064 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001067void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1068{
1069 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001070 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071}
Hugh Dickins94c1e622011-06-27 16:18:03 -07001072EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David Howellsa528d352017-01-31 16:46:22 +00001074static int shmem_getattr(const struct path *path, struct kstat *stat,
1075 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -07001076{
David Howellsa528d352017-01-31 16:46:22 +00001077 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001078 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001079 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001080
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001081 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001082 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001083 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001084 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001085 }
Yu Zhao44a30222015-09-08 15:03:33 -07001086 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001087
1088 if (is_huge_enabled(sb_info))
1089 stat->blksize = HPAGE_PMD_SIZE;
1090
Yu Zhao44a30222015-09-08 15:03:33 -07001091 return 0;
1092}
1093
Hugh Dickins94c1e622011-06-27 16:18:03 -07001094static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
David Howells75c3cfa2015-03-17 22:26:12 +00001096 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001097 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001098 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 int error;
1100
Jan Kara31051c82016-05-26 16:55:18 +02001101 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001102 if (error)
1103 return error;
1104
Hugh Dickins94c1e622011-06-27 16:18:03 -07001105 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1106 loff_t oldsize = inode->i_size;
1107 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001108
David Herrmann40e041a2014-08-08 14:25:27 -07001109 /* protected by i_mutex */
1110 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1111 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1112 return -EPERM;
1113
Hugh Dickins94c1e622011-06-27 16:18:03 -07001114 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001115 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1116 oldsize, newsize);
1117 if (error)
1118 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001119 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001120 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001121 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001122 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001123 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001124 if (oldsize > holebegin)
1125 unmap_mapping_range(inode->i_mapping,
1126 holebegin, 0, 1);
1127 if (info->alloced)
1128 shmem_truncate_range(inode,
1129 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001130 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001131 if (oldsize > holebegin)
1132 unmap_mapping_range(inode->i_mapping,
1133 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001134
1135 /*
1136 * Part of the huge page can be beyond i_size: subject
1137 * to shrink under memory pressure.
1138 */
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001139 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001140 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001141 /*
1142 * _careful to defend against unlocked access to
1143 * ->shrink_list in shmem_unused_huge_shrink()
1144 */
1145 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001146 list_add_tail(&info->shrinklist,
1147 &sbinfo->shrinklist);
1148 sbinfo->shrinklist_len++;
1149 }
1150 spin_unlock(&sbinfo->shrinklist_lock);
1151 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001155 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001156 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001157 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return error;
1159}
1160
Al Viro1f895f72010-06-05 19:10:41 -04001161static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001164 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001166 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 shmem_unacct_size(info->flags, inode->i_size);
1168 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001169 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001170 if (!list_empty(&info->shrinklist)) {
1171 spin_lock(&sbinfo->shrinklist_lock);
1172 if (!list_empty(&info->shrinklist)) {
1173 list_del_init(&info->shrinklist);
1174 sbinfo->shrinklist_len--;
1175 }
1176 spin_unlock(&sbinfo->shrinklist_lock);
1177 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001178 while (!list_empty(&info->swaplist)) {
1179 /* Wait while shmem_unuse() is scanning this inode... */
1180 wait_var_event(&info->stop_eviction,
1181 !atomic_read(&info->stop_eviction));
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001182 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001183 /* ...but beware of the race if we peeked too early */
1184 if (!atomic_read(&info->stop_eviction))
1185 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001186 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 }
Al Viro3ed47db2016-01-22 18:08:52 -05001188 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001189
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001190 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001191 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001192 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001193 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001196extern struct swap_info_struct *swap_info[];
1197
1198static int shmem_find_swap_entries(struct address_space *mapping,
1199 pgoff_t start, unsigned int nr_entries,
1200 struct page **entries, pgoff_t *indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001201 unsigned int type, bool frontswap)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001202{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001203 XA_STATE(xas, &mapping->i_pages, start);
1204 struct page *page;
Hugh Dickins87039542019-04-18 17:49:58 -07001205 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001206 unsigned int ret = 0;
1207
1208 if (!nr_entries)
1209 return 0;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001210
1211 rcu_read_lock();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001212 xas_for_each(&xas, page, ULONG_MAX) {
1213 if (xas_retry(&xas, page))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001214 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001215
1216 if (!xa_is_value(page))
1217 continue;
1218
Hugh Dickins87039542019-04-18 17:49:58 -07001219 entry = radix_to_swp_entry(page);
1220 if (swp_type(entry) != type)
1221 continue;
1222 if (frontswap &&
1223 !frontswap_test(swap_info[type], swp_offset(entry)))
1224 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001225
1226 indices[ret] = xas.xa_index;
1227 entries[ret] = page;
1228
1229 if (need_resched()) {
1230 xas_pause(&xas);
1231 cond_resched_rcu();
1232 }
1233 if (++ret == nr_entries)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001234 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001235 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001236 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001237
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001238 return ret;
1239}
1240
1241/*
1242 * Move the swapped pages for an inode to page cache. Returns the count
1243 * of pages swapped in, or the error in case of failure.
1244 */
1245static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
1246 pgoff_t *indices)
1247{
1248 int i = 0;
1249 int ret = 0;
1250 int error = 0;
1251 struct address_space *mapping = inode->i_mapping;
1252
1253 for (i = 0; i < pvec.nr; i++) {
1254 struct page *page = pvec.pages[i];
1255
1256 if (!xa_is_value(page))
1257 continue;
1258 error = shmem_swapin_page(inode, indices[i],
1259 &page, SGP_CACHE,
1260 mapping_gfp_mask(mapping),
1261 NULL, NULL);
1262 if (error == 0) {
1263 unlock_page(page);
1264 put_page(page);
1265 ret++;
1266 }
1267 if (error == -ENOMEM)
1268 break;
1269 error = 0;
1270 }
1271 return error ? error : ret;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001272}
1273
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001274/*
1275 * If swap found in inode, free it and move page from swapcache to filecache.
1276 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001277static int shmem_unuse_inode(struct inode *inode, unsigned int type,
1278 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001280 struct address_space *mapping = inode->i_mapping;
1281 pgoff_t start = 0;
1282 struct pagevec pvec;
1283 pgoff_t indices[PAGEVEC_SIZE];
1284 bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
1285 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001287 pagevec_init(&pvec);
1288 do {
1289 unsigned int nr_entries = PAGEVEC_SIZE;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001290
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001291 if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
1292 nr_entries = *fs_pages_to_unuse;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001293
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001294 pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
1295 pvec.pages, indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001296 type, frontswap);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001297 if (pvec.nr == 0) {
1298 ret = 0;
1299 break;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001300 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001301
1302 ret = shmem_unuse_swap_entries(inode, pvec, indices);
1303 if (ret < 0)
1304 break;
1305
1306 if (frontswap_partial) {
1307 *fs_pages_to_unuse -= ret;
1308 if (*fs_pages_to_unuse == 0) {
1309 ret = FRONTSWAP_PAGES_UNUSED;
1310 break;
1311 }
1312 }
1313
1314 start = indices[pvec.nr - 1];
1315 } while (true);
1316
1317 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318}
1319
1320/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001321 * Read all the shared memory data that resides in the swap
1322 * device 'type' back into memory, so the swap device can be
1323 * unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001325int shmem_unuse(unsigned int type, bool frontswap,
1326 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001328 struct shmem_inode_info *info, *next;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001329 int error = 0;
1330
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001331 if (list_empty(&shmem_swaplist))
1332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001334 mutex_lock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001335 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) {
1336 if (!info->swapped) {
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001337 list_del_init(&info->swaplist);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001338 continue;
1339 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001340 /*
1341 * Drop the swaplist mutex while searching the inode for swap;
1342 * but before doing so, make sure shmem_evict_inode() will not
1343 * remove placeholder inode from swaplist, nor let it be freed
1344 * (igrab() would protect from unlink, but not from unmount).
1345 */
1346 atomic_inc(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001347 mutex_unlock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001348
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001349 error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001350 fs_pages_to_unuse);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001351 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001352
1353 mutex_lock(&shmem_swaplist_mutex);
1354 next = list_next_entry(info, swaplist);
1355 if (!info->swapped)
1356 list_del_init(&info->swaplist);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001357 if (atomic_dec_and_test(&info->stop_eviction))
1358 wake_up_var(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001359 if (error)
Hugh Dickins778dd892011-05-11 15:13:37 -07001360 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001362 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001363
Hugh Dickins778dd892011-05-11 15:13:37 -07001364 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
1367/*
1368 * Move the page from the page cache to the swap cache.
1369 */
1370static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1371{
1372 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001375 swp_entry_t swap;
1376 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001378 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 mapping = page->mapping;
1381 index = page->index;
1382 inode = mapping->host;
1383 info = SHMEM_I(inode);
1384 if (info->flags & VM_LOCKED)
1385 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001386 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 goto redirty;
1388
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001389 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001390 * Our capabilities prevent regular writeback or sync from ever calling
1391 * shmem_writepage; but a stacking filesystem might use ->writepage of
1392 * its underlying filesystem, in which case tmpfs should write out to
1393 * swap only in response to memory pressure, and not for the writeback
1394 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001395 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001396 if (!wbc->for_reclaim) {
1397 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1398 goto redirty;
1399 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001400
1401 /*
1402 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1403 * value into swapfile.c, the only way we can correctly account for a
1404 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001405 *
1406 * That's okay for a page already fallocated earlier, but if we have
1407 * not yet completed the fallocation, then (a) we want to keep track
1408 * of this page in case we have to undo it, and (b) it may not be a
1409 * good idea to continue anyway, once we're pushing into swap. So
1410 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001411 */
1412 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001413 if (inode->i_private) {
1414 struct shmem_falloc *shmem_falloc;
1415 spin_lock(&inode->i_lock);
1416 shmem_falloc = inode->i_private;
1417 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001418 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001419 index >= shmem_falloc->start &&
1420 index < shmem_falloc->next)
1421 shmem_falloc->nr_unswapped++;
1422 else
1423 shmem_falloc = NULL;
1424 spin_unlock(&inode->i_lock);
1425 if (shmem_falloc)
1426 goto redirty;
1427 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001428 clear_highpage(page);
1429 flush_dcache_page(page);
1430 SetPageUptodate(page);
1431 }
1432
Huang Ying38d8b4e2017-07-06 15:37:18 -07001433 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001434 if (!swap.val)
1435 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001436
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001437 /*
1438 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001439 * if it's not already there. Do it now before the page is
1440 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001441 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001442 * we've incremented swapped, because shmem_unuse_inode() will
1443 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001444 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001445 mutex_lock(&shmem_swaplist_mutex);
1446 if (list_empty(&info->swaplist))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001447 list_add(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001448
Yang Shi4afab1c2019-11-30 17:58:07 -08001449 if (add_to_swap_cache(page, swap,
Joonsoo Kim3852f672020-08-11 18:30:47 -07001450 __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN,
1451 NULL) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001452 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001453 shmem_recalc_inode(inode);
1454 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001455 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001456
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001457 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001458 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1459
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001460 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001461 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001462 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return 0;
1464 }
1465
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001466 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001467 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468redirty:
1469 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001470 if (wbc->for_reclaim)
1471 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1472 unlock_page(page);
1473 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474}
1475
Hugh Dickins75edd342016-05-19 17:12:44 -07001476#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001477static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001478{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001479 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001480
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001481 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001482 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001483
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001484 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001485
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001486 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001487}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001488
1489static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1490{
1491 struct mempolicy *mpol = NULL;
1492 if (sbinfo->mpol) {
1493 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1494 mpol = sbinfo->mpol;
1495 mpol_get(mpol);
1496 spin_unlock(&sbinfo->stat_lock);
1497 }
1498 return mpol;
1499}
Hugh Dickins75edd342016-05-19 17:12:44 -07001500#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1501static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1502{
1503}
1504static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1505{
1506 return NULL;
1507}
1508#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1509#ifndef CONFIG_NUMA
1510#define vm_policy vm_private_data
1511#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001512
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001513static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1514 struct shmem_inode_info *info, pgoff_t index)
1515{
1516 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001517 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001518 /* Bias interleave by inode number to distribute better across nodes */
1519 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001520 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1521}
1522
1523static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1524{
1525 /* Drop reference taken by mpol_shared_policy_lookup() */
1526 mpol_cond_put(vma->vm_policy);
1527}
1528
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001529static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1530 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001533 struct page *page;
Will Deaconcab48b22021-01-14 15:42:14 +00001534 struct vm_fault vmf = {
1535 .vma = &pvma,
1536 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001538 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001539 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001540 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001541
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001542 return page;
1543}
Mel Gorman18a2f372012-12-05 14:01:41 -08001544
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001545static struct page *shmem_alloc_hugepage(gfp_t gfp,
1546 struct shmem_inode_info *info, pgoff_t index)
1547{
1548 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001549 struct address_space *mapping = info->vfs_inode.i_mapping;
1550 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001551 struct page *page;
1552
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001553 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001554 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1555 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001556 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001557
1558 shmem_pseudo_vma_init(&pvma, info, hindex);
1559 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes19deb762019-09-04 12:54:20 -07001560 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001561 shmem_pseudo_vma_destroy(&pvma);
1562 if (page)
1563 prep_transhuge_page(page);
David Rientjesdcdf11e2020-04-06 20:04:25 -07001564 else
1565 count_vm_event(THP_FILE_FALLBACK);
Mel Gorman18a2f372012-12-05 14:01:41 -08001566 return page;
1567}
1568
1569static struct page *shmem_alloc_page(gfp_t gfp,
1570 struct shmem_inode_info *info, pgoff_t index)
1571{
1572 struct vm_area_struct pvma;
Charan Teja Reddy964220d2021-07-06 19:04:50 +05301573 struct page *page = NULL;
1574
1575 trace_android_vh_shmem_alloc_page(&page);
1576 if (page)
1577 return page;
Mel Gorman18a2f372012-12-05 14:01:41 -08001578
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001579 shmem_pseudo_vma_init(&pvma, info, index);
1580 page = alloc_page_vma(gfp, &pvma, 0);
1581 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001582
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001583 return page;
1584}
1585
1586static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001587 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001588 pgoff_t index, bool huge)
1589{
Mike Rapoport0f079692017-09-06 16:22:59 -07001590 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001591 struct page *page;
1592 int nr;
1593 int err = -ENOSPC;
1594
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001595 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001596 huge = false;
1597 nr = huge ? HPAGE_PMD_NR : 1;
1598
Mike Rapoport0f079692017-09-06 16:22:59 -07001599 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001600 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001601
1602 if (huge)
1603 page = shmem_alloc_hugepage(gfp, info, index);
1604 else
1605 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001606 if (page) {
1607 __SetPageLocked(page);
1608 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001609 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001610 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001611
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001612 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001613 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001614failed:
1615 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001619 * When a page is moved from swapcache to shmem filecache (either by the
1620 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1621 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1622 * ignorance of the mapping it belongs to. If that mapping has special
1623 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1624 * we may need to copy to a suitable page before moving to filecache.
1625 *
1626 * In a future release, this may well be extended to respect cpuset and
1627 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1628 * but for now it is a simple matter of zone.
1629 */
1630static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1631{
1632 return page_zonenum(page) > gfp_zone(gfp);
1633}
1634
1635static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1636 struct shmem_inode_info *info, pgoff_t index)
1637{
1638 struct page *oldpage, *newpage;
1639 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001640 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001641 pgoff_t swap_index;
1642 int error;
1643
1644 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001645 entry.val = page_private(oldpage);
1646 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001647 swap_mapping = page_mapping(oldpage);
1648
1649 /*
1650 * We have arrived here because our zones are constrained, so don't
1651 * limit chance of success by further cpuset and node constraints.
1652 */
1653 gfp &= ~GFP_CONSTRAINT_MASK;
1654 newpage = shmem_alloc_page(gfp, info, index);
1655 if (!newpage)
1656 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001657
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001658 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001659 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001660 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001661
Hugh Dickins9956edf2016-11-10 10:46:11 -08001662 __SetPageLocked(newpage);
1663 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001664 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001665 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001666 SetPageSwapCache(newpage);
1667
1668 /*
1669 * Our caller will very soon move newpage out of swapcache, but it's
1670 * a nice clean interface for us to replace oldpage by newpage there.
1671 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001672 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001673 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001674 if (!error) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07001675 mem_cgroup_migrate(oldpage, newpage);
1676 __inc_lruvec_page_state(newpage, NR_FILE_PAGES);
1677 __dec_lruvec_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001678 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001679 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001680
Hugh Dickins0142ef62012-06-07 14:21:09 -07001681 if (unlikely(error)) {
1682 /*
1683 * Is this possible? I think not, now that our callers check
1684 * both PageSwapCache and page_private after getting page lock;
1685 * but be defensive. Reverse old to newpage for clear and free.
1686 */
1687 oldpage = newpage;
1688 } else {
Johannes Weiner6058eae2020-06-03 16:02:40 -07001689 lru_cache_add(newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001690 *pagep = newpage;
1691 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001692
1693 ClearPageSwapCache(oldpage);
1694 set_page_private(oldpage, 0);
1695
1696 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001697 put_page(oldpage);
1698 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001699 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001700}
1701
1702/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001703 * Swap in the page pointed to by *pagep.
1704 * Caller has to make sure that *pagep contains a valid swapped page.
1705 * Returns 0 and the page in pagep if success. On failure, returns the
Randy Dunlapaf44c122020-08-11 18:33:17 -07001706 * error code and NULL in *pagep.
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001707 */
1708static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1709 struct page **pagep, enum sgp_type sgp,
1710 gfp_t gfp, struct vm_area_struct *vma,
1711 vm_fault_t *fault_type)
1712{
1713 struct address_space *mapping = inode->i_mapping;
1714 struct shmem_inode_info *info = SHMEM_I(inode);
1715 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
Greg Kroah-Hartman7d7d0e82021-07-22 15:46:08 +02001716 struct page *page;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001717 swp_entry_t swap;
1718 int error;
1719
1720 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1721 swap = radix_to_swp_entry(*pagep);
1722 *pagep = NULL;
1723
1724 /* Look it up and read it in.. */
1725 page = lookup_swap_cache(swap, NULL, 0);
1726 if (!page) {
1727 /* Or update major stats only when swapin succeeds?? */
1728 if (fault_type) {
1729 *fault_type |= VM_FAULT_MAJOR;
1730 count_vm_event(PGMAJFAULT);
1731 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1732 }
1733 /* Here we actually start the io */
1734 page = shmem_swapin(swap, gfp, info, index);
1735 if (!page) {
1736 error = -ENOMEM;
1737 goto failed;
1738 }
1739 }
1740
1741 /* We have to do this with page locked to prevent races */
1742 lock_page(page);
1743 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1744 !shmem_confirm_swap(mapping, index, swap)) {
1745 error = -EEXIST;
1746 goto unlock;
1747 }
1748 if (!PageUptodate(page)) {
1749 error = -EIO;
1750 goto failed;
1751 }
1752 wait_on_page_writeback(page);
1753
Steven Price8a848022020-05-13 16:37:49 +01001754 /*
1755 * Some architectures may have to restore extra metadata to the
1756 * physical page after reading from swap.
1757 */
1758 arch_swap_restore(swap, page);
1759
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001760 if (shmem_should_replace_page(page, gfp)) {
1761 error = shmem_replace_page(&page, gfp, info, index);
1762 if (error)
1763 goto failed;
1764 }
1765
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001766 error = shmem_add_to_page_cache(page, mapping, index,
1767 swp_to_radix_entry(swap), gfp,
1768 charge_mm);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001769 if (error)
1770 goto failed;
1771
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001772 spin_lock_irq(&info->lock);
1773 info->swapped--;
1774 shmem_recalc_inode(inode);
1775 spin_unlock_irq(&info->lock);
1776
1777 if (sgp == SGP_WRITE)
1778 mark_page_accessed(page);
1779
1780 delete_from_swap_cache(page);
1781 set_page_dirty(page);
1782 swap_free(swap);
1783
1784 *pagep = page;
1785 return 0;
1786failed:
1787 if (!shmem_confirm_swap(mapping, index, swap))
1788 error = -EEXIST;
1789unlock:
1790 if (page) {
1791 unlock_page(page);
1792 put_page(page);
1793 }
1794
1795 return error;
1796}
1797
1798/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001799 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *
1801 * If we allocate a new one we do not mark it dirty. That's up to the
1802 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001803 * entry since a page cannot live in both the swap and page cache.
1804 *
Axel Rasmussen0c959cd2021-05-11 15:05:58 +10001805 * vma, vmf, and fault_type are only supplied by shmem_fault:
Lokesh Gidra84330a52021-05-13 05:11:10 -07001806 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001808static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001809 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001810 struct vm_area_struct *vma, struct vm_fault *vmf,
1811 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
1813 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001814 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001816 struct mm_struct *charge_mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001817 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001818 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001819 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001821 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001822 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001824 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001826 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1827 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001829 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001830 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001831 return -EINVAL;
1832 }
1833
1834 sbinfo = SHMEM_SB(inode->i_sb);
1835 charge_mm = vma ? vma->vm_mm : current->mm;
1836
1837 page = find_lock_entry(mapping, index);
Axel Rasmussen0c959cd2021-05-11 15:05:58 +10001838
1839 if (page && vma && userfaultfd_minor(vma)) {
1840 if (!xa_is_value(page)) {
1841 unlock_page(page);
1842 put_page(page);
1843 }
1844 *fault_type = handle_userfault(vmf, VM_UFFD_MINOR);
1845 return 0;
1846 }
1847
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001848 if (xa_is_value(page)) {
1849 error = shmem_swapin_page(inode, index, &page,
1850 sgp, gfp, vma, fault_type);
1851 if (error == -EEXIST)
1852 goto repeat;
1853
1854 *pagep = page;
1855 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001856 }
1857
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001858 if (page)
1859 hindex = page->index;
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001860 if (page && sgp == SGP_WRITE)
1861 mark_page_accessed(page);
1862
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001863 /* fallocated page? */
1864 if (page && !PageUptodate(page)) {
1865 if (sgp != SGP_READ)
1866 goto clear;
1867 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001868 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001869 page = NULL;
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001870 hindex = index;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001871 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001872 if (page || sgp == SGP_READ)
1873 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001876 * Fast cache lookup did not find it:
1877 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001879
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001880 if (vma && userfaultfd_missing(vma)) {
1881 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1882 return 0;
1883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001885 /* shmem_symlink() */
1886 if (mapping->a_ops != &shmem_aops)
1887 goto alloc_nohuge;
1888 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1889 goto alloc_nohuge;
1890 if (shmem_huge == SHMEM_HUGE_FORCE)
1891 goto alloc_huge;
1892 switch (sbinfo->huge) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001893 case SHMEM_HUGE_NEVER:
1894 goto alloc_nohuge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001895 case SHMEM_HUGE_WITHIN_SIZE: {
1896 loff_t i_size;
1897 pgoff_t off;
1898
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001899 off = round_up(index, HPAGE_PMD_NR);
1900 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1901 if (i_size >= HPAGE_PMD_SIZE &&
1902 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001903 goto alloc_huge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001904
1905 fallthrough;
1906 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001907 case SHMEM_HUGE_ADVISE:
1908 if (sgp_huge == SGP_HUGE)
1909 goto alloc_huge;
1910 /* TODO: implement fadvise() hints */
1911 goto alloc_nohuge;
1912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001914alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001915 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1916 if (IS_ERR(page)) {
1917alloc_nohuge:
1918 page = shmem_alloc_and_acct_page(gfp, inode,
1919 index, false);
1920 }
1921 if (IS_ERR(page)) {
1922 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001923
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001924 error = PTR_ERR(page);
1925 page = NULL;
1926 if (error != -ENOSPC)
1927 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001928 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001929 * Try to reclaim some space by splitting a huge page
1930 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001931 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001932 while (retry--) {
1933 int ret;
1934
1935 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1936 if (ret == SHRINK_STOP)
1937 break;
1938 if (ret)
1939 goto alloc_nohuge;
1940 }
1941 goto unlock;
1942 }
1943
1944 if (PageTransHuge(page))
1945 hindex = round_down(index, HPAGE_PMD_NR);
1946 else
1947 hindex = index;
1948
1949 if (sgp == SGP_WRITE)
1950 __SetPageReferenced(page);
1951
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001952 error = shmem_add_to_page_cache(page, mapping, hindex,
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001953 NULL, gfp & GFP_RECLAIM_MASK,
1954 charge_mm);
1955 if (error)
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001956 goto unacct;
Johannes Weiner6058eae2020-06-03 16:02:40 -07001957 lru_cache_add(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001958
1959 spin_lock_irq(&info->lock);
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001960 info->alloced += compound_nr(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001961 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1962 shmem_recalc_inode(inode);
1963 spin_unlock_irq(&info->lock);
1964 alloced = true;
1965
1966 if (PageTransHuge(page) &&
1967 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1968 hindex + HPAGE_PMD_NR - 1) {
1969 /*
1970 * Part of the huge page is beyond i_size: subject
1971 * to shrink under memory pressure.
1972 */
1973 spin_lock(&sbinfo->shrinklist_lock);
1974 /*
1975 * _careful to defend against unlocked access to
1976 * ->shrink_list in shmem_unused_huge_shrink()
1977 */
1978 if (list_empty_careful(&info->shrinklist)) {
1979 list_add_tail(&info->shrinklist,
1980 &sbinfo->shrinklist);
1981 sbinfo->shrinklist_len++;
1982 }
1983 spin_unlock(&sbinfo->shrinklist_lock);
1984 }
1985
1986 /*
1987 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1988 */
1989 if (sgp == SGP_FALLOC)
1990 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001991clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001992 /*
1993 * Let SGP_WRITE caller clear ends if write does not fill page;
1994 * but SGP_FALLOC on a page fallocated earlier must initialize
1995 * it now, lest undo on failure cancel our earlier guarantee.
1996 */
1997 if (sgp != SGP_WRITE && !PageUptodate(page)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001998 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001999
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07002000 for (i = 0; i < compound_nr(page); i++) {
2001 clear_highpage(page + i);
2002 flush_dcache_page(page + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002003 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07002004 SetPageUptodate(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07002005 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07002006
Hugh Dickins54af60422011-08-03 16:21:24 -07002007 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07002008 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002009 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08002010 if (alloced) {
2011 ClearPageDirty(page);
2012 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002013 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08002014 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002015 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08002016 }
Hugh Dickins54af60422011-08-03 16:21:24 -07002017 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08002018 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07002019 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07002020out:
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002021 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07002022 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07002023
Nick Piggind0217ac2007-07-19 01:47:03 -07002024 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002025 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Hugh Dickins54af60422011-08-03 16:21:24 -07002027unacct:
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07002028 shmem_inode_unacct_blocks(inode, compound_nr(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002029
2030 if (PageTransHuge(page)) {
2031 unlock_page(page);
2032 put_page(page);
2033 goto alloc_nohuge;
2034 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07002035unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07002036 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07002037 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002038 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07002039 }
2040 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002041 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07002042 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002043 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07002044 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08002045 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002046 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07002047 goto repeat;
2048 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002051/*
2052 * This is like autoremove_wake_function, but it removes the wait queue
2053 * entry unconditionally - even if something else had already woken the
2054 * target.
2055 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02002056static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002057{
2058 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02002059 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002060 return ret;
2061}
2062
Souptick Joarder20acce62018-06-07 17:09:17 -07002063static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Dave Jiang11bac802017-02-24 14:56:41 -08002065 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05002066 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002067 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002068 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07002069 int err;
2070 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002072 /*
2073 * Trinity finds that probing a hole which tmpfs is punching can
2074 * prevent the hole-punch from ever completing: which in turn
2075 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07002076 * faulting pages into the hole while it's being punched. Although
2077 * shmem_undo_range() does remove the additions, it may be unable to
2078 * keep up, as each new page needs its own unmap_mapping_range() call,
2079 * and the i_mmap tree grows ever slower to scan if new vmas are added.
2080 *
2081 * It does not matter if we sometimes reach this check just before the
2082 * hole-punch begins, so that one fault then races with the punch:
2083 * we just need to make racing faults a rare case.
2084 *
2085 * The implementation below would be much simpler if we just used a
2086 * standard mutex or completion: but we cannot take i_mutex in fault,
2087 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002088 */
2089 if (unlikely(inode->i_private)) {
2090 struct shmem_falloc *shmem_falloc;
2091
2092 spin_lock(&inode->i_lock);
2093 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002094 if (shmem_falloc &&
2095 shmem_falloc->waitq &&
2096 vmf->pgoff >= shmem_falloc->start &&
2097 vmf->pgoff < shmem_falloc->next) {
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002098 struct file *fpin;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002099 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002100 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002101
2102 ret = VM_FAULT_NOPAGE;
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002103 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2104 if (fpin)
Hugh Dickins8e205f72014-07-23 14:00:10 -07002105 ret = VM_FAULT_RETRY;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002106
2107 shmem_falloc_waitq = shmem_falloc->waitq;
2108 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2109 TASK_UNINTERRUPTIBLE);
2110 spin_unlock(&inode->i_lock);
2111 schedule();
2112
2113 /*
2114 * shmem_falloc_waitq points into the shmem_fallocate()
2115 * stack of the hole-punching task: shmem_falloc_waitq
2116 * is usually invalid by the time we reach here, but
2117 * finish_wait() does not dereference it in that case;
2118 * though i_lock needed lest racing with wake_up_all().
2119 */
2120 spin_lock(&inode->i_lock);
2121 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2122 spin_unlock(&inode->i_lock);
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002123
2124 if (fpin)
2125 fput(fpin);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002126 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002127 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002128 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002129 }
2130
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002131 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002132
2133 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2134 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002135 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002136 else if (vma->vm_flags & VM_HUGEPAGE)
2137 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002138
Souptick Joarder20acce62018-06-07 17:09:17 -07002139 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002140 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002141 if (err)
2142 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002146unsigned long shmem_get_unmapped_area(struct file *file,
2147 unsigned long uaddr, unsigned long len,
2148 unsigned long pgoff, unsigned long flags)
2149{
2150 unsigned long (*get_area)(struct file *,
2151 unsigned long, unsigned long, unsigned long, unsigned long);
2152 unsigned long addr;
2153 unsigned long offset;
2154 unsigned long inflated_len;
2155 unsigned long inflated_addr;
2156 unsigned long inflated_offset;
2157
2158 if (len > TASK_SIZE)
2159 return -ENOMEM;
2160
2161 get_area = current->mm->get_unmapped_area;
2162 addr = get_area(file, uaddr, len, pgoff, flags);
2163
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002164 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002165 return addr;
2166 if (IS_ERR_VALUE(addr))
2167 return addr;
2168 if (addr & ~PAGE_MASK)
2169 return addr;
2170 if (addr > TASK_SIZE - len)
2171 return addr;
2172
2173 if (shmem_huge == SHMEM_HUGE_DENY)
2174 return addr;
2175 if (len < HPAGE_PMD_SIZE)
2176 return addr;
2177 if (flags & MAP_FIXED)
2178 return addr;
2179 /*
2180 * Our priority is to support MAP_SHARED mapped hugely;
2181 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002182 * But if caller specified an address hint and we allocated area there
2183 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002184 */
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002185 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002186 return addr;
2187
2188 if (shmem_huge != SHMEM_HUGE_FORCE) {
2189 struct super_block *sb;
2190
2191 if (file) {
2192 VM_BUG_ON(file->f_op != &shmem_file_operations);
2193 sb = file_inode(file)->i_sb;
2194 } else {
2195 /*
2196 * Called directly from mm/mmap.c, or drivers/char/mem.c
2197 * for "/dev/zero", to create a shared anonymous object.
2198 */
2199 if (IS_ERR(shm_mnt))
2200 return addr;
2201 sb = shm_mnt->mnt_sb;
2202 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002203 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002204 return addr;
2205 }
2206
2207 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2208 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2209 return addr;
2210 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2211 return addr;
2212
2213 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2214 if (inflated_len > TASK_SIZE)
2215 return addr;
2216 if (inflated_len < len)
2217 return addr;
2218
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002219 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002220 if (IS_ERR_VALUE(inflated_addr))
2221 return addr;
2222 if (inflated_addr & ~PAGE_MASK)
2223 return addr;
2224
2225 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2226 inflated_addr += offset - inflated_offset;
2227 if (inflated_offset > offset)
2228 inflated_addr += HPAGE_PMD_SIZE;
2229
2230 if (inflated_addr > TASK_SIZE - len)
2231 return addr;
2232 return inflated_addr;
2233}
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002236static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Al Viro496ad9a2013-01-23 17:07:38 -05002238 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002239 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002242static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2243 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Al Viro496ad9a2013-01-23 17:07:38 -05002245 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002246 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002248 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2249 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251#endif
2252
2253int shmem_lock(struct file *file, int lock, struct user_struct *user)
2254{
Al Viro496ad9a2013-01-23 17:07:38 -05002255 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct shmem_inode_info *info = SHMEM_I(inode);
2257 int retval = -ENOMEM;
2258
Hugh Dickinsea0dfeb2020-04-20 18:14:14 -07002259 /*
2260 * What serializes the accesses to info->flags?
2261 * ipc_lock_object() when called from shmctl_do_lock(),
2262 * no serialization needed when called from shm_destroy().
2263 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (lock && !(info->flags & VM_LOCKED)) {
2265 if (!user_shm_lock(inode->i_size, user))
2266 goto out_nomem;
2267 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002268 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 if (!lock && (info->flags & VM_LOCKED) && user) {
2271 user_shm_unlock(inode->i_size, user);
2272 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002273 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return retval;
2279}
2280
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002281static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002283 struct shmem_inode_info *info = SHMEM_I(file_inode(file));
Peter Xu01486862021-05-14 17:27:04 -07002284 int ret;
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002285
Peter Xu01486862021-05-14 17:27:04 -07002286 ret = seal_check_future_write(info->seals, vma);
2287 if (ret)
2288 return ret;
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002289
Catalin Marinas51b0bff2019-11-29 12:45:08 +00002290 /* arm64 - allow memory tagging on RAM-based files */
2291 vma->vm_flags |= VM_MTE_ALLOWED;
2292
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 file_accessed(file);
2294 vma->vm_ops = &shmem_vm_ops;
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002295 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002296 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2297 (vma->vm_end & HPAGE_PMD_MASK)) {
2298 khugepaged_enter(vma, vma->vm_flags);
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
2301}
2302
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002303static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002304 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305{
2306 struct inode *inode;
2307 struct shmem_inode_info *info;
2308 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Chris Downe809d5f2020-08-06 23:20:20 -07002309 ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Chris Downe809d5f2020-08-06 23:20:20 -07002311 if (shmem_reserve_inode(sb, &ino))
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002312 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 inode = new_inode(sb);
2315 if (inode) {
Chris Downe809d5f2020-08-06 23:20:20 -07002316 inode->i_ino = ino;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002317 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002319 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002320 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 info = SHMEM_I(inode);
2322 memset(info, 0, (char *)inode - (char *)info);
2323 spin_lock_init(&info->lock);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002324 atomic_set(&info->stop_eviction, 0);
David Herrmann40e041a2014-08-08 14:25:27 -07002325 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002326 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002327 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002329 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002330 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 switch (mode & S_IFMT) {
2333 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002334 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 init_special_inode(inode, mode, dev);
2336 break;
2337 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002338 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 inode->i_op = &shmem_inode_operations;
2340 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002341 mpol_shared_policy_init(&info->policy,
2342 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 break;
2344 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002345 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /* Some things misbehave if size == 0 on a directory */
2347 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2348 inode->i_op = &shmem_dir_inode_operations;
2349 inode->i_fop = &simple_dir_operations;
2350 break;
2351 case S_IFLNK:
2352 /*
2353 * Must not load anything in the rbtree,
2354 * mpol_free_shared_policy will not be called.
2355 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002356 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 break;
2358 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002359
2360 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002361 } else
2362 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return inode;
2364}
2365
Johannes Weiner0cd61442014-04-03 14:47:46 -07002366bool shmem_mapping(struct address_space *mapping)
2367{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002368 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002369}
2370
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002371#ifdef CONFIG_USERFAULTFD
2372int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2373 pmd_t *dst_pmd,
2374 struct vm_area_struct *dst_vma,
2375 unsigned long dst_addr,
2376 unsigned long src_addr,
2377 bool zeropage,
2378 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002379{
2380 struct inode *inode = file_inode(dst_vma->vm_file);
2381 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002382 struct address_space *mapping = inode->i_mapping;
2383 gfp_t gfp = mapping_gfp_mask(mapping);
2384 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002385 void *page_kaddr;
2386 struct page *page;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002387 int ret;
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002388 pgoff_t max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002389
Axel Rasmussen140cfd92021-05-14 17:27:19 -07002390 if (!shmem_inode_acct_block(inode, 1)) {
2391 /*
2392 * We may have got a page, returned -ENOENT triggering a retry,
2393 * and now we find ourselves with -ENOMEM. Release the page, to
2394 * avoid a BUG_ON in our caller.
2395 */
2396 if (unlikely(*pagep)) {
2397 put_page(*pagep);
2398 *pagep = NULL;
2399 }
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002400 return -ENOMEM;
Axel Rasmussen140cfd92021-05-14 17:27:19 -07002401 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002402
Lokesh Gidra84330a52021-05-13 05:11:10 -07002403 if (!*pagep) {
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002404 ret = -ENOMEM;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002405 page = shmem_alloc_page(gfp, info, pgoff);
2406 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002407 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002408
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002409 if (!zeropage) { /* COPY */
Mike Rapoport8d103962017-09-06 16:23:02 -07002410 page_kaddr = kmap_atomic(page);
2411 ret = copy_from_user(page_kaddr,
2412 (const void __user *)src_addr,
2413 PAGE_SIZE);
2414 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002415
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002416 /* fallback to copy_from_user outside mmap_lock */
Mike Rapoport8d103962017-09-06 16:23:02 -07002417 if (unlikely(ret)) {
2418 *pagep = page;
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002419 ret = -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002420 /* don't free the page */
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002421 goto out_unacct_blocks;
Mike Rapoport8d103962017-09-06 16:23:02 -07002422 }
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002423 } else { /* ZEROPAGE */
Mike Rapoport8d103962017-09-06 16:23:02 -07002424 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002425 }
2426 } else {
2427 page = *pagep;
2428 *pagep = NULL;
2429 }
2430
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002431 VM_BUG_ON(PageLocked(page));
2432 VM_BUG_ON(PageSwapBacked(page));
Lokesh Gidra84330a52021-05-13 05:11:10 -07002433 __SetPageLocked(page);
2434 __SetPageSwapBacked(page);
2435 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002436
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002437 ret = -EFAULT;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002438 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002439 if (unlikely(pgoff >= max_off))
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002440 goto out_release;
2441
Lokesh Gidra84330a52021-05-13 05:11:10 -07002442 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2443 gfp & GFP_RECLAIM_MASK, dst_mm);
2444 if (ret)
2445 goto out_release;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002446
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002447 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
2448 page, true, false);
2449 if (ret)
2450 goto out_delete_from_cache;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002451
Lokesh Gidra84330a52021-05-13 05:11:10 -07002452 spin_lock_irq(&info->lock);
2453 info->alloced++;
2454 inode->i_blocks += BLOCKS_PER_PAGE;
2455 shmem_recalc_inode(inode);
2456 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002457
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002458 SetPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002459 unlock_page(page);
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002460 return 0;
2461out_delete_from_cache:
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002462 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002463out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002464 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002465 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002466out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002467 shmem_inode_unacct_blocks(inode, 1);
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002468 return ret;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002469}
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002470#endif /* CONFIG_USERFAULTFD */
Mike Rapoport8d103962017-09-06 16:23:02 -07002471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002473static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002474static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002476#ifdef CONFIG_TMPFS_XATTR
2477static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2478#else
2479#define shmem_initxattrs NULL
2480#endif
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002483shmem_write_begin(struct file *file, struct address_space *mapping,
2484 loff_t pos, unsigned len, unsigned flags,
2485 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486{
Nick Piggin800d15a2007-10-16 01:25:03 -07002487 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002488 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002489 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002490
2491 /* i_mutex is held by caller */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002492 if (unlikely(info->seals & (F_SEAL_GROW |
2493 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2494 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
David Herrmann40e041a2014-08-08 14:25:27 -07002495 return -EPERM;
2496 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2497 return -EPERM;
2498 }
2499
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002500 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002501}
2502
2503static int
2504shmem_write_end(struct file *file, struct address_space *mapping,
2505 loff_t pos, unsigned len, unsigned copied,
2506 struct page *page, void *fsdata)
2507{
2508 struct inode *inode = mapping->host;
2509
Hugh Dickinsd3602442008-02-04 22:28:44 -08002510 if (pos + copied > inode->i_size)
2511 i_size_write(inode, pos + copied);
2512
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002513 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002514 struct page *head = compound_head(page);
2515 if (PageTransCompound(page)) {
2516 int i;
2517
2518 for (i = 0; i < HPAGE_PMD_NR; i++) {
2519 if (head + i == page)
2520 continue;
2521 clear_highpage(head + i);
2522 flush_dcache_page(head + i);
2523 }
2524 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002525 if (copied < PAGE_SIZE) {
2526 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002527 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002528 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002529 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002530 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002531 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002532 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002533 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002534 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002535
Nick Piggin800d15a2007-10-16 01:25:03 -07002536 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Al Viro2ba5bbe2014-04-02 20:00:02 -04002539static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Al Viro6e58e792014-02-03 17:07:03 -05002541 struct file *file = iocb->ki_filp;
2542 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002544 pgoff_t index;
2545 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002546 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002547 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002548 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002549 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002550
2551 /*
2552 * Might this read be for a stacking filesystem? Then when reading
2553 * holes of a sparse file, we actually need to allocate those pages,
2554 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2555 */
Al Viro777eda22014-12-17 04:46:46 -05002556 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002557 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002559 index = *ppos >> PAGE_SHIFT;
2560 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561
2562 for (;;) {
2563 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002564 pgoff_t end_index;
2565 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 loff_t i_size = i_size_read(inode);
2567
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002568 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if (index > end_index)
2570 break;
2571 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002572 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (nr <= offset)
2574 break;
2575 }
2576
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002577 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002578 if (error) {
2579 if (error == -EINVAL)
2580 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 break;
2582 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002583 if (page) {
2584 if (sgp == SGP_CACHE)
2585 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002586 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589 /*
2590 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002591 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002593 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002595 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002597 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (nr <= offset) {
2599 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002600 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 break;
2602 }
2603 }
2604 nr -= offset;
2605
2606 if (page) {
2607 /*
2608 * If users can be writing to this page using arbitrary
2609 * virtual addresses, take care about potential aliasing
2610 * before reading the page on the kernel side.
2611 */
2612 if (mapping_writably_mapped(mapping))
2613 flush_dcache_page(page);
2614 /*
2615 * Mark the page accessed if we read the beginning.
2616 */
2617 if (!offset)
2618 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002619 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002621 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 /*
2625 * Ok, we have the page, and it's up-to-date, so
2626 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002628 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002629 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002631 index += offset >> PAGE_SHIFT;
2632 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002634 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002635 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 break;
Al Viro6e58e792014-02-03 17:07:03 -05002637 if (ret < nr) {
2638 error = -EFAULT;
2639 break;
2640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 cond_resched();
2642 }
2643
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002644 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002645 file_accessed(file);
2646 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002649/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002650 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002651 */
2652static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002653 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002654{
2655 struct page *page;
2656 struct pagevec pvec;
2657 pgoff_t indices[PAGEVEC_SIZE];
2658 bool done = false;
2659 int i;
2660
Mel Gorman86679822017-11-15 17:37:52 -08002661 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002662 pvec.nr = 1; /* start small: we may be there already */
2663 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002664 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002665 pvec.nr, pvec.pages, indices);
2666 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002667 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002668 index = end;
2669 break;
2670 }
2671 for (i = 0; i < pvec.nr; i++, index++) {
2672 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002673 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002674 done = true;
2675 break;
2676 }
2677 index = indices[i];
2678 }
2679 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002680 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002681 if (!PageUptodate(page))
2682 page = NULL;
2683 }
2684 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002685 (page && whence == SEEK_DATA) ||
2686 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002687 done = true;
2688 break;
2689 }
2690 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002691 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002692 pagevec_release(&pvec);
2693 pvec.nr = PAGEVEC_SIZE;
2694 cond_resched();
2695 }
2696 return index;
2697}
2698
Andrew Morton965c8e52012-12-17 15:59:39 -08002699static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002700{
2701 struct address_space *mapping = file->f_mapping;
2702 struct inode *inode = mapping->host;
2703 pgoff_t start, end;
2704 loff_t new_offset;
2705
Andrew Morton965c8e52012-12-17 15:59:39 -08002706 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2707 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002708 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002709 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002710 /* We're holding i_mutex so we can access i_size directly */
2711
Yufen Yu1a413642018-11-16 15:08:39 -08002712 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002713 offset = -ENXIO;
2714 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002715 start = offset >> PAGE_SHIFT;
2716 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002717 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002718 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002719 if (new_offset > offset) {
2720 if (new_offset < inode->i_size)
2721 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002722 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002723 offset = -ENXIO;
2724 else
2725 offset = inode->i_size;
2726 }
2727 }
2728
Hugh Dickins387aae62013-08-04 11:30:25 -07002729 if (offset >= 0)
2730 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002731 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002732 return offset;
2733}
2734
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002735static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2736 loff_t len)
2737{
Al Viro496ad9a2013-01-23 17:07:38 -05002738 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002739 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002740 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002741 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002742 pgoff_t start, index, end;
2743 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002744
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002745 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2746 return -EOPNOTSUPP;
2747
Al Viro59551022016-01-22 15:40:57 -05002748 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002749
2750 if (mode & FALLOC_FL_PUNCH_HOLE) {
2751 struct address_space *mapping = file->f_mapping;
2752 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2753 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002754 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002755
David Herrmann40e041a2014-08-08 14:25:27 -07002756 /* protected by i_mutex */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002757 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
David Herrmann40e041a2014-08-08 14:25:27 -07002758 error = -EPERM;
2759 goto out;
2760 }
2761
Hugh Dickins8e205f72014-07-23 14:00:10 -07002762 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Junaa71ecd2019-11-30 17:58:11 -08002763 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002764 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2765 spin_lock(&inode->i_lock);
2766 inode->i_private = &shmem_falloc;
2767 spin_unlock(&inode->i_lock);
2768
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002769 if ((u64)unmap_end > (u64)unmap_start)
2770 unmap_mapping_range(mapping, unmap_start,
2771 1 + unmap_end - unmap_start, 0);
2772 shmem_truncate_range(inode, offset, offset + len - 1);
2773 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002774
2775 spin_lock(&inode->i_lock);
2776 inode->i_private = NULL;
2777 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002778 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002779 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002780 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002781 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002782 }
2783
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002784 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2785 error = inode_newsize_ok(inode, offset + len);
2786 if (error)
2787 goto out;
2788
David Herrmann40e041a2014-08-08 14:25:27 -07002789 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2790 error = -EPERM;
2791 goto out;
2792 }
2793
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002794 start = offset >> PAGE_SHIFT;
2795 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002796 /* Try to avoid a swapstorm if len is impossible to satisfy */
2797 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2798 error = -ENOSPC;
2799 goto out;
2800 }
2801
Hugh Dickins8e205f72014-07-23 14:00:10 -07002802 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002803 shmem_falloc.start = start;
2804 shmem_falloc.next = start;
2805 shmem_falloc.nr_falloced = 0;
2806 shmem_falloc.nr_unswapped = 0;
2807 spin_lock(&inode->i_lock);
2808 inode->i_private = &shmem_falloc;
2809 spin_unlock(&inode->i_lock);
2810
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002811 for (index = start; index < end; index++) {
2812 struct page *page;
2813
2814 /*
2815 * Good, the fallocate(2) manpage permits EINTR: we may have
2816 * been interrupted because we are using up too much memory.
2817 */
2818 if (signal_pending(current))
2819 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002820 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2821 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002822 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002823 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002824 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002825 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002826 if (index > start) {
2827 shmem_undo_range(inode,
2828 (loff_t)start << PAGE_SHIFT,
2829 ((loff_t)index << PAGE_SHIFT) - 1, true);
2830 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002831 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002832 }
2833
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002834 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002835 * Inform shmem_writepage() how far we have reached.
2836 * No need for lock or barrier: we have the page lock.
2837 */
2838 shmem_falloc.next++;
2839 if (!PageUptodate(page))
2840 shmem_falloc.nr_falloced++;
2841
2842 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002843 * If !PageUptodate, leave it that way so that freeable pages
2844 * can be recognized if we need to rollback on error later.
2845 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002846 * than free the pages we are allocating (and SGP_CACHE pages
2847 * might still be clean: we now need to mark those dirty too).
2848 */
2849 set_page_dirty(page);
2850 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002851 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002852 cond_resched();
2853 }
2854
2855 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2856 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002857 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002858undone:
2859 spin_lock(&inode->i_lock);
2860 inode->i_private = NULL;
2861 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002862out:
Al Viro59551022016-01-22 15:40:57 -05002863 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002864 return error;
2865}
2866
David Howells726c3342006-06-23 02:02:58 -07002867static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
David Howells726c3342006-06-23 02:02:58 -07002869 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002872 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002874 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002876 buf->f_bavail =
2877 buf->f_bfree = sbinfo->max_blocks -
2878 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002879 }
2880 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 buf->f_files = sbinfo->max_inodes;
2882 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
2884 /* else leave those fields 0 like simple_statfs */
2885 return 0;
2886}
2887
2888/*
2889 * File creation. Allocate an inode, and we're done..
2890 */
2891static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002892shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002894 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 int error = -ENOSPC;
2896
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002897 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002899 error = simple_acl_create(dir, inode);
2900 if (error)
2901 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002902 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002903 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002904 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002905 if (error && error != -EOPNOTSUPP)
2906 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002907
Al Viro718deb62009-12-16 19:35:36 -05002908 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002910 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 d_instantiate(dentry, inode);
2912 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002915out_iput:
2916 iput(inode);
2917 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
Al Viro60545d02013-06-07 01:20:27 -04002920static int
2921shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2922{
2923 struct inode *inode;
2924 int error = -ENOSPC;
2925
2926 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2927 if (inode) {
2928 error = security_inode_init_security(inode, dir,
2929 NULL,
2930 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002931 if (error && error != -EOPNOTSUPP)
2932 goto out_iput;
2933 error = simple_acl_create(dir, inode);
2934 if (error)
2935 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002936 d_tmpfile(dentry, inode);
2937 }
2938 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002939out_iput:
2940 iput(inode);
2941 return error;
Al Viro60545d02013-06-07 01:20:27 -04002942}
2943
Al Viro18bb1db2011-07-26 01:41:39 -04002944static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
2946 int error;
2947
2948 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2949 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002950 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 return 0;
2952}
2953
Al Viro4acdaf22011-07-26 01:42:34 -04002954static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002955 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2958}
2959
2960/*
2961 * Link a file..
2962 */
2963static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2964{
David Howells75c3cfa2015-03-17 22:26:12 +00002965 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e602019-02-22 22:35:32 -08002966 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
2968 /*
2969 * No ordinary (disk based) filesystem counts links as inodes;
2970 * but each new link needs a new dentry, pinning lowmem, and
2971 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002972 * But if an O_TMPFILE file is linked into the tmpfs, the
2973 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002975 if (inode->i_nlink) {
Chris Downe809d5f2020-08-06 23:20:20 -07002976 ret = shmem_reserve_inode(inode->i_sb, NULL);
Darrick J. Wong1062af92019-02-21 08:48:09 -08002977 if (ret)
2978 goto out;
2979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002982 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002983 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002984 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 dget(dentry); /* Extra pinning count for the created dentry */
2986 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002987out:
2988 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989}
2990
2991static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2992{
David Howells75c3cfa2015-03-17 22:26:12 +00002993 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002995 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2996 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002999 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003000 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 dput(dentry); /* Undo the count from "create" - this does all the work */
3002 return 0;
3003}
3004
3005static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
3006{
3007 if (!simple_empty(dentry))
3008 return -ENOTEMPTY;
3009
David Howells75c3cfa2015-03-17 22:26:12 +00003010 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003011 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return shmem_unlink(dir, dentry);
3013}
3014
Miklos Szeredi37456772014-07-23 15:15:34 +02003015static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
3016{
David Howellse36cb0b2015-01-29 12:02:35 +00003017 bool old_is_dir = d_is_dir(old_dentry);
3018 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02003019
3020 if (old_dir != new_dir && old_is_dir != new_is_dir) {
3021 if (old_is_dir) {
3022 drop_nlink(old_dir);
3023 inc_nlink(new_dir);
3024 } else {
3025 drop_nlink(new_dir);
3026 inc_nlink(old_dir);
3027 }
3028 }
3029 old_dir->i_ctime = old_dir->i_mtime =
3030 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00003031 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003032 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02003033
3034 return 0;
3035}
3036
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003037static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
3038{
3039 struct dentry *whiteout;
3040 int error;
3041
3042 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3043 if (!whiteout)
3044 return -ENOMEM;
3045
3046 error = shmem_mknod(old_dir, whiteout,
3047 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3048 dput(whiteout);
3049 if (error)
3050 return error;
3051
3052 /*
3053 * Cheat and hash the whiteout while the old dentry is still in
3054 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3055 *
3056 * d_lookup() will consistently find one of them at this point,
3057 * not sure which one, but that isn't even important.
3058 */
3059 d_rehash(whiteout);
3060 return 0;
3061}
3062
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063/*
3064 * The VFS layer already does all the dentry stuff for rename,
3065 * we just have to decrement the usage count for the target if
3066 * it exists so that the VFS layer correctly free's it when it
3067 * gets overwritten.
3068 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003069static 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 -07003070{
David Howells75c3cfa2015-03-17 22:26:12 +00003071 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 int they_are_dirs = S_ISDIR(inode->i_mode);
3073
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003074 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003075 return -EINVAL;
3076
Miklos Szeredi37456772014-07-23 15:15:34 +02003077 if (flags & RENAME_EXCHANGE)
3078 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 if (!simple_empty(new_dentry))
3081 return -ENOTEMPTY;
3082
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003083 if (flags & RENAME_WHITEOUT) {
3084 int error;
3085
3086 error = shmem_whiteout(old_dir, old_dentry);
3087 if (error)
3088 return error;
3089 }
3090
David Howells75c3cfa2015-03-17 22:26:12 +00003091 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003093 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003094 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003095 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003098 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003099 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
3101
3102 old_dir->i_size -= BOGO_DIRENT_SIZE;
3103 new_dir->i_size += BOGO_DIRENT_SIZE;
3104 old_dir->i_ctime = old_dir->i_mtime =
3105 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003106 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 return 0;
3108}
3109
3110static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3111{
3112 int error;
3113 int len;
3114 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003115 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003118 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 return -ENAMETOOLONG;
3120
Joe Perches0825a6f2018-06-14 15:27:58 -07003121 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3122 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 if (!inode)
3124 return -ENOSPC;
3125
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003126 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003127 shmem_initxattrs, NULL);
Mateusz Nosek343c3d72020-04-06 20:07:54 -07003128 if (error && error != -EOPNOTSUPP) {
3129 iput(inode);
3130 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003131 }
3132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003134 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003135 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3136 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003137 iput(inode);
3138 return -ENOMEM;
3139 }
3140 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003142 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003143 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 if (error) {
3145 iput(inode);
3146 return error;
3147 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003148 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003150 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003151 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003153 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003154 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003157 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 d_instantiate(dentry, inode);
3159 dget(dentry);
3160 return 0;
3161}
3162
Al Virofceef392015-12-29 15:58:39 -05003163static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Al Virofceef392015-12-29 15:58:39 -05003165 mark_page_accessed(arg);
3166 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Al Viro6b255392015-11-17 10:20:54 -05003169static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003170 struct inode *inode,
3171 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003174 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003175 if (!dentry) {
3176 page = find_get_page(inode->i_mapping, 0);
3177 if (!page)
3178 return ERR_PTR(-ECHILD);
3179 if (!PageUptodate(page)) {
3180 put_page(page);
3181 return ERR_PTR(-ECHILD);
3182 }
3183 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003184 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003185 if (error)
3186 return ERR_PTR(error);
3187 unlock_page(page);
3188 }
Al Virofceef392015-12-29 15:58:39 -05003189 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003190 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Eric Parisb09e0fa2011-05-24 17:12:39 -07003193#ifdef CONFIG_TMPFS_XATTR
3194/*
3195 * Superblocks without xattr inode operations may get some security.* xattr
3196 * support from the LSM "for free". As soon as we have any other xattrs
3197 * like ACLs, we also need to implement the security.* handlers at
3198 * filesystem level, though.
3199 */
3200
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003201/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003202 * Callback for security_inode_init_security() for acquiring xattrs.
3203 */
3204static int shmem_initxattrs(struct inode *inode,
3205 const struct xattr *xattr_array,
3206 void *fs_info)
3207{
3208 struct shmem_inode_info *info = SHMEM_I(inode);
3209 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003210 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003211 size_t len;
3212
3213 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003214 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003215 if (!new_xattr)
3216 return -ENOMEM;
3217
3218 len = strlen(xattr->name) + 1;
3219 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3220 GFP_KERNEL);
3221 if (!new_xattr->name) {
Chengguang Xu3bef7352020-07-23 21:15:14 -07003222 kvfree(new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003223 return -ENOMEM;
3224 }
3225
3226 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3227 XATTR_SECURITY_PREFIX_LEN);
3228 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3229 xattr->name, len);
3230
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003231 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003232 }
3233
3234 return 0;
3235}
3236
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003237static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003238 struct dentry *unused, struct inode *inode,
Mark Salyzyn3484eba2019-11-04 08:57:10 -08003239 const char *name, void *buffer, size_t size,
3240 int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003241{
Al Virob2968212016-04-10 20:48:24 -04003242 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003243
3244 name = xattr_full_name(handler, name);
3245 return simple_xattr_get(&info->xattrs, name, buffer, size);
3246}
3247
3248static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003249 struct dentry *unused, struct inode *inode,
3250 const char *name, const void *value,
3251 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003252{
Al Viro59301222016-05-27 10:19:30 -04003253 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003254
3255 name = xattr_full_name(handler, name);
Daniel Xua46a2292020-03-12 13:03:15 -07003256 return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003257}
3258
3259static const struct xattr_handler shmem_security_xattr_handler = {
3260 .prefix = XATTR_SECURITY_PREFIX,
3261 .get = shmem_xattr_handler_get,
3262 .set = shmem_xattr_handler_set,
3263};
3264
3265static const struct xattr_handler shmem_trusted_xattr_handler = {
3266 .prefix = XATTR_TRUSTED_PREFIX,
3267 .get = shmem_xattr_handler_get,
3268 .set = shmem_xattr_handler_set,
3269};
3270
Eric Parisb09e0fa2011-05-24 17:12:39 -07003271static const struct xattr_handler *shmem_xattr_handlers[] = {
3272#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003273 &posix_acl_access_xattr_handler,
3274 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003275#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003276 &shmem_security_xattr_handler,
3277 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003278 NULL
3279};
3280
Eric Parisb09e0fa2011-05-24 17:12:39 -07003281static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3282{
David Howells75c3cfa2015-03-17 22:26:12 +00003283 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003284 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003285}
3286#endif /* CONFIG_TMPFS_XATTR */
3287
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003288static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003289 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003290#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003291 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003292#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293};
3294
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003295static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003296 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003297#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003298 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003299#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003300};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003301
David M. Grimes91828a42006-10-17 00:09:45 -07003302static struct dentry *shmem_get_parent(struct dentry *child)
3303{
3304 return ERR_PTR(-ESTALE);
3305}
3306
3307static int shmem_match(struct inode *ino, void *vfh)
3308{
3309 __u32 *fh = vfh;
3310 __u64 inum = fh[2];
3311 inum = (inum << 32) | fh[1];
3312 return ino->i_ino == inum && fh[0] == ino->i_generation;
3313}
3314
Amir Goldstein12ba7802018-06-07 17:07:15 -07003315/* Find any alias of inode, but prefer a hashed alias */
3316static struct dentry *shmem_find_alias(struct inode *inode)
3317{
3318 struct dentry *alias = d_find_alias(inode);
3319
3320 return alias ?: d_find_any_alias(inode);
3321}
3322
3323
Christoph Hellwig480b1162007-10-21 16:42:13 -07003324static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3325 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003326{
David M. Grimes91828a42006-10-17 00:09:45 -07003327 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003328 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003329 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003330
Christoph Hellwig480b1162007-10-21 16:42:13 -07003331 if (fh_len < 3)
3332 return NULL;
3333
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003334 inum = fid->raw[2];
3335 inum = (inum << 32) | fid->raw[1];
3336
Christoph Hellwig480b1162007-10-21 16:42:13 -07003337 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3338 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003339 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003340 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003341 iput(inode);
3342 }
3343
Christoph Hellwig480b1162007-10-21 16:42:13 -07003344 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003345}
3346
Al Virob0b03822012-04-02 14:34:06 -04003347static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3348 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003349{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303350 if (*len < 3) {
3351 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003352 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303353 }
David M. Grimes91828a42006-10-17 00:09:45 -07003354
Al Viro1d3382cb2010-10-23 15:19:20 -04003355 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003356 /* Unfortunately insert_inode_hash is not idempotent,
3357 * so as we hash inodes here rather than at creation
3358 * time, we need a lock to ensure we only try
3359 * to do it once
3360 */
3361 static DEFINE_SPINLOCK(lock);
3362 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003363 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003364 __insert_inode_hash(inode,
3365 inode->i_ino + inode->i_generation);
3366 spin_unlock(&lock);
3367 }
3368
3369 fh[0] = inode->i_generation;
3370 fh[1] = inode->i_ino;
3371 fh[2] = ((__u64)inode->i_ino) >> 32;
3372
3373 *len = 3;
3374 return 1;
3375}
3376
Christoph Hellwig39655162007-10-21 16:42:17 -07003377static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003378 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003379 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003380 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003381};
3382
Al Viro626c3922019-09-08 20:28:06 -04003383enum shmem_param {
3384 Opt_gid,
3385 Opt_huge,
3386 Opt_mode,
3387 Opt_mpol,
3388 Opt_nr_blocks,
3389 Opt_nr_inodes,
3390 Opt_size,
3391 Opt_uid,
Chris Downea3271f2020-08-06 23:20:25 -07003392 Opt_inode32,
3393 Opt_inode64,
Al Viro626c3922019-09-08 20:28:06 -04003394};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Al Viro5eede622019-12-16 13:33:32 -05003396static const struct constant_table shmem_param_enums_huge[] = {
Al Viro2710c957a2019-09-06 22:12:08 -04003397 {"never", SHMEM_HUGE_NEVER },
3398 {"always", SHMEM_HUGE_ALWAYS },
3399 {"within_size", SHMEM_HUGE_WITHIN_SIZE },
3400 {"advise", SHMEM_HUGE_ADVISE },
Al Viro2710c957a2019-09-06 22:12:08 -04003401 {}
3402};
3403
Al Virod7167b12019-09-07 07:23:15 -04003404const struct fs_parameter_spec shmem_fs_parameters[] = {
Al Viro626c3922019-09-08 20:28:06 -04003405 fsparam_u32 ("gid", Opt_gid),
Al Viro2710c957a2019-09-06 22:12:08 -04003406 fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge),
Al Viro626c3922019-09-08 20:28:06 -04003407 fsparam_u32oct("mode", Opt_mode),
3408 fsparam_string("mpol", Opt_mpol),
3409 fsparam_string("nr_blocks", Opt_nr_blocks),
3410 fsparam_string("nr_inodes", Opt_nr_inodes),
3411 fsparam_string("size", Opt_size),
3412 fsparam_u32 ("uid", Opt_uid),
Chris Downea3271f2020-08-06 23:20:25 -07003413 fsparam_flag ("inode32", Opt_inode32),
3414 fsparam_flag ("inode64", Opt_inode64),
Al Viro626c3922019-09-08 20:28:06 -04003415 {}
3416};
3417
David Howellsf3235622019-03-25 16:38:31 +00003418static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
Al Viro626c3922019-09-08 20:28:06 -04003419{
David Howellsf3235622019-03-25 16:38:31 +00003420 struct shmem_options *ctx = fc->fs_private;
Al Viro626c3922019-09-08 20:28:06 -04003421 struct fs_parse_result result;
3422 unsigned long long size;
3423 char *rest;
3424 int opt;
3425
Al Virod7167b12019-09-07 07:23:15 -04003426 opt = fs_parse(fc, shmem_fs_parameters, param, &result);
David Howellsf3235622019-03-25 16:38:31 +00003427 if (opt < 0)
Al Viro626c3922019-09-08 20:28:06 -04003428 return opt;
Al Viro626c3922019-09-08 20:28:06 -04003429
3430 switch (opt) {
3431 case Opt_size:
3432 size = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003433 if (*rest == '%') {
3434 size <<= PAGE_SHIFT;
3435 size *= totalram_pages();
3436 do_div(size, 100);
3437 rest++;
3438 }
3439 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003440 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003441 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE);
3442 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003443 break;
3444 case Opt_nr_blocks:
3445 ctx->blocks = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003446 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003447 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003448 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003449 break;
3450 case Opt_nr_inodes:
3451 ctx->inodes = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003452 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003453 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003454 ctx->seen |= SHMEM_SEEN_INODES;
Al Viro626c3922019-09-08 20:28:06 -04003455 break;
3456 case Opt_mode:
3457 ctx->mode = result.uint_32 & 07777;
3458 break;
3459 case Opt_uid:
3460 ctx->uid = make_kuid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003461 if (!uid_valid(ctx->uid))
Al Viro626c3922019-09-08 20:28:06 -04003462 goto bad_value;
3463 break;
3464 case Opt_gid:
3465 ctx->gid = make_kgid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003466 if (!gid_valid(ctx->gid))
Al Viro626c3922019-09-08 20:28:06 -04003467 goto bad_value;
3468 break;
3469 case Opt_huge:
3470 ctx->huge = result.uint_32;
3471 if (ctx->huge != SHMEM_HUGE_NEVER &&
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003472 !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Al Viro626c3922019-09-08 20:28:06 -04003473 has_transparent_hugepage()))
3474 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003475 ctx->seen |= SHMEM_SEEN_HUGE;
Al Viro626c3922019-09-08 20:28:06 -04003476 break;
3477 case Opt_mpol:
3478 if (IS_ENABLED(CONFIG_NUMA)) {
3479 mpol_put(ctx->mpol);
3480 ctx->mpol = NULL;
3481 if (mpol_parse_str(param->string, &ctx->mpol))
3482 goto bad_value;
3483 break;
3484 }
3485 goto unsupported_parameter;
Chris Downea3271f2020-08-06 23:20:25 -07003486 case Opt_inode32:
3487 ctx->full_inums = false;
3488 ctx->seen |= SHMEM_SEEN_INUMS;
3489 break;
3490 case Opt_inode64:
3491 if (sizeof(ino_t) < 8) {
3492 return invalfc(fc,
3493 "Cannot use inode64 with <64bit inums in kernel\n");
3494 }
3495 ctx->full_inums = true;
3496 ctx->seen |= SHMEM_SEEN_INUMS;
3497 break;
Al Viroe04dc422019-09-08 19:20:12 -04003498 }
3499 return 0;
3500
Al Viro626c3922019-09-08 20:28:06 -04003501unsupported_parameter:
Al Virof35aa2b2019-12-21 21:35:55 -05003502 return invalfc(fc, "Unsupported parameter '%s'", param->key);
Al Viro626c3922019-09-08 20:28:06 -04003503bad_value:
Al Virof35aa2b2019-12-21 21:35:55 -05003504 return invalfc(fc, "Bad value for '%s'", param->key);
Al Viroe04dc422019-09-08 19:20:12 -04003505}
3506
David Howellsf3235622019-03-25 16:38:31 +00003507static int shmem_parse_options(struct fs_context *fc, void *data)
Al Viroe04dc422019-09-08 19:20:12 -04003508{
David Howellsf3235622019-03-25 16:38:31 +00003509 char *options = data;
3510
Al Viro33f37c62019-10-09 22:48:01 -04003511 if (options) {
3512 int err = security_sb_eat_lsm_opts(options, &fc->security);
3513 if (err)
3514 return err;
3515 }
3516
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003517 while (options != NULL) {
Al Viro626c3922019-09-08 20:28:06 -04003518 char *this_char = options;
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003519 for (;;) {
3520 /*
3521 * NUL-terminate this option: unfortunately,
3522 * mount options form a comma-separated list,
3523 * but mpol's nodelist may also contain commas.
3524 */
3525 options = strchr(options, ',');
3526 if (options == NULL)
3527 break;
3528 options++;
3529 if (!isdigit(*options)) {
3530 options[-1] = '\0';
3531 break;
3532 }
3533 }
Al Viro626c3922019-09-08 20:28:06 -04003534 if (*this_char) {
3535 char *value = strchr(this_char,'=');
David Howellsf3235622019-03-25 16:38:31 +00003536 size_t len = 0;
Al Viro626c3922019-09-08 20:28:06 -04003537 int err;
3538
3539 if (value) {
3540 *value++ = '\0';
David Howellsf3235622019-03-25 16:38:31 +00003541 len = strlen(value);
Al Viro626c3922019-09-08 20:28:06 -04003542 }
David Howellsf3235622019-03-25 16:38:31 +00003543 err = vfs_parse_fs_string(fc, this_char, value, len);
3544 if (err < 0)
3545 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
3548 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
David Howellsf3235622019-03-25 16:38:31 +00003551/*
3552 * Reconfigure a shmem filesystem.
3553 *
3554 * Note that we disallow change from limited->unlimited blocks/inodes while any
3555 * are in use; but we must separately disallow unlimited->limited, because in
3556 * that case we have no record of how much is already in use.
3557 */
3558static int shmem_reconfigure(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559{
David Howellsf3235622019-03-25 16:38:31 +00003560 struct shmem_options *ctx = fc->fs_private;
3561 struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003562 unsigned long inodes;
David Howellsf3235622019-03-25 16:38:31 +00003563 const char *err;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003564
3565 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003566 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
David Howellsf3235622019-03-25 16:38:31 +00003567 if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) {
3568 if (!sbinfo->max_blocks) {
3569 err = "Cannot retroactively limit size";
Al Viro0b5071d2019-09-08 18:49:18 -04003570 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003571 }
Al Viro0b5071d2019-09-08 18:49:18 -04003572 if (percpu_counter_compare(&sbinfo->used_blocks,
David Howellsf3235622019-03-25 16:38:31 +00003573 ctx->blocks) > 0) {
3574 err = "Too small a size for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003575 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003576 }
Al Viro0b5071d2019-09-08 18:49:18 -04003577 }
David Howellsf3235622019-03-25 16:38:31 +00003578 if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) {
3579 if (!sbinfo->max_inodes) {
3580 err = "Cannot retroactively limit inodes";
Al Viro0b5071d2019-09-08 18:49:18 -04003581 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003582 }
3583 if (ctx->inodes < inodes) {
3584 err = "Too few inodes for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003585 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003586 }
Al Viro0b5071d2019-09-08 18:49:18 -04003587 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003588
Chris Downea3271f2020-08-06 23:20:25 -07003589 if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums &&
3590 sbinfo->next_ino > UINT_MAX) {
3591 err = "Current inum too high to switch to 32-bit inums";
3592 goto out;
3593 }
3594
David Howellsf3235622019-03-25 16:38:31 +00003595 if (ctx->seen & SHMEM_SEEN_HUGE)
3596 sbinfo->huge = ctx->huge;
Chris Downea3271f2020-08-06 23:20:25 -07003597 if (ctx->seen & SHMEM_SEEN_INUMS)
3598 sbinfo->full_inums = ctx->full_inums;
David Howellsf3235622019-03-25 16:38:31 +00003599 if (ctx->seen & SHMEM_SEEN_BLOCKS)
3600 sbinfo->max_blocks = ctx->blocks;
3601 if (ctx->seen & SHMEM_SEEN_INODES) {
3602 sbinfo->max_inodes = ctx->inodes;
3603 sbinfo->free_inodes = ctx->inodes - inodes;
Al Viro0b5071d2019-09-08 18:49:18 -04003604 }
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003605
Greg Thelen5f001102013-02-22 16:36:01 -08003606 /*
3607 * Preserve previous mempolicy unless mpol remount option was specified.
3608 */
David Howellsf3235622019-03-25 16:38:31 +00003609 if (ctx->mpol) {
Greg Thelen5f001102013-02-22 16:36:01 -08003610 mpol_put(sbinfo->mpol);
David Howellsf3235622019-03-25 16:38:31 +00003611 sbinfo->mpol = ctx->mpol; /* transfers initial ref */
3612 ctx->mpol = NULL;
Greg Thelen5f001102013-02-22 16:36:01 -08003613 }
David Howellsf3235622019-03-25 16:38:31 +00003614 spin_unlock(&sbinfo->stat_lock);
3615 return 0;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003616out:
3617 spin_unlock(&sbinfo->stat_lock);
Al Virof35aa2b2019-12-21 21:35:55 -05003618 return invalfc(fc, "%s", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003620
Al Viro34c80b12011-12-08 21:32:45 -05003621static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003622{
Al Viro34c80b12011-12-08 21:32:45 -05003623 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003624
3625 if (sbinfo->max_blocks != shmem_default_max_blocks())
3626 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003627 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003628 if (sbinfo->max_inodes != shmem_default_max_inodes())
3629 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003630 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003631 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003632 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3633 seq_printf(seq, ",uid=%u",
3634 from_kuid_munged(&init_user_ns, sbinfo->uid));
3635 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3636 seq_printf(seq, ",gid=%u",
3637 from_kgid_munged(&init_user_ns, sbinfo->gid));
Chris Downea3271f2020-08-06 23:20:25 -07003638
3639 /*
3640 * Showing inode{64,32} might be useful even if it's the system default,
3641 * since then people don't have to resort to checking both here and
3642 * /proc/config.gz to confirm 64-bit inums were successfully applied
3643 * (which may not even exist if IKCONFIG_PROC isn't enabled).
3644 *
3645 * We hide it when inode64 isn't the default and we are using 32-bit
3646 * inodes, since that probably just means the feature isn't even under
3647 * consideration.
3648 *
3649 * As such:
3650 *
3651 * +-----------------+-----------------+
3652 * | TMPFS_INODE64=y | TMPFS_INODE64=n |
3653 * +------------------+-----------------+-----------------+
3654 * | full_inums=true | show | show |
3655 * | full_inums=false | show | hide |
3656 * +------------------+-----------------+-----------------+
3657 *
3658 */
3659 if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums)
3660 seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32));
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003661#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003662 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3663 if (sbinfo->huge)
3664 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3665#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003666 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003667 return 0;
3668}
David Herrmann9183df22014-08-08 14:25:29 -07003669
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003670#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
3672static void shmem_put_super(struct super_block *sb)
3673{
Hugh Dickins602586a2010-08-17 15:23:56 -07003674 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3675
Chris Downe809d5f2020-08-06 23:20:20 -07003676 free_percpu(sbinfo->ino_batch);
Hugh Dickins602586a2010-08-17 15:23:56 -07003677 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003678 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003679 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 sb->s_fs_info = NULL;
3681}
3682
David Howellsf3235622019-03-25 16:38:31 +00003683static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
David Howellsf3235622019-03-25 16:38:31 +00003685 struct shmem_options *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003687 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003688 int err = -ENOMEM;
3689
3690 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003691 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003692 L1_CACHE_BYTES), GFP_KERNEL);
3693 if (!sbinfo)
3694 return -ENOMEM;
3695
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003696 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003698#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 /*
3700 * Per default we only allow half of the physical ram per
3701 * tmpfs instance, limiting inodes to one per page of lowmem;
3702 * but the internal instance is left unlimited.
3703 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003704 if (!(sb->s_flags & SB_KERNMOUNT)) {
David Howellsf3235622019-03-25 16:38:31 +00003705 if (!(ctx->seen & SHMEM_SEEN_BLOCKS))
3706 ctx->blocks = shmem_default_max_blocks();
3707 if (!(ctx->seen & SHMEM_SEEN_INODES))
3708 ctx->inodes = shmem_default_max_inodes();
Chris Downea3271f2020-08-06 23:20:25 -07003709 if (!(ctx->seen & SHMEM_SEEN_INUMS))
3710 ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64);
Al Viroca4e0512013-08-31 12:57:10 -04003711 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003712 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 }
David M. Grimes91828a42006-10-17 00:09:45 -07003714 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003715 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003717 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718#endif
David Howellsf3235622019-03-25 16:38:31 +00003719 sbinfo->max_blocks = ctx->blocks;
3720 sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
Chris Downe809d5f2020-08-06 23:20:20 -07003721 if (sb->s_flags & SB_KERNMOUNT) {
3722 sbinfo->ino_batch = alloc_percpu(ino_t);
3723 if (!sbinfo->ino_batch)
3724 goto failed;
3725 }
David Howellsf3235622019-03-25 16:38:31 +00003726 sbinfo->uid = ctx->uid;
3727 sbinfo->gid = ctx->gid;
Chris Downea3271f2020-08-06 23:20:25 -07003728 sbinfo->full_inums = ctx->full_inums;
David Howellsf3235622019-03-25 16:38:31 +00003729 sbinfo->mode = ctx->mode;
3730 sbinfo->huge = ctx->huge;
3731 sbinfo->mpol = ctx->mpol;
3732 ctx->mpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003734 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003735 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003736 goto failed;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003737 spin_lock_init(&sbinfo->shrinklist_lock);
3738 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003739
Hugh Dickins285b2c42011-08-03 16:21:20 -07003740 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003741 sb->s_blocksize = PAGE_SIZE;
3742 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 sb->s_magic = TMPFS_MAGIC;
3744 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003745 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003746#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003747 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003748#endif
3749#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003750 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003751#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003752 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003753
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003754 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (!inode)
3756 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003757 inode->i_uid = sbinfo->uid;
3758 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003759 sb->s_root = d_make_root(inode);
3760 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003761 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return 0;
3763
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764failed:
3765 shmem_put_super(sb);
3766 return err;
3767}
3768
David Howellsf3235622019-03-25 16:38:31 +00003769static int shmem_get_tree(struct fs_context *fc)
3770{
3771 return get_tree_nodev(fc, shmem_fill_super);
3772}
3773
3774static void shmem_free_fc(struct fs_context *fc)
3775{
3776 struct shmem_options *ctx = fc->fs_private;
3777
3778 if (ctx) {
3779 mpol_put(ctx->mpol);
3780 kfree(ctx);
3781 }
3782}
3783
3784static const struct fs_context_operations shmem_fs_context_ops = {
3785 .free = shmem_free_fc,
3786 .get_tree = shmem_get_tree,
3787#ifdef CONFIG_TMPFS
3788 .parse_monolithic = shmem_parse_options,
3789 .parse_param = shmem_parse_one,
3790 .reconfigure = shmem_reconfigure,
3791#endif
3792};
3793
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003794static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
3796static struct inode *shmem_alloc_inode(struct super_block *sb)
3797{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003798 struct shmem_inode_info *info;
3799 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3800 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003802 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803}
3804
Al Viro74b1da52019-04-15 23:19:05 -04003805static void shmem_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003806{
Al Viro84e710d2016-04-15 00:58:55 -04003807 if (S_ISLNK(inode->i_mode))
3808 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003809 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3810}
3811
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812static void shmem_destroy_inode(struct inode *inode)
3813{
Al Viro09208d12011-07-26 03:15:03 -04003814 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003818static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003820 struct shmem_inode_info *info = foo;
3821 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003824static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3827 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003828 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003831static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003833 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003836static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003838 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003840 .write_begin = shmem_write_begin,
3841 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003843#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003844 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003845#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003846 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847};
3848
Helge Deller15ad7cd2006-12-06 20:40:36 -08003849static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003851 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003853 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003854 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003855 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003856 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003857 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003858 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003859 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860#endif
3861};
3862
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003863static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003864 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003865 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003866#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003867 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003868 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003869#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870};
3871
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003872static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873#ifdef CONFIG_TMPFS
3874 .create = shmem_create,
3875 .lookup = simple_lookup,
3876 .link = shmem_link,
3877 .unlink = shmem_unlink,
3878 .symlink = shmem_symlink,
3879 .mkdir = shmem_mkdir,
3880 .rmdir = shmem_rmdir,
3881 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003882 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003883 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003885#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003886 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003887#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003888#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003889 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003890 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003891#endif
3892};
3893
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003894static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003895#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003896 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003897#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003898#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003899 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003900 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902};
3903
Hugh Dickins759b9772007-03-05 00:30:28 -08003904static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 .alloc_inode = shmem_alloc_inode,
Al Viro74b1da52019-04-15 23:19:05 -04003906 .free_inode = shmem_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 .destroy_inode = shmem_destroy_inode,
3908#ifdef CONFIG_TMPFS
3909 .statfs = shmem_statfs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003910 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911#endif
Al Viro1f895f72010-06-05 19:10:41 -04003912 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 .drop_inode = generic_delete_inode,
3914 .put_super = shmem_put_super,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003915#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003916 .nr_cached_objects = shmem_unused_huge_count,
3917 .free_cached_objects = shmem_unused_huge_scan,
3918#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919};
3920
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003921static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003922 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003923 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924#ifdef CONFIG_NUMA
3925 .set_policy = shmem_set_policy,
3926 .get_policy = shmem_get_policy,
3927#endif
Vinayak Menon35eacb52021-03-18 15:20:17 +05303928#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
3929 .allow_speculation = filemap_allow_speculation,
3930#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931};
3932
David Howellsf3235622019-03-25 16:38:31 +00003933int shmem_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
David Howellsf3235622019-03-25 16:38:31 +00003935 struct shmem_options *ctx;
3936
3937 ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL);
3938 if (!ctx)
3939 return -ENOMEM;
3940
3941 ctx->mode = 0777 | S_ISVTX;
3942 ctx->uid = current_fsuid();
3943 ctx->gid = current_fsgid();
3944
3945 fc->fs_private = ctx;
3946 fc->ops = &shmem_fs_context_ops;
3947 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003950static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 .owner = THIS_MODULE,
3952 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00003953 .init_fs_context = shmem_init_fs_context,
3954#ifdef CONFIG_TMPFS
Al Virod7167b12019-09-07 07:23:15 -04003955 .parameters = shmem_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +00003956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 .kill_sb = kill_litter_super,
Matthew Wilcox (Oracle)01c70262020-10-15 20:06:00 -07003958 .fs_flags = FS_USERNS_MOUNT | FS_THP_SUPPORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003961int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
3963 int error;
3964
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003965 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003967 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003969 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 goto out2;
3971 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003972
Al Viroca4e0512013-08-31 12:57:10 -04003973 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 if (IS_ERR(shm_mnt)) {
3975 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003976 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 goto out1;
3978 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003979
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003980#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003981 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003982 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3983 else
3984 shmem_huge = 0; /* just in case it was patched */
3985#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 return 0;
3987
3988out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003989 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003991 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 shm_mnt = ERR_PTR(error);
3993 return error;
3994}
Matt Mackall853ac432009-01-06 14:40:20 -08003995
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003996#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003997static ssize_t shmem_enabled_show(struct kobject *kobj,
3998 struct kobj_attribute *attr, char *buf)
3999{
Colin Ian King26083eb62019-11-30 17:58:04 -08004000 static const int values[] = {
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004001 SHMEM_HUGE_ALWAYS,
4002 SHMEM_HUGE_WITHIN_SIZE,
4003 SHMEM_HUGE_ADVISE,
4004 SHMEM_HUGE_NEVER,
4005 SHMEM_HUGE_DENY,
4006 SHMEM_HUGE_FORCE,
4007 };
4008 int i, count;
4009
4010 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
4011 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
4012
4013 count += sprintf(buf + count, fmt,
4014 shmem_format_huge(values[i]));
4015 }
4016 buf[count - 1] = '\n';
4017 return count;
4018}
4019
4020static ssize_t shmem_enabled_store(struct kobject *kobj,
4021 struct kobj_attribute *attr, const char *buf, size_t count)
4022{
4023 char tmp[16];
4024 int huge;
4025
4026 if (count + 1 > sizeof(tmp))
4027 return -EINVAL;
4028 memcpy(tmp, buf, count);
4029 tmp[count] = '\0';
4030 if (count && tmp[count - 1] == '\n')
4031 tmp[count - 1] = '\0';
4032
4033 huge = shmem_parse_huge(tmp);
4034 if (huge == -EINVAL)
4035 return -EINVAL;
4036 if (!has_transparent_hugepage() &&
4037 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
4038 return -EINVAL;
4039
4040 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07004041 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004042 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
4043 return count;
4044}
4045
4046struct kobj_attribute shmem_enabled_attr =
4047 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004048#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004049
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004050#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004051bool shmem_huge_enabled(struct vm_area_struct *vma)
4052{
4053 struct inode *inode = file_inode(vma->vm_file);
4054 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4055 loff_t i_size;
4056 pgoff_t off;
4057
Miaohe Linb6559732021-06-30 18:47:50 -07004058 if (!transhuge_vma_enabled(vma, vma->vm_flags))
Yang Shic0630662019-07-18 15:57:27 -07004059 return false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004060 if (shmem_huge == SHMEM_HUGE_FORCE)
4061 return true;
4062 if (shmem_huge == SHMEM_HUGE_DENY)
4063 return false;
4064 switch (sbinfo->huge) {
4065 case SHMEM_HUGE_NEVER:
4066 return false;
4067 case SHMEM_HUGE_ALWAYS:
4068 return true;
4069 case SHMEM_HUGE_WITHIN_SIZE:
4070 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
4071 i_size = round_up(i_size_read(inode), PAGE_SIZE);
4072 if (i_size >= HPAGE_PMD_SIZE &&
4073 i_size >> PAGE_SHIFT >= off)
4074 return true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07004075 fallthrough;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004076 case SHMEM_HUGE_ADVISE:
4077 /* TODO: implement fadvise() hints */
4078 return (vma->vm_flags & VM_HUGEPAGE);
4079 default:
4080 VM_BUG_ON(1);
4081 return false;
4082 }
4083}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004084#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004085
Matt Mackall853ac432009-01-06 14:40:20 -08004086#else /* !CONFIG_SHMEM */
4087
4088/*
4089 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
4090 *
4091 * This is intended for small system where the benefits of the full
4092 * shmem code (swap-backed and resource-limited) are outweighed by
4093 * their complexity. On systems without swap this code should be
4094 * effectively equivalent, but much lighter weight.
4095 */
4096
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004097static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08004098 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00004099 .init_fs_context = ramfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04004100 .parameters = ramfs_fs_parameters,
Matt Mackall853ac432009-01-06 14:40:20 -08004101 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004102 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08004103};
4104
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004105int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08004106{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004107 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08004108
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004109 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08004110 BUG_ON(IS_ERR(shm_mnt));
4111
4112 return 0;
4113}
4114
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08004115int shmem_unuse(unsigned int type, bool frontswap,
4116 unsigned long *fs_pages_to_unuse)
Matt Mackall853ac432009-01-06 14:40:20 -08004117{
4118 return 0;
4119}
4120
Hugh Dickins3f96b792009-09-21 17:03:37 -07004121int shmem_lock(struct file *file, int lock, struct user_struct *user)
4122{
4123 return 0;
4124}
4125
Hugh Dickins24513262012-01-20 14:34:21 -08004126void shmem_unlock_mapping(struct address_space *mapping)
4127{
4128}
4129
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07004130#ifdef CONFIG_MMU
4131unsigned long shmem_get_unmapped_area(struct file *file,
4132 unsigned long addr, unsigned long len,
4133 unsigned long pgoff, unsigned long flags)
4134{
4135 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4136}
4137#endif
4138
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004139void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07004140{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004141 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07004142}
4143EXPORT_SYMBOL_GPL(shmem_truncate_range);
4144
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004145#define shmem_vm_ops generic_file_vm_ops
4146#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03004147#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004148#define shmem_acct_size(flags, size) 0
4149#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08004150
4151#endif /* CONFIG_SHMEM */
4152
4153/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Matthew Auld703321b2017-10-06 23:18:13 +01004155static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00004156 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04004159 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Matthew Auld703321b2017-10-06 23:18:13 +01004161 if (IS_ERR(mnt))
4162 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Hugh Dickins285b2c42011-08-03 16:21:20 -07004164 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 return ERR_PTR(-EINVAL);
4166
4167 if (shmem_acct_size(flags, size))
4168 return ERR_PTR(-ENOMEM);
4169
Al Viro93dec2d2018-07-08 23:02:03 -04004170 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
4171 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04004172 if (unlikely(!inode)) {
4173 shmem_unacct_size(flags, size);
4174 return ERR_PTR(-ENOSPC);
4175 }
Eric Parisc7277092013-12-02 11:24:19 +00004176 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004178 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004179 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04004180 if (!IS_ERR(res))
4181 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
4182 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05004183 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04004184 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05004185 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186}
Eric Parisc7277092013-12-02 11:24:19 +00004187
4188/**
4189 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4190 * kernel internal. There will be NO LSM permission checks against the
4191 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004192 * higher layer. The users are the big_key and shm implementations. LSM
4193 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004194 * @name: name for dentry (to be seen in /proc/<pid>/maps
4195 * @size: size to be set for the file
4196 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4197 */
4198struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4199{
Matthew Auld703321b2017-10-06 23:18:13 +01004200 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00004201}
4202
4203/**
4204 * shmem_file_setup - get an unlinked file living in tmpfs
4205 * @name: name for dentry (to be seen in /proc/<pid>/maps
4206 * @size: size to be set for the file
4207 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4208 */
4209struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4210{
Matthew Auld703321b2017-10-06 23:18:13 +01004211 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00004212}
Keith Packard395e0dd2008-06-20 00:08:06 -07004213EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Randy Dunlap46711812008-03-19 17:00:41 -07004215/**
Matthew Auld703321b2017-10-06 23:18:13 +01004216 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4217 * @mnt: the tmpfs mount where the file will be created
4218 * @name: name for dentry (to be seen in /proc/<pid>/maps
4219 * @size: size to be set for the file
4220 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4221 */
4222struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4223 loff_t size, unsigned long flags)
4224{
4225 return __shmem_file_setup(mnt, name, size, flags, 0);
4226}
4227EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4228
4229/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 * shmem_zero_setup - setup a shared anonymous mapping
Peter Collingbourne45e55302020-08-06 23:23:37 -07004231 * @vma: the vma to be mmapped is prepared by do_mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 */
4233int shmem_zero_setup(struct vm_area_struct *vma)
4234{
4235 struct file *file;
4236 loff_t size = vma->vm_end - vma->vm_start;
4237
Hugh Dickins66fc1302015-06-14 09:48:09 -07004238 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004239 * Cloning a new file under mmap_lock leads to a lock ordering conflict
Hugh Dickins66fc1302015-06-14 09:48:09 -07004240 * between XFS directory reading and selinux: since this file is only
4241 * accessible to the user through its mapping, use S_PRIVATE flag to
4242 * bypass file security, in the same way as shmem_kernel_file_setup().
4243 */
Matthew Auld703321b2017-10-06 23:18:13 +01004244 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 if (IS_ERR(file))
4246 return PTR_ERR(file);
4247
4248 if (vma->vm_file)
4249 fput(vma->vm_file);
4250 vma->vm_file = file;
4251 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004252
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004253 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004254 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4255 (vma->vm_end & HPAGE_PMD_MASK)) {
4256 khugepaged_enter(vma, vma->vm_flags);
4257 }
4258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 return 0;
4260}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004261
4262/**
4263 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4264 * @mapping: the page's address_space
4265 * @index: the page index
4266 * @gfp: the page allocator flags to use if allocating
4267 *
4268 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4269 * with any new page allocations done using the specified allocation flags.
4270 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4271 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4272 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4273 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004274 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4275 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004276 */
4277struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4278 pgoff_t index, gfp_t gfp)
4279{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004280#ifdef CONFIG_SHMEM
4281 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004282 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004283 int error;
4284
4285 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004286 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004287 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004288 if (error)
4289 page = ERR_PTR(error);
4290 else
4291 unlock_page(page);
4292 return page;
4293#else
4294 /*
4295 * The tiny !SHMEM case uses ramfs without swap
4296 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004297 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004298#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004299}
4300EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
Vinayak Menon9975da52021-03-18 16:49:18 +05304301
Charan Teja Reddy8011eb22021-06-25 12:30:50 +05304302void shmem_mark_page_lazyfree(struct page *page, bool tail)
Vinayak Menon9975da52021-03-18 16:49:18 +05304303{
Charan Teja Reddy8011eb22021-06-25 12:30:50 +05304304 mark_page_lazyfree_movetail(page, tail);
Vinayak Menon9975da52021-03-18 16:49:18 +05304305}
4306EXPORT_SYMBOL_GPL(shmem_mark_page_lazyfree);
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304307
4308int reclaim_shmem_address_space(struct address_space *mapping)
4309{
Suren Baghdasaryan404df472022-03-24 11:00:05 -07004310#ifdef CONFIG_SHMEM
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304311 pgoff_t start = 0;
4312 struct page *page;
4313 LIST_HEAD(page_list);
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304314 XA_STATE(xas, &mapping->i_pages, start);
4315
4316 if (!shmem_mapping(mapping))
4317 return -EINVAL;
4318
4319 lru_add_drain();
4320
4321 rcu_read_lock();
4322 xas_for_each(&xas, page, ULONG_MAX) {
4323 if (xas_retry(&xas, page))
4324 continue;
4325 if (xa_is_value(page))
4326 continue;
4327 if (isolate_lru_page(page))
4328 continue;
4329
4330 list_add(&page->lru, &page_list);
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304331
4332 if (need_resched()) {
4333 xas_pause(&xas);
4334 cond_resched_rcu();
4335 }
4336 }
4337 rcu_read_unlock();
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304338
Charan Teja Reddy100274742022-04-07 15:17:24 +05304339 return reclaim_pages(&page_list);
Suren Baghdasaryan404df472022-03-24 11:00:05 -07004340#else
4341 return 0;
4342#endif
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304343}
4344EXPORT_SYMBOL_GPL(reclaim_shmem_address_space);