2 * Resizable virtual memory filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
6 * 2000-2001 Christoph Rohland
9 * Copyright (C) 2002-2011 Hugh Dickins.
10 * Copyright (C) 2011 Google Inc.
11 * Copyright (C) 2002-2005 VERITAS Software Corporation.
12 * Copyright (C) 2004 Andi Kleen, SuSE Labs
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>
19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com>
21 * This file is released under the GPL.
25 #include <linux/init.h>
26 #include <linux/vfs.h>
27 #include <linux/mount.h>
28 #include <linux/ramfs.h>
29 #include <linux/pagemap.h>
30 #include <linux/file.h>
32 #include <linux/random.h>
33 #include <linux/sched/signal.h>
34 #include <linux/export.h>
35 #include <linux/swap.h>
36 #include <linux/uio.h>
37 #include <linux/khugepaged.h>
38 #include <linux/hugetlb.h>
40 #include <asm/tlbflush.h> /* for arch/microblaze update_mmu_cache() */
42 static struct vfsmount
*shm_mnt
;
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.
51 #include <linux/xattr.h>
52 #include <linux/exportfs.h>
53 #include <linux/posix_acl.h>
54 #include <linux/posix_acl_xattr.h>
55 #include <linux/mman.h>
56 #include <linux/string.h>
57 #include <linux/slab.h>
58 #include <linux/backing-dev.h>
59 #include <linux/shmem_fs.h>
60 #include <linux/writeback.h>
61 #include <linux/blkdev.h>
62 #include <linux/pagevec.h>
63 #include <linux/percpu_counter.h>
64 #include <linux/falloc.h>
65 #include <linux/splice.h>
66 #include <linux/security.h>
67 #include <linux/swapops.h>
68 #include <linux/mempolicy.h>
69 #include <linux/namei.h>
70 #include <linux/ctype.h>
71 #include <linux/migrate.h>
72 #include <linux/highmem.h>
73 #include <linux/seq_file.h>
74 #include <linux/magic.h>
75 #include <linux/syscalls.h>
76 #include <linux/fcntl.h>
77 #include <uapi/linux/memfd.h>
78 #include <linux/userfaultfd_k.h>
79 #include <linux/rmap.h>
80 #include <linux/uuid.h>
82 #include <linux/uaccess.h>
83 #include <asm/pgtable.h>
87 #define BLOCKS_PER_PAGE (PAGE_SIZE/512)
88 #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT)
90 /* Pretend that each entry is of this size in directory's i_size */
91 #define BOGO_DIRENT_SIZE 20
93 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */
94 #define SHORT_SYMLINK_LEN 128
97 * 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.
101 struct shmem_falloc
{
102 wait_queue_head_t
*waitq
; /* faults into hole wait for punch to end */
103 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 */
110 static unsigned long shmem_default_max_blocks(void)
112 return totalram_pages() / 2;
115 static int shmem_default_max_inodes(void)
117 unsigned long nr_pages
= totalram_pages();
121 ul
= min3(ul
, nr_pages
- totalhigh_pages(), nr_pages
/ 2);
126 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
);
127 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
128 struct shmem_inode_info
*info
, pgoff_t index
);
129 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
130 struct page
**pagep
, enum sgp_type sgp
,
131 gfp_t gfp
, struct vm_area_struct
*vma
,
132 struct vm_fault
*vmf
, vm_fault_t
*fault_type
);
134 int shmem_getpage(struct inode
*inode
, pgoff_t index
,
135 struct page
**pagep
, enum sgp_type sgp
)
137 return shmem_getpage_gfp(inode
, index
, pagep
, sgp
,
138 mapping_gfp_mask(inode
->i_mapping
), NULL
, NULL
, NULL
);
141 static inline struct shmem_sb_info
*SHMEM_SB(struct super_block
*sb
)
143 return sb
->s_fs_info
;
147 * shmem_file_setup pre-accounts the whole fixed size of a VM object,
148 * for shared memory and for shared anonymous (/dev/zero) mappings
149 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1),
150 * consistent with the pre-accounting of private mappings ...
152 static inline int shmem_acct_size(unsigned long flags
, loff_t size
)
154 return (flags
& VM_NORESERVE
) ?
155 0 : security_vm_enough_memory_mm(current
->mm
, VM_ACCT(size
));
158 static inline void shmem_unacct_size(unsigned long flags
, loff_t size
)
160 if (!(flags
& VM_NORESERVE
))
161 vm_unacct_memory(VM_ACCT(size
));
164 static inline int shmem_reacct_size(unsigned long flags
,
165 loff_t oldsize
, loff_t newsize
)
167 if (!(flags
& VM_NORESERVE
)) {
168 if (VM_ACCT(newsize
) > VM_ACCT(oldsize
))
169 return security_vm_enough_memory_mm(current
->mm
,
170 VM_ACCT(newsize
) - VM_ACCT(oldsize
));
171 else if (VM_ACCT(newsize
) < VM_ACCT(oldsize
))
172 vm_unacct_memory(VM_ACCT(oldsize
) - VM_ACCT(newsize
));
178 * ... whereas tmpfs objects are accounted incrementally as
179 * pages are allocated, in order to allow large sparse files.
180 * shmem_getpage reports shmem_acct_block failure as -ENOSPC not -ENOMEM,
181 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM.
183 static inline int shmem_acct_block(unsigned long flags
, long pages
)
185 if (!(flags
& VM_NORESERVE
))
188 return security_vm_enough_memory_mm(current
->mm
,
189 pages
* VM_ACCT(PAGE_SIZE
));
192 static inline void shmem_unacct_blocks(unsigned long flags
, long pages
)
194 if (flags
& VM_NORESERVE
)
195 vm_unacct_memory(pages
* VM_ACCT(PAGE_SIZE
));
198 static inline bool shmem_inode_acct_block(struct inode
*inode
, long pages
)
200 struct shmem_inode_info
*info
= SHMEM_I(inode
);
201 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
203 if (shmem_acct_block(info
->flags
, pages
))
206 if (sbinfo
->max_blocks
) {
207 if (percpu_counter_compare(&sbinfo
->used_blocks
,
208 sbinfo
->max_blocks
- pages
) > 0)
210 percpu_counter_add(&sbinfo
->used_blocks
, pages
);
216 shmem_unacct_blocks(info
->flags
, pages
);
220 static inline void shmem_inode_unacct_blocks(struct inode
*inode
, long pages
)
222 struct shmem_inode_info
*info
= SHMEM_I(inode
);
223 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
225 if (sbinfo
->max_blocks
)
226 percpu_counter_sub(&sbinfo
->used_blocks
, pages
);
227 shmem_unacct_blocks(info
->flags
, pages
);
230 static const struct super_operations shmem_ops
;
231 static const struct address_space_operations shmem_aops
;
232 static const struct file_operations shmem_file_operations
;
233 static const struct inode_operations shmem_inode_operations
;
234 static const struct inode_operations shmem_dir_inode_operations
;
235 static const struct inode_operations shmem_special_inode_operations
;
236 static const struct vm_operations_struct shmem_vm_ops
;
237 static struct file_system_type shmem_fs_type
;
239 bool vma_is_shmem(struct vm_area_struct
*vma
)
241 return vma
->vm_ops
== &shmem_vm_ops
;
244 static LIST_HEAD(shmem_swaplist
);
245 static DEFINE_MUTEX(shmem_swaplist_mutex
);
247 static int shmem_reserve_inode(struct super_block
*sb
)
249 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
250 if (sbinfo
->max_inodes
) {
251 spin_lock(&sbinfo
->stat_lock
);
252 if (!sbinfo
->free_inodes
) {
253 spin_unlock(&sbinfo
->stat_lock
);
256 sbinfo
->free_inodes
--;
257 spin_unlock(&sbinfo
->stat_lock
);
262 static void shmem_free_inode(struct super_block
*sb
)
264 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
265 if (sbinfo
->max_inodes
) {
266 spin_lock(&sbinfo
->stat_lock
);
267 sbinfo
->free_inodes
++;
268 spin_unlock(&sbinfo
->stat_lock
);
273 * shmem_recalc_inode - recalculate the block usage of an inode
274 * @inode: inode to recalc
276 * We have to calculate the free blocks since the mm can drop
277 * undirtied hole pages behind our back.
279 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped
280 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped)
282 * It has to be called with the spinlock held.
284 static void shmem_recalc_inode(struct inode
*inode
)
286 struct shmem_inode_info
*info
= SHMEM_I(inode
);
289 freed
= info
->alloced
- info
->swapped
- inode
->i_mapping
->nrpages
;
291 info
->alloced
-= freed
;
292 inode
->i_blocks
-= freed
* BLOCKS_PER_PAGE
;
293 shmem_inode_unacct_blocks(inode
, freed
);
297 bool shmem_charge(struct inode
*inode
, long pages
)
299 struct shmem_inode_info
*info
= SHMEM_I(inode
);
302 if (!shmem_inode_acct_block(inode
, pages
))
305 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */
306 inode
->i_mapping
->nrpages
+= pages
;
308 spin_lock_irqsave(&info
->lock
, flags
);
309 info
->alloced
+= pages
;
310 inode
->i_blocks
+= pages
* BLOCKS_PER_PAGE
;
311 shmem_recalc_inode(inode
);
312 spin_unlock_irqrestore(&info
->lock
, flags
);
317 void shmem_uncharge(struct inode
*inode
, long pages
)
319 struct shmem_inode_info
*info
= SHMEM_I(inode
);
322 /* nrpages adjustment done by __delete_from_page_cache() or caller */
324 spin_lock_irqsave(&info
->lock
, flags
);
325 info
->alloced
-= pages
;
326 inode
->i_blocks
-= pages
* BLOCKS_PER_PAGE
;
327 shmem_recalc_inode(inode
);
328 spin_unlock_irqrestore(&info
->lock
, flags
);
330 shmem_inode_unacct_blocks(inode
, pages
);
334 * Replace item expected in xarray by a new item, while holding xa_lock.
336 static int shmem_replace_entry(struct address_space
*mapping
,
337 pgoff_t index
, void *expected
, void *replacement
)
339 XA_STATE(xas
, &mapping
->i_pages
, index
);
342 VM_BUG_ON(!expected
);
343 VM_BUG_ON(!replacement
);
344 item
= xas_load(&xas
);
345 if (item
!= expected
)
347 xas_store(&xas
, replacement
);
352 * Sometimes, before we decide whether to proceed or to fail, we must check
353 * that an entry was not already brought back from swap by a racing thread.
355 * Checking page is not enough: by the time a SwapCache page is locked, it
356 * might be reused, and again be SwapCache, using the same swap as before.
358 static bool shmem_confirm_swap(struct address_space
*mapping
,
359 pgoff_t index
, swp_entry_t swap
)
361 return xa_load(&mapping
->i_pages
, index
) == swp_to_radix_entry(swap
);
365 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option
368 * disables huge pages for the mount;
370 * enables huge pages for the mount;
371 * SHMEM_HUGE_WITHIN_SIZE:
372 * only allocate huge pages if the page will be fully within i_size,
373 * also respect fadvise()/madvise() hints;
375 * only allocate huge pages if requested with fadvise()/madvise();
378 #define SHMEM_HUGE_NEVER 0
379 #define SHMEM_HUGE_ALWAYS 1
380 #define SHMEM_HUGE_WITHIN_SIZE 2
381 #define SHMEM_HUGE_ADVISE 3
385 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled:
388 * disables huge on shm_mnt and all mounts, for emergency use;
390 * enables huge on shm_mnt and all mounts, w/o needing option, for testing;
393 #define SHMEM_HUGE_DENY (-1)
394 #define SHMEM_HUGE_FORCE (-2)
396 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
397 /* ifdef here to avoid bloating shmem.o when not necessary */
399 static int shmem_huge __read_mostly
;
401 #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS)
402 static int shmem_parse_huge(const char *str
)
404 if (!strcmp(str
, "never"))
405 return SHMEM_HUGE_NEVER
;
406 if (!strcmp(str
, "always"))
407 return SHMEM_HUGE_ALWAYS
;
408 if (!strcmp(str
, "within_size"))
409 return SHMEM_HUGE_WITHIN_SIZE
;
410 if (!strcmp(str
, "advise"))
411 return SHMEM_HUGE_ADVISE
;
412 if (!strcmp(str
, "deny"))
413 return SHMEM_HUGE_DENY
;
414 if (!strcmp(str
, "force"))
415 return SHMEM_HUGE_FORCE
;
419 static const char *shmem_format_huge(int huge
)
422 case SHMEM_HUGE_NEVER
:
424 case SHMEM_HUGE_ALWAYS
:
426 case SHMEM_HUGE_WITHIN_SIZE
:
427 return "within_size";
428 case SHMEM_HUGE_ADVISE
:
430 case SHMEM_HUGE_DENY
:
432 case SHMEM_HUGE_FORCE
:
441 static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info
*sbinfo
,
442 struct shrink_control
*sc
, unsigned long nr_to_split
)
444 LIST_HEAD(list
), *pos
, *next
;
445 LIST_HEAD(to_remove
);
447 struct shmem_inode_info
*info
;
449 unsigned long batch
= sc
? sc
->nr_to_scan
: 128;
450 int removed
= 0, split
= 0;
452 if (list_empty(&sbinfo
->shrinklist
))
455 spin_lock(&sbinfo
->shrinklist_lock
);
456 list_for_each_safe(pos
, next
, &sbinfo
->shrinklist
) {
457 info
= list_entry(pos
, struct shmem_inode_info
, shrinklist
);
460 inode
= igrab(&info
->vfs_inode
);
462 /* inode is about to be evicted */
464 list_del_init(&info
->shrinklist
);
469 /* Check if there's anything to gain */
470 if (round_up(inode
->i_size
, PAGE_SIZE
) ==
471 round_up(inode
->i_size
, HPAGE_PMD_SIZE
)) {
472 list_move(&info
->shrinklist
, &to_remove
);
477 list_move(&info
->shrinklist
, &list
);
482 spin_unlock(&sbinfo
->shrinklist_lock
);
484 list_for_each_safe(pos
, next
, &to_remove
) {
485 info
= list_entry(pos
, struct shmem_inode_info
, shrinklist
);
486 inode
= &info
->vfs_inode
;
487 list_del_init(&info
->shrinklist
);
491 list_for_each_safe(pos
, next
, &list
) {
494 info
= list_entry(pos
, struct shmem_inode_info
, shrinklist
);
495 inode
= &info
->vfs_inode
;
497 if (nr_to_split
&& split
>= nr_to_split
)
500 page
= find_get_page(inode
->i_mapping
,
501 (inode
->i_size
& HPAGE_PMD_MASK
) >> PAGE_SHIFT
);
505 /* No huge page at the end of the file: nothing to split */
506 if (!PageTransHuge(page
)) {
512 * Leave the inode on the list if we failed to lock
513 * the page at this time.
515 * Waiting for the lock may lead to deadlock in the
518 if (!trylock_page(page
)) {
523 ret
= split_huge_page(page
);
527 /* If split failed leave the inode on the list */
533 list_del_init(&info
->shrinklist
);
539 spin_lock(&sbinfo
->shrinklist_lock
);
540 list_splice_tail(&list
, &sbinfo
->shrinklist
);
541 sbinfo
->shrinklist_len
-= removed
;
542 spin_unlock(&sbinfo
->shrinklist_lock
);
547 static long shmem_unused_huge_scan(struct super_block
*sb
,
548 struct shrink_control
*sc
)
550 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
552 if (!READ_ONCE(sbinfo
->shrinklist_len
))
555 return shmem_unused_huge_shrink(sbinfo
, sc
, 0);
558 static long shmem_unused_huge_count(struct super_block
*sb
,
559 struct shrink_control
*sc
)
561 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
562 return READ_ONCE(sbinfo
->shrinklist_len
);
564 #else /* !CONFIG_TRANSPARENT_HUGE_PAGECACHE */
566 #define shmem_huge SHMEM_HUGE_DENY
568 static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info
*sbinfo
,
569 struct shrink_control
*sc
, unsigned long nr_to_split
)
573 #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
575 static inline bool is_huge_enabled(struct shmem_sb_info
*sbinfo
)
577 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
) &&
578 (shmem_huge
== SHMEM_HUGE_FORCE
|| sbinfo
->huge
) &&
579 shmem_huge
!= SHMEM_HUGE_DENY
)
585 * Like add_to_page_cache_locked, but error if expected item has gone.
587 static int shmem_add_to_page_cache(struct page
*page
,
588 struct address_space
*mapping
,
589 pgoff_t index
, void *expected
, gfp_t gfp
)
591 XA_STATE_ORDER(xas
, &mapping
->i_pages
, index
, compound_order(page
));
593 unsigned long nr
= 1UL << compound_order(page
);
595 VM_BUG_ON_PAGE(PageTail(page
), page
);
596 VM_BUG_ON_PAGE(index
!= round_down(index
, nr
), page
);
597 VM_BUG_ON_PAGE(!PageLocked(page
), page
);
598 VM_BUG_ON_PAGE(!PageSwapBacked(page
), page
);
599 VM_BUG_ON(expected
&& PageTransHuge(page
));
601 page_ref_add(page
, nr
);
602 page
->mapping
= mapping
;
608 entry
= xas_find_conflict(&xas
);
609 if (entry
!= expected
)
610 xas_set_err(&xas
, -EEXIST
);
611 xas_create_range(&xas
);
615 xas_store(&xas
, page
+ i
);
620 if (PageTransHuge(page
)) {
621 count_vm_event(THP_FILE_ALLOC
);
622 __inc_node_page_state(page
, NR_SHMEM_THPS
);
624 mapping
->nrpages
+= nr
;
625 __mod_node_page_state(page_pgdat(page
), NR_FILE_PAGES
, nr
);
626 __mod_node_page_state(page_pgdat(page
), NR_SHMEM
, nr
);
628 xas_unlock_irq(&xas
);
629 } while (xas_nomem(&xas
, gfp
));
631 if (xas_error(&xas
)) {
632 page
->mapping
= NULL
;
633 page_ref_sub(page
, nr
);
634 return xas_error(&xas
);
641 * Like delete_from_page_cache, but substitutes swap for page.
643 static void shmem_delete_from_page_cache(struct page
*page
, void *radswap
)
645 struct address_space
*mapping
= page
->mapping
;
648 VM_BUG_ON_PAGE(PageCompound(page
), page
);
650 xa_lock_irq(&mapping
->i_pages
);
651 error
= shmem_replace_entry(mapping
, page
->index
, page
, radswap
);
652 page
->mapping
= NULL
;
654 __dec_node_page_state(page
, NR_FILE_PAGES
);
655 __dec_node_page_state(page
, NR_SHMEM
);
656 xa_unlock_irq(&mapping
->i_pages
);
662 * Remove swap entry from page cache, free the swap and its page cache.
664 static int shmem_free_swap(struct address_space
*mapping
,
665 pgoff_t index
, void *radswap
)
669 old
= xa_cmpxchg_irq(&mapping
->i_pages
, index
, radswap
, NULL
, 0);
672 free_swap_and_cache(radix_to_swp_entry(radswap
));
677 * Determine (in bytes) how many of the shmem object's pages mapped by the
678 * given offsets are swapped out.
680 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
681 * as long as the inode doesn't go away and racy results are not a problem.
683 unsigned long shmem_partial_swap_usage(struct address_space
*mapping
,
684 pgoff_t start
, pgoff_t end
)
686 XA_STATE(xas
, &mapping
->i_pages
, start
);
688 unsigned long swapped
= 0;
691 xas_for_each(&xas
, page
, end
- 1) {
692 if (xas_retry(&xas
, page
))
694 if (xa_is_value(page
))
697 if (need_resched()) {
705 return swapped
<< PAGE_SHIFT
;
709 * Determine (in bytes) how many of the shmem object's pages mapped by the
710 * given vma is swapped out.
712 * This is safe to call without i_mutex or the i_pages lock thanks to RCU,
713 * as long as the inode doesn't go away and racy results are not a problem.
715 unsigned long shmem_swap_usage(struct vm_area_struct
*vma
)
717 struct inode
*inode
= file_inode(vma
->vm_file
);
718 struct shmem_inode_info
*info
= SHMEM_I(inode
);
719 struct address_space
*mapping
= inode
->i_mapping
;
720 unsigned long swapped
;
722 /* Be careful as we don't hold info->lock */
723 swapped
= READ_ONCE(info
->swapped
);
726 * The easier cases are when the shmem object has nothing in swap, or
727 * the vma maps it whole. Then we can simply use the stats that we
733 if (!vma
->vm_pgoff
&& vma
->vm_end
- vma
->vm_start
>= inode
->i_size
)
734 return swapped
<< PAGE_SHIFT
;
736 /* Here comes the more involved part */
737 return shmem_partial_swap_usage(mapping
,
738 linear_page_index(vma
, vma
->vm_start
),
739 linear_page_index(vma
, vma
->vm_end
));
743 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists.
745 void shmem_unlock_mapping(struct address_space
*mapping
)
748 pgoff_t indices
[PAGEVEC_SIZE
];
753 * Minor point, but we might as well stop if someone else SHM_LOCKs it.
755 while (!mapping_unevictable(mapping
)) {
757 * Avoid pagevec_lookup(): find_get_pages() returns 0 as if it
758 * has finished, if it hits a row of PAGEVEC_SIZE swap entries.
760 pvec
.nr
= find_get_entries(mapping
, index
,
761 PAGEVEC_SIZE
, pvec
.pages
, indices
);
764 index
= indices
[pvec
.nr
- 1] + 1;
765 pagevec_remove_exceptionals(&pvec
);
766 check_move_unevictable_pages(&pvec
);
767 pagevec_release(&pvec
);
773 * Remove range of pages and swap entries from page cache, and free them.
774 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate.
776 static void shmem_undo_range(struct inode
*inode
, loff_t lstart
, loff_t lend
,
779 struct address_space
*mapping
= inode
->i_mapping
;
780 struct shmem_inode_info
*info
= SHMEM_I(inode
);
781 pgoff_t start
= (lstart
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
782 pgoff_t end
= (lend
+ 1) >> PAGE_SHIFT
;
783 unsigned int partial_start
= lstart
& (PAGE_SIZE
- 1);
784 unsigned int partial_end
= (lend
+ 1) & (PAGE_SIZE
- 1);
786 pgoff_t indices
[PAGEVEC_SIZE
];
787 long nr_swaps_freed
= 0;
792 end
= -1; /* unsigned, so actually very big */
796 while (index
< end
) {
797 pvec
.nr
= find_get_entries(mapping
, index
,
798 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
799 pvec
.pages
, indices
);
802 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
803 struct page
*page
= pvec
.pages
[i
];
809 if (xa_is_value(page
)) {
812 nr_swaps_freed
+= !shmem_free_swap(mapping
,
817 VM_BUG_ON_PAGE(page_to_pgoff(page
) != index
, page
);
819 if (!trylock_page(page
))
822 if (PageTransTail(page
)) {
823 /* Middle of THP: zero out the page */
824 clear_highpage(page
);
827 } else if (PageTransHuge(page
)) {
828 if (index
== round_down(end
, HPAGE_PMD_NR
)) {
830 * Range ends in the middle of THP:
833 clear_highpage(page
);
837 index
+= HPAGE_PMD_NR
- 1;
838 i
+= HPAGE_PMD_NR
- 1;
841 if (!unfalloc
|| !PageUptodate(page
)) {
842 VM_BUG_ON_PAGE(PageTail(page
), page
);
843 if (page_mapping(page
) == mapping
) {
844 VM_BUG_ON_PAGE(PageWriteback(page
), page
);
845 truncate_inode_page(mapping
, page
);
850 pagevec_remove_exceptionals(&pvec
);
851 pagevec_release(&pvec
);
857 struct page
*page
= NULL
;
858 shmem_getpage(inode
, start
- 1, &page
, SGP_READ
);
860 unsigned int top
= PAGE_SIZE
;
865 zero_user_segment(page
, partial_start
, top
);
866 set_page_dirty(page
);
872 struct page
*page
= NULL
;
873 shmem_getpage(inode
, end
, &page
, SGP_READ
);
875 zero_user_segment(page
, 0, partial_end
);
876 set_page_dirty(page
);
885 while (index
< end
) {
888 pvec
.nr
= find_get_entries(mapping
, index
,
889 min(end
- index
, (pgoff_t
)PAGEVEC_SIZE
),
890 pvec
.pages
, indices
);
892 /* If all gone or hole-punch or unfalloc, we're done */
893 if (index
== start
|| end
!= -1)
895 /* But if truncating, restart to make sure all gone */
899 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
900 struct page
*page
= pvec
.pages
[i
];
906 if (xa_is_value(page
)) {
909 if (shmem_free_swap(mapping
, index
, page
)) {
910 /* Swap was replaced by page: retry */
920 if (PageTransTail(page
)) {
921 /* Middle of THP: zero out the page */
922 clear_highpage(page
);
925 * Partial thp truncate due 'start' in middle
926 * of THP: don't need to look on these pages
927 * again on !pvec.nr restart.
929 if (index
!= round_down(end
, HPAGE_PMD_NR
))
932 } else if (PageTransHuge(page
)) {
933 if (index
== round_down(end
, HPAGE_PMD_NR
)) {
935 * Range ends in the middle of THP:
938 clear_highpage(page
);
942 index
+= HPAGE_PMD_NR
- 1;
943 i
+= HPAGE_PMD_NR
- 1;
946 if (!unfalloc
|| !PageUptodate(page
)) {
947 VM_BUG_ON_PAGE(PageTail(page
), page
);
948 if (page_mapping(page
) == mapping
) {
949 VM_BUG_ON_PAGE(PageWriteback(page
), page
);
950 truncate_inode_page(mapping
, page
);
952 /* Page was replaced by swap: retry */
960 pagevec_remove_exceptionals(&pvec
);
961 pagevec_release(&pvec
);
965 spin_lock_irq(&info
->lock
);
966 info
->swapped
-= nr_swaps_freed
;
967 shmem_recalc_inode(inode
);
968 spin_unlock_irq(&info
->lock
);
971 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
973 shmem_undo_range(inode
, lstart
, lend
, false);
974 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
976 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
978 static int shmem_getattr(const struct path
*path
, struct kstat
*stat
,
979 u32 request_mask
, unsigned int query_flags
)
981 struct inode
*inode
= path
->dentry
->d_inode
;
982 struct shmem_inode_info
*info
= SHMEM_I(inode
);
983 struct shmem_sb_info
*sb_info
= SHMEM_SB(inode
->i_sb
);
985 if (info
->alloced
- info
->swapped
!= inode
->i_mapping
->nrpages
) {
986 spin_lock_irq(&info
->lock
);
987 shmem_recalc_inode(inode
);
988 spin_unlock_irq(&info
->lock
);
990 generic_fillattr(inode
, stat
);
992 if (is_huge_enabled(sb_info
))
993 stat
->blksize
= HPAGE_PMD_SIZE
;
998 static int shmem_setattr(struct dentry
*dentry
, struct iattr
*attr
)
1000 struct inode
*inode
= d_inode(dentry
);
1001 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1002 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
1005 error
= setattr_prepare(dentry
, attr
);
1009 if (S_ISREG(inode
->i_mode
) && (attr
->ia_valid
& ATTR_SIZE
)) {
1010 loff_t oldsize
= inode
->i_size
;
1011 loff_t newsize
= attr
->ia_size
;
1013 /* protected by i_mutex */
1014 if ((newsize
< oldsize
&& (info
->seals
& F_SEAL_SHRINK
)) ||
1015 (newsize
> oldsize
&& (info
->seals
& F_SEAL_GROW
)))
1018 if (newsize
!= oldsize
) {
1019 error
= shmem_reacct_size(SHMEM_I(inode
)->flags
,
1023 i_size_write(inode
, newsize
);
1024 inode
->i_ctime
= inode
->i_mtime
= current_time(inode
);
1026 if (newsize
<= oldsize
) {
1027 loff_t holebegin
= round_up(newsize
, PAGE_SIZE
);
1028 if (oldsize
> holebegin
)
1029 unmap_mapping_range(inode
->i_mapping
,
1032 shmem_truncate_range(inode
,
1033 newsize
, (loff_t
)-1);
1034 /* unmap again to remove racily COWed private pages */
1035 if (oldsize
> holebegin
)
1036 unmap_mapping_range(inode
->i_mapping
,
1040 * Part of the huge page can be beyond i_size: subject
1041 * to shrink under memory pressure.
1043 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
)) {
1044 spin_lock(&sbinfo
->shrinklist_lock
);
1046 * _careful to defend against unlocked access to
1047 * ->shrink_list in shmem_unused_huge_shrink()
1049 if (list_empty_careful(&info
->shrinklist
)) {
1050 list_add_tail(&info
->shrinklist
,
1051 &sbinfo
->shrinklist
);
1052 sbinfo
->shrinklist_len
++;
1054 spin_unlock(&sbinfo
->shrinklist_lock
);
1059 setattr_copy(inode
, attr
);
1060 if (attr
->ia_valid
& ATTR_MODE
)
1061 error
= posix_acl_chmod(inode
, inode
->i_mode
);
1065 static void shmem_evict_inode(struct inode
*inode
)
1067 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1068 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
1070 if (inode
->i_mapping
->a_ops
== &shmem_aops
) {
1071 shmem_unacct_size(info
->flags
, inode
->i_size
);
1073 shmem_truncate_range(inode
, 0, (loff_t
)-1);
1074 if (!list_empty(&info
->shrinklist
)) {
1075 spin_lock(&sbinfo
->shrinklist_lock
);
1076 if (!list_empty(&info
->shrinklist
)) {
1077 list_del_init(&info
->shrinklist
);
1078 sbinfo
->shrinklist_len
--;
1080 spin_unlock(&sbinfo
->shrinklist_lock
);
1082 if (!list_empty(&info
->swaplist
)) {
1083 mutex_lock(&shmem_swaplist_mutex
);
1084 list_del_init(&info
->swaplist
);
1085 mutex_unlock(&shmem_swaplist_mutex
);
1089 simple_xattrs_free(&info
->xattrs
);
1090 WARN_ON(inode
->i_blocks
);
1091 if (!sbinfo
->idr_nouse
&& inode
->i_ino
) {
1092 mutex_lock(&sbinfo
->idr_lock
);
1093 idr_remove(&sbinfo
->idr
, inode
->i_ino
);
1094 mutex_unlock(&sbinfo
->idr_lock
);
1096 shmem_free_inode(inode
->i_sb
);
1100 static unsigned long find_swap_entry(struct xarray
*xa
, void *item
)
1102 XA_STATE(xas
, xa
, 0);
1103 unsigned int checked
= 0;
1107 xas_for_each(&xas
, entry
, ULONG_MAX
) {
1108 if (xas_retry(&xas
, entry
))
1113 if ((checked
% XA_CHECK_SCHED
) != 0)
1120 return entry
? xas
.xa_index
: -1;
1124 * If swap found in inode, free it and move page from swapcache to filecache.
1126 static int shmem_unuse_inode(struct shmem_inode_info
*info
,
1127 swp_entry_t swap
, struct page
**pagep
)
1129 struct address_space
*mapping
= info
->vfs_inode
.i_mapping
;
1135 radswap
= swp_to_radix_entry(swap
);
1136 index
= find_swap_entry(&mapping
->i_pages
, radswap
);
1138 return -EAGAIN
; /* tell shmem_unuse we found nothing */
1141 * Move _head_ to start search for next from here.
1142 * But be careful: shmem_evict_inode checks list_empty without taking
1143 * mutex, and there's an instant in list_move_tail when info->swaplist
1144 * would appear empty, if it were the only one on shmem_swaplist.
1146 if (shmem_swaplist
.next
!= &info
->swaplist
)
1147 list_move_tail(&shmem_swaplist
, &info
->swaplist
);
1149 gfp
= mapping_gfp_mask(mapping
);
1150 if (shmem_should_replace_page(*pagep
, gfp
)) {
1151 mutex_unlock(&shmem_swaplist_mutex
);
1152 error
= shmem_replace_page(pagep
, gfp
, info
, index
);
1153 mutex_lock(&shmem_swaplist_mutex
);
1155 * We needed to drop mutex to make that restrictive page
1156 * allocation, but the inode might have been freed while we
1157 * dropped it: although a racing shmem_evict_inode() cannot
1158 * complete without emptying the page cache, our page lock
1159 * on this swapcache page is not enough to prevent that -
1160 * free_swap_and_cache() of our swap entry will only
1161 * trylock_page(), removing swap from page cache whatever.
1163 * We must not proceed to shmem_add_to_page_cache() if the
1164 * inode has been freed, but of course we cannot rely on
1165 * inode or mapping or info to check that. However, we can
1166 * safely check if our swap entry is still in use (and here
1167 * it can't have got reused for another page): if it's still
1168 * in use, then the inode cannot have been freed yet, and we
1169 * can safely proceed (if it's no longer in use, that tells
1170 * nothing about the inode, but we don't need to unuse swap).
1172 if (!page_swapcount(*pagep
))
1177 * We rely on shmem_swaplist_mutex, not only to protect the swaplist,
1178 * but also to hold up shmem_evict_inode(): so inode cannot be freed
1179 * beneath us (pagelock doesn't help until the page is in pagecache).
1182 error
= shmem_add_to_page_cache(*pagep
, mapping
, index
,
1184 if (error
!= -ENOMEM
) {
1186 * Truncation and eviction use free_swap_and_cache(), which
1187 * only does trylock page: if we raced, best clean up here.
1189 delete_from_swap_cache(*pagep
);
1190 set_page_dirty(*pagep
);
1192 spin_lock_irq(&info
->lock
);
1194 spin_unlock_irq(&info
->lock
);
1202 * Search through swapped inodes to find and replace swap by page.
1204 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
1206 struct list_head
*this, *next
;
1207 struct shmem_inode_info
*info
;
1208 struct mem_cgroup
*memcg
;
1212 * There's a faint possibility that swap page was replaced before
1213 * caller locked it: caller will come back later with the right page.
1215 if (unlikely(!PageSwapCache(page
) || page_private(page
) != swap
.val
))
1219 * Charge page using GFP_KERNEL while we can wait, before taking
1220 * the shmem_swaplist_mutex which might hold up shmem_writepage().
1221 * Charged back to the user (not to caller) when swap account is used.
1223 error
= mem_cgroup_try_charge_delay(page
, current
->mm
, GFP_KERNEL
,
1227 /* No memory allocation: swap entry occupies the slot for the page */
1230 mutex_lock(&shmem_swaplist_mutex
);
1231 list_for_each_safe(this, next
, &shmem_swaplist
) {
1232 info
= list_entry(this, struct shmem_inode_info
, swaplist
);
1234 error
= shmem_unuse_inode(info
, swap
, &page
);
1236 list_del_init(&info
->swaplist
);
1238 if (error
!= -EAGAIN
)
1240 /* found nothing in this: move on to search the next */
1242 mutex_unlock(&shmem_swaplist_mutex
);
1245 if (error
!= -ENOMEM
)
1247 mem_cgroup_cancel_charge(page
, memcg
, false);
1249 mem_cgroup_commit_charge(page
, memcg
, true, false);
1257 * Move the page from the page cache to the swap cache.
1259 static int shmem_writepage(struct page
*page
, struct writeback_control
*wbc
)
1261 struct shmem_inode_info
*info
;
1262 struct address_space
*mapping
;
1263 struct inode
*inode
;
1267 VM_BUG_ON_PAGE(PageCompound(page
), page
);
1268 BUG_ON(!PageLocked(page
));
1269 mapping
= page
->mapping
;
1270 index
= page
->index
;
1271 inode
= mapping
->host
;
1272 info
= SHMEM_I(inode
);
1273 if (info
->flags
& VM_LOCKED
)
1275 if (!total_swap_pages
)
1279 * Our capabilities prevent regular writeback or sync from ever calling
1280 * shmem_writepage; but a stacking filesystem might use ->writepage of
1281 * its underlying filesystem, in which case tmpfs should write out to
1282 * swap only in response to memory pressure, and not for the writeback
1285 if (!wbc
->for_reclaim
) {
1286 WARN_ON_ONCE(1); /* Still happens? Tell us about it! */
1291 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC
1292 * value into swapfile.c, the only way we can correctly account for a
1293 * fallocated page arriving here is now to initialize it and write it.
1295 * That's okay for a page already fallocated earlier, but if we have
1296 * not yet completed the fallocation, then (a) we want to keep track
1297 * of this page in case we have to undo it, and (b) it may not be a
1298 * good idea to continue anyway, once we're pushing into swap. So
1299 * reactivate the page, and let shmem_fallocate() quit when too many.
1301 if (!PageUptodate(page
)) {
1302 if (inode
->i_private
) {
1303 struct shmem_falloc
*shmem_falloc
;
1304 spin_lock(&inode
->i_lock
);
1305 shmem_falloc
= inode
->i_private
;
1307 !shmem_falloc
->waitq
&&
1308 index
>= shmem_falloc
->start
&&
1309 index
< shmem_falloc
->next
)
1310 shmem_falloc
->nr_unswapped
++;
1312 shmem_falloc
= NULL
;
1313 spin_unlock(&inode
->i_lock
);
1317 clear_highpage(page
);
1318 flush_dcache_page(page
);
1319 SetPageUptodate(page
);
1322 swap
= get_swap_page(page
);
1327 * Add inode to shmem_unuse()'s list of swapped-out inodes,
1328 * if it's not already there. Do it now before the page is
1329 * moved to swap cache, when its pagelock no longer protects
1330 * the inode from eviction. But don't unlock the mutex until
1331 * we've incremented swapped, because shmem_unuse_inode() will
1332 * prune a !swapped inode from the swaplist under this mutex.
1334 mutex_lock(&shmem_swaplist_mutex
);
1335 if (list_empty(&info
->swaplist
))
1336 list_add_tail(&info
->swaplist
, &shmem_swaplist
);
1338 if (add_to_swap_cache(page
, swap
, GFP_ATOMIC
) == 0) {
1339 spin_lock_irq(&info
->lock
);
1340 shmem_recalc_inode(inode
);
1342 spin_unlock_irq(&info
->lock
);
1344 swap_shmem_alloc(swap
);
1345 shmem_delete_from_page_cache(page
, swp_to_radix_entry(swap
));
1347 mutex_unlock(&shmem_swaplist_mutex
);
1348 BUG_ON(page_mapped(page
));
1349 swap_writepage(page
, wbc
);
1353 mutex_unlock(&shmem_swaplist_mutex
);
1354 put_swap_page(page
, swap
);
1356 set_page_dirty(page
);
1357 if (wbc
->for_reclaim
)
1358 return AOP_WRITEPAGE_ACTIVATE
; /* Return with page locked */
1363 #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS)
1364 static void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
1368 if (!mpol
|| mpol
->mode
== MPOL_DEFAULT
)
1369 return; /* show nothing */
1371 mpol_to_str(buffer
, sizeof(buffer
), mpol
);
1373 seq_printf(seq
, ",mpol=%s", buffer
);
1376 static struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
1378 struct mempolicy
*mpol
= NULL
;
1380 spin_lock(&sbinfo
->stat_lock
); /* prevent replace/use races */
1381 mpol
= sbinfo
->mpol
;
1383 spin_unlock(&sbinfo
->stat_lock
);
1387 #else /* !CONFIG_NUMA || !CONFIG_TMPFS */
1388 static inline void shmem_show_mpol(struct seq_file
*seq
, struct mempolicy
*mpol
)
1391 static inline struct mempolicy
*shmem_get_sbmpol(struct shmem_sb_info
*sbinfo
)
1395 #endif /* CONFIG_NUMA && CONFIG_TMPFS */
1397 #define vm_policy vm_private_data
1400 static void shmem_pseudo_vma_init(struct vm_area_struct
*vma
,
1401 struct shmem_inode_info
*info
, pgoff_t index
)
1403 /* Create a pseudo vma that just contains the policy */
1404 vma_init(vma
, NULL
);
1405 /* Bias interleave by inode number to distribute better across nodes */
1406 vma
->vm_pgoff
= index
+ info
->vfs_inode
.i_ino
;
1407 vma
->vm_policy
= mpol_shared_policy_lookup(&info
->policy
, index
);
1410 static void shmem_pseudo_vma_destroy(struct vm_area_struct
*vma
)
1412 /* Drop reference taken by mpol_shared_policy_lookup() */
1413 mpol_cond_put(vma
->vm_policy
);
1416 static struct page
*shmem_swapin(swp_entry_t swap
, gfp_t gfp
,
1417 struct shmem_inode_info
*info
, pgoff_t index
)
1419 struct vm_area_struct pvma
;
1421 struct vm_fault vmf
;
1423 shmem_pseudo_vma_init(&pvma
, info
, index
);
1426 page
= swap_cluster_readahead(swap
, gfp
, &vmf
);
1427 shmem_pseudo_vma_destroy(&pvma
);
1432 static struct page
*shmem_alloc_hugepage(gfp_t gfp
,
1433 struct shmem_inode_info
*info
, pgoff_t index
)
1435 struct vm_area_struct pvma
;
1436 struct address_space
*mapping
= info
->vfs_inode
.i_mapping
;
1440 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
))
1443 hindex
= round_down(index
, HPAGE_PMD_NR
);
1444 if (xa_find(&mapping
->i_pages
, &hindex
, hindex
+ HPAGE_PMD_NR
- 1,
1448 shmem_pseudo_vma_init(&pvma
, info
, hindex
);
1449 page
= alloc_pages_vma(gfp
| __GFP_COMP
| __GFP_NORETRY
| __GFP_NOWARN
,
1450 HPAGE_PMD_ORDER
, &pvma
, 0, numa_node_id(), true);
1451 shmem_pseudo_vma_destroy(&pvma
);
1453 prep_transhuge_page(page
);
1457 static struct page
*shmem_alloc_page(gfp_t gfp
,
1458 struct shmem_inode_info
*info
, pgoff_t index
)
1460 struct vm_area_struct pvma
;
1463 shmem_pseudo_vma_init(&pvma
, info
, index
);
1464 page
= alloc_page_vma(gfp
, &pvma
, 0);
1465 shmem_pseudo_vma_destroy(&pvma
);
1470 static struct page
*shmem_alloc_and_acct_page(gfp_t gfp
,
1471 struct inode
*inode
,
1472 pgoff_t index
, bool huge
)
1474 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1479 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
))
1481 nr
= huge
? HPAGE_PMD_NR
: 1;
1483 if (!shmem_inode_acct_block(inode
, nr
))
1487 page
= shmem_alloc_hugepage(gfp
, info
, index
);
1489 page
= shmem_alloc_page(gfp
, info
, index
);
1491 __SetPageLocked(page
);
1492 __SetPageSwapBacked(page
);
1497 shmem_inode_unacct_blocks(inode
, nr
);
1499 return ERR_PTR(err
);
1503 * When a page is moved from swapcache to shmem filecache (either by the
1504 * usual swapin of shmem_getpage_gfp(), or by the less common swapoff of
1505 * shmem_unuse_inode()), it may have been read in earlier from swap, in
1506 * ignorance of the mapping it belongs to. If that mapping has special
1507 * constraints (like the gma500 GEM driver, which requires RAM below 4GB),
1508 * we may need to copy to a suitable page before moving to filecache.
1510 * In a future release, this may well be extended to respect cpuset and
1511 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page();
1512 * but for now it is a simple matter of zone.
1514 static bool shmem_should_replace_page(struct page
*page
, gfp_t gfp
)
1516 return page_zonenum(page
) > gfp_zone(gfp
);
1519 static int shmem_replace_page(struct page
**pagep
, gfp_t gfp
,
1520 struct shmem_inode_info
*info
, pgoff_t index
)
1522 struct page
*oldpage
, *newpage
;
1523 struct address_space
*swap_mapping
;
1529 entry
.val
= page_private(oldpage
);
1530 swap_index
= swp_offset(entry
);
1531 swap_mapping
= page_mapping(oldpage
);
1534 * We have arrived here because our zones are constrained, so don't
1535 * limit chance of success by further cpuset and node constraints.
1537 gfp
&= ~GFP_CONSTRAINT_MASK
;
1538 newpage
= shmem_alloc_page(gfp
, info
, index
);
1543 copy_highpage(newpage
, oldpage
);
1544 flush_dcache_page(newpage
);
1546 __SetPageLocked(newpage
);
1547 __SetPageSwapBacked(newpage
);
1548 SetPageUptodate(newpage
);
1549 set_page_private(newpage
, entry
.val
);
1550 SetPageSwapCache(newpage
);
1553 * Our caller will very soon move newpage out of swapcache, but it's
1554 * a nice clean interface for us to replace oldpage by newpage there.
1556 xa_lock_irq(&swap_mapping
->i_pages
);
1557 error
= shmem_replace_entry(swap_mapping
, swap_index
, oldpage
, newpage
);
1559 __inc_node_page_state(newpage
, NR_FILE_PAGES
);
1560 __dec_node_page_state(oldpage
, NR_FILE_PAGES
);
1562 xa_unlock_irq(&swap_mapping
->i_pages
);
1564 if (unlikely(error
)) {
1566 * Is this possible? I think not, now that our callers check
1567 * both PageSwapCache and page_private after getting page lock;
1568 * but be defensive. Reverse old to newpage for clear and free.
1572 mem_cgroup_migrate(oldpage
, newpage
);
1573 lru_cache_add_anon(newpage
);
1577 ClearPageSwapCache(oldpage
);
1578 set_page_private(oldpage
, 0);
1580 unlock_page(oldpage
);
1587 * shmem_getpage_gfp - find page in cache, or get from swap, or allocate
1589 * If we allocate a new one we do not mark it dirty. That's up to the
1590 * vm. If we swap it in we mark it dirty since we also free the swap
1591 * entry since a page cannot live in both the swap and page cache.
1593 * fault_mm and fault_type are only supplied by shmem_fault:
1594 * otherwise they are NULL.
1596 static int shmem_getpage_gfp(struct inode
*inode
, pgoff_t index
,
1597 struct page
**pagep
, enum sgp_type sgp
, gfp_t gfp
,
1598 struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
1599 vm_fault_t
*fault_type
)
1601 struct address_space
*mapping
= inode
->i_mapping
;
1602 struct shmem_inode_info
*info
= SHMEM_I(inode
);
1603 struct shmem_sb_info
*sbinfo
;
1604 struct mm_struct
*charge_mm
;
1605 struct mem_cgroup
*memcg
;
1608 enum sgp_type sgp_huge
= sgp
;
1609 pgoff_t hindex
= index
;
1614 if (index
> (MAX_LFS_FILESIZE
>> PAGE_SHIFT
))
1616 if (sgp
== SGP_NOHUGE
|| sgp
== SGP_HUGE
)
1620 page
= find_lock_entry(mapping
, index
);
1621 if (xa_is_value(page
)) {
1622 swap
= radix_to_swp_entry(page
);
1626 if (sgp
<= SGP_CACHE
&&
1627 ((loff_t
)index
<< PAGE_SHIFT
) >= i_size_read(inode
)) {
1632 if (page
&& sgp
== SGP_WRITE
)
1633 mark_page_accessed(page
);
1635 /* fallocated page? */
1636 if (page
&& !PageUptodate(page
)) {
1637 if (sgp
!= SGP_READ
)
1643 if (page
|| (sgp
== SGP_READ
&& !swap
.val
)) {
1649 * Fast cache lookup did not find it:
1650 * bring it back from swap or allocate.
1652 sbinfo
= SHMEM_SB(inode
->i_sb
);
1653 charge_mm
= vma
? vma
->vm_mm
: current
->mm
;
1656 /* Look it up and read it in.. */
1657 page
= lookup_swap_cache(swap
, NULL
, 0);
1659 /* Or update major stats only when swapin succeeds?? */
1661 *fault_type
|= VM_FAULT_MAJOR
;
1662 count_vm_event(PGMAJFAULT
);
1663 count_memcg_event_mm(charge_mm
, PGMAJFAULT
);
1665 /* Here we actually start the io */
1666 page
= shmem_swapin(swap
, gfp
, info
, index
);
1673 /* We have to do this with page locked to prevent races */
1675 if (!PageSwapCache(page
) || page_private(page
) != swap
.val
||
1676 !shmem_confirm_swap(mapping
, index
, swap
)) {
1677 error
= -EEXIST
; /* try again */
1680 if (!PageUptodate(page
)) {
1684 wait_on_page_writeback(page
);
1686 if (shmem_should_replace_page(page
, gfp
)) {
1687 error
= shmem_replace_page(&page
, gfp
, info
, index
);
1692 error
= mem_cgroup_try_charge_delay(page
, charge_mm
, gfp
, &memcg
,
1695 error
= shmem_add_to_page_cache(page
, mapping
, index
,
1696 swp_to_radix_entry(swap
), gfp
);
1698 * We already confirmed swap under page lock, and make
1699 * no memory allocation here, so usually no possibility
1700 * of error; but free_swap_and_cache() only trylocks a
1701 * page, so it is just possible that the entry has been
1702 * truncated or holepunched since swap was confirmed.
1703 * shmem_undo_range() will have done some of the
1704 * unaccounting, now delete_from_swap_cache() will do
1706 * Reset swap.val? No, leave it so "failed" goes back to
1707 * "repeat": reading a hole and writing should succeed.
1710 mem_cgroup_cancel_charge(page
, memcg
, false);
1711 delete_from_swap_cache(page
);
1717 mem_cgroup_commit_charge(page
, memcg
, true, false);
1719 spin_lock_irq(&info
->lock
);
1721 shmem_recalc_inode(inode
);
1722 spin_unlock_irq(&info
->lock
);
1724 if (sgp
== SGP_WRITE
)
1725 mark_page_accessed(page
);
1727 delete_from_swap_cache(page
);
1728 set_page_dirty(page
);
1732 if (vma
&& userfaultfd_missing(vma
)) {
1733 *fault_type
= handle_userfault(vmf
, VM_UFFD_MISSING
);
1737 /* shmem_symlink() */
1738 if (mapping
->a_ops
!= &shmem_aops
)
1740 if (shmem_huge
== SHMEM_HUGE_DENY
|| sgp_huge
== SGP_NOHUGE
)
1742 if (shmem_huge
== SHMEM_HUGE_FORCE
)
1744 switch (sbinfo
->huge
) {
1747 case SHMEM_HUGE_NEVER
:
1749 case SHMEM_HUGE_WITHIN_SIZE
:
1750 off
= round_up(index
, HPAGE_PMD_NR
);
1751 i_size
= round_up(i_size_read(inode
), PAGE_SIZE
);
1752 if (i_size
>= HPAGE_PMD_SIZE
&&
1753 i_size
>> PAGE_SHIFT
>= off
)
1756 case SHMEM_HUGE_ADVISE
:
1757 if (sgp_huge
== SGP_HUGE
)
1759 /* TODO: implement fadvise() hints */
1764 page
= shmem_alloc_and_acct_page(gfp
, inode
, index
, true);
1766 alloc_nohuge
: page
= shmem_alloc_and_acct_page(gfp
, inode
,
1771 error
= PTR_ERR(page
);
1773 if (error
!= -ENOSPC
)
1776 * Try to reclaim some spece by splitting a huge page
1777 * beyond i_size on the filesystem.
1781 ret
= shmem_unused_huge_shrink(sbinfo
, NULL
, 1);
1782 if (ret
== SHRINK_STOP
)
1790 if (PageTransHuge(page
))
1791 hindex
= round_down(index
, HPAGE_PMD_NR
);
1795 if (sgp
== SGP_WRITE
)
1796 __SetPageReferenced(page
);
1798 error
= mem_cgroup_try_charge_delay(page
, charge_mm
, gfp
, &memcg
,
1799 PageTransHuge(page
));
1802 error
= shmem_add_to_page_cache(page
, mapping
, hindex
,
1803 NULL
, gfp
& GFP_RECLAIM_MASK
);
1805 mem_cgroup_cancel_charge(page
, memcg
,
1806 PageTransHuge(page
));
1809 mem_cgroup_commit_charge(page
, memcg
, false,
1810 PageTransHuge(page
));
1811 lru_cache_add_anon(page
);
1813 spin_lock_irq(&info
->lock
);
1814 info
->alloced
+= 1 << compound_order(page
);
1815 inode
->i_blocks
+= BLOCKS_PER_PAGE
<< compound_order(page
);
1816 shmem_recalc_inode(inode
);
1817 spin_unlock_irq(&info
->lock
);
1820 if (PageTransHuge(page
) &&
1821 DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
) <
1822 hindex
+ HPAGE_PMD_NR
- 1) {
1824 * Part of the huge page is beyond i_size: subject
1825 * to shrink under memory pressure.
1827 spin_lock(&sbinfo
->shrinklist_lock
);
1829 * _careful to defend against unlocked access to
1830 * ->shrink_list in shmem_unused_huge_shrink()
1832 if (list_empty_careful(&info
->shrinklist
)) {
1833 list_add_tail(&info
->shrinklist
,
1834 &sbinfo
->shrinklist
);
1835 sbinfo
->shrinklist_len
++;
1837 spin_unlock(&sbinfo
->shrinklist_lock
);
1841 * Let SGP_FALLOC use the SGP_WRITE optimization on a new page.
1843 if (sgp
== SGP_FALLOC
)
1847 * Let SGP_WRITE caller clear ends if write does not fill page;
1848 * but SGP_FALLOC on a page fallocated earlier must initialize
1849 * it now, lest undo on failure cancel our earlier guarantee.
1851 if (sgp
!= SGP_WRITE
&& !PageUptodate(page
)) {
1852 struct page
*head
= compound_head(page
);
1855 for (i
= 0; i
< (1 << compound_order(head
)); i
++) {
1856 clear_highpage(head
+ i
);
1857 flush_dcache_page(head
+ i
);
1859 SetPageUptodate(head
);
1863 /* Perhaps the file has been truncated since we checked */
1864 if (sgp
<= SGP_CACHE
&&
1865 ((loff_t
)index
<< PAGE_SHIFT
) >= i_size_read(inode
)) {
1867 ClearPageDirty(page
);
1868 delete_from_page_cache(page
);
1869 spin_lock_irq(&info
->lock
);
1870 shmem_recalc_inode(inode
);
1871 spin_unlock_irq(&info
->lock
);
1876 *pagep
= page
+ index
- hindex
;
1883 shmem_inode_unacct_blocks(inode
, 1 << compound_order(page
));
1885 if (PageTransHuge(page
)) {
1891 if (swap
.val
&& !shmem_confirm_swap(mapping
, index
, swap
))
1898 if (error
== -ENOSPC
&& !once
++) {
1899 spin_lock_irq(&info
->lock
);
1900 shmem_recalc_inode(inode
);
1901 spin_unlock_irq(&info
->lock
);
1904 if (error
== -EEXIST
)
1910 * This is like autoremove_wake_function, but it removes the wait queue
1911 * entry unconditionally - even if something else had already woken the
1914 static int synchronous_wake_function(wait_queue_entry_t
*wait
, unsigned mode
, int sync
, void *key
)
1916 int ret
= default_wake_function(wait
, mode
, sync
, key
);
1917 list_del_init(&wait
->entry
);
1921 static vm_fault_t
shmem_fault(struct vm_fault
*vmf
)
1923 struct vm_area_struct
*vma
= vmf
->vma
;
1924 struct inode
*inode
= file_inode(vma
->vm_file
);
1925 gfp_t gfp
= mapping_gfp_mask(inode
->i_mapping
);
1928 vm_fault_t ret
= VM_FAULT_LOCKED
;
1931 * Trinity finds that probing a hole which tmpfs is punching can
1932 * prevent the hole-punch from ever completing: which in turn
1933 * locks writers out with its hold on i_mutex. So refrain from
1934 * faulting pages into the hole while it's being punched. Although
1935 * shmem_undo_range() does remove the additions, it may be unable to
1936 * keep up, as each new page needs its own unmap_mapping_range() call,
1937 * and the i_mmap tree grows ever slower to scan if new vmas are added.
1939 * It does not matter if we sometimes reach this check just before the
1940 * hole-punch begins, so that one fault then races with the punch:
1941 * we just need to make racing faults a rare case.
1943 * The implementation below would be much simpler if we just used a
1944 * standard mutex or completion: but we cannot take i_mutex in fault,
1945 * and bloating every shmem inode for this unlikely case would be sad.
1947 if (unlikely(inode
->i_private
)) {
1948 struct shmem_falloc
*shmem_falloc
;
1950 spin_lock(&inode
->i_lock
);
1951 shmem_falloc
= inode
->i_private
;
1953 shmem_falloc
->waitq
&&
1954 vmf
->pgoff
>= shmem_falloc
->start
&&
1955 vmf
->pgoff
< shmem_falloc
->next
) {
1956 wait_queue_head_t
*shmem_falloc_waitq
;
1957 DEFINE_WAIT_FUNC(shmem_fault_wait
, synchronous_wake_function
);
1959 ret
= VM_FAULT_NOPAGE
;
1960 if ((vmf
->flags
& FAULT_FLAG_ALLOW_RETRY
) &&
1961 !(vmf
->flags
& FAULT_FLAG_RETRY_NOWAIT
)) {
1962 /* It's polite to up mmap_sem if we can */
1963 up_read(&vma
->vm_mm
->mmap_sem
);
1964 ret
= VM_FAULT_RETRY
;
1967 shmem_falloc_waitq
= shmem_falloc
->waitq
;
1968 prepare_to_wait(shmem_falloc_waitq
, &shmem_fault_wait
,
1969 TASK_UNINTERRUPTIBLE
);
1970 spin_unlock(&inode
->i_lock
);
1974 * shmem_falloc_waitq points into the shmem_fallocate()
1975 * stack of the hole-punching task: shmem_falloc_waitq
1976 * is usually invalid by the time we reach here, but
1977 * finish_wait() does not dereference it in that case;
1978 * though i_lock needed lest racing with wake_up_all().
1980 spin_lock(&inode
->i_lock
);
1981 finish_wait(shmem_falloc_waitq
, &shmem_fault_wait
);
1982 spin_unlock(&inode
->i_lock
);
1985 spin_unlock(&inode
->i_lock
);
1990 if ((vma
->vm_flags
& VM_NOHUGEPAGE
) ||
1991 test_bit(MMF_DISABLE_THP
, &vma
->vm_mm
->flags
))
1993 else if (vma
->vm_flags
& VM_HUGEPAGE
)
1996 err
= shmem_getpage_gfp(inode
, vmf
->pgoff
, &vmf
->page
, sgp
,
1997 gfp
, vma
, vmf
, &ret
);
1999 return vmf_error(err
);
2003 unsigned long shmem_get_unmapped_area(struct file
*file
,
2004 unsigned long uaddr
, unsigned long len
,
2005 unsigned long pgoff
, unsigned long flags
)
2007 unsigned long (*get_area
)(struct file
*,
2008 unsigned long, unsigned long, unsigned long, unsigned long);
2010 unsigned long offset
;
2011 unsigned long inflated_len
;
2012 unsigned long inflated_addr
;
2013 unsigned long inflated_offset
;
2015 if (len
> TASK_SIZE
)
2018 get_area
= current
->mm
->get_unmapped_area
;
2019 addr
= get_area(file
, uaddr
, len
, pgoff
, flags
);
2021 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
))
2023 if (IS_ERR_VALUE(addr
))
2025 if (addr
& ~PAGE_MASK
)
2027 if (addr
> TASK_SIZE
- len
)
2030 if (shmem_huge
== SHMEM_HUGE_DENY
)
2032 if (len
< HPAGE_PMD_SIZE
)
2034 if (flags
& MAP_FIXED
)
2037 * Our priority is to support MAP_SHARED mapped hugely;
2038 * and support MAP_PRIVATE mapped hugely too, until it is COWed.
2039 * But if caller specified an address hint, respect that as before.
2044 if (shmem_huge
!= SHMEM_HUGE_FORCE
) {
2045 struct super_block
*sb
;
2048 VM_BUG_ON(file
->f_op
!= &shmem_file_operations
);
2049 sb
= file_inode(file
)->i_sb
;
2052 * Called directly from mm/mmap.c, or drivers/char/mem.c
2053 * for "/dev/zero", to create a shared anonymous object.
2055 if (IS_ERR(shm_mnt
))
2057 sb
= shm_mnt
->mnt_sb
;
2059 if (SHMEM_SB(sb
)->huge
== SHMEM_HUGE_NEVER
)
2063 offset
= (pgoff
<< PAGE_SHIFT
) & (HPAGE_PMD_SIZE
-1);
2064 if (offset
&& offset
+ len
< 2 * HPAGE_PMD_SIZE
)
2066 if ((addr
& (HPAGE_PMD_SIZE
-1)) == offset
)
2069 inflated_len
= len
+ HPAGE_PMD_SIZE
- PAGE_SIZE
;
2070 if (inflated_len
> TASK_SIZE
)
2072 if (inflated_len
< len
)
2075 inflated_addr
= get_area(NULL
, 0, inflated_len
, 0, flags
);
2076 if (IS_ERR_VALUE(inflated_addr
))
2078 if (inflated_addr
& ~PAGE_MASK
)
2081 inflated_offset
= inflated_addr
& (HPAGE_PMD_SIZE
-1);
2082 inflated_addr
+= offset
- inflated_offset
;
2083 if (inflated_offset
> offset
)
2084 inflated_addr
+= HPAGE_PMD_SIZE
;
2086 if (inflated_addr
> TASK_SIZE
- len
)
2088 return inflated_addr
;
2092 static int shmem_set_policy(struct vm_area_struct
*vma
, struct mempolicy
*mpol
)
2094 struct inode
*inode
= file_inode(vma
->vm_file
);
2095 return mpol_set_shared_policy(&SHMEM_I(inode
)->policy
, vma
, mpol
);
2098 static struct mempolicy
*shmem_get_policy(struct vm_area_struct
*vma
,
2101 struct inode
*inode
= file_inode(vma
->vm_file
);
2104 index
= ((addr
- vma
->vm_start
) >> PAGE_SHIFT
) + vma
->vm_pgoff
;
2105 return mpol_shared_policy_lookup(&SHMEM_I(inode
)->policy
, index
);
2109 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
2111 struct inode
*inode
= file_inode(file
);
2112 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2113 int retval
= -ENOMEM
;
2115 spin_lock_irq(&info
->lock
);
2116 if (lock
&& !(info
->flags
& VM_LOCKED
)) {
2117 if (!user_shm_lock(inode
->i_size
, user
))
2119 info
->flags
|= VM_LOCKED
;
2120 mapping_set_unevictable(file
->f_mapping
);
2122 if (!lock
&& (info
->flags
& VM_LOCKED
) && user
) {
2123 user_shm_unlock(inode
->i_size
, user
);
2124 info
->flags
&= ~VM_LOCKED
;
2125 mapping_clear_unevictable(file
->f_mapping
);
2130 spin_unlock_irq(&info
->lock
);
2134 static int shmem_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2136 file_accessed(file
);
2137 vma
->vm_ops
= &shmem_vm_ops
;
2138 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
) &&
2139 ((vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
) <
2140 (vma
->vm_end
& HPAGE_PMD_MASK
)) {
2141 khugepaged_enter(vma
, vma
->vm_flags
);
2146 static struct inode
*shmem_get_inode(struct super_block
*sb
, const struct inode
*dir
,
2147 umode_t mode
, dev_t dev
, unsigned long flags
)
2149 struct inode
*inode
;
2150 struct shmem_inode_info
*info
;
2151 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
2154 if (shmem_reserve_inode(sb
))
2157 inode
= new_inode(sb
);
2159 inode_init_owner(inode
, dir
, mode
);
2160 inode
->i_blocks
= 0;
2161 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
2162 inode
->i_generation
= prandom_u32();
2163 info
= SHMEM_I(inode
);
2164 memset(info
, 0, (char *)inode
- (char *)info
);
2165 spin_lock_init(&info
->lock
);
2166 info
->seals
= F_SEAL_SEAL
;
2167 info
->flags
= flags
& VM_NORESERVE
;
2168 INIT_LIST_HEAD(&info
->shrinklist
);
2169 INIT_LIST_HEAD(&info
->swaplist
);
2170 simple_xattrs_init(&info
->xattrs
);
2171 cache_no_acl(inode
);
2173 switch (mode
& S_IFMT
) {
2175 inode
->i_op
= &shmem_special_inode_operations
;
2176 init_special_inode(inode
, mode
, dev
);
2179 inode
->i_mapping
->a_ops
= &shmem_aops
;
2180 inode
->i_op
= &shmem_inode_operations
;
2181 inode
->i_fop
= &shmem_file_operations
;
2182 mpol_shared_policy_init(&info
->policy
,
2183 shmem_get_sbmpol(sbinfo
));
2187 /* Some things misbehave if size == 0 on a directory */
2188 inode
->i_size
= 2 * BOGO_DIRENT_SIZE
;
2189 inode
->i_op
= &shmem_dir_inode_operations
;
2190 inode
->i_fop
= &simple_dir_operations
;
2194 * Must not load anything in the rbtree,
2195 * mpol_free_shared_policy will not be called.
2197 mpol_shared_policy_init(&info
->policy
, NULL
);
2201 if (!sbinfo
->idr_nouse
) {
2202 /* inum 0 and 1 are unused */
2203 mutex_lock(&sbinfo
->idr_lock
);
2204 ino
= idr_alloc(&sbinfo
->idr
, inode
, 2, INT_MAX
,
2208 mutex_unlock(&sbinfo
->idr_lock
);
2209 __insert_inode_hash(inode
, inode
->i_ino
);
2212 mutex_unlock(&sbinfo
->idr_lock
);
2214 /* shmem_free_inode() will be called */
2218 inode
->i_ino
= get_next_ino();
2220 lockdep_annotate_inode_mutex_key(inode
);
2222 shmem_free_inode(sb
);
2226 bool shmem_mapping(struct address_space
*mapping
)
2228 return mapping
->a_ops
== &shmem_aops
;
2231 static int shmem_mfill_atomic_pte(struct mm_struct
*dst_mm
,
2233 struct vm_area_struct
*dst_vma
,
2234 unsigned long dst_addr
,
2235 unsigned long src_addr
,
2237 struct page
**pagep
)
2239 struct inode
*inode
= file_inode(dst_vma
->vm_file
);
2240 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2241 struct address_space
*mapping
= inode
->i_mapping
;
2242 gfp_t gfp
= mapping_gfp_mask(mapping
);
2243 pgoff_t pgoff
= linear_page_index(dst_vma
, dst_addr
);
2244 struct mem_cgroup
*memcg
;
2248 pte_t _dst_pte
, *dst_pte
;
2250 pgoff_t offset
, max_off
;
2253 if (!shmem_inode_acct_block(inode
, 1))
2257 page
= shmem_alloc_page(gfp
, info
, pgoff
);
2259 goto out_unacct_blocks
;
2261 if (!zeropage
) { /* mcopy_atomic */
2262 page_kaddr
= kmap_atomic(page
);
2263 ret
= copy_from_user(page_kaddr
,
2264 (const void __user
*)src_addr
,
2266 kunmap_atomic(page_kaddr
);
2268 /* fallback to copy_from_user outside mmap_sem */
2269 if (unlikely(ret
)) {
2271 shmem_inode_unacct_blocks(inode
, 1);
2272 /* don't free the page */
2275 } else { /* mfill_zeropage_atomic */
2276 clear_highpage(page
);
2283 VM_BUG_ON(PageLocked(page
) || PageSwapBacked(page
));
2284 __SetPageLocked(page
);
2285 __SetPageSwapBacked(page
);
2286 __SetPageUptodate(page
);
2289 offset
= linear_page_index(dst_vma
, dst_addr
);
2290 max_off
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
2291 if (unlikely(offset
>= max_off
))
2294 ret
= mem_cgroup_try_charge_delay(page
, dst_mm
, gfp
, &memcg
, false);
2298 ret
= shmem_add_to_page_cache(page
, mapping
, pgoff
, NULL
,
2299 gfp
& GFP_RECLAIM_MASK
);
2301 goto out_release_uncharge
;
2303 mem_cgroup_commit_charge(page
, memcg
, false, false);
2305 _dst_pte
= mk_pte(page
, dst_vma
->vm_page_prot
);
2306 if (dst_vma
->vm_flags
& VM_WRITE
)
2307 _dst_pte
= pte_mkwrite(pte_mkdirty(_dst_pte
));
2310 * We don't set the pte dirty if the vma has no
2311 * VM_WRITE permission, so mark the page dirty or it
2312 * could be freed from under us. We could do it
2313 * unconditionally before unlock_page(), but doing it
2314 * only if VM_WRITE is not set is faster.
2316 set_page_dirty(page
);
2319 dst_pte
= pte_offset_map_lock(dst_mm
, dst_pmd
, dst_addr
, &ptl
);
2322 max_off
= DIV_ROUND_UP(i_size_read(inode
), PAGE_SIZE
);
2323 if (unlikely(offset
>= max_off
))
2324 goto out_release_uncharge_unlock
;
2327 if (!pte_none(*dst_pte
))
2328 goto out_release_uncharge_unlock
;
2330 lru_cache_add_anon(page
);
2332 spin_lock(&info
->lock
);
2334 inode
->i_blocks
+= BLOCKS_PER_PAGE
;
2335 shmem_recalc_inode(inode
);
2336 spin_unlock(&info
->lock
);
2338 inc_mm_counter(dst_mm
, mm_counter_file(page
));
2339 page_add_file_rmap(page
, false);
2340 set_pte_at(dst_mm
, dst_addr
, dst_pte
, _dst_pte
);
2342 /* No need to invalidate - it was non-present before */
2343 update_mmu_cache(dst_vma
, dst_addr
, dst_pte
);
2344 pte_unmap_unlock(dst_pte
, ptl
);
2349 out_release_uncharge_unlock
:
2350 pte_unmap_unlock(dst_pte
, ptl
);
2351 ClearPageDirty(page
);
2352 delete_from_page_cache(page
);
2353 out_release_uncharge
:
2354 mem_cgroup_cancel_charge(page
, memcg
, false);
2359 shmem_inode_unacct_blocks(inode
, 1);
2363 int shmem_mcopy_atomic_pte(struct mm_struct
*dst_mm
,
2365 struct vm_area_struct
*dst_vma
,
2366 unsigned long dst_addr
,
2367 unsigned long src_addr
,
2368 struct page
**pagep
)
2370 return shmem_mfill_atomic_pte(dst_mm
, dst_pmd
, dst_vma
,
2371 dst_addr
, src_addr
, false, pagep
);
2374 int shmem_mfill_zeropage_pte(struct mm_struct
*dst_mm
,
2376 struct vm_area_struct
*dst_vma
,
2377 unsigned long dst_addr
)
2379 struct page
*page
= NULL
;
2381 return shmem_mfill_atomic_pte(dst_mm
, dst_pmd
, dst_vma
,
2382 dst_addr
, 0, true, &page
);
2386 static const struct inode_operations shmem_symlink_inode_operations
;
2387 static const struct inode_operations shmem_short_symlink_operations
;
2389 #ifdef CONFIG_TMPFS_XATTR
2390 static int shmem_initxattrs(struct inode
*, const struct xattr
*, void *);
2392 #define shmem_initxattrs NULL
2396 shmem_write_begin(struct file
*file
, struct address_space
*mapping
,
2397 loff_t pos
, unsigned len
, unsigned flags
,
2398 struct page
**pagep
, void **fsdata
)
2400 struct inode
*inode
= mapping
->host
;
2401 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2402 pgoff_t index
= pos
>> PAGE_SHIFT
;
2404 /* i_mutex is held by caller */
2405 if (unlikely(info
->seals
& (F_SEAL_WRITE
| F_SEAL_GROW
))) {
2406 if (info
->seals
& F_SEAL_WRITE
)
2408 if ((info
->seals
& F_SEAL_GROW
) && pos
+ len
> inode
->i_size
)
2412 return shmem_getpage(inode
, index
, pagep
, SGP_WRITE
);
2416 shmem_write_end(struct file
*file
, struct address_space
*mapping
,
2417 loff_t pos
, unsigned len
, unsigned copied
,
2418 struct page
*page
, void *fsdata
)
2420 struct inode
*inode
= mapping
->host
;
2422 if (pos
+ copied
> inode
->i_size
)
2423 i_size_write(inode
, pos
+ copied
);
2425 if (!PageUptodate(page
)) {
2426 struct page
*head
= compound_head(page
);
2427 if (PageTransCompound(page
)) {
2430 for (i
= 0; i
< HPAGE_PMD_NR
; i
++) {
2431 if (head
+ i
== page
)
2433 clear_highpage(head
+ i
);
2434 flush_dcache_page(head
+ i
);
2437 if (copied
< PAGE_SIZE
) {
2438 unsigned from
= pos
& (PAGE_SIZE
- 1);
2439 zero_user_segments(page
, 0, from
,
2440 from
+ copied
, PAGE_SIZE
);
2442 SetPageUptodate(head
);
2444 set_page_dirty(page
);
2451 static ssize_t
shmem_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
2453 struct file
*file
= iocb
->ki_filp
;
2454 struct inode
*inode
= file_inode(file
);
2455 struct address_space
*mapping
= inode
->i_mapping
;
2457 unsigned long offset
;
2458 enum sgp_type sgp
= SGP_READ
;
2461 loff_t
*ppos
= &iocb
->ki_pos
;
2464 * Might this read be for a stacking filesystem? Then when reading
2465 * holes of a sparse file, we actually need to allocate those pages,
2466 * and even mark them dirty, so it cannot exceed the max_blocks limit.
2468 if (!iter_is_iovec(to
))
2471 index
= *ppos
>> PAGE_SHIFT
;
2472 offset
= *ppos
& ~PAGE_MASK
;
2475 struct page
*page
= NULL
;
2477 unsigned long nr
, ret
;
2478 loff_t i_size
= i_size_read(inode
);
2480 end_index
= i_size
>> PAGE_SHIFT
;
2481 if (index
> end_index
)
2483 if (index
== end_index
) {
2484 nr
= i_size
& ~PAGE_MASK
;
2489 error
= shmem_getpage(inode
, index
, &page
, sgp
);
2491 if (error
== -EINVAL
)
2496 if (sgp
== SGP_CACHE
)
2497 set_page_dirty(page
);
2502 * We must evaluate after, since reads (unlike writes)
2503 * are called without i_mutex protection against truncate
2506 i_size
= i_size_read(inode
);
2507 end_index
= i_size
>> PAGE_SHIFT
;
2508 if (index
== end_index
) {
2509 nr
= i_size
& ~PAGE_MASK
;
2520 * If users can be writing to this page using arbitrary
2521 * virtual addresses, take care about potential aliasing
2522 * before reading the page on the kernel side.
2524 if (mapping_writably_mapped(mapping
))
2525 flush_dcache_page(page
);
2527 * Mark the page accessed if we read the beginning.
2530 mark_page_accessed(page
);
2532 page
= ZERO_PAGE(0);
2537 * Ok, we have the page, and it's up-to-date, so
2538 * now we can copy it to user space...
2540 ret
= copy_page_to_iter(page
, offset
, nr
, to
);
2543 index
+= offset
>> PAGE_SHIFT
;
2544 offset
&= ~PAGE_MASK
;
2547 if (!iov_iter_count(to
))
2556 *ppos
= ((loff_t
) index
<< PAGE_SHIFT
) + offset
;
2557 file_accessed(file
);
2558 return retval
? retval
: error
;
2562 * llseek SEEK_DATA or SEEK_HOLE through the page cache.
2564 static pgoff_t
shmem_seek_hole_data(struct address_space
*mapping
,
2565 pgoff_t index
, pgoff_t end
, int whence
)
2568 struct pagevec pvec
;
2569 pgoff_t indices
[PAGEVEC_SIZE
];
2573 pagevec_init(&pvec
);
2574 pvec
.nr
= 1; /* start small: we may be there already */
2576 pvec
.nr
= find_get_entries(mapping
, index
,
2577 pvec
.nr
, pvec
.pages
, indices
);
2579 if (whence
== SEEK_DATA
)
2583 for (i
= 0; i
< pvec
.nr
; i
++, index
++) {
2584 if (index
< indices
[i
]) {
2585 if (whence
== SEEK_HOLE
) {
2591 page
= pvec
.pages
[i
];
2592 if (page
&& !xa_is_value(page
)) {
2593 if (!PageUptodate(page
))
2597 (page
&& whence
== SEEK_DATA
) ||
2598 (!page
&& whence
== SEEK_HOLE
)) {
2603 pagevec_remove_exceptionals(&pvec
);
2604 pagevec_release(&pvec
);
2605 pvec
.nr
= PAGEVEC_SIZE
;
2611 static loff_t
shmem_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2613 struct address_space
*mapping
= file
->f_mapping
;
2614 struct inode
*inode
= mapping
->host
;
2618 if (whence
!= SEEK_DATA
&& whence
!= SEEK_HOLE
)
2619 return generic_file_llseek_size(file
, offset
, whence
,
2620 MAX_LFS_FILESIZE
, i_size_read(inode
));
2622 /* We're holding i_mutex so we can access i_size directly */
2624 if (offset
< 0 || offset
>= inode
->i_size
)
2627 start
= offset
>> PAGE_SHIFT
;
2628 end
= (inode
->i_size
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2629 new_offset
= shmem_seek_hole_data(mapping
, start
, end
, whence
);
2630 new_offset
<<= PAGE_SHIFT
;
2631 if (new_offset
> offset
) {
2632 if (new_offset
< inode
->i_size
)
2633 offset
= new_offset
;
2634 else if (whence
== SEEK_DATA
)
2637 offset
= inode
->i_size
;
2642 offset
= vfs_setpos(file
, offset
, MAX_LFS_FILESIZE
);
2643 inode_unlock(inode
);
2647 static long shmem_fallocate(struct file
*file
, int mode
, loff_t offset
,
2650 struct inode
*inode
= file_inode(file
);
2651 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
2652 struct shmem_inode_info
*info
= SHMEM_I(inode
);
2653 struct shmem_falloc shmem_falloc
;
2654 pgoff_t start
, index
, end
;
2657 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2662 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2663 struct address_space
*mapping
= file
->f_mapping
;
2664 loff_t unmap_start
= round_up(offset
, PAGE_SIZE
);
2665 loff_t unmap_end
= round_down(offset
+ len
, PAGE_SIZE
) - 1;
2666 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq
);
2668 /* protected by i_mutex */
2669 if (info
->seals
& F_SEAL_WRITE
) {
2674 shmem_falloc
.waitq
= &shmem_falloc_waitq
;
2675 shmem_falloc
.start
= unmap_start
>> PAGE_SHIFT
;
2676 shmem_falloc
.next
= (unmap_end
+ 1) >> PAGE_SHIFT
;
2677 spin_lock(&inode
->i_lock
);
2678 inode
->i_private
= &shmem_falloc
;
2679 spin_unlock(&inode
->i_lock
);
2681 if ((u64
)unmap_end
> (u64
)unmap_start
)
2682 unmap_mapping_range(mapping
, unmap_start
,
2683 1 + unmap_end
- unmap_start
, 0);
2684 shmem_truncate_range(inode
, offset
, offset
+ len
- 1);
2685 /* No need to unmap again: hole-punching leaves COWed pages */
2687 spin_lock(&inode
->i_lock
);
2688 inode
->i_private
= NULL
;
2689 wake_up_all(&shmem_falloc_waitq
);
2690 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq
.head
));
2691 spin_unlock(&inode
->i_lock
);
2696 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
2697 error
= inode_newsize_ok(inode
, offset
+ len
);
2701 if ((info
->seals
& F_SEAL_GROW
) && offset
+ len
> inode
->i_size
) {
2706 start
= offset
>> PAGE_SHIFT
;
2707 end
= (offset
+ len
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
2708 /* Try to avoid a swapstorm if len is impossible to satisfy */
2709 if (sbinfo
->max_blocks
&& end
- start
> sbinfo
->max_blocks
) {
2714 shmem_falloc
.waitq
= NULL
;
2715 shmem_falloc
.start
= start
;
2716 shmem_falloc
.next
= start
;
2717 shmem_falloc
.nr_falloced
= 0;
2718 shmem_falloc
.nr_unswapped
= 0;
2719 spin_lock(&inode
->i_lock
);
2720 inode
->i_private
= &shmem_falloc
;
2721 spin_unlock(&inode
->i_lock
);
2723 for (index
= start
; index
< end
; index
++) {
2727 * Good, the fallocate(2) manpage permits EINTR: we may have
2728 * been interrupted because we are using up too much memory.
2730 if (signal_pending(current
))
2732 else if (shmem_falloc
.nr_unswapped
> shmem_falloc
.nr_falloced
)
2735 error
= shmem_getpage(inode
, index
, &page
, SGP_FALLOC
);
2737 /* Remove the !PageUptodate pages we added */
2738 if (index
> start
) {
2739 shmem_undo_range(inode
,
2740 (loff_t
)start
<< PAGE_SHIFT
,
2741 ((loff_t
)index
<< PAGE_SHIFT
) - 1, true);
2747 * Inform shmem_writepage() how far we have reached.
2748 * No need for lock or barrier: we have the page lock.
2750 shmem_falloc
.next
++;
2751 if (!PageUptodate(page
))
2752 shmem_falloc
.nr_falloced
++;
2755 * If !PageUptodate, leave it that way so that freeable pages
2756 * can be recognized if we need to rollback on error later.
2757 * But set_page_dirty so that memory pressure will swap rather
2758 * than free the pages we are allocating (and SGP_CACHE pages
2759 * might still be clean: we now need to mark those dirty too).
2761 set_page_dirty(page
);
2767 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ len
> inode
->i_size
)
2768 i_size_write(inode
, offset
+ len
);
2769 inode
->i_ctime
= current_time(inode
);
2771 spin_lock(&inode
->i_lock
);
2772 inode
->i_private
= NULL
;
2773 spin_unlock(&inode
->i_lock
);
2775 inode_unlock(inode
);
2779 static int shmem_statfs(struct dentry
*dentry
, struct kstatfs
*buf
)
2781 struct shmem_sb_info
*sbinfo
= SHMEM_SB(dentry
->d_sb
);
2783 buf
->f_type
= TMPFS_MAGIC
;
2784 buf
->f_bsize
= PAGE_SIZE
;
2785 buf
->f_namelen
= NAME_MAX
;
2786 if (sbinfo
->max_blocks
) {
2787 buf
->f_blocks
= sbinfo
->max_blocks
;
2789 buf
->f_bfree
= sbinfo
->max_blocks
-
2790 percpu_counter_sum(&sbinfo
->used_blocks
);
2792 if (sbinfo
->max_inodes
) {
2793 buf
->f_files
= sbinfo
->max_inodes
;
2794 buf
->f_ffree
= sbinfo
->free_inodes
;
2796 /* else leave those fields 0 like simple_statfs */
2801 * File creation. Allocate an inode, and we're done..
2804 shmem_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
, dev_t dev
)
2806 struct inode
*inode
;
2807 int error
= -ENOSPC
;
2809 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, dev
, VM_NORESERVE
);
2811 error
= simple_acl_create(dir
, inode
);
2814 error
= security_inode_init_security(inode
, dir
,
2816 shmem_initxattrs
, NULL
);
2817 if (error
&& error
!= -EOPNOTSUPP
)
2821 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2822 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
2823 d_instantiate(dentry
, inode
);
2824 dget(dentry
); /* Extra count - pin the dentry in core */
2833 shmem_tmpfile(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2835 struct inode
*inode
;
2836 int error
= -ENOSPC
;
2838 inode
= shmem_get_inode(dir
->i_sb
, dir
, mode
, 0, VM_NORESERVE
);
2840 error
= security_inode_init_security(inode
, dir
,
2842 shmem_initxattrs
, NULL
);
2843 if (error
&& error
!= -EOPNOTSUPP
)
2845 error
= simple_acl_create(dir
, inode
);
2848 d_tmpfile(dentry
, inode
);
2856 static int shmem_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
2860 if ((error
= shmem_mknod(dir
, dentry
, mode
| S_IFDIR
, 0)))
2866 static int shmem_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
2869 return shmem_mknod(dir
, dentry
, mode
| S_IFREG
, 0);
2875 static int shmem_link(struct dentry
*old_dentry
, struct inode
*dir
, struct dentry
*dentry
)
2877 struct inode
*inode
= d_inode(old_dentry
);
2881 * No ordinary (disk based) filesystem counts links as inodes;
2882 * but each new link needs a new dentry, pinning lowmem, and
2883 * tmpfs dentries cannot be pruned until they are unlinked.
2884 * But if an O_TMPFILE file is linked into the tmpfs, the
2885 * first link must skip that, to get the accounting right.
2887 if (inode
->i_nlink
) {
2888 ret
= shmem_reserve_inode(inode
->i_sb
);
2893 dir
->i_size
+= BOGO_DIRENT_SIZE
;
2894 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= current_time(inode
);
2896 ihold(inode
); /* New dentry reference */
2897 dget(dentry
); /* Extra pinning count for the created dentry */
2898 d_instantiate(dentry
, inode
);
2903 static int shmem_unlink(struct inode
*dir
, struct dentry
*dentry
)
2905 struct inode
*inode
= d_inode(dentry
);
2907 if (inode
->i_nlink
> 1 && !S_ISDIR(inode
->i_mode
))
2908 shmem_free_inode(inode
->i_sb
);
2910 dir
->i_size
-= BOGO_DIRENT_SIZE
;
2911 inode
->i_ctime
= dir
->i_ctime
= dir
->i_mtime
= current_time(inode
);
2913 dput(dentry
); /* Undo the count from "create" - this does all the work */
2917 static int shmem_rmdir(struct inode
*dir
, struct dentry
*dentry
)
2919 if (!simple_empty(dentry
))
2922 drop_nlink(d_inode(dentry
));
2924 return shmem_unlink(dir
, dentry
);
2927 static int shmem_exchange(struct inode
*old_dir
, struct dentry
*old_dentry
, struct inode
*new_dir
, struct dentry
*new_dentry
)
2929 bool old_is_dir
= d_is_dir(old_dentry
);
2930 bool new_is_dir
= d_is_dir(new_dentry
);
2932 if (old_dir
!= new_dir
&& old_is_dir
!= new_is_dir
) {
2934 drop_nlink(old_dir
);
2937 drop_nlink(new_dir
);
2941 old_dir
->i_ctime
= old_dir
->i_mtime
=
2942 new_dir
->i_ctime
= new_dir
->i_mtime
=
2943 d_inode(old_dentry
)->i_ctime
=
2944 d_inode(new_dentry
)->i_ctime
= current_time(old_dir
);
2949 static int shmem_whiteout(struct inode
*old_dir
, struct dentry
*old_dentry
)
2951 struct dentry
*whiteout
;
2954 whiteout
= d_alloc(old_dentry
->d_parent
, &old_dentry
->d_name
);
2958 error
= shmem_mknod(old_dir
, whiteout
,
2959 S_IFCHR
| WHITEOUT_MODE
, WHITEOUT_DEV
);
2965 * Cheat and hash the whiteout while the old dentry is still in
2966 * place, instead of playing games with FS_RENAME_DOES_D_MOVE.
2968 * d_lookup() will consistently find one of them at this point,
2969 * not sure which one, but that isn't even important.
2976 * The VFS layer already does all the dentry stuff for rename,
2977 * we just have to decrement the usage count for the target if
2978 * it exists so that the VFS layer correctly free's it when it
2981 static int shmem_rename2(struct inode
*old_dir
, struct dentry
*old_dentry
, struct inode
*new_dir
, struct dentry
*new_dentry
, unsigned int flags
)
2983 struct inode
*inode
= d_inode(old_dentry
);
2984 int they_are_dirs
= S_ISDIR(inode
->i_mode
);
2986 if (flags
& ~(RENAME_NOREPLACE
| RENAME_EXCHANGE
| RENAME_WHITEOUT
))
2989 if (flags
& RENAME_EXCHANGE
)
2990 return shmem_exchange(old_dir
, old_dentry
, new_dir
, new_dentry
);
2992 if (!simple_empty(new_dentry
))
2995 if (flags
& RENAME_WHITEOUT
) {
2998 error
= shmem_whiteout(old_dir
, old_dentry
);
3003 if (d_really_is_positive(new_dentry
)) {
3004 (void) shmem_unlink(new_dir
, new_dentry
);
3005 if (they_are_dirs
) {
3006 drop_nlink(d_inode(new_dentry
));
3007 drop_nlink(old_dir
);
3009 } else if (they_are_dirs
) {
3010 drop_nlink(old_dir
);
3014 old_dir
->i_size
-= BOGO_DIRENT_SIZE
;
3015 new_dir
->i_size
+= BOGO_DIRENT_SIZE
;
3016 old_dir
->i_ctime
= old_dir
->i_mtime
=
3017 new_dir
->i_ctime
= new_dir
->i_mtime
=
3018 inode
->i_ctime
= current_time(old_dir
);
3022 static int shmem_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
3026 struct inode
*inode
;
3029 len
= strlen(symname
) + 1;
3030 if (len
> PAGE_SIZE
)
3031 return -ENAMETOOLONG
;
3033 inode
= shmem_get_inode(dir
->i_sb
, dir
, S_IFLNK
| 0777, 0,
3038 error
= security_inode_init_security(inode
, dir
, &dentry
->d_name
,
3039 shmem_initxattrs
, NULL
);
3041 if (error
!= -EOPNOTSUPP
) {
3048 inode
->i_size
= len
-1;
3049 if (len
<= SHORT_SYMLINK_LEN
) {
3050 inode
->i_link
= kmemdup(symname
, len
, GFP_KERNEL
);
3051 if (!inode
->i_link
) {
3055 inode
->i_op
= &shmem_short_symlink_operations
;
3057 inode_nohighmem(inode
);
3058 error
= shmem_getpage(inode
, 0, &page
, SGP_WRITE
);
3063 inode
->i_mapping
->a_ops
= &shmem_aops
;
3064 inode
->i_op
= &shmem_symlink_inode_operations
;
3065 memcpy(page_address(page
), symname
, len
);
3066 SetPageUptodate(page
);
3067 set_page_dirty(page
);
3071 dir
->i_size
+= BOGO_DIRENT_SIZE
;
3072 dir
->i_ctime
= dir
->i_mtime
= current_time(dir
);
3073 d_instantiate(dentry
, inode
);
3078 static void shmem_put_link(void *arg
)
3080 mark_page_accessed(arg
);
3084 static const char *shmem_get_link(struct dentry
*dentry
,
3085 struct inode
*inode
,
3086 struct delayed_call
*done
)
3088 struct page
*page
= NULL
;
3091 page
= find_get_page(inode
->i_mapping
, 0);
3093 return ERR_PTR(-ECHILD
);
3094 if (!PageUptodate(page
)) {
3096 return ERR_PTR(-ECHILD
);
3099 error
= shmem_getpage(inode
, 0, &page
, SGP_READ
);
3101 return ERR_PTR(error
);
3104 set_delayed_call(done
, shmem_put_link
, page
);
3105 return page_address(page
);
3108 #ifdef CONFIG_TMPFS_XATTR
3110 * Superblocks without xattr inode operations may get some security.* xattr
3111 * support from the LSM "for free". As soon as we have any other xattrs
3112 * like ACLs, we also need to implement the security.* handlers at
3113 * filesystem level, though.
3117 * Callback for security_inode_init_security() for acquiring xattrs.
3119 static int shmem_initxattrs(struct inode
*inode
,
3120 const struct xattr
*xattr_array
,
3123 struct shmem_inode_info
*info
= SHMEM_I(inode
);
3124 const struct xattr
*xattr
;
3125 struct simple_xattr
*new_xattr
;
3128 for (xattr
= xattr_array
; xattr
->name
!= NULL
; xattr
++) {
3129 new_xattr
= simple_xattr_alloc(xattr
->value
, xattr
->value_len
);
3133 len
= strlen(xattr
->name
) + 1;
3134 new_xattr
->name
= kmalloc(XATTR_SECURITY_PREFIX_LEN
+ len
,
3136 if (!new_xattr
->name
) {
3141 memcpy(new_xattr
->name
, XATTR_SECURITY_PREFIX
,
3142 XATTR_SECURITY_PREFIX_LEN
);
3143 memcpy(new_xattr
->name
+ XATTR_SECURITY_PREFIX_LEN
,
3146 simple_xattr_list_add(&info
->xattrs
, new_xattr
);
3152 static int shmem_xattr_handler_get(const struct xattr_handler
*handler
,
3153 struct dentry
*unused
, struct inode
*inode
,
3154 const char *name
, void *buffer
, size_t size
)
3156 struct shmem_inode_info
*info
= SHMEM_I(inode
);
3158 name
= xattr_full_name(handler
, name
);
3159 return simple_xattr_get(&info
->xattrs
, name
, buffer
, size
);
3162 static int shmem_xattr_handler_set(const struct xattr_handler
*handler
,
3163 struct dentry
*unused
, struct inode
*inode
,
3164 const char *name
, const void *value
,
3165 size_t size
, int flags
)
3167 struct shmem_inode_info
*info
= SHMEM_I(inode
);
3169 name
= xattr_full_name(handler
, name
);
3170 return simple_xattr_set(&info
->xattrs
, name
, value
, size
, flags
);
3173 static const struct xattr_handler shmem_security_xattr_handler
= {
3174 .prefix
= XATTR_SECURITY_PREFIX
,
3175 .get
= shmem_xattr_handler_get
,
3176 .set
= shmem_xattr_handler_set
,
3179 static const struct xattr_handler shmem_trusted_xattr_handler
= {
3180 .prefix
= XATTR_TRUSTED_PREFIX
,
3181 .get
= shmem_xattr_handler_get
,
3182 .set
= shmem_xattr_handler_set
,
3185 static const struct xattr_handler
*shmem_xattr_handlers
[] = {
3186 #ifdef CONFIG_TMPFS_POSIX_ACL
3187 &posix_acl_access_xattr_handler
,
3188 &posix_acl_default_xattr_handler
,
3190 &shmem_security_xattr_handler
,
3191 &shmem_trusted_xattr_handler
,
3195 static ssize_t
shmem_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
3197 struct shmem_inode_info
*info
= SHMEM_I(d_inode(dentry
));
3198 return simple_xattr_list(d_inode(dentry
), &info
->xattrs
, buffer
, size
);
3200 #endif /* CONFIG_TMPFS_XATTR */
3202 static const struct inode_operations shmem_short_symlink_operations
= {
3203 .get_link
= simple_get_link
,
3204 #ifdef CONFIG_TMPFS_XATTR
3205 .listxattr
= shmem_listxattr
,
3209 static const struct inode_operations shmem_symlink_inode_operations
= {
3210 .get_link
= shmem_get_link
,
3211 #ifdef CONFIG_TMPFS_XATTR
3212 .listxattr
= shmem_listxattr
,
3216 static struct dentry
*shmem_get_parent(struct dentry
*child
)
3218 return ERR_PTR(-ESTALE
);
3221 static int shmem_match(struct inode
*ino
, void *vfh
)
3225 return ino
->i_ino
== inum
&& fh
[0] == ino
->i_generation
;
3228 /* Find any alias of inode, but prefer a hashed alias */
3229 static struct dentry
*shmem_find_alias(struct inode
*inode
)
3231 struct dentry
*alias
= d_find_alias(inode
);
3233 return alias
?: d_find_any_alias(inode
);
3237 static struct dentry
*shmem_fh_to_dentry(struct super_block
*sb
,
3238 struct fid
*fid
, int fh_len
, int fh_type
)
3240 struct inode
*inode
;
3241 struct dentry
*dentry
= NULL
;
3248 inode
= ilookup5(sb
, inum
, shmem_match
, fid
->raw
);
3250 dentry
= shmem_find_alias(inode
);
3257 static int shmem_encode_fh(struct inode
*inode
, __u32
*fh
, int *len
,
3258 struct inode
*parent
)
3262 return FILEID_INVALID
;
3265 fh
[0] = inode
->i_generation
;
3266 fh
[1] = inode
->i_ino
;
3272 static const struct export_operations shmem_export_ops
= {
3273 .get_parent
= shmem_get_parent
,
3274 .encode_fh
= shmem_encode_fh
,
3275 .fh_to_dentry
= shmem_fh_to_dentry
,
3278 static int shmem_parse_options(char *options
, struct shmem_sb_info
*sbinfo
,
3281 char *this_char
, *value
, *rest
;
3282 struct mempolicy
*mpol
= NULL
;
3286 while (options
!= NULL
) {
3287 this_char
= options
;
3290 * NUL-terminate this option: unfortunately,
3291 * mount options form a comma-separated list,
3292 * but mpol's nodelist may also contain commas.
3294 options
= strchr(options
, ',');
3295 if (options
== NULL
)
3298 if (!isdigit(*options
)) {
3305 if ((value
= strchr(this_char
,'=')) != NULL
) {
3308 pr_err("tmpfs: No value for mount option '%s'\n",
3313 if (!strcmp(this_char
,"size")) {
3314 unsigned long long size
;
3315 size
= memparse(value
,&rest
);
3317 size
<<= PAGE_SHIFT
;
3318 size
*= totalram_pages();
3324 sbinfo
->max_blocks
=
3325 DIV_ROUND_UP(size
, PAGE_SIZE
);
3326 } else if (!strcmp(this_char
,"nr_blocks")) {
3327 sbinfo
->max_blocks
= memparse(value
, &rest
);
3330 } else if (!strcmp(this_char
,"nr_inodes")) {
3331 sbinfo
->max_inodes
= memparse(value
, &rest
);
3332 if (*rest
|| sbinfo
->max_inodes
< 2)
3334 } else if (!strcmp(this_char
,"mode")) {
3337 sbinfo
->mode
= simple_strtoul(value
, &rest
, 8) & 07777;
3340 } else if (!strcmp(this_char
,"uid")) {
3343 uid
= simple_strtoul(value
, &rest
, 0);
3346 sbinfo
->uid
= make_kuid(current_user_ns(), uid
);
3347 if (!uid_valid(sbinfo
->uid
))
3349 } else if (!strcmp(this_char
,"gid")) {
3352 gid
= simple_strtoul(value
, &rest
, 0);
3355 sbinfo
->gid
= make_kgid(current_user_ns(), gid
);
3356 if (!gid_valid(sbinfo
->gid
))
3358 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3359 } else if (!strcmp(this_char
, "huge")) {
3361 huge
= shmem_parse_huge(value
);
3364 if (!has_transparent_hugepage() &&
3365 huge
!= SHMEM_HUGE_NEVER
)
3367 sbinfo
->huge
= huge
;
3370 } else if (!strcmp(this_char
,"mpol")) {
3373 if (mpol_parse_str(value
, &mpol
))
3377 pr_err("tmpfs: Bad mount option %s\n", this_char
);
3381 sbinfo
->mpol
= mpol
;
3385 pr_err("tmpfs: Bad value '%s' for mount option '%s'\n",
3393 static int shmem_remount_fs(struct super_block
*sb
, int *flags
, char *data
)
3395 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
3396 struct shmem_sb_info config
= *sbinfo
;
3398 int error
= -EINVAL
;
3401 if (shmem_parse_options(data
, &config
, true))
3404 spin_lock(&sbinfo
->stat_lock
);
3405 inodes
= sbinfo
->max_inodes
- sbinfo
->free_inodes
;
3406 if (percpu_counter_compare(&sbinfo
->used_blocks
, config
.max_blocks
) > 0)
3408 if (config
.max_inodes
< inodes
)
3411 * Those tests disallow limited->unlimited while any are in use;
3412 * but we must separately disallow unlimited->limited, because
3413 * in that case we have no record of how much is already in use.
3415 if (config
.max_blocks
&& !sbinfo
->max_blocks
)
3417 if (config
.max_inodes
&& !sbinfo
->max_inodes
)
3421 sbinfo
->huge
= config
.huge
;
3422 sbinfo
->max_blocks
= config
.max_blocks
;
3423 sbinfo
->max_inodes
= config
.max_inodes
;
3424 sbinfo
->free_inodes
= config
.max_inodes
- inodes
;
3427 * Preserve previous mempolicy unless mpol remount option was specified.
3430 mpol_put(sbinfo
->mpol
);
3431 sbinfo
->mpol
= config
.mpol
; /* transfers initial ref */
3434 spin_unlock(&sbinfo
->stat_lock
);
3438 static int shmem_show_options(struct seq_file
*seq
, struct dentry
*root
)
3440 struct shmem_sb_info
*sbinfo
= SHMEM_SB(root
->d_sb
);
3442 if (sbinfo
->max_blocks
!= shmem_default_max_blocks())
3443 seq_printf(seq
, ",size=%luk",
3444 sbinfo
->max_blocks
<< (PAGE_SHIFT
- 10));
3445 if (sbinfo
->max_inodes
!= shmem_default_max_inodes())
3446 seq_printf(seq
, ",nr_inodes=%d", sbinfo
->max_inodes
);
3447 if (sbinfo
->mode
!= (0777 | S_ISVTX
))
3448 seq_printf(seq
, ",mode=%03ho", sbinfo
->mode
);
3449 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
));
3455 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3456 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */
3458 seq_printf(seq
, ",huge=%s", shmem_format_huge(sbinfo
->huge
));
3460 shmem_show_mpol(seq
, sbinfo
->mpol
);
3464 #endif /* CONFIG_TMPFS */
3466 static void shmem_put_super(struct super_block
*sb
)
3468 struct shmem_sb_info
*sbinfo
= SHMEM_SB(sb
);
3470 if (!sbinfo
->idr_nouse
)
3471 idr_destroy(&sbinfo
->idr
);
3472 percpu_counter_destroy(&sbinfo
->used_blocks
);
3473 mpol_put(sbinfo
->mpol
);
3475 sb
->s_fs_info
= NULL
;
3478 int shmem_fill_super(struct super_block
*sb
, void *data
, int silent
)
3480 struct inode
*inode
;
3481 struct shmem_sb_info
*sbinfo
;
3484 /* Round up to L1_CACHE_BYTES to resist false sharing */
3485 sbinfo
= kzalloc(max((int)sizeof(struct shmem_sb_info
),
3486 L1_CACHE_BYTES
), GFP_KERNEL
);
3490 mutex_init(&sbinfo
->idr_lock
);
3491 idr_init(&sbinfo
->idr
);
3492 sbinfo
->mode
= 0777 | S_ISVTX
;
3493 sbinfo
->uid
= current_fsuid();
3494 sbinfo
->gid
= current_fsgid();
3495 sb
->s_fs_info
= sbinfo
;
3499 * Per default we only allow half of the physical ram per
3500 * tmpfs instance, limiting inodes to one per page of lowmem;
3501 * but the internal instance is left unlimited.
3503 if (!(sb
->s_flags
& SB_KERNMOUNT
)) {
3504 sbinfo
->max_blocks
= shmem_default_max_blocks();
3505 sbinfo
->max_inodes
= shmem_default_max_inodes();
3506 if (shmem_parse_options(data
, sbinfo
, false)) {
3511 sb
->s_flags
|= SB_NOUSER
;
3513 sb
->s_export_op
= &shmem_export_ops
;
3514 sb
->s_flags
|= SB_NOSEC
;
3516 sb
->s_flags
|= SB_NOUSER
;
3519 spin_lock_init(&sbinfo
->stat_lock
);
3520 if (percpu_counter_init(&sbinfo
->used_blocks
, 0, GFP_KERNEL
))
3522 sbinfo
->free_inodes
= sbinfo
->max_inodes
;
3523 spin_lock_init(&sbinfo
->shrinklist_lock
);
3524 INIT_LIST_HEAD(&sbinfo
->shrinklist
);
3526 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
3527 sb
->s_blocksize
= PAGE_SIZE
;
3528 sb
->s_blocksize_bits
= PAGE_SHIFT
;
3529 sb
->s_magic
= TMPFS_MAGIC
;
3530 sb
->s_op
= &shmem_ops
;
3531 sb
->s_time_gran
= 1;
3532 #ifdef CONFIG_TMPFS_XATTR
3533 sb
->s_xattr
= shmem_xattr_handlers
;
3535 #ifdef CONFIG_TMPFS_POSIX_ACL
3536 sb
->s_flags
|= SB_POSIXACL
;
3538 uuid_gen(&sb
->s_uuid
);
3540 inode
= shmem_get_inode(sb
, NULL
, S_IFDIR
| sbinfo
->mode
, 0, VM_NORESERVE
);
3543 inode
->i_uid
= sbinfo
->uid
;
3544 inode
->i_gid
= sbinfo
->gid
;
3545 sb
->s_root
= d_make_root(inode
);
3551 shmem_put_super(sb
);
3555 static struct kmem_cache
*shmem_inode_cachep
;
3557 static struct inode
*shmem_alloc_inode(struct super_block
*sb
)
3559 struct shmem_inode_info
*info
;
3560 info
= kmem_cache_alloc(shmem_inode_cachep
, GFP_KERNEL
);
3563 return &info
->vfs_inode
;
3566 static void shmem_destroy_callback(struct rcu_head
*head
)
3568 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
3569 if (S_ISLNK(inode
->i_mode
))
3570 kfree(inode
->i_link
);
3571 kmem_cache_free(shmem_inode_cachep
, SHMEM_I(inode
));
3574 static void shmem_destroy_inode(struct inode
*inode
)
3576 if (S_ISREG(inode
->i_mode
))
3577 mpol_free_shared_policy(&SHMEM_I(inode
)->policy
);
3578 call_rcu(&inode
->i_rcu
, shmem_destroy_callback
);
3581 static void shmem_init_inode(void *foo
)
3583 struct shmem_inode_info
*info
= foo
;
3584 inode_init_once(&info
->vfs_inode
);
3587 static void shmem_init_inodecache(void)
3589 shmem_inode_cachep
= kmem_cache_create("shmem_inode_cache",
3590 sizeof(struct shmem_inode_info
),
3591 0, SLAB_PANIC
|SLAB_ACCOUNT
, shmem_init_inode
);
3594 static void shmem_destroy_inodecache(void)
3596 kmem_cache_destroy(shmem_inode_cachep
);
3599 static __init
void shmem_no_idr(struct super_block
*sb
)
3601 struct shmem_sb_info
*sbinfo
;
3603 sbinfo
= SHMEM_SB(sb
);
3604 sbinfo
->idr_nouse
= true;
3605 idr_destroy(&sbinfo
->idr
);
3608 static const struct address_space_operations shmem_aops
= {
3609 .writepage
= shmem_writepage
,
3610 .set_page_dirty
= __set_page_dirty_no_writeback
,
3612 .write_begin
= shmem_write_begin
,
3613 .write_end
= shmem_write_end
,
3615 #ifdef CONFIG_MIGRATION
3616 .migratepage
= migrate_page
,
3618 .error_remove_page
= generic_error_remove_page
,
3621 static const struct file_operations shmem_file_operations
= {
3623 .get_unmapped_area
= shmem_get_unmapped_area
,
3625 .llseek
= shmem_file_llseek
,
3626 .read_iter
= shmem_file_read_iter
,
3627 .write_iter
= generic_file_write_iter
,
3628 .fsync
= noop_fsync
,
3629 .splice_read
= generic_file_splice_read
,
3630 .splice_write
= iter_file_splice_write
,
3631 .fallocate
= shmem_fallocate
,
3635 static const struct inode_operations shmem_inode_operations
= {
3636 .getattr
= shmem_getattr
,
3637 .setattr
= shmem_setattr
,
3638 #ifdef CONFIG_TMPFS_XATTR
3639 .listxattr
= shmem_listxattr
,
3640 .set_acl
= simple_set_acl
,
3644 static const struct inode_operations shmem_dir_inode_operations
= {
3646 .create
= shmem_create
,
3647 .lookup
= simple_lookup
,
3649 .unlink
= shmem_unlink
,
3650 .symlink
= shmem_symlink
,
3651 .mkdir
= shmem_mkdir
,
3652 .rmdir
= shmem_rmdir
,
3653 .mknod
= shmem_mknod
,
3654 .rename
= shmem_rename2
,
3655 .tmpfile
= shmem_tmpfile
,
3657 #ifdef CONFIG_TMPFS_XATTR
3658 .listxattr
= shmem_listxattr
,
3660 #ifdef CONFIG_TMPFS_POSIX_ACL
3661 .setattr
= shmem_setattr
,
3662 .set_acl
= simple_set_acl
,
3666 static const struct inode_operations shmem_special_inode_operations
= {
3667 #ifdef CONFIG_TMPFS_XATTR
3668 .listxattr
= shmem_listxattr
,
3670 #ifdef CONFIG_TMPFS_POSIX_ACL
3671 .setattr
= shmem_setattr
,
3672 .set_acl
= simple_set_acl
,
3676 static const struct super_operations shmem_ops
= {
3677 .alloc_inode
= shmem_alloc_inode
,
3678 .destroy_inode
= shmem_destroy_inode
,
3680 .statfs
= shmem_statfs
,
3681 .remount_fs
= shmem_remount_fs
,
3682 .show_options
= shmem_show_options
,
3684 .evict_inode
= shmem_evict_inode
,
3685 .drop_inode
= generic_delete_inode
,
3686 .put_super
= shmem_put_super
,
3687 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3688 .nr_cached_objects
= shmem_unused_huge_count
,
3689 .free_cached_objects
= shmem_unused_huge_scan
,
3693 static const struct vm_operations_struct shmem_vm_ops
= {
3694 .fault
= shmem_fault
,
3695 .map_pages
= filemap_map_pages
,
3697 .set_policy
= shmem_set_policy
,
3698 .get_policy
= shmem_get_policy
,
3702 static struct dentry
*shmem_mount(struct file_system_type
*fs_type
,
3703 int flags
, const char *dev_name
, void *data
)
3705 return mount_nodev(fs_type
, flags
, data
, shmem_fill_super
);
3708 static struct file_system_type shmem_fs_type
= {
3709 .owner
= THIS_MODULE
,
3711 .mount
= shmem_mount
,
3712 .kill_sb
= kill_litter_super
,
3713 .fs_flags
= FS_USERNS_MOUNT
,
3716 int __init
shmem_init(void)
3720 /* If rootfs called this, don't re-init */
3721 if (shmem_inode_cachep
)
3724 shmem_init_inodecache();
3726 error
= register_filesystem(&shmem_fs_type
);
3728 pr_err("Could not register tmpfs\n");
3732 shm_mnt
= kern_mount(&shmem_fs_type
);
3733 if (IS_ERR(shm_mnt
)) {
3734 error
= PTR_ERR(shm_mnt
);
3735 pr_err("Could not kern_mount tmpfs\n");
3738 shmem_no_idr(shm_mnt
->mnt_sb
);
3740 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3741 if (has_transparent_hugepage() && shmem_huge
> SHMEM_HUGE_DENY
)
3742 SHMEM_SB(shm_mnt
->mnt_sb
)->huge
= shmem_huge
;
3744 shmem_huge
= 0; /* just in case it was patched */
3749 unregister_filesystem(&shmem_fs_type
);
3751 shmem_destroy_inodecache();
3752 shm_mnt
= ERR_PTR(error
);
3756 #if defined(CONFIG_TRANSPARENT_HUGE_PAGECACHE) && defined(CONFIG_SYSFS)
3757 static ssize_t
shmem_enabled_show(struct kobject
*kobj
,
3758 struct kobj_attribute
*attr
, char *buf
)
3762 SHMEM_HUGE_WITHIN_SIZE
,
3770 for (i
= 0, count
= 0; i
< ARRAY_SIZE(values
); i
++) {
3771 const char *fmt
= shmem_huge
== values
[i
] ? "[%s] " : "%s ";
3773 count
+= sprintf(buf
+ count
, fmt
,
3774 shmem_format_huge(values
[i
]));
3776 buf
[count
- 1] = '\n';
3780 static ssize_t
shmem_enabled_store(struct kobject
*kobj
,
3781 struct kobj_attribute
*attr
, const char *buf
, size_t count
)
3786 if (count
+ 1 > sizeof(tmp
))
3788 memcpy(tmp
, buf
, count
);
3790 if (count
&& tmp
[count
- 1] == '\n')
3791 tmp
[count
- 1] = '\0';
3793 huge
= shmem_parse_huge(tmp
);
3794 if (huge
== -EINVAL
)
3796 if (!has_transparent_hugepage() &&
3797 huge
!= SHMEM_HUGE_NEVER
&& huge
!= SHMEM_HUGE_DENY
)
3801 if (shmem_huge
> SHMEM_HUGE_DENY
)
3802 SHMEM_SB(shm_mnt
->mnt_sb
)->huge
= shmem_huge
;
3806 struct kobj_attribute shmem_enabled_attr
=
3807 __ATTR(shmem_enabled
, 0644, shmem_enabled_show
, shmem_enabled_store
);
3808 #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE && CONFIG_SYSFS */
3810 #ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE
3811 bool shmem_huge_enabled(struct vm_area_struct
*vma
)
3813 struct inode
*inode
= file_inode(vma
->vm_file
);
3814 struct shmem_sb_info
*sbinfo
= SHMEM_SB(inode
->i_sb
);
3818 if (shmem_huge
== SHMEM_HUGE_FORCE
)
3820 if (shmem_huge
== SHMEM_HUGE_DENY
)
3822 switch (sbinfo
->huge
) {
3823 case SHMEM_HUGE_NEVER
:
3825 case SHMEM_HUGE_ALWAYS
:
3827 case SHMEM_HUGE_WITHIN_SIZE
:
3828 off
= round_up(vma
->vm_pgoff
, HPAGE_PMD_NR
);
3829 i_size
= round_up(i_size_read(inode
), PAGE_SIZE
);
3830 if (i_size
>= HPAGE_PMD_SIZE
&&
3831 i_size
>> PAGE_SHIFT
>= off
)
3834 case SHMEM_HUGE_ADVISE
:
3835 /* TODO: implement fadvise() hints */
3836 return (vma
->vm_flags
& VM_HUGEPAGE
);
3842 #endif /* CONFIG_TRANSPARENT_HUGE_PAGECACHE */
3844 #else /* !CONFIG_SHMEM */
3847 * tiny-shmem: simple shmemfs and tmpfs using ramfs code
3849 * This is intended for small system where the benefits of the full
3850 * shmem code (swap-backed and resource-limited) are outweighed by
3851 * their complexity. On systems without swap this code should be
3852 * effectively equivalent, but much lighter weight.
3855 static struct file_system_type shmem_fs_type
= {
3857 .mount
= ramfs_mount
,
3858 .kill_sb
= kill_litter_super
,
3859 .fs_flags
= FS_USERNS_MOUNT
,
3862 int __init
shmem_init(void)
3864 BUG_ON(register_filesystem(&shmem_fs_type
) != 0);
3866 shm_mnt
= kern_mount(&shmem_fs_type
);
3867 BUG_ON(IS_ERR(shm_mnt
));
3872 int shmem_unuse(swp_entry_t swap
, struct page
*page
)
3877 int shmem_lock(struct file
*file
, int lock
, struct user_struct
*user
)
3882 void shmem_unlock_mapping(struct address_space
*mapping
)
3887 unsigned long shmem_get_unmapped_area(struct file
*file
,
3888 unsigned long addr
, unsigned long len
,
3889 unsigned long pgoff
, unsigned long flags
)
3891 return current
->mm
->get_unmapped_area(file
, addr
, len
, pgoff
, flags
);
3895 void shmem_truncate_range(struct inode
*inode
, loff_t lstart
, loff_t lend
)
3897 truncate_inode_pages_range(inode
->i_mapping
, lstart
, lend
);
3899 EXPORT_SYMBOL_GPL(shmem_truncate_range
);
3901 #define shmem_vm_ops generic_file_vm_ops
3902 #define shmem_file_operations ramfs_file_operations
3903 #define shmem_get_inode(sb, dir, mode, dev, flags) ramfs_get_inode(sb, dir, mode, dev)
3904 #define shmem_acct_size(flags, size) 0
3905 #define shmem_unacct_size(flags, size) do {} while (0)
3907 #endif /* CONFIG_SHMEM */
3911 static struct file
*__shmem_file_setup(struct vfsmount
*mnt
, const char *name
, loff_t size
,
3912 unsigned long flags
, unsigned int i_flags
)
3914 struct inode
*inode
;
3918 return ERR_CAST(mnt
);
3920 if (size
< 0 || size
> MAX_LFS_FILESIZE
)
3921 return ERR_PTR(-EINVAL
);
3923 if (shmem_acct_size(flags
, size
))
3924 return ERR_PTR(-ENOMEM
);
3926 inode
= shmem_get_inode(mnt
->mnt_sb
, NULL
, S_IFREG
| S_IRWXUGO
, 0,
3928 if (unlikely(!inode
)) {
3929 shmem_unacct_size(flags
, size
);
3930 return ERR_PTR(-ENOSPC
);
3932 inode
->i_flags
|= i_flags
;
3933 inode
->i_size
= size
;
3934 clear_nlink(inode
); /* It is unlinked */
3935 res
= ERR_PTR(ramfs_nommu_expand_for_mapping(inode
, size
));
3937 res
= alloc_file_pseudo(inode
, mnt
, name
, O_RDWR
,
3938 &shmem_file_operations
);
3945 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be
3946 * kernel internal. There will be NO LSM permission checks against the
3947 * underlying inode. So users of this interface must do LSM checks at a
3948 * higher layer. The users are the big_key and shm implementations. LSM
3949 * checks are provided at the key or shm level rather than the inode.
3950 * @name: name for dentry (to be seen in /proc/<pid>/maps
3951 * @size: size to be set for the file
3952 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3954 struct file
*shmem_kernel_file_setup(const char *name
, loff_t size
, unsigned long flags
)
3956 return __shmem_file_setup(shm_mnt
, name
, size
, flags
, S_PRIVATE
);
3960 * shmem_file_setup - get an unlinked file living in tmpfs
3961 * @name: name for dentry (to be seen in /proc/<pid>/maps
3962 * @size: size to be set for the file
3963 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3965 struct file
*shmem_file_setup(const char *name
, loff_t size
, unsigned long flags
)
3967 return __shmem_file_setup(shm_mnt
, name
, size
, flags
, 0);
3969 EXPORT_SYMBOL_GPL(shmem_file_setup
);
3972 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs
3973 * @mnt: the tmpfs mount where the file will be created
3974 * @name: name for dentry (to be seen in /proc/<pid>/maps
3975 * @size: size to be set for the file
3976 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size
3978 struct file
*shmem_file_setup_with_mnt(struct vfsmount
*mnt
, const char *name
,
3979 loff_t size
, unsigned long flags
)
3981 return __shmem_file_setup(mnt
, name
, size
, flags
, 0);
3983 EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt
);
3986 * shmem_zero_setup - setup a shared anonymous mapping
3987 * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
3989 int shmem_zero_setup(struct vm_area_struct
*vma
)
3992 loff_t size
= vma
->vm_end
- vma
->vm_start
;
3995 * Cloning a new file under mmap_sem leads to a lock ordering conflict
3996 * between XFS directory reading and selinux: since this file is only
3997 * accessible to the user through its mapping, use S_PRIVATE flag to
3998 * bypass file security, in the same way as shmem_kernel_file_setup().
4000 file
= shmem_kernel_file_setup("dev/zero", size
, vma
->vm_flags
);
4002 return PTR_ERR(file
);
4006 vma
->vm_file
= file
;
4007 vma
->vm_ops
= &shmem_vm_ops
;
4009 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGE_PAGECACHE
) &&
4010 ((vma
->vm_start
+ ~HPAGE_PMD_MASK
) & HPAGE_PMD_MASK
) <
4011 (vma
->vm_end
& HPAGE_PMD_MASK
)) {
4012 khugepaged_enter(vma
, vma
->vm_flags
);
4017 EXPORT_SYMBOL_GPL(shmem_zero_setup
);
4020 * shmem_read_mapping_page_gfp - read into page cache, using specified page allocation flags.
4021 * @mapping: the page's address_space
4022 * @index: the page index
4023 * @gfp: the page allocator flags to use if allocating
4025 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)",
4026 * with any new page allocations done using the specified allocation flags.
4027 * But read_cache_page_gfp() uses the ->readpage() method: which does not
4028 * suit tmpfs, since it may have pages in swapcache, and needs to find those
4029 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support.
4031 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in
4032 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily.
4034 struct page
*shmem_read_mapping_page_gfp(struct address_space
*mapping
,
4035 pgoff_t index
, gfp_t gfp
)
4038 struct inode
*inode
= mapping
->host
;
4042 BUG_ON(mapping
->a_ops
!= &shmem_aops
);
4043 error
= shmem_getpage_gfp(inode
, index
, &page
, SGP_CACHE
,
4044 gfp
, NULL
, NULL
, NULL
);
4046 page
= ERR_PTR(error
);
4052 * The tiny !SHMEM case uses ramfs without swap
4054 return read_cache_page_gfp(mapping
, index
, gfp
);
4057 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp
);