1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * fs/dax.c - Direct Access filesystem code 4 * Copyright (c) 2013-2014 Intel Corporation 5 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com> 6 * Author: Ross Zwisler <ross.zwisler@linux.intel.com> 7 */ 8 9 #include <linux/atomic.h> 10 #include <linux/blkdev.h> 11 #include <linux/buffer_head.h> 12 #include <linux/dax.h> 13 #include <linux/fs.h> 14 #include <linux/highmem.h> 15 #include <linux/memcontrol.h> 16 #include <linux/mm.h> 17 #include <linux/mutex.h> 18 #include <linux/pagevec.h> 19 #include <linux/sched.h> 20 #include <linux/sched/signal.h> 21 #include <linux/uio.h> 22 #include <linux/vmstat.h> 23 #include <linux/pfn_t.h> 24 #include <linux/sizes.h> 25 #include <linux/mmu_notifier.h> 26 #include <linux/iomap.h> 27 #include <linux/rmap.h> 28 #include <asm/pgalloc.h> 29 30 #define CREATE_TRACE_POINTS 31 #include <trace/events/fs_dax.h> 32 33 /* We choose 4096 entries - same as per-zone page wait tables */ 34 #define DAX_WAIT_TABLE_BITS 12 35 #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 36 37 /* The 'colour' (ie low bits) within a PMD of a page offset. */ 38 #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 39 #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) 40 41 static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 42 43 static int __init init_dax_wait_table(void) 44 { 45 int i; 46 47 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 48 init_waitqueue_head(wait_table + i); 49 return 0; 50 } 51 fs_initcall(init_dax_wait_table); 52 53 /* 54 * DAX pagecache entries use XArray value entries so they can't be mistaken 55 * for pages. We use one bit for locking, one bit for the entry size (PMD) 56 * and two more to tell us if the entry is a zero page or an empty entry that 57 * is just used for locking. In total four special bits. 58 * 59 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE 60 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem 61 * block allocation. 62 */ 63 #define DAX_SHIFT (4) 64 #define DAX_LOCKED (1UL << 0) 65 #define DAX_PMD (1UL << 1) 66 #define DAX_ZERO_PAGE (1UL << 2) 67 #define DAX_EMPTY (1UL << 3) 68 69 static unsigned long dax_to_pfn(void *entry) 70 { 71 return xa_to_value(entry) >> DAX_SHIFT; 72 } 73 74 static struct folio *dax_to_folio(void *entry) 75 { 76 return page_folio(pfn_to_page(dax_to_pfn(entry))); 77 } 78 79 static void *dax_make_entry(pfn_t pfn, unsigned long flags) 80 { 81 return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT)); 82 } 83 84 static bool dax_is_locked(void *entry) 85 { 86 return xa_to_value(entry) & DAX_LOCKED; 87 } 88 89 static unsigned int dax_entry_order(void *entry) 90 { 91 if (xa_to_value(entry) & DAX_PMD) 92 return PMD_ORDER; 93 return 0; 94 } 95 96 static unsigned long dax_is_pmd_entry(void *entry) 97 { 98 return xa_to_value(entry) & DAX_PMD; 99 } 100 101 static bool dax_is_pte_entry(void *entry) 102 { 103 return !(xa_to_value(entry) & DAX_PMD); 104 } 105 106 static int dax_is_zero_entry(void *entry) 107 { 108 return xa_to_value(entry) & DAX_ZERO_PAGE; 109 } 110 111 static int dax_is_empty_entry(void *entry) 112 { 113 return xa_to_value(entry) & DAX_EMPTY; 114 } 115 116 /* 117 * true if the entry that was found is of a smaller order than the entry 118 * we were looking for 119 */ 120 static bool dax_is_conflict(void *entry) 121 { 122 return entry == XA_RETRY_ENTRY; 123 } 124 125 /* 126 * DAX page cache entry locking 127 */ 128 struct exceptional_entry_key { 129 struct xarray *xa; 130 pgoff_t entry_start; 131 }; 132 133 struct wait_exceptional_entry_queue { 134 wait_queue_entry_t wait; 135 struct exceptional_entry_key key; 136 }; 137 138 /** 139 * enum dax_wake_mode: waitqueue wakeup behaviour 140 * @WAKE_ALL: wake all waiters in the waitqueue 141 * @WAKE_NEXT: wake only the first waiter in the waitqueue 142 */ 143 enum dax_wake_mode { 144 WAKE_ALL, 145 WAKE_NEXT, 146 }; 147 148 static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas, 149 void *entry, struct exceptional_entry_key *key) 150 { 151 unsigned long hash; 152 unsigned long index = xas->xa_index; 153 154 /* 155 * If 'entry' is a PMD, align the 'index' that we use for the wait 156 * queue to the start of that PMD. This ensures that all offsets in 157 * the range covered by the PMD map to the same bit lock. 158 */ 159 if (dax_is_pmd_entry(entry)) 160 index &= ~PG_PMD_COLOUR; 161 key->xa = xas->xa; 162 key->entry_start = index; 163 164 hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS); 165 return wait_table + hash; 166 } 167 168 static int wake_exceptional_entry_func(wait_queue_entry_t *wait, 169 unsigned int mode, int sync, void *keyp) 170 { 171 struct exceptional_entry_key *key = keyp; 172 struct wait_exceptional_entry_queue *ewait = 173 container_of(wait, struct wait_exceptional_entry_queue, wait); 174 175 if (key->xa != ewait->key.xa || 176 key->entry_start != ewait->key.entry_start) 177 return 0; 178 return autoremove_wake_function(wait, mode, sync, NULL); 179 } 180 181 /* 182 * @entry may no longer be the entry at the index in the mapping. 183 * The important information it's conveying is whether the entry at 184 * this index used to be a PMD entry. 185 */ 186 static void dax_wake_entry(struct xa_state *xas, void *entry, 187 enum dax_wake_mode mode) 188 { 189 struct exceptional_entry_key key; 190 wait_queue_head_t *wq; 191 192 wq = dax_entry_waitqueue(xas, entry, &key); 193 194 /* 195 * Checking for locked entry and prepare_to_wait_exclusive() happens 196 * under the i_pages lock, ditto for entry handling in our callers. 197 * So at this point all tasks that could have seen our entry locked 198 * must be in the waitqueue and the following check will see them. 199 */ 200 if (waitqueue_active(wq)) 201 __wake_up(wq, TASK_NORMAL, mode == WAKE_ALL ? 0 : 1, &key); 202 } 203 204 /* 205 * Look up entry in page cache, wait for it to become unlocked if it 206 * is a DAX entry and return it. The caller must subsequently call 207 * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry() 208 * if it did. The entry returned may have a larger order than @order. 209 * If @order is larger than the order of the entry found in i_pages, this 210 * function returns a dax_is_conflict entry. 211 * 212 * Must be called with the i_pages lock held. 213 */ 214 static void *get_next_unlocked_entry(struct xa_state *xas, unsigned int order) 215 { 216 void *entry; 217 struct wait_exceptional_entry_queue ewait; 218 wait_queue_head_t *wq; 219 220 init_wait(&ewait.wait); 221 ewait.wait.func = wake_exceptional_entry_func; 222 223 for (;;) { 224 entry = xas_find_conflict(xas); 225 if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 226 return entry; 227 if (dax_entry_order(entry) < order) 228 return XA_RETRY_ENTRY; 229 if (!dax_is_locked(entry)) 230 return entry; 231 232 wq = dax_entry_waitqueue(xas, entry, &ewait.key); 233 prepare_to_wait_exclusive(wq, &ewait.wait, 234 TASK_UNINTERRUPTIBLE); 235 xas_unlock_irq(xas); 236 xas_reset(xas); 237 schedule(); 238 finish_wait(wq, &ewait.wait); 239 xas_lock_irq(xas); 240 } 241 } 242 243 /* 244 * Wait for the given entry to become unlocked. Caller must hold the i_pages 245 * lock and call either put_unlocked_entry() if it did not lock the entry or 246 * dax_unlock_entry() if it did. Returns an unlocked entry if still present. 247 */ 248 static void *wait_entry_unlocked_exclusive(struct xa_state *xas, void *entry) 249 { 250 struct wait_exceptional_entry_queue ewait; 251 wait_queue_head_t *wq; 252 253 init_wait(&ewait.wait); 254 ewait.wait.func = wake_exceptional_entry_func; 255 256 while (unlikely(dax_is_locked(entry))) { 257 wq = dax_entry_waitqueue(xas, entry, &ewait.key); 258 prepare_to_wait_exclusive(wq, &ewait.wait, 259 TASK_UNINTERRUPTIBLE); 260 xas_reset(xas); 261 xas_unlock_irq(xas); 262 schedule(); 263 finish_wait(wq, &ewait.wait); 264 xas_lock_irq(xas); 265 entry = xas_load(xas); 266 } 267 268 if (xa_is_internal(entry)) 269 return NULL; 270 271 return entry; 272 } 273 274 /* 275 * The only thing keeping the address space around is the i_pages lock 276 * (it's cycled in clear_inode() after removing the entries from i_pages) 277 * After we call xas_unlock_irq(), we cannot touch xas->xa. 278 */ 279 static void wait_entry_unlocked(struct xa_state *xas, void *entry) 280 { 281 struct wait_exceptional_entry_queue ewait; 282 wait_queue_head_t *wq; 283 284 init_wait(&ewait.wait); 285 ewait.wait.func = wake_exceptional_entry_func; 286 287 wq = dax_entry_waitqueue(xas, entry, &ewait.key); 288 /* 289 * Unlike get_next_unlocked_entry() there is no guarantee that this 290 * path ever successfully retrieves an unlocked entry before an 291 * inode dies. Perform a non-exclusive wait in case this path 292 * never successfully performs its own wake up. 293 */ 294 prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); 295 xas_unlock_irq(xas); 296 schedule(); 297 finish_wait(wq, &ewait.wait); 298 } 299 300 static void put_unlocked_entry(struct xa_state *xas, void *entry, 301 enum dax_wake_mode mode) 302 { 303 if (entry && !dax_is_conflict(entry)) 304 dax_wake_entry(xas, entry, mode); 305 } 306 307 /* 308 * We used the xa_state to get the entry, but then we locked the entry and 309 * dropped the xa_lock, so we know the xa_state is stale and must be reset 310 * before use. 311 */ 312 static void dax_unlock_entry(struct xa_state *xas, void *entry) 313 { 314 void *old; 315 316 BUG_ON(dax_is_locked(entry)); 317 xas_reset(xas); 318 xas_lock_irq(xas); 319 old = xas_store(xas, entry); 320 xas_unlock_irq(xas); 321 BUG_ON(!dax_is_locked(old)); 322 dax_wake_entry(xas, entry, WAKE_NEXT); 323 } 324 325 /* 326 * Return: The entry stored at this location before it was locked. 327 */ 328 static void *dax_lock_entry(struct xa_state *xas, void *entry) 329 { 330 unsigned long v = xa_to_value(entry); 331 return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 332 } 333 334 static unsigned long dax_entry_size(void *entry) 335 { 336 if (dax_is_zero_entry(entry)) 337 return 0; 338 else if (dax_is_empty_entry(entry)) 339 return 0; 340 else if (dax_is_pmd_entry(entry)) 341 return PMD_SIZE; 342 else 343 return PAGE_SIZE; 344 } 345 346 /* 347 * A DAX folio is considered shared if it has no mapping set and ->share (which 348 * shares the ->index field) is non-zero. Note this may return false even if the 349 * page is shared between multiple files but has not yet actually been mapped 350 * into multiple address spaces. 351 */ 352 static inline bool dax_folio_is_shared(struct folio *folio) 353 { 354 return !folio->mapping && folio->share; 355 } 356 357 /* 358 * When it is called by dax_insert_entry(), the shared flag will indicate 359 * whether this entry is shared by multiple files. If the page has not 360 * previously been associated with any mappings the ->mapping and ->index 361 * fields will be set. If it has already been associated with a mapping 362 * the mapping will be cleared and the share count set. It's then up to 363 * reverse map users like memory_failure() to call back into the filesystem to 364 * recover ->mapping and ->index information. For example by implementing 365 * dax_holder_operations. 366 */ 367 static void dax_folio_make_shared(struct folio *folio) 368 { 369 /* 370 * folio is not currently shared so mark it as shared by clearing 371 * folio->mapping. 372 */ 373 folio->mapping = NULL; 374 375 /* 376 * folio has previously been mapped into one address space so set the 377 * share count. 378 */ 379 folio->share = 1; 380 } 381 382 static inline unsigned long dax_folio_put(struct folio *folio) 383 { 384 unsigned long ref; 385 int order, i; 386 387 if (!dax_folio_is_shared(folio)) 388 ref = 0; 389 else 390 ref = --folio->share; 391 392 if (ref) 393 return ref; 394 395 folio->mapping = NULL; 396 order = folio_order(folio); 397 if (!order) 398 return 0; 399 folio_reset_order(folio); 400 401 for (i = 0; i < (1UL << order); i++) { 402 struct dev_pagemap *pgmap = page_pgmap(&folio->page); 403 struct page *page = folio_page(folio, i); 404 struct folio *new_folio = (struct folio *)page; 405 406 ClearPageHead(page); 407 clear_compound_head(page); 408 409 new_folio->mapping = NULL; 410 /* 411 * Reset pgmap which was over-written by 412 * prep_compound_page(). 413 */ 414 new_folio->pgmap = pgmap; 415 new_folio->share = 0; 416 WARN_ON_ONCE(folio_ref_count(new_folio)); 417 } 418 419 return ref; 420 } 421 422 static void dax_folio_init(void *entry) 423 { 424 struct folio *folio = dax_to_folio(entry); 425 int order = dax_entry_order(entry); 426 427 /* 428 * Folio should have been split back to order-0 pages in 429 * dax_folio_put() when they were removed from their 430 * final mapping. 431 */ 432 WARN_ON_ONCE(folio_order(folio)); 433 434 if (order > 0) { 435 prep_compound_page(&folio->page, order); 436 if (order > 1) 437 INIT_LIST_HEAD(&folio->_deferred_list); 438 WARN_ON_ONCE(folio_ref_count(folio)); 439 } 440 } 441 442 static void dax_associate_entry(void *entry, struct address_space *mapping, 443 struct vm_area_struct *vma, 444 unsigned long address, bool shared) 445 { 446 unsigned long size = dax_entry_size(entry), index; 447 struct folio *folio = dax_to_folio(entry); 448 449 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) 450 return; 451 452 index = linear_page_index(vma, address & ~(size - 1)); 453 if (shared && (folio->mapping || dax_folio_is_shared(folio))) { 454 if (folio->mapping) 455 dax_folio_make_shared(folio); 456 457 WARN_ON_ONCE(!folio->share); 458 WARN_ON_ONCE(dax_entry_order(entry) != folio_order(folio)); 459 folio->share++; 460 } else { 461 WARN_ON_ONCE(folio->mapping); 462 dax_folio_init(entry); 463 folio = dax_to_folio(entry); 464 folio->mapping = mapping; 465 folio->index = index; 466 } 467 } 468 469 static void dax_disassociate_entry(void *entry, struct address_space *mapping, 470 bool trunc) 471 { 472 struct folio *folio = dax_to_folio(entry); 473 474 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) 475 return; 476 477 dax_folio_put(folio); 478 } 479 480 static struct page *dax_busy_page(void *entry) 481 { 482 struct folio *folio = dax_to_folio(entry); 483 484 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) 485 return NULL; 486 487 if (folio_ref_count(folio) - folio_mapcount(folio)) 488 return &folio->page; 489 else 490 return NULL; 491 } 492 493 /** 494 * dax_lock_folio - Lock the DAX entry corresponding to a folio 495 * @folio: The folio whose entry we want to lock 496 * 497 * Context: Process context. 498 * Return: A cookie to pass to dax_unlock_folio() or 0 if the entry could 499 * not be locked. 500 */ 501 dax_entry_t dax_lock_folio(struct folio *folio) 502 { 503 XA_STATE(xas, NULL, 0); 504 void *entry; 505 506 /* Ensure folio->mapping isn't freed while we look at it */ 507 rcu_read_lock(); 508 for (;;) { 509 struct address_space *mapping = READ_ONCE(folio->mapping); 510 511 entry = NULL; 512 if (!mapping || !dax_mapping(mapping)) 513 break; 514 515 /* 516 * In the device-dax case there's no need to lock, a 517 * struct dev_pagemap pin is sufficient to keep the 518 * inode alive, and we assume we have dev_pagemap pin 519 * otherwise we would not have a valid pfn_to_page() 520 * translation. 521 */ 522 entry = (void *)~0UL; 523 if (S_ISCHR(mapping->host->i_mode)) 524 break; 525 526 xas.xa = &mapping->i_pages; 527 xas_lock_irq(&xas); 528 if (mapping != folio->mapping) { 529 xas_unlock_irq(&xas); 530 continue; 531 } 532 xas_set(&xas, folio->index); 533 entry = xas_load(&xas); 534 if (dax_is_locked(entry)) { 535 rcu_read_unlock(); 536 wait_entry_unlocked(&xas, entry); 537 rcu_read_lock(); 538 continue; 539 } 540 dax_lock_entry(&xas, entry); 541 xas_unlock_irq(&xas); 542 break; 543 } 544 rcu_read_unlock(); 545 return (dax_entry_t)entry; 546 } 547 548 void dax_unlock_folio(struct folio *folio, dax_entry_t cookie) 549 { 550 struct address_space *mapping = folio->mapping; 551 XA_STATE(xas, &mapping->i_pages, folio->index); 552 553 if (S_ISCHR(mapping->host->i_mode)) 554 return; 555 556 dax_unlock_entry(&xas, (void *)cookie); 557 } 558 559 /* 560 * dax_lock_mapping_entry - Lock the DAX entry corresponding to a mapping 561 * @mapping: the file's mapping whose entry we want to lock 562 * @index: the offset within this file 563 * @page: output the dax page corresponding to this dax entry 564 * 565 * Return: A cookie to pass to dax_unlock_mapping_entry() or 0 if the entry 566 * could not be locked. 567 */ 568 dax_entry_t dax_lock_mapping_entry(struct address_space *mapping, pgoff_t index, 569 struct page **page) 570 { 571 XA_STATE(xas, NULL, 0); 572 void *entry; 573 574 rcu_read_lock(); 575 for (;;) { 576 entry = NULL; 577 if (!dax_mapping(mapping)) 578 break; 579 580 xas.xa = &mapping->i_pages; 581 xas_lock_irq(&xas); 582 xas_set(&xas, index); 583 entry = xas_load(&xas); 584 if (dax_is_locked(entry)) { 585 rcu_read_unlock(); 586 wait_entry_unlocked(&xas, entry); 587 rcu_read_lock(); 588 continue; 589 } 590 if (!entry || 591 dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 592 /* 593 * Because we are looking for entry from file's mapping 594 * and index, so the entry may not be inserted for now, 595 * or even a zero/empty entry. We don't think this is 596 * an error case. So, return a special value and do 597 * not output @page. 598 */ 599 entry = (void *)~0UL; 600 } else { 601 *page = pfn_to_page(dax_to_pfn(entry)); 602 dax_lock_entry(&xas, entry); 603 } 604 xas_unlock_irq(&xas); 605 break; 606 } 607 rcu_read_unlock(); 608 return (dax_entry_t)entry; 609 } 610 611 void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index, 612 dax_entry_t cookie) 613 { 614 XA_STATE(xas, &mapping->i_pages, index); 615 616 if (cookie == ~0UL) 617 return; 618 619 dax_unlock_entry(&xas, (void *)cookie); 620 } 621 622 /* 623 * Find page cache entry at given index. If it is a DAX entry, return it 624 * with the entry locked. If the page cache doesn't contain an entry at 625 * that index, add a locked empty entry. 626 * 627 * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 628 * either return that locked entry or will return VM_FAULT_FALLBACK. 629 * This will happen if there are any PTE entries within the PMD range 630 * that we are requesting. 631 * 632 * We always favor PTE entries over PMD entries. There isn't a flow where we 633 * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 634 * insertion will fail if it finds any PTE entries already in the tree, and a 635 * PTE insertion will cause an existing PMD entry to be unmapped and 636 * downgraded to PTE entries. This happens for both PMD zero pages as 637 * well as PMD empty entries. 638 * 639 * The exception to this downgrade path is for PMD entries that have 640 * real storage backing them. We will leave these real PMD entries in 641 * the tree, and PTE writes will simply dirty the entire PMD entry. 642 * 643 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 644 * persistent memory the benefit is doubtful. We can add that later if we can 645 * show it helps. 646 * 647 * On error, this function does not return an ERR_PTR. Instead it returns 648 * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 649 * overlap with xarray value entries. 650 */ 651 static void *grab_mapping_entry(struct xa_state *xas, 652 struct address_space *mapping, unsigned int order) 653 { 654 unsigned long index = xas->xa_index; 655 bool pmd_downgrade; /* splitting PMD entry into PTE entries? */ 656 void *entry; 657 658 retry: 659 pmd_downgrade = false; 660 xas_lock_irq(xas); 661 entry = get_next_unlocked_entry(xas, order); 662 663 if (entry) { 664 if (dax_is_conflict(entry)) 665 goto fallback; 666 if (!xa_is_value(entry)) { 667 xas_set_err(xas, -EIO); 668 goto out_unlock; 669 } 670 671 if (order == 0) { 672 if (dax_is_pmd_entry(entry) && 673 (dax_is_zero_entry(entry) || 674 dax_is_empty_entry(entry))) { 675 pmd_downgrade = true; 676 } 677 } 678 } 679 680 if (pmd_downgrade) { 681 /* 682 * Make sure 'entry' remains valid while we drop 683 * the i_pages lock. 684 */ 685 dax_lock_entry(xas, entry); 686 687 /* 688 * Besides huge zero pages the only other thing that gets 689 * downgraded are empty entries which don't need to be 690 * unmapped. 691 */ 692 if (dax_is_zero_entry(entry)) { 693 xas_unlock_irq(xas); 694 unmap_mapping_pages(mapping, 695 xas->xa_index & ~PG_PMD_COLOUR, 696 PG_PMD_NR, false); 697 xas_reset(xas); 698 xas_lock_irq(xas); 699 } 700 701 dax_disassociate_entry(entry, mapping, false); 702 xas_store(xas, NULL); /* undo the PMD join */ 703 dax_wake_entry(xas, entry, WAKE_ALL); 704 mapping->nrpages -= PG_PMD_NR; 705 entry = NULL; 706 xas_set(xas, index); 707 } 708 709 if (entry) { 710 dax_lock_entry(xas, entry); 711 } else { 712 unsigned long flags = DAX_EMPTY; 713 714 if (order > 0) 715 flags |= DAX_PMD; 716 entry = dax_make_entry(pfn_to_pfn_t(0), flags); 717 dax_lock_entry(xas, entry); 718 if (xas_error(xas)) 719 goto out_unlock; 720 mapping->nrpages += 1UL << order; 721 } 722 723 out_unlock: 724 xas_unlock_irq(xas); 725 if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 726 goto retry; 727 if (xas->xa_node == XA_ERROR(-ENOMEM)) 728 return xa_mk_internal(VM_FAULT_OOM); 729 if (xas_error(xas)) 730 return xa_mk_internal(VM_FAULT_SIGBUS); 731 return entry; 732 fallback: 733 xas_unlock_irq(xas); 734 return xa_mk_internal(VM_FAULT_FALLBACK); 735 } 736 737 /** 738 * dax_layout_busy_page_range - find first pinned page in @mapping 739 * @mapping: address space to scan for a page with ref count > 1 740 * @start: Starting offset. Page containing 'start' is included. 741 * @end: End offset. Page containing 'end' is included. If 'end' is LLONG_MAX, 742 * pages from 'start' till the end of file are included. 743 * 744 * DAX requires ZONE_DEVICE mapped pages. These pages are never 745 * 'onlined' to the page allocator so they are considered idle when 746 * page->count == 1. A filesystem uses this interface to determine if 747 * any page in the mapping is busy, i.e. for DMA, or other 748 * get_user_pages() usages. 749 * 750 * It is expected that the filesystem is holding locks to block the 751 * establishment of new mappings in this address_space. I.e. it expects 752 * to be able to run unmap_mapping_range() and subsequently not race 753 * mapping_mapped() becoming true. 754 */ 755 struct page *dax_layout_busy_page_range(struct address_space *mapping, 756 loff_t start, loff_t end) 757 { 758 void *entry; 759 unsigned int scanned = 0; 760 struct page *page = NULL; 761 pgoff_t start_idx = start >> PAGE_SHIFT; 762 pgoff_t end_idx; 763 XA_STATE(xas, &mapping->i_pages, start_idx); 764 765 if (!dax_mapping(mapping)) 766 return NULL; 767 768 /* If end == LLONG_MAX, all pages from start to till end of file */ 769 if (end == LLONG_MAX) 770 end_idx = ULONG_MAX; 771 else 772 end_idx = end >> PAGE_SHIFT; 773 /* 774 * If we race get_user_pages_fast() here either we'll see the 775 * elevated page count in the iteration and wait, or 776 * get_user_pages_fast() will see that the page it took a reference 777 * against is no longer mapped in the page tables and bail to the 778 * get_user_pages() slow path. The slow path is protected by 779 * pte_lock() and pmd_lock(). New references are not taken without 780 * holding those locks, and unmap_mapping_pages() will not zero the 781 * pte or pmd without holding the respective lock, so we are 782 * guaranteed to either see new references or prevent new 783 * references from being established. 784 */ 785 unmap_mapping_pages(mapping, start_idx, end_idx - start_idx + 1, 0); 786 787 xas_lock_irq(&xas); 788 xas_for_each(&xas, entry, end_idx) { 789 if (WARN_ON_ONCE(!xa_is_value(entry))) 790 continue; 791 entry = wait_entry_unlocked_exclusive(&xas, entry); 792 if (entry) 793 page = dax_busy_page(entry); 794 put_unlocked_entry(&xas, entry, WAKE_NEXT); 795 if (page) 796 break; 797 if (++scanned % XA_CHECK_SCHED) 798 continue; 799 800 xas_pause(&xas); 801 xas_unlock_irq(&xas); 802 cond_resched(); 803 xas_lock_irq(&xas); 804 } 805 xas_unlock_irq(&xas); 806 return page; 807 } 808 EXPORT_SYMBOL_GPL(dax_layout_busy_page_range); 809 810 struct page *dax_layout_busy_page(struct address_space *mapping) 811 { 812 return dax_layout_busy_page_range(mapping, 0, LLONG_MAX); 813 } 814 EXPORT_SYMBOL_GPL(dax_layout_busy_page); 815 816 static int __dax_invalidate_entry(struct address_space *mapping, 817 pgoff_t index, bool trunc) 818 { 819 XA_STATE(xas, &mapping->i_pages, index); 820 int ret = 0; 821 void *entry; 822 823 xas_lock_irq(&xas); 824 entry = get_next_unlocked_entry(&xas, 0); 825 if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 826 goto out; 827 if (!trunc && 828 (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 829 xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 830 goto out; 831 dax_disassociate_entry(entry, mapping, trunc); 832 xas_store(&xas, NULL); 833 mapping->nrpages -= 1UL << dax_entry_order(entry); 834 ret = 1; 835 out: 836 put_unlocked_entry(&xas, entry, WAKE_ALL); 837 xas_unlock_irq(&xas); 838 return ret; 839 } 840 841 static int __dax_clear_dirty_range(struct address_space *mapping, 842 pgoff_t start, pgoff_t end) 843 { 844 XA_STATE(xas, &mapping->i_pages, start); 845 unsigned int scanned = 0; 846 void *entry; 847 848 xas_lock_irq(&xas); 849 xas_for_each(&xas, entry, end) { 850 entry = wait_entry_unlocked_exclusive(&xas, entry); 851 if (!entry) 852 continue; 853 xas_clear_mark(&xas, PAGECACHE_TAG_DIRTY); 854 xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE); 855 put_unlocked_entry(&xas, entry, WAKE_NEXT); 856 857 if (++scanned % XA_CHECK_SCHED) 858 continue; 859 860 xas_pause(&xas); 861 xas_unlock_irq(&xas); 862 cond_resched(); 863 xas_lock_irq(&xas); 864 } 865 xas_unlock_irq(&xas); 866 867 return 0; 868 } 869 870 /* 871 * Delete DAX entry at @index from @mapping. Wait for it 872 * to be unlocked before deleting it. 873 */ 874 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 875 { 876 int ret = __dax_invalidate_entry(mapping, index, true); 877 878 /* 879 * This gets called from truncate / punch_hole path. As such, the caller 880 * must hold locks protecting against concurrent modifications of the 881 * page cache (usually fs-private i_mmap_sem for writing). Since the 882 * caller has seen a DAX entry for this index, we better find it 883 * at that index as well... 884 */ 885 WARN_ON_ONCE(!ret); 886 return ret; 887 } 888 889 void dax_delete_mapping_range(struct address_space *mapping, 890 loff_t start, loff_t end) 891 { 892 void *entry; 893 pgoff_t start_idx = start >> PAGE_SHIFT; 894 pgoff_t end_idx; 895 XA_STATE(xas, &mapping->i_pages, start_idx); 896 897 /* If end == LLONG_MAX, all pages from start to till end of file */ 898 if (end == LLONG_MAX) 899 end_idx = ULONG_MAX; 900 else 901 end_idx = end >> PAGE_SHIFT; 902 903 xas_lock_irq(&xas); 904 xas_for_each(&xas, entry, end_idx) { 905 if (!xa_is_value(entry)) 906 continue; 907 entry = wait_entry_unlocked_exclusive(&xas, entry); 908 if (!entry) 909 continue; 910 dax_disassociate_entry(entry, mapping, true); 911 xas_store(&xas, NULL); 912 mapping->nrpages -= 1UL << dax_entry_order(entry); 913 put_unlocked_entry(&xas, entry, WAKE_ALL); 914 } 915 xas_unlock_irq(&xas); 916 } 917 EXPORT_SYMBOL_GPL(dax_delete_mapping_range); 918 919 static int wait_page_idle(struct page *page, 920 void (cb)(struct inode *), 921 struct inode *inode) 922 { 923 return ___wait_var_event(page, dax_page_is_idle(page), 924 TASK_INTERRUPTIBLE, 0, 0, cb(inode)); 925 } 926 927 static void wait_page_idle_uninterruptible(struct page *page, 928 struct inode *inode) 929 { 930 ___wait_var_event(page, dax_page_is_idle(page), 931 TASK_UNINTERRUPTIBLE, 0, 0, schedule()); 932 } 933 934 /* 935 * Unmaps the inode and waits for any DMA to complete prior to deleting the 936 * DAX mapping entries for the range. 937 * 938 * For NOWAIT behavior, pass @cb as NULL to early-exit on first found 939 * busy page 940 */ 941 int dax_break_layout(struct inode *inode, loff_t start, loff_t end, 942 void (cb)(struct inode *)) 943 { 944 struct page *page; 945 int error = 0; 946 947 if (!dax_mapping(inode->i_mapping)) 948 return 0; 949 950 do { 951 page = dax_layout_busy_page_range(inode->i_mapping, start, end); 952 if (!page) 953 break; 954 if (!cb) { 955 error = -ERESTARTSYS; 956 break; 957 } 958 959 error = wait_page_idle(page, cb, inode); 960 } while (error == 0); 961 962 if (!page) 963 dax_delete_mapping_range(inode->i_mapping, start, end); 964 965 return error; 966 } 967 EXPORT_SYMBOL_GPL(dax_break_layout); 968 969 void dax_break_layout_final(struct inode *inode) 970 { 971 struct page *page; 972 973 if (!dax_mapping(inode->i_mapping)) 974 return; 975 976 do { 977 page = dax_layout_busy_page_range(inode->i_mapping, 0, 978 LLONG_MAX); 979 if (!page) 980 break; 981 982 wait_page_idle_uninterruptible(page, inode); 983 } while (true); 984 985 if (!page) 986 dax_delete_mapping_range(inode->i_mapping, 0, LLONG_MAX); 987 } 988 EXPORT_SYMBOL_GPL(dax_break_layout_final); 989 990 /* 991 * Invalidate DAX entry if it is clean. 992 */ 993 int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 994 pgoff_t index) 995 { 996 return __dax_invalidate_entry(mapping, index, false); 997 } 998 999 static pgoff_t dax_iomap_pgoff(const struct iomap *iomap, loff_t pos) 1000 { 1001 return PHYS_PFN(iomap->addr + (pos & PAGE_MASK) - iomap->offset); 1002 } 1003 1004 static int copy_cow_page_dax(struct vm_fault *vmf, const struct iomap_iter *iter) 1005 { 1006 pgoff_t pgoff = dax_iomap_pgoff(&iter->iomap, iter->pos); 1007 void *vto, *kaddr; 1008 long rc; 1009 int id; 1010 1011 id = dax_read_lock(); 1012 rc = dax_direct_access(iter->iomap.dax_dev, pgoff, 1, DAX_ACCESS, 1013 &kaddr, NULL); 1014 if (rc < 0) { 1015 dax_read_unlock(id); 1016 return rc; 1017 } 1018 vto = kmap_atomic(vmf->cow_page); 1019 copy_user_page(vto, kaddr, vmf->address, vmf->cow_page); 1020 kunmap_atomic(vto); 1021 dax_read_unlock(id); 1022 return 0; 1023 } 1024 1025 /* 1026 * MAP_SYNC on a dax mapping guarantees dirty metadata is 1027 * flushed on write-faults (non-cow), but not read-faults. 1028 */ 1029 static bool dax_fault_is_synchronous(const struct iomap_iter *iter, 1030 struct vm_area_struct *vma) 1031 { 1032 return (iter->flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) && 1033 (iter->iomap.flags & IOMAP_F_DIRTY); 1034 } 1035 1036 /* 1037 * By this point grab_mapping_entry() has ensured that we have a locked entry 1038 * of the appropriate size so we don't have to worry about downgrading PMDs to 1039 * PTEs. If we happen to be trying to insert a PTE and there is a PMD 1040 * already in the tree, we will skip the insertion and just dirty the PMD as 1041 * appropriate. 1042 */ 1043 static void *dax_insert_entry(struct xa_state *xas, struct vm_fault *vmf, 1044 const struct iomap_iter *iter, void *entry, pfn_t pfn, 1045 unsigned long flags) 1046 { 1047 struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1048 void *new_entry = dax_make_entry(pfn, flags); 1049 bool write = iter->flags & IOMAP_WRITE; 1050 bool dirty = write && !dax_fault_is_synchronous(iter, vmf->vma); 1051 bool shared = iter->iomap.flags & IOMAP_F_SHARED; 1052 1053 if (dirty) 1054 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 1055 1056 if (shared || (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE))) { 1057 unsigned long index = xas->xa_index; 1058 /* we are replacing a zero page with block mapping */ 1059 if (dax_is_pmd_entry(entry)) 1060 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 1061 PG_PMD_NR, false); 1062 else /* pte entry */ 1063 unmap_mapping_pages(mapping, index, 1, false); 1064 } 1065 1066 xas_reset(xas); 1067 xas_lock_irq(xas); 1068 if (shared || dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 1069 void *old; 1070 1071 dax_disassociate_entry(entry, mapping, false); 1072 dax_associate_entry(new_entry, mapping, vmf->vma, 1073 vmf->address, shared); 1074 1075 /* 1076 * Only swap our new entry into the page cache if the current 1077 * entry is a zero page or an empty entry. If a normal PTE or 1078 * PMD entry is already in the cache, we leave it alone. This 1079 * means that if we are trying to insert a PTE and the 1080 * existing entry is a PMD, we will just leave the PMD in the 1081 * tree and dirty it if necessary. 1082 */ 1083 old = dax_lock_entry(xas, new_entry); 1084 WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 1085 DAX_LOCKED)); 1086 entry = new_entry; 1087 } else { 1088 xas_load(xas); /* Walk the xa_state */ 1089 } 1090 1091 if (dirty) 1092 xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 1093 1094 if (write && shared) 1095 xas_set_mark(xas, PAGECACHE_TAG_TOWRITE); 1096 1097 xas_unlock_irq(xas); 1098 return entry; 1099 } 1100 1101 static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 1102 struct address_space *mapping, void *entry) 1103 { 1104 unsigned long pfn, index, count, end; 1105 long ret = 0; 1106 struct vm_area_struct *vma; 1107 1108 /* 1109 * A page got tagged dirty in DAX mapping? Something is seriously 1110 * wrong. 1111 */ 1112 if (WARN_ON(!xa_is_value(entry))) 1113 return -EIO; 1114 1115 if (unlikely(dax_is_locked(entry))) { 1116 void *old_entry = entry; 1117 1118 entry = get_next_unlocked_entry(xas, 0); 1119 1120 /* Entry got punched out / reallocated? */ 1121 if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 1122 goto put_unlocked; 1123 /* 1124 * Entry got reallocated elsewhere? No need to writeback. 1125 * We have to compare pfns as we must not bail out due to 1126 * difference in lockbit or entry type. 1127 */ 1128 if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 1129 goto put_unlocked; 1130 if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 1131 dax_is_zero_entry(entry))) { 1132 ret = -EIO; 1133 goto put_unlocked; 1134 } 1135 1136 /* Another fsync thread may have already done this entry */ 1137 if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 1138 goto put_unlocked; 1139 } 1140 1141 /* Lock the entry to serialize with page faults */ 1142 dax_lock_entry(xas, entry); 1143 1144 /* 1145 * We can clear the tag now but we have to be careful so that concurrent 1146 * dax_writeback_one() calls for the same index cannot finish before we 1147 * actually flush the caches. This is achieved as the calls will look 1148 * at the entry only under the i_pages lock and once they do that 1149 * they will see the entry locked and wait for it to unlock. 1150 */ 1151 xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 1152 xas_unlock_irq(xas); 1153 1154 /* 1155 * If dax_writeback_mapping_range() was given a wbc->range_start 1156 * in the middle of a PMD, the 'index' we use needs to be 1157 * aligned to the start of the PMD. 1158 * This allows us to flush for PMD_SIZE and not have to worry about 1159 * partial PMD writebacks. 1160 */ 1161 pfn = dax_to_pfn(entry); 1162 count = 1UL << dax_entry_order(entry); 1163 index = xas->xa_index & ~(count - 1); 1164 end = index + count - 1; 1165 1166 /* Walk all mappings of a given index of a file and writeprotect them */ 1167 i_mmap_lock_read(mapping); 1168 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, end) { 1169 pfn_mkclean_range(pfn, count, index, vma); 1170 cond_resched(); 1171 } 1172 i_mmap_unlock_read(mapping); 1173 1174 dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE); 1175 /* 1176 * After we have flushed the cache, we can clear the dirty tag. There 1177 * cannot be new dirty data in the pfn after the flush has completed as 1178 * the pfn mappings are writeprotected and fault waits for mapping 1179 * entry lock. 1180 */ 1181 xas_reset(xas); 1182 xas_lock_irq(xas); 1183 xas_store(xas, entry); 1184 xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 1185 dax_wake_entry(xas, entry, WAKE_NEXT); 1186 1187 trace_dax_writeback_one(mapping->host, index, count); 1188 return ret; 1189 1190 put_unlocked: 1191 put_unlocked_entry(xas, entry, WAKE_NEXT); 1192 return ret; 1193 } 1194 1195 /* 1196 * Flush the mapping to the persistent domain within the byte range of [start, 1197 * end]. This is required by data integrity operations to ensure file data is 1198 * on persistent storage prior to completion of the operation. 1199 */ 1200 int dax_writeback_mapping_range(struct address_space *mapping, 1201 struct dax_device *dax_dev, struct writeback_control *wbc) 1202 { 1203 XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 1204 struct inode *inode = mapping->host; 1205 pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 1206 void *entry; 1207 int ret = 0; 1208 unsigned int scanned = 0; 1209 1210 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 1211 return -EIO; 1212 1213 if (mapping_empty(mapping) || wbc->sync_mode != WB_SYNC_ALL) 1214 return 0; 1215 1216 trace_dax_writeback_range(inode, xas.xa_index, end_index); 1217 1218 tag_pages_for_writeback(mapping, xas.xa_index, end_index); 1219 1220 xas_lock_irq(&xas); 1221 xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 1222 ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 1223 if (ret < 0) { 1224 mapping_set_error(mapping, ret); 1225 break; 1226 } 1227 if (++scanned % XA_CHECK_SCHED) 1228 continue; 1229 1230 xas_pause(&xas); 1231 xas_unlock_irq(&xas); 1232 cond_resched(); 1233 xas_lock_irq(&xas); 1234 } 1235 xas_unlock_irq(&xas); 1236 trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 1237 return ret; 1238 } 1239 EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 1240 1241 static int dax_iomap_direct_access(const struct iomap *iomap, loff_t pos, 1242 size_t size, void **kaddr, pfn_t *pfnp) 1243 { 1244 pgoff_t pgoff = dax_iomap_pgoff(iomap, pos); 1245 int id, rc = 0; 1246 long length; 1247 1248 id = dax_read_lock(); 1249 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 1250 DAX_ACCESS, kaddr, pfnp); 1251 if (length < 0) { 1252 rc = length; 1253 goto out; 1254 } 1255 if (!pfnp) 1256 goto out_check_addr; 1257 rc = -EINVAL; 1258 if (PFN_PHYS(length) < size) 1259 goto out; 1260 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 1261 goto out; 1262 1263 rc = 0; 1264 1265 out_check_addr: 1266 if (!kaddr) 1267 goto out; 1268 if (!*kaddr) 1269 rc = -EFAULT; 1270 out: 1271 dax_read_unlock(id); 1272 return rc; 1273 } 1274 1275 /** 1276 * dax_iomap_copy_around - Prepare for an unaligned write to a shared/cow page 1277 * by copying the data before and after the range to be written. 1278 * @pos: address to do copy from. 1279 * @length: size of copy operation. 1280 * @align_size: aligned w.r.t align_size (either PMD_SIZE or PAGE_SIZE) 1281 * @srcmap: iomap srcmap 1282 * @daddr: destination address to copy to. 1283 * 1284 * This can be called from two places. Either during DAX write fault (page 1285 * aligned), to copy the length size data to daddr. Or, while doing normal DAX 1286 * write operation, dax_iomap_iter() might call this to do the copy of either 1287 * start or end unaligned address. In the latter case the rest of the copy of 1288 * aligned ranges is taken care by dax_iomap_iter() itself. 1289 * If the srcmap contains invalid data, such as HOLE and UNWRITTEN, zero the 1290 * area to make sure no old data remains. 1291 */ 1292 static int dax_iomap_copy_around(loff_t pos, uint64_t length, size_t align_size, 1293 const struct iomap *srcmap, void *daddr) 1294 { 1295 loff_t head_off = pos & (align_size - 1); 1296 size_t size = ALIGN(head_off + length, align_size); 1297 loff_t end = pos + length; 1298 loff_t pg_end = round_up(end, align_size); 1299 /* copy_all is usually in page fault case */ 1300 bool copy_all = head_off == 0 && end == pg_end; 1301 /* zero the edges if srcmap is a HOLE or IOMAP_UNWRITTEN */ 1302 bool zero_edge = srcmap->flags & IOMAP_F_SHARED || 1303 srcmap->type == IOMAP_UNWRITTEN; 1304 void *saddr = NULL; 1305 int ret = 0; 1306 1307 if (!zero_edge) { 1308 ret = dax_iomap_direct_access(srcmap, pos, size, &saddr, NULL); 1309 if (ret) 1310 return dax_mem2blk_err(ret); 1311 } 1312 1313 if (copy_all) { 1314 if (zero_edge) 1315 memset(daddr, 0, size); 1316 else 1317 ret = copy_mc_to_kernel(daddr, saddr, length); 1318 goto out; 1319 } 1320 1321 /* Copy the head part of the range */ 1322 if (head_off) { 1323 if (zero_edge) 1324 memset(daddr, 0, head_off); 1325 else { 1326 ret = copy_mc_to_kernel(daddr, saddr, head_off); 1327 if (ret) 1328 return -EIO; 1329 } 1330 } 1331 1332 /* Copy the tail part of the range */ 1333 if (end < pg_end) { 1334 loff_t tail_off = head_off + length; 1335 loff_t tail_len = pg_end - end; 1336 1337 if (zero_edge) 1338 memset(daddr + tail_off, 0, tail_len); 1339 else { 1340 ret = copy_mc_to_kernel(daddr + tail_off, 1341 saddr + tail_off, tail_len); 1342 if (ret) 1343 return -EIO; 1344 } 1345 } 1346 out: 1347 if (zero_edge) 1348 dax_flush(srcmap->dax_dev, daddr, size); 1349 return ret ? -EIO : 0; 1350 } 1351 1352 /* 1353 * The user has performed a load from a hole in the file. Allocating a new 1354 * page in the file would cause excessive storage usage for workloads with 1355 * sparse files. Instead we insert a read-only mapping of the 4k zero page. 1356 * If this page is ever written to we will re-fault and change the mapping to 1357 * point to real DAX storage instead. 1358 */ 1359 static vm_fault_t dax_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1360 const struct iomap_iter *iter, void **entry) 1361 { 1362 struct inode *inode = iter->inode; 1363 unsigned long vaddr = vmf->address; 1364 pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1365 vm_fault_t ret; 1366 1367 *entry = dax_insert_entry(xas, vmf, iter, *entry, pfn, DAX_ZERO_PAGE); 1368 1369 ret = vmf_insert_page_mkwrite(vmf, pfn_t_to_page(pfn), false); 1370 trace_dax_load_hole(inode, vmf, ret); 1371 return ret; 1372 } 1373 1374 #ifdef CONFIG_FS_DAX_PMD 1375 static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1376 const struct iomap_iter *iter, void **entry) 1377 { 1378 struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1379 unsigned long pmd_addr = vmf->address & PMD_MASK; 1380 struct vm_area_struct *vma = vmf->vma; 1381 struct inode *inode = mapping->host; 1382 pgtable_t pgtable = NULL; 1383 struct folio *zero_folio; 1384 spinlock_t *ptl; 1385 pmd_t pmd_entry; 1386 pfn_t pfn; 1387 1388 zero_folio = mm_get_huge_zero_folio(vmf->vma->vm_mm); 1389 1390 if (unlikely(!zero_folio)) 1391 goto fallback; 1392 1393 pfn = page_to_pfn_t(&zero_folio->page); 1394 *entry = dax_insert_entry(xas, vmf, iter, *entry, pfn, 1395 DAX_PMD | DAX_ZERO_PAGE); 1396 1397 if (arch_needs_pgtable_deposit()) { 1398 pgtable = pte_alloc_one(vma->vm_mm); 1399 if (!pgtable) 1400 return VM_FAULT_OOM; 1401 } 1402 1403 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1404 if (!pmd_none(*(vmf->pmd))) { 1405 spin_unlock(ptl); 1406 goto fallback; 1407 } 1408 1409 if (pgtable) { 1410 pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 1411 mm_inc_nr_ptes(vma->vm_mm); 1412 } 1413 pmd_entry = folio_mk_pmd(zero_folio, vmf->vma->vm_page_prot); 1414 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1415 spin_unlock(ptl); 1416 trace_dax_pmd_load_hole(inode, vmf, zero_folio, *entry); 1417 return VM_FAULT_NOPAGE; 1418 1419 fallback: 1420 if (pgtable) 1421 pte_free(vma->vm_mm, pgtable); 1422 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_folio, *entry); 1423 return VM_FAULT_FALLBACK; 1424 } 1425 #else 1426 static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1427 const struct iomap_iter *iter, void **entry) 1428 { 1429 return VM_FAULT_FALLBACK; 1430 } 1431 #endif /* CONFIG_FS_DAX_PMD */ 1432 1433 static int dax_unshare_iter(struct iomap_iter *iter) 1434 { 1435 struct iomap *iomap = &iter->iomap; 1436 const struct iomap *srcmap = iomap_iter_srcmap(iter); 1437 loff_t copy_pos = iter->pos; 1438 u64 copy_len = iomap_length(iter); 1439 u32 mod; 1440 int id = 0; 1441 s64 ret; 1442 void *daddr = NULL, *saddr = NULL; 1443 1444 if (!iomap_want_unshare_iter(iter)) 1445 return iomap_iter_advance_full(iter); 1446 1447 /* 1448 * Extend the file range to be aligned to fsblock/pagesize, because 1449 * we need to copy entire blocks, not just the byte range specified. 1450 * Invalidate the mapping because we're about to CoW. 1451 */ 1452 mod = offset_in_page(copy_pos); 1453 if (mod) { 1454 copy_len += mod; 1455 copy_pos -= mod; 1456 } 1457 1458 mod = offset_in_page(copy_pos + copy_len); 1459 if (mod) 1460 copy_len += PAGE_SIZE - mod; 1461 1462 invalidate_inode_pages2_range(iter->inode->i_mapping, 1463 copy_pos >> PAGE_SHIFT, 1464 (copy_pos + copy_len - 1) >> PAGE_SHIFT); 1465 1466 id = dax_read_lock(); 1467 ret = dax_iomap_direct_access(iomap, copy_pos, copy_len, &daddr, NULL); 1468 if (ret < 0) 1469 goto out_unlock; 1470 1471 ret = dax_iomap_direct_access(srcmap, copy_pos, copy_len, &saddr, NULL); 1472 if (ret < 0) 1473 goto out_unlock; 1474 1475 if (copy_mc_to_kernel(daddr, saddr, copy_len) != 0) 1476 ret = -EIO; 1477 1478 out_unlock: 1479 dax_read_unlock(id); 1480 if (ret < 0) 1481 return dax_mem2blk_err(ret); 1482 return iomap_iter_advance_full(iter); 1483 } 1484 1485 int dax_file_unshare(struct inode *inode, loff_t pos, loff_t len, 1486 const struct iomap_ops *ops) 1487 { 1488 struct iomap_iter iter = { 1489 .inode = inode, 1490 .pos = pos, 1491 .flags = IOMAP_WRITE | IOMAP_UNSHARE | IOMAP_DAX, 1492 }; 1493 loff_t size = i_size_read(inode); 1494 int ret; 1495 1496 if (pos < 0 || pos >= size) 1497 return 0; 1498 1499 iter.len = min(len, size - pos); 1500 while ((ret = iomap_iter(&iter, ops)) > 0) 1501 iter.status = dax_unshare_iter(&iter); 1502 return ret; 1503 } 1504 EXPORT_SYMBOL_GPL(dax_file_unshare); 1505 1506 static int dax_memzero(struct iomap_iter *iter, loff_t pos, size_t size) 1507 { 1508 const struct iomap *iomap = &iter->iomap; 1509 const struct iomap *srcmap = iomap_iter_srcmap(iter); 1510 unsigned offset = offset_in_page(pos); 1511 pgoff_t pgoff = dax_iomap_pgoff(iomap, pos); 1512 void *kaddr; 1513 long ret; 1514 1515 ret = dax_direct_access(iomap->dax_dev, pgoff, 1, DAX_ACCESS, &kaddr, 1516 NULL); 1517 if (ret < 0) 1518 return dax_mem2blk_err(ret); 1519 1520 memset(kaddr + offset, 0, size); 1521 if (iomap->flags & IOMAP_F_SHARED) 1522 ret = dax_iomap_copy_around(pos, size, PAGE_SIZE, srcmap, 1523 kaddr); 1524 else 1525 dax_flush(iomap->dax_dev, kaddr + offset, size); 1526 return ret; 1527 } 1528 1529 static int dax_zero_iter(struct iomap_iter *iter, bool *did_zero) 1530 { 1531 const struct iomap *iomap = &iter->iomap; 1532 const struct iomap *srcmap = iomap_iter_srcmap(iter); 1533 u64 length = iomap_length(iter); 1534 int ret; 1535 1536 /* already zeroed? we're done. */ 1537 if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN) 1538 return iomap_iter_advance(iter, &length); 1539 1540 /* 1541 * invalidate the pages whose sharing state is to be changed 1542 * because of CoW. 1543 */ 1544 if (iomap->flags & IOMAP_F_SHARED) 1545 invalidate_inode_pages2_range(iter->inode->i_mapping, 1546 iter->pos >> PAGE_SHIFT, 1547 (iter->pos + length - 1) >> PAGE_SHIFT); 1548 1549 do { 1550 loff_t pos = iter->pos; 1551 unsigned offset = offset_in_page(pos); 1552 pgoff_t pgoff = dax_iomap_pgoff(iomap, pos); 1553 int id; 1554 1555 length = min_t(u64, PAGE_SIZE - offset, length); 1556 1557 id = dax_read_lock(); 1558 if (IS_ALIGNED(pos, PAGE_SIZE) && length == PAGE_SIZE) 1559 ret = dax_zero_page_range(iomap->dax_dev, pgoff, 1); 1560 else 1561 ret = dax_memzero(iter, pos, length); 1562 dax_read_unlock(id); 1563 1564 if (ret < 0) 1565 return ret; 1566 1567 ret = iomap_iter_advance(iter, &length); 1568 if (ret) 1569 return ret; 1570 } while (length > 0); 1571 1572 if (did_zero) 1573 *did_zero = true; 1574 return ret; 1575 } 1576 1577 int dax_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, 1578 const struct iomap_ops *ops) 1579 { 1580 struct iomap_iter iter = { 1581 .inode = inode, 1582 .pos = pos, 1583 .len = len, 1584 .flags = IOMAP_DAX | IOMAP_ZERO, 1585 }; 1586 int ret; 1587 1588 while ((ret = iomap_iter(&iter, ops)) > 0) 1589 iter.status = dax_zero_iter(&iter, did_zero); 1590 return ret; 1591 } 1592 EXPORT_SYMBOL_GPL(dax_zero_range); 1593 1594 int dax_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, 1595 const struct iomap_ops *ops) 1596 { 1597 unsigned int blocksize = i_blocksize(inode); 1598 unsigned int off = pos & (blocksize - 1); 1599 1600 /* Block boundary? Nothing to do */ 1601 if (!off) 1602 return 0; 1603 return dax_zero_range(inode, pos, blocksize - off, did_zero, ops); 1604 } 1605 EXPORT_SYMBOL_GPL(dax_truncate_page); 1606 1607 static int dax_iomap_iter(struct iomap_iter *iomi, struct iov_iter *iter) 1608 { 1609 const struct iomap *iomap = &iomi->iomap; 1610 const struct iomap *srcmap = iomap_iter_srcmap(iomi); 1611 loff_t length = iomap_length(iomi); 1612 loff_t pos = iomi->pos; 1613 struct dax_device *dax_dev = iomap->dax_dev; 1614 loff_t end = pos + length, done = 0; 1615 bool write = iov_iter_rw(iter) == WRITE; 1616 bool cow = write && iomap->flags & IOMAP_F_SHARED; 1617 ssize_t ret = 0; 1618 size_t xfer; 1619 int id; 1620 1621 if (!write) { 1622 end = min(end, i_size_read(iomi->inode)); 1623 if (pos >= end) 1624 return 0; 1625 1626 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) { 1627 done = iov_iter_zero(min(length, end - pos), iter); 1628 return iomap_iter_advance(iomi, &done); 1629 } 1630 } 1631 1632 /* 1633 * In DAX mode, enforce either pure overwrites of written extents, or 1634 * writes to unwritten extents as part of a copy-on-write operation. 1635 */ 1636 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED && 1637 !(iomap->flags & IOMAP_F_SHARED))) 1638 return -EIO; 1639 1640 /* 1641 * Write can allocate block for an area which has a hole page mapped 1642 * into page tables. We have to tear down these mappings so that data 1643 * written by write(2) is visible in mmap. 1644 */ 1645 if (iomap->flags & IOMAP_F_NEW || cow) { 1646 /* 1647 * Filesystem allows CoW on non-shared extents. The src extents 1648 * may have been mmapped with dirty mark before. To be able to 1649 * invalidate its dax entries, we need to clear the dirty mark 1650 * in advance. 1651 */ 1652 if (cow) 1653 __dax_clear_dirty_range(iomi->inode->i_mapping, 1654 pos >> PAGE_SHIFT, 1655 (end - 1) >> PAGE_SHIFT); 1656 invalidate_inode_pages2_range(iomi->inode->i_mapping, 1657 pos >> PAGE_SHIFT, 1658 (end - 1) >> PAGE_SHIFT); 1659 } 1660 1661 id = dax_read_lock(); 1662 while ((pos = iomi->pos) < end) { 1663 unsigned offset = pos & (PAGE_SIZE - 1); 1664 const size_t size = ALIGN(length + offset, PAGE_SIZE); 1665 pgoff_t pgoff = dax_iomap_pgoff(iomap, pos); 1666 ssize_t map_len; 1667 bool recovery = false; 1668 void *kaddr; 1669 1670 if (fatal_signal_pending(current)) { 1671 ret = -EINTR; 1672 break; 1673 } 1674 1675 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 1676 DAX_ACCESS, &kaddr, NULL); 1677 if (map_len == -EHWPOISON && iov_iter_rw(iter) == WRITE) { 1678 map_len = dax_direct_access(dax_dev, pgoff, 1679 PHYS_PFN(size), DAX_RECOVERY_WRITE, 1680 &kaddr, NULL); 1681 if (map_len > 0) 1682 recovery = true; 1683 } 1684 if (map_len < 0) { 1685 ret = dax_mem2blk_err(map_len); 1686 break; 1687 } 1688 1689 if (cow) { 1690 ret = dax_iomap_copy_around(pos, length, PAGE_SIZE, 1691 srcmap, kaddr); 1692 if (ret) 1693 break; 1694 } 1695 1696 map_len = PFN_PHYS(map_len); 1697 kaddr += offset; 1698 map_len -= offset; 1699 if (map_len > end - pos) 1700 map_len = end - pos; 1701 1702 if (recovery) 1703 xfer = dax_recovery_write(dax_dev, pgoff, kaddr, 1704 map_len, iter); 1705 else if (write) 1706 xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1707 map_len, iter); 1708 else 1709 xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1710 map_len, iter); 1711 1712 length = xfer; 1713 ret = iomap_iter_advance(iomi, &length); 1714 if (!ret && xfer == 0) 1715 ret = -EFAULT; 1716 if (xfer < map_len) 1717 break; 1718 } 1719 dax_read_unlock(id); 1720 1721 return ret; 1722 } 1723 1724 /** 1725 * dax_iomap_rw - Perform I/O to a DAX file 1726 * @iocb: The control block for this I/O 1727 * @iter: The addresses to do I/O from or to 1728 * @ops: iomap ops passed from the file system 1729 * 1730 * This function performs read and write operations to directly mapped 1731 * persistent memory. The callers needs to take care of read/write exclusion 1732 * and evicting any page cache pages in the region under I/O. 1733 */ 1734 ssize_t 1735 dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 1736 const struct iomap_ops *ops) 1737 { 1738 struct iomap_iter iomi = { 1739 .inode = iocb->ki_filp->f_mapping->host, 1740 .pos = iocb->ki_pos, 1741 .len = iov_iter_count(iter), 1742 .flags = IOMAP_DAX, 1743 }; 1744 loff_t done = 0; 1745 int ret; 1746 1747 if (!iomi.len) 1748 return 0; 1749 1750 if (iov_iter_rw(iter) == WRITE) { 1751 lockdep_assert_held_write(&iomi.inode->i_rwsem); 1752 iomi.flags |= IOMAP_WRITE; 1753 } else { 1754 lockdep_assert_held(&iomi.inode->i_rwsem); 1755 } 1756 1757 if (iocb->ki_flags & IOCB_NOWAIT) 1758 iomi.flags |= IOMAP_NOWAIT; 1759 1760 while ((ret = iomap_iter(&iomi, ops)) > 0) 1761 iomi.status = dax_iomap_iter(&iomi, iter); 1762 1763 done = iomi.pos - iocb->ki_pos; 1764 iocb->ki_pos = iomi.pos; 1765 return done ? done : ret; 1766 } 1767 EXPORT_SYMBOL_GPL(dax_iomap_rw); 1768 1769 static vm_fault_t dax_fault_return(int error) 1770 { 1771 if (error == 0) 1772 return VM_FAULT_NOPAGE; 1773 return vmf_error(error); 1774 } 1775 1776 /* 1777 * When handling a synchronous page fault and the inode need a fsync, we can 1778 * insert the PTE/PMD into page tables only after that fsync happened. Skip 1779 * insertion for now and return the pfn so that caller can insert it after the 1780 * fsync is done. 1781 */ 1782 static vm_fault_t dax_fault_synchronous_pfnp(pfn_t *pfnp, pfn_t pfn) 1783 { 1784 if (WARN_ON_ONCE(!pfnp)) 1785 return VM_FAULT_SIGBUS; 1786 *pfnp = pfn; 1787 return VM_FAULT_NEEDDSYNC; 1788 } 1789 1790 static vm_fault_t dax_fault_cow_page(struct vm_fault *vmf, 1791 const struct iomap_iter *iter) 1792 { 1793 vm_fault_t ret; 1794 int error = 0; 1795 1796 switch (iter->iomap.type) { 1797 case IOMAP_HOLE: 1798 case IOMAP_UNWRITTEN: 1799 clear_user_highpage(vmf->cow_page, vmf->address); 1800 break; 1801 case IOMAP_MAPPED: 1802 error = copy_cow_page_dax(vmf, iter); 1803 break; 1804 default: 1805 WARN_ON_ONCE(1); 1806 error = -EIO; 1807 break; 1808 } 1809 1810 if (error) 1811 return dax_fault_return(error); 1812 1813 __SetPageUptodate(vmf->cow_page); 1814 ret = finish_fault(vmf); 1815 if (!ret) 1816 return VM_FAULT_DONE_COW; 1817 return ret; 1818 } 1819 1820 /** 1821 * dax_fault_iter - Common actor to handle pfn insertion in PTE/PMD fault. 1822 * @vmf: vm fault instance 1823 * @iter: iomap iter 1824 * @pfnp: pfn to be returned 1825 * @xas: the dax mapping tree of a file 1826 * @entry: an unlocked dax entry to be inserted 1827 * @pmd: distinguish whether it is a pmd fault 1828 */ 1829 static vm_fault_t dax_fault_iter(struct vm_fault *vmf, 1830 const struct iomap_iter *iter, pfn_t *pfnp, 1831 struct xa_state *xas, void **entry, bool pmd) 1832 { 1833 const struct iomap *iomap = &iter->iomap; 1834 const struct iomap *srcmap = iomap_iter_srcmap(iter); 1835 size_t size = pmd ? PMD_SIZE : PAGE_SIZE; 1836 loff_t pos = (loff_t)xas->xa_index << PAGE_SHIFT; 1837 bool write = iter->flags & IOMAP_WRITE; 1838 unsigned long entry_flags = pmd ? DAX_PMD : 0; 1839 struct folio *folio; 1840 int ret, err = 0; 1841 pfn_t pfn; 1842 void *kaddr; 1843 1844 if (!pmd && vmf->cow_page) 1845 return dax_fault_cow_page(vmf, iter); 1846 1847 /* if we are reading UNWRITTEN and HOLE, return a hole. */ 1848 if (!write && 1849 (iomap->type == IOMAP_UNWRITTEN || iomap->type == IOMAP_HOLE)) { 1850 if (!pmd) 1851 return dax_load_hole(xas, vmf, iter, entry); 1852 return dax_pmd_load_hole(xas, vmf, iter, entry); 1853 } 1854 1855 if (iomap->type != IOMAP_MAPPED && !(iomap->flags & IOMAP_F_SHARED)) { 1856 WARN_ON_ONCE(1); 1857 return pmd ? VM_FAULT_FALLBACK : VM_FAULT_SIGBUS; 1858 } 1859 1860 err = dax_iomap_direct_access(iomap, pos, size, &kaddr, &pfn); 1861 if (err) 1862 return pmd ? VM_FAULT_FALLBACK : dax_fault_return(err); 1863 1864 *entry = dax_insert_entry(xas, vmf, iter, *entry, pfn, entry_flags); 1865 1866 if (write && iomap->flags & IOMAP_F_SHARED) { 1867 err = dax_iomap_copy_around(pos, size, size, srcmap, kaddr); 1868 if (err) 1869 return dax_fault_return(err); 1870 } 1871 1872 folio = dax_to_folio(*entry); 1873 if (dax_fault_is_synchronous(iter, vmf->vma)) 1874 return dax_fault_synchronous_pfnp(pfnp, pfn); 1875 1876 folio_ref_inc(folio); 1877 if (pmd) 1878 ret = vmf_insert_folio_pmd(vmf, pfn_folio(pfn_t_to_pfn(pfn)), 1879 write); 1880 else 1881 ret = vmf_insert_page_mkwrite(vmf, pfn_t_to_page(pfn), write); 1882 folio_put(folio); 1883 1884 return ret; 1885 } 1886 1887 static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1888 int *iomap_errp, const struct iomap_ops *ops) 1889 { 1890 struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1891 XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1892 struct iomap_iter iter = { 1893 .inode = mapping->host, 1894 .pos = (loff_t)vmf->pgoff << PAGE_SHIFT, 1895 .len = PAGE_SIZE, 1896 .flags = IOMAP_DAX | IOMAP_FAULT, 1897 }; 1898 vm_fault_t ret = 0; 1899 void *entry; 1900 int error; 1901 1902 trace_dax_pte_fault(iter.inode, vmf, ret); 1903 /* 1904 * Check whether offset isn't beyond end of file now. Caller is supposed 1905 * to hold locks serializing us with truncate / punch hole so this is 1906 * a reliable test. 1907 */ 1908 if (iter.pos >= i_size_read(iter.inode)) { 1909 ret = VM_FAULT_SIGBUS; 1910 goto out; 1911 } 1912 1913 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page) 1914 iter.flags |= IOMAP_WRITE; 1915 1916 entry = grab_mapping_entry(&xas, mapping, 0); 1917 if (xa_is_internal(entry)) { 1918 ret = xa_to_internal(entry); 1919 goto out; 1920 } 1921 1922 /* 1923 * It is possible, particularly with mixed reads & writes to private 1924 * mappings, that we have raced with a PMD fault that overlaps with 1925 * the PTE we need to set up. If so just return and the fault will be 1926 * retried. 1927 */ 1928 if (pmd_trans_huge(*vmf->pmd)) { 1929 ret = VM_FAULT_NOPAGE; 1930 goto unlock_entry; 1931 } 1932 1933 while ((error = iomap_iter(&iter, ops)) > 0) { 1934 if (WARN_ON_ONCE(iomap_length(&iter) < PAGE_SIZE)) { 1935 iter.status = -EIO; /* fs corruption? */ 1936 continue; 1937 } 1938 1939 ret = dax_fault_iter(vmf, &iter, pfnp, &xas, &entry, false); 1940 if (ret != VM_FAULT_SIGBUS && 1941 (iter.iomap.flags & IOMAP_F_NEW)) { 1942 count_vm_event(PGMAJFAULT); 1943 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); 1944 ret |= VM_FAULT_MAJOR; 1945 } 1946 1947 if (!(ret & VM_FAULT_ERROR)) { 1948 u64 length = PAGE_SIZE; 1949 iter.status = iomap_iter_advance(&iter, &length); 1950 } 1951 } 1952 1953 if (iomap_errp) 1954 *iomap_errp = error; 1955 if (!ret && error) 1956 ret = dax_fault_return(error); 1957 1958 unlock_entry: 1959 dax_unlock_entry(&xas, entry); 1960 out: 1961 trace_dax_pte_fault_done(iter.inode, vmf, ret); 1962 return ret; 1963 } 1964 1965 #ifdef CONFIG_FS_DAX_PMD 1966 static bool dax_fault_check_fallback(struct vm_fault *vmf, struct xa_state *xas, 1967 pgoff_t max_pgoff) 1968 { 1969 unsigned long pmd_addr = vmf->address & PMD_MASK; 1970 bool write = vmf->flags & FAULT_FLAG_WRITE; 1971 1972 /* 1973 * Make sure that the faulting address's PMD offset (color) matches 1974 * the PMD offset from the start of the file. This is necessary so 1975 * that a PMD range in the page table overlaps exactly with a PMD 1976 * range in the page cache. 1977 */ 1978 if ((vmf->pgoff & PG_PMD_COLOUR) != 1979 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1980 return true; 1981 1982 /* Fall back to PTEs if we're going to COW */ 1983 if (write && !(vmf->vma->vm_flags & VM_SHARED)) 1984 return true; 1985 1986 /* If the PMD would extend outside the VMA */ 1987 if (pmd_addr < vmf->vma->vm_start) 1988 return true; 1989 if ((pmd_addr + PMD_SIZE) > vmf->vma->vm_end) 1990 return true; 1991 1992 /* If the PMD would extend beyond the file size */ 1993 if ((xas->xa_index | PG_PMD_COLOUR) >= max_pgoff) 1994 return true; 1995 1996 return false; 1997 } 1998 1999 static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 2000 const struct iomap_ops *ops) 2001 { 2002 struct address_space *mapping = vmf->vma->vm_file->f_mapping; 2003 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 2004 struct iomap_iter iter = { 2005 .inode = mapping->host, 2006 .len = PMD_SIZE, 2007 .flags = IOMAP_DAX | IOMAP_FAULT, 2008 }; 2009 vm_fault_t ret = VM_FAULT_FALLBACK; 2010 pgoff_t max_pgoff; 2011 void *entry; 2012 2013 if (vmf->flags & FAULT_FLAG_WRITE) 2014 iter.flags |= IOMAP_WRITE; 2015 2016 /* 2017 * Check whether offset isn't beyond end of file now. Caller is 2018 * supposed to hold locks serializing us with truncate / punch hole so 2019 * this is a reliable test. 2020 */ 2021 max_pgoff = DIV_ROUND_UP(i_size_read(iter.inode), PAGE_SIZE); 2022 2023 trace_dax_pmd_fault(iter.inode, vmf, max_pgoff, 0); 2024 2025 if (xas.xa_index >= max_pgoff) { 2026 ret = VM_FAULT_SIGBUS; 2027 goto out; 2028 } 2029 2030 if (dax_fault_check_fallback(vmf, &xas, max_pgoff)) 2031 goto fallback; 2032 2033 /* 2034 * grab_mapping_entry() will make sure we get an empty PMD entry, 2035 * a zero PMD entry or a DAX PMD. If it can't (because a PTE 2036 * entry is already in the array, for instance), it will return 2037 * VM_FAULT_FALLBACK. 2038 */ 2039 entry = grab_mapping_entry(&xas, mapping, PMD_ORDER); 2040 if (xa_is_internal(entry)) { 2041 ret = xa_to_internal(entry); 2042 goto fallback; 2043 } 2044 2045 /* 2046 * It is possible, particularly with mixed reads & writes to private 2047 * mappings, that we have raced with a PTE fault that overlaps with 2048 * the PMD we need to set up. If so just return and the fault will be 2049 * retried. 2050 */ 2051 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd)) { 2052 ret = 0; 2053 goto unlock_entry; 2054 } 2055 2056 iter.pos = (loff_t)xas.xa_index << PAGE_SHIFT; 2057 while (iomap_iter(&iter, ops) > 0) { 2058 if (iomap_length(&iter) < PMD_SIZE) 2059 continue; /* actually breaks out of the loop */ 2060 2061 ret = dax_fault_iter(vmf, &iter, pfnp, &xas, &entry, true); 2062 if (ret != VM_FAULT_FALLBACK) { 2063 u64 length = PMD_SIZE; 2064 iter.status = iomap_iter_advance(&iter, &length); 2065 } 2066 } 2067 2068 unlock_entry: 2069 dax_unlock_entry(&xas, entry); 2070 fallback: 2071 if (ret == VM_FAULT_FALLBACK) { 2072 split_huge_pmd(vmf->vma, vmf->pmd, vmf->address); 2073 count_vm_event(THP_FAULT_FALLBACK); 2074 } 2075 out: 2076 trace_dax_pmd_fault_done(iter.inode, vmf, max_pgoff, ret); 2077 return ret; 2078 } 2079 #else 2080 static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 2081 const struct iomap_ops *ops) 2082 { 2083 return VM_FAULT_FALLBACK; 2084 } 2085 #endif /* CONFIG_FS_DAX_PMD */ 2086 2087 /** 2088 * dax_iomap_fault - handle a page fault on a DAX file 2089 * @vmf: The description of the fault 2090 * @order: Order of the page to fault in 2091 * @pfnp: PFN to insert for synchronous faults if fsync is required 2092 * @iomap_errp: Storage for detailed error code in case of error 2093 * @ops: Iomap ops passed from the file system 2094 * 2095 * When a page fault occurs, filesystems may call this helper in 2096 * their fault handler for DAX files. dax_iomap_fault() assumes the caller 2097 * has done all the necessary locking for page fault to proceed 2098 * successfully. 2099 */ 2100 vm_fault_t dax_iomap_fault(struct vm_fault *vmf, unsigned int order, 2101 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 2102 { 2103 if (order == 0) 2104 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 2105 else if (order == PMD_ORDER) 2106 return dax_iomap_pmd_fault(vmf, pfnp, ops); 2107 else 2108 return VM_FAULT_FALLBACK; 2109 } 2110 EXPORT_SYMBOL_GPL(dax_iomap_fault); 2111 2112 /* 2113 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 2114 * @vmf: The description of the fault 2115 * @pfn: PFN to insert 2116 * @order: Order of entry to insert. 2117 * 2118 * This function inserts a writeable PTE or PMD entry into the page tables 2119 * for an mmaped DAX file. It also marks the page cache entry as dirty. 2120 */ 2121 static vm_fault_t 2122 dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 2123 { 2124 struct address_space *mapping = vmf->vma->vm_file->f_mapping; 2125 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 2126 struct folio *folio; 2127 void *entry; 2128 vm_fault_t ret; 2129 2130 xas_lock_irq(&xas); 2131 entry = get_next_unlocked_entry(&xas, order); 2132 /* Did we race with someone splitting entry or so? */ 2133 if (!entry || dax_is_conflict(entry) || 2134 (order == 0 && !dax_is_pte_entry(entry))) { 2135 put_unlocked_entry(&xas, entry, WAKE_NEXT); 2136 xas_unlock_irq(&xas); 2137 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 2138 VM_FAULT_NOPAGE); 2139 return VM_FAULT_NOPAGE; 2140 } 2141 xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 2142 dax_lock_entry(&xas, entry); 2143 xas_unlock_irq(&xas); 2144 folio = pfn_folio(pfn_t_to_pfn(pfn)); 2145 folio_ref_inc(folio); 2146 if (order == 0) 2147 ret = vmf_insert_page_mkwrite(vmf, &folio->page, true); 2148 #ifdef CONFIG_FS_DAX_PMD 2149 else if (order == PMD_ORDER) 2150 ret = vmf_insert_folio_pmd(vmf, folio, FAULT_FLAG_WRITE); 2151 #endif 2152 else 2153 ret = VM_FAULT_FALLBACK; 2154 folio_put(folio); 2155 dax_unlock_entry(&xas, entry); 2156 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 2157 return ret; 2158 } 2159 2160 /** 2161 * dax_finish_sync_fault - finish synchronous page fault 2162 * @vmf: The description of the fault 2163 * @order: Order of entry to be inserted 2164 * @pfn: PFN to insert 2165 * 2166 * This function ensures that the file range touched by the page fault is 2167 * stored persistently on the media and handles inserting of appropriate page 2168 * table entry. 2169 */ 2170 vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, unsigned int order, 2171 pfn_t pfn) 2172 { 2173 int err; 2174 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 2175 size_t len = PAGE_SIZE << order; 2176 2177 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 2178 if (err) 2179 return VM_FAULT_SIGBUS; 2180 return dax_insert_pfn_mkwrite(vmf, pfn, order); 2181 } 2182 EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 2183 2184 static int dax_range_compare_iter(struct iomap_iter *it_src, 2185 struct iomap_iter *it_dest, u64 len, bool *same) 2186 { 2187 const struct iomap *smap = &it_src->iomap; 2188 const struct iomap *dmap = &it_dest->iomap; 2189 loff_t pos1 = it_src->pos, pos2 = it_dest->pos; 2190 u64 dest_len; 2191 void *saddr, *daddr; 2192 int id, ret; 2193 2194 len = min(len, min(smap->length, dmap->length)); 2195 2196 if (smap->type == IOMAP_HOLE && dmap->type == IOMAP_HOLE) { 2197 *same = true; 2198 goto advance; 2199 } 2200 2201 if (smap->type == IOMAP_HOLE || dmap->type == IOMAP_HOLE) { 2202 *same = false; 2203 return 0; 2204 } 2205 2206 id = dax_read_lock(); 2207 ret = dax_iomap_direct_access(smap, pos1, ALIGN(pos1 + len, PAGE_SIZE), 2208 &saddr, NULL); 2209 if (ret < 0) 2210 goto out_unlock; 2211 2212 ret = dax_iomap_direct_access(dmap, pos2, ALIGN(pos2 + len, PAGE_SIZE), 2213 &daddr, NULL); 2214 if (ret < 0) 2215 goto out_unlock; 2216 2217 *same = !memcmp(saddr, daddr, len); 2218 if (!*same) 2219 len = 0; 2220 dax_read_unlock(id); 2221 2222 advance: 2223 dest_len = len; 2224 ret = iomap_iter_advance(it_src, &len); 2225 if (!ret) 2226 ret = iomap_iter_advance(it_dest, &dest_len); 2227 return ret; 2228 2229 out_unlock: 2230 dax_read_unlock(id); 2231 return -EIO; 2232 } 2233 2234 int dax_dedupe_file_range_compare(struct inode *src, loff_t srcoff, 2235 struct inode *dst, loff_t dstoff, loff_t len, bool *same, 2236 const struct iomap_ops *ops) 2237 { 2238 struct iomap_iter src_iter = { 2239 .inode = src, 2240 .pos = srcoff, 2241 .len = len, 2242 .flags = IOMAP_DAX, 2243 }; 2244 struct iomap_iter dst_iter = { 2245 .inode = dst, 2246 .pos = dstoff, 2247 .len = len, 2248 .flags = IOMAP_DAX, 2249 }; 2250 int ret, status; 2251 2252 while ((ret = iomap_iter(&src_iter, ops)) > 0 && 2253 (ret = iomap_iter(&dst_iter, ops)) > 0) { 2254 status = dax_range_compare_iter(&src_iter, &dst_iter, 2255 min(src_iter.len, dst_iter.len), same); 2256 if (status < 0) 2257 return ret; 2258 src_iter.status = dst_iter.status = status; 2259 } 2260 return ret; 2261 } 2262 2263 int dax_remap_file_range_prep(struct file *file_in, loff_t pos_in, 2264 struct file *file_out, loff_t pos_out, 2265 loff_t *len, unsigned int remap_flags, 2266 const struct iomap_ops *ops) 2267 { 2268 return __generic_remap_file_range_prep(file_in, pos_in, file_out, 2269 pos_out, len, remap_flags, ops); 2270 } 2271 EXPORT_SYMBOL_GPL(dax_remap_file_range_prep); 2272