blob: e639188f9da436dd380594ccc98f85913c51bf69 [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>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010032#include <linux/sched/signal.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040033#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080034#include <linux/swap.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080035#include <linux/uio.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070036#include <linux/khugepaged.h>
Mike Kravetz749df872017-09-06 16:24:16 -070037#include <linux/hugetlb.h>
Matt Mackall853ac432009-01-06 14:40:20 -080038
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080039#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
40
Matt Mackall853ac432009-01-06 14:40:20 -080041static struct vfsmount *shm_mnt;
42
43#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * This virtual memory filesystem is heavily based on the ramfs. It
46 * extends ramfs by the ability to use swap and honor resource limits
47 * which makes it a completely usable filesystem.
48 */
49
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070050#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070051#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010052#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080053#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/string.h>
56#include <linux/slab.h>
57#include <linux/backing-dev.h>
58#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070061#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070062#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070063#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070064#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <linux/security.h>
66#include <linux/swapops.h>
67#include <linux/mempolicy.h>
68#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000069#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070070#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070071#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080072#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040073#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070074#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070075#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070076#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080077#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080078#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030079#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070080
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080081#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <asm/pgtable.h>
83
Mel Gormandd56b042015-11-06 16:28:43 -080084#include "internal.h"
85
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030086#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
87#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Pretend that each entry is of this size in directory's i_size */
90#define BOGO_DIRENT_SIZE 20
91
Hugh Dickins69f07ec2011-08-03 16:21:26 -070092/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
93#define SHORT_SYMLINK_LEN 128
94
Hugh Dickins1aac1402012-05-29 15:06:42 -070095/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070096 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
97 * inode->i_private (with i_mutex making sure that it has only one user at
98 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -070099 */
100struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700101 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700102 pgoff_t start; /* start of range currently being fallocated */
103 pgoff_t next; /* the next page offset to be fallocated */
104 pgoff_t nr_falloced; /* how many new pages have been fallocated */
105 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
106};
107
Andrew Mortonb76db732008-02-08 04:21:49 -0800108#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800109static unsigned long shmem_default_max_blocks(void)
110{
111 return totalram_pages / 2;
112}
113
114static unsigned long shmem_default_max_inodes(void)
115{
116 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
117}
Andrew Mortonb76db732008-02-08 04:21:49 -0800118#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800119
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700120static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
121static int shmem_replace_page(struct page **pagep, gfp_t gfp,
122 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700123static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700124 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800125 gfp_t gfp, struct vm_area_struct *vma,
126 struct vm_fault *vmf, int *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700127
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700128int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700129 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700130{
131 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800132 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
136{
137 return sb->s_fs_info;
138}
139
140/*
141 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
142 * for shared memory and for shared anonymous (/dev/zero) mappings
143 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
144 * consistent with the pre-accounting of private mappings ...
145 */
146static inline int shmem_acct_size(unsigned long flags, loff_t size)
147{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000148 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000149 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152static inline void shmem_unacct_size(unsigned long flags, loff_t size)
153{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000154 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 vm_unacct_memory(VM_ACCT(size));
156}
157
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700158static inline int shmem_reacct_size(unsigned long flags,
159 loff_t oldsize, loff_t newsize)
160{
161 if (!(flags & VM_NORESERVE)) {
162 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
163 return security_vm_enough_memory_mm(current->mm,
164 VM_ACCT(newsize) - VM_ACCT(oldsize));
165 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
166 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
167 }
168 return 0;
169}
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700173 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
175 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
176 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700177static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700179 if (!(flags & VM_NORESERVE))
180 return 0;
181
182 return security_vm_enough_memory_mm(current->mm,
183 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186static inline void shmem_unacct_blocks(unsigned long flags, long pages)
187{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000188 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300189 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Mike Rapoport0f079692017-09-06 16:22:59 -0700192static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
193{
194 struct shmem_inode_info *info = SHMEM_I(inode);
195 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
196
197 if (shmem_acct_block(info->flags, pages))
198 return false;
199
200 if (sbinfo->max_blocks) {
201 if (percpu_counter_compare(&sbinfo->used_blocks,
202 sbinfo->max_blocks - pages) > 0)
203 goto unacct;
204 percpu_counter_add(&sbinfo->used_blocks, pages);
205 }
206
207 return true;
208
209unacct:
210 shmem_unacct_blocks(info->flags, pages);
211 return false;
212}
213
214static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
215{
216 struct shmem_inode_info *info = SHMEM_I(inode);
217 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
218
219 if (sbinfo->max_blocks)
220 percpu_counter_sub(&sbinfo->used_blocks, pages);
221 shmem_unacct_blocks(info->flags, pages);
222}
223
Hugh Dickins759b9772007-03-05 00:30:28 -0800224static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700225static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800226static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800227static const struct inode_operations shmem_inode_operations;
228static const struct inode_operations shmem_dir_inode_operations;
229static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400230static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700231static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Mike Rapoportb0506e42017-02-22 15:43:28 -0800233bool vma_is_shmem(struct vm_area_struct *vma)
234{
235 return vma->vm_ops == &shmem_vm_ops;
236}
237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800239static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800241static int shmem_reserve_inode(struct super_block *sb)
242{
243 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
244 if (sbinfo->max_inodes) {
245 spin_lock(&sbinfo->stat_lock);
246 if (!sbinfo->free_inodes) {
247 spin_unlock(&sbinfo->stat_lock);
248 return -ENOSPC;
249 }
250 sbinfo->free_inodes--;
251 spin_unlock(&sbinfo->stat_lock);
252 }
253 return 0;
254}
255
256static void shmem_free_inode(struct super_block *sb)
257{
258 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
259 if (sbinfo->max_inodes) {
260 spin_lock(&sbinfo->stat_lock);
261 sbinfo->free_inodes++;
262 spin_unlock(&sbinfo->stat_lock);
263 }
264}
265
Randy Dunlap46711812008-03-19 17:00:41 -0700266/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700267 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 * @inode: inode to recalc
269 *
270 * We have to calculate the free blocks since the mm can drop
271 * undirtied hole pages behind our back.
272 *
273 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
274 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
275 *
276 * It has to be called with the spinlock held.
277 */
278static void shmem_recalc_inode(struct inode *inode)
279{
280 struct shmem_inode_info *info = SHMEM_I(inode);
281 long freed;
282
283 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
284 if (freed > 0) {
285 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700286 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700287 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 }
289}
290
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700291bool shmem_charge(struct inode *inode, long pages)
292{
293 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700294 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700295
Mike Rapoport0f079692017-09-06 16:22:59 -0700296 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700297 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700298
Hugh Dickins98f1ae12018-11-30 14:10:29 -0800299 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
300 inode->i_mapping->nrpages += pages;
301
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700302 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700303 info->alloced += pages;
304 inode->i_blocks += pages * BLOCKS_PER_PAGE;
305 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700306 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700307
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700308 return true;
309}
310
311void shmem_uncharge(struct inode *inode, long pages)
312{
313 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700314 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700315
Hugh Dickins98f1ae12018-11-30 14:10:29 -0800316 /* nrpages adjustment done by __delete_from_page_cache() or caller */
317
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700318 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700319 info->alloced -= pages;
320 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
321 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700322 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700323
Mike Rapoport0f079692017-09-06 16:22:59 -0700324 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700325}
326
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700327/*
328 * Replace item expected in radix tree by a new item, while holding tree lock.
329 */
330static int shmem_radix_tree_replace(struct address_space *mapping,
331 pgoff_t index, void *expected, void *replacement)
332{
Johannes Weinerf7942432016-12-12 16:43:41 -0800333 struct radix_tree_node *node;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700334 void **pslot;
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700335 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700336
337 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700338 VM_BUG_ON(!replacement);
Johannes Weinerf7942432016-12-12 16:43:41 -0800339 item = __radix_tree_lookup(&mapping->page_tree, index, &node, &pslot);
340 if (!item)
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700341 return -ENOENT;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700342 if (item != expected)
343 return -ENOENT;
Johannes Weiner4d693d02016-12-12 16:43:49 -0800344 __radix_tree_replace(&mapping->page_tree, node, pslot,
345 replacement, NULL, NULL);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700346 return 0;
347}
348
349/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700350 * Sometimes, before we decide whether to proceed or to fail, we must check
351 * that an entry was not already brought back from swap by a racing thread.
352 *
353 * Checking page is not enough: by the time a SwapCache page is locked, it
354 * might be reused, and again be SwapCache, using the same swap as before.
355 */
356static bool shmem_confirm_swap(struct address_space *mapping,
357 pgoff_t index, swp_entry_t swap)
358{
359 void *item;
360
361 rcu_read_lock();
362 item = radix_tree_lookup(&mapping->page_tree, index);
363 rcu_read_unlock();
364 return item == swp_to_radix_entry(swap);
365}
366
367/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700368 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
369 *
370 * SHMEM_HUGE_NEVER:
371 * disables huge pages for the mount;
372 * SHMEM_HUGE_ALWAYS:
373 * enables huge pages for the mount;
374 * SHMEM_HUGE_WITHIN_SIZE:
375 * only allocate huge pages if the page will be fully within i_size,
376 * also respect fadvise()/madvise() hints;
377 * SHMEM_HUGE_ADVISE:
378 * only allocate huge pages if requested with fadvise()/madvise();
379 */
380
381#define SHMEM_HUGE_NEVER 0
382#define SHMEM_HUGE_ALWAYS 1
383#define SHMEM_HUGE_WITHIN_SIZE 2
384#define SHMEM_HUGE_ADVISE 3
385
386/*
387 * Special values.
388 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
389 *
390 * SHMEM_HUGE_DENY:
391 * disables huge on shm_mnt and all mounts, for emergency use;
392 * SHMEM_HUGE_FORCE:
393 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
394 *
395 */
396#define SHMEM_HUGE_DENY (-1)
397#define SHMEM_HUGE_FORCE (-2)
398
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700399#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700400/* ifdef here to avoid bloating shmem.o when not necessary */
401
402int shmem_huge __read_mostly;
403
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800404#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700405static int shmem_parse_huge(const char *str)
406{
407 if (!strcmp(str, "never"))
408 return SHMEM_HUGE_NEVER;
409 if (!strcmp(str, "always"))
410 return SHMEM_HUGE_ALWAYS;
411 if (!strcmp(str, "within_size"))
412 return SHMEM_HUGE_WITHIN_SIZE;
413 if (!strcmp(str, "advise"))
414 return SHMEM_HUGE_ADVISE;
415 if (!strcmp(str, "deny"))
416 return SHMEM_HUGE_DENY;
417 if (!strcmp(str, "force"))
418 return SHMEM_HUGE_FORCE;
419 return -EINVAL;
420}
421
422static const char *shmem_format_huge(int huge)
423{
424 switch (huge) {
425 case SHMEM_HUGE_NEVER:
426 return "never";
427 case SHMEM_HUGE_ALWAYS:
428 return "always";
429 case SHMEM_HUGE_WITHIN_SIZE:
430 return "within_size";
431 case SHMEM_HUGE_ADVISE:
432 return "advise";
433 case SHMEM_HUGE_DENY:
434 return "deny";
435 case SHMEM_HUGE_FORCE:
436 return "force";
437 default:
438 VM_BUG_ON(1);
439 return "bad_val";
440 }
441}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800442#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700443
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700444static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
445 struct shrink_control *sc, unsigned long nr_to_split)
446{
447 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800448 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700449 struct inode *inode;
450 struct shmem_inode_info *info;
451 struct page *page;
452 unsigned long batch = sc ? sc->nr_to_scan : 128;
Gang Lid8d95592022-01-14 14:05:23 -0800453 int split = 0;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700454
455 if (list_empty(&sbinfo->shrinklist))
456 return SHRINK_STOP;
457
458 spin_lock(&sbinfo->shrinklist_lock);
459 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
460 info = list_entry(pos, struct shmem_inode_info, shrinklist);
461
462 /* pin the inode */
463 inode = igrab(&info->vfs_inode);
464
465 /* inode is about to be evicted */
466 if (!inode) {
467 list_del_init(&info->shrinklist);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700468 goto next;
469 }
470
471 /* Check if there's anything to gain */
472 if (round_up(inode->i_size, PAGE_SIZE) ==
473 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800474 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700475 goto next;
476 }
477
478 list_move(&info->shrinklist, &list);
479next:
Gang Lid8d95592022-01-14 14:05:23 -0800480 sbinfo->shrinklist_len--;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700481 if (!--batch)
482 break;
483 }
484 spin_unlock(&sbinfo->shrinklist_lock);
485
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800486 list_for_each_safe(pos, next, &to_remove) {
487 info = list_entry(pos, struct shmem_inode_info, shrinklist);
488 inode = &info->vfs_inode;
489 list_del_init(&info->shrinklist);
490 iput(inode);
491 }
492
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700493 list_for_each_safe(pos, next, &list) {
494 int ret;
495
496 info = list_entry(pos, struct shmem_inode_info, shrinklist);
497 inode = &info->vfs_inode;
498
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700499 if (nr_to_split && split >= nr_to_split)
Gang Lid8d95592022-01-14 14:05:23 -0800500 goto move_back;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700501
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700502 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700503 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
504 if (!page)
505 goto drop;
506
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700507 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700508 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700509 put_page(page);
510 goto drop;
511 }
512
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700513 /*
Gang Lid8d95592022-01-14 14:05:23 -0800514 * Move the inode on the list back to shrinklist if we failed
515 * to lock the page at this time.
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700516 *
517 * Waiting for the lock may lead to deadlock in the
518 * reclaim path.
519 */
520 if (!trylock_page(page)) {
521 put_page(page);
Gang Lid8d95592022-01-14 14:05:23 -0800522 goto move_back;
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700523 }
524
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700525 ret = split_huge_page(page);
526 unlock_page(page);
527 put_page(page);
528
Gang Lid8d95592022-01-14 14:05:23 -0800529 /* If split failed move the inode on the list back to shrinklist */
Kirill A. Shutemovf5dad042018-03-22 16:17:35 -0700530 if (ret)
Gang Lid8d95592022-01-14 14:05:23 -0800531 goto move_back;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700532
533 split++;
534drop:
535 list_del_init(&info->shrinklist);
Gang Lid8d95592022-01-14 14:05:23 -0800536 goto put;
537move_back:
538 /*
539 * Make sure the inode is either on the global list or deleted
540 * from any local list before iput() since it could be deleted
541 * in another thread once we put the inode (then the local list
542 * is corrupted).
543 */
544 spin_lock(&sbinfo->shrinklist_lock);
545 list_move(&info->shrinklist, &sbinfo->shrinklist);
546 sbinfo->shrinklist_len++;
547 spin_unlock(&sbinfo->shrinklist_lock);
548put:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700549 iput(inode);
550 }
551
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700552 return split;
553}
554
555static long shmem_unused_huge_scan(struct super_block *sb,
556 struct shrink_control *sc)
557{
558 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
559
560 if (!READ_ONCE(sbinfo->shrinklist_len))
561 return SHRINK_STOP;
562
563 return shmem_unused_huge_shrink(sbinfo, sc, 0);
564}
565
566static long shmem_unused_huge_count(struct super_block *sb,
567 struct shrink_control *sc)
568{
569 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
570 return READ_ONCE(sbinfo->shrinklist_len);
571}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700572#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700573
574#define shmem_huge SHMEM_HUGE_DENY
575
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700576static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
577 struct shrink_control *sc, unsigned long nr_to_split)
578{
579 return 0;
580}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700581#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700582
583/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700584 * Like add_to_page_cache_locked, but error if expected item has gone.
585 */
586static int shmem_add_to_page_cache(struct page *page,
587 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700588 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700589{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700590 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700591
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700592 VM_BUG_ON_PAGE(PageTail(page), page);
593 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800594 VM_BUG_ON_PAGE(!PageLocked(page), page);
595 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700596 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700597
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700598 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700599 page->mapping = mapping;
600 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700601
Hugh Dickinsb065b432012-07-11 14:02:48 -0700602 spin_lock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700603 if (PageTransHuge(page)) {
604 void __rcu **results;
605 pgoff_t idx;
606 int i;
607
608 error = 0;
609 if (radix_tree_gang_lookup_slot(&mapping->page_tree,
610 &results, &idx, index, 1) &&
611 idx < index + HPAGE_PMD_NR) {
612 error = -EEXIST;
613 }
614
615 if (!error) {
616 for (i = 0; i < HPAGE_PMD_NR; i++) {
617 error = radix_tree_insert(&mapping->page_tree,
618 index + i, page + i);
619 VM_BUG_ON(error);
620 }
621 count_vm_event(THP_FILE_ALLOC);
622 }
623 } else if (!expected) {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700624 error = radix_tree_insert(&mapping->page_tree, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700625 } else {
Hugh Dickinsb065b432012-07-11 14:02:48 -0700626 error = shmem_radix_tree_replace(mapping, index, expected,
627 page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700628 }
629
Hugh Dickinsb065b432012-07-11 14:02:48 -0700630 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700631 mapping->nrpages += nr;
632 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700633 __inc_node_page_state(page, NR_SHMEM_THPS);
634 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
635 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700636 spin_unlock_irq(&mapping->tree_lock);
637 } else {
638 page->mapping = NULL;
639 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700640 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700641 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700642 return error;
643}
644
645/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700646 * Like delete_from_page_cache, but substitutes swap for page.
647 */
648static void shmem_delete_from_page_cache(struct page *page, void *radswap)
649{
650 struct address_space *mapping = page->mapping;
651 int error;
652
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700653 VM_BUG_ON_PAGE(PageCompound(page), page);
654
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700655 spin_lock_irq(&mapping->tree_lock);
656 error = shmem_radix_tree_replace(mapping, page->index, page, radswap);
657 page->mapping = NULL;
658 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700659 __dec_node_page_state(page, NR_FILE_PAGES);
660 __dec_node_page_state(page, NR_SHMEM);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700661 spin_unlock_irq(&mapping->tree_lock);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300662 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700663 BUG_ON(error);
664}
665
666/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700667 * Remove swap entry from radix tree, free the swap and its page cache.
668 */
669static int shmem_free_swap(struct address_space *mapping,
670 pgoff_t index, void *radswap)
671{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700672 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700673
674 spin_lock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700675 old = radix_tree_delete_item(&mapping->page_tree, index, radswap);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700676 spin_unlock_irq(&mapping->tree_lock);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700677 if (old != radswap)
678 return -ENOENT;
679 free_swap_and_cache(radix_to_swp_entry(radswap));
680 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700681}
682
683/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800684 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800685 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800686 *
687 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
688 * as long as the inode doesn't go away and racy results are not a problem.
689 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800690unsigned long shmem_partial_swap_usage(struct address_space *mapping,
691 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800692{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800693 struct radix_tree_iter iter;
694 void **slot;
695 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800696 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800697
698 rcu_read_lock();
699
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800700 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
701 if (iter.index >= end)
702 break;
703
704 page = radix_tree_deref_slot(slot);
705
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700706 if (radix_tree_deref_retry(page)) {
707 slot = radix_tree_iter_retry(&iter);
708 continue;
709 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800710
711 if (radix_tree_exceptional_entry(page))
712 swapped++;
713
714 if (need_resched()) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800715 slot = radix_tree_iter_resume(slot, &iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800716 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800717 }
718 }
719
720 rcu_read_unlock();
721
722 return swapped << PAGE_SHIFT;
723}
724
725/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800726 * Determine (in bytes) how many of the shmem object's pages mapped by the
727 * given vma is swapped out.
728 *
729 * This is safe to call without i_mutex or mapping->tree_lock thanks to RCU,
730 * as long as the inode doesn't go away and racy results are not a problem.
731 */
732unsigned long shmem_swap_usage(struct vm_area_struct *vma)
733{
734 struct inode *inode = file_inode(vma->vm_file);
735 struct shmem_inode_info *info = SHMEM_I(inode);
736 struct address_space *mapping = inode->i_mapping;
737 unsigned long swapped;
738
739 /* Be careful as we don't hold info->lock */
740 swapped = READ_ONCE(info->swapped);
741
742 /*
743 * The easier cases are when the shmem object has nothing in swap, or
744 * the vma maps it whole. Then we can simply use the stats that we
745 * already track.
746 */
747 if (!swapped)
748 return 0;
749
750 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
751 return swapped << PAGE_SHIFT;
752
753 /* Here comes the more involved part */
754 return shmem_partial_swap_usage(mapping,
755 linear_page_index(vma, vma->vm_start),
756 linear_page_index(vma, vma->vm_end));
757}
758
759/*
Hugh Dickins24513262012-01-20 14:34:21 -0800760 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
761 */
762void shmem_unlock_mapping(struct address_space *mapping)
763{
764 struct pagevec pvec;
765 pgoff_t indices[PAGEVEC_SIZE];
766 pgoff_t index = 0;
767
768 pagevec_init(&pvec, 0);
769 /*
770 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
771 */
772 while (!mapping_unevictable(mapping)) {
773 /*
774 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
775 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
776 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700777 pvec.nr = find_get_entries(mapping, index,
778 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800779 if (!pvec.nr)
780 break;
781 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700782 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800783 check_move_unevictable_pages(pvec.pages, pvec.nr);
784 pagevec_release(&pvec);
785 cond_resched();
786 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700787}
788
789/*
790 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700791 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700792 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700793static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
794 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700796 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300798 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
799 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
800 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
801 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700802 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700803 pgoff_t indices[PAGEVEC_SIZE];
804 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700805 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700806 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700808 if (lend == -1)
809 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700810
811 pagevec_init(&pvec, 0);
812 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700813 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700814 pvec.nr = find_get_entries(mapping, index,
815 min(end - index, (pgoff_t)PAGEVEC_SIZE),
816 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700817 if (!pvec.nr)
818 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700819 for (i = 0; i < pagevec_count(&pvec); i++) {
820 struct page *page = pvec.pages[i];
821
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700822 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700823 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700824 break;
825
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700826 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700827 if (unfalloc)
828 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700829 nr_swaps_freed += !shmem_free_swap(mapping,
830 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700831 continue;
832 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700833
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700834 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
835
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700836 if (!trylock_page(page))
837 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700838
839 if (PageTransTail(page)) {
840 /* Middle of THP: zero out the page */
841 clear_highpage(page);
842 unlock_page(page);
843 continue;
844 } else if (PageTransHuge(page)) {
845 if (index == round_down(end, HPAGE_PMD_NR)) {
846 /*
847 * Range ends in the middle of THP:
848 * zero out the page
849 */
850 clear_highpage(page);
851 unlock_page(page);
852 continue;
853 }
854 index += HPAGE_PMD_NR - 1;
855 i += HPAGE_PMD_NR - 1;
856 }
857
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700858 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700859 VM_BUG_ON_PAGE(PageTail(page), page);
860 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800861 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700862 truncate_inode_page(mapping, page);
863 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700864 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700865 unlock_page(page);
866 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700867 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800868 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700869 cond_resched();
870 index++;
871 }
872
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700873 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700874 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700875 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700876 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300877 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700878 if (start > end) {
879 top = partial_end;
880 partial_end = 0;
881 }
882 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700883 set_page_dirty(page);
884 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300885 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700886 }
887 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700888 if (partial_end) {
889 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700890 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700891 if (page) {
892 zero_user_segment(page, 0, partial_end);
893 set_page_dirty(page);
894 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300895 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700896 }
897 }
898 if (start >= end)
899 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700900
901 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700902 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700903 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700904
905 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700906 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700907 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700908 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700909 /* If all gone or hole-punch or unfalloc, we're done */
910 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700911 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700912 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700913 index = start;
914 continue;
915 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700916 for (i = 0; i < pagevec_count(&pvec); i++) {
917 struct page *page = pvec.pages[i];
918
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700919 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700920 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700921 break;
922
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700923 if (radix_tree_exceptional_entry(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700924 if (unfalloc)
925 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700926 if (shmem_free_swap(mapping, index, page)) {
927 /* Swap was replaced by page: retry */
928 index--;
929 break;
930 }
931 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700932 continue;
933 }
934
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700935 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700936
937 if (PageTransTail(page)) {
938 /* Middle of THP: zero out the page */
939 clear_highpage(page);
940 unlock_page(page);
941 /*
942 * Partial thp truncate due 'start' in middle
943 * of THP: don't need to look on these pages
944 * again on !pvec.nr restart.
945 */
946 if (index != round_down(end, HPAGE_PMD_NR))
947 start++;
948 continue;
949 } else if (PageTransHuge(page)) {
950 if (index == round_down(end, HPAGE_PMD_NR)) {
951 /*
952 * Range ends in the middle of THP:
953 * zero out the page
954 */
955 clear_highpage(page);
956 unlock_page(page);
957 continue;
958 }
959 index += HPAGE_PMD_NR - 1;
960 i += HPAGE_PMD_NR - 1;
961 }
962
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700963 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700964 VM_BUG_ON_PAGE(PageTail(page), page);
965 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800966 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700967 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700968 } else {
969 /* Page was replaced by swap: retry */
970 unlock_page(page);
971 index--;
972 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700973 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700974 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700975 unlock_page(page);
976 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700977 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800978 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700979 index++;
980 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700981
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700982 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700983 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700985 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700986}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700988void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
989{
990 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700991 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700993EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
David Howellsa528d352017-01-31 16:46:22 +0000995static int shmem_getattr(const struct path *path, struct kstat *stat,
996 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700997{
David Howellsa528d352017-01-31 16:46:22 +0000998 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -0700999 struct shmem_inode_info *info = SHMEM_I(inode);
1000
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001001 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001002 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001003 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001004 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001005 }
Yu Zhao44a30222015-09-08 15:03:33 -07001006 generic_fillattr(inode, stat);
Yu Zhao44a30222015-09-08 15:03:33 -07001007 return 0;
1008}
1009
Hugh Dickins94c1e622011-06-27 16:18:03 -07001010static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
David Howells75c3cfa2015-03-17 22:26:12 +00001012 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001013 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001014 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int error;
1016
Jan Kara31051c82016-05-26 16:55:18 +02001017 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001018 if (error)
1019 return error;
1020
Hugh Dickins94c1e622011-06-27 16:18:03 -07001021 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1022 loff_t oldsize = inode->i_size;
1023 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001024
David Herrmann40e041a2014-08-08 14:25:27 -07001025 /* protected by i_mutex */
1026 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1027 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1028 return -EPERM;
1029
Hugh Dickins94c1e622011-06-27 16:18:03 -07001030 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001031 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1032 oldsize, newsize);
1033 if (error)
1034 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001035 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001036 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001037 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001038 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001039 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001040 if (oldsize > holebegin)
1041 unmap_mapping_range(inode->i_mapping,
1042 holebegin, 0, 1);
1043 if (info->alloced)
1044 shmem_truncate_range(inode,
1045 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001046 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001047 if (oldsize > holebegin)
1048 unmap_mapping_range(inode->i_mapping,
1049 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001050
1051 /*
1052 * Part of the huge page can be beyond i_size: subject
1053 * to shrink under memory pressure.
1054 */
1055 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1056 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001057 /*
1058 * _careful to defend against unlocked access to
1059 * ->shrink_list in shmem_unused_huge_shrink()
1060 */
1061 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001062 list_add_tail(&info->shrinklist,
1063 &sbinfo->shrinklist);
1064 sbinfo->shrinklist_len++;
1065 }
1066 spin_unlock(&sbinfo->shrinklist_lock);
1067 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001071 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001072 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001073 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return error;
1075}
1076
Al Viro1f895f72010-06-05 19:10:41 -04001077static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001080 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001082 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 shmem_unacct_size(info->flags, inode->i_size);
1084 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001085 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001086 if (!list_empty(&info->shrinklist)) {
1087 spin_lock(&sbinfo->shrinklist_lock);
1088 if (!list_empty(&info->shrinklist)) {
1089 list_del_init(&info->shrinklist);
1090 sbinfo->shrinklist_len--;
1091 }
1092 spin_unlock(&sbinfo->shrinklist_lock);
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001095 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001097 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
Al Viro3ed47db2016-01-22 18:08:52 -05001099 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001100
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001101 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001102 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001103 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001104 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
1106
Matthew Wilcox478922e2016-12-14 15:08:52 -08001107static unsigned long find_swap_entry(struct radix_tree_root *root, void *item)
1108{
1109 struct radix_tree_iter iter;
1110 void **slot;
1111 unsigned long found = -1;
1112 unsigned int checked = 0;
1113
1114 rcu_read_lock();
1115 radix_tree_for_each_slot(slot, root, &iter, 0) {
1116 if (*slot == item) {
1117 found = iter.index;
1118 break;
1119 }
1120 checked++;
1121 if ((checked % 4096) != 0)
1122 continue;
1123 slot = radix_tree_iter_resume(slot, &iter);
1124 cond_resched_rcu();
1125 }
1126
1127 rcu_read_unlock();
1128 return found;
1129}
1130
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001131/*
1132 * If swap found in inode, free it and move page from swapcache to filecache.
1133 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001134static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001135 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001137 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001138 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001139 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001140 gfp_t gfp;
1141 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001143 radswap = swp_to_radix_entry(swap);
Matthew Wilcox478922e2016-12-14 15:08:52 -08001144 index = find_swap_entry(&mapping->page_tree, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001145 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001146 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001147
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001148 /*
1149 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001150 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001151 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001152 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001153 */
1154 if (shmem_swaplist.next != &info->swaplist)
1155 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001156
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001157 gfp = mapping_gfp_mask(mapping);
1158 if (shmem_should_replace_page(*pagep, gfp)) {
1159 mutex_unlock(&shmem_swaplist_mutex);
1160 error = shmem_replace_page(pagep, gfp, info, index);
1161 mutex_lock(&shmem_swaplist_mutex);
1162 /*
1163 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001164 * allocation, but the inode might have been freed while we
1165 * dropped it: although a racing shmem_evict_inode() cannot
1166 * complete without emptying the radix_tree, our page lock
1167 * on this swapcache page is not enough to prevent that -
1168 * free_swap_and_cache() of our swap entry will only
1169 * trylock_page(), removing swap from radix_tree whatever.
1170 *
1171 * We must not proceed to shmem_add_to_page_cache() if the
1172 * inode has been freed, but of course we cannot rely on
1173 * inode or mapping or info to check that. However, we can
1174 * safely check if our swap entry is still in use (and here
1175 * it can't have got reused for another page): if it's still
1176 * in use, then the inode cannot have been freed yet, and we
1177 * can safely proceed (if it's no longer in use, that tells
1178 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001179 */
1180 if (!page_swapcount(*pagep))
1181 error = -ENOENT;
1182 }
1183
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001184 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001185 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1186 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1187 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001188 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001189 if (!error)
1190 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001191 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001192 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001193 /*
1194 * Truncation and eviction use free_swap_and_cache(), which
1195 * only does trylock page: if we raced, best clean up here.
1196 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001197 delete_from_swap_cache(*pagep);
1198 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001199 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001200 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001201 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001202 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001203 swap_free(swap);
1204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001206 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
1208
1209/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001210 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001212int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001214 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001216 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001217 int error = 0;
1218
1219 /*
1220 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001221 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001222 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001223 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001224 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001225
1226 /*
1227 * Charge page using GFP_KERNEL while we can wait, before taking
1228 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1229 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001230 */
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001231 error = mem_cgroup_try_charge(page, current->mm, GFP_KERNEL, &memcg,
1232 false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001233 if (error)
1234 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001235 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001236 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001238 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001239 list_for_each_safe(this, next, &shmem_swaplist) {
1240 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001241 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001242 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001243 else
1244 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001245 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001246 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001247 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001248 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001250 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001251
Johannes Weiner00501b52014-08-08 14:19:20 -07001252 if (error) {
1253 if (error != -ENOMEM)
1254 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001255 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001256 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001257 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001258out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001259 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001260 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001261 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
1264/*
1265 * Move the page from the page cache to the swap cache.
1266 */
1267static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1268{
1269 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001272 swp_entry_t swap;
1273 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001275 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 mapping = page->mapping;
1278 index = page->index;
1279 inode = mapping->host;
1280 info = SHMEM_I(inode);
1281 if (info->flags & VM_LOCKED)
1282 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001283 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 goto redirty;
1285
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001286 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001287 * Our capabilities prevent regular writeback or sync from ever calling
1288 * shmem_writepage; but a stacking filesystem might use ->writepage of
1289 * its underlying filesystem, in which case tmpfs should write out to
1290 * swap only in response to memory pressure, and not for the writeback
1291 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001292 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001293 if (!wbc->for_reclaim) {
1294 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1295 goto redirty;
1296 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001297
1298 /*
1299 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1300 * value into swapfile.c, the only way we can correctly account for a
1301 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001302 *
1303 * That's okay for a page already fallocated earlier, but if we have
1304 * not yet completed the fallocation, then (a) we want to keep track
1305 * of this page in case we have to undo it, and (b) it may not be a
1306 * good idea to continue anyway, once we're pushing into swap. So
1307 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001308 */
1309 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001310 if (inode->i_private) {
1311 struct shmem_falloc *shmem_falloc;
1312 spin_lock(&inode->i_lock);
1313 shmem_falloc = inode->i_private;
1314 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001315 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001316 index >= shmem_falloc->start &&
1317 index < shmem_falloc->next)
1318 shmem_falloc->nr_unswapped++;
1319 else
1320 shmem_falloc = NULL;
1321 spin_unlock(&inode->i_lock);
1322 if (shmem_falloc)
1323 goto redirty;
1324 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001325 clear_highpage(page);
1326 flush_dcache_page(page);
1327 SetPageUptodate(page);
1328 }
1329
Huang Ying38d8b4e2017-07-06 15:37:18 -07001330 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001331 if (!swap.val)
1332 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001333
Vladimir Davydov37e84352016-01-20 15:02:56 -08001334 if (mem_cgroup_try_charge_swap(page, swap))
1335 goto free_swap;
1336
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001337 /*
1338 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001339 * if it's not already there. Do it now before the page is
1340 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001341 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001342 * we've incremented swapped, because shmem_unuse_inode() will
1343 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001344 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001345 mutex_lock(&shmem_swaplist_mutex);
1346 if (list_empty(&info->swaplist))
1347 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001348
Hugh Dickins48f170f2011-07-25 17:12:37 -07001349 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001350 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001351 shmem_recalc_inode(inode);
1352 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001353 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001354
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001355 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001356 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1357
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001358 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001359 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001360 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362 }
1363
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001364 mutex_unlock(&shmem_swaplist_mutex);
Vladimir Davydov37e84352016-01-20 15:02:56 -08001365free_swap:
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001366 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367redirty:
1368 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001369 if (wbc->for_reclaim)
1370 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1371 unlock_page(page);
1372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Hugh Dickins75edd342016-05-19 17:12:44 -07001375#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001376static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001377{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001378 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001379
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001380 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001381 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001382
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001383 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001384
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001385 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001386}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001387
1388static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1389{
1390 struct mempolicy *mpol = NULL;
1391 if (sbinfo->mpol) {
1392 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1393 mpol = sbinfo->mpol;
1394 mpol_get(mpol);
1395 spin_unlock(&sbinfo->stat_lock);
1396 }
1397 return mpol;
1398}
Hugh Dickins75edd342016-05-19 17:12:44 -07001399#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1400static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1401{
1402}
1403static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1404{
1405 return NULL;
1406}
1407#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1408#ifndef CONFIG_NUMA
1409#define vm_policy vm_private_data
1410#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001411
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001412static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1413 struct shmem_inode_info *info, pgoff_t index)
1414{
1415 /* Create a pseudo vma that just contains the policy */
1416 vma->vm_start = 0;
1417 /* Bias interleave by inode number to distribute better across nodes */
1418 vma->vm_pgoff = index + info->vfs_inode.i_ino;
1419 vma->vm_ops = NULL;
1420 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1421}
1422
1423static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1424{
1425 /* Drop reference taken by mpol_shared_policy_lookup() */
1426 mpol_cond_put(vma->vm_policy);
1427}
1428
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001429static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1430 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001433 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001435 shmem_pseudo_vma_init(&pvma, info, index);
Mel Gorman18a2f372012-12-05 14:01:41 -08001436 page = swapin_readahead(swap, gfp, &pvma, 0);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001437 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001438
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001439 return page;
1440}
Mel Gorman18a2f372012-12-05 14:01:41 -08001441
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001442static struct page *shmem_alloc_hugepage(gfp_t gfp,
1443 struct shmem_inode_info *info, pgoff_t index)
1444{
1445 struct vm_area_struct pvma;
1446 struct inode *inode = &info->vfs_inode;
1447 struct address_space *mapping = inode->i_mapping;
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001448 pgoff_t idx, hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001449 void __rcu **results;
1450 struct page *page;
1451
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001452 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001453 return NULL;
1454
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001455 hindex = round_down(index, HPAGE_PMD_NR);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001456 rcu_read_lock();
1457 if (radix_tree_gang_lookup_slot(&mapping->page_tree, &results, &idx,
1458 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1459 rcu_read_unlock();
1460 return NULL;
1461 }
1462 rcu_read_unlock();
1463
1464 shmem_pseudo_vma_init(&pvma, info, hindex);
1465 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1466 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1467 shmem_pseudo_vma_destroy(&pvma);
1468 if (page)
1469 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001470 return page;
1471}
1472
1473static struct page *shmem_alloc_page(gfp_t gfp,
1474 struct shmem_inode_info *info, pgoff_t index)
1475{
1476 struct vm_area_struct pvma;
1477 struct page *page;
1478
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001479 shmem_pseudo_vma_init(&pvma, info, index);
1480 page = alloc_page_vma(gfp, &pvma, 0);
1481 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001482
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001483 return page;
1484}
1485
1486static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001487 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001488 pgoff_t index, bool huge)
1489{
Mike Rapoport0f079692017-09-06 16:22:59 -07001490 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001491 struct page *page;
1492 int nr;
1493 int err = -ENOSPC;
1494
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001495 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001496 huge = false;
1497 nr = huge ? HPAGE_PMD_NR : 1;
1498
Mike Rapoport0f079692017-09-06 16:22:59 -07001499 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001500 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001501
1502 if (huge)
1503 page = shmem_alloc_hugepage(gfp, info, index);
1504 else
1505 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001506 if (page) {
1507 __SetPageLocked(page);
1508 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001509 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001510 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001511
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001512 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001513 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001514failed:
1515 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001519 * When a page is moved from swapcache to shmem filecache (either by the
1520 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1521 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1522 * ignorance of the mapping it belongs to. If that mapping has special
1523 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1524 * we may need to copy to a suitable page before moving to filecache.
1525 *
1526 * In a future release, this may well be extended to respect cpuset and
1527 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1528 * but for now it is a simple matter of zone.
1529 */
1530static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1531{
1532 return page_zonenum(page) > gfp_zone(gfp);
1533}
1534
1535static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1536 struct shmem_inode_info *info, pgoff_t index)
1537{
1538 struct page *oldpage, *newpage;
1539 struct address_space *swap_mapping;
Yu Zhao7c1ba1a2018-11-30 14:09:03 -08001540 swp_entry_t entry;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001541 pgoff_t swap_index;
1542 int error;
1543
1544 oldpage = *pagep;
Yu Zhao7c1ba1a2018-11-30 14:09:03 -08001545 entry.val = page_private(oldpage);
1546 swap_index = swp_offset(entry);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001547 swap_mapping = page_mapping(oldpage);
1548
1549 /*
1550 * We have arrived here because our zones are constrained, so don't
1551 * limit chance of success by further cpuset and node constraints.
1552 */
1553 gfp &= ~GFP_CONSTRAINT_MASK;
1554 newpage = shmem_alloc_page(gfp, info, index);
1555 if (!newpage)
1556 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001557
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001558 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001559 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001560 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001561
Hugh Dickins9956edf2016-11-10 10:46:11 -08001562 __SetPageLocked(newpage);
1563 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001564 SetPageUptodate(newpage);
Yu Zhao7c1ba1a2018-11-30 14:09:03 -08001565 set_page_private(newpage, entry.val);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001566 SetPageSwapCache(newpage);
1567
1568 /*
1569 * Our caller will very soon move newpage out of swapcache, but it's
1570 * a nice clean interface for us to replace oldpage by newpage there.
1571 */
1572 spin_lock_irq(&swap_mapping->tree_lock);
1573 error = shmem_radix_tree_replace(swap_mapping, swap_index, oldpage,
1574 newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001575 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001576 __inc_node_page_state(newpage, NR_FILE_PAGES);
1577 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001578 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001579 spin_unlock_irq(&swap_mapping->tree_lock);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001580
Hugh Dickins0142ef62012-06-07 14:21:09 -07001581 if (unlikely(error)) {
1582 /*
1583 * Is this possible? I think not, now that our callers check
1584 * both PageSwapCache and page_private after getting page lock;
1585 * but be defensive. Reverse old to newpage for clear and free.
1586 */
1587 oldpage = newpage;
1588 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001589 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001590 lru_cache_add_anon(newpage);
1591 *pagep = newpage;
1592 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001593
1594 ClearPageSwapCache(oldpage);
1595 set_page_private(oldpage, 0);
1596
1597 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001598 put_page(oldpage);
1599 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001600 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001601}
1602
1603/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001604 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 *
1606 * If we allocate a new one we do not mark it dirty. That's up to the
1607 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001608 * entry since a page cannot live in both the swap and page cache.
1609 *
1610 * fault_mm and fault_type are only supplied by shmem_fault:
1611 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001613static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001614 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08001615 struct vm_area_struct *vma, struct vm_fault *vmf, int *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001618 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001620 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001621 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001622 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001624 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001625 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001627 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001628 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001630 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001632 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1633 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001635 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001636 page = find_lock_entry(mapping, index);
Hugh Dickins54af60422011-08-03 16:21:24 -07001637 if (radix_tree_exceptional_entry(page)) {
1638 swap = radix_to_swp_entry(page);
1639 page = NULL;
1640 }
1641
Hugh Dickins75edd342016-05-19 17:12:44 -07001642 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001643 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001644 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001645 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001646 }
1647
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001648 if (page && sgp == SGP_WRITE)
1649 mark_page_accessed(page);
1650
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001651 /* fallocated page? */
1652 if (page && !PageUptodate(page)) {
1653 if (sgp != SGP_READ)
1654 goto clear;
1655 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001656 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001657 page = NULL;
1658 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001659 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001660 *pagep = page;
1661 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001665 * Fast cache lookup did not find it:
1666 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001668 sbinfo = SHMEM_SB(inode->i_sb);
Mike Rapoportcfda0522017-02-22 15:43:37 -08001669 charge_mm = vma ? vma->vm_mm : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 if (swap.val) {
1672 /* Look it up and read it in.. */
Huang Yingec560172017-09-06 16:24:36 -07001673 page = lookup_swap_cache(swap, NULL, 0);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001674 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001675 /* Or update major stats only when swapin succeeds?? */
1676 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001677 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001678 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001679 count_memcg_event_mm(charge_mm, PGMAJFAULT);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001680 }
1681 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001682 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001683 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001684 error = -ENOMEM;
1685 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 }
1688
1689 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001690 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001691 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001692 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001693 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001694 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001695 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001696 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001698 goto failed;
1699 }
1700 wait_on_page_writeback(page);
1701
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001702 if (shmem_should_replace_page(page, gfp)) {
1703 error = shmem_replace_page(&page, gfp, info, index);
1704 if (error)
1705 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001708 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001709 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001710 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001711 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001712 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001713 /*
1714 * We already confirmed swap under page lock, and make
1715 * no memory allocation here, so usually no possibility
1716 * of error; but free_swap_and_cache() only trylocks a
1717 * page, so it is just possible that the entry has been
1718 * truncated or holepunched since swap was confirmed.
1719 * shmem_undo_range() will have done some of the
1720 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001721 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001722 * Reset swap.val? No, leave it so "failed" goes back to
1723 * "repeat": reading a hole and writing should succeed.
1724 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001725 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001726 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001727 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001728 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001729 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001730 if (error)
1731 goto failed;
1732
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001733 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001734
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001735 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001736 info->swapped--;
1737 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001738 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001739
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001740 if (sgp == SGP_WRITE)
1741 mark_page_accessed(page);
1742
Hugh Dickins27ab7002011-07-25 17:12:36 -07001743 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001744 set_page_dirty(page);
1745 swap_free(swap);
1746
Hugh Dickins54af60422011-08-03 16:21:24 -07001747 } else {
Mike Rapoportcfda0522017-02-22 15:43:37 -08001748 if (vma && userfaultfd_missing(vma)) {
1749 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1750 return 0;
1751 }
1752
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001753 /* shmem_symlink() */
1754 if (mapping->a_ops != &shmem_aops)
1755 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001756 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001757 goto alloc_nohuge;
1758 if (shmem_huge == SHMEM_HUGE_FORCE)
1759 goto alloc_huge;
1760 switch (sbinfo->huge) {
1761 loff_t i_size;
1762 pgoff_t off;
1763 case SHMEM_HUGE_NEVER:
1764 goto alloc_nohuge;
1765 case SHMEM_HUGE_WITHIN_SIZE:
1766 off = round_up(index, HPAGE_PMD_NR);
1767 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1768 if (i_size >= HPAGE_PMD_SIZE &&
1769 i_size >> PAGE_SHIFT >= off)
1770 goto alloc_huge;
1771 /* fallthrough */
1772 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001773 if (sgp_huge == SGP_HUGE)
1774 goto alloc_huge;
1775 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001776 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001779alloc_huge:
Mike Rapoport0f079692017-09-06 16:22:59 -07001780 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001781 if (IS_ERR(page)) {
Mike Rapoport0f079692017-09-06 16:22:59 -07001782alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001783 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001784 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001785 if (IS_ERR(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001786 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001787 error = PTR_ERR(page);
1788 page = NULL;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001789 if (error != -ENOSPC)
1790 goto failed;
1791 /*
1792 * Try to reclaim some spece by splitting a huge page
1793 * beyond i_size on the filesystem.
1794 */
1795 while (retry--) {
1796 int ret;
1797 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1798 if (ret == SHRINK_STOP)
1799 break;
1800 if (ret)
1801 goto alloc_nohuge;
1802 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001803 goto failed;
1804 }
1805
1806 if (PageTransHuge(page))
1807 hindex = round_down(index, HPAGE_PMD_NR);
1808 else
1809 hindex = index;
1810
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001811 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001812 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001813
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001814 error = mem_cgroup_try_charge(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001815 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001816 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001817 goto unacct;
1818 error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1819 compound_order(page));
Hugh Dickinsb065b432012-07-11 14:02:48 -07001820 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001821 error = shmem_add_to_page_cache(page, mapping, hindex,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001822 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001823 radix_tree_preload_end();
1824 }
1825 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001826 mem_cgroup_cancel_charge(page, memcg,
1827 PageTransHuge(page));
1828 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001829 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001830 mem_cgroup_commit_charge(page, memcg, false,
1831 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001832 lru_cache_add_anon(page);
1833
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001834 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001835 info->alloced += 1 << compound_order(page);
1836 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001837 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001838 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001839 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001840
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001841 if (PageTransHuge(page) &&
1842 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1843 hindex + HPAGE_PMD_NR - 1) {
1844 /*
1845 * Part of the huge page is beyond i_size: subject
1846 * to shrink under memory pressure.
1847 */
1848 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001849 /*
1850 * _careful to defend against unlocked access to
1851 * ->shrink_list in shmem_unused_huge_shrink()
1852 */
1853 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001854 list_add_tail(&info->shrinklist,
1855 &sbinfo->shrinklist);
1856 sbinfo->shrinklist_len++;
1857 }
1858 spin_unlock(&sbinfo->shrinklist_lock);
1859 }
1860
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001861 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001862 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1863 */
1864 if (sgp == SGP_FALLOC)
1865 sgp = SGP_WRITE;
1866clear:
1867 /*
1868 * Let SGP_WRITE caller clear ends if write does not fill page;
1869 * but SGP_FALLOC on a page fallocated earlier must initialize
1870 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001871 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001872 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1873 struct page *head = compound_head(page);
1874 int i;
1875
1876 for (i = 0; i < (1 << compound_order(head)); i++) {
1877 clear_highpage(head + i);
1878 flush_dcache_page(head + i);
1879 }
1880 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001881 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001882 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001883
Hugh Dickins54af60422011-08-03 16:21:24 -07001884 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001885 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001886 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001887 if (alloced) {
1888 ClearPageDirty(page);
1889 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001890 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001891 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001892 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001893 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001894 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001895 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001896 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001897 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001898 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001899
Nick Piggind0217ac2007-07-19 01:47:03 -07001900 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001901 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001903unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001904 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001905
1906 if (PageTransHuge(page)) {
1907 unlock_page(page);
1908 put_page(page);
1909 goto alloc_nohuge;
1910 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001911failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001912 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001913 error = -EEXIST;
1914unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001915 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001916 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001917 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001918 }
1919 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001920 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001921 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001922 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001923 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001924 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001925 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001926 goto repeat;
1927 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001930/*
1931 * This is like autoremove_wake_function, but it removes the wait queue
1932 * entry unconditionally - even if something else had already woken the
1933 * target.
1934 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001935static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001936{
1937 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001938 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001939 return ret;
1940}
1941
Dave Jiang11bac802017-02-24 14:56:41 -08001942static int shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
Dave Jiang11bac802017-02-24 14:56:41 -08001944 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001945 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001946 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001947 enum sgp_type sgp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 int error;
Hugh Dickins68da9f02011-07-25 17:12:34 -07001949 int ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001951 /*
1952 * Trinity finds that probing a hole which tmpfs is punching can
1953 * prevent the hole-punch from ever completing: which in turn
1954 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001955 * faulting pages into the hole while it's being punched. Although
1956 * shmem_undo_range() does remove the additions, it may be unable to
1957 * keep up, as each new page needs its own unmap_mapping_range() call,
1958 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1959 *
1960 * It does not matter if we sometimes reach this check just before the
1961 * hole-punch begins, so that one fault then races with the punch:
1962 * we just need to make racing faults a rare case.
1963 *
1964 * The implementation below would be much simpler if we just used a
1965 * standard mutex or completion: but we cannot take i_mutex in fault,
1966 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001967 */
1968 if (unlikely(inode->i_private)) {
1969 struct shmem_falloc *shmem_falloc;
1970
1971 spin_lock(&inode->i_lock);
1972 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001973 if (shmem_falloc &&
1974 shmem_falloc->waitq &&
1975 vmf->pgoff >= shmem_falloc->start &&
1976 vmf->pgoff < shmem_falloc->next) {
1977 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001978 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001979
1980 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001981 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1982 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001983 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001984 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001985 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001986 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001987
1988 shmem_falloc_waitq = shmem_falloc->waitq;
1989 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1990 TASK_UNINTERRUPTIBLE);
1991 spin_unlock(&inode->i_lock);
1992 schedule();
1993
1994 /*
1995 * shmem_falloc_waitq points into the shmem_fallocate()
1996 * stack of the hole-punching task: shmem_falloc_waitq
1997 * is usually invalid by the time we reach here, but
1998 * finish_wait() does not dereference it in that case;
1999 * though i_lock needed lest racing with wake_up_all().
2000 */
2001 spin_lock(&inode->i_lock);
2002 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2003 spin_unlock(&inode->i_lock);
2004 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002005 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002006 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002007 }
2008
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002009 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002010
2011 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2012 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002013 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002014 else if (vma->vm_flags & VM_HUGEPAGE)
2015 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002016
2017 error = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002018 gfp, vma, vmf, &ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (error)
2020 return ((error == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002021 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022}
2023
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002024unsigned long shmem_get_unmapped_area(struct file *file,
2025 unsigned long uaddr, unsigned long len,
2026 unsigned long pgoff, unsigned long flags)
2027{
2028 unsigned long (*get_area)(struct file *,
2029 unsigned long, unsigned long, unsigned long, unsigned long);
2030 unsigned long addr;
2031 unsigned long offset;
2032 unsigned long inflated_len;
2033 unsigned long inflated_addr;
2034 unsigned long inflated_offset;
2035
2036 if (len > TASK_SIZE)
2037 return -ENOMEM;
2038
2039 get_area = current->mm->get_unmapped_area;
2040 addr = get_area(file, uaddr, len, pgoff, flags);
2041
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002042 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002043 return addr;
2044 if (IS_ERR_VALUE(addr))
2045 return addr;
2046 if (addr & ~PAGE_MASK)
2047 return addr;
2048 if (addr > TASK_SIZE - len)
2049 return addr;
2050
2051 if (shmem_huge == SHMEM_HUGE_DENY)
2052 return addr;
2053 if (len < HPAGE_PMD_SIZE)
2054 return addr;
2055 if (flags & MAP_FIXED)
2056 return addr;
2057 /*
2058 * Our priority is to support MAP_SHARED mapped hugely;
2059 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
Kirill A. Shutemovccf53a82020-01-13 16:29:13 -08002060 * But if caller specified an address hint and we allocated area there
2061 * successfully, respect that as before.
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002062 */
Kirill A. Shutemovccf53a82020-01-13 16:29:13 -08002063 if (uaddr == addr)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002064 return addr;
2065
2066 if (shmem_huge != SHMEM_HUGE_FORCE) {
2067 struct super_block *sb;
2068
2069 if (file) {
2070 VM_BUG_ON(file->f_op != &shmem_file_operations);
2071 sb = file_inode(file)->i_sb;
2072 } else {
2073 /*
2074 * Called directly from mm/mmap.c, or drivers/char/mem.c
2075 * for "/dev/zero", to create a shared anonymous object.
2076 */
2077 if (IS_ERR(shm_mnt))
2078 return addr;
2079 sb = shm_mnt->mnt_sb;
2080 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002081 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002082 return addr;
2083 }
2084
2085 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2086 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2087 return addr;
2088 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2089 return addr;
2090
2091 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2092 if (inflated_len > TASK_SIZE)
2093 return addr;
2094 if (inflated_len < len)
2095 return addr;
2096
Kirill A. Shutemovccf53a82020-01-13 16:29:13 -08002097 inflated_addr = get_area(NULL, uaddr, inflated_len, 0, flags);
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002098 if (IS_ERR_VALUE(inflated_addr))
2099 return addr;
2100 if (inflated_addr & ~PAGE_MASK)
2101 return addr;
2102
2103 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2104 inflated_addr += offset - inflated_offset;
2105 if (inflated_offset > offset)
2106 inflated_addr += HPAGE_PMD_SIZE;
2107
2108 if (inflated_addr > TASK_SIZE - len)
2109 return addr;
2110 return inflated_addr;
2111}
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002114static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115{
Al Viro496ad9a2013-01-23 17:07:38 -05002116 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002117 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002120static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2121 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122{
Al Viro496ad9a2013-01-23 17:07:38 -05002123 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002124 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002126 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2127 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129#endif
2130
2131int shmem_lock(struct file *file, int lock, struct user_struct *user)
2132{
Al Viro496ad9a2013-01-23 17:07:38 -05002133 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct shmem_inode_info *info = SHMEM_I(inode);
2135 int retval = -ENOMEM;
2136
Hugh Dickins5bb1c0f2020-04-20 18:14:14 -07002137 /*
2138 * What serializes the accesses to info->flags?
2139 * ipc_lock_object() when called from shmctl_do_lock(),
2140 * no serialization needed when called from shm_destroy().
2141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 if (lock && !(info->flags & VM_LOCKED)) {
2143 if (!user_shm_lock(inode->i_size, user))
2144 goto out_nomem;
2145 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002146 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 if (!lock && (info->flags & VM_LOCKED) && user) {
2149 user_shm_unlock(inode->i_size, user);
2150 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002151 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155out_nomem:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return retval;
2157}
2158
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002159static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
2161 file_accessed(file);
2162 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002163 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002164 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2165 (vma->vm_end & HPAGE_PMD_MASK)) {
2166 khugepaged_enter(vma, vma->vm_flags);
2167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return 0;
2169}
2170
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002171static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002172 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct inode *inode;
2175 struct shmem_inode_info *info;
2176 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2177
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002178 if (shmem_reserve_inode(sb))
2179 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
2181 inode = new_inode(sb);
2182 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002183 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002184 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002186 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07002187 inode->i_generation = get_seconds();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 info = SHMEM_I(inode);
2189 memset(info, 0, (char *)inode - (char *)info);
2190 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002191 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002192 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002193 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002195 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002196 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 switch (mode & S_IFMT) {
2199 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002200 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 init_special_inode(inode, mode, dev);
2202 break;
2203 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002204 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 inode->i_op = &shmem_inode_operations;
2206 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002207 mpol_shared_policy_init(&info->policy,
2208 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 break;
2210 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002211 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /* Some things misbehave if size == 0 on a directory */
2213 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2214 inode->i_op = &shmem_dir_inode_operations;
2215 inode->i_fop = &simple_dir_operations;
2216 break;
2217 case S_IFLNK:
2218 /*
2219 * Must not load anything in the rbtree,
2220 * mpol_free_shared_policy will not be called.
2221 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002222 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 }
Joel Fernandes (Google)6447b342018-09-20 12:22:39 -07002225
2226 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002227 } else
2228 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return inode;
2230}
2231
Johannes Weiner0cd61442014-04-03 14:47:46 -07002232bool shmem_mapping(struct address_space *mapping)
2233{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002234 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002235}
2236
Mike Rapoport8d103962017-09-06 16:23:02 -07002237static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2238 pmd_t *dst_pmd,
2239 struct vm_area_struct *dst_vma,
2240 unsigned long dst_addr,
2241 unsigned long src_addr,
2242 bool zeropage,
2243 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002244{
2245 struct inode *inode = file_inode(dst_vma->vm_file);
2246 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002247 struct address_space *mapping = inode->i_mapping;
2248 gfp_t gfp = mapping_gfp_mask(mapping);
2249 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2250 struct mem_cgroup *memcg;
2251 spinlock_t *ptl;
2252 void *page_kaddr;
2253 struct page *page;
2254 pte_t _dst_pte, *dst_pte;
2255 int ret;
Andrea Arcangeliaf3edb32018-11-30 14:09:37 -08002256 pgoff_t offset, max_off;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002257
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002258 ret = -ENOMEM;
Axel Rasmussen31911622021-05-14 17:27:19 -07002259 if (!shmem_inode_acct_block(inode, 1)) {
2260 /*
2261 * We may have got a page, returned -ENOENT triggering a retry,
2262 * and now we find ourselves with -ENOMEM. Release the page, to
2263 * avoid a BUG_ON in our caller.
2264 */
2265 if (unlikely(*pagep)) {
2266 put_page(*pagep);
2267 *pagep = NULL;
2268 }
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002269 goto out;
Axel Rasmussen31911622021-05-14 17:27:19 -07002270 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002271
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002272 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002273 page = shmem_alloc_page(gfp, info, pgoff);
2274 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002275 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002276
Mike Rapoport8d103962017-09-06 16:23:02 -07002277 if (!zeropage) { /* mcopy_atomic */
2278 page_kaddr = kmap_atomic(page);
2279 ret = copy_from_user(page_kaddr,
2280 (const void __user *)src_addr,
2281 PAGE_SIZE);
2282 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002283
Mike Rapoport8d103962017-09-06 16:23:02 -07002284 /* fallback to copy_from_user outside mmap_sem */
2285 if (unlikely(ret)) {
2286 *pagep = page;
2287 shmem_inode_unacct_blocks(inode, 1);
2288 /* don't free the page */
Andrea Arcangeli82c5a8c2018-11-30 14:09:25 -08002289 return -ENOENT;
Mike Rapoport8d103962017-09-06 16:23:02 -07002290 }
2291 } else { /* mfill_zeropage_atomic */
2292 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002293 }
2294 } else {
2295 page = *pagep;
2296 *pagep = NULL;
2297 }
2298
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002299 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2300 __SetPageLocked(page);
2301 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002302 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002303
Andrea Arcangeliaf3edb32018-11-30 14:09:37 -08002304 ret = -EFAULT;
2305 offset = linear_page_index(dst_vma, dst_addr);
2306 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2307 if (unlikely(offset >= max_off))
2308 goto out_release;
2309
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002310 ret = mem_cgroup_try_charge(page, dst_mm, gfp, &memcg, false);
2311 if (ret)
2312 goto out_release;
2313
2314 ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
2315 if (!ret) {
2316 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL);
2317 radix_tree_preload_end();
2318 }
2319 if (ret)
2320 goto out_release_uncharge;
2321
2322 mem_cgroup_commit_charge(page, memcg, false, false);
2323
2324 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2325 if (dst_vma->vm_flags & VM_WRITE)
2326 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
Andrea Arcangeli46466e22018-11-30 14:09:43 -08002327 else {
2328 /*
2329 * We don't set the pte dirty if the vma has no
2330 * VM_WRITE permission, so mark the page dirty or it
2331 * could be freed from under us. We could do it
2332 * unconditionally before unlock_page(), but doing it
2333 * only if VM_WRITE is not set is faster.
2334 */
2335 set_page_dirty(page);
2336 }
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002337
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002338 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
Andrea Arcangeliaf3edb32018-11-30 14:09:37 -08002339
2340 ret = -EFAULT;
2341 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
2342 if (unlikely(offset >= max_off))
2343 goto out_release_uncharge_unlock;
2344
2345 ret = -EEXIST;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002346 if (!pte_none(*dst_pte))
2347 goto out_release_uncharge_unlock;
2348
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002349 lru_cache_add_anon(page);
2350
Yang Shibc0a3592020-04-20 18:14:17 -07002351 spin_lock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002352 info->alloced++;
2353 inode->i_blocks += BLOCKS_PER_PAGE;
2354 shmem_recalc_inode(inode);
Yang Shibc0a3592020-04-20 18:14:17 -07002355 spin_unlock_irq(&info->lock);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002356
2357 inc_mm_counter(dst_mm, mm_counter_file(page));
2358 page_add_file_rmap(page, false);
2359 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2360
2361 /* No need to invalidate - it was non-present before */
2362 update_mmu_cache(dst_vma, dst_addr, dst_pte);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002363 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeliaf3edb32018-11-30 14:09:37 -08002364 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002365 ret = 0;
2366out:
2367 return ret;
2368out_release_uncharge_unlock:
2369 pte_unmap_unlock(dst_pte, ptl);
Andrea Arcangeli46466e22018-11-30 14:09:43 -08002370 ClearPageDirty(page);
Andrea Arcangeliaf3edb32018-11-30 14:09:37 -08002371 delete_from_page_cache(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002372out_release_uncharge:
2373 mem_cgroup_cancel_charge(page, memcg, false);
2374out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002375 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002376 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002377out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002378 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002379 goto out;
2380}
2381
Mike Rapoport8d103962017-09-06 16:23:02 -07002382int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2383 pmd_t *dst_pmd,
2384 struct vm_area_struct *dst_vma,
2385 unsigned long dst_addr,
2386 unsigned long src_addr,
2387 struct page **pagep)
2388{
2389 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2390 dst_addr, src_addr, false, pagep);
2391}
2392
2393int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2394 pmd_t *dst_pmd,
2395 struct vm_area_struct *dst_vma,
2396 unsigned long dst_addr)
2397{
2398 struct page *page = NULL;
2399
2400 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2401 dst_addr, 0, true, &page);
2402}
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002405static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002406static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002408#ifdef CONFIG_TMPFS_XATTR
2409static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2410#else
2411#define shmem_initxattrs NULL
2412#endif
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002415shmem_write_begin(struct file *file, struct address_space *mapping,
2416 loff_t pos, unsigned len, unsigned flags,
2417 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418{
Nick Piggin800d15a2007-10-16 01:25:03 -07002419 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002420 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002421 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002422
2423 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002424 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002425 if (info->seals & F_SEAL_WRITE)
2426 return -EPERM;
2427 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2428 return -EPERM;
2429 }
2430
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002431 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002432}
2433
2434static int
2435shmem_write_end(struct file *file, struct address_space *mapping,
2436 loff_t pos, unsigned len, unsigned copied,
2437 struct page *page, void *fsdata)
2438{
2439 struct inode *inode = mapping->host;
2440
Hugh Dickinsd3602442008-02-04 22:28:44 -08002441 if (pos + copied > inode->i_size)
2442 i_size_write(inode, pos + copied);
2443
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002444 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002445 struct page *head = compound_head(page);
2446 if (PageTransCompound(page)) {
2447 int i;
2448
2449 for (i = 0; i < HPAGE_PMD_NR; i++) {
2450 if (head + i == page)
2451 continue;
2452 clear_highpage(head + i);
2453 flush_dcache_page(head + i);
2454 }
2455 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002456 if (copied < PAGE_SIZE) {
2457 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002458 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002459 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002460 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002461 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002462 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002463 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002464 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002465 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002466
Nick Piggin800d15a2007-10-16 01:25:03 -07002467 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Al Viro2ba5bbe2014-04-02 20:00:02 -04002470static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Al Viro6e58e792014-02-03 17:07:03 -05002472 struct file *file = iocb->ki_filp;
2473 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002475 pgoff_t index;
2476 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002477 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002478 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002479 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002480 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002481
2482 /*
2483 * Might this read be for a stacking filesystem? Then when reading
2484 * holes of a sparse file, we actually need to allocate those pages,
2485 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2486 */
Al Viro777eda22014-12-17 04:46:46 -05002487 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002488 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002490 index = *ppos >> PAGE_SHIFT;
2491 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 for (;;) {
2494 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002495 pgoff_t end_index;
2496 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 loff_t i_size = i_size_read(inode);
2498
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002499 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 if (index > end_index)
2501 break;
2502 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002503 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (nr <= offset)
2505 break;
2506 }
2507
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002508 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002509 if (error) {
2510 if (error == -EINVAL)
2511 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 break;
2513 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002514 if (page) {
2515 if (sgp == SGP_CACHE)
2516 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002517 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 /*
2521 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002522 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002524 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002526 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002528 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (nr <= offset) {
2530 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002531 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 break;
2533 }
2534 }
2535 nr -= offset;
2536
2537 if (page) {
2538 /*
2539 * If users can be writing to this page using arbitrary
2540 * virtual addresses, take care about potential aliasing
2541 * before reading the page on the kernel side.
2542 */
2543 if (mapping_writably_mapped(mapping))
2544 flush_dcache_page(page);
2545 /*
2546 * Mark the page accessed if we read the beginning.
2547 */
2548 if (!offset)
2549 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002550 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002552 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /*
2556 * Ok, we have the page, and it's up-to-date, so
2557 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002559 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002560 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002562 index += offset >> PAGE_SHIFT;
2563 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002565 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002566 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 break;
Al Viro6e58e792014-02-03 17:07:03 -05002568 if (ret < nr) {
2569 error = -EFAULT;
2570 break;
2571 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 cond_resched();
2573 }
2574
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002575 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002576 file_accessed(file);
2577 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002580/*
2581 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2582 */
2583static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002584 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002585{
2586 struct page *page;
2587 struct pagevec pvec;
2588 pgoff_t indices[PAGEVEC_SIZE];
2589 bool done = false;
2590 int i;
2591
2592 pagevec_init(&pvec, 0);
2593 pvec.nr = 1; /* start small: we may be there already */
2594 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002595 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002596 pvec.nr, pvec.pages, indices);
2597 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002598 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002599 index = end;
2600 break;
2601 }
2602 for (i = 0; i < pvec.nr; i++, index++) {
2603 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002604 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002605 done = true;
2606 break;
2607 }
2608 index = indices[i];
2609 }
2610 page = pvec.pages[i];
2611 if (page && !radix_tree_exceptional_entry(page)) {
2612 if (!PageUptodate(page))
2613 page = NULL;
2614 }
2615 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002616 (page && whence == SEEK_DATA) ||
2617 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002618 done = true;
2619 break;
2620 }
2621 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002622 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002623 pagevec_release(&pvec);
2624 pvec.nr = PAGEVEC_SIZE;
2625 cond_resched();
2626 }
2627 return index;
2628}
2629
Andrew Morton965c8e52012-12-17 15:59:39 -08002630static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002631{
2632 struct address_space *mapping = file->f_mapping;
2633 struct inode *inode = mapping->host;
2634 pgoff_t start, end;
2635 loff_t new_offset;
2636
Andrew Morton965c8e52012-12-17 15:59:39 -08002637 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2638 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002639 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002640 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002641 /* We're holding i_mutex so we can access i_size directly */
2642
Yufen Yua84c8722018-11-16 15:08:39 -08002643 if (offset < 0 || offset >= inode->i_size)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002644 offset = -ENXIO;
2645 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002646 start = offset >> PAGE_SHIFT;
2647 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002648 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002649 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002650 if (new_offset > offset) {
2651 if (new_offset < inode->i_size)
2652 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002653 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002654 offset = -ENXIO;
2655 else
2656 offset = inode->i_size;
2657 }
2658 }
2659
Hugh Dickins387aae62013-08-04 11:30:25 -07002660 if (offset >= 0)
2661 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002662 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002663 return offset;
2664}
2665
David Herrmann05f65b52014-08-08 14:25:36 -07002666/*
2667 * We need a tag: a new tag would expand every radix_tree_node by 8 bytes,
2668 * so reuse a tag which we firmly believe is never set or cleared on shmem.
2669 */
2670#define SHMEM_TAG_PINNED PAGECACHE_TAG_TOWRITE
2671#define LAST_SCAN 4 /* about 150ms max */
2672
2673static void shmem_tag_pins(struct address_space *mapping)
2674{
2675 struct radix_tree_iter iter;
2676 void **slot;
2677 pgoff_t start;
2678 struct page *page;
Matthew Wilcox (Oracle)391d4ee2019-10-25 09:58:35 -07002679 unsigned int tagged = 0;
David Herrmann05f65b52014-08-08 14:25:36 -07002680
2681 lru_add_drain();
2682 start = 0;
David Herrmann05f65b52014-08-08 14:25:36 -07002683
Matthew Wilcox (Oracle)391d4ee2019-10-25 09:58:35 -07002684 spin_lock_irq(&mapping->tree_lock);
David Herrmann05f65b52014-08-08 14:25:36 -07002685 radix_tree_for_each_slot(slot, &mapping->page_tree, &iter, start) {
zhong jiang988f7012019-11-18 11:26:07 +08002686 page = radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
David Herrmann05f65b52014-08-08 14:25:36 -07002687 if (!page || radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002688 if (radix_tree_deref_retry(page)) {
2689 slot = radix_tree_iter_retry(&iter);
2690 continue;
2691 }
Hugh Dickinsda0acf92022-03-04 20:29:01 -08002692 } else if (!PageTail(page) && page_count(page) !=
2693 hpage_nr_pages(page) + total_mapcount(page)) {
David Herrmann05f65b52014-08-08 14:25:36 -07002694 radix_tree_tag_set(&mapping->page_tree, iter.index,
2695 SHMEM_TAG_PINNED);
David Herrmann05f65b52014-08-08 14:25:36 -07002696 }
2697
Matthew Wilcox (Oracle)391d4ee2019-10-25 09:58:35 -07002698 if (++tagged % 1024)
2699 continue;
2700
2701 slot = radix_tree_iter_resume(slot, &iter);
2702 spin_unlock_irq(&mapping->tree_lock);
2703 cond_resched();
2704 spin_lock_irq(&mapping->tree_lock);
David Herrmann05f65b52014-08-08 14:25:36 -07002705 }
Matthew Wilcox (Oracle)391d4ee2019-10-25 09:58:35 -07002706 spin_unlock_irq(&mapping->tree_lock);
David Herrmann05f65b52014-08-08 14:25:36 -07002707}
2708
2709/*
2710 * Setting SEAL_WRITE requires us to verify there's no pending writer. However,
2711 * via get_user_pages(), drivers might have some pending I/O without any active
2712 * user-space mappings (eg., direct-IO, AIO). Therefore, we look at all pages
2713 * and see whether it has an elevated ref-count. If so, we tag them and wait for
2714 * them to be dropped.
2715 * The caller must guarantee that no new user will acquire writable references
2716 * to those pages to avoid races.
2717 */
David Herrmann40e041a2014-08-08 14:25:27 -07002718static int shmem_wait_for_pins(struct address_space *mapping)
2719{
David Herrmann05f65b52014-08-08 14:25:36 -07002720 struct radix_tree_iter iter;
2721 void **slot;
2722 pgoff_t start;
2723 struct page *page;
2724 int error, scan;
2725
2726 shmem_tag_pins(mapping);
2727
2728 error = 0;
2729 for (scan = 0; scan <= LAST_SCAN; scan++) {
2730 if (!radix_tree_tagged(&mapping->page_tree, SHMEM_TAG_PINNED))
2731 break;
2732
2733 if (!scan)
2734 lru_add_drain_all();
2735 else if (schedule_timeout_killable((HZ << scan) / 200))
2736 scan = LAST_SCAN;
2737
2738 start = 0;
2739 rcu_read_lock();
David Herrmann05f65b52014-08-08 14:25:36 -07002740 radix_tree_for_each_tagged(slot, &mapping->page_tree, &iter,
2741 start, SHMEM_TAG_PINNED) {
2742
2743 page = radix_tree_deref_slot(slot);
2744 if (radix_tree_exception(page)) {
Matthew Wilcox2cf938a2016-03-17 14:22:03 -07002745 if (radix_tree_deref_retry(page)) {
2746 slot = radix_tree_iter_retry(&iter);
2747 continue;
2748 }
David Herrmann05f65b52014-08-08 14:25:36 -07002749
2750 page = NULL;
2751 }
2752
Hugh Dickinsda0acf92022-03-04 20:29:01 -08002753 if (page && page_count(page) !=
2754 hpage_nr_pages(page) + total_mapcount(page)) {
David Herrmann05f65b52014-08-08 14:25:36 -07002755 if (scan < LAST_SCAN)
2756 goto continue_resched;
2757
2758 /*
2759 * On the last scan, we clean up all those tags
2760 * we inserted; but make a note that we still
2761 * found pages pinned.
2762 */
2763 error = -EBUSY;
2764 }
2765
2766 spin_lock_irq(&mapping->tree_lock);
2767 radix_tree_tag_clear(&mapping->page_tree,
2768 iter.index, SHMEM_TAG_PINNED);
2769 spin_unlock_irq(&mapping->tree_lock);
2770continue_resched:
2771 if (need_resched()) {
Matthew Wilcox148deab2016-12-14 15:08:49 -08002772 slot = radix_tree_iter_resume(slot, &iter);
David Herrmann05f65b52014-08-08 14:25:36 -07002773 cond_resched_rcu();
David Herrmann05f65b52014-08-08 14:25:36 -07002774 }
2775 }
2776 rcu_read_unlock();
2777 }
2778
2779 return error;
David Herrmann40e041a2014-08-08 14:25:27 -07002780}
2781
2782#define F_ALL_SEALS (F_SEAL_SEAL | \
2783 F_SEAL_SHRINK | \
2784 F_SEAL_GROW | \
2785 F_SEAL_WRITE)
2786
2787int shmem_add_seals(struct file *file, unsigned int seals)
2788{
2789 struct inode *inode = file_inode(file);
2790 struct shmem_inode_info *info = SHMEM_I(inode);
2791 int error;
2792
2793 /*
2794 * SEALING
2795 * Sealing allows multiple parties to share a shmem-file but restrict
2796 * access to a specific subset of file operations. Seals can only be
2797 * added, but never removed. This way, mutually untrusted parties can
2798 * share common memory regions with a well-defined policy. A malicious
2799 * peer can thus never perform unwanted operations on a shared object.
2800 *
2801 * Seals are only supported on special shmem-files and always affect
2802 * the whole underlying inode. Once a seal is set, it may prevent some
2803 * kinds of access to the file. Currently, the following seals are
2804 * defined:
2805 * SEAL_SEAL: Prevent further seals from being set on this file
2806 * SEAL_SHRINK: Prevent the file from shrinking
2807 * SEAL_GROW: Prevent the file from growing
2808 * SEAL_WRITE: Prevent write access to the file
2809 *
2810 * As we don't require any trust relationship between two parties, we
2811 * must prevent seals from being removed. Therefore, sealing a file
2812 * only adds a given set of seals to the file, it never touches
2813 * existing seals. Furthermore, the "setting seals"-operation can be
2814 * sealed itself, which basically prevents any further seal from being
2815 * added.
2816 *
2817 * Semantics of sealing are only defined on volatile files. Only
2818 * anonymous shmem files support sealing. More importantly, seals are
2819 * never written to disk. Therefore, there's no plan to support it on
2820 * other file types.
2821 */
2822
2823 if (file->f_op != &shmem_file_operations)
2824 return -EINVAL;
2825 if (!(file->f_mode & FMODE_WRITE))
2826 return -EPERM;
2827 if (seals & ~(unsigned int)F_ALL_SEALS)
2828 return -EINVAL;
2829
Al Viro59551022016-01-22 15:40:57 -05002830 inode_lock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002831
2832 if (info->seals & F_SEAL_SEAL) {
2833 error = -EPERM;
2834 goto unlock;
2835 }
2836
2837 if ((seals & F_SEAL_WRITE) && !(info->seals & F_SEAL_WRITE)) {
2838 error = mapping_deny_writable(file->f_mapping);
2839 if (error)
2840 goto unlock;
2841
2842 error = shmem_wait_for_pins(file->f_mapping);
2843 if (error) {
2844 mapping_allow_writable(file->f_mapping);
2845 goto unlock;
2846 }
2847 }
2848
2849 info->seals |= seals;
2850 error = 0;
2851
2852unlock:
Al Viro59551022016-01-22 15:40:57 -05002853 inode_unlock(inode);
David Herrmann40e041a2014-08-08 14:25:27 -07002854 return error;
2855}
2856EXPORT_SYMBOL_GPL(shmem_add_seals);
2857
2858int shmem_get_seals(struct file *file)
2859{
2860 if (file->f_op != &shmem_file_operations)
2861 return -EINVAL;
2862
2863 return SHMEM_I(file_inode(file))->seals;
2864}
2865EXPORT_SYMBOL_GPL(shmem_get_seals);
2866
2867long shmem_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
2868{
2869 long error;
2870
2871 switch (cmd) {
2872 case F_ADD_SEALS:
2873 /* disallow upper 32bit */
2874 if (arg > UINT_MAX)
2875 return -EINVAL;
2876
2877 error = shmem_add_seals(file, arg);
2878 break;
2879 case F_GET_SEALS:
2880 error = shmem_get_seals(file);
2881 break;
2882 default:
2883 error = -EINVAL;
2884 break;
2885 }
2886
2887 return error;
2888}
2889
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002890static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2891 loff_t len)
2892{
Al Viro496ad9a2013-01-23 17:07:38 -05002893 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002894 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002895 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002896 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002897 pgoff_t start, index, end;
2898 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002899
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002900 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2901 return -EOPNOTSUPP;
2902
Al Viro59551022016-01-22 15:40:57 -05002903 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002904
2905 if (mode & FALLOC_FL_PUNCH_HOLE) {
2906 struct address_space *mapping = file->f_mapping;
2907 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2908 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002909 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002910
David Herrmann40e041a2014-08-08 14:25:27 -07002911 /* protected by i_mutex */
2912 if (info->seals & F_SEAL_WRITE) {
2913 error = -EPERM;
2914 goto out;
2915 }
2916
Hugh Dickins8e205f72014-07-23 14:00:10 -07002917 shmem_falloc.waitq = &shmem_falloc_waitq;
Chen Junf49aa242019-11-30 17:58:11 -08002918 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002919 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2920 spin_lock(&inode->i_lock);
2921 inode->i_private = &shmem_falloc;
2922 spin_unlock(&inode->i_lock);
2923
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002924 if ((u64)unmap_end > (u64)unmap_start)
2925 unmap_mapping_range(mapping, unmap_start,
2926 1 + unmap_end - unmap_start, 0);
2927 shmem_truncate_range(inode, offset, offset + len - 1);
2928 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002929
2930 spin_lock(&inode->i_lock);
2931 inode->i_private = NULL;
2932 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002933 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002934 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002935 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002936 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002937 }
2938
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002939 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2940 error = inode_newsize_ok(inode, offset + len);
2941 if (error)
2942 goto out;
2943
David Herrmann40e041a2014-08-08 14:25:27 -07002944 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2945 error = -EPERM;
2946 goto out;
2947 }
2948
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002949 start = offset >> PAGE_SHIFT;
2950 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002951 /* Try to avoid a swapstorm if len is impossible to satisfy */
2952 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2953 error = -ENOSPC;
2954 goto out;
2955 }
2956
Hugh Dickins8e205f72014-07-23 14:00:10 -07002957 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002958 shmem_falloc.start = start;
2959 shmem_falloc.next = start;
2960 shmem_falloc.nr_falloced = 0;
2961 shmem_falloc.nr_unswapped = 0;
2962 spin_lock(&inode->i_lock);
2963 inode->i_private = &shmem_falloc;
2964 spin_unlock(&inode->i_lock);
2965
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002966 for (index = start; index < end; index++) {
2967 struct page *page;
2968
2969 /*
2970 * Good, the fallocate(2) manpage permits EINTR: we may have
2971 * been interrupted because we are using up too much memory.
2972 */
2973 if (signal_pending(current))
2974 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002975 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2976 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002977 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002978 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002979 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002980 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002981 if (index > start) {
2982 shmem_undo_range(inode,
2983 (loff_t)start << PAGE_SHIFT,
2984 ((loff_t)index << PAGE_SHIFT) - 1, true);
2985 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002986 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002987 }
2988
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002989 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002990 * Inform shmem_writepage() how far we have reached.
2991 * No need for lock or barrier: we have the page lock.
2992 */
2993 shmem_falloc.next++;
2994 if (!PageUptodate(page))
2995 shmem_falloc.nr_falloced++;
2996
2997 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002998 * If !PageUptodate, leave it that way so that freeable pages
2999 * can be recognized if we need to rollback on error later.
3000 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07003001 * than free the pages we are allocating (and SGP_CACHE pages
3002 * might still be clean: we now need to mark those dirty too).
3003 */
3004 set_page_dirty(page);
3005 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003006 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07003007 cond_resched();
3008 }
3009
3010 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
3011 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07003012 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07003013undone:
3014 spin_lock(&inode->i_lock);
3015 inode->i_private = NULL;
3016 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07003017out:
Al Viro59551022016-01-22 15:40:57 -05003018 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003019 return error;
3020}
3021
David Howells726c3342006-06-23 02:02:58 -07003022static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023{
David Howells726c3342006-06-23 02:02:58 -07003024 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003027 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003029 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003031 buf->f_bavail =
3032 buf->f_bfree = sbinfo->max_blocks -
3033 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003034 }
3035 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 buf->f_files = sbinfo->max_inodes;
3037 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
3039 /* else leave those fields 0 like simple_statfs */
3040 return 0;
3041}
3042
3043/*
3044 * File creation. Allocate an inode, and we're done..
3045 */
3046static int
Al Viro1a67aaf2011-07-26 01:52:52 -04003047shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003049 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 int error = -ENOSPC;
3051
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003052 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003054 error = simple_acl_create(dir, inode);
3055 if (error)
3056 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05003057 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003058 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003059 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003060 if (error && error != -EOPNOTSUPP)
3061 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04003062
Al Viro718deb62009-12-16 19:35:36 -05003063 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003065 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 d_instantiate(dentry, inode);
3067 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
3069 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003070out_iput:
3071 iput(inode);
3072 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073}
3074
Al Viro60545d02013-06-07 01:20:27 -04003075static int
3076shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
3077{
3078 struct inode *inode;
3079 int error = -ENOSPC;
3080
3081 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
3082 if (inode) {
3083 error = security_inode_init_security(inode, dir,
3084 NULL,
3085 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003086 if (error && error != -EOPNOTSUPP)
3087 goto out_iput;
3088 error = simple_acl_create(dir, inode);
3089 if (error)
3090 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04003091 d_tmpfile(dentry, inode);
3092 }
3093 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003094out_iput:
3095 iput(inode);
3096 return error;
Al Viro60545d02013-06-07 01:20:27 -04003097}
3098
Al Viro18bb1db2011-07-26 01:41:39 -04003099static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
3101 int error;
3102
3103 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
3104 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07003105 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 return 0;
3107}
3108
Al Viro4acdaf22011-07-26 01:42:34 -04003109static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04003110 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111{
3112 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
3113}
3114
3115/*
3116 * Link a file..
3117 */
3118static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
3119{
David Howells75c3cfa2015-03-17 22:26:12 +00003120 struct inode *inode = d_inode(old_dentry);
Darrick J. Wong4cf6ad02019-02-22 22:35:32 -08003121 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122
3123 /*
3124 * No ordinary (disk based) filesystem counts links as inodes;
3125 * but each new link needs a new dentry, pinning lowmem, and
3126 * tmpfs dentries cannot be pruned until they are unlinked.
Darrick J. Wong00df9622019-02-21 08:48:09 -08003127 * But if an O_TMPFILE file is linked into the tmpfs, the
3128 * first link must skip that, to get the accounting right.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
Darrick J. Wong00df9622019-02-21 08:48:09 -08003130 if (inode->i_nlink) {
3131 ret = shmem_reserve_inode(inode->i_sb);
3132 if (ret)
3133 goto out;
3134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003137 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07003138 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04003139 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 dget(dentry); /* Extra pinning count for the created dentry */
3141 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08003142out:
3143 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
3146static int shmem_unlink(struct inode *dir, struct dentry *dentry)
3147{
David Howells75c3cfa2015-03-17 22:26:12 +00003148 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08003150 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
3151 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
3153 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003154 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003155 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 dput(dentry); /* Undo the count from "create" - this does all the work */
3157 return 0;
3158}
3159
3160static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
3161{
3162 if (!simple_empty(dentry))
3163 return -ENOTEMPTY;
3164
David Howells75c3cfa2015-03-17 22:26:12 +00003165 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003166 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return shmem_unlink(dir, dentry);
3168}
3169
Miklos Szeredi37456772014-07-23 15:15:34 +02003170static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
3171{
David Howellse36cb0b2015-01-29 12:02:35 +00003172 bool old_is_dir = d_is_dir(old_dentry);
3173 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02003174
3175 if (old_dir != new_dir && old_is_dir != new_is_dir) {
3176 if (old_is_dir) {
3177 drop_nlink(old_dir);
3178 inc_nlink(new_dir);
3179 } else {
3180 drop_nlink(new_dir);
3181 inc_nlink(old_dir);
3182 }
3183 }
3184 old_dir->i_ctime = old_dir->i_mtime =
3185 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00003186 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003187 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02003188
3189 return 0;
3190}
3191
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003192static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
3193{
3194 struct dentry *whiteout;
3195 int error;
3196
3197 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
3198 if (!whiteout)
3199 return -ENOMEM;
3200
3201 error = shmem_mknod(old_dir, whiteout,
3202 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
3203 dput(whiteout);
3204 if (error)
3205 return error;
3206
3207 /*
3208 * Cheat and hash the whiteout while the old dentry is still in
3209 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
3210 *
3211 * d_lookup() will consistently find one of them at this point,
3212 * not sure which one, but that isn't even important.
3213 */
3214 d_rehash(whiteout);
3215 return 0;
3216}
3217
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218/*
3219 * The VFS layer already does all the dentry stuff for rename,
3220 * we just have to decrement the usage count for the target if
3221 * it exists so that the VFS layer correctly free's it when it
3222 * gets overwritten.
3223 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003224static 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 -07003225{
David Howells75c3cfa2015-03-17 22:26:12 +00003226 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 int they_are_dirs = S_ISDIR(inode->i_mode);
3228
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003229 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02003230 return -EINVAL;
3231
Miklos Szeredi37456772014-07-23 15:15:34 +02003232 if (flags & RENAME_EXCHANGE)
3233 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
3234
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 if (!simple_empty(new_dentry))
3236 return -ENOTEMPTY;
3237
Miklos Szeredi46fdb792014-10-24 00:14:37 +02003238 if (flags & RENAME_WHITEOUT) {
3239 int error;
3240
3241 error = shmem_whiteout(old_dir, old_dentry);
3242 if (error)
3243 return error;
3244 }
3245
David Howells75c3cfa2015-03-17 22:26:12 +00003246 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02003248 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00003249 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003250 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02003251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07003253 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07003254 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
3256
3257 old_dir->i_size -= BOGO_DIRENT_SIZE;
3258 new_dir->i_size += BOGO_DIRENT_SIZE;
3259 old_dir->i_ctime = old_dir->i_mtime =
3260 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07003261 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return 0;
3263}
3264
3265static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3266{
3267 int error;
3268 int len;
3269 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003270 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 struct shmem_inode_info *info;
3272
3273 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003274 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 return -ENAMETOOLONG;
3276
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003277 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 if (!inode)
3279 return -ENOSPC;
3280
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003281 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003282 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003283 if (error) {
3284 if (error != -EOPNOTSUPP) {
3285 iput(inode);
3286 return error;
3287 }
3288 error = 0;
3289 }
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 info = SHMEM_I(inode);
3292 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003293 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003294 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3295 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003296 iput(inode);
3297 return -ENOMEM;
3298 }
3299 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003301 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003302 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (error) {
3304 iput(inode);
3305 return error;
3306 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003307 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003309 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003310 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003312 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003313 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003316 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 d_instantiate(dentry, inode);
3318 dget(dentry);
3319 return 0;
3320}
3321
Al Virofceef392015-12-29 15:58:39 -05003322static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Al Virofceef392015-12-29 15:58:39 -05003324 mark_page_accessed(arg);
3325 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Al Viro6b255392015-11-17 10:20:54 -05003328static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003329 struct inode *inode,
3330 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003333 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003334 if (!dentry) {
3335 page = find_get_page(inode->i_mapping, 0);
3336 if (!page)
3337 return ERR_PTR(-ECHILD);
3338 if (!PageUptodate(page)) {
3339 put_page(page);
3340 return ERR_PTR(-ECHILD);
3341 }
3342 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003343 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003344 if (error)
3345 return ERR_PTR(error);
3346 unlock_page(page);
3347 }
Al Virofceef392015-12-29 15:58:39 -05003348 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003349 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350}
3351
Eric Parisb09e0fa2011-05-24 17:12:39 -07003352#ifdef CONFIG_TMPFS_XATTR
3353/*
3354 * Superblocks without xattr inode operations may get some security.* xattr
3355 * support from the LSM "for free". As soon as we have any other xattrs
3356 * like ACLs, we also need to implement the security.* handlers at
3357 * filesystem level, though.
3358 */
3359
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003360/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003361 * Callback for security_inode_init_security() for acquiring xattrs.
3362 */
3363static int shmem_initxattrs(struct inode *inode,
3364 const struct xattr *xattr_array,
3365 void *fs_info)
3366{
3367 struct shmem_inode_info *info = SHMEM_I(inode);
3368 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003369 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003370 size_t len;
3371
3372 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003373 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003374 if (!new_xattr)
3375 return -ENOMEM;
3376
3377 len = strlen(xattr->name) + 1;
3378 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3379 GFP_KERNEL);
3380 if (!new_xattr->name) {
3381 kfree(new_xattr);
3382 return -ENOMEM;
3383 }
3384
3385 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3386 XATTR_SECURITY_PREFIX_LEN);
3387 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3388 xattr->name, len);
3389
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003390 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003391 }
3392
3393 return 0;
3394}
3395
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003396static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003397 struct dentry *unused, struct inode *inode,
3398 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003399{
Al Virob2968212016-04-10 20:48:24 -04003400 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003401
3402 name = xattr_full_name(handler, name);
3403 return simple_xattr_get(&info->xattrs, name, buffer, size);
3404}
3405
3406static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003407 struct dentry *unused, struct inode *inode,
3408 const char *name, const void *value,
3409 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003410{
Al Viro59301222016-05-27 10:19:30 -04003411 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003412
3413 name = xattr_full_name(handler, name);
3414 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3415}
3416
3417static const struct xattr_handler shmem_security_xattr_handler = {
3418 .prefix = XATTR_SECURITY_PREFIX,
3419 .get = shmem_xattr_handler_get,
3420 .set = shmem_xattr_handler_set,
3421};
3422
3423static const struct xattr_handler shmem_trusted_xattr_handler = {
3424 .prefix = XATTR_TRUSTED_PREFIX,
3425 .get = shmem_xattr_handler_get,
3426 .set = shmem_xattr_handler_set,
3427};
3428
Eric Parisb09e0fa2011-05-24 17:12:39 -07003429static const struct xattr_handler *shmem_xattr_handlers[] = {
3430#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003431 &posix_acl_access_xattr_handler,
3432 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003433#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003434 &shmem_security_xattr_handler,
3435 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003436 NULL
3437};
3438
Eric Parisb09e0fa2011-05-24 17:12:39 -07003439static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3440{
David Howells75c3cfa2015-03-17 22:26:12 +00003441 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003442 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003443}
3444#endif /* CONFIG_TMPFS_XATTR */
3445
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003446static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003447 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003448#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003449 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003450#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451};
3452
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003453static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003454 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003455#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003456 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003457#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003458};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003459
David M. Grimes91828a42006-10-17 00:09:45 -07003460static struct dentry *shmem_get_parent(struct dentry *child)
3461{
3462 return ERR_PTR(-ESTALE);
3463}
3464
3465static int shmem_match(struct inode *ino, void *vfh)
3466{
3467 __u32 *fh = vfh;
3468 __u64 inum = fh[2];
3469 inum = (inum << 32) | fh[1];
3470 return ino->i_ino == inum && fh[0] == ino->i_generation;
3471}
3472
Christoph Hellwig480b1162007-10-21 16:42:13 -07003473static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3474 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003475{
David M. Grimes91828a42006-10-17 00:09:45 -07003476 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003477 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003478 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003479
Christoph Hellwig480b1162007-10-21 16:42:13 -07003480 if (fh_len < 3)
3481 return NULL;
3482
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003483 inum = fid->raw[2];
3484 inum = (inum << 32) | fid->raw[1];
3485
Christoph Hellwig480b1162007-10-21 16:42:13 -07003486 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3487 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003488 if (inode) {
Christoph Hellwig480b1162007-10-21 16:42:13 -07003489 dentry = d_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003490 iput(inode);
3491 }
3492
Christoph Hellwig480b1162007-10-21 16:42:13 -07003493 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003494}
3495
Al Virob0b03822012-04-02 14:34:06 -04003496static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3497 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003498{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303499 if (*len < 3) {
3500 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003501 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303502 }
David M. Grimes91828a42006-10-17 00:09:45 -07003503
Al Viro1d3382cb2010-10-23 15:19:20 -04003504 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003505 /* Unfortunately insert_inode_hash is not idempotent,
3506 * so as we hash inodes here rather than at creation
3507 * time, we need a lock to ensure we only try
3508 * to do it once
3509 */
3510 static DEFINE_SPINLOCK(lock);
3511 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003512 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003513 __insert_inode_hash(inode,
3514 inode->i_ino + inode->i_generation);
3515 spin_unlock(&lock);
3516 }
3517
3518 fh[0] = inode->i_generation;
3519 fh[1] = inode->i_ino;
3520 fh[2] = ((__u64)inode->i_ino) >> 32;
3521
3522 *len = 3;
3523 return 1;
3524}
3525
Christoph Hellwig39655162007-10-21 16:42:17 -07003526static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003527 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003528 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003529 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003530};
3531
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003532static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3533 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534{
3535 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003536 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003537 uid_t uid;
3538 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003540 while (options != NULL) {
3541 this_char = options;
3542 for (;;) {
3543 /*
3544 * NUL-terminate this option: unfortunately,
3545 * mount options form a comma-separated list,
3546 * but mpol's nodelist may also contain commas.
3547 */
3548 options = strchr(options, ',');
3549 if (options == NULL)
3550 break;
3551 options++;
3552 if (!isdigit(*options)) {
3553 options[-1] = '\0';
3554 break;
3555 }
3556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 if (!*this_char)
3558 continue;
3559 if ((value = strchr(this_char,'=')) != NULL) {
3560 *value++ = 0;
3561 } else {
Joe Perches11705322016-03-17 14:19:50 -07003562 pr_err("tmpfs: No value for mount option '%s'\n",
3563 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003564 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 }
3566
3567 if (!strcmp(this_char,"size")) {
3568 unsigned long long size;
3569 size = memparse(value,&rest);
3570 if (*rest == '%') {
3571 size <<= PAGE_SHIFT;
3572 size *= totalram_pages;
3573 do_div(size, 100);
3574 rest++;
3575 }
3576 if (*rest)
3577 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003578 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003579 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003581 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 if (*rest)
3583 goto bad_val;
3584 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003585 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (*rest)
3587 goto bad_val;
3588 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003589 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003591 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 if (*rest)
3593 goto bad_val;
3594 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003595 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003597 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 if (*rest)
3599 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003600 sbinfo->uid = make_kuid(current_user_ns(), uid);
3601 if (!uid_valid(sbinfo->uid))
3602 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003604 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003606 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 if (*rest)
3608 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003609 sbinfo->gid = make_kgid(current_user_ns(), gid);
3610 if (!gid_valid(sbinfo->gid))
3611 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003612#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003613 } else if (!strcmp(this_char, "huge")) {
3614 int huge;
3615 huge = shmem_parse_huge(value);
3616 if (huge < 0)
3617 goto bad_val;
3618 if (!has_transparent_hugepage() &&
3619 huge != SHMEM_HUGE_NEVER)
3620 goto bad_val;
3621 sbinfo->huge = huge;
3622#endif
3623#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003624 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003625 mpol_put(mpol);
3626 mpol = NULL;
3627 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003628 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003629#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 } else {
Joe Perches11705322016-03-17 14:19:50 -07003631 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003632 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
3634 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003635 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636 return 0;
3637
3638bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003639 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003641error:
3642 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 return 1;
3644
3645}
3646
3647static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3648{
3649 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003650 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003651 unsigned long inodes;
3652 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Greg Thelen5f001102013-02-22 16:36:01 -08003654 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003655 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003656 return error;
3657
3658 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003659 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003660 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003661 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003662 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003663 goto out;
3664 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003665 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003666 * but we must separately disallow unlimited->limited, because
3667 * in that case we have no record of how much is already in use.
3668 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003669 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003670 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003671 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003672 goto out;
3673
3674 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003675 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003676 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003677 sbinfo->max_inodes = config.max_inodes;
3678 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003679
Greg Thelen5f001102013-02-22 16:36:01 -08003680 /*
3681 * Preserve previous mempolicy unless mpol remount option was specified.
3682 */
3683 if (config.mpol) {
3684 mpol_put(sbinfo->mpol);
3685 sbinfo->mpol = config.mpol; /* transfers initial ref */
3686 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003687out:
3688 spin_unlock(&sbinfo->stat_lock);
3689 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003691
Al Viro34c80b12011-12-08 21:32:45 -05003692static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003693{
Al Viro34c80b12011-12-08 21:32:45 -05003694 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003695
3696 if (sbinfo->max_blocks != shmem_default_max_blocks())
3697 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003698 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003699 if (sbinfo->max_inodes != shmem_default_max_inodes())
3700 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
3701 if (sbinfo->mode != (S_IRWXUGO | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003702 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003703 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3704 seq_printf(seq, ",uid=%u",
3705 from_kuid_munged(&init_user_ns, sbinfo->uid));
3706 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3707 seq_printf(seq, ",gid=%u",
3708 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003709#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003710 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3711 if (sbinfo->huge)
3712 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3713#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003714 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003715 return 0;
3716}
David Herrmann9183df22014-08-08 14:25:29 -07003717
3718#define MFD_NAME_PREFIX "memfd:"
3719#define MFD_NAME_PREFIX_LEN (sizeof(MFD_NAME_PREFIX) - 1)
3720#define MFD_NAME_MAX_LEN (NAME_MAX - MFD_NAME_PREFIX_LEN)
3721
Mike Kravetz749df872017-09-06 16:24:16 -07003722#define MFD_ALL_FLAGS (MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_HUGETLB)
David Herrmann9183df22014-08-08 14:25:29 -07003723
3724SYSCALL_DEFINE2(memfd_create,
3725 const char __user *, uname,
3726 unsigned int, flags)
3727{
3728 struct shmem_inode_info *info;
3729 struct file *file;
3730 int fd, error;
3731 char *name;
3732 long len;
3733
Mike Kravetz749df872017-09-06 16:24:16 -07003734 if (!(flags & MFD_HUGETLB)) {
3735 if (flags & ~(unsigned int)MFD_ALL_FLAGS)
3736 return -EINVAL;
3737 } else {
3738 /* Sealing not supported in hugetlbfs (MFD_HUGETLB) */
3739 if (flags & MFD_ALLOW_SEALING)
3740 return -EINVAL;
3741 /* Allow huge page size encoding in flags. */
3742 if (flags & ~(unsigned int)(MFD_ALL_FLAGS |
3743 (MFD_HUGE_MASK << MFD_HUGE_SHIFT)))
3744 return -EINVAL;
3745 }
David Herrmann9183df22014-08-08 14:25:29 -07003746
3747 /* length includes terminating zero */
3748 len = strnlen_user(uname, MFD_NAME_MAX_LEN + 1);
3749 if (len <= 0)
3750 return -EFAULT;
3751 if (len > MFD_NAME_MAX_LEN + 1)
3752 return -EINVAL;
3753
Michal Hocko0ee931c2017-09-13 16:28:29 -07003754 name = kmalloc(len + MFD_NAME_PREFIX_LEN, GFP_KERNEL);
David Herrmann9183df22014-08-08 14:25:29 -07003755 if (!name)
3756 return -ENOMEM;
3757
3758 strcpy(name, MFD_NAME_PREFIX);
3759 if (copy_from_user(&name[MFD_NAME_PREFIX_LEN], uname, len)) {
3760 error = -EFAULT;
3761 goto err_name;
3762 }
3763
3764 /* terminating-zero may have changed after strnlen_user() returned */
3765 if (name[len + MFD_NAME_PREFIX_LEN - 1]) {
3766 error = -EFAULT;
3767 goto err_name;
3768 }
3769
3770 fd = get_unused_fd_flags((flags & MFD_CLOEXEC) ? O_CLOEXEC : 0);
3771 if (fd < 0) {
3772 error = fd;
3773 goto err_name;
3774 }
3775
Mike Kravetz749df872017-09-06 16:24:16 -07003776 if (flags & MFD_HUGETLB) {
3777 struct user_struct *user = NULL;
3778
3779 file = hugetlb_file_setup(name, 0, VM_NORESERVE, &user,
3780 HUGETLB_ANONHUGE_INODE,
3781 (flags >> MFD_HUGE_SHIFT) &
3782 MFD_HUGE_MASK);
3783 } else
3784 file = shmem_file_setup(name, 0, VM_NORESERVE);
David Herrmann9183df22014-08-08 14:25:29 -07003785 if (IS_ERR(file)) {
3786 error = PTR_ERR(file);
3787 goto err_fd;
3788 }
David Herrmann9183df22014-08-08 14:25:29 -07003789 file->f_mode |= FMODE_LSEEK | FMODE_PREAD | FMODE_PWRITE;
3790 file->f_flags |= O_RDWR | O_LARGEFILE;
Mike Kravetz749df872017-09-06 16:24:16 -07003791
3792 if (flags & MFD_ALLOW_SEALING) {
3793 /*
3794 * flags check at beginning of function ensures
3795 * this is not a hugetlbfs (MFD_HUGETLB) file.
3796 */
3797 info = SHMEM_I(file_inode(file));
David Herrmann9183df22014-08-08 14:25:29 -07003798 info->seals &= ~F_SEAL_SEAL;
Mike Kravetz749df872017-09-06 16:24:16 -07003799 }
David Herrmann9183df22014-08-08 14:25:29 -07003800
3801 fd_install(fd, file);
3802 kfree(name);
3803 return fd;
3804
3805err_fd:
3806 put_unused_fd(fd);
3807err_name:
3808 kfree(name);
3809 return error;
3810}
3811
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003812#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
3814static void shmem_put_super(struct super_block *sb)
3815{
Hugh Dickins602586a2010-08-17 15:23:56 -07003816 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3817
3818 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003819 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003820 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 sb->s_fs_info = NULL;
3822}
3823
Kay Sievers2b2af542009-04-30 15:23:42 +02003824int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825{
3826 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003827 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003828 int err = -ENOMEM;
3829
3830 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003831 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003832 L1_CACHE_BYTES), GFP_KERNEL);
3833 if (!sbinfo)
3834 return -ENOMEM;
3835
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003836 sbinfo->mode = S_IRWXUGO | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003837 sbinfo->uid = current_fsuid();
3838 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003839 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003841#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 /*
3843 * Per default we only allow half of the physical ram per
3844 * tmpfs instance, limiting inodes to one per page of lowmem;
3845 * but the internal instance is left unlimited.
3846 */
Al Viroca4e0512013-08-31 12:57:10 -04003847 if (!(sb->s_flags & MS_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003848 sbinfo->max_blocks = shmem_default_max_blocks();
3849 sbinfo->max_inodes = shmem_default_max_inodes();
3850 if (shmem_parse_options(data, sbinfo, false)) {
3851 err = -EINVAL;
3852 goto failed;
3853 }
Al Viroca4e0512013-08-31 12:57:10 -04003854 } else {
3855 sb->s_flags |= MS_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
David M. Grimes91828a42006-10-17 00:09:45 -07003857 sb->s_export_op = &shmem_export_ops;
Hugh Dickins2f6e38f2012-05-29 15:06:38 -07003858 sb->s_flags |= MS_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859#else
3860 sb->s_flags |= MS_NOUSER;
3861#endif
3862
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003863 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003864 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003865 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003866 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003867 spin_lock_init(&sbinfo->shrinklist_lock);
3868 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003869
Hugh Dickins285b2c42011-08-03 16:21:20 -07003870 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003871 sb->s_blocksize = PAGE_SIZE;
3872 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 sb->s_magic = TMPFS_MAGIC;
3874 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003875 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003876#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003877 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003878#endif
3879#ifdef CONFIG_TMPFS_POSIX_ACL
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003880 sb->s_flags |= MS_POSIXACL;
3881#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003882 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003883
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003884 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 if (!inode)
3886 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003887 inode->i_uid = sbinfo->uid;
3888 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003889 sb->s_root = d_make_root(inode);
3890 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003891 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 return 0;
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894failed:
3895 shmem_put_super(sb);
3896 return err;
3897}
3898
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003899static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900
3901static struct inode *shmem_alloc_inode(struct super_block *sb)
3902{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003903 struct shmem_inode_info *info;
3904 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3905 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003907 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003910static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003911{
3912 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003913 if (S_ISLNK(inode->i_mode))
3914 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003915 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3916}
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918static void shmem_destroy_inode(struct inode *inode)
3919{
Al Viro09208d12011-07-26 03:15:03 -04003920 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003922 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003925static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003927 struct shmem_inode_info *info = foo;
3928 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929}
3930
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003931static int shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932{
3933 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3934 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003935 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 return 0;
3937}
3938
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003939static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003941 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003944static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003946 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003948 .write_begin = shmem_write_begin,
3949 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003951#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003952 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003953#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003954 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955};
3956
Helge Deller15ad7cd2006-12-06 20:40:36 -08003957static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003959 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003961 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003962 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003963 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003964 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003965 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003966 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003967 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968#endif
3969};
3970
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003971static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003972 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003973 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003974#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003975 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003976 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978};
3979
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003980static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981#ifdef CONFIG_TMPFS
3982 .create = shmem_create,
3983 .lookup = simple_lookup,
3984 .link = shmem_link,
3985 .unlink = shmem_unlink,
3986 .symlink = shmem_symlink,
3987 .mkdir = shmem_mkdir,
3988 .rmdir = shmem_rmdir,
3989 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003990 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003991 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003993#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003994 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003995#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003996#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003997 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003998 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003999#endif
4000};
4001
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004002static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07004003#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07004004 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07004005#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07004006#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07004007 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08004008 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07004009#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010};
4011
Hugh Dickins759b9772007-03-05 00:30:28 -08004012static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .alloc_inode = shmem_alloc_inode,
4014 .destroy_inode = shmem_destroy_inode,
4015#ifdef CONFIG_TMPFS
4016 .statfs = shmem_statfs,
4017 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08004018 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019#endif
Al Viro1f895f72010-06-05 19:10:41 -04004020 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 .drop_inode = generic_delete_inode,
4022 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07004023#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
4024 .nr_cached_objects = shmem_unused_huge_count,
4025 .free_cached_objects = shmem_unused_huge_scan,
4026#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027};
4028
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04004029static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07004030 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07004031 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032#ifdef CONFIG_NUMA
4033 .set_policy = shmem_set_policy,
4034 .get_policy = shmem_get_policy,
4035#endif
4036};
4037
Al Viro3c26ff62010-07-25 11:46:36 +04004038static struct dentry *shmem_mount(struct file_system_type *fs_type,
4039 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040{
Al Viro3c26ff62010-07-25 11:46:36 +04004041 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004044static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 .owner = THIS_MODULE,
4046 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04004047 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004049 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004052int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053{
4054 int error;
4055
Rob Landley16203a72013-09-11 14:26:12 -07004056 /* If rootfs called this, don't re-init */
4057 if (shmem_inode_cachep)
4058 return 0;
4059
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004060 error = shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (error)
4062 goto out3;
4063
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004064 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07004066 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 goto out2;
4068 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07004069
Al Viroca4e0512013-08-31 12:57:10 -04004070 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (IS_ERR(shm_mnt)) {
4072 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07004073 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 goto out1;
4075 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004076
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004077#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07004078 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004079 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
4080 else
4081 shmem_huge = 0; /* just in case it was patched */
4082#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 return 0;
4084
4085out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004086 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004088 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089out3:
4090 shm_mnt = ERR_PTR(error);
4091 return error;
4092}
Matt Mackall853ac432009-01-06 14:40:20 -08004093
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004094#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004095static ssize_t shmem_enabled_show(struct kobject *kobj,
4096 struct kobj_attribute *attr, char *buf)
4097{
4098 int values[] = {
4099 SHMEM_HUGE_ALWAYS,
4100 SHMEM_HUGE_WITHIN_SIZE,
4101 SHMEM_HUGE_ADVISE,
4102 SHMEM_HUGE_NEVER,
4103 SHMEM_HUGE_DENY,
4104 SHMEM_HUGE_FORCE,
4105 };
4106 int i, count;
4107
4108 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
4109 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
4110
4111 count += sprintf(buf + count, fmt,
4112 shmem_format_huge(values[i]));
4113 }
4114 buf[count - 1] = '\n';
4115 return count;
4116}
4117
4118static ssize_t shmem_enabled_store(struct kobject *kobj,
4119 struct kobj_attribute *attr, const char *buf, size_t count)
4120{
4121 char tmp[16];
4122 int huge;
4123
4124 if (count + 1 > sizeof(tmp))
4125 return -EINVAL;
4126 memcpy(tmp, buf, count);
4127 tmp[count] = '\0';
4128 if (count && tmp[count - 1] == '\n')
4129 tmp[count - 1] = '\0';
4130
4131 huge = shmem_parse_huge(tmp);
4132 if (huge == -EINVAL)
4133 return -EINVAL;
4134 if (!has_transparent_hugepage() &&
4135 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
4136 return -EINVAL;
4137
4138 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07004139 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004140 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
4141 return count;
4142}
4143
4144struct kobj_attribute shmem_enabled_attr =
4145 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07004146#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004147
Arnd Bergmann3b337192016-08-10 16:27:44 -07004148#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004149bool shmem_huge_enabled(struct vm_area_struct *vma)
4150{
4151 struct inode *inode = file_inode(vma->vm_file);
4152 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
4153 loff_t i_size;
4154 pgoff_t off;
4155
4156 if (shmem_huge == SHMEM_HUGE_FORCE)
4157 return true;
4158 if (shmem_huge == SHMEM_HUGE_DENY)
4159 return false;
4160 switch (sbinfo->huge) {
4161 case SHMEM_HUGE_NEVER:
4162 return false;
4163 case SHMEM_HUGE_ALWAYS:
4164 return true;
4165 case SHMEM_HUGE_WITHIN_SIZE:
4166 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
4167 i_size = round_up(i_size_read(inode), PAGE_SIZE);
4168 if (i_size >= HPAGE_PMD_SIZE &&
4169 i_size >> PAGE_SHIFT >= off)
4170 return true;
4171 case SHMEM_HUGE_ADVISE:
4172 /* TODO: implement fadvise() hints */
4173 return (vma->vm_flags & VM_HUGEPAGE);
4174 default:
4175 VM_BUG_ON(1);
4176 return false;
4177 }
4178}
Arnd Bergmann3b337192016-08-10 16:27:44 -07004179#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07004180
Matt Mackall853ac432009-01-06 14:40:20 -08004181#else /* !CONFIG_SHMEM */
4182
4183/*
4184 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
4185 *
4186 * This is intended for small system where the benefits of the full
4187 * shmem code (swap-backed and resource-limited) are outweighed by
4188 * their complexity. On systems without swap this code should be
4189 * effectively equivalent, but much lighter weight.
4190 */
4191
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004192static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08004193 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04004194 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08004195 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08004196 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08004197};
4198
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004199int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08004200{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004201 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08004202
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004203 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08004204 BUG_ON(IS_ERR(shm_mnt));
4205
4206 return 0;
4207}
4208
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004209int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08004210{
4211 return 0;
4212}
4213
Hugh Dickins3f96b792009-09-21 17:03:37 -07004214int shmem_lock(struct file *file, int lock, struct user_struct *user)
4215{
4216 return 0;
4217}
4218
Hugh Dickins24513262012-01-20 14:34:21 -08004219void shmem_unlock_mapping(struct address_space *mapping)
4220{
4221}
4222
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07004223#ifdef CONFIG_MMU
4224unsigned long shmem_get_unmapped_area(struct file *file,
4225 unsigned long addr, unsigned long len,
4226 unsigned long pgoff, unsigned long flags)
4227{
4228 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
4229}
4230#endif
4231
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004232void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07004233{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07004234 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07004235}
4236EXPORT_SYMBOL_GPL(shmem_truncate_range);
4237
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004238#define shmem_vm_ops generic_file_vm_ops
4239#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03004240#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00004241#define shmem_acct_size(flags, size) 0
4242#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08004243
4244#endif /* CONFIG_SHMEM */
4245
4246/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Rasmus Villemoes19938e32016-10-07 17:01:01 -07004248static const struct dentry_operations anon_ops = {
Al Viro118b2302013-08-24 12:08:17 -04004249 .d_dname = simple_dname
Al Viro34515382013-02-14 22:38:02 -05004250};
4251
Eric Parisc7277092013-12-02 11:24:19 +00004252static struct file *__shmem_file_setup(const char *name, loff_t size,
4253 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Al Viro6b4d0b22013-02-14 21:37:26 -05004255 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 struct inode *inode;
Al Viro2c48b9c2009-08-09 00:52:35 +04004257 struct path path;
Al Viro34515382013-02-14 22:38:02 -05004258 struct super_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 struct qstr this;
4260
4261 if (IS_ERR(shm_mnt))
Al Viro6b4d0b22013-02-14 21:37:26 -05004262 return ERR_CAST(shm_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Hugh Dickins285b2c42011-08-03 16:21:20 -07004264 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 return ERR_PTR(-EINVAL);
4266
4267 if (shmem_acct_size(flags, size))
4268 return ERR_PTR(-ENOMEM);
4269
Al Viro6b4d0b22013-02-14 21:37:26 -05004270 res = ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 this.name = name;
4272 this.len = strlen(name);
4273 this.hash = 0; /* will go */
Al Viro34515382013-02-14 22:38:02 -05004274 sb = shm_mnt->mnt_sb;
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07004275 path.mnt = mntget(shm_mnt);
Al Viro34515382013-02-14 22:38:02 -05004276 path.dentry = d_alloc_pseudo(sb, &this);
Al Viro2c48b9c2009-08-09 00:52:35 +04004277 if (!path.dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 goto put_memory;
Al Viro34515382013-02-14 22:38:02 -05004279 d_set_d_op(path.dentry, &anon_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Al Viro6b4d0b22013-02-14 21:37:26 -05004281 res = ERR_PTR(-ENOSPC);
Al Viro34515382013-02-14 22:38:02 -05004282 inode = shmem_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (!inode)
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07004284 goto put_memory;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Eric Parisc7277092013-12-02 11:24:19 +00004286 inode->i_flags |= i_flags;
Al Viro2c48b9c2009-08-09 00:52:35 +04004287 d_instantiate(path.dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02004289 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05004290 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
4291 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07004292 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04004293
Al Viro6b4d0b22013-02-14 21:37:26 -05004294 res = alloc_file(&path, FMODE_WRITE | FMODE_READ,
Al Viro4b42af82009-08-05 18:25:56 +04004295 &shmem_file_operations);
Al Viro6b4d0b22013-02-14 21:37:26 -05004296 if (IS_ERR(res))
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07004297 goto put_path;
Al Viro4b42af82009-08-05 18:25:56 +04004298
Al Viro6b4d0b22013-02-14 21:37:26 -05004299 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301put_memory:
4302 shmem_unacct_size(flags, size);
Konstantin Khlebnikov66ee4b82014-08-06 16:06:32 -07004303put_path:
4304 path_put(&path);
Al Viro6b4d0b22013-02-14 21:37:26 -05004305 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306}
Eric Parisc7277092013-12-02 11:24:19 +00004307
4308/**
4309 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
4310 * kernel internal. There will be NO LSM permission checks against the
4311 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07004312 * higher layer. The users are the big_key and shm implementations. LSM
4313 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00004314 * @name: name for dentry (to be seen in /proc/<pid>/maps
4315 * @size: size to be set for the file
4316 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4317 */
4318struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
4319{
4320 return __shmem_file_setup(name, size, flags, S_PRIVATE);
4321}
4322
4323/**
4324 * shmem_file_setup - get an unlinked file living in tmpfs
4325 * @name: name for dentry (to be seen in /proc/<pid>/maps
4326 * @size: size to be set for the file
4327 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
4328 */
4329struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
4330{
4331 return __shmem_file_setup(name, size, flags, 0);
4332}
Keith Packard395e0dd2008-06-20 00:08:06 -07004333EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334
Randy Dunlap46711812008-03-19 17:00:41 -07004335/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
4338 */
4339int shmem_zero_setup(struct vm_area_struct *vma)
4340{
4341 struct file *file;
4342 loff_t size = vma->vm_end - vma->vm_start;
4343
Hugh Dickins66fc1302015-06-14 09:48:09 -07004344 /*
4345 * Cloning a new file under mmap_sem leads to a lock ordering conflict
4346 * between XFS directory reading and selinux: since this file is only
4347 * accessible to the user through its mapping, use S_PRIVATE flag to
4348 * bypass file security, in the same way as shmem_kernel_file_setup().
4349 */
4350 file = __shmem_file_setup("dev/zero", size, vma->vm_flags, S_PRIVATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 if (IS_ERR(file))
4352 return PTR_ERR(file);
4353
4354 if (vma->vm_file)
4355 fput(vma->vm_file);
4356 vma->vm_file = file;
4357 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004358
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07004359 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07004360 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
4361 (vma->vm_end & HPAGE_PMD_MASK)) {
4362 khugepaged_enter(vma, vma->vm_flags);
4363 }
4364
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 return 0;
4366}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004367
4368/**
4369 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4370 * @mapping: the page's address_space
4371 * @index: the page index
4372 * @gfp: the page allocator flags to use if allocating
4373 *
4374 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4375 * with any new page allocations done using the specified allocation flags.
4376 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4377 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4378 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4379 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004380 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4381 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004382 */
4383struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4384 pgoff_t index, gfp_t gfp)
4385{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004386#ifdef CONFIG_SHMEM
4387 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004388 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004389 int error;
4390
4391 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004392 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004393 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004394 if (error)
4395 page = ERR_PTR(error);
4396 else
4397 unlock_page(page);
4398 return page;
4399#else
4400 /*
4401 * The tiny !SHMEM case uses ramfs without swap
4402 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004403 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004404#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004405}
4406EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);