blob: dea5120565d30c299516c2973d1a8ab85779ba7a [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>
Matt Mackall853ac432009-01-06 14:40:20 -080039
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080040#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
41
Matt Mackall853ac432009-01-06 14:40:20 -080042static struct vfsmount *shm_mnt;
43
44#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * This virtual memory filesystem is heavily based on the ramfs. It
47 * extends ramfs by the ability to use swap and honor resource limits
48 * which makes it a completely usable filesystem.
49 */
50
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070051#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070052#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010053#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080054#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/string.h>
57#include <linux/slab.h>
58#include <linux/backing-dev.h>
59#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070062#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070063#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070064#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070065#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/security.h>
67#include <linux/swapops.h>
68#include <linux/mempolicy.h>
69#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000070#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070071#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070072#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080073#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040074#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070075#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070076#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070077#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080078#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080079#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030080#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070081
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080082#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/pgtable.h>
84
Mel Gormandd56b042015-11-06 16:28:43 -080085#include "internal.h"
86
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030087#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
88#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Pretend that each entry is of this size in directory's i_size */
91#define BOGO_DIRENT_SIZE 20
92
Hugh Dickins69f07ec2011-08-03 16:21:26 -070093/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
94#define SHORT_SYMLINK_LEN 128
95
Hugh Dickins1aac1402012-05-29 15:06:42 -070096/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070097 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
98 * inode->i_private (with i_mutex making sure that it has only one user at
99 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700100 */
101struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700102 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700103 pgoff_t start; /* start of range currently being fallocated */
104 pgoff_t next; /* the next page offset to be fallocated */
105 pgoff_t nr_falloced; /* how many new pages have been fallocated */
106 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
107};
108
Andrew Mortonb76db732008-02-08 04:21:49 -0800109#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800110static unsigned long shmem_default_max_blocks(void)
111{
112 return totalram_pages / 2;
113}
114
115static unsigned long shmem_default_max_inodes(void)
116{
117 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
118}
Andrew Mortonb76db732008-02-08 04:21:49 -0800119#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800120
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700121static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
122static int shmem_replace_page(struct page **pagep, gfp_t gfp,
123 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700124static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700125 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800126 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700127 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700128
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700129int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700130 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700131{
132 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800133 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
137{
138 return sb->s_fs_info;
139}
140
141/*
142 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
143 * for shared memory and for shared anonymous (/dev/zero) mappings
144 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
145 * consistent with the pre-accounting of private mappings ...
146 */
147static inline int shmem_acct_size(unsigned long flags, loff_t size)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000150 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void shmem_unacct_size(unsigned long flags, loff_t size)
154{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000155 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vm_unacct_memory(VM_ACCT(size));
157}
158
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700159static inline int shmem_reacct_size(unsigned long flags,
160 loff_t oldsize, loff_t newsize)
161{
162 if (!(flags & VM_NORESERVE)) {
163 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
164 return security_vm_enough_memory_mm(current->mm,
165 VM_ACCT(newsize) - VM_ACCT(oldsize));
166 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
167 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
168 }
169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700174 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
176 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
177 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700178static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700180 if (!(flags & VM_NORESERVE))
181 return 0;
182
183 return security_vm_enough_memory_mm(current->mm,
184 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static inline void shmem_unacct_blocks(unsigned long flags, long pages)
188{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000189 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300190 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Mike Rapoport0f079692017-09-06 16:22:59 -0700193static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
194{
195 struct shmem_inode_info *info = SHMEM_I(inode);
196 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
197
198 if (shmem_acct_block(info->flags, pages))
199 return false;
200
201 if (sbinfo->max_blocks) {
202 if (percpu_counter_compare(&sbinfo->used_blocks,
203 sbinfo->max_blocks - pages) > 0)
204 goto unacct;
205 percpu_counter_add(&sbinfo->used_blocks, pages);
206 }
207
208 return true;
209
210unacct:
211 shmem_unacct_blocks(info->flags, pages);
212 return false;
213}
214
215static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
216{
217 struct shmem_inode_info *info = SHMEM_I(inode);
218 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
219
220 if (sbinfo->max_blocks)
221 percpu_counter_sub(&sbinfo->used_blocks, pages);
222 shmem_unacct_blocks(info->flags, pages);
223}
224
Hugh Dickins759b9772007-03-05 00:30:28 -0800225static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700226static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800227static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800228static const struct inode_operations shmem_inode_operations;
229static const struct inode_operations shmem_dir_inode_operations;
230static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400231static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700232static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mike Rapoportb0506e42017-02-22 15:43:28 -0800234bool vma_is_shmem(struct vm_area_struct *vma)
235{
236 return vma->vm_ops == &shmem_vm_ops;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800240static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800242static int shmem_reserve_inode(struct super_block *sb)
243{
244 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
245 if (sbinfo->max_inodes) {
246 spin_lock(&sbinfo->stat_lock);
247 if (!sbinfo->free_inodes) {
248 spin_unlock(&sbinfo->stat_lock);
249 return -ENOSPC;
250 }
251 sbinfo->free_inodes--;
252 spin_unlock(&sbinfo->stat_lock);
253 }
254 return 0;
255}
256
257static void shmem_free_inode(struct super_block *sb)
258{
259 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
260 if (sbinfo->max_inodes) {
261 spin_lock(&sbinfo->stat_lock);
262 sbinfo->free_inodes++;
263 spin_unlock(&sbinfo->stat_lock);
264 }
265}
266
Randy Dunlap46711812008-03-19 17:00:41 -0700267/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700268 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * @inode: inode to recalc
270 *
271 * We have to calculate the free blocks since the mm can drop
272 * undirtied hole pages behind our back.
273 *
274 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
275 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
276 *
277 * It has to be called with the spinlock held.
278 */
279static void shmem_recalc_inode(struct inode *inode)
280{
281 struct shmem_inode_info *info = SHMEM_I(inode);
282 long freed;
283
284 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
285 if (freed > 0) {
286 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700287 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700288 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700292bool shmem_charge(struct inode *inode, long pages)
293{
294 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700295 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700296
Mike Rapoport0f079692017-09-06 16:22:59 -0700297 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700299
Hugh Dickins78141aa2018-11-30 14:10:29 -0800300 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
301 inode->i_mapping->nrpages += pages;
302
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700303 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700304 info->alloced += pages;
305 inode->i_blocks += pages * BLOCKS_PER_PAGE;
306 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700307 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700308
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700309 return true;
310}
311
312void shmem_uncharge(struct inode *inode, long pages)
313{
314 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700315 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700316
Hugh Dickins78141aa2018-11-30 14:10:29 -0800317 /* nrpages adjustment done by __delete_from_page_cache() or caller */
318
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700319 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700320 info->alloced -= pages;
321 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
322 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700323 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700324
Mike Rapoport0f079692017-09-06 16:22:59 -0700325 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700326}
327
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700328/*
329 * Replace item expected in radix tree by a new item, while holding tree lock.
330 */
331static int shmem_radix_tree_replace(struct address_space *mapping,
332 pgoff_t index, void *expected, void *replacement)
333{
Johannes Weinerf7942432016-12-12 16:43:41 -0800334 struct radix_tree_node *node;
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700335 void __rcu **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700336 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700337
338 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700339 VM_BUG_ON(!replacement);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700340 item = __radix_tree_lookup(&mapping->i_pages, index, &node, &pslot);
Johannes Weinerf7942432016-12-12 16:43:41 -0800341 if (!item)
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700342 return -ENOENT;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700343 if (item != expected)
344 return -ENOENT;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700345 __radix_tree_replace(&mapping->i_pages, node, pslot,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800346 replacement, NULL);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700347 return 0;
348}
349
350/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700351 * Sometimes, before we decide whether to proceed or to fail, we must check
352 * that an entry was not already brought back from swap by a racing thread.
353 *
354 * Checking page is not enough: by the time a SwapCache page is locked, it
355 * might be reused, and again be SwapCache, using the same swap as before.
356 */
357static bool shmem_confirm_swap(struct address_space *mapping,
358 pgoff_t index, swp_entry_t swap)
359{
360 void *item;
361
362 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700363 item = radix_tree_lookup(&mapping->i_pages, index);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700364 rcu_read_unlock();
365 return item == swp_to_radix_entry(swap);
366}
367
368/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700369 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
370 *
371 * SHMEM_HUGE_NEVER:
372 * disables huge pages for the mount;
373 * SHMEM_HUGE_ALWAYS:
374 * enables huge pages for the mount;
375 * SHMEM_HUGE_WITHIN_SIZE:
376 * only allocate huge pages if the page will be fully within i_size,
377 * also respect fadvise()/madvise() hints;
378 * SHMEM_HUGE_ADVISE:
379 * only allocate huge pages if requested with fadvise()/madvise();
380 */
381
382#define SHMEM_HUGE_NEVER 0
383#define SHMEM_HUGE_ALWAYS 1
384#define SHMEM_HUGE_WITHIN_SIZE 2
385#define SHMEM_HUGE_ADVISE 3
386
387/*
388 * Special values.
389 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
390 *
391 * SHMEM_HUGE_DENY:
392 * disables huge on shm_mnt and all mounts, for emergency use;
393 * SHMEM_HUGE_FORCE:
394 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
395 *
396 */
397#define SHMEM_HUGE_DENY (-1)
398#define SHMEM_HUGE_FORCE (-2)
399
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700400#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700401/* ifdef here to avoid bloating shmem.o when not necessary */
402
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700403static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700404
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800405#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700406static int shmem_parse_huge(const char *str)
407{
408 if (!strcmp(str, "never"))
409 return SHMEM_HUGE_NEVER;
410 if (!strcmp(str, "always"))
411 return SHMEM_HUGE_ALWAYS;
412 if (!strcmp(str, "within_size"))
413 return SHMEM_HUGE_WITHIN_SIZE;
414 if (!strcmp(str, "advise"))
415 return SHMEM_HUGE_ADVISE;
416 if (!strcmp(str, "deny"))
417 return SHMEM_HUGE_DENY;
418 if (!strcmp(str, "force"))
419 return SHMEM_HUGE_FORCE;
420 return -EINVAL;
421}
422
423static const char *shmem_format_huge(int huge)
424{
425 switch (huge) {
426 case SHMEM_HUGE_NEVER:
427 return "never";
428 case SHMEM_HUGE_ALWAYS:
429 return "always";
430 case SHMEM_HUGE_WITHIN_SIZE:
431 return "within_size";
432 case SHMEM_HUGE_ADVISE:
433 return "advise";
434 case SHMEM_HUGE_DENY:
435 return "deny";
436 case SHMEM_HUGE_FORCE:
437 return "force";
438 default:
439 VM_BUG_ON(1);
440 return "bad_val";
441 }
442}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800443#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700444
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700445static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
446 struct shrink_control *sc, unsigned long nr_to_split)
447{
448 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800449 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700450 struct inode *inode;
451 struct shmem_inode_info *info;
452 struct page *page;
453 unsigned long batch = sc ? sc->nr_to_scan : 128;
454 int removed = 0, split = 0;
455
456 if (list_empty(&sbinfo->shrinklist))
457 return SHRINK_STOP;
458
459 spin_lock(&sbinfo->shrinklist_lock);
460 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
461 info = list_entry(pos, struct shmem_inode_info, shrinklist);
462
463 /* pin the inode */
464 inode = igrab(&info->vfs_inode);
465
466 /* inode is about to be evicted */
467 if (!inode) {
468 list_del_init(&info->shrinklist);
469 removed++;
470 goto next;
471 }
472
473 /* Check if there's anything to gain */
474 if (round_up(inode->i_size, PAGE_SIZE) ==
475 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800476 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700477 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700478 goto next;
479 }
480
481 list_move(&info->shrinklist, &list);
482next:
483 if (!--batch)
484 break;
485 }
486 spin_unlock(&sbinfo->shrinklist_lock);
487
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800488 list_for_each_safe(pos, next, &to_remove) {
489 info = list_entry(pos, struct shmem_inode_info, shrinklist);
490 inode = &info->vfs_inode;
491 list_del_init(&info->shrinklist);
492 iput(inode);
493 }
494
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700495 list_for_each_safe(pos, next, &list) {
496 int ret;
497
498 info = list_entry(pos, struct shmem_inode_info, shrinklist);
499 inode = &info->vfs_inode;
500
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700501 if (nr_to_split && split >= nr_to_split)
502 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700503
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700504 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700505 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
506 if (!page)
507 goto drop;
508
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700509 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700510 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700511 put_page(page);
512 goto drop;
513 }
514
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700515 /*
516 * Leave the inode on the list if we failed to lock
517 * the page at this time.
518 *
519 * Waiting for the lock may lead to deadlock in the
520 * reclaim path.
521 */
522 if (!trylock_page(page)) {
523 put_page(page);
524 goto leave;
525 }
526
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700527 ret = split_huge_page(page);
528 unlock_page(page);
529 put_page(page);
530
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700531 /* If split failed leave the inode on the list */
532 if (ret)
533 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700534
535 split++;
536drop:
537 list_del_init(&info->shrinklist);
538 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700539leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700540 iput(inode);
541 }
542
543 spin_lock(&sbinfo->shrinklist_lock);
544 list_splice_tail(&list, &sbinfo->shrinklist);
545 sbinfo->shrinklist_len -= removed;
546 spin_unlock(&sbinfo->shrinklist_lock);
547
548 return split;
549}
550
551static long shmem_unused_huge_scan(struct super_block *sb,
552 struct shrink_control *sc)
553{
554 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
555
556 if (!READ_ONCE(sbinfo->shrinklist_len))
557 return SHRINK_STOP;
558
559 return shmem_unused_huge_shrink(sbinfo, sc, 0);
560}
561
562static long shmem_unused_huge_count(struct super_block *sb,
563 struct shrink_control *sc)
564{
565 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
566 return READ_ONCE(sbinfo->shrinklist_len);
567}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700568#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700569
570#define shmem_huge SHMEM_HUGE_DENY
571
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700572static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
573 struct shrink_control *sc, unsigned long nr_to_split)
574{
575 return 0;
576}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700577#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700578
Yang Shi89fdcd22018-06-07 17:06:59 -0700579static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
580{
581 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
582 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
583 shmem_huge != SHMEM_HUGE_DENY)
584 return true;
585 return false;
586}
587
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700588/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700589 * Like add_to_page_cache_locked, but error if expected item has gone.
590 */
591static int shmem_add_to_page_cache(struct page *page,
592 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700593 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700594{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700595 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700596
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700597 VM_BUG_ON_PAGE(PageTail(page), page);
598 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800599 VM_BUG_ON_PAGE(!PageLocked(page), page);
600 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700601 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700602
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700603 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700604 page->mapping = mapping;
605 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700606
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700607 xa_lock_irq(&mapping->i_pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700608 if (PageTransHuge(page)) {
609 void __rcu **results;
610 pgoff_t idx;
611 int i;
612
613 error = 0;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700614 if (radix_tree_gang_lookup_slot(&mapping->i_pages,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700615 &results, &idx, index, 1) &&
616 idx < index + HPAGE_PMD_NR) {
617 error = -EEXIST;
618 }
619
620 if (!error) {
621 for (i = 0; i < HPAGE_PMD_NR; i++) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700622 error = radix_tree_insert(&mapping->i_pages,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700623 index + i, page + i);
624 VM_BUG_ON(error);
625 }
626 count_vm_event(THP_FILE_ALLOC);
627 }
628 } else if (!expected) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700629 error = radix_tree_insert(&mapping->i_pages, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700630 } else {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700631 error = shmem_radix_tree_replace(mapping, index, expected,
632 page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700633 }
634
Hugh Dickinsb065b432012-07-11 14:02:48 -0700635 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700636 mapping->nrpages += nr;
637 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700638 __inc_node_page_state(page, NR_SHMEM_THPS);
639 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
640 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700641 xa_unlock_irq(&mapping->i_pages);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700642 } else {
643 page->mapping = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700644 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700645 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700646 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700647 return error;
648}
649
650/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700651 * Like delete_from_page_cache, but substitutes swap for page.
652 */
653static void shmem_delete_from_page_cache(struct page *page, void *radswap)
654{
655 struct address_space *mapping = page->mapping;
656 int error;
657
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700658 VM_BUG_ON_PAGE(PageCompound(page), page);
659
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700660 xa_lock_irq(&mapping->i_pages);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700661 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
662 page->mapping = NULL;
663 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700664 __dec_node_page_state(page, NR_FILE_PAGES);
665 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700666 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300667 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700668 BUG_ON(error);
669}
670
671/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700672 * Remove swap entry from radix tree, free the swap and its page cache.
673 */
674static int shmem_free_swap(struct address_space *mapping,
675 pgoff_t index, void *radswap)
676{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700677 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700678
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700679 xa_lock_irq(&mapping->i_pages);
680 old = radix_tree_delete_item(&mapping->i_pages, index, radswap);
681 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700682 if (old != radswap)
683 return -ENOENT;
684 free_swap_and_cache(radix_to_swp_entry(radswap));
685 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700686}
687
688/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800689 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800690 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800691 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700692 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800693 * as long as the inode doesn't go away and racy results are not a problem.
694 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800695unsigned long shmem_partial_swap_usage(struct address_space *mapping,
696 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800697{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800698 struct radix_tree_iter iter;
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700699 void __rcu **slot;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800700 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800701 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800702
703 rcu_read_lock();
704
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700705 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800706 if (iter.index >= end)
707 break;
708
709 page = radix_tree_deref_slot(slot);
710
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700711 if (radix_tree_deref_retry(page)) {
712 slot = radix_tree_iter_retry(&iter);
713 continue;
714 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800715
716 if (radix_tree_exceptional_entry(page))
717 swapped++;
718
719 if (need_resched()) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800720 slot = radix_tree_iter_resume(slot, &iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800721 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800722 }
723 }
724
725 rcu_read_unlock();
726
727 return swapped << PAGE_SHIFT;
728}
729
730/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800731 * Determine (in bytes) how many of the shmem object's pages mapped by the
732 * given vma is swapped out.
733 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700734 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800735 * as long as the inode doesn't go away and racy results are not a problem.
736 */
737unsigned long shmem_swap_usage(struct vm_area_struct *vma)
738{
739 struct inode *inode = file_inode(vma->vm_file);
740 struct shmem_inode_info *info = SHMEM_I(inode);
741 struct address_space *mapping = inode->i_mapping;
742 unsigned long swapped;
743
744 /* Be careful as we don't hold info->lock */
745 swapped = READ_ONCE(info->swapped);
746
747 /*
748 * The easier cases are when the shmem object has nothing in swap, or
749 * the vma maps it whole. Then we can simply use the stats that we
750 * already track.
751 */
752 if (!swapped)
753 return 0;
754
755 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
756 return swapped << PAGE_SHIFT;
757
758 /* Here comes the more involved part */
759 return shmem_partial_swap_usage(mapping,
760 linear_page_index(vma, vma->vm_start),
761 linear_page_index(vma, vma->vm_end));
762}
763
764/*
Hugh Dickins24513262012-01-20 14:34:21 -0800765 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
766 */
767void shmem_unlock_mapping(struct address_space *mapping)
768{
769 struct pagevec pvec;
770 pgoff_t indices[PAGEVEC_SIZE];
771 pgoff_t index = 0;
772
Mel Gorman86679822017-11-15 17:37:52 -0800773 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800774 /*
775 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
776 */
777 while (!mapping_unevictable(mapping)) {
778 /*
779 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
780 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
781 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700782 pvec.nr = find_get_entries(mapping, index,
783 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800784 if (!pvec.nr)
785 break;
786 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700787 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800788 check_move_unevictable_pages(pvec.pages, pvec.nr);
789 pagevec_release(&pvec);
790 cond_resched();
791 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700792}
793
794/*
795 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700796 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700797 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700798static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
799 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700801 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300803 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
804 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
805 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
806 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700807 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700808 pgoff_t indices[PAGEVEC_SIZE];
809 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700810 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700811 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700813 if (lend == -1)
814 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700815
Mel Gorman86679822017-11-15 17:37:52 -0800816 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700817 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700818 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700819 pvec.nr = find_get_entries(mapping, index,
820 min(end - index, (pgoff_t)PAGEVEC_SIZE),
821 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700822 if (!pvec.nr)
823 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700824 for (i = 0; i < pagevec_count(&pvec); i++) {
825 struct page *page = pvec.pages[i];
826
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700827 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700828 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700829 break;
830
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700831 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700832 if (unfalloc)
833 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700834 nr_swaps_freed += !shmem_free_swap(mapping,
835 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700836 continue;
837 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700838
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700839 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
840
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700841 if (!trylock_page(page))
842 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700843
844 if (PageTransTail(page)) {
845 /* Middle of THP: zero out the page */
846 clear_highpage(page);
847 unlock_page(page);
848 continue;
849 } else if (PageTransHuge(page)) {
850 if (index == round_down(end, HPAGE_PMD_NR)) {
851 /*
852 * Range ends in the middle of THP:
853 * zero out the page
854 */
855 clear_highpage(page);
856 unlock_page(page);
857 continue;
858 }
859 index += HPAGE_PMD_NR - 1;
860 i += HPAGE_PMD_NR - 1;
861 }
862
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700863 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700864 VM_BUG_ON_PAGE(PageTail(page), page);
865 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800866 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700867 truncate_inode_page(mapping, page);
868 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700869 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700870 unlock_page(page);
871 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700872 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800873 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700874 cond_resched();
875 index++;
876 }
877
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700878 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700879 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700880 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700881 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300882 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700883 if (start > end) {
884 top = partial_end;
885 partial_end = 0;
886 }
887 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700888 set_page_dirty(page);
889 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300890 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700891 }
892 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700893 if (partial_end) {
894 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700895 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700896 if (page) {
897 zero_user_segment(page, 0, partial_end);
898 set_page_dirty(page);
899 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300900 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700901 }
902 }
903 if (start >= end)
904 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700905
906 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700907 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700908 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700909
910 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700911 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700912 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700913 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700914 /* If all gone or hole-punch or unfalloc, we're done */
915 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700916 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700917 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700918 index = start;
919 continue;
920 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700921 for (i = 0; i < pagevec_count(&pvec); i++) {
922 struct page *page = pvec.pages[i];
923
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700924 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700925 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700926 break;
927
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700928 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700929 if (unfalloc)
930 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700931 if (shmem_free_swap(mapping, index, page)) {
932 /* Swap was replaced by page: retry */
933 index--;
934 break;
935 }
936 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700937 continue;
938 }
939
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700940 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700941
942 if (PageTransTail(page)) {
943 /* Middle of THP: zero out the page */
944 clear_highpage(page);
945 unlock_page(page);
946 /*
947 * Partial thp truncate due 'start' in middle
948 * of THP: don't need to look on these pages
949 * again on !pvec.nr restart.
950 */
951 if (index != round_down(end, HPAGE_PMD_NR))
952 start++;
953 continue;
954 } else if (PageTransHuge(page)) {
955 if (index == round_down(end, HPAGE_PMD_NR)) {
956 /*
957 * Range ends in the middle of THP:
958 * zero out the page
959 */
960 clear_highpage(page);
961 unlock_page(page);
962 continue;
963 }
964 index += HPAGE_PMD_NR - 1;
965 i += HPAGE_PMD_NR - 1;
966 }
967
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700968 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700969 VM_BUG_ON_PAGE(PageTail(page), page);
970 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800971 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700972 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700973 } else {
974 /* Page was replaced by swap: retry */
975 unlock_page(page);
976 index--;
977 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700978 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700979 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700980 unlock_page(page);
981 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700982 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800983 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700984 index++;
985 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700986
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700987 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700988 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700990 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700991}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700993void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
994{
995 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700996 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700998EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
David Howellsa528d352017-01-31 16:46:22 +00001000static int shmem_getattr(const struct path *path, struct kstat *stat,
1001 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -07001002{
David Howellsa528d352017-01-31 16:46:22 +00001003 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -07001004 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -07001005 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -07001006
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001007 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001008 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001009 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001010 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001011 }
Yu Zhao44a30222015-09-08 15:03:33 -07001012 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001013
1014 if (is_huge_enabled(sb_info))
1015 stat->blksize = HPAGE_PMD_SIZE;
1016
Yu Zhao44a30222015-09-08 15:03:33 -07001017 return 0;
1018}
1019
Hugh Dickins94c1e622011-06-27 16:18:03 -07001020static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
David Howells75c3cfa2015-03-17 22:26:12 +00001022 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001023 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001024 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 int error;
1026
Jan Kara31051c82016-05-26 16:55:18 +02001027 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001028 if (error)
1029 return error;
1030
Hugh Dickins94c1e622011-06-27 16:18:03 -07001031 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1032 loff_t oldsize = inode->i_size;
1033 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001034
David Herrmann40e041a2014-08-08 14:25:27 -07001035 /* protected by i_mutex */
1036 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1037 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1038 return -EPERM;
1039
Hugh Dickins94c1e622011-06-27 16:18:03 -07001040 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001041 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1042 oldsize, newsize);
1043 if (error)
1044 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001045 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001046 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001047 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001048 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001049 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001050 if (oldsize > holebegin)
1051 unmap_mapping_range(inode->i_mapping,
1052 holebegin, 0, 1);
1053 if (info->alloced)
1054 shmem_truncate_range(inode,
1055 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001056 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001057 if (oldsize > holebegin)
1058 unmap_mapping_range(inode->i_mapping,
1059 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001060
1061 /*
1062 * Part of the huge page can be beyond i_size: subject
1063 * to shrink under memory pressure.
1064 */
1065 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1066 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001067 /*
1068 * _careful to defend against unlocked access to
1069 * ->shrink_list in shmem_unused_huge_shrink()
1070 */
1071 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001072 list_add_tail(&info->shrinklist,
1073 &sbinfo->shrinklist);
1074 sbinfo->shrinklist_len++;
1075 }
1076 spin_unlock(&sbinfo->shrinklist_lock);
1077 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001081 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001082 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001083 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return error;
1085}
1086
Al Viro1f895f72010-06-05 19:10:41 -04001087static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001090 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001092 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 shmem_unacct_size(info->flags, inode->i_size);
1094 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001095 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001096 if (!list_empty(&info->shrinklist)) {
1097 spin_lock(&sbinfo->shrinklist_lock);
1098 if (!list_empty(&info->shrinklist)) {
1099 list_del_init(&info->shrinklist);
1100 sbinfo->shrinklist_len--;
1101 }
1102 spin_unlock(&sbinfo->shrinklist_lock);
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001105 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001107 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Al Viro3ed47db2016-01-22 18:08:52 -05001109 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001110
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001111 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001112 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001113 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001114 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
Matthew Wilcox478922e2016-12-14 15:08:52 -08001117static unsigned long find_swap_entry(struct radix_tree_root *root, void *item)
1118{
1119 struct radix_tree_iter iter;
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001120 void __rcu **slot;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001121 unsigned long found = -1;
1122 unsigned int checked = 0;
1123
1124 rcu_read_lock();
1125 radix_tree_for_each_slot(slot, root, &iter, 0) {
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001126 void *entry = radix_tree_deref_slot(slot);
1127
1128 if (radix_tree_deref_retry(entry)) {
1129 slot = radix_tree_iter_retry(&iter);
1130 continue;
1131 }
1132 if (entry == item) {
Matthew Wilcox478922e2016-12-14 15:08:52 -08001133 found = iter.index;
1134 break;
1135 }
1136 checked++;
1137 if ((checked % 4096) != 0)
1138 continue;
1139 slot = radix_tree_iter_resume(slot, &iter);
1140 cond_resched_rcu();
1141 }
1142
1143 rcu_read_unlock();
1144 return found;
1145}
1146
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001147/*
1148 * If swap found in inode, free it and move page from swapcache to filecache.
1149 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001150static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001151 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001153 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001154 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001155 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001156 gfp_t gfp;
1157 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001159 radswap = swp_to_radix_entry(swap);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001160 index = find_swap_entry(&mapping->i_pages, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001161 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001162 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001163
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001164 /*
1165 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001166 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001167 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001168 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001169 */
1170 if (shmem_swaplist.next != &info->swaplist)
1171 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001172
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001173 gfp = mapping_gfp_mask(mapping);
1174 if (shmem_should_replace_page(*pagep, gfp)) {
1175 mutex_unlock(&shmem_swaplist_mutex);
1176 error = shmem_replace_page(pagep, gfp, info, index);
1177 mutex_lock(&shmem_swaplist_mutex);
1178 /*
1179 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001180 * allocation, but the inode might have been freed while we
1181 * dropped it: although a racing shmem_evict_inode() cannot
1182 * complete without emptying the radix_tree, our page lock
1183 * on this swapcache page is not enough to prevent that -
1184 * free_swap_and_cache() of our swap entry will only
1185 * trylock_page(), removing swap from radix_tree whatever.
1186 *
1187 * We must not proceed to shmem_add_to_page_cache() if the
1188 * inode has been freed, but of course we cannot rely on
1189 * inode or mapping or info to check that. However, we can
1190 * safely check if our swap entry is still in use (and here
1191 * it can't have got reused for another page): if it's still
1192 * in use, then the inode cannot have been freed yet, and we
1193 * can safely proceed (if it's no longer in use, that tells
1194 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001195 */
1196 if (!page_swapcount(*pagep))
1197 error = -ENOENT;
1198 }
1199
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001200 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001201 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1202 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1203 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001204 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001205 if (!error)
1206 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001207 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001208 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001209 /*
1210 * Truncation and eviction use free_swap_and_cache(), which
1211 * only does trylock page: if we raced, best clean up here.
1212 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001213 delete_from_swap_cache(*pagep);
1214 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001215 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001216 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001217 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001218 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001219 swap_free(swap);
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001222 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
1225/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001226 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001228int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001230 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001232 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001233 int error = 0;
1234
1235 /*
1236 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001237 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001238 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001239 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001240 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001241
1242 /*
1243 * Charge page using GFP_KERNEL while we can wait, before taking
1244 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1245 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001246 */
Tejun Heo2cf85582018-07-03 11:14:56 -04001247 error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
1248 &memcg, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001249 if (error)
1250 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001251 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001252 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001254 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001255 list_for_each_safe(this, next, &shmem_swaplist) {
1256 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001257 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001258 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001259 else
1260 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001261 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001262 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001263 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001264 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001266 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001267
Johannes Weiner00501b52014-08-08 14:19:20 -07001268 if (error) {
1269 if (error != -ENOMEM)
1270 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001271 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001272 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001273 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001274out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001275 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001276 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001277 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
1280/*
1281 * Move the page from the page cache to the swap cache.
1282 */
1283static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1284{
1285 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001288 swp_entry_t swap;
1289 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001291 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 mapping = page->mapping;
1294 index = page->index;
1295 inode = mapping->host;
1296 info = SHMEM_I(inode);
1297 if (info->flags & VM_LOCKED)
1298 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001299 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 goto redirty;
1301
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001302 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001303 * Our capabilities prevent regular writeback or sync from ever calling
1304 * shmem_writepage; but a stacking filesystem might use ->writepage of
1305 * its underlying filesystem, in which case tmpfs should write out to
1306 * swap only in response to memory pressure, and not for the writeback
1307 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001308 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001309 if (!wbc->for_reclaim) {
1310 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1311 goto redirty;
1312 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001313
1314 /*
1315 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1316 * value into swapfile.c, the only way we can correctly account for a
1317 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001318 *
1319 * That's okay for a page already fallocated earlier, but if we have
1320 * not yet completed the fallocation, then (a) we want to keep track
1321 * of this page in case we have to undo it, and (b) it may not be a
1322 * good idea to continue anyway, once we're pushing into swap. So
1323 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001324 */
1325 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001326 if (inode->i_private) {
1327 struct shmem_falloc *shmem_falloc;
1328 spin_lock(&inode->i_lock);
1329 shmem_falloc = inode->i_private;
1330 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001331 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001332 index >= shmem_falloc->start &&
1333 index < shmem_falloc->next)
1334 shmem_falloc->nr_unswapped++;
1335 else
1336 shmem_falloc = NULL;
1337 spin_unlock(&inode->i_lock);
1338 if (shmem_falloc)
1339 goto redirty;
1340 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001341 clear_highpage(page);
1342 flush_dcache_page(page);
1343 SetPageUptodate(page);
1344 }
1345
Huang Ying38d8b4e2017-07-06 15:37:18 -07001346 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001347 if (!swap.val)
1348 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001349
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001350 /*
1351 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001352 * if it's not already there. Do it now before the page is
1353 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001354 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001355 * we've incremented swapped, because shmem_unuse_inode() will
1356 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001357 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001358 mutex_lock(&shmem_swaplist_mutex);
1359 if (list_empty(&info->swaplist))
1360 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001361
Hugh Dickins48f170f2011-07-25 17:12:37 -07001362 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001363 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001364 shmem_recalc_inode(inode);
1365 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001366 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001367
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001368 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001369 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1370
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001371 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001372 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001373 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 return 0;
1375 }
1376
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001377 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001378 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379redirty:
1380 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001381 if (wbc->for_reclaim)
1382 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1383 unlock_page(page);
1384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
Hugh Dickins75edd342016-05-19 17:12:44 -07001387#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001388static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001389{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001390 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001391
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001392 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001393 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001394
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001395 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001396
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001397 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001398}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001399
1400static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1401{
1402 struct mempolicy *mpol = NULL;
1403 if (sbinfo->mpol) {
1404 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1405 mpol = sbinfo->mpol;
1406 mpol_get(mpol);
1407 spin_unlock(&sbinfo->stat_lock);
1408 }
1409 return mpol;
1410}
Hugh Dickins75edd342016-05-19 17:12:44 -07001411#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1412static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1413{
1414}
1415static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1416{
1417 return NULL;
1418}
1419#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1420#ifndef CONFIG_NUMA
1421#define vm_policy vm_private_data
1422#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001423
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001424static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1425 struct shmem_inode_info *info, pgoff_t index)
1426{
1427 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001428 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001429 /* Bias interleave by inode number to distribute better across nodes */
1430 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001431 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1432}
1433
1434static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1435{
1436 /* Drop reference taken by mpol_shared_policy_lookup() */
1437 mpol_cond_put(vma->vm_policy);
1438}
1439
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001440static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1441 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001444 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001445 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001447 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001448 vmf.vma = &pvma;
1449 vmf.address = 0;
1450 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001451 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001452
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001453 return page;
1454}
Mel Gorman18a2f372012-12-05 14:01:41 -08001455
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001456static struct page *shmem_alloc_hugepage(gfp_t gfp,
1457 struct shmem_inode_info *info, pgoff_t index)
1458{
1459 struct vm_area_struct pvma;
1460 struct inode *inode = &info->vfs_inode;
1461 struct address_space *mapping = inode->i_mapping;
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001462 pgoff_t idx, hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001463 void __rcu **results;
1464 struct page *page;
1465
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001466 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001467 return NULL;
1468
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001469 hindex = round_down(index, HPAGE_PMD_NR);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001470 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001471 if (radix_tree_gang_lookup_slot(&mapping->i_pages, &results, &idx,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001472 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1473 rcu_read_unlock();
1474 return NULL;
1475 }
1476 rcu_read_unlock();
1477
1478 shmem_pseudo_vma_init(&pvma, info, hindex);
1479 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1480 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1481 shmem_pseudo_vma_destroy(&pvma);
1482 if (page)
1483 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001484 return page;
1485}
1486
1487static struct page *shmem_alloc_page(gfp_t gfp,
1488 struct shmem_inode_info *info, pgoff_t index)
1489{
1490 struct vm_area_struct pvma;
1491 struct page *page;
1492
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001493 shmem_pseudo_vma_init(&pvma, info, index);
1494 page = alloc_page_vma(gfp, &pvma, 0);
1495 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001496
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001497 return page;
1498}
1499
1500static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001501 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001502 pgoff_t index, bool huge)
1503{
Mike Rapoport0f079692017-09-06 16:22:59 -07001504 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001505 struct page *page;
1506 int nr;
1507 int err = -ENOSPC;
1508
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001509 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001510 huge = false;
1511 nr = huge ? HPAGE_PMD_NR : 1;
1512
Mike Rapoport0f079692017-09-06 16:22:59 -07001513 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001514 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001515
1516 if (huge)
1517 page = shmem_alloc_hugepage(gfp, info, index);
1518 else
1519 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001520 if (page) {
1521 __SetPageLocked(page);
1522 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001523 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001524 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001525
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001526 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001527 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001528failed:
1529 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001533 * When a page is moved from swapcache to shmem filecache (either by the
1534 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1535 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1536 * ignorance of the mapping it belongs to. If that mapping has special
1537 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1538 * we may need to copy to a suitable page before moving to filecache.
1539 *
1540 * In a future release, this may well be extended to respect cpuset and
1541 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1542 * but for now it is a simple matter of zone.
1543 */
1544static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1545{
1546 return page_zonenum(page) > gfp_zone(gfp);
1547}
1548
1549static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1550 struct shmem_inode_info *info, pgoff_t index)
1551{
1552 struct page *oldpage, *newpage;
1553 struct address_space *swap_mapping;
Yu Zhaob66375b2018-11-30 14:09:03 -08001554 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001555 pgoff_t swap_index;
1556 int error;
1557
1558 oldpage = *pagep;
Yu Zhaob66375b2018-11-30 14:09:03 -08001559 entry.val = page_private(oldpage);
1560 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001561 swap_mapping = page_mapping(oldpage);
1562
1563 /*
1564 * We have arrived here because our zones are constrained, so don't
1565 * limit chance of success by further cpuset and node constraints.
1566 */
1567 gfp &= ~GFP_CONSTRAINT_MASK;
1568 newpage = shmem_alloc_page(gfp, info, index);
1569 if (!newpage)
1570 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001571
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001572 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001573 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001574 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001575
Hugh Dickins9956edf2016-11-10 10:46:11 -08001576 __SetPageLocked(newpage);
1577 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001578 SetPageUptodate(newpage);
Yu Zhaob66375b2018-11-30 14:09:03 -08001579 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001580 SetPageSwapCache(newpage);
1581
1582 /*
1583 * Our caller will very soon move newpage out of swapcache, but it's
1584 * a nice clean interface for us to replace oldpage by newpage there.
1585 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001586 xa_lock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001587 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1588 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001589 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001590 __inc_node_page_state(newpage, NR_FILE_PAGES);
1591 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001592 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001593 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001594
Hugh Dickins0142ef62012-06-07 14:21:09 -07001595 if (unlikely(error)) {
1596 /*
1597 * Is this possible? I think not, now that our callers check
1598 * both PageSwapCache and page_private after getting page lock;
1599 * but be defensive. Reverse old to newpage for clear and free.
1600 */
1601 oldpage = newpage;
1602 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001603 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001604 lru_cache_add_anon(newpage);
1605 *pagep = newpage;
1606 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001607
1608 ClearPageSwapCache(oldpage);
1609 set_page_private(oldpage, 0);
1610
1611 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001612 put_page(oldpage);
1613 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001614 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001615}
1616
1617/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001618 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 *
1620 * If we allocate a new one we do not mark it dirty. That's up to the
1621 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001622 * entry since a page cannot live in both the swap and page cache.
1623 *
1624 * fault_mm and fault_type are only supplied by shmem_fault:
1625 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001627static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001628 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001629 struct vm_area_struct *vma, struct vm_fault *vmf,
1630 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631{
1632 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001633 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001635 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001636 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001637 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001639 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001640 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001642 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001643 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001645 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001647 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1648 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001650 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001651 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001652 if (radix_tree_exceptional_entry(page)) {
1653 swap = radix_to_swp_entry(page);
1654 page = NULL;
1655 }
1656
Hugh Dickins75edd342016-05-19 17:12:44 -07001657 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001658 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001659 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001660 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001661 }
1662
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001663 if (page && sgp == SGP_WRITE)
1664 mark_page_accessed(page);
1665
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001666 /* fallocated page? */
1667 if (page && !PageUptodate(page)) {
1668 if (sgp != SGP_READ)
1669 goto clear;
1670 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001671 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001672 page = NULL;
1673 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001674 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001675 *pagep = page;
1676 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
1679 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001680 * Fast cache lookup did not find it:
1681 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001683 sbinfo = SHMEM_SB(inode->i_sb);
Mike Rapoportcfda0522017-02-22 15:43:37 -08001684 charge_mm = vma ? vma->vm_mm : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 if (swap.val) {
1687 /* Look it up and read it in.. */
Huang Yingec560172017-09-06 16:24:36 -07001688 page = lookup_swap_cache(swap, NULL, 0);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001689 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001690 /* Or update major stats only when swapin succeeds?? */
1691 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001692 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001693 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001694 count_memcg_event_mm(charge_mm, PGMAJFAULT);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001695 }
1696 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001697 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001698 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001699 error = -ENOMEM;
1700 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
1704 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001705 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001706 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001707 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001708 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001709 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001710 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001711 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001713 goto failed;
1714 }
1715 wait_on_page_writeback(page);
1716
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001717 if (shmem_should_replace_page(page, gfp)) {
1718 error = shmem_replace_page(&page, gfp, info, index);
1719 if (error)
1720 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
Tejun Heo2cf85582018-07-03 11:14:56 -04001723 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001724 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001725 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001726 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001727 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001728 /*
1729 * We already confirmed swap under page lock, and make
1730 * no memory allocation here, so usually no possibility
1731 * of error; but free_swap_and_cache() only trylocks a
1732 * page, so it is just possible that the entry has been
1733 * truncated or holepunched since swap was confirmed.
1734 * shmem_undo_range() will have done some of the
1735 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001736 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001737 * Reset swap.val? No, leave it so "failed" goes back to
1738 * "repeat": reading a hole and writing should succeed.
1739 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001740 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001741 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001742 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001743 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001744 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001745 if (error)
1746 goto failed;
1747
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001748 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001749
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001750 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001751 info->swapped--;
1752 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001753 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001754
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001755 if (sgp == SGP_WRITE)
1756 mark_page_accessed(page);
1757
Hugh Dickins27ab7002011-07-25 17:12:36 -07001758 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001759 set_page_dirty(page);
1760 swap_free(swap);
1761
Hugh Dickins54af60422011-08-03 16:21:24 -07001762 } else {
Mike Rapoportcfda0522017-02-22 15:43:37 -08001763 if (vma && userfaultfd_missing(vma)) {
1764 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1765 return 0;
1766 }
1767
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001768 /* shmem_symlink() */
1769 if (mapping->a_ops != &shmem_aops)
1770 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001771 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001772 goto alloc_nohuge;
1773 if (shmem_huge == SHMEM_HUGE_FORCE)
1774 goto alloc_huge;
1775 switch (sbinfo->huge) {
1776 loff_t i_size;
1777 pgoff_t off;
1778 case SHMEM_HUGE_NEVER:
1779 goto alloc_nohuge;
1780 case SHMEM_HUGE_WITHIN_SIZE:
1781 off = round_up(index, HPAGE_PMD_NR);
1782 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1783 if (i_size >= HPAGE_PMD_SIZE &&
1784 i_size >> PAGE_SHIFT >= off)
1785 goto alloc_huge;
1786 /* fallthrough */
1787 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001788 if (sgp_huge == SGP_HUGE)
1789 goto alloc_huge;
1790 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001791 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001792 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001794alloc_huge:
Mike Rapoport0f079692017-09-06 16:22:59 -07001795 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001796 if (IS_ERR(page)) {
Mike Rapoport0f079692017-09-06 16:22:59 -07001797alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001798 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001799 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001800 if (IS_ERR(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001801 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001802 error = PTR_ERR(page);
1803 page = NULL;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001804 if (error != -ENOSPC)
1805 goto failed;
1806 /*
1807 * Try to reclaim some spece by splitting a huge page
1808 * beyond i_size on the filesystem.
1809 */
1810 while (retry--) {
1811 int ret;
1812 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1813 if (ret == SHRINK_STOP)
1814 break;
1815 if (ret)
1816 goto alloc_nohuge;
1817 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001818 goto failed;
1819 }
1820
1821 if (PageTransHuge(page))
1822 hindex = round_down(index, HPAGE_PMD_NR);
1823 else
1824 hindex = index;
1825
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001826 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001827 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001828
Tejun Heo2cf85582018-07-03 11:14:56 -04001829 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001830 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001831 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001832 goto unacct;
1833 error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1834 compound_order(page));
Hugh Dickinsb065b432012-07-11 14:02:48 -07001835 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001836 error = shmem_add_to_page_cache(page, mapping, hindex,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001837 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001838 radix_tree_preload_end();
1839 }
1840 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001841 mem_cgroup_cancel_charge(page, memcg,
1842 PageTransHuge(page));
1843 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001844 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001845 mem_cgroup_commit_charge(page, memcg, false,
1846 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001847 lru_cache_add_anon(page);
1848
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001849 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001850 info->alloced += 1 << compound_order(page);
1851 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001852 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001853 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001854 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001855
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001856 if (PageTransHuge(page) &&
1857 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1858 hindex + HPAGE_PMD_NR - 1) {
1859 /*
1860 * Part of the huge page is beyond i_size: subject
1861 * to shrink under memory pressure.
1862 */
1863 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001864 /*
1865 * _careful to defend against unlocked access to
1866 * ->shrink_list in shmem_unused_huge_shrink()
1867 */
1868 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001869 list_add_tail(&info->shrinklist,
1870 &sbinfo->shrinklist);
1871 sbinfo->shrinklist_len++;
1872 }
1873 spin_unlock(&sbinfo->shrinklist_lock);
1874 }
1875
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001876 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001877 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1878 */
1879 if (sgp == SGP_FALLOC)
1880 sgp = SGP_WRITE;
1881clear:
1882 /*
1883 * Let SGP_WRITE caller clear ends if write does not fill page;
1884 * but SGP_FALLOC on a page fallocated earlier must initialize
1885 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001886 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001887 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1888 struct page *head = compound_head(page);
1889 int i;
1890
1891 for (i = 0; i < (1 << compound_order(head)); i++) {
1892 clear_highpage(head + i);
1893 flush_dcache_page(head + i);
1894 }
1895 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001896 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001897 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001898
Hugh Dickins54af60422011-08-03 16:21:24 -07001899 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001900 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001901 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001902 if (alloced) {
1903 ClearPageDirty(page);
1904 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001905 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001906 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001907 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001908 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001909 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001910 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001911 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001912 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001913 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001914
Nick Piggind0217ac2007-07-19 01:47:03 -07001915 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001916 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001918unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001919 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001920
1921 if (PageTransHuge(page)) {
1922 unlock_page(page);
1923 put_page(page);
1924 goto alloc_nohuge;
1925 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001926failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001927 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001928 error = -EEXIST;
1929unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001930 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001931 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001932 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001933 }
1934 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001935 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001936 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001937 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001938 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001939 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001940 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001941 goto repeat;
1942 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001945/*
1946 * This is like autoremove_wake_function, but it removes the wait queue
1947 * entry unconditionally - even if something else had already woken the
1948 * target.
1949 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001950static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001951{
1952 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001953 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001954 return ret;
1955}
1956
Souptick Joarder20acce62018-06-07 17:09:17 -07001957static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Dave Jiang11bac802017-02-24 14:56:41 -08001959 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001960 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001961 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001962 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001963 int err;
1964 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001966 /*
1967 * Trinity finds that probing a hole which tmpfs is punching can
1968 * prevent the hole-punch from ever completing: which in turn
1969 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001970 * faulting pages into the hole while it's being punched. Although
1971 * shmem_undo_range() does remove the additions, it may be unable to
1972 * keep up, as each new page needs its own unmap_mapping_range() call,
1973 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1974 *
1975 * It does not matter if we sometimes reach this check just before the
1976 * hole-punch begins, so that one fault then races with the punch:
1977 * we just need to make racing faults a rare case.
1978 *
1979 * The implementation below would be much simpler if we just used a
1980 * standard mutex or completion: but we cannot take i_mutex in fault,
1981 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001982 */
1983 if (unlikely(inode->i_private)) {
1984 struct shmem_falloc *shmem_falloc;
1985
1986 spin_lock(&inode->i_lock);
1987 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001988 if (shmem_falloc &&
1989 shmem_falloc->waitq &&
1990 vmf->pgoff >= shmem_falloc->start &&
1991 vmf->pgoff < shmem_falloc->next) {
1992 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001993 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001994
1995 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001996 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1997 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001998 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001999 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07002000 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002001 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002002
2003 shmem_falloc_waitq = shmem_falloc->waitq;
2004 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
2005 TASK_UNINTERRUPTIBLE);
2006 spin_unlock(&inode->i_lock);
2007 schedule();
2008
2009 /*
2010 * shmem_falloc_waitq points into the shmem_fallocate()
2011 * stack of the hole-punching task: shmem_falloc_waitq
2012 * is usually invalid by the time we reach here, but
2013 * finish_wait() does not dereference it in that case;
2014 * though i_lock needed lest racing with wake_up_all().
2015 */
2016 spin_lock(&inode->i_lock);
2017 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2018 spin_unlock(&inode->i_lock);
2019 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002020 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002021 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002022 }
2023
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002024 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002025
2026 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2027 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002028 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002029 else if (vma->vm_flags & VM_HUGEPAGE)
2030 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002031
Souptick Joarder20acce62018-06-07 17:09:17 -07002032 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002033 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002034 if (err)
2035 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002036 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002039unsigned long shmem_get_unmapped_area(struct file *file,
2040 unsigned long uaddr, unsigned long len,
2041 unsigned long pgoff, unsigned long flags)
2042{
2043 unsigned long (*get_area)(struct file *,
2044 unsigned long, unsigned long, unsigned long, unsigned long);
2045 unsigned long addr;
2046 unsigned long offset;
2047 unsigned long inflated_len;
2048 unsigned long inflated_addr;
2049 unsigned long inflated_offset;
2050
2051 if (len > TASK_SIZE)
2052 return -ENOMEM;
2053
2054 get_area = current->mm->get_unmapped_area;
2055 addr = get_area(file, uaddr, len, pgoff, flags);
2056
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002057 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002058 return addr;
2059 if (IS_ERR_VALUE(addr))
2060 return addr;
2061 if (addr & ~PAGE_MASK)
2062 return addr;
2063 if (addr > TASK_SIZE - len)
2064 return addr;
2065
2066 if (shmem_huge == SHMEM_HUGE_DENY)
2067 return addr;
2068 if (len < HPAGE_PMD_SIZE)
2069 return addr;
2070 if (flags & MAP_FIXED)
2071 return addr;
2072 /*
2073 * Our priority is to support MAP_SHARED mapped hugely;
2074 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemova3071de2020-01-13 16:29:13 -08002075 * But if caller specified an address hint and we allocated area there
2076 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002077 */
Kirill A. Shutemova3071de2020-01-13 16:29:13 -08002078 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002079 return addr;
2080
2081 if (shmem_huge != SHMEM_HUGE_FORCE) {
2082 struct super_block *sb;
2083
2084 if (file) {
2085 VM_BUG_ON(file->f_op != &shmem_file_operations);
2086 sb = file_inode(file)->i_sb;
2087 } else {
2088 /*
2089 * Called directly from mm/mmap.c, or drivers/char/mem.c
2090 * for "/dev/zero", to create a shared anonymous object.
2091 */
2092 if (IS_ERR(shm_mnt))
2093 return addr;
2094 sb = shm_mnt->mnt_sb;
2095 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002096 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002097 return addr;
2098 }
2099
2100 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2101 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2102 return addr;
2103 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2104 return addr;
2105
2106 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2107 if (inflated_len > TASK_SIZE)
2108 return addr;
2109 if (inflated_len < len)
2110 return addr;
2111
Kirill A. Shutemova3071de2020-01-13 16:29:13 -08002112 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002113 if (IS_ERR_VALUE(inflated_addr))
2114 return addr;
2115 if (inflated_addr & ~PAGE_MASK)
2116 return addr;
2117
2118 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2119 inflated_addr += offset - inflated_offset;
2120 if (inflated_offset > offset)
2121 inflated_addr += HPAGE_PMD_SIZE;
2122
2123 if (inflated_addr > TASK_SIZE - len)
2124 return addr;
2125 return inflated_addr;
2126}
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002129static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Al Viro496ad9a2013-01-23 17:07:38 -05002131 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002132 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002135static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2136 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137{
Al Viro496ad9a2013-01-23 17:07:38 -05002138 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002139 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002141 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2142 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144#endif
2145
2146int shmem_lock(struct file *file, int lock, struct user_struct *user)
2147{
Al Viro496ad9a2013-01-23 17:07:38 -05002148 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 struct shmem_inode_info *info = SHMEM_I(inode);
2150 int retval = -ENOMEM;
2151
Hugh Dickins4ad0f432020-04-20 18:14:14 -07002152 /*
2153 * What serializes the accesses to info->flags?
2154 * ipc_lock_object() when called from shmctl_do_lock(),
2155 * no serialization needed when called from shm_destroy().
2156 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (lock && !(info->flags & VM_LOCKED)) {
2158 if (!user_shm_lock(inode->i_size, user))
2159 goto out_nomem;
2160 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002161 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 if (!lock && (info->flags & VM_LOCKED) && user) {
2164 user_shm_unlock(inode->i_size, user);
2165 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002166 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return retval;
2172}
2173
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002174static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
2176 file_accessed(file);
2177 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002178 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002179 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2180 (vma->vm_end & HPAGE_PMD_MASK)) {
2181 khugepaged_enter(vma, vma->vm_flags);
2182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return 0;
2184}
2185
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002186static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002187 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
2189 struct inode *inode;
2190 struct shmem_inode_info *info;
2191 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2192
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002193 if (shmem_reserve_inode(sb))
2194 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 inode = new_inode(sb);
2197 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002198 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002199 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002201 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002202 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 info = SHMEM_I(inode);
2204 memset(info, 0, (char *)inode - (char *)info);
2205 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002206 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002207 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002208 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002210 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002211 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 switch (mode & S_IFMT) {
2214 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002215 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 init_special_inode(inode, mode, dev);
2217 break;
2218 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002219 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 inode->i_op = &shmem_inode_operations;
2221 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002222 mpol_shared_policy_init(&info->policy,
2223 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 break;
2225 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002226 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* Some things misbehave if size == 0 on a directory */
2228 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2229 inode->i_op = &shmem_dir_inode_operations;
2230 inode->i_fop = &simple_dir_operations;
2231 break;
2232 case S_IFLNK:
2233 /*
2234 * Must not load anything in the rbtree,
2235 * mpol_free_shared_policy will not be called.
2236 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002237 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 break;
2239 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002240
2241 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002242 } else
2243 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 return inode;
2245}
2246
Johannes Weiner0cd61442014-04-03 14:47:46 -07002247bool shmem_mapping(struct address_space *mapping)
2248{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002249 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002250}
2251
Mike Rapoport8d103962017-09-06 16:23:02 -07002252static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2253 pmd_t *dst_pmd,
2254 struct vm_area_struct *dst_vma,
2255 unsigned long dst_addr,
2256 unsigned long src_addr,
2257 bool zeropage,
2258 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002259{
2260 struct inode *inode = file_inode(dst_vma->vm_file);
2261 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002262 struct address_space *mapping = inode->i_mapping;
2263 gfp_t gfp = mapping_gfp_mask(mapping);
2264 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2265 struct mem_cgroup *memcg;
2266 spinlock_t *ptl;
2267 void *page_kaddr;
2268 struct page *page;
2269 pte_t _dst_pte, *dst_pte;
2270 int ret;
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002271 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002272
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002273 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002274 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002275 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002276
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002277 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002278 page = shmem_alloc_page(gfp, info, pgoff);
2279 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002280 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002281
Mike Rapoport8d103962017-09-06 16:23:02 -07002282 if (!zeropage) { /* mcopy_atomic */
2283 page_kaddr = kmap_atomic(page);
2284 ret = copy_from_user(page_kaddr,
2285 (const void __user *)src_addr,
2286 PAGE_SIZE);
2287 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002288
Mike Rapoport8d103962017-09-06 16:23:02 -07002289 /* fallback to copy_from_user outside mmap_sem */
2290 if (unlikely(ret)) {
2291 *pagep = page;
2292 shmem_inode_unacct_blocks(inode, 1);
2293 /* don't free the page */
Andrea Arcangeli10f98c12018-11-30 14:09:25 -08002294 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002295 }
2296 } else { /* mfill_zeropage_atomic */
2297 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002298 }
2299 } else {
2300 page = *pagep;
2301 *pagep = NULL;
2302 }
2303
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002304 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2305 __SetPageLocked(page);
2306 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002307 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002308
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002309 ret = -EFAULT;
2310 offset = linear_page_index(dst_vma, dst_addr);
2311 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2312 if (unlikely(offset >= max_off))
2313 goto out_release;
2314
Tejun Heo2cf85582018-07-03 11:14:56 -04002315 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002316 if (ret)
2317 goto out_release;
2318
2319 ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
2320 if (!ret) {
2321 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL);
2322 radix_tree_preload_end();
2323 }
2324 if (ret)
2325 goto out_release_uncharge;
2326
2327 mem_cgroup_commit_charge(page, memcg, false, false);
2328
2329 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2330 if (dst_vma->vm_flags & VM_WRITE)
2331 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangeli8f193a72018-11-30 14:09:43 -08002332 else {
2333 /*
2334 * We don't set the pte dirty if the vma has no
2335 * VM_WRITE permission, so mark the page dirty or it
2336 * could be freed from under us. We could do it
2337 * unconditionally before unlock_page(), but doing it
2338 * only if VM_WRITE is not set is faster.
2339 */
2340 set_page_dirty(page);
2341 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002342
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002343 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002344
2345 ret = -EFAULT;
2346 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2347 if (unlikely(offset >= max_off))
2348 goto out_release_uncharge_unlock;
2349
2350 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002351 if (!pte_none(*dst_pte))
2352 goto out_release_uncharge_unlock;
2353
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002354 lru_cache_add_anon(page);
2355
Yang Shi905df102020-04-20 18:14:17 -07002356 spin_lock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002357 info->alloced++;
2358 inode->i_blocks += BLOCKS_PER_PAGE;
2359 shmem_recalc_inode(inode);
Yang Shi905df102020-04-20 18:14:17 -07002360 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002361
2362 inc_mm_counter(dst_mm, mm_counter_file(page));
2363 page_add_file_rmap(page, false);
2364 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2365
2366 /* No need to invalidate - it was non-present before */
2367 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002368 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002369 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002370 ret = 0;
2371out:
2372 return ret;
2373out_release_uncharge_unlock:
2374 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeli8f193a72018-11-30 14:09:43 -08002375 ClearPageDirty(page);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002376 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002377out_release_uncharge:
2378 mem_cgroup_cancel_charge(page, memcg, false);
2379out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002380 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002381 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002382out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002383 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002384 goto out;
2385}
2386
Mike Rapoport8d103962017-09-06 16:23:02 -07002387int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2388 pmd_t *dst_pmd,
2389 struct vm_area_struct *dst_vma,
2390 unsigned long dst_addr,
2391 unsigned long src_addr,
2392 struct page **pagep)
2393{
2394 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2395 dst_addr, src_addr, false, pagep);
2396}
2397
2398int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2399 pmd_t *dst_pmd,
2400 struct vm_area_struct *dst_vma,
2401 unsigned long dst_addr)
2402{
2403 struct page *page = NULL;
2404
2405 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2406 dst_addr, 0, true, &page);
2407}
2408
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002410static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002411static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002413#ifdef CONFIG_TMPFS_XATTR
2414static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2415#else
2416#define shmem_initxattrs NULL
2417#endif
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002420shmem_write_begin(struct file *file, struct address_space *mapping,
2421 loff_t pos, unsigned len, unsigned flags,
2422 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Nick Piggin800d15a2007-10-16 01:25:03 -07002424 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002425 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002426 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002427
2428 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002429 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002430 if (info->seals & F_SEAL_WRITE)
2431 return -EPERM;
2432 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2433 return -EPERM;
2434 }
2435
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002436 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002437}
2438
2439static int
2440shmem_write_end(struct file *file, struct address_space *mapping,
2441 loff_t pos, unsigned len, unsigned copied,
2442 struct page *page, void *fsdata)
2443{
2444 struct inode *inode = mapping->host;
2445
Hugh Dickinsd3602442008-02-04 22:28:44 -08002446 if (pos + copied > inode->i_size)
2447 i_size_write(inode, pos + copied);
2448
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002449 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002450 struct page *head = compound_head(page);
2451 if (PageTransCompound(page)) {
2452 int i;
2453
2454 for (i = 0; i < HPAGE_PMD_NR; i++) {
2455 if (head + i == page)
2456 continue;
2457 clear_highpage(head + i);
2458 flush_dcache_page(head + i);
2459 }
2460 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002461 if (copied < PAGE_SIZE) {
2462 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002463 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002464 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002465 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002466 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002467 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002468 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002469 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002470 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002471
Nick Piggin800d15a2007-10-16 01:25:03 -07002472 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Al Viro2ba5bbe2014-04-02 20:00:02 -04002475static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Al Viro6e58e792014-02-03 17:07:03 -05002477 struct file *file = iocb->ki_filp;
2478 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002480 pgoff_t index;
2481 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002482 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002483 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002484 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002485 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002486
2487 /*
2488 * Might this read be for a stacking filesystem? Then when reading
2489 * holes of a sparse file, we actually need to allocate those pages,
2490 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2491 */
Al Viro777eda22014-12-17 04:46:46 -05002492 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002493 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002495 index = *ppos >> PAGE_SHIFT;
2496 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 for (;;) {
2499 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002500 pgoff_t end_index;
2501 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 loff_t i_size = i_size_read(inode);
2503
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002504 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (index > end_index)
2506 break;
2507 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002508 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 if (nr <= offset)
2510 break;
2511 }
2512
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002513 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002514 if (error) {
2515 if (error == -EINVAL)
2516 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 break;
2518 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002519 if (page) {
2520 if (sgp == SGP_CACHE)
2521 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002522 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 /*
2526 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002527 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002529 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002531 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002533 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (nr <= offset) {
2535 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002536 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 break;
2538 }
2539 }
2540 nr -= offset;
2541
2542 if (page) {
2543 /*
2544 * If users can be writing to this page using arbitrary
2545 * virtual addresses, take care about potential aliasing
2546 * before reading the page on the kernel side.
2547 */
2548 if (mapping_writably_mapped(mapping))
2549 flush_dcache_page(page);
2550 /*
2551 * Mark the page accessed if we read the beginning.
2552 */
2553 if (!offset)
2554 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002555 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002557 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 /*
2561 * Ok, we have the page, and it's up-to-date, so
2562 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002564 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002565 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002567 index += offset >> PAGE_SHIFT;
2568 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002570 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002571 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 break;
Al Viro6e58e792014-02-03 17:07:03 -05002573 if (ret < nr) {
2574 error = -EFAULT;
2575 break;
2576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 cond_resched();
2578 }
2579
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002580 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002581 file_accessed(file);
2582 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002585/*
2586 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2587 */
2588static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002589 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002590{
2591 struct page *page;
2592 struct pagevec pvec;
2593 pgoff_t indices[PAGEVEC_SIZE];
2594 bool done = false;
2595 int i;
2596
Mel Gorman86679822017-11-15 17:37:52 -08002597 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002598 pvec.nr = 1; /* start small: we may be there already */
2599 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002600 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002601 pvec.nr, pvec.pages, indices);
2602 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002603 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002604 index = end;
2605 break;
2606 }
2607 for (i = 0; i < pvec.nr; i++, index++) {
2608 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002609 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002610 done = true;
2611 break;
2612 }
2613 index = indices[i];
2614 }
2615 page = pvec.pages[i];
2616 if (page && !radix_tree_exceptional_entry(page)) {
2617 if (!PageUptodate(page))
2618 page = NULL;
2619 }
2620 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002621 (page && whence == SEEK_DATA) ||
2622 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002623 done = true;
2624 break;
2625 }
2626 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002627 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002628 pagevec_release(&pvec);
2629 pvec.nr = PAGEVEC_SIZE;
2630 cond_resched();
2631 }
2632 return index;
2633}
2634
Andrew Morton965c8e52012-12-17 15:59:39 -08002635static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002636{
2637 struct address_space *mapping = file->f_mapping;
2638 struct inode *inode = mapping->host;
2639 pgoff_t start, end;
2640 loff_t new_offset;
2641
Andrew Morton965c8e52012-12-17 15:59:39 -08002642 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2643 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002644 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002645 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002646 /* We're holding i_mutex so we can access i_size directly */
2647
Yufen Yudb89fc02018-11-16 15:08:39 -08002648 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002649 offset = -ENXIO;
2650 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002651 start = offset >> PAGE_SHIFT;
2652 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002653 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002654 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002655 if (new_offset > offset) {
2656 if (new_offset < inode->i_size)
2657 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002658 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002659 offset = -ENXIO;
2660 else
2661 offset = inode->i_size;
2662 }
2663 }
2664
Hugh Dickins387aae62013-08-04 11:30:25 -07002665 if (offset >= 0)
2666 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002667 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002668 return offset;
2669}
2670
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002671static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2672 loff_t len)
2673{
Al Viro496ad9a2013-01-23 17:07:38 -05002674 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002675 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002676 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002677 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002678 pgoff_t start, index, end;
2679 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002680
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002681 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2682 return -EOPNOTSUPP;
2683
Al Viro59551022016-01-22 15:40:57 -05002684 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002685
2686 if (mode & FALLOC_FL_PUNCH_HOLE) {
2687 struct address_space *mapping = file->f_mapping;
2688 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2689 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002690 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002691
David Herrmann40e041a2014-08-08 14:25:27 -07002692 /* protected by i_mutex */
2693 if (info->seals & F_SEAL_WRITE) {
2694 error = -EPERM;
2695 goto out;
2696 }
2697
Hugh Dickins8e205f72014-07-23 14:00:10 -07002698 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Jun32b02bf2019-11-30 17:58:11 -08002699 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002700 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2701 spin_lock(&inode->i_lock);
2702 inode->i_private = &shmem_falloc;
2703 spin_unlock(&inode->i_lock);
2704
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002705 if ((u64)unmap_end > (u64)unmap_start)
2706 unmap_mapping_range(mapping, unmap_start,
2707 1 + unmap_end - unmap_start, 0);
2708 shmem_truncate_range(inode, offset, offset + len - 1);
2709 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002710
2711 spin_lock(&inode->i_lock);
2712 inode->i_private = NULL;
2713 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002714 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002715 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002716 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002717 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002718 }
2719
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002720 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2721 error = inode_newsize_ok(inode, offset + len);
2722 if (error)
2723 goto out;
2724
David Herrmann40e041a2014-08-08 14:25:27 -07002725 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2726 error = -EPERM;
2727 goto out;
2728 }
2729
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002730 start = offset >> PAGE_SHIFT;
2731 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002732 /* Try to avoid a swapstorm if len is impossible to satisfy */
2733 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2734 error = -ENOSPC;
2735 goto out;
2736 }
2737
Hugh Dickins8e205f72014-07-23 14:00:10 -07002738 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002739 shmem_falloc.start = start;
2740 shmem_falloc.next = start;
2741 shmem_falloc.nr_falloced = 0;
2742 shmem_falloc.nr_unswapped = 0;
2743 spin_lock(&inode->i_lock);
2744 inode->i_private = &shmem_falloc;
2745 spin_unlock(&inode->i_lock);
2746
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002747 for (index = start; index < end; index++) {
2748 struct page *page;
2749
2750 /*
2751 * Good, the fallocate(2) manpage permits EINTR: we may have
2752 * been interrupted because we are using up too much memory.
2753 */
2754 if (signal_pending(current))
2755 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002756 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2757 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002758 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002759 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002760 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002761 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002762 if (index > start) {
2763 shmem_undo_range(inode,
2764 (loff_t)start << PAGE_SHIFT,
2765 ((loff_t)index << PAGE_SHIFT) - 1, true);
2766 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002767 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002768 }
2769
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002770 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002771 * Inform shmem_writepage() how far we have reached.
2772 * No need for lock or barrier: we have the page lock.
2773 */
2774 shmem_falloc.next++;
2775 if (!PageUptodate(page))
2776 shmem_falloc.nr_falloced++;
2777
2778 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002779 * If !PageUptodate, leave it that way so that freeable pages
2780 * can be recognized if we need to rollback on error later.
2781 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002782 * than free the pages we are allocating (and SGP_CACHE pages
2783 * might still be clean: we now need to mark those dirty too).
2784 */
2785 set_page_dirty(page);
2786 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002787 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002788 cond_resched();
2789 }
2790
2791 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2792 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002793 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002794undone:
2795 spin_lock(&inode->i_lock);
2796 inode->i_private = NULL;
2797 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002798out:
Al Viro59551022016-01-22 15:40:57 -05002799 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002800 return error;
2801}
2802
David Howells726c3342006-06-23 02:02:58 -07002803static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
David Howells726c3342006-06-23 02:02:58 -07002805 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002808 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002810 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002812 buf->f_bavail =
2813 buf->f_bfree = sbinfo->max_blocks -
2814 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002815 }
2816 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 buf->f_files = sbinfo->max_inodes;
2818 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 /* else leave those fields 0 like simple_statfs */
2821 return 0;
2822}
2823
2824/*
2825 * File creation. Allocate an inode, and we're done..
2826 */
2827static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002828shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002830 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 int error = -ENOSPC;
2832
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002833 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002835 error = simple_acl_create(dir, inode);
2836 if (error)
2837 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002838 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002839 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002840 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002841 if (error && error != -EOPNOTSUPP)
2842 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002843
Al Viro718deb62009-12-16 19:35:36 -05002844 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002846 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 d_instantiate(dentry, inode);
2848 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
2850 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002851out_iput:
2852 iput(inode);
2853 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Al Viro60545d02013-06-07 01:20:27 -04002856static int
2857shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2858{
2859 struct inode *inode;
2860 int error = -ENOSPC;
2861
2862 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2863 if (inode) {
2864 error = security_inode_init_security(inode, dir,
2865 NULL,
2866 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002867 if (error && error != -EOPNOTSUPP)
2868 goto out_iput;
2869 error = simple_acl_create(dir, inode);
2870 if (error)
2871 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002872 d_tmpfile(dentry, inode);
2873 }
2874 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002875out_iput:
2876 iput(inode);
2877 return error;
Al Viro60545d02013-06-07 01:20:27 -04002878}
2879
Al Viro18bb1db2011-07-26 01:41:39 -04002880static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 int error;
2883
2884 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2885 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002886 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return 0;
2888}
2889
Al Viro4acdaf22011-07-26 01:42:34 -04002890static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002891 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
2893 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2894}
2895
2896/*
2897 * Link a file..
2898 */
2899static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2900{
David Howells75c3cfa2015-03-17 22:26:12 +00002901 struct inode *inode = d_inode(old_dentry);
Darrick J. Wongb3139fb2019-02-22 22:35:32 -08002902 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 /*
2905 * No ordinary (disk based) filesystem counts links as inodes;
2906 * but each new link needs a new dentry, pinning lowmem, and
2907 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong064a61d2019-02-21 08:48:09 -08002908 * But if an O_TMPFILE file is linked into the tmpfs, the
2909 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 */
Darrick J. Wong064a61d2019-02-21 08:48:09 -08002911 if (inode->i_nlink) {
2912 ret = shmem_reserve_inode(inode->i_sb);
2913 if (ret)
2914 goto out;
2915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002918 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002919 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002920 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 dget(dentry); /* Extra pinning count for the created dentry */
2922 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002923out:
2924 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
2927static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2928{
David Howells75c3cfa2015-03-17 22:26:12 +00002929 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002931 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2932 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002935 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002936 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 dput(dentry); /* Undo the count from "create" - this does all the work */
2938 return 0;
2939}
2940
2941static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2942{
2943 if (!simple_empty(dentry))
2944 return -ENOTEMPTY;
2945
David Howells75c3cfa2015-03-17 22:26:12 +00002946 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002947 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return shmem_unlink(dir, dentry);
2949}
2950
Miklos Szeredi37456772014-07-23 15:15:34 +02002951static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2952{
David Howellse36cb0b2015-01-29 12:02:35 +00002953 bool old_is_dir = d_is_dir(old_dentry);
2954 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002955
2956 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2957 if (old_is_dir) {
2958 drop_nlink(old_dir);
2959 inc_nlink(new_dir);
2960 } else {
2961 drop_nlink(new_dir);
2962 inc_nlink(old_dir);
2963 }
2964 }
2965 old_dir->i_ctime = old_dir->i_mtime =
2966 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002967 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002968 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002969
2970 return 0;
2971}
2972
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002973static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2974{
2975 struct dentry *whiteout;
2976 int error;
2977
2978 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2979 if (!whiteout)
2980 return -ENOMEM;
2981
2982 error = shmem_mknod(old_dir, whiteout,
2983 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2984 dput(whiteout);
2985 if (error)
2986 return error;
2987
2988 /*
2989 * Cheat and hash the whiteout while the old dentry is still in
2990 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2991 *
2992 * d_lookup() will consistently find one of them at this point,
2993 * not sure which one, but that isn't even important.
2994 */
2995 d_rehash(whiteout);
2996 return 0;
2997}
2998
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999/*
3000 * The VFS layer already does all the dentry stuff for rename,
3001 * we just have to decrement the usage count for the target if
3002 * it exists so that the VFS layer correctly free's it when it
3003 * gets overwritten.
3004 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003005static 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 -07003006{
David Howells75c3cfa2015-03-17 22:26:12 +00003007 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 int they_are_dirs = S_ISDIR(inode->i_mode);
3009
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003010 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003011 return -EINVAL;
3012
Miklos Szeredi37456772014-07-23 15:15:34 +02003013 if (flags & RENAME_EXCHANGE)
3014 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3015
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (!simple_empty(new_dentry))
3017 return -ENOTEMPTY;
3018
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003019 if (flags & RENAME_WHITEOUT) {
3020 int error;
3021
3022 error = shmem_whiteout(old_dir, old_dentry);
3023 if (error)
3024 return error;
3025 }
3026
David Howells75c3cfa2015-03-17 22:26:12 +00003027 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003029 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003030 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003031 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003032 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003034 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003035 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037
3038 old_dir->i_size -= BOGO_DIRENT_SIZE;
3039 new_dir->i_size += BOGO_DIRENT_SIZE;
3040 old_dir->i_ctime = old_dir->i_mtime =
3041 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003042 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 return 0;
3044}
3045
3046static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3047{
3048 int error;
3049 int len;
3050 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003051 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
3053 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003054 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 return -ENAMETOOLONG;
3056
Joe Perches0825a6f2018-06-14 15:27:58 -07003057 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3058 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (!inode)
3060 return -ENOSPC;
3061
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003062 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003063 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003064 if (error) {
3065 if (error != -EOPNOTSUPP) {
3066 iput(inode);
3067 return error;
3068 }
3069 error = 0;
3070 }
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003073 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003074 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3075 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003076 iput(inode);
3077 return -ENOMEM;
3078 }
3079 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003081 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003082 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 if (error) {
3084 iput(inode);
3085 return error;
3086 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003087 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003089 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003090 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003092 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003093 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003096 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 d_instantiate(dentry, inode);
3098 dget(dentry);
3099 return 0;
3100}
3101
Al Virofceef392015-12-29 15:58:39 -05003102static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Al Virofceef392015-12-29 15:58:39 -05003104 mark_page_accessed(arg);
3105 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Al Viro6b255392015-11-17 10:20:54 -05003108static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003109 struct inode *inode,
3110 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003113 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003114 if (!dentry) {
3115 page = find_get_page(inode->i_mapping, 0);
3116 if (!page)
3117 return ERR_PTR(-ECHILD);
3118 if (!PageUptodate(page)) {
3119 put_page(page);
3120 return ERR_PTR(-ECHILD);
3121 }
3122 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003123 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003124 if (error)
3125 return ERR_PTR(error);
3126 unlock_page(page);
3127 }
Al Virofceef392015-12-29 15:58:39 -05003128 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003129 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Eric Parisb09e0fa2011-05-24 17:12:39 -07003132#ifdef CONFIG_TMPFS_XATTR
3133/*
3134 * Superblocks without xattr inode operations may get some security.* xattr
3135 * support from the LSM "for free". As soon as we have any other xattrs
3136 * like ACLs, we also need to implement the security.* handlers at
3137 * filesystem level, though.
3138 */
3139
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003140/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003141 * Callback for security_inode_init_security() for acquiring xattrs.
3142 */
3143static int shmem_initxattrs(struct inode *inode,
3144 const struct xattr *xattr_array,
3145 void *fs_info)
3146{
3147 struct shmem_inode_info *info = SHMEM_I(inode);
3148 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003149 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003150 size_t len;
3151
3152 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003153 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003154 if (!new_xattr)
3155 return -ENOMEM;
3156
3157 len = strlen(xattr->name) + 1;
3158 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3159 GFP_KERNEL);
3160 if (!new_xattr->name) {
3161 kfree(new_xattr);
3162 return -ENOMEM;
3163 }
3164
3165 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3166 XATTR_SECURITY_PREFIX_LEN);
3167 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3168 xattr->name, len);
3169
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003170 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003171 }
3172
3173 return 0;
3174}
3175
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003176static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003177 struct dentry *unused, struct inode *inode,
3178 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003179{
Al Virob2968212016-04-10 20:48:24 -04003180 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003181
3182 name = xattr_full_name(handler, name);
3183 return simple_xattr_get(&info->xattrs, name, buffer, size);
3184}
3185
3186static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003187 struct dentry *unused, struct inode *inode,
3188 const char *name, const void *value,
3189 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003190{
Al Viro59301222016-05-27 10:19:30 -04003191 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003192
3193 name = xattr_full_name(handler, name);
3194 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3195}
3196
3197static const struct xattr_handler shmem_security_xattr_handler = {
3198 .prefix = XATTR_SECURITY_PREFIX,
3199 .get = shmem_xattr_handler_get,
3200 .set = shmem_xattr_handler_set,
3201};
3202
3203static const struct xattr_handler shmem_trusted_xattr_handler = {
3204 .prefix = XATTR_TRUSTED_PREFIX,
3205 .get = shmem_xattr_handler_get,
3206 .set = shmem_xattr_handler_set,
3207};
3208
Eric Parisb09e0fa2011-05-24 17:12:39 -07003209static const struct xattr_handler *shmem_xattr_handlers[] = {
3210#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003211 &posix_acl_access_xattr_handler,
3212 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003213#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003214 &shmem_security_xattr_handler,
3215 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003216 NULL
3217};
3218
Eric Parisb09e0fa2011-05-24 17:12:39 -07003219static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3220{
David Howells75c3cfa2015-03-17 22:26:12 +00003221 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003222 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003223}
3224#endif /* CONFIG_TMPFS_XATTR */
3225
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003226static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003227 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003228#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003229 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003230#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231};
3232
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003233static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003234 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003235#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003236 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003237#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003238};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003239
David M. Grimes91828a42006-10-17 00:09:45 -07003240static struct dentry *shmem_get_parent(struct dentry *child)
3241{
3242 return ERR_PTR(-ESTALE);
3243}
3244
3245static int shmem_match(struct inode *ino, void *vfh)
3246{
3247 __u32 *fh = vfh;
3248 __u64 inum = fh[2];
3249 inum = (inum << 32) | fh[1];
3250 return ino->i_ino == inum && fh[0] == ino->i_generation;
3251}
3252
Amir Goldstein12ba7802018-06-07 17:07:15 -07003253/* Find any alias of inode, but prefer a hashed alias */
3254static struct dentry *shmem_find_alias(struct inode *inode)
3255{
3256 struct dentry *alias = d_find_alias(inode);
3257
3258 return alias ?: d_find_any_alias(inode);
3259}
3260
3261
Christoph Hellwig480b1162007-10-21 16:42:13 -07003262static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3263 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003264{
David M. Grimes91828a42006-10-17 00:09:45 -07003265 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003266 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003267 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003268
Christoph Hellwig480b1162007-10-21 16:42:13 -07003269 if (fh_len < 3)
3270 return NULL;
3271
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003272 inum = fid->raw[2];
3273 inum = (inum << 32) | fid->raw[1];
3274
Christoph Hellwig480b1162007-10-21 16:42:13 -07003275 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3276 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003277 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003278 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003279 iput(inode);
3280 }
3281
Christoph Hellwig480b1162007-10-21 16:42:13 -07003282 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003283}
3284
Al Virob0b03822012-04-02 14:34:06 -04003285static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3286 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003287{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303288 if (*len < 3) {
3289 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003290 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303291 }
David M. Grimes91828a42006-10-17 00:09:45 -07003292
Al Viro1d3382cb2010-10-23 15:19:20 -04003293 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003294 /* Unfortunately insert_inode_hash is not idempotent,
3295 * so as we hash inodes here rather than at creation
3296 * time, we need a lock to ensure we only try
3297 * to do it once
3298 */
3299 static DEFINE_SPINLOCK(lock);
3300 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003301 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003302 __insert_inode_hash(inode,
3303 inode->i_ino + inode->i_generation);
3304 spin_unlock(&lock);
3305 }
3306
3307 fh[0] = inode->i_generation;
3308 fh[1] = inode->i_ino;
3309 fh[2] = ((__u64)inode->i_ino) >> 32;
3310
3311 *len = 3;
3312 return 1;
3313}
3314
Christoph Hellwig39655162007-10-21 16:42:17 -07003315static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003316 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003317 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003318 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003319};
3320
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003321static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3322 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
3324 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003325 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003326 uid_t uid;
3327 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003329 while (options != NULL) {
3330 this_char = options;
3331 for (;;) {
3332 /*
3333 * NUL-terminate this option: unfortunately,
3334 * mount options form a comma-separated list,
3335 * but mpol's nodelist may also contain commas.
3336 */
3337 options = strchr(options, ',');
3338 if (options == NULL)
3339 break;
3340 options++;
3341 if (!isdigit(*options)) {
3342 options[-1] = '\0';
3343 break;
3344 }
3345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 if (!*this_char)
3347 continue;
3348 if ((value = strchr(this_char,'=')) != NULL) {
3349 *value++ = 0;
3350 } else {
Joe Perches11705322016-03-17 14:19:50 -07003351 pr_err("tmpfs: No value for mount option '%s'\n",
3352 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003353 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 }
3355
3356 if (!strcmp(this_char,"size")) {
3357 unsigned long long size;
3358 size = memparse(value,&rest);
3359 if (*rest == '%') {
3360 size <<= PAGE_SHIFT;
3361 size *= totalram_pages;
3362 do_div(size, 100);
3363 rest++;
3364 }
3365 if (*rest)
3366 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003367 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003368 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003370 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (*rest)
3372 goto bad_val;
3373 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003374 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 if (*rest)
3376 goto bad_val;
3377 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003378 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003380 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 if (*rest)
3382 goto bad_val;
3383 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003384 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003386 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (*rest)
3388 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003389 sbinfo->uid = make_kuid(current_user_ns(), uid);
3390 if (!uid_valid(sbinfo->uid))
3391 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003393 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003395 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 if (*rest)
3397 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003398 sbinfo->gid = make_kgid(current_user_ns(), gid);
3399 if (!gid_valid(sbinfo->gid))
3400 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003401#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003402 } else if (!strcmp(this_char, "huge")) {
3403 int huge;
3404 huge = shmem_parse_huge(value);
3405 if (huge < 0)
3406 goto bad_val;
3407 if (!has_transparent_hugepage() &&
3408 huge != SHMEM_HUGE_NEVER)
3409 goto bad_val;
3410 sbinfo->huge = huge;
3411#endif
3412#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003413 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003414 mpol_put(mpol);
3415 mpol = NULL;
3416 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003417 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003418#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 } else {
Joe Perches11705322016-03-17 14:19:50 -07003420 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003421 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003424 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return 0;
3426
3427bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003428 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003430error:
3431 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 return 1;
3433
3434}
3435
3436static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3437{
3438 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003439 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003440 unsigned long inodes;
3441 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Greg Thelen5f001102013-02-22 16:36:01 -08003443 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003444 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003445 return error;
3446
3447 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003448 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003449 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003450 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003451 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003452 goto out;
3453 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003454 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003455 * but we must separately disallow unlimited->limited, because
3456 * in that case we have no record of how much is already in use.
3457 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003458 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003459 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003460 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003461 goto out;
3462
3463 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003464 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003465 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003466 sbinfo->max_inodes = config.max_inodes;
3467 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003468
Greg Thelen5f001102013-02-22 16:36:01 -08003469 /*
3470 * Preserve previous mempolicy unless mpol remount option was specified.
3471 */
3472 if (config.mpol) {
3473 mpol_put(sbinfo->mpol);
3474 sbinfo->mpol = config.mpol; /* transfers initial ref */
3475 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003476out:
3477 spin_unlock(&sbinfo->stat_lock);
3478 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003480
Al Viro34c80b12011-12-08 21:32:45 -05003481static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003482{
Al Viro34c80b12011-12-08 21:32:45 -05003483 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003484
3485 if (sbinfo->max_blocks != shmem_default_max_blocks())
3486 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003487 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003488 if (sbinfo->max_inodes != shmem_default_max_inodes())
3489 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003490 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003491 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003492 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3493 seq_printf(seq, ",uid=%u",
3494 from_kuid_munged(&init_user_ns, sbinfo->uid));
3495 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3496 seq_printf(seq, ",gid=%u",
3497 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003498#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003499 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3500 if (sbinfo->huge)
3501 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3502#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003503 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003504 return 0;
3505}
David Herrmann9183df22014-08-08 14:25:29 -07003506
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003507#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509static void shmem_put_super(struct super_block *sb)
3510{
Hugh Dickins602586a2010-08-17 15:23:56 -07003511 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3512
3513 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003514 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003515 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 sb->s_fs_info = NULL;
3517}
3518
Kay Sievers2b2af542009-04-30 15:23:42 +02003519int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003522 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003523 int err = -ENOMEM;
3524
3525 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003526 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003527 L1_CACHE_BYTES), GFP_KERNEL);
3528 if (!sbinfo)
3529 return -ENOMEM;
3530
Joe Perches0825a6f2018-06-14 15:27:58 -07003531 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003532 sbinfo->uid = current_fsuid();
3533 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003534 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003536#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 /*
3538 * Per default we only allow half of the physical ram per
3539 * tmpfs instance, limiting inodes to one per page of lowmem;
3540 * but the internal instance is left unlimited.
3541 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003542 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003543 sbinfo->max_blocks = shmem_default_max_blocks();
3544 sbinfo->max_inodes = shmem_default_max_inodes();
3545 if (shmem_parse_options(data, sbinfo, false)) {
3546 err = -EINVAL;
3547 goto failed;
3548 }
Al Viroca4e0512013-08-31 12:57:10 -04003549 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003550 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 }
David M. Grimes91828a42006-10-17 00:09:45 -07003552 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003553 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003555 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556#endif
3557
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003558 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003559 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003560 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003561 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003562 spin_lock_init(&sbinfo->shrinklist_lock);
3563 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003564
Hugh Dickins285b2c42011-08-03 16:21:20 -07003565 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003566 sb->s_blocksize = PAGE_SIZE;
3567 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 sb->s_magic = TMPFS_MAGIC;
3569 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003570 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003571#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003572 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003573#endif
3574#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003575 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003576#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003577 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003578
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003579 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 if (!inode)
3581 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003582 inode->i_uid = sbinfo->uid;
3583 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003584 sb->s_root = d_make_root(inode);
3585 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003586 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return 0;
3588
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589failed:
3590 shmem_put_super(sb);
3591 return err;
3592}
3593
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003594static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596static struct inode *shmem_alloc_inode(struct super_block *sb)
3597{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003598 struct shmem_inode_info *info;
3599 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3600 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003602 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003605static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003606{
3607 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003608 if (S_ISLNK(inode->i_mode))
3609 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003610 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3611}
3612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613static void shmem_destroy_inode(struct inode *inode)
3614{
Al Viro09208d12011-07-26 03:15:03 -04003615 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003617 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618}
3619
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003620static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003622 struct shmem_inode_info *info = foo;
3623 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624}
3625
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003626static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3629 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003630 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003633static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003635 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636}
3637
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003638static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003640 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003642 .write_begin = shmem_write_begin,
3643 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003645#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003646 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003647#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003648 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649};
3650
Helge Deller15ad7cd2006-12-06 20:40:36 -08003651static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003653 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003655 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003656 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003657 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003658 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003659 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003660 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003661 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662#endif
3663};
3664
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003665static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003666 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003667 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003668#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003669 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003670 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672};
3673
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003674static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675#ifdef CONFIG_TMPFS
3676 .create = shmem_create,
3677 .lookup = simple_lookup,
3678 .link = shmem_link,
3679 .unlink = shmem_unlink,
3680 .symlink = shmem_symlink,
3681 .mkdir = shmem_mkdir,
3682 .rmdir = shmem_rmdir,
3683 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003684 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003685 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003687#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003688 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003689#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003690#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003691 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003692 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003693#endif
3694};
3695
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003696static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003697#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003698 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003699#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003700#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003701 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003702 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003703#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704};
3705
Hugh Dickins759b9772007-03-05 00:30:28 -08003706static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .alloc_inode = shmem_alloc_inode,
3708 .destroy_inode = shmem_destroy_inode,
3709#ifdef CONFIG_TMPFS
3710 .statfs = shmem_statfs,
3711 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003712 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713#endif
Al Viro1f895f72010-06-05 19:10:41 -04003714 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 .drop_inode = generic_delete_inode,
3716 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003717#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3718 .nr_cached_objects = shmem_unused_huge_count,
3719 .free_cached_objects = shmem_unused_huge_scan,
3720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721};
3722
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003723static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003724 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003725 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726#ifdef CONFIG_NUMA
3727 .set_policy = shmem_set_policy,
3728 .get_policy = shmem_get_policy,
3729#endif
3730};
3731
Al Viro3c26ff62010-07-25 11:46:36 +04003732static struct dentry *shmem_mount(struct file_system_type *fs_type,
3733 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734{
Al Viro3c26ff62010-07-25 11:46:36 +04003735 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003738static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 .owner = THIS_MODULE,
3740 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003741 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003743 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003746int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
3748 int error;
3749
Rob Landley16203a72013-09-11 14:26:12 -07003750 /* If rootfs called this, don't re-init */
3751 if (shmem_inode_cachep)
3752 return 0;
3753
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003754 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003756 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003758 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 goto out2;
3760 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003761
Al Viroca4e0512013-08-31 12:57:10 -04003762 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 if (IS_ERR(shm_mnt)) {
3764 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003765 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 goto out1;
3767 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003768
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003769#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003770 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003771 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3772 else
3773 shmem_huge = 0; /* just in case it was patched */
3774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return 0;
3776
3777out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003778 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003780 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 shm_mnt = ERR_PTR(error);
3782 return error;
3783}
Matt Mackall853ac432009-01-06 14:40:20 -08003784
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003785#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003786static ssize_t shmem_enabled_show(struct kobject *kobj,
3787 struct kobj_attribute *attr, char *buf)
3788{
3789 int values[] = {
3790 SHMEM_HUGE_ALWAYS,
3791 SHMEM_HUGE_WITHIN_SIZE,
3792 SHMEM_HUGE_ADVISE,
3793 SHMEM_HUGE_NEVER,
3794 SHMEM_HUGE_DENY,
3795 SHMEM_HUGE_FORCE,
3796 };
3797 int i, count;
3798
3799 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3800 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3801
3802 count += sprintf(buf + count, fmt,
3803 shmem_format_huge(values[i]));
3804 }
3805 buf[count - 1] = '\n';
3806 return count;
3807}
3808
3809static ssize_t shmem_enabled_store(struct kobject *kobj,
3810 struct kobj_attribute *attr, const char *buf, size_t count)
3811{
3812 char tmp[16];
3813 int huge;
3814
3815 if (count + 1 > sizeof(tmp))
3816 return -EINVAL;
3817 memcpy(tmp, buf, count);
3818 tmp[count] = '\0';
3819 if (count && tmp[count - 1] == '\n')
3820 tmp[count - 1] = '\0';
3821
3822 huge = shmem_parse_huge(tmp);
3823 if (huge == -EINVAL)
3824 return -EINVAL;
3825 if (!has_transparent_hugepage() &&
3826 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3827 return -EINVAL;
3828
3829 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003830 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003831 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3832 return count;
3833}
3834
3835struct kobj_attribute shmem_enabled_attr =
3836 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003837#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003838
Arnd Bergmann3b337192016-08-10 16:27:44 -07003839#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003840bool shmem_huge_enabled(struct vm_area_struct *vma)
3841{
3842 struct inode *inode = file_inode(vma->vm_file);
3843 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3844 loff_t i_size;
3845 pgoff_t off;
3846
3847 if (shmem_huge == SHMEM_HUGE_FORCE)
3848 return true;
3849 if (shmem_huge == SHMEM_HUGE_DENY)
3850 return false;
3851 switch (sbinfo->huge) {
3852 case SHMEM_HUGE_NEVER:
3853 return false;
3854 case SHMEM_HUGE_ALWAYS:
3855 return true;
3856 case SHMEM_HUGE_WITHIN_SIZE:
3857 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3858 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3859 if (i_size >= HPAGE_PMD_SIZE &&
3860 i_size >> PAGE_SHIFT >= off)
3861 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003862 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003863 case SHMEM_HUGE_ADVISE:
3864 /* TODO: implement fadvise() hints */
3865 return (vma->vm_flags & VM_HUGEPAGE);
3866 default:
3867 VM_BUG_ON(1);
3868 return false;
3869 }
3870}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003871#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003872
Matt Mackall853ac432009-01-06 14:40:20 -08003873#else /* !CONFIG_SHMEM */
3874
3875/*
3876 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3877 *
3878 * This is intended for small system where the benefits of the full
3879 * shmem code (swap-backed and resource-limited) are outweighed by
3880 * their complexity. On systems without swap this code should be
3881 * effectively equivalent, but much lighter weight.
3882 */
3883
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003884static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003885 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003886 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003887 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003888 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003889};
3890
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003891int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003892{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003893 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003894
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003895 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003896 BUG_ON(IS_ERR(shm_mnt));
3897
3898 return 0;
3899}
3900
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003901int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003902{
3903 return 0;
3904}
3905
Hugh Dickins3f96b792009-09-21 17:03:37 -07003906int shmem_lock(struct file *file, int lock, struct user_struct *user)
3907{
3908 return 0;
3909}
3910
Hugh Dickins24513262012-01-20 14:34:21 -08003911void shmem_unlock_mapping(struct address_space *mapping)
3912{
3913}
3914
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003915#ifdef CONFIG_MMU
3916unsigned long shmem_get_unmapped_area(struct file *file,
3917 unsigned long addr, unsigned long len,
3918 unsigned long pgoff, unsigned long flags)
3919{
3920 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3921}
3922#endif
3923
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003924void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003925{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003926 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003927}
3928EXPORT_SYMBOL_GPL(shmem_truncate_range);
3929
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003930#define shmem_vm_ops generic_file_vm_ops
3931#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003932#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003933#define shmem_acct_size(flags, size) 0
3934#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003935
3936#endif /* CONFIG_SHMEM */
3937
3938/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Matthew Auld703321b2017-10-06 23:18:13 +01003940static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003941 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003944 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Matthew Auld703321b2017-10-06 23:18:13 +01003946 if (IS_ERR(mnt))
3947 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Hugh Dickins285b2c42011-08-03 16:21:20 -07003949 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return ERR_PTR(-EINVAL);
3951
3952 if (shmem_acct_size(flags, size))
3953 return ERR_PTR(-ENOMEM);
3954
Al Viro93dec2d2018-07-08 23:02:03 -04003955 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3956 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003957 if (unlikely(!inode)) {
3958 shmem_unacct_size(flags, size);
3959 return ERR_PTR(-ENOSPC);
3960 }
Eric Parisc7277092013-12-02 11:24:19 +00003961 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003963 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003964 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003965 if (!IS_ERR(res))
3966 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3967 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003968 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003969 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003970 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971}
Eric Parisc7277092013-12-02 11:24:19 +00003972
3973/**
3974 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3975 * kernel internal. There will be NO LSM permission checks against the
3976 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003977 * higher layer. The users are the big_key and shm implementations. LSM
3978 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003979 * @name: name for dentry (to be seen in /proc/<pid>/maps
3980 * @size: size to be set for the file
3981 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3982 */
3983struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3984{
Matthew Auld703321b2017-10-06 23:18:13 +01003985 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003986}
3987
3988/**
3989 * shmem_file_setup - get an unlinked file living in tmpfs
3990 * @name: name for dentry (to be seen in /proc/<pid>/maps
3991 * @size: size to be set for the file
3992 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3993 */
3994struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3995{
Matthew Auld703321b2017-10-06 23:18:13 +01003996 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003997}
Keith Packard395e0dd2008-06-20 00:08:06 -07003998EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Randy Dunlap46711812008-03-19 17:00:41 -07004000/**
Matthew Auld703321b2017-10-06 23:18:13 +01004001 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
4002 * @mnt: the tmpfs mount where the file will be created
4003 * @name: name for dentry (to be seen in /proc/<pid>/maps
4004 * @size: size to be set for the file
4005 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4006 */
4007struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4008 loff_t size, unsigned long flags)
4009{
4010 return __shmem_file_setup(mnt, name, size, flags, 0);
4011}
4012EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4013
4014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4017 */
4018int shmem_zero_setup(struct vm_area_struct *vma)
4019{
4020 struct file *file;
4021 loff_t size = vma->vm_end - vma->vm_start;
4022
Hugh Dickins66fc1302015-06-14 09:48:09 -07004023 /*
4024 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4025 * between XFS directory reading and selinux: since this file is only
4026 * accessible to the user through its mapping, use S_PRIVATE flag to
4027 * bypass file security, in the same way as shmem_kernel_file_setup().
4028 */
Matthew Auld703321b2017-10-06 23:18:13 +01004029 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 if (IS_ERR(file))
4031 return PTR_ERR(file);
4032
4033 if (vma->vm_file)
4034 fput(vma->vm_file);
4035 vma->vm_file = file;
4036 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004037
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004038 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004039 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4040 (vma->vm_end & HPAGE_PMD_MASK)) {
4041 khugepaged_enter(vma, vma->vm_flags);
4042 }
4043
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 return 0;
4045}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004046
4047/**
4048 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4049 * @mapping: the page's address_space
4050 * @index: the page index
4051 * @gfp: the page allocator flags to use if allocating
4052 *
4053 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4054 * with any new page allocations done using the specified allocation flags.
4055 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4056 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4057 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4058 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004059 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4060 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004061 */
4062struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4063 pgoff_t index, gfp_t gfp)
4064{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004065#ifdef CONFIG_SHMEM
4066 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004067 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004068 int error;
4069
4070 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004071 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004072 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004073 if (error)
4074 page = ERR_PTR(error);
4075 else
4076 unlock_page(page);
4077 return page;
4078#else
4079 /*
4080 * The tiny !SHMEM case uses ramfs without swap
4081 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004082 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004083#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004084}
4085EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);