blob: 5697c8fecdfcb747ee1e725d7751eaee44dab1f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Resizable virtual memory filesystem for Linux.
3 *
4 * Copyright (C) 2000 Linus Torvalds.
5 * 2000 Transmeta Corp.
6 * 2000-2001 Christoph Rohland
7 * 2000-2001 SAP AG
8 * 2002 Red Hat Inc.
Hugh Dickins6922c0c2011-08-03 16:21:25 -07009 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
Hugh Dickins0edd73b2005-06-21 17:15:04 -070011 * Copyright (C) 2002-2005 VERITAS Software Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright (C) 2004 Andi Kleen, SuSE Labs
13 *
14 * Extended attribute support for tmpfs:
15 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net>
16 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com>
17 *
Matt Mackall853ac432009-01-06 14:40:20 -080018 * tiny-shmem:
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
20 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 * This file is released under the GPL.
22 */
23
Matt Mackall853ac432009-01-06 14:40:20 -080024#include <linux/fs.h>
25#include <linux/init.h>
26#include <linux/vfs.h>
27#include <linux/mount.h>
Andrew Morton250297e2013-04-29 15:06:12 -070028#include <linux/ramfs.h>
Hugh Dickinscaefba12009-04-13 14:40:12 -070029#include <linux/pagemap.h>
Matt Mackall853ac432009-01-06 14:40:20 -080030#include <linux/file.h>
31#include <linux/mm.h>
Arnd Bergmann46c9a942018-08-17 15:45:09 -070032#include <linux/random.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010033#include <linux/sched/signal.h>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040034#include <linux/export.h>
Matt Mackall853ac432009-01-06 14:40:20 -080035#include <linux/swap.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080036#include <linux/uio.h>
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -070037#include <linux/khugepaged.h>
Mike Kravetz749df872017-09-06 16:24:16 -070038#include <linux/hugetlb.h>
Matt Mackall853ac432009-01-06 14:40:20 -080039
Andrea Arcangeli95cc09d2017-02-22 15:43:31 -080040#include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
41
Matt Mackall853ac432009-01-06 14:40:20 -080042static struct vfsmount *shm_mnt;
43
44#ifdef CONFIG_SHMEM
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/*
46 * This virtual memory filesystem is heavily based on the ramfs. It
47 * extends ramfs by the ability to use swap and honor resource limits
48 * which makes it a completely usable filesystem.
49 */
50
Andreas Gruenbacher39f02472006-09-29 02:01:35 -070051#include <linux/xattr.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070052#include <linux/exportfs.h>
Christoph Hellwig1c7c4742009-11-03 16:44:44 +010053#include <linux/posix_acl.h>
Christoph Hellwigfeda8212013-12-20 05:16:54 -080054#include <linux/posix_acl_xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <linux/mman.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/string.h>
57#include <linux/slab.h>
58#include <linux/backing-dev.h>
59#include <linux/shmem_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <linux/blkdev.h>
Hugh Dickinsbda97ea2011-08-03 16:21:21 -070062#include <linux/pagevec.h>
Hugh Dickins41ffe5d2011-08-03 16:21:21 -070063#include <linux/percpu_counter.h>
Hugh Dickins83e4fa92012-05-29 15:06:40 -070064#include <linux/falloc.h>
Hugh Dickins708e3502011-07-25 17:12:32 -070065#include <linux/splice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <linux/security.h>
67#include <linux/swapops.h>
68#include <linux/mempolicy.h>
69#include <linux/namei.h>
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +000070#include <linux/ctype.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070071#include <linux/migrate.h>
Christoph Lameterc1f60a52006-09-25 23:31:11 -070072#include <linux/highmem.h>
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -080073#include <linux/seq_file.h>
Mimi Zohar92562922008-10-07 14:00:12 -040074#include <linux/magic.h>
David Herrmann9183df22014-08-08 14:25:29 -070075#include <linux/syscalls.h>
David Herrmann40e041a2014-08-08 14:25:27 -070076#include <linux/fcntl.h>
David Herrmann9183df22014-08-08 14:25:29 -070077#include <uapi/linux/memfd.h>
Mike Rapoportcfda0522017-02-22 15:43:37 -080078#include <linux/userfaultfd_k.h>
Mike Rapoport4c27fe42017-02-22 15:43:25 -080079#include <linux/rmap.h>
Amir Goldstein2b4db792017-05-18 15:29:33 +030080#include <linux/uuid.h>
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -070081
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080082#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#include <asm/pgtable.h>
84
Mel Gormandd56b042015-11-06 16:28:43 -080085#include "internal.h"
86
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030087#define BLOCKS_PER_PAGE (PAGE_SIZE/512)
88#define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Pretend that each entry is of this size in directory's i_size */
91#define BOGO_DIRENT_SIZE 20
92
Hugh Dickins69f07ec2011-08-03 16:21:26 -070093/* Symlink up to this size is kmalloc'ed instead of using a swappable page */
94#define SHORT_SYMLINK_LEN 128
95
Hugh Dickins1aac1402012-05-29 15:06:42 -070096/*
Hugh Dickinsf00cdc62014-06-23 13:22:06 -070097 * shmem_fallocate communicates with shmem_fault or shmem_writepage via
98 * inode->i_private (with i_mutex making sure that it has only one user at
99 * a time): we would prefer not to enlarge the shmem inode just for that.
Hugh Dickins1aac1402012-05-29 15:06:42 -0700100 */
101struct shmem_falloc {
Hugh Dickins8e205f72014-07-23 14:00:10 -0700102 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */
Hugh Dickins1aac1402012-05-29 15:06:42 -0700103 pgoff_t start; /* start of range currently being fallocated */
104 pgoff_t next; /* the next page offset to be fallocated */
105 pgoff_t nr_falloced; /* how many new pages have been fallocated */
106 pgoff_t nr_unswapped; /* how often writepage refused to swap out */
107};
108
Andrew Mortonb76db732008-02-08 04:21:49 -0800109#ifdef CONFIG_TMPFS
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800110static unsigned long shmem_default_max_blocks(void)
111{
112 return totalram_pages / 2;
113}
114
115static unsigned long shmem_default_max_inodes(void)
116{
117 return min(totalram_pages - totalhigh_pages, totalram_pages / 2);
118}
Andrew Mortonb76db732008-02-08 04:21:49 -0800119#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -0800120
Hugh Dickinsbde05d12012-05-29 15:06:38 -0700121static bool shmem_should_replace_page(struct page *page, gfp_t gfp);
122static int shmem_replace_page(struct page **pagep, gfp_t gfp,
123 struct shmem_inode_info *info, pgoff_t index);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700124static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700125 struct page **pagep, enum sgp_type sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800126 gfp_t gfp, struct vm_area_struct *vma,
Souptick Joarder2b740302018-08-23 17:01:36 -0700127 struct vm_fault *vmf, vm_fault_t *fault_type);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700128
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -0700129int shmem_getpage(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700130 struct page **pagep, enum sgp_type sgp)
Hugh Dickins68da9f02011-07-25 17:12:34 -0700131{
132 return shmem_getpage_gfp(inode, index, pagep, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -0800133 mapping_gfp_mask(inode->i_mapping), NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -0700134}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb)
137{
138 return sb->s_fs_info;
139}
140
141/*
142 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
143 * for shared memory and for shared anonymous (/dev/zero) mappings
144 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
145 * consistent with the pre-accounting of private mappings ...
146 */
147static inline int shmem_acct_size(unsigned long flags, loff_t size)
148{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000149 return (flags & VM_NORESERVE) ?
Al Viro191c5422012-02-13 03:58:52 +0000150 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static inline void shmem_unacct_size(unsigned long flags, loff_t size)
154{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000155 if (!(flags & VM_NORESERVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 vm_unacct_memory(VM_ACCT(size));
157}
158
Konstantin Khlebnikov77142512014-08-06 16:06:34 -0700159static inline int shmem_reacct_size(unsigned long flags,
160 loff_t oldsize, loff_t newsize)
161{
162 if (!(flags & VM_NORESERVE)) {
163 if (VM_ACCT(newsize) > VM_ACCT(oldsize))
164 return security_vm_enough_memory_mm(current->mm,
165 VM_ACCT(newsize) - VM_ACCT(oldsize));
166 else if (VM_ACCT(newsize) < VM_ACCT(oldsize))
167 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize));
168 }
169 return 0;
170}
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ... whereas tmpfs objects are accounted incrementally as
Hugh Dickins75edd342016-05-19 17:12:44 -0700174 * pages are allocated, in order to allow large sparse files.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
176 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
177 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700178static inline int shmem_acct_block(unsigned long flags, long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700180 if (!(flags & VM_NORESERVE))
181 return 0;
182
183 return security_vm_enough_memory_mm(current->mm,
184 pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187static inline void shmem_unacct_blocks(unsigned long flags, long pages)
188{
Hugh Dickins0b0a0802009-02-24 20:51:52 +0000189 if (flags & VM_NORESERVE)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300190 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Mike Rapoport0f079692017-09-06 16:22:59 -0700193static inline bool shmem_inode_acct_block(struct inode *inode, long pages)
194{
195 struct shmem_inode_info *info = SHMEM_I(inode);
196 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
197
198 if (shmem_acct_block(info->flags, pages))
199 return false;
200
201 if (sbinfo->max_blocks) {
202 if (percpu_counter_compare(&sbinfo->used_blocks,
203 sbinfo->max_blocks - pages) > 0)
204 goto unacct;
205 percpu_counter_add(&sbinfo->used_blocks, pages);
206 }
207
208 return true;
209
210unacct:
211 shmem_unacct_blocks(info->flags, pages);
212 return false;
213}
214
215static inline void shmem_inode_unacct_blocks(struct inode *inode, long pages)
216{
217 struct shmem_inode_info *info = SHMEM_I(inode);
218 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
219
220 if (sbinfo->max_blocks)
221 percpu_counter_sub(&sbinfo->used_blocks, pages);
222 shmem_unacct_blocks(info->flags, pages);
223}
224
Hugh Dickins759b9772007-03-05 00:30:28 -0800225static const struct super_operations shmem_ops;
Christoph Hellwigf5e54d62006-06-28 04:26:44 -0700226static const struct address_space_operations shmem_aops;
Helge Deller15ad7cd2006-12-06 20:40:36 -0800227static const struct file_operations shmem_file_operations;
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800228static const struct inode_operations shmem_inode_operations;
229static const struct inode_operations shmem_dir_inode_operations;
230static const struct inode_operations shmem_special_inode_operations;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400231static const struct vm_operations_struct shmem_vm_ops;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700232static struct file_system_type shmem_fs_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Mike Rapoportb0506e42017-02-22 15:43:28 -0800234bool vma_is_shmem(struct vm_area_struct *vma)
235{
236 return vma->vm_ops == &shmem_vm_ops;
237}
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static LIST_HEAD(shmem_swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -0800240static DEFINE_MUTEX(shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Pavel Emelyanov5b04c682008-02-04 22:28:47 -0800242static int shmem_reserve_inode(struct super_block *sb)
243{
244 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
245 if (sbinfo->max_inodes) {
246 spin_lock(&sbinfo->stat_lock);
247 if (!sbinfo->free_inodes) {
248 spin_unlock(&sbinfo->stat_lock);
249 return -ENOSPC;
250 }
251 sbinfo->free_inodes--;
252 spin_unlock(&sbinfo->stat_lock);
253 }
254 return 0;
255}
256
257static void shmem_free_inode(struct super_block *sb)
258{
259 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
260 if (sbinfo->max_inodes) {
261 spin_lock(&sbinfo->stat_lock);
262 sbinfo->free_inodes++;
263 spin_unlock(&sbinfo->stat_lock);
264 }
265}
266
Randy Dunlap46711812008-03-19 17:00:41 -0700267/**
Hugh Dickins41ffe5d2011-08-03 16:21:21 -0700268 * shmem_recalc_inode - recalculate the block usage of an inode
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 * @inode: inode to recalc
270 *
271 * We have to calculate the free blocks since the mm can drop
272 * undirtied hole pages behind our back.
273 *
274 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
275 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
276 *
277 * It has to be called with the spinlock held.
278 */
279static void shmem_recalc_inode(struct inode *inode)
280{
281 struct shmem_inode_info *info = SHMEM_I(inode);
282 long freed;
283
284 freed = info->alloced - info->swapped - inode->i_mapping->nrpages;
285 if (freed > 0) {
286 info->alloced -= freed;
Hugh Dickins54af60422011-08-03 16:21:24 -0700287 inode->i_blocks -= freed * BLOCKS_PER_PAGE;
Mike Rapoport0f079692017-09-06 16:22:59 -0700288 shmem_inode_unacct_blocks(inode, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290}
291
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700292bool shmem_charge(struct inode *inode, long pages)
293{
294 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700295 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700296
Mike Rapoport0f079692017-09-06 16:22:59 -0700297 if (!shmem_inode_acct_block(inode, pages))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700298 return false;
Mike Rapoportb1cc94a2017-09-06 16:22:56 -0700299
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700300 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700301 info->alloced += pages;
302 inode->i_blocks += pages * BLOCKS_PER_PAGE;
303 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700304 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700305 inode->i_mapping->nrpages += pages;
306
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700307 return true;
308}
309
310void shmem_uncharge(struct inode *inode, long pages)
311{
312 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700313 unsigned long flags;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700314
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700315 spin_lock_irqsave(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700316 info->alloced -= pages;
317 inode->i_blocks -= pages * BLOCKS_PER_PAGE;
318 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700319 spin_unlock_irqrestore(&info->lock, flags);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700320
Mike Rapoport0f079692017-09-06 16:22:59 -0700321 shmem_inode_unacct_blocks(inode, pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700322}
323
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700324/*
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500325 * Replace item expected in xarray by a new item, while holding xa_lock.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700326 */
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500327static int shmem_replace_entry(struct address_space *mapping,
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700328 pgoff_t index, void *expected, void *replacement)
329{
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500330 XA_STATE(xas, &mapping->i_pages, index);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700331 void *item;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700332
333 VM_BUG_ON(!expected);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700334 VM_BUG_ON(!replacement);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500335 item = xas_load(&xas);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700336 if (item != expected)
337 return -ENOENT;
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500338 xas_store(&xas, replacement);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700339 return 0;
340}
341
342/*
Hugh Dickinsd1899222012-07-11 14:02:47 -0700343 * Sometimes, before we decide whether to proceed or to fail, we must check
344 * that an entry was not already brought back from swap by a racing thread.
345 *
346 * Checking page is not enough: by the time a SwapCache page is locked, it
347 * might be reused, and again be SwapCache, using the same swap as before.
348 */
349static bool shmem_confirm_swap(struct address_space *mapping,
350 pgoff_t index, swp_entry_t swap)
351{
352 void *item;
353
354 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700355 item = radix_tree_lookup(&mapping->i_pages, index);
Hugh Dickinsd1899222012-07-11 14:02:47 -0700356 rcu_read_unlock();
357 return item == swp_to_radix_entry(swap);
358}
359
360/*
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700361 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
362 *
363 * SHMEM_HUGE_NEVER:
364 * disables huge pages for the mount;
365 * SHMEM_HUGE_ALWAYS:
366 * enables huge pages for the mount;
367 * SHMEM_HUGE_WITHIN_SIZE:
368 * only allocate huge pages if the page will be fully within i_size,
369 * also respect fadvise()/madvise() hints;
370 * SHMEM_HUGE_ADVISE:
371 * only allocate huge pages if requested with fadvise()/madvise();
372 */
373
374#define SHMEM_HUGE_NEVER 0
375#define SHMEM_HUGE_ALWAYS 1
376#define SHMEM_HUGE_WITHIN_SIZE 2
377#define SHMEM_HUGE_ADVISE 3
378
379/*
380 * Special values.
381 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
382 *
383 * SHMEM_HUGE_DENY:
384 * disables huge on shm_mnt and all mounts, for emergency use;
385 * SHMEM_HUGE_FORCE:
386 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
387 *
388 */
389#define SHMEM_HUGE_DENY (-1)
390#define SHMEM_HUGE_FORCE (-2)
391
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700392#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700393/* ifdef here to avoid bloating shmem.o when not necessary */
394
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700395static int shmem_huge __read_mostly;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700396
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800397#if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700398static int shmem_parse_huge(const char *str)
399{
400 if (!strcmp(str, "never"))
401 return SHMEM_HUGE_NEVER;
402 if (!strcmp(str, "always"))
403 return SHMEM_HUGE_ALWAYS;
404 if (!strcmp(str, "within_size"))
405 return SHMEM_HUGE_WITHIN_SIZE;
406 if (!strcmp(str, "advise"))
407 return SHMEM_HUGE_ADVISE;
408 if (!strcmp(str, "deny"))
409 return SHMEM_HUGE_DENY;
410 if (!strcmp(str, "force"))
411 return SHMEM_HUGE_FORCE;
412 return -EINVAL;
413}
414
415static const char *shmem_format_huge(int huge)
416{
417 switch (huge) {
418 case SHMEM_HUGE_NEVER:
419 return "never";
420 case SHMEM_HUGE_ALWAYS:
421 return "always";
422 case SHMEM_HUGE_WITHIN_SIZE:
423 return "within_size";
424 case SHMEM_HUGE_ADVISE:
425 return "advise";
426 case SHMEM_HUGE_DENY:
427 return "deny";
428 case SHMEM_HUGE_FORCE:
429 return "force";
430 default:
431 VM_BUG_ON(1);
432 return "bad_val";
433 }
434}
Jérémy Lefauref1f59292016-12-12 16:43:23 -0800435#endif
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700436
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700437static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
438 struct shrink_control *sc, unsigned long nr_to_split)
439{
440 LIST_HEAD(list), *pos, *next;
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800441 LIST_HEAD(to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700442 struct inode *inode;
443 struct shmem_inode_info *info;
444 struct page *page;
445 unsigned long batch = sc ? sc->nr_to_scan : 128;
446 int removed = 0, split = 0;
447
448 if (list_empty(&sbinfo->shrinklist))
449 return SHRINK_STOP;
450
451 spin_lock(&sbinfo->shrinklist_lock);
452 list_for_each_safe(pos, next, &sbinfo->shrinklist) {
453 info = list_entry(pos, struct shmem_inode_info, shrinklist);
454
455 /* pin the inode */
456 inode = igrab(&info->vfs_inode);
457
458 /* inode is about to be evicted */
459 if (!inode) {
460 list_del_init(&info->shrinklist);
461 removed++;
462 goto next;
463 }
464
465 /* Check if there's anything to gain */
466 if (round_up(inode->i_size, PAGE_SIZE) ==
467 round_up(inode->i_size, HPAGE_PMD_SIZE)) {
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800468 list_move(&info->shrinklist, &to_remove);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700469 removed++;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700470 goto next;
471 }
472
473 list_move(&info->shrinklist, &list);
474next:
475 if (!--batch)
476 break;
477 }
478 spin_unlock(&sbinfo->shrinklist_lock);
479
Kirill A. Shutemov253fd0f2017-02-03 13:13:15 -0800480 list_for_each_safe(pos, next, &to_remove) {
481 info = list_entry(pos, struct shmem_inode_info, shrinklist);
482 inode = &info->vfs_inode;
483 list_del_init(&info->shrinklist);
484 iput(inode);
485 }
486
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700487 list_for_each_safe(pos, next, &list) {
488 int ret;
489
490 info = list_entry(pos, struct shmem_inode_info, shrinklist);
491 inode = &info->vfs_inode;
492
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700493 if (nr_to_split && split >= nr_to_split)
494 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700495
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700496 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700497 (inode->i_size & HPAGE_PMD_MASK) >> PAGE_SHIFT);
498 if (!page)
499 goto drop;
500
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700501 /* No huge page at the end of the file: nothing to split */
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700502 if (!PageTransHuge(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700503 put_page(page);
504 goto drop;
505 }
506
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700507 /*
508 * Leave the inode on the list if we failed to lock
509 * the page at this time.
510 *
511 * Waiting for the lock may lead to deadlock in the
512 * reclaim path.
513 */
514 if (!trylock_page(page)) {
515 put_page(page);
516 goto leave;
517 }
518
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700519 ret = split_huge_page(page);
520 unlock_page(page);
521 put_page(page);
522
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700523 /* If split failed leave the inode on the list */
524 if (ret)
525 goto leave;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700526
527 split++;
528drop:
529 list_del_init(&info->shrinklist);
530 removed++;
Kirill A. Shutemovb3cd54b2018-03-22 16:17:35 -0700531leave:
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700532 iput(inode);
533 }
534
535 spin_lock(&sbinfo->shrinklist_lock);
536 list_splice_tail(&list, &sbinfo->shrinklist);
537 sbinfo->shrinklist_len -= removed;
538 spin_unlock(&sbinfo->shrinklist_lock);
539
540 return split;
541}
542
543static long shmem_unused_huge_scan(struct super_block *sb,
544 struct shrink_control *sc)
545{
546 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
547
548 if (!READ_ONCE(sbinfo->shrinklist_len))
549 return SHRINK_STOP;
550
551 return shmem_unused_huge_shrink(sbinfo, sc, 0);
552}
553
554static long shmem_unused_huge_count(struct super_block *sb,
555 struct shrink_control *sc)
556{
557 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
558 return READ_ONCE(sbinfo->shrinklist_len);
559}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700560#else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700561
562#define shmem_huge SHMEM_HUGE_DENY
563
Kirill A. Shutemov779750d2016-07-26 15:26:38 -0700564static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo,
565 struct shrink_control *sc, unsigned long nr_to_split)
566{
567 return 0;
568}
Kirill A. Shutemove496cf32016-07-26 15:26:35 -0700569#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700570
Yang Shi89fdcd22018-06-07 17:06:59 -0700571static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)
572{
573 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
574 (shmem_huge == SHMEM_HUGE_FORCE || sbinfo->huge) &&
575 shmem_huge != SHMEM_HUGE_DENY)
576 return true;
577 return false;
578}
579
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -0700580/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700581 * Like add_to_page_cache_locked, but error if expected item has gone.
582 */
583static int shmem_add_to_page_cache(struct page *page,
584 struct address_space *mapping,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -0700585 pgoff_t index, void *expected)
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700586{
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700587 int error, nr = hpage_nr_pages(page);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700588
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700589 VM_BUG_ON_PAGE(PageTail(page), page);
590 VM_BUG_ON_PAGE(index != round_down(index, nr), page);
Sasha Levin309381fea2014-01-23 15:52:54 -0800591 VM_BUG_ON_PAGE(!PageLocked(page), page);
592 VM_BUG_ON_PAGE(!PageSwapBacked(page), page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700593 VM_BUG_ON(expected && PageTransHuge(page));
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700594
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700595 page_ref_add(page, nr);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700596 page->mapping = mapping;
597 page->index = index;
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700598
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700599 xa_lock_irq(&mapping->i_pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700600 if (PageTransHuge(page)) {
601 void __rcu **results;
602 pgoff_t idx;
603 int i;
604
605 error = 0;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700606 if (radix_tree_gang_lookup_slot(&mapping->i_pages,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700607 &results, &idx, index, 1) &&
608 idx < index + HPAGE_PMD_NR) {
609 error = -EEXIST;
610 }
611
612 if (!error) {
613 for (i = 0; i < HPAGE_PMD_NR; i++) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700614 error = radix_tree_insert(&mapping->i_pages,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700615 index + i, page + i);
616 VM_BUG_ON(error);
617 }
618 count_vm_event(THP_FILE_ALLOC);
619 }
620 } else if (!expected) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700621 error = radix_tree_insert(&mapping->i_pages, index, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700622 } else {
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500623 error = shmem_replace_entry(mapping, index, expected, page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700624 }
625
Hugh Dickinsb065b432012-07-11 14:02:48 -0700626 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700627 mapping->nrpages += nr;
628 if (PageTransHuge(page))
Mel Gorman11fb9982016-07-28 15:46:20 -0700629 __inc_node_page_state(page, NR_SHMEM_THPS);
630 __mod_node_page_state(page_pgdat(page), NR_FILE_PAGES, nr);
631 __mod_node_page_state(page_pgdat(page), NR_SHMEM, nr);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700632 xa_unlock_irq(&mapping->i_pages);
Hugh Dickinsb065b432012-07-11 14:02:48 -0700633 } else {
634 page->mapping = NULL;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700635 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700636 page_ref_sub(page, nr);
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700637 }
Hugh Dickins46f65ec2011-08-03 16:21:23 -0700638 return error;
639}
640
641/*
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700642 * Like delete_from_page_cache, but substitutes swap for page.
643 */
644static void shmem_delete_from_page_cache(struct page *page, void *radswap)
645{
646 struct address_space *mapping = page->mapping;
647 int error;
648
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700649 VM_BUG_ON_PAGE(PageCompound(page), page);
650
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700651 xa_lock_irq(&mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -0500652 error = shmem_replace_entry(mapping, page->index, page, radswap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700653 page->mapping = NULL;
654 mapping->nrpages--;
Mel Gorman11fb9982016-07-28 15:46:20 -0700655 __dec_node_page_state(page, NR_FILE_PAGES);
656 __dec_node_page_state(page, NR_SHMEM);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700657 xa_unlock_irq(&mapping->i_pages);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300658 put_page(page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -0700659 BUG_ON(error);
660}
661
662/*
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700663 * Remove swap entry from radix tree, free the swap and its page cache.
664 */
665static int shmem_free_swap(struct address_space *mapping,
666 pgoff_t index, void *radswap)
667{
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700668 void *old;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700669
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700670 xa_lock_irq(&mapping->i_pages);
671 old = radix_tree_delete_item(&mapping->i_pages, index, radswap);
672 xa_unlock_irq(&mapping->i_pages);
Johannes Weiner6dbaf222014-04-03 14:47:41 -0700673 if (old != radswap)
674 return -ENOENT;
675 free_swap_and_cache(radix_to_swp_entry(radswap));
676 return 0;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700677}
678
679/*
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800680 * Determine (in bytes) how many of the shmem object's pages mapped by the
Vlastimil Babka48131e02016-01-14 15:19:23 -0800681 * given offsets are swapped out.
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800682 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700683 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800684 * as long as the inode doesn't go away and racy results are not a problem.
685 */
Vlastimil Babka48131e02016-01-14 15:19:23 -0800686unsigned long shmem_partial_swap_usage(struct address_space *mapping,
687 pgoff_t start, pgoff_t end)
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800688{
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800689 struct radix_tree_iter iter;
Mike Kravetz5b9c98f2018-06-07 17:05:53 -0700690 void __rcu **slot;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800691 struct page *page;
Vlastimil Babka48131e02016-01-14 15:19:23 -0800692 unsigned long swapped = 0;
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800693
694 rcu_read_lock();
695
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700696 radix_tree_for_each_slot(slot, &mapping->i_pages, &iter, start) {
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800697 if (iter.index >= end)
698 break;
699
700 page = radix_tree_deref_slot(slot);
701
Matthew Wilcox2cf938a2016-03-17 14:22:03 -0700702 if (radix_tree_deref_retry(page)) {
703 slot = radix_tree_iter_retry(&iter);
704 continue;
705 }
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800706
Matthew Wilcox3159f942017-11-03 13:30:42 -0400707 if (xa_is_value(page))
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800708 swapped++;
709
710 if (need_resched()) {
Matthew Wilcox148deab2016-12-14 15:08:49 -0800711 slot = radix_tree_iter_resume(slot, &iter);
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800712 cond_resched_rcu();
Vlastimil Babka6a15a372016-01-14 15:19:20 -0800713 }
714 }
715
716 rcu_read_unlock();
717
718 return swapped << PAGE_SHIFT;
719}
720
721/*
Vlastimil Babka48131e02016-01-14 15:19:23 -0800722 * Determine (in bytes) how many of the shmem object's pages mapped by the
723 * given vma is swapped out.
724 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700725 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
Vlastimil Babka48131e02016-01-14 15:19:23 -0800726 * as long as the inode doesn't go away and racy results are not a problem.
727 */
728unsigned long shmem_swap_usage(struct vm_area_struct *vma)
729{
730 struct inode *inode = file_inode(vma->vm_file);
731 struct shmem_inode_info *info = SHMEM_I(inode);
732 struct address_space *mapping = inode->i_mapping;
733 unsigned long swapped;
734
735 /* Be careful as we don't hold info->lock */
736 swapped = READ_ONCE(info->swapped);
737
738 /*
739 * The easier cases are when the shmem object has nothing in swap, or
740 * the vma maps it whole. Then we can simply use the stats that we
741 * already track.
742 */
743 if (!swapped)
744 return 0;
745
746 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size)
747 return swapped << PAGE_SHIFT;
748
749 /* Here comes the more involved part */
750 return shmem_partial_swap_usage(mapping,
751 linear_page_index(vma, vma->vm_start),
752 linear_page_index(vma, vma->vm_end));
753}
754
755/*
Hugh Dickins24513262012-01-20 14:34:21 -0800756 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
757 */
758void shmem_unlock_mapping(struct address_space *mapping)
759{
760 struct pagevec pvec;
761 pgoff_t indices[PAGEVEC_SIZE];
762 pgoff_t index = 0;
763
Mel Gorman86679822017-11-15 17:37:52 -0800764 pagevec_init(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800765 /*
766 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
767 */
768 while (!mapping_unevictable(mapping)) {
769 /*
770 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
771 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
772 */
Johannes Weiner0cd61442014-04-03 14:47:46 -0700773 pvec.nr = find_get_entries(mapping, index,
774 PAGEVEC_SIZE, pvec.pages, indices);
Hugh Dickins24513262012-01-20 14:34:21 -0800775 if (!pvec.nr)
776 break;
777 index = indices[pvec.nr - 1] + 1;
Johannes Weiner0cd61442014-04-03 14:47:46 -0700778 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800779 check_move_unevictable_pages(pvec.pages, pvec.nr);
780 pagevec_release(&pvec);
781 cond_resched();
782 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700783}
784
785/*
786 * Remove range of pages and swap entries from radix tree, and free them.
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700787 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700788 */
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700789static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
790 bool unfalloc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Hugh Dickins285b2c42011-08-03 16:21:20 -0700792 struct address_space *mapping = inode->i_mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300794 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
795 pgoff_t end = (lend + 1) >> PAGE_SHIFT;
796 unsigned int partial_start = lstart & (PAGE_SIZE - 1);
797 unsigned int partial_end = (lend + 1) & (PAGE_SIZE - 1);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700798 struct pagevec pvec;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700799 pgoff_t indices[PAGEVEC_SIZE];
800 long nr_swaps_freed = 0;
Hugh Dickins285b2c42011-08-03 16:21:20 -0700801 pgoff_t index;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700802 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700804 if (lend == -1)
805 end = -1; /* unsigned, so actually very big */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700806
Mel Gorman86679822017-11-15 17:37:52 -0800807 pagevec_init(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700808 index = start;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700809 while (index < end) {
Johannes Weiner0cd61442014-04-03 14:47:46 -0700810 pvec.nr = find_get_entries(mapping, index,
811 min(end - index, (pgoff_t)PAGEVEC_SIZE),
812 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700813 if (!pvec.nr)
814 break;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700815 for (i = 0; i < pagevec_count(&pvec); i++) {
816 struct page *page = pvec.pages[i];
817
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700818 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700819 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700820 break;
821
Matthew Wilcox3159f942017-11-03 13:30:42 -0400822 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700823 if (unfalloc)
824 continue;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700825 nr_swaps_freed += !shmem_free_swap(mapping,
826 index, page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700827 continue;
828 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700829
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700830 VM_BUG_ON_PAGE(page_to_pgoff(page) != index, page);
831
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700832 if (!trylock_page(page))
833 continue;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700834
835 if (PageTransTail(page)) {
836 /* Middle of THP: zero out the page */
837 clear_highpage(page);
838 unlock_page(page);
839 continue;
840 } else if (PageTransHuge(page)) {
841 if (index == round_down(end, HPAGE_PMD_NR)) {
842 /*
843 * Range ends in the middle of THP:
844 * zero out the page
845 */
846 clear_highpage(page);
847 unlock_page(page);
848 continue;
849 }
850 index += HPAGE_PMD_NR - 1;
851 i += HPAGE_PMD_NR - 1;
852 }
853
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700854 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700855 VM_BUG_ON_PAGE(PageTail(page), page);
856 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800857 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700858 truncate_inode_page(mapping, page);
859 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700860 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700861 unlock_page(page);
862 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700863 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800864 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700865 cond_resched();
866 index++;
867 }
868
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700869 if (partial_start) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700870 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700871 shmem_getpage(inode, start - 1, &page, SGP_READ);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700872 if (page) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300873 unsigned int top = PAGE_SIZE;
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700874 if (start > end) {
875 top = partial_end;
876 partial_end = 0;
877 }
878 zero_user_segment(page, partial_start, top);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700879 set_page_dirty(page);
880 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300881 put_page(page);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700882 }
883 }
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700884 if (partial_end) {
885 struct page *page = NULL;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -0700886 shmem_getpage(inode, end, &page, SGP_READ);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700887 if (page) {
888 zero_user_segment(page, 0, partial_end);
889 set_page_dirty(page);
890 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300891 put_page(page);
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700892 }
893 }
894 if (start >= end)
895 return;
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700896
897 index = start;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700898 while (index < end) {
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700899 cond_resched();
Johannes Weiner0cd61442014-04-03 14:47:46 -0700900
901 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700902 min(end - index, (pgoff_t)PAGEVEC_SIZE),
Johannes Weiner0cd61442014-04-03 14:47:46 -0700903 pvec.pages, indices);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700904 if (!pvec.nr) {
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700905 /* If all gone or hole-punch or unfalloc, we're done */
906 if (index == start || end != -1)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700907 break;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700908 /* But if truncating, restart to make sure all gone */
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700909 index = start;
910 continue;
911 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700912 for (i = 0; i < pagevec_count(&pvec); i++) {
913 struct page *page = pvec.pages[i];
914
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700915 index = indices[i];
Hugh Dickins83e4fa92012-05-29 15:06:40 -0700916 if (index >= end)
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700917 break;
918
Matthew Wilcox3159f942017-11-03 13:30:42 -0400919 if (xa_is_value(page)) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700920 if (unfalloc)
921 continue;
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700922 if (shmem_free_swap(mapping, index, page)) {
923 /* Swap was replaced by page: retry */
924 index--;
925 break;
926 }
927 nr_swaps_freed++;
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700928 continue;
929 }
930
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700931 lock_page(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700932
933 if (PageTransTail(page)) {
934 /* Middle of THP: zero out the page */
935 clear_highpage(page);
936 unlock_page(page);
937 /*
938 * Partial thp truncate due 'start' in middle
939 * of THP: don't need to look on these pages
940 * again on !pvec.nr restart.
941 */
942 if (index != round_down(end, HPAGE_PMD_NR))
943 start++;
944 continue;
945 } else if (PageTransHuge(page)) {
946 if (index == round_down(end, HPAGE_PMD_NR)) {
947 /*
948 * Range ends in the middle of THP:
949 * zero out the page
950 */
951 clear_highpage(page);
952 unlock_page(page);
953 continue;
954 }
955 index += HPAGE_PMD_NR - 1;
956 i += HPAGE_PMD_NR - 1;
957 }
958
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700959 if (!unfalloc || !PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -0700960 VM_BUG_ON_PAGE(PageTail(page), page);
961 if (page_mapping(page) == mapping) {
Sasha Levin309381fea2014-01-23 15:52:54 -0800962 VM_BUG_ON_PAGE(PageWriteback(page), page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700963 truncate_inode_page(mapping, page);
Hugh Dickinsb1a36652014-07-23 14:00:13 -0700964 } else {
965 /* Page was replaced by swap: retry */
966 unlock_page(page);
967 index--;
968 break;
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700969 }
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700970 }
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700971 unlock_page(page);
972 }
Johannes Weiner0cd61442014-04-03 14:47:46 -0700973 pagevec_remove_exceptionals(&pvec);
Hugh Dickins24513262012-01-20 14:34:21 -0800974 pagevec_release(&pvec);
Hugh Dickinsbda97ea2011-08-03 16:21:21 -0700975 index++;
976 }
Hugh Dickins94c1e622011-06-27 16:18:03 -0700977
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700978 spin_lock_irq(&info->lock);
Hugh Dickins7a5d0fb2011-08-03 16:21:22 -0700979 info->swapped -= nr_swaps_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700981 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700982}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Hugh Dickins1635f6a2012-05-29 15:06:42 -0700984void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
985{
986 shmem_undo_range(inode, lstart, lend, false);
Deepa Dinamani078cd822016-09-14 07:48:04 -0700987 inode->i_ctime = inode->i_mtime = current_time(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988}
Hugh Dickins94c1e622011-06-27 16:18:03 -0700989EXPORT_SYMBOL_GPL(shmem_truncate_range);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
David Howellsa528d352017-01-31 16:46:22 +0000991static int shmem_getattr(const struct path *path, struct kstat *stat,
992 u32 request_mask, unsigned int query_flags)
Yu Zhao44a30222015-09-08 15:03:33 -0700993{
David Howellsa528d352017-01-31 16:46:22 +0000994 struct inode *inode = path->dentry->d_inode;
Yu Zhao44a30222015-09-08 15:03:33 -0700995 struct shmem_inode_info *info = SHMEM_I(inode);
Yang Shi89fdcd22018-06-07 17:06:59 -0700996 struct shmem_sb_info *sb_info = SHMEM_SB(inode->i_sb);
Yu Zhao44a30222015-09-08 15:03:33 -0700997
Hugh Dickinsd0424c42015-11-05 18:50:34 -0800998 if (info->alloced - info->swapped != inode->i_mapping->nrpages) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -0700999 spin_lock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001000 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001001 spin_unlock_irq(&info->lock);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001002 }
Yu Zhao44a30222015-09-08 15:03:33 -07001003 generic_fillattr(inode, stat);
Yang Shi89fdcd22018-06-07 17:06:59 -07001004
1005 if (is_huge_enabled(sb_info))
1006 stat->blksize = HPAGE_PMD_SIZE;
1007
Yu Zhao44a30222015-09-08 15:03:33 -07001008 return 0;
1009}
1010
Hugh Dickins94c1e622011-06-27 16:18:03 -07001011static int shmem_setattr(struct dentry *dentry, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
David Howells75c3cfa2015-03-17 22:26:12 +00001013 struct inode *inode = d_inode(dentry);
David Herrmann40e041a2014-08-08 14:25:27 -07001014 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001015 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int error;
1017
Jan Kara31051c82016-05-26 16:55:18 +02001018 error = setattr_prepare(dentry, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001019 if (error)
1020 return error;
1021
Hugh Dickins94c1e622011-06-27 16:18:03 -07001022 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
1023 loff_t oldsize = inode->i_size;
1024 loff_t newsize = attr->ia_size;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001025
David Herrmann40e041a2014-08-08 14:25:27 -07001026 /* protected by i_mutex */
1027 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
1028 (newsize > oldsize && (info->seals & F_SEAL_GROW)))
1029 return -EPERM;
1030
Hugh Dickins94c1e622011-06-27 16:18:03 -07001031 if (newsize != oldsize) {
Konstantin Khlebnikov77142512014-08-06 16:06:34 -07001032 error = shmem_reacct_size(SHMEM_I(inode)->flags,
1033 oldsize, newsize);
1034 if (error)
1035 return error;
Hugh Dickins94c1e622011-06-27 16:18:03 -07001036 i_size_write(inode, newsize);
Deepa Dinamani078cd822016-09-14 07:48:04 -07001037 inode->i_ctime = inode->i_mtime = current_time(inode);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001038 }
Josef Bacikafa2db22015-06-24 16:58:45 -07001039 if (newsize <= oldsize) {
Hugh Dickins94c1e622011-06-27 16:18:03 -07001040 loff_t holebegin = round_up(newsize, PAGE_SIZE);
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001041 if (oldsize > holebegin)
1042 unmap_mapping_range(inode->i_mapping,
1043 holebegin, 0, 1);
1044 if (info->alloced)
1045 shmem_truncate_range(inode,
1046 newsize, (loff_t)-1);
Hugh Dickins94c1e622011-06-27 16:18:03 -07001047 /* unmap again to remove racily COWed private pages */
Hugh Dickinsd0424c42015-11-05 18:50:34 -08001048 if (oldsize > holebegin)
1049 unmap_mapping_range(inode->i_mapping,
1050 holebegin, 0, 1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001051
1052 /*
1053 * Part of the huge page can be beyond i_size: subject
1054 * to shrink under memory pressure.
1055 */
1056 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE)) {
1057 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001058 /*
1059 * _careful to defend against unlocked access to
1060 * ->shrink_list in shmem_unused_huge_shrink()
1061 */
1062 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001063 list_add_tail(&info->shrinklist,
1064 &sbinfo->shrinklist);
1065 sbinfo->shrinklist_len++;
1066 }
1067 spin_unlock(&sbinfo->shrinklist_lock);
1068 }
Hugh Dickins94c1e622011-06-27 16:18:03 -07001069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001072 setattr_copy(inode, attr);
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001073 if (attr->ia_valid & ATTR_MODE)
Christoph Hellwigfeda8212013-12-20 05:16:54 -08001074 error = posix_acl_chmod(inode, inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return error;
1076}
1077
Al Viro1f895f72010-06-05 19:10:41 -04001078static void shmem_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001081 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001083 if (inode->i_mapping->a_ops == &shmem_aops) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 shmem_unacct_size(info->flags, inode->i_size);
1085 inode->i_size = 0;
npiggin@suse.de3889e6e2010-05-27 01:05:36 +10001086 shmem_truncate_range(inode, 0, (loff_t)-1);
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001087 if (!list_empty(&info->shrinklist)) {
1088 spin_lock(&sbinfo->shrinklist_lock);
1089 if (!list_empty(&info->shrinklist)) {
1090 list_del_init(&info->shrinklist);
1091 sbinfo->shrinklist_len--;
1092 }
1093 spin_unlock(&sbinfo->shrinklist_lock);
1094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (!list_empty(&info->swaplist)) {
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001096 mutex_lock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001098 mutex_unlock(&shmem_swaplist_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Al Viro3ed47db2016-01-22 18:08:52 -05001100 }
Eric Parisb09e0fa2011-05-24 17:12:39 -07001101
Aristeu Rozanski38f38652012-08-23 16:53:28 -04001102 simple_xattrs_free(&info->xattrs);
Hugh Dickins0f3c42f2012-11-16 14:15:04 -08001103 WARN_ON(inode->i_blocks);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08001104 shmem_free_inode(inode->i_sb);
Jan Karadbd57682012-05-03 14:48:02 +02001105 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Matthew Wilcox478922e2016-12-14 15:08:52 -08001108static unsigned long find_swap_entry(struct radix_tree_root *root, void *item)
1109{
1110 struct radix_tree_iter iter;
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001111 void __rcu **slot;
Matthew Wilcox478922e2016-12-14 15:08:52 -08001112 unsigned long found = -1;
1113 unsigned int checked = 0;
1114
1115 rcu_read_lock();
1116 radix_tree_for_each_slot(slot, root, &iter, 0) {
Mike Kravetz5b9c98f2018-06-07 17:05:53 -07001117 void *entry = radix_tree_deref_slot(slot);
1118
1119 if (radix_tree_deref_retry(entry)) {
1120 slot = radix_tree_iter_retry(&iter);
1121 continue;
1122 }
1123 if (entry == item) {
Matthew Wilcox478922e2016-12-14 15:08:52 -08001124 found = iter.index;
1125 break;
1126 }
1127 checked++;
1128 if ((checked % 4096) != 0)
1129 continue;
1130 slot = radix_tree_iter_resume(slot, &iter);
1131 cond_resched_rcu();
1132 }
1133
1134 rcu_read_unlock();
1135 return found;
1136}
1137
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001138/*
1139 * If swap found in inode, free it and move page from swapcache to filecache.
1140 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001141static int shmem_unuse_inode(struct shmem_inode_info *info,
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001142 swp_entry_t swap, struct page **pagep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Hugh Dickins285b2c42011-08-03 16:21:20 -07001144 struct address_space *mapping = info->vfs_inode.i_mapping;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001145 void *radswap;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001146 pgoff_t index;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001147 gfp_t gfp;
1148 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001150 radswap = swp_to_radix_entry(swap);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001151 index = find_swap_entry(&mapping->i_pages, radswap);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001152 if (index == -1)
Johannes Weiner00501b52014-08-08 14:19:20 -07001153 return -EAGAIN; /* tell shmem_unuse we found nothing */
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001154
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001155 /*
1156 * Move _head_ to start search for next from here.
Al Viro1f895f72010-06-05 19:10:41 -04001157 * But be careful: shmem_evict_inode checks list_empty without taking
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001158 * mutex, and there's an instant in list_move_tail when info->swaplist
Hugh Dickins285b2c42011-08-03 16:21:20 -07001159 * would appear empty, if it were the only one on shmem_swaplist.
Hugh Dickins1b1b32f2008-02-04 22:28:55 -08001160 */
1161 if (shmem_swaplist.next != &info->swaplist)
1162 list_move_tail(&shmem_swaplist, &info->swaplist);
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001163
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001164 gfp = mapping_gfp_mask(mapping);
1165 if (shmem_should_replace_page(*pagep, gfp)) {
1166 mutex_unlock(&shmem_swaplist_mutex);
1167 error = shmem_replace_page(pagep, gfp, info, index);
1168 mutex_lock(&shmem_swaplist_mutex);
1169 /*
1170 * We needed to drop mutex to make that restrictive page
Hugh Dickins0142ef62012-06-07 14:21:09 -07001171 * allocation, but the inode might have been freed while we
1172 * dropped it: although a racing shmem_evict_inode() cannot
1173 * complete without emptying the radix_tree, our page lock
1174 * on this swapcache page is not enough to prevent that -
1175 * free_swap_and_cache() of our swap entry will only
1176 * trylock_page(), removing swap from radix_tree whatever.
1177 *
1178 * We must not proceed to shmem_add_to_page_cache() if the
1179 * inode has been freed, but of course we cannot rely on
1180 * inode or mapping or info to check that. However, we can
1181 * safely check if our swap entry is still in use (and here
1182 * it can't have got reused for another page): if it's still
1183 * in use, then the inode cannot have been freed yet, and we
1184 * can safely proceed (if it's no longer in use, that tells
1185 * nothing about the inode, but we don't need to unuse swap).
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001186 */
1187 if (!page_swapcount(*pagep))
1188 error = -ENOENT;
1189 }
1190
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001191 /*
Hugh Dickins778dd892011-05-11 15:13:37 -07001192 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1193 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1194 * beneath us (pagelock doesn't help until the page is in pagecache).
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001195 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001196 if (!error)
1197 error = shmem_add_to_page_cache(*pagep, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001198 radswap);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001199 if (error != -ENOMEM) {
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001200 /*
1201 * Truncation and eviction use free_swap_and_cache(), which
1202 * only does trylock page: if we raced, best clean up here.
1203 */
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001204 delete_from_swap_cache(*pagep);
1205 set_page_dirty(*pagep);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001206 if (!error) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001207 spin_lock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001208 info->swapped--;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001209 spin_unlock_irq(&info->lock);
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001210 swap_free(swap);
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
Hugh Dickins2e0e26c2008-02-04 22:28:53 -08001213 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
1216/*
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001217 * Search through swapped inodes to find and replace swap by page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001219int shmem_unuse(swp_entry_t swap, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001221 struct list_head *this, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct shmem_inode_info *info;
Johannes Weiner00501b52014-08-08 14:19:20 -07001223 struct mem_cgroup *memcg;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001224 int error = 0;
1225
1226 /*
1227 * There's a faint possibility that swap page was replaced before
Hugh Dickins0142ef62012-06-07 14:21:09 -07001228 * caller locked it: caller will come back later with the right page.
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001229 */
Hugh Dickins0142ef62012-06-07 14:21:09 -07001230 if (unlikely(!PageSwapCache(page) || page_private(page) != swap.val))
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001231 goto out;
Hugh Dickins778dd892011-05-11 15:13:37 -07001232
1233 /*
1234 * Charge page using GFP_KERNEL while we can wait, before taking
1235 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1236 * Charged back to the user (not to caller) when swap account is used.
Hugh Dickins778dd892011-05-11 15:13:37 -07001237 */
Tejun Heo2cf85582018-07-03 11:14:56 -04001238 error = mem_cgroup_try_charge_delay(page, current->mm, GFP_KERNEL,
1239 &memcg, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001240 if (error)
1241 goto out;
Hugh Dickins46f65ec2011-08-03 16:21:23 -07001242 /* No radix_tree_preload: swap entry keeps a place for page in tree */
Johannes Weiner00501b52014-08-08 14:19:20 -07001243 error = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001245 mutex_lock(&shmem_swaplist_mutex);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001246 list_for_each_safe(this, next, &shmem_swaplist) {
1247 info = list_entry(this, struct shmem_inode_info, swaplist);
Hugh Dickins285b2c42011-08-03 16:21:20 -07001248 if (info->swapped)
Johannes Weiner00501b52014-08-08 14:19:20 -07001249 error = shmem_unuse_inode(info, swap, &page);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001250 else
1251 list_del_init(&info->swaplist);
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001252 cond_resched();
Johannes Weiner00501b52014-08-08 14:19:20 -07001253 if (error != -EAGAIN)
Hugh Dickins778dd892011-05-11 15:13:37 -07001254 break;
Johannes Weiner00501b52014-08-08 14:19:20 -07001255 /* found nothing in this: move on to search the next */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
Hugh Dickinscb5f7b92008-02-04 22:28:52 -08001257 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickins778dd892011-05-11 15:13:37 -07001258
Johannes Weiner00501b52014-08-08 14:19:20 -07001259 if (error) {
1260 if (error != -ENOMEM)
1261 error = 0;
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001262 mem_cgroup_cancel_charge(page, memcg, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001263 } else
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001264 mem_cgroup_commit_charge(page, memcg, true, false);
Hugh Dickins778dd892011-05-11 15:13:37 -07001265out:
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001266 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001267 put_page(page);
Hugh Dickins778dd892011-05-11 15:13:37 -07001268 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
1271/*
1272 * Move the page from the page cache to the swap cache.
1273 */
1274static int shmem_writepage(struct page *page, struct writeback_control *wbc)
1275{
1276 struct shmem_inode_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 struct address_space *mapping;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 struct inode *inode;
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001279 swp_entry_t swap;
1280 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001282 VM_BUG_ON_PAGE(PageCompound(page), page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 BUG_ON(!PageLocked(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 mapping = page->mapping;
1285 index = page->index;
1286 inode = mapping->host;
1287 info = SHMEM_I(inode);
1288 if (info->flags & VM_LOCKED)
1289 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001290 if (!total_swap_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto redirty;
1292
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001293 /*
Christoph Hellwig97b713b2015-01-14 10:42:31 +01001294 * Our capabilities prevent regular writeback or sync from ever calling
1295 * shmem_writepage; but a stacking filesystem might use ->writepage of
1296 * its underlying filesystem, in which case tmpfs should write out to
1297 * swap only in response to memory pressure, and not for the writeback
1298 * threads or sync.
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001299 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001300 if (!wbc->for_reclaim) {
1301 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1302 goto redirty;
1303 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001304
1305 /*
1306 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1307 * value into swapfile.c, the only way we can correctly account for a
1308 * fallocated page arriving here is now to initialize it and write it.
Hugh Dickins1aac1402012-05-29 15:06:42 -07001309 *
1310 * That's okay for a page already fallocated earlier, but if we have
1311 * not yet completed the fallocation, then (a) we want to keep track
1312 * of this page in case we have to undo it, and (b) it may not be a
1313 * good idea to continue anyway, once we're pushing into swap. So
1314 * reactivate the page, and let shmem_fallocate() quit when too many.
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001315 */
1316 if (!PageUptodate(page)) {
Hugh Dickins1aac1402012-05-29 15:06:42 -07001317 if (inode->i_private) {
1318 struct shmem_falloc *shmem_falloc;
1319 spin_lock(&inode->i_lock);
1320 shmem_falloc = inode->i_private;
1321 if (shmem_falloc &&
Hugh Dickins8e205f72014-07-23 14:00:10 -07001322 !shmem_falloc->waitq &&
Hugh Dickins1aac1402012-05-29 15:06:42 -07001323 index >= shmem_falloc->start &&
1324 index < shmem_falloc->next)
1325 shmem_falloc->nr_unswapped++;
1326 else
1327 shmem_falloc = NULL;
1328 spin_unlock(&inode->i_lock);
1329 if (shmem_falloc)
1330 goto redirty;
1331 }
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001332 clear_highpage(page);
1333 flush_dcache_page(page);
1334 SetPageUptodate(page);
1335 }
1336
Huang Ying38d8b4e2017-07-06 15:37:18 -07001337 swap = get_swap_page(page);
Hugh Dickins48f170f2011-07-25 17:12:37 -07001338 if (!swap.val)
1339 goto redirty;
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001340
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001341 /*
1342 * Add inode to shmem_unuse()'s list of swapped-out inodes,
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001343 * if it's not already there. Do it now before the page is
1344 * moved to swap cache, when its pagelock no longer protects
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001345 * the inode from eviction. But don't unlock the mutex until
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001346 * we've incremented swapped, because shmem_unuse_inode() will
1347 * prune a !swapped inode from the swaplist under this mutex.
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001348 */
Hugh Dickins48f170f2011-07-25 17:12:37 -07001349 mutex_lock(&shmem_swaplist_mutex);
1350 if (list_empty(&info->swaplist))
1351 list_add_tail(&info->swaplist, &shmem_swaplist);
Hugh Dickinsb1dea802011-05-11 15:13:36 -07001352
Hugh Dickins48f170f2011-07-25 17:12:37 -07001353 if (add_to_swap_cache(page, swap, GFP_ATOMIC) == 0) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001354 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001355 shmem_recalc_inode(inode);
1356 info->swapped++;
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001357 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001358
Hugh Dickinsaaa46862009-12-14 17:58:47 -08001359 swap_shmem_alloc(swap);
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001360 shmem_delete_from_page_cache(page, swp_to_radix_entry(swap));
1361
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001362 mutex_unlock(&shmem_swaplist_mutex);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001363 BUG_ON(page_mapped(page));
Hugh Dickins9fab5612009-03-31 15:23:33 -07001364 swap_writepage(page, wbc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366 }
1367
Hugh Dickins6922c0c2011-08-03 16:21:25 -07001368 mutex_unlock(&shmem_swaplist_mutex);
Minchan Kim75f6d6d2017-07-06 15:37:21 -07001369 put_swap_page(page, swap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370redirty:
1371 set_page_dirty(page);
Hugh Dickinsd9fe5262008-02-04 22:28:51 -08001372 if (wbc->for_reclaim)
1373 return AOP_WRITEPAGE_ACTIVATE; /* Return with page locked */
1374 unlock_page(page);
1375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Hugh Dickins75edd342016-05-19 17:12:44 -07001378#if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001379static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001380{
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001381 char buffer[64];
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001382
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001383 if (!mpol || mpol->mode == MPOL_DEFAULT)
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001384 return; /* show nothing */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001385
Hugh Dickinsa7a88b22013-01-02 02:04:23 -08001386 mpol_to_str(buffer, sizeof(buffer), mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001387
Lee Schermerhorn095f1fc2008-04-28 02:13:23 -07001388 seq_printf(seq, ",mpol=%s", buffer);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001389}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001390
1391static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1392{
1393 struct mempolicy *mpol = NULL;
1394 if (sbinfo->mpol) {
1395 spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */
1396 mpol = sbinfo->mpol;
1397 mpol_get(mpol);
1398 spin_unlock(&sbinfo->stat_lock);
1399 }
1400 return mpol;
1401}
Hugh Dickins75edd342016-05-19 17:12:44 -07001402#else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1403static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol)
1404{
1405}
1406static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo)
1407{
1408 return NULL;
1409}
1410#endif /* CONFIG_NUMA && CONFIG_TMPFS */
1411#ifndef CONFIG_NUMA
1412#define vm_policy vm_private_data
1413#endif
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08001414
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001415static void shmem_pseudo_vma_init(struct vm_area_struct *vma,
1416 struct shmem_inode_info *info, pgoff_t index)
1417{
1418 /* Create a pseudo vma that just contains the policy */
Kirill A. Shutemov2c4541e2018-07-26 16:37:30 -07001419 vma_init(vma, NULL);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001420 /* Bias interleave by inode number to distribute better across nodes */
1421 vma->vm_pgoff = index + info->vfs_inode.i_ino;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001422 vma->vm_policy = mpol_shared_policy_lookup(&info->policy, index);
1423}
1424
1425static void shmem_pseudo_vma_destroy(struct vm_area_struct *vma)
1426{
1427 /* Drop reference taken by mpol_shared_policy_lookup() */
1428 mpol_cond_put(vma->vm_policy);
1429}
1430
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001431static struct page *shmem_swapin(swp_entry_t swap, gfp_t gfp,
1432 struct shmem_inode_info *info, pgoff_t index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 struct vm_area_struct pvma;
Mel Gorman18a2f372012-12-05 14:01:41 -08001435 struct page *page;
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001436 struct vm_fault vmf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001438 shmem_pseudo_vma_init(&pvma, info, index);
Minchan Kime9e9b7e2018-04-05 16:23:42 -07001439 vmf.vma = &pvma;
1440 vmf.address = 0;
1441 page = swap_cluster_readahead(swap, gfp, &vmf);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001442 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001443
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001444 return page;
1445}
Mel Gorman18a2f372012-12-05 14:01:41 -08001446
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001447static struct page *shmem_alloc_hugepage(gfp_t gfp,
1448 struct shmem_inode_info *info, pgoff_t index)
1449{
1450 struct vm_area_struct pvma;
1451 struct inode *inode = &info->vfs_inode;
1452 struct address_space *mapping = inode->i_mapping;
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001453 pgoff_t idx, hindex;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001454 void __rcu **results;
1455 struct page *page;
1456
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001457 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001458 return NULL;
1459
Geert Uytterhoeven4620a062016-08-03 19:58:19 +02001460 hindex = round_down(index, HPAGE_PMD_NR);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001461 rcu_read_lock();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001462 if (radix_tree_gang_lookup_slot(&mapping->i_pages, &results, &idx,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001463 hindex, 1) && idx < hindex + HPAGE_PMD_NR) {
1464 rcu_read_unlock();
1465 return NULL;
1466 }
1467 rcu_read_unlock();
1468
1469 shmem_pseudo_vma_init(&pvma, info, hindex);
1470 page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
1471 HPAGE_PMD_ORDER, &pvma, 0, numa_node_id(), true);
1472 shmem_pseudo_vma_destroy(&pvma);
1473 if (page)
1474 prep_transhuge_page(page);
Mel Gorman18a2f372012-12-05 14:01:41 -08001475 return page;
1476}
1477
1478static struct page *shmem_alloc_page(gfp_t gfp,
1479 struct shmem_inode_info *info, pgoff_t index)
1480{
1481 struct vm_area_struct pvma;
1482 struct page *page;
1483
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001484 shmem_pseudo_vma_init(&pvma, info, index);
1485 page = alloc_page_vma(gfp, &pvma, 0);
1486 shmem_pseudo_vma_destroy(&pvma);
Mel Gorman18a2f372012-12-05 14:01:41 -08001487
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001488 return page;
1489}
1490
1491static struct page *shmem_alloc_and_acct_page(gfp_t gfp,
Mike Rapoport0f079692017-09-06 16:22:59 -07001492 struct inode *inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001493 pgoff_t index, bool huge)
1494{
Mike Rapoport0f079692017-09-06 16:22:59 -07001495 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001496 struct page *page;
1497 int nr;
1498 int err = -ENOSPC;
1499
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07001500 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001501 huge = false;
1502 nr = huge ? HPAGE_PMD_NR : 1;
1503
Mike Rapoport0f079692017-09-06 16:22:59 -07001504 if (!shmem_inode_acct_block(inode, nr))
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001505 goto failed;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001506
1507 if (huge)
1508 page = shmem_alloc_hugepage(gfp, info, index);
1509 else
1510 page = shmem_alloc_page(gfp, info, index);
Hugh Dickins75edd342016-05-19 17:12:44 -07001511 if (page) {
1512 __SetPageLocked(page);
1513 __SetPageSwapBacked(page);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001514 return page;
Hugh Dickins75edd342016-05-19 17:12:44 -07001515 }
Mel Gorman18a2f372012-12-05 14:01:41 -08001516
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001517 err = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07001518 shmem_inode_unacct_blocks(inode, nr);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001519failed:
1520 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07001522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523/*
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001524 * When a page is moved from swapcache to shmem filecache (either by the
1525 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1526 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1527 * ignorance of the mapping it belongs to. If that mapping has special
1528 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1529 * we may need to copy to a suitable page before moving to filecache.
1530 *
1531 * In a future release, this may well be extended to respect cpuset and
1532 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1533 * but for now it is a simple matter of zone.
1534 */
1535static bool shmem_should_replace_page(struct page *page, gfp_t gfp)
1536{
1537 return page_zonenum(page) > gfp_zone(gfp);
1538}
1539
1540static int shmem_replace_page(struct page **pagep, gfp_t gfp,
1541 struct shmem_inode_info *info, pgoff_t index)
1542{
1543 struct page *oldpage, *newpage;
1544 struct address_space *swap_mapping;
1545 pgoff_t swap_index;
1546 int error;
1547
1548 oldpage = *pagep;
1549 swap_index = page_private(oldpage);
1550 swap_mapping = page_mapping(oldpage);
1551
1552 /*
1553 * We have arrived here because our zones are constrained, so don't
1554 * limit chance of success by further cpuset and node constraints.
1555 */
1556 gfp &= ~GFP_CONSTRAINT_MASK;
1557 newpage = shmem_alloc_page(gfp, info, index);
1558 if (!newpage)
1559 return -ENOMEM;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001560
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001561 get_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001562 copy_highpage(newpage, oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001563 flush_dcache_page(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001564
Hugh Dickins9956edf2016-11-10 10:46:11 -08001565 __SetPageLocked(newpage);
1566 __SetPageSwapBacked(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001567 SetPageUptodate(newpage);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001568 set_page_private(newpage, swap_index);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001569 SetPageSwapCache(newpage);
1570
1571 /*
1572 * Our caller will very soon move newpage out of swapcache, but it's
1573 * a nice clean interface for us to replace oldpage by newpage there.
1574 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001575 xa_lock_irq(&swap_mapping->i_pages);
Matthew Wilcox62f945b2017-11-17 10:22:37 -05001576 error = shmem_replace_entry(swap_mapping, swap_index, oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001577 if (!error) {
Mel Gorman11fb9982016-07-28 15:46:20 -07001578 __inc_node_page_state(newpage, NR_FILE_PAGES);
1579 __dec_node_page_state(oldpage, NR_FILE_PAGES);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001580 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001581 xa_unlock_irq(&swap_mapping->i_pages);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001582
Hugh Dickins0142ef62012-06-07 14:21:09 -07001583 if (unlikely(error)) {
1584 /*
1585 * Is this possible? I think not, now that our callers check
1586 * both PageSwapCache and page_private after getting page lock;
1587 * but be defensive. Reverse old to newpage for clear and free.
1588 */
1589 oldpage = newpage;
1590 } else {
Johannes Weiner6a93ca82016-03-15 14:57:19 -07001591 mem_cgroup_migrate(oldpage, newpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001592 lru_cache_add_anon(newpage);
1593 *pagep = newpage;
1594 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001595
1596 ClearPageSwapCache(oldpage);
1597 set_page_private(oldpage, 0);
1598
1599 unlock_page(oldpage);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001600 put_page(oldpage);
1601 put_page(oldpage);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001602 return error;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001603}
1604
1605/*
Hugh Dickins68da9f02011-07-25 17:12:34 -07001606 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 *
1608 * If we allocate a new one we do not mark it dirty. That's up to the
1609 * vm. If we swap it in we mark it dirty since we also free the swap
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001610 * entry since a page cannot live in both the swap and page cache.
1611 *
1612 * fault_mm and fault_type are only supplied by shmem_fault:
1613 * otherwise they are NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001615static int shmem_getpage_gfp(struct inode *inode, pgoff_t index,
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001616 struct page **pagep, enum sgp_type sgp, gfp_t gfp,
Souptick Joarder2b740302018-08-23 17:01:36 -07001617 struct vm_area_struct *vma, struct vm_fault *vmf,
1618 vm_fault_t *fault_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619{
1620 struct address_space *mapping = inode->i_mapping;
Arnd Bergmann23f919d2016-12-12 16:42:28 -08001621 struct shmem_inode_info *info = SHMEM_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct shmem_sb_info *sbinfo;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001623 struct mm_struct *charge_mm;
Johannes Weiner00501b52014-08-08 14:19:20 -07001624 struct mem_cgroup *memcg;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001625 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 swp_entry_t swap;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001627 enum sgp_type sgp_huge = sgp;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001628 pgoff_t hindex = index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int error;
Hugh Dickins54af60422011-08-03 16:21:24 -07001630 int once = 0;
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001631 int alloced = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001633 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return -EFBIG;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001635 if (sgp == SGP_NOHUGE || sgp == SGP_HUGE)
1636 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637repeat:
Hugh Dickins54af60422011-08-03 16:21:24 -07001638 swap.val = 0;
Johannes Weiner0cd61442014-04-03 14:47:46 -07001639 page = find_lock_entry(mapping, index);
Matthew Wilcox3159f942017-11-03 13:30:42 -04001640 if (xa_is_value(page)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001641 swap = radix_to_swp_entry(page);
1642 page = NULL;
1643 }
1644
Hugh Dickins75edd342016-05-19 17:12:44 -07001645 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001646 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001647 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001648 goto unlock;
Hugh Dickins54af60422011-08-03 16:21:24 -07001649 }
1650
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001651 if (page && sgp == SGP_WRITE)
1652 mark_page_accessed(page);
1653
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001654 /* fallocated page? */
1655 if (page && !PageUptodate(page)) {
1656 if (sgp != SGP_READ)
1657 goto clear;
1658 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001659 put_page(page);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001660 page = NULL;
1661 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001662 if (page || (sgp == SGP_READ && !swap.val)) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001663 *pagep = page;
1664 return 0;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001668 * Fast cache lookup did not find it:
1669 * bring it back from swap or allocate.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001671 sbinfo = SHMEM_SB(inode->i_sb);
Mike Rapoportcfda0522017-02-22 15:43:37 -08001672 charge_mm = vma ? vma->vm_mm : current->mm;
Hugh Dickins27ab7002011-07-25 17:12:36 -07001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (swap.val) {
1675 /* Look it up and read it in.. */
Huang Yingec560172017-09-06 16:24:36 -07001676 page = lookup_swap_cache(swap, NULL, 0);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001677 if (!page) {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001678 /* Or update major stats only when swapin succeeds?? */
1679 if (fault_type) {
Hugh Dickins68da9f02011-07-25 17:12:34 -07001680 *fault_type |= VM_FAULT_MAJOR;
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001681 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001682 count_memcg_event_mm(charge_mm, PGMAJFAULT);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001683 }
1684 /* Here we actually start the io */
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07001685 page = shmem_swapin(swap, gfp, info, index);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001686 if (!page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001687 error = -ENOMEM;
1688 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
1691
1692 /* We have to do this with page locked to prevent races */
Hugh Dickins54af60422011-08-03 16:21:24 -07001693 lock_page(page);
Hugh Dickins0142ef62012-06-07 14:21:09 -07001694 if (!PageSwapCache(page) || page_private(page) != swap.val ||
Hugh Dickinsd1899222012-07-11 14:02:47 -07001695 !shmem_confirm_swap(mapping, index, swap)) {
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001696 error = -EEXIST; /* try again */
Hugh Dickinsd1899222012-07-11 14:02:47 -07001697 goto unlock;
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001698 }
Hugh Dickins27ab7002011-07-25 17:12:36 -07001699 if (!PageUptodate(page)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 error = -EIO;
Hugh Dickins54af60422011-08-03 16:21:24 -07001701 goto failed;
1702 }
1703 wait_on_page_writeback(page);
1704
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001705 if (shmem_should_replace_page(page, gfp)) {
1706 error = shmem_replace_page(&page, gfp, info, index);
1707 if (error)
1708 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710
Tejun Heo2cf85582018-07-03 11:14:56 -04001711 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001712 false);
Hugh Dickinsd1899222012-07-11 14:02:47 -07001713 if (!error) {
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07001714 error = shmem_add_to_page_cache(page, mapping, index,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001715 swp_to_radix_entry(swap));
Hugh Dickins215c02b2012-11-16 14:15:03 -08001716 /*
1717 * We already confirmed swap under page lock, and make
1718 * no memory allocation here, so usually no possibility
1719 * of error; but free_swap_and_cache() only trylocks a
1720 * page, so it is just possible that the entry has been
1721 * truncated or holepunched since swap was confirmed.
1722 * shmem_undo_range() will have done some of the
1723 * unaccounting, now delete_from_swap_cache() will do
Vladimir Davydov93aa7d92015-02-11 15:24:59 -08001724 * the rest.
Hugh Dickins215c02b2012-11-16 14:15:03 -08001725 * Reset swap.val? No, leave it so "failed" goes back to
1726 * "repeat": reading a hole and writing should succeed.
1727 */
Johannes Weiner00501b52014-08-08 14:19:20 -07001728 if (error) {
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001729 mem_cgroup_cancel_charge(page, memcg, false);
Hugh Dickins215c02b2012-11-16 14:15:03 -08001730 delete_from_swap_cache(page);
Johannes Weiner00501b52014-08-08 14:19:20 -07001731 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001732 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001733 if (error)
1734 goto failed;
1735
Kirill A. Shutemovf627c2f2016-01-15 16:52:20 -08001736 mem_cgroup_commit_charge(page, memcg, true, false);
Johannes Weiner00501b52014-08-08 14:19:20 -07001737
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001738 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001739 info->swapped--;
1740 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001741 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001742
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001743 if (sgp == SGP_WRITE)
1744 mark_page_accessed(page);
1745
Hugh Dickins27ab7002011-07-25 17:12:36 -07001746 delete_from_swap_cache(page);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001747 set_page_dirty(page);
1748 swap_free(swap);
1749
Hugh Dickins54af60422011-08-03 16:21:24 -07001750 } else {
Mike Rapoportcfda0522017-02-22 15:43:37 -08001751 if (vma && userfaultfd_missing(vma)) {
1752 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING);
1753 return 0;
1754 }
1755
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001756 /* shmem_symlink() */
1757 if (mapping->a_ops != &shmem_aops)
1758 goto alloc_nohuge;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001759 if (shmem_huge == SHMEM_HUGE_DENY || sgp_huge == SGP_NOHUGE)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001760 goto alloc_nohuge;
1761 if (shmem_huge == SHMEM_HUGE_FORCE)
1762 goto alloc_huge;
1763 switch (sbinfo->huge) {
1764 loff_t i_size;
1765 pgoff_t off;
1766 case SHMEM_HUGE_NEVER:
1767 goto alloc_nohuge;
1768 case SHMEM_HUGE_WITHIN_SIZE:
1769 off = round_up(index, HPAGE_PMD_NR);
1770 i_size = round_up(i_size_read(inode), PAGE_SIZE);
1771 if (i_size >= HPAGE_PMD_SIZE &&
1772 i_size >> PAGE_SHIFT >= off)
1773 goto alloc_huge;
1774 /* fallthrough */
1775 case SHMEM_HUGE_ADVISE:
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001776 if (sgp_huge == SGP_HUGE)
1777 goto alloc_huge;
1778 /* TODO: implement fadvise() hints */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001779 goto alloc_nohuge;
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001782alloc_huge:
Mike Rapoport0f079692017-09-06 16:22:59 -07001783 page = shmem_alloc_and_acct_page(gfp, inode, index, true);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001784 if (IS_ERR(page)) {
Mike Rapoport0f079692017-09-06 16:22:59 -07001785alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001786 index, false);
Hugh Dickins54af60422011-08-03 16:21:24 -07001787 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001788 if (IS_ERR(page)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001789 int retry = 5;
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001790 error = PTR_ERR(page);
1791 page = NULL;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001792 if (error != -ENOSPC)
1793 goto failed;
1794 /*
1795 * Try to reclaim some spece by splitting a huge page
1796 * beyond i_size on the filesystem.
1797 */
1798 while (retry--) {
1799 int ret;
1800 ret = shmem_unused_huge_shrink(sbinfo, NULL, 1);
1801 if (ret == SHRINK_STOP)
1802 break;
1803 if (ret)
1804 goto alloc_nohuge;
1805 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001806 goto failed;
1807 }
1808
1809 if (PageTransHuge(page))
1810 hindex = round_down(index, HPAGE_PMD_NR);
1811 else
1812 hindex = index;
1813
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001814 if (sgp == SGP_WRITE)
Hugh Dickinseb39d612014-08-06 16:06:43 -07001815 __SetPageReferenced(page);
Hugh Dickins66d2f4d2014-07-02 15:22:38 -07001816
Tejun Heo2cf85582018-07-03 11:14:56 -04001817 error = mem_cgroup_try_charge_delay(page, charge_mm, gfp, &memcg,
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001818 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001819 if (error)
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001820 goto unacct;
1821 error = radix_tree_maybe_preload_order(gfp & GFP_RECLAIM_MASK,
1822 compound_order(page));
Hugh Dickinsb065b432012-07-11 14:02:48 -07001823 if (!error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001824 error = shmem_add_to_page_cache(page, mapping, hindex,
Wang Sheng-Huifed400a2014-08-06 16:07:26 -07001825 NULL);
Hugh Dickinsb065b432012-07-11 14:02:48 -07001826 radix_tree_preload_end();
1827 }
1828 if (error) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001829 mem_cgroup_cancel_charge(page, memcg,
1830 PageTransHuge(page));
1831 goto unacct;
Hugh Dickinsb065b432012-07-11 14:02:48 -07001832 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001833 mem_cgroup_commit_charge(page, memcg, false,
1834 PageTransHuge(page));
Hugh Dickins54af60422011-08-03 16:21:24 -07001835 lru_cache_add_anon(page);
1836
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001837 spin_lock_irq(&info->lock);
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001838 info->alloced += 1 << compound_order(page);
1839 inode->i_blocks += BLOCKS_PER_PAGE << compound_order(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001840 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001841 spin_unlock_irq(&info->lock);
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001842 alloced = true;
Hugh Dickins54af60422011-08-03 16:21:24 -07001843
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001844 if (PageTransHuge(page) &&
1845 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) <
1846 hindex + HPAGE_PMD_NR - 1) {
1847 /*
1848 * Part of the huge page is beyond i_size: subject
1849 * to shrink under memory pressure.
1850 */
1851 spin_lock(&sbinfo->shrinklist_lock);
Cong Wangd0413532017-08-10 15:24:24 -07001852 /*
1853 * _careful to defend against unlocked access to
1854 * ->shrink_list in shmem_unused_huge_shrink()
1855 */
1856 if (list_empty_careful(&info->shrinklist)) {
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07001857 list_add_tail(&info->shrinklist,
1858 &sbinfo->shrinklist);
1859 sbinfo->shrinklist_len++;
1860 }
1861 spin_unlock(&sbinfo->shrinklist_lock);
1862 }
1863
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001864 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07001865 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1866 */
1867 if (sgp == SGP_FALLOC)
1868 sgp = SGP_WRITE;
1869clear:
1870 /*
1871 * Let SGP_WRITE caller clear ends if write does not fill page;
1872 * but SGP_FALLOC on a page fallocated earlier must initialize
1873 * it now, lest undo on failure cancel our earlier guarantee.
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001874 */
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001875 if (sgp != SGP_WRITE && !PageUptodate(page)) {
1876 struct page *head = compound_head(page);
1877 int i;
1878
1879 for (i = 0; i < (1 << compound_order(head)); i++) {
1880 clear_highpage(head + i);
1881 flush_dcache_page(head + i);
1882 }
1883 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07001884 }
Hugh Dickins59a16ea2011-05-11 15:13:38 -07001885 }
Hugh Dickinsbde05d12012-05-29 15:06:38 -07001886
Hugh Dickins54af60422011-08-03 16:21:24 -07001887 /* Perhaps the file has been truncated since we checked */
Hugh Dickins75edd342016-05-19 17:12:44 -07001888 if (sgp <= SGP_CACHE &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001889 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) {
Hugh Dickins267a4c72015-12-11 13:40:55 -08001890 if (alloced) {
1891 ClearPageDirty(page);
1892 delete_from_page_cache(page);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001893 spin_lock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001894 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001895 spin_unlock_irq(&info->lock);
Hugh Dickins267a4c72015-12-11 13:40:55 -08001896 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001897 error = -EINVAL;
Hugh Dickins267a4c72015-12-11 13:40:55 -08001898 goto unlock;
Shaohua Liff36b8012010-08-09 17:19:06 -07001899 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001900 *pagep = page + index - hindex;
Hugh Dickins54af60422011-08-03 16:21:24 -07001901 return 0;
Nick Piggind00806b2007-07-19 01:46:57 -07001902
Nick Piggind0217ac2007-07-19 01:47:03 -07001903 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07001904 * Error recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 */
Hugh Dickins54af60422011-08-03 16:21:24 -07001906unacct:
Mike Rapoport0f079692017-09-06 16:22:59 -07001907 shmem_inode_unacct_blocks(inode, 1 << compound_order(page));
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07001908
1909 if (PageTransHuge(page)) {
1910 unlock_page(page);
1911 put_page(page);
1912 goto alloc_nohuge;
1913 }
Hugh Dickins54af60422011-08-03 16:21:24 -07001914failed:
Hugh Dickins267a4c72015-12-11 13:40:55 -08001915 if (swap.val && !shmem_confirm_swap(mapping, index, swap))
Hugh Dickinsd1899222012-07-11 14:02:47 -07001916 error = -EEXIST;
1917unlock:
Hugh Dickins27ab7002011-07-25 17:12:36 -07001918 if (page) {
Hugh Dickins54af60422011-08-03 16:21:24 -07001919 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001920 put_page(page);
Hugh Dickins54af60422011-08-03 16:21:24 -07001921 }
1922 if (error == -ENOSPC && !once++) {
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001923 spin_lock_irq(&info->lock);
Hugh Dickins54af60422011-08-03 16:21:24 -07001924 shmem_recalc_inode(inode);
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07001925 spin_unlock_irq(&info->lock);
Hugh Dickins27ab7002011-07-25 17:12:36 -07001926 goto repeat;
Josef "Jeff" Sipekd3ac7f82006-12-08 02:36:44 -08001927 }
Hugh Dickinsd1899222012-07-11 14:02:47 -07001928 if (error == -EEXIST) /* from above or from radix_tree_insert */
Hugh Dickins54af60422011-08-03 16:21:24 -07001929 goto repeat;
1930 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001933/*
1934 * This is like autoremove_wake_function, but it removes the wait queue
1935 * entry unconditionally - even if something else had already woken the
1936 * target.
1937 */
Ingo Molnarac6424b2017-06-20 12:06:13 +02001938static int synchronous_wake_function(wait_queue_entry_t *wait, unsigned mode, int sync, void *key)
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001939{
1940 int ret = default_wake_function(wait, mode, sync, key);
Ingo Molnar2055da92017-06-20 12:06:46 +02001941 list_del_init(&wait->entry);
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001942 return ret;
1943}
1944
Souptick Joarder20acce62018-06-07 17:09:17 -07001945static vm_fault_t shmem_fault(struct vm_fault *vmf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Dave Jiang11bac802017-02-24 14:56:41 -08001947 struct vm_area_struct *vma = vmf->vma;
Al Viro496ad9a2013-01-23 17:07:38 -05001948 struct inode *inode = file_inode(vma->vm_file);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07001949 gfp_t gfp = mapping_gfp_mask(inode->i_mapping);
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07001950 enum sgp_type sgp;
Souptick Joarder20acce62018-06-07 17:09:17 -07001951 int err;
1952 vm_fault_t ret = VM_FAULT_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001954 /*
1955 * Trinity finds that probing a hole which tmpfs is punching can
1956 * prevent the hole-punch from ever completing: which in turn
1957 * locks writers out with its hold on i_mutex. So refrain from
Hugh Dickins8e205f72014-07-23 14:00:10 -07001958 * faulting pages into the hole while it's being punched. Although
1959 * shmem_undo_range() does remove the additions, it may be unable to
1960 * keep up, as each new page needs its own unmap_mapping_range() call,
1961 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1962 *
1963 * It does not matter if we sometimes reach this check just before the
1964 * hole-punch begins, so that one fault then races with the punch:
1965 * we just need to make racing faults a rare case.
1966 *
1967 * The implementation below would be much simpler if we just used a
1968 * standard mutex or completion: but we cannot take i_mutex in fault,
1969 * and bloating every shmem inode for this unlikely case would be sad.
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001970 */
1971 if (unlikely(inode->i_private)) {
1972 struct shmem_falloc *shmem_falloc;
1973
1974 spin_lock(&inode->i_lock);
1975 shmem_falloc = inode->i_private;
Hugh Dickins8e205f72014-07-23 14:00:10 -07001976 if (shmem_falloc &&
1977 shmem_falloc->waitq &&
1978 vmf->pgoff >= shmem_falloc->start &&
1979 vmf->pgoff < shmem_falloc->next) {
1980 wait_queue_head_t *shmem_falloc_waitq;
Linus Torvalds10d20bd2016-12-05 12:10:29 -08001981 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001982
1983 ret = VM_FAULT_NOPAGE;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001984 if ((vmf->flags & FAULT_FLAG_ALLOW_RETRY) &&
1985 !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT)) {
Hugh Dickins8e205f72014-07-23 14:00:10 -07001986 /* It's polite to up mmap_sem if we can */
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001987 up_read(&vma->vm_mm->mmap_sem);
Hugh Dickins8e205f72014-07-23 14:00:10 -07001988 ret = VM_FAULT_RETRY;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07001989 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07001990
1991 shmem_falloc_waitq = shmem_falloc->waitq;
1992 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait,
1993 TASK_UNINTERRUPTIBLE);
1994 spin_unlock(&inode->i_lock);
1995 schedule();
1996
1997 /*
1998 * shmem_falloc_waitq points into the shmem_fallocate()
1999 * stack of the hole-punching task: shmem_falloc_waitq
2000 * is usually invalid by the time we reach here, but
2001 * finish_wait() does not dereference it in that case;
2002 * though i_lock needed lest racing with wake_up_all().
2003 */
2004 spin_lock(&inode->i_lock);
2005 finish_wait(shmem_falloc_waitq, &shmem_fault_wait);
2006 spin_unlock(&inode->i_lock);
2007 return ret;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002008 }
Hugh Dickins8e205f72014-07-23 14:00:10 -07002009 spin_unlock(&inode->i_lock);
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002010 }
2011
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002012 sgp = SGP_CACHE;
Michal Hocko18600332017-07-10 15:48:02 -07002013
2014 if ((vma->vm_flags & VM_NOHUGEPAGE) ||
2015 test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags))
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002016 sgp = SGP_NOHUGE;
Michal Hocko18600332017-07-10 15:48:02 -07002017 else if (vma->vm_flags & VM_HUGEPAGE)
2018 sgp = SGP_HUGE;
Kirill A. Shutemov657e3032016-07-26 15:26:21 -07002019
Souptick Joarder20acce62018-06-07 17:09:17 -07002020 err = shmem_getpage_gfp(inode, vmf->pgoff, &vmf->page, sgp,
Mike Rapoportcfda0522017-02-22 15:43:37 -08002021 gfp, vma, vmf, &ret);
Souptick Joarder20acce62018-06-07 17:09:17 -07002022 if (err)
2023 return vmf_error(err);
Hugh Dickins68da9f02011-07-25 17:12:34 -07002024 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002027unsigned long shmem_get_unmapped_area(struct file *file,
2028 unsigned long uaddr, unsigned long len,
2029 unsigned long pgoff, unsigned long flags)
2030{
2031 unsigned long (*get_area)(struct file *,
2032 unsigned long, unsigned long, unsigned long, unsigned long);
2033 unsigned long addr;
2034 unsigned long offset;
2035 unsigned long inflated_len;
2036 unsigned long inflated_addr;
2037 unsigned long inflated_offset;
2038
2039 if (len > TASK_SIZE)
2040 return -ENOMEM;
2041
2042 get_area = current->mm->get_unmapped_area;
2043 addr = get_area(file, uaddr, len, pgoff, flags);
2044
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002045 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE))
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002046 return addr;
2047 if (IS_ERR_VALUE(addr))
2048 return addr;
2049 if (addr & ~PAGE_MASK)
2050 return addr;
2051 if (addr > TASK_SIZE - len)
2052 return addr;
2053
2054 if (shmem_huge == SHMEM_HUGE_DENY)
2055 return addr;
2056 if (len < HPAGE_PMD_SIZE)
2057 return addr;
2058 if (flags & MAP_FIXED)
2059 return addr;
2060 /*
2061 * Our priority is to support MAP_SHARED mapped hugely;
2062 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2063 * But if caller specified an address hint, respect that as before.
2064 */
2065 if (uaddr)
2066 return addr;
2067
2068 if (shmem_huge != SHMEM_HUGE_FORCE) {
2069 struct super_block *sb;
2070
2071 if (file) {
2072 VM_BUG_ON(file->f_op != &shmem_file_operations);
2073 sb = file_inode(file)->i_sb;
2074 } else {
2075 /*
2076 * Called directly from mm/mmap.c, or drivers/char/mem.c
2077 * for "/dev/zero", to create a shared anonymous object.
2078 */
2079 if (IS_ERR(shm_mnt))
2080 return addr;
2081 sb = shm_mnt->mnt_sb;
2082 }
Toshi Kani3089bf62016-09-23 20:21:56 -07002083 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER)
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07002084 return addr;
2085 }
2086
2087 offset = (pgoff << PAGE_SHIFT) & (HPAGE_PMD_SIZE-1);
2088 if (offset && offset + len < 2 * HPAGE_PMD_SIZE)
2089 return addr;
2090 if ((addr & (HPAGE_PMD_SIZE-1)) == offset)
2091 return addr;
2092
2093 inflated_len = len + HPAGE_PMD_SIZE - PAGE_SIZE;
2094 if (inflated_len > TASK_SIZE)
2095 return addr;
2096 if (inflated_len < len)
2097 return addr;
2098
2099 inflated_addr = get_area(NULL, 0, inflated_len, 0, flags);
2100 if (IS_ERR_VALUE(inflated_addr))
2101 return addr;
2102 if (inflated_addr & ~PAGE_MASK)
2103 return addr;
2104
2105 inflated_offset = inflated_addr & (HPAGE_PMD_SIZE-1);
2106 inflated_addr += offset - inflated_offset;
2107 if (inflated_offset > offset)
2108 inflated_addr += HPAGE_PMD_SIZE;
2109
2110 if (inflated_addr > TASK_SIZE - len)
2111 return addr;
2112 return inflated_addr;
2113}
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115#ifdef CONFIG_NUMA
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002116static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
Al Viro496ad9a2013-01-23 17:07:38 -05002118 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002119 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120}
2121
Adrian Bunkd8dc74f2007-10-16 01:26:26 -07002122static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma,
2123 unsigned long addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Al Viro496ad9a2013-01-23 17:07:38 -05002125 struct inode *inode = file_inode(vma->vm_file);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002126 pgoff_t index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002128 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff;
2129 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130}
2131#endif
2132
2133int shmem_lock(struct file *file, int lock, struct user_struct *user)
2134{
Al Viro496ad9a2013-01-23 17:07:38 -05002135 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct shmem_inode_info *info = SHMEM_I(inode);
2137 int retval = -ENOMEM;
2138
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002139 spin_lock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (lock && !(info->flags & VM_LOCKED)) {
2141 if (!user_shm_lock(inode->i_size, user))
2142 goto out_nomem;
2143 info->flags |= VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002144 mapping_set_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 }
2146 if (!lock && (info->flags & VM_LOCKED) && user) {
2147 user_shm_unlock(inode->i_size, user);
2148 info->flags &= ~VM_LOCKED;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002149 mapping_clear_unevictable(file->f_mapping);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 }
2151 retval = 0;
Lee Schermerhorn89e004ea2008-10-18 20:26:43 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153out_nomem:
Kirill A. Shutemov4595ef82016-07-26 15:26:29 -07002154 spin_unlock_irq(&info->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 return retval;
2156}
2157
Adrian Bunk9b83a6a2007-02-28 20:11:03 -08002158static int shmem_mmap(struct file *file, struct vm_area_struct *vma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159{
2160 file_accessed(file);
2161 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07002162 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07002163 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
2164 (vma->vm_end & HPAGE_PMD_MASK)) {
2165 khugepaged_enter(vma, vma->vm_flags);
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return 0;
2168}
2169
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002170static struct inode *shmem_get_inode(struct super_block *sb, const struct inode *dir,
Al Viro09208d12011-07-26 03:15:03 -04002171 umode_t mode, dev_t dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct inode *inode;
2174 struct shmem_inode_info *info;
2175 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
2176
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002177 if (shmem_reserve_inode(sb))
2178 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 inode = new_inode(sb);
2181 if (inode) {
Christoph Hellwig85fe4022010-10-23 11:19:54 -04002182 inode->i_ino = get_next_ino();
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002183 inode_init_owner(inode, dir, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 inode->i_blocks = 0;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002185 inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
Arnd Bergmann46c9a942018-08-17 15:45:09 -07002186 inode->i_generation = prandom_u32();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 info = SHMEM_I(inode);
2188 memset(info, 0, (char *)inode - (char *)info);
2189 spin_lock_init(&info->lock);
David Herrmann40e041a2014-08-08 14:25:27 -07002190 info->seals = F_SEAL_SEAL;
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002191 info->flags = flags & VM_NORESERVE;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07002192 INIT_LIST_HEAD(&info->shrinklist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 INIT_LIST_HEAD(&info->swaplist);
Aristeu Rozanski38f38652012-08-23 16:53:28 -04002194 simple_xattrs_init(&info->xattrs);
Al Viro72c04902009-06-24 16:58:48 -04002195 cache_no_acl(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 switch (mode & S_IFMT) {
2198 default:
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07002199 inode->i_op = &shmem_special_inode_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 init_special_inode(inode, mode, dev);
2201 break;
2202 case S_IFREG:
Hugh Dickins14fcc232008-07-28 15:46:19 -07002203 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 inode->i_op = &shmem_inode_operations;
2205 inode->i_fop = &shmem_file_operations;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002206 mpol_shared_policy_init(&info->policy,
2207 shmem_get_sbmpol(sbinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 break;
2209 case S_IFDIR:
Dave Hansend8c76e62006-09-30 23:29:04 -07002210 inc_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* Some things misbehave if size == 0 on a directory */
2212 inode->i_size = 2 * BOGO_DIRENT_SIZE;
2213 inode->i_op = &shmem_dir_inode_operations;
2214 inode->i_fop = &simple_dir_operations;
2215 break;
2216 case S_IFLNK:
2217 /*
2218 * Must not load anything in the rbtree,
2219 * mpol_free_shared_policy will not be called.
2220 */
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07002221 mpol_shared_policy_init(&info->policy, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 break;
2223 }
Joel Fernandes (Google)b45d71f2018-09-20 12:22:39 -07002224
2225 lockdep_annotate_inode_mutex_key(inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002226 } else
2227 shmem_free_inode(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return inode;
2229}
2230
Johannes Weiner0cd61442014-04-03 14:47:46 -07002231bool shmem_mapping(struct address_space *mapping)
2232{
Hugh Dickinsf8005452017-02-22 15:41:42 -08002233 return mapping->a_ops == &shmem_aops;
Johannes Weiner0cd61442014-04-03 14:47:46 -07002234}
2235
Mike Rapoport8d103962017-09-06 16:23:02 -07002236static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,
2237 pmd_t *dst_pmd,
2238 struct vm_area_struct *dst_vma,
2239 unsigned long dst_addr,
2240 unsigned long src_addr,
2241 bool zeropage,
2242 struct page **pagep)
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002243{
2244 struct inode *inode = file_inode(dst_vma->vm_file);
2245 struct shmem_inode_info *info = SHMEM_I(inode);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002246 struct address_space *mapping = inode->i_mapping;
2247 gfp_t gfp = mapping_gfp_mask(mapping);
2248 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr);
2249 struct mem_cgroup *memcg;
2250 spinlock_t *ptl;
2251 void *page_kaddr;
2252 struct page *page;
2253 pte_t _dst_pte, *dst_pte;
2254 int ret;
2255
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002256 ret = -ENOMEM;
Mike Rapoport0f079692017-09-06 16:22:59 -07002257 if (!shmem_inode_acct_block(inode, 1))
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002258 goto out;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002259
Andrea Arcangelicb658a42017-02-22 15:43:55 -08002260 if (!*pagep) {
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002261 page = shmem_alloc_page(gfp, info, pgoff);
2262 if (!page)
Mike Rapoport0f079692017-09-06 16:22:59 -07002263 goto out_unacct_blocks;
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002264
Mike Rapoport8d103962017-09-06 16:23:02 -07002265 if (!zeropage) { /* mcopy_atomic */
2266 page_kaddr = kmap_atomic(page);
2267 ret = copy_from_user(page_kaddr,
2268 (const void __user *)src_addr,
2269 PAGE_SIZE);
2270 kunmap_atomic(page_kaddr);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002271
Mike Rapoport8d103962017-09-06 16:23:02 -07002272 /* fallback to copy_from_user outside mmap_sem */
2273 if (unlikely(ret)) {
2274 *pagep = page;
2275 shmem_inode_unacct_blocks(inode, 1);
2276 /* don't free the page */
2277 return -EFAULT;
2278 }
2279 } else { /* mfill_zeropage_atomic */
2280 clear_highpage(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002281 }
2282 } else {
2283 page = *pagep;
2284 *pagep = NULL;
2285 }
2286
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002287 VM_BUG_ON(PageLocked(page) || PageSwapBacked(page));
2288 __SetPageLocked(page);
2289 __SetPageSwapBacked(page);
Andrea Arcangelia425d352017-02-22 15:43:52 -08002290 __SetPageUptodate(page);
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002291
Tejun Heo2cf85582018-07-03 11:14:56 -04002292 ret = mem_cgroup_try_charge_delay(page, dst_mm, gfp, &memcg, false);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002293 if (ret)
2294 goto out_release;
2295
2296 ret = radix_tree_maybe_preload(gfp & GFP_RECLAIM_MASK);
2297 if (!ret) {
2298 ret = shmem_add_to_page_cache(page, mapping, pgoff, NULL);
2299 radix_tree_preload_end();
2300 }
2301 if (ret)
2302 goto out_release_uncharge;
2303
2304 mem_cgroup_commit_charge(page, memcg, false, false);
2305
2306 _dst_pte = mk_pte(page, dst_vma->vm_page_prot);
2307 if (dst_vma->vm_flags & VM_WRITE)
2308 _dst_pte = pte_mkwrite(pte_mkdirty(_dst_pte));
2309
2310 ret = -EEXIST;
2311 dst_pte = pte_offset_map_lock(dst_mm, dst_pmd, dst_addr, &ptl);
2312 if (!pte_none(*dst_pte))
2313 goto out_release_uncharge_unlock;
2314
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002315 lru_cache_add_anon(page);
2316
2317 spin_lock(&info->lock);
2318 info->alloced++;
2319 inode->i_blocks += BLOCKS_PER_PAGE;
2320 shmem_recalc_inode(inode);
2321 spin_unlock(&info->lock);
2322
2323 inc_mm_counter(dst_mm, mm_counter_file(page));
2324 page_add_file_rmap(page, false);
2325 set_pte_at(dst_mm, dst_addr, dst_pte, _dst_pte);
2326
2327 /* No need to invalidate - it was non-present before */
2328 update_mmu_cache(dst_vma, dst_addr, dst_pte);
2329 unlock_page(page);
2330 pte_unmap_unlock(dst_pte, ptl);
2331 ret = 0;
2332out:
2333 return ret;
2334out_release_uncharge_unlock:
2335 pte_unmap_unlock(dst_pte, ptl);
2336out_release_uncharge:
2337 mem_cgroup_cancel_charge(page, memcg, false);
2338out_release:
Andrea Arcangeli9cc90c62017-02-22 15:43:49 -08002339 unlock_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002340 put_page(page);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002341out_unacct_blocks:
Mike Rapoport0f079692017-09-06 16:22:59 -07002342 shmem_inode_unacct_blocks(inode, 1);
Mike Rapoport4c27fe42017-02-22 15:43:25 -08002343 goto out;
2344}
2345
Mike Rapoport8d103962017-09-06 16:23:02 -07002346int shmem_mcopy_atomic_pte(struct mm_struct *dst_mm,
2347 pmd_t *dst_pmd,
2348 struct vm_area_struct *dst_vma,
2349 unsigned long dst_addr,
2350 unsigned long src_addr,
2351 struct page **pagep)
2352{
2353 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2354 dst_addr, src_addr, false, pagep);
2355}
2356
2357int shmem_mfill_zeropage_pte(struct mm_struct *dst_mm,
2358 pmd_t *dst_pmd,
2359 struct vm_area_struct *dst_vma,
2360 unsigned long dst_addr)
2361{
2362 struct page *page = NULL;
2363
2364 return shmem_mfill_atomic_pte(dst_mm, dst_pmd, dst_vma,
2365 dst_addr, 0, true, &page);
2366}
2367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368#ifdef CONFIG_TMPFS
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002369static const struct inode_operations shmem_symlink_inode_operations;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07002370static const struct inode_operations shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002372#ifdef CONFIG_TMPFS_XATTR
2373static int shmem_initxattrs(struct inode *, const struct xattr *, void *);
2374#else
2375#define shmem_initxattrs NULL
2376#endif
2377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378static int
Nick Piggin800d15a2007-10-16 01:25:03 -07002379shmem_write_begin(struct file *file, struct address_space *mapping,
2380 loff_t pos, unsigned len, unsigned flags,
2381 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Nick Piggin800d15a2007-10-16 01:25:03 -07002383 struct inode *inode = mapping->host;
David Herrmann40e041a2014-08-08 14:25:27 -07002384 struct shmem_inode_info *info = SHMEM_I(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002385 pgoff_t index = pos >> PAGE_SHIFT;
David Herrmann40e041a2014-08-08 14:25:27 -07002386
2387 /* i_mutex is held by caller */
Steven Rostedt (VMware)3f472cc2017-02-24 14:59:10 -08002388 if (unlikely(info->seals & (F_SEAL_WRITE | F_SEAL_GROW))) {
David Herrmann40e041a2014-08-08 14:25:27 -07002389 if (info->seals & F_SEAL_WRITE)
2390 return -EPERM;
2391 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size)
2392 return -EPERM;
2393 }
2394
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002395 return shmem_getpage(inode, index, pagep, SGP_WRITE);
Nick Piggin800d15a2007-10-16 01:25:03 -07002396}
2397
2398static int
2399shmem_write_end(struct file *file, struct address_space *mapping,
2400 loff_t pos, unsigned len, unsigned copied,
2401 struct page *page, void *fsdata)
2402{
2403 struct inode *inode = mapping->host;
2404
Hugh Dickinsd3602442008-02-04 22:28:44 -08002405 if (pos + copied > inode->i_size)
2406 i_size_write(inode, pos + copied);
2407
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002408 if (!PageUptodate(page)) {
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002409 struct page *head = compound_head(page);
2410 if (PageTransCompound(page)) {
2411 int i;
2412
2413 for (i = 0; i < HPAGE_PMD_NR; i++) {
2414 if (head + i == page)
2415 continue;
2416 clear_highpage(head + i);
2417 flush_dcache_page(head + i);
2418 }
2419 }
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002420 if (copied < PAGE_SIZE) {
2421 unsigned from = pos & (PAGE_SIZE - 1);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002422 zero_user_segments(page, 0, from,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002423 from + copied, PAGE_SIZE);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002424 }
Kirill A. Shutemov800d8c62016-07-26 15:26:18 -07002425 SetPageUptodate(head);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07002426 }
Nick Piggin800d15a2007-10-16 01:25:03 -07002427 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02002428 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002429 put_page(page);
Nick Piggin800d15a2007-10-16 01:25:03 -07002430
Nick Piggin800d15a2007-10-16 01:25:03 -07002431 return copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Al Viro2ba5bbe2014-04-02 20:00:02 -04002434static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435{
Al Viro6e58e792014-02-03 17:07:03 -05002436 struct file *file = iocb->ki_filp;
2437 struct inode *inode = file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 struct address_space *mapping = inode->i_mapping;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002439 pgoff_t index;
2440 unsigned long offset;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002441 enum sgp_type sgp = SGP_READ;
Geert Uytterhoevenf7c1d072014-04-13 20:46:22 +02002442 int error = 0;
Al Virocb66a7a2014-03-04 15:24:06 -05002443 ssize_t retval = 0;
Al Viro6e58e792014-02-03 17:07:03 -05002444 loff_t *ppos = &iocb->ki_pos;
Hugh Dickinsa0ee5ec2008-02-04 22:28:51 -08002445
2446 /*
2447 * Might this read be for a stacking filesystem? Then when reading
2448 * holes of a sparse file, we actually need to allocate those pages,
2449 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2450 */
Al Viro777eda22014-12-17 04:46:46 -05002451 if (!iter_is_iovec(to))
Hugh Dickins75edd342016-05-19 17:12:44 -07002452 sgp = SGP_CACHE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002454 index = *ppos >> PAGE_SHIFT;
2455 offset = *ppos & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457 for (;;) {
2458 struct page *page = NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002459 pgoff_t end_index;
2460 unsigned long nr, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 loff_t i_size = i_size_read(inode);
2462
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002463 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (index > end_index)
2465 break;
2466 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002467 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (nr <= offset)
2469 break;
2470 }
2471
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002472 error = shmem_getpage(inode, index, &page, sgp);
Al Viro6e58e792014-02-03 17:07:03 -05002473 if (error) {
2474 if (error == -EINVAL)
2475 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 break;
2477 }
Hugh Dickins75edd342016-05-19 17:12:44 -07002478 if (page) {
2479 if (sgp == SGP_CACHE)
2480 set_page_dirty(page);
Hugh Dickinsd3602442008-02-04 22:28:44 -08002481 unlock_page(page);
Hugh Dickins75edd342016-05-19 17:12:44 -07002482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
2484 /*
2485 * We must evaluate after, since reads (unlike writes)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002486 * are called without i_mutex protection against truncate
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002488 nr = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 i_size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002490 end_index = i_size >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (index == end_index) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002492 nr = i_size & ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (nr <= offset) {
2494 if (page)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002495 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 break;
2497 }
2498 }
2499 nr -= offset;
2500
2501 if (page) {
2502 /*
2503 * If users can be writing to this page using arbitrary
2504 * virtual addresses, take care about potential aliasing
2505 * before reading the page on the kernel side.
2506 */
2507 if (mapping_writably_mapped(mapping))
2508 flush_dcache_page(page);
2509 /*
2510 * Mark the page accessed if we read the beginning.
2511 */
2512 if (!offset)
2513 mark_page_accessed(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002514 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 page = ZERO_PAGE(0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002516 get_page(page);
Nick Pigginb5810032005-10-29 18:16:12 -07002517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
2519 /*
2520 * Ok, we have the page, and it's up-to-date, so
2521 * now we can copy it to user space...
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 */
Al Viro2ba5bbe2014-04-02 20:00:02 -04002523 ret = copy_page_to_iter(page, offset, nr, to);
Al Viro6e58e792014-02-03 17:07:03 -05002524 retval += ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 offset += ret;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002526 index += offset >> PAGE_SHIFT;
2527 offset &= ~PAGE_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002529 put_page(page);
Al Viro2ba5bbe2014-04-02 20:00:02 -04002530 if (!iov_iter_count(to))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 break;
Al Viro6e58e792014-02-03 17:07:03 -05002532 if (ret < nr) {
2533 error = -EFAULT;
2534 break;
2535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 cond_resched();
2537 }
2538
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002539 *ppos = ((loff_t) index << PAGE_SHIFT) + offset;
Al Viro6e58e792014-02-03 17:07:03 -05002540 file_accessed(file);
2541 return retval ? retval : error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542}
2543
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002544/*
2545 * llseek SEEK_DATA or SEEK_HOLE through the radix_tree.
2546 */
2547static pgoff_t shmem_seek_hole_data(struct address_space *mapping,
Andrew Morton965c8e52012-12-17 15:59:39 -08002548 pgoff_t index, pgoff_t end, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002549{
2550 struct page *page;
2551 struct pagevec pvec;
2552 pgoff_t indices[PAGEVEC_SIZE];
2553 bool done = false;
2554 int i;
2555
Mel Gorman86679822017-11-15 17:37:52 -08002556 pagevec_init(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002557 pvec.nr = 1; /* start small: we may be there already */
2558 while (!done) {
Johannes Weiner0cd61442014-04-03 14:47:46 -07002559 pvec.nr = find_get_entries(mapping, index,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002560 pvec.nr, pvec.pages, indices);
2561 if (!pvec.nr) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002562 if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002563 index = end;
2564 break;
2565 }
2566 for (i = 0; i < pvec.nr; i++, index++) {
2567 if (index < indices[i]) {
Andrew Morton965c8e52012-12-17 15:59:39 -08002568 if (whence == SEEK_HOLE) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002569 done = true;
2570 break;
2571 }
2572 index = indices[i];
2573 }
2574 page = pvec.pages[i];
Matthew Wilcox3159f942017-11-03 13:30:42 -04002575 if (page && !xa_is_value(page)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002576 if (!PageUptodate(page))
2577 page = NULL;
2578 }
2579 if (index >= end ||
Andrew Morton965c8e52012-12-17 15:59:39 -08002580 (page && whence == SEEK_DATA) ||
2581 (!page && whence == SEEK_HOLE)) {
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002582 done = true;
2583 break;
2584 }
2585 }
Johannes Weiner0cd61442014-04-03 14:47:46 -07002586 pagevec_remove_exceptionals(&pvec);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002587 pagevec_release(&pvec);
2588 pvec.nr = PAGEVEC_SIZE;
2589 cond_resched();
2590 }
2591 return index;
2592}
2593
Andrew Morton965c8e52012-12-17 15:59:39 -08002594static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002595{
2596 struct address_space *mapping = file->f_mapping;
2597 struct inode *inode = mapping->host;
2598 pgoff_t start, end;
2599 loff_t new_offset;
2600
Andrew Morton965c8e52012-12-17 15:59:39 -08002601 if (whence != SEEK_DATA && whence != SEEK_HOLE)
2602 return generic_file_llseek_size(file, offset, whence,
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002603 MAX_LFS_FILESIZE, i_size_read(inode));
Al Viro59551022016-01-22 15:40:57 -05002604 inode_lock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002605 /* We're holding i_mutex so we can access i_size directly */
2606
2607 if (offset < 0)
2608 offset = -EINVAL;
2609 else if (offset >= inode->i_size)
2610 offset = -ENXIO;
2611 else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002612 start = offset >> PAGE_SHIFT;
2613 end = (inode->i_size + PAGE_SIZE - 1) >> PAGE_SHIFT;
Andrew Morton965c8e52012-12-17 15:59:39 -08002614 new_offset = shmem_seek_hole_data(mapping, start, end, whence);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002615 new_offset <<= PAGE_SHIFT;
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002616 if (new_offset > offset) {
2617 if (new_offset < inode->i_size)
2618 offset = new_offset;
Andrew Morton965c8e52012-12-17 15:59:39 -08002619 else if (whence == SEEK_DATA)
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002620 offset = -ENXIO;
2621 else
2622 offset = inode->i_size;
2623 }
2624 }
2625
Hugh Dickins387aae62013-08-04 11:30:25 -07002626 if (offset >= 0)
2627 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE);
Al Viro59551022016-01-22 15:40:57 -05002628 inode_unlock(inode);
Hugh Dickins220f2ac2012-12-12 13:52:21 -08002629 return offset;
2630}
2631
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002632static long shmem_fallocate(struct file *file, int mode, loff_t offset,
2633 loff_t len)
2634{
Al Viro496ad9a2013-01-23 17:07:38 -05002635 struct inode *inode = file_inode(file);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002636 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
David Herrmann40e041a2014-08-08 14:25:27 -07002637 struct shmem_inode_info *info = SHMEM_I(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002638 struct shmem_falloc shmem_falloc;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002639 pgoff_t start, index, end;
2640 int error;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002641
Hugh Dickins13ace4d2014-06-23 13:22:03 -07002642 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2643 return -EOPNOTSUPP;
2644
Al Viro59551022016-01-22 15:40:57 -05002645 inode_lock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002646
2647 if (mode & FALLOC_FL_PUNCH_HOLE) {
2648 struct address_space *mapping = file->f_mapping;
2649 loff_t unmap_start = round_up(offset, PAGE_SIZE);
2650 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002651 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002652
David Herrmann40e041a2014-08-08 14:25:27 -07002653 /* protected by i_mutex */
2654 if (info->seals & F_SEAL_WRITE) {
2655 error = -EPERM;
2656 goto out;
2657 }
2658
Hugh Dickins8e205f72014-07-23 14:00:10 -07002659 shmem_falloc.waitq = &shmem_falloc_waitq;
Hugh Dickinsf00cdc62014-06-23 13:22:06 -07002660 shmem_falloc.start = unmap_start >> PAGE_SHIFT;
2661 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT;
2662 spin_lock(&inode->i_lock);
2663 inode->i_private = &shmem_falloc;
2664 spin_unlock(&inode->i_lock);
2665
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002666 if ((u64)unmap_end > (u64)unmap_start)
2667 unmap_mapping_range(mapping, unmap_start,
2668 1 + unmap_end - unmap_start, 0);
2669 shmem_truncate_range(inode, offset, offset + len - 1);
2670 /* No need to unmap again: hole-punching leaves COWed pages */
Hugh Dickins8e205f72014-07-23 14:00:10 -07002671
2672 spin_lock(&inode->i_lock);
2673 inode->i_private = NULL;
2674 wake_up_all(&shmem_falloc_waitq);
Ingo Molnar2055da92017-06-20 12:06:46 +02002675 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head));
Hugh Dickins8e205f72014-07-23 14:00:10 -07002676 spin_unlock(&inode->i_lock);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002677 error = 0;
Hugh Dickins8e205f72014-07-23 14:00:10 -07002678 goto out;
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002679 }
2680
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002681 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2682 error = inode_newsize_ok(inode, offset + len);
2683 if (error)
2684 goto out;
2685
David Herrmann40e041a2014-08-08 14:25:27 -07002686 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
2687 error = -EPERM;
2688 goto out;
2689 }
2690
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002691 start = offset >> PAGE_SHIFT;
2692 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002693 /* Try to avoid a swapstorm if len is impossible to satisfy */
2694 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) {
2695 error = -ENOSPC;
2696 goto out;
2697 }
2698
Hugh Dickins8e205f72014-07-23 14:00:10 -07002699 shmem_falloc.waitq = NULL;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002700 shmem_falloc.start = start;
2701 shmem_falloc.next = start;
2702 shmem_falloc.nr_falloced = 0;
2703 shmem_falloc.nr_unswapped = 0;
2704 spin_lock(&inode->i_lock);
2705 inode->i_private = &shmem_falloc;
2706 spin_unlock(&inode->i_lock);
2707
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002708 for (index = start; index < end; index++) {
2709 struct page *page;
2710
2711 /*
2712 * Good, the fallocate(2) manpage permits EINTR: we may have
2713 * been interrupted because we are using up too much memory.
2714 */
2715 if (signal_pending(current))
2716 error = -EINTR;
Hugh Dickins1aac1402012-05-29 15:06:42 -07002717 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced)
2718 error = -ENOMEM;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002719 else
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07002720 error = shmem_getpage(inode, index, &page, SGP_FALLOC);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002721 if (error) {
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002722 /* Remove the !PageUptodate pages we added */
Hugh Dickins7f556562016-07-10 16:46:32 -07002723 if (index > start) {
2724 shmem_undo_range(inode,
2725 (loff_t)start << PAGE_SHIFT,
2726 ((loff_t)index << PAGE_SHIFT) - 1, true);
2727 }
Hugh Dickins1aac1402012-05-29 15:06:42 -07002728 goto undone;
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002729 }
2730
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002731 /*
Hugh Dickins1aac1402012-05-29 15:06:42 -07002732 * Inform shmem_writepage() how far we have reached.
2733 * No need for lock or barrier: we have the page lock.
2734 */
2735 shmem_falloc.next++;
2736 if (!PageUptodate(page))
2737 shmem_falloc.nr_falloced++;
2738
2739 /*
Hugh Dickins1635f6a2012-05-29 15:06:42 -07002740 * If !PageUptodate, leave it that way so that freeable pages
2741 * can be recognized if we need to rollback on error later.
2742 * But set_page_dirty so that memory pressure will swap rather
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002743 * than free the pages we are allocating (and SGP_CACHE pages
2744 * might still be clean: we now need to mark those dirty too).
2745 */
2746 set_page_dirty(page);
2747 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002748 put_page(page);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002749 cond_resched();
2750 }
2751
2752 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
2753 i_size_write(inode, offset + len);
Deepa Dinamani078cd822016-09-14 07:48:04 -07002754 inode->i_ctime = current_time(inode);
Hugh Dickins1aac1402012-05-29 15:06:42 -07002755undone:
2756 spin_lock(&inode->i_lock);
2757 inode->i_private = NULL;
2758 spin_unlock(&inode->i_lock);
Hugh Dickinse2d12e22012-05-29 15:06:41 -07002759out:
Al Viro59551022016-01-22 15:40:57 -05002760 inode_unlock(inode);
Hugh Dickins83e4fa92012-05-29 15:06:40 -07002761 return error;
2762}
2763
David Howells726c3342006-06-23 02:02:58 -07002764static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
David Howells726c3342006-06-23 02:02:58 -07002766 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
2768 buf->f_type = TMPFS_MAGIC;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002769 buf->f_bsize = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 buf->f_namelen = NAME_MAX;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002771 if (sbinfo->max_blocks) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 buf->f_blocks = sbinfo->max_blocks;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07002773 buf->f_bavail =
2774 buf->f_bfree = sbinfo->max_blocks -
2775 percpu_counter_sum(&sbinfo->used_blocks);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07002776 }
2777 if (sbinfo->max_inodes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 buf->f_files = sbinfo->max_inodes;
2779 buf->f_ffree = sbinfo->free_inodes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 }
2781 /* else leave those fields 0 like simple_statfs */
2782 return 0;
2783}
2784
2785/*
2786 * File creation. Allocate an inode, and we're done..
2787 */
2788static int
Al Viro1a67aaf2011-07-26 01:52:52 -04002789shmem_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790{
Hugh Dickins0b0a0802009-02-24 20:51:52 +00002791 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 int error = -ENOSPC;
2793
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03002794 inode = shmem_get_inode(dir->i_sb, dir, mode, dev, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (inode) {
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002796 error = simple_acl_create(dir, inode);
2797 if (error)
2798 goto out_iput;
Eric Paris2a7dba32011-02-01 11:05:39 -05002799 error = security_inode_init_security(inode, dir,
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04002800 &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07002801 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002802 if (error && error != -EOPNOTSUPP)
2803 goto out_iput;
Mimi Zohar37ec43c2013-04-14 09:21:47 -04002804
Al Viro718deb62009-12-16 19:35:36 -05002805 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002807 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 d_instantiate(dentry, inode);
2809 dget(dentry); /* Extra count - pin the dentry in core */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 }
2811 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002812out_iput:
2813 iput(inode);
2814 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815}
2816
Al Viro60545d02013-06-07 01:20:27 -04002817static int
2818shmem_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
2819{
2820 struct inode *inode;
2821 int error = -ENOSPC;
2822
2823 inode = shmem_get_inode(dir->i_sb, dir, mode, 0, VM_NORESERVE);
2824 if (inode) {
2825 error = security_inode_init_security(inode, dir,
2826 NULL,
2827 shmem_initxattrs, NULL);
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002828 if (error && error != -EOPNOTSUPP)
2829 goto out_iput;
2830 error = simple_acl_create(dir, inode);
2831 if (error)
2832 goto out_iput;
Al Viro60545d02013-06-07 01:20:27 -04002833 d_tmpfile(dentry, inode);
2834 }
2835 return error;
Christoph Hellwigfeda8212013-12-20 05:16:54 -08002836out_iput:
2837 iput(inode);
2838 return error;
Al Viro60545d02013-06-07 01:20:27 -04002839}
2840
Al Viro18bb1db2011-07-26 01:41:39 -04002841static int shmem_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
2843 int error;
2844
2845 if ((error = shmem_mknod(dir, dentry, mode | S_IFDIR, 0)))
2846 return error;
Dave Hansend8c76e62006-09-30 23:29:04 -07002847 inc_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 return 0;
2849}
2850
Al Viro4acdaf22011-07-26 01:42:34 -04002851static int shmem_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -04002852 bool excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 return shmem_mknod(dir, dentry, mode | S_IFREG, 0);
2855}
2856
2857/*
2858 * Link a file..
2859 */
2860static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2861{
David Howells75c3cfa2015-03-17 22:26:12 +00002862 struct inode *inode = d_inode(old_dentry);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002863 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 /*
2866 * No ordinary (disk based) filesystem counts links as inodes;
2867 * but each new link needs a new dentry, pinning lowmem, and
2868 * tmpfs dentries cannot be pruned until they are unlinked.
2869 */
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002870 ret = shmem_reserve_inode(inode->i_sb);
2871 if (ret)
2872 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002875 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansend8c76e62006-09-30 23:29:04 -07002876 inc_nlink(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04002877 ihold(inode); /* New dentry reference */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 dget(dentry); /* Extra pinning count for the created dentry */
2879 d_instantiate(dentry, inode);
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002880out:
2881 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884static int shmem_unlink(struct inode *dir, struct dentry *dentry)
2885{
David Howells75c3cfa2015-03-17 22:26:12 +00002886 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Pavel Emelyanov5b04c682008-02-04 22:28:47 -08002888 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode))
2889 shmem_free_inode(inode->i_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 dir->i_size -= BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07002892 inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002893 drop_nlink(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 dput(dentry); /* Undo the count from "create" - this does all the work */
2895 return 0;
2896}
2897
2898static int shmem_rmdir(struct inode *dir, struct dentry *dentry)
2899{
2900 if (!simple_empty(dentry))
2901 return -ENOTEMPTY;
2902
David Howells75c3cfa2015-03-17 22:26:12 +00002903 drop_nlink(d_inode(dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002904 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 return shmem_unlink(dir, dentry);
2906}
2907
Miklos Szeredi37456772014-07-23 15:15:34 +02002908static int shmem_exchange(struct inode *old_dir, struct dentry *old_dentry, struct inode *new_dir, struct dentry *new_dentry)
2909{
David Howellse36cb0b2015-01-29 12:02:35 +00002910 bool old_is_dir = d_is_dir(old_dentry);
2911 bool new_is_dir = d_is_dir(new_dentry);
Miklos Szeredi37456772014-07-23 15:15:34 +02002912
2913 if (old_dir != new_dir && old_is_dir != new_is_dir) {
2914 if (old_is_dir) {
2915 drop_nlink(old_dir);
2916 inc_nlink(new_dir);
2917 } else {
2918 drop_nlink(new_dir);
2919 inc_nlink(old_dir);
2920 }
2921 }
2922 old_dir->i_ctime = old_dir->i_mtime =
2923 new_dir->i_ctime = new_dir->i_mtime =
David Howells75c3cfa2015-03-17 22:26:12 +00002924 d_inode(old_dentry)->i_ctime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002925 d_inode(new_dentry)->i_ctime = current_time(old_dir);
Miklos Szeredi37456772014-07-23 15:15:34 +02002926
2927 return 0;
2928}
2929
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002930static int shmem_whiteout(struct inode *old_dir, struct dentry *old_dentry)
2931{
2932 struct dentry *whiteout;
2933 int error;
2934
2935 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name);
2936 if (!whiteout)
2937 return -ENOMEM;
2938
2939 error = shmem_mknod(old_dir, whiteout,
2940 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
2941 dput(whiteout);
2942 if (error)
2943 return error;
2944
2945 /*
2946 * Cheat and hash the whiteout while the old dentry is still in
2947 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2948 *
2949 * d_lookup() will consistently find one of them at this point,
2950 * not sure which one, but that isn't even important.
2951 */
2952 d_rehash(whiteout);
2953 return 0;
2954}
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956/*
2957 * The VFS layer already does all the dentry stuff for rename,
2958 * we just have to decrement the usage count for the target if
2959 * it exists so that the VFS layer correctly free's it when it
2960 * gets overwritten.
2961 */
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002962static 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 -07002963{
David Howells75c3cfa2015-03-17 22:26:12 +00002964 struct inode *inode = d_inode(old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 int they_are_dirs = S_ISDIR(inode->i_mode);
2966
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002967 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi3b69ff52014-07-23 15:15:33 +02002968 return -EINVAL;
2969
Miklos Szeredi37456772014-07-23 15:15:34 +02002970 if (flags & RENAME_EXCHANGE)
2971 return shmem_exchange(old_dir, old_dentry, new_dir, new_dentry);
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (!simple_empty(new_dentry))
2974 return -ENOTEMPTY;
2975
Miklos Szeredi46fdb792014-10-24 00:14:37 +02002976 if (flags & RENAME_WHITEOUT) {
2977 int error;
2978
2979 error = shmem_whiteout(old_dir, old_dentry);
2980 if (error)
2981 return error;
2982 }
2983
David Howells75c3cfa2015-03-17 22:26:12 +00002984 if (d_really_is_positive(new_dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 (void) shmem_unlink(new_dir, new_dentry);
Miklos Szeredib9280952014-09-24 17:56:17 +02002986 if (they_are_dirs) {
David Howells75c3cfa2015-03-17 22:26:12 +00002987 drop_nlink(d_inode(new_dentry));
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002988 drop_nlink(old_dir);
Miklos Szeredib9280952014-09-24 17:56:17 +02002989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 } else if (they_are_dirs) {
Dave Hansen9a53c3a2006-09-30 23:29:03 -07002991 drop_nlink(old_dir);
Dave Hansend8c76e62006-09-30 23:29:04 -07002992 inc_nlink(new_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
2994
2995 old_dir->i_size -= BOGO_DIRENT_SIZE;
2996 new_dir->i_size += BOGO_DIRENT_SIZE;
2997 old_dir->i_ctime = old_dir->i_mtime =
2998 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamani078cd822016-09-14 07:48:04 -07002999 inode->i_ctime = current_time(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return 0;
3001}
3002
3003static int shmem_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
3004{
3005 int error;
3006 int len;
3007 struct inode *inode;
Hugh Dickins9276aad2011-07-25 17:12:34 -07003008 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009
3010 len = strlen(symname) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003011 if (len > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 return -ENAMETOOLONG;
3013
Joe Perches0825a6f2018-06-14 15:27:58 -07003014 inode = shmem_get_inode(dir->i_sb, dir, S_IFLNK | 0777, 0,
3015 VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (!inode)
3017 return -ENOSPC;
3018
Mimi Zohar9d8f13b2011-06-06 15:29:25 -04003019 error = security_inode_init_security(inode, dir, &dentry->d_name,
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003020 shmem_initxattrs, NULL);
Stephen Smalley570bc1c2005-09-09 13:01:43 -07003021 if (error) {
3022 if (error != -EOPNOTSUPP) {
3023 iput(inode);
3024 return error;
3025 }
3026 error = 0;
3027 }
3028
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 inode->i_size = len-1;
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003030 if (len <= SHORT_SYMLINK_LEN) {
Al Viro3ed47db2016-01-22 18:08:52 -05003031 inode->i_link = kmemdup(symname, len, GFP_KERNEL);
3032 if (!inode->i_link) {
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003033 iput(inode);
3034 return -ENOMEM;
3035 }
3036 inode->i_op = &shmem_short_symlink_operations;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 } else {
Al Viroe8ecde22016-01-14 17:52:59 -05003038 inode_nohighmem(inode);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003039 error = shmem_getpage(inode, 0, &page, SGP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (error) {
3041 iput(inode);
3042 return error;
3043 }
Hugh Dickins14fcc232008-07-28 15:46:19 -07003044 inode->i_mapping->a_ops = &shmem_aops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 inode->i_op = &shmem_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003046 memcpy(page_address(page), symname, len);
Hugh Dickinsec9516f2012-05-29 15:06:39 -07003047 SetPageUptodate(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 set_page_dirty(page);
Wu Fengguang6746aff2009-09-16 11:50:14 +02003049 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003050 put_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 dir->i_size += BOGO_DIRENT_SIZE;
Deepa Dinamani078cd822016-09-14 07:48:04 -07003053 dir->i_ctime = dir->i_mtime = current_time(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 d_instantiate(dentry, inode);
3055 dget(dentry);
3056 return 0;
3057}
3058
Al Virofceef392015-12-29 15:58:39 -05003059static void shmem_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
Al Virofceef392015-12-29 15:58:39 -05003061 mark_page_accessed(arg);
3062 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063}
3064
Al Viro6b255392015-11-17 10:20:54 -05003065static const char *shmem_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -05003066 struct inode *inode,
3067 struct delayed_call *done)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 struct page *page = NULL;
Al Viro6b255392015-11-17 10:20:54 -05003070 int error;
Al Viro6a6c9902015-11-17 10:54:32 -05003071 if (!dentry) {
3072 page = find_get_page(inode->i_mapping, 0);
3073 if (!page)
3074 return ERR_PTR(-ECHILD);
3075 if (!PageUptodate(page)) {
3076 put_page(page);
3077 return ERR_PTR(-ECHILD);
3078 }
3079 } else {
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07003080 error = shmem_getpage(inode, 0, &page, SGP_READ);
Al Viro6a6c9902015-11-17 10:54:32 -05003081 if (error)
3082 return ERR_PTR(error);
3083 unlock_page(page);
3084 }
Al Virofceef392015-12-29 15:58:39 -05003085 set_delayed_call(done, shmem_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05003086 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
Eric Parisb09e0fa2011-05-24 17:12:39 -07003089#ifdef CONFIG_TMPFS_XATTR
3090/*
3091 * Superblocks without xattr inode operations may get some security.* xattr
3092 * support from the LSM "for free". As soon as we have any other xattrs
3093 * like ACLs, we also need to implement the security.* handlers at
3094 * filesystem level, though.
3095 */
3096
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003097/*
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003098 * Callback for security_inode_init_security() for acquiring xattrs.
3099 */
3100static int shmem_initxattrs(struct inode *inode,
3101 const struct xattr *xattr_array,
3102 void *fs_info)
3103{
3104 struct shmem_inode_info *info = SHMEM_I(inode);
3105 const struct xattr *xattr;
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003106 struct simple_xattr *new_xattr;
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003107 size_t len;
3108
3109 for (xattr = xattr_array; xattr->name != NULL; xattr++) {
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003110 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003111 if (!new_xattr)
3112 return -ENOMEM;
3113
3114 len = strlen(xattr->name) + 1;
3115 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len,
3116 GFP_KERNEL);
3117 if (!new_xattr->name) {
3118 kfree(new_xattr);
3119 return -ENOMEM;
3120 }
3121
3122 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX,
3123 XATTR_SECURITY_PREFIX_LEN);
3124 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN,
3125 xattr->name, len);
3126
Aristeu Rozanski38f38652012-08-23 16:53:28 -04003127 simple_xattr_list_add(&info->xattrs, new_xattr);
Jarkko Sakkinen6d9d88d2012-03-21 16:34:05 -07003128 }
3129
3130 return 0;
3131}
3132
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003133static int shmem_xattr_handler_get(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04003134 struct dentry *unused, struct inode *inode,
3135 const char *name, void *buffer, size_t size)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003136{
Al Virob2968212016-04-10 20:48:24 -04003137 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003138
3139 name = xattr_full_name(handler, name);
3140 return simple_xattr_get(&info->xattrs, name, buffer, size);
3141}
3142
3143static int shmem_xattr_handler_set(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04003144 struct dentry *unused, struct inode *inode,
3145 const char *name, const void *value,
3146 size_t size, int flags)
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003147{
Al Viro59301222016-05-27 10:19:30 -04003148 struct shmem_inode_info *info = SHMEM_I(inode);
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003149
3150 name = xattr_full_name(handler, name);
3151 return simple_xattr_set(&info->xattrs, name, value, size, flags);
3152}
3153
3154static const struct xattr_handler shmem_security_xattr_handler = {
3155 .prefix = XATTR_SECURITY_PREFIX,
3156 .get = shmem_xattr_handler_get,
3157 .set = shmem_xattr_handler_set,
3158};
3159
3160static const struct xattr_handler shmem_trusted_xattr_handler = {
3161 .prefix = XATTR_TRUSTED_PREFIX,
3162 .get = shmem_xattr_handler_get,
3163 .set = shmem_xattr_handler_set,
3164};
3165
Eric Parisb09e0fa2011-05-24 17:12:39 -07003166static const struct xattr_handler *shmem_xattr_handlers[] = {
3167#ifdef CONFIG_TMPFS_POSIX_ACL
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003168 &posix_acl_access_xattr_handler,
3169 &posix_acl_default_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003170#endif
Andreas Gruenbacheraa7c5242015-12-02 14:44:38 +01003171 &shmem_security_xattr_handler,
3172 &shmem_trusted_xattr_handler,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003173 NULL
3174};
3175
Eric Parisb09e0fa2011-05-24 17:12:39 -07003176static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size)
3177{
David Howells75c3cfa2015-03-17 22:26:12 +00003178 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry));
Andreas Gruenbacher786534b2015-12-02 14:44:39 +01003179 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size);
Eric Parisb09e0fa2011-05-24 17:12:39 -07003180}
3181#endif /* CONFIG_TMPFS_XATTR */
3182
Hugh Dickins69f07ec2011-08-03 16:21:26 -07003183static const struct inode_operations shmem_short_symlink_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003184 .get_link = simple_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003185#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003186 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188};
3189
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003190static const struct inode_operations shmem_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05003191 .get_link = shmem_get_link,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003192#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003193 .listxattr = shmem_listxattr,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003194#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003195};
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003196
David M. Grimes91828a42006-10-17 00:09:45 -07003197static struct dentry *shmem_get_parent(struct dentry *child)
3198{
3199 return ERR_PTR(-ESTALE);
3200}
3201
3202static int shmem_match(struct inode *ino, void *vfh)
3203{
3204 __u32 *fh = vfh;
3205 __u64 inum = fh[2];
3206 inum = (inum << 32) | fh[1];
3207 return ino->i_ino == inum && fh[0] == ino->i_generation;
3208}
3209
Amir Goldstein12ba7802018-06-07 17:07:15 -07003210/* Find any alias of inode, but prefer a hashed alias */
3211static struct dentry *shmem_find_alias(struct inode *inode)
3212{
3213 struct dentry *alias = d_find_alias(inode);
3214
3215 return alias ?: d_find_any_alias(inode);
3216}
3217
3218
Christoph Hellwig480b1162007-10-21 16:42:13 -07003219static struct dentry *shmem_fh_to_dentry(struct super_block *sb,
3220 struct fid *fid, int fh_len, int fh_type)
David M. Grimes91828a42006-10-17 00:09:45 -07003221{
David M. Grimes91828a42006-10-17 00:09:45 -07003222 struct inode *inode;
Christoph Hellwig480b1162007-10-21 16:42:13 -07003223 struct dentry *dentry = NULL;
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003224 u64 inum;
David M. Grimes91828a42006-10-17 00:09:45 -07003225
Christoph Hellwig480b1162007-10-21 16:42:13 -07003226 if (fh_len < 3)
3227 return NULL;
3228
Hugh Dickins35c2a7f2012-10-07 20:32:51 -07003229 inum = fid->raw[2];
3230 inum = (inum << 32) | fid->raw[1];
3231
Christoph Hellwig480b1162007-10-21 16:42:13 -07003232 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]),
3233 shmem_match, fid->raw);
David M. Grimes91828a42006-10-17 00:09:45 -07003234 if (inode) {
Amir Goldstein12ba7802018-06-07 17:07:15 -07003235 dentry = shmem_find_alias(inode);
David M. Grimes91828a42006-10-17 00:09:45 -07003236 iput(inode);
3237 }
3238
Christoph Hellwig480b1162007-10-21 16:42:13 -07003239 return dentry;
David M. Grimes91828a42006-10-17 00:09:45 -07003240}
3241
Al Virob0b03822012-04-02 14:34:06 -04003242static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len,
3243 struct inode *parent)
David M. Grimes91828a42006-10-17 00:09:45 -07003244{
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303245 if (*len < 3) {
3246 *len = 3;
Namjae Jeon94e07a752013-02-17 15:48:11 +09003247 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +05303248 }
David M. Grimes91828a42006-10-17 00:09:45 -07003249
Al Viro1d3382cb2010-10-23 15:19:20 -04003250 if (inode_unhashed(inode)) {
David M. Grimes91828a42006-10-17 00:09:45 -07003251 /* Unfortunately insert_inode_hash is not idempotent,
3252 * so as we hash inodes here rather than at creation
3253 * time, we need a lock to ensure we only try
3254 * to do it once
3255 */
3256 static DEFINE_SPINLOCK(lock);
3257 spin_lock(&lock);
Al Viro1d3382cb2010-10-23 15:19:20 -04003258 if (inode_unhashed(inode))
David M. Grimes91828a42006-10-17 00:09:45 -07003259 __insert_inode_hash(inode,
3260 inode->i_ino + inode->i_generation);
3261 spin_unlock(&lock);
3262 }
3263
3264 fh[0] = inode->i_generation;
3265 fh[1] = inode->i_ino;
3266 fh[2] = ((__u64)inode->i_ino) >> 32;
3267
3268 *len = 3;
3269 return 1;
3270}
3271
Christoph Hellwig39655162007-10-21 16:42:17 -07003272static const struct export_operations shmem_export_ops = {
David M. Grimes91828a42006-10-17 00:09:45 -07003273 .get_parent = shmem_get_parent,
David M. Grimes91828a42006-10-17 00:09:45 -07003274 .encode_fh = shmem_encode_fh,
Christoph Hellwig480b1162007-10-21 16:42:13 -07003275 .fh_to_dentry = shmem_fh_to_dentry,
David M. Grimes91828a42006-10-17 00:09:45 -07003276};
3277
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003278static int shmem_parse_options(char *options, struct shmem_sb_info *sbinfo,
3279 bool remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 char *this_char, *value, *rest;
Greg Thelen49cd0a52013-02-22 16:36:02 -08003282 struct mempolicy *mpol = NULL;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003283 uid_t uid;
3284 gid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Hugh Dickinsb00dc3a2006-02-21 23:49:47 +00003286 while (options != NULL) {
3287 this_char = options;
3288 for (;;) {
3289 /*
3290 * NUL-terminate this option: unfortunately,
3291 * mount options form a comma-separated list,
3292 * but mpol's nodelist may also contain commas.
3293 */
3294 options = strchr(options, ',');
3295 if (options == NULL)
3296 break;
3297 options++;
3298 if (!isdigit(*options)) {
3299 options[-1] = '\0';
3300 break;
3301 }
3302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 if (!*this_char)
3304 continue;
3305 if ((value = strchr(this_char,'=')) != NULL) {
3306 *value++ = 0;
3307 } else {
Joe Perches11705322016-03-17 14:19:50 -07003308 pr_err("tmpfs: No value for mount option '%s'\n",
3309 this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003310 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 }
3312
3313 if (!strcmp(this_char,"size")) {
3314 unsigned long long size;
3315 size = memparse(value,&rest);
3316 if (*rest == '%') {
3317 size <<= PAGE_SHIFT;
3318 size *= totalram_pages;
3319 do_div(size, 100);
3320 rest++;
3321 }
3322 if (*rest)
3323 goto bad_val;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003324 sbinfo->max_blocks =
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003325 DIV_ROUND_UP(size, PAGE_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 } else if (!strcmp(this_char,"nr_blocks")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003327 sbinfo->max_blocks = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 if (*rest)
3329 goto bad_val;
3330 } else if (!strcmp(this_char,"nr_inodes")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003331 sbinfo->max_inodes = memparse(value, &rest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 if (*rest)
3333 goto bad_val;
3334 } else if (!strcmp(this_char,"mode")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003335 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 continue;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003337 sbinfo->mode = simple_strtoul(value, &rest, 8) & 07777;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (*rest)
3339 goto bad_val;
3340 } else if (!strcmp(this_char,"uid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003341 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003343 uid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (*rest)
3345 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003346 sbinfo->uid = make_kuid(current_user_ns(), uid);
3347 if (!uid_valid(sbinfo->uid))
3348 goto bad_val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 } else if (!strcmp(this_char,"gid")) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003350 if (remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 continue;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003352 gid = simple_strtoul(value, &rest, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 if (*rest)
3354 goto bad_val;
Eric W. Biederman8751e032012-02-07 16:46:12 -08003355 sbinfo->gid = make_kgid(current_user_ns(), gid);
3356 if (!gid_valid(sbinfo->gid))
3357 goto bad_val;
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003358#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003359 } else if (!strcmp(this_char, "huge")) {
3360 int huge;
3361 huge = shmem_parse_huge(value);
3362 if (huge < 0)
3363 goto bad_val;
3364 if (!has_transparent_hugepage() &&
3365 huge != SHMEM_HUGE_NEVER)
3366 goto bad_val;
3367 sbinfo->huge = huge;
3368#endif
3369#ifdef CONFIG_NUMA
Robin Holt7339ff82006-01-14 13:20:48 -08003370 } else if (!strcmp(this_char,"mpol")) {
Greg Thelen49cd0a52013-02-22 16:36:02 -08003371 mpol_put(mpol);
3372 mpol = NULL;
3373 if (mpol_parse_str(value, &mpol))
Robin Holt7339ff82006-01-14 13:20:48 -08003374 goto bad_val;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 } else {
Joe Perches11705322016-03-17 14:19:50 -07003377 pr_err("tmpfs: Bad mount option %s\n", this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003378 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 }
3380 }
Greg Thelen49cd0a52013-02-22 16:36:02 -08003381 sbinfo->mpol = mpol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return 0;
3383
3384bad_val:
Joe Perches11705322016-03-17 14:19:50 -07003385 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 value, this_char);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003387error:
3388 mpol_put(mpol);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 return 1;
3390
3391}
3392
3393static int shmem_remount_fs(struct super_block *sb, int *flags, char *data)
3394{
3395 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003396 struct shmem_sb_info config = *sbinfo;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003397 unsigned long inodes;
3398 int error = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Greg Thelen5f001102013-02-22 16:36:01 -08003400 config.mpol = NULL;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003401 if (shmem_parse_options(data, &config, true))
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003402 return error;
3403
3404 spin_lock(&sbinfo->stat_lock);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003405 inodes = sbinfo->max_inodes - sbinfo->free_inodes;
Tim Chen7e496292010-08-09 17:19:05 -07003406 if (percpu_counter_compare(&sbinfo->used_blocks, config.max_blocks) > 0)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003407 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003408 if (config.max_inodes < inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003409 goto out;
3410 /*
Hugh Dickins54af60422011-08-03 16:21:24 -07003411 * Those tests disallow limited->unlimited while any are in use;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003412 * but we must separately disallow unlimited->limited, because
3413 * in that case we have no record of how much is already in use.
3414 */
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003415 if (config.max_blocks && !sbinfo->max_blocks)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003416 goto out;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003417 if (config.max_inodes && !sbinfo->max_inodes)
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003418 goto out;
3419
3420 error = 0;
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003421 sbinfo->huge = config.huge;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003422 sbinfo->max_blocks = config.max_blocks;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003423 sbinfo->max_inodes = config.max_inodes;
3424 sbinfo->free_inodes = config.max_inodes - inodes;
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003425
Greg Thelen5f001102013-02-22 16:36:01 -08003426 /*
3427 * Preserve previous mempolicy unless mpol remount option was specified.
3428 */
3429 if (config.mpol) {
3430 mpol_put(sbinfo->mpol);
3431 sbinfo->mpol = config.mpol; /* transfers initial ref */
3432 }
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003433out:
3434 spin_unlock(&sbinfo->stat_lock);
3435 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436}
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003437
Al Viro34c80b12011-12-08 21:32:45 -05003438static int shmem_show_options(struct seq_file *seq, struct dentry *root)
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003439{
Al Viro34c80b12011-12-08 21:32:45 -05003440 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003441
3442 if (sbinfo->max_blocks != shmem_default_max_blocks())
3443 seq_printf(seq, ",size=%luk",
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003444 sbinfo->max_blocks << (PAGE_SHIFT - 10));
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003445 if (sbinfo->max_inodes != shmem_default_max_inodes())
3446 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes);
Joe Perches0825a6f2018-06-14 15:27:58 -07003447 if (sbinfo->mode != (0777 | S_ISVTX))
Al Viro09208d12011-07-26 03:15:03 -04003448 seq_printf(seq, ",mode=%03ho", sbinfo->mode);
Eric W. Biederman8751e032012-02-07 16:46:12 -08003449 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
3450 seq_printf(seq, ",uid=%u",
3451 from_kuid_munged(&init_user_ns, sbinfo->uid));
3452 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
3453 seq_printf(seq, ",gid=%u",
3454 from_kgid_munged(&init_user_ns, sbinfo->gid));
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003455#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003456 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3457 if (sbinfo->huge)
3458 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge));
3459#endif
Lee Schermerhorn71fe8042008-04-28 02:13:26 -07003460 shmem_show_mpol(seq, sbinfo->mpol);
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003461 return 0;
3462}
David Herrmann9183df22014-08-08 14:25:29 -07003463
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003464#endif /* CONFIG_TMPFS */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
3466static void shmem_put_super(struct super_block *sb)
3467{
Hugh Dickins602586a2010-08-17 15:23:56 -07003468 struct shmem_sb_info *sbinfo = SHMEM_SB(sb);
3469
3470 percpu_counter_destroy(&sbinfo->used_blocks);
Greg Thelen49cd0a52013-02-22 16:36:02 -08003471 mpol_put(sbinfo->mpol);
Hugh Dickins602586a2010-08-17 15:23:56 -07003472 kfree(sbinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 sb->s_fs_info = NULL;
3474}
3475
Kay Sievers2b2af542009-04-30 15:23:42 +02003476int shmem_fill_super(struct super_block *sb, void *data, int silent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 struct inode *inode;
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003479 struct shmem_sb_info *sbinfo;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003480 int err = -ENOMEM;
3481
3482 /* Round up to L1_CACHE_BYTES to resist false sharing */
Pekka Enberg425fbf02009-09-21 17:03:50 -07003483 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info),
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003484 L1_CACHE_BYTES), GFP_KERNEL);
3485 if (!sbinfo)
3486 return -ENOMEM;
3487
Joe Perches0825a6f2018-06-14 15:27:58 -07003488 sbinfo->mode = 0777 | S_ISVTX;
David Howells76aac0e2008-11-14 10:39:12 +11003489 sbinfo->uid = current_fsuid();
3490 sbinfo->gid = current_fsgid();
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003491 sb->s_fs_info = sbinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003493#ifdef CONFIG_TMPFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 /*
3495 * Per default we only allow half of the physical ram per
3496 * tmpfs instance, limiting inodes to one per page of lowmem;
3497 * but the internal instance is left unlimited.
3498 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003499 if (!(sb->s_flags & SB_KERNMOUNT)) {
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003500 sbinfo->max_blocks = shmem_default_max_blocks();
3501 sbinfo->max_inodes = shmem_default_max_inodes();
3502 if (shmem_parse_options(data, sbinfo, false)) {
3503 err = -EINVAL;
3504 goto failed;
3505 }
Al Viroca4e0512013-08-31 12:57:10 -04003506 } else {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003507 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
David M. Grimes91828a42006-10-17 00:09:45 -07003509 sb->s_export_op = &shmem_export_ops;
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003510 sb->s_flags |= SB_NOSEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511#else
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003512 sb->s_flags |= SB_NOUSER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513#endif
3514
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003515 spin_lock_init(&sbinfo->stat_lock);
Tejun Heo908c7f12014-09-08 09:51:29 +09003516 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL))
Hugh Dickins602586a2010-08-17 15:23:56 -07003517 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003518 sbinfo->free_inodes = sbinfo->max_inodes;
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003519 spin_lock_init(&sbinfo->shrinklist_lock);
3520 INIT_LIST_HEAD(&sbinfo->shrinklist);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003521
Hugh Dickins285b2c42011-08-03 16:21:20 -07003522 sb->s_maxbytes = MAX_LFS_FILESIZE;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003523 sb->s_blocksize = PAGE_SIZE;
3524 sb->s_blocksize_bits = PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 sb->s_magic = TMPFS_MAGIC;
3526 sb->s_op = &shmem_ops;
Robin H. Johnsoncfd95a92006-06-12 21:50:25 +01003527 sb->s_time_gran = 1;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003528#ifdef CONFIG_TMPFS_XATTR
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003529 sb->s_xattr = shmem_xattr_handlers;
Eric Parisb09e0fa2011-05-24 17:12:39 -07003530#endif
3531#ifdef CONFIG_TMPFS_POSIX_ACL
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003532 sb->s_flags |= SB_POSIXACL;
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003533#endif
Amir Goldstein2b4db792017-05-18 15:29:33 +03003534 uuid_gen(&sb->s_uuid);
Hugh Dickins0edd73b2005-06-21 17:15:04 -07003535
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003536 inode = shmem_get_inode(sb, NULL, S_IFDIR | sbinfo->mode, 0, VM_NORESERVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if (!inode)
3538 goto failed;
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003539 inode->i_uid = sbinfo->uid;
3540 inode->i_gid = sbinfo->gid;
Al Viro318ceed2012-02-12 22:08:01 -05003541 sb->s_root = d_make_root(inode);
3542 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -05003543 goto failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return 0;
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546failed:
3547 shmem_put_super(sb);
3548 return err;
3549}
3550
Pekka Enbergfcc234f2006-03-22 00:08:13 -08003551static struct kmem_cache *shmem_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552
3553static struct inode *shmem_alloc_inode(struct super_block *sb)
3554{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003555 struct shmem_inode_info *info;
3556 info = kmem_cache_alloc(shmem_inode_cachep, GFP_KERNEL);
3557 if (!info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return NULL;
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003559 return &info->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560}
3561
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003562static void shmem_destroy_callback(struct rcu_head *head)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003563{
3564 struct inode *inode = container_of(head, struct inode, i_rcu);
Al Viro84e710d2016-04-15 00:58:55 -04003565 if (S_ISLNK(inode->i_mode))
3566 kfree(inode->i_link);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11003567 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode));
3568}
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570static void shmem_destroy_inode(struct inode *inode)
3571{
Al Viro09208d12011-07-26 03:15:03 -04003572 if (S_ISREG(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 mpol_free_shared_policy(&SHMEM_I(inode)->policy);
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003574 call_rcu(&inode->i_rcu, shmem_destroy_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575}
3576
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003577static void shmem_init_inode(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003579 struct shmem_inode_info *info = foo;
3580 inode_init_once(&info->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003583static void shmem_init_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
3586 sizeof(struct shmem_inode_info),
Vladimir Davydov5d097052016-01-14 15:18:21 -08003587 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588}
3589
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003590static void shmem_destroy_inodecache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07003592 kmem_cache_destroy(shmem_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07003595static const struct address_space_operations shmem_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .writepage = shmem_writepage,
Ken Chen76719322007-02-10 01:43:15 -08003597 .set_page_dirty = __set_page_dirty_no_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598#ifdef CONFIG_TMPFS
Nick Piggin800d15a2007-10-16 01:25:03 -07003599 .write_begin = shmem_write_begin,
3600 .write_end = shmem_write_end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601#endif
Andrew Morton1c939232014-10-09 15:27:59 -07003602#ifdef CONFIG_MIGRATION
Lee Schermerhorn304dbdb2006-04-22 02:35:48 -07003603 .migratepage = migrate_page,
Andrew Morton1c939232014-10-09 15:27:59 -07003604#endif
Andi Kleenaa261f52009-09-16 11:50:16 +02003605 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606};
3607
Helge Deller15ad7cd2006-12-06 20:40:36 -08003608static const struct file_operations shmem_file_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 .mmap = shmem_mmap,
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003610 .get_unmapped_area = shmem_get_unmapped_area,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611#ifdef CONFIG_TMPFS
Hugh Dickins220f2ac2012-12-12 13:52:21 -08003612 .llseek = shmem_file_llseek,
Al Viro2ba5bbe2014-04-02 20:00:02 -04003613 .read_iter = shmem_file_read_iter,
Al Viro81742022014-04-03 03:17:43 -04003614 .write_iter = generic_file_write_iter,
Christoph Hellwig1b061d92010-05-26 17:53:41 +02003615 .fsync = noop_fsync,
Al Viro82c156f2016-09-22 23:35:42 -04003616 .splice_read = generic_file_splice_read,
Al Virof6cb85d2014-04-05 04:38:56 -04003617 .splice_write = iter_file_splice_write,
Hugh Dickins83e4fa92012-05-29 15:06:40 -07003618 .fallocate = shmem_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619#endif
3620};
3621
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003622static const struct inode_operations shmem_inode_operations = {
Yu Zhao44a30222015-09-08 15:03:33 -07003623 .getattr = shmem_getattr,
Hugh Dickins94c1e622011-06-27 16:18:03 -07003624 .setattr = shmem_setattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003625#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003626 .listxattr = shmem_listxattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003627 .set_acl = simple_set_acl,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003628#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629};
3630
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003631static const struct inode_operations shmem_dir_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632#ifdef CONFIG_TMPFS
3633 .create = shmem_create,
3634 .lookup = simple_lookup,
3635 .link = shmem_link,
3636 .unlink = shmem_unlink,
3637 .symlink = shmem_symlink,
3638 .mkdir = shmem_mkdir,
3639 .rmdir = shmem_rmdir,
3640 .mknod = shmem_mknod,
Miklos Szeredi2773bf02016-09-27 11:03:58 +02003641 .rename = shmem_rename2,
Al Viro60545d02013-06-07 01:20:27 -04003642 .tmpfile = shmem_tmpfile,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643#endif
Eric Parisb09e0fa2011-05-24 17:12:39 -07003644#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003645 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003646#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003647#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003648 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003649 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003650#endif
3651};
3652
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08003653static const struct inode_operations shmem_special_inode_operations = {
Eric Parisb09e0fa2011-05-24 17:12:39 -07003654#ifdef CONFIG_TMPFS_XATTR
Eric Parisb09e0fa2011-05-24 17:12:39 -07003655 .listxattr = shmem_listxattr,
Eric Parisb09e0fa2011-05-24 17:12:39 -07003656#endif
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003657#ifdef CONFIG_TMPFS_POSIX_ACL
Hugh Dickins94c1e622011-06-27 16:18:03 -07003658 .setattr = shmem_setattr,
Christoph Hellwigfeda8212013-12-20 05:16:54 -08003659 .set_acl = simple_set_acl,
Andreas Gruenbacher39f02472006-09-29 02:01:35 -07003660#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661};
3662
Hugh Dickins759b9772007-03-05 00:30:28 -08003663static const struct super_operations shmem_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 .alloc_inode = shmem_alloc_inode,
3665 .destroy_inode = shmem_destroy_inode,
3666#ifdef CONFIG_TMPFS
3667 .statfs = shmem_statfs,
3668 .remount_fs = shmem_remount_fs,
akpm@linux-foundation.org680d7942008-02-08 04:21:48 -08003669 .show_options = shmem_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670#endif
Al Viro1f895f72010-06-05 19:10:41 -04003671 .evict_inode = shmem_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 .drop_inode = generic_delete_inode,
3673 .put_super = shmem_put_super,
Kirill A. Shutemov779750d2016-07-26 15:26:38 -07003674#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3675 .nr_cached_objects = shmem_unused_huge_count,
3676 .free_cached_objects = shmem_unused_huge_scan,
3677#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678};
3679
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04003680static const struct vm_operations_struct shmem_vm_ops = {
Nick Piggin54cb8822007-07-19 01:46:59 -07003681 .fault = shmem_fault,
Ning Qud7c17552014-04-07 15:37:24 -07003682 .map_pages = filemap_map_pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683#ifdef CONFIG_NUMA
3684 .set_policy = shmem_set_policy,
3685 .get_policy = shmem_get_policy,
3686#endif
3687};
3688
Al Viro3c26ff62010-07-25 11:46:36 +04003689static struct dentry *shmem_mount(struct file_system_type *fs_type,
3690 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Al Viro3c26ff62010-07-25 11:46:36 +04003692 return mount_nodev(fs_type, flags, data, shmem_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693}
3694
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003695static struct file_system_type shmem_fs_type = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 .owner = THIS_MODULE,
3697 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003698 .mount = shmem_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003700 .fs_flags = FS_USERNS_MOUNT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003703int __init shmem_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704{
3705 int error;
3706
Rob Landley16203a72013-09-11 14:26:12 -07003707 /* If rootfs called this, don't re-init */
3708 if (shmem_inode_cachep)
3709 return 0;
3710
weiping zhang9a8ec03e2017-11-15 17:38:18 -08003711 shmem_init_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003713 error = register_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 if (error) {
Joe Perches11705322016-03-17 14:19:50 -07003715 pr_err("Could not register tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 goto out2;
3717 }
Greg Kroah-Hartman95dc1122005-06-20 21:15:16 -07003718
Al Viroca4e0512013-08-31 12:57:10 -04003719 shm_mnt = kern_mount(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (IS_ERR(shm_mnt)) {
3721 error = PTR_ERR(shm_mnt);
Joe Perches11705322016-03-17 14:19:50 -07003722 pr_err("Could not kern_mount tmpfs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 goto out1;
3724 }
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003725
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003726#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003727 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003728 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3729 else
3730 shmem_huge = 0; /* just in case it was patched */
3731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 return 0;
3733
3734out1:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003735 unregister_filesystem(&shmem_fs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736out2:
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003737 shmem_destroy_inodecache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 shm_mnt = ERR_PTR(error);
3739 return error;
3740}
Matt Mackall853ac432009-01-06 14:40:20 -08003741
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003742#if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003743static ssize_t shmem_enabled_show(struct kobject *kobj,
3744 struct kobj_attribute *attr, char *buf)
3745{
3746 int values[] = {
3747 SHMEM_HUGE_ALWAYS,
3748 SHMEM_HUGE_WITHIN_SIZE,
3749 SHMEM_HUGE_ADVISE,
3750 SHMEM_HUGE_NEVER,
3751 SHMEM_HUGE_DENY,
3752 SHMEM_HUGE_FORCE,
3753 };
3754 int i, count;
3755
3756 for (i = 0, count = 0; i < ARRAY_SIZE(values); i++) {
3757 const char *fmt = shmem_huge == values[i] ? "[%s] " : "%s ";
3758
3759 count += sprintf(buf + count, fmt,
3760 shmem_format_huge(values[i]));
3761 }
3762 buf[count - 1] = '\n';
3763 return count;
3764}
3765
3766static ssize_t shmem_enabled_store(struct kobject *kobj,
3767 struct kobj_attribute *attr, const char *buf, size_t count)
3768{
3769 char tmp[16];
3770 int huge;
3771
3772 if (count + 1 > sizeof(tmp))
3773 return -EINVAL;
3774 memcpy(tmp, buf, count);
3775 tmp[count] = '\0';
3776 if (count && tmp[count - 1] == '\n')
3777 tmp[count - 1] = '\0';
3778
3779 huge = shmem_parse_huge(tmp);
3780 if (huge == -EINVAL)
3781 return -EINVAL;
3782 if (!has_transparent_hugepage() &&
3783 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY)
3784 return -EINVAL;
3785
3786 shmem_huge = huge;
Kirill A. Shutemov435c0b872017-08-25 15:55:33 -07003787 if (shmem_huge > SHMEM_HUGE_DENY)
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003788 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge;
3789 return count;
3790}
3791
3792struct kobj_attribute shmem_enabled_attr =
3793 __ATTR(shmem_enabled, 0644, shmem_enabled_show, shmem_enabled_store);
Arnd Bergmann3b337192016-08-10 16:27:44 -07003794#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003795
Arnd Bergmann3b337192016-08-10 16:27:44 -07003796#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003797bool shmem_huge_enabled(struct vm_area_struct *vma)
3798{
3799 struct inode *inode = file_inode(vma->vm_file);
3800 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb);
3801 loff_t i_size;
3802 pgoff_t off;
3803
3804 if (shmem_huge == SHMEM_HUGE_FORCE)
3805 return true;
3806 if (shmem_huge == SHMEM_HUGE_DENY)
3807 return false;
3808 switch (sbinfo->huge) {
3809 case SHMEM_HUGE_NEVER:
3810 return false;
3811 case SHMEM_HUGE_ALWAYS:
3812 return true;
3813 case SHMEM_HUGE_WITHIN_SIZE:
3814 off = round_up(vma->vm_pgoff, HPAGE_PMD_NR);
3815 i_size = round_up(i_size_read(inode), PAGE_SIZE);
3816 if (i_size >= HPAGE_PMD_SIZE &&
3817 i_size >> PAGE_SHIFT >= off)
3818 return true;
Gustavo A. R. Silvac8402872017-11-15 17:38:45 -08003819 /* fall through */
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003820 case SHMEM_HUGE_ADVISE:
3821 /* TODO: implement fadvise() hints */
3822 return (vma->vm_flags & VM_HUGEPAGE);
3823 default:
3824 VM_BUG_ON(1);
3825 return false;
3826 }
3827}
Arnd Bergmann3b337192016-08-10 16:27:44 -07003828#endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
Kirill A. Shutemov5a6e75f2016-07-26 15:26:13 -07003829
Matt Mackall853ac432009-01-06 14:40:20 -08003830#else /* !CONFIG_SHMEM */
3831
3832/*
3833 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3834 *
3835 * This is intended for small system where the benefits of the full
3836 * shmem code (swap-backed and resource-limited) are outweighed by
3837 * their complexity. On systems without swap this code should be
3838 * effectively equivalent, but much lighter weight.
3839 */
3840
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003841static struct file_system_type shmem_fs_type = {
Matt Mackall853ac432009-01-06 14:40:20 -08003842 .name = "tmpfs",
Al Viro3c26ff62010-07-25 11:46:36 +04003843 .mount = ramfs_mount,
Matt Mackall853ac432009-01-06 14:40:20 -08003844 .kill_sb = kill_litter_super,
Eric W. Biederman2b8576c2013-01-25 16:32:10 -08003845 .fs_flags = FS_USERNS_MOUNT,
Matt Mackall853ac432009-01-06 14:40:20 -08003846};
3847
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003848int __init shmem_init(void)
Matt Mackall853ac432009-01-06 14:40:20 -08003849{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003850 BUG_ON(register_filesystem(&shmem_fs_type) != 0);
Matt Mackall853ac432009-01-06 14:40:20 -08003851
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003852 shm_mnt = kern_mount(&shmem_fs_type);
Matt Mackall853ac432009-01-06 14:40:20 -08003853 BUG_ON(IS_ERR(shm_mnt));
3854
3855 return 0;
3856}
3857
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003858int shmem_unuse(swp_entry_t swap, struct page *page)
Matt Mackall853ac432009-01-06 14:40:20 -08003859{
3860 return 0;
3861}
3862
Hugh Dickins3f96b792009-09-21 17:03:37 -07003863int shmem_lock(struct file *file, int lock, struct user_struct *user)
3864{
3865 return 0;
3866}
3867
Hugh Dickins24513262012-01-20 14:34:21 -08003868void shmem_unlock_mapping(struct address_space *mapping)
3869{
3870}
3871
Hugh Dickinsc01d5b32016-07-26 15:26:15 -07003872#ifdef CONFIG_MMU
3873unsigned long shmem_get_unmapped_area(struct file *file,
3874 unsigned long addr, unsigned long len,
3875 unsigned long pgoff, unsigned long flags)
3876{
3877 return current->mm->get_unmapped_area(file, addr, len, pgoff, flags);
3878}
3879#endif
3880
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003881void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend)
Hugh Dickins94c1e622011-06-27 16:18:03 -07003882{
Hugh Dickins41ffe5d2011-08-03 16:21:21 -07003883 truncate_inode_pages_range(inode->i_mapping, lstart, lend);
Hugh Dickins94c1e622011-06-27 16:18:03 -07003884}
3885EXPORT_SYMBOL_GPL(shmem_truncate_range);
3886
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003887#define shmem_vm_ops generic_file_vm_ops
3888#define shmem_file_operations ramfs_file_operations
Dmitry Monakhov454abaf2010-03-04 17:32:18 +03003889#define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
Hugh Dickins0b0a0802009-02-24 20:51:52 +00003890#define shmem_acct_size(flags, size) 0
3891#define shmem_unacct_size(flags, size) do {} while (0)
Matt Mackall853ac432009-01-06 14:40:20 -08003892
3893#endif /* CONFIG_SHMEM */
3894
3895/* common code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Matthew Auld703321b2017-10-06 23:18:13 +01003897static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, loff_t size,
Eric Parisc7277092013-12-02 11:24:19 +00003898 unsigned long flags, unsigned int i_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 struct inode *inode;
Al Viro93dec2d2018-07-08 23:02:03 -04003901 struct file *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902
Matthew Auld703321b2017-10-06 23:18:13 +01003903 if (IS_ERR(mnt))
3904 return ERR_CAST(mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Hugh Dickins285b2c42011-08-03 16:21:20 -07003906 if (size < 0 || size > MAX_LFS_FILESIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 return ERR_PTR(-EINVAL);
3908
3909 if (shmem_acct_size(flags, size))
3910 return ERR_PTR(-ENOMEM);
3911
Al Viro93dec2d2018-07-08 23:02:03 -04003912 inode = shmem_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0,
3913 flags);
Al Virodac2d1f2018-06-09 09:27:41 -04003914 if (unlikely(!inode)) {
3915 shmem_unacct_size(flags, size);
3916 return ERR_PTR(-ENOSPC);
3917 }
Eric Parisc7277092013-12-02 11:24:19 +00003918 inode->i_flags |= i_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 inode->i_size = size;
Miklos Szeredi6d6b77f2011-10-28 14:13:28 +02003920 clear_nlink(inode); /* It is unlinked */
Al Viro26567cd2013-03-01 20:22:53 -05003921 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size));
Al Viro93dec2d2018-07-08 23:02:03 -04003922 if (!IS_ERR(res))
3923 res = alloc_file_pseudo(inode, mnt, name, O_RDWR,
3924 &shmem_file_operations);
Al Viro26567cd2013-03-01 20:22:53 -05003925 if (IS_ERR(res))
Al Viro93dec2d2018-07-08 23:02:03 -04003926 iput(inode);
Al Viro6b4d0b22013-02-14 21:37:26 -05003927 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
Eric Parisc7277092013-12-02 11:24:19 +00003929
3930/**
3931 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3932 * kernel internal. There will be NO LSM permission checks against the
3933 * underlying inode. So users of this interface must do LSM checks at a
Stephen Smalleye1832f22015-08-06 15:46:55 -07003934 * higher layer. The users are the big_key and shm implementations. LSM
3935 * checks are provided at the key or shm level rather than the inode.
Eric Parisc7277092013-12-02 11:24:19 +00003936 * @name: name for dentry (to be seen in /proc/<pid>/maps
3937 * @size: size to be set for the file
3938 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3939 */
3940struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags)
3941{
Matthew Auld703321b2017-10-06 23:18:13 +01003942 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE);
Eric Parisc7277092013-12-02 11:24:19 +00003943}
3944
3945/**
3946 * shmem_file_setup - get an unlinked file living in tmpfs
3947 * @name: name for dentry (to be seen in /proc/<pid>/maps
3948 * @size: size to be set for the file
3949 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3950 */
3951struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags)
3952{
Matthew Auld703321b2017-10-06 23:18:13 +01003953 return __shmem_file_setup(shm_mnt, name, size, flags, 0);
Eric Parisc7277092013-12-02 11:24:19 +00003954}
Keith Packard395e0dd2008-06-20 00:08:06 -07003955EXPORT_SYMBOL_GPL(shmem_file_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956
Randy Dunlap46711812008-03-19 17:00:41 -07003957/**
Matthew Auld703321b2017-10-06 23:18:13 +01003958 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3959 * @mnt: the tmpfs mount where the file will be created
3960 * @name: name for dentry (to be seen in /proc/<pid>/maps
3961 * @size: size to be set for the file
3962 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3963 */
3964struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name,
3965 loff_t size, unsigned long flags)
3966{
3967 return __shmem_file_setup(mnt, name, size, flags, 0);
3968}
3969EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt);
3970
3971/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 * shmem_zero_setup - setup a shared anonymous mapping
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3974 */
3975int shmem_zero_setup(struct vm_area_struct *vma)
3976{
3977 struct file *file;
3978 loff_t size = vma->vm_end - vma->vm_start;
3979
Hugh Dickins66fc1302015-06-14 09:48:09 -07003980 /*
3981 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3982 * between XFS directory reading and selinux: since this file is only
3983 * accessible to the user through its mapping, use S_PRIVATE flag to
3984 * bypass file security, in the same way as shmem_kernel_file_setup().
3985 */
Matthew Auld703321b2017-10-06 23:18:13 +01003986 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 if (IS_ERR(file))
3988 return PTR_ERR(file);
3989
3990 if (vma->vm_file)
3991 fput(vma->vm_file);
3992 vma->vm_file = file;
3993 vma->vm_ops = &shmem_vm_ops;
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003994
Kirill A. Shutemove496cf32016-07-26 15:26:35 -07003995 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE) &&
Kirill A. Shutemovf3f0e1d2016-07-26 15:26:32 -07003996 ((vma->vm_start + ~HPAGE_PMD_MASK) & HPAGE_PMD_MASK) <
3997 (vma->vm_end & HPAGE_PMD_MASK)) {
3998 khugepaged_enter(vma, vma->vm_flags);
3999 }
4000
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 return 0;
4002}
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004003
4004/**
4005 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4006 * @mapping: the page's address_space
4007 * @index: the page index
4008 * @gfp: the page allocator flags to use if allocating
4009 *
4010 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4011 * with any new page allocations done using the specified allocation flags.
4012 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4013 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4014 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4015 *
Hugh Dickins68da9f02011-07-25 17:12:34 -07004016 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4017 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004018 */
4019struct page *shmem_read_mapping_page_gfp(struct address_space *mapping,
4020 pgoff_t index, gfp_t gfp)
4021{
Hugh Dickins68da9f02011-07-25 17:12:34 -07004022#ifdef CONFIG_SHMEM
4023 struct inode *inode = mapping->host;
Hugh Dickins9276aad2011-07-25 17:12:34 -07004024 struct page *page;
Hugh Dickins68da9f02011-07-25 17:12:34 -07004025 int error;
4026
4027 BUG_ON(mapping->a_ops != &shmem_aops);
Andres Lagar-Cavilla9e18eb22016-05-19 17:12:47 -07004028 error = shmem_getpage_gfp(inode, index, &page, SGP_CACHE,
Mike Rapoportcfda0522017-02-22 15:43:37 -08004029 gfp, NULL, NULL, NULL);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004030 if (error)
4031 page = ERR_PTR(error);
4032 else
4033 unlock_page(page);
4034 return page;
4035#else
4036 /*
4037 * The tiny !SHMEM case uses ramfs without swap
4038 */
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004039 return read_cache_page_gfp(mapping, index, gfp);
Hugh Dickins68da9f02011-07-25 17:12:34 -07004040#endif
Hugh Dickinsd9d90e52011-06-27 16:18:04 -07004041}
4042EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp);