blob: 8bca06c12b9a566909ce34975eed0c6874e8693d [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>
Matt Mackall853ac432009-01-06 14:40:20 -080040
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080041#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
42
Matt Mackall853ac432009-01-06 14:40:20 -080043static struct vfsmount *shm_mnt;
44
45#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * This virtual memory filesystem is heavily based on the ramfs. It
48 * extends ramfs by the ability to use swap and honor resource limits
49 * which makes it a completely usable filesystem.
50 */
51
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070052#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070053#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010054#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080055#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include <linux/string.h>
58#include <linux/slab.h>
59#include <linux/backing-dev.h>
60#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070063#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070064#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070065#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070066#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/security.h>
68#include <linux/swapops.h>
69#include <linux/mempolicy.h>
70#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000071#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070072#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070073#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080074#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040075#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070076#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070077#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070078#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080079#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080080#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030081#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070082
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080083#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include <asm/pgtable.h>
85
Mel Gormandd56b042015-11-06 16:28:43 -080086#include "internal.h"
87
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030088#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
89#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/* Pretend that each entry is of this size in directory's i_size */
92#define BOGO_DIRENT_SIZE 20
93
Hugh Dickins69f07ec2011-08-03 16:21:26 -070094/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
95#define SHORT_SYMLINK_LEN 128
96
Hugh Dickins1aac1402012-05-29 15:06:42 -070097/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070098 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
99 * inode->i_private (with i_mutex making sure that it has only one user at
100 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700101 */
102struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700103 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700104 pgoff_t start; /* start of range currently being fallocated */
105 pgoff_t next; /* the next page offset to be fallocated */
106 pgoff_t nr_falloced; /* how many new pages have been fallocated */
107 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
108};
109
Al Viro0b5071d2019-09-08 18:49:18 -0400110struct shmem_options {
111 unsigned long long blocks;
112 unsigned long long inodes;
113 struct mempolicy *mpol;
114 kuid_t uid;
115 kgid_t gid;
116 umode_t mode;
117 int huge;
118 int seen;
119#define SHMEM_SEEN_BLOCKS 1
120#define SHMEM_SEEN_INODES 2
121#define SHMEM_SEEN_HUGE 4
122};
123
Andrew Mortonb76db732008-02-08 04:21:49 -0800124#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800125static unsigned long shmem_default_max_blocks(void)
126{
Arun KSca79b0c2018-12-28 00:34:29 -0800127 return totalram_pages() / 2;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800128}
129
130static unsigned long shmem_default_max_inodes(void)
131{
Arun KSca79b0c2018-12-28 00:34:29 -0800132 unsigned long nr_pages = totalram_pages();
133
134 return min(nr_pages - totalhigh_pages(), nr_pages / 2);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800135}
Andrew Mortonb76db732008-02-08 04:21:49 -0800136#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800137
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700138static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
139static int shmem_replace_page(struct page **pagep, gfp_t gfp,
140 struct shmem_inode_info *info, pgoff_t index);
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -0800141static int shmem_swapin_page(struct inode *inode, pgoff_t index,
142 struct page **pagep, enum sgp_type sgp,
143 gfp_t gfp, struct vm_area_struct *vma,
144 vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700145static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700146 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800147 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700148 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700149
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700150int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700151 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700152{
153 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800154 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700155}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
158{
159 return sb->s_fs_info;
160}
161
162/*
163 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
164 * for shared memory and for shared anonymous (/dev/zero) mappings
165 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
166 * consistent with the pre-accounting of private mappings ...
167 */
168static inline int shmem_acct_size(unsigned long flags, loff_t size)
169{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000170 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000171 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static inline void shmem_unacct_size(unsigned long flags, loff_t size)
175{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000176 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 vm_unacct_memory(VM_ACCT(size));
178}
179
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700180static inline int shmem_reacct_size(unsigned long flags,
181 loff_t oldsize, loff_t newsize)
182{
183 if (!(flags & VM_NORESERVE)) {
184 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
185 return security_vm_enough_memory_mm(current->mm,
186 VM_ACCT(newsize) - VM_ACCT(oldsize));
187 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
188 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
189 }
190 return 0;
191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700195 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
197 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
198 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700199static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700201 if (!(flags & VM_NORESERVE))
202 return 0;
203
204 return security_vm_enough_memory_mm(current->mm,
205 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static inline void shmem_unacct_blocks(unsigned long flags, long pages)
209{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000210 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300211 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Mike Rapoport0f079692017-09-06 16:22:59 -0700214static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
215{
216 struct shmem_inode_info *info = SHMEM_I(inode);
217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218
219 if (shmem_acct_block(info->flags, pages))
220 return false;
221
222 if (sbinfo->max_blocks) {
223 if (percpu_counter_compare(&sbinfo->used_blocks,
224 sbinfo->max_blocks - pages) > 0)
225 goto unacct;
226 percpu_counter_add(&sbinfo->used_blocks, pages);
227 }
228
229 return true;
230
231unacct:
232 shmem_unacct_blocks(info->flags, pages);
233 return false;
234}
235
236static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
237{
238 struct shmem_inode_info *info = SHMEM_I(inode);
239 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
240
241 if (sbinfo->max_blocks)
242 percpu_counter_sub(&sbinfo->used_blocks, pages);
243 shmem_unacct_blocks(info->flags, pages);
244}
245
Hugh Dickins759b9772007-03-05 00:30:28 -0800246static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700247static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800248static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800249static const struct inode_operations shmem_inode_operations;
250static const struct inode_operations shmem_dir_inode_operations;
251static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +0400252static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700253static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Mike Rapoportb0506e42017-02-22 15:43:28 -0800255bool vma_is_shmem(struct vm_area_struct *vma)
256{
257 return vma->vm_ops == &shmem_vm_ops;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800261static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800263static int shmem_reserve_inode(struct super_block *sb)
264{
265 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
266 if (sbinfo->max_inodes) {
267 spin_lock(&sbinfo->stat_lock);
268 if (!sbinfo->free_inodes) {
269 spin_unlock(&sbinfo->stat_lock);
270 return -ENOSPC;
271 }
272 sbinfo->free_inodes--;
273 spin_unlock(&sbinfo->stat_lock);
274 }
275 return 0;
276}
277
278static void shmem_free_inode(struct super_block *sb)
279{
280 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
281 if (sbinfo->max_inodes) {
282 spin_lock(&sbinfo->stat_lock);
283 sbinfo->free_inodes++;
284 spin_unlock(&sbinfo->stat_lock);
285 }
286}
287
Randy Dunlap46711812008-03-19 17:00:41 -0700288/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700289 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * @inode: inode to recalc
291 *
292 * We have to calculate the free blocks since the mm can drop
293 * undirtied hole pages behind our back.
294 *
295 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
296 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
297 *
298 * It has to be called with the spinlock held.
299 */
300static void shmem_recalc_inode(struct inode *inode)
301{
302 struct shmem_inode_info *info = SHMEM_I(inode);
303 long freed;
304
305 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
306 if (freed > 0) {
307 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700308 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700309 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311}
312
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700313bool shmem_charge(struct inode *inode, long pages)
314{
315 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700316 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700317
Mike Rapoport0f079692017-09-06 16:22:59 -0700318 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700319 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700320
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800321 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
322 inode->i_mapping->nrpages += pages;
323
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700324 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700325 info->alloced += pages;
326 inode->i_blocks += pages * BLOCKS_PER_PAGE;
327 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700328 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700329
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700330 return true;
331}
332
333void shmem_uncharge(struct inode *inode, long pages)
334{
335 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700336 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700337
Hugh Dickinsaaa52e32018-11-30 14:10:29 -0800338 /* nrpages adjustment done by __delete_from_page_cache() or caller */
339
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700340 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700341 info->alloced -= pages;
342 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
343 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700344 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700345
Mike Rapoport0f079692017-09-06 16:22:59 -0700346 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700347}
348
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700349/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500350 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700351 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500352static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700353 pgoff_t index, void *expected, void *replacement)
354{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500355 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700356 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700357
358 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700359 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500360 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700361 if (item != expected)
362 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500363 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700364 return 0;
365}
366
367/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700368 * Sometimes, before we decide whether to proceed or to fail, we must check
369 * that an entry was not already brought back from swap by a racing thread.
370 *
371 * Checking page is not enough: by the time a SwapCache page is locked, it
372 * might be reused, and again be SwapCache, using the same swap as before.
373 */
374static bool shmem_confirm_swap(struct address_space *mapping,
375 pgoff_t index, swp_entry_t swap)
376{
Matthew Wilcoxa12831b2017-11-22 08:34:58 -0500377 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700378}
379
380/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700381 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
382 *
383 * SHMEM_HUGE_NEVER:
384 * disables huge pages for the mount;
385 * SHMEM_HUGE_ALWAYS:
386 * enables huge pages for the mount;
387 * SHMEM_HUGE_WITHIN_SIZE:
388 * only allocate huge pages if the page will be fully within i_size,
389 * also respect fadvise()/madvise() hints;
390 * SHMEM_HUGE_ADVISE:
391 * only allocate huge pages if requested with fadvise()/madvise();
392 */
393
394#define SHMEM_HUGE_NEVER 0
395#define SHMEM_HUGE_ALWAYS 1
396#define SHMEM_HUGE_WITHIN_SIZE 2
397#define SHMEM_HUGE_ADVISE 3
398
399/*
400 * Special values.
401 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
402 *
403 * SHMEM_HUGE_DENY:
404 * disables huge on shm_mnt and all mounts, for emergency use;
405 * SHMEM_HUGE_FORCE:
406 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
407 *
408 */
409#define SHMEM_HUGE_DENY (-1)
410#define SHMEM_HUGE_FORCE (-2)
411
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700412#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700413/* ifdef here to avoid bloating shmem.o when not necessary */
414
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700415static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700416
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700417#if defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700418static int shmem_parse_huge(const char *str)
419{
420 if (!strcmp(str, "never"))
421 return SHMEM_HUGE_NEVER;
422 if (!strcmp(str, "always"))
423 return SHMEM_HUGE_ALWAYS;
424 if (!strcmp(str, "within_size"))
425 return SHMEM_HUGE_WITHIN_SIZE;
426 if (!strcmp(str, "advise"))
427 return SHMEM_HUGE_ADVISE;
428 if (!strcmp(str, "deny"))
429 return SHMEM_HUGE_DENY;
430 if (!strcmp(str, "force"))
431 return SHMEM_HUGE_FORCE;
432 return -EINVAL;
433}
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700434#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700435
Arnd Bergmanne5f22492019-07-16 16:25:51 -0700436#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700437static const char *shmem_format_huge(int huge)
438{
439 switch (huge) {
440 case SHMEM_HUGE_NEVER:
441 return "never";
442 case SHMEM_HUGE_ALWAYS:
443 return "always";
444 case SHMEM_HUGE_WITHIN_SIZE:
445 return "within_size";
446 case SHMEM_HUGE_ADVISE:
447 return "advise";
448 case SHMEM_HUGE_DENY:
449 return "deny";
450 case SHMEM_HUGE_FORCE:
451 return "force";
452 default:
453 VM_BUG_ON(1);
454 return "bad_val";
455 }
456}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800457#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700458
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700459static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
460 struct shrink_control *sc, unsigned long nr_to_split)
461{
462 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800463 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700464 struct inode *inode;
465 struct shmem_inode_info *info;
466 struct page *page;
467 unsigned long batch = sc ? sc->nr_to_scan : 128;
468 int removed = 0, split = 0;
469
470 if (list_empty(&sbinfo->shrinklist))
471 return SHRINK_STOP;
472
473 spin_lock(&sbinfo->shrinklist_lock);
474 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
475 info = list_entry(pos, struct shmem_inode_info, shrinklist);
476
477 /* pin the inode */
478 inode = igrab(&info->vfs_inode);
479
480 /* inode is about to be evicted */
481 if (!inode) {
482 list_del_init(&info->shrinklist);
483 removed++;
484 goto next;
485 }
486
487 /* Check if there's anything to gain */
488 if (round_up(inode->i_size, PAGE_SIZE) ==
489 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800490 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700491 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700492 goto next;
493 }
494
495 list_move(&info->shrinklist, &list);
496next:
497 if (!--batch)
498 break;
499 }
500 spin_unlock(&sbinfo->shrinklist_lock);
501
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800502 list_for_each_safe(pos, next, &to_remove) {
503 info = list_entry(pos, struct shmem_inode_info, shrinklist);
504 inode = &info->vfs_inode;
505 list_del_init(&info->shrinklist);
506 iput(inode);
507 }
508
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700509 list_for_each_safe(pos, next, &list) {
510 int ret;
511
512 info = list_entry(pos, struct shmem_inode_info, shrinklist);
513 inode = &info->vfs_inode;
514
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700515 if (nr_to_split && split >= nr_to_split)
516 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700517
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700518 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700519 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
520 if (!page)
521 goto drop;
522
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700523 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700524 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700525 put_page(page);
526 goto drop;
527 }
528
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700529 /*
530 * Leave the inode on the list if we failed to lock
531 * the page at this time.
532 *
533 * Waiting for the lock may lead to deadlock in the
534 * reclaim path.
535 */
536 if (!trylock_page(page)) {
537 put_page(page);
538 goto leave;
539 }
540
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700541 ret = split_huge_page(page);
542 unlock_page(page);
543 put_page(page);
544
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700545 /* If split failed leave the inode on the list */
546 if (ret)
547 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700548
549 split++;
550drop:
551 list_del_init(&info->shrinklist);
552 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700553leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700554 iput(inode);
555 }
556
557 spin_lock(&sbinfo->shrinklist_lock);
558 list_splice_tail(&list, &sbinfo->shrinklist);
559 sbinfo->shrinklist_len -= removed;
560 spin_unlock(&sbinfo->shrinklist_lock);
561
562 return split;
563}
564
565static long shmem_unused_huge_scan(struct super_block *sb,
566 struct shrink_control *sc)
567{
568 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
569
570 if (!READ_ONCE(sbinfo->shrinklist_len))
571 return SHRINK_STOP;
572
573 return shmem_unused_huge_shrink(sbinfo, sc, 0);
574}
575
576static long shmem_unused_huge_count(struct super_block *sb,
577 struct shrink_control *sc)
578{
579 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
580 return READ_ONCE(sbinfo->shrinklist_len);
581}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700582#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700583
584#define shmem_huge SHMEM_HUGE_DENY
585
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700586static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
587 struct shrink_control *sc, unsigned long nr_to_split)
588{
589 return 0;
590}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700591#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700592
Yang Shi89fdcd22018-06-07 17:06:59 -0700593static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
594{
595 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
596 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
597 shmem_huge != SHMEM_HUGE_DENY)
598 return true;
599 return false;
600}
601
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700602/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700603 * Like add_to_page_cache_locked, but error if expected item has gone.
604 */
605static int shmem_add_to_page_cache(struct page *page,
606 struct address_space *mapping,
Matthew Wilcox552446a42017-12-01 13:25:14 -0500607 pgoff_t index, void *expected, gfp_t gfp)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700608{
Matthew Wilcox552446a42017-12-01 13:25:14 -0500609 XA_STATE_ORDER(xas, &mapping->i_pages, index, compound_order(page));
610 unsigned long i = 0;
611 unsigned long nr = 1UL << compound_order(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700612
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700613 VM_BUG_ON_PAGE(PageTail(page), page);
614 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800615 VM_BUG_ON_PAGE(!PageLocked(page), page);
616 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700617 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700618
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700619 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700620 page->mapping = mapping;
621 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700622
Matthew Wilcox552446a42017-12-01 13:25:14 -0500623 do {
624 void *entry;
625 xas_lock_irq(&xas);
626 entry = xas_find_conflict(&xas);
627 if (entry != expected)
628 xas_set_err(&xas, -EEXIST);
629 xas_create_range(&xas);
630 if (xas_error(&xas))
631 goto unlock;
632next:
Linus Torvalds69bf4b62019-07-05 19:55:18 -0700633 xas_store(&xas, page + i);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500634 if (++i < nr) {
635 xas_next(&xas);
636 goto next;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700637 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500638 if (PageTransHuge(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700639 count_vm_event(THP_FILE_ALLOC);
Mel Gorman11fb9982016-07-28 15:46:20 -0700640 __inc_node_page_state(page, NR_SHMEM_THPS);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500641 }
642 mapping->nrpages += nr;
Mel Gorman11fb9982016-07-28 15:46:20 -0700643 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
644 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500645unlock:
646 xas_unlock_irq(&xas);
647 } while (xas_nomem(&xas, gfp));
648
649 if (xas_error(&xas)) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700650 page->mapping = NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700651 page_ref_sub(page, nr);
Matthew Wilcox552446a42017-12-01 13:25:14 -0500652 return xas_error(&xas);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700653 }
Matthew Wilcox552446a42017-12-01 13:25:14 -0500654
655 return 0;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700656}
657
658/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700659 * Like delete_from_page_cache, but substitutes swap for page.
660 */
661static void shmem_delete_from_page_cache(struct page *page, void *radswap)
662{
663 struct address_space *mapping = page->mapping;
664 int error;
665
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700666 VM_BUG_ON_PAGE(PageCompound(page), page);
667
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700668 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500669 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700670 page->mapping = NULL;
671 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700672 __dec_node_page_state(page, NR_FILE_PAGES);
673 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700674 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300675 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700676 BUG_ON(error);
677}
678
679/*
Matthew Wilcoxc121d3b2017-12-04 03:13:54 -0500680 * Remove swap entry from page cache, free the swap and its page cache.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700681 */
682static int shmem_free_swap(struct address_space *mapping,
683 pgoff_t index, void *radswap)
684{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700685 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700686
Matthew Wilcox55f3f7e2018-11-26 16:08:43 -0500687 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700688 if (old != radswap)
689 return -ENOENT;
690 free_swap_and_cache(radix_to_swp_entry(radswap));
691 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700692}
693
694/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800695 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800696 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800697 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700698 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800699 * as long as the inode doesn't go away and racy results are not a problem.
700 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800701unsigned long shmem_partial_swap_usage(struct address_space *mapping,
702 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800703{
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500704 XA_STATE(xas, &mapping->i_pages, start);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800705 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800706 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800707
708 rcu_read_lock();
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500709 xas_for_each(&xas, page, end - 1) {
710 if (xas_retry(&xas, page))
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700711 continue;
Matthew Wilcox3159f942017-11-03 13:30:42 -0400712 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800713 swapped++;
714
715 if (need_resched()) {
Matthew Wilcox7ae34242017-12-04 03:28:00 -0500716 xas_pause(&xas);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800717 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800718 }
719 }
720
721 rcu_read_unlock();
722
723 return swapped << PAGE_SHIFT;
724}
725
726/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800727 * Determine (in bytes) how many of the shmem object's pages mapped by the
728 * given vma is swapped out.
729 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700730 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800731 * as long as the inode doesn't go away and racy results are not a problem.
732 */
733unsigned long shmem_swap_usage(struct vm_area_struct *vma)
734{
735 struct inode *inode = file_inode(vma->vm_file);
736 struct shmem_inode_info *info = SHMEM_I(inode);
737 struct address_space *mapping = inode->i_mapping;
738 unsigned long swapped;
739
740 /* Be careful as we don't hold info->lock */
741 swapped = READ_ONCE(info->swapped);
742
743 /*
744 * The easier cases are when the shmem object has nothing in swap, or
745 * the vma maps it whole. Then we can simply use the stats that we
746 * already track.
747 */
748 if (!swapped)
749 return 0;
750
751 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
752 return swapped << PAGE_SHIFT;
753
754 /* Here comes the more involved part */
755 return shmem_partial_swap_usage(mapping,
756 linear_page_index(vma, vma->vm_start),
757 linear_page_index(vma, vma->vm_end));
758}
759
760/*
Hugh Dickins24513262012-01-20 14:34:21 -0800761 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
762 */
763void shmem_unlock_mapping(struct address_space *mapping)
764{
765 struct pagevec pvec;
766 pgoff_t indices[PAGEVEC_SIZE];
767 pgoff_t index = 0;
768
Mel Gorman86679822017-11-15 17:37:52 -0800769 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800770 /*
771 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
772 */
773 while (!mapping_unevictable(mapping)) {
774 /*
775 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
776 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
777 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700778 pvec.nr = find_get_entries(mapping, index,
779 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800780 if (!pvec.nr)
781 break;
782 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700783 pagevec_remove_exceptionals(&pvec);
Kuo-Hsin Yang64e3d122018-11-06 13:23:24 +0000784 check_move_unevictable_pages(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800785 pagevec_release(&pvec);
786 cond_resched();
787 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700788}
789
790/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -0500791 * Remove range of pages and swap entries from page cache, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700792 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700793 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700794static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
795 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700797 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300799 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
800 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
801 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
802 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700803 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700804 pgoff_t indices[PAGEVEC_SIZE];
805 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700806 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700807 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700809 if (lend == -1)
810 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700811
Mel Gorman86679822017-11-15 17:37:52 -0800812 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700813 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700814 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700815 pvec.nr = find_get_entries(mapping, index,
816 min(end - index, (pgoff_t)PAGEVEC_SIZE),
817 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700818 if (!pvec.nr)
819 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700820 for (i = 0; i < pagevec_count(&pvec); i++) {
821 struct page *page = pvec.pages[i];
822
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700823 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700824 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700825 break;
826
Matthew Wilcox3159f942017-11-03 13:30:42 -0400827 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700828 if (unfalloc)
829 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700830 nr_swaps_freed += !shmem_free_swap(mapping,
831 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700832 continue;
833 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700834
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700835 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
836
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700837 if (!trylock_page(page))
838 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700839
840 if (PageTransTail(page)) {
841 /* Middle of THP: zero out the page */
842 clear_highpage(page);
843 unlock_page(page);
844 continue;
845 } else if (PageTransHuge(page)) {
846 if (index == round_down(end, HPAGE_PMD_NR)) {
847 /*
848 * Range ends in the middle of THP:
849 * zero out the page
850 */
851 clear_highpage(page);
852 unlock_page(page);
853 continue;
854 }
855 index += HPAGE_PMD_NR - 1;
856 i += HPAGE_PMD_NR - 1;
857 }
858
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700859 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700860 VM_BUG_ON_PAGE(PageTail(page), page);
861 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800862 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700863 truncate_inode_page(mapping, page);
864 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700865 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700866 unlock_page(page);
867 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700868 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800869 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700870 cond_resched();
871 index++;
872 }
873
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700874 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700875 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700876 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700877 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300878 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700879 if (start > end) {
880 top = partial_end;
881 partial_end = 0;
882 }
883 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700884 set_page_dirty(page);
885 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300886 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700887 }
888 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700889 if (partial_end) {
890 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700891 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700892 if (page) {
893 zero_user_segment(page, 0, partial_end);
894 set_page_dirty(page);
895 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300896 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700897 }
898 }
899 if (start >= end)
900 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700901
902 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700903 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700904 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700905
906 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700907 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700908 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700909 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700910 /* If all gone or hole-punch or unfalloc, we're done */
911 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700912 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700913 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700914 index = start;
915 continue;
916 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700917 for (i = 0; i < pagevec_count(&pvec); i++) {
918 struct page *page = pvec.pages[i];
919
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700920 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700921 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700922 break;
923
Matthew Wilcox3159f942017-11-03 13:30:42 -0400924 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700925 if (unfalloc)
926 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700927 if (shmem_free_swap(mapping, index, page)) {
928 /* Swap was replaced by page: retry */
929 index--;
930 break;
931 }
932 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700933 continue;
934 }
935
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700936 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700937
938 if (PageTransTail(page)) {
939 /* Middle of THP: zero out the page */
940 clear_highpage(page);
941 unlock_page(page);
942 /*
943 * Partial thp truncate due 'start' in middle
944 * of THP: don't need to look on these pages
945 * again on !pvec.nr restart.
946 */
947 if (index != round_down(end, HPAGE_PMD_NR))
948 start++;
949 continue;
950 } else if (PageTransHuge(page)) {
951 if (index == round_down(end, HPAGE_PMD_NR)) {
952 /*
953 * Range ends in the middle of THP:
954 * zero out the page
955 */
956 clear_highpage(page);
957 unlock_page(page);
958 continue;
959 }
960 index += HPAGE_PMD_NR - 1;
961 i += HPAGE_PMD_NR - 1;
962 }
963
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700964 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700965 VM_BUG_ON_PAGE(PageTail(page), page);
966 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800967 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700968 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700969 } else {
970 /* Page was replaced by swap: retry */
971 unlock_page(page);
972 index--;
973 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700974 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700975 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700976 unlock_page(page);
977 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700978 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800979 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700980 index++;
981 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700982
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700983 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700984 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700986 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700987}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700989void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
990{
991 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700992 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700994EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
David Howellsa528d352017-01-31 16:46:22 +0000996static int shmem_getattr(const struct path *path, struct kstat *stat,
997 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700998{
David Howellsa528d352017-01-31 16:46:22 +0000999 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001000 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001001 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001002
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001003 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001004 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001005 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001006 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001007 }
Yu Zhao44a30222015-09-08 15:03:33 -07001008 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001009
1010 if (is_huge_enabled(sb_info))
1011 stat->blksize = HPAGE_PMD_SIZE;
1012
Yu Zhao44a30222015-09-08 15:03:33 -07001013 return 0;
1014}
1015
Hugh Dickins94c1e622011-06-27 16:18:03 -07001016static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
David Howells75c3cfa2015-03-17 22:26:12 +00001018 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001019 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001020 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 int error;
1022
Jan Kara31051c82016-05-26 16:55:18 +02001023 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001024 if (error)
1025 return error;
1026
Hugh Dickins94c1e622011-06-27 16:18:03 -07001027 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1028 loff_t oldsize = inode->i_size;
1029 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001030
David Herrmann40e041a2014-08-08 14:25:27 -07001031 /* protected by i_mutex */
1032 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1033 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1034 return -EPERM;
1035
Hugh Dickins94c1e622011-06-27 16:18:03 -07001036 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001037 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1038 oldsize, newsize);
1039 if (error)
1040 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001041 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001042 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001043 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001044 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001045 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001046 if (oldsize > holebegin)
1047 unmap_mapping_range(inode->i_mapping,
1048 holebegin, 0, 1);
1049 if (info->alloced)
1050 shmem_truncate_range(inode,
1051 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001052 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001053 if (oldsize > holebegin)
1054 unmap_mapping_range(inode->i_mapping,
1055 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001056
1057 /*
1058 * Part of the huge page can be beyond i_size: subject
1059 * to shrink under memory pressure.
1060 */
1061 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1062 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001063 /*
1064 * _careful to defend against unlocked access to
1065 * ->shrink_list in shmem_unused_huge_shrink()
1066 */
1067 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001068 list_add_tail(&info->shrinklist,
1069 &sbinfo->shrinklist);
1070 sbinfo->shrinklist_len++;
1071 }
1072 spin_unlock(&sbinfo->shrinklist_lock);
1073 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001077 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001078 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001079 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return error;
1081}
1082
Al Viro1f895f72010-06-05 19:10:41 -04001083static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001086 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001088 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 shmem_unacct_size(info->flags, inode->i_size);
1090 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001091 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001092 if (!list_empty(&info->shrinklist)) {
1093 spin_lock(&sbinfo->shrinklist_lock);
1094 if (!list_empty(&info->shrinklist)) {
1095 list_del_init(&info->shrinklist);
1096 sbinfo->shrinklist_len--;
1097 }
1098 spin_unlock(&sbinfo->shrinklist_lock);
1099 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001100 while (!list_empty(&info->swaplist)) {
1101 /* Wait while shmem_unuse() is scanning this inode... */
1102 wait_var_event(&info->stop_eviction,
1103 !atomic_read(&info->stop_eviction));
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001104 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001105 /* ...but beware of the race if we peeked too early */
1106 if (!atomic_read(&info->stop_eviction))
1107 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001108 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Al Viro3ed47db2016-01-22 18:08:52 -05001110 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001111
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001112 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001113 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001114 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001115 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001118extern struct swap_info_struct *swap_info[];
1119
1120static int shmem_find_swap_entries(struct address_space *mapping,
1121 pgoff_t start, unsigned int nr_entries,
1122 struct page **entries, pgoff_t *indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001123 unsigned int type, bool frontswap)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001124{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001125 XA_STATE(xas, &mapping->i_pages, start);
1126 struct page *page;
Hugh Dickins87039542019-04-18 17:49:58 -07001127 swp_entry_t entry;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001128 unsigned int ret = 0;
1129
1130 if (!nr_entries)
1131 return 0;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001132
1133 rcu_read_lock();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001134 xas_for_each(&xas, page, ULONG_MAX) {
1135 if (xas_retry(&xas, page))
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001136 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001137
1138 if (!xa_is_value(page))
1139 continue;
1140
Hugh Dickins87039542019-04-18 17:49:58 -07001141 entry = radix_to_swp_entry(page);
1142 if (swp_type(entry) != type)
1143 continue;
1144 if (frontswap &&
1145 !frontswap_test(swap_info[type], swp_offset(entry)))
1146 continue;
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001147
1148 indices[ret] = xas.xa_index;
1149 entries[ret] = page;
1150
1151 if (need_resched()) {
1152 xas_pause(&xas);
1153 cond_resched_rcu();
1154 }
1155 if (++ret == nr_entries)
Matthew Wilcox478922e2016-12-14 15:08:52 -08001156 break;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001157 }
Matthew Wilcox478922e2016-12-14 15:08:52 -08001158 rcu_read_unlock();
Matthew Wilcoxe21a2952017-11-22 08:36:00 -05001159
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001160 return ret;
1161}
1162
1163/*
1164 * Move the swapped pages for an inode to page cache. Returns the count
1165 * of pages swapped in, or the error in case of failure.
1166 */
1167static int shmem_unuse_swap_entries(struct inode *inode, struct pagevec pvec,
1168 pgoff_t *indices)
1169{
1170 int i = 0;
1171 int ret = 0;
1172 int error = 0;
1173 struct address_space *mapping = inode->i_mapping;
1174
1175 for (i = 0; i < pvec.nr; i++) {
1176 struct page *page = pvec.pages[i];
1177
1178 if (!xa_is_value(page))
1179 continue;
1180 error = shmem_swapin_page(inode, indices[i],
1181 &page, SGP_CACHE,
1182 mapping_gfp_mask(mapping),
1183 NULL, NULL);
1184 if (error == 0) {
1185 unlock_page(page);
1186 put_page(page);
1187 ret++;
1188 }
1189 if (error == -ENOMEM)
1190 break;
1191 error = 0;
1192 }
1193 return error ? error : ret;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001194}
1195
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001196/*
1197 * If swap found in inode, free it and move page from swapcache to filecache.
1198 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001199static int shmem_unuse_inode(struct inode *inode, unsigned int type,
1200 bool frontswap, unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001202 struct address_space *mapping = inode->i_mapping;
1203 pgoff_t start = 0;
1204 struct pagevec pvec;
1205 pgoff_t indices[PAGEVEC_SIZE];
1206 bool frontswap_partial = (frontswap && *fs_pages_to_unuse > 0);
1207 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001209 pagevec_init(&pvec);
1210 do {
1211 unsigned int nr_entries = PAGEVEC_SIZE;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001212
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001213 if (frontswap_partial && *fs_pages_to_unuse < PAGEVEC_SIZE)
1214 nr_entries = *fs_pages_to_unuse;
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001215
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001216 pvec.nr = shmem_find_swap_entries(mapping, start, nr_entries,
1217 pvec.pages, indices,
Hugh Dickins87039542019-04-18 17:49:58 -07001218 type, frontswap);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001219 if (pvec.nr == 0) {
1220 ret = 0;
1221 break;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001222 }
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001223
1224 ret = shmem_unuse_swap_entries(inode, pvec, indices);
1225 if (ret < 0)
1226 break;
1227
1228 if (frontswap_partial) {
1229 *fs_pages_to_unuse -= ret;
1230 if (*fs_pages_to_unuse == 0) {
1231 ret = FRONTSWAP_PAGES_UNUSED;
1232 break;
1233 }
1234 }
1235
1236 start = indices[pvec.nr - 1];
1237 } while (true);
1238
1239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
1242/*
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001243 * Read all the shared memory data that resides in the swap
1244 * device 'type' back into memory, so the swap device can be
1245 * unused.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 */
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001247int shmem_unuse(unsigned int type, bool frontswap,
1248 unsigned long *fs_pages_to_unuse)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001250 struct shmem_inode_info *info, *next;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001251 int error = 0;
1252
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001253 if (list_empty(&shmem_swaplist))
1254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001256 mutex_lock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001257 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) {
1258 if (!info->swapped) {
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001259 list_del_init(&info->swaplist);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001260 continue;
1261 }
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001262 /*
1263 * Drop the swaplist mutex while searching the inode for swap;
1264 * but before doing so, make sure shmem_evict_inode() will not
1265 * remove placeholder inode from swaplist, nor let it be freed
1266 * (igrab() would protect from unlink, but not from unmount).
1267 */
1268 atomic_inc(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001269 mutex_unlock(&shmem_swaplist_mutex);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001270
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001271 error = shmem_unuse_inode(&info->vfs_inode, type, frontswap,
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001272 fs_pages_to_unuse);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001273 cond_resched();
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001274
1275 mutex_lock(&shmem_swaplist_mutex);
1276 next = list_next_entry(info, swaplist);
1277 if (!info->swapped)
1278 list_del_init(&info->swaplist);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07001279 if (atomic_dec_and_test(&info->stop_eviction))
1280 wake_up_var(&info->stop_eviction);
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001281 if (error)
Hugh Dickins778dd892011-05-11 15:13:37 -07001282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001284 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001285
Hugh Dickins778dd892011-05-11 15:13:37 -07001286 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289/*
1290 * Move the page from the page cache to the swap cache.
1291 */
1292static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1293{
1294 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001297 swp_entry_t swap;
1298 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001300 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 mapping = page->mapping;
1303 index = page->index;
1304 inode = mapping->host;
1305 info = SHMEM_I(inode);
1306 if (info->flags & VM_LOCKED)
1307 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001308 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 goto redirty;
1310
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001311 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001312 * Our capabilities prevent regular writeback or sync from ever calling
1313 * shmem_writepage; but a stacking filesystem might use ->writepage of
1314 * its underlying filesystem, in which case tmpfs should write out to
1315 * swap only in response to memory pressure, and not for the writeback
1316 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001317 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001318 if (!wbc->for_reclaim) {
1319 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1320 goto redirty;
1321 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001322
1323 /*
1324 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1325 * value into swapfile.c, the only way we can correctly account for a
1326 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001327 *
1328 * That's okay for a page already fallocated earlier, but if we have
1329 * not yet completed the fallocation, then (a) we want to keep track
1330 * of this page in case we have to undo it, and (b) it may not be a
1331 * good idea to continue anyway, once we're pushing into swap. So
1332 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001333 */
1334 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001335 if (inode->i_private) {
1336 struct shmem_falloc *shmem_falloc;
1337 spin_lock(&inode->i_lock);
1338 shmem_falloc = inode->i_private;
1339 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001340 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001341 index >= shmem_falloc->start &&
1342 index < shmem_falloc->next)
1343 shmem_falloc->nr_unswapped++;
1344 else
1345 shmem_falloc = NULL;
1346 spin_unlock(&inode->i_lock);
1347 if (shmem_falloc)
1348 goto redirty;
1349 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001350 clear_highpage(page);
1351 flush_dcache_page(page);
1352 SetPageUptodate(page);
1353 }
1354
Huang Ying38d8b4e2017-07-06 15:37:18 -07001355 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001356 if (!swap.val)
1357 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001358
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001359 /*
1360 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001361 * if it's not already there. Do it now before the page is
1362 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001363 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001364 * we've incremented swapped, because shmem_unuse_inode() will
1365 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001366 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001367 mutex_lock(&shmem_swaplist_mutex);
1368 if (list_empty(&info->swaplist))
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08001369 list_add(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001370
Hugh Dickins48f170f2011-07-25 17:12:37 -07001371 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001372 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001373 shmem_recalc_inode(inode);
1374 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001375 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001376
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001377 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001378 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1379
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001380 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001381 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001382 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 return 0;
1384 }
1385
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001386 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001387 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388redirty:
1389 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001390 if (wbc->for_reclaim)
1391 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1392 unlock_page(page);
1393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
1395
Hugh Dickins75edd342016-05-19 17:12:44 -07001396#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001397static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001398{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001399 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001400
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001401 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001402 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001403
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001404 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001405
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001406 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001407}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001408
1409static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1410{
1411 struct mempolicy *mpol = NULL;
1412 if (sbinfo->mpol) {
1413 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1414 mpol = sbinfo->mpol;
1415 mpol_get(mpol);
1416 spin_unlock(&sbinfo->stat_lock);
1417 }
1418 return mpol;
1419}
Hugh Dickins75edd342016-05-19 17:12:44 -07001420#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1421static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1422{
1423}
1424static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1425{
1426 return NULL;
1427}
1428#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1429#ifndef CONFIG_NUMA
1430#define vm_policy vm_private_data
1431#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001432
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001433static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1434 struct shmem_inode_info *info, pgoff_t index)
1435{
1436 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001437 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001438 /* Bias interleave by inode number to distribute better across nodes */
1439 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001440 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1441}
1442
1443static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1444{
1445 /* Drop reference taken by mpol_shared_policy_lookup() */
1446 mpol_cond_put(vma->vm_policy);
1447}
1448
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001449static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1450 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001453 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001454 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001456 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001457 vmf.vma = &pvma;
1458 vmf.address = 0;
1459 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001460 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001461
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001462 return page;
1463}
Mel Gorman18a2f372012-12-05 14:01:41 -08001464
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001465static struct page *shmem_alloc_hugepage(gfp_t gfp,
1466 struct shmem_inode_info *info, pgoff_t index)
1467{
1468 struct vm_area_struct pvma;
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001469 struct address_space *mapping = info->vfs_inode.i_mapping;
1470 pgoff_t hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001471 struct page *page;
1472
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001473 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001474 return NULL;
1475
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001476 hindex = round_down(index, HPAGE_PMD_NR);
Matthew Wilcox7b8d0462017-12-01 22:13:06 -05001477 if (xa_find(&mapping->i_pages, &hindex, hindex + HPAGE_PMD_NR - 1,
1478 XA_PRESENT))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001479 return NULL;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001480
1481 shmem_pseudo_vma_init(&pvma, info, hindex);
1482 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
Andrea Arcangeli92717d42019-08-13 15:37:50 -07001483 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id());
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001484 shmem_pseudo_vma_destroy(&pvma);
1485 if (page)
1486 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001487 return page;
1488}
1489
1490static struct page *shmem_alloc_page(gfp_t gfp,
1491 struct shmem_inode_info *info, pgoff_t index)
1492{
1493 struct vm_area_struct pvma;
1494 struct page *page;
1495
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001496 shmem_pseudo_vma_init(&pvma, info, index);
1497 page = alloc_page_vma(gfp, &pvma, 0);
1498 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001499
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001500 return page;
1501}
1502
1503static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001504 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001505 pgoff_t index, bool huge)
1506{
Mike Rapoport0f079692017-09-06 16:22:59 -07001507 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001508 struct page *page;
1509 int nr;
1510 int err = -ENOSPC;
1511
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001512 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001513 huge = false;
1514 nr = huge ? HPAGE_PMD_NR : 1;
1515
Mike Rapoport0f079692017-09-06 16:22:59 -07001516 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001517 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001518
1519 if (huge)
1520 page = shmem_alloc_hugepage(gfp, info, index);
1521 else
1522 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001523 if (page) {
1524 __SetPageLocked(page);
1525 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001526 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001527 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001528
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001529 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001530 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001531failed:
1532 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001536 * When a page is moved from swapcache to shmem filecache (either by the
1537 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1538 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1539 * ignorance of the mapping it belongs to. If that mapping has special
1540 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1541 * we may need to copy to a suitable page before moving to filecache.
1542 *
1543 * In a future release, this may well be extended to respect cpuset and
1544 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1545 * but for now it is a simple matter of zone.
1546 */
1547static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1548{
1549 return page_zonenum(page) > gfp_zone(gfp);
1550}
1551
1552static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1553 struct shmem_inode_info *info, pgoff_t index)
1554{
1555 struct page *oldpage, *newpage;
1556 struct address_space *swap_mapping;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001557 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001558 pgoff_t swap_index;
1559 int error;
1560
1561 oldpage = *pagep;
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001562 entry.val = page_private(oldpage);
1563 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001564 swap_mapping = page_mapping(oldpage);
1565
1566 /*
1567 * We have arrived here because our zones are constrained, so don't
1568 * limit chance of success by further cpuset and node constraints.
1569 */
1570 gfp &= ~GFP_CONSTRAINT_MASK;
1571 newpage = shmem_alloc_page(gfp, info, index);
1572 if (!newpage)
1573 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001574
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001575 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001576 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001577 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001578
Hugh Dickins9956edf2016-11-10 10:46:11 -08001579 __SetPageLocked(newpage);
1580 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001581 SetPageUptodate(newpage);
Yu Zhaoc1cb20d2018-11-30 14:09:03 -08001582 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001583 SetPageSwapCache(newpage);
1584
1585 /*
1586 * Our caller will very soon move newpage out of swapcache, but it's
1587 * a nice clean interface for us to replace oldpage by newpage there.
1588 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001589 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001590 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001591 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001592 __inc_node_page_state(newpage, NR_FILE_PAGES);
1593 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001594 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001595 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001596
Hugh Dickins0142ef62012-06-07 14:21:09 -07001597 if (unlikely(error)) {
1598 /*
1599 * Is this possible? I think not, now that our callers check
1600 * both PageSwapCache and page_private after getting page lock;
1601 * but be defensive. Reverse old to newpage for clear and free.
1602 */
1603 oldpage = newpage;
1604 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001605 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001606 lru_cache_add_anon(newpage);
1607 *pagep = newpage;
1608 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001609
1610 ClearPageSwapCache(oldpage);
1611 set_page_private(oldpage, 0);
1612
1613 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001614 put_page(oldpage);
1615 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001616 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001617}
1618
1619/*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001620 * Swap in the page pointed to by *pagep.
1621 * Caller has to make sure that *pagep contains a valid swapped page.
1622 * Returns 0 and the page in pagep if success. On failure, returns the
1623 * the error code and NULL in *pagep.
1624 */
1625static int shmem_swapin_page(struct inode *inode, pgoff_t index,
1626 struct page **pagep, enum sgp_type sgp,
1627 gfp_t gfp, struct vm_area_struct *vma,
1628 vm_fault_t *fault_type)
1629{
1630 struct address_space *mapping = inode->i_mapping;
1631 struct shmem_inode_info *info = SHMEM_I(inode);
1632 struct mm_struct *charge_mm = vma ? vma->vm_mm : current->mm;
1633 struct mem_cgroup *memcg;
1634 struct page *page;
1635 swp_entry_t swap;
1636 int error;
1637
1638 VM_BUG_ON(!*pagep || !xa_is_value(*pagep));
1639 swap = radix_to_swp_entry(*pagep);
1640 *pagep = NULL;
1641
1642 /* Look it up and read it in.. */
1643 page = lookup_swap_cache(swap, NULL, 0);
1644 if (!page) {
1645 /* Or update major stats only when swapin succeeds?? */
1646 if (fault_type) {
1647 *fault_type |= VM_FAULT_MAJOR;
1648 count_vm_event(PGMAJFAULT);
1649 count_memcg_event_mm(charge_mm, PGMAJFAULT);
1650 }
1651 /* Here we actually start the io */
1652 page = shmem_swapin(swap, gfp, info, index);
1653 if (!page) {
1654 error = -ENOMEM;
1655 goto failed;
1656 }
1657 }
1658
1659 /* We have to do this with page locked to prevent races */
1660 lock_page(page);
1661 if (!PageSwapCache(page) || page_private(page) != swap.val ||
1662 !shmem_confirm_swap(mapping, index, swap)) {
1663 error = -EEXIST;
1664 goto unlock;
1665 }
1666 if (!PageUptodate(page)) {
1667 error = -EIO;
1668 goto failed;
1669 }
1670 wait_on_page_writeback(page);
1671
1672 if (shmem_should_replace_page(page, gfp)) {
1673 error = shmem_replace_page(&page, gfp, info, index);
1674 if (error)
1675 goto failed;
1676 }
1677
1678 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1679 false);
1680 if (!error) {
1681 error = shmem_add_to_page_cache(page, mapping, index,
1682 swp_to_radix_entry(swap), gfp);
1683 /*
1684 * We already confirmed swap under page lock, and make
1685 * no memory allocation here, so usually no possibility
1686 * of error; but free_swap_and_cache() only trylocks a
1687 * page, so it is just possible that the entry has been
1688 * truncated or holepunched since swap was confirmed.
1689 * shmem_undo_range() will have done some of the
1690 * unaccounting, now delete_from_swap_cache() will do
1691 * the rest.
1692 */
1693 if (error) {
1694 mem_cgroup_cancel_charge(page, memcg, false);
1695 delete_from_swap_cache(page);
1696 }
1697 }
1698 if (error)
1699 goto failed;
1700
1701 mem_cgroup_commit_charge(page, memcg, true, false);
1702
1703 spin_lock_irq(&info->lock);
1704 info->swapped--;
1705 shmem_recalc_inode(inode);
1706 spin_unlock_irq(&info->lock);
1707
1708 if (sgp == SGP_WRITE)
1709 mark_page_accessed(page);
1710
1711 delete_from_swap_cache(page);
1712 set_page_dirty(page);
1713 swap_free(swap);
1714
1715 *pagep = page;
1716 return 0;
1717failed:
1718 if (!shmem_confirm_swap(mapping, index, swap))
1719 error = -EEXIST;
1720unlock:
1721 if (page) {
1722 unlock_page(page);
1723 put_page(page);
1724 }
1725
1726 return error;
1727}
1728
1729/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001730 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *
1732 * If we allocate a new one we do not mark it dirty. That's up to the
1733 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001734 * entry since a page cannot live in both the swap and page cache.
1735 *
1736 * fault_mm and fault_type are only supplied by shmem_fault:
1737 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001739static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001740 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001741 struct vm_area_struct *vma, struct vm_fault *vmf,
1742 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001745 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001747 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001748 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001749 struct page *page;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001750 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001751 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001753 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001754 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001756 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001758 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1759 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760repeat:
Hugh Dickins75edd342016-05-19 17:12:44 -07001761 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001762 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001763 return -EINVAL;
1764 }
1765
1766 sbinfo = SHMEM_SB(inode->i_sb);
1767 charge_mm = vma ? vma->vm_mm : current->mm;
1768
1769 page = find_lock_entry(mapping, index);
1770 if (xa_is_value(page)) {
1771 error = shmem_swapin_page(inode, index, &page,
1772 sgp, gfp, vma, fault_type);
1773 if (error == -EEXIST)
1774 goto repeat;
1775
1776 *pagep = page;
1777 return error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001778 }
1779
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001780 if (page && sgp == SGP_WRITE)
1781 mark_page_accessed(page);
1782
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001783 /* fallocated page? */
1784 if (page && !PageUptodate(page)) {
1785 if (sgp != SGP_READ)
1786 goto clear;
1787 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001788 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001789 page = NULL;
1790 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001791 if (page || sgp == SGP_READ) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001792 *pagep = page;
1793 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001797 * Fast cache lookup did not find it:
1798 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 */
Hugh Dickins27ab7002011-07-25 17:12:36 -07001800
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001801 if (vma && userfaultfd_missing(vma)) {
1802 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1803 return 0;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001806 /* shmem_symlink() */
1807 if (mapping->a_ops != &shmem_aops)
1808 goto alloc_nohuge;
1809 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
1810 goto alloc_nohuge;
1811 if (shmem_huge == SHMEM_HUGE_FORCE)
1812 goto alloc_huge;
1813 switch (sbinfo->huge) {
1814 loff_t i_size;
1815 pgoff_t off;
1816 case SHMEM_HUGE_NEVER:
1817 goto alloc_nohuge;
1818 case SHMEM_HUGE_WITHIN_SIZE:
1819 off = round_up(index, HPAGE_PMD_NR);
1820 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1821 if (i_size >= HPAGE_PMD_SIZE &&
1822 i_size >> PAGE_SHIFT >= off)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001823 goto alloc_huge;
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001824 /* fallthrough */
1825 case SHMEM_HUGE_ADVISE:
1826 if (sgp_huge == SGP_HUGE)
1827 goto alloc_huge;
1828 /* TODO: implement fadvise() hints */
1829 goto alloc_nohuge;
1830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001832alloc_huge:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001833 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
1834 if (IS_ERR(page)) {
1835alloc_nohuge:
1836 page = shmem_alloc_and_acct_page(gfp, inode,
1837 index, false);
1838 }
1839 if (IS_ERR(page)) {
1840 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001841
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001842 error = PTR_ERR(page);
1843 page = NULL;
1844 if (error != -ENOSPC)
1845 goto unlock;
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001846 /*
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001847 * Try to reclaim some space by splitting a huge page
1848 * beyond i_size on the filesystem.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001849 */
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001850 while (retry--) {
1851 int ret;
1852
1853 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1854 if (ret == SHRINK_STOP)
1855 break;
1856 if (ret)
1857 goto alloc_nohuge;
1858 }
1859 goto unlock;
1860 }
1861
1862 if (PageTransHuge(page))
1863 hindex = round_down(index, HPAGE_PMD_NR);
1864 else
1865 hindex = index;
1866
1867 if (sgp == SGP_WRITE)
1868 __SetPageReferenced(page);
1869
1870 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
1871 PageTransHuge(page));
1872 if (error)
1873 goto unacct;
1874 error = shmem_add_to_page_cache(page, mapping, hindex,
1875 NULL, gfp & GFP_RECLAIM_MASK);
1876 if (error) {
1877 mem_cgroup_cancel_charge(page, memcg,
1878 PageTransHuge(page));
1879 goto unacct;
1880 }
1881 mem_cgroup_commit_charge(page, memcg, false,
1882 PageTransHuge(page));
1883 lru_cache_add_anon(page);
1884
1885 spin_lock_irq(&info->lock);
1886 info->alloced += 1 << compound_order(page);
1887 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
1888 shmem_recalc_inode(inode);
1889 spin_unlock_irq(&info->lock);
1890 alloced = true;
1891
1892 if (PageTransHuge(page) &&
1893 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1894 hindex + HPAGE_PMD_NR - 1) {
1895 /*
1896 * Part of the huge page is beyond i_size: subject
1897 * to shrink under memory pressure.
1898 */
1899 spin_lock(&sbinfo->shrinklist_lock);
1900 /*
1901 * _careful to defend against unlocked access to
1902 * ->shrink_list in shmem_unused_huge_shrink()
1903 */
1904 if (list_empty_careful(&info->shrinklist)) {
1905 list_add_tail(&info->shrinklist,
1906 &sbinfo->shrinklist);
1907 sbinfo->shrinklist_len++;
1908 }
1909 spin_unlock(&sbinfo->shrinklist_lock);
1910 }
1911
1912 /*
1913 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1914 */
1915 if (sgp == SGP_FALLOC)
1916 sgp = SGP_WRITE;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001917clear:
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001918 /*
1919 * Let SGP_WRITE caller clear ends if write does not fill page;
1920 * but SGP_FALLOC on a page fallocated earlier must initialize
1921 * it now, lest undo on failure cancel our earlier guarantee.
1922 */
1923 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1924 struct page *head = compound_head(page);
1925 int i;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001926
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001927 for (i = 0; i < (1 << compound_order(head)); i++) {
1928 clear_highpage(head + i);
1929 flush_dcache_page(head + i);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001930 }
Vineeth Remanan Pillaic5bf1212019-03-05 15:46:58 -08001931 SetPageUptodate(head);
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001932 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001933
Hugh Dickins54af60422011-08-03 16:21:24 -07001934 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001935 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001936 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001937 if (alloced) {
1938 ClearPageDirty(page);
1939 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001940 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001941 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001942 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001943 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001944 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001945 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001946 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001947 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001948 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001949
Nick Piggind0217ac2007-07-19 01:47:03 -07001950 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001951 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001953unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001954 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001955
1956 if (PageTransHuge(page)) {
1957 unlock_page(page);
1958 put_page(page);
1959 goto alloc_nohuge;
1960 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001961unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001962 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001963 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001964 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001965 }
1966 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001967 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001968 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001969 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001970 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001971 }
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05001972 if (error == -EEXIST)
Hugh Dickins54af60422011-08-03 16:21:24 -07001973 goto repeat;
1974 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975}
1976
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001977/*
1978 * This is like autoremove_wake_function, but it removes the wait queue
1979 * entry unconditionally - even if something else had already woken the
1980 * target.
1981 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001982static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001983{
1984 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001985 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001986 return ret;
1987}
1988
Souptick Joarder20acce62018-06-07 17:09:17 -07001989static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
Dave Jiang11bac802017-02-24 14:56:41 -08001991 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001992 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001993 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001994 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001995 int err;
1996 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001998 /*
1999 * Trinity finds that probing a hole which tmpfs is punching can
2000 * prevent the hole-punch from ever completing: which in turn
2001 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07002002 * faulting pages into the hole while it's being punched. Although
2003 * shmem_undo_range() does remove the additions, it may be unable to
2004 * keep up, as each new page needs its own unmap_mapping_range() call,
2005 * and the i_mmap tree grows ever slower to scan if new vmas are added.
2006 *
2007 * It does not matter if we sometimes reach this check just before the
2008 * hole-punch begins, so that one fault then races with the punch:
2009 * we just need to make racing faults a rare case.
2010 *
2011 * The implementation below would be much simpler if we just used a
2012 * standard mutex or completion: but we cannot take i_mutex in fault,
2013 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002014 */
2015 if (unlikely(inode->i_private)) {
2016 struct shmem_falloc *shmem_falloc;
2017
2018 spin_lock(&inode->i_lock);
2019 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002020 if (shmem_falloc &&
2021 shmem_falloc->waitq &&
2022 vmf->pgoff >= shmem_falloc->start &&
2023 vmf->pgoff < shmem_falloc->next) {
2024 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08002025 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002026
2027 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002028 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
2029 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07002030 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002031 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002032 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002033 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002034
2035 shmem_falloc_waitq = shmem_falloc->waitq;
2036 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2037 TASK_UNINTERRUPTIBLE);
2038 spin_unlock(&inode->i_lock);
2039 schedule();
2040
2041 /*
2042 * shmem_falloc_waitq points into the shmem_fallocate()
2043 * stack of the hole-punching task: shmem_falloc_waitq
2044 * is usually invalid by the time we reach here, but
2045 * finish_wait() does not dereference it in that case;
2046 * though i_lock needed lest racing with wake_up_all().
2047 */
2048 spin_lock(&inode->i_lock);
2049 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2050 spin_unlock(&inode->i_lock);
2051 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002052 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002053 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002054 }
2055
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002056 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002057
2058 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2059 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002060 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002061 else if (vma->vm_flags & VM_HUGEPAGE)
2062 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002063
Souptick Joarder20acce62018-06-07 17:09:17 -07002064 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002065 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002066 if (err)
2067 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002068 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002071unsigned long shmem_get_unmapped_area(struct file *file,
2072 unsigned long uaddr, unsigned long len,
2073 unsigned long pgoff, unsigned long flags)
2074{
2075 unsigned long (*get_area)(struct file *,
2076 unsigned long, unsigned long, unsigned long, unsigned long);
2077 unsigned long addr;
2078 unsigned long offset;
2079 unsigned long inflated_len;
2080 unsigned long inflated_addr;
2081 unsigned long inflated_offset;
2082
2083 if (len > TASK_SIZE)
2084 return -ENOMEM;
2085
2086 get_area = current->mm->get_unmapped_area;
2087 addr = get_area(file, uaddr, len, pgoff, flags);
2088
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002089 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002090 return addr;
2091 if (IS_ERR_VALUE(addr))
2092 return addr;
2093 if (addr & ~PAGE_MASK)
2094 return addr;
2095 if (addr > TASK_SIZE - len)
2096 return addr;
2097
2098 if (shmem_huge == SHMEM_HUGE_DENY)
2099 return addr;
2100 if (len < HPAGE_PMD_SIZE)
2101 return addr;
2102 if (flags & MAP_FIXED)
2103 return addr;
2104 /*
2105 * Our priority is to support MAP_SHARED mapped hugely;
2106 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2107 * But if caller specified an address hint, respect that as before.
2108 */
2109 if (uaddr)
2110 return addr;
2111
2112 if (shmem_huge != SHMEM_HUGE_FORCE) {
2113 struct super_block *sb;
2114
2115 if (file) {
2116 VM_BUG_ON(file->f_op != &shmem_file_operations);
2117 sb = file_inode(file)->i_sb;
2118 } else {
2119 /*
2120 * Called directly from mm/mmap.c, or drivers/char/mem.c
2121 * for "/dev/zero", to create a shared anonymous object.
2122 */
2123 if (IS_ERR(shm_mnt))
2124 return addr;
2125 sb = shm_mnt->mnt_sb;
2126 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002127 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002128 return addr;
2129 }
2130
2131 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2132 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2133 return addr;
2134 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2135 return addr;
2136
2137 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2138 if (inflated_len > TASK_SIZE)
2139 return addr;
2140 if (inflated_len < len)
2141 return addr;
2142
2143 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2144 if (IS_ERR_VALUE(inflated_addr))
2145 return addr;
2146 if (inflated_addr & ~PAGE_MASK)
2147 return addr;
2148
2149 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2150 inflated_addr += offset - inflated_offset;
2151 if (inflated_offset > offset)
2152 inflated_addr += HPAGE_PMD_SIZE;
2153
2154 if (inflated_addr > TASK_SIZE - len)
2155 return addr;
2156 return inflated_addr;
2157}
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002160static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Al Viro496ad9a2013-01-23 17:07:38 -05002162 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002163 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002166static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2167 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Al Viro496ad9a2013-01-23 17:07:38 -05002169 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002170 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002172 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2173 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175#endif
2176
2177int shmem_lock(struct file *file, int lock, struct user_struct *user)
2178{
Al Viro496ad9a2013-01-23 17:07:38 -05002179 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 struct shmem_inode_info *info = SHMEM_I(inode);
2181 int retval = -ENOMEM;
2182
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002183 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if (lock && !(info->flags & VM_LOCKED)) {
2185 if (!user_shm_lock(inode->i_size, user))
2186 goto out_nomem;
2187 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002188 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190 if (!lock && (info->flags & VM_LOCKED) && user) {
2191 user_shm_unlock(inode->i_size, user);
2192 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002193 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
2195 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002198 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return retval;
2200}
2201
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002202static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203{
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002204 struct shmem_inode_info *info = SHMEM_I(file_inode(file));
2205
2206 if (info->seals & F_SEAL_FUTURE_WRITE) {
2207 /*
2208 * New PROT_WRITE and MAP_SHARED mmaps are not allowed when
2209 * "future write" seal active.
2210 */
2211 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_WRITE))
2212 return -EPERM;
2213
2214 /*
2215 * Since the F_SEAL_FUTURE_WRITE seals allow for a MAP_SHARED
2216 * read-only mapping, take care to not allow mprotect to revert
2217 * protections.
2218 */
2219 vma->vm_flags &= ~(VM_MAYWRITE);
2220 }
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 file_accessed(file);
2223 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002224 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002225 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2226 (vma->vm_end & HPAGE_PMD_MASK)) {
2227 khugepaged_enter(vma, vma->vm_flags);
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
2230}
2231
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002232static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002233 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 struct inode *inode;
2236 struct shmem_inode_info *info;
2237 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2238
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002239 if (shmem_reserve_inode(sb))
2240 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
2242 inode = new_inode(sb);
2243 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002244 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002245 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002247 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002248 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 info = SHMEM_I(inode);
2250 memset(info, 0, (char *)inode - (char *)info);
2251 spin_lock_init(&info->lock);
Hugh Dickinsaf53d3e2019-04-18 17:50:13 -07002252 atomic_set(&info->stop_eviction, 0);
David Herrmann40e041a2014-08-08 14:25:27 -07002253 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002254 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002255 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002257 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002258 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 switch (mode & S_IFMT) {
2261 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002262 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 init_special_inode(inode, mode, dev);
2264 break;
2265 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002266 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 inode->i_op = &shmem_inode_operations;
2268 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002269 mpol_shared_policy_init(&info->policy,
2270 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 break;
2272 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002273 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /* Some things misbehave if size == 0 on a directory */
2275 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2276 inode->i_op = &shmem_dir_inode_operations;
2277 inode->i_fop = &simple_dir_operations;
2278 break;
2279 case S_IFLNK:
2280 /*
2281 * Must not load anything in the rbtree,
2282 * mpol_free_shared_policy will not be called.
2283 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002284 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 break;
2286 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002287
2288 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002289 } else
2290 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return inode;
2292}
2293
Johannes Weiner0cd61442014-04-03 14:47:46 -07002294bool shmem_mapping(struct address_space *mapping)
2295{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002296 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002297}
2298
Mike Rapoport8d103962017-09-06 16:23:02 -07002299static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2300 pmd_t *dst_pmd,
2301 struct vm_area_struct *dst_vma,
2302 unsigned long dst_addr,
2303 unsigned long src_addr,
2304 bool zeropage,
2305 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002306{
2307 struct inode *inode = file_inode(dst_vma->vm_file);
2308 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002309 struct address_space *mapping = inode->i_mapping;
2310 gfp_t gfp = mapping_gfp_mask(mapping);
2311 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2312 struct mem_cgroup *memcg;
2313 spinlock_t *ptl;
2314 void *page_kaddr;
2315 struct page *page;
2316 pte_t _dst_pte, *dst_pte;
2317 int ret;
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002318 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002319
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002320 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002321 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002322 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002323
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002324 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002325 page = shmem_alloc_page(gfp, info, pgoff);
2326 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002327 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002328
Mike Rapoport8d103962017-09-06 16:23:02 -07002329 if (!zeropage) { /* mcopy_atomic */
2330 page_kaddr = kmap_atomic(page);
2331 ret = copy_from_user(page_kaddr,
2332 (const void __user *)src_addr,
2333 PAGE_SIZE);
2334 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002335
Mike Rapoport8d103962017-09-06 16:23:02 -07002336 /* fallback to copy_from_user outside mmap_sem */
2337 if (unlikely(ret)) {
2338 *pagep = page;
2339 shmem_inode_unacct_blocks(inode, 1);
2340 /* don't free the page */
Andrea Arcangeli9e368252018-11-30 14:09:25 -08002341 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002342 }
2343 } else { /* mfill_zeropage_atomic */
2344 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002345 }
2346 } else {
2347 page = *pagep;
2348 *pagep = NULL;
2349 }
2350
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002351 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2352 __SetPageLocked(page);
2353 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002354 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002355
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002356 ret = -EFAULT;
2357 offset = linear_page_index(dst_vma, dst_addr);
2358 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2359 if (unlikely(offset >= max_off))
2360 goto out_release;
2361
Tejun Heo2cf85582018-07-03 11:14:56 -04002362 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002363 if (ret)
2364 goto out_release;
2365
Matthew Wilcox552446a42017-12-01 13:25:14 -05002366 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL,
2367 gfp & GFP_RECLAIM_MASK);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002368 if (ret)
2369 goto out_release_uncharge;
2370
2371 mem_cgroup_commit_charge(page, memcg, false, false);
2372
2373 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2374 if (dst_vma->vm_flags & VM_WRITE)
2375 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002376 else {
2377 /*
2378 * We don't set the pte dirty if the vma has no
2379 * VM_WRITE permission, so mark the page dirty or it
2380 * could be freed from under us. We could do it
2381 * unconditionally before unlock_page(), but doing it
2382 * only if VM_WRITE is not set is faster.
2383 */
2384 set_page_dirty(page);
2385 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002386
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002387 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002388
2389 ret = -EFAULT;
2390 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2391 if (unlikely(offset >= max_off))
2392 goto out_release_uncharge_unlock;
2393
2394 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002395 if (!pte_none(*dst_pte))
2396 goto out_release_uncharge_unlock;
2397
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002398 lru_cache_add_anon(page);
2399
2400 spin_lock(&info->lock);
2401 info->alloced++;
2402 inode->i_blocks += BLOCKS_PER_PAGE;
2403 shmem_recalc_inode(inode);
2404 spin_unlock(&info->lock);
2405
2406 inc_mm_counter(dst_mm, mm_counter_file(page));
2407 page_add_file_rmap(page, false);
2408 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2409
2410 /* No need to invalidate - it was non-present before */
2411 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002412 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002413 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002414 ret = 0;
2415out:
2416 return ret;
2417out_release_uncharge_unlock:
2418 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangelidcf7fe92018-11-30 14:09:43 -08002419 ClearPageDirty(page);
Andrea Arcangelie2a50c12018-11-30 14:09:37 -08002420 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002421out_release_uncharge:
2422 mem_cgroup_cancel_charge(page, memcg, false);
2423out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002424 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002425 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002426out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002427 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002428 goto out;
2429}
2430
Mike Rapoport8d103962017-09-06 16:23:02 -07002431int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2432 pmd_t *dst_pmd,
2433 struct vm_area_struct *dst_vma,
2434 unsigned long dst_addr,
2435 unsigned long src_addr,
2436 struct page **pagep)
2437{
2438 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2439 dst_addr, src_addr, false, pagep);
2440}
2441
2442int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2443 pmd_t *dst_pmd,
2444 struct vm_area_struct *dst_vma,
2445 unsigned long dst_addr)
2446{
2447 struct page *page = NULL;
2448
2449 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2450 dst_addr, 0, true, &page);
2451}
2452
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002454static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002455static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002457#ifdef CONFIG_TMPFS_XATTR
2458static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2459#else
2460#define shmem_initxattrs NULL
2461#endif
2462
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002464shmem_write_begin(struct file *file, struct address_space *mapping,
2465 loff_t pos, unsigned len, unsigned flags,
2466 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Nick Piggin800d15a2007-10-16 01:25:03 -07002468 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002469 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002470 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002471
2472 /* i_mutex is held by caller */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002473 if (unlikely(info->seals & (F_SEAL_GROW |
2474 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) {
2475 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))
David Herrmann40e041a2014-08-08 14:25:27 -07002476 return -EPERM;
2477 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2478 return -EPERM;
2479 }
2480
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002481 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002482}
2483
2484static int
2485shmem_write_end(struct file *file, struct address_space *mapping,
2486 loff_t pos, unsigned len, unsigned copied,
2487 struct page *page, void *fsdata)
2488{
2489 struct inode *inode = mapping->host;
2490
Hugh Dickinsd3602442008-02-04 22:28:44 -08002491 if (pos + copied > inode->i_size)
2492 i_size_write(inode, pos + copied);
2493
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002494 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002495 struct page *head = compound_head(page);
2496 if (PageTransCompound(page)) {
2497 int i;
2498
2499 for (i = 0; i < HPAGE_PMD_NR; i++) {
2500 if (head + i == page)
2501 continue;
2502 clear_highpage(head + i);
2503 flush_dcache_page(head + i);
2504 }
2505 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002506 if (copied < PAGE_SIZE) {
2507 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002508 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002509 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002510 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002511 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002512 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002513 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002514 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002515 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002516
Nick Piggin800d15a2007-10-16 01:25:03 -07002517 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Al Viro2ba5bbe2014-04-02 20:00:02 -04002520static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Al Viro6e58e792014-02-03 17:07:03 -05002522 struct file *file = iocb->ki_filp;
2523 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002525 pgoff_t index;
2526 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002527 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002528 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002529 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002530 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002531
2532 /*
2533 * Might this read be for a stacking filesystem? Then when reading
2534 * holes of a sparse file, we actually need to allocate those pages,
2535 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2536 */
Al Viro777eda22014-12-17 04:46:46 -05002537 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002538 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002540 index = *ppos >> PAGE_SHIFT;
2541 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
2543 for (;;) {
2544 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002545 pgoff_t end_index;
2546 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 loff_t i_size = i_size_read(inode);
2548
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002549 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (index > end_index)
2551 break;
2552 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002553 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (nr <= offset)
2555 break;
2556 }
2557
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002558 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002559 if (error) {
2560 if (error == -EINVAL)
2561 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 break;
2563 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002564 if (page) {
2565 if (sgp == SGP_CACHE)
2566 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002567 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
2570 /*
2571 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002572 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002574 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002576 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002578 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 if (nr <= offset) {
2580 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002581 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 break;
2583 }
2584 }
2585 nr -= offset;
2586
2587 if (page) {
2588 /*
2589 * If users can be writing to this page using arbitrary
2590 * virtual addresses, take care about potential aliasing
2591 * before reading the page on the kernel side.
2592 */
2593 if (mapping_writably_mapped(mapping))
2594 flush_dcache_page(page);
2595 /*
2596 * Mark the page accessed if we read the beginning.
2597 */
2598 if (!offset)
2599 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002600 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002602 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
2605 /*
2606 * Ok, we have the page, and it's up-to-date, so
2607 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002609 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002610 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002612 index += offset >> PAGE_SHIFT;
2613 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002615 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002616 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 break;
Al Viro6e58e792014-02-03 17:07:03 -05002618 if (ret < nr) {
2619 error = -EFAULT;
2620 break;
2621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 cond_resched();
2623 }
2624
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002625 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002626 file_accessed(file);
2627 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002630/*
Matthew Wilcox7f4446e2017-12-04 03:31:13 -05002631 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002632 */
2633static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002634 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002635{
2636 struct page *page;
2637 struct pagevec pvec;
2638 pgoff_t indices[PAGEVEC_SIZE];
2639 bool done = false;
2640 int i;
2641
Mel Gorman86679822017-11-15 17:37:52 -08002642 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002643 pvec.nr = 1; /* start small: we may be there already */
2644 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002645 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002646 pvec.nr, pvec.pages, indices);
2647 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002648 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002649 index = end;
2650 break;
2651 }
2652 for (i = 0; i < pvec.nr; i++, index++) {
2653 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002654 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002655 done = true;
2656 break;
2657 }
2658 index = indices[i];
2659 }
2660 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002661 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002662 if (!PageUptodate(page))
2663 page = NULL;
2664 }
2665 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002666 (page && whence == SEEK_DATA) ||
2667 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002668 done = true;
2669 break;
2670 }
2671 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002672 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002673 pagevec_release(&pvec);
2674 pvec.nr = PAGEVEC_SIZE;
2675 cond_resched();
2676 }
2677 return index;
2678}
2679
Andrew Morton965c8e52012-12-17 15:59:39 -08002680static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002681{
2682 struct address_space *mapping = file->f_mapping;
2683 struct inode *inode = mapping->host;
2684 pgoff_t start, end;
2685 loff_t new_offset;
2686
Andrew Morton965c8e52012-12-17 15:59:39 -08002687 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2688 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002689 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002690 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002691 /* We're holding i_mutex so we can access i_size directly */
2692
Yufen Yu1a413642018-11-16 15:08:39 -08002693 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002694 offset = -ENXIO;
2695 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002696 start = offset >> PAGE_SHIFT;
2697 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002698 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002699 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002700 if (new_offset > offset) {
2701 if (new_offset < inode->i_size)
2702 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002703 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002704 offset = -ENXIO;
2705 else
2706 offset = inode->i_size;
2707 }
2708 }
2709
Hugh Dickins387aae62013-08-04 11:30:25 -07002710 if (offset >= 0)
2711 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002712 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002713 return offset;
2714}
2715
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002716static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2717 loff_t len)
2718{
Al Viro496ad9a2013-01-23 17:07:38 -05002719 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002720 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002721 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002722 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002723 pgoff_t start, index, end;
2724 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002725
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002726 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2727 return -EOPNOTSUPP;
2728
Al Viro59551022016-01-22 15:40:57 -05002729 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002730
2731 if (mode & FALLOC_FL_PUNCH_HOLE) {
2732 struct address_space *mapping = file->f_mapping;
2733 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2734 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002735 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002736
David Herrmann40e041a2014-08-08 14:25:27 -07002737 /* protected by i_mutex */
Joel Fernandes (Google)ab3948f2019-03-05 15:47:54 -08002738 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) {
David Herrmann40e041a2014-08-08 14:25:27 -07002739 error = -EPERM;
2740 goto out;
2741 }
2742
Hugh Dickins8e205f72014-07-23 14:00:10 -07002743 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002744 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2745 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2746 spin_lock(&inode->i_lock);
2747 inode->i_private = &shmem_falloc;
2748 spin_unlock(&inode->i_lock);
2749
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002750 if ((u64)unmap_end > (u64)unmap_start)
2751 unmap_mapping_range(mapping, unmap_start,
2752 1 + unmap_end - unmap_start, 0);
2753 shmem_truncate_range(inode, offset, offset + len - 1);
2754 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002755
2756 spin_lock(&inode->i_lock);
2757 inode->i_private = NULL;
2758 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002759 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002760 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002761 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002762 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002763 }
2764
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002765 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2766 error = inode_newsize_ok(inode, offset + len);
2767 if (error)
2768 goto out;
2769
David Herrmann40e041a2014-08-08 14:25:27 -07002770 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2771 error = -EPERM;
2772 goto out;
2773 }
2774
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002775 start = offset >> PAGE_SHIFT;
2776 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002777 /* Try to avoid a swapstorm if len is impossible to satisfy */
2778 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2779 error = -ENOSPC;
2780 goto out;
2781 }
2782
Hugh Dickins8e205f72014-07-23 14:00:10 -07002783 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002784 shmem_falloc.start = start;
2785 shmem_falloc.next = start;
2786 shmem_falloc.nr_falloced = 0;
2787 shmem_falloc.nr_unswapped = 0;
2788 spin_lock(&inode->i_lock);
2789 inode->i_private = &shmem_falloc;
2790 spin_unlock(&inode->i_lock);
2791
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002792 for (index = start; index < end; index++) {
2793 struct page *page;
2794
2795 /*
2796 * Good, the fallocate(2) manpage permits EINTR: we may have
2797 * been interrupted because we are using up too much memory.
2798 */
2799 if (signal_pending(current))
2800 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002801 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2802 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002803 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002804 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002805 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002806 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002807 if (index > start) {
2808 shmem_undo_range(inode,
2809 (loff_t)start << PAGE_SHIFT,
2810 ((loff_t)index << PAGE_SHIFT) - 1, true);
2811 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002812 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002813 }
2814
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002815 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002816 * Inform shmem_writepage() how far we have reached.
2817 * No need for lock or barrier: we have the page lock.
2818 */
2819 shmem_falloc.next++;
2820 if (!PageUptodate(page))
2821 shmem_falloc.nr_falloced++;
2822
2823 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002824 * If !PageUptodate, leave it that way so that freeable pages
2825 * can be recognized if we need to rollback on error later.
2826 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002827 * than free the pages we are allocating (and SGP_CACHE pages
2828 * might still be clean: we now need to mark those dirty too).
2829 */
2830 set_page_dirty(page);
2831 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002832 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002833 cond_resched();
2834 }
2835
2836 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2837 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002838 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002839undone:
2840 spin_lock(&inode->i_lock);
2841 inode->i_private = NULL;
2842 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002843out:
Al Viro59551022016-01-22 15:40:57 -05002844 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002845 return error;
2846}
2847
David Howells726c3342006-06-23 02:02:58 -07002848static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
David Howells726c3342006-06-23 02:02:58 -07002850 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002853 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002855 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002857 buf->f_bavail =
2858 buf->f_bfree = sbinfo->max_blocks -
2859 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002860 }
2861 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 buf->f_files = sbinfo->max_inodes;
2863 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865 /* else leave those fields 0 like simple_statfs */
2866 return 0;
2867}
2868
2869/*
2870 * File creation. Allocate an inode, and we're done..
2871 */
2872static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002873shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002875 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 int error = -ENOSPC;
2877
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002878 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002880 error = simple_acl_create(dir, inode);
2881 if (error)
2882 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002883 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002884 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002885 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002886 if (error && error != -EOPNOTSUPP)
2887 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002888
Al Viro718deb62009-12-16 19:35:36 -05002889 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002891 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 d_instantiate(dentry, inode);
2893 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
2895 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002896out_iput:
2897 iput(inode);
2898 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
Al Viro60545d02013-06-07 01:20:27 -04002901static int
2902shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2903{
2904 struct inode *inode;
2905 int error = -ENOSPC;
2906
2907 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2908 if (inode) {
2909 error = security_inode_init_security(inode, dir,
2910 NULL,
2911 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002912 if (error && error != -EOPNOTSUPP)
2913 goto out_iput;
2914 error = simple_acl_create(dir, inode);
2915 if (error)
2916 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002917 d_tmpfile(dentry, inode);
2918 }
2919 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002920out_iput:
2921 iput(inode);
2922 return error;
Al Viro60545d02013-06-07 01:20:27 -04002923}
2924
Al Viro18bb1db2011-07-26 01:41:39 -04002925static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926{
2927 int error;
2928
2929 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2930 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002931 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return 0;
2933}
2934
Al Viro4acdaf22011-07-26 01:42:34 -04002935static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002936 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2939}
2940
2941/*
2942 * Link a file..
2943 */
2944static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2945{
David Howells75c3cfa2015-03-17 22:26:12 +00002946 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong29b00e602019-02-22 22:35:32 -08002947 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 /*
2950 * No ordinary (disk based) filesystem counts links as inodes;
2951 * but each new link needs a new dentry, pinning lowmem, and
2952 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong1062af92019-02-21 08:48:09 -08002953 * But if an O_TMPFILE file is linked into the tmpfs, the
2954 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 */
Darrick J. Wong1062af92019-02-21 08:48:09 -08002956 if (inode->i_nlink) {
2957 ret = shmem_reserve_inode(inode->i_sb);
2958 if (ret)
2959 goto out;
2960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002963 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002964 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002965 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 dget(dentry); /* Extra pinning count for the created dentry */
2967 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002968out:
2969 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970}
2971
2972static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2973{
David Howells75c3cfa2015-03-17 22:26:12 +00002974 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002976 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2977 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002980 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002981 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 dput(dentry); /* Undo the count from "create" - this does all the work */
2983 return 0;
2984}
2985
2986static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2987{
2988 if (!simple_empty(dentry))
2989 return -ENOTEMPTY;
2990
David Howells75c3cfa2015-03-17 22:26:12 +00002991 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002992 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 return shmem_unlink(dir, dentry);
2994}
2995
Miklos Szeredi37456772014-07-23 15:15:34 +02002996static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2997{
David Howellse36cb0b2015-01-29 12:02:35 +00002998 bool old_is_dir = d_is_dir(old_dentry);
2999 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02003000
3001 if (old_dir != new_dir && old_is_dir != new_is_dir) {
3002 if (old_is_dir) {
3003 drop_nlink(old_dir);
3004 inc_nlink(new_dir);
3005 } else {
3006 drop_nlink(new_dir);
3007 inc_nlink(old_dir);
3008 }
3009 }
3010 old_dir->i_ctime = old_dir->i_mtime =
3011 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00003012 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003013 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02003014
3015 return 0;
3016}
3017
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003018static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
3019{
3020 struct dentry *whiteout;
3021 int error;
3022
3023 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3024 if (!whiteout)
3025 return -ENOMEM;
3026
3027 error = shmem_mknod(old_dir, whiteout,
3028 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3029 dput(whiteout);
3030 if (error)
3031 return error;
3032
3033 /*
3034 * Cheat and hash the whiteout while the old dentry is still in
3035 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3036 *
3037 * d_lookup() will consistently find one of them at this point,
3038 * not sure which one, but that isn't even important.
3039 */
3040 d_rehash(whiteout);
3041 return 0;
3042}
3043
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044/*
3045 * The VFS layer already does all the dentry stuff for rename,
3046 * we just have to decrement the usage count for the target if
3047 * it exists so that the VFS layer correctly free's it when it
3048 * gets overwritten.
3049 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003050static 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 -07003051{
David Howells75c3cfa2015-03-17 22:26:12 +00003052 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 int they_are_dirs = S_ISDIR(inode->i_mode);
3054
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003055 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003056 return -EINVAL;
3057
Miklos Szeredi37456772014-07-23 15:15:34 +02003058 if (flags & RENAME_EXCHANGE)
3059 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 if (!simple_empty(new_dentry))
3062 return -ENOTEMPTY;
3063
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003064 if (flags & RENAME_WHITEOUT) {
3065 int error;
3066
3067 error = shmem_whiteout(old_dir, old_dentry);
3068 if (error)
3069 return error;
3070 }
3071
David Howells75c3cfa2015-03-17 22:26:12 +00003072 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003074 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003075 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003076 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003079 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003080 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 }
3082
3083 old_dir->i_size -= BOGO_DIRENT_SIZE;
3084 new_dir->i_size += BOGO_DIRENT_SIZE;
3085 old_dir->i_ctime = old_dir->i_mtime =
3086 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003087 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 return 0;
3089}
3090
3091static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3092{
3093 int error;
3094 int len;
3095 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003096 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097
3098 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003099 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 return -ENAMETOOLONG;
3101
Joe Perches0825a6f2018-06-14 15:27:58 -07003102 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3103 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 if (!inode)
3105 return -ENOSPC;
3106
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003107 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003108 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003109 if (error) {
3110 if (error != -EOPNOTSUPP) {
3111 iput(inode);
3112 return error;
3113 }
3114 error = 0;
3115 }
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003118 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003119 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3120 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003121 iput(inode);
3122 return -ENOMEM;
3123 }
3124 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003126 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003127 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (error) {
3129 iput(inode);
3130 return error;
3131 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003132 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003134 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003135 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003137 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003138 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003141 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 d_instantiate(dentry, inode);
3143 dget(dentry);
3144 return 0;
3145}
3146
Al Virofceef392015-12-29 15:58:39 -05003147static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Al Virofceef392015-12-29 15:58:39 -05003149 mark_page_accessed(arg);
3150 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
Al Viro6b255392015-11-17 10:20:54 -05003153static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003154 struct inode *inode,
3155 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003158 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003159 if (!dentry) {
3160 page = find_get_page(inode->i_mapping, 0);
3161 if (!page)
3162 return ERR_PTR(-ECHILD);
3163 if (!PageUptodate(page)) {
3164 put_page(page);
3165 return ERR_PTR(-ECHILD);
3166 }
3167 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003168 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003169 if (error)
3170 return ERR_PTR(error);
3171 unlock_page(page);
3172 }
Al Virofceef392015-12-29 15:58:39 -05003173 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003174 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
Eric Parisb09e0fa2011-05-24 17:12:39 -07003177#ifdef CONFIG_TMPFS_XATTR
3178/*
3179 * Superblocks without xattr inode operations may get some security.* xattr
3180 * support from the LSM "for free". As soon as we have any other xattrs
3181 * like ACLs, we also need to implement the security.* handlers at
3182 * filesystem level, though.
3183 */
3184
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003185/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003186 * Callback for security_inode_init_security() for acquiring xattrs.
3187 */
3188static int shmem_initxattrs(struct inode *inode,
3189 const struct xattr *xattr_array,
3190 void *fs_info)
3191{
3192 struct shmem_inode_info *info = SHMEM_I(inode);
3193 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003194 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003195 size_t len;
3196
3197 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003198 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003199 if (!new_xattr)
3200 return -ENOMEM;
3201
3202 len = strlen(xattr->name) + 1;
3203 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3204 GFP_KERNEL);
3205 if (!new_xattr->name) {
3206 kfree(new_xattr);
3207 return -ENOMEM;
3208 }
3209
3210 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3211 XATTR_SECURITY_PREFIX_LEN);
3212 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3213 xattr->name, len);
3214
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003215 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003216 }
3217
3218 return 0;
3219}
3220
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003221static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003222 struct dentry *unused, struct inode *inode,
3223 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003224{
Al Virob2968212016-04-10 20:48:24 -04003225 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003226
3227 name = xattr_full_name(handler, name);
3228 return simple_xattr_get(&info->xattrs, name, buffer, size);
3229}
3230
3231static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003232 struct dentry *unused, struct inode *inode,
3233 const char *name, const void *value,
3234 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003235{
Al Viro59301222016-05-27 10:19:30 -04003236 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003237
3238 name = xattr_full_name(handler, name);
3239 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3240}
3241
3242static const struct xattr_handler shmem_security_xattr_handler = {
3243 .prefix = XATTR_SECURITY_PREFIX,
3244 .get = shmem_xattr_handler_get,
3245 .set = shmem_xattr_handler_set,
3246};
3247
3248static const struct xattr_handler shmem_trusted_xattr_handler = {
3249 .prefix = XATTR_TRUSTED_PREFIX,
3250 .get = shmem_xattr_handler_get,
3251 .set = shmem_xattr_handler_set,
3252};
3253
Eric Parisb09e0fa2011-05-24 17:12:39 -07003254static const struct xattr_handler *shmem_xattr_handlers[] = {
3255#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003256 &posix_acl_access_xattr_handler,
3257 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003258#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003259 &shmem_security_xattr_handler,
3260 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003261 NULL
3262};
3263
Eric Parisb09e0fa2011-05-24 17:12:39 -07003264static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3265{
David Howells75c3cfa2015-03-17 22:26:12 +00003266 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003267 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003268}
3269#endif /* CONFIG_TMPFS_XATTR */
3270
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003271static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003272 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003273#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003274 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003275#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276};
3277
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003278static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003279 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003280#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003281 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003282#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003283};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003284
David M. Grimes91828a42006-10-17 00:09:45 -07003285static struct dentry *shmem_get_parent(struct dentry *child)
3286{
3287 return ERR_PTR(-ESTALE);
3288}
3289
3290static int shmem_match(struct inode *ino, void *vfh)
3291{
3292 __u32 *fh = vfh;
3293 __u64 inum = fh[2];
3294 inum = (inum << 32) | fh[1];
3295 return ino->i_ino == inum && fh[0] == ino->i_generation;
3296}
3297
Amir Goldstein12ba7802018-06-07 17:07:15 -07003298/* Find any alias of inode, but prefer a hashed alias */
3299static struct dentry *shmem_find_alias(struct inode *inode)
3300{
3301 struct dentry *alias = d_find_alias(inode);
3302
3303 return alias ?: d_find_any_alias(inode);
3304}
3305
3306
Christoph Hellwig480b1162007-10-21 16:42:13 -07003307static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3308 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003309{
David M. Grimes91828a42006-10-17 00:09:45 -07003310 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003311 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003312 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003313
Christoph Hellwig480b1162007-10-21 16:42:13 -07003314 if (fh_len < 3)
3315 return NULL;
3316
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003317 inum = fid->raw[2];
3318 inum = (inum << 32) | fid->raw[1];
3319
Christoph Hellwig480b1162007-10-21 16:42:13 -07003320 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3321 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003322 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003323 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003324 iput(inode);
3325 }
3326
Christoph Hellwig480b1162007-10-21 16:42:13 -07003327 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003328}
3329
Al Virob0b03822012-04-02 14:34:06 -04003330static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3331 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003332{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303333 if (*len < 3) {
3334 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003335 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303336 }
David M. Grimes91828a42006-10-17 00:09:45 -07003337
Al Viro1d3382cb2010-10-23 15:19:20 -04003338 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003339 /* Unfortunately insert_inode_hash is not idempotent,
3340 * so as we hash inodes here rather than at creation
3341 * time, we need a lock to ensure we only try
3342 * to do it once
3343 */
3344 static DEFINE_SPINLOCK(lock);
3345 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003346 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003347 __insert_inode_hash(inode,
3348 inode->i_ino + inode->i_generation);
3349 spin_unlock(&lock);
3350 }
3351
3352 fh[0] = inode->i_generation;
3353 fh[1] = inode->i_ino;
3354 fh[2] = ((__u64)inode->i_ino) >> 32;
3355
3356 *len = 3;
3357 return 1;
3358}
3359
Christoph Hellwig39655162007-10-21 16:42:17 -07003360static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003361 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003362 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003363 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003364};
3365
Al Viro0b5071d2019-09-08 18:49:18 -04003366static int shmem_parse_options(char *options, struct shmem_options *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
3368 char *this_char, *value, *rest;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003369 uid_t uid;
3370 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003372 while (options != NULL) {
3373 this_char = options;
3374 for (;;) {
3375 /*
3376 * NUL-terminate this option: unfortunately,
3377 * mount options form a comma-separated list,
3378 * but mpol's nodelist may also contain commas.
3379 */
3380 options = strchr(options, ',');
3381 if (options == NULL)
3382 break;
3383 options++;
3384 if (!isdigit(*options)) {
3385 options[-1] = '\0';
3386 break;
3387 }
3388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (!*this_char)
3390 continue;
3391 if ((value = strchr(this_char,'=')) != NULL) {
3392 *value++ = 0;
3393 } else {
Joe Perches11705322016-03-17 14:19:50 -07003394 pr_err("tmpfs: No value for mount option '%s'\n",
3395 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003396 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398
3399 if (!strcmp(this_char,"size")) {
3400 unsigned long long size;
3401 size = memparse(value,&rest);
3402 if (*rest == '%') {
3403 size <<= PAGE_SHIFT;
Arun KSca79b0c2018-12-28 00:34:29 -08003404 size *= totalram_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 do_div(size, 100);
3406 rest++;
3407 }
3408 if (*rest)
3409 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003410 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE);
3411 ctx->seen |= SHMEM_SEEN_BLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 } else if (!strcmp(this_char,"nr_blocks")) {
Al Viro0b5071d2019-09-08 18:49:18 -04003413 ctx->blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if (*rest)
3415 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003416 ctx->seen |= SHMEM_SEEN_BLOCKS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 } else if (!strcmp(this_char,"nr_inodes")) {
Al Viro0b5071d2019-09-08 18:49:18 -04003418 ctx->inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 if (*rest)
3420 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003421 ctx->seen |= SHMEM_SEEN_INODES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 } else if (!strcmp(this_char,"mode")) {
Al Viro0b5071d2019-09-08 18:49:18 -04003423 ctx->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 if (*rest)
3425 goto bad_val;
3426 } else if (!strcmp(this_char,"uid")) {
Eric W. Biederman8751e032012-02-07 16:46:12 -08003427 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 if (*rest)
3429 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003430 ctx->uid = make_kuid(current_user_ns(), uid);
3431 if (!uid_valid(ctx->uid))
Eric W. Biederman8751e032012-02-07 16:46:12 -08003432 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 } else if (!strcmp(this_char,"gid")) {
Eric W. Biederman8751e032012-02-07 16:46:12 -08003434 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 if (*rest)
3436 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003437 ctx->gid = make_kgid(current_user_ns(), gid);
3438 if (!gid_valid(ctx->gid))
Eric W. Biederman8751e032012-02-07 16:46:12 -08003439 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003440#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003441 } else if (!strcmp(this_char, "huge")) {
3442 int huge;
3443 huge = shmem_parse_huge(value);
3444 if (huge < 0)
3445 goto bad_val;
3446 if (!has_transparent_hugepage() &&
3447 huge != SHMEM_HUGE_NEVER)
3448 goto bad_val;
Al Viro0b5071d2019-09-08 18:49:18 -04003449 ctx->huge = huge;
3450 ctx->seen |= SHMEM_SEEN_HUGE;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003451#endif
3452#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003453 } else if (!strcmp(this_char,"mpol")) {
Al Virof6490b72019-09-08 19:11:46 -04003454 mpol_put(ctx->mpol);
3455 ctx->mpol = NULL;
3456 if (mpol_parse_str(value, &ctx->mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003457 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 } else {
Joe Perches11705322016-03-17 14:19:50 -07003460 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003461 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 }
3463 }
3464 return 0;
3465
3466bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003467 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003469error:
Al Virof6490b72019-09-08 19:11:46 -04003470 mpol_put(ctx->mpol);
3471 ctx->mpol = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 return 1;
3473
3474}
3475
3476static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3477{
3478 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
Al Viro0b5071d2019-09-08 18:49:18 -04003479 struct shmem_options ctx = {.seen = 0};
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003480 unsigned long inodes;
3481 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Al Viro0b5071d2019-09-08 18:49:18 -04003483 if (shmem_parse_options(data, &ctx))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003484 return error;
3485
3486 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003487 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003488 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003489 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003490 * but we must separately disallow unlimited->limited, because
3491 * in that case we have no record of how much is already in use.
3492 */
Al Viro0b5071d2019-09-08 18:49:18 -04003493 if ((ctx.seen & SHMEM_SEEN_BLOCKS) && ctx.blocks) {
3494 if (!sbinfo->max_blocks)
3495 goto out;
3496 if (percpu_counter_compare(&sbinfo->used_blocks,
3497 ctx.blocks) > 0)
3498 goto out;
3499 }
3500 if ((ctx.seen & SHMEM_SEEN_INODES) && ctx.inodes) {
3501 if (!sbinfo->max_inodes)
3502 goto out;
3503 if (ctx.inodes < inodes)
3504 goto out;
3505 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003506
3507 error = 0;
Al Viro0b5071d2019-09-08 18:49:18 -04003508 if (ctx.seen & SHMEM_SEEN_HUGE)
3509 sbinfo->huge = ctx.huge;
3510 if (ctx.seen & SHMEM_SEEN_BLOCKS)
3511 sbinfo->max_blocks = ctx.blocks;
3512 if (ctx.seen & SHMEM_SEEN_INODES) {
3513 sbinfo->max_inodes = ctx.inodes;
3514 sbinfo->free_inodes = ctx.inodes - inodes;
3515 }
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003516
Greg Thelen5f001102013-02-22 16:36:01 -08003517 /*
3518 * Preserve previous mempolicy unless mpol remount option was specified.
3519 */
Al Viro0b5071d2019-09-08 18:49:18 -04003520 if (ctx.mpol) {
Greg Thelen5f001102013-02-22 16:36:01 -08003521 mpol_put(sbinfo->mpol);
Al Viro0b5071d2019-09-08 18:49:18 -04003522 sbinfo->mpol = ctx.mpol; /* transfers initial ref */
Greg Thelen5f001102013-02-22 16:36:01 -08003523 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003524out:
3525 spin_unlock(&sbinfo->stat_lock);
3526 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003528
Al Viro34c80b12011-12-08 21:32:45 -05003529static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003530{
Al Viro34c80b12011-12-08 21:32:45 -05003531 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003532
3533 if (sbinfo->max_blocks != shmem_default_max_blocks())
3534 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003535 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003536 if (sbinfo->max_inodes != shmem_default_max_inodes())
3537 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003538 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003539 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003540 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3541 seq_printf(seq, ",uid=%u",
3542 from_kuid_munged(&init_user_ns, sbinfo->uid));
3543 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3544 seq_printf(seq, ",gid=%u",
3545 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003546#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003547 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3548 if (sbinfo->huge)
3549 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3550#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003551 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003552 return 0;
3553}
David Herrmann9183df22014-08-08 14:25:29 -07003554
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003555#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556
3557static void shmem_put_super(struct super_block *sb)
3558{
Hugh Dickins602586a2010-08-17 15:23:56 -07003559 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3560
3561 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003562 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003563 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 sb->s_fs_info = NULL;
3565}
3566
Al Viro7e30d2a52019-06-01 18:56:53 -04003567static int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568{
3569 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003570 struct shmem_sb_info *sbinfo;
Al Viro0b5071d2019-09-08 18:49:18 -04003571 struct shmem_options ctx = {.mode = 0777 | S_ISVTX,
3572 .uid = current_fsuid(),
3573 .gid = current_fsgid()};
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003574 int err = -ENOMEM;
3575
3576 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003577 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003578 L1_CACHE_BYTES), GFP_KERNEL);
3579 if (!sbinfo)
3580 return -ENOMEM;
3581
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003582 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003584#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 /*
3586 * Per default we only allow half of the physical ram per
3587 * tmpfs instance, limiting inodes to one per page of lowmem;
3588 * but the internal instance is left unlimited.
3589 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003590 if (!(sb->s_flags & SB_KERNMOUNT)) {
Al Viro0b5071d2019-09-08 18:49:18 -04003591 ctx.blocks = shmem_default_max_blocks();
3592 ctx.inodes = shmem_default_max_inodes();
3593 if (shmem_parse_options(data, &ctx)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003594 err = -EINVAL;
3595 goto failed;
3596 }
Al Viroca4e0512013-08-31 12:57:10 -04003597 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003598 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 }
David M. Grimes91828a42006-10-17 00:09:45 -07003600 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003601 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003603 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604#endif
Al Viro0b5071d2019-09-08 18:49:18 -04003605 sbinfo->max_blocks = ctx.blocks;
3606 sbinfo->free_inodes = sbinfo->max_inodes = ctx.inodes;
3607 sbinfo->uid = ctx.uid;
3608 sbinfo->gid = ctx.gid;
3609 sbinfo->mode = ctx.mode;
3610 sbinfo->huge = ctx.huge;
3611 sbinfo->mpol = ctx.mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003613 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003614 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003615 goto failed;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003616 spin_lock_init(&sbinfo->shrinklist_lock);
3617 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003618
Hugh Dickins285b2c42011-08-03 16:21:20 -07003619 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003620 sb->s_blocksize = PAGE_SIZE;
3621 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 sb->s_magic = TMPFS_MAGIC;
3623 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003624 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003625#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003626 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003627#endif
3628#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003629 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003630#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003631 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003632
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003633 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 if (!inode)
3635 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003636 inode->i_uid = sbinfo->uid;
3637 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003638 sb->s_root = d_make_root(inode);
3639 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003640 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 return 0;
3642
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643failed:
3644 shmem_put_super(sb);
3645 return err;
3646}
3647
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003648static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650static struct inode *shmem_alloc_inode(struct super_block *sb)
3651{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003652 struct shmem_inode_info *info;
3653 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3654 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003656 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657}
3658
Al Viro74b1da52019-04-15 23:19:05 -04003659static void shmem_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003660{
Al Viro84e710d2016-04-15 00:58:55 -04003661 if (S_ISLNK(inode->i_mode))
3662 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003663 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3664}
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666static void shmem_destroy_inode(struct inode *inode)
3667{
Al Viro09208d12011-07-26 03:15:03 -04003668 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003672static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003674 struct shmem_inode_info *info = foo;
3675 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676}
3677
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003678static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
3680 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3681 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003682 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683}
3684
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003685static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003687 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003690static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003692 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003694 .write_begin = shmem_write_begin,
3695 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003697#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003698 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003699#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003700 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701};
3702
Helge Deller15ad7cd2006-12-06 20:40:36 -08003703static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003705 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003707 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003708 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003709 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003710 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003711 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003712 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003713 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714#endif
3715};
3716
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003717static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003718 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003719 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003720#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003721 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003722 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003723#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724};
3725
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003726static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727#ifdef CONFIG_TMPFS
3728 .create = shmem_create,
3729 .lookup = simple_lookup,
3730 .link = shmem_link,
3731 .unlink = shmem_unlink,
3732 .symlink = shmem_symlink,
3733 .mkdir = shmem_mkdir,
3734 .rmdir = shmem_rmdir,
3735 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003736 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003737 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003739#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003740 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003741#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003742#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003743 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003744 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003745#endif
3746};
3747
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003748static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003749#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003750 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003751#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003752#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003753 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003754 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003755#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756};
3757
Hugh Dickins759b9772007-03-05 00:30:28 -08003758static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 .alloc_inode = shmem_alloc_inode,
Al Viro74b1da52019-04-15 23:19:05 -04003760 .free_inode = shmem_free_in_core_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 .destroy_inode = shmem_destroy_inode,
3762#ifdef CONFIG_TMPFS
3763 .statfs = shmem_statfs,
3764 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003765 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766#endif
Al Viro1f895f72010-06-05 19:10:41 -04003767 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 .drop_inode = generic_delete_inode,
3769 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003770#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3771 .nr_cached_objects = shmem_unused_huge_count,
3772 .free_cached_objects = shmem_unused_huge_scan,
3773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774};
3775
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04003776static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003777 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003778 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779#ifdef CONFIG_NUMA
3780 .set_policy = shmem_set_policy,
3781 .get_policy = shmem_get_policy,
3782#endif
3783};
3784
Al Viro7e30d2a52019-06-01 18:56:53 -04003785struct dentry *shmem_mount(struct file_system_type *fs_type,
Al Viro3c26ff62010-07-25 11:46:36 +04003786 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
Al Viro3c26ff62010-07-25 11:46:36 +04003788 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003791static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 .owner = THIS_MODULE,
3793 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003794 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003796 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003799int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 int error;
3802
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003803 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003805 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003807 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 goto out2;
3809 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003810
Al Viroca4e0512013-08-31 12:57:10 -04003811 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 if (IS_ERR(shm_mnt)) {
3813 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003814 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 goto out1;
3816 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003817
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003818#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003819 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003820 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3821 else
3822 shmem_huge = 0; /* just in case it was patched */
3823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return 0;
3825
3826out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003827 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003829 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 shm_mnt = ERR_PTR(error);
3831 return error;
3832}
Matt Mackall853ac432009-01-06 14:40:20 -08003833
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003834#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003835static ssize_t shmem_enabled_show(struct kobject *kobj,
3836 struct kobj_attribute *attr, char *buf)
3837{
3838 int values[] = {
3839 SHMEM_HUGE_ALWAYS,
3840 SHMEM_HUGE_WITHIN_SIZE,
3841 SHMEM_HUGE_ADVISE,
3842 SHMEM_HUGE_NEVER,
3843 SHMEM_HUGE_DENY,
3844 SHMEM_HUGE_FORCE,
3845 };
3846 int i, count;
3847
3848 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3849 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3850
3851 count += sprintf(buf + count, fmt,
3852 shmem_format_huge(values[i]));
3853 }
3854 buf[count - 1] = '\n';
3855 return count;
3856}
3857
3858static ssize_t shmem_enabled_store(struct kobject *kobj,
3859 struct kobj_attribute *attr, const char *buf, size_t count)
3860{
3861 char tmp[16];
3862 int huge;
3863
3864 if (count + 1 > sizeof(tmp))
3865 return -EINVAL;
3866 memcpy(tmp, buf, count);
3867 tmp[count] = '\0';
3868 if (count && tmp[count - 1] == '\n')
3869 tmp[count - 1] = '\0';
3870
3871 huge = shmem_parse_huge(tmp);
3872 if (huge == -EINVAL)
3873 return -EINVAL;
3874 if (!has_transparent_hugepage() &&
3875 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3876 return -EINVAL;
3877
3878 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003879 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003880 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3881 return count;
3882}
3883
3884struct kobj_attribute shmem_enabled_attr =
3885 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003886#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003887
Arnd Bergmann3b337192016-08-10 16:27:44 -07003888#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003889bool shmem_huge_enabled(struct vm_area_struct *vma)
3890{
3891 struct inode *inode = file_inode(vma->vm_file);
3892 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3893 loff_t i_size;
3894 pgoff_t off;
3895
Yang Shic0630662019-07-18 15:57:27 -07003896 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
3897 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
3898 return false;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003899 if (shmem_huge == SHMEM_HUGE_FORCE)
3900 return true;
3901 if (shmem_huge == SHMEM_HUGE_DENY)
3902 return false;
3903 switch (sbinfo->huge) {
3904 case SHMEM_HUGE_NEVER:
3905 return false;
3906 case SHMEM_HUGE_ALWAYS:
3907 return true;
3908 case SHMEM_HUGE_WITHIN_SIZE:
3909 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3910 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3911 if (i_size >= HPAGE_PMD_SIZE &&
3912 i_size >> PAGE_SHIFT >= off)
3913 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003914 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003915 case SHMEM_HUGE_ADVISE:
3916 /* TODO: implement fadvise() hints */
3917 return (vma->vm_flags & VM_HUGEPAGE);
3918 default:
3919 VM_BUG_ON(1);
3920 return false;
3921 }
3922}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003923#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003924
Matt Mackall853ac432009-01-06 14:40:20 -08003925#else /* !CONFIG_SHMEM */
3926
3927/*
3928 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3929 *
3930 * This is intended for small system where the benefits of the full
3931 * shmem code (swap-backed and resource-limited) are outweighed by
3932 * their complexity. On systems without swap this code should be
3933 * effectively equivalent, but much lighter weight.
3934 */
3935
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003936static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003937 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003938 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003939 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003940 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003941};
3942
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003943int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003944{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003945 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003946
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003947 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003948 BUG_ON(IS_ERR(shm_mnt));
3949
3950 return 0;
3951}
3952
Vineeth Remanan Pillaib56a2d82019-03-05 15:47:03 -08003953int shmem_unuse(unsigned int type, bool frontswap,
3954 unsigned long *fs_pages_to_unuse)
Matt Mackall853ac432009-01-06 14:40:20 -08003955{
3956 return 0;
3957}
3958
Hugh Dickins3f96b792009-09-21 17:03:37 -07003959int shmem_lock(struct file *file, int lock, struct user_struct *user)
3960{
3961 return 0;
3962}
3963
Hugh Dickins24513262012-01-20 14:34:21 -08003964void shmem_unlock_mapping(struct address_space *mapping)
3965{
3966}
3967
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003968#ifdef CONFIG_MMU
3969unsigned long shmem_get_unmapped_area(struct file *file,
3970 unsigned long addr, unsigned long len,
3971 unsigned long pgoff, unsigned long flags)
3972{
3973 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3974}
3975#endif
3976
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003977void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003978{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003979 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003980}
3981EXPORT_SYMBOL_GPL(shmem_truncate_range);
3982
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003983#define shmem_vm_ops generic_file_vm_ops
3984#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003985#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003986#define shmem_acct_size(flags, size) 0
3987#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003988
3989#endif /* CONFIG_SHMEM */
3990
3991/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Matthew Auld703321b2017-10-06 23:18:13 +01003993static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003994 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003997 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998
Matthew Auld703321b2017-10-06 23:18:13 +01003999 if (IS_ERR(mnt))
4000 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Hugh Dickins285b2c42011-08-03 16:21:20 -07004002 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return ERR_PTR(-EINVAL);
4004
4005 if (shmem_acct_size(flags, size))
4006 return ERR_PTR(-ENOMEM);
4007
Al Viro93dec2d2018-07-08 23:02:03 -04004008 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
4009 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04004010 if (unlikely(!inode)) {
4011 shmem_unacct_size(flags, size);
4012 return ERR_PTR(-ENOSPC);
4013 }
Eric Parisc7277092013-12-02 11:24:19 +00004014 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004016 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004017 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04004018 if (!IS_ERR(res))
4019 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
4020 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05004021 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04004022 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05004023 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024}
Eric Parisc7277092013-12-02 11:24:19 +00004025
4026/**
4027 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4028 * kernel internal. There will be NO LSM permission checks against the
4029 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004030 * higher layer. The users are the big_key and shm implementations. LSM
4031 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004032 * @name: name for dentry (to be seen in /proc/<pid>/maps
4033 * @size: size to be set for the file
4034 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4035 */
4036struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4037{
Matthew Auld703321b2017-10-06 23:18:13 +01004038 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00004039}
4040
4041/**
4042 * shmem_file_setup - get an unlinked file living in tmpfs
4043 * @name: name for dentry (to be seen in /proc/<pid>/maps
4044 * @size: size to be set for the file
4045 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4046 */
4047struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4048{
Matthew Auld703321b2017-10-06 23:18:13 +01004049 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00004050}
Keith Packard395e0dd2008-06-20 00:08:06 -07004051EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Randy Dunlap46711812008-03-19 17:00:41 -07004053/**
Matthew Auld703321b2017-10-06 23:18:13 +01004054 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4055 * @mnt: the tmpfs mount where the file will be created
4056 * @name: name for dentry (to be seen in /proc/<pid>/maps
4057 * @size: size to be set for the file
4058 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4059 */
4060struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4061 loff_t size, unsigned long flags)
4062{
4063 return __shmem_file_setup(mnt, name, size, flags, 0);
4064}
4065EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4066
4067/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4070 */
4071int shmem_zero_setup(struct vm_area_struct *vma)
4072{
4073 struct file *file;
4074 loff_t size = vma->vm_end - vma->vm_start;
4075
Hugh Dickins66fc1302015-06-14 09:48:09 -07004076 /*
4077 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4078 * between XFS directory reading and selinux: since this file is only
4079 * accessible to the user through its mapping, use S_PRIVATE flag to
4080 * bypass file security, in the same way as shmem_kernel_file_setup().
4081 */
Matthew Auld703321b2017-10-06 23:18:13 +01004082 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 if (IS_ERR(file))
4084 return PTR_ERR(file);
4085
4086 if (vma->vm_file)
4087 fput(vma->vm_file);
4088 vma->vm_file = file;
4089 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004090
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004091 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004092 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4093 (vma->vm_end & HPAGE_PMD_MASK)) {
4094 khugepaged_enter(vma, vma->vm_flags);
4095 }
4096
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return 0;
4098}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004099
4100/**
4101 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4102 * @mapping: the page's address_space
4103 * @index: the page index
4104 * @gfp: the page allocator flags to use if allocating
4105 *
4106 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4107 * with any new page allocations done using the specified allocation flags.
4108 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4109 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4110 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4111 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004112 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4113 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004114 */
4115struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4116 pgoff_t index, gfp_t gfp)
4117{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004118#ifdef CONFIG_SHMEM
4119 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004120 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004121 int error;
4122
4123 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004124 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004125 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004126 if (error)
4127 page = ERR_PTR(error);
4128 else
4129 unlock_page(page);
4130 return page;
4131#else
4132 /*
4133 * The tiny !SHMEM case uses ramfs without swap
4134 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004135 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004136#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004137}
4138EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);