1 /* 2 * Resizable virtual memory filesystem for Linux. 3 * 4 * Copyright (C) 2000 Linus Torvalds. 5 * 2000 Transmeta Corp. 6 * 2000-2001 Christoph Rohland 7 * 2000-2001 SAP AG 8 * 2002 Red Hat Inc. 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 13 * 14 * Extended attribute support for tmpfs: 15 * Copyright (c) 2004, Luke Kenneth Casson Leighton <lkcl@lkcl.net> 16 * Copyright (c) 2004 Red Hat, Inc., James Morris <jmorris@redhat.com> 17 * 18 * tiny-shmem: 19 * Copyright (c) 2004, 2008 Matt Mackall <mpm@selenic.com> 20 * 21 * This file is released under the GPL. 22 */ 23 24 #include <linux/fs.h> 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> 31 #include <linux/fileattr.h> 32 #include <linux/mm.h> 33 #include <linux/random.h> 34 #include <linux/sched/signal.h> 35 #include <linux/export.h> 36 #include <linux/shmem_fs.h> 37 #include <linux/swap.h> 38 #include <linux/uio.h> 39 #include <linux/hugetlb.h> 40 #include <linux/fs_parser.h> 41 #include <linux/swapfile.h> 42 #include <linux/iversion.h> 43 #include <linux/unicode.h> 44 #include "swap.h" 45 46 static struct vfsmount *shm_mnt __ro_after_init; 47 48 #ifdef CONFIG_SHMEM 49 /* 50 * This virtual memory filesystem is heavily based on the ramfs. It 51 * extends ramfs by the ability to use swap and honor resource limits 52 * which makes it a completely usable filesystem. 53 */ 54 55 #include <linux/xattr.h> 56 #include <linux/exportfs.h> 57 #include <linux/posix_acl.h> 58 #include <linux/posix_acl_xattr.h> 59 #include <linux/mman.h> 60 #include <linux/string.h> 61 #include <linux/slab.h> 62 #include <linux/backing-dev.h> 63 #include <linux/writeback.h> 64 #include <linux/pagevec.h> 65 #include <linux/percpu_counter.h> 66 #include <linux/falloc.h> 67 #include <linux/splice.h> 68 #include <linux/security.h> 69 #include <linux/swapops.h> 70 #include <linux/mempolicy.h> 71 #include <linux/namei.h> 72 #include <linux/ctype.h> 73 #include <linux/migrate.h> 74 #include <linux/highmem.h> 75 #include <linux/seq_file.h> 76 #include <linux/magic.h> 77 #include <linux/syscalls.h> 78 #include <linux/fcntl.h> 79 #include <uapi/linux/memfd.h> 80 #include <linux/rmap.h> 81 #include <linux/uuid.h> 82 #include <linux/quotaops.h> 83 #include <linux/rcupdate_wait.h> 84 85 #include <linux/uaccess.h> 86 87 #include "internal.h" 88 89 #define VM_ACCT(size) (PAGE_ALIGN(size) >> PAGE_SHIFT) 90 91 /* Pretend that each entry is of this size in directory's i_size */ 92 #define BOGO_DIRENT_SIZE 20 93 94 /* Pretend that one inode + its dentry occupy this much memory */ 95 #define BOGO_INODE_SIZE 1024 96 97 /* Symlink up to this size is kmalloc'ed instead of using a swappable page */ 98 #define SHORT_SYMLINK_LEN 128 99 100 /* 101 * shmem_fallocate communicates with shmem_fault or shmem_writeout via 102 * inode->i_private (with i_rwsem making sure that it has only one user at 103 * a time): we would prefer not to enlarge the shmem inode just for that. 104 */ 105 struct shmem_falloc { 106 wait_queue_head_t *waitq; /* faults into hole wait for punch to end */ 107 pgoff_t start; /* start of range currently being fallocated */ 108 pgoff_t next; /* the next page offset to be fallocated */ 109 pgoff_t nr_falloced; /* how many new pages have been fallocated */ 110 pgoff_t nr_unswapped; /* how often writeout refused to swap out */ 111 }; 112 113 struct shmem_options { 114 unsigned long long blocks; 115 unsigned long long inodes; 116 struct mempolicy *mpol; 117 kuid_t uid; 118 kgid_t gid; 119 umode_t mode; 120 bool full_inums; 121 int huge; 122 int seen; 123 bool noswap; 124 unsigned short quota_types; 125 struct shmem_quota_limits qlimits; 126 #if IS_ENABLED(CONFIG_UNICODE) 127 struct unicode_map *encoding; 128 bool strict_encoding; 129 #endif 130 #define SHMEM_SEEN_BLOCKS 1 131 #define SHMEM_SEEN_INODES 2 132 #define SHMEM_SEEN_HUGE 4 133 #define SHMEM_SEEN_INUMS 8 134 #define SHMEM_SEEN_NOSWAP 16 135 #define SHMEM_SEEN_QUOTA 32 136 }; 137 138 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 139 static unsigned long huge_shmem_orders_always __read_mostly; 140 static unsigned long huge_shmem_orders_madvise __read_mostly; 141 static unsigned long huge_shmem_orders_inherit __read_mostly; 142 static unsigned long huge_shmem_orders_within_size __read_mostly; 143 static bool shmem_orders_configured __initdata; 144 #endif 145 146 #ifdef CONFIG_TMPFS 147 static unsigned long shmem_default_max_blocks(void) 148 { 149 return totalram_pages() / 2; 150 } 151 152 static unsigned long shmem_default_max_inodes(void) 153 { 154 unsigned long nr_pages = totalram_pages(); 155 156 return min3(nr_pages - totalhigh_pages(), nr_pages / 2, 157 ULONG_MAX / BOGO_INODE_SIZE); 158 } 159 #endif 160 161 static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 162 struct folio **foliop, enum sgp_type sgp, gfp_t gfp, 163 struct vm_area_struct *vma, vm_fault_t *fault_type); 164 165 static inline struct shmem_sb_info *SHMEM_SB(struct super_block *sb) 166 { 167 return sb->s_fs_info; 168 } 169 170 /* 171 * shmem_file_setup pre-accounts the whole fixed size of a VM object, 172 * for shared memory and for shared anonymous (/dev/zero) mappings 173 * (unless MAP_NORESERVE and sysctl_overcommit_memory <= 1), 174 * consistent with the pre-accounting of private mappings ... 175 */ 176 static inline int shmem_acct_size(unsigned long flags, loff_t size) 177 { 178 return (flags & VM_NORESERVE) ? 179 0 : security_vm_enough_memory_mm(current->mm, VM_ACCT(size)); 180 } 181 182 static inline void shmem_unacct_size(unsigned long flags, loff_t size) 183 { 184 if (!(flags & VM_NORESERVE)) 185 vm_unacct_memory(VM_ACCT(size)); 186 } 187 188 static inline int shmem_reacct_size(unsigned long flags, 189 loff_t oldsize, loff_t newsize) 190 { 191 if (!(flags & VM_NORESERVE)) { 192 if (VM_ACCT(newsize) > VM_ACCT(oldsize)) 193 return security_vm_enough_memory_mm(current->mm, 194 VM_ACCT(newsize) - VM_ACCT(oldsize)); 195 else if (VM_ACCT(newsize) < VM_ACCT(oldsize)) 196 vm_unacct_memory(VM_ACCT(oldsize) - VM_ACCT(newsize)); 197 } 198 return 0; 199 } 200 201 /* 202 * ... whereas tmpfs objects are accounted incrementally as 203 * pages are allocated, in order to allow large sparse files. 204 * shmem_get_folio reports shmem_acct_blocks failure as -ENOSPC not -ENOMEM, 205 * so that a failure on a sparse tmpfs mapping will give SIGBUS not OOM. 206 */ 207 static inline int shmem_acct_blocks(unsigned long flags, long pages) 208 { 209 if (!(flags & VM_NORESERVE)) 210 return 0; 211 212 return security_vm_enough_memory_mm(current->mm, 213 pages * VM_ACCT(PAGE_SIZE)); 214 } 215 216 static inline void shmem_unacct_blocks(unsigned long flags, long pages) 217 { 218 if (flags & VM_NORESERVE) 219 vm_unacct_memory(pages * VM_ACCT(PAGE_SIZE)); 220 } 221 222 static int shmem_inode_acct_blocks(struct inode *inode, long pages) 223 { 224 struct shmem_inode_info *info = SHMEM_I(inode); 225 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 226 int err = -ENOSPC; 227 228 if (shmem_acct_blocks(info->flags, pages)) 229 return err; 230 231 might_sleep(); /* when quotas */ 232 if (sbinfo->max_blocks) { 233 if (!percpu_counter_limited_add(&sbinfo->used_blocks, 234 sbinfo->max_blocks, pages)) 235 goto unacct; 236 237 err = dquot_alloc_block_nodirty(inode, pages); 238 if (err) { 239 percpu_counter_sub(&sbinfo->used_blocks, pages); 240 goto unacct; 241 } 242 } else { 243 err = dquot_alloc_block_nodirty(inode, pages); 244 if (err) 245 goto unacct; 246 } 247 248 return 0; 249 250 unacct: 251 shmem_unacct_blocks(info->flags, pages); 252 return err; 253 } 254 255 static void shmem_inode_unacct_blocks(struct inode *inode, long pages) 256 { 257 struct shmem_inode_info *info = SHMEM_I(inode); 258 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 259 260 might_sleep(); /* when quotas */ 261 dquot_free_block_nodirty(inode, pages); 262 263 if (sbinfo->max_blocks) 264 percpu_counter_sub(&sbinfo->used_blocks, pages); 265 shmem_unacct_blocks(info->flags, pages); 266 } 267 268 static const struct super_operations shmem_ops; 269 static const struct address_space_operations shmem_aops; 270 static const struct file_operations shmem_file_operations; 271 static const struct inode_operations shmem_inode_operations; 272 static const struct inode_operations shmem_dir_inode_operations; 273 static const struct inode_operations shmem_special_inode_operations; 274 static const struct vm_operations_struct shmem_vm_ops; 275 static const struct vm_operations_struct shmem_anon_vm_ops; 276 static struct file_system_type shmem_fs_type; 277 278 bool shmem_mapping(struct address_space *mapping) 279 { 280 return mapping->a_ops == &shmem_aops; 281 } 282 EXPORT_SYMBOL_GPL(shmem_mapping); 283 284 bool vma_is_anon_shmem(struct vm_area_struct *vma) 285 { 286 return vma->vm_ops == &shmem_anon_vm_ops; 287 } 288 289 bool vma_is_shmem(struct vm_area_struct *vma) 290 { 291 return vma_is_anon_shmem(vma) || vma->vm_ops == &shmem_vm_ops; 292 } 293 294 static LIST_HEAD(shmem_swaplist); 295 static DEFINE_MUTEX(shmem_swaplist_mutex); 296 297 #ifdef CONFIG_TMPFS_QUOTA 298 299 static int shmem_enable_quotas(struct super_block *sb, 300 unsigned short quota_types) 301 { 302 int type, err = 0; 303 304 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY; 305 for (type = 0; type < SHMEM_MAXQUOTAS; type++) { 306 if (!(quota_types & (1 << type))) 307 continue; 308 err = dquot_load_quota_sb(sb, type, QFMT_SHMEM, 309 DQUOT_USAGE_ENABLED | 310 DQUOT_LIMITS_ENABLED); 311 if (err) 312 goto out_err; 313 } 314 return 0; 315 316 out_err: 317 pr_warn("tmpfs: failed to enable quota tracking (type=%d, err=%d)\n", 318 type, err); 319 for (type--; type >= 0; type--) 320 dquot_quota_off(sb, type); 321 return err; 322 } 323 324 static void shmem_disable_quotas(struct super_block *sb) 325 { 326 int type; 327 328 for (type = 0; type < SHMEM_MAXQUOTAS; type++) 329 dquot_quota_off(sb, type); 330 } 331 332 static struct dquot __rcu **shmem_get_dquots(struct inode *inode) 333 { 334 return SHMEM_I(inode)->i_dquot; 335 } 336 #endif /* CONFIG_TMPFS_QUOTA */ 337 338 /* 339 * shmem_reserve_inode() performs bookkeeping to reserve a shmem inode, and 340 * produces a novel ino for the newly allocated inode. 341 * 342 * It may also be called when making a hard link to permit the space needed by 343 * each dentry. However, in that case, no new inode number is needed since that 344 * internally draws from another pool of inode numbers (currently global 345 * get_next_ino()). This case is indicated by passing NULL as inop. 346 */ 347 #define SHMEM_INO_BATCH 1024 348 static int shmem_reserve_inode(struct super_block *sb, ino_t *inop) 349 { 350 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 351 ino_t ino; 352 353 if (!(sb->s_flags & SB_KERNMOUNT)) { 354 raw_spin_lock(&sbinfo->stat_lock); 355 if (sbinfo->max_inodes) { 356 if (sbinfo->free_ispace < BOGO_INODE_SIZE) { 357 raw_spin_unlock(&sbinfo->stat_lock); 358 return -ENOSPC; 359 } 360 sbinfo->free_ispace -= BOGO_INODE_SIZE; 361 } 362 if (inop) { 363 ino = sbinfo->next_ino++; 364 if (unlikely(is_zero_ino(ino))) 365 ino = sbinfo->next_ino++; 366 if (unlikely(!sbinfo->full_inums && 367 ino > UINT_MAX)) { 368 /* 369 * Emulate get_next_ino uint wraparound for 370 * compatibility 371 */ 372 if (IS_ENABLED(CONFIG_64BIT)) 373 pr_warn("%s: inode number overflow on device %d, consider using inode64 mount option\n", 374 __func__, MINOR(sb->s_dev)); 375 sbinfo->next_ino = 1; 376 ino = sbinfo->next_ino++; 377 } 378 *inop = ino; 379 } 380 raw_spin_unlock(&sbinfo->stat_lock); 381 } else if (inop) { 382 /* 383 * __shmem_file_setup, one of our callers, is lock-free: it 384 * doesn't hold stat_lock in shmem_reserve_inode since 385 * max_inodes is always 0, and is called from potentially 386 * unknown contexts. As such, use a per-cpu batched allocator 387 * which doesn't require the per-sb stat_lock unless we are at 388 * the batch boundary. 389 * 390 * We don't need to worry about inode{32,64} since SB_KERNMOUNT 391 * shmem mounts are not exposed to userspace, so we don't need 392 * to worry about things like glibc compatibility. 393 */ 394 ino_t *next_ino; 395 396 next_ino = per_cpu_ptr(sbinfo->ino_batch, get_cpu()); 397 ino = *next_ino; 398 if (unlikely(ino % SHMEM_INO_BATCH == 0)) { 399 raw_spin_lock(&sbinfo->stat_lock); 400 ino = sbinfo->next_ino; 401 sbinfo->next_ino += SHMEM_INO_BATCH; 402 raw_spin_unlock(&sbinfo->stat_lock); 403 if (unlikely(is_zero_ino(ino))) 404 ino++; 405 } 406 *inop = ino; 407 *next_ino = ++ino; 408 put_cpu(); 409 } 410 411 return 0; 412 } 413 414 static void shmem_free_inode(struct super_block *sb, size_t freed_ispace) 415 { 416 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 417 if (sbinfo->max_inodes) { 418 raw_spin_lock(&sbinfo->stat_lock); 419 sbinfo->free_ispace += BOGO_INODE_SIZE + freed_ispace; 420 raw_spin_unlock(&sbinfo->stat_lock); 421 } 422 } 423 424 /** 425 * shmem_recalc_inode - recalculate the block usage of an inode 426 * @inode: inode to recalc 427 * @alloced: the change in number of pages allocated to inode 428 * @swapped: the change in number of pages swapped from inode 429 * 430 * We have to calculate the free blocks since the mm can drop 431 * undirtied hole pages behind our back. 432 * 433 * But normally info->alloced == inode->i_mapping->nrpages + info->swapped 434 * So mm freed is info->alloced - (inode->i_mapping->nrpages + info->swapped) 435 */ 436 static void shmem_recalc_inode(struct inode *inode, long alloced, long swapped) 437 { 438 struct shmem_inode_info *info = SHMEM_I(inode); 439 long freed; 440 441 spin_lock(&info->lock); 442 info->alloced += alloced; 443 info->swapped += swapped; 444 freed = info->alloced - info->swapped - 445 READ_ONCE(inode->i_mapping->nrpages); 446 /* 447 * Special case: whereas normally shmem_recalc_inode() is called 448 * after i_mapping->nrpages has already been adjusted (up or down), 449 * shmem_writeout() has to raise swapped before nrpages is lowered - 450 * to stop a racing shmem_recalc_inode() from thinking that a page has 451 * been freed. Compensate here, to avoid the need for a followup call. 452 */ 453 if (swapped > 0) 454 freed += swapped; 455 if (freed > 0) 456 info->alloced -= freed; 457 spin_unlock(&info->lock); 458 459 /* The quota case may block */ 460 if (freed > 0) 461 shmem_inode_unacct_blocks(inode, freed); 462 } 463 464 bool shmem_charge(struct inode *inode, long pages) 465 { 466 struct address_space *mapping = inode->i_mapping; 467 468 if (shmem_inode_acct_blocks(inode, pages)) 469 return false; 470 471 /* nrpages adjustment first, then shmem_recalc_inode() when balanced */ 472 xa_lock_irq(&mapping->i_pages); 473 mapping->nrpages += pages; 474 xa_unlock_irq(&mapping->i_pages); 475 476 shmem_recalc_inode(inode, pages, 0); 477 return true; 478 } 479 480 void shmem_uncharge(struct inode *inode, long pages) 481 { 482 /* pages argument is currently unused: keep it to help debugging */ 483 /* nrpages adjustment done by __filemap_remove_folio() or caller */ 484 485 shmem_recalc_inode(inode, 0, 0); 486 } 487 488 /* 489 * Replace item expected in xarray by a new item, while holding xa_lock. 490 */ 491 static int shmem_replace_entry(struct address_space *mapping, 492 pgoff_t index, void *expected, void *replacement) 493 { 494 XA_STATE(xas, &mapping->i_pages, index); 495 void *item; 496 497 VM_BUG_ON(!expected); 498 VM_BUG_ON(!replacement); 499 item = xas_load(&xas); 500 if (item != expected) 501 return -ENOENT; 502 xas_store(&xas, replacement); 503 return 0; 504 } 505 506 /* 507 * Sometimes, before we decide whether to proceed or to fail, we must check 508 * that an entry was not already brought back from swap by a racing thread. 509 * 510 * Checking folio is not enough: by the time a swapcache folio is locked, it 511 * might be reused, and again be swapcache, using the same swap as before. 512 */ 513 static bool shmem_confirm_swap(struct address_space *mapping, 514 pgoff_t index, swp_entry_t swap) 515 { 516 return xa_load(&mapping->i_pages, index) == swp_to_radix_entry(swap); 517 } 518 519 /* 520 * Definitions for "huge tmpfs": tmpfs mounted with the huge= option 521 * 522 * SHMEM_HUGE_NEVER: 523 * disables huge pages for the mount; 524 * SHMEM_HUGE_ALWAYS: 525 * enables huge pages for the mount; 526 * SHMEM_HUGE_WITHIN_SIZE: 527 * only allocate huge pages if the page will be fully within i_size, 528 * also respect madvise() hints; 529 * SHMEM_HUGE_ADVISE: 530 * only allocate huge pages if requested with madvise(); 531 */ 532 533 #define SHMEM_HUGE_NEVER 0 534 #define SHMEM_HUGE_ALWAYS 1 535 #define SHMEM_HUGE_WITHIN_SIZE 2 536 #define SHMEM_HUGE_ADVISE 3 537 538 /* 539 * Special values. 540 * Only can be set via /sys/kernel/mm/transparent_hugepage/shmem_enabled: 541 * 542 * SHMEM_HUGE_DENY: 543 * disables huge on shm_mnt and all mounts, for emergency use; 544 * SHMEM_HUGE_FORCE: 545 * enables huge on shm_mnt and all mounts, w/o needing option, for testing; 546 * 547 */ 548 #define SHMEM_HUGE_DENY (-1) 549 #define SHMEM_HUGE_FORCE (-2) 550 551 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 552 /* ifdef here to avoid bloating shmem.o when not necessary */ 553 554 static int shmem_huge __read_mostly = SHMEM_HUGE_NEVER; 555 static int tmpfs_huge __read_mostly = SHMEM_HUGE_NEVER; 556 557 /** 558 * shmem_mapping_size_orders - Get allowable folio orders for the given file size. 559 * @mapping: Target address_space. 560 * @index: The page index. 561 * @write_end: end of a write, could extend inode size. 562 * 563 * This returns huge orders for folios (when supported) based on the file size 564 * which the mapping currently allows at the given index. The index is relevant 565 * due to alignment considerations the mapping might have. The returned order 566 * may be less than the size passed. 567 * 568 * Return: The orders. 569 */ 570 static inline unsigned int 571 shmem_mapping_size_orders(struct address_space *mapping, pgoff_t index, loff_t write_end) 572 { 573 unsigned int order; 574 size_t size; 575 576 if (!mapping_large_folio_support(mapping) || !write_end) 577 return 0; 578 579 /* Calculate the write size based on the write_end */ 580 size = write_end - (index << PAGE_SHIFT); 581 order = filemap_get_order(size); 582 if (!order) 583 return 0; 584 585 /* If we're not aligned, allocate a smaller folio */ 586 if (index & ((1UL << order) - 1)) 587 order = __ffs(index); 588 589 order = min_t(size_t, order, MAX_PAGECACHE_ORDER); 590 return order > 0 ? BIT(order + 1) - 1 : 0; 591 } 592 593 static unsigned int shmem_get_orders_within_size(struct inode *inode, 594 unsigned long within_size_orders, pgoff_t index, 595 loff_t write_end) 596 { 597 pgoff_t aligned_index; 598 unsigned long order; 599 loff_t i_size; 600 601 order = highest_order(within_size_orders); 602 while (within_size_orders) { 603 aligned_index = round_up(index + 1, 1 << order); 604 i_size = max(write_end, i_size_read(inode)); 605 i_size = round_up(i_size, PAGE_SIZE); 606 if (i_size >> PAGE_SHIFT >= aligned_index) 607 return within_size_orders; 608 609 order = next_order(&within_size_orders, order); 610 } 611 612 return 0; 613 } 614 615 static unsigned int shmem_huge_global_enabled(struct inode *inode, pgoff_t index, 616 loff_t write_end, bool shmem_huge_force, 617 struct vm_area_struct *vma, 618 unsigned long vm_flags) 619 { 620 unsigned int maybe_pmd_order = HPAGE_PMD_ORDER > MAX_PAGECACHE_ORDER ? 621 0 : BIT(HPAGE_PMD_ORDER); 622 unsigned long within_size_orders; 623 624 if (!S_ISREG(inode->i_mode)) 625 return 0; 626 if (shmem_huge == SHMEM_HUGE_DENY) 627 return 0; 628 if (shmem_huge_force || shmem_huge == SHMEM_HUGE_FORCE) 629 return maybe_pmd_order; 630 631 /* 632 * The huge order allocation for anon shmem is controlled through 633 * the mTHP interface, so we still use PMD-sized huge order to 634 * check whether global control is enabled. 635 * 636 * For tmpfs mmap()'s huge order, we still use PMD-sized order to 637 * allocate huge pages due to lack of a write size hint. 638 * 639 * Otherwise, tmpfs will allow getting a highest order hint based on 640 * the size of write and fallocate paths, then will try each allowable 641 * huge orders. 642 */ 643 switch (SHMEM_SB(inode->i_sb)->huge) { 644 case SHMEM_HUGE_ALWAYS: 645 if (vma) 646 return maybe_pmd_order; 647 648 return shmem_mapping_size_orders(inode->i_mapping, index, write_end); 649 case SHMEM_HUGE_WITHIN_SIZE: 650 if (vma) 651 within_size_orders = maybe_pmd_order; 652 else 653 within_size_orders = shmem_mapping_size_orders(inode->i_mapping, 654 index, write_end); 655 656 within_size_orders = shmem_get_orders_within_size(inode, within_size_orders, 657 index, write_end); 658 if (within_size_orders > 0) 659 return within_size_orders; 660 661 fallthrough; 662 case SHMEM_HUGE_ADVISE: 663 if (vm_flags & VM_HUGEPAGE) 664 return maybe_pmd_order; 665 fallthrough; 666 default: 667 return 0; 668 } 669 } 670 671 static int shmem_parse_huge(const char *str) 672 { 673 int huge; 674 675 if (!str) 676 return -EINVAL; 677 678 if (!strcmp(str, "never")) 679 huge = SHMEM_HUGE_NEVER; 680 else if (!strcmp(str, "always")) 681 huge = SHMEM_HUGE_ALWAYS; 682 else if (!strcmp(str, "within_size")) 683 huge = SHMEM_HUGE_WITHIN_SIZE; 684 else if (!strcmp(str, "advise")) 685 huge = SHMEM_HUGE_ADVISE; 686 else if (!strcmp(str, "deny")) 687 huge = SHMEM_HUGE_DENY; 688 else if (!strcmp(str, "force")) 689 huge = SHMEM_HUGE_FORCE; 690 else 691 return -EINVAL; 692 693 if (!has_transparent_hugepage() && 694 huge != SHMEM_HUGE_NEVER && huge != SHMEM_HUGE_DENY) 695 return -EINVAL; 696 697 /* Do not override huge allocation policy with non-PMD sized mTHP */ 698 if (huge == SHMEM_HUGE_FORCE && 699 huge_shmem_orders_inherit != BIT(HPAGE_PMD_ORDER)) 700 return -EINVAL; 701 702 return huge; 703 } 704 705 #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) 706 static const char *shmem_format_huge(int huge) 707 { 708 switch (huge) { 709 case SHMEM_HUGE_NEVER: 710 return "never"; 711 case SHMEM_HUGE_ALWAYS: 712 return "always"; 713 case SHMEM_HUGE_WITHIN_SIZE: 714 return "within_size"; 715 case SHMEM_HUGE_ADVISE: 716 return "advise"; 717 case SHMEM_HUGE_DENY: 718 return "deny"; 719 case SHMEM_HUGE_FORCE: 720 return "force"; 721 default: 722 VM_BUG_ON(1); 723 return "bad_val"; 724 } 725 } 726 #endif 727 728 static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 729 struct shrink_control *sc, unsigned long nr_to_free) 730 { 731 LIST_HEAD(list), *pos, *next; 732 struct inode *inode; 733 struct shmem_inode_info *info; 734 struct folio *folio; 735 unsigned long batch = sc ? sc->nr_to_scan : 128; 736 unsigned long split = 0, freed = 0; 737 738 if (list_empty(&sbinfo->shrinklist)) 739 return SHRINK_STOP; 740 741 spin_lock(&sbinfo->shrinklist_lock); 742 list_for_each_safe(pos, next, &sbinfo->shrinklist) { 743 info = list_entry(pos, struct shmem_inode_info, shrinklist); 744 745 /* pin the inode */ 746 inode = igrab(&info->vfs_inode); 747 748 /* inode is about to be evicted */ 749 if (!inode) { 750 list_del_init(&info->shrinklist); 751 goto next; 752 } 753 754 list_move(&info->shrinklist, &list); 755 next: 756 sbinfo->shrinklist_len--; 757 if (!--batch) 758 break; 759 } 760 spin_unlock(&sbinfo->shrinklist_lock); 761 762 list_for_each_safe(pos, next, &list) { 763 pgoff_t next, end; 764 loff_t i_size; 765 int ret; 766 767 info = list_entry(pos, struct shmem_inode_info, shrinklist); 768 inode = &info->vfs_inode; 769 770 if (nr_to_free && freed >= nr_to_free) 771 goto move_back; 772 773 i_size = i_size_read(inode); 774 folio = filemap_get_entry(inode->i_mapping, i_size / PAGE_SIZE); 775 if (!folio || xa_is_value(folio)) 776 goto drop; 777 778 /* No large folio at the end of the file: nothing to split */ 779 if (!folio_test_large(folio)) { 780 folio_put(folio); 781 goto drop; 782 } 783 784 /* Check if there is anything to gain from splitting */ 785 next = folio_next_index(folio); 786 end = shmem_fallocend(inode, DIV_ROUND_UP(i_size, PAGE_SIZE)); 787 if (end <= folio->index || end >= next) { 788 folio_put(folio); 789 goto drop; 790 } 791 792 /* 793 * Move the inode on the list back to shrinklist if we failed 794 * to lock the page at this time. 795 * 796 * Waiting for the lock may lead to deadlock in the 797 * reclaim path. 798 */ 799 if (!folio_trylock(folio)) { 800 folio_put(folio); 801 goto move_back; 802 } 803 804 ret = split_folio(folio); 805 folio_unlock(folio); 806 folio_put(folio); 807 808 /* If split failed move the inode on the list back to shrinklist */ 809 if (ret) 810 goto move_back; 811 812 freed += next - end; 813 split++; 814 drop: 815 list_del_init(&info->shrinklist); 816 goto put; 817 move_back: 818 /* 819 * Make sure the inode is either on the global list or deleted 820 * from any local list before iput() since it could be deleted 821 * in another thread once we put the inode (then the local list 822 * is corrupted). 823 */ 824 spin_lock(&sbinfo->shrinklist_lock); 825 list_move(&info->shrinklist, &sbinfo->shrinklist); 826 sbinfo->shrinklist_len++; 827 spin_unlock(&sbinfo->shrinklist_lock); 828 put: 829 iput(inode); 830 } 831 832 return split; 833 } 834 835 static long shmem_unused_huge_scan(struct super_block *sb, 836 struct shrink_control *sc) 837 { 838 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 839 840 if (!READ_ONCE(sbinfo->shrinklist_len)) 841 return SHRINK_STOP; 842 843 return shmem_unused_huge_shrink(sbinfo, sc, 0); 844 } 845 846 static long shmem_unused_huge_count(struct super_block *sb, 847 struct shrink_control *sc) 848 { 849 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 850 return READ_ONCE(sbinfo->shrinklist_len); 851 } 852 #else /* !CONFIG_TRANSPARENT_HUGEPAGE */ 853 854 #define shmem_huge SHMEM_HUGE_DENY 855 856 static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, 857 struct shrink_control *sc, unsigned long nr_to_free) 858 { 859 return 0; 860 } 861 862 static unsigned int shmem_huge_global_enabled(struct inode *inode, pgoff_t index, 863 loff_t write_end, bool shmem_huge_force, 864 struct vm_area_struct *vma, 865 unsigned long vm_flags) 866 { 867 return 0; 868 } 869 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 870 871 static void shmem_update_stats(struct folio *folio, int nr_pages) 872 { 873 if (folio_test_pmd_mappable(folio)) 874 __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, nr_pages); 875 __lruvec_stat_mod_folio(folio, NR_FILE_PAGES, nr_pages); 876 __lruvec_stat_mod_folio(folio, NR_SHMEM, nr_pages); 877 } 878 879 /* 880 * Somewhat like filemap_add_folio, but error if expected item has gone. 881 */ 882 static int shmem_add_to_page_cache(struct folio *folio, 883 struct address_space *mapping, 884 pgoff_t index, void *expected, gfp_t gfp) 885 { 886 XA_STATE_ORDER(xas, &mapping->i_pages, index, folio_order(folio)); 887 long nr = folio_nr_pages(folio); 888 889 VM_BUG_ON_FOLIO(index != round_down(index, nr), folio); 890 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 891 VM_BUG_ON_FOLIO(!folio_test_swapbacked(folio), folio); 892 893 folio_ref_add(folio, nr); 894 folio->mapping = mapping; 895 folio->index = index; 896 897 gfp &= GFP_RECLAIM_MASK; 898 folio_throttle_swaprate(folio, gfp); 899 900 do { 901 xas_lock_irq(&xas); 902 if (expected != xas_find_conflict(&xas)) { 903 xas_set_err(&xas, -EEXIST); 904 goto unlock; 905 } 906 if (expected && xas_find_conflict(&xas)) { 907 xas_set_err(&xas, -EEXIST); 908 goto unlock; 909 } 910 xas_store(&xas, folio); 911 if (xas_error(&xas)) 912 goto unlock; 913 shmem_update_stats(folio, nr); 914 mapping->nrpages += nr; 915 unlock: 916 xas_unlock_irq(&xas); 917 } while (xas_nomem(&xas, gfp)); 918 919 if (xas_error(&xas)) { 920 folio->mapping = NULL; 921 folio_ref_sub(folio, nr); 922 return xas_error(&xas); 923 } 924 925 return 0; 926 } 927 928 /* 929 * Somewhat like filemap_remove_folio, but substitutes swap for @folio. 930 */ 931 static void shmem_delete_from_page_cache(struct folio *folio, void *radswap) 932 { 933 struct address_space *mapping = folio->mapping; 934 long nr = folio_nr_pages(folio); 935 int error; 936 937 xa_lock_irq(&mapping->i_pages); 938 error = shmem_replace_entry(mapping, folio->index, folio, radswap); 939 folio->mapping = NULL; 940 mapping->nrpages -= nr; 941 shmem_update_stats(folio, -nr); 942 xa_unlock_irq(&mapping->i_pages); 943 folio_put_refs(folio, nr); 944 BUG_ON(error); 945 } 946 947 /* 948 * Remove swap entry from page cache, free the swap and its page cache. Returns 949 * the number of pages being freed. 0 means entry not found in XArray (0 pages 950 * being freed). 951 */ 952 static long shmem_free_swap(struct address_space *mapping, 953 pgoff_t index, void *radswap) 954 { 955 int order = xa_get_order(&mapping->i_pages, index); 956 void *old; 957 958 old = xa_cmpxchg_irq(&mapping->i_pages, index, radswap, NULL, 0); 959 if (old != radswap) 960 return 0; 961 free_swap_and_cache_nr(radix_to_swp_entry(radswap), 1 << order); 962 963 return 1 << order; 964 } 965 966 /* 967 * Determine (in bytes) how many of the shmem object's pages mapped by the 968 * given offsets are swapped out. 969 * 970 * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 971 * as long as the inode doesn't go away and racy results are not a problem. 972 */ 973 unsigned long shmem_partial_swap_usage(struct address_space *mapping, 974 pgoff_t start, pgoff_t end) 975 { 976 XA_STATE(xas, &mapping->i_pages, start); 977 struct page *page; 978 unsigned long swapped = 0; 979 unsigned long max = end - 1; 980 981 rcu_read_lock(); 982 xas_for_each(&xas, page, max) { 983 if (xas_retry(&xas, page)) 984 continue; 985 if (xa_is_value(page)) 986 swapped += 1 << xas_get_order(&xas); 987 if (xas.xa_index == max) 988 break; 989 if (need_resched()) { 990 xas_pause(&xas); 991 cond_resched_rcu(); 992 } 993 } 994 rcu_read_unlock(); 995 996 return swapped << PAGE_SHIFT; 997 } 998 999 /* 1000 * Determine (in bytes) how many of the shmem object's pages mapped by the 1001 * given vma is swapped out. 1002 * 1003 * This is safe to call without i_rwsem or the i_pages lock thanks to RCU, 1004 * as long as the inode doesn't go away and racy results are not a problem. 1005 */ 1006 unsigned long shmem_swap_usage(struct vm_area_struct *vma) 1007 { 1008 struct inode *inode = file_inode(vma->vm_file); 1009 struct shmem_inode_info *info = SHMEM_I(inode); 1010 struct address_space *mapping = inode->i_mapping; 1011 unsigned long swapped; 1012 1013 /* Be careful as we don't hold info->lock */ 1014 swapped = READ_ONCE(info->swapped); 1015 1016 /* 1017 * The easier cases are when the shmem object has nothing in swap, or 1018 * the vma maps it whole. Then we can simply use the stats that we 1019 * already track. 1020 */ 1021 if (!swapped) 1022 return 0; 1023 1024 if (!vma->vm_pgoff && vma->vm_end - vma->vm_start >= inode->i_size) 1025 return swapped << PAGE_SHIFT; 1026 1027 /* Here comes the more involved part */ 1028 return shmem_partial_swap_usage(mapping, vma->vm_pgoff, 1029 vma->vm_pgoff + vma_pages(vma)); 1030 } 1031 1032 /* 1033 * SysV IPC SHM_UNLOCK restore Unevictable pages to their evictable lists. 1034 */ 1035 void shmem_unlock_mapping(struct address_space *mapping) 1036 { 1037 struct folio_batch fbatch; 1038 pgoff_t index = 0; 1039 1040 folio_batch_init(&fbatch); 1041 /* 1042 * Minor point, but we might as well stop if someone else SHM_LOCKs it. 1043 */ 1044 while (!mapping_unevictable(mapping) && 1045 filemap_get_folios(mapping, &index, ~0UL, &fbatch)) { 1046 check_move_unevictable_folios(&fbatch); 1047 folio_batch_release(&fbatch); 1048 cond_resched(); 1049 } 1050 } 1051 1052 static struct folio *shmem_get_partial_folio(struct inode *inode, pgoff_t index) 1053 { 1054 struct folio *folio; 1055 1056 /* 1057 * At first avoid shmem_get_folio(,,,SGP_READ): that fails 1058 * beyond i_size, and reports fallocated folios as holes. 1059 */ 1060 folio = filemap_get_entry(inode->i_mapping, index); 1061 if (!folio) 1062 return folio; 1063 if (!xa_is_value(folio)) { 1064 folio_lock(folio); 1065 if (folio->mapping == inode->i_mapping) 1066 return folio; 1067 /* The folio has been swapped out */ 1068 folio_unlock(folio); 1069 folio_put(folio); 1070 } 1071 /* 1072 * But read a folio back from swap if any of it is within i_size 1073 * (although in some cases this is just a waste of time). 1074 */ 1075 folio = NULL; 1076 shmem_get_folio(inode, index, 0, &folio, SGP_READ); 1077 return folio; 1078 } 1079 1080 /* 1081 * Remove range of pages and swap entries from page cache, and free them. 1082 * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. 1083 */ 1084 static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend, 1085 bool unfalloc) 1086 { 1087 struct address_space *mapping = inode->i_mapping; 1088 struct shmem_inode_info *info = SHMEM_I(inode); 1089 pgoff_t start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 1090 pgoff_t end = (lend + 1) >> PAGE_SHIFT; 1091 struct folio_batch fbatch; 1092 pgoff_t indices[PAGEVEC_SIZE]; 1093 struct folio *folio; 1094 bool same_folio; 1095 long nr_swaps_freed = 0; 1096 pgoff_t index; 1097 int i; 1098 1099 if (lend == -1) 1100 end = -1; /* unsigned, so actually very big */ 1101 1102 if (info->fallocend > start && info->fallocend <= end && !unfalloc) 1103 info->fallocend = start; 1104 1105 folio_batch_init(&fbatch); 1106 index = start; 1107 while (index < end && find_lock_entries(mapping, &index, end - 1, 1108 &fbatch, indices)) { 1109 for (i = 0; i < folio_batch_count(&fbatch); i++) { 1110 folio = fbatch.folios[i]; 1111 1112 if (xa_is_value(folio)) { 1113 if (unfalloc) 1114 continue; 1115 nr_swaps_freed += shmem_free_swap(mapping, 1116 indices[i], folio); 1117 continue; 1118 } 1119 1120 if (!unfalloc || !folio_test_uptodate(folio)) 1121 truncate_inode_folio(mapping, folio); 1122 folio_unlock(folio); 1123 } 1124 folio_batch_remove_exceptionals(&fbatch); 1125 folio_batch_release(&fbatch); 1126 cond_resched(); 1127 } 1128 1129 /* 1130 * When undoing a failed fallocate, we want none of the partial folio 1131 * zeroing and splitting below, but shall want to truncate the whole 1132 * folio when !uptodate indicates that it was added by this fallocate, 1133 * even when [lstart, lend] covers only a part of the folio. 1134 */ 1135 if (unfalloc) 1136 goto whole_folios; 1137 1138 same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 1139 folio = shmem_get_partial_folio(inode, lstart >> PAGE_SHIFT); 1140 if (folio) { 1141 same_folio = lend < folio_pos(folio) + folio_size(folio); 1142 folio_mark_dirty(folio); 1143 if (!truncate_inode_partial_folio(folio, lstart, lend)) { 1144 start = folio_next_index(folio); 1145 if (same_folio) 1146 end = folio->index; 1147 } 1148 folio_unlock(folio); 1149 folio_put(folio); 1150 folio = NULL; 1151 } 1152 1153 if (!same_folio) 1154 folio = shmem_get_partial_folio(inode, lend >> PAGE_SHIFT); 1155 if (folio) { 1156 folio_mark_dirty(folio); 1157 if (!truncate_inode_partial_folio(folio, lstart, lend)) 1158 end = folio->index; 1159 folio_unlock(folio); 1160 folio_put(folio); 1161 } 1162 1163 whole_folios: 1164 1165 index = start; 1166 while (index < end) { 1167 cond_resched(); 1168 1169 if (!find_get_entries(mapping, &index, end - 1, &fbatch, 1170 indices)) { 1171 /* If all gone or hole-punch or unfalloc, we're done */ 1172 if (index == start || end != -1) 1173 break; 1174 /* But if truncating, restart to make sure all gone */ 1175 index = start; 1176 continue; 1177 } 1178 for (i = 0; i < folio_batch_count(&fbatch); i++) { 1179 folio = fbatch.folios[i]; 1180 1181 if (xa_is_value(folio)) { 1182 long swaps_freed; 1183 1184 if (unfalloc) 1185 continue; 1186 swaps_freed = shmem_free_swap(mapping, indices[i], folio); 1187 if (!swaps_freed) { 1188 /* Swap was replaced by page: retry */ 1189 index = indices[i]; 1190 break; 1191 } 1192 nr_swaps_freed += swaps_freed; 1193 continue; 1194 } 1195 1196 folio_lock(folio); 1197 1198 if (!unfalloc || !folio_test_uptodate(folio)) { 1199 if (folio_mapping(folio) != mapping) { 1200 /* Page was replaced by swap: retry */ 1201 folio_unlock(folio); 1202 index = indices[i]; 1203 break; 1204 } 1205 VM_BUG_ON_FOLIO(folio_test_writeback(folio), 1206 folio); 1207 1208 if (!folio_test_large(folio)) { 1209 truncate_inode_folio(mapping, folio); 1210 } else if (truncate_inode_partial_folio(folio, lstart, lend)) { 1211 /* 1212 * If we split a page, reset the loop so 1213 * that we pick up the new sub pages. 1214 * Otherwise the THP was entirely 1215 * dropped or the target range was 1216 * zeroed, so just continue the loop as 1217 * is. 1218 */ 1219 if (!folio_test_large(folio)) { 1220 folio_unlock(folio); 1221 index = start; 1222 break; 1223 } 1224 } 1225 } 1226 folio_unlock(folio); 1227 } 1228 folio_batch_remove_exceptionals(&fbatch); 1229 folio_batch_release(&fbatch); 1230 } 1231 1232 shmem_recalc_inode(inode, 0, -nr_swaps_freed); 1233 } 1234 1235 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 1236 { 1237 shmem_undo_range(inode, lstart, lend, false); 1238 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 1239 inode_inc_iversion(inode); 1240 } 1241 EXPORT_SYMBOL_GPL(shmem_truncate_range); 1242 1243 static int shmem_getattr(struct mnt_idmap *idmap, 1244 const struct path *path, struct kstat *stat, 1245 u32 request_mask, unsigned int query_flags) 1246 { 1247 struct inode *inode = path->dentry->d_inode; 1248 struct shmem_inode_info *info = SHMEM_I(inode); 1249 1250 if (info->alloced - info->swapped != inode->i_mapping->nrpages) 1251 shmem_recalc_inode(inode, 0, 0); 1252 1253 if (info->fsflags & FS_APPEND_FL) 1254 stat->attributes |= STATX_ATTR_APPEND; 1255 if (info->fsflags & FS_IMMUTABLE_FL) 1256 stat->attributes |= STATX_ATTR_IMMUTABLE; 1257 if (info->fsflags & FS_NODUMP_FL) 1258 stat->attributes |= STATX_ATTR_NODUMP; 1259 stat->attributes_mask |= (STATX_ATTR_APPEND | 1260 STATX_ATTR_IMMUTABLE | 1261 STATX_ATTR_NODUMP); 1262 generic_fillattr(idmap, request_mask, inode, stat); 1263 1264 if (shmem_huge_global_enabled(inode, 0, 0, false, NULL, 0)) 1265 stat->blksize = HPAGE_PMD_SIZE; 1266 1267 if (request_mask & STATX_BTIME) { 1268 stat->result_mask |= STATX_BTIME; 1269 stat->btime.tv_sec = info->i_crtime.tv_sec; 1270 stat->btime.tv_nsec = info->i_crtime.tv_nsec; 1271 } 1272 1273 return 0; 1274 } 1275 1276 static int shmem_setattr(struct mnt_idmap *idmap, 1277 struct dentry *dentry, struct iattr *attr) 1278 { 1279 struct inode *inode = d_inode(dentry); 1280 struct shmem_inode_info *info = SHMEM_I(inode); 1281 int error; 1282 bool update_mtime = false; 1283 bool update_ctime = true; 1284 1285 error = setattr_prepare(idmap, dentry, attr); 1286 if (error) 1287 return error; 1288 1289 if ((info->seals & F_SEAL_EXEC) && (attr->ia_valid & ATTR_MODE)) { 1290 if ((inode->i_mode ^ attr->ia_mode) & 0111) { 1291 return -EPERM; 1292 } 1293 } 1294 1295 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) { 1296 loff_t oldsize = inode->i_size; 1297 loff_t newsize = attr->ia_size; 1298 1299 /* protected by i_rwsem */ 1300 if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) || 1301 (newsize > oldsize && (info->seals & F_SEAL_GROW))) 1302 return -EPERM; 1303 1304 if (newsize != oldsize) { 1305 error = shmem_reacct_size(SHMEM_I(inode)->flags, 1306 oldsize, newsize); 1307 if (error) 1308 return error; 1309 i_size_write(inode, newsize); 1310 update_mtime = true; 1311 } else { 1312 update_ctime = false; 1313 } 1314 if (newsize <= oldsize) { 1315 loff_t holebegin = round_up(newsize, PAGE_SIZE); 1316 if (oldsize > holebegin) 1317 unmap_mapping_range(inode->i_mapping, 1318 holebegin, 0, 1); 1319 if (info->alloced) 1320 shmem_truncate_range(inode, 1321 newsize, (loff_t)-1); 1322 /* unmap again to remove racily COWed private pages */ 1323 if (oldsize > holebegin) 1324 unmap_mapping_range(inode->i_mapping, 1325 holebegin, 0, 1); 1326 } 1327 } 1328 1329 if (is_quota_modification(idmap, inode, attr)) { 1330 error = dquot_initialize(inode); 1331 if (error) 1332 return error; 1333 } 1334 1335 /* Transfer quota accounting */ 1336 if (i_uid_needs_update(idmap, attr, inode) || 1337 i_gid_needs_update(idmap, attr, inode)) { 1338 error = dquot_transfer(idmap, inode, attr); 1339 if (error) 1340 return error; 1341 } 1342 1343 setattr_copy(idmap, inode, attr); 1344 if (attr->ia_valid & ATTR_MODE) 1345 error = posix_acl_chmod(idmap, dentry, inode->i_mode); 1346 if (!error && update_ctime) { 1347 inode_set_ctime_current(inode); 1348 if (update_mtime) 1349 inode_set_mtime_to_ts(inode, inode_get_ctime(inode)); 1350 inode_inc_iversion(inode); 1351 } 1352 return error; 1353 } 1354 1355 static void shmem_evict_inode(struct inode *inode) 1356 { 1357 struct shmem_inode_info *info = SHMEM_I(inode); 1358 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1359 size_t freed = 0; 1360 1361 if (shmem_mapping(inode->i_mapping)) { 1362 shmem_unacct_size(info->flags, inode->i_size); 1363 inode->i_size = 0; 1364 mapping_set_exiting(inode->i_mapping); 1365 shmem_truncate_range(inode, 0, (loff_t)-1); 1366 if (!list_empty(&info->shrinklist)) { 1367 spin_lock(&sbinfo->shrinklist_lock); 1368 if (!list_empty(&info->shrinklist)) { 1369 list_del_init(&info->shrinklist); 1370 sbinfo->shrinklist_len--; 1371 } 1372 spin_unlock(&sbinfo->shrinklist_lock); 1373 } 1374 while (!list_empty(&info->swaplist)) { 1375 /* Wait while shmem_unuse() is scanning this inode... */ 1376 wait_var_event(&info->stop_eviction, 1377 !atomic_read(&info->stop_eviction)); 1378 mutex_lock(&shmem_swaplist_mutex); 1379 /* ...but beware of the race if we peeked too early */ 1380 if (!atomic_read(&info->stop_eviction)) 1381 list_del_init(&info->swaplist); 1382 mutex_unlock(&shmem_swaplist_mutex); 1383 } 1384 } 1385 1386 simple_xattrs_free(&info->xattrs, sbinfo->max_inodes ? &freed : NULL); 1387 shmem_free_inode(inode->i_sb, freed); 1388 WARN_ON(inode->i_blocks); 1389 clear_inode(inode); 1390 #ifdef CONFIG_TMPFS_QUOTA 1391 dquot_free_inode(inode); 1392 dquot_drop(inode); 1393 #endif 1394 } 1395 1396 static unsigned int shmem_find_swap_entries(struct address_space *mapping, 1397 pgoff_t start, struct folio_batch *fbatch, 1398 pgoff_t *indices, unsigned int type) 1399 { 1400 XA_STATE(xas, &mapping->i_pages, start); 1401 struct folio *folio; 1402 swp_entry_t entry; 1403 1404 rcu_read_lock(); 1405 xas_for_each(&xas, folio, ULONG_MAX) { 1406 if (xas_retry(&xas, folio)) 1407 continue; 1408 1409 if (!xa_is_value(folio)) 1410 continue; 1411 1412 entry = radix_to_swp_entry(folio); 1413 /* 1414 * swapin error entries can be found in the mapping. But they're 1415 * deliberately ignored here as we've done everything we can do. 1416 */ 1417 if (swp_type(entry) != type) 1418 continue; 1419 1420 indices[folio_batch_count(fbatch)] = xas.xa_index; 1421 if (!folio_batch_add(fbatch, folio)) 1422 break; 1423 1424 if (need_resched()) { 1425 xas_pause(&xas); 1426 cond_resched_rcu(); 1427 } 1428 } 1429 rcu_read_unlock(); 1430 1431 return folio_batch_count(fbatch); 1432 } 1433 1434 /* 1435 * Move the swapped pages for an inode to page cache. Returns the count 1436 * of pages swapped in, or the error in case of failure. 1437 */ 1438 static int shmem_unuse_swap_entries(struct inode *inode, 1439 struct folio_batch *fbatch, pgoff_t *indices) 1440 { 1441 int i = 0; 1442 int ret = 0; 1443 int error = 0; 1444 struct address_space *mapping = inode->i_mapping; 1445 1446 for (i = 0; i < folio_batch_count(fbatch); i++) { 1447 struct folio *folio = fbatch->folios[i]; 1448 1449 error = shmem_swapin_folio(inode, indices[i], &folio, SGP_CACHE, 1450 mapping_gfp_mask(mapping), NULL, NULL); 1451 if (error == 0) { 1452 folio_unlock(folio); 1453 folio_put(folio); 1454 ret++; 1455 } 1456 if (error == -ENOMEM) 1457 break; 1458 error = 0; 1459 } 1460 return error ? error : ret; 1461 } 1462 1463 /* 1464 * If swap found in inode, free it and move page from swapcache to filecache. 1465 */ 1466 static int shmem_unuse_inode(struct inode *inode, unsigned int type) 1467 { 1468 struct address_space *mapping = inode->i_mapping; 1469 pgoff_t start = 0; 1470 struct folio_batch fbatch; 1471 pgoff_t indices[PAGEVEC_SIZE]; 1472 int ret = 0; 1473 1474 do { 1475 folio_batch_init(&fbatch); 1476 if (!shmem_find_swap_entries(mapping, start, &fbatch, 1477 indices, type)) { 1478 ret = 0; 1479 break; 1480 } 1481 1482 ret = shmem_unuse_swap_entries(inode, &fbatch, indices); 1483 if (ret < 0) 1484 break; 1485 1486 start = indices[folio_batch_count(&fbatch) - 1]; 1487 } while (true); 1488 1489 return ret; 1490 } 1491 1492 /* 1493 * Read all the shared memory data that resides in the swap 1494 * device 'type' back into memory, so the swap device can be 1495 * unused. 1496 */ 1497 int shmem_unuse(unsigned int type) 1498 { 1499 struct shmem_inode_info *info, *next; 1500 int error = 0; 1501 1502 if (list_empty(&shmem_swaplist)) 1503 return 0; 1504 1505 mutex_lock(&shmem_swaplist_mutex); 1506 start_over: 1507 list_for_each_entry_safe(info, next, &shmem_swaplist, swaplist) { 1508 if (!info->swapped) { 1509 list_del_init(&info->swaplist); 1510 continue; 1511 } 1512 /* 1513 * Drop the swaplist mutex while searching the inode for swap; 1514 * but before doing so, make sure shmem_evict_inode() will not 1515 * remove placeholder inode from swaplist, nor let it be freed 1516 * (igrab() would protect from unlink, but not from unmount). 1517 */ 1518 atomic_inc(&info->stop_eviction); 1519 mutex_unlock(&shmem_swaplist_mutex); 1520 1521 error = shmem_unuse_inode(&info->vfs_inode, type); 1522 cond_resched(); 1523 1524 mutex_lock(&shmem_swaplist_mutex); 1525 if (atomic_dec_and_test(&info->stop_eviction)) 1526 wake_up_var(&info->stop_eviction); 1527 if (error) 1528 break; 1529 if (list_empty(&info->swaplist)) 1530 goto start_over; 1531 next = list_next_entry(info, swaplist); 1532 if (!info->swapped) 1533 list_del_init(&info->swaplist); 1534 } 1535 mutex_unlock(&shmem_swaplist_mutex); 1536 1537 return error; 1538 } 1539 1540 /** 1541 * shmem_writeout - Write the folio to swap 1542 * @folio: The folio to write 1543 * @wbc: How writeback is to be done 1544 * 1545 * Move the folio from the page cache to the swap cache. 1546 */ 1547 int shmem_writeout(struct folio *folio, struct writeback_control *wbc) 1548 { 1549 struct address_space *mapping = folio->mapping; 1550 struct inode *inode = mapping->host; 1551 struct shmem_inode_info *info = SHMEM_I(inode); 1552 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1553 pgoff_t index; 1554 int nr_pages; 1555 bool split = false; 1556 1557 if (WARN_ON_ONCE(!wbc->for_reclaim)) 1558 goto redirty; 1559 1560 if ((info->flags & VM_LOCKED) || sbinfo->noswap) 1561 goto redirty; 1562 1563 if (!total_swap_pages) 1564 goto redirty; 1565 1566 /* 1567 * If CONFIG_THP_SWAP is not enabled, the large folio should be 1568 * split when swapping. 1569 * 1570 * And shrinkage of pages beyond i_size does not split swap, so 1571 * swapout of a large folio crossing i_size needs to split too 1572 * (unless fallocate has been used to preallocate beyond EOF). 1573 */ 1574 if (folio_test_large(folio)) { 1575 index = shmem_fallocend(inode, 1576 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE)); 1577 if ((index > folio->index && index < folio_next_index(folio)) || 1578 !IS_ENABLED(CONFIG_THP_SWAP)) 1579 split = true; 1580 } 1581 1582 if (split) { 1583 try_split: 1584 /* Ensure the subpages are still dirty */ 1585 folio_test_set_dirty(folio); 1586 if (split_folio_to_list(folio, wbc->list)) 1587 goto redirty; 1588 folio_clear_dirty(folio); 1589 } 1590 1591 index = folio->index; 1592 nr_pages = folio_nr_pages(folio); 1593 1594 /* 1595 * This is somewhat ridiculous, but without plumbing a SWAP_MAP_FALLOC 1596 * value into swapfile.c, the only way we can correctly account for a 1597 * fallocated folio arriving here is now to initialize it and write it. 1598 * 1599 * That's okay for a folio already fallocated earlier, but if we have 1600 * not yet completed the fallocation, then (a) we want to keep track 1601 * of this folio in case we have to undo it, and (b) it may not be a 1602 * good idea to continue anyway, once we're pushing into swap. So 1603 * reactivate the folio, and let shmem_fallocate() quit when too many. 1604 */ 1605 if (!folio_test_uptodate(folio)) { 1606 if (inode->i_private) { 1607 struct shmem_falloc *shmem_falloc; 1608 spin_lock(&inode->i_lock); 1609 shmem_falloc = inode->i_private; 1610 if (shmem_falloc && 1611 !shmem_falloc->waitq && 1612 index >= shmem_falloc->start && 1613 index < shmem_falloc->next) 1614 shmem_falloc->nr_unswapped += nr_pages; 1615 else 1616 shmem_falloc = NULL; 1617 spin_unlock(&inode->i_lock); 1618 if (shmem_falloc) 1619 goto redirty; 1620 } 1621 folio_zero_range(folio, 0, folio_size(folio)); 1622 flush_dcache_folio(folio); 1623 folio_mark_uptodate(folio); 1624 } 1625 1626 /* 1627 * Add inode to shmem_unuse()'s list of swapped-out inodes, 1628 * if it's not already there. Do it now before the folio is 1629 * moved to swap cache, when its pagelock no longer protects 1630 * the inode from eviction. But don't unlock the mutex until 1631 * we've incremented swapped, because shmem_unuse_inode() will 1632 * prune a !swapped inode from the swaplist under this mutex. 1633 */ 1634 mutex_lock(&shmem_swaplist_mutex); 1635 if (list_empty(&info->swaplist)) 1636 list_add(&info->swaplist, &shmem_swaplist); 1637 1638 if (!folio_alloc_swap(folio, __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN)) { 1639 shmem_recalc_inode(inode, 0, nr_pages); 1640 swap_shmem_alloc(folio->swap, nr_pages); 1641 shmem_delete_from_page_cache(folio, swp_to_radix_entry(folio->swap)); 1642 1643 mutex_unlock(&shmem_swaplist_mutex); 1644 BUG_ON(folio_mapped(folio)); 1645 return swap_writeout(folio, wbc); 1646 } 1647 if (!info->swapped) 1648 list_del_init(&info->swaplist); 1649 mutex_unlock(&shmem_swaplist_mutex); 1650 if (nr_pages > 1) 1651 goto try_split; 1652 redirty: 1653 folio_mark_dirty(folio); 1654 if (wbc->for_reclaim) 1655 return AOP_WRITEPAGE_ACTIVATE; /* Return with folio locked */ 1656 folio_unlock(folio); 1657 return 0; 1658 } 1659 EXPORT_SYMBOL_GPL(shmem_writeout); 1660 1661 #if defined(CONFIG_NUMA) && defined(CONFIG_TMPFS) 1662 static void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1663 { 1664 char buffer[64]; 1665 1666 if (!mpol || mpol->mode == MPOL_DEFAULT) 1667 return; /* show nothing */ 1668 1669 mpol_to_str(buffer, sizeof(buffer), mpol); 1670 1671 seq_printf(seq, ",mpol=%s", buffer); 1672 } 1673 1674 static struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 1675 { 1676 struct mempolicy *mpol = NULL; 1677 if (sbinfo->mpol) { 1678 raw_spin_lock(&sbinfo->stat_lock); /* prevent replace/use races */ 1679 mpol = sbinfo->mpol; 1680 mpol_get(mpol); 1681 raw_spin_unlock(&sbinfo->stat_lock); 1682 } 1683 return mpol; 1684 } 1685 #else /* !CONFIG_NUMA || !CONFIG_TMPFS */ 1686 static inline void shmem_show_mpol(struct seq_file *seq, struct mempolicy *mpol) 1687 { 1688 } 1689 static inline struct mempolicy *shmem_get_sbmpol(struct shmem_sb_info *sbinfo) 1690 { 1691 return NULL; 1692 } 1693 #endif /* CONFIG_NUMA && CONFIG_TMPFS */ 1694 1695 static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *info, 1696 pgoff_t index, unsigned int order, pgoff_t *ilx); 1697 1698 static struct folio *shmem_swapin_cluster(swp_entry_t swap, gfp_t gfp, 1699 struct shmem_inode_info *info, pgoff_t index) 1700 { 1701 struct mempolicy *mpol; 1702 pgoff_t ilx; 1703 struct folio *folio; 1704 1705 mpol = shmem_get_pgoff_policy(info, index, 0, &ilx); 1706 folio = swap_cluster_readahead(swap, gfp, mpol, ilx); 1707 mpol_cond_put(mpol); 1708 1709 return folio; 1710 } 1711 1712 /* 1713 * Make sure huge_gfp is always more limited than limit_gfp. 1714 * Some of the flags set permissions, while others set limitations. 1715 */ 1716 static gfp_t limit_gfp_mask(gfp_t huge_gfp, gfp_t limit_gfp) 1717 { 1718 gfp_t allowflags = __GFP_IO | __GFP_FS | __GFP_RECLAIM; 1719 gfp_t denyflags = __GFP_NOWARN | __GFP_NORETRY; 1720 gfp_t zoneflags = limit_gfp & GFP_ZONEMASK; 1721 gfp_t result = huge_gfp & ~(allowflags | GFP_ZONEMASK); 1722 1723 /* Allow allocations only from the originally specified zones. */ 1724 result |= zoneflags; 1725 1726 /* 1727 * Minimize the result gfp by taking the union with the deny flags, 1728 * and the intersection of the allow flags. 1729 */ 1730 result |= (limit_gfp & denyflags); 1731 result |= (huge_gfp & limit_gfp) & allowflags; 1732 1733 return result; 1734 } 1735 1736 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 1737 bool shmem_hpage_pmd_enabled(void) 1738 { 1739 if (shmem_huge == SHMEM_HUGE_DENY) 1740 return false; 1741 if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_always)) 1742 return true; 1743 if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_madvise)) 1744 return true; 1745 if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_within_size)) 1746 return true; 1747 if (test_bit(HPAGE_PMD_ORDER, &huge_shmem_orders_inherit) && 1748 shmem_huge != SHMEM_HUGE_NEVER) 1749 return true; 1750 1751 return false; 1752 } 1753 1754 unsigned long shmem_allowable_huge_orders(struct inode *inode, 1755 struct vm_area_struct *vma, pgoff_t index, 1756 loff_t write_end, bool shmem_huge_force) 1757 { 1758 unsigned long mask = READ_ONCE(huge_shmem_orders_always); 1759 unsigned long within_size_orders = READ_ONCE(huge_shmem_orders_within_size); 1760 unsigned long vm_flags = vma ? vma->vm_flags : 0; 1761 unsigned int global_orders; 1762 1763 if (thp_disabled_by_hw() || (vma && vma_thp_disabled(vma, vm_flags))) 1764 return 0; 1765 1766 global_orders = shmem_huge_global_enabled(inode, index, write_end, 1767 shmem_huge_force, vma, vm_flags); 1768 /* Tmpfs huge pages allocation */ 1769 if (!vma || !vma_is_anon_shmem(vma)) 1770 return global_orders; 1771 1772 /* 1773 * Following the 'deny' semantics of the top level, force the huge 1774 * option off from all mounts. 1775 */ 1776 if (shmem_huge == SHMEM_HUGE_DENY) 1777 return 0; 1778 1779 /* 1780 * Only allow inherit orders if the top-level value is 'force', which 1781 * means non-PMD sized THP can not override 'huge' mount option now. 1782 */ 1783 if (shmem_huge == SHMEM_HUGE_FORCE) 1784 return READ_ONCE(huge_shmem_orders_inherit); 1785 1786 /* Allow mTHP that will be fully within i_size. */ 1787 mask |= shmem_get_orders_within_size(inode, within_size_orders, index, 0); 1788 1789 if (vm_flags & VM_HUGEPAGE) 1790 mask |= READ_ONCE(huge_shmem_orders_madvise); 1791 1792 if (global_orders > 0) 1793 mask |= READ_ONCE(huge_shmem_orders_inherit); 1794 1795 return THP_ORDERS_ALL_FILE_DEFAULT & mask; 1796 } 1797 1798 static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault *vmf, 1799 struct address_space *mapping, pgoff_t index, 1800 unsigned long orders) 1801 { 1802 struct vm_area_struct *vma = vmf ? vmf->vma : NULL; 1803 pgoff_t aligned_index; 1804 unsigned long pages; 1805 int order; 1806 1807 if (vma) { 1808 orders = thp_vma_suitable_orders(vma, vmf->address, orders); 1809 if (!orders) 1810 return 0; 1811 } 1812 1813 /* Find the highest order that can add into the page cache */ 1814 order = highest_order(orders); 1815 while (orders) { 1816 pages = 1UL << order; 1817 aligned_index = round_down(index, pages); 1818 /* 1819 * Check for conflict before waiting on a huge allocation. 1820 * Conflict might be that a huge page has just been allocated 1821 * and added to page cache by a racing thread, or that there 1822 * is already at least one small page in the huge extent. 1823 * Be careful to retry when appropriate, but not forever! 1824 * Elsewhere -EEXIST would be the right code, but not here. 1825 */ 1826 if (!xa_find(&mapping->i_pages, &aligned_index, 1827 aligned_index + pages - 1, XA_PRESENT)) 1828 break; 1829 order = next_order(&orders, order); 1830 } 1831 1832 return orders; 1833 } 1834 #else 1835 static unsigned long shmem_suitable_orders(struct inode *inode, struct vm_fault *vmf, 1836 struct address_space *mapping, pgoff_t index, 1837 unsigned long orders) 1838 { 1839 return 0; 1840 } 1841 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 1842 1843 static struct folio *shmem_alloc_folio(gfp_t gfp, int order, 1844 struct shmem_inode_info *info, pgoff_t index) 1845 { 1846 struct mempolicy *mpol; 1847 pgoff_t ilx; 1848 struct folio *folio; 1849 1850 mpol = shmem_get_pgoff_policy(info, index, order, &ilx); 1851 folio = folio_alloc_mpol(gfp, order, mpol, ilx, numa_node_id()); 1852 mpol_cond_put(mpol); 1853 1854 return folio; 1855 } 1856 1857 static struct folio *shmem_alloc_and_add_folio(struct vm_fault *vmf, 1858 gfp_t gfp, struct inode *inode, pgoff_t index, 1859 struct mm_struct *fault_mm, unsigned long orders) 1860 { 1861 struct address_space *mapping = inode->i_mapping; 1862 struct shmem_inode_info *info = SHMEM_I(inode); 1863 unsigned long suitable_orders = 0; 1864 struct folio *folio = NULL; 1865 long pages; 1866 int error, order; 1867 1868 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 1869 orders = 0; 1870 1871 if (orders > 0) { 1872 suitable_orders = shmem_suitable_orders(inode, vmf, 1873 mapping, index, orders); 1874 1875 order = highest_order(suitable_orders); 1876 while (suitable_orders) { 1877 pages = 1UL << order; 1878 index = round_down(index, pages); 1879 folio = shmem_alloc_folio(gfp, order, info, index); 1880 if (folio) 1881 goto allocated; 1882 1883 if (pages == HPAGE_PMD_NR) 1884 count_vm_event(THP_FILE_FALLBACK); 1885 count_mthp_stat(order, MTHP_STAT_SHMEM_FALLBACK); 1886 order = next_order(&suitable_orders, order); 1887 } 1888 } else { 1889 pages = 1; 1890 folio = shmem_alloc_folio(gfp, 0, info, index); 1891 } 1892 if (!folio) 1893 return ERR_PTR(-ENOMEM); 1894 1895 allocated: 1896 __folio_set_locked(folio); 1897 __folio_set_swapbacked(folio); 1898 1899 gfp &= GFP_RECLAIM_MASK; 1900 error = mem_cgroup_charge(folio, fault_mm, gfp); 1901 if (error) { 1902 if (xa_find(&mapping->i_pages, &index, 1903 index + pages - 1, XA_PRESENT)) { 1904 error = -EEXIST; 1905 } else if (pages > 1) { 1906 if (pages == HPAGE_PMD_NR) { 1907 count_vm_event(THP_FILE_FALLBACK); 1908 count_vm_event(THP_FILE_FALLBACK_CHARGE); 1909 } 1910 count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_FALLBACK); 1911 count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_FALLBACK_CHARGE); 1912 } 1913 goto unlock; 1914 } 1915 1916 error = shmem_add_to_page_cache(folio, mapping, index, NULL, gfp); 1917 if (error) 1918 goto unlock; 1919 1920 error = shmem_inode_acct_blocks(inode, pages); 1921 if (error) { 1922 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 1923 long freed; 1924 /* 1925 * Try to reclaim some space by splitting a few 1926 * large folios beyond i_size on the filesystem. 1927 */ 1928 shmem_unused_huge_shrink(sbinfo, NULL, pages); 1929 /* 1930 * And do a shmem_recalc_inode() to account for freed pages: 1931 * except our folio is there in cache, so not quite balanced. 1932 */ 1933 spin_lock(&info->lock); 1934 freed = pages + info->alloced - info->swapped - 1935 READ_ONCE(mapping->nrpages); 1936 if (freed > 0) 1937 info->alloced -= freed; 1938 spin_unlock(&info->lock); 1939 if (freed > 0) 1940 shmem_inode_unacct_blocks(inode, freed); 1941 error = shmem_inode_acct_blocks(inode, pages); 1942 if (error) { 1943 filemap_remove_folio(folio); 1944 goto unlock; 1945 } 1946 } 1947 1948 shmem_recalc_inode(inode, pages, 0); 1949 folio_add_lru(folio); 1950 return folio; 1951 1952 unlock: 1953 folio_unlock(folio); 1954 folio_put(folio); 1955 return ERR_PTR(error); 1956 } 1957 1958 static struct folio *shmem_swap_alloc_folio(struct inode *inode, 1959 struct vm_area_struct *vma, pgoff_t index, 1960 swp_entry_t entry, int order, gfp_t gfp) 1961 { 1962 struct shmem_inode_info *info = SHMEM_I(inode); 1963 struct folio *new; 1964 void *shadow; 1965 int nr_pages; 1966 1967 /* 1968 * We have arrived here because our zones are constrained, so don't 1969 * limit chance of success with further cpuset and node constraints. 1970 */ 1971 gfp &= ~GFP_CONSTRAINT_MASK; 1972 if (IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && order > 0) { 1973 gfp_t huge_gfp = vma_thp_gfp_mask(vma); 1974 1975 gfp = limit_gfp_mask(huge_gfp, gfp); 1976 } 1977 1978 new = shmem_alloc_folio(gfp, order, info, index); 1979 if (!new) 1980 return ERR_PTR(-ENOMEM); 1981 1982 nr_pages = folio_nr_pages(new); 1983 if (mem_cgroup_swapin_charge_folio(new, vma ? vma->vm_mm : NULL, 1984 gfp, entry)) { 1985 folio_put(new); 1986 return ERR_PTR(-ENOMEM); 1987 } 1988 1989 /* 1990 * Prevent parallel swapin from proceeding with the swap cache flag. 1991 * 1992 * Of course there is another possible concurrent scenario as well, 1993 * that is to say, the swap cache flag of a large folio has already 1994 * been set by swapcache_prepare(), while another thread may have 1995 * already split the large swap entry stored in the shmem mapping. 1996 * In this case, shmem_add_to_page_cache() will help identify the 1997 * concurrent swapin and return -EEXIST. 1998 */ 1999 if (swapcache_prepare(entry, nr_pages)) { 2000 folio_put(new); 2001 return ERR_PTR(-EEXIST); 2002 } 2003 2004 __folio_set_locked(new); 2005 __folio_set_swapbacked(new); 2006 new->swap = entry; 2007 2008 memcg1_swapin(entry, nr_pages); 2009 shadow = get_shadow_from_swap_cache(entry); 2010 if (shadow) 2011 workingset_refault(new, shadow); 2012 folio_add_lru(new); 2013 swap_read_folio(new, NULL); 2014 return new; 2015 } 2016 2017 /* 2018 * When a page is moved from swapcache to shmem filecache (either by the 2019 * usual swapin of shmem_get_folio_gfp(), or by the less common swapoff of 2020 * shmem_unuse_inode()), it may have been read in earlier from swap, in 2021 * ignorance of the mapping it belongs to. If that mapping has special 2022 * constraints (like the gma500 GEM driver, which requires RAM below 4GB), 2023 * we may need to copy to a suitable page before moving to filecache. 2024 * 2025 * In a future release, this may well be extended to respect cpuset and 2026 * NUMA mempolicy, and applied also to anonymous pages in do_swap_page(); 2027 * but for now it is a simple matter of zone. 2028 */ 2029 static bool shmem_should_replace_folio(struct folio *folio, gfp_t gfp) 2030 { 2031 return folio_zonenum(folio) > gfp_zone(gfp); 2032 } 2033 2034 static int shmem_replace_folio(struct folio **foliop, gfp_t gfp, 2035 struct shmem_inode_info *info, pgoff_t index, 2036 struct vm_area_struct *vma) 2037 { 2038 struct folio *new, *old = *foliop; 2039 swp_entry_t entry = old->swap; 2040 struct address_space *swap_mapping = swap_address_space(entry); 2041 pgoff_t swap_index = swap_cache_index(entry); 2042 XA_STATE(xas, &swap_mapping->i_pages, swap_index); 2043 int nr_pages = folio_nr_pages(old); 2044 int error = 0, i; 2045 2046 /* 2047 * We have arrived here because our zones are constrained, so don't 2048 * limit chance of success by further cpuset and node constraints. 2049 */ 2050 gfp &= ~GFP_CONSTRAINT_MASK; 2051 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2052 if (nr_pages > 1) { 2053 gfp_t huge_gfp = vma_thp_gfp_mask(vma); 2054 2055 gfp = limit_gfp_mask(huge_gfp, gfp); 2056 } 2057 #endif 2058 2059 new = shmem_alloc_folio(gfp, folio_order(old), info, index); 2060 if (!new) 2061 return -ENOMEM; 2062 2063 folio_ref_add(new, nr_pages); 2064 folio_copy(new, old); 2065 flush_dcache_folio(new); 2066 2067 __folio_set_locked(new); 2068 __folio_set_swapbacked(new); 2069 folio_mark_uptodate(new); 2070 new->swap = entry; 2071 folio_set_swapcache(new); 2072 2073 /* Swap cache still stores N entries instead of a high-order entry */ 2074 xa_lock_irq(&swap_mapping->i_pages); 2075 for (i = 0; i < nr_pages; i++) { 2076 void *item = xas_load(&xas); 2077 2078 if (item != old) { 2079 error = -ENOENT; 2080 break; 2081 } 2082 2083 xas_store(&xas, new); 2084 xas_next(&xas); 2085 } 2086 if (!error) { 2087 mem_cgroup_replace_folio(old, new); 2088 shmem_update_stats(new, nr_pages); 2089 shmem_update_stats(old, -nr_pages); 2090 } 2091 xa_unlock_irq(&swap_mapping->i_pages); 2092 2093 if (unlikely(error)) { 2094 /* 2095 * Is this possible? I think not, now that our callers 2096 * check both the swapcache flag and folio->private 2097 * after getting the folio lock; but be defensive. 2098 * Reverse old to newpage for clear and free. 2099 */ 2100 old = new; 2101 } else { 2102 folio_add_lru(new); 2103 *foliop = new; 2104 } 2105 2106 folio_clear_swapcache(old); 2107 old->private = NULL; 2108 2109 folio_unlock(old); 2110 /* 2111 * The old folio are removed from swap cache, drop the 'nr_pages' 2112 * reference, as well as one temporary reference getting from swap 2113 * cache. 2114 */ 2115 folio_put_refs(old, nr_pages + 1); 2116 return error; 2117 } 2118 2119 static void shmem_set_folio_swapin_error(struct inode *inode, pgoff_t index, 2120 struct folio *folio, swp_entry_t swap, 2121 bool skip_swapcache) 2122 { 2123 struct address_space *mapping = inode->i_mapping; 2124 swp_entry_t swapin_error; 2125 void *old; 2126 int nr_pages; 2127 2128 swapin_error = make_poisoned_swp_entry(); 2129 old = xa_cmpxchg_irq(&mapping->i_pages, index, 2130 swp_to_radix_entry(swap), 2131 swp_to_radix_entry(swapin_error), 0); 2132 if (old != swp_to_radix_entry(swap)) 2133 return; 2134 2135 nr_pages = folio_nr_pages(folio); 2136 folio_wait_writeback(folio); 2137 if (!skip_swapcache) 2138 delete_from_swap_cache(folio); 2139 /* 2140 * Don't treat swapin error folio as alloced. Otherwise inode->i_blocks 2141 * won't be 0 when inode is released and thus trigger WARN_ON(i_blocks) 2142 * in shmem_evict_inode(). 2143 */ 2144 shmem_recalc_inode(inode, -nr_pages, -nr_pages); 2145 swap_free_nr(swap, nr_pages); 2146 } 2147 2148 static int shmem_split_large_entry(struct inode *inode, pgoff_t index, 2149 swp_entry_t swap, gfp_t gfp) 2150 { 2151 struct address_space *mapping = inode->i_mapping; 2152 XA_STATE_ORDER(xas, &mapping->i_pages, index, 0); 2153 int split_order = 0, entry_order; 2154 int i; 2155 2156 /* Convert user data gfp flags to xarray node gfp flags */ 2157 gfp &= GFP_RECLAIM_MASK; 2158 2159 for (;;) { 2160 void *old = NULL; 2161 int cur_order; 2162 pgoff_t swap_index; 2163 2164 xas_lock_irq(&xas); 2165 old = xas_load(&xas); 2166 if (!xa_is_value(old) || swp_to_radix_entry(swap) != old) { 2167 xas_set_err(&xas, -EEXIST); 2168 goto unlock; 2169 } 2170 2171 entry_order = xas_get_order(&xas); 2172 2173 if (!entry_order) 2174 goto unlock; 2175 2176 /* Try to split large swap entry in pagecache */ 2177 cur_order = entry_order; 2178 swap_index = round_down(index, 1 << entry_order); 2179 2180 split_order = xas_try_split_min_order(cur_order); 2181 2182 while (cur_order > 0) { 2183 pgoff_t aligned_index = 2184 round_down(index, 1 << cur_order); 2185 pgoff_t swap_offset = aligned_index - swap_index; 2186 2187 xas_set_order(&xas, index, split_order); 2188 xas_try_split(&xas, old, cur_order); 2189 if (xas_error(&xas)) 2190 goto unlock; 2191 2192 /* 2193 * Re-set the swap entry after splitting, and the swap 2194 * offset of the original large entry must be continuous. 2195 */ 2196 for (i = 0; i < 1 << cur_order; 2197 i += (1 << split_order)) { 2198 swp_entry_t tmp; 2199 2200 tmp = swp_entry(swp_type(swap), 2201 swp_offset(swap) + swap_offset + 2202 i); 2203 __xa_store(&mapping->i_pages, aligned_index + i, 2204 swp_to_radix_entry(tmp), 0); 2205 } 2206 cur_order = split_order; 2207 split_order = xas_try_split_min_order(split_order); 2208 } 2209 2210 unlock: 2211 xas_unlock_irq(&xas); 2212 2213 if (!xas_nomem(&xas, gfp)) 2214 break; 2215 } 2216 2217 if (xas_error(&xas)) 2218 return xas_error(&xas); 2219 2220 return entry_order; 2221 } 2222 2223 /* 2224 * Swap in the folio pointed to by *foliop. 2225 * Caller has to make sure that *foliop contains a valid swapped folio. 2226 * Returns 0 and the folio in foliop if success. On failure, returns the 2227 * error code and NULL in *foliop. 2228 */ 2229 static int shmem_swapin_folio(struct inode *inode, pgoff_t index, 2230 struct folio **foliop, enum sgp_type sgp, 2231 gfp_t gfp, struct vm_area_struct *vma, 2232 vm_fault_t *fault_type) 2233 { 2234 struct address_space *mapping = inode->i_mapping; 2235 struct mm_struct *fault_mm = vma ? vma->vm_mm : NULL; 2236 struct shmem_inode_info *info = SHMEM_I(inode); 2237 struct swap_info_struct *si; 2238 struct folio *folio = NULL; 2239 bool skip_swapcache = false; 2240 swp_entry_t swap; 2241 int error, nr_pages, order, split_order; 2242 2243 VM_BUG_ON(!*foliop || !xa_is_value(*foliop)); 2244 swap = radix_to_swp_entry(*foliop); 2245 *foliop = NULL; 2246 2247 if (is_poisoned_swp_entry(swap)) 2248 return -EIO; 2249 2250 si = get_swap_device(swap); 2251 if (!si) { 2252 if (!shmem_confirm_swap(mapping, index, swap)) 2253 return -EEXIST; 2254 else 2255 return -EINVAL; 2256 } 2257 2258 /* Look it up and read it in.. */ 2259 folio = swap_cache_get_folio(swap, NULL, 0); 2260 order = xa_get_order(&mapping->i_pages, index); 2261 if (!folio) { 2262 int nr_pages = 1 << order; 2263 bool fallback_order0 = false; 2264 2265 /* Or update major stats only when swapin succeeds?? */ 2266 if (fault_type) { 2267 *fault_type |= VM_FAULT_MAJOR; 2268 count_vm_event(PGMAJFAULT); 2269 count_memcg_event_mm(fault_mm, PGMAJFAULT); 2270 } 2271 2272 /* 2273 * If uffd is active for the vma, we need per-page fault 2274 * fidelity to maintain the uffd semantics, then fallback 2275 * to swapin order-0 folio, as well as for zswap case. 2276 * Any existing sub folio in the swap cache also blocks 2277 * mTHP swapin. 2278 */ 2279 if (order > 0 && ((vma && unlikely(userfaultfd_armed(vma))) || 2280 !zswap_never_enabled() || 2281 non_swapcache_batch(swap, nr_pages) != nr_pages)) 2282 fallback_order0 = true; 2283 2284 /* Skip swapcache for synchronous device. */ 2285 if (!fallback_order0 && data_race(si->flags & SWP_SYNCHRONOUS_IO)) { 2286 folio = shmem_swap_alloc_folio(inode, vma, index, swap, order, gfp); 2287 if (!IS_ERR(folio)) { 2288 skip_swapcache = true; 2289 goto alloced; 2290 } 2291 2292 /* 2293 * Fallback to swapin order-0 folio unless the swap entry 2294 * already exists. 2295 */ 2296 error = PTR_ERR(folio); 2297 folio = NULL; 2298 if (error == -EEXIST) 2299 goto failed; 2300 } 2301 2302 /* 2303 * Now swap device can only swap in order 0 folio, then we 2304 * should split the large swap entry stored in the pagecache 2305 * if necessary. 2306 */ 2307 split_order = shmem_split_large_entry(inode, index, swap, gfp); 2308 if (split_order < 0) { 2309 error = split_order; 2310 goto failed; 2311 } 2312 2313 /* 2314 * If the large swap entry has already been split, it is 2315 * necessary to recalculate the new swap entry based on 2316 * the old order alignment. 2317 */ 2318 if (split_order > 0) { 2319 pgoff_t offset = index - round_down(index, 1 << split_order); 2320 2321 swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); 2322 } 2323 2324 /* Here we actually start the io */ 2325 folio = shmem_swapin_cluster(swap, gfp, info, index); 2326 if (!folio) { 2327 error = -ENOMEM; 2328 goto failed; 2329 } 2330 } else if (order != folio_order(folio)) { 2331 /* 2332 * Swap readahead may swap in order 0 folios into swapcache 2333 * asynchronously, while the shmem mapping can still stores 2334 * large swap entries. In such cases, we should split the 2335 * large swap entry to prevent possible data corruption. 2336 */ 2337 split_order = shmem_split_large_entry(inode, index, swap, gfp); 2338 if (split_order < 0) { 2339 folio_put(folio); 2340 folio = NULL; 2341 error = split_order; 2342 goto failed; 2343 } 2344 2345 /* 2346 * If the large swap entry has already been split, it is 2347 * necessary to recalculate the new swap entry based on 2348 * the old order alignment. 2349 */ 2350 if (split_order > 0) { 2351 pgoff_t offset = index - round_down(index, 1 << split_order); 2352 2353 swap = swp_entry(swp_type(swap), swp_offset(swap) + offset); 2354 } 2355 } 2356 2357 alloced: 2358 /* We have to do this with folio locked to prevent races */ 2359 folio_lock(folio); 2360 if ((!skip_swapcache && !folio_test_swapcache(folio)) || 2361 folio->swap.val != swap.val || 2362 !shmem_confirm_swap(mapping, index, swap) || 2363 xa_get_order(&mapping->i_pages, index) != folio_order(folio)) { 2364 error = -EEXIST; 2365 goto unlock; 2366 } 2367 if (!folio_test_uptodate(folio)) { 2368 error = -EIO; 2369 goto failed; 2370 } 2371 folio_wait_writeback(folio); 2372 nr_pages = folio_nr_pages(folio); 2373 2374 /* 2375 * Some architectures may have to restore extra metadata to the 2376 * folio after reading from swap. 2377 */ 2378 arch_swap_restore(folio_swap(swap, folio), folio); 2379 2380 if (shmem_should_replace_folio(folio, gfp)) { 2381 error = shmem_replace_folio(&folio, gfp, info, index, vma); 2382 if (error) 2383 goto failed; 2384 } 2385 2386 error = shmem_add_to_page_cache(folio, mapping, 2387 round_down(index, nr_pages), 2388 swp_to_radix_entry(swap), gfp); 2389 if (error) 2390 goto failed; 2391 2392 shmem_recalc_inode(inode, 0, -nr_pages); 2393 2394 if (sgp == SGP_WRITE) 2395 folio_mark_accessed(folio); 2396 2397 if (skip_swapcache) { 2398 folio->swap.val = 0; 2399 swapcache_clear(si, swap, nr_pages); 2400 } else { 2401 delete_from_swap_cache(folio); 2402 } 2403 folio_mark_dirty(folio); 2404 swap_free_nr(swap, nr_pages); 2405 put_swap_device(si); 2406 2407 *foliop = folio; 2408 return 0; 2409 failed: 2410 if (!shmem_confirm_swap(mapping, index, swap)) 2411 error = -EEXIST; 2412 if (error == -EIO) 2413 shmem_set_folio_swapin_error(inode, index, folio, swap, 2414 skip_swapcache); 2415 unlock: 2416 if (skip_swapcache) 2417 swapcache_clear(si, swap, folio_nr_pages(folio)); 2418 if (folio) { 2419 folio_unlock(folio); 2420 folio_put(folio); 2421 } 2422 put_swap_device(si); 2423 2424 return error; 2425 } 2426 2427 /* 2428 * shmem_get_folio_gfp - find page in cache, or get from swap, or allocate 2429 * 2430 * If we allocate a new one we do not mark it dirty. That's up to the 2431 * vm. If we swap it in we mark it dirty since we also free the swap 2432 * entry since a page cannot live in both the swap and page cache. 2433 * 2434 * vmf and fault_type are only supplied by shmem_fault: otherwise they are NULL. 2435 */ 2436 static int shmem_get_folio_gfp(struct inode *inode, pgoff_t index, 2437 loff_t write_end, struct folio **foliop, enum sgp_type sgp, 2438 gfp_t gfp, struct vm_fault *vmf, vm_fault_t *fault_type) 2439 { 2440 struct vm_area_struct *vma = vmf ? vmf->vma : NULL; 2441 struct mm_struct *fault_mm; 2442 struct folio *folio; 2443 int error; 2444 bool alloced; 2445 unsigned long orders = 0; 2446 2447 if (WARN_ON_ONCE(!shmem_mapping(inode->i_mapping))) 2448 return -EINVAL; 2449 2450 if (index > (MAX_LFS_FILESIZE >> PAGE_SHIFT)) 2451 return -EFBIG; 2452 repeat: 2453 if (sgp <= SGP_CACHE && 2454 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) 2455 return -EINVAL; 2456 2457 alloced = false; 2458 fault_mm = vma ? vma->vm_mm : NULL; 2459 2460 folio = filemap_get_entry(inode->i_mapping, index); 2461 if (folio && vma && userfaultfd_minor(vma)) { 2462 if (!xa_is_value(folio)) 2463 folio_put(folio); 2464 *fault_type = handle_userfault(vmf, VM_UFFD_MINOR); 2465 return 0; 2466 } 2467 2468 if (xa_is_value(folio)) { 2469 error = shmem_swapin_folio(inode, index, &folio, 2470 sgp, gfp, vma, fault_type); 2471 if (error == -EEXIST) 2472 goto repeat; 2473 2474 *foliop = folio; 2475 return error; 2476 } 2477 2478 if (folio) { 2479 folio_lock(folio); 2480 2481 /* Has the folio been truncated or swapped out? */ 2482 if (unlikely(folio->mapping != inode->i_mapping)) { 2483 folio_unlock(folio); 2484 folio_put(folio); 2485 goto repeat; 2486 } 2487 if (sgp == SGP_WRITE) 2488 folio_mark_accessed(folio); 2489 if (folio_test_uptodate(folio)) 2490 goto out; 2491 /* fallocated folio */ 2492 if (sgp != SGP_READ) 2493 goto clear; 2494 folio_unlock(folio); 2495 folio_put(folio); 2496 } 2497 2498 /* 2499 * SGP_READ: succeed on hole, with NULL folio, letting caller zero. 2500 * SGP_NOALLOC: fail on hole, with NULL folio, letting caller fail. 2501 */ 2502 *foliop = NULL; 2503 if (sgp == SGP_READ) 2504 return 0; 2505 if (sgp == SGP_NOALLOC) 2506 return -ENOENT; 2507 2508 /* 2509 * Fast cache lookup and swap lookup did not find it: allocate. 2510 */ 2511 2512 if (vma && userfaultfd_missing(vma)) { 2513 *fault_type = handle_userfault(vmf, VM_UFFD_MISSING); 2514 return 0; 2515 } 2516 2517 /* Find hugepage orders that are allowed for anonymous shmem and tmpfs. */ 2518 orders = shmem_allowable_huge_orders(inode, vma, index, write_end, false); 2519 if (orders > 0) { 2520 gfp_t huge_gfp; 2521 2522 huge_gfp = vma_thp_gfp_mask(vma); 2523 huge_gfp = limit_gfp_mask(huge_gfp, gfp); 2524 folio = shmem_alloc_and_add_folio(vmf, huge_gfp, 2525 inode, index, fault_mm, orders); 2526 if (!IS_ERR(folio)) { 2527 if (folio_test_pmd_mappable(folio)) 2528 count_vm_event(THP_FILE_ALLOC); 2529 count_mthp_stat(folio_order(folio), MTHP_STAT_SHMEM_ALLOC); 2530 goto alloced; 2531 } 2532 if (PTR_ERR(folio) == -EEXIST) 2533 goto repeat; 2534 } 2535 2536 folio = shmem_alloc_and_add_folio(vmf, gfp, inode, index, fault_mm, 0); 2537 if (IS_ERR(folio)) { 2538 error = PTR_ERR(folio); 2539 if (error == -EEXIST) 2540 goto repeat; 2541 folio = NULL; 2542 goto unlock; 2543 } 2544 2545 alloced: 2546 alloced = true; 2547 if (folio_test_large(folio) && 2548 DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE) < 2549 folio_next_index(folio)) { 2550 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 2551 struct shmem_inode_info *info = SHMEM_I(inode); 2552 /* 2553 * Part of the large folio is beyond i_size: subject 2554 * to shrink under memory pressure. 2555 */ 2556 spin_lock(&sbinfo->shrinklist_lock); 2557 /* 2558 * _careful to defend against unlocked access to 2559 * ->shrink_list in shmem_unused_huge_shrink() 2560 */ 2561 if (list_empty_careful(&info->shrinklist)) { 2562 list_add_tail(&info->shrinklist, 2563 &sbinfo->shrinklist); 2564 sbinfo->shrinklist_len++; 2565 } 2566 spin_unlock(&sbinfo->shrinklist_lock); 2567 } 2568 2569 if (sgp == SGP_WRITE) 2570 folio_set_referenced(folio); 2571 /* 2572 * Let SGP_FALLOC use the SGP_WRITE optimization on a new folio. 2573 */ 2574 if (sgp == SGP_FALLOC) 2575 sgp = SGP_WRITE; 2576 clear: 2577 /* 2578 * Let SGP_WRITE caller clear ends if write does not fill folio; 2579 * but SGP_FALLOC on a folio fallocated earlier must initialize 2580 * it now, lest undo on failure cancel our earlier guarantee. 2581 */ 2582 if (sgp != SGP_WRITE && !folio_test_uptodate(folio)) { 2583 long i, n = folio_nr_pages(folio); 2584 2585 for (i = 0; i < n; i++) 2586 clear_highpage(folio_page(folio, i)); 2587 flush_dcache_folio(folio); 2588 folio_mark_uptodate(folio); 2589 } 2590 2591 /* Perhaps the file has been truncated since we checked */ 2592 if (sgp <= SGP_CACHE && 2593 ((loff_t)index << PAGE_SHIFT) >= i_size_read(inode)) { 2594 error = -EINVAL; 2595 goto unlock; 2596 } 2597 out: 2598 *foliop = folio; 2599 return 0; 2600 2601 /* 2602 * Error recovery. 2603 */ 2604 unlock: 2605 if (alloced) 2606 filemap_remove_folio(folio); 2607 shmem_recalc_inode(inode, 0, 0); 2608 if (folio) { 2609 folio_unlock(folio); 2610 folio_put(folio); 2611 } 2612 return error; 2613 } 2614 2615 /** 2616 * shmem_get_folio - find, and lock a shmem folio. 2617 * @inode: inode to search 2618 * @index: the page index. 2619 * @write_end: end of a write, could extend inode size 2620 * @foliop: pointer to the folio if found 2621 * @sgp: SGP_* flags to control behavior 2622 * 2623 * Looks up the page cache entry at @inode & @index. If a folio is 2624 * present, it is returned locked with an increased refcount. 2625 * 2626 * If the caller modifies data in the folio, it must call folio_mark_dirty() 2627 * before unlocking the folio to ensure that the folio is not reclaimed. 2628 * There is no need to reserve space before calling folio_mark_dirty(). 2629 * 2630 * When no folio is found, the behavior depends on @sgp: 2631 * - for SGP_READ, *@foliop is %NULL and 0 is returned 2632 * - for SGP_NOALLOC, *@foliop is %NULL and -ENOENT is returned 2633 * - for all other flags a new folio is allocated, inserted into the 2634 * page cache and returned locked in @foliop. 2635 * 2636 * Context: May sleep. 2637 * Return: 0 if successful, else a negative error code. 2638 */ 2639 int shmem_get_folio(struct inode *inode, pgoff_t index, loff_t write_end, 2640 struct folio **foliop, enum sgp_type sgp) 2641 { 2642 return shmem_get_folio_gfp(inode, index, write_end, foliop, sgp, 2643 mapping_gfp_mask(inode->i_mapping), NULL, NULL); 2644 } 2645 EXPORT_SYMBOL_GPL(shmem_get_folio); 2646 2647 /* 2648 * This is like autoremove_wake_function, but it removes the wait queue 2649 * entry unconditionally - even if something else had already woken the 2650 * target. 2651 */ 2652 static int synchronous_wake_function(wait_queue_entry_t *wait, 2653 unsigned int mode, int sync, void *key) 2654 { 2655 int ret = default_wake_function(wait, mode, sync, key); 2656 list_del_init(&wait->entry); 2657 return ret; 2658 } 2659 2660 /* 2661 * Trinity finds that probing a hole which tmpfs is punching can 2662 * prevent the hole-punch from ever completing: which in turn 2663 * locks writers out with its hold on i_rwsem. So refrain from 2664 * faulting pages into the hole while it's being punched. Although 2665 * shmem_undo_range() does remove the additions, it may be unable to 2666 * keep up, as each new page needs its own unmap_mapping_range() call, 2667 * and the i_mmap tree grows ever slower to scan if new vmas are added. 2668 * 2669 * It does not matter if we sometimes reach this check just before the 2670 * hole-punch begins, so that one fault then races with the punch: 2671 * we just need to make racing faults a rare case. 2672 * 2673 * The implementation below would be much simpler if we just used a 2674 * standard mutex or completion: but we cannot take i_rwsem in fault, 2675 * and bloating every shmem inode for this unlikely case would be sad. 2676 */ 2677 static vm_fault_t shmem_falloc_wait(struct vm_fault *vmf, struct inode *inode) 2678 { 2679 struct shmem_falloc *shmem_falloc; 2680 struct file *fpin = NULL; 2681 vm_fault_t ret = 0; 2682 2683 spin_lock(&inode->i_lock); 2684 shmem_falloc = inode->i_private; 2685 if (shmem_falloc && 2686 shmem_falloc->waitq && 2687 vmf->pgoff >= shmem_falloc->start && 2688 vmf->pgoff < shmem_falloc->next) { 2689 wait_queue_head_t *shmem_falloc_waitq; 2690 DEFINE_WAIT_FUNC(shmem_fault_wait, synchronous_wake_function); 2691 2692 ret = VM_FAULT_NOPAGE; 2693 fpin = maybe_unlock_mmap_for_io(vmf, NULL); 2694 shmem_falloc_waitq = shmem_falloc->waitq; 2695 prepare_to_wait(shmem_falloc_waitq, &shmem_fault_wait, 2696 TASK_UNINTERRUPTIBLE); 2697 spin_unlock(&inode->i_lock); 2698 schedule(); 2699 2700 /* 2701 * shmem_falloc_waitq points into the shmem_fallocate() 2702 * stack of the hole-punching task: shmem_falloc_waitq 2703 * is usually invalid by the time we reach here, but 2704 * finish_wait() does not dereference it in that case; 2705 * though i_lock needed lest racing with wake_up_all(). 2706 */ 2707 spin_lock(&inode->i_lock); 2708 finish_wait(shmem_falloc_waitq, &shmem_fault_wait); 2709 } 2710 spin_unlock(&inode->i_lock); 2711 if (fpin) { 2712 fput(fpin); 2713 ret = VM_FAULT_RETRY; 2714 } 2715 return ret; 2716 } 2717 2718 static vm_fault_t shmem_fault(struct vm_fault *vmf) 2719 { 2720 struct inode *inode = file_inode(vmf->vma->vm_file); 2721 gfp_t gfp = mapping_gfp_mask(inode->i_mapping); 2722 struct folio *folio = NULL; 2723 vm_fault_t ret = 0; 2724 int err; 2725 2726 /* 2727 * Trinity finds that probing a hole which tmpfs is punching can 2728 * prevent the hole-punch from ever completing: noted in i_private. 2729 */ 2730 if (unlikely(inode->i_private)) { 2731 ret = shmem_falloc_wait(vmf, inode); 2732 if (ret) 2733 return ret; 2734 } 2735 2736 WARN_ON_ONCE(vmf->page != NULL); 2737 err = shmem_get_folio_gfp(inode, vmf->pgoff, 0, &folio, SGP_CACHE, 2738 gfp, vmf, &ret); 2739 if (err) 2740 return vmf_error(err); 2741 if (folio) { 2742 vmf->page = folio_file_page(folio, vmf->pgoff); 2743 ret |= VM_FAULT_LOCKED; 2744 } 2745 return ret; 2746 } 2747 2748 unsigned long shmem_get_unmapped_area(struct file *file, 2749 unsigned long uaddr, unsigned long len, 2750 unsigned long pgoff, unsigned long flags) 2751 { 2752 unsigned long addr; 2753 unsigned long offset; 2754 unsigned long inflated_len; 2755 unsigned long inflated_addr; 2756 unsigned long inflated_offset; 2757 unsigned long hpage_size; 2758 2759 if (len > TASK_SIZE) 2760 return -ENOMEM; 2761 2762 addr = mm_get_unmapped_area(current->mm, file, uaddr, len, pgoff, 2763 flags); 2764 2765 if (!IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE)) 2766 return addr; 2767 if (IS_ERR_VALUE(addr)) 2768 return addr; 2769 if (addr & ~PAGE_MASK) 2770 return addr; 2771 if (addr > TASK_SIZE - len) 2772 return addr; 2773 2774 if (shmem_huge == SHMEM_HUGE_DENY) 2775 return addr; 2776 if (flags & MAP_FIXED) 2777 return addr; 2778 /* 2779 * Our priority is to support MAP_SHARED mapped hugely; 2780 * and support MAP_PRIVATE mapped hugely too, until it is COWed. 2781 * But if caller specified an address hint and we allocated area there 2782 * successfully, respect that as before. 2783 */ 2784 if (uaddr == addr) 2785 return addr; 2786 2787 hpage_size = HPAGE_PMD_SIZE; 2788 if (shmem_huge != SHMEM_HUGE_FORCE) { 2789 struct super_block *sb; 2790 unsigned long __maybe_unused hpage_orders; 2791 int order = 0; 2792 2793 if (file) { 2794 VM_BUG_ON(file->f_op != &shmem_file_operations); 2795 sb = file_inode(file)->i_sb; 2796 } else { 2797 /* 2798 * Called directly from mm/mmap.c, or drivers/char/mem.c 2799 * for "/dev/zero", to create a shared anonymous object. 2800 */ 2801 if (IS_ERR(shm_mnt)) 2802 return addr; 2803 sb = shm_mnt->mnt_sb; 2804 2805 /* 2806 * Find the highest mTHP order used for anonymous shmem to 2807 * provide a suitable alignment address. 2808 */ 2809 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 2810 hpage_orders = READ_ONCE(huge_shmem_orders_always); 2811 hpage_orders |= READ_ONCE(huge_shmem_orders_within_size); 2812 hpage_orders |= READ_ONCE(huge_shmem_orders_madvise); 2813 if (SHMEM_SB(sb)->huge != SHMEM_HUGE_NEVER) 2814 hpage_orders |= READ_ONCE(huge_shmem_orders_inherit); 2815 2816 if (hpage_orders > 0) { 2817 order = highest_order(hpage_orders); 2818 hpage_size = PAGE_SIZE << order; 2819 } 2820 #endif 2821 } 2822 if (SHMEM_SB(sb)->huge == SHMEM_HUGE_NEVER && !order) 2823 return addr; 2824 } 2825 2826 if (len < hpage_size) 2827 return addr; 2828 2829 offset = (pgoff << PAGE_SHIFT) & (hpage_size - 1); 2830 if (offset && offset + len < 2 * hpage_size) 2831 return addr; 2832 if ((addr & (hpage_size - 1)) == offset) 2833 return addr; 2834 2835 inflated_len = len + hpage_size - PAGE_SIZE; 2836 if (inflated_len > TASK_SIZE) 2837 return addr; 2838 if (inflated_len < len) 2839 return addr; 2840 2841 inflated_addr = mm_get_unmapped_area(current->mm, NULL, uaddr, 2842 inflated_len, 0, flags); 2843 if (IS_ERR_VALUE(inflated_addr)) 2844 return addr; 2845 if (inflated_addr & ~PAGE_MASK) 2846 return addr; 2847 2848 inflated_offset = inflated_addr & (hpage_size - 1); 2849 inflated_addr += offset - inflated_offset; 2850 if (inflated_offset > offset) 2851 inflated_addr += hpage_size; 2852 2853 if (inflated_addr > TASK_SIZE - len) 2854 return addr; 2855 return inflated_addr; 2856 } 2857 2858 #ifdef CONFIG_NUMA 2859 static int shmem_set_policy(struct vm_area_struct *vma, struct mempolicy *mpol) 2860 { 2861 struct inode *inode = file_inode(vma->vm_file); 2862 return mpol_set_shared_policy(&SHMEM_I(inode)->policy, vma, mpol); 2863 } 2864 2865 static struct mempolicy *shmem_get_policy(struct vm_area_struct *vma, 2866 unsigned long addr, pgoff_t *ilx) 2867 { 2868 struct inode *inode = file_inode(vma->vm_file); 2869 pgoff_t index; 2870 2871 /* 2872 * Bias interleave by inode number to distribute better across nodes; 2873 * but this interface is independent of which page order is used, so 2874 * supplies only that bias, letting caller apply the offset (adjusted 2875 * by page order, as in shmem_get_pgoff_policy() and get_vma_policy()). 2876 */ 2877 *ilx = inode->i_ino; 2878 index = ((addr - vma->vm_start) >> PAGE_SHIFT) + vma->vm_pgoff; 2879 return mpol_shared_policy_lookup(&SHMEM_I(inode)->policy, index); 2880 } 2881 2882 static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *info, 2883 pgoff_t index, unsigned int order, pgoff_t *ilx) 2884 { 2885 struct mempolicy *mpol; 2886 2887 /* Bias interleave by inode number to distribute better across nodes */ 2888 *ilx = info->vfs_inode.i_ino + (index >> order); 2889 2890 mpol = mpol_shared_policy_lookup(&info->policy, index); 2891 return mpol ? mpol : get_task_policy(current); 2892 } 2893 #else 2894 static struct mempolicy *shmem_get_pgoff_policy(struct shmem_inode_info *info, 2895 pgoff_t index, unsigned int order, pgoff_t *ilx) 2896 { 2897 *ilx = 0; 2898 return NULL; 2899 } 2900 #endif /* CONFIG_NUMA */ 2901 2902 int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 2903 { 2904 struct inode *inode = file_inode(file); 2905 struct shmem_inode_info *info = SHMEM_I(inode); 2906 int retval = -ENOMEM; 2907 2908 /* 2909 * What serializes the accesses to info->flags? 2910 * ipc_lock_object() when called from shmctl_do_lock(), 2911 * no serialization needed when called from shm_destroy(). 2912 */ 2913 if (lock && !(info->flags & VM_LOCKED)) { 2914 if (!user_shm_lock(inode->i_size, ucounts)) 2915 goto out_nomem; 2916 info->flags |= VM_LOCKED; 2917 mapping_set_unevictable(file->f_mapping); 2918 } 2919 if (!lock && (info->flags & VM_LOCKED) && ucounts) { 2920 user_shm_unlock(inode->i_size, ucounts); 2921 info->flags &= ~VM_LOCKED; 2922 mapping_clear_unevictable(file->f_mapping); 2923 } 2924 retval = 0; 2925 2926 out_nomem: 2927 return retval; 2928 } 2929 2930 static int shmem_mmap(struct file *file, struct vm_area_struct *vma) 2931 { 2932 struct inode *inode = file_inode(file); 2933 2934 file_accessed(file); 2935 /* This is anonymous shared memory if it is unlinked at the time of mmap */ 2936 if (inode->i_nlink) 2937 vma->vm_ops = &shmem_vm_ops; 2938 else 2939 vma->vm_ops = &shmem_anon_vm_ops; 2940 return 0; 2941 } 2942 2943 static int shmem_file_open(struct inode *inode, struct file *file) 2944 { 2945 file->f_mode |= FMODE_CAN_ODIRECT; 2946 return generic_file_open(inode, file); 2947 } 2948 2949 #ifdef CONFIG_TMPFS_XATTR 2950 static int shmem_initxattrs(struct inode *, const struct xattr *, void *); 2951 2952 #if IS_ENABLED(CONFIG_UNICODE) 2953 /* 2954 * shmem_inode_casefold_flags - Deal with casefold file attribute flag 2955 * 2956 * The casefold file attribute needs some special checks. I can just be added to 2957 * an empty dir, and can't be removed from a non-empty dir. 2958 */ 2959 static int shmem_inode_casefold_flags(struct inode *inode, unsigned int fsflags, 2960 struct dentry *dentry, unsigned int *i_flags) 2961 { 2962 unsigned int old = inode->i_flags; 2963 struct super_block *sb = inode->i_sb; 2964 2965 if (fsflags & FS_CASEFOLD_FL) { 2966 if (!(old & S_CASEFOLD)) { 2967 if (!sb->s_encoding) 2968 return -EOPNOTSUPP; 2969 2970 if (!S_ISDIR(inode->i_mode)) 2971 return -ENOTDIR; 2972 2973 if (dentry && !simple_empty(dentry)) 2974 return -ENOTEMPTY; 2975 } 2976 2977 *i_flags = *i_flags | S_CASEFOLD; 2978 } else if (old & S_CASEFOLD) { 2979 if (dentry && !simple_empty(dentry)) 2980 return -ENOTEMPTY; 2981 } 2982 2983 return 0; 2984 } 2985 #else 2986 static int shmem_inode_casefold_flags(struct inode *inode, unsigned int fsflags, 2987 struct dentry *dentry, unsigned int *i_flags) 2988 { 2989 if (fsflags & FS_CASEFOLD_FL) 2990 return -EOPNOTSUPP; 2991 2992 return 0; 2993 } 2994 #endif 2995 2996 /* 2997 * chattr's fsflags are unrelated to extended attributes, 2998 * but tmpfs has chosen to enable them under the same config option. 2999 */ 3000 static int shmem_set_inode_flags(struct inode *inode, unsigned int fsflags, struct dentry *dentry) 3001 { 3002 unsigned int i_flags = 0; 3003 int ret; 3004 3005 ret = shmem_inode_casefold_flags(inode, fsflags, dentry, &i_flags); 3006 if (ret) 3007 return ret; 3008 3009 if (fsflags & FS_NOATIME_FL) 3010 i_flags |= S_NOATIME; 3011 if (fsflags & FS_APPEND_FL) 3012 i_flags |= S_APPEND; 3013 if (fsflags & FS_IMMUTABLE_FL) 3014 i_flags |= S_IMMUTABLE; 3015 /* 3016 * But FS_NODUMP_FL does not require any action in i_flags. 3017 */ 3018 inode_set_flags(inode, i_flags, S_NOATIME | S_APPEND | S_IMMUTABLE | S_CASEFOLD); 3019 3020 return 0; 3021 } 3022 #else 3023 static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags, struct dentry *dentry) 3024 { 3025 } 3026 #define shmem_initxattrs NULL 3027 #endif 3028 3029 static struct offset_ctx *shmem_get_offset_ctx(struct inode *inode) 3030 { 3031 return &SHMEM_I(inode)->dir_offsets; 3032 } 3033 3034 static struct inode *__shmem_get_inode(struct mnt_idmap *idmap, 3035 struct super_block *sb, 3036 struct inode *dir, umode_t mode, 3037 dev_t dev, unsigned long flags) 3038 { 3039 struct inode *inode; 3040 struct shmem_inode_info *info; 3041 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 3042 ino_t ino; 3043 int err; 3044 3045 err = shmem_reserve_inode(sb, &ino); 3046 if (err) 3047 return ERR_PTR(err); 3048 3049 inode = new_inode(sb); 3050 if (!inode) { 3051 shmem_free_inode(sb, 0); 3052 return ERR_PTR(-ENOSPC); 3053 } 3054 3055 inode->i_ino = ino; 3056 inode_init_owner(idmap, inode, dir, mode); 3057 inode->i_blocks = 0; 3058 simple_inode_init_ts(inode); 3059 inode->i_generation = get_random_u32(); 3060 info = SHMEM_I(inode); 3061 memset(info, 0, (char *)inode - (char *)info); 3062 spin_lock_init(&info->lock); 3063 atomic_set(&info->stop_eviction, 0); 3064 info->seals = F_SEAL_SEAL; 3065 info->flags = flags & VM_NORESERVE; 3066 info->i_crtime = inode_get_mtime(inode); 3067 info->fsflags = (dir == NULL) ? 0 : 3068 SHMEM_I(dir)->fsflags & SHMEM_FL_INHERITED; 3069 if (info->fsflags) 3070 shmem_set_inode_flags(inode, info->fsflags, NULL); 3071 INIT_LIST_HEAD(&info->shrinklist); 3072 INIT_LIST_HEAD(&info->swaplist); 3073 simple_xattrs_init(&info->xattrs); 3074 cache_no_acl(inode); 3075 if (sbinfo->noswap) 3076 mapping_set_unevictable(inode->i_mapping); 3077 3078 /* Don't consider 'deny' for emergencies and 'force' for testing */ 3079 if (sbinfo->huge) 3080 mapping_set_large_folios(inode->i_mapping); 3081 3082 switch (mode & S_IFMT) { 3083 default: 3084 inode->i_op = &shmem_special_inode_operations; 3085 init_special_inode(inode, mode, dev); 3086 break; 3087 case S_IFREG: 3088 inode->i_mapping->a_ops = &shmem_aops; 3089 inode->i_op = &shmem_inode_operations; 3090 inode->i_fop = &shmem_file_operations; 3091 mpol_shared_policy_init(&info->policy, 3092 shmem_get_sbmpol(sbinfo)); 3093 break; 3094 case S_IFDIR: 3095 inc_nlink(inode); 3096 /* Some things misbehave if size == 0 on a directory */ 3097 inode->i_size = 2 * BOGO_DIRENT_SIZE; 3098 inode->i_op = &shmem_dir_inode_operations; 3099 inode->i_fop = &simple_offset_dir_operations; 3100 simple_offset_init(shmem_get_offset_ctx(inode)); 3101 break; 3102 case S_IFLNK: 3103 /* 3104 * Must not load anything in the rbtree, 3105 * mpol_free_shared_policy will not be called. 3106 */ 3107 mpol_shared_policy_init(&info->policy, NULL); 3108 break; 3109 } 3110 3111 lockdep_annotate_inode_mutex_key(inode); 3112 return inode; 3113 } 3114 3115 #ifdef CONFIG_TMPFS_QUOTA 3116 static struct inode *shmem_get_inode(struct mnt_idmap *idmap, 3117 struct super_block *sb, struct inode *dir, 3118 umode_t mode, dev_t dev, unsigned long flags) 3119 { 3120 int err; 3121 struct inode *inode; 3122 3123 inode = __shmem_get_inode(idmap, sb, dir, mode, dev, flags); 3124 if (IS_ERR(inode)) 3125 return inode; 3126 3127 err = dquot_initialize(inode); 3128 if (err) 3129 goto errout; 3130 3131 err = dquot_alloc_inode(inode); 3132 if (err) { 3133 dquot_drop(inode); 3134 goto errout; 3135 } 3136 return inode; 3137 3138 errout: 3139 inode->i_flags |= S_NOQUOTA; 3140 iput(inode); 3141 return ERR_PTR(err); 3142 } 3143 #else 3144 static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, 3145 struct super_block *sb, struct inode *dir, 3146 umode_t mode, dev_t dev, unsigned long flags) 3147 { 3148 return __shmem_get_inode(idmap, sb, dir, mode, dev, flags); 3149 } 3150 #endif /* CONFIG_TMPFS_QUOTA */ 3151 3152 #ifdef CONFIG_USERFAULTFD 3153 int shmem_mfill_atomic_pte(pmd_t *dst_pmd, 3154 struct vm_area_struct *dst_vma, 3155 unsigned long dst_addr, 3156 unsigned long src_addr, 3157 uffd_flags_t flags, 3158 struct folio **foliop) 3159 { 3160 struct inode *inode = file_inode(dst_vma->vm_file); 3161 struct shmem_inode_info *info = SHMEM_I(inode); 3162 struct address_space *mapping = inode->i_mapping; 3163 gfp_t gfp = mapping_gfp_mask(mapping); 3164 pgoff_t pgoff = linear_page_index(dst_vma, dst_addr); 3165 void *page_kaddr; 3166 struct folio *folio; 3167 int ret; 3168 pgoff_t max_off; 3169 3170 if (shmem_inode_acct_blocks(inode, 1)) { 3171 /* 3172 * We may have got a page, returned -ENOENT triggering a retry, 3173 * and now we find ourselves with -ENOMEM. Release the page, to 3174 * avoid a BUG_ON in our caller. 3175 */ 3176 if (unlikely(*foliop)) { 3177 folio_put(*foliop); 3178 *foliop = NULL; 3179 } 3180 return -ENOMEM; 3181 } 3182 3183 if (!*foliop) { 3184 ret = -ENOMEM; 3185 folio = shmem_alloc_folio(gfp, 0, info, pgoff); 3186 if (!folio) 3187 goto out_unacct_blocks; 3188 3189 if (uffd_flags_mode_is(flags, MFILL_ATOMIC_COPY)) { 3190 page_kaddr = kmap_local_folio(folio, 0); 3191 /* 3192 * The read mmap_lock is held here. Despite the 3193 * mmap_lock being read recursive a deadlock is still 3194 * possible if a writer has taken a lock. For example: 3195 * 3196 * process A thread 1 takes read lock on own mmap_lock 3197 * process A thread 2 calls mmap, blocks taking write lock 3198 * process B thread 1 takes page fault, read lock on own mmap lock 3199 * process B thread 2 calls mmap, blocks taking write lock 3200 * process A thread 1 blocks taking read lock on process B 3201 * process B thread 1 blocks taking read lock on process A 3202 * 3203 * Disable page faults to prevent potential deadlock 3204 * and retry the copy outside the mmap_lock. 3205 */ 3206 pagefault_disable(); 3207 ret = copy_from_user(page_kaddr, 3208 (const void __user *)src_addr, 3209 PAGE_SIZE); 3210 pagefault_enable(); 3211 kunmap_local(page_kaddr); 3212 3213 /* fallback to copy_from_user outside mmap_lock */ 3214 if (unlikely(ret)) { 3215 *foliop = folio; 3216 ret = -ENOENT; 3217 /* don't free the page */ 3218 goto out_unacct_blocks; 3219 } 3220 3221 flush_dcache_folio(folio); 3222 } else { /* ZEROPAGE */ 3223 clear_user_highpage(&folio->page, dst_addr); 3224 } 3225 } else { 3226 folio = *foliop; 3227 VM_BUG_ON_FOLIO(folio_test_large(folio), folio); 3228 *foliop = NULL; 3229 } 3230 3231 VM_BUG_ON(folio_test_locked(folio)); 3232 VM_BUG_ON(folio_test_swapbacked(folio)); 3233 __folio_set_locked(folio); 3234 __folio_set_swapbacked(folio); 3235 __folio_mark_uptodate(folio); 3236 3237 ret = -EFAULT; 3238 max_off = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 3239 if (unlikely(pgoff >= max_off)) 3240 goto out_release; 3241 3242 ret = mem_cgroup_charge(folio, dst_vma->vm_mm, gfp); 3243 if (ret) 3244 goto out_release; 3245 ret = shmem_add_to_page_cache(folio, mapping, pgoff, NULL, gfp); 3246 if (ret) 3247 goto out_release; 3248 3249 ret = mfill_atomic_install_pte(dst_pmd, dst_vma, dst_addr, 3250 &folio->page, true, flags); 3251 if (ret) 3252 goto out_delete_from_cache; 3253 3254 shmem_recalc_inode(inode, 1, 0); 3255 folio_unlock(folio); 3256 return 0; 3257 out_delete_from_cache: 3258 filemap_remove_folio(folio); 3259 out_release: 3260 folio_unlock(folio); 3261 folio_put(folio); 3262 out_unacct_blocks: 3263 shmem_inode_unacct_blocks(inode, 1); 3264 return ret; 3265 } 3266 #endif /* CONFIG_USERFAULTFD */ 3267 3268 #ifdef CONFIG_TMPFS 3269 static const struct inode_operations shmem_symlink_inode_operations; 3270 static const struct inode_operations shmem_short_symlink_operations; 3271 3272 static int 3273 shmem_write_begin(struct file *file, struct address_space *mapping, 3274 loff_t pos, unsigned len, 3275 struct folio **foliop, void **fsdata) 3276 { 3277 struct inode *inode = mapping->host; 3278 struct shmem_inode_info *info = SHMEM_I(inode); 3279 pgoff_t index = pos >> PAGE_SHIFT; 3280 struct folio *folio; 3281 int ret = 0; 3282 3283 /* i_rwsem is held by caller */ 3284 if (unlikely(info->seals & (F_SEAL_GROW | 3285 F_SEAL_WRITE | F_SEAL_FUTURE_WRITE))) { 3286 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) 3287 return -EPERM; 3288 if ((info->seals & F_SEAL_GROW) && pos + len > inode->i_size) 3289 return -EPERM; 3290 } 3291 3292 ret = shmem_get_folio(inode, index, pos + len, &folio, SGP_WRITE); 3293 if (ret) 3294 return ret; 3295 3296 if (folio_contain_hwpoisoned_page(folio)) { 3297 folio_unlock(folio); 3298 folio_put(folio); 3299 return -EIO; 3300 } 3301 3302 *foliop = folio; 3303 return 0; 3304 } 3305 3306 static int 3307 shmem_write_end(struct file *file, struct address_space *mapping, 3308 loff_t pos, unsigned len, unsigned copied, 3309 struct folio *folio, void *fsdata) 3310 { 3311 struct inode *inode = mapping->host; 3312 3313 if (pos + copied > inode->i_size) 3314 i_size_write(inode, pos + copied); 3315 3316 if (!folio_test_uptodate(folio)) { 3317 if (copied < folio_size(folio)) { 3318 size_t from = offset_in_folio(folio, pos); 3319 folio_zero_segments(folio, 0, from, 3320 from + copied, folio_size(folio)); 3321 } 3322 folio_mark_uptodate(folio); 3323 } 3324 folio_mark_dirty(folio); 3325 folio_unlock(folio); 3326 folio_put(folio); 3327 3328 return copied; 3329 } 3330 3331 static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) 3332 { 3333 struct file *file = iocb->ki_filp; 3334 struct inode *inode = file_inode(file); 3335 struct address_space *mapping = inode->i_mapping; 3336 pgoff_t index; 3337 unsigned long offset; 3338 int error = 0; 3339 ssize_t retval = 0; 3340 3341 for (;;) { 3342 struct folio *folio = NULL; 3343 struct page *page = NULL; 3344 unsigned long nr, ret; 3345 loff_t end_offset, i_size = i_size_read(inode); 3346 bool fallback_page_copy = false; 3347 size_t fsize; 3348 3349 if (unlikely(iocb->ki_pos >= i_size)) 3350 break; 3351 3352 index = iocb->ki_pos >> PAGE_SHIFT; 3353 error = shmem_get_folio(inode, index, 0, &folio, SGP_READ); 3354 if (error) { 3355 if (error == -EINVAL) 3356 error = 0; 3357 break; 3358 } 3359 if (folio) { 3360 folio_unlock(folio); 3361 3362 page = folio_file_page(folio, index); 3363 if (PageHWPoison(page)) { 3364 folio_put(folio); 3365 error = -EIO; 3366 break; 3367 } 3368 3369 if (folio_test_large(folio) && 3370 folio_test_has_hwpoisoned(folio)) 3371 fallback_page_copy = true; 3372 } 3373 3374 /* 3375 * We must evaluate after, since reads (unlike writes) 3376 * are called without i_rwsem protection against truncate 3377 */ 3378 i_size = i_size_read(inode); 3379 if (unlikely(iocb->ki_pos >= i_size)) { 3380 if (folio) 3381 folio_put(folio); 3382 break; 3383 } 3384 end_offset = min_t(loff_t, i_size, iocb->ki_pos + to->count); 3385 if (folio && likely(!fallback_page_copy)) 3386 fsize = folio_size(folio); 3387 else 3388 fsize = PAGE_SIZE; 3389 offset = iocb->ki_pos & (fsize - 1); 3390 nr = min_t(loff_t, end_offset - iocb->ki_pos, fsize - offset); 3391 3392 if (folio) { 3393 /* 3394 * If users can be writing to this page using arbitrary 3395 * virtual addresses, take care about potential aliasing 3396 * before reading the page on the kernel side. 3397 */ 3398 if (mapping_writably_mapped(mapping)) { 3399 if (likely(!fallback_page_copy)) 3400 flush_dcache_folio(folio); 3401 else 3402 flush_dcache_page(page); 3403 } 3404 3405 /* 3406 * Mark the folio accessed if we read the beginning. 3407 */ 3408 if (!offset) 3409 folio_mark_accessed(folio); 3410 /* 3411 * Ok, we have the page, and it's up-to-date, so 3412 * now we can copy it to user space... 3413 */ 3414 if (likely(!fallback_page_copy)) 3415 ret = copy_folio_to_iter(folio, offset, nr, to); 3416 else 3417 ret = copy_page_to_iter(page, offset, nr, to); 3418 folio_put(folio); 3419 } else if (user_backed_iter(to)) { 3420 /* 3421 * Copy to user tends to be so well optimized, but 3422 * clear_user() not so much, that it is noticeably 3423 * faster to copy the zero page instead of clearing. 3424 */ 3425 ret = copy_page_to_iter(ZERO_PAGE(0), offset, nr, to); 3426 } else { 3427 /* 3428 * But submitting the same page twice in a row to 3429 * splice() - or others? - can result in confusion: 3430 * so don't attempt that optimization on pipes etc. 3431 */ 3432 ret = iov_iter_zero(nr, to); 3433 } 3434 3435 retval += ret; 3436 iocb->ki_pos += ret; 3437 3438 if (!iov_iter_count(to)) 3439 break; 3440 if (ret < nr) { 3441 error = -EFAULT; 3442 break; 3443 } 3444 cond_resched(); 3445 } 3446 3447 file_accessed(file); 3448 return retval ? retval : error; 3449 } 3450 3451 static ssize_t shmem_file_write_iter(struct kiocb *iocb, struct iov_iter *from) 3452 { 3453 struct file *file = iocb->ki_filp; 3454 struct inode *inode = file->f_mapping->host; 3455 ssize_t ret; 3456 3457 inode_lock(inode); 3458 ret = generic_write_checks(iocb, from); 3459 if (ret <= 0) 3460 goto unlock; 3461 ret = file_remove_privs(file); 3462 if (ret) 3463 goto unlock; 3464 ret = file_update_time(file); 3465 if (ret) 3466 goto unlock; 3467 ret = generic_perform_write(iocb, from); 3468 unlock: 3469 inode_unlock(inode); 3470 return ret; 3471 } 3472 3473 static bool zero_pipe_buf_get(struct pipe_inode_info *pipe, 3474 struct pipe_buffer *buf) 3475 { 3476 return true; 3477 } 3478 3479 static void zero_pipe_buf_release(struct pipe_inode_info *pipe, 3480 struct pipe_buffer *buf) 3481 { 3482 } 3483 3484 static bool zero_pipe_buf_try_steal(struct pipe_inode_info *pipe, 3485 struct pipe_buffer *buf) 3486 { 3487 return false; 3488 } 3489 3490 static const struct pipe_buf_operations zero_pipe_buf_ops = { 3491 .release = zero_pipe_buf_release, 3492 .try_steal = zero_pipe_buf_try_steal, 3493 .get = zero_pipe_buf_get, 3494 }; 3495 3496 static size_t splice_zeropage_into_pipe(struct pipe_inode_info *pipe, 3497 loff_t fpos, size_t size) 3498 { 3499 size_t offset = fpos & ~PAGE_MASK; 3500 3501 size = min_t(size_t, size, PAGE_SIZE - offset); 3502 3503 if (!pipe_is_full(pipe)) { 3504 struct pipe_buffer *buf = pipe_head_buf(pipe); 3505 3506 *buf = (struct pipe_buffer) { 3507 .ops = &zero_pipe_buf_ops, 3508 .page = ZERO_PAGE(0), 3509 .offset = offset, 3510 .len = size, 3511 }; 3512 pipe->head++; 3513 } 3514 3515 return size; 3516 } 3517 3518 static ssize_t shmem_file_splice_read(struct file *in, loff_t *ppos, 3519 struct pipe_inode_info *pipe, 3520 size_t len, unsigned int flags) 3521 { 3522 struct inode *inode = file_inode(in); 3523 struct address_space *mapping = inode->i_mapping; 3524 struct folio *folio = NULL; 3525 size_t total_spliced = 0, used, npages, n, part; 3526 loff_t isize; 3527 int error = 0; 3528 3529 /* Work out how much data we can actually add into the pipe */ 3530 used = pipe_buf_usage(pipe); 3531 npages = max_t(ssize_t, pipe->max_usage - used, 0); 3532 len = min_t(size_t, len, npages * PAGE_SIZE); 3533 3534 do { 3535 bool fallback_page_splice = false; 3536 struct page *page = NULL; 3537 pgoff_t index; 3538 size_t size; 3539 3540 if (*ppos >= i_size_read(inode)) 3541 break; 3542 3543 index = *ppos >> PAGE_SHIFT; 3544 error = shmem_get_folio(inode, index, 0, &folio, SGP_READ); 3545 if (error) { 3546 if (error == -EINVAL) 3547 error = 0; 3548 break; 3549 } 3550 if (folio) { 3551 folio_unlock(folio); 3552 3553 page = folio_file_page(folio, index); 3554 if (PageHWPoison(page)) { 3555 error = -EIO; 3556 break; 3557 } 3558 3559 if (folio_test_large(folio) && 3560 folio_test_has_hwpoisoned(folio)) 3561 fallback_page_splice = true; 3562 } 3563 3564 /* 3565 * i_size must be checked after we know the pages are Uptodate. 3566 * 3567 * Checking i_size after the check allows us to calculate 3568 * the correct value for "nr", which means the zero-filled 3569 * part of the page is not copied back to userspace (unless 3570 * another truncate extends the file - this is desired though). 3571 */ 3572 isize = i_size_read(inode); 3573 if (unlikely(*ppos >= isize)) 3574 break; 3575 /* 3576 * Fallback to PAGE_SIZE splice if the large folio has hwpoisoned 3577 * pages. 3578 */ 3579 size = len; 3580 if (unlikely(fallback_page_splice)) { 3581 size_t offset = *ppos & ~PAGE_MASK; 3582 3583 size = umin(size, PAGE_SIZE - offset); 3584 } 3585 part = min_t(loff_t, isize - *ppos, size); 3586 3587 if (folio) { 3588 /* 3589 * If users can be writing to this page using arbitrary 3590 * virtual addresses, take care about potential aliasing 3591 * before reading the page on the kernel side. 3592 */ 3593 if (mapping_writably_mapped(mapping)) { 3594 if (likely(!fallback_page_splice)) 3595 flush_dcache_folio(folio); 3596 else 3597 flush_dcache_page(page); 3598 } 3599 folio_mark_accessed(folio); 3600 /* 3601 * Ok, we have the page, and it's up-to-date, so we can 3602 * now splice it into the pipe. 3603 */ 3604 n = splice_folio_into_pipe(pipe, folio, *ppos, part); 3605 folio_put(folio); 3606 folio = NULL; 3607 } else { 3608 n = splice_zeropage_into_pipe(pipe, *ppos, part); 3609 } 3610 3611 if (!n) 3612 break; 3613 len -= n; 3614 total_spliced += n; 3615 *ppos += n; 3616 in->f_ra.prev_pos = *ppos; 3617 if (pipe_is_full(pipe)) 3618 break; 3619 3620 cond_resched(); 3621 } while (len); 3622 3623 if (folio) 3624 folio_put(folio); 3625 3626 file_accessed(in); 3627 return total_spliced ? total_spliced : error; 3628 } 3629 3630 static loff_t shmem_file_llseek(struct file *file, loff_t offset, int whence) 3631 { 3632 struct address_space *mapping = file->f_mapping; 3633 struct inode *inode = mapping->host; 3634 3635 if (whence != SEEK_DATA && whence != SEEK_HOLE) 3636 return generic_file_llseek_size(file, offset, whence, 3637 MAX_LFS_FILESIZE, i_size_read(inode)); 3638 if (offset < 0) 3639 return -ENXIO; 3640 3641 inode_lock(inode); 3642 /* We're holding i_rwsem so we can access i_size directly */ 3643 offset = mapping_seek_hole_data(mapping, offset, inode->i_size, whence); 3644 if (offset >= 0) 3645 offset = vfs_setpos(file, offset, MAX_LFS_FILESIZE); 3646 inode_unlock(inode); 3647 return offset; 3648 } 3649 3650 static long shmem_fallocate(struct file *file, int mode, loff_t offset, 3651 loff_t len) 3652 { 3653 struct inode *inode = file_inode(file); 3654 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 3655 struct shmem_inode_info *info = SHMEM_I(inode); 3656 struct shmem_falloc shmem_falloc; 3657 pgoff_t start, index, end, undo_fallocend; 3658 int error; 3659 3660 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) 3661 return -EOPNOTSUPP; 3662 3663 inode_lock(inode); 3664 3665 if (mode & FALLOC_FL_PUNCH_HOLE) { 3666 struct address_space *mapping = file->f_mapping; 3667 loff_t unmap_start = round_up(offset, PAGE_SIZE); 3668 loff_t unmap_end = round_down(offset + len, PAGE_SIZE) - 1; 3669 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(shmem_falloc_waitq); 3670 3671 /* protected by i_rwsem */ 3672 if (info->seals & (F_SEAL_WRITE | F_SEAL_FUTURE_WRITE)) { 3673 error = -EPERM; 3674 goto out; 3675 } 3676 3677 shmem_falloc.waitq = &shmem_falloc_waitq; 3678 shmem_falloc.start = (u64)unmap_start >> PAGE_SHIFT; 3679 shmem_falloc.next = (unmap_end + 1) >> PAGE_SHIFT; 3680 spin_lock(&inode->i_lock); 3681 inode->i_private = &shmem_falloc; 3682 spin_unlock(&inode->i_lock); 3683 3684 if ((u64)unmap_end > (u64)unmap_start) 3685 unmap_mapping_range(mapping, unmap_start, 3686 1 + unmap_end - unmap_start, 0); 3687 shmem_truncate_range(inode, offset, offset + len - 1); 3688 /* No need to unmap again: hole-punching leaves COWed pages */ 3689 3690 spin_lock(&inode->i_lock); 3691 inode->i_private = NULL; 3692 wake_up_all(&shmem_falloc_waitq); 3693 WARN_ON_ONCE(!list_empty(&shmem_falloc_waitq.head)); 3694 spin_unlock(&inode->i_lock); 3695 error = 0; 3696 goto out; 3697 } 3698 3699 /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */ 3700 error = inode_newsize_ok(inode, offset + len); 3701 if (error) 3702 goto out; 3703 3704 if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) { 3705 error = -EPERM; 3706 goto out; 3707 } 3708 3709 start = offset >> PAGE_SHIFT; 3710 end = (offset + len + PAGE_SIZE - 1) >> PAGE_SHIFT; 3711 /* Try to avoid a swapstorm if len is impossible to satisfy */ 3712 if (sbinfo->max_blocks && end - start > sbinfo->max_blocks) { 3713 error = -ENOSPC; 3714 goto out; 3715 } 3716 3717 shmem_falloc.waitq = NULL; 3718 shmem_falloc.start = start; 3719 shmem_falloc.next = start; 3720 shmem_falloc.nr_falloced = 0; 3721 shmem_falloc.nr_unswapped = 0; 3722 spin_lock(&inode->i_lock); 3723 inode->i_private = &shmem_falloc; 3724 spin_unlock(&inode->i_lock); 3725 3726 /* 3727 * info->fallocend is only relevant when huge pages might be 3728 * involved: to prevent split_huge_page() freeing fallocated 3729 * pages when FALLOC_FL_KEEP_SIZE committed beyond i_size. 3730 */ 3731 undo_fallocend = info->fallocend; 3732 if (info->fallocend < end) 3733 info->fallocend = end; 3734 3735 for (index = start; index < end; ) { 3736 struct folio *folio; 3737 3738 /* 3739 * Check for fatal signal so that we abort early in OOM 3740 * situations. We don't want to abort in case of non-fatal 3741 * signals as large fallocate can take noticeable time and 3742 * e.g. periodic timers may result in fallocate constantly 3743 * restarting. 3744 */ 3745 if (fatal_signal_pending(current)) 3746 error = -EINTR; 3747 else if (shmem_falloc.nr_unswapped > shmem_falloc.nr_falloced) 3748 error = -ENOMEM; 3749 else 3750 error = shmem_get_folio(inode, index, offset + len, 3751 &folio, SGP_FALLOC); 3752 if (error) { 3753 info->fallocend = undo_fallocend; 3754 /* Remove the !uptodate folios we added */ 3755 if (index > start) { 3756 shmem_undo_range(inode, 3757 (loff_t)start << PAGE_SHIFT, 3758 ((loff_t)index << PAGE_SHIFT) - 1, true); 3759 } 3760 goto undone; 3761 } 3762 3763 /* 3764 * Here is a more important optimization than it appears: 3765 * a second SGP_FALLOC on the same large folio will clear it, 3766 * making it uptodate and un-undoable if we fail later. 3767 */ 3768 index = folio_next_index(folio); 3769 /* Beware 32-bit wraparound */ 3770 if (!index) 3771 index--; 3772 3773 /* 3774 * Inform shmem_writeout() how far we have reached. 3775 * No need for lock or barrier: we have the page lock. 3776 */ 3777 if (!folio_test_uptodate(folio)) 3778 shmem_falloc.nr_falloced += index - shmem_falloc.next; 3779 shmem_falloc.next = index; 3780 3781 /* 3782 * If !uptodate, leave it that way so that freeable folios 3783 * can be recognized if we need to rollback on error later. 3784 * But mark it dirty so that memory pressure will swap rather 3785 * than free the folios we are allocating (and SGP_CACHE folios 3786 * might still be clean: we now need to mark those dirty too). 3787 */ 3788 folio_mark_dirty(folio); 3789 folio_unlock(folio); 3790 folio_put(folio); 3791 cond_resched(); 3792 } 3793 3794 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size) 3795 i_size_write(inode, offset + len); 3796 undone: 3797 spin_lock(&inode->i_lock); 3798 inode->i_private = NULL; 3799 spin_unlock(&inode->i_lock); 3800 out: 3801 if (!error) 3802 file_modified(file); 3803 inode_unlock(inode); 3804 return error; 3805 } 3806 3807 static int shmem_statfs(struct dentry *dentry, struct kstatfs *buf) 3808 { 3809 struct shmem_sb_info *sbinfo = SHMEM_SB(dentry->d_sb); 3810 3811 buf->f_type = TMPFS_MAGIC; 3812 buf->f_bsize = PAGE_SIZE; 3813 buf->f_namelen = NAME_MAX; 3814 if (sbinfo->max_blocks) { 3815 buf->f_blocks = sbinfo->max_blocks; 3816 buf->f_bavail = 3817 buf->f_bfree = sbinfo->max_blocks - 3818 percpu_counter_sum(&sbinfo->used_blocks); 3819 } 3820 if (sbinfo->max_inodes) { 3821 buf->f_files = sbinfo->max_inodes; 3822 buf->f_ffree = sbinfo->free_ispace / BOGO_INODE_SIZE; 3823 } 3824 /* else leave those fields 0 like simple_statfs */ 3825 3826 buf->f_fsid = uuid_to_fsid(dentry->d_sb->s_uuid.b); 3827 3828 return 0; 3829 } 3830 3831 /* 3832 * File creation. Allocate an inode, and we're done.. 3833 */ 3834 static int 3835 shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, 3836 struct dentry *dentry, umode_t mode, dev_t dev) 3837 { 3838 struct inode *inode; 3839 int error; 3840 3841 if (!generic_ci_validate_strict_name(dir, &dentry->d_name)) 3842 return -EINVAL; 3843 3844 inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, dev, VM_NORESERVE); 3845 if (IS_ERR(inode)) 3846 return PTR_ERR(inode); 3847 3848 error = simple_acl_create(dir, inode); 3849 if (error) 3850 goto out_iput; 3851 error = security_inode_init_security(inode, dir, &dentry->d_name, 3852 shmem_initxattrs, NULL); 3853 if (error && error != -EOPNOTSUPP) 3854 goto out_iput; 3855 3856 error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 3857 if (error) 3858 goto out_iput; 3859 3860 dir->i_size += BOGO_DIRENT_SIZE; 3861 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 3862 inode_inc_iversion(dir); 3863 3864 if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir)) 3865 d_add(dentry, inode); 3866 else 3867 d_instantiate(dentry, inode); 3868 3869 dget(dentry); /* Extra count - pin the dentry in core */ 3870 return error; 3871 3872 out_iput: 3873 iput(inode); 3874 return error; 3875 } 3876 3877 static int 3878 shmem_tmpfile(struct mnt_idmap *idmap, struct inode *dir, 3879 struct file *file, umode_t mode) 3880 { 3881 struct inode *inode; 3882 int error; 3883 3884 inode = shmem_get_inode(idmap, dir->i_sb, dir, mode, 0, VM_NORESERVE); 3885 if (IS_ERR(inode)) { 3886 error = PTR_ERR(inode); 3887 goto err_out; 3888 } 3889 error = security_inode_init_security(inode, dir, NULL, 3890 shmem_initxattrs, NULL); 3891 if (error && error != -EOPNOTSUPP) 3892 goto out_iput; 3893 error = simple_acl_create(dir, inode); 3894 if (error) 3895 goto out_iput; 3896 d_tmpfile(file, inode); 3897 3898 err_out: 3899 return finish_open_simple(file, error); 3900 out_iput: 3901 iput(inode); 3902 return error; 3903 } 3904 3905 static struct dentry *shmem_mkdir(struct mnt_idmap *idmap, struct inode *dir, 3906 struct dentry *dentry, umode_t mode) 3907 { 3908 int error; 3909 3910 error = shmem_mknod(idmap, dir, dentry, mode | S_IFDIR, 0); 3911 if (error) 3912 return ERR_PTR(error); 3913 inc_nlink(dir); 3914 return NULL; 3915 } 3916 3917 static int shmem_create(struct mnt_idmap *idmap, struct inode *dir, 3918 struct dentry *dentry, umode_t mode, bool excl) 3919 { 3920 return shmem_mknod(idmap, dir, dentry, mode | S_IFREG, 0); 3921 } 3922 3923 /* 3924 * Link a file.. 3925 */ 3926 static int shmem_link(struct dentry *old_dentry, struct inode *dir, 3927 struct dentry *dentry) 3928 { 3929 struct inode *inode = d_inode(old_dentry); 3930 int ret = 0; 3931 3932 /* 3933 * No ordinary (disk based) filesystem counts links as inodes; 3934 * but each new link needs a new dentry, pinning lowmem, and 3935 * tmpfs dentries cannot be pruned until they are unlinked. 3936 * But if an O_TMPFILE file is linked into the tmpfs, the 3937 * first link must skip that, to get the accounting right. 3938 */ 3939 if (inode->i_nlink) { 3940 ret = shmem_reserve_inode(inode->i_sb, NULL); 3941 if (ret) 3942 goto out; 3943 } 3944 3945 ret = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 3946 if (ret) { 3947 if (inode->i_nlink) 3948 shmem_free_inode(inode->i_sb, 0); 3949 goto out; 3950 } 3951 3952 dir->i_size += BOGO_DIRENT_SIZE; 3953 inode_set_mtime_to_ts(dir, 3954 inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode))); 3955 inode_inc_iversion(dir); 3956 inc_nlink(inode); 3957 ihold(inode); /* New dentry reference */ 3958 dget(dentry); /* Extra pinning count for the created dentry */ 3959 if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir)) 3960 d_add(dentry, inode); 3961 else 3962 d_instantiate(dentry, inode); 3963 out: 3964 return ret; 3965 } 3966 3967 static int shmem_unlink(struct inode *dir, struct dentry *dentry) 3968 { 3969 struct inode *inode = d_inode(dentry); 3970 3971 if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) 3972 shmem_free_inode(inode->i_sb, 0); 3973 3974 simple_offset_remove(shmem_get_offset_ctx(dir), dentry); 3975 3976 dir->i_size -= BOGO_DIRENT_SIZE; 3977 inode_set_mtime_to_ts(dir, 3978 inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode))); 3979 inode_inc_iversion(dir); 3980 drop_nlink(inode); 3981 dput(dentry); /* Undo the count from "create" - does all the work */ 3982 3983 /* 3984 * For now, VFS can't deal with case-insensitive negative dentries, so 3985 * we invalidate them 3986 */ 3987 if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir)) 3988 d_invalidate(dentry); 3989 3990 return 0; 3991 } 3992 3993 static int shmem_rmdir(struct inode *dir, struct dentry *dentry) 3994 { 3995 if (!simple_empty(dentry)) 3996 return -ENOTEMPTY; 3997 3998 drop_nlink(d_inode(dentry)); 3999 drop_nlink(dir); 4000 return shmem_unlink(dir, dentry); 4001 } 4002 4003 static int shmem_whiteout(struct mnt_idmap *idmap, 4004 struct inode *old_dir, struct dentry *old_dentry) 4005 { 4006 struct dentry *whiteout; 4007 int error; 4008 4009 whiteout = d_alloc(old_dentry->d_parent, &old_dentry->d_name); 4010 if (!whiteout) 4011 return -ENOMEM; 4012 4013 error = shmem_mknod(idmap, old_dir, whiteout, 4014 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); 4015 dput(whiteout); 4016 if (error) 4017 return error; 4018 4019 /* 4020 * Cheat and hash the whiteout while the old dentry is still in 4021 * place, instead of playing games with FS_RENAME_DOES_D_MOVE. 4022 * 4023 * d_lookup() will consistently find one of them at this point, 4024 * not sure which one, but that isn't even important. 4025 */ 4026 d_rehash(whiteout); 4027 return 0; 4028 } 4029 4030 /* 4031 * The VFS layer already does all the dentry stuff for rename, 4032 * we just have to decrement the usage count for the target if 4033 * it exists so that the VFS layer correctly free's it when it 4034 * gets overwritten. 4035 */ 4036 static int shmem_rename2(struct mnt_idmap *idmap, 4037 struct inode *old_dir, struct dentry *old_dentry, 4038 struct inode *new_dir, struct dentry *new_dentry, 4039 unsigned int flags) 4040 { 4041 struct inode *inode = d_inode(old_dentry); 4042 int they_are_dirs = S_ISDIR(inode->i_mode); 4043 int error; 4044 4045 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) 4046 return -EINVAL; 4047 4048 if (flags & RENAME_EXCHANGE) 4049 return simple_offset_rename_exchange(old_dir, old_dentry, 4050 new_dir, new_dentry); 4051 4052 if (!simple_empty(new_dentry)) 4053 return -ENOTEMPTY; 4054 4055 if (flags & RENAME_WHITEOUT) { 4056 error = shmem_whiteout(idmap, old_dir, old_dentry); 4057 if (error) 4058 return error; 4059 } 4060 4061 error = simple_offset_rename(old_dir, old_dentry, new_dir, new_dentry); 4062 if (error) 4063 return error; 4064 4065 if (d_really_is_positive(new_dentry)) { 4066 (void) shmem_unlink(new_dir, new_dentry); 4067 if (they_are_dirs) { 4068 drop_nlink(d_inode(new_dentry)); 4069 drop_nlink(old_dir); 4070 } 4071 } else if (they_are_dirs) { 4072 drop_nlink(old_dir); 4073 inc_nlink(new_dir); 4074 } 4075 4076 old_dir->i_size -= BOGO_DIRENT_SIZE; 4077 new_dir->i_size += BOGO_DIRENT_SIZE; 4078 simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry); 4079 inode_inc_iversion(old_dir); 4080 inode_inc_iversion(new_dir); 4081 return 0; 4082 } 4083 4084 static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, 4085 struct dentry *dentry, const char *symname) 4086 { 4087 int error; 4088 int len; 4089 struct inode *inode; 4090 struct folio *folio; 4091 char *link; 4092 4093 len = strlen(symname) + 1; 4094 if (len > PAGE_SIZE) 4095 return -ENAMETOOLONG; 4096 4097 inode = shmem_get_inode(idmap, dir->i_sb, dir, S_IFLNK | 0777, 0, 4098 VM_NORESERVE); 4099 if (IS_ERR(inode)) 4100 return PTR_ERR(inode); 4101 4102 error = security_inode_init_security(inode, dir, &dentry->d_name, 4103 shmem_initxattrs, NULL); 4104 if (error && error != -EOPNOTSUPP) 4105 goto out_iput; 4106 4107 error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); 4108 if (error) 4109 goto out_iput; 4110 4111 inode->i_size = len-1; 4112 if (len <= SHORT_SYMLINK_LEN) { 4113 link = kmemdup(symname, len, GFP_KERNEL); 4114 if (!link) { 4115 error = -ENOMEM; 4116 goto out_remove_offset; 4117 } 4118 inode->i_op = &shmem_short_symlink_operations; 4119 inode_set_cached_link(inode, link, len - 1); 4120 } else { 4121 inode_nohighmem(inode); 4122 inode->i_mapping->a_ops = &shmem_aops; 4123 error = shmem_get_folio(inode, 0, 0, &folio, SGP_WRITE); 4124 if (error) 4125 goto out_remove_offset; 4126 inode->i_op = &shmem_symlink_inode_operations; 4127 memcpy(folio_address(folio), symname, len); 4128 folio_mark_uptodate(folio); 4129 folio_mark_dirty(folio); 4130 folio_unlock(folio); 4131 folio_put(folio); 4132 } 4133 dir->i_size += BOGO_DIRENT_SIZE; 4134 inode_set_mtime_to_ts(dir, inode_set_ctime_current(dir)); 4135 inode_inc_iversion(dir); 4136 if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir)) 4137 d_add(dentry, inode); 4138 else 4139 d_instantiate(dentry, inode); 4140 dget(dentry); 4141 return 0; 4142 4143 out_remove_offset: 4144 simple_offset_remove(shmem_get_offset_ctx(dir), dentry); 4145 out_iput: 4146 iput(inode); 4147 return error; 4148 } 4149 4150 static void shmem_put_link(void *arg) 4151 { 4152 folio_mark_accessed(arg); 4153 folio_put(arg); 4154 } 4155 4156 static const char *shmem_get_link(struct dentry *dentry, struct inode *inode, 4157 struct delayed_call *done) 4158 { 4159 struct folio *folio = NULL; 4160 int error; 4161 4162 if (!dentry) { 4163 folio = filemap_get_folio(inode->i_mapping, 0); 4164 if (IS_ERR(folio)) 4165 return ERR_PTR(-ECHILD); 4166 if (PageHWPoison(folio_page(folio, 0)) || 4167 !folio_test_uptodate(folio)) { 4168 folio_put(folio); 4169 return ERR_PTR(-ECHILD); 4170 } 4171 } else { 4172 error = shmem_get_folio(inode, 0, 0, &folio, SGP_READ); 4173 if (error) 4174 return ERR_PTR(error); 4175 if (!folio) 4176 return ERR_PTR(-ECHILD); 4177 if (PageHWPoison(folio_page(folio, 0))) { 4178 folio_unlock(folio); 4179 folio_put(folio); 4180 return ERR_PTR(-ECHILD); 4181 } 4182 folio_unlock(folio); 4183 } 4184 set_delayed_call(done, shmem_put_link, folio); 4185 return folio_address(folio); 4186 } 4187 4188 #ifdef CONFIG_TMPFS_XATTR 4189 4190 static int shmem_fileattr_get(struct dentry *dentry, struct fileattr *fa) 4191 { 4192 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 4193 4194 fileattr_fill_flags(fa, info->fsflags & SHMEM_FL_USER_VISIBLE); 4195 4196 return 0; 4197 } 4198 4199 static int shmem_fileattr_set(struct mnt_idmap *idmap, 4200 struct dentry *dentry, struct fileattr *fa) 4201 { 4202 struct inode *inode = d_inode(dentry); 4203 struct shmem_inode_info *info = SHMEM_I(inode); 4204 int ret, flags; 4205 4206 if (fileattr_has_fsx(fa)) 4207 return -EOPNOTSUPP; 4208 if (fa->flags & ~SHMEM_FL_USER_MODIFIABLE) 4209 return -EOPNOTSUPP; 4210 4211 flags = (info->fsflags & ~SHMEM_FL_USER_MODIFIABLE) | 4212 (fa->flags & SHMEM_FL_USER_MODIFIABLE); 4213 4214 ret = shmem_set_inode_flags(inode, flags, dentry); 4215 4216 if (ret) 4217 return ret; 4218 4219 info->fsflags = flags; 4220 4221 inode_set_ctime_current(inode); 4222 inode_inc_iversion(inode); 4223 return 0; 4224 } 4225 4226 /* 4227 * Superblocks without xattr inode operations may get some security.* xattr 4228 * support from the LSM "for free". As soon as we have any other xattrs 4229 * like ACLs, we also need to implement the security.* handlers at 4230 * filesystem level, though. 4231 */ 4232 4233 /* 4234 * Callback for security_inode_init_security() for acquiring xattrs. 4235 */ 4236 static int shmem_initxattrs(struct inode *inode, 4237 const struct xattr *xattr_array, void *fs_info) 4238 { 4239 struct shmem_inode_info *info = SHMEM_I(inode); 4240 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 4241 const struct xattr *xattr; 4242 struct simple_xattr *new_xattr; 4243 size_t ispace = 0; 4244 size_t len; 4245 4246 if (sbinfo->max_inodes) { 4247 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4248 ispace += simple_xattr_space(xattr->name, 4249 xattr->value_len + XATTR_SECURITY_PREFIX_LEN); 4250 } 4251 if (ispace) { 4252 raw_spin_lock(&sbinfo->stat_lock); 4253 if (sbinfo->free_ispace < ispace) 4254 ispace = 0; 4255 else 4256 sbinfo->free_ispace -= ispace; 4257 raw_spin_unlock(&sbinfo->stat_lock); 4258 if (!ispace) 4259 return -ENOSPC; 4260 } 4261 } 4262 4263 for (xattr = xattr_array; xattr->name != NULL; xattr++) { 4264 new_xattr = simple_xattr_alloc(xattr->value, xattr->value_len); 4265 if (!new_xattr) 4266 break; 4267 4268 len = strlen(xattr->name) + 1; 4269 new_xattr->name = kmalloc(XATTR_SECURITY_PREFIX_LEN + len, 4270 GFP_KERNEL_ACCOUNT); 4271 if (!new_xattr->name) { 4272 kvfree(new_xattr); 4273 break; 4274 } 4275 4276 memcpy(new_xattr->name, XATTR_SECURITY_PREFIX, 4277 XATTR_SECURITY_PREFIX_LEN); 4278 memcpy(new_xattr->name + XATTR_SECURITY_PREFIX_LEN, 4279 xattr->name, len); 4280 4281 simple_xattr_add(&info->xattrs, new_xattr); 4282 } 4283 4284 if (xattr->name != NULL) { 4285 if (ispace) { 4286 raw_spin_lock(&sbinfo->stat_lock); 4287 sbinfo->free_ispace += ispace; 4288 raw_spin_unlock(&sbinfo->stat_lock); 4289 } 4290 simple_xattrs_free(&info->xattrs, NULL); 4291 return -ENOMEM; 4292 } 4293 4294 return 0; 4295 } 4296 4297 static int shmem_xattr_handler_get(const struct xattr_handler *handler, 4298 struct dentry *unused, struct inode *inode, 4299 const char *name, void *buffer, size_t size) 4300 { 4301 struct shmem_inode_info *info = SHMEM_I(inode); 4302 4303 name = xattr_full_name(handler, name); 4304 return simple_xattr_get(&info->xattrs, name, buffer, size); 4305 } 4306 4307 static int shmem_xattr_handler_set(const struct xattr_handler *handler, 4308 struct mnt_idmap *idmap, 4309 struct dentry *unused, struct inode *inode, 4310 const char *name, const void *value, 4311 size_t size, int flags) 4312 { 4313 struct shmem_inode_info *info = SHMEM_I(inode); 4314 struct shmem_sb_info *sbinfo = SHMEM_SB(inode->i_sb); 4315 struct simple_xattr *old_xattr; 4316 size_t ispace = 0; 4317 4318 name = xattr_full_name(handler, name); 4319 if (value && sbinfo->max_inodes) { 4320 ispace = simple_xattr_space(name, size); 4321 raw_spin_lock(&sbinfo->stat_lock); 4322 if (sbinfo->free_ispace < ispace) 4323 ispace = 0; 4324 else 4325 sbinfo->free_ispace -= ispace; 4326 raw_spin_unlock(&sbinfo->stat_lock); 4327 if (!ispace) 4328 return -ENOSPC; 4329 } 4330 4331 old_xattr = simple_xattr_set(&info->xattrs, name, value, size, flags); 4332 if (!IS_ERR(old_xattr)) { 4333 ispace = 0; 4334 if (old_xattr && sbinfo->max_inodes) 4335 ispace = simple_xattr_space(old_xattr->name, 4336 old_xattr->size); 4337 simple_xattr_free(old_xattr); 4338 old_xattr = NULL; 4339 inode_set_ctime_current(inode); 4340 inode_inc_iversion(inode); 4341 } 4342 if (ispace) { 4343 raw_spin_lock(&sbinfo->stat_lock); 4344 sbinfo->free_ispace += ispace; 4345 raw_spin_unlock(&sbinfo->stat_lock); 4346 } 4347 return PTR_ERR(old_xattr); 4348 } 4349 4350 static const struct xattr_handler shmem_security_xattr_handler = { 4351 .prefix = XATTR_SECURITY_PREFIX, 4352 .get = shmem_xattr_handler_get, 4353 .set = shmem_xattr_handler_set, 4354 }; 4355 4356 static const struct xattr_handler shmem_trusted_xattr_handler = { 4357 .prefix = XATTR_TRUSTED_PREFIX, 4358 .get = shmem_xattr_handler_get, 4359 .set = shmem_xattr_handler_set, 4360 }; 4361 4362 static const struct xattr_handler shmem_user_xattr_handler = { 4363 .prefix = XATTR_USER_PREFIX, 4364 .get = shmem_xattr_handler_get, 4365 .set = shmem_xattr_handler_set, 4366 }; 4367 4368 static const struct xattr_handler * const shmem_xattr_handlers[] = { 4369 &shmem_security_xattr_handler, 4370 &shmem_trusted_xattr_handler, 4371 &shmem_user_xattr_handler, 4372 NULL 4373 }; 4374 4375 static ssize_t shmem_listxattr(struct dentry *dentry, char *buffer, size_t size) 4376 { 4377 struct shmem_inode_info *info = SHMEM_I(d_inode(dentry)); 4378 return simple_xattr_list(d_inode(dentry), &info->xattrs, buffer, size); 4379 } 4380 #endif /* CONFIG_TMPFS_XATTR */ 4381 4382 static const struct inode_operations shmem_short_symlink_operations = { 4383 .getattr = shmem_getattr, 4384 .setattr = shmem_setattr, 4385 .get_link = simple_get_link, 4386 #ifdef CONFIG_TMPFS_XATTR 4387 .listxattr = shmem_listxattr, 4388 #endif 4389 }; 4390 4391 static const struct inode_operations shmem_symlink_inode_operations = { 4392 .getattr = shmem_getattr, 4393 .setattr = shmem_setattr, 4394 .get_link = shmem_get_link, 4395 #ifdef CONFIG_TMPFS_XATTR 4396 .listxattr = shmem_listxattr, 4397 #endif 4398 }; 4399 4400 static struct dentry *shmem_get_parent(struct dentry *child) 4401 { 4402 return ERR_PTR(-ESTALE); 4403 } 4404 4405 static int shmem_match(struct inode *ino, void *vfh) 4406 { 4407 __u32 *fh = vfh; 4408 __u64 inum = fh[2]; 4409 inum = (inum << 32) | fh[1]; 4410 return ino->i_ino == inum && fh[0] == ino->i_generation; 4411 } 4412 4413 /* Find any alias of inode, but prefer a hashed alias */ 4414 static struct dentry *shmem_find_alias(struct inode *inode) 4415 { 4416 struct dentry *alias = d_find_alias(inode); 4417 4418 return alias ?: d_find_any_alias(inode); 4419 } 4420 4421 static struct dentry *shmem_fh_to_dentry(struct super_block *sb, 4422 struct fid *fid, int fh_len, int fh_type) 4423 { 4424 struct inode *inode; 4425 struct dentry *dentry = NULL; 4426 u64 inum; 4427 4428 if (fh_len < 3) 4429 return NULL; 4430 4431 inum = fid->raw[2]; 4432 inum = (inum << 32) | fid->raw[1]; 4433 4434 inode = ilookup5(sb, (unsigned long)(inum + fid->raw[0]), 4435 shmem_match, fid->raw); 4436 if (inode) { 4437 dentry = shmem_find_alias(inode); 4438 iput(inode); 4439 } 4440 4441 return dentry; 4442 } 4443 4444 static int shmem_encode_fh(struct inode *inode, __u32 *fh, int *len, 4445 struct inode *parent) 4446 { 4447 if (*len < 3) { 4448 *len = 3; 4449 return FILEID_INVALID; 4450 } 4451 4452 if (inode_unhashed(inode)) { 4453 /* Unfortunately insert_inode_hash is not idempotent, 4454 * so as we hash inodes here rather than at creation 4455 * time, we need a lock to ensure we only try 4456 * to do it once 4457 */ 4458 static DEFINE_SPINLOCK(lock); 4459 spin_lock(&lock); 4460 if (inode_unhashed(inode)) 4461 __insert_inode_hash(inode, 4462 inode->i_ino + inode->i_generation); 4463 spin_unlock(&lock); 4464 } 4465 4466 fh[0] = inode->i_generation; 4467 fh[1] = inode->i_ino; 4468 fh[2] = ((__u64)inode->i_ino) >> 32; 4469 4470 *len = 3; 4471 return 1; 4472 } 4473 4474 static const struct export_operations shmem_export_ops = { 4475 .get_parent = shmem_get_parent, 4476 .encode_fh = shmem_encode_fh, 4477 .fh_to_dentry = shmem_fh_to_dentry, 4478 }; 4479 4480 enum shmem_param { 4481 Opt_gid, 4482 Opt_huge, 4483 Opt_mode, 4484 Opt_mpol, 4485 Opt_nr_blocks, 4486 Opt_nr_inodes, 4487 Opt_size, 4488 Opt_uid, 4489 Opt_inode32, 4490 Opt_inode64, 4491 Opt_noswap, 4492 Opt_quota, 4493 Opt_usrquota, 4494 Opt_grpquota, 4495 Opt_usrquota_block_hardlimit, 4496 Opt_usrquota_inode_hardlimit, 4497 Opt_grpquota_block_hardlimit, 4498 Opt_grpquota_inode_hardlimit, 4499 Opt_casefold_version, 4500 Opt_casefold, 4501 Opt_strict_encoding, 4502 }; 4503 4504 static const struct constant_table shmem_param_enums_huge[] = { 4505 {"never", SHMEM_HUGE_NEVER }, 4506 {"always", SHMEM_HUGE_ALWAYS }, 4507 {"within_size", SHMEM_HUGE_WITHIN_SIZE }, 4508 {"advise", SHMEM_HUGE_ADVISE }, 4509 {} 4510 }; 4511 4512 const struct fs_parameter_spec shmem_fs_parameters[] = { 4513 fsparam_gid ("gid", Opt_gid), 4514 fsparam_enum ("huge", Opt_huge, shmem_param_enums_huge), 4515 fsparam_u32oct("mode", Opt_mode), 4516 fsparam_string("mpol", Opt_mpol), 4517 fsparam_string("nr_blocks", Opt_nr_blocks), 4518 fsparam_string("nr_inodes", Opt_nr_inodes), 4519 fsparam_string("size", Opt_size), 4520 fsparam_uid ("uid", Opt_uid), 4521 fsparam_flag ("inode32", Opt_inode32), 4522 fsparam_flag ("inode64", Opt_inode64), 4523 fsparam_flag ("noswap", Opt_noswap), 4524 #ifdef CONFIG_TMPFS_QUOTA 4525 fsparam_flag ("quota", Opt_quota), 4526 fsparam_flag ("usrquota", Opt_usrquota), 4527 fsparam_flag ("grpquota", Opt_grpquota), 4528 fsparam_string("usrquota_block_hardlimit", Opt_usrquota_block_hardlimit), 4529 fsparam_string("usrquota_inode_hardlimit", Opt_usrquota_inode_hardlimit), 4530 fsparam_string("grpquota_block_hardlimit", Opt_grpquota_block_hardlimit), 4531 fsparam_string("grpquota_inode_hardlimit", Opt_grpquota_inode_hardlimit), 4532 #endif 4533 fsparam_string("casefold", Opt_casefold_version), 4534 fsparam_flag ("casefold", Opt_casefold), 4535 fsparam_flag ("strict_encoding", Opt_strict_encoding), 4536 {} 4537 }; 4538 4539 #if IS_ENABLED(CONFIG_UNICODE) 4540 static int shmem_parse_opt_casefold(struct fs_context *fc, struct fs_parameter *param, 4541 bool latest_version) 4542 { 4543 struct shmem_options *ctx = fc->fs_private; 4544 int version = UTF8_LATEST; 4545 struct unicode_map *encoding; 4546 char *version_str = param->string + 5; 4547 4548 if (!latest_version) { 4549 if (strncmp(param->string, "utf8-", 5)) 4550 return invalfc(fc, "Only UTF-8 encodings are supported " 4551 "in the format: utf8-<version number>"); 4552 4553 version = utf8_parse_version(version_str); 4554 if (version < 0) 4555 return invalfc(fc, "Invalid UTF-8 version: %s", version_str); 4556 } 4557 4558 encoding = utf8_load(version); 4559 4560 if (IS_ERR(encoding)) { 4561 return invalfc(fc, "Failed loading UTF-8 version: utf8-%u.%u.%u\n", 4562 unicode_major(version), unicode_minor(version), 4563 unicode_rev(version)); 4564 } 4565 4566 pr_info("tmpfs: Using encoding : utf8-%u.%u.%u\n", 4567 unicode_major(version), unicode_minor(version), unicode_rev(version)); 4568 4569 ctx->encoding = encoding; 4570 4571 return 0; 4572 } 4573 #else 4574 static int shmem_parse_opt_casefold(struct fs_context *fc, struct fs_parameter *param, 4575 bool latest_version) 4576 { 4577 return invalfc(fc, "tmpfs: Kernel not built with CONFIG_UNICODE\n"); 4578 } 4579 #endif 4580 4581 static int shmem_parse_one(struct fs_context *fc, struct fs_parameter *param) 4582 { 4583 struct shmem_options *ctx = fc->fs_private; 4584 struct fs_parse_result result; 4585 unsigned long long size; 4586 char *rest; 4587 int opt; 4588 kuid_t kuid; 4589 kgid_t kgid; 4590 4591 opt = fs_parse(fc, shmem_fs_parameters, param, &result); 4592 if (opt < 0) 4593 return opt; 4594 4595 switch (opt) { 4596 case Opt_size: 4597 size = memparse(param->string, &rest); 4598 if (*rest == '%') { 4599 size <<= PAGE_SHIFT; 4600 size *= totalram_pages(); 4601 do_div(size, 100); 4602 rest++; 4603 } 4604 if (*rest) 4605 goto bad_value; 4606 ctx->blocks = DIV_ROUND_UP(size, PAGE_SIZE); 4607 ctx->seen |= SHMEM_SEEN_BLOCKS; 4608 break; 4609 case Opt_nr_blocks: 4610 ctx->blocks = memparse(param->string, &rest); 4611 if (*rest || ctx->blocks > LONG_MAX) 4612 goto bad_value; 4613 ctx->seen |= SHMEM_SEEN_BLOCKS; 4614 break; 4615 case Opt_nr_inodes: 4616 ctx->inodes = memparse(param->string, &rest); 4617 if (*rest || ctx->inodes > ULONG_MAX / BOGO_INODE_SIZE) 4618 goto bad_value; 4619 ctx->seen |= SHMEM_SEEN_INODES; 4620 break; 4621 case Opt_mode: 4622 ctx->mode = result.uint_32 & 07777; 4623 break; 4624 case Opt_uid: 4625 kuid = result.uid; 4626 4627 /* 4628 * The requested uid must be representable in the 4629 * filesystem's idmapping. 4630 */ 4631 if (!kuid_has_mapping(fc->user_ns, kuid)) 4632 goto bad_value; 4633 4634 ctx->uid = kuid; 4635 break; 4636 case Opt_gid: 4637 kgid = result.gid; 4638 4639 /* 4640 * The requested gid must be representable in the 4641 * filesystem's idmapping. 4642 */ 4643 if (!kgid_has_mapping(fc->user_ns, kgid)) 4644 goto bad_value; 4645 4646 ctx->gid = kgid; 4647 break; 4648 case Opt_huge: 4649 ctx->huge = result.uint_32; 4650 if (ctx->huge != SHMEM_HUGE_NEVER && 4651 !(IS_ENABLED(CONFIG_TRANSPARENT_HUGEPAGE) && 4652 has_transparent_hugepage())) 4653 goto unsupported_parameter; 4654 ctx->seen |= SHMEM_SEEN_HUGE; 4655 break; 4656 case Opt_mpol: 4657 if (IS_ENABLED(CONFIG_NUMA)) { 4658 mpol_put(ctx->mpol); 4659 ctx->mpol = NULL; 4660 if (mpol_parse_str(param->string, &ctx->mpol)) 4661 goto bad_value; 4662 break; 4663 } 4664 goto unsupported_parameter; 4665 case Opt_inode32: 4666 ctx->full_inums = false; 4667 ctx->seen |= SHMEM_SEEN_INUMS; 4668 break; 4669 case Opt_inode64: 4670 if (sizeof(ino_t) < 8) { 4671 return invalfc(fc, 4672 "Cannot use inode64 with <64bit inums in kernel\n"); 4673 } 4674 ctx->full_inums = true; 4675 ctx->seen |= SHMEM_SEEN_INUMS; 4676 break; 4677 case Opt_noswap: 4678 if ((fc->user_ns != &init_user_ns) || !capable(CAP_SYS_ADMIN)) { 4679 return invalfc(fc, 4680 "Turning off swap in unprivileged tmpfs mounts unsupported"); 4681 } 4682 ctx->noswap = true; 4683 ctx->seen |= SHMEM_SEEN_NOSWAP; 4684 break; 4685 case Opt_quota: 4686 if (fc->user_ns != &init_user_ns) 4687 return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 4688 ctx->seen |= SHMEM_SEEN_QUOTA; 4689 ctx->quota_types |= (QTYPE_MASK_USR | QTYPE_MASK_GRP); 4690 break; 4691 case Opt_usrquota: 4692 if (fc->user_ns != &init_user_ns) 4693 return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 4694 ctx->seen |= SHMEM_SEEN_QUOTA; 4695 ctx->quota_types |= QTYPE_MASK_USR; 4696 break; 4697 case Opt_grpquota: 4698 if (fc->user_ns != &init_user_ns) 4699 return invalfc(fc, "Quotas in unprivileged tmpfs mounts are unsupported"); 4700 ctx->seen |= SHMEM_SEEN_QUOTA; 4701 ctx->quota_types |= QTYPE_MASK_GRP; 4702 break; 4703 case Opt_usrquota_block_hardlimit: 4704 size = memparse(param->string, &rest); 4705 if (*rest || !size) 4706 goto bad_value; 4707 if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) 4708 return invalfc(fc, 4709 "User quota block hardlimit too large."); 4710 ctx->qlimits.usrquota_bhardlimit = size; 4711 break; 4712 case Opt_grpquota_block_hardlimit: 4713 size = memparse(param->string, &rest); 4714 if (*rest || !size) 4715 goto bad_value; 4716 if (size > SHMEM_QUOTA_MAX_SPC_LIMIT) 4717 return invalfc(fc, 4718 "Group quota block hardlimit too large."); 4719 ctx->qlimits.grpquota_bhardlimit = size; 4720 break; 4721 case Opt_usrquota_inode_hardlimit: 4722 size = memparse(param->string, &rest); 4723 if (*rest || !size) 4724 goto bad_value; 4725 if (size > SHMEM_QUOTA_MAX_INO_LIMIT) 4726 return invalfc(fc, 4727 "User quota inode hardlimit too large."); 4728 ctx->qlimits.usrquota_ihardlimit = size; 4729 break; 4730 case Opt_grpquota_inode_hardlimit: 4731 size = memparse(param->string, &rest); 4732 if (*rest || !size) 4733 goto bad_value; 4734 if (size > SHMEM_QUOTA_MAX_INO_LIMIT) 4735 return invalfc(fc, 4736 "Group quota inode hardlimit too large."); 4737 ctx->qlimits.grpquota_ihardlimit = size; 4738 break; 4739 case Opt_casefold_version: 4740 return shmem_parse_opt_casefold(fc, param, false); 4741 case Opt_casefold: 4742 return shmem_parse_opt_casefold(fc, param, true); 4743 case Opt_strict_encoding: 4744 #if IS_ENABLED(CONFIG_UNICODE) 4745 ctx->strict_encoding = true; 4746 break; 4747 #else 4748 return invalfc(fc, "tmpfs: Kernel not built with CONFIG_UNICODE\n"); 4749 #endif 4750 } 4751 return 0; 4752 4753 unsupported_parameter: 4754 return invalfc(fc, "Unsupported parameter '%s'", param->key); 4755 bad_value: 4756 return invalfc(fc, "Bad value for '%s'", param->key); 4757 } 4758 4759 static char *shmem_next_opt(char **s) 4760 { 4761 char *sbegin = *s; 4762 char *p; 4763 4764 if (sbegin == NULL) 4765 return NULL; 4766 4767 /* 4768 * NUL-terminate this option: unfortunately, 4769 * mount options form a comma-separated list, 4770 * but mpol's nodelist may also contain commas. 4771 */ 4772 for (;;) { 4773 p = strchr(*s, ','); 4774 if (p == NULL) 4775 break; 4776 *s = p + 1; 4777 if (!isdigit(*(p+1))) { 4778 *p = '\0'; 4779 return sbegin; 4780 } 4781 } 4782 4783 *s = NULL; 4784 return sbegin; 4785 } 4786 4787 static int shmem_parse_monolithic(struct fs_context *fc, void *data) 4788 { 4789 return vfs_parse_monolithic_sep(fc, data, shmem_next_opt); 4790 } 4791 4792 /* 4793 * Reconfigure a shmem filesystem. 4794 */ 4795 static int shmem_reconfigure(struct fs_context *fc) 4796 { 4797 struct shmem_options *ctx = fc->fs_private; 4798 struct shmem_sb_info *sbinfo = SHMEM_SB(fc->root->d_sb); 4799 unsigned long used_isp; 4800 struct mempolicy *mpol = NULL; 4801 const char *err; 4802 4803 raw_spin_lock(&sbinfo->stat_lock); 4804 used_isp = sbinfo->max_inodes * BOGO_INODE_SIZE - sbinfo->free_ispace; 4805 4806 if ((ctx->seen & SHMEM_SEEN_BLOCKS) && ctx->blocks) { 4807 if (!sbinfo->max_blocks) { 4808 err = "Cannot retroactively limit size"; 4809 goto out; 4810 } 4811 if (percpu_counter_compare(&sbinfo->used_blocks, 4812 ctx->blocks) > 0) { 4813 err = "Too small a size for current use"; 4814 goto out; 4815 } 4816 } 4817 if ((ctx->seen & SHMEM_SEEN_INODES) && ctx->inodes) { 4818 if (!sbinfo->max_inodes) { 4819 err = "Cannot retroactively limit inodes"; 4820 goto out; 4821 } 4822 if (ctx->inodes * BOGO_INODE_SIZE < used_isp) { 4823 err = "Too few inodes for current use"; 4824 goto out; 4825 } 4826 } 4827 4828 if ((ctx->seen & SHMEM_SEEN_INUMS) && !ctx->full_inums && 4829 sbinfo->next_ino > UINT_MAX) { 4830 err = "Current inum too high to switch to 32-bit inums"; 4831 goto out; 4832 } 4833 if ((ctx->seen & SHMEM_SEEN_NOSWAP) && ctx->noswap && !sbinfo->noswap) { 4834 err = "Cannot disable swap on remount"; 4835 goto out; 4836 } 4837 if (!(ctx->seen & SHMEM_SEEN_NOSWAP) && !ctx->noswap && sbinfo->noswap) { 4838 err = "Cannot enable swap on remount if it was disabled on first mount"; 4839 goto out; 4840 } 4841 4842 if (ctx->seen & SHMEM_SEEN_QUOTA && 4843 !sb_any_quota_loaded(fc->root->d_sb)) { 4844 err = "Cannot enable quota on remount"; 4845 goto out; 4846 } 4847 4848 #ifdef CONFIG_TMPFS_QUOTA 4849 #define CHANGED_LIMIT(name) \ 4850 (ctx->qlimits.name## hardlimit && \ 4851 (ctx->qlimits.name## hardlimit != sbinfo->qlimits.name## hardlimit)) 4852 4853 if (CHANGED_LIMIT(usrquota_b) || CHANGED_LIMIT(usrquota_i) || 4854 CHANGED_LIMIT(grpquota_b) || CHANGED_LIMIT(grpquota_i)) { 4855 err = "Cannot change global quota limit on remount"; 4856 goto out; 4857 } 4858 #endif /* CONFIG_TMPFS_QUOTA */ 4859 4860 if (ctx->seen & SHMEM_SEEN_HUGE) 4861 sbinfo->huge = ctx->huge; 4862 if (ctx->seen & SHMEM_SEEN_INUMS) 4863 sbinfo->full_inums = ctx->full_inums; 4864 if (ctx->seen & SHMEM_SEEN_BLOCKS) 4865 sbinfo->max_blocks = ctx->blocks; 4866 if (ctx->seen & SHMEM_SEEN_INODES) { 4867 sbinfo->max_inodes = ctx->inodes; 4868 sbinfo->free_ispace = ctx->inodes * BOGO_INODE_SIZE - used_isp; 4869 } 4870 4871 /* 4872 * Preserve previous mempolicy unless mpol remount option was specified. 4873 */ 4874 if (ctx->mpol) { 4875 mpol = sbinfo->mpol; 4876 sbinfo->mpol = ctx->mpol; /* transfers initial ref */ 4877 ctx->mpol = NULL; 4878 } 4879 4880 if (ctx->noswap) 4881 sbinfo->noswap = true; 4882 4883 raw_spin_unlock(&sbinfo->stat_lock); 4884 mpol_put(mpol); 4885 return 0; 4886 out: 4887 raw_spin_unlock(&sbinfo->stat_lock); 4888 return invalfc(fc, "%s", err); 4889 } 4890 4891 static int shmem_show_options(struct seq_file *seq, struct dentry *root) 4892 { 4893 struct shmem_sb_info *sbinfo = SHMEM_SB(root->d_sb); 4894 struct mempolicy *mpol; 4895 4896 if (sbinfo->max_blocks != shmem_default_max_blocks()) 4897 seq_printf(seq, ",size=%luk", K(sbinfo->max_blocks)); 4898 if (sbinfo->max_inodes != shmem_default_max_inodes()) 4899 seq_printf(seq, ",nr_inodes=%lu", sbinfo->max_inodes); 4900 if (sbinfo->mode != (0777 | S_ISVTX)) 4901 seq_printf(seq, ",mode=%03ho", sbinfo->mode); 4902 if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID)) 4903 seq_printf(seq, ",uid=%u", 4904 from_kuid_munged(&init_user_ns, sbinfo->uid)); 4905 if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID)) 4906 seq_printf(seq, ",gid=%u", 4907 from_kgid_munged(&init_user_ns, sbinfo->gid)); 4908 4909 /* 4910 * Showing inode{64,32} might be useful even if it's the system default, 4911 * since then people don't have to resort to checking both here and 4912 * /proc/config.gz to confirm 64-bit inums were successfully applied 4913 * (which may not even exist if IKCONFIG_PROC isn't enabled). 4914 * 4915 * We hide it when inode64 isn't the default and we are using 32-bit 4916 * inodes, since that probably just means the feature isn't even under 4917 * consideration. 4918 * 4919 * As such: 4920 * 4921 * +-----------------+-----------------+ 4922 * | TMPFS_INODE64=y | TMPFS_INODE64=n | 4923 * +------------------+-----------------+-----------------+ 4924 * | full_inums=true | show | show | 4925 * | full_inums=false | show | hide | 4926 * +------------------+-----------------+-----------------+ 4927 * 4928 */ 4929 if (IS_ENABLED(CONFIG_TMPFS_INODE64) || sbinfo->full_inums) 4930 seq_printf(seq, ",inode%d", (sbinfo->full_inums ? 64 : 32)); 4931 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 4932 /* Rightly or wrongly, show huge mount option unmasked by shmem_huge */ 4933 if (sbinfo->huge) 4934 seq_printf(seq, ",huge=%s", shmem_format_huge(sbinfo->huge)); 4935 #endif 4936 mpol = shmem_get_sbmpol(sbinfo); 4937 shmem_show_mpol(seq, mpol); 4938 mpol_put(mpol); 4939 if (sbinfo->noswap) 4940 seq_printf(seq, ",noswap"); 4941 #ifdef CONFIG_TMPFS_QUOTA 4942 if (sb_has_quota_active(root->d_sb, USRQUOTA)) 4943 seq_printf(seq, ",usrquota"); 4944 if (sb_has_quota_active(root->d_sb, GRPQUOTA)) 4945 seq_printf(seq, ",grpquota"); 4946 if (sbinfo->qlimits.usrquota_bhardlimit) 4947 seq_printf(seq, ",usrquota_block_hardlimit=%lld", 4948 sbinfo->qlimits.usrquota_bhardlimit); 4949 if (sbinfo->qlimits.grpquota_bhardlimit) 4950 seq_printf(seq, ",grpquota_block_hardlimit=%lld", 4951 sbinfo->qlimits.grpquota_bhardlimit); 4952 if (sbinfo->qlimits.usrquota_ihardlimit) 4953 seq_printf(seq, ",usrquota_inode_hardlimit=%lld", 4954 sbinfo->qlimits.usrquota_ihardlimit); 4955 if (sbinfo->qlimits.grpquota_ihardlimit) 4956 seq_printf(seq, ",grpquota_inode_hardlimit=%lld", 4957 sbinfo->qlimits.grpquota_ihardlimit); 4958 #endif 4959 return 0; 4960 } 4961 4962 #endif /* CONFIG_TMPFS */ 4963 4964 static void shmem_put_super(struct super_block *sb) 4965 { 4966 struct shmem_sb_info *sbinfo = SHMEM_SB(sb); 4967 4968 #if IS_ENABLED(CONFIG_UNICODE) 4969 if (sb->s_encoding) 4970 utf8_unload(sb->s_encoding); 4971 #endif 4972 4973 #ifdef CONFIG_TMPFS_QUOTA 4974 shmem_disable_quotas(sb); 4975 #endif 4976 free_percpu(sbinfo->ino_batch); 4977 percpu_counter_destroy(&sbinfo->used_blocks); 4978 mpol_put(sbinfo->mpol); 4979 kfree(sbinfo); 4980 sb->s_fs_info = NULL; 4981 } 4982 4983 #if IS_ENABLED(CONFIG_UNICODE) && defined(CONFIG_TMPFS) 4984 static const struct dentry_operations shmem_ci_dentry_ops = { 4985 .d_hash = generic_ci_d_hash, 4986 .d_compare = generic_ci_d_compare, 4987 .d_delete = always_delete_dentry, 4988 }; 4989 #endif 4990 4991 static int shmem_fill_super(struct super_block *sb, struct fs_context *fc) 4992 { 4993 struct shmem_options *ctx = fc->fs_private; 4994 struct inode *inode; 4995 struct shmem_sb_info *sbinfo; 4996 int error = -ENOMEM; 4997 4998 /* Round up to L1_CACHE_BYTES to resist false sharing */ 4999 sbinfo = kzalloc(max((int)sizeof(struct shmem_sb_info), 5000 L1_CACHE_BYTES), GFP_KERNEL); 5001 if (!sbinfo) 5002 return error; 5003 5004 sb->s_fs_info = sbinfo; 5005 5006 #ifdef CONFIG_TMPFS 5007 /* 5008 * Per default we only allow half of the physical ram per 5009 * tmpfs instance, limiting inodes to one per page of lowmem; 5010 * but the internal instance is left unlimited. 5011 */ 5012 if (!(sb->s_flags & SB_KERNMOUNT)) { 5013 if (!(ctx->seen & SHMEM_SEEN_BLOCKS)) 5014 ctx->blocks = shmem_default_max_blocks(); 5015 if (!(ctx->seen & SHMEM_SEEN_INODES)) 5016 ctx->inodes = shmem_default_max_inodes(); 5017 if (!(ctx->seen & SHMEM_SEEN_INUMS)) 5018 ctx->full_inums = IS_ENABLED(CONFIG_TMPFS_INODE64); 5019 sbinfo->noswap = ctx->noswap; 5020 } else { 5021 sb->s_flags |= SB_NOUSER; 5022 } 5023 sb->s_export_op = &shmem_export_ops; 5024 sb->s_flags |= SB_NOSEC | SB_I_VERSION; 5025 5026 #if IS_ENABLED(CONFIG_UNICODE) 5027 if (!ctx->encoding && ctx->strict_encoding) { 5028 pr_err("tmpfs: strict_encoding option without encoding is forbidden\n"); 5029 error = -EINVAL; 5030 goto failed; 5031 } 5032 5033 if (ctx->encoding) { 5034 sb->s_encoding = ctx->encoding; 5035 sb->s_d_op = &shmem_ci_dentry_ops; 5036 if (ctx->strict_encoding) 5037 sb->s_encoding_flags = SB_ENC_STRICT_MODE_FL; 5038 } 5039 #endif 5040 5041 #else 5042 sb->s_flags |= SB_NOUSER; 5043 #endif /* CONFIG_TMPFS */ 5044 sbinfo->max_blocks = ctx->blocks; 5045 sbinfo->max_inodes = ctx->inodes; 5046 sbinfo->free_ispace = sbinfo->max_inodes * BOGO_INODE_SIZE; 5047 if (sb->s_flags & SB_KERNMOUNT) { 5048 sbinfo->ino_batch = alloc_percpu(ino_t); 5049 if (!sbinfo->ino_batch) 5050 goto failed; 5051 } 5052 sbinfo->uid = ctx->uid; 5053 sbinfo->gid = ctx->gid; 5054 sbinfo->full_inums = ctx->full_inums; 5055 sbinfo->mode = ctx->mode; 5056 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 5057 if (ctx->seen & SHMEM_SEEN_HUGE) 5058 sbinfo->huge = ctx->huge; 5059 else 5060 sbinfo->huge = tmpfs_huge; 5061 #endif 5062 sbinfo->mpol = ctx->mpol; 5063 ctx->mpol = NULL; 5064 5065 raw_spin_lock_init(&sbinfo->stat_lock); 5066 if (percpu_counter_init(&sbinfo->used_blocks, 0, GFP_KERNEL)) 5067 goto failed; 5068 spin_lock_init(&sbinfo->shrinklist_lock); 5069 INIT_LIST_HEAD(&sbinfo->shrinklist); 5070 5071 sb->s_maxbytes = MAX_LFS_FILESIZE; 5072 sb->s_blocksize = PAGE_SIZE; 5073 sb->s_blocksize_bits = PAGE_SHIFT; 5074 sb->s_magic = TMPFS_MAGIC; 5075 sb->s_op = &shmem_ops; 5076 sb->s_time_gran = 1; 5077 #ifdef CONFIG_TMPFS_XATTR 5078 sb->s_xattr = shmem_xattr_handlers; 5079 #endif 5080 #ifdef CONFIG_TMPFS_POSIX_ACL 5081 sb->s_flags |= SB_POSIXACL; 5082 #endif 5083 uuid_t uuid; 5084 uuid_gen(&uuid); 5085 super_set_uuid(sb, uuid.b, sizeof(uuid)); 5086 5087 #ifdef CONFIG_TMPFS_QUOTA 5088 if (ctx->seen & SHMEM_SEEN_QUOTA) { 5089 sb->dq_op = &shmem_quota_operations; 5090 sb->s_qcop = &dquot_quotactl_sysfile_ops; 5091 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP; 5092 5093 /* Copy the default limits from ctx into sbinfo */ 5094 memcpy(&sbinfo->qlimits, &ctx->qlimits, 5095 sizeof(struct shmem_quota_limits)); 5096 5097 if (shmem_enable_quotas(sb, ctx->quota_types)) 5098 goto failed; 5099 } 5100 #endif /* CONFIG_TMPFS_QUOTA */ 5101 5102 inode = shmem_get_inode(&nop_mnt_idmap, sb, NULL, 5103 S_IFDIR | sbinfo->mode, 0, VM_NORESERVE); 5104 if (IS_ERR(inode)) { 5105 error = PTR_ERR(inode); 5106 goto failed; 5107 } 5108 inode->i_uid = sbinfo->uid; 5109 inode->i_gid = sbinfo->gid; 5110 sb->s_root = d_make_root(inode); 5111 if (!sb->s_root) 5112 goto failed; 5113 return 0; 5114 5115 failed: 5116 shmem_put_super(sb); 5117 return error; 5118 } 5119 5120 static int shmem_get_tree(struct fs_context *fc) 5121 { 5122 return get_tree_nodev(fc, shmem_fill_super); 5123 } 5124 5125 static void shmem_free_fc(struct fs_context *fc) 5126 { 5127 struct shmem_options *ctx = fc->fs_private; 5128 5129 if (ctx) { 5130 mpol_put(ctx->mpol); 5131 kfree(ctx); 5132 } 5133 } 5134 5135 static const struct fs_context_operations shmem_fs_context_ops = { 5136 .free = shmem_free_fc, 5137 .get_tree = shmem_get_tree, 5138 #ifdef CONFIG_TMPFS 5139 .parse_monolithic = shmem_parse_monolithic, 5140 .parse_param = shmem_parse_one, 5141 .reconfigure = shmem_reconfigure, 5142 #endif 5143 }; 5144 5145 static struct kmem_cache *shmem_inode_cachep __ro_after_init; 5146 5147 static struct inode *shmem_alloc_inode(struct super_block *sb) 5148 { 5149 struct shmem_inode_info *info; 5150 info = alloc_inode_sb(sb, shmem_inode_cachep, GFP_KERNEL); 5151 if (!info) 5152 return NULL; 5153 return &info->vfs_inode; 5154 } 5155 5156 static void shmem_free_in_core_inode(struct inode *inode) 5157 { 5158 if (S_ISLNK(inode->i_mode)) 5159 kfree(inode->i_link); 5160 kmem_cache_free(shmem_inode_cachep, SHMEM_I(inode)); 5161 } 5162 5163 static void shmem_destroy_inode(struct inode *inode) 5164 { 5165 if (S_ISREG(inode->i_mode)) 5166 mpol_free_shared_policy(&SHMEM_I(inode)->policy); 5167 if (S_ISDIR(inode->i_mode)) 5168 simple_offset_destroy(shmem_get_offset_ctx(inode)); 5169 } 5170 5171 static void shmem_init_inode(void *foo) 5172 { 5173 struct shmem_inode_info *info = foo; 5174 inode_init_once(&info->vfs_inode); 5175 } 5176 5177 static void __init shmem_init_inodecache(void) 5178 { 5179 shmem_inode_cachep = kmem_cache_create("shmem_inode_cache", 5180 sizeof(struct shmem_inode_info), 5181 0, SLAB_PANIC|SLAB_ACCOUNT, shmem_init_inode); 5182 } 5183 5184 static void __init shmem_destroy_inodecache(void) 5185 { 5186 kmem_cache_destroy(shmem_inode_cachep); 5187 } 5188 5189 /* Keep the page in page cache instead of truncating it */ 5190 static int shmem_error_remove_folio(struct address_space *mapping, 5191 struct folio *folio) 5192 { 5193 return 0; 5194 } 5195 5196 static const struct address_space_operations shmem_aops = { 5197 .dirty_folio = noop_dirty_folio, 5198 #ifdef CONFIG_TMPFS 5199 .write_begin = shmem_write_begin, 5200 .write_end = shmem_write_end, 5201 #endif 5202 #ifdef CONFIG_MIGRATION 5203 .migrate_folio = migrate_folio, 5204 #endif 5205 .error_remove_folio = shmem_error_remove_folio, 5206 }; 5207 5208 static const struct file_operations shmem_file_operations = { 5209 .mmap = shmem_mmap, 5210 .open = shmem_file_open, 5211 .get_unmapped_area = shmem_get_unmapped_area, 5212 #ifdef CONFIG_TMPFS 5213 .llseek = shmem_file_llseek, 5214 .read_iter = shmem_file_read_iter, 5215 .write_iter = shmem_file_write_iter, 5216 .fsync = noop_fsync, 5217 .splice_read = shmem_file_splice_read, 5218 .splice_write = iter_file_splice_write, 5219 .fallocate = shmem_fallocate, 5220 #endif 5221 }; 5222 5223 static const struct inode_operations shmem_inode_operations = { 5224 .getattr = shmem_getattr, 5225 .setattr = shmem_setattr, 5226 #ifdef CONFIG_TMPFS_XATTR 5227 .listxattr = shmem_listxattr, 5228 .set_acl = simple_set_acl, 5229 .fileattr_get = shmem_fileattr_get, 5230 .fileattr_set = shmem_fileattr_set, 5231 #endif 5232 }; 5233 5234 static const struct inode_operations shmem_dir_inode_operations = { 5235 #ifdef CONFIG_TMPFS 5236 .getattr = shmem_getattr, 5237 .create = shmem_create, 5238 .lookup = simple_lookup, 5239 .link = shmem_link, 5240 .unlink = shmem_unlink, 5241 .symlink = shmem_symlink, 5242 .mkdir = shmem_mkdir, 5243 .rmdir = shmem_rmdir, 5244 .mknod = shmem_mknod, 5245 .rename = shmem_rename2, 5246 .tmpfile = shmem_tmpfile, 5247 .get_offset_ctx = shmem_get_offset_ctx, 5248 #endif 5249 #ifdef CONFIG_TMPFS_XATTR 5250 .listxattr = shmem_listxattr, 5251 .fileattr_get = shmem_fileattr_get, 5252 .fileattr_set = shmem_fileattr_set, 5253 #endif 5254 #ifdef CONFIG_TMPFS_POSIX_ACL 5255 .setattr = shmem_setattr, 5256 .set_acl = simple_set_acl, 5257 #endif 5258 }; 5259 5260 static const struct inode_operations shmem_special_inode_operations = { 5261 .getattr = shmem_getattr, 5262 #ifdef CONFIG_TMPFS_XATTR 5263 .listxattr = shmem_listxattr, 5264 #endif 5265 #ifdef CONFIG_TMPFS_POSIX_ACL 5266 .setattr = shmem_setattr, 5267 .set_acl = simple_set_acl, 5268 #endif 5269 }; 5270 5271 static const struct super_operations shmem_ops = { 5272 .alloc_inode = shmem_alloc_inode, 5273 .free_inode = shmem_free_in_core_inode, 5274 .destroy_inode = shmem_destroy_inode, 5275 #ifdef CONFIG_TMPFS 5276 .statfs = shmem_statfs, 5277 .show_options = shmem_show_options, 5278 #endif 5279 #ifdef CONFIG_TMPFS_QUOTA 5280 .get_dquots = shmem_get_dquots, 5281 #endif 5282 .evict_inode = shmem_evict_inode, 5283 .drop_inode = generic_delete_inode, 5284 .put_super = shmem_put_super, 5285 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 5286 .nr_cached_objects = shmem_unused_huge_count, 5287 .free_cached_objects = shmem_unused_huge_scan, 5288 #endif 5289 }; 5290 5291 static const struct vm_operations_struct shmem_vm_ops = { 5292 .fault = shmem_fault, 5293 .map_pages = filemap_map_pages, 5294 #ifdef CONFIG_NUMA 5295 .set_policy = shmem_set_policy, 5296 .get_policy = shmem_get_policy, 5297 #endif 5298 }; 5299 5300 static const struct vm_operations_struct shmem_anon_vm_ops = { 5301 .fault = shmem_fault, 5302 .map_pages = filemap_map_pages, 5303 #ifdef CONFIG_NUMA 5304 .set_policy = shmem_set_policy, 5305 .get_policy = shmem_get_policy, 5306 #endif 5307 }; 5308 5309 int shmem_init_fs_context(struct fs_context *fc) 5310 { 5311 struct shmem_options *ctx; 5312 5313 ctx = kzalloc(sizeof(struct shmem_options), GFP_KERNEL); 5314 if (!ctx) 5315 return -ENOMEM; 5316 5317 ctx->mode = 0777 | S_ISVTX; 5318 ctx->uid = current_fsuid(); 5319 ctx->gid = current_fsgid(); 5320 5321 #if IS_ENABLED(CONFIG_UNICODE) 5322 ctx->encoding = NULL; 5323 #endif 5324 5325 fc->fs_private = ctx; 5326 fc->ops = &shmem_fs_context_ops; 5327 return 0; 5328 } 5329 5330 static struct file_system_type shmem_fs_type = { 5331 .owner = THIS_MODULE, 5332 .name = "tmpfs", 5333 .init_fs_context = shmem_init_fs_context, 5334 #ifdef CONFIG_TMPFS 5335 .parameters = shmem_fs_parameters, 5336 #endif 5337 .kill_sb = kill_litter_super, 5338 .fs_flags = FS_USERNS_MOUNT | FS_ALLOW_IDMAP | FS_MGTIME, 5339 }; 5340 5341 #if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS) 5342 5343 #define __INIT_KOBJ_ATTR(_name, _mode, _show, _store) \ 5344 { \ 5345 .attr = { .name = __stringify(_name), .mode = _mode }, \ 5346 .show = _show, \ 5347 .store = _store, \ 5348 } 5349 5350 #define TMPFS_ATTR_W(_name, _store) \ 5351 static struct kobj_attribute tmpfs_attr_##_name = \ 5352 __INIT_KOBJ_ATTR(_name, 0200, NULL, _store) 5353 5354 #define TMPFS_ATTR_RW(_name, _show, _store) \ 5355 static struct kobj_attribute tmpfs_attr_##_name = \ 5356 __INIT_KOBJ_ATTR(_name, 0644, _show, _store) 5357 5358 #define TMPFS_ATTR_RO(_name, _show) \ 5359 static struct kobj_attribute tmpfs_attr_##_name = \ 5360 __INIT_KOBJ_ATTR(_name, 0444, _show, NULL) 5361 5362 #if IS_ENABLED(CONFIG_UNICODE) 5363 static ssize_t casefold_show(struct kobject *kobj, struct kobj_attribute *a, 5364 char *buf) 5365 { 5366 return sysfs_emit(buf, "supported\n"); 5367 } 5368 TMPFS_ATTR_RO(casefold, casefold_show); 5369 #endif 5370 5371 static struct attribute *tmpfs_attributes[] = { 5372 #if IS_ENABLED(CONFIG_UNICODE) 5373 &tmpfs_attr_casefold.attr, 5374 #endif 5375 NULL 5376 }; 5377 5378 static const struct attribute_group tmpfs_attribute_group = { 5379 .attrs = tmpfs_attributes, 5380 .name = "features" 5381 }; 5382 5383 static struct kobject *tmpfs_kobj; 5384 5385 static int __init tmpfs_sysfs_init(void) 5386 { 5387 int ret; 5388 5389 tmpfs_kobj = kobject_create_and_add("tmpfs", fs_kobj); 5390 if (!tmpfs_kobj) 5391 return -ENOMEM; 5392 5393 ret = sysfs_create_group(tmpfs_kobj, &tmpfs_attribute_group); 5394 if (ret) 5395 kobject_put(tmpfs_kobj); 5396 5397 return ret; 5398 } 5399 #endif /* CONFIG_SYSFS && CONFIG_TMPFS */ 5400 5401 void __init shmem_init(void) 5402 { 5403 int error; 5404 5405 shmem_init_inodecache(); 5406 5407 #ifdef CONFIG_TMPFS_QUOTA 5408 register_quota_format(&shmem_quota_format); 5409 #endif 5410 5411 error = register_filesystem(&shmem_fs_type); 5412 if (error) { 5413 pr_err("Could not register tmpfs\n"); 5414 goto out2; 5415 } 5416 5417 shm_mnt = kern_mount(&shmem_fs_type); 5418 if (IS_ERR(shm_mnt)) { 5419 error = PTR_ERR(shm_mnt); 5420 pr_err("Could not kern_mount tmpfs\n"); 5421 goto out1; 5422 } 5423 5424 #if defined(CONFIG_SYSFS) && defined(CONFIG_TMPFS) 5425 error = tmpfs_sysfs_init(); 5426 if (error) { 5427 pr_err("Could not init tmpfs sysfs\n"); 5428 goto out1; 5429 } 5430 #endif 5431 5432 #ifdef CONFIG_TRANSPARENT_HUGEPAGE 5433 if (has_transparent_hugepage() && shmem_huge > SHMEM_HUGE_DENY) 5434 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 5435 else 5436 shmem_huge = SHMEM_HUGE_NEVER; /* just in case it was patched */ 5437 5438 /* 5439 * Default to setting PMD-sized THP to inherit the global setting and 5440 * disable all other multi-size THPs. 5441 */ 5442 if (!shmem_orders_configured) 5443 huge_shmem_orders_inherit = BIT(HPAGE_PMD_ORDER); 5444 #endif 5445 return; 5446 5447 out1: 5448 unregister_filesystem(&shmem_fs_type); 5449 out2: 5450 #ifdef CONFIG_TMPFS_QUOTA 5451 unregister_quota_format(&shmem_quota_format); 5452 #endif 5453 shmem_destroy_inodecache(); 5454 shm_mnt = ERR_PTR(error); 5455 } 5456 5457 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && defined(CONFIG_SYSFS) 5458 static ssize_t shmem_enabled_show(struct kobject *kobj, 5459 struct kobj_attribute *attr, char *buf) 5460 { 5461 static const int values[] = { 5462 SHMEM_HUGE_ALWAYS, 5463 SHMEM_HUGE_WITHIN_SIZE, 5464 SHMEM_HUGE_ADVISE, 5465 SHMEM_HUGE_NEVER, 5466 SHMEM_HUGE_DENY, 5467 SHMEM_HUGE_FORCE, 5468 }; 5469 int len = 0; 5470 int i; 5471 5472 for (i = 0; i < ARRAY_SIZE(values); i++) { 5473 len += sysfs_emit_at(buf, len, 5474 shmem_huge == values[i] ? "%s[%s]" : "%s%s", 5475 i ? " " : "", shmem_format_huge(values[i])); 5476 } 5477 len += sysfs_emit_at(buf, len, "\n"); 5478 5479 return len; 5480 } 5481 5482 static ssize_t shmem_enabled_store(struct kobject *kobj, 5483 struct kobj_attribute *attr, const char *buf, size_t count) 5484 { 5485 char tmp[16]; 5486 int huge, err; 5487 5488 if (count + 1 > sizeof(tmp)) 5489 return -EINVAL; 5490 memcpy(tmp, buf, count); 5491 tmp[count] = '\0'; 5492 if (count && tmp[count - 1] == '\n') 5493 tmp[count - 1] = '\0'; 5494 5495 huge = shmem_parse_huge(tmp); 5496 if (huge == -EINVAL) 5497 return huge; 5498 5499 shmem_huge = huge; 5500 if (shmem_huge > SHMEM_HUGE_DENY) 5501 SHMEM_SB(shm_mnt->mnt_sb)->huge = shmem_huge; 5502 5503 err = start_stop_khugepaged(); 5504 return err ? err : count; 5505 } 5506 5507 struct kobj_attribute shmem_enabled_attr = __ATTR_RW(shmem_enabled); 5508 static DEFINE_SPINLOCK(huge_shmem_orders_lock); 5509 5510 static ssize_t thpsize_shmem_enabled_show(struct kobject *kobj, 5511 struct kobj_attribute *attr, char *buf) 5512 { 5513 int order = to_thpsize(kobj)->order; 5514 const char *output; 5515 5516 if (test_bit(order, &huge_shmem_orders_always)) 5517 output = "[always] inherit within_size advise never"; 5518 else if (test_bit(order, &huge_shmem_orders_inherit)) 5519 output = "always [inherit] within_size advise never"; 5520 else if (test_bit(order, &huge_shmem_orders_within_size)) 5521 output = "always inherit [within_size] advise never"; 5522 else if (test_bit(order, &huge_shmem_orders_madvise)) 5523 output = "always inherit within_size [advise] never"; 5524 else 5525 output = "always inherit within_size advise [never]"; 5526 5527 return sysfs_emit(buf, "%s\n", output); 5528 } 5529 5530 static ssize_t thpsize_shmem_enabled_store(struct kobject *kobj, 5531 struct kobj_attribute *attr, 5532 const char *buf, size_t count) 5533 { 5534 int order = to_thpsize(kobj)->order; 5535 ssize_t ret = count; 5536 5537 if (sysfs_streq(buf, "always")) { 5538 spin_lock(&huge_shmem_orders_lock); 5539 clear_bit(order, &huge_shmem_orders_inherit); 5540 clear_bit(order, &huge_shmem_orders_madvise); 5541 clear_bit(order, &huge_shmem_orders_within_size); 5542 set_bit(order, &huge_shmem_orders_always); 5543 spin_unlock(&huge_shmem_orders_lock); 5544 } else if (sysfs_streq(buf, "inherit")) { 5545 /* Do not override huge allocation policy with non-PMD sized mTHP */ 5546 if (shmem_huge == SHMEM_HUGE_FORCE && 5547 order != HPAGE_PMD_ORDER) 5548 return -EINVAL; 5549 5550 spin_lock(&huge_shmem_orders_lock); 5551 clear_bit(order, &huge_shmem_orders_always); 5552 clear_bit(order, &huge_shmem_orders_madvise); 5553 clear_bit(order, &huge_shmem_orders_within_size); 5554 set_bit(order, &huge_shmem_orders_inherit); 5555 spin_unlock(&huge_shmem_orders_lock); 5556 } else if (sysfs_streq(buf, "within_size")) { 5557 spin_lock(&huge_shmem_orders_lock); 5558 clear_bit(order, &huge_shmem_orders_always); 5559 clear_bit(order, &huge_shmem_orders_inherit); 5560 clear_bit(order, &huge_shmem_orders_madvise); 5561 set_bit(order, &huge_shmem_orders_within_size); 5562 spin_unlock(&huge_shmem_orders_lock); 5563 } else if (sysfs_streq(buf, "advise")) { 5564 spin_lock(&huge_shmem_orders_lock); 5565 clear_bit(order, &huge_shmem_orders_always); 5566 clear_bit(order, &huge_shmem_orders_inherit); 5567 clear_bit(order, &huge_shmem_orders_within_size); 5568 set_bit(order, &huge_shmem_orders_madvise); 5569 spin_unlock(&huge_shmem_orders_lock); 5570 } else if (sysfs_streq(buf, "never")) { 5571 spin_lock(&huge_shmem_orders_lock); 5572 clear_bit(order, &huge_shmem_orders_always); 5573 clear_bit(order, &huge_shmem_orders_inherit); 5574 clear_bit(order, &huge_shmem_orders_within_size); 5575 clear_bit(order, &huge_shmem_orders_madvise); 5576 spin_unlock(&huge_shmem_orders_lock); 5577 } else { 5578 ret = -EINVAL; 5579 } 5580 5581 if (ret > 0) { 5582 int err = start_stop_khugepaged(); 5583 5584 if (err) 5585 ret = err; 5586 } 5587 return ret; 5588 } 5589 5590 struct kobj_attribute thpsize_shmem_enabled_attr = 5591 __ATTR(shmem_enabled, 0644, thpsize_shmem_enabled_show, thpsize_shmem_enabled_store); 5592 #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ 5593 5594 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) 5595 5596 static int __init setup_transparent_hugepage_shmem(char *str) 5597 { 5598 int huge; 5599 5600 huge = shmem_parse_huge(str); 5601 if (huge == -EINVAL) { 5602 pr_warn("transparent_hugepage_shmem= cannot parse, ignored\n"); 5603 return huge; 5604 } 5605 5606 shmem_huge = huge; 5607 return 1; 5608 } 5609 __setup("transparent_hugepage_shmem=", setup_transparent_hugepage_shmem); 5610 5611 static int __init setup_transparent_hugepage_tmpfs(char *str) 5612 { 5613 int huge; 5614 5615 huge = shmem_parse_huge(str); 5616 if (huge < 0) { 5617 pr_warn("transparent_hugepage_tmpfs= cannot parse, ignored\n"); 5618 return huge; 5619 } 5620 5621 tmpfs_huge = huge; 5622 return 1; 5623 } 5624 __setup("transparent_hugepage_tmpfs=", setup_transparent_hugepage_tmpfs); 5625 5626 static char str_dup[PAGE_SIZE] __initdata; 5627 static int __init setup_thp_shmem(char *str) 5628 { 5629 char *token, *range, *policy, *subtoken; 5630 unsigned long always, inherit, madvise, within_size; 5631 char *start_size, *end_size; 5632 int start, end, nr; 5633 char *p; 5634 5635 if (!str || strlen(str) + 1 > PAGE_SIZE) 5636 goto err; 5637 strscpy(str_dup, str); 5638 5639 always = huge_shmem_orders_always; 5640 inherit = huge_shmem_orders_inherit; 5641 madvise = huge_shmem_orders_madvise; 5642 within_size = huge_shmem_orders_within_size; 5643 p = str_dup; 5644 while ((token = strsep(&p, ";")) != NULL) { 5645 range = strsep(&token, ":"); 5646 policy = token; 5647 5648 if (!policy) 5649 goto err; 5650 5651 while ((subtoken = strsep(&range, ",")) != NULL) { 5652 if (strchr(subtoken, '-')) { 5653 start_size = strsep(&subtoken, "-"); 5654 end_size = subtoken; 5655 5656 start = get_order_from_str(start_size, 5657 THP_ORDERS_ALL_FILE_DEFAULT); 5658 end = get_order_from_str(end_size, 5659 THP_ORDERS_ALL_FILE_DEFAULT); 5660 } else { 5661 start_size = end_size = subtoken; 5662 start = end = get_order_from_str(subtoken, 5663 THP_ORDERS_ALL_FILE_DEFAULT); 5664 } 5665 5666 if (start < 0) { 5667 pr_err("invalid size %s in thp_shmem boot parameter\n", 5668 start_size); 5669 goto err; 5670 } 5671 5672 if (end < 0) { 5673 pr_err("invalid size %s in thp_shmem boot parameter\n", 5674 end_size); 5675 goto err; 5676 } 5677 5678 if (start > end) 5679 goto err; 5680 5681 nr = end - start + 1; 5682 if (!strcmp(policy, "always")) { 5683 bitmap_set(&always, start, nr); 5684 bitmap_clear(&inherit, start, nr); 5685 bitmap_clear(&madvise, start, nr); 5686 bitmap_clear(&within_size, start, nr); 5687 } else if (!strcmp(policy, "advise")) { 5688 bitmap_set(&madvise, start, nr); 5689 bitmap_clear(&inherit, start, nr); 5690 bitmap_clear(&always, start, nr); 5691 bitmap_clear(&within_size, start, nr); 5692 } else if (!strcmp(policy, "inherit")) { 5693 bitmap_set(&inherit, start, nr); 5694 bitmap_clear(&madvise, start, nr); 5695 bitmap_clear(&always, start, nr); 5696 bitmap_clear(&within_size, start, nr); 5697 } else if (!strcmp(policy, "within_size")) { 5698 bitmap_set(&within_size, start, nr); 5699 bitmap_clear(&inherit, start, nr); 5700 bitmap_clear(&madvise, start, nr); 5701 bitmap_clear(&always, start, nr); 5702 } else if (!strcmp(policy, "never")) { 5703 bitmap_clear(&inherit, start, nr); 5704 bitmap_clear(&madvise, start, nr); 5705 bitmap_clear(&always, start, nr); 5706 bitmap_clear(&within_size, start, nr); 5707 } else { 5708 pr_err("invalid policy %s in thp_shmem boot parameter\n", policy); 5709 goto err; 5710 } 5711 } 5712 } 5713 5714 huge_shmem_orders_always = always; 5715 huge_shmem_orders_madvise = madvise; 5716 huge_shmem_orders_inherit = inherit; 5717 huge_shmem_orders_within_size = within_size; 5718 shmem_orders_configured = true; 5719 return 1; 5720 5721 err: 5722 pr_warn("thp_shmem=%s: error parsing string, ignoring setting\n", str); 5723 return 0; 5724 } 5725 __setup("thp_shmem=", setup_thp_shmem); 5726 5727 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ 5728 5729 #else /* !CONFIG_SHMEM */ 5730 5731 /* 5732 * tiny-shmem: simple shmemfs and tmpfs using ramfs code 5733 * 5734 * This is intended for small system where the benefits of the full 5735 * shmem code (swap-backed and resource-limited) are outweighed by 5736 * their complexity. On systems without swap this code should be 5737 * effectively equivalent, but much lighter weight. 5738 */ 5739 5740 static struct file_system_type shmem_fs_type = { 5741 .name = "tmpfs", 5742 .init_fs_context = ramfs_init_fs_context, 5743 .parameters = ramfs_fs_parameters, 5744 .kill_sb = ramfs_kill_sb, 5745 .fs_flags = FS_USERNS_MOUNT, 5746 }; 5747 5748 void __init shmem_init(void) 5749 { 5750 BUG_ON(register_filesystem(&shmem_fs_type) != 0); 5751 5752 shm_mnt = kern_mount(&shmem_fs_type); 5753 BUG_ON(IS_ERR(shm_mnt)); 5754 } 5755 5756 int shmem_unuse(unsigned int type) 5757 { 5758 return 0; 5759 } 5760 5761 int shmem_lock(struct file *file, int lock, struct ucounts *ucounts) 5762 { 5763 return 0; 5764 } 5765 5766 void shmem_unlock_mapping(struct address_space *mapping) 5767 { 5768 } 5769 5770 #ifdef CONFIG_MMU 5771 unsigned long shmem_get_unmapped_area(struct file *file, 5772 unsigned long addr, unsigned long len, 5773 unsigned long pgoff, unsigned long flags) 5774 { 5775 return mm_get_unmapped_area(current->mm, file, addr, len, pgoff, flags); 5776 } 5777 #endif 5778 5779 void shmem_truncate_range(struct inode *inode, loff_t lstart, loff_t lend) 5780 { 5781 truncate_inode_pages_range(inode->i_mapping, lstart, lend); 5782 } 5783 EXPORT_SYMBOL_GPL(shmem_truncate_range); 5784 5785 #define shmem_vm_ops generic_file_vm_ops 5786 #define shmem_anon_vm_ops generic_file_vm_ops 5787 #define shmem_file_operations ramfs_file_operations 5788 #define shmem_acct_size(flags, size) 0 5789 #define shmem_unacct_size(flags, size) do {} while (0) 5790 5791 static inline struct inode *shmem_get_inode(struct mnt_idmap *idmap, 5792 struct super_block *sb, struct inode *dir, 5793 umode_t mode, dev_t dev, unsigned long flags) 5794 { 5795 struct inode *inode = ramfs_get_inode(sb, dir, mode, dev); 5796 return inode ? inode : ERR_PTR(-ENOSPC); 5797 } 5798 5799 #endif /* CONFIG_SHMEM */ 5800 5801 /* common code */ 5802 5803 static struct file *__shmem_file_setup(struct vfsmount *mnt, const char *name, 5804 loff_t size, unsigned long flags, unsigned int i_flags) 5805 { 5806 struct inode *inode; 5807 struct file *res; 5808 5809 if (IS_ERR(mnt)) 5810 return ERR_CAST(mnt); 5811 5812 if (size < 0 || size > MAX_LFS_FILESIZE) 5813 return ERR_PTR(-EINVAL); 5814 5815 if (is_idmapped_mnt(mnt)) 5816 return ERR_PTR(-EINVAL); 5817 5818 if (shmem_acct_size(flags, size)) 5819 return ERR_PTR(-ENOMEM); 5820 5821 inode = shmem_get_inode(&nop_mnt_idmap, mnt->mnt_sb, NULL, 5822 S_IFREG | S_IRWXUGO, 0, flags); 5823 if (IS_ERR(inode)) { 5824 shmem_unacct_size(flags, size); 5825 return ERR_CAST(inode); 5826 } 5827 inode->i_flags |= i_flags; 5828 inode->i_size = size; 5829 clear_nlink(inode); /* It is unlinked */ 5830 res = ERR_PTR(ramfs_nommu_expand_for_mapping(inode, size)); 5831 if (!IS_ERR(res)) 5832 res = alloc_file_pseudo(inode, mnt, name, O_RDWR, 5833 &shmem_file_operations); 5834 if (IS_ERR(res)) 5835 iput(inode); 5836 return res; 5837 } 5838 5839 /** 5840 * shmem_kernel_file_setup - get an unlinked file living in tmpfs which must be 5841 * kernel internal. There will be NO LSM permission checks against the 5842 * underlying inode. So users of this interface must do LSM checks at a 5843 * higher layer. The users are the big_key and shm implementations. LSM 5844 * checks are provided at the key or shm level rather than the inode. 5845 * @name: name for dentry (to be seen in /proc/<pid>/maps) 5846 * @size: size to be set for the file 5847 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 5848 */ 5849 struct file *shmem_kernel_file_setup(const char *name, loff_t size, unsigned long flags) 5850 { 5851 return __shmem_file_setup(shm_mnt, name, size, flags, S_PRIVATE); 5852 } 5853 EXPORT_SYMBOL_GPL(shmem_kernel_file_setup); 5854 5855 /** 5856 * shmem_file_setup - get an unlinked file living in tmpfs 5857 * @name: name for dentry (to be seen in /proc/<pid>/maps) 5858 * @size: size to be set for the file 5859 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 5860 */ 5861 struct file *shmem_file_setup(const char *name, loff_t size, unsigned long flags) 5862 { 5863 return __shmem_file_setup(shm_mnt, name, size, flags, 0); 5864 } 5865 EXPORT_SYMBOL_GPL(shmem_file_setup); 5866 5867 /** 5868 * shmem_file_setup_with_mnt - get an unlinked file living in tmpfs 5869 * @mnt: the tmpfs mount where the file will be created 5870 * @name: name for dentry (to be seen in /proc/<pid>/maps) 5871 * @size: size to be set for the file 5872 * @flags: VM_NORESERVE suppresses pre-accounting of the entire object size 5873 */ 5874 struct file *shmem_file_setup_with_mnt(struct vfsmount *mnt, const char *name, 5875 loff_t size, unsigned long flags) 5876 { 5877 return __shmem_file_setup(mnt, name, size, flags, 0); 5878 } 5879 EXPORT_SYMBOL_GPL(shmem_file_setup_with_mnt); 5880 5881 /** 5882 * shmem_zero_setup - setup a shared anonymous mapping 5883 * @vma: the vma to be mmapped is prepared by do_mmap 5884 */ 5885 int shmem_zero_setup(struct vm_area_struct *vma) 5886 { 5887 struct file *file; 5888 loff_t size = vma->vm_end - vma->vm_start; 5889 5890 /* 5891 * Cloning a new file under mmap_lock leads to a lock ordering conflict 5892 * between XFS directory reading and selinux: since this file is only 5893 * accessible to the user through its mapping, use S_PRIVATE flag to 5894 * bypass file security, in the same way as shmem_kernel_file_setup(). 5895 */ 5896 file = shmem_kernel_file_setup("dev/zero", size, vma->vm_flags); 5897 if (IS_ERR(file)) 5898 return PTR_ERR(file); 5899 5900 if (vma->vm_file) 5901 fput(vma->vm_file); 5902 vma->vm_file = file; 5903 vma->vm_ops = &shmem_anon_vm_ops; 5904 5905 return 0; 5906 } 5907 5908 /** 5909 * shmem_read_folio_gfp - read into page cache, using specified page allocation flags. 5910 * @mapping: the folio's address_space 5911 * @index: the folio index 5912 * @gfp: the page allocator flags to use if allocating 5913 * 5914 * This behaves as a tmpfs "read_cache_page_gfp(mapping, index, gfp)", 5915 * with any new page allocations done using the specified allocation flags. 5916 * But read_cache_page_gfp() uses the ->read_folio() method: which does not 5917 * suit tmpfs, since it may have pages in swapcache, and needs to find those 5918 * for itself; although drivers/gpu/drm i915 and ttm rely upon this support. 5919 * 5920 * i915_gem_object_get_pages_gtt() mixes __GFP_NORETRY | __GFP_NOWARN in 5921 * with the mapping_gfp_mask(), to avoid OOMing the machine unnecessarily. 5922 */ 5923 struct folio *shmem_read_folio_gfp(struct address_space *mapping, 5924 pgoff_t index, gfp_t gfp) 5925 { 5926 #ifdef CONFIG_SHMEM 5927 struct inode *inode = mapping->host; 5928 struct folio *folio; 5929 int error; 5930 5931 error = shmem_get_folio_gfp(inode, index, 0, &folio, SGP_CACHE, 5932 gfp, NULL, NULL); 5933 if (error) 5934 return ERR_PTR(error); 5935 5936 folio_unlock(folio); 5937 return folio; 5938 #else 5939 /* 5940 * The tiny !SHMEM case uses ramfs without swap 5941 */ 5942 return mapping_read_folio_gfp(mapping, index, gfp); 5943 #endif 5944 } 5945 EXPORT_SYMBOL_GPL(shmem_read_folio_gfp); 5946 5947 struct page *shmem_read_mapping_page_gfp(struct address_space *mapping, 5948 pgoff_t index, gfp_t gfp) 5949 { 5950 struct folio *folio = shmem_read_folio_gfp(mapping, index, gfp); 5951 struct page *page; 5952 5953 if (IS_ERR(folio)) 5954 return &folio->page; 5955 5956 page = folio_file_page(folio, index); 5957 if (PageHWPoison(page)) { 5958 folio_put(folio); 5959 return ERR_PTR(-EIO); 5960 } 5961 5962 return page; 5963 } 5964 EXPORT_SYMBOL_GPL(shmem_read_mapping_page_gfp); 5965