12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2d475c634SMatthew Wilcox /* 3d475c634SMatthew Wilcox * fs/dax.c - Direct Access filesystem code 4d475c634SMatthew Wilcox * Copyright (c) 2013-2014 Intel Corporation 5d475c634SMatthew Wilcox * Author: Matthew Wilcox <matthew.r.wilcox@intel.com> 6d475c634SMatthew Wilcox * Author: Ross Zwisler <ross.zwisler@linux.intel.com> 7d475c634SMatthew Wilcox */ 8d475c634SMatthew Wilcox 9d475c634SMatthew Wilcox #include <linux/atomic.h> 10d475c634SMatthew Wilcox #include <linux/blkdev.h> 11d475c634SMatthew Wilcox #include <linux/buffer_head.h> 12d77e92e2SRoss Zwisler #include <linux/dax.h> 13d475c634SMatthew Wilcox #include <linux/fs.h> 14d475c634SMatthew Wilcox #include <linux/genhd.h> 15f7ca90b1SMatthew Wilcox #include <linux/highmem.h> 16f7ca90b1SMatthew Wilcox #include <linux/memcontrol.h> 17f7ca90b1SMatthew Wilcox #include <linux/mm.h> 18d475c634SMatthew Wilcox #include <linux/mutex.h> 199973c98eSRoss Zwisler #include <linux/pagevec.h> 20289c6aedSMatthew Wilcox #include <linux/sched.h> 21f361bf4aSIngo Molnar #include <linux/sched/signal.h> 22d475c634SMatthew Wilcox #include <linux/uio.h> 23f7ca90b1SMatthew Wilcox #include <linux/vmstat.h> 2434c0fd54SDan Williams #include <linux/pfn_t.h> 250e749e54SDan Williams #include <linux/sizes.h> 264b4bb46dSJan Kara #include <linux/mmu_notifier.h> 27a254e568SChristoph Hellwig #include <linux/iomap.h> 2811cf9d86SAneesh Kumar K.V #include <asm/pgalloc.h> 29d475c634SMatthew Wilcox 30282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS 31282a8e03SRoss Zwisler #include <trace/events/fs_dax.h> 32282a8e03SRoss Zwisler 33cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size) 34cfc93c6cSMatthew Wilcox { 35cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PTE) 36cfc93c6cSMatthew Wilcox return PAGE_SHIFT - PAGE_SHIFT; 37cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PMD) 38cfc93c6cSMatthew Wilcox return PMD_SHIFT - PAGE_SHIFT; 39cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PUD) 40cfc93c6cSMatthew Wilcox return PUD_SHIFT - PAGE_SHIFT; 41cfc93c6cSMatthew Wilcox return ~0; 42cfc93c6cSMatthew Wilcox } 43cfc93c6cSMatthew Wilcox 44ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */ 45ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12 46ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 47ac401cc7SJan Kara 48917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset. */ 49917f3452SRoss Zwisler #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 50977fbdcdSMatthew Wilcox #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) 51917f3452SRoss Zwisler 52cfc93c6cSMatthew Wilcox /* The order of a PMD entry */ 53cfc93c6cSMatthew Wilcox #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) 54cfc93c6cSMatthew Wilcox 55ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 56ac401cc7SJan Kara 57ac401cc7SJan Kara static int __init init_dax_wait_table(void) 58ac401cc7SJan Kara { 59ac401cc7SJan Kara int i; 60ac401cc7SJan Kara 61ac401cc7SJan Kara for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 62ac401cc7SJan Kara init_waitqueue_head(wait_table + i); 63ac401cc7SJan Kara return 0; 64ac401cc7SJan Kara } 65ac401cc7SJan Kara fs_initcall(init_dax_wait_table); 66ac401cc7SJan Kara 67527b19d0SRoss Zwisler /* 683159f943SMatthew Wilcox * DAX pagecache entries use XArray value entries so they can't be mistaken 693159f943SMatthew Wilcox * for pages. We use one bit for locking, one bit for the entry size (PMD) 703159f943SMatthew Wilcox * and two more to tell us if the entry is a zero page or an empty entry that 713159f943SMatthew Wilcox * is just used for locking. In total four special bits. 72527b19d0SRoss Zwisler * 73527b19d0SRoss Zwisler * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE 74527b19d0SRoss Zwisler * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem 75527b19d0SRoss Zwisler * block allocation. 76527b19d0SRoss Zwisler */ 773159f943SMatthew Wilcox #define DAX_SHIFT (4) 783159f943SMatthew Wilcox #define DAX_LOCKED (1UL << 0) 793159f943SMatthew Wilcox #define DAX_PMD (1UL << 1) 803159f943SMatthew Wilcox #define DAX_ZERO_PAGE (1UL << 2) 813159f943SMatthew Wilcox #define DAX_EMPTY (1UL << 3) 82527b19d0SRoss Zwisler 83a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry) 84527b19d0SRoss Zwisler { 853159f943SMatthew Wilcox return xa_to_value(entry) >> DAX_SHIFT; 86527b19d0SRoss Zwisler } 87527b19d0SRoss Zwisler 889f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags) 899f32d221SMatthew Wilcox { 909f32d221SMatthew Wilcox return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT)); 919f32d221SMatthew Wilcox } 929f32d221SMatthew Wilcox 93cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry) 94cfc93c6cSMatthew Wilcox { 95cfc93c6cSMatthew Wilcox return xa_to_value(entry) & DAX_LOCKED; 96cfc93c6cSMatthew Wilcox } 97cfc93c6cSMatthew Wilcox 98a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry) 99527b19d0SRoss Zwisler { 1003159f943SMatthew Wilcox if (xa_to_value(entry) & DAX_PMD) 101cfc93c6cSMatthew Wilcox return PMD_ORDER; 102527b19d0SRoss Zwisler return 0; 103527b19d0SRoss Zwisler } 104527b19d0SRoss Zwisler 105fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry) 106642261acSRoss Zwisler { 1073159f943SMatthew Wilcox return xa_to_value(entry) & DAX_PMD; 108642261acSRoss Zwisler } 109642261acSRoss Zwisler 110fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry) 111642261acSRoss Zwisler { 1123159f943SMatthew Wilcox return !(xa_to_value(entry) & DAX_PMD); 113642261acSRoss Zwisler } 114642261acSRoss Zwisler 115642261acSRoss Zwisler static int dax_is_zero_entry(void *entry) 116642261acSRoss Zwisler { 1173159f943SMatthew Wilcox return xa_to_value(entry) & DAX_ZERO_PAGE; 118642261acSRoss Zwisler } 119642261acSRoss Zwisler 120642261acSRoss Zwisler static int dax_is_empty_entry(void *entry) 121642261acSRoss Zwisler { 1223159f943SMatthew Wilcox return xa_to_value(entry) & DAX_EMPTY; 123642261acSRoss Zwisler } 124642261acSRoss Zwisler 125f7ca90b1SMatthew Wilcox /* 12623c84eb7SMatthew Wilcox (Oracle) * true if the entry that was found is of a smaller order than the entry 12723c84eb7SMatthew Wilcox (Oracle) * we were looking for 12823c84eb7SMatthew Wilcox (Oracle) */ 12923c84eb7SMatthew Wilcox (Oracle) static bool dax_is_conflict(void *entry) 13023c84eb7SMatthew Wilcox (Oracle) { 13123c84eb7SMatthew Wilcox (Oracle) return entry == XA_RETRY_ENTRY; 13223c84eb7SMatthew Wilcox (Oracle) } 13323c84eb7SMatthew Wilcox (Oracle) 13423c84eb7SMatthew Wilcox (Oracle) /* 135a77d19f4SMatthew Wilcox * DAX page cache entry locking 136ac401cc7SJan Kara */ 137ac401cc7SJan Kara struct exceptional_entry_key { 138ec4907ffSMatthew Wilcox struct xarray *xa; 13963e95b5cSRoss Zwisler pgoff_t entry_start; 140ac401cc7SJan Kara }; 141ac401cc7SJan Kara 142ac401cc7SJan Kara struct wait_exceptional_entry_queue { 143ac6424b9SIngo Molnar wait_queue_entry_t wait; 144ac401cc7SJan Kara struct exceptional_entry_key key; 145ac401cc7SJan Kara }; 146ac401cc7SJan Kara 147698ab77aSVivek Goyal /** 148698ab77aSVivek Goyal * enum dax_wake_mode: waitqueue wakeup behaviour 149698ab77aSVivek Goyal * @WAKE_ALL: wake all waiters in the waitqueue 150698ab77aSVivek Goyal * @WAKE_NEXT: wake only the first waiter in the waitqueue 151698ab77aSVivek Goyal */ 152698ab77aSVivek Goyal enum dax_wake_mode { 153698ab77aSVivek Goyal WAKE_ALL, 154698ab77aSVivek Goyal WAKE_NEXT, 155698ab77aSVivek Goyal }; 156698ab77aSVivek Goyal 157b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas, 158b15cd800SMatthew Wilcox void *entry, struct exceptional_entry_key *key) 15963e95b5cSRoss Zwisler { 16063e95b5cSRoss Zwisler unsigned long hash; 161b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 16263e95b5cSRoss Zwisler 16363e95b5cSRoss Zwisler /* 16463e95b5cSRoss Zwisler * If 'entry' is a PMD, align the 'index' that we use for the wait 16563e95b5cSRoss Zwisler * queue to the start of that PMD. This ensures that all offsets in 16663e95b5cSRoss Zwisler * the range covered by the PMD map to the same bit lock. 16763e95b5cSRoss Zwisler */ 168642261acSRoss Zwisler if (dax_is_pmd_entry(entry)) 169917f3452SRoss Zwisler index &= ~PG_PMD_COLOUR; 170b15cd800SMatthew Wilcox key->xa = xas->xa; 17163e95b5cSRoss Zwisler key->entry_start = index; 17263e95b5cSRoss Zwisler 173b15cd800SMatthew Wilcox hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS); 17463e95b5cSRoss Zwisler return wait_table + hash; 17563e95b5cSRoss Zwisler } 17663e95b5cSRoss Zwisler 177ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait, 178ec4907ffSMatthew Wilcox unsigned int mode, int sync, void *keyp) 179ac401cc7SJan Kara { 180ac401cc7SJan Kara struct exceptional_entry_key *key = keyp; 181ac401cc7SJan Kara struct wait_exceptional_entry_queue *ewait = 182ac401cc7SJan Kara container_of(wait, struct wait_exceptional_entry_queue, wait); 183ac401cc7SJan Kara 184ec4907ffSMatthew Wilcox if (key->xa != ewait->key.xa || 18563e95b5cSRoss Zwisler key->entry_start != ewait->key.entry_start) 186ac401cc7SJan Kara return 0; 187ac401cc7SJan Kara return autoremove_wake_function(wait, mode, sync, NULL); 188ac401cc7SJan Kara } 189ac401cc7SJan Kara 190ac401cc7SJan Kara /* 191b93b0163SMatthew Wilcox * @entry may no longer be the entry at the index in the mapping. 192b93b0163SMatthew Wilcox * The important information it's conveying is whether the entry at 193b93b0163SMatthew Wilcox * this index used to be a PMD entry. 194e30331ffSRoss Zwisler */ 195698ab77aSVivek Goyal static void dax_wake_entry(struct xa_state *xas, void *entry, 196698ab77aSVivek Goyal enum dax_wake_mode mode) 197e30331ffSRoss Zwisler { 198e30331ffSRoss Zwisler struct exceptional_entry_key key; 199e30331ffSRoss Zwisler wait_queue_head_t *wq; 200e30331ffSRoss Zwisler 201b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &key); 202e30331ffSRoss Zwisler 203e30331ffSRoss Zwisler /* 204e30331ffSRoss Zwisler * Checking for locked entry and prepare_to_wait_exclusive() happens 205b93b0163SMatthew Wilcox * under the i_pages lock, ditto for entry handling in our callers. 206e30331ffSRoss Zwisler * So at this point all tasks that could have seen our entry locked 207e30331ffSRoss Zwisler * must be in the waitqueue and the following check will see them. 208e30331ffSRoss Zwisler */ 209e30331ffSRoss Zwisler if (waitqueue_active(wq)) 210698ab77aSVivek Goyal __wake_up(wq, TASK_NORMAL, mode == WAKE_ALL ? 0 : 1, &key); 211e30331ffSRoss Zwisler } 212e30331ffSRoss Zwisler 213cfc93c6cSMatthew Wilcox /* 214cfc93c6cSMatthew Wilcox * Look up entry in page cache, wait for it to become unlocked if it 215cfc93c6cSMatthew Wilcox * is a DAX entry and return it. The caller must subsequently call 216cfc93c6cSMatthew Wilcox * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry() 21723c84eb7SMatthew Wilcox (Oracle) * if it did. The entry returned may have a larger order than @order. 21823c84eb7SMatthew Wilcox (Oracle) * If @order is larger than the order of the entry found in i_pages, this 21923c84eb7SMatthew Wilcox (Oracle) * function returns a dax_is_conflict entry. 220cfc93c6cSMatthew Wilcox * 221cfc93c6cSMatthew Wilcox * Must be called with the i_pages lock held. 222cfc93c6cSMatthew Wilcox */ 22323c84eb7SMatthew Wilcox (Oracle) static void *get_unlocked_entry(struct xa_state *xas, unsigned int order) 224cfc93c6cSMatthew Wilcox { 225cfc93c6cSMatthew Wilcox void *entry; 226cfc93c6cSMatthew Wilcox struct wait_exceptional_entry_queue ewait; 227cfc93c6cSMatthew Wilcox wait_queue_head_t *wq; 228cfc93c6cSMatthew Wilcox 229cfc93c6cSMatthew Wilcox init_wait(&ewait.wait); 230cfc93c6cSMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 231cfc93c6cSMatthew Wilcox 232cfc93c6cSMatthew Wilcox for (;;) { 2330e40de03SMatthew Wilcox entry = xas_find_conflict(xas); 2346370740eSDan Williams if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 2356370740eSDan Williams return entry; 23623c84eb7SMatthew Wilcox (Oracle) if (dax_entry_order(entry) < order) 23723c84eb7SMatthew Wilcox (Oracle) return XA_RETRY_ENTRY; 2386370740eSDan Williams if (!dax_is_locked(entry)) 239cfc93c6cSMatthew Wilcox return entry; 240cfc93c6cSMatthew Wilcox 241b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 242cfc93c6cSMatthew Wilcox prepare_to_wait_exclusive(wq, &ewait.wait, 243cfc93c6cSMatthew Wilcox TASK_UNINTERRUPTIBLE); 244cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 245cfc93c6cSMatthew Wilcox xas_reset(xas); 246cfc93c6cSMatthew Wilcox schedule(); 247cfc93c6cSMatthew Wilcox finish_wait(wq, &ewait.wait); 248cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 249cfc93c6cSMatthew Wilcox } 250cfc93c6cSMatthew Wilcox } 251cfc93c6cSMatthew Wilcox 25255e56f06SMatthew Wilcox /* 25355e56f06SMatthew Wilcox * The only thing keeping the address space around is the i_pages lock 25455e56f06SMatthew Wilcox * (it's cycled in clear_inode() after removing the entries from i_pages) 25555e56f06SMatthew Wilcox * After we call xas_unlock_irq(), we cannot touch xas->xa. 25655e56f06SMatthew Wilcox */ 25755e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry) 25855e56f06SMatthew Wilcox { 25955e56f06SMatthew Wilcox struct wait_exceptional_entry_queue ewait; 26055e56f06SMatthew Wilcox wait_queue_head_t *wq; 26155e56f06SMatthew Wilcox 26255e56f06SMatthew Wilcox init_wait(&ewait.wait); 26355e56f06SMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 26455e56f06SMatthew Wilcox 26555e56f06SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 266d8a70641SDan Williams /* 267d8a70641SDan Williams * Unlike get_unlocked_entry() there is no guarantee that this 268d8a70641SDan Williams * path ever successfully retrieves an unlocked entry before an 269d8a70641SDan Williams * inode dies. Perform a non-exclusive wait in case this path 270d8a70641SDan Williams * never successfully performs its own wake up. 271d8a70641SDan Williams */ 272d8a70641SDan Williams prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); 27355e56f06SMatthew Wilcox xas_unlock_irq(xas); 27455e56f06SMatthew Wilcox schedule(); 27555e56f06SMatthew Wilcox finish_wait(wq, &ewait.wait); 27655e56f06SMatthew Wilcox } 27755e56f06SMatthew Wilcox 2784c3d043dSVivek Goyal static void put_unlocked_entry(struct xa_state *xas, void *entry, 2794c3d043dSVivek Goyal enum dax_wake_mode mode) 280cfc93c6cSMatthew Wilcox { 28161c30c98SJan Kara if (entry && !dax_is_conflict(entry)) 2824c3d043dSVivek Goyal dax_wake_entry(xas, entry, mode); 283cfc93c6cSMatthew Wilcox } 284cfc93c6cSMatthew Wilcox 285cfc93c6cSMatthew Wilcox /* 286cfc93c6cSMatthew Wilcox * We used the xa_state to get the entry, but then we locked the entry and 287cfc93c6cSMatthew Wilcox * dropped the xa_lock, so we know the xa_state is stale and must be reset 288cfc93c6cSMatthew Wilcox * before use. 289cfc93c6cSMatthew Wilcox */ 290cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry) 291cfc93c6cSMatthew Wilcox { 292cfc93c6cSMatthew Wilcox void *old; 293cfc93c6cSMatthew Wilcox 2947ae2ea7dSMatthew Wilcox BUG_ON(dax_is_locked(entry)); 295cfc93c6cSMatthew Wilcox xas_reset(xas); 296cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 297cfc93c6cSMatthew Wilcox old = xas_store(xas, entry); 298cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 299cfc93c6cSMatthew Wilcox BUG_ON(!dax_is_locked(old)); 300698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_NEXT); 301cfc93c6cSMatthew Wilcox } 302cfc93c6cSMatthew Wilcox 303cfc93c6cSMatthew Wilcox /* 304cfc93c6cSMatthew Wilcox * Return: The entry stored at this location before it was locked. 305cfc93c6cSMatthew Wilcox */ 306cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry) 307cfc93c6cSMatthew Wilcox { 308cfc93c6cSMatthew Wilcox unsigned long v = xa_to_value(entry); 309cfc93c6cSMatthew Wilcox return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 310cfc93c6cSMatthew Wilcox } 311cfc93c6cSMatthew Wilcox 312d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry) 313d2c997c0SDan Williams { 314d2c997c0SDan Williams if (dax_is_zero_entry(entry)) 315d2c997c0SDan Williams return 0; 316d2c997c0SDan Williams else if (dax_is_empty_entry(entry)) 317d2c997c0SDan Williams return 0; 318d2c997c0SDan Williams else if (dax_is_pmd_entry(entry)) 319d2c997c0SDan Williams return PMD_SIZE; 320d2c997c0SDan Williams else 321d2c997c0SDan Williams return PAGE_SIZE; 322d2c997c0SDan Williams } 323d2c997c0SDan Williams 324a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry) 325d2c997c0SDan Williams { 326a77d19f4SMatthew Wilcox return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE; 327d2c997c0SDan Williams } 328d2c997c0SDan Williams 329d2c997c0SDan Williams /* 330d2c997c0SDan Williams * Iterate through all mapped pfns represented by an entry, i.e. skip 331d2c997c0SDan Williams * 'empty' and 'zero' entries. 332d2c997c0SDan Williams */ 333d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \ 334a77d19f4SMatthew Wilcox for (pfn = dax_to_pfn(entry); \ 335a77d19f4SMatthew Wilcox pfn < dax_end_pfn(entry); pfn++) 336d2c997c0SDan Williams 33773449dafSDan Williams /* 33873449dafSDan Williams * TODO: for reflink+dax we need a way to associate a single page with 33973449dafSDan Williams * multiple address_space instances at different linear_page_index() 34073449dafSDan Williams * offsets. 34173449dafSDan Williams */ 34273449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping, 34373449dafSDan Williams struct vm_area_struct *vma, unsigned long address) 344d2c997c0SDan Williams { 34573449dafSDan Williams unsigned long size = dax_entry_size(entry), pfn, index; 34673449dafSDan Williams int i = 0; 347d2c997c0SDan Williams 348d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 349d2c997c0SDan Williams return; 350d2c997c0SDan Williams 35173449dafSDan Williams index = linear_page_index(vma, address & ~(size - 1)); 352d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 353d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 354d2c997c0SDan Williams 355d2c997c0SDan Williams WARN_ON_ONCE(page->mapping); 356d2c997c0SDan Williams page->mapping = mapping; 35773449dafSDan Williams page->index = index + i++; 358d2c997c0SDan Williams } 359d2c997c0SDan Williams } 360d2c997c0SDan Williams 361d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping, 362d2c997c0SDan Williams bool trunc) 363d2c997c0SDan Williams { 364d2c997c0SDan Williams unsigned long pfn; 365d2c997c0SDan Williams 366d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 367d2c997c0SDan Williams return; 368d2c997c0SDan Williams 369d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 370d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 371d2c997c0SDan Williams 372d2c997c0SDan Williams WARN_ON_ONCE(trunc && page_ref_count(page) > 1); 373d2c997c0SDan Williams WARN_ON_ONCE(page->mapping && page->mapping != mapping); 374d2c997c0SDan Williams page->mapping = NULL; 37573449dafSDan Williams page->index = 0; 376d2c997c0SDan Williams } 377d2c997c0SDan Williams } 378d2c997c0SDan Williams 3795fac7408SDan Williams static struct page *dax_busy_page(void *entry) 3805fac7408SDan Williams { 3815fac7408SDan Williams unsigned long pfn; 3825fac7408SDan Williams 3835fac7408SDan Williams for_each_mapped_pfn(entry, pfn) { 3845fac7408SDan Williams struct page *page = pfn_to_page(pfn); 3855fac7408SDan Williams 3865fac7408SDan Williams if (page_ref_count(page) > 1) 3875fac7408SDan Williams return page; 3885fac7408SDan Williams } 3895fac7408SDan Williams return NULL; 3905fac7408SDan Williams } 3915fac7408SDan Williams 392c5bbd451SMatthew Wilcox /* 393c5bbd451SMatthew Wilcox * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page 394c5bbd451SMatthew Wilcox * @page: The page whose entry we want to lock 395c5bbd451SMatthew Wilcox * 396c5bbd451SMatthew Wilcox * Context: Process context. 39727359fd6SMatthew Wilcox * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could 39827359fd6SMatthew Wilcox * not be locked. 399c5bbd451SMatthew Wilcox */ 40027359fd6SMatthew Wilcox dax_entry_t dax_lock_page(struct page *page) 401c2a7d2a1SDan Williams { 4029f32d221SMatthew Wilcox XA_STATE(xas, NULL, 0); 4039f32d221SMatthew Wilcox void *entry; 404c2a7d2a1SDan Williams 405c5bbd451SMatthew Wilcox /* Ensure page->mapping isn't freed while we look at it */ 406c5bbd451SMatthew Wilcox rcu_read_lock(); 407c2a7d2a1SDan Williams for (;;) { 4089f32d221SMatthew Wilcox struct address_space *mapping = READ_ONCE(page->mapping); 409c2a7d2a1SDan Williams 41027359fd6SMatthew Wilcox entry = NULL; 411c93db7bbSMatthew Wilcox if (!mapping || !dax_mapping(mapping)) 412c5bbd451SMatthew Wilcox break; 413c2a7d2a1SDan Williams 414c2a7d2a1SDan Williams /* 415c2a7d2a1SDan Williams * In the device-dax case there's no need to lock, a 416c2a7d2a1SDan Williams * struct dev_pagemap pin is sufficient to keep the 417c2a7d2a1SDan Williams * inode alive, and we assume we have dev_pagemap pin 418c2a7d2a1SDan Williams * otherwise we would not have a valid pfn_to_page() 419c2a7d2a1SDan Williams * translation. 420c2a7d2a1SDan Williams */ 42127359fd6SMatthew Wilcox entry = (void *)~0UL; 4229f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 423c5bbd451SMatthew Wilcox break; 424c2a7d2a1SDan Williams 4259f32d221SMatthew Wilcox xas.xa = &mapping->i_pages; 4269f32d221SMatthew Wilcox xas_lock_irq(&xas); 427c2a7d2a1SDan Williams if (mapping != page->mapping) { 4289f32d221SMatthew Wilcox xas_unlock_irq(&xas); 429c2a7d2a1SDan Williams continue; 430c2a7d2a1SDan Williams } 4319f32d221SMatthew Wilcox xas_set(&xas, page->index); 4329f32d221SMatthew Wilcox entry = xas_load(&xas); 4339f32d221SMatthew Wilcox if (dax_is_locked(entry)) { 434c5bbd451SMatthew Wilcox rcu_read_unlock(); 43555e56f06SMatthew Wilcox wait_entry_unlocked(&xas, entry); 436c5bbd451SMatthew Wilcox rcu_read_lock(); 437c2a7d2a1SDan Williams continue; 438c2a7d2a1SDan Williams } 4399f32d221SMatthew Wilcox dax_lock_entry(&xas, entry); 4409f32d221SMatthew Wilcox xas_unlock_irq(&xas); 441c5bbd451SMatthew Wilcox break; 4429f32d221SMatthew Wilcox } 443c5bbd451SMatthew Wilcox rcu_read_unlock(); 44427359fd6SMatthew Wilcox return (dax_entry_t)entry; 445c2a7d2a1SDan Williams } 446c2a7d2a1SDan Williams 44727359fd6SMatthew Wilcox void dax_unlock_page(struct page *page, dax_entry_t cookie) 448c2a7d2a1SDan Williams { 449c2a7d2a1SDan Williams struct address_space *mapping = page->mapping; 4509f32d221SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, page->index); 451c2a7d2a1SDan Williams 4529f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 453c2a7d2a1SDan Williams return; 454c2a7d2a1SDan Williams 45527359fd6SMatthew Wilcox dax_unlock_entry(&xas, (void *)cookie); 456c2a7d2a1SDan Williams } 457c2a7d2a1SDan Williams 458ac401cc7SJan Kara /* 459a77d19f4SMatthew Wilcox * Find page cache entry at given index. If it is a DAX entry, return it 460a77d19f4SMatthew Wilcox * with the entry locked. If the page cache doesn't contain an entry at 461a77d19f4SMatthew Wilcox * that index, add a locked empty entry. 462ac401cc7SJan Kara * 4633159f943SMatthew Wilcox * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 464b15cd800SMatthew Wilcox * either return that locked entry or will return VM_FAULT_FALLBACK. 465b15cd800SMatthew Wilcox * This will happen if there are any PTE entries within the PMD range 466b15cd800SMatthew Wilcox * that we are requesting. 467642261acSRoss Zwisler * 468b15cd800SMatthew Wilcox * We always favor PTE entries over PMD entries. There isn't a flow where we 469b15cd800SMatthew Wilcox * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 470b15cd800SMatthew Wilcox * insertion will fail if it finds any PTE entries already in the tree, and a 471b15cd800SMatthew Wilcox * PTE insertion will cause an existing PMD entry to be unmapped and 472b15cd800SMatthew Wilcox * downgraded to PTE entries. This happens for both PMD zero pages as 473b15cd800SMatthew Wilcox * well as PMD empty entries. 474642261acSRoss Zwisler * 475b15cd800SMatthew Wilcox * The exception to this downgrade path is for PMD entries that have 476b15cd800SMatthew Wilcox * real storage backing them. We will leave these real PMD entries in 477b15cd800SMatthew Wilcox * the tree, and PTE writes will simply dirty the entire PMD entry. 478642261acSRoss Zwisler * 479ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 480ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 481ac401cc7SJan Kara * show it helps. 482b15cd800SMatthew Wilcox * 483b15cd800SMatthew Wilcox * On error, this function does not return an ERR_PTR. Instead it returns 484b15cd800SMatthew Wilcox * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 485b15cd800SMatthew Wilcox * overlap with xarray value entries. 486ac401cc7SJan Kara */ 487b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas, 48823c84eb7SMatthew Wilcox (Oracle) struct address_space *mapping, unsigned int order) 489ac401cc7SJan Kara { 490b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 491*1a14e377SJan Kara bool pmd_downgrade; /* splitting PMD entry into PTE entries? */ 492b15cd800SMatthew Wilcox void *entry; 493ac401cc7SJan Kara 494b15cd800SMatthew Wilcox retry: 495*1a14e377SJan Kara pmd_downgrade = false; 496b15cd800SMatthew Wilcox xas_lock_irq(xas); 49723c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(xas, order); 498642261acSRoss Zwisler 499b15cd800SMatthew Wilcox if (entry) { 50023c84eb7SMatthew Wilcox (Oracle) if (dax_is_conflict(entry)) 50123c84eb7SMatthew Wilcox (Oracle) goto fallback; 5020e40de03SMatthew Wilcox if (!xa_is_value(entry)) { 50349688e65SHao Li xas_set_err(xas, -EIO); 50491d25ba8SRoss Zwisler goto out_unlock; 50591d25ba8SRoss Zwisler } 50691d25ba8SRoss Zwisler 50723c84eb7SMatthew Wilcox (Oracle) if (order == 0) { 50891d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 509642261acSRoss Zwisler (dax_is_zero_entry(entry) || 510642261acSRoss Zwisler dax_is_empty_entry(entry))) { 511642261acSRoss Zwisler pmd_downgrade = true; 512642261acSRoss Zwisler } 513642261acSRoss Zwisler } 514642261acSRoss Zwisler } 515642261acSRoss Zwisler 516642261acSRoss Zwisler if (pmd_downgrade) { 517642261acSRoss Zwisler /* 518642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 519b93b0163SMatthew Wilcox * the i_pages lock. 520642261acSRoss Zwisler */ 521b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 522642261acSRoss Zwisler 523642261acSRoss Zwisler /* 524642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 525642261acSRoss Zwisler * downgraded are empty entries which don't need to be 526642261acSRoss Zwisler * unmapped. 527642261acSRoss Zwisler */ 528b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 529b15cd800SMatthew Wilcox xas_unlock_irq(xas); 530b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 531b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 532977fbdcdSMatthew Wilcox PG_PMD_NR, false); 533b15cd800SMatthew Wilcox xas_reset(xas); 534b15cd800SMatthew Wilcox xas_lock_irq(xas); 535e11f8b7bSRoss Zwisler } 536e11f8b7bSRoss Zwisler 537d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 538b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 539698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_ALL); 5407f0e07fbSMatthew Wilcox (Oracle) mapping->nrpages -= PG_PMD_NR; 541b15cd800SMatthew Wilcox entry = NULL; 542b15cd800SMatthew Wilcox xas_set(xas, index); 543642261acSRoss Zwisler } 544642261acSRoss Zwisler 545b15cd800SMatthew Wilcox if (entry) { 546b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 547b15cd800SMatthew Wilcox } else { 54823c84eb7SMatthew Wilcox (Oracle) unsigned long flags = DAX_EMPTY; 54923c84eb7SMatthew Wilcox (Oracle) 55023c84eb7SMatthew Wilcox (Oracle) if (order > 0) 55123c84eb7SMatthew Wilcox (Oracle) flags |= DAX_PMD; 55223c84eb7SMatthew Wilcox (Oracle) entry = dax_make_entry(pfn_to_pfn_t(0), flags); 553b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 554b15cd800SMatthew Wilcox if (xas_error(xas)) 555b15cd800SMatthew Wilcox goto out_unlock; 5567f0e07fbSMatthew Wilcox (Oracle) mapping->nrpages += 1UL << order; 557ac401cc7SJan Kara } 558b15cd800SMatthew Wilcox 559642261acSRoss Zwisler out_unlock: 560b15cd800SMatthew Wilcox xas_unlock_irq(xas); 561b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 562b15cd800SMatthew Wilcox goto retry; 563b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 564b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 565b15cd800SMatthew Wilcox if (xas_error(xas)) 566b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 567e3ad61c6SRoss Zwisler return entry; 568b15cd800SMatthew Wilcox fallback: 569b15cd800SMatthew Wilcox xas_unlock_irq(xas); 570b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 571ac401cc7SJan Kara } 572ac401cc7SJan Kara 5735fac7408SDan Williams /** 5746bbdd563SVivek Goyal * dax_layout_busy_page_range - find first pinned page in @mapping 5755fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5766bbdd563SVivek Goyal * @start: Starting offset. Page containing 'start' is included. 5776bbdd563SVivek Goyal * @end: End offset. Page containing 'end' is included. If 'end' is LLONG_MAX, 5786bbdd563SVivek Goyal * pages from 'start' till the end of file are included. 5795fac7408SDan Williams * 5805fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5815fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5825fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5835fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5845fac7408SDan Williams * get_user_pages() usages. 5855fac7408SDan Williams * 5865fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5875fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5885fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5895fac7408SDan Williams * mapping_mapped() becoming true. 5905fac7408SDan Williams */ 5916bbdd563SVivek Goyal struct page *dax_layout_busy_page_range(struct address_space *mapping, 5926bbdd563SVivek Goyal loff_t start, loff_t end) 5935fac7408SDan Williams { 594084a8990SMatthew Wilcox void *entry; 595084a8990SMatthew Wilcox unsigned int scanned = 0; 5965fac7408SDan Williams struct page *page = NULL; 5976bbdd563SVivek Goyal pgoff_t start_idx = start >> PAGE_SHIFT; 5986bbdd563SVivek Goyal pgoff_t end_idx; 5996bbdd563SVivek Goyal XA_STATE(xas, &mapping->i_pages, start_idx); 6005fac7408SDan Williams 6015fac7408SDan Williams /* 6025fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 6035fac7408SDan Williams */ 6045fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 6055fac7408SDan Williams return NULL; 6065fac7408SDan Williams 6075fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 6085fac7408SDan Williams return NULL; 6095fac7408SDan Williams 6106bbdd563SVivek Goyal /* If end == LLONG_MAX, all pages from start to till end of file */ 6116bbdd563SVivek Goyal if (end == LLONG_MAX) 6126bbdd563SVivek Goyal end_idx = ULONG_MAX; 6136bbdd563SVivek Goyal else 6146bbdd563SVivek Goyal end_idx = end >> PAGE_SHIFT; 6155fac7408SDan Williams /* 6165fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 617084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 6185fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 6195fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 6205fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 6215fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 6226bbdd563SVivek Goyal * holding those locks, and unmap_mapping_pages() will not zero the 6235fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 6245fac7408SDan Williams * guaranteed to either see new references or prevent new 6255fac7408SDan Williams * references from being established. 6265fac7408SDan Williams */ 6276bbdd563SVivek Goyal unmap_mapping_pages(mapping, start_idx, end_idx - start_idx + 1, 0); 6285fac7408SDan Williams 629084a8990SMatthew Wilcox xas_lock_irq(&xas); 6306bbdd563SVivek Goyal xas_for_each(&xas, entry, end_idx) { 631084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 6325fac7408SDan Williams continue; 633084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 63423c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, 0); 6355fac7408SDan Williams if (entry) 6365fac7408SDan Williams page = dax_busy_page(entry); 6374c3d043dSVivek Goyal put_unlocked_entry(&xas, entry, WAKE_NEXT); 6385fac7408SDan Williams if (page) 6395fac7408SDan Williams break; 640084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 641084a8990SMatthew Wilcox continue; 642cdbf8897SRoss Zwisler 643084a8990SMatthew Wilcox xas_pause(&xas); 644084a8990SMatthew Wilcox xas_unlock_irq(&xas); 645084a8990SMatthew Wilcox cond_resched(); 646084a8990SMatthew Wilcox xas_lock_irq(&xas); 6475fac7408SDan Williams } 648084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6495fac7408SDan Williams return page; 6505fac7408SDan Williams } 6516bbdd563SVivek Goyal EXPORT_SYMBOL_GPL(dax_layout_busy_page_range); 6526bbdd563SVivek Goyal 6536bbdd563SVivek Goyal struct page *dax_layout_busy_page(struct address_space *mapping) 6546bbdd563SVivek Goyal { 6556bbdd563SVivek Goyal return dax_layout_busy_page_range(mapping, 0, LLONG_MAX); 6566bbdd563SVivek Goyal } 6575fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6585fac7408SDan Williams 659a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 660c6dcf52cSJan Kara pgoff_t index, bool trunc) 661c6dcf52cSJan Kara { 66207f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 663c6dcf52cSJan Kara int ret = 0; 664c6dcf52cSJan Kara void *entry; 665c6dcf52cSJan Kara 66607f2d89cSMatthew Wilcox xas_lock_irq(&xas); 66723c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, 0); 6683159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 669c6dcf52cSJan Kara goto out; 670c6dcf52cSJan Kara if (!trunc && 67107f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 67207f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 673c6dcf52cSJan Kara goto out; 674d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 67507f2d89cSMatthew Wilcox xas_store(&xas, NULL); 6767f0e07fbSMatthew Wilcox (Oracle) mapping->nrpages -= 1UL << dax_entry_order(entry); 677c6dcf52cSJan Kara ret = 1; 678c6dcf52cSJan Kara out: 67923738832SVivek Goyal put_unlocked_entry(&xas, entry, WAKE_ALL); 68007f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 681c6dcf52cSJan Kara return ret; 682c6dcf52cSJan Kara } 68307f2d89cSMatthew Wilcox 684ac401cc7SJan Kara /* 6853159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6863159f943SMatthew Wilcox * to be unlocked before deleting it. 687ac401cc7SJan Kara */ 688ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 689ac401cc7SJan Kara { 690a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 691ac401cc7SJan Kara 692ac401cc7SJan Kara /* 693ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 694ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 695a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6963159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 697ac401cc7SJan Kara * at that index as well... 698ac401cc7SJan Kara */ 699c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 700c6dcf52cSJan Kara return ret; 701ac401cc7SJan Kara } 702ac401cc7SJan Kara 703c6dcf52cSJan Kara /* 7043159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 705c6dcf52cSJan Kara */ 706c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 707c6dcf52cSJan Kara pgoff_t index) 708c6dcf52cSJan Kara { 709a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 710ac401cc7SJan Kara } 711ac401cc7SJan Kara 712c7fe193fSIra Weiny static int copy_cow_page_dax(struct block_device *bdev, struct dax_device *dax_dev, 713c7fe193fSIra Weiny sector_t sector, struct page *to, unsigned long vaddr) 714f7ca90b1SMatthew Wilcox { 715cccbce67SDan Williams void *vto, *kaddr; 716cccbce67SDan Williams pgoff_t pgoff; 717cccbce67SDan Williams long rc; 718cccbce67SDan Williams int id; 719e2e05394SRoss Zwisler 720c7fe193fSIra Weiny rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 721cccbce67SDan Williams if (rc) 722cccbce67SDan Williams return rc; 723cccbce67SDan Williams 724cccbce67SDan Williams id = dax_read_lock(); 725c7fe193fSIra Weiny rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(PAGE_SIZE), &kaddr, NULL); 726cccbce67SDan Williams if (rc < 0) { 727cccbce67SDan Williams dax_read_unlock(id); 728cccbce67SDan Williams return rc; 729cccbce67SDan Williams } 730f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 731cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 732f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 733cccbce67SDan Williams dax_read_unlock(id); 734f7ca90b1SMatthew Wilcox return 0; 735f7ca90b1SMatthew Wilcox } 736f7ca90b1SMatthew Wilcox 737642261acSRoss Zwisler /* 738642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 739642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 740642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 741642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 742642261acSRoss Zwisler * appropriate. 743642261acSRoss Zwisler */ 744b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 745b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 746b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 7479973c98eSRoss Zwisler { 748b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 7499973c98eSRoss Zwisler 750f5b7b748SJan Kara if (dirty) 7519973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7529973c98eSRoss Zwisler 7533159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 754b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 75591d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 75691d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 757977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 758977fbdcdSMatthew Wilcox PG_PMD_NR, false); 75991d25ba8SRoss Zwisler else /* pte entry */ 760b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 761ac401cc7SJan Kara } 7629973c98eSRoss Zwisler 763b15cd800SMatthew Wilcox xas_reset(xas); 764b15cd800SMatthew Wilcox xas_lock_irq(xas); 7651571c029SJan Kara if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 7661571c029SJan Kara void *old; 7671571c029SJan Kara 768d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 76973449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 770642261acSRoss Zwisler /* 771a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 772642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 773a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 774642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 775642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 776642261acSRoss Zwisler * tree and dirty it if necessary. 777642261acSRoss Zwisler */ 7781571c029SJan Kara old = dax_lock_entry(xas, new_entry); 779b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 780b15cd800SMatthew Wilcox DAX_LOCKED)); 78191d25ba8SRoss Zwisler entry = new_entry; 782b15cd800SMatthew Wilcox } else { 783b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 784ac401cc7SJan Kara } 78591d25ba8SRoss Zwisler 786f5b7b748SJan Kara if (dirty) 787b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 78891d25ba8SRoss Zwisler 789b15cd800SMatthew Wilcox xas_unlock_irq(xas); 79091d25ba8SRoss Zwisler return entry; 7919973c98eSRoss Zwisler } 7929973c98eSRoss Zwisler 793a77d19f4SMatthew Wilcox static inline 794a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7954b4bb46dSJan Kara { 7964b4bb46dSJan Kara unsigned long address; 7974b4bb46dSJan Kara 7984b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7994b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 8004b4bb46dSJan Kara return address; 8014b4bb46dSJan Kara } 8024b4bb46dSJan Kara 8034b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 804a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 805a77d19f4SMatthew Wilcox unsigned long pfn) 8064b4bb46dSJan Kara { 8074b4bb46dSJan Kara struct vm_area_struct *vma; 808f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 809f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 8104b4bb46dSJan Kara spinlock_t *ptl; 8114b4bb46dSJan Kara 8124b4bb46dSJan Kara i_mmap_lock_read(mapping); 8134b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 814ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 815ac46d4f3SJérôme Glisse unsigned long address; 8164b4bb46dSJan Kara 8174b4bb46dSJan Kara cond_resched(); 8184b4bb46dSJan Kara 8194b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 8204b4bb46dSJan Kara continue; 8214b4bb46dSJan Kara 8224b4bb46dSJan Kara address = pgoff_address(index, vma); 823a4d1a885SJérôme Glisse 824a4d1a885SJérôme Glisse /* 8259fd6dad1SPaolo Bonzini * follow_invalidate_pte() will use the range to call 826ff5c19edSChristoph Hellwig * mmu_notifier_invalidate_range_start() on our behalf before 827ff5c19edSChristoph Hellwig * taking any lock. 828a4d1a885SJérôme Glisse */ 8299fd6dad1SPaolo Bonzini if (follow_invalidate_pte(vma->vm_mm, address, &range, &ptep, 8309fd6dad1SPaolo Bonzini &pmdp, &ptl)) 8314b4bb46dSJan Kara continue; 832f729c8c9SRoss Zwisler 8330f10851eSJérôme Glisse /* 8340f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 8350f10851eSJérôme Glisse * downgrading page table protection not changing it to point 8360f10851eSJérôme Glisse * to a new page. 8370f10851eSJérôme Glisse * 838ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 8390f10851eSJérôme Glisse */ 840f729c8c9SRoss Zwisler if (pmdp) { 841f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 842f729c8c9SRoss Zwisler pmd_t pmd; 843f729c8c9SRoss Zwisler 844f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 845f729c8c9SRoss Zwisler goto unlock_pmd; 846f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 847f729c8c9SRoss Zwisler goto unlock_pmd; 848f729c8c9SRoss Zwisler 849f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 850024eee0eSAneesh Kumar K.V pmd = pmdp_invalidate(vma, address, pmdp); 851f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 852f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 853f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 854f729c8c9SRoss Zwisler unlock_pmd: 855f729c8c9SRoss Zwisler #endif 856ee190ca6SJan H. Schönherr spin_unlock(ptl); 857f729c8c9SRoss Zwisler } else { 8584b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 859f729c8c9SRoss Zwisler goto unlock_pte; 8604b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 861f729c8c9SRoss Zwisler goto unlock_pte; 8624b4bb46dSJan Kara 8634b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8644b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8654b4bb46dSJan Kara pte = pte_wrprotect(pte); 8664b4bb46dSJan Kara pte = pte_mkclean(pte); 8674b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 868f729c8c9SRoss Zwisler unlock_pte: 8694b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 870f729c8c9SRoss Zwisler } 8714b4bb46dSJan Kara 872ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 8734b4bb46dSJan Kara } 8744b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8754b4bb46dSJan Kara } 8764b4bb46dSJan Kara 8779fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8789fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8799973c98eSRoss Zwisler { 880e4b3448bSMatthew Wilcox unsigned long pfn, index, count; 8813fe0791cSDan Williams long ret = 0; 8829973c98eSRoss Zwisler 8839973c98eSRoss Zwisler /* 884a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 885a6abc2c0SJan Kara * wrong. 8869973c98eSRoss Zwisler */ 8873159f943SMatthew Wilcox if (WARN_ON(!xa_is_value(entry))) 888a6abc2c0SJan Kara return -EIO; 8899973c98eSRoss Zwisler 8909fc747f6SMatthew Wilcox if (unlikely(dax_is_locked(entry))) { 8919fc747f6SMatthew Wilcox void *old_entry = entry; 8929fc747f6SMatthew Wilcox 89323c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(xas, 0); 8949fc747f6SMatthew Wilcox 895a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 8969fc747f6SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 897a6abc2c0SJan Kara goto put_unlocked; 898a6abc2c0SJan Kara /* 8999fc747f6SMatthew Wilcox * Entry got reallocated elsewhere? No need to writeback. 9009fc747f6SMatthew Wilcox * We have to compare pfns as we must not bail out due to 9019fc747f6SMatthew Wilcox * difference in lockbit or entry type. 902a6abc2c0SJan Kara */ 9039fc747f6SMatthew Wilcox if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 904a6abc2c0SJan Kara goto put_unlocked; 905642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 906642261acSRoss Zwisler dax_is_zero_entry(entry))) { 9079973c98eSRoss Zwisler ret = -EIO; 908a6abc2c0SJan Kara goto put_unlocked; 9099973c98eSRoss Zwisler } 9109973c98eSRoss Zwisler 9119fc747f6SMatthew Wilcox /* Another fsync thread may have already done this entry */ 9129fc747f6SMatthew Wilcox if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 913a6abc2c0SJan Kara goto put_unlocked; 9149fc747f6SMatthew Wilcox } 9159fc747f6SMatthew Wilcox 916a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 9179fc747f6SMatthew Wilcox dax_lock_entry(xas, entry); 9189fc747f6SMatthew Wilcox 919a6abc2c0SJan Kara /* 920a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 921a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 922a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 923b93b0163SMatthew Wilcox * at the entry only under the i_pages lock and once they do that 924b93b0163SMatthew Wilcox * they will see the entry locked and wait for it to unlock. 925a6abc2c0SJan Kara */ 9269fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 9279fc747f6SMatthew Wilcox xas_unlock_irq(xas); 928a6abc2c0SJan Kara 929642261acSRoss Zwisler /* 930e4b3448bSMatthew Wilcox * If dax_writeback_mapping_range() was given a wbc->range_start 931e4b3448bSMatthew Wilcox * in the middle of a PMD, the 'index' we use needs to be 932e4b3448bSMatthew Wilcox * aligned to the start of the PMD. 9333fe0791cSDan Williams * This allows us to flush for PMD_SIZE and not have to worry about 9343fe0791cSDan Williams * partial PMD writebacks. 935642261acSRoss Zwisler */ 936a77d19f4SMatthew Wilcox pfn = dax_to_pfn(entry); 937e4b3448bSMatthew Wilcox count = 1UL << dax_entry_order(entry); 938e4b3448bSMatthew Wilcox index = xas->xa_index & ~(count - 1); 939cccbce67SDan Williams 940e4b3448bSMatthew Wilcox dax_entry_mkclean(mapping, index, pfn); 941e4b3448bSMatthew Wilcox dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE); 9424b4bb46dSJan Kara /* 9434b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 9444b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 9454b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 9464b4bb46dSJan Kara * entry lock. 9474b4bb46dSJan Kara */ 9489fc747f6SMatthew Wilcox xas_reset(xas); 9499fc747f6SMatthew Wilcox xas_lock_irq(xas); 9509fc747f6SMatthew Wilcox xas_store(xas, entry); 9519fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 952698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_NEXT); 9539fc747f6SMatthew Wilcox 954e4b3448bSMatthew Wilcox trace_dax_writeback_one(mapping->host, index, count); 9559973c98eSRoss Zwisler return ret; 9569973c98eSRoss Zwisler 957a6abc2c0SJan Kara put_unlocked: 9584c3d043dSVivek Goyal put_unlocked_entry(xas, entry, WAKE_NEXT); 9599973c98eSRoss Zwisler return ret; 9609973c98eSRoss Zwisler } 9619973c98eSRoss Zwisler 9629973c98eSRoss Zwisler /* 9639973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 9649973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 9659973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 9669973c98eSRoss Zwisler */ 9677f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 9683f666c56SVivek Goyal struct dax_device *dax_dev, struct writeback_control *wbc) 9699973c98eSRoss Zwisler { 9709fc747f6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 9719973c98eSRoss Zwisler struct inode *inode = mapping->host; 9729fc747f6SMatthew Wilcox pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 9739fc747f6SMatthew Wilcox void *entry; 9749fc747f6SMatthew Wilcox int ret = 0; 9759fc747f6SMatthew Wilcox unsigned int scanned = 0; 9769973c98eSRoss Zwisler 9779973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 9789973c98eSRoss Zwisler return -EIO; 9799973c98eSRoss Zwisler 9807716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping) || wbc->sync_mode != WB_SYNC_ALL) 9817f6d5b52SRoss Zwisler return 0; 9827f6d5b52SRoss Zwisler 9839fc747f6SMatthew Wilcox trace_dax_writeback_range(inode, xas.xa_index, end_index); 9849973c98eSRoss Zwisler 9859fc747f6SMatthew Wilcox tag_pages_for_writeback(mapping, xas.xa_index, end_index); 986d14a3f48SRoss Zwisler 9879fc747f6SMatthew Wilcox xas_lock_irq(&xas); 9889fc747f6SMatthew Wilcox xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 9899fc747f6SMatthew Wilcox ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 990819ec6b9SJeff Layton if (ret < 0) { 991819ec6b9SJeff Layton mapping_set_error(mapping, ret); 9929fc747f6SMatthew Wilcox break; 993d14a3f48SRoss Zwisler } 9949fc747f6SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 9959fc747f6SMatthew Wilcox continue; 9969fc747f6SMatthew Wilcox 9979fc747f6SMatthew Wilcox xas_pause(&xas); 9989fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 9999fc747f6SMatthew Wilcox cond_resched(); 10009fc747f6SMatthew Wilcox xas_lock_irq(&xas); 1001d14a3f48SRoss Zwisler } 10029fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 10039fc747f6SMatthew Wilcox trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 10049fc747f6SMatthew Wilcox return ret; 10059973c98eSRoss Zwisler } 10069973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 10079973c98eSRoss Zwisler 100831a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 1009f7ca90b1SMatthew Wilcox { 1010a3841f94SLinus Torvalds return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; 101131a6f1a6SJan Kara } 1012f7ca90b1SMatthew Wilcox 10135e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, 10145e161e40SJan Kara pfn_t *pfnp) 10155e161e40SJan Kara { 10165e161e40SJan Kara const sector_t sector = dax_iomap_sector(iomap, pos); 10175e161e40SJan Kara pgoff_t pgoff; 10185e161e40SJan Kara int id, rc; 10195e161e40SJan Kara long length; 10205e161e40SJan Kara 10215e161e40SJan Kara rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); 1022cccbce67SDan Williams if (rc) 1023cccbce67SDan Williams return rc; 1024cccbce67SDan Williams id = dax_read_lock(); 10255e161e40SJan Kara length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 102686ed913bSHuaisheng Ye NULL, pfnp); 10275e161e40SJan Kara if (length < 0) { 10285e161e40SJan Kara rc = length; 10295e161e40SJan Kara goto out; 10305e161e40SJan Kara } 10315e161e40SJan Kara rc = -EINVAL; 10325e161e40SJan Kara if (PFN_PHYS(length) < size) 10335e161e40SJan Kara goto out; 10345e161e40SJan Kara if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 10355e161e40SJan Kara goto out; 10365e161e40SJan Kara /* For larger pages we need devmap */ 10375e161e40SJan Kara if (length > 1 && !pfn_t_devmap(*pfnp)) 10385e161e40SJan Kara goto out; 10395e161e40SJan Kara rc = 0; 10405e161e40SJan Kara out: 1041cccbce67SDan Williams dax_read_unlock(id); 1042cccbce67SDan Williams return rc; 1043cccbce67SDan Williams } 1044f7ca90b1SMatthew Wilcox 10452f89dc12SJan Kara /* 104691d25ba8SRoss Zwisler * The user has performed a load from a hole in the file. Allocating a new 104791d25ba8SRoss Zwisler * page in the file would cause excessive storage usage for workloads with 104891d25ba8SRoss Zwisler * sparse files. Instead we insert a read-only mapping of the 4k zero page. 104991d25ba8SRoss Zwisler * If this page is ever written to we will re-fault and change the mapping to 105091d25ba8SRoss Zwisler * point to real DAX storage instead. 10512f89dc12SJan Kara */ 1052b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas, 1053b15cd800SMatthew Wilcox struct address_space *mapping, void **entry, 1054e30331ffSRoss Zwisler struct vm_fault *vmf) 1055e30331ffSRoss Zwisler { 1056e30331ffSRoss Zwisler struct inode *inode = mapping->host; 105791d25ba8SRoss Zwisler unsigned long vaddr = vmf->address; 1058b90ca5ccSMatthew Wilcox pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1059b90ca5ccSMatthew Wilcox vm_fault_t ret; 1060e30331ffSRoss Zwisler 1061b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 10623159f943SMatthew Wilcox DAX_ZERO_PAGE, false); 10633159f943SMatthew Wilcox 1064ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vmf->vma, vaddr, pfn); 1065e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 1066e30331ffSRoss Zwisler return ret; 1067e30331ffSRoss Zwisler } 1068e30331ffSRoss Zwisler 106981ee8e52SMatthew Wilcox (Oracle) s64 dax_iomap_zero(loff_t pos, u64 length, struct iomap *iomap) 1070679c8bd3SChristoph Hellwig { 10714f3b4f16SVivek Goyal sector_t sector = iomap_sector(iomap, pos & PAGE_MASK); 1072cccbce67SDan Williams pgoff_t pgoff; 1073cccbce67SDan Williams long rc, id; 1074cccbce67SDan Williams void *kaddr; 10750a23f9ffSVivek Goyal bool page_aligned = false; 107681ee8e52SMatthew Wilcox (Oracle) unsigned offset = offset_in_page(pos); 107781ee8e52SMatthew Wilcox (Oracle) unsigned size = min_t(u64, PAGE_SIZE - offset, length); 10780a23f9ffSVivek Goyal 10790a23f9ffSVivek Goyal if (IS_ALIGNED(sector << SECTOR_SHIFT, PAGE_SIZE) && 108081ee8e52SMatthew Wilcox (Oracle) (size == PAGE_SIZE)) 10810a23f9ffSVivek Goyal page_aligned = true; 1082cccbce67SDan Williams 10834f3b4f16SVivek Goyal rc = bdev_dax_pgoff(iomap->bdev, sector, PAGE_SIZE, &pgoff); 1084cccbce67SDan Williams if (rc) 1085cccbce67SDan Williams return rc; 1086cccbce67SDan Williams 1087cccbce67SDan Williams id = dax_read_lock(); 10880a23f9ffSVivek Goyal 10890a23f9ffSVivek Goyal if (page_aligned) 109081ee8e52SMatthew Wilcox (Oracle) rc = dax_zero_page_range(iomap->dax_dev, pgoff, 1); 10910a23f9ffSVivek Goyal else 10924f3b4f16SVivek Goyal rc = dax_direct_access(iomap->dax_dev, pgoff, 1, &kaddr, NULL); 1093cccbce67SDan Williams if (rc < 0) { 1094cccbce67SDan Williams dax_read_unlock(id); 1095cccbce67SDan Williams return rc; 1096cccbce67SDan Williams } 10970a23f9ffSVivek Goyal 10980a23f9ffSVivek Goyal if (!page_aligned) { 109981f55870SDan Williams memset(kaddr + offset, 0, size); 11004f3b4f16SVivek Goyal dax_flush(iomap->dax_dev, kaddr + offset, size); 11014b0228faSVishal Verma } 11020a23f9ffSVivek Goyal dax_read_unlock(id); 110381ee8e52SMatthew Wilcox (Oracle) return size; 1104679c8bd3SChristoph Hellwig } 1105679c8bd3SChristoph Hellwig 1106a254e568SChristoph Hellwig static loff_t 110711c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 1108c039b997SGoldwyn Rodrigues struct iomap *iomap, struct iomap *srcmap) 1109a254e568SChristoph Hellwig { 1110cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1111cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1112a254e568SChristoph Hellwig struct iov_iter *iter = data; 1113a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1114a254e568SChristoph Hellwig ssize_t ret = 0; 1115a77d4786SDan Williams size_t xfer; 1116cccbce67SDan Williams int id; 1117a254e568SChristoph Hellwig 1118a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1119a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1120a254e568SChristoph Hellwig if (pos >= end) 1121a254e568SChristoph Hellwig return 0; 1122a254e568SChristoph Hellwig 1123a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1124a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1125a254e568SChristoph Hellwig } 1126a254e568SChristoph Hellwig 1127a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1128a254e568SChristoph Hellwig return -EIO; 1129a254e568SChristoph Hellwig 1130e3fce68cSJan Kara /* 1131e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1132e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1133e3fce68cSJan Kara * written by write(2) is visible in mmap. 1134e3fce68cSJan Kara */ 1135cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1136e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1137e3fce68cSJan Kara pos >> PAGE_SHIFT, 1138e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1139e3fce68cSJan Kara } 1140e3fce68cSJan Kara 1141cccbce67SDan Williams id = dax_read_lock(); 1142a254e568SChristoph Hellwig while (pos < end) { 1143a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1144cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1145cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1146a254e568SChristoph Hellwig ssize_t map_len; 1147cccbce67SDan Williams pgoff_t pgoff; 1148cccbce67SDan Williams void *kaddr; 1149a254e568SChristoph Hellwig 1150d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1151d1908f52SMichal Hocko ret = -EINTR; 1152d1908f52SMichal Hocko break; 1153d1908f52SMichal Hocko } 1154d1908f52SMichal Hocko 1155cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1156cccbce67SDan Williams if (ret) 1157cccbce67SDan Williams break; 1158cccbce67SDan Williams 1159cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 116086ed913bSHuaisheng Ye &kaddr, NULL); 1161a254e568SChristoph Hellwig if (map_len < 0) { 1162a254e568SChristoph Hellwig ret = map_len; 1163a254e568SChristoph Hellwig break; 1164a254e568SChristoph Hellwig } 1165a254e568SChristoph Hellwig 1166cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1167cccbce67SDan Williams kaddr += offset; 1168a254e568SChristoph Hellwig map_len -= offset; 1169a254e568SChristoph Hellwig if (map_len > end - pos) 1170a254e568SChristoph Hellwig map_len = end - pos; 1171a254e568SChristoph Hellwig 1172a2e050f5SRoss Zwisler /* 1173a2e050f5SRoss Zwisler * The userspace address for the memory copy has already been 1174a2e050f5SRoss Zwisler * validated via access_ok() in either vfs_read() or 1175a2e050f5SRoss Zwisler * vfs_write(), depending on which operation we are doing. 1176a2e050f5SRoss Zwisler */ 1177a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1178a77d4786SDan Williams xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1179fec53774SDan Williams map_len, iter); 1180a254e568SChristoph Hellwig else 1181a77d4786SDan Williams xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1182b3a9a0c3SDan Williams map_len, iter); 1183a254e568SChristoph Hellwig 1184a77d4786SDan Williams pos += xfer; 1185a77d4786SDan Williams length -= xfer; 1186a77d4786SDan Williams done += xfer; 1187a77d4786SDan Williams 1188a77d4786SDan Williams if (xfer == 0) 1189a77d4786SDan Williams ret = -EFAULT; 1190a77d4786SDan Williams if (xfer < map_len) 1191a77d4786SDan Williams break; 1192a254e568SChristoph Hellwig } 1193cccbce67SDan Williams dax_read_unlock(id); 1194a254e568SChristoph Hellwig 1195a254e568SChristoph Hellwig return done ? done : ret; 1196a254e568SChristoph Hellwig } 1197a254e568SChristoph Hellwig 1198a254e568SChristoph Hellwig /** 119911c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1200a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1201a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1202a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1203a254e568SChristoph Hellwig * 1204a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1205a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1206a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1207a254e568SChristoph Hellwig */ 1208a254e568SChristoph Hellwig ssize_t 120911c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 12108ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1211a254e568SChristoph Hellwig { 1212a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1213a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1214a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1215a254e568SChristoph Hellwig unsigned flags = 0; 1216a254e568SChristoph Hellwig 1217168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 12189ffbe8acSNikolay Borisov lockdep_assert_held_write(&inode->i_rwsem); 1219a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1220168316dbSChristoph Hellwig } else { 1221168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1222168316dbSChristoph Hellwig } 1223a254e568SChristoph Hellwig 122496222d53SJeff Moyer if (iocb->ki_flags & IOCB_NOWAIT) 122596222d53SJeff Moyer flags |= IOMAP_NOWAIT; 122696222d53SJeff Moyer 1227a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1228a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 122911c59c92SRoss Zwisler iter, dax_iomap_actor); 1230a254e568SChristoph Hellwig if (ret <= 0) 1231a254e568SChristoph Hellwig break; 1232a254e568SChristoph Hellwig pos += ret; 1233a254e568SChristoph Hellwig done += ret; 1234a254e568SChristoph Hellwig } 1235a254e568SChristoph Hellwig 1236a254e568SChristoph Hellwig iocb->ki_pos += done; 1237a254e568SChristoph Hellwig return done ? done : ret; 1238a254e568SChristoph Hellwig } 123911c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1240a7d73fe6SChristoph Hellwig 1241ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error) 12429f141d6eSJan Kara { 12439f141d6eSJan Kara if (error == 0) 12449f141d6eSJan Kara return VM_FAULT_NOPAGE; 1245c9aed74eSSouptick Joarder return vmf_error(error); 12469f141d6eSJan Kara } 12479f141d6eSJan Kara 1248aaa422c4SDan Williams /* 1249aaa422c4SDan Williams * MAP_SYNC on a dax mapping guarantees dirty metadata is 1250aaa422c4SDan Williams * flushed on write-faults (non-cow), but not read-faults. 1251aaa422c4SDan Williams */ 1252aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags, 1253aaa422c4SDan Williams struct vm_area_struct *vma, struct iomap *iomap) 1254aaa422c4SDan Williams { 1255aaa422c4SDan Williams return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) 1256aaa422c4SDan Williams && (iomap->flags & IOMAP_F_DIRTY); 1257aaa422c4SDan Williams } 1258aaa422c4SDan Williams 1259ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1260c0b24625SJan Kara int *iomap_errp, const struct iomap_ops *ops) 1261a7d73fe6SChristoph Hellwig { 1262a0987ad5SJan Kara struct vm_area_struct *vma = vmf->vma; 1263a0987ad5SJan Kara struct address_space *mapping = vma->vm_file->f_mapping; 1264b15cd800SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1265a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 12661a29d85eSJan Kara unsigned long vaddr = vmf->address; 1267a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1268c039b997SGoldwyn Rodrigues struct iomap iomap = { .type = IOMAP_HOLE }; 1269c039b997SGoldwyn Rodrigues struct iomap srcmap = { .type = IOMAP_HOLE }; 12709484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1271a7d73fe6SChristoph Hellwig int error, major = 0; 1272d2c43ef1SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 1273caa51d26SJan Kara bool sync; 1274ab77dab4SSouptick Joarder vm_fault_t ret = 0; 1275a7d73fe6SChristoph Hellwig void *entry; 12761b5a1cb2SJan Kara pfn_t pfn; 1277a7d73fe6SChristoph Hellwig 1278ab77dab4SSouptick Joarder trace_dax_pte_fault(inode, vmf, ret); 1279a7d73fe6SChristoph Hellwig /* 1280a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1281a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1282a7d73fe6SChristoph Hellwig * a reliable test. 1283a7d73fe6SChristoph Hellwig */ 1284a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1285ab77dab4SSouptick Joarder ret = VM_FAULT_SIGBUS; 1286a9c42b33SRoss Zwisler goto out; 1287a9c42b33SRoss Zwisler } 1288a7d73fe6SChristoph Hellwig 1289d2c43ef1SJan Kara if (write && !vmf->cow_page) 1290a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1291a7d73fe6SChristoph Hellwig 1292b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, 0); 1293b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1294b15cd800SMatthew Wilcox ret = xa_to_internal(entry); 129513e451fdSJan Kara goto out; 129613e451fdSJan Kara } 129713e451fdSJan Kara 1298a7d73fe6SChristoph Hellwig /* 1299e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1300e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1301e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1302e2093926SRoss Zwisler * retried. 1303e2093926SRoss Zwisler */ 1304e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1305ab77dab4SSouptick Joarder ret = VM_FAULT_NOPAGE; 1306e2093926SRoss Zwisler goto unlock_entry; 1307e2093926SRoss Zwisler } 1308e2093926SRoss Zwisler 1309e2093926SRoss Zwisler /* 1310a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1311a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1312a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1313a7d73fe6SChristoph Hellwig */ 1314c039b997SGoldwyn Rodrigues error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap, &srcmap); 1315c0b24625SJan Kara if (iomap_errp) 1316c0b24625SJan Kara *iomap_errp = error; 1317a9c42b33SRoss Zwisler if (error) { 1318ab77dab4SSouptick Joarder ret = dax_fault_return(error); 131913e451fdSJan Kara goto unlock_entry; 1320a9c42b33SRoss Zwisler } 1321a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 132213e451fdSJan Kara error = -EIO; /* fs corruption? */ 132313e451fdSJan Kara goto error_finish_iomap; 1324a7d73fe6SChristoph Hellwig } 1325a7d73fe6SChristoph Hellwig 1326a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 132731a6f1a6SJan Kara sector_t sector = dax_iomap_sector(&iomap, pos); 132831a6f1a6SJan Kara 1329a7d73fe6SChristoph Hellwig switch (iomap.type) { 1330a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1331a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1332a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1333a7d73fe6SChristoph Hellwig break; 1334a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1335c7fe193fSIra Weiny error = copy_cow_page_dax(iomap.bdev, iomap.dax_dev, 1336c7fe193fSIra Weiny sector, vmf->cow_page, vaddr); 1337a7d73fe6SChristoph Hellwig break; 1338a7d73fe6SChristoph Hellwig default: 1339a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1340a7d73fe6SChristoph Hellwig error = -EIO; 1341a7d73fe6SChristoph Hellwig break; 1342a7d73fe6SChristoph Hellwig } 1343a7d73fe6SChristoph Hellwig 1344a7d73fe6SChristoph Hellwig if (error) 134513e451fdSJan Kara goto error_finish_iomap; 1346b1aa812bSJan Kara 1347b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1348ab77dab4SSouptick Joarder ret = finish_fault(vmf); 1349ab77dab4SSouptick Joarder if (!ret) 1350ab77dab4SSouptick Joarder ret = VM_FAULT_DONE_COW; 135113e451fdSJan Kara goto finish_iomap; 1352a7d73fe6SChristoph Hellwig } 1353a7d73fe6SChristoph Hellwig 1354aaa422c4SDan Williams sync = dax_fault_is_synchronous(flags, vma, &iomap); 1355caa51d26SJan Kara 1356a7d73fe6SChristoph Hellwig switch (iomap.type) { 1357a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1358a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1359a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 1360a0987ad5SJan Kara count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 1361a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1362a7d73fe6SChristoph Hellwig } 13631b5a1cb2SJan Kara error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); 13641b5a1cb2SJan Kara if (error < 0) 13651b5a1cb2SJan Kara goto error_finish_iomap; 13661b5a1cb2SJan Kara 1367b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 1368caa51d26SJan Kara 0, write && !sync); 13691b5a1cb2SJan Kara 1370caa51d26SJan Kara /* 1371caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1372caa51d26SJan Kara * we can insert PTE into page tables only after that happens. 1373caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1374caa51d26SJan Kara * insert it after fsync is done. 1375caa51d26SJan Kara */ 1376caa51d26SJan Kara if (sync) { 1377caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) { 1378caa51d26SJan Kara error = -EIO; 1379caa51d26SJan Kara goto error_finish_iomap; 1380caa51d26SJan Kara } 1381caa51d26SJan Kara *pfnp = pfn; 1382ab77dab4SSouptick Joarder ret = VM_FAULT_NEEDDSYNC | major; 1383caa51d26SJan Kara goto finish_iomap; 1384caa51d26SJan Kara } 13851b5a1cb2SJan Kara trace_dax_insert_mapping(inode, vmf, entry); 13861b5a1cb2SJan Kara if (write) 1387ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn); 13881b5a1cb2SJan Kara else 1389ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vma, vaddr, pfn); 13901b5a1cb2SJan Kara 1391ab77dab4SSouptick Joarder goto finish_iomap; 1392a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1393a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1394d2c43ef1SJan Kara if (!write) { 1395b15cd800SMatthew Wilcox ret = dax_load_hole(&xas, mapping, &entry, vmf); 139613e451fdSJan Kara goto finish_iomap; 13971550290bSRoss Zwisler } 1398df561f66SGustavo A. R. Silva fallthrough; 1399a7d73fe6SChristoph Hellwig default: 1400a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1401a7d73fe6SChristoph Hellwig error = -EIO; 1402a7d73fe6SChristoph Hellwig break; 1403a7d73fe6SChristoph Hellwig } 1404a7d73fe6SChristoph Hellwig 140513e451fdSJan Kara error_finish_iomap: 1406ab77dab4SSouptick Joarder ret = dax_fault_return(error); 14079f141d6eSJan Kara finish_iomap: 14089f141d6eSJan Kara if (ops->iomap_end) { 14099f141d6eSJan Kara int copied = PAGE_SIZE; 14109f141d6eSJan Kara 1411ab77dab4SSouptick Joarder if (ret & VM_FAULT_ERROR) 14129f141d6eSJan Kara copied = 0; 14139f141d6eSJan Kara /* 14149f141d6eSJan Kara * The fault is done by now and there's no way back (other 14159f141d6eSJan Kara * thread may be already happily using PTE we have installed). 14169f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 14179f141d6eSJan Kara * with it. 14189f141d6eSJan Kara */ 14199f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 14201550290bSRoss Zwisler } 142113e451fdSJan Kara unlock_entry: 1422b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1423a9c42b33SRoss Zwisler out: 1424ab77dab4SSouptick Joarder trace_dax_pte_fault_done(inode, vmf, ret); 1425ab77dab4SSouptick Joarder return ret | major; 1426a7d73fe6SChristoph Hellwig } 1427642261acSRoss Zwisler 1428642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1429b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1430b15cd800SMatthew Wilcox struct iomap *iomap, void **entry) 1431642261acSRoss Zwisler { 1432f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1433f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 143411cf9d86SAneesh Kumar K.V struct vm_area_struct *vma = vmf->vma; 1435653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 143611cf9d86SAneesh Kumar K.V pgtable_t pgtable = NULL; 1437642261acSRoss Zwisler struct page *zero_page; 1438642261acSRoss Zwisler spinlock_t *ptl; 1439642261acSRoss Zwisler pmd_t pmd_entry; 14403fe0791cSDan Williams pfn_t pfn; 1441642261acSRoss Zwisler 1442f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1443642261acSRoss Zwisler 1444642261acSRoss Zwisler if (unlikely(!zero_page)) 1445653b2ea3SRoss Zwisler goto fallback; 1446642261acSRoss Zwisler 14473fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1448b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14493159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1450642261acSRoss Zwisler 145111cf9d86SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) { 145211cf9d86SAneesh Kumar K.V pgtable = pte_alloc_one(vma->vm_mm); 145311cf9d86SAneesh Kumar K.V if (!pgtable) 145411cf9d86SAneesh Kumar K.V return VM_FAULT_OOM; 145511cf9d86SAneesh Kumar K.V } 145611cf9d86SAneesh Kumar K.V 1457f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1458f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1459642261acSRoss Zwisler spin_unlock(ptl); 1460653b2ea3SRoss Zwisler goto fallback; 1461642261acSRoss Zwisler } 1462642261acSRoss Zwisler 146311cf9d86SAneesh Kumar K.V if (pgtable) { 146411cf9d86SAneesh Kumar K.V pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 146511cf9d86SAneesh Kumar K.V mm_inc_nr_ptes(vma->vm_mm); 146611cf9d86SAneesh Kumar K.V } 1467f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1468642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1469f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1470642261acSRoss Zwisler spin_unlock(ptl); 1471b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1472642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1473653b2ea3SRoss Zwisler 1474653b2ea3SRoss Zwisler fallback: 147511cf9d86SAneesh Kumar K.V if (pgtable) 147611cf9d86SAneesh Kumar K.V pte_free(vma->vm_mm, pgtable); 1477b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1478642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1479642261acSRoss Zwisler } 1480642261acSRoss Zwisler 1481ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1482a2d58167SDave Jiang const struct iomap_ops *ops) 1483642261acSRoss Zwisler { 1484f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1485642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1486b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1487d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1488d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1489caa51d26SJan Kara bool sync; 14909484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1491642261acSRoss Zwisler struct inode *inode = mapping->host; 1492ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1493c039b997SGoldwyn Rodrigues struct iomap iomap = { .type = IOMAP_HOLE }; 1494c039b997SGoldwyn Rodrigues struct iomap srcmap = { .type = IOMAP_HOLE }; 1495b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1496642261acSRoss Zwisler void *entry; 1497642261acSRoss Zwisler loff_t pos; 1498642261acSRoss Zwisler int error; 1499302a5e31SJan Kara pfn_t pfn; 1500642261acSRoss Zwisler 1501282a8e03SRoss Zwisler /* 1502282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1503282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1504282a8e03SRoss Zwisler * this is a reliable test. 1505282a8e03SRoss Zwisler */ 1506957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1507282a8e03SRoss Zwisler 1508f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1509282a8e03SRoss Zwisler 1510fffa281bSRoss Zwisler /* 1511fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1512fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1513fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1514a77d19f4SMatthew Wilcox * range in the page cache. 1515fffa281bSRoss Zwisler */ 1516fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1517fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1518fffa281bSRoss Zwisler goto fallback; 1519fffa281bSRoss Zwisler 1520642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1521642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1522642261acSRoss Zwisler goto fallback; 1523642261acSRoss Zwisler 1524642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1525642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1526642261acSRoss Zwisler goto fallback; 1527642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1528642261acSRoss Zwisler goto fallback; 1529642261acSRoss Zwisler 1530b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1531282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1532282a8e03SRoss Zwisler goto out; 1533282a8e03SRoss Zwisler } 1534642261acSRoss Zwisler 1535642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1536b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1537642261acSRoss Zwisler goto fallback; 1538642261acSRoss Zwisler 1539642261acSRoss Zwisler /* 1540b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1541b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1542b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1543b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 15449f141d6eSJan Kara */ 154523c84eb7SMatthew Wilcox (Oracle) entry = grab_mapping_entry(&xas, mapping, PMD_ORDER); 1546b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1547b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1548876f2946SRoss Zwisler goto fallback; 1549b15cd800SMatthew Wilcox } 1550876f2946SRoss Zwisler 1551876f2946SRoss Zwisler /* 1552e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1553e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1554e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1555e2093926SRoss Zwisler * retried. 1556e2093926SRoss Zwisler */ 1557e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1558e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1559e2093926SRoss Zwisler result = 0; 1560e2093926SRoss Zwisler goto unlock_entry; 1561e2093926SRoss Zwisler } 1562e2093926SRoss Zwisler 1563e2093926SRoss Zwisler /* 1564876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1565876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1566876f2946SRoss Zwisler * to look up our filesystem block. 1567876f2946SRoss Zwisler */ 1568b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1569c039b997SGoldwyn Rodrigues error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap, 1570c039b997SGoldwyn Rodrigues &srcmap); 1571876f2946SRoss Zwisler if (error) 1572876f2946SRoss Zwisler goto unlock_entry; 1573876f2946SRoss Zwisler 1574876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15759f141d6eSJan Kara goto finish_iomap; 15769f141d6eSJan Kara 1577aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1578caa51d26SJan Kara 1579642261acSRoss Zwisler switch (iomap.type) { 1580642261acSRoss Zwisler case IOMAP_MAPPED: 1581302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1582302a5e31SJan Kara if (error < 0) 1583302a5e31SJan Kara goto finish_iomap; 1584302a5e31SJan Kara 1585b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15863159f943SMatthew Wilcox DAX_PMD, write && !sync); 1587302a5e31SJan Kara 1588caa51d26SJan Kara /* 1589caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1590caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1591caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1592caa51d26SJan Kara * insert it after fsync is done. 1593caa51d26SJan Kara */ 1594caa51d26SJan Kara if (sync) { 1595caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1596caa51d26SJan Kara goto finish_iomap; 1597caa51d26SJan Kara *pfnp = pfn; 1598caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1599caa51d26SJan Kara goto finish_iomap; 1600caa51d26SJan Kara } 1601caa51d26SJan Kara 1602302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1603fce86ff5SDan Williams result = vmf_insert_pfn_pmd(vmf, pfn, write); 1604642261acSRoss Zwisler break; 1605642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1606642261acSRoss Zwisler case IOMAP_HOLE: 1607642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1608876f2946SRoss Zwisler break; 1609b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1610642261acSRoss Zwisler break; 1611642261acSRoss Zwisler default: 1612642261acSRoss Zwisler WARN_ON_ONCE(1); 1613642261acSRoss Zwisler break; 1614642261acSRoss Zwisler } 1615642261acSRoss Zwisler 16169f141d6eSJan Kara finish_iomap: 16179f141d6eSJan Kara if (ops->iomap_end) { 16189f141d6eSJan Kara int copied = PMD_SIZE; 16199f141d6eSJan Kara 16209f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 16219f141d6eSJan Kara copied = 0; 16229f141d6eSJan Kara /* 16239f141d6eSJan Kara * The fault is done by now and there's no way back (other 16249f141d6eSJan Kara * thread may be already happily using PMD we have installed). 16259f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 16269f141d6eSJan Kara * with it. 16279f141d6eSJan Kara */ 16289f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 16299f141d6eSJan Kara &iomap); 16309f141d6eSJan Kara } 1631876f2946SRoss Zwisler unlock_entry: 1632b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1633642261acSRoss Zwisler fallback: 1634642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1635d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1636642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1637642261acSRoss Zwisler } 1638282a8e03SRoss Zwisler out: 1639f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1640642261acSRoss Zwisler return result; 1641642261acSRoss Zwisler } 1642a2d58167SDave Jiang #else 1643ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 164401cddfe9SArnd Bergmann const struct iomap_ops *ops) 1645a2d58167SDave Jiang { 1646a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1647a2d58167SDave Jiang } 1648642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1649a2d58167SDave Jiang 1650a2d58167SDave Jiang /** 1651a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1652a2d58167SDave Jiang * @vmf: The description of the fault 1653cec04e8cSJan Kara * @pe_size: Size of the page to fault in 16549a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1655c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1656cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1657a2d58167SDave Jiang * 1658a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1659a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1660a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1661a2d58167SDave Jiang * successfully. 1662a2d58167SDave Jiang */ 1663ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1664c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1665a2d58167SDave Jiang { 1666c791ace1SDave Jiang switch (pe_size) { 1667c791ace1SDave Jiang case PE_SIZE_PTE: 1668c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1669c791ace1SDave Jiang case PE_SIZE_PMD: 16709a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1671a2d58167SDave Jiang default: 1672a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1673a2d58167SDave Jiang } 1674a2d58167SDave Jiang } 1675a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 167671eab6dfSJan Kara 1677a77d19f4SMatthew Wilcox /* 167871eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 167971eab6dfSJan Kara * @vmf: The description of the fault 168071eab6dfSJan Kara * @pfn: PFN to insert 1681cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 168271eab6dfSJan Kara * 1683a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1684a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 168571eab6dfSJan Kara */ 1686cfc93c6cSMatthew Wilcox static vm_fault_t 1687cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 168871eab6dfSJan Kara { 168971eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1690cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1691cfc93c6cSMatthew Wilcox void *entry; 1692ab77dab4SSouptick Joarder vm_fault_t ret; 169371eab6dfSJan Kara 1694cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 169523c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, order); 169671eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 169723c84eb7SMatthew Wilcox (Oracle) if (!entry || dax_is_conflict(entry) || 169823c84eb7SMatthew Wilcox (Oracle) (order == 0 && !dax_is_pte_entry(entry))) { 16994c3d043dSVivek Goyal put_unlocked_entry(&xas, entry, WAKE_NEXT); 1700cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 170171eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 170271eab6dfSJan Kara VM_FAULT_NOPAGE); 170371eab6dfSJan Kara return VM_FAULT_NOPAGE; 170471eab6dfSJan Kara } 1705cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1706cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1707cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1708cfc93c6cSMatthew Wilcox if (order == 0) 1709ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 171071eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1711cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1712fce86ff5SDan Williams ret = vmf_insert_pfn_pmd(vmf, pfn, FAULT_FLAG_WRITE); 171371eab6dfSJan Kara #endif 1714cfc93c6cSMatthew Wilcox else 1715ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1716cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1717ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1718ab77dab4SSouptick Joarder return ret; 171971eab6dfSJan Kara } 172071eab6dfSJan Kara 172171eab6dfSJan Kara /** 172271eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 172371eab6dfSJan Kara * @vmf: The description of the fault 172471eab6dfSJan Kara * @pe_size: Size of entry to be inserted 172571eab6dfSJan Kara * @pfn: PFN to insert 172671eab6dfSJan Kara * 172771eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 172871eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 172971eab6dfSJan Kara * table entry. 173071eab6dfSJan Kara */ 1731ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1732ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 173371eab6dfSJan Kara { 173471eab6dfSJan Kara int err; 173571eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1736cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1737cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 173871eab6dfSJan Kara 173971eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 174071eab6dfSJan Kara if (err) 174171eab6dfSJan Kara return VM_FAULT_SIGBUS; 1742cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 174371eab6dfSJan Kara } 174471eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1745