blob: efcffd4836c3f4485528021a9164e7bcdd558100 [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;
536 int removed = 0, split = 0;
537
538 if (list_empty(&sbinfo->shrinklist))
539 return SHRINK_STOP;
540
541 spin_lock(&sbinfo->shrinklist_lock);
542 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
543 info = list_entry(pos, struct shmem_inode_info, shrinklist);
544
545 /* pin the inode */
546 inode = igrab(&info->vfs_inode);
547
548 /* inode is about to be evicted */
549 if (!inode) {
550 list_del_init(&info->shrinklist);
551 removed++;
552 goto next;
553 }
554
555 /* Check if there's anything to gain */
556 if (round_up(inode->i_size, PAGE_SIZE) ==
557 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800558 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700559 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700560 goto next;
561 }
562
563 list_move(&info->shrinklist, &list);
564next:
565 if (!--batch)
566 break;
567 }
568 spin_unlock(&sbinfo->shrinklist_lock);
569
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800570 list_for_each_safe(pos, next, &to_remove) {
571 info = list_entry(pos, struct shmem_inode_info, shrinklist);
572 inode = &info->vfs_inode;
573 list_del_init(&info->shrinklist);
574 iput(inode);
575 }
576
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700577 list_for_each_safe(pos, next, &list) {
578 int ret;
579
580 info = list_entry(pos, struct shmem_inode_info, shrinklist);
581 inode = &info->vfs_inode;
582
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700583 if (nr_to_split && split >= nr_to_split)
584 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700585
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700586 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700587 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
588 if (!page)
589 goto drop;
590
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700591 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700592 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700593 put_page(page);
594 goto drop;
595 }
596
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700597 /*
598 * Leave the inode on the list if we failed to lock
599 * the page at this time.
600 *
601 * Waiting for the lock may lead to deadlock in the
602 * reclaim path.
603 */
604 if (!trylock_page(page)) {
605 put_page(page);
606 goto leave;
607 }
608
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700609 ret = split_huge_page(page);
610 unlock_page(page);
611 put_page(page);
612
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700613 /* If split failed leave the inode on the list */
614 if (ret)
615 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700616
617 split++;
618drop:
619 list_del_init(&info->shrinklist);
620 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700621leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700622 iput(inode);
623 }
624
625 spin_lock(&sbinfo->shrinklist_lock);
626 list_splice_tail(&list, &sbinfo->shrinklist);
627 sbinfo->shrinklist_len -= removed;
628 spin_unlock(&sbinfo->shrinklist_lock);
629
630 return split;
631}
632
633static long shmem_unused_huge_scan(struct super_block *sb,
634 struct shrink_control *sc)
635{
636 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
637
638 if (!READ_ONCE(sbinfo->shrinklist_len))
639 return SHRINK_STOP;
640
641 return shmem_unused_huge_shrink(sbinfo, sc, 0);
642}
643
644static long shmem_unused_huge_count(struct super_block *sb,
645 struct shrink_control *sc)
646{
647 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
648 return READ_ONCE(sbinfo->shrinklist_len);
649}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700650#else /* !CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700651
652#define shmem_huge SHMEM_HUGE_DENY
653
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700654static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
655 struct shrink_control *sc, unsigned long nr_to_split)
656{
657 return 0;
658}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700659#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700660
Yang Shi89fdcd22018-06-07 17:06:59 -0700661static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
662{
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -0700663 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Yang Shi89fdcd22018-06-07 17:06:59 -0700664 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
665 shmem_huge != SHMEM_HUGE_DENY)
666 return true;
667 return false;
668}
669
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700670/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700671 * Like add_to_page_cache_locked, but error if expected item has gone.
672 */
673static int shmem_add_to_page_cache(struct page *page,
674 struct address_space *mapping,
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700675 pgoff_t index, void *expected, gfp_t gfp,
676 struct mm_struct *charge_mm)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700677{
Matthew Wilcox552446a42017-12-01 13:25:14 -0500678 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
679 unsigned long i = 0;
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -0700680 unsigned long nr = compound_nr(page);
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700681 int error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700682
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700683 VM_BUG_ON_PAGE(PageTail(page), page);
684 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800685 VM_BUG_ON_PAGE(!PageLocked(page), page);
686 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700687 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700688
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700689 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700690 page->mapping = mapping;
691 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700692
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700693 if (!PageSwapCache(page)) {
Johannes Weinerd9eb1ea2020-06-03 16:02:24 -0700694 error = mem_cgroup_charge(page, charge_mm, gfp);
Johannes Weiner4c6355b2020-06-03 16:02:17 -0700695 if (error) {
696 if (PageTransHuge(page)) {
697 count_vm_event(THP_FILE_FALLBACK);
698 count_vm_event(THP_FILE_FALLBACK_CHARGE);
699 }
700 goto error;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700701 }
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700702 }
703 cgroup_throttle_swaprate(page, gfp);
704
Matthew Wilcox552446a42017-12-01 13:25:14 -0500705 do {
706 void *entry;
707 xas_lock_irq(&xas);
708 entry = xas_find_conflict(&xas);
709 if (entry != expected)
710 xas_set_err(&xas, -EEXIST);
711 xas_create_range(&xas);
712 if (xas_error(&xas))
713 goto unlock;
714next:
Matthew Wilcox (Oracle)41011962019-09-23 15:34:52 -0700715 xas_store(&xas, page);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500716 if (++i < nr) {
717 xas_next(&xas);
718 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700719 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500720 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700721 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700722 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500723 }
724 mapping->nrpages += nr;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700725 __mod_lruvec_page_state(page, NR_FILE_PAGES, nr);
726 __mod_lruvec_page_state(page, NR_SHMEM, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500727unlock:
728 xas_unlock_irq(&xas);
729 } while (xas_nomem(&xas, gfp));
730
731 if (xas_error(&xas)) {
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700732 error = xas_error(&xas);
733 goto error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700734 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500735
736 return 0;
Johannes Weiner3fea5a42020-06-03 16:01:41 -0700737error:
738 page->mapping = NULL;
739 page_ref_sub(page, nr);
740 return error;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700741}
742
743/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700744 * Like delete_from_page_cache, but substitutes swap for page.
745 */
746static void shmem_delete_from_page_cache(struct page *page, void *radswap)
747{
748 struct address_space *mapping = page->mapping;
749 int error;
750
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700751 VM_BUG_ON_PAGE(PageCompound(page), page);
752
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700753 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500754 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700755 page->mapping = NULL;
756 mapping->nrpages--;
Johannes Weiner0d1c2072020-06-03 16:01:54 -0700757 __dec_lruvec_page_state(page, NR_FILE_PAGES);
758 __dec_lruvec_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700759 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300760 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700761 BUG_ON(error);
762}
763
764/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500765 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700766 */
767static int shmem_free_swap(struct address_space *mapping,
768 pgoff_t index, void *radswap)
769{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700770 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700771
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500772 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700773 if (old != radswap)
774 return -ENOENT;
775 free_swap_and_cache(radix_to_swp_entry(radswap));
776 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700777}
778
779/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800780 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800781 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800782 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700783 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800784 * as long as the inode doesn't go away and racy results are not a problem.
785 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800786unsigned long shmem_partial_swap_usage(struct address_space *mapping,
787 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800788{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500789 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800790 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800791 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800792
793 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500794 xas_for_each(&xas, page, end - 1) {
795 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700796 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400797 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800798 swapped++;
799
800 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500801 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800802 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800803 }
804 }
805
806 rcu_read_unlock();
807
808 return swapped << PAGE_SHIFT;
809}
810
811/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800812 * Determine (in bytes) how many of the shmem object's pages mapped by the
813 * given vma is swapped out.
814 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700815 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800816 * as long as the inode doesn't go away and racy results are not a problem.
817 */
818unsigned long shmem_swap_usage(struct vm_area_struct *vma)
819{
820 struct inode *inode = file_inode(vma->vm_file);
821 struct shmem_inode_info *info = SHMEM_I(inode);
822 struct address_space *mapping = inode->i_mapping;
823 unsigned long swapped;
824
825 /* Be careful as we don't hold info->lock */
826 swapped = READ_ONCE(info->swapped);
827
828 /*
829 * The easier cases are when the shmem object has nothing in swap, or
830 * the vma maps it whole. Then we can simply use the stats that we
831 * already track.
832 */
833 if (!swapped)
834 return 0;
835
836 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
837 return swapped << PAGE_SHIFT;
838
839 /* Here comes the more involved part */
840 return shmem_partial_swap_usage(mapping,
841 linear_page_index(vma, vma->vm_start),
842 linear_page_index(vma, vma->vm_end));
843}
844
845/*
Hugh Dickins24513262012-01-20 14:34:21 -0800846 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
847 */
848void shmem_unlock_mapping(struct address_space *mapping)
849{
850 struct pagevec pvec;
851 pgoff_t indices[PAGEVEC_SIZE];
852 pgoff_t index = 0;
853
Mel Gorman86679822017-11-15 17:37:52 -0800854 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800855 /*
856 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
857 */
858 while (!mapping_unevictable(mapping)) {
859 /*
860 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
861 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
862 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700863 pvec.nr = find_get_entries(mapping, index,
864 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800865 if (!pvec.nr)
866 break;
867 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700868 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000869 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800870 pagevec_release(&pvec);
871 cond_resched();
872 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700873}
874
875/*
Hugh Dickins71725ed2020-04-06 20:07:57 -0700876 * Check whether a hole-punch or truncation needs to split a huge page,
877 * returning true if no split was required, or the split has been successful.
878 *
879 * Eviction (or truncation to 0 size) should never need to split a huge page;
880 * but in rare cases might do so, if shmem_undo_range() failed to trylock on
881 * head, and then succeeded to trylock on tail.
882 *
883 * A split can only succeed when there are no additional references on the
884 * huge page: so the split below relies upon find_get_entries() having stopped
885 * when it found a subpage of the huge page, without getting further references.
886 */
887static bool shmem_punch_compound(struct page *page, pgoff_t start, pgoff_t end)
888{
889 if (!PageTransCompound(page))
890 return true;
891
892 /* Just proceed to delete a huge page wholly within the range punched */
893 if (PageHead(page) &&
894 page->index >= start && page->index + HPAGE_PMD_NR <= end)
895 return true;
896
897 /* Try to split huge page, so we can truly punch the hole or truncate */
898 return split_huge_page(page) >= 0;
899}
900
901/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500902 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700903 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700904 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700905static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
906 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700908 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300910 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
911 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
912 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
913 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700914 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700915 pgoff_t indices[PAGEVEC_SIZE];
916 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700917 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700918 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700920 if (lend == -1)
921 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700922
Mel Gorman86679822017-11-15 17:37:52 -0800923 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700924 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700925 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700926 pvec.nr = find_get_entries(mapping, index,
927 min(end - index, (pgoff_t)PAGEVEC_SIZE),
928 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700929 if (!pvec.nr)
930 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700931 for (i = 0; i < pagevec_count(&pvec); i++) {
932 struct page *page = pvec.pages[i];
933
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700934 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700935 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700936 break;
937
Matthew Wilcox3159f942017-11-03 13:30:42 -0400938 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700939 if (unfalloc)
940 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700941 nr_swaps_freed += !shmem_free_swap(mapping,
942 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700943 continue;
944 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700945
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700946 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
947
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700948 if (!trylock_page(page))
949 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700950
Hugh Dickins71725ed2020-04-06 20:07:57 -0700951 if ((!unfalloc || !PageUptodate(page)) &&
952 page_mapping(page) == mapping) {
953 VM_BUG_ON_PAGE(PageWriteback(page), page);
954 if (shmem_punch_compound(page, start, end))
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700955 truncate_inode_page(mapping, page);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700956 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700957 unlock_page(page);
958 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700959 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800960 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700961 cond_resched();
962 index++;
963 }
964
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700965 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700966 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700967 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700968 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300969 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700970 if (start > end) {
971 top = partial_end;
972 partial_end = 0;
973 }
974 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700975 set_page_dirty(page);
976 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300977 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700978 }
979 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700980 if (partial_end) {
981 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700982 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700983 if (page) {
984 zero_user_segment(page, 0, partial_end);
985 set_page_dirty(page);
986 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300987 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700988 }
989 }
990 if (start >= end)
991 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700992
993 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700994 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700995 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700996
997 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700998 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700999 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001000 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001001 /* If all gone or hole-punch or unfalloc, we're done */
1002 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001003 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001004 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001005 index = start;
1006 continue;
1007 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001008 for (i = 0; i < pagevec_count(&pvec); i++) {
1009 struct page *page = pvec.pages[i];
1010
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001011 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -07001012 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001013 break;
1014
Matthew Wilcox3159f942017-11-03 13:30:42 -04001015 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001016 if (unfalloc)
1017 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001018 if (shmem_free_swap(mapping, index, page)) {
1019 /* Swap was replaced by page: retry */
1020 index--;
1021 break;
1022 }
1023 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001024 continue;
1025 }
1026
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001027 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001028
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001029 if (!unfalloc || !PageUptodate(page)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001030 if (page_mapping(page) != mapping) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -07001031 /* Page was replaced by swap: retry */
1032 unlock_page(page);
1033 index--;
1034 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001035 }
Hugh Dickins71725ed2020-04-06 20:07:57 -07001036 VM_BUG_ON_PAGE(PageWriteback(page), page);
1037 if (shmem_punch_compound(page, start, end))
1038 truncate_inode_page(mapping, page);
Hugh Dickins0783ac95b2020-04-20 18:14:07 -07001039 else if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Hugh Dickins71725ed2020-04-06 20:07:57 -07001040 /* Wipe the page and don't get stuck */
1041 clear_highpage(page);
1042 flush_dcache_page(page);
1043 set_page_dirty(page);
1044 if (index <
1045 round_up(start, HPAGE_PMD_NR))
1046 start = index + 1;
1047 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001048 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001049 unlock_page(page);
1050 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07001051 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -08001052 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -07001053 index++;
1054 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001055
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001056 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -07001057 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001059 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001060}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001062void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
1063{
1064 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001065 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
Hugh Dickins94c1e622011-06-27 16:18:03 -07001067EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
David Howellsa528d352017-01-31 16:46:22 +00001069static int shmem_getattr(const struct path *path, struct kstat *stat,
1070 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -07001071{
David Howellsa528d352017-01-31 16:46:22 +00001072 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001073 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001074 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001075
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001076 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001077 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001078 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001079 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001080 }
Yu Zhao44a30222015-09-08 15:03:33 -07001081 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001082
1083 if (is_huge_enabled(sb_info))
1084 stat->blksize = HPAGE_PMD_SIZE;
1085
Yu Zhao44a30222015-09-08 15:03:33 -07001086 return 0;
1087}
1088
Hugh Dickins94c1e622011-06-27 16:18:03 -07001089static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
David Howells75c3cfa2015-03-17 22:26:12 +00001091 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001092 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001093 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 int error;
1095
Jan Kara31051c82016-05-26 16:55:18 +02001096 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001097 if (error)
1098 return error;
1099
Hugh Dickins94c1e622011-06-27 16:18:03 -07001100 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1101 loff_t oldsize = inode->i_size;
1102 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001103
David Herrmann40e041a2014-08-08 14:25:27 -07001104 /* protected by i_mutex */
1105 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1106 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1107 return -EPERM;
1108
Hugh Dickins94c1e622011-06-27 16:18:03 -07001109 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001110 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1111 oldsize, newsize);
1112 if (error)
1113 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001114 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001115 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001116 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001117 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001118 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001119 if (oldsize > holebegin)
1120 unmap_mapping_range(inode->i_mapping,
1121 holebegin, 0, 1);
1122 if (info->alloced)
1123 shmem_truncate_range(inode,
1124 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001125 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001126 if (oldsize > holebegin)
1127 unmap_mapping_range(inode->i_mapping,
1128 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001129
1130 /*
1131 * Part of the huge page can be beyond i_size: subject
1132 * to shrink under memory pressure.
1133 */
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001134 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001135 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001136 /*
1137 * _careful to defend against unlocked access to
1138 * ->shrink_list in shmem_unused_huge_shrink()
1139 */
1140 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001141 list_add_tail(&info->shrinklist,
1142 &sbinfo->shrinklist);
1143 sbinfo->shrinklist_len++;
1144 }
1145 spin_unlock(&sbinfo->shrinklist_lock);
1146 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 }
1149
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001150 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001151 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001152 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return error;
1154}
1155
Al Viro1f895f72010-06-05 19:10:41 -04001156static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001159 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001161 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 shmem_unacct_size(info->flags, inode->i_size);
1163 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001164 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001165 if (!list_empty(&info->shrinklist)) {
1166 spin_lock(&sbinfo->shrinklist_lock);
1167 if (!list_empty(&info->shrinklist)) {
1168 list_del_init(&info->shrinklist);
1169 sbinfo->shrinklist_len--;
1170 }
1171 spin_unlock(&sbinfo->shrinklist_lock);
1172 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001173 while (!list_empty(&info->swaplist)) {
1174 /* Wait while shmem_unuse() is scanning this inode... */
1175 wait_var_event(&info->stop_eviction,
1176 !atomic_read(&info->stop_eviction));
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001177 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001178 /* ...but beware of the race if we peeked too early */
1179 if (!atomic_read(&info->stop_eviction))
1180 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001181 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
Al Viro3ed47db2016-01-22 18:08:52 -05001183 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001184
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001185 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001186 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001187 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001188 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001191extern struct swap_info_struct *swap_info[];
1192
1193static int shmem_find_swap_entries(struct address_space *mapping,
1194 pgoff_t start, unsigned int nr_entries,
1195 struct page **entries, pgoff_t *indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001196 unsigned int type, bool frontswap)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001197{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001198 XA_STATE(xas, &mapping->i_pages, start);
1199 struct page *page;
Hugh Dickins87039542019-04-18 17:49:58 -07001200 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001201 unsigned int ret = 0;
1202
1203 if (!nr_entries)
1204 return 0;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001205
1206 rcu_read_lock();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001207 xas_for_each(&xas, page, ULONG_MAX) {
1208 if (xas_retry(&xas, page))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001209 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001210
1211 if (!xa_is_value(page))
1212 continue;
1213
Hugh Dickins87039542019-04-18 17:49:58 -07001214 entry = radix_to_swp_entry(page);
1215 if (swp_type(entry) != type)
1216 continue;
1217 if (frontswap &&
1218 !frontswap_test(swap_info[type], swp_offset(entry)))
1219 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001220
1221 indices[ret] = xas.xa_index;
1222 entries[ret] = page;
1223
1224 if (need_resched()) {
1225 xas_pause(&xas);
1226 cond_resched_rcu();
1227 }
1228 if (++ret == nr_entries)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001229 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001230 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001231 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001232
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001233 return ret;
1234}
1235
1236/*
1237 * Move the swapped pages for an inode to page cache. Returns the count
1238 * of pages swapped in, or the error in case of failure.
1239 */
1240static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
1241 pgoff_t *indices)
1242{
1243 int i = 0;
1244 int ret = 0;
1245 int error = 0;
1246 struct address_space *mapping = inode->i_mapping;
1247
1248 for (i = 0; i < pvec.nr; i++) {
1249 struct page *page = pvec.pages[i];
1250
1251 if (!xa_is_value(page))
1252 continue;
1253 error = shmem_swapin_page(inode, indices[i],
1254 &page, SGP_CACHE,
1255 mapping_gfp_mask(mapping),
1256 NULL, NULL);
1257 if (error == 0) {
1258 unlock_page(page);
1259 put_page(page);
1260 ret++;
1261 }
1262 if (error == -ENOMEM)
1263 break;
1264 error = 0;
1265 }
1266 return error ? error : ret;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001267}
1268
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001269/*
1270 * If swap found in inode, free it and move page from swapcache to filecache.
1271 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001272static int shmem_unuse_inode(struct inode *inode, unsigned int type,
1273 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001275 struct address_space *mapping = inode->i_mapping;
1276 pgoff_t start = 0;
1277 struct pagevec pvec;
1278 pgoff_t indices[PAGEVEC_SIZE];
1279 bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
1280 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001282 pagevec_init(&pvec);
1283 do {
1284 unsigned int nr_entries = PAGEVEC_SIZE;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001285
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001286 if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
1287 nr_entries = *fs_pages_to_unuse;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001288
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001289 pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
1290 pvec.pages, indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001291 type, frontswap);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001292 if (pvec.nr == 0) {
1293 ret = 0;
1294 break;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001295 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001296
1297 ret = shmem_unuse_swap_entries(inode, pvec, indices);
1298 if (ret < 0)
1299 break;
1300
1301 if (frontswap_partial) {
1302 *fs_pages_to_unuse -= ret;
1303 if (*fs_pages_to_unuse == 0) {
1304 ret = FRONTSWAP_PAGES_UNUSED;
1305 break;
1306 }
1307 }
1308
1309 start = indices[pvec.nr - 1];
1310 } while (true);
1311
1312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001316 * Read all the shared memory data that resides in the swap
1317 * device 'type' back into memory, so the swap device can be
1318 * unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001320int shmem_unuse(unsigned int type, bool frontswap,
1321 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001323 struct shmem_inode_info *info, *next;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001324 int error = 0;
1325
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001326 if (list_empty(&shmem_swaplist))
1327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001329 mutex_lock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001330 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) {
1331 if (!info->swapped) {
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001332 list_del_init(&info->swaplist);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001333 continue;
1334 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001335 /*
1336 * Drop the swaplist mutex while searching the inode for swap;
1337 * but before doing so, make sure shmem_evict_inode() will not
1338 * remove placeholder inode from swaplist, nor let it be freed
1339 * (igrab() would protect from unlink, but not from unmount).
1340 */
1341 atomic_inc(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001342 mutex_unlock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001343
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001344 error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001345 fs_pages_to_unuse);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001346 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001347
1348 mutex_lock(&shmem_swaplist_mutex);
1349 next = list_next_entry(info, swaplist);
1350 if (!info->swapped)
1351 list_del_init(&info->swaplist);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001352 if (atomic_dec_and_test(&info->stop_eviction))
1353 wake_up_var(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001354 if (error)
Hugh Dickins778dd892011-05-11 15:13:37 -07001355 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001357 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001358
Hugh Dickins778dd892011-05-11 15:13:37 -07001359 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
1362/*
1363 * Move the page from the page cache to the swap cache.
1364 */
1365static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1366{
1367 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001370 swp_entry_t swap;
1371 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001373 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 mapping = page->mapping;
1376 index = page->index;
1377 inode = mapping->host;
1378 info = SHMEM_I(inode);
1379 if (info->flags & VM_LOCKED)
1380 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001381 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 goto redirty;
1383
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001384 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001385 * Our capabilities prevent regular writeback or sync from ever calling
1386 * shmem_writepage; but a stacking filesystem might use ->writepage of
1387 * its underlying filesystem, in which case tmpfs should write out to
1388 * swap only in response to memory pressure, and not for the writeback
1389 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001390 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001391 if (!wbc->for_reclaim) {
1392 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1393 goto redirty;
1394 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001395
1396 /*
1397 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1398 * value into swapfile.c, the only way we can correctly account for a
1399 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001400 *
1401 * That's okay for a page already fallocated earlier, but if we have
1402 * not yet completed the fallocation, then (a) we want to keep track
1403 * of this page in case we have to undo it, and (b) it may not be a
1404 * good idea to continue anyway, once we're pushing into swap. So
1405 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001406 */
1407 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001408 if (inode->i_private) {
1409 struct shmem_falloc *shmem_falloc;
1410 spin_lock(&inode->i_lock);
1411 shmem_falloc = inode->i_private;
1412 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001413 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001414 index >= shmem_falloc->start &&
1415 index < shmem_falloc->next)
1416 shmem_falloc->nr_unswapped++;
1417 else
1418 shmem_falloc = NULL;
1419 spin_unlock(&inode->i_lock);
1420 if (shmem_falloc)
1421 goto redirty;
1422 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001423 clear_highpage(page);
1424 flush_dcache_page(page);
1425 SetPageUptodate(page);
1426 }
1427
Huang Ying38d8b4e2017-07-06 15:37:18 -07001428 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001429 if (!swap.val)
1430 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001431
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001432 /*
1433 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001434 * if it's not already there. Do it now before the page is
1435 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001436 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001437 * we've incremented swapped, because shmem_unuse_inode() will
1438 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001439 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001440 mutex_lock(&shmem_swaplist_mutex);
1441 if (list_empty(&info->swaplist))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001442 list_add(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001443
Yang Shi4afab1c2019-11-30 17:58:07 -08001444 if (add_to_swap_cache(page, swap,
Joonsoo Kim3852f672020-08-11 18:30:47 -07001445 __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN,
1446 NULL) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001447 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001448 shmem_recalc_inode(inode);
1449 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001450 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001451
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001452 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001453 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1454
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001455 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001456 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001457 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return 0;
1459 }
1460
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001461 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001462 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463redirty:
1464 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001465 if (wbc->for_reclaim)
1466 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1467 unlock_page(page);
1468 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Hugh Dickins75edd342016-05-19 17:12:44 -07001471#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001472static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001473{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001474 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001475
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001476 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001477 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001478
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001479 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001480
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001481 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001482}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001483
1484static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1485{
1486 struct mempolicy *mpol = NULL;
1487 if (sbinfo->mpol) {
1488 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1489 mpol = sbinfo->mpol;
1490 mpol_get(mpol);
1491 spin_unlock(&sbinfo->stat_lock);
1492 }
1493 return mpol;
1494}
Hugh Dickins75edd342016-05-19 17:12:44 -07001495#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1496static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1497{
1498}
1499static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1500{
1501 return NULL;
1502}
1503#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1504#ifndef CONFIG_NUMA
1505#define vm_policy vm_private_data
1506#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001507
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001508static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1509 struct shmem_inode_info *info, pgoff_t index)
1510{
1511 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001512 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001513 /* Bias interleave by inode number to distribute better across nodes */
1514 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001515 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1516}
1517
1518static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1519{
1520 /* Drop reference taken by mpol_shared_policy_lookup() */
1521 mpol_cond_put(vma->vm_policy);
1522}
1523
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001524static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1525 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001528 struct page *page;
Will Deaconcab48b22021-01-14 15:42:14 +00001529 struct vm_fault vmf = {
1530 .vma = &pvma,
1531 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001533 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001534 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001535 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001536
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001537 return page;
1538}
Mel Gorman18a2f372012-12-05 14:01:41 -08001539
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001540static struct page *shmem_alloc_hugepage(gfp_t gfp,
1541 struct shmem_inode_info *info, pgoff_t index)
1542{
1543 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001544 struct address_space *mapping = info->vfs_inode.i_mapping;
1545 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001546 struct page *page;
1547
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001548 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001549 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1550 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001551 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001552
1553 shmem_pseudo_vma_init(&pvma, info, hindex);
1554 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
David Rientjes19deb762019-09-04 12:54:20 -07001555 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001556 shmem_pseudo_vma_destroy(&pvma);
1557 if (page)
1558 prep_transhuge_page(page);
David Rientjesdcdf11e2020-04-06 20:04:25 -07001559 else
1560 count_vm_event(THP_FILE_FALLBACK);
Mel Gorman18a2f372012-12-05 14:01:41 -08001561 return page;
1562}
1563
1564static struct page *shmem_alloc_page(gfp_t gfp,
1565 struct shmem_inode_info *info, pgoff_t index)
1566{
1567 struct vm_area_struct pvma;
Charan Teja Reddy964220d2021-07-06 19:04:50 +05301568 struct page *page = NULL;
1569
1570 trace_android_vh_shmem_alloc_page(&page);
1571 if (page)
1572 return page;
Mel Gorman18a2f372012-12-05 14:01:41 -08001573
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001574 shmem_pseudo_vma_init(&pvma, info, index);
1575 page = alloc_page_vma(gfp, &pvma, 0);
1576 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001577
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001578 return page;
1579}
1580
1581static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001582 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001583 pgoff_t index, bool huge)
1584{
Mike Rapoport0f079692017-09-06 16:22:59 -07001585 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001586 struct page *page;
1587 int nr;
1588 int err = -ENOSPC;
1589
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07001590 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001591 huge = false;
1592 nr = huge ? HPAGE_PMD_NR : 1;
1593
Mike Rapoport0f079692017-09-06 16:22:59 -07001594 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001595 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001596
1597 if (huge)
1598 page = shmem_alloc_hugepage(gfp, info, index);
1599 else
1600 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001601 if (page) {
1602 __SetPageLocked(page);
1603 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001604 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001605 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001606
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001607 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001608 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001609failed:
1610 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001614 * When a page is moved from swapcache to shmem filecache (either by the
1615 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1616 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1617 * ignorance of the mapping it belongs to. If that mapping has special
1618 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1619 * we may need to copy to a suitable page before moving to filecache.
1620 *
1621 * In a future release, this may well be extended to respect cpuset and
1622 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1623 * but for now it is a simple matter of zone.
1624 */
1625static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1626{
1627 return page_zonenum(page) > gfp_zone(gfp);
1628}
1629
1630static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1631 struct shmem_inode_info *info, pgoff_t index)
1632{
1633 struct page *oldpage, *newpage;
1634 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001635 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001636 pgoff_t swap_index;
1637 int error;
1638
1639 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001640 entry.val = page_private(oldpage);
1641 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001642 swap_mapping = page_mapping(oldpage);
1643
1644 /*
1645 * We have arrived here because our zones are constrained, so don't
1646 * limit chance of success by further cpuset and node constraints.
1647 */
1648 gfp &= ~GFP_CONSTRAINT_MASK;
1649 newpage = shmem_alloc_page(gfp, info, index);
1650 if (!newpage)
1651 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001652
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001653 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001654 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001655 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001656
Hugh Dickins9956edf2016-11-10 10:46:11 -08001657 __SetPageLocked(newpage);
1658 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001659 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001660 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001661 SetPageSwapCache(newpage);
1662
1663 /*
1664 * Our caller will very soon move newpage out of swapcache, but it's
1665 * a nice clean interface for us to replace oldpage by newpage there.
1666 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001667 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001668 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001669 if (!error) {
Johannes Weiner0d1c2072020-06-03 16:01:54 -07001670 mem_cgroup_migrate(oldpage, newpage);
1671 __inc_lruvec_page_state(newpage, NR_FILE_PAGES);
1672 __dec_lruvec_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001673 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001674 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001675
Hugh Dickins0142ef62012-06-07 14:21:09 -07001676 if (unlikely(error)) {
1677 /*
1678 * Is this possible? I think not, now that our callers check
1679 * both PageSwapCache and page_private after getting page lock;
1680 * but be defensive. Reverse old to newpage for clear and free.
1681 */
1682 oldpage = newpage;
1683 } else {
Johannes Weiner6058eae2020-06-03 16:02:40 -07001684 lru_cache_add(newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001685 *pagep = newpage;
1686 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001687
1688 ClearPageSwapCache(oldpage);
1689 set_page_private(oldpage, 0);
1690
1691 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001692 put_page(oldpage);
1693 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001694 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001695}
1696
1697/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001698 * Swap in the page pointed to by *pagep.
1699 * Caller has to make sure that *pagep contains a valid swapped page.
1700 * Returns 0 and the page in pagep if success. On failure, returns the
Randy Dunlapaf44c122020-08-11 18:33:17 -07001701 * error code and NULL in *pagep.
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001702 */
1703static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1704 struct page **pagep, enum sgp_type sgp,
1705 gfp_t gfp, struct vm_area_struct *vma,
1706 vm_fault_t *fault_type)
1707{
1708 struct address_space *mapping = inode->i_mapping;
1709 struct shmem_inode_info *info = SHMEM_I(inode);
1710 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001711 struct page *page;
1712 swp_entry_t swap;
1713 int error;
1714
1715 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1716 swap = radix_to_swp_entry(*pagep);
1717 *pagep = NULL;
1718
1719 /* Look it up and read it in.. */
1720 page = lookup_swap_cache(swap, NULL, 0);
1721 if (!page) {
1722 /* Or update major stats only when swapin succeeds?? */
1723 if (fault_type) {
1724 *fault_type |= VM_FAULT_MAJOR;
1725 count_vm_event(PGMAJFAULT);
1726 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1727 }
1728 /* Here we actually start the io */
1729 page = shmem_swapin(swap, gfp, info, index);
1730 if (!page) {
1731 error = -ENOMEM;
1732 goto failed;
1733 }
1734 }
1735
1736 /* We have to do this with page locked to prevent races */
1737 lock_page(page);
1738 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1739 !shmem_confirm_swap(mapping, index, swap)) {
1740 error = -EEXIST;
1741 goto unlock;
1742 }
1743 if (!PageUptodate(page)) {
1744 error = -EIO;
1745 goto failed;
1746 }
1747 wait_on_page_writeback(page);
1748
Steven Price8a848022020-05-13 16:37:49 +01001749 /*
1750 * Some architectures may have to restore extra metadata to the
1751 * physical page after reading from swap.
1752 */
1753 arch_swap_restore(swap, page);
1754
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001755 if (shmem_should_replace_page(page, gfp)) {
1756 error = shmem_replace_page(&page, gfp, info, index);
1757 if (error)
1758 goto failed;
1759 }
1760
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001761 error = shmem_add_to_page_cache(page, mapping, index,
1762 swp_to_radix_entry(swap), gfp,
1763 charge_mm);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001764 if (error)
1765 goto failed;
1766
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001767 spin_lock_irq(&info->lock);
1768 info->swapped--;
1769 shmem_recalc_inode(inode);
1770 spin_unlock_irq(&info->lock);
1771
1772 if (sgp == SGP_WRITE)
1773 mark_page_accessed(page);
1774
1775 delete_from_swap_cache(page);
1776 set_page_dirty(page);
1777 swap_free(swap);
1778
1779 *pagep = page;
1780 return 0;
1781failed:
1782 if (!shmem_confirm_swap(mapping, index, swap))
1783 error = -EEXIST;
1784unlock:
1785 if (page) {
1786 unlock_page(page);
1787 put_page(page);
1788 }
1789
1790 return error;
1791}
1792
1793/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001794 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 *
1796 * If we allocate a new one we do not mark it dirty. That's up to the
1797 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001798 * entry since a page cannot live in both the swap and page cache.
1799 *
Axel Rasmussen0c959cd2021-05-11 15:05:58 +10001800 * vma, vmf, and fault_type are only supplied by shmem_fault:
Lokesh Gidra84330a52021-05-13 05:11:10 -07001801 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001803static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001804 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001805 struct vm_area_struct *vma, struct vm_fault *vmf,
1806 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
1808 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001809 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001811 struct mm_struct *charge_mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001812 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001813 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001814 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001816 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001817 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001819 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001821 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1822 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001824 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001825 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001826 return -EINVAL;
1827 }
1828
1829 sbinfo = SHMEM_SB(inode->i_sb);
1830 charge_mm = vma ? vma->vm_mm : current->mm;
1831
1832 page = find_lock_entry(mapping, index);
Axel Rasmussen0c959cd2021-05-11 15:05:58 +10001833
1834 if (page && vma && userfaultfd_minor(vma)) {
1835 if (!xa_is_value(page)) {
1836 unlock_page(page);
1837 put_page(page);
1838 }
1839 *fault_type = handle_userfault(vmf, VM_UFFD_MINOR);
1840 return 0;
1841 }
1842
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001843 if (xa_is_value(page)) {
1844 error = shmem_swapin_page(inode, index, &page,
1845 sgp, gfp, vma, fault_type);
1846 if (error == -EEXIST)
1847 goto repeat;
1848
1849 *pagep = page;
1850 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001851 }
1852
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001853 if (page)
1854 hindex = page->index;
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001855 if (page && sgp == SGP_WRITE)
1856 mark_page_accessed(page);
1857
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001858 /* fallocated page? */
1859 if (page && !PageUptodate(page)) {
1860 if (sgp != SGP_READ)
1861 goto clear;
1862 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001863 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001864 page = NULL;
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001865 hindex = index;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001866 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001867 if (page || sgp == SGP_READ)
1868 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
1870 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001871 * Fast cache lookup did not find it:
1872 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001874
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001875 if (vma && userfaultfd_missing(vma)) {
1876 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1877 return 0;
1878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001880 /* shmem_symlink() */
1881 if (mapping->a_ops != &shmem_aops)
1882 goto alloc_nohuge;
1883 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1884 goto alloc_nohuge;
1885 if (shmem_huge == SHMEM_HUGE_FORCE)
1886 goto alloc_huge;
1887 switch (sbinfo->huge) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001888 case SHMEM_HUGE_NEVER:
1889 goto alloc_nohuge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001890 case SHMEM_HUGE_WITHIN_SIZE: {
1891 loff_t i_size;
1892 pgoff_t off;
1893
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001894 off = round_up(index, HPAGE_PMD_NR);
1895 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1896 if (i_size >= HPAGE_PMD_SIZE &&
1897 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001898 goto alloc_huge;
Kees Cook27d80fa22020-04-06 20:07:51 -07001899
1900 fallthrough;
1901 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001902 case SHMEM_HUGE_ADVISE:
1903 if (sgp_huge == SGP_HUGE)
1904 goto alloc_huge;
1905 /* TODO: implement fadvise() hints */
1906 goto alloc_nohuge;
1907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001909alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001910 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1911 if (IS_ERR(page)) {
1912alloc_nohuge:
1913 page = shmem_alloc_and_acct_page(gfp, inode,
1914 index, false);
1915 }
1916 if (IS_ERR(page)) {
1917 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001918
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001919 error = PTR_ERR(page);
1920 page = NULL;
1921 if (error != -ENOSPC)
1922 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001923 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001924 * Try to reclaim some space by splitting a huge page
1925 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001926 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001927 while (retry--) {
1928 int ret;
1929
1930 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1931 if (ret == SHRINK_STOP)
1932 break;
1933 if (ret)
1934 goto alloc_nohuge;
1935 }
1936 goto unlock;
1937 }
1938
1939 if (PageTransHuge(page))
1940 hindex = round_down(index, HPAGE_PMD_NR);
1941 else
1942 hindex = index;
1943
1944 if (sgp == SGP_WRITE)
1945 __SetPageReferenced(page);
1946
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001947 error = shmem_add_to_page_cache(page, mapping, hindex,
Johannes Weiner3fea5a42020-06-03 16:01:41 -07001948 NULL, gfp & GFP_RECLAIM_MASK,
1949 charge_mm);
1950 if (error)
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001951 goto unacct;
Johannes Weiner6058eae2020-06-03 16:02:40 -07001952 lru_cache_add(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001953
1954 spin_lock_irq(&info->lock);
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07001955 info->alloced += compound_nr(page);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001956 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1957 shmem_recalc_inode(inode);
1958 spin_unlock_irq(&info->lock);
1959 alloced = true;
1960
1961 if (PageTransHuge(page) &&
1962 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1963 hindex + HPAGE_PMD_NR - 1) {
1964 /*
1965 * Part of the huge page is beyond i_size: subject
1966 * to shrink under memory pressure.
1967 */
1968 spin_lock(&sbinfo->shrinklist_lock);
1969 /*
1970 * _careful to defend against unlocked access to
1971 * ->shrink_list in shmem_unused_huge_shrink()
1972 */
1973 if (list_empty_careful(&info->shrinklist)) {
1974 list_add_tail(&info->shrinklist,
1975 &sbinfo->shrinklist);
1976 sbinfo->shrinklist_len++;
1977 }
1978 spin_unlock(&sbinfo->shrinklist_lock);
1979 }
1980
1981 /*
1982 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1983 */
1984 if (sgp == SGP_FALLOC)
1985 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001986clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001987 /*
1988 * Let SGP_WRITE caller clear ends if write does not fill page;
1989 * but SGP_FALLOC on a page fallocated earlier must initialize
1990 * it now, lest undo on failure cancel our earlier guarantee.
1991 */
1992 if (sgp != SGP_WRITE && !PageUptodate(page)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001993 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001994
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001995 for (i = 0; i < compound_nr(page); i++) {
1996 clear_highpage(page + i);
1997 flush_dcache_page(page + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001998 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07001999 SetPageUptodate(page);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07002000 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07002001
Hugh Dickins54af60422011-08-03 16:21:24 -07002002 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07002003 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002004 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08002005 if (alloced) {
2006 ClearPageDirty(page);
2007 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002008 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08002009 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002010 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08002011 }
Hugh Dickins54af60422011-08-03 16:21:24 -07002012 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08002013 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07002014 }
Matthew Wilcox (Oracle)63ec1972020-10-13 16:51:38 -07002015out:
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002016 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07002017 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07002018
Nick Piggind0217ac2007-07-19 01:47:03 -07002019 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07002020 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
Hugh Dickins54af60422011-08-03 16:21:24 -07002022unacct:
Matthew Wilcox (Oracle)d8c65462019-09-23 15:34:30 -07002023 shmem_inode_unacct_blocks(inode, compound_nr(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002024
2025 if (PageTransHuge(page)) {
2026 unlock_page(page);
2027 put_page(page);
2028 goto alloc_nohuge;
2029 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07002030unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07002031 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07002032 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002033 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07002034 }
2035 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002036 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07002037 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002038 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07002039 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08002040 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002041 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07002042 goto repeat;
2043 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002046/*
2047 * This is like autoremove_wake_function, but it removes the wait queue
2048 * entry unconditionally - even if something else had already woken the
2049 * target.
2050 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02002051static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002052{
2053 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02002054 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002055 return ret;
2056}
2057
Souptick Joarder20acce62018-06-07 17:09:17 -07002058static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Dave Jiang11bac802017-02-24 14:56:41 -08002060 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05002061 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002062 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002063 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07002064 int err;
2065 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002067 /*
2068 * Trinity finds that probing a hole which tmpfs is punching can
2069 * prevent the hole-punch from ever completing: which in turn
2070 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07002071 * faulting pages into the hole while it's being punched. Although
2072 * shmem_undo_range() does remove the additions, it may be unable to
2073 * keep up, as each new page needs its own unmap_mapping_range() call,
2074 * and the i_mmap tree grows ever slower to scan if new vmas are added.
2075 *
2076 * It does not matter if we sometimes reach this check just before the
2077 * hole-punch begins, so that one fault then races with the punch:
2078 * we just need to make racing faults a rare case.
2079 *
2080 * The implementation below would be much simpler if we just used a
2081 * standard mutex or completion: but we cannot take i_mutex in fault,
2082 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002083 */
2084 if (unlikely(inode->i_private)) {
2085 struct shmem_falloc *shmem_falloc;
2086
2087 spin_lock(&inode->i_lock);
2088 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002089 if (shmem_falloc &&
2090 shmem_falloc->waitq &&
2091 vmf->pgoff >= shmem_falloc->start &&
2092 vmf->pgoff < shmem_falloc->next) {
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002093 struct file *fpin;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002094 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002095 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002096
2097 ret = VM_FAULT_NOPAGE;
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002098 fpin = maybe_unlock_mmap_for_io(vmf, NULL);
2099 if (fpin)
Hugh Dickins8e205f72014-07-23 14:00:10 -07002100 ret = VM_FAULT_RETRY;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002101
2102 shmem_falloc_waitq = shmem_falloc->waitq;
2103 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2104 TASK_UNINTERRUPTIBLE);
2105 spin_unlock(&inode->i_lock);
2106 schedule();
2107
2108 /*
2109 * shmem_falloc_waitq points into the shmem_fallocate()
2110 * stack of the hole-punching task: shmem_falloc_waitq
2111 * is usually invalid by the time we reach here, but
2112 * finish_wait() does not dereference it in that case;
2113 * though i_lock needed lest racing with wake_up_all().
2114 */
2115 spin_lock(&inode->i_lock);
2116 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2117 spin_unlock(&inode->i_lock);
Kirill A. Shutemov8897c1b2019-11-30 17:50:26 -08002118
2119 if (fpin)
2120 fput(fpin);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002121 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002122 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002123 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002124 }
2125
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002126 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002127
2128 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2129 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002130 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002131 else if (vma->vm_flags & VM_HUGEPAGE)
2132 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002133
Souptick Joarder20acce62018-06-07 17:09:17 -07002134 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002135 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002136 if (err)
2137 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002138 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002141unsigned long shmem_get_unmapped_area(struct file *file,
2142 unsigned long uaddr, unsigned long len,
2143 unsigned long pgoff, unsigned long flags)
2144{
2145 unsigned long (*get_area)(struct file *,
2146 unsigned long, unsigned long, unsigned long, unsigned long);
2147 unsigned long addr;
2148 unsigned long offset;
2149 unsigned long inflated_len;
2150 unsigned long inflated_addr;
2151 unsigned long inflated_offset;
2152
2153 if (len > TASK_SIZE)
2154 return -ENOMEM;
2155
2156 get_area = current->mm->get_unmapped_area;
2157 addr = get_area(file, uaddr, len, pgoff, flags);
2158
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002159 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002160 return addr;
2161 if (IS_ERR_VALUE(addr))
2162 return addr;
2163 if (addr & ~PAGE_MASK)
2164 return addr;
2165 if (addr > TASK_SIZE - len)
2166 return addr;
2167
2168 if (shmem_huge == SHMEM_HUGE_DENY)
2169 return addr;
2170 if (len < HPAGE_PMD_SIZE)
2171 return addr;
2172 if (flags & MAP_FIXED)
2173 return addr;
2174 /*
2175 * Our priority is to support MAP_SHARED mapped hugely;
2176 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002177 * But if caller specified an address hint and we allocated area there
2178 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002179 */
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002180 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002181 return addr;
2182
2183 if (shmem_huge != SHMEM_HUGE_FORCE) {
2184 struct super_block *sb;
2185
2186 if (file) {
2187 VM_BUG_ON(file->f_op != &shmem_file_operations);
2188 sb = file_inode(file)->i_sb;
2189 } else {
2190 /*
2191 * Called directly from mm/mmap.c, or drivers/char/mem.c
2192 * for "/dev/zero", to create a shared anonymous object.
2193 */
2194 if (IS_ERR(shm_mnt))
2195 return addr;
2196 sb = shm_mnt->mnt_sb;
2197 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002198 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002199 return addr;
2200 }
2201
2202 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2203 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2204 return addr;
2205 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2206 return addr;
2207
2208 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2209 if (inflated_len > TASK_SIZE)
2210 return addr;
2211 if (inflated_len < len)
2212 return addr;
2213
Kirill A. Shutemov99158992020-01-13 16:29:13 -08002214 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002215 if (IS_ERR_VALUE(inflated_addr))
2216 return addr;
2217 if (inflated_addr & ~PAGE_MASK)
2218 return addr;
2219
2220 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2221 inflated_addr += offset - inflated_offset;
2222 if (inflated_offset > offset)
2223 inflated_addr += HPAGE_PMD_SIZE;
2224
2225 if (inflated_addr > TASK_SIZE - len)
2226 return addr;
2227 return inflated_addr;
2228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002231static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
Al Viro496ad9a2013-01-23 17:07:38 -05002233 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002234 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235}
2236
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002237static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2238 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Al Viro496ad9a2013-01-23 17:07:38 -05002240 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002241 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002243 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2244 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246#endif
2247
2248int shmem_lock(struct file *file, int lock, struct user_struct *user)
2249{
Al Viro496ad9a2013-01-23 17:07:38 -05002250 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 struct shmem_inode_info *info = SHMEM_I(inode);
2252 int retval = -ENOMEM;
2253
Hugh Dickinsea0dfeb2020-04-20 18:14:14 -07002254 /*
2255 * What serializes the accesses to info->flags?
2256 * ipc_lock_object() when called from shmctl_do_lock(),
2257 * no serialization needed when called from shm_destroy().
2258 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (lock && !(info->flags & VM_LOCKED)) {
2260 if (!user_shm_lock(inode->i_size, user))
2261 goto out_nomem;
2262 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002263 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 if (!lock && (info->flags & VM_LOCKED) && user) {
2266 user_shm_unlock(inode->i_size, user);
2267 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002268 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return retval;
2274}
2275
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002276static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002278 struct shmem_inode_info *info = SHMEM_I(file_inode(file));
Peter Xu01486862021-05-14 17:27:04 -07002279 int ret;
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002280
Peter Xu01486862021-05-14 17:27:04 -07002281 ret = seal_check_future_write(info->seals, vma);
2282 if (ret)
2283 return ret;
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002284
Catalin Marinas51b0bff2019-11-29 12:45:08 +00002285 /* arm64 - allow memory tagging on RAM-based files */
2286 vma->vm_flags |= VM_MTE_ALLOWED;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 file_accessed(file);
2289 vma->vm_ops = &shmem_vm_ops;
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07002290 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002291 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2292 (vma->vm_end & HPAGE_PMD_MASK)) {
2293 khugepaged_enter(vma, vma->vm_flags);
2294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return 0;
2296}
2297
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002298static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002299 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
2301 struct inode *inode;
2302 struct shmem_inode_info *info;
2303 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Chris Downe809d5f2020-08-06 23:20:20 -07002304 ino_t ino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Chris Downe809d5f2020-08-06 23:20:20 -07002306 if (shmem_reserve_inode(sb, &ino))
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002307 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
2309 inode = new_inode(sb);
2310 if (inode) {
Chris Downe809d5f2020-08-06 23:20:20 -07002311 inode->i_ino = ino;
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002312 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002314 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002315 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 info = SHMEM_I(inode);
2317 memset(info, 0, (char *)inode - (char *)info);
2318 spin_lock_init(&info->lock);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002319 atomic_set(&info->stop_eviction, 0);
David Herrmann40e041a2014-08-08 14:25:27 -07002320 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002321 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002322 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002324 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002325 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 switch (mode & S_IFMT) {
2328 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002329 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 init_special_inode(inode, mode, dev);
2331 break;
2332 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002333 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 inode->i_op = &shmem_inode_operations;
2335 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002336 mpol_shared_policy_init(&info->policy,
2337 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 break;
2339 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002340 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /* Some things misbehave if size == 0 on a directory */
2342 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2343 inode->i_op = &shmem_dir_inode_operations;
2344 inode->i_fop = &simple_dir_operations;
2345 break;
2346 case S_IFLNK:
2347 /*
2348 * Must not load anything in the rbtree,
2349 * mpol_free_shared_policy will not be called.
2350 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002351 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002354
2355 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002356 } else
2357 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return inode;
2359}
2360
Johannes Weiner0cd61442014-04-03 14:47:46 -07002361bool shmem_mapping(struct address_space *mapping)
2362{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002363 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002364}
2365
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002366#ifdef CONFIG_USERFAULTFD
2367int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2368 pmd_t *dst_pmd,
2369 struct vm_area_struct *dst_vma,
2370 unsigned long dst_addr,
2371 unsigned long src_addr,
2372 bool zeropage,
2373 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002374{
2375 struct inode *inode = file_inode(dst_vma->vm_file);
2376 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002377 struct address_space *mapping = inode->i_mapping;
2378 gfp_t gfp = mapping_gfp_mask(mapping);
2379 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002380 void *page_kaddr;
2381 struct page *page;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002382 int ret;
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002383 pgoff_t max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002384
Axel Rasmussen140cfd92021-05-14 17:27:19 -07002385 if (!shmem_inode_acct_block(inode, 1)) {
2386 /*
2387 * We may have got a page, returned -ENOENT triggering a retry,
2388 * and now we find ourselves with -ENOMEM. Release the page, to
2389 * avoid a BUG_ON in our caller.
2390 */
2391 if (unlikely(*pagep)) {
2392 put_page(*pagep);
2393 *pagep = NULL;
2394 }
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002395 return -ENOMEM;
Axel Rasmussen140cfd92021-05-14 17:27:19 -07002396 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002397
Lokesh Gidra84330a52021-05-13 05:11:10 -07002398 if (!*pagep) {
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002399 ret = -ENOMEM;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002400 page = shmem_alloc_page(gfp, info, pgoff);
2401 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002402 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002403
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002404 if (!zeropage) { /* COPY */
Mike Rapoport8d103962017-09-06 16:23:02 -07002405 page_kaddr = kmap_atomic(page);
2406 ret = copy_from_user(page_kaddr,
2407 (const void __user *)src_addr,
2408 PAGE_SIZE);
2409 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002410
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002411 /* fallback to copy_from_user outside mmap_lock */
Mike Rapoport8d103962017-09-06 16:23:02 -07002412 if (unlikely(ret)) {
2413 *pagep = page;
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002414 ret = -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002415 /* don't free the page */
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002416 goto out_unacct_blocks;
Mike Rapoport8d103962017-09-06 16:23:02 -07002417 }
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002418 } else { /* ZEROPAGE */
Mike Rapoport8d103962017-09-06 16:23:02 -07002419 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002420 }
2421 } else {
2422 page = *pagep;
2423 *pagep = NULL;
2424 }
2425
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002426 VM_BUG_ON(PageLocked(page));
2427 VM_BUG_ON(PageSwapBacked(page));
Lokesh Gidra84330a52021-05-13 05:11:10 -07002428 __SetPageLocked(page);
2429 __SetPageSwapBacked(page);
2430 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002431
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002432 ret = -EFAULT;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002433 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002434 if (unlikely(pgoff >= max_off))
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002435 goto out_release;
2436
Lokesh Gidra84330a52021-05-13 05:11:10 -07002437 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2438 gfp & GFP_RECLAIM_MASK, dst_mm);
2439 if (ret)
2440 goto out_release;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002441
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002442 ret = mfill_atomic_install_pte(dst_mm, dst_pmd, dst_vma, dst_addr,
2443 page, true, false);
2444 if (ret)
2445 goto out_delete_from_cache;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002446
Lokesh Gidra84330a52021-05-13 05:11:10 -07002447 spin_lock_irq(&info->lock);
2448 info->alloced++;
2449 inode->i_blocks += BLOCKS_PER_PAGE;
2450 shmem_recalc_inode(inode);
2451 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002452
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002453 SetPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002454 unlock_page(page);
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002455 return 0;
2456out_delete_from_cache:
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002457 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002458out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002459 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002460 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002461out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002462 shmem_inode_unacct_blocks(inode, 1);
Axel Rasmussen0b161cf2021-05-11 15:05:59 +10002463 return ret;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002464}
Axel Rasmussen5f6dc072021-05-11 15:05:57 +10002465#endif /* CONFIG_USERFAULTFD */
Mike Rapoport8d103962017-09-06 16:23:02 -07002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002468static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002469static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002471#ifdef CONFIG_TMPFS_XATTR
2472static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2473#else
2474#define shmem_initxattrs NULL
2475#endif
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002478shmem_write_begin(struct file *file, struct address_space *mapping,
2479 loff_t pos, unsigned len, unsigned flags,
2480 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
Nick Piggin800d15a2007-10-16 01:25:03 -07002482 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002483 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002484 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002485
2486 /* i_mutex is held by caller */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002487 if (unlikely(info->seals & (F_SEAL_GROW |
2488 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2489 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
David Herrmann40e041a2014-08-08 14:25:27 -07002490 return -EPERM;
2491 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2492 return -EPERM;
2493 }
2494
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002495 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002496}
2497
2498static int
2499shmem_write_end(struct file *file, struct address_space *mapping,
2500 loff_t pos, unsigned len, unsigned copied,
2501 struct page *page, void *fsdata)
2502{
2503 struct inode *inode = mapping->host;
2504
Hugh Dickinsd3602442008-02-04 22:28:44 -08002505 if (pos + copied > inode->i_size)
2506 i_size_write(inode, pos + copied);
2507
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002508 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002509 struct page *head = compound_head(page);
2510 if (PageTransCompound(page)) {
2511 int i;
2512
2513 for (i = 0; i < HPAGE_PMD_NR; i++) {
2514 if (head + i == page)
2515 continue;
2516 clear_highpage(head + i);
2517 flush_dcache_page(head + i);
2518 }
2519 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002520 if (copied < PAGE_SIZE) {
2521 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002522 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002523 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002524 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002525 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002526 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002527 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002528 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002529 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002530
Nick Piggin800d15a2007-10-16 01:25:03 -07002531 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Al Viro2ba5bbe2014-04-02 20:00:02 -04002534static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Al Viro6e58e792014-02-03 17:07:03 -05002536 struct file *file = iocb->ki_filp;
2537 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002539 pgoff_t index;
2540 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002541 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002542 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002543 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002544 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002545
2546 /*
2547 * Might this read be for a stacking filesystem? Then when reading
2548 * holes of a sparse file, we actually need to allocate those pages,
2549 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2550 */
Al Viro777eda22014-12-17 04:46:46 -05002551 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002552 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002554 index = *ppos >> PAGE_SHIFT;
2555 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 for (;;) {
2558 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002559 pgoff_t end_index;
2560 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 loff_t i_size = i_size_read(inode);
2562
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002563 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (index > end_index)
2565 break;
2566 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002567 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (nr <= offset)
2569 break;
2570 }
2571
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002572 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002573 if (error) {
2574 if (error == -EINVAL)
2575 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 break;
2577 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002578 if (page) {
2579 if (sgp == SGP_CACHE)
2580 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002581 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 /*
2585 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002586 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002588 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002590 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002592 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (nr <= offset) {
2594 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002595 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 break;
2597 }
2598 }
2599 nr -= offset;
2600
2601 if (page) {
2602 /*
2603 * If users can be writing to this page using arbitrary
2604 * virtual addresses, take care about potential aliasing
2605 * before reading the page on the kernel side.
2606 */
2607 if (mapping_writably_mapped(mapping))
2608 flush_dcache_page(page);
2609 /*
2610 * Mark the page accessed if we read the beginning.
2611 */
2612 if (!offset)
2613 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002614 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002616 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 /*
2620 * Ok, we have the page, and it's up-to-date, so
2621 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002623 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002624 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002626 index += offset >> PAGE_SHIFT;
2627 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002629 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002630 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
Al Viro6e58e792014-02-03 17:07:03 -05002632 if (ret < nr) {
2633 error = -EFAULT;
2634 break;
2635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 cond_resched();
2637 }
2638
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002639 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002640 file_accessed(file);
2641 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
2643
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002644/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002645 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002646 */
2647static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002648 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002649{
2650 struct page *page;
2651 struct pagevec pvec;
2652 pgoff_t indices[PAGEVEC_SIZE];
2653 bool done = false;
2654 int i;
2655
Mel Gorman86679822017-11-15 17:37:52 -08002656 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002657 pvec.nr = 1; /* start small: we may be there already */
2658 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002659 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002660 pvec.nr, pvec.pages, indices);
2661 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002662 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002663 index = end;
2664 break;
2665 }
2666 for (i = 0; i < pvec.nr; i++, index++) {
2667 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002668 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002669 done = true;
2670 break;
2671 }
2672 index = indices[i];
2673 }
2674 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002675 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002676 if (!PageUptodate(page))
2677 page = NULL;
2678 }
2679 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002680 (page && whence == SEEK_DATA) ||
2681 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002682 done = true;
2683 break;
2684 }
2685 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002686 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002687 pagevec_release(&pvec);
2688 pvec.nr = PAGEVEC_SIZE;
2689 cond_resched();
2690 }
2691 return index;
2692}
2693
Andrew Morton965c8e52012-12-17 15:59:39 -08002694static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002695{
2696 struct address_space *mapping = file->f_mapping;
2697 struct inode *inode = mapping->host;
2698 pgoff_t start, end;
2699 loff_t new_offset;
2700
Andrew Morton965c8e52012-12-17 15:59:39 -08002701 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2702 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002703 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002704 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002705 /* We're holding i_mutex so we can access i_size directly */
2706
Yufen Yu1a413642018-11-16 15:08:39 -08002707 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002708 offset = -ENXIO;
2709 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002710 start = offset >> PAGE_SHIFT;
2711 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002712 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002713 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002714 if (new_offset > offset) {
2715 if (new_offset < inode->i_size)
2716 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002717 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002718 offset = -ENXIO;
2719 else
2720 offset = inode->i_size;
2721 }
2722 }
2723
Hugh Dickins387aae62013-08-04 11:30:25 -07002724 if (offset >= 0)
2725 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002726 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002727 return offset;
2728}
2729
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002730static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2731 loff_t len)
2732{
Al Viro496ad9a2013-01-23 17:07:38 -05002733 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002734 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002735 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002736 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002737 pgoff_t start, index, end;
2738 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002739
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002740 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2741 return -EOPNOTSUPP;
2742
Al Viro59551022016-01-22 15:40:57 -05002743 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002744
2745 if (mode & FALLOC_FL_PUNCH_HOLE) {
2746 struct address_space *mapping = file->f_mapping;
2747 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2748 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002749 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002750
David Herrmann40e041a2014-08-08 14:25:27 -07002751 /* protected by i_mutex */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002752 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
David Herrmann40e041a2014-08-08 14:25:27 -07002753 error = -EPERM;
2754 goto out;
2755 }
2756
Hugh Dickins8e205f72014-07-23 14:00:10 -07002757 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Junaa71ecd2019-11-30 17:58:11 -08002758 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002759 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2760 spin_lock(&inode->i_lock);
2761 inode->i_private = &shmem_falloc;
2762 spin_unlock(&inode->i_lock);
2763
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002764 if ((u64)unmap_end > (u64)unmap_start)
2765 unmap_mapping_range(mapping, unmap_start,
2766 1 + unmap_end - unmap_start, 0);
2767 shmem_truncate_range(inode, offset, offset + len - 1);
2768 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002769
2770 spin_lock(&inode->i_lock);
2771 inode->i_private = NULL;
2772 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002773 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002774 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002775 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002776 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002777 }
2778
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002779 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2780 error = inode_newsize_ok(inode, offset + len);
2781 if (error)
2782 goto out;
2783
David Herrmann40e041a2014-08-08 14:25:27 -07002784 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2785 error = -EPERM;
2786 goto out;
2787 }
2788
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002789 start = offset >> PAGE_SHIFT;
2790 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002791 /* Try to avoid a swapstorm if len is impossible to satisfy */
2792 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2793 error = -ENOSPC;
2794 goto out;
2795 }
2796
Hugh Dickins8e205f72014-07-23 14:00:10 -07002797 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002798 shmem_falloc.start = start;
2799 shmem_falloc.next = start;
2800 shmem_falloc.nr_falloced = 0;
2801 shmem_falloc.nr_unswapped = 0;
2802 spin_lock(&inode->i_lock);
2803 inode->i_private = &shmem_falloc;
2804 spin_unlock(&inode->i_lock);
2805
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002806 for (index = start; index < end; index++) {
2807 struct page *page;
2808
2809 /*
2810 * Good, the fallocate(2) manpage permits EINTR: we may have
2811 * been interrupted because we are using up too much memory.
2812 */
2813 if (signal_pending(current))
2814 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002815 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2816 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002817 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002818 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002819 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002820 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002821 if (index > start) {
2822 shmem_undo_range(inode,
2823 (loff_t)start << PAGE_SHIFT,
2824 ((loff_t)index << PAGE_SHIFT) - 1, true);
2825 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002826 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002827 }
2828
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002829 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002830 * Inform shmem_writepage() how far we have reached.
2831 * No need for lock or barrier: we have the page lock.
2832 */
2833 shmem_falloc.next++;
2834 if (!PageUptodate(page))
2835 shmem_falloc.nr_falloced++;
2836
2837 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002838 * If !PageUptodate, leave it that way so that freeable pages
2839 * can be recognized if we need to rollback on error later.
2840 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002841 * than free the pages we are allocating (and SGP_CACHE pages
2842 * might still be clean: we now need to mark those dirty too).
2843 */
2844 set_page_dirty(page);
2845 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002846 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002847 cond_resched();
2848 }
2849
2850 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2851 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002852 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002853undone:
2854 spin_lock(&inode->i_lock);
2855 inode->i_private = NULL;
2856 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002857out:
Al Viro59551022016-01-22 15:40:57 -05002858 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002859 return error;
2860}
2861
David Howells726c3342006-06-23 02:02:58 -07002862static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
David Howells726c3342006-06-23 02:02:58 -07002864 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
2866 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002867 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002869 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002871 buf->f_bavail =
2872 buf->f_bfree = sbinfo->max_blocks -
2873 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002874 }
2875 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 buf->f_files = sbinfo->max_inodes;
2877 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 }
2879 /* else leave those fields 0 like simple_statfs */
2880 return 0;
2881}
2882
2883/*
2884 * File creation. Allocate an inode, and we're done..
2885 */
2886static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002887shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002889 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 int error = -ENOSPC;
2891
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002892 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002894 error = simple_acl_create(dir, inode);
2895 if (error)
2896 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002897 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002898 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002899 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002900 if (error && error != -EOPNOTSUPP)
2901 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002902
Al Viro718deb62009-12-16 19:35:36 -05002903 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002905 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 d_instantiate(dentry, inode);
2907 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002910out_iput:
2911 iput(inode);
2912 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
Al Viro60545d02013-06-07 01:20:27 -04002915static int
2916shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2917{
2918 struct inode *inode;
2919 int error = -ENOSPC;
2920
2921 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2922 if (inode) {
2923 error = security_inode_init_security(inode, dir,
2924 NULL,
2925 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002926 if (error && error != -EOPNOTSUPP)
2927 goto out_iput;
2928 error = simple_acl_create(dir, inode);
2929 if (error)
2930 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002931 d_tmpfile(dentry, inode);
2932 }
2933 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002934out_iput:
2935 iput(inode);
2936 return error;
Al Viro60545d02013-06-07 01:20:27 -04002937}
2938
Al Viro18bb1db2011-07-26 01:41:39 -04002939static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940{
2941 int error;
2942
2943 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2944 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002945 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return 0;
2947}
2948
Al Viro4acdaf22011-07-26 01:42:34 -04002949static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002950 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2953}
2954
2955/*
2956 * Link a file..
2957 */
2958static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2959{
David Howells75c3cfa2015-03-17 22:26:12 +00002960 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e602019-02-22 22:35:32 -08002961 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963 /*
2964 * No ordinary (disk based) filesystem counts links as inodes;
2965 * but each new link needs a new dentry, pinning lowmem, and
2966 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002967 * But if an O_TMPFILE file is linked into the tmpfs, the
2968 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002970 if (inode->i_nlink) {
Chris Downe809d5f2020-08-06 23:20:20 -07002971 ret = shmem_reserve_inode(inode->i_sb, NULL);
Darrick J. Wong1062af92019-02-21 08:48:09 -08002972 if (ret)
2973 goto out;
2974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002977 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002978 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002979 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 dget(dentry); /* Extra pinning count for the created dentry */
2981 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002982out:
2983 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2987{
David Howells75c3cfa2015-03-17 22:26:12 +00002988 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002990 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2991 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002994 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002995 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 dput(dentry); /* Undo the count from "create" - this does all the work */
2997 return 0;
2998}
2999
3000static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
3001{
3002 if (!simple_empty(dentry))
3003 return -ENOTEMPTY;
3004
David Howells75c3cfa2015-03-17 22:26:12 +00003005 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003006 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return shmem_unlink(dir, dentry);
3008}
3009
Miklos Szeredi37456772014-07-23 15:15:34 +02003010static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
3011{
David Howellse36cb0b2015-01-29 12:02:35 +00003012 bool old_is_dir = d_is_dir(old_dentry);
3013 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02003014
3015 if (old_dir != new_dir && old_is_dir != new_is_dir) {
3016 if (old_is_dir) {
3017 drop_nlink(old_dir);
3018 inc_nlink(new_dir);
3019 } else {
3020 drop_nlink(new_dir);
3021 inc_nlink(old_dir);
3022 }
3023 }
3024 old_dir->i_ctime = old_dir->i_mtime =
3025 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00003026 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003027 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02003028
3029 return 0;
3030}
3031
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003032static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
3033{
3034 struct dentry *whiteout;
3035 int error;
3036
3037 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3038 if (!whiteout)
3039 return -ENOMEM;
3040
3041 error = shmem_mknod(old_dir, whiteout,
3042 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3043 dput(whiteout);
3044 if (error)
3045 return error;
3046
3047 /*
3048 * Cheat and hash the whiteout while the old dentry is still in
3049 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3050 *
3051 * d_lookup() will consistently find one of them at this point,
3052 * not sure which one, but that isn't even important.
3053 */
3054 d_rehash(whiteout);
3055 return 0;
3056}
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058/*
3059 * The VFS layer already does all the dentry stuff for rename,
3060 * we just have to decrement the usage count for the target if
3061 * it exists so that the VFS layer correctly free's it when it
3062 * gets overwritten.
3063 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003064static 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 -07003065{
David Howells75c3cfa2015-03-17 22:26:12 +00003066 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 int they_are_dirs = S_ISDIR(inode->i_mode);
3068
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003069 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003070 return -EINVAL;
3071
Miklos Szeredi37456772014-07-23 15:15:34 +02003072 if (flags & RENAME_EXCHANGE)
3073 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (!simple_empty(new_dentry))
3076 return -ENOTEMPTY;
3077
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003078 if (flags & RENAME_WHITEOUT) {
3079 int error;
3080
3081 error = shmem_whiteout(old_dir, old_dentry);
3082 if (error)
3083 return error;
3084 }
3085
David Howells75c3cfa2015-03-17 22:26:12 +00003086 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003088 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003089 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003090 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003093 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003094 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
3096
3097 old_dir->i_size -= BOGO_DIRENT_SIZE;
3098 new_dir->i_size += BOGO_DIRENT_SIZE;
3099 old_dir->i_ctime = old_dir->i_mtime =
3100 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003101 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 return 0;
3103}
3104
3105static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3106{
3107 int error;
3108 int len;
3109 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003110 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
3112 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003113 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return -ENAMETOOLONG;
3115
Joe Perches0825a6f2018-06-14 15:27:58 -07003116 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3117 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (!inode)
3119 return -ENOSPC;
3120
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003121 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003122 shmem_initxattrs, NULL);
Mateusz Nosek343c3d72020-04-06 20:07:54 -07003123 if (error && error != -EOPNOTSUPP) {
3124 iput(inode);
3125 return error;
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003126 }
3127
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003129 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003130 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3131 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003132 iput(inode);
3133 return -ENOMEM;
3134 }
3135 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003137 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003138 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 if (error) {
3140 iput(inode);
3141 return error;
3142 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003143 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003145 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003146 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003148 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003149 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003152 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 d_instantiate(dentry, inode);
3154 dget(dentry);
3155 return 0;
3156}
3157
Al Virofceef392015-12-29 15:58:39 -05003158static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Al Virofceef392015-12-29 15:58:39 -05003160 mark_page_accessed(arg);
3161 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Al Viro6b255392015-11-17 10:20:54 -05003164static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003165 struct inode *inode,
3166 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003169 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003170 if (!dentry) {
3171 page = find_get_page(inode->i_mapping, 0);
3172 if (!page)
3173 return ERR_PTR(-ECHILD);
3174 if (!PageUptodate(page)) {
3175 put_page(page);
3176 return ERR_PTR(-ECHILD);
3177 }
3178 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003179 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003180 if (error)
3181 return ERR_PTR(error);
3182 unlock_page(page);
3183 }
Al Virofceef392015-12-29 15:58:39 -05003184 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003185 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
Eric Parisb09e0fa2011-05-24 17:12:39 -07003188#ifdef CONFIG_TMPFS_XATTR
3189/*
3190 * Superblocks without xattr inode operations may get some security.* xattr
3191 * support from the LSM "for free". As soon as we have any other xattrs
3192 * like ACLs, we also need to implement the security.* handlers at
3193 * filesystem level, though.
3194 */
3195
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003196/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003197 * Callback for security_inode_init_security() for acquiring xattrs.
3198 */
3199static int shmem_initxattrs(struct inode *inode,
3200 const struct xattr *xattr_array,
3201 void *fs_info)
3202{
3203 struct shmem_inode_info *info = SHMEM_I(inode);
3204 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003205 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003206 size_t len;
3207
3208 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003209 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003210 if (!new_xattr)
3211 return -ENOMEM;
3212
3213 len = strlen(xattr->name) + 1;
3214 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3215 GFP_KERNEL);
3216 if (!new_xattr->name) {
Chengguang Xu3bef7352020-07-23 21:15:14 -07003217 kvfree(new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003218 return -ENOMEM;
3219 }
3220
3221 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3222 XATTR_SECURITY_PREFIX_LEN);
3223 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3224 xattr->name, len);
3225
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003226 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003227 }
3228
3229 return 0;
3230}
3231
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003232static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003233 struct dentry *unused, struct inode *inode,
Mark Salyzyn3484eba2019-11-04 08:57:10 -08003234 const char *name, void *buffer, size_t size,
3235 int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003236{
Al Virob2968212016-04-10 20:48:24 -04003237 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003238
3239 name = xattr_full_name(handler, name);
3240 return simple_xattr_get(&info->xattrs, name, buffer, size);
3241}
3242
3243static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003244 struct dentry *unused, struct inode *inode,
3245 const char *name, const void *value,
3246 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003247{
Al Viro59301222016-05-27 10:19:30 -04003248 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003249
3250 name = xattr_full_name(handler, name);
Daniel Xua46a2292020-03-12 13:03:15 -07003251 return simple_xattr_set(&info->xattrs, name, value, size, flags, NULL);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003252}
3253
3254static const struct xattr_handler shmem_security_xattr_handler = {
3255 .prefix = XATTR_SECURITY_PREFIX,
3256 .get = shmem_xattr_handler_get,
3257 .set = shmem_xattr_handler_set,
3258};
3259
3260static const struct xattr_handler shmem_trusted_xattr_handler = {
3261 .prefix = XATTR_TRUSTED_PREFIX,
3262 .get = shmem_xattr_handler_get,
3263 .set = shmem_xattr_handler_set,
3264};
3265
Eric Parisb09e0fa2011-05-24 17:12:39 -07003266static const struct xattr_handler *shmem_xattr_handlers[] = {
3267#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003268 &posix_acl_access_xattr_handler,
3269 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003270#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003271 &shmem_security_xattr_handler,
3272 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003273 NULL
3274};
3275
Eric Parisb09e0fa2011-05-24 17:12:39 -07003276static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3277{
David Howells75c3cfa2015-03-17 22:26:12 +00003278 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003279 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003280}
3281#endif /* CONFIG_TMPFS_XATTR */
3282
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003283static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003284 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003285#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003286 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288};
3289
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003290static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003291 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003292#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003293 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003294#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003295};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003296
David M. Grimes91828a42006-10-17 00:09:45 -07003297static struct dentry *shmem_get_parent(struct dentry *child)
3298{
3299 return ERR_PTR(-ESTALE);
3300}
3301
3302static int shmem_match(struct inode *ino, void *vfh)
3303{
3304 __u32 *fh = vfh;
3305 __u64 inum = fh[2];
3306 inum = (inum << 32) | fh[1];
3307 return ino->i_ino == inum && fh[0] == ino->i_generation;
3308}
3309
Amir Goldstein12ba7802018-06-07 17:07:15 -07003310/* Find any alias of inode, but prefer a hashed alias */
3311static struct dentry *shmem_find_alias(struct inode *inode)
3312{
3313 struct dentry *alias = d_find_alias(inode);
3314
3315 return alias ?: d_find_any_alias(inode);
3316}
3317
3318
Christoph Hellwig480b1162007-10-21 16:42:13 -07003319static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3320 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003321{
David M. Grimes91828a42006-10-17 00:09:45 -07003322 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003323 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003324 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003325
Christoph Hellwig480b1162007-10-21 16:42:13 -07003326 if (fh_len < 3)
3327 return NULL;
3328
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003329 inum = fid->raw[2];
3330 inum = (inum << 32) | fid->raw[1];
3331
Christoph Hellwig480b1162007-10-21 16:42:13 -07003332 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3333 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003334 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003335 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003336 iput(inode);
3337 }
3338
Christoph Hellwig480b1162007-10-21 16:42:13 -07003339 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003340}
3341
Al Virob0b03822012-04-02 14:34:06 -04003342static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3343 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003344{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303345 if (*len < 3) {
3346 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003347 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303348 }
David M. Grimes91828a42006-10-17 00:09:45 -07003349
Al Viro1d3382cb2010-10-23 15:19:20 -04003350 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003351 /* Unfortunately insert_inode_hash is not idempotent,
3352 * so as we hash inodes here rather than at creation
3353 * time, we need a lock to ensure we only try
3354 * to do it once
3355 */
3356 static DEFINE_SPINLOCK(lock);
3357 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003358 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003359 __insert_inode_hash(inode,
3360 inode->i_ino + inode->i_generation);
3361 spin_unlock(&lock);
3362 }
3363
3364 fh[0] = inode->i_generation;
3365 fh[1] = inode->i_ino;
3366 fh[2] = ((__u64)inode->i_ino) >> 32;
3367
3368 *len = 3;
3369 return 1;
3370}
3371
Christoph Hellwig39655162007-10-21 16:42:17 -07003372static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003373 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003374 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003375 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003376};
3377
Al Viro626c3922019-09-08 20:28:06 -04003378enum shmem_param {
3379 Opt_gid,
3380 Opt_huge,
3381 Opt_mode,
3382 Opt_mpol,
3383 Opt_nr_blocks,
3384 Opt_nr_inodes,
3385 Opt_size,
3386 Opt_uid,
Chris Downea3271f2020-08-06 23:20:25 -07003387 Opt_inode32,
3388 Opt_inode64,
Al Viro626c3922019-09-08 20:28:06 -04003389};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390
Al Viro5eede622019-12-16 13:33:32 -05003391static const struct constant_table shmem_param_enums_huge[] = {
Al Viro2710c957a2019-09-06 22:12:08 -04003392 {"never", SHMEM_HUGE_NEVER },
3393 {"always", SHMEM_HUGE_ALWAYS },
3394 {"within_size", SHMEM_HUGE_WITHIN_SIZE },
3395 {"advise", SHMEM_HUGE_ADVISE },
Al Viro2710c957a2019-09-06 22:12:08 -04003396 {}
3397};
3398
Al Virod7167b12019-09-07 07:23:15 -04003399const struct fs_parameter_spec shmem_fs_parameters[] = {
Al Viro626c3922019-09-08 20:28:06 -04003400 fsparam_u32 ("gid", Opt_gid),
Al Viro2710c957a2019-09-06 22:12:08 -04003401 fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge),
Al Viro626c3922019-09-08 20:28:06 -04003402 fsparam_u32oct("mode", Opt_mode),
3403 fsparam_string("mpol", Opt_mpol),
3404 fsparam_string("nr_blocks", Opt_nr_blocks),
3405 fsparam_string("nr_inodes", Opt_nr_inodes),
3406 fsparam_string("size", Opt_size),
3407 fsparam_u32 ("uid", Opt_uid),
Chris Downea3271f2020-08-06 23:20:25 -07003408 fsparam_flag ("inode32", Opt_inode32),
3409 fsparam_flag ("inode64", Opt_inode64),
Al Viro626c3922019-09-08 20:28:06 -04003410 {}
3411};
3412
David Howellsf3235622019-03-25 16:38:31 +00003413static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param)
Al Viro626c3922019-09-08 20:28:06 -04003414{
David Howellsf3235622019-03-25 16:38:31 +00003415 struct shmem_options *ctx = fc->fs_private;
Al Viro626c3922019-09-08 20:28:06 -04003416 struct fs_parse_result result;
3417 unsigned long long size;
3418 char *rest;
3419 int opt;
3420
Al Virod7167b12019-09-07 07:23:15 -04003421 opt = fs_parse(fc, shmem_fs_parameters, param, &result);
David Howellsf3235622019-03-25 16:38:31 +00003422 if (opt < 0)
Al Viro626c3922019-09-08 20:28:06 -04003423 return opt;
Al Viro626c3922019-09-08 20:28:06 -04003424
3425 switch (opt) {
3426 case Opt_size:
3427 size = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003428 if (*rest == '%') {
3429 size <<= PAGE_SHIFT;
3430 size *= totalram_pages();
3431 do_div(size, 100);
3432 rest++;
3433 }
3434 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003435 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003436 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE);
3437 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003438 break;
3439 case Opt_nr_blocks:
3440 ctx->blocks = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003441 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003442 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003443 ctx->seen |= SHMEM_SEEN_BLOCKS;
Al Viro626c3922019-09-08 20:28:06 -04003444 break;
3445 case Opt_nr_inodes:
3446 ctx->inodes = memparse(param->string, &rest);
Al Viroe04dc422019-09-08 19:20:12 -04003447 if (*rest)
Al Viro626c3922019-09-08 20:28:06 -04003448 goto bad_value;
Al Viroe04dc422019-09-08 19:20:12 -04003449 ctx->seen |= SHMEM_SEEN_INODES;
Al Viro626c3922019-09-08 20:28:06 -04003450 break;
3451 case Opt_mode:
3452 ctx->mode = result.uint_32 & 07777;
3453 break;
3454 case Opt_uid:
3455 ctx->uid = make_kuid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003456 if (!uid_valid(ctx->uid))
Al Viro626c3922019-09-08 20:28:06 -04003457 goto bad_value;
3458 break;
3459 case Opt_gid:
3460 ctx->gid = make_kgid(current_user_ns(), result.uint_32);
Al Viroe04dc422019-09-08 19:20:12 -04003461 if (!gid_valid(ctx->gid))
Al Viro626c3922019-09-08 20:28:06 -04003462 goto bad_value;
3463 break;
3464 case Opt_huge:
3465 ctx->huge = result.uint_32;
3466 if (ctx->huge != SHMEM_HUGE_NEVER &&
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003467 !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Al Viro626c3922019-09-08 20:28:06 -04003468 has_transparent_hugepage()))
3469 goto unsupported_parameter;
Al Viroe04dc422019-09-08 19:20:12 -04003470 ctx->seen |= SHMEM_SEEN_HUGE;
Al Viro626c3922019-09-08 20:28:06 -04003471 break;
3472 case Opt_mpol:
3473 if (IS_ENABLED(CONFIG_NUMA)) {
3474 mpol_put(ctx->mpol);
3475 ctx->mpol = NULL;
3476 if (mpol_parse_str(param->string, &ctx->mpol))
3477 goto bad_value;
3478 break;
3479 }
3480 goto unsupported_parameter;
Chris Downea3271f2020-08-06 23:20:25 -07003481 case Opt_inode32:
3482 ctx->full_inums = false;
3483 ctx->seen |= SHMEM_SEEN_INUMS;
3484 break;
3485 case Opt_inode64:
3486 if (sizeof(ino_t) < 8) {
3487 return invalfc(fc,
3488 "Cannot use inode64 with <64bit inums in kernel\n");
3489 }
3490 ctx->full_inums = true;
3491 ctx->seen |= SHMEM_SEEN_INUMS;
3492 break;
Al Viroe04dc422019-09-08 19:20:12 -04003493 }
3494 return 0;
3495
Al Viro626c3922019-09-08 20:28:06 -04003496unsupported_parameter:
Al Virof35aa2b2019-12-21 21:35:55 -05003497 return invalfc(fc, "Unsupported parameter '%s'", param->key);
Al Viro626c3922019-09-08 20:28:06 -04003498bad_value:
Al Virof35aa2b2019-12-21 21:35:55 -05003499 return invalfc(fc, "Bad value for '%s'", param->key);
Al Viroe04dc422019-09-08 19:20:12 -04003500}
3501
David Howellsf3235622019-03-25 16:38:31 +00003502static int shmem_parse_options(struct fs_context *fc, void *data)
Al Viroe04dc422019-09-08 19:20:12 -04003503{
David Howellsf3235622019-03-25 16:38:31 +00003504 char *options = data;
3505
Al Viro33f37c62019-10-09 22:48:01 -04003506 if (options) {
3507 int err = security_sb_eat_lsm_opts(options, &fc->security);
3508 if (err)
3509 return err;
3510 }
3511
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003512 while (options != NULL) {
Al Viro626c3922019-09-08 20:28:06 -04003513 char *this_char = options;
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003514 for (;;) {
3515 /*
3516 * NUL-terminate this option: unfortunately,
3517 * mount options form a comma-separated list,
3518 * but mpol's nodelist may also contain commas.
3519 */
3520 options = strchr(options, ',');
3521 if (options == NULL)
3522 break;
3523 options++;
3524 if (!isdigit(*options)) {
3525 options[-1] = '\0';
3526 break;
3527 }
3528 }
Al Viro626c3922019-09-08 20:28:06 -04003529 if (*this_char) {
3530 char *value = strchr(this_char,'=');
David Howellsf3235622019-03-25 16:38:31 +00003531 size_t len = 0;
Al Viro626c3922019-09-08 20:28:06 -04003532 int err;
3533
3534 if (value) {
3535 *value++ = '\0';
David Howellsf3235622019-03-25 16:38:31 +00003536 len = strlen(value);
Al Viro626c3922019-09-08 20:28:06 -04003537 }
David Howellsf3235622019-03-25 16:38:31 +00003538 err = vfs_parse_fs_string(fc, this_char, value, len);
3539 if (err < 0)
3540 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
David Howellsf3235622019-03-25 16:38:31 +00003546/*
3547 * Reconfigure a shmem filesystem.
3548 *
3549 * Note that we disallow change from limited->unlimited blocks/inodes while any
3550 * are in use; but we must separately disallow unlimited->limited, because in
3551 * that case we have no record of how much is already in use.
3552 */
3553static int shmem_reconfigure(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
David Howellsf3235622019-03-25 16:38:31 +00003555 struct shmem_options *ctx = fc->fs_private;
3556 struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003557 unsigned long inodes;
David Howellsf3235622019-03-25 16:38:31 +00003558 const char *err;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003559
3560 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003561 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
David Howellsf3235622019-03-25 16:38:31 +00003562 if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) {
3563 if (!sbinfo->max_blocks) {
3564 err = "Cannot retroactively limit size";
Al Viro0b5071d2019-09-08 18:49:18 -04003565 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003566 }
Al Viro0b5071d2019-09-08 18:49:18 -04003567 if (percpu_counter_compare(&sbinfo->used_blocks,
David Howellsf3235622019-03-25 16:38:31 +00003568 ctx->blocks) > 0) {
3569 err = "Too small a size for current use";
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 }
David Howellsf3235622019-03-25 16:38:31 +00003573 if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) {
3574 if (!sbinfo->max_inodes) {
3575 err = "Cannot retroactively limit inodes";
Al Viro0b5071d2019-09-08 18:49:18 -04003576 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003577 }
3578 if (ctx->inodes < inodes) {
3579 err = "Too few inodes for current use";
Al Viro0b5071d2019-09-08 18:49:18 -04003580 goto out;
David Howellsf3235622019-03-25 16:38:31 +00003581 }
Al Viro0b5071d2019-09-08 18:49:18 -04003582 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003583
Chris Downea3271f2020-08-06 23:20:25 -07003584 if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums &&
3585 sbinfo->next_ino > UINT_MAX) {
3586 err = "Current inum too high to switch to 32-bit inums";
3587 goto out;
3588 }
3589
David Howellsf3235622019-03-25 16:38:31 +00003590 if (ctx->seen & SHMEM_SEEN_HUGE)
3591 sbinfo->huge = ctx->huge;
Chris Downea3271f2020-08-06 23:20:25 -07003592 if (ctx->seen & SHMEM_SEEN_INUMS)
3593 sbinfo->full_inums = ctx->full_inums;
David Howellsf3235622019-03-25 16:38:31 +00003594 if (ctx->seen & SHMEM_SEEN_BLOCKS)
3595 sbinfo->max_blocks = ctx->blocks;
3596 if (ctx->seen & SHMEM_SEEN_INODES) {
3597 sbinfo->max_inodes = ctx->inodes;
3598 sbinfo->free_inodes = ctx->inodes - inodes;
Al Viro0b5071d2019-09-08 18:49:18 -04003599 }
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003600
Greg Thelen5f001102013-02-22 16:36:01 -08003601 /*
3602 * Preserve previous mempolicy unless mpol remount option was specified.
3603 */
David Howellsf3235622019-03-25 16:38:31 +00003604 if (ctx->mpol) {
Greg Thelen5f001102013-02-22 16:36:01 -08003605 mpol_put(sbinfo->mpol);
David Howellsf3235622019-03-25 16:38:31 +00003606 sbinfo->mpol = ctx->mpol; /* transfers initial ref */
3607 ctx->mpol = NULL;
Greg Thelen5f001102013-02-22 16:36:01 -08003608 }
David Howellsf3235622019-03-25 16:38:31 +00003609 spin_unlock(&sbinfo->stat_lock);
3610 return 0;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003611out:
3612 spin_unlock(&sbinfo->stat_lock);
Al Virof35aa2b2019-12-21 21:35:55 -05003613 return invalfc(fc, "%s", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003615
Al Viro34c80b12011-12-08 21:32:45 -05003616static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003617{
Al Viro34c80b12011-12-08 21:32:45 -05003618 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003619
3620 if (sbinfo->max_blocks != shmem_default_max_blocks())
3621 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003622 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003623 if (sbinfo->max_inodes != shmem_default_max_inodes())
3624 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003625 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003626 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003627 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3628 seq_printf(seq, ",uid=%u",
3629 from_kuid_munged(&init_user_ns, sbinfo->uid));
3630 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3631 seq_printf(seq, ",gid=%u",
3632 from_kgid_munged(&init_user_ns, sbinfo->gid));
Chris Downea3271f2020-08-06 23:20:25 -07003633
3634 /*
3635 * Showing inode{64,32} might be useful even if it's the system default,
3636 * since then people don't have to resort to checking both here and
3637 * /proc/config.gz to confirm 64-bit inums were successfully applied
3638 * (which may not even exist if IKCONFIG_PROC isn't enabled).
3639 *
3640 * We hide it when inode64 isn't the default and we are using 32-bit
3641 * inodes, since that probably just means the feature isn't even under
3642 * consideration.
3643 *
3644 * As such:
3645 *
3646 * +-----------------+-----------------+
3647 * | TMPFS_INODE64=y | TMPFS_INODE64=n |
3648 * +------------------+-----------------+-----------------+
3649 * | full_inums=true | show | show |
3650 * | full_inums=false | show | hide |
3651 * +------------------+-----------------+-----------------+
3652 *
3653 */
3654 if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums)
3655 seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32));
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003656#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003657 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3658 if (sbinfo->huge)
3659 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3660#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003661 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003662 return 0;
3663}
David Herrmann9183df22014-08-08 14:25:29 -07003664
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003665#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666
3667static void shmem_put_super(struct super_block *sb)
3668{
Hugh Dickins602586a2010-08-17 15:23:56 -07003669 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3670
Chris Downe809d5f2020-08-06 23:20:20 -07003671 free_percpu(sbinfo->ino_batch);
Hugh Dickins602586a2010-08-17 15:23:56 -07003672 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003673 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003674 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 sb->s_fs_info = NULL;
3676}
3677
David Howellsf3235622019-03-25 16:38:31 +00003678static int shmem_fill_super(struct super_block *sb, struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
David Howellsf3235622019-03-25 16:38:31 +00003680 struct shmem_options *ctx = fc->fs_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003682 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003683 int err = -ENOMEM;
3684
3685 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003686 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003687 L1_CACHE_BYTES), GFP_KERNEL);
3688 if (!sbinfo)
3689 return -ENOMEM;
3690
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003691 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003693#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 /*
3695 * Per default we only allow half of the physical ram per
3696 * tmpfs instance, limiting inodes to one per page of lowmem;
3697 * but the internal instance is left unlimited.
3698 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003699 if (!(sb->s_flags & SB_KERNMOUNT)) {
David Howellsf3235622019-03-25 16:38:31 +00003700 if (!(ctx->seen & SHMEM_SEEN_BLOCKS))
3701 ctx->blocks = shmem_default_max_blocks();
3702 if (!(ctx->seen & SHMEM_SEEN_INODES))
3703 ctx->inodes = shmem_default_max_inodes();
Chris Downea3271f2020-08-06 23:20:25 -07003704 if (!(ctx->seen & SHMEM_SEEN_INUMS))
3705 ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64);
Al Viroca4e0512013-08-31 12:57:10 -04003706 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003707 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 }
David M. Grimes91828a42006-10-17 00:09:45 -07003709 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003710 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003712 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713#endif
David Howellsf3235622019-03-25 16:38:31 +00003714 sbinfo->max_blocks = ctx->blocks;
3715 sbinfo->free_inodes = sbinfo->max_inodes = ctx->inodes;
Chris Downe809d5f2020-08-06 23:20:20 -07003716 if (sb->s_flags & SB_KERNMOUNT) {
3717 sbinfo->ino_batch = alloc_percpu(ino_t);
3718 if (!sbinfo->ino_batch)
3719 goto failed;
3720 }
David Howellsf3235622019-03-25 16:38:31 +00003721 sbinfo->uid = ctx->uid;
3722 sbinfo->gid = ctx->gid;
Chris Downea3271f2020-08-06 23:20:25 -07003723 sbinfo->full_inums = ctx->full_inums;
David Howellsf3235622019-03-25 16:38:31 +00003724 sbinfo->mode = ctx->mode;
3725 sbinfo->huge = ctx->huge;
3726 sbinfo->mpol = ctx->mpol;
3727 ctx->mpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003729 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003730 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003731 goto failed;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003732 spin_lock_init(&sbinfo->shrinklist_lock);
3733 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003734
Hugh Dickins285b2c42011-08-03 16:21:20 -07003735 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003736 sb->s_blocksize = PAGE_SIZE;
3737 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 sb->s_magic = TMPFS_MAGIC;
3739 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003740 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003741#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003742 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003743#endif
3744#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003745 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003746#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003747 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003748
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003749 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 if (!inode)
3751 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003752 inode->i_uid = sbinfo->uid;
3753 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003754 sb->s_root = d_make_root(inode);
3755 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003756 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 return 0;
3758
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759failed:
3760 shmem_put_super(sb);
3761 return err;
3762}
3763
David Howellsf3235622019-03-25 16:38:31 +00003764static int shmem_get_tree(struct fs_context *fc)
3765{
3766 return get_tree_nodev(fc, shmem_fill_super);
3767}
3768
3769static void shmem_free_fc(struct fs_context *fc)
3770{
3771 struct shmem_options *ctx = fc->fs_private;
3772
3773 if (ctx) {
3774 mpol_put(ctx->mpol);
3775 kfree(ctx);
3776 }
3777}
3778
3779static const struct fs_context_operations shmem_fs_context_ops = {
3780 .free = shmem_free_fc,
3781 .get_tree = shmem_get_tree,
3782#ifdef CONFIG_TMPFS
3783 .parse_monolithic = shmem_parse_options,
3784 .parse_param = shmem_parse_one,
3785 .reconfigure = shmem_reconfigure,
3786#endif
3787};
3788
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003789static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790
3791static struct inode *shmem_alloc_inode(struct super_block *sb)
3792{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003793 struct shmem_inode_info *info;
3794 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3795 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003797 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Al Viro74b1da52019-04-15 23:19:05 -04003800static void shmem_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003801{
Al Viro84e710d2016-04-15 00:58:55 -04003802 if (S_ISLNK(inode->i_mode))
3803 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003804 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3805}
3806
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807static void shmem_destroy_inode(struct inode *inode)
3808{
Al Viro09208d12011-07-26 03:15:03 -04003809 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811}
3812
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003813static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003815 struct shmem_inode_info *info = foo;
3816 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817}
3818
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003819static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
3821 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3822 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003823 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003826static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003828 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003831static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003833 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003835 .write_begin = shmem_write_begin,
3836 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003838#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003839 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003840#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003841 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842};
3843
Helge Deller15ad7cd2006-12-06 20:40:36 -08003844static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003846 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003848 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003849 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003850 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003851 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003852 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003853 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003854 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855#endif
3856};
3857
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003858static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003859 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003860 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003861#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003862 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003863 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003864#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865};
3866
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003867static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868#ifdef CONFIG_TMPFS
3869 .create = shmem_create,
3870 .lookup = simple_lookup,
3871 .link = shmem_link,
3872 .unlink = shmem_unlink,
3873 .symlink = shmem_symlink,
3874 .mkdir = shmem_mkdir,
3875 .rmdir = shmem_rmdir,
3876 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003877 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003878 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003880#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003881 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003882#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003883#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003884 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003885 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003886#endif
3887};
3888
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003889static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003890#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003891 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003892#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003893#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003894 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003895 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003896#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897};
3898
Hugh Dickins759b9772007-03-05 00:30:28 -08003899static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 .alloc_inode = shmem_alloc_inode,
Al Viro74b1da52019-04-15 23:19:05 -04003901 .free_inode = shmem_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 .destroy_inode = shmem_destroy_inode,
3903#ifdef CONFIG_TMPFS
3904 .statfs = shmem_statfs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003905 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906#endif
Al Viro1f895f72010-06-05 19:10:41 -04003907 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 .drop_inode = generic_delete_inode,
3909 .put_super = shmem_put_super,
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003910#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003911 .nr_cached_objects = shmem_unused_huge_count,
3912 .free_cached_objects = shmem_unused_huge_scan,
3913#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914};
3915
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003916static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003917 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003918 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919#ifdef CONFIG_NUMA
3920 .set_policy = shmem_set_policy,
3921 .get_policy = shmem_get_policy,
3922#endif
Vinayak Menon35eacb52021-03-18 15:20:17 +05303923#ifdef CONFIG_SPECULATIVE_PAGE_FAULT
3924 .allow_speculation = filemap_allow_speculation,
3925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926};
3927
David Howellsf3235622019-03-25 16:38:31 +00003928int shmem_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929{
David Howellsf3235622019-03-25 16:38:31 +00003930 struct shmem_options *ctx;
3931
3932 ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL);
3933 if (!ctx)
3934 return -ENOMEM;
3935
3936 ctx->mode = 0777 | S_ISVTX;
3937 ctx->uid = current_fsuid();
3938 ctx->gid = current_fsgid();
3939
3940 fc->fs_private = ctx;
3941 fc->ops = &shmem_fs_context_ops;
3942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943}
3944
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003945static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 .owner = THIS_MODULE,
3947 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00003948 .init_fs_context = shmem_init_fs_context,
3949#ifdef CONFIG_TMPFS
Al Virod7167b12019-09-07 07:23:15 -04003950 .parameters = shmem_fs_parameters,
David Howellsf3235622019-03-25 16:38:31 +00003951#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 .kill_sb = kill_litter_super,
Matthew Wilcox (Oracle)01c70262020-10-15 20:06:00 -07003953 .fs_flags = FS_USERNS_MOUNT | FS_THP_SUPPORT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003956int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957{
3958 int error;
3959
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003960 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003962 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003964 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 goto out2;
3966 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003967
Al Viroca4e0512013-08-31 12:57:10 -04003968 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (IS_ERR(shm_mnt)) {
3970 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003971 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 goto out1;
3973 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003974
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003975#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003976 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003977 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3978 else
3979 shmem_huge = 0; /* just in case it was patched */
3980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 return 0;
3982
3983out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003984 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003986 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 shm_mnt = ERR_PTR(error);
3988 return error;
3989}
Matt Mackall853ac432009-01-06 14:40:20 -08003990
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07003991#if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003992static ssize_t shmem_enabled_show(struct kobject *kobj,
3993 struct kobj_attribute *attr, char *buf)
3994{
Colin Ian King26083eb62019-11-30 17:58:04 -08003995 static const int values[] = {
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003996 SHMEM_HUGE_ALWAYS,
3997 SHMEM_HUGE_WITHIN_SIZE,
3998 SHMEM_HUGE_ADVISE,
3999 SHMEM_HUGE_NEVER,
4000 SHMEM_HUGE_DENY,
4001 SHMEM_HUGE_FORCE,
4002 };
4003 int i, count;
4004
4005 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
4006 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
4007
4008 count += sprintf(buf + count, fmt,
4009 shmem_format_huge(values[i]));
4010 }
4011 buf[count - 1] = '\n';
4012 return count;
4013}
4014
4015static ssize_t shmem_enabled_store(struct kobject *kobj,
4016 struct kobj_attribute *attr, const char *buf, size_t count)
4017{
4018 char tmp[16];
4019 int huge;
4020
4021 if (count + 1 > sizeof(tmp))
4022 return -EINVAL;
4023 memcpy(tmp, buf, count);
4024 tmp[count] = '\0';
4025 if (count && tmp[count - 1] == '\n')
4026 tmp[count - 1] = '\0';
4027
4028 huge = shmem_parse_huge(tmp);
4029 if (huge == -EINVAL)
4030 return -EINVAL;
4031 if (!has_transparent_hugepage() &&
4032 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
4033 return -EINVAL;
4034
4035 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07004036 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004037 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
4038 return count;
4039}
4040
4041struct kobj_attribute shmem_enabled_attr =
4042 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004043#endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004044
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004045#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004046bool shmem_huge_enabled(struct vm_area_struct *vma)
4047{
4048 struct inode *inode = file_inode(vma->vm_file);
4049 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4050 loff_t i_size;
4051 pgoff_t off;
4052
Yang Shic0630662019-07-18 15:57:27 -07004053 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
4054 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
4055 return false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004056 if (shmem_huge == SHMEM_HUGE_FORCE)
4057 return true;
4058 if (shmem_huge == SHMEM_HUGE_DENY)
4059 return false;
4060 switch (sbinfo->huge) {
4061 case SHMEM_HUGE_NEVER:
4062 return false;
4063 case SHMEM_HUGE_ALWAYS:
4064 return true;
4065 case SHMEM_HUGE_WITHIN_SIZE:
4066 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
4067 i_size = round_up(i_size_read(inode), PAGE_SIZE);
4068 if (i_size >= HPAGE_PMD_SIZE &&
4069 i_size >> PAGE_SHIFT >= off)
4070 return true;
Joe Perchese4a9bc52020-04-06 20:08:39 -07004071 fallthrough;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004072 case SHMEM_HUGE_ADVISE:
4073 /* TODO: implement fadvise() hints */
4074 return (vma->vm_flags & VM_HUGEPAGE);
4075 default:
4076 VM_BUG_ON(1);
4077 return false;
4078 }
4079}
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004080#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004081
Matt Mackall853ac432009-01-06 14:40:20 -08004082#else /* !CONFIG_SHMEM */
4083
4084/*
4085 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
4086 *
4087 * This is intended for small system where the benefits of the full
4088 * shmem code (swap-backed and resource-limited) are outweighed by
4089 * their complexity. On systems without swap this code should be
4090 * effectively equivalent, but much lighter weight.
4091 */
4092
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004093static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08004094 .name = "tmpfs",
David Howellsf3235622019-03-25 16:38:31 +00004095 .init_fs_context = ramfs_init_fs_context,
Al Virod7167b12019-09-07 07:23:15 -04004096 .parameters = ramfs_fs_parameters,
Matt Mackall853ac432009-01-06 14:40:20 -08004097 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004098 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08004099};
4100
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004101int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08004102{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004103 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08004104
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004105 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08004106 BUG_ON(IS_ERR(shm_mnt));
4107
4108 return 0;
4109}
4110
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08004111int shmem_unuse(unsigned int type, bool frontswap,
4112 unsigned long *fs_pages_to_unuse)
Matt Mackall853ac432009-01-06 14:40:20 -08004113{
4114 return 0;
4115}
4116
Hugh Dickins3f96b792009-09-21 17:03:37 -07004117int shmem_lock(struct file *file, int lock, struct user_struct *user)
4118{
4119 return 0;
4120}
4121
Hugh Dickins24513262012-01-20 14:34:21 -08004122void shmem_unlock_mapping(struct address_space *mapping)
4123{
4124}
4125
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07004126#ifdef CONFIG_MMU
4127unsigned long shmem_get_unmapped_area(struct file *file,
4128 unsigned long addr, unsigned long len,
4129 unsigned long pgoff, unsigned long flags)
4130{
4131 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4132}
4133#endif
4134
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004135void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07004136{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004137 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07004138}
4139EXPORT_SYMBOL_GPL(shmem_truncate_range);
4140
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004141#define shmem_vm_ops generic_file_vm_ops
4142#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03004143#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004144#define shmem_acct_size(flags, size) 0
4145#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08004146
4147#endif /* CONFIG_SHMEM */
4148
4149/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Matthew Auld703321b2017-10-06 23:18:13 +01004151static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00004152 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04004155 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156
Matthew Auld703321b2017-10-06 23:18:13 +01004157 if (IS_ERR(mnt))
4158 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159
Hugh Dickins285b2c42011-08-03 16:21:20 -07004160 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 return ERR_PTR(-EINVAL);
4162
4163 if (shmem_acct_size(flags, size))
4164 return ERR_PTR(-ENOMEM);
4165
Al Viro93dec2d2018-07-08 23:02:03 -04004166 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
4167 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04004168 if (unlikely(!inode)) {
4169 shmem_unacct_size(flags, size);
4170 return ERR_PTR(-ENOSPC);
4171 }
Eric Parisc7277092013-12-02 11:24:19 +00004172 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004174 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004175 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04004176 if (!IS_ERR(res))
4177 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
4178 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05004179 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04004180 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05004181 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182}
Eric Parisc7277092013-12-02 11:24:19 +00004183
4184/**
4185 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4186 * kernel internal. There will be NO LSM permission checks against the
4187 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004188 * higher layer. The users are the big_key and shm implementations. LSM
4189 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004190 * @name: name for dentry (to be seen in /proc/<pid>/maps
4191 * @size: size to be set for the file
4192 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4193 */
4194struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4195{
Matthew Auld703321b2017-10-06 23:18:13 +01004196 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00004197}
4198
4199/**
4200 * shmem_file_setup - get an unlinked file living in tmpfs
4201 * @name: name for dentry (to be seen in /proc/<pid>/maps
4202 * @size: size to be set for the file
4203 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4204 */
4205struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4206{
Matthew Auld703321b2017-10-06 23:18:13 +01004207 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00004208}
Keith Packard395e0dd2008-06-20 00:08:06 -07004209EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Randy Dunlap46711812008-03-19 17:00:41 -07004211/**
Matthew Auld703321b2017-10-06 23:18:13 +01004212 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4213 * @mnt: the tmpfs mount where the file will be created
4214 * @name: name for dentry (to be seen in /proc/<pid>/maps
4215 * @size: size to be set for the file
4216 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4217 */
4218struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4219 loff_t size, unsigned long flags)
4220{
4221 return __shmem_file_setup(mnt, name, size, flags, 0);
4222}
4223EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4224
4225/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 * shmem_zero_setup - setup a shared anonymous mapping
Peter Collingbourne45e55302020-08-06 23:23:37 -07004227 * @vma: the vma to be mmapped is prepared by do_mmap
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 */
4229int shmem_zero_setup(struct vm_area_struct *vma)
4230{
4231 struct file *file;
4232 loff_t size = vma->vm_end - vma->vm_start;
4233
Hugh Dickins66fc1302015-06-14 09:48:09 -07004234 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07004235 * Cloning a new file under mmap_lock leads to a lock ordering conflict
Hugh Dickins66fc1302015-06-14 09:48:09 -07004236 * between XFS directory reading and selinux: since this file is only
4237 * accessible to the user through its mapping, use S_PRIVATE flag to
4238 * bypass file security, in the same way as shmem_kernel_file_setup().
4239 */
Matthew Auld703321b2017-10-06 23:18:13 +01004240 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 if (IS_ERR(file))
4242 return PTR_ERR(file);
4243
4244 if (vma->vm_file)
4245 fput(vma->vm_file);
4246 vma->vm_file = file;
4247 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004248
Matthew Wilcox (Oracle)396bcc52020-04-06 20:04:35 -07004249 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004250 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4251 (vma->vm_end & HPAGE_PMD_MASK)) {
4252 khugepaged_enter(vma, vma->vm_flags);
4253 }
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return 0;
4256}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004257
4258/**
4259 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4260 * @mapping: the page's address_space
4261 * @index: the page index
4262 * @gfp: the page allocator flags to use if allocating
4263 *
4264 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4265 * with any new page allocations done using the specified allocation flags.
4266 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4267 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4268 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4269 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004270 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4271 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004272 */
4273struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4274 pgoff_t index, gfp_t gfp)
4275{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004276#ifdef CONFIG_SHMEM
4277 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004278 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004279 int error;
4280
4281 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004282 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004283 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004284 if (error)
4285 page = ERR_PTR(error);
4286 else
4287 unlock_page(page);
4288 return page;
4289#else
4290 /*
4291 * The tiny !SHMEM case uses ramfs without swap
4292 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004293 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004294#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004295}
4296EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);
Vinayak Menon9975da52021-03-18 16:49:18 +05304297
4298void shmem_mark_page_lazyfree(struct page *page)
4299{
4300 mark_page_lazyfree_movetail(page);
4301}
4302EXPORT_SYMBOL_GPL(shmem_mark_page_lazyfree);
Charan Teja Reddydaeabfe2021-06-28 16:00:03 +05304303
4304int reclaim_shmem_address_space(struct address_space *mapping)
4305{
4306 pgoff_t start = 0;
4307 struct page *page;
4308 LIST_HEAD(page_list);
4309 int reclaimed;
4310 XA_STATE(xas, &mapping->i_pages, start);
4311
4312 if (!shmem_mapping(mapping))
4313 return -EINVAL;
4314
4315 lru_add_drain();
4316
4317 rcu_read_lock();
4318 xas_for_each(&xas, page, ULONG_MAX) {
4319 if (xas_retry(&xas, page))
4320 continue;
4321 if (xa_is_value(page))
4322 continue;
4323 if (isolate_lru_page(page))
4324 continue;
4325
4326 list_add(&page->lru, &page_list);
4327 inc_node_page_state(page, NR_ISOLATED_ANON +
4328 page_is_file_lru(page));
4329
4330 if (need_resched()) {
4331 xas_pause(&xas);
4332 cond_resched_rcu();
4333 }
4334 }
4335 rcu_read_unlock();
4336 reclaimed = reclaim_pages_from_list(&page_list);
4337
4338 return reclaimed;
4339}
4340EXPORT_SYMBOL_GPL(reclaim_shmem_address_space);