blob: b6cf0e8e685bfad8c714c691bdc655b2b06ad9e9 [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.
2075 * But if caller specified an address hint, respect that as before.
2076 */
2077 if (uaddr)
2078 return addr;
2079
2080 if (shmem_huge != SHMEM_HUGE_FORCE) {
2081 struct super_block *sb;
2082
2083 if (file) {
2084 VM_BUG_ON(file->f_op != &shmem_file_operations);
2085 sb = file_inode(file)->i_sb;
2086 } else {
2087 /*
2088 * Called directly from mm/mmap.c, or drivers/char/mem.c
2089 * for "/dev/zero", to create a shared anonymous object.
2090 */
2091 if (IS_ERR(shm_mnt))
2092 return addr;
2093 sb = shm_mnt->mnt_sb;
2094 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002095 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002096 return addr;
2097 }
2098
2099 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2100 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2101 return addr;
2102 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2103 return addr;
2104
2105 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2106 if (inflated_len > TASK_SIZE)
2107 return addr;
2108 if (inflated_len < len)
2109 return addr;
2110
2111 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2112 if (IS_ERR_VALUE(inflated_addr))
2113 return addr;
2114 if (inflated_addr & ~PAGE_MASK)
2115 return addr;
2116
2117 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2118 inflated_addr += offset - inflated_offset;
2119 if (inflated_offset > offset)
2120 inflated_addr += HPAGE_PMD_SIZE;
2121
2122 if (inflated_addr > TASK_SIZE - len)
2123 return addr;
2124 return inflated_addr;
2125}
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002128static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Al Viro496ad9a2013-01-23 17:07:38 -05002130 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002131 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002134static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2135 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136{
Al Viro496ad9a2013-01-23 17:07:38 -05002137 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002138 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002140 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2141 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143#endif
2144
2145int shmem_lock(struct file *file, int lock, struct user_struct *user)
2146{
Al Viro496ad9a2013-01-23 17:07:38 -05002147 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct shmem_inode_info *info = SHMEM_I(inode);
2149 int retval = -ENOMEM;
2150
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002151 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (lock && !(info->flags & VM_LOCKED)) {
2153 if (!user_shm_lock(inode->i_size, user))
2154 goto out_nomem;
2155 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002156 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158 if (!lock && (info->flags & VM_LOCKED) && user) {
2159 user_shm_unlock(inode->i_size, user);
2160 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002161 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 }
2163 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002166 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return retval;
2168}
2169
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002170static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 file_accessed(file);
2173 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002174 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002175 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2176 (vma->vm_end & HPAGE_PMD_MASK)) {
2177 khugepaged_enter(vma, vma->vm_flags);
2178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return 0;
2180}
2181
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002182static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002183 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 struct inode *inode;
2186 struct shmem_inode_info *info;
2187 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2188
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002189 if (shmem_reserve_inode(sb))
2190 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 inode = new_inode(sb);
2193 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002194 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002195 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002197 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002198 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 info = SHMEM_I(inode);
2200 memset(info, 0, (char *)inode - (char *)info);
2201 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002202 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002203 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002204 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002206 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002207 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
2209 switch (mode & S_IFMT) {
2210 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002211 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 init_special_inode(inode, mode, dev);
2213 break;
2214 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002215 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 inode->i_op = &shmem_inode_operations;
2217 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002218 mpol_shared_policy_init(&info->policy,
2219 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 break;
2221 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002222 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /* Some things misbehave if size == 0 on a directory */
2224 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2225 inode->i_op = &shmem_dir_inode_operations;
2226 inode->i_fop = &simple_dir_operations;
2227 break;
2228 case S_IFLNK:
2229 /*
2230 * Must not load anything in the rbtree,
2231 * mpol_free_shared_policy will not be called.
2232 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002233 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 break;
2235 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002236
2237 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002238 } else
2239 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 return inode;
2241}
2242
Johannes Weiner0cd61442014-04-03 14:47:46 -07002243bool shmem_mapping(struct address_space *mapping)
2244{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002245 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002246}
2247
Mike Rapoport8d103962017-09-06 16:23:02 -07002248static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2249 pmd_t *dst_pmd,
2250 struct vm_area_struct *dst_vma,
2251 unsigned long dst_addr,
2252 unsigned long src_addr,
2253 bool zeropage,
2254 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002255{
2256 struct inode *inode = file_inode(dst_vma->vm_file);
2257 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002258 struct address_space *mapping = inode->i_mapping;
2259 gfp_t gfp = mapping_gfp_mask(mapping);
2260 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2261 struct mem_cgroup *memcg;
2262 spinlock_t *ptl;
2263 void *page_kaddr;
2264 struct page *page;
2265 pte_t _dst_pte, *dst_pte;
2266 int ret;
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002267 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002268
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002269 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002270 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002271 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002272
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002273 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002274 page = shmem_alloc_page(gfp, info, pgoff);
2275 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002276 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002277
Mike Rapoport8d103962017-09-06 16:23:02 -07002278 if (!zeropage) { /* mcopy_atomic */
2279 page_kaddr = kmap_atomic(page);
2280 ret = copy_from_user(page_kaddr,
2281 (const void __user *)src_addr,
2282 PAGE_SIZE);
2283 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002284
Mike Rapoport8d103962017-09-06 16:23:02 -07002285 /* fallback to copy_from_user outside mmap_sem */
2286 if (unlikely(ret)) {
2287 *pagep = page;
2288 shmem_inode_unacct_blocks(inode, 1);
2289 /* don't free the page */
Andrea Arcangeli10f98c12018-11-30 14:09:25 -08002290 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002291 }
2292 } else { /* mfill_zeropage_atomic */
2293 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002294 }
2295 } else {
2296 page = *pagep;
2297 *pagep = NULL;
2298 }
2299
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002300 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2301 __SetPageLocked(page);
2302 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002303 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002304
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002305 ret = -EFAULT;
2306 offset = linear_page_index(dst_vma, dst_addr);
2307 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2308 if (unlikely(offset >= max_off))
2309 goto out_release;
2310
Tejun Heo2cf85582018-07-03 11:14:56 -04002311 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002312 if (ret)
2313 goto out_release;
2314
2315 ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
2316 if (!ret) {
2317 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL);
2318 radix_tree_preload_end();
2319 }
2320 if (ret)
2321 goto out_release_uncharge;
2322
2323 mem_cgroup_commit_charge(page, memcg, false, false);
2324
2325 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2326 if (dst_vma->vm_flags & VM_WRITE)
2327 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangeli8f193a72018-11-30 14:09:43 -08002328 else {
2329 /*
2330 * We don't set the pte dirty if the vma has no
2331 * VM_WRITE permission, so mark the page dirty or it
2332 * could be freed from under us. We could do it
2333 * unconditionally before unlock_page(), but doing it
2334 * only if VM_WRITE is not set is faster.
2335 */
2336 set_page_dirty(page);
2337 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002338
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002339 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002340
2341 ret = -EFAULT;
2342 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2343 if (unlikely(offset >= max_off))
2344 goto out_release_uncharge_unlock;
2345
2346 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002347 if (!pte_none(*dst_pte))
2348 goto out_release_uncharge_unlock;
2349
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002350 lru_cache_add_anon(page);
2351
2352 spin_lock(&info->lock);
2353 info->alloced++;
2354 inode->i_blocks += BLOCKS_PER_PAGE;
2355 shmem_recalc_inode(inode);
2356 spin_unlock(&info->lock);
2357
2358 inc_mm_counter(dst_mm, mm_counter_file(page));
2359 page_add_file_rmap(page, false);
2360 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2361
2362 /* No need to invalidate - it was non-present before */
2363 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002364 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002365 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002366 ret = 0;
2367out:
2368 return ret;
2369out_release_uncharge_unlock:
2370 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeli8f193a72018-11-30 14:09:43 -08002371 ClearPageDirty(page);
Andrea Arcangeli4ce33762018-11-30 14:09:37 -08002372 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002373out_release_uncharge:
2374 mem_cgroup_cancel_charge(page, memcg, false);
2375out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002376 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002377 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002378out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002379 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002380 goto out;
2381}
2382
Mike Rapoport8d103962017-09-06 16:23:02 -07002383int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2384 pmd_t *dst_pmd,
2385 struct vm_area_struct *dst_vma,
2386 unsigned long dst_addr,
2387 unsigned long src_addr,
2388 struct page **pagep)
2389{
2390 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2391 dst_addr, src_addr, false, pagep);
2392}
2393
2394int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2395 pmd_t *dst_pmd,
2396 struct vm_area_struct *dst_vma,
2397 unsigned long dst_addr)
2398{
2399 struct page *page = NULL;
2400
2401 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2402 dst_addr, 0, true, &page);
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002406static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002407static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002409#ifdef CONFIG_TMPFS_XATTR
2410static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2411#else
2412#define shmem_initxattrs NULL
2413#endif
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002416shmem_write_begin(struct file *file, struct address_space *mapping,
2417 loff_t pos, unsigned len, unsigned flags,
2418 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Nick Piggin800d15a2007-10-16 01:25:03 -07002420 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002421 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002422 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002423
2424 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002425 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002426 if (info->seals & F_SEAL_WRITE)
2427 return -EPERM;
2428 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2429 return -EPERM;
2430 }
2431
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002432 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002433}
2434
2435static int
2436shmem_write_end(struct file *file, struct address_space *mapping,
2437 loff_t pos, unsigned len, unsigned copied,
2438 struct page *page, void *fsdata)
2439{
2440 struct inode *inode = mapping->host;
2441
Hugh Dickinsd3602442008-02-04 22:28:44 -08002442 if (pos + copied > inode->i_size)
2443 i_size_write(inode, pos + copied);
2444
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002445 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002446 struct page *head = compound_head(page);
2447 if (PageTransCompound(page)) {
2448 int i;
2449
2450 for (i = 0; i < HPAGE_PMD_NR; i++) {
2451 if (head + i == page)
2452 continue;
2453 clear_highpage(head + i);
2454 flush_dcache_page(head + i);
2455 }
2456 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002457 if (copied < PAGE_SIZE) {
2458 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002459 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002460 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002461 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002462 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002463 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002464 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002465 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002466 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002467
Nick Piggin800d15a2007-10-16 01:25:03 -07002468 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Al Viro2ba5bbe2014-04-02 20:00:02 -04002471static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Al Viro6e58e792014-02-03 17:07:03 -05002473 struct file *file = iocb->ki_filp;
2474 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002476 pgoff_t index;
2477 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002478 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002479 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002480 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002481 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002482
2483 /*
2484 * Might this read be for a stacking filesystem? Then when reading
2485 * holes of a sparse file, we actually need to allocate those pages,
2486 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2487 */
Al Viro777eda22014-12-17 04:46:46 -05002488 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002489 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002491 index = *ppos >> PAGE_SHIFT;
2492 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
2494 for (;;) {
2495 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002496 pgoff_t end_index;
2497 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 loff_t i_size = i_size_read(inode);
2499
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002500 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 if (index > end_index)
2502 break;
2503 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002504 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (nr <= offset)
2506 break;
2507 }
2508
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002509 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002510 if (error) {
2511 if (error == -EINVAL)
2512 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 break;
2514 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002515 if (page) {
2516 if (sgp == SGP_CACHE)
2517 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002518 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 /*
2522 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002523 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002525 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002527 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002529 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (nr <= offset) {
2531 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002532 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 break;
2534 }
2535 }
2536 nr -= offset;
2537
2538 if (page) {
2539 /*
2540 * If users can be writing to this page using arbitrary
2541 * virtual addresses, take care about potential aliasing
2542 * before reading the page on the kernel side.
2543 */
2544 if (mapping_writably_mapped(mapping))
2545 flush_dcache_page(page);
2546 /*
2547 * Mark the page accessed if we read the beginning.
2548 */
2549 if (!offset)
2550 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002551 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002553 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
2556 /*
2557 * Ok, we have the page, and it's up-to-date, so
2558 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002560 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002561 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002563 index += offset >> PAGE_SHIFT;
2564 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002566 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002567 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 break;
Al Viro6e58e792014-02-03 17:07:03 -05002569 if (ret < nr) {
2570 error = -EFAULT;
2571 break;
2572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 cond_resched();
2574 }
2575
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002576 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002577 file_accessed(file);
2578 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002581/*
2582 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2583 */
2584static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002585 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002586{
2587 struct page *page;
2588 struct pagevec pvec;
2589 pgoff_t indices[PAGEVEC_SIZE];
2590 bool done = false;
2591 int i;
2592
Mel Gorman86679822017-11-15 17:37:52 -08002593 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002594 pvec.nr = 1; /* start small: we may be there already */
2595 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002596 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002597 pvec.nr, pvec.pages, indices);
2598 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002599 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002600 index = end;
2601 break;
2602 }
2603 for (i = 0; i < pvec.nr; i++, index++) {
2604 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002605 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002606 done = true;
2607 break;
2608 }
2609 index = indices[i];
2610 }
2611 page = pvec.pages[i];
2612 if (page && !radix_tree_exceptional_entry(page)) {
2613 if (!PageUptodate(page))
2614 page = NULL;
2615 }
2616 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002617 (page && whence == SEEK_DATA) ||
2618 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002619 done = true;
2620 break;
2621 }
2622 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002623 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002624 pagevec_release(&pvec);
2625 pvec.nr = PAGEVEC_SIZE;
2626 cond_resched();
2627 }
2628 return index;
2629}
2630
Andrew Morton965c8e52012-12-17 15:59:39 -08002631static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002632{
2633 struct address_space *mapping = file->f_mapping;
2634 struct inode *inode = mapping->host;
2635 pgoff_t start, end;
2636 loff_t new_offset;
2637
Andrew Morton965c8e52012-12-17 15:59:39 -08002638 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2639 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002640 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002641 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002642 /* We're holding i_mutex so we can access i_size directly */
2643
Yufen Yudb89fc02018-11-16 15:08:39 -08002644 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002645 offset = -ENXIO;
2646 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002647 start = offset >> PAGE_SHIFT;
2648 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002649 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002650 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002651 if (new_offset > offset) {
2652 if (new_offset < inode->i_size)
2653 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002654 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002655 offset = -ENXIO;
2656 else
2657 offset = inode->i_size;
2658 }
2659 }
2660
Hugh Dickins387aae62013-08-04 11:30:25 -07002661 if (offset >= 0)
2662 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002663 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002664 return offset;
2665}
2666
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002667static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2668 loff_t len)
2669{
Al Viro496ad9a2013-01-23 17:07:38 -05002670 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002671 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002672 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002673 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002674 pgoff_t start, index, end;
2675 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002676
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002677 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2678 return -EOPNOTSUPP;
2679
Al Viro59551022016-01-22 15:40:57 -05002680 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002681
2682 if (mode & FALLOC_FL_PUNCH_HOLE) {
2683 struct address_space *mapping = file->f_mapping;
2684 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2685 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002686 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002687
David Herrmann40e041a2014-08-08 14:25:27 -07002688 /* protected by i_mutex */
2689 if (info->seals & F_SEAL_WRITE) {
2690 error = -EPERM;
2691 goto out;
2692 }
2693
Hugh Dickins8e205f72014-07-23 14:00:10 -07002694 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002695 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2696 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2697 spin_lock(&inode->i_lock);
2698 inode->i_private = &shmem_falloc;
2699 spin_unlock(&inode->i_lock);
2700
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002701 if ((u64)unmap_end > (u64)unmap_start)
2702 unmap_mapping_range(mapping, unmap_start,
2703 1 + unmap_end - unmap_start, 0);
2704 shmem_truncate_range(inode, offset, offset + len - 1);
2705 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002706
2707 spin_lock(&inode->i_lock);
2708 inode->i_private = NULL;
2709 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002710 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002711 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002712 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002713 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002714 }
2715
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002716 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2717 error = inode_newsize_ok(inode, offset + len);
2718 if (error)
2719 goto out;
2720
David Herrmann40e041a2014-08-08 14:25:27 -07002721 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2722 error = -EPERM;
2723 goto out;
2724 }
2725
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002726 start = offset >> PAGE_SHIFT;
2727 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002728 /* Try to avoid a swapstorm if len is impossible to satisfy */
2729 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2730 error = -ENOSPC;
2731 goto out;
2732 }
2733
Hugh Dickins8e205f72014-07-23 14:00:10 -07002734 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002735 shmem_falloc.start = start;
2736 shmem_falloc.next = start;
2737 shmem_falloc.nr_falloced = 0;
2738 shmem_falloc.nr_unswapped = 0;
2739 spin_lock(&inode->i_lock);
2740 inode->i_private = &shmem_falloc;
2741 spin_unlock(&inode->i_lock);
2742
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002743 for (index = start; index < end; index++) {
2744 struct page *page;
2745
2746 /*
2747 * Good, the fallocate(2) manpage permits EINTR: we may have
2748 * been interrupted because we are using up too much memory.
2749 */
2750 if (signal_pending(current))
2751 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002752 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2753 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002754 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002755 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002756 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002757 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002758 if (index > start) {
2759 shmem_undo_range(inode,
2760 (loff_t)start << PAGE_SHIFT,
2761 ((loff_t)index << PAGE_SHIFT) - 1, true);
2762 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002763 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002764 }
2765
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002766 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002767 * Inform shmem_writepage() how far we have reached.
2768 * No need for lock or barrier: we have the page lock.
2769 */
2770 shmem_falloc.next++;
2771 if (!PageUptodate(page))
2772 shmem_falloc.nr_falloced++;
2773
2774 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002775 * If !PageUptodate, leave it that way so that freeable pages
2776 * can be recognized if we need to rollback on error later.
2777 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002778 * than free the pages we are allocating (and SGP_CACHE pages
2779 * might still be clean: we now need to mark those dirty too).
2780 */
2781 set_page_dirty(page);
2782 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002783 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002784 cond_resched();
2785 }
2786
2787 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2788 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002789 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002790undone:
2791 spin_lock(&inode->i_lock);
2792 inode->i_private = NULL;
2793 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002794out:
Al Viro59551022016-01-22 15:40:57 -05002795 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002796 return error;
2797}
2798
David Howells726c3342006-06-23 02:02:58 -07002799static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
David Howells726c3342006-06-23 02:02:58 -07002801 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002804 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002806 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002808 buf->f_bavail =
2809 buf->f_bfree = sbinfo->max_blocks -
2810 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002811 }
2812 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 buf->f_files = sbinfo->max_inodes;
2814 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
2816 /* else leave those fields 0 like simple_statfs */
2817 return 0;
2818}
2819
2820/*
2821 * File creation. Allocate an inode, and we're done..
2822 */
2823static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002824shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002826 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 int error = -ENOSPC;
2828
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002829 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002831 error = simple_acl_create(dir, inode);
2832 if (error)
2833 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002834 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002835 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002836 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002837 if (error && error != -EOPNOTSUPP)
2838 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002839
Al Viro718deb62009-12-16 19:35:36 -05002840 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002842 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 d_instantiate(dentry, inode);
2844 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
2846 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002847out_iput:
2848 iput(inode);
2849 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Al Viro60545d02013-06-07 01:20:27 -04002852static int
2853shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2854{
2855 struct inode *inode;
2856 int error = -ENOSPC;
2857
2858 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2859 if (inode) {
2860 error = security_inode_init_security(inode, dir,
2861 NULL,
2862 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002863 if (error && error != -EOPNOTSUPP)
2864 goto out_iput;
2865 error = simple_acl_create(dir, inode);
2866 if (error)
2867 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002868 d_tmpfile(dentry, inode);
2869 }
2870 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002871out_iput:
2872 iput(inode);
2873 return error;
Al Viro60545d02013-06-07 01:20:27 -04002874}
2875
Al Viro18bb1db2011-07-26 01:41:39 -04002876static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
2878 int error;
2879
2880 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2881 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002882 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return 0;
2884}
2885
Al Viro4acdaf22011-07-26 01:42:34 -04002886static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002887 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2890}
2891
2892/*
2893 * Link a file..
2894 */
2895static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2896{
David Howells75c3cfa2015-03-17 22:26:12 +00002897 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002898 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
2900 /*
2901 * No ordinary (disk based) filesystem counts links as inodes;
2902 * but each new link needs a new dentry, pinning lowmem, and
2903 * tmpfs dentries cannot be pruned until they are unlinked.
2904 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002905 ret = shmem_reserve_inode(inode->i_sb);
2906 if (ret)
2907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002910 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002911 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002912 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 dget(dentry); /* Extra pinning count for the created dentry */
2914 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002915out:
2916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917}
2918
2919static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2920{
David Howells75c3cfa2015-03-17 22:26:12 +00002921 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002923 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2924 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002927 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002928 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 dput(dentry); /* Undo the count from "create" - this does all the work */
2930 return 0;
2931}
2932
2933static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2934{
2935 if (!simple_empty(dentry))
2936 return -ENOTEMPTY;
2937
David Howells75c3cfa2015-03-17 22:26:12 +00002938 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002939 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return shmem_unlink(dir, dentry);
2941}
2942
Miklos Szeredi37456772014-07-23 15:15:34 +02002943static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2944{
David Howellse36cb0b2015-01-29 12:02:35 +00002945 bool old_is_dir = d_is_dir(old_dentry);
2946 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002947
2948 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2949 if (old_is_dir) {
2950 drop_nlink(old_dir);
2951 inc_nlink(new_dir);
2952 } else {
2953 drop_nlink(new_dir);
2954 inc_nlink(old_dir);
2955 }
2956 }
2957 old_dir->i_ctime = old_dir->i_mtime =
2958 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002959 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002960 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002961
2962 return 0;
2963}
2964
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002965static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2966{
2967 struct dentry *whiteout;
2968 int error;
2969
2970 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2971 if (!whiteout)
2972 return -ENOMEM;
2973
2974 error = shmem_mknod(old_dir, whiteout,
2975 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2976 dput(whiteout);
2977 if (error)
2978 return error;
2979
2980 /*
2981 * Cheat and hash the whiteout while the old dentry is still in
2982 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2983 *
2984 * d_lookup() will consistently find one of them at this point,
2985 * not sure which one, but that isn't even important.
2986 */
2987 d_rehash(whiteout);
2988 return 0;
2989}
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991/*
2992 * The VFS layer already does all the dentry stuff for rename,
2993 * we just have to decrement the usage count for the target if
2994 * it exists so that the VFS layer correctly free's it when it
2995 * gets overwritten.
2996 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002997static 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 -07002998{
David Howells75c3cfa2015-03-17 22:26:12 +00002999 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 int they_are_dirs = S_ISDIR(inode->i_mode);
3001
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003002 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003003 return -EINVAL;
3004
Miklos Szeredi37456772014-07-23 15:15:34 +02003005 if (flags & RENAME_EXCHANGE)
3006 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 if (!simple_empty(new_dentry))
3009 return -ENOTEMPTY;
3010
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003011 if (flags & RENAME_WHITEOUT) {
3012 int error;
3013
3014 error = shmem_whiteout(old_dir, old_dentry);
3015 if (error)
3016 return error;
3017 }
3018
David Howells75c3cfa2015-03-17 22:26:12 +00003019 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003021 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003022 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003023 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003026 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003027 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029
3030 old_dir->i_size -= BOGO_DIRENT_SIZE;
3031 new_dir->i_size += BOGO_DIRENT_SIZE;
3032 old_dir->i_ctime = old_dir->i_mtime =
3033 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003034 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 return 0;
3036}
3037
3038static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3039{
3040 int error;
3041 int len;
3042 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003043 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003046 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 return -ENAMETOOLONG;
3048
Joe Perches0825a6f2018-06-14 15:27:58 -07003049 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3050 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 if (!inode)
3052 return -ENOSPC;
3053
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003054 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003055 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003056 if (error) {
3057 if (error != -EOPNOTSUPP) {
3058 iput(inode);
3059 return error;
3060 }
3061 error = 0;
3062 }
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003065 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003066 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3067 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003068 iput(inode);
3069 return -ENOMEM;
3070 }
3071 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003073 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003074 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 if (error) {
3076 iput(inode);
3077 return error;
3078 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003079 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003081 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003082 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003084 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003085 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003088 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 d_instantiate(dentry, inode);
3090 dget(dentry);
3091 return 0;
3092}
3093
Al Virofceef392015-12-29 15:58:39 -05003094static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
Al Virofceef392015-12-29 15:58:39 -05003096 mark_page_accessed(arg);
3097 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
Al Viro6b255392015-11-17 10:20:54 -05003100static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003101 struct inode *inode,
3102 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003105 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003106 if (!dentry) {
3107 page = find_get_page(inode->i_mapping, 0);
3108 if (!page)
3109 return ERR_PTR(-ECHILD);
3110 if (!PageUptodate(page)) {
3111 put_page(page);
3112 return ERR_PTR(-ECHILD);
3113 }
3114 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003115 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003116 if (error)
3117 return ERR_PTR(error);
3118 unlock_page(page);
3119 }
Al Virofceef392015-12-29 15:58:39 -05003120 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003121 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
Eric Parisb09e0fa2011-05-24 17:12:39 -07003124#ifdef CONFIG_TMPFS_XATTR
3125/*
3126 * Superblocks without xattr inode operations may get some security.* xattr
3127 * support from the LSM "for free". As soon as we have any other xattrs
3128 * like ACLs, we also need to implement the security.* handlers at
3129 * filesystem level, though.
3130 */
3131
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003132/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003133 * Callback for security_inode_init_security() for acquiring xattrs.
3134 */
3135static int shmem_initxattrs(struct inode *inode,
3136 const struct xattr *xattr_array,
3137 void *fs_info)
3138{
3139 struct shmem_inode_info *info = SHMEM_I(inode);
3140 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003141 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003142 size_t len;
3143
3144 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003145 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003146 if (!new_xattr)
3147 return -ENOMEM;
3148
3149 len = strlen(xattr->name) + 1;
3150 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3151 GFP_KERNEL);
3152 if (!new_xattr->name) {
3153 kfree(new_xattr);
3154 return -ENOMEM;
3155 }
3156
3157 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3158 XATTR_SECURITY_PREFIX_LEN);
3159 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3160 xattr->name, len);
3161
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003162 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003163 }
3164
3165 return 0;
3166}
3167
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003168static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003169 struct dentry *unused, struct inode *inode,
3170 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003171{
Al Virob2968212016-04-10 20:48:24 -04003172 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003173
3174 name = xattr_full_name(handler, name);
3175 return simple_xattr_get(&info->xattrs, name, buffer, size);
3176}
3177
3178static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003179 struct dentry *unused, struct inode *inode,
3180 const char *name, const void *value,
3181 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003182{
Al Viro59301222016-05-27 10:19:30 -04003183 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003184
3185 name = xattr_full_name(handler, name);
3186 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3187}
3188
3189static const struct xattr_handler shmem_security_xattr_handler = {
3190 .prefix = XATTR_SECURITY_PREFIX,
3191 .get = shmem_xattr_handler_get,
3192 .set = shmem_xattr_handler_set,
3193};
3194
3195static const struct xattr_handler shmem_trusted_xattr_handler = {
3196 .prefix = XATTR_TRUSTED_PREFIX,
3197 .get = shmem_xattr_handler_get,
3198 .set = shmem_xattr_handler_set,
3199};
3200
Eric Parisb09e0fa2011-05-24 17:12:39 -07003201static const struct xattr_handler *shmem_xattr_handlers[] = {
3202#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003203 &posix_acl_access_xattr_handler,
3204 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003205#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003206 &shmem_security_xattr_handler,
3207 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003208 NULL
3209};
3210
Eric Parisb09e0fa2011-05-24 17:12:39 -07003211static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3212{
David Howells75c3cfa2015-03-17 22:26:12 +00003213 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003214 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003215}
3216#endif /* CONFIG_TMPFS_XATTR */
3217
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003218static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003219 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003220#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003221 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003222#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223};
3224
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003225static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003226 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003227#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003228 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003229#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003230};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003231
David M. Grimes91828a42006-10-17 00:09:45 -07003232static struct dentry *shmem_get_parent(struct dentry *child)
3233{
3234 return ERR_PTR(-ESTALE);
3235}
3236
3237static int shmem_match(struct inode *ino, void *vfh)
3238{
3239 __u32 *fh = vfh;
3240 __u64 inum = fh[2];
3241 inum = (inum << 32) | fh[1];
3242 return ino->i_ino == inum && fh[0] == ino->i_generation;
3243}
3244
Amir Goldstein12ba7802018-06-07 17:07:15 -07003245/* Find any alias of inode, but prefer a hashed alias */
3246static struct dentry *shmem_find_alias(struct inode *inode)
3247{
3248 struct dentry *alias = d_find_alias(inode);
3249
3250 return alias ?: d_find_any_alias(inode);
3251}
3252
3253
Christoph Hellwig480b1162007-10-21 16:42:13 -07003254static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3255 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003256{
David M. Grimes91828a42006-10-17 00:09:45 -07003257 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003258 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003259 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003260
Christoph Hellwig480b1162007-10-21 16:42:13 -07003261 if (fh_len < 3)
3262 return NULL;
3263
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003264 inum = fid->raw[2];
3265 inum = (inum << 32) | fid->raw[1];
3266
Christoph Hellwig480b1162007-10-21 16:42:13 -07003267 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3268 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003269 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003270 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003271 iput(inode);
3272 }
3273
Christoph Hellwig480b1162007-10-21 16:42:13 -07003274 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003275}
3276
Al Virob0b03822012-04-02 14:34:06 -04003277static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3278 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003279{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303280 if (*len < 3) {
3281 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003282 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303283 }
David M. Grimes91828a42006-10-17 00:09:45 -07003284
Al Viro1d3382cb2010-10-23 15:19:20 -04003285 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003286 /* Unfortunately insert_inode_hash is not idempotent,
3287 * so as we hash inodes here rather than at creation
3288 * time, we need a lock to ensure we only try
3289 * to do it once
3290 */
3291 static DEFINE_SPINLOCK(lock);
3292 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003293 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003294 __insert_inode_hash(inode,
3295 inode->i_ino + inode->i_generation);
3296 spin_unlock(&lock);
3297 }
3298
3299 fh[0] = inode->i_generation;
3300 fh[1] = inode->i_ino;
3301 fh[2] = ((__u64)inode->i_ino) >> 32;
3302
3303 *len = 3;
3304 return 1;
3305}
3306
Christoph Hellwig39655162007-10-21 16:42:17 -07003307static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003308 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003309 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003310 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003311};
3312
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003313static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3314 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003317 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003318 uid_t uid;
3319 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003321 while (options != NULL) {
3322 this_char = options;
3323 for (;;) {
3324 /*
3325 * NUL-terminate this option: unfortunately,
3326 * mount options form a comma-separated list,
3327 * but mpol's nodelist may also contain commas.
3328 */
3329 options = strchr(options, ',');
3330 if (options == NULL)
3331 break;
3332 options++;
3333 if (!isdigit(*options)) {
3334 options[-1] = '\0';
3335 break;
3336 }
3337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (!*this_char)
3339 continue;
3340 if ((value = strchr(this_char,'=')) != NULL) {
3341 *value++ = 0;
3342 } else {
Joe Perches11705322016-03-17 14:19:50 -07003343 pr_err("tmpfs: No value for mount option '%s'\n",
3344 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003345 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347
3348 if (!strcmp(this_char,"size")) {
3349 unsigned long long size;
3350 size = memparse(value,&rest);
3351 if (*rest == '%') {
3352 size <<= PAGE_SHIFT;
3353 size *= totalram_pages;
3354 do_div(size, 100);
3355 rest++;
3356 }
3357 if (*rest)
3358 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003359 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003360 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003362 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (*rest)
3364 goto bad_val;
3365 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003366 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (*rest)
3368 goto bad_val;
3369 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003370 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003372 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (*rest)
3374 goto bad_val;
3375 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003376 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003378 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (*rest)
3380 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003381 sbinfo->uid = make_kuid(current_user_ns(), uid);
3382 if (!uid_valid(sbinfo->uid))
3383 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003385 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003387 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 if (*rest)
3389 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003390 sbinfo->gid = make_kgid(current_user_ns(), gid);
3391 if (!gid_valid(sbinfo->gid))
3392 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003393#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003394 } else if (!strcmp(this_char, "huge")) {
3395 int huge;
3396 huge = shmem_parse_huge(value);
3397 if (huge < 0)
3398 goto bad_val;
3399 if (!has_transparent_hugepage() &&
3400 huge != SHMEM_HUGE_NEVER)
3401 goto bad_val;
3402 sbinfo->huge = huge;
3403#endif
3404#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003405 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003406 mpol_put(mpol);
3407 mpol = NULL;
3408 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003409 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 } else {
Joe Perches11705322016-03-17 14:19:50 -07003412 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003413 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
3415 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003416 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return 0;
3418
3419bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003420 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003422error:
3423 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 1;
3425
3426}
3427
3428static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3429{
3430 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003431 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003432 unsigned long inodes;
3433 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Greg Thelen5f001102013-02-22 16:36:01 -08003435 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003436 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003437 return error;
3438
3439 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003440 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003441 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003442 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003443 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003444 goto out;
3445 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003446 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003447 * but we must separately disallow unlimited->limited, because
3448 * in that case we have no record of how much is already in use.
3449 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003450 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003451 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003452 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003453 goto out;
3454
3455 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003456 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003457 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003458 sbinfo->max_inodes = config.max_inodes;
3459 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003460
Greg Thelen5f001102013-02-22 16:36:01 -08003461 /*
3462 * Preserve previous mempolicy unless mpol remount option was specified.
3463 */
3464 if (config.mpol) {
3465 mpol_put(sbinfo->mpol);
3466 sbinfo->mpol = config.mpol; /* transfers initial ref */
3467 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003468out:
3469 spin_unlock(&sbinfo->stat_lock);
3470 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003472
Al Viro34c80b12011-12-08 21:32:45 -05003473static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003474{
Al Viro34c80b12011-12-08 21:32:45 -05003475 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003476
3477 if (sbinfo->max_blocks != shmem_default_max_blocks())
3478 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003479 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003480 if (sbinfo->max_inodes != shmem_default_max_inodes())
3481 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003482 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003483 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003484 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3485 seq_printf(seq, ",uid=%u",
3486 from_kuid_munged(&init_user_ns, sbinfo->uid));
3487 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3488 seq_printf(seq, ",gid=%u",
3489 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003490#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003491 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3492 if (sbinfo->huge)
3493 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3494#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003495 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003496 return 0;
3497}
David Herrmann9183df22014-08-08 14:25:29 -07003498
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003499#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500
3501static void shmem_put_super(struct super_block *sb)
3502{
Hugh Dickins602586a2010-08-17 15:23:56 -07003503 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3504
3505 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003506 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003507 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 sb->s_fs_info = NULL;
3509}
3510
Kay Sievers2b2af542009-04-30 15:23:42 +02003511int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
3513 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003514 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003515 int err = -ENOMEM;
3516
3517 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003518 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003519 L1_CACHE_BYTES), GFP_KERNEL);
3520 if (!sbinfo)
3521 return -ENOMEM;
3522
Joe Perches0825a6f2018-06-14 15:27:58 -07003523 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003524 sbinfo->uid = current_fsuid();
3525 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003526 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003528#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 /*
3530 * Per default we only allow half of the physical ram per
3531 * tmpfs instance, limiting inodes to one per page of lowmem;
3532 * but the internal instance is left unlimited.
3533 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003534 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003535 sbinfo->max_blocks = shmem_default_max_blocks();
3536 sbinfo->max_inodes = shmem_default_max_inodes();
3537 if (shmem_parse_options(data, sbinfo, false)) {
3538 err = -EINVAL;
3539 goto failed;
3540 }
Al Viroca4e0512013-08-31 12:57:10 -04003541 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003542 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 }
David M. Grimes91828a42006-10-17 00:09:45 -07003544 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003545 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003547 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548#endif
3549
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003550 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003551 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003552 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003553 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003554 spin_lock_init(&sbinfo->shrinklist_lock);
3555 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003556
Hugh Dickins285b2c42011-08-03 16:21:20 -07003557 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003558 sb->s_blocksize = PAGE_SIZE;
3559 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 sb->s_magic = TMPFS_MAGIC;
3561 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003562 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003563#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003564 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003565#endif
3566#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003567 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003568#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003569 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003570
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003571 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 if (!inode)
3573 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003574 inode->i_uid = sbinfo->uid;
3575 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003576 sb->s_root = d_make_root(inode);
3577 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003578 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 return 0;
3580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581failed:
3582 shmem_put_super(sb);
3583 return err;
3584}
3585
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003586static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588static struct inode *shmem_alloc_inode(struct super_block *sb)
3589{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003590 struct shmem_inode_info *info;
3591 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3592 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003594 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
3596
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003597static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003598{
3599 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003600 if (S_ISLNK(inode->i_mode))
3601 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003602 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3603}
3604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605static void shmem_destroy_inode(struct inode *inode)
3606{
Al Viro09208d12011-07-26 03:15:03 -04003607 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003609 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610}
3611
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003612static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003614 struct shmem_inode_info *info = foo;
3615 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616}
3617
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003618static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619{
3620 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3621 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003622 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003625static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003627 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628}
3629
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003630static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003632 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003634 .write_begin = shmem_write_begin,
3635 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003637#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003638 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003639#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003640 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641};
3642
Helge Deller15ad7cd2006-12-06 20:40:36 -08003643static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003645 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003647 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003648 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003649 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003650 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003651 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003652 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003653 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654#endif
3655};
3656
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003657static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003658 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003659 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003660#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003661 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003662 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664};
3665
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003666static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667#ifdef CONFIG_TMPFS
3668 .create = shmem_create,
3669 .lookup = simple_lookup,
3670 .link = shmem_link,
3671 .unlink = shmem_unlink,
3672 .symlink = shmem_symlink,
3673 .mkdir = shmem_mkdir,
3674 .rmdir = shmem_rmdir,
3675 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003676 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003677 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003679#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003680 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003681#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003682#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003683 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003684 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003685#endif
3686};
3687
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003688static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003689#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003690 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003691#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003692#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003693 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003694 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003695#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696};
3697
Hugh Dickins759b9772007-03-05 00:30:28 -08003698static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 .alloc_inode = shmem_alloc_inode,
3700 .destroy_inode = shmem_destroy_inode,
3701#ifdef CONFIG_TMPFS
3702 .statfs = shmem_statfs,
3703 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003704 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705#endif
Al Viro1f895f72010-06-05 19:10:41 -04003706 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 .drop_inode = generic_delete_inode,
3708 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003709#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3710 .nr_cached_objects = shmem_unused_huge_count,
3711 .free_cached_objects = shmem_unused_huge_scan,
3712#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713};
3714
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003715static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003716 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003717 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718#ifdef CONFIG_NUMA
3719 .set_policy = shmem_set_policy,
3720 .get_policy = shmem_get_policy,
3721#endif
3722};
3723
Al Viro3c26ff62010-07-25 11:46:36 +04003724static struct dentry *shmem_mount(struct file_system_type *fs_type,
3725 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Al Viro3c26ff62010-07-25 11:46:36 +04003727 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003730static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 .owner = THIS_MODULE,
3732 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003733 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003735 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003738int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739{
3740 int error;
3741
Rob Landley16203a72013-09-11 14:26:12 -07003742 /* If rootfs called this, don't re-init */
3743 if (shmem_inode_cachep)
3744 return 0;
3745
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003746 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003748 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003750 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 goto out2;
3752 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003753
Al Viroca4e0512013-08-31 12:57:10 -04003754 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 if (IS_ERR(shm_mnt)) {
3756 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003757 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 goto out1;
3759 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003760
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003761#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003762 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003763 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3764 else
3765 shmem_huge = 0; /* just in case it was patched */
3766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 return 0;
3768
3769out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003770 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003772 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 shm_mnt = ERR_PTR(error);
3774 return error;
3775}
Matt Mackall853ac432009-01-06 14:40:20 -08003776
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003777#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003778static ssize_t shmem_enabled_show(struct kobject *kobj,
3779 struct kobj_attribute *attr, char *buf)
3780{
3781 int values[] = {
3782 SHMEM_HUGE_ALWAYS,
3783 SHMEM_HUGE_WITHIN_SIZE,
3784 SHMEM_HUGE_ADVISE,
3785 SHMEM_HUGE_NEVER,
3786 SHMEM_HUGE_DENY,
3787 SHMEM_HUGE_FORCE,
3788 };
3789 int i, count;
3790
3791 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3792 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3793
3794 count += sprintf(buf + count, fmt,
3795 shmem_format_huge(values[i]));
3796 }
3797 buf[count - 1] = '\n';
3798 return count;
3799}
3800
3801static ssize_t shmem_enabled_store(struct kobject *kobj,
3802 struct kobj_attribute *attr, const char *buf, size_t count)
3803{
3804 char tmp[16];
3805 int huge;
3806
3807 if (count + 1 > sizeof(tmp))
3808 return -EINVAL;
3809 memcpy(tmp, buf, count);
3810 tmp[count] = '\0';
3811 if (count && tmp[count - 1] == '\n')
3812 tmp[count - 1] = '\0';
3813
3814 huge = shmem_parse_huge(tmp);
3815 if (huge == -EINVAL)
3816 return -EINVAL;
3817 if (!has_transparent_hugepage() &&
3818 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3819 return -EINVAL;
3820
3821 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003822 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003823 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3824 return count;
3825}
3826
3827struct kobj_attribute shmem_enabled_attr =
3828 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003829#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003830
Arnd Bergmann3b337192016-08-10 16:27:44 -07003831#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003832bool shmem_huge_enabled(struct vm_area_struct *vma)
3833{
3834 struct inode *inode = file_inode(vma->vm_file);
3835 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3836 loff_t i_size;
3837 pgoff_t off;
3838
3839 if (shmem_huge == SHMEM_HUGE_FORCE)
3840 return true;
3841 if (shmem_huge == SHMEM_HUGE_DENY)
3842 return false;
3843 switch (sbinfo->huge) {
3844 case SHMEM_HUGE_NEVER:
3845 return false;
3846 case SHMEM_HUGE_ALWAYS:
3847 return true;
3848 case SHMEM_HUGE_WITHIN_SIZE:
3849 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3850 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3851 if (i_size >= HPAGE_PMD_SIZE &&
3852 i_size >> PAGE_SHIFT >= off)
3853 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003854 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003855 case SHMEM_HUGE_ADVISE:
3856 /* TODO: implement fadvise() hints */
3857 return (vma->vm_flags & VM_HUGEPAGE);
3858 default:
3859 VM_BUG_ON(1);
3860 return false;
3861 }
3862}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003863#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003864
Matt Mackall853ac432009-01-06 14:40:20 -08003865#else /* !CONFIG_SHMEM */
3866
3867/*
3868 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3869 *
3870 * This is intended for small system where the benefits of the full
3871 * shmem code (swap-backed and resource-limited) are outweighed by
3872 * their complexity. On systems without swap this code should be
3873 * effectively equivalent, but much lighter weight.
3874 */
3875
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003876static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003877 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003878 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003879 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003880 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003881};
3882
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003883int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003884{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003885 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003886
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003887 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003888 BUG_ON(IS_ERR(shm_mnt));
3889
3890 return 0;
3891}
3892
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003893int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003894{
3895 return 0;
3896}
3897
Hugh Dickins3f96b792009-09-21 17:03:37 -07003898int shmem_lock(struct file *file, int lock, struct user_struct *user)
3899{
3900 return 0;
3901}
3902
Hugh Dickins24513262012-01-20 14:34:21 -08003903void shmem_unlock_mapping(struct address_space *mapping)
3904{
3905}
3906
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003907#ifdef CONFIG_MMU
3908unsigned long shmem_get_unmapped_area(struct file *file,
3909 unsigned long addr, unsigned long len,
3910 unsigned long pgoff, unsigned long flags)
3911{
3912 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3913}
3914#endif
3915
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003916void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003917{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003918 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003919}
3920EXPORT_SYMBOL_GPL(shmem_truncate_range);
3921
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003922#define shmem_vm_ops generic_file_vm_ops
3923#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003924#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003925#define shmem_acct_size(flags, size) 0
3926#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003927
3928#endif /* CONFIG_SHMEM */
3929
3930/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Matthew Auld703321b2017-10-06 23:18:13 +01003932static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003933 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003936 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937
Matthew Auld703321b2017-10-06 23:18:13 +01003938 if (IS_ERR(mnt))
3939 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Hugh Dickins285b2c42011-08-03 16:21:20 -07003941 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 return ERR_PTR(-EINVAL);
3943
3944 if (shmem_acct_size(flags, size))
3945 return ERR_PTR(-ENOMEM);
3946
Al Viro93dec2d2018-07-08 23:02:03 -04003947 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3948 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003949 if (unlikely(!inode)) {
3950 shmem_unacct_size(flags, size);
3951 return ERR_PTR(-ENOSPC);
3952 }
Eric Parisc7277092013-12-02 11:24:19 +00003953 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003955 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003956 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003957 if (!IS_ERR(res))
3958 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3959 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003960 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003961 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003962 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963}
Eric Parisc7277092013-12-02 11:24:19 +00003964
3965/**
3966 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3967 * kernel internal. There will be NO LSM permission checks against the
3968 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003969 * higher layer. The users are the big_key and shm implementations. LSM
3970 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003971 * @name: name for dentry (to be seen in /proc/<pid>/maps
3972 * @size: size to be set for the file
3973 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3974 */
3975struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3976{
Matthew Auld703321b2017-10-06 23:18:13 +01003977 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003978}
3979
3980/**
3981 * shmem_file_setup - get an unlinked file living in tmpfs
3982 * @name: name for dentry (to be seen in /proc/<pid>/maps
3983 * @size: size to be set for the file
3984 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3985 */
3986struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3987{
Matthew Auld703321b2017-10-06 23:18:13 +01003988 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003989}
Keith Packard395e0dd2008-06-20 00:08:06 -07003990EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Randy Dunlap46711812008-03-19 17:00:41 -07003992/**
Matthew Auld703321b2017-10-06 23:18:13 +01003993 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3994 * @mnt: the tmpfs mount where the file will be created
3995 * @name: name for dentry (to be seen in /proc/<pid>/maps
3996 * @size: size to be set for the file
3997 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3998 */
3999struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
4000 loff_t size, unsigned long flags)
4001{
4002 return __shmem_file_setup(mnt, name, size, flags, 0);
4003}
4004EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
4005
4006/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4009 */
4010int shmem_zero_setup(struct vm_area_struct *vma)
4011{
4012 struct file *file;
4013 loff_t size = vma->vm_end - vma->vm_start;
4014
Hugh Dickins66fc1302015-06-14 09:48:09 -07004015 /*
4016 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4017 * between XFS directory reading and selinux: since this file is only
4018 * accessible to the user through its mapping, use S_PRIVATE flag to
4019 * bypass file security, in the same way as shmem_kernel_file_setup().
4020 */
Matthew Auld703321b2017-10-06 23:18:13 +01004021 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 if (IS_ERR(file))
4023 return PTR_ERR(file);
4024
4025 if (vma->vm_file)
4026 fput(vma->vm_file);
4027 vma->vm_file = file;
4028 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004029
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004030 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004031 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4032 (vma->vm_end & HPAGE_PMD_MASK)) {
4033 khugepaged_enter(vma, vma->vm_flags);
4034 }
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 return 0;
4037}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004038
4039/**
4040 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4041 * @mapping: the page's address_space
4042 * @index: the page index
4043 * @gfp: the page allocator flags to use if allocating
4044 *
4045 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4046 * with any new page allocations done using the specified allocation flags.
4047 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4048 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4049 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4050 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004051 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4052 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004053 */
4054struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4055 pgoff_t index, gfp_t gfp)
4056{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004057#ifdef CONFIG_SHMEM
4058 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004059 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004060 int error;
4061
4062 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004063 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004064 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004065 if (error)
4066 page = ERR_PTR(error);
4067 else
4068 unlock_page(page);
4069 return page;
4070#else
4071 /*
4072 * The tiny !SHMEM case uses ramfs without swap
4073 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004074 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004075#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004076}
4077EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);