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 147*698ab77aSVivek Goyal /** 148*698ab77aSVivek Goyal * enum dax_wake_mode: waitqueue wakeup behaviour 149*698ab77aSVivek Goyal * @WAKE_ALL: wake all waiters in the waitqueue 150*698ab77aSVivek Goyal * @WAKE_NEXT: wake only the first waiter in the waitqueue 151*698ab77aSVivek Goyal */ 152*698ab77aSVivek Goyal enum dax_wake_mode { 153*698ab77aSVivek Goyal WAKE_ALL, 154*698ab77aSVivek Goyal WAKE_NEXT, 155*698ab77aSVivek Goyal }; 156*698ab77aSVivek 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 */ 195*698ab77aSVivek Goyal static void dax_wake_entry(struct xa_state *xas, void *entry, 196*698ab77aSVivek 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)) 210*698ab77aSVivek 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 278cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry) 279cfc93c6cSMatthew Wilcox { 280cfc93c6cSMatthew Wilcox /* If we were the only waiter woken, wake the next one */ 28161c30c98SJan Kara if (entry && !dax_is_conflict(entry)) 282*698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_NEXT); 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)); 300*698ab77aSVivek 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; 491b15cd800SMatthew Wilcox bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */ 492b15cd800SMatthew Wilcox void *entry; 493ac401cc7SJan Kara 494b15cd800SMatthew Wilcox retry: 495b15cd800SMatthew Wilcox xas_lock_irq(xas); 49623c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(xas, order); 497642261acSRoss Zwisler 498b15cd800SMatthew Wilcox if (entry) { 49923c84eb7SMatthew Wilcox (Oracle) if (dax_is_conflict(entry)) 50023c84eb7SMatthew Wilcox (Oracle) goto fallback; 5010e40de03SMatthew Wilcox if (!xa_is_value(entry)) { 50249688e65SHao Li xas_set_err(xas, -EIO); 50391d25ba8SRoss Zwisler goto out_unlock; 50491d25ba8SRoss Zwisler } 50591d25ba8SRoss Zwisler 50623c84eb7SMatthew Wilcox (Oracle) if (order == 0) { 50791d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 508642261acSRoss Zwisler (dax_is_zero_entry(entry) || 509642261acSRoss Zwisler dax_is_empty_entry(entry))) { 510642261acSRoss Zwisler pmd_downgrade = true; 511642261acSRoss Zwisler } 512642261acSRoss Zwisler } 513642261acSRoss Zwisler } 514642261acSRoss Zwisler 515642261acSRoss Zwisler if (pmd_downgrade) { 516642261acSRoss Zwisler /* 517642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 518b93b0163SMatthew Wilcox * the i_pages lock. 519642261acSRoss Zwisler */ 520b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 521642261acSRoss Zwisler 522642261acSRoss Zwisler /* 523642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 524642261acSRoss Zwisler * downgraded are empty entries which don't need to be 525642261acSRoss Zwisler * unmapped. 526642261acSRoss Zwisler */ 527b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 528b15cd800SMatthew Wilcox xas_unlock_irq(xas); 529b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 530b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 531977fbdcdSMatthew Wilcox PG_PMD_NR, false); 532b15cd800SMatthew Wilcox xas_reset(xas); 533b15cd800SMatthew Wilcox xas_lock_irq(xas); 534e11f8b7bSRoss Zwisler } 535e11f8b7bSRoss Zwisler 536d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 537b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 538*698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_ALL); 539642261acSRoss Zwisler mapping->nrexceptional--; 540b15cd800SMatthew Wilcox entry = NULL; 541b15cd800SMatthew Wilcox xas_set(xas, index); 542642261acSRoss Zwisler } 543642261acSRoss Zwisler 544b15cd800SMatthew Wilcox if (entry) { 545b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 546b15cd800SMatthew Wilcox } else { 54723c84eb7SMatthew Wilcox (Oracle) unsigned long flags = DAX_EMPTY; 54823c84eb7SMatthew Wilcox (Oracle) 54923c84eb7SMatthew Wilcox (Oracle) if (order > 0) 55023c84eb7SMatthew Wilcox (Oracle) flags |= DAX_PMD; 55123c84eb7SMatthew Wilcox (Oracle) entry = dax_make_entry(pfn_to_pfn_t(0), flags); 552b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 553b15cd800SMatthew Wilcox if (xas_error(xas)) 554b15cd800SMatthew Wilcox goto out_unlock; 555ac401cc7SJan Kara mapping->nrexceptional++; 556ac401cc7SJan Kara } 557b15cd800SMatthew Wilcox 558642261acSRoss Zwisler out_unlock: 559b15cd800SMatthew Wilcox xas_unlock_irq(xas); 560b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 561b15cd800SMatthew Wilcox goto retry; 562b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 563b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 564b15cd800SMatthew Wilcox if (xas_error(xas)) 565b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 566e3ad61c6SRoss Zwisler return entry; 567b15cd800SMatthew Wilcox fallback: 568b15cd800SMatthew Wilcox xas_unlock_irq(xas); 569b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 570ac401cc7SJan Kara } 571ac401cc7SJan Kara 5725fac7408SDan Williams /** 5736bbdd563SVivek Goyal * dax_layout_busy_page_range - find first pinned page in @mapping 5745fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5756bbdd563SVivek Goyal * @start: Starting offset. Page containing 'start' is included. 5766bbdd563SVivek Goyal * @end: End offset. Page containing 'end' is included. If 'end' is LLONG_MAX, 5776bbdd563SVivek Goyal * pages from 'start' till the end of file are included. 5785fac7408SDan Williams * 5795fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5805fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5815fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5825fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5835fac7408SDan Williams * get_user_pages() usages. 5845fac7408SDan Williams * 5855fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5865fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5875fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5885fac7408SDan Williams * mapping_mapped() becoming true. 5895fac7408SDan Williams */ 5906bbdd563SVivek Goyal struct page *dax_layout_busy_page_range(struct address_space *mapping, 5916bbdd563SVivek Goyal loff_t start, loff_t end) 5925fac7408SDan Williams { 593084a8990SMatthew Wilcox void *entry; 594084a8990SMatthew Wilcox unsigned int scanned = 0; 5955fac7408SDan Williams struct page *page = NULL; 5966bbdd563SVivek Goyal pgoff_t start_idx = start >> PAGE_SHIFT; 5976bbdd563SVivek Goyal pgoff_t end_idx; 5986bbdd563SVivek Goyal XA_STATE(xas, &mapping->i_pages, start_idx); 5995fac7408SDan Williams 6005fac7408SDan Williams /* 6015fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 6025fac7408SDan Williams */ 6035fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 6045fac7408SDan Williams return NULL; 6055fac7408SDan Williams 6065fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 6075fac7408SDan Williams return NULL; 6085fac7408SDan Williams 6096bbdd563SVivek Goyal /* If end == LLONG_MAX, all pages from start to till end of file */ 6106bbdd563SVivek Goyal if (end == LLONG_MAX) 6116bbdd563SVivek Goyal end_idx = ULONG_MAX; 6126bbdd563SVivek Goyal else 6136bbdd563SVivek Goyal end_idx = end >> PAGE_SHIFT; 6145fac7408SDan Williams /* 6155fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 616084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 6175fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 6185fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 6195fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 6205fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 6216bbdd563SVivek Goyal * holding those locks, and unmap_mapping_pages() will not zero the 6225fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 6235fac7408SDan Williams * guaranteed to either see new references or prevent new 6245fac7408SDan Williams * references from being established. 6255fac7408SDan Williams */ 6266bbdd563SVivek Goyal unmap_mapping_pages(mapping, start_idx, end_idx - start_idx + 1, 0); 6275fac7408SDan Williams 628084a8990SMatthew Wilcox xas_lock_irq(&xas); 6296bbdd563SVivek Goyal xas_for_each(&xas, entry, end_idx) { 630084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 6315fac7408SDan Williams continue; 632084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 63323c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, 0); 6345fac7408SDan Williams if (entry) 6355fac7408SDan Williams page = dax_busy_page(entry); 636084a8990SMatthew Wilcox put_unlocked_entry(&xas, entry); 6375fac7408SDan Williams if (page) 6385fac7408SDan Williams break; 639084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 640084a8990SMatthew Wilcox continue; 641cdbf8897SRoss Zwisler 642084a8990SMatthew Wilcox xas_pause(&xas); 643084a8990SMatthew Wilcox xas_unlock_irq(&xas); 644084a8990SMatthew Wilcox cond_resched(); 645084a8990SMatthew Wilcox xas_lock_irq(&xas); 6465fac7408SDan Williams } 647084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6485fac7408SDan Williams return page; 6495fac7408SDan Williams } 6506bbdd563SVivek Goyal EXPORT_SYMBOL_GPL(dax_layout_busy_page_range); 6516bbdd563SVivek Goyal 6526bbdd563SVivek Goyal struct page *dax_layout_busy_page(struct address_space *mapping) 6536bbdd563SVivek Goyal { 6546bbdd563SVivek Goyal return dax_layout_busy_page_range(mapping, 0, LLONG_MAX); 6556bbdd563SVivek Goyal } 6565fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6575fac7408SDan Williams 658a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 659c6dcf52cSJan Kara pgoff_t index, bool trunc) 660c6dcf52cSJan Kara { 66107f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 662c6dcf52cSJan Kara int ret = 0; 663c6dcf52cSJan Kara void *entry; 664c6dcf52cSJan Kara 66507f2d89cSMatthew Wilcox xas_lock_irq(&xas); 66623c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, 0); 6673159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 668c6dcf52cSJan Kara goto out; 669c6dcf52cSJan Kara if (!trunc && 67007f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 67107f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 672c6dcf52cSJan Kara goto out; 673d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 67407f2d89cSMatthew Wilcox xas_store(&xas, NULL); 675c6dcf52cSJan Kara mapping->nrexceptional--; 676c6dcf52cSJan Kara ret = 1; 677c6dcf52cSJan Kara out: 67807f2d89cSMatthew Wilcox put_unlocked_entry(&xas, entry); 67907f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 680c6dcf52cSJan Kara return ret; 681c6dcf52cSJan Kara } 68207f2d89cSMatthew Wilcox 683ac401cc7SJan Kara /* 6843159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6853159f943SMatthew Wilcox * to be unlocked before deleting it. 686ac401cc7SJan Kara */ 687ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 688ac401cc7SJan Kara { 689a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 690ac401cc7SJan Kara 691ac401cc7SJan Kara /* 692ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 693ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 694a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6953159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 696ac401cc7SJan Kara * at that index as well... 697ac401cc7SJan Kara */ 698c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 699c6dcf52cSJan Kara return ret; 700ac401cc7SJan Kara } 701ac401cc7SJan Kara 702c6dcf52cSJan Kara /* 7033159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 704c6dcf52cSJan Kara */ 705c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 706c6dcf52cSJan Kara pgoff_t index) 707c6dcf52cSJan Kara { 708a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 709ac401cc7SJan Kara } 710ac401cc7SJan Kara 711c7fe193fSIra Weiny static int copy_cow_page_dax(struct block_device *bdev, struct dax_device *dax_dev, 712c7fe193fSIra Weiny sector_t sector, struct page *to, unsigned long vaddr) 713f7ca90b1SMatthew Wilcox { 714cccbce67SDan Williams void *vto, *kaddr; 715cccbce67SDan Williams pgoff_t pgoff; 716cccbce67SDan Williams long rc; 717cccbce67SDan Williams int id; 718e2e05394SRoss Zwisler 719c7fe193fSIra Weiny rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 720cccbce67SDan Williams if (rc) 721cccbce67SDan Williams return rc; 722cccbce67SDan Williams 723cccbce67SDan Williams id = dax_read_lock(); 724c7fe193fSIra Weiny rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(PAGE_SIZE), &kaddr, NULL); 725cccbce67SDan Williams if (rc < 0) { 726cccbce67SDan Williams dax_read_unlock(id); 727cccbce67SDan Williams return rc; 728cccbce67SDan Williams } 729f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 730cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 731f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 732cccbce67SDan Williams dax_read_unlock(id); 733f7ca90b1SMatthew Wilcox return 0; 734f7ca90b1SMatthew Wilcox } 735f7ca90b1SMatthew Wilcox 736642261acSRoss Zwisler /* 737642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 738642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 739642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 740642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 741642261acSRoss Zwisler * appropriate. 742642261acSRoss Zwisler */ 743b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 744b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 745b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 7469973c98eSRoss Zwisler { 747b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 7489973c98eSRoss Zwisler 749f5b7b748SJan Kara if (dirty) 7509973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7519973c98eSRoss Zwisler 7523159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 753b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 75491d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 75591d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 756977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 757977fbdcdSMatthew Wilcox PG_PMD_NR, false); 75891d25ba8SRoss Zwisler else /* pte entry */ 759b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 760ac401cc7SJan Kara } 7619973c98eSRoss Zwisler 762b15cd800SMatthew Wilcox xas_reset(xas); 763b15cd800SMatthew Wilcox xas_lock_irq(xas); 7641571c029SJan Kara if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 7651571c029SJan Kara void *old; 7661571c029SJan Kara 767d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 76873449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 769642261acSRoss Zwisler /* 770a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 771642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 772a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 773642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 774642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 775642261acSRoss Zwisler * tree and dirty it if necessary. 776642261acSRoss Zwisler */ 7771571c029SJan Kara old = dax_lock_entry(xas, new_entry); 778b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 779b15cd800SMatthew Wilcox DAX_LOCKED)); 78091d25ba8SRoss Zwisler entry = new_entry; 781b15cd800SMatthew Wilcox } else { 782b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 783ac401cc7SJan Kara } 78491d25ba8SRoss Zwisler 785f5b7b748SJan Kara if (dirty) 786b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 78791d25ba8SRoss Zwisler 788b15cd800SMatthew Wilcox xas_unlock_irq(xas); 78991d25ba8SRoss Zwisler return entry; 7909973c98eSRoss Zwisler } 7919973c98eSRoss Zwisler 792a77d19f4SMatthew Wilcox static inline 793a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7944b4bb46dSJan Kara { 7954b4bb46dSJan Kara unsigned long address; 7964b4bb46dSJan Kara 7974b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7984b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 7994b4bb46dSJan Kara return address; 8004b4bb46dSJan Kara } 8014b4bb46dSJan Kara 8024b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 803a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 804a77d19f4SMatthew Wilcox unsigned long pfn) 8054b4bb46dSJan Kara { 8064b4bb46dSJan Kara struct vm_area_struct *vma; 807f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 808f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 8094b4bb46dSJan Kara spinlock_t *ptl; 8104b4bb46dSJan Kara 8114b4bb46dSJan Kara i_mmap_lock_read(mapping); 8124b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 813ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 814ac46d4f3SJérôme Glisse unsigned long address; 8154b4bb46dSJan Kara 8164b4bb46dSJan Kara cond_resched(); 8174b4bb46dSJan Kara 8184b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 8194b4bb46dSJan Kara continue; 8204b4bb46dSJan Kara 8214b4bb46dSJan Kara address = pgoff_address(index, vma); 822a4d1a885SJérôme Glisse 823a4d1a885SJérôme Glisse /* 8249fd6dad1SPaolo Bonzini * follow_invalidate_pte() will use the range to call 825ff5c19edSChristoph Hellwig * mmu_notifier_invalidate_range_start() on our behalf before 826ff5c19edSChristoph Hellwig * taking any lock. 827a4d1a885SJérôme Glisse */ 8289fd6dad1SPaolo Bonzini if (follow_invalidate_pte(vma->vm_mm, address, &range, &ptep, 8299fd6dad1SPaolo Bonzini &pmdp, &ptl)) 8304b4bb46dSJan Kara continue; 831f729c8c9SRoss Zwisler 8320f10851eSJérôme Glisse /* 8330f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 8340f10851eSJérôme Glisse * downgrading page table protection not changing it to point 8350f10851eSJérôme Glisse * to a new page. 8360f10851eSJérôme Glisse * 837ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 8380f10851eSJérôme Glisse */ 839f729c8c9SRoss Zwisler if (pmdp) { 840f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 841f729c8c9SRoss Zwisler pmd_t pmd; 842f729c8c9SRoss Zwisler 843f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 844f729c8c9SRoss Zwisler goto unlock_pmd; 845f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 846f729c8c9SRoss Zwisler goto unlock_pmd; 847f729c8c9SRoss Zwisler 848f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 849024eee0eSAneesh Kumar K.V pmd = pmdp_invalidate(vma, address, pmdp); 850f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 851f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 852f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 853f729c8c9SRoss Zwisler unlock_pmd: 854f729c8c9SRoss Zwisler #endif 855ee190ca6SJan H. Schönherr spin_unlock(ptl); 856f729c8c9SRoss Zwisler } else { 8574b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 858f729c8c9SRoss Zwisler goto unlock_pte; 8594b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 860f729c8c9SRoss Zwisler goto unlock_pte; 8614b4bb46dSJan Kara 8624b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8634b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8644b4bb46dSJan Kara pte = pte_wrprotect(pte); 8654b4bb46dSJan Kara pte = pte_mkclean(pte); 8664b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 867f729c8c9SRoss Zwisler unlock_pte: 8684b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 869f729c8c9SRoss Zwisler } 8704b4bb46dSJan Kara 871ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 8724b4bb46dSJan Kara } 8734b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8744b4bb46dSJan Kara } 8754b4bb46dSJan Kara 8769fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8779fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8789973c98eSRoss Zwisler { 879e4b3448bSMatthew Wilcox unsigned long pfn, index, count; 8803fe0791cSDan Williams long ret = 0; 8819973c98eSRoss Zwisler 8829973c98eSRoss Zwisler /* 883a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 884a6abc2c0SJan Kara * wrong. 8859973c98eSRoss Zwisler */ 8863159f943SMatthew Wilcox if (WARN_ON(!xa_is_value(entry))) 887a6abc2c0SJan Kara return -EIO; 8889973c98eSRoss Zwisler 8899fc747f6SMatthew Wilcox if (unlikely(dax_is_locked(entry))) { 8909fc747f6SMatthew Wilcox void *old_entry = entry; 8919fc747f6SMatthew Wilcox 89223c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(xas, 0); 8939fc747f6SMatthew Wilcox 894a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 8959fc747f6SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 896a6abc2c0SJan Kara goto put_unlocked; 897a6abc2c0SJan Kara /* 8989fc747f6SMatthew Wilcox * Entry got reallocated elsewhere? No need to writeback. 8999fc747f6SMatthew Wilcox * We have to compare pfns as we must not bail out due to 9009fc747f6SMatthew Wilcox * difference in lockbit or entry type. 901a6abc2c0SJan Kara */ 9029fc747f6SMatthew Wilcox if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 903a6abc2c0SJan Kara goto put_unlocked; 904642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 905642261acSRoss Zwisler dax_is_zero_entry(entry))) { 9069973c98eSRoss Zwisler ret = -EIO; 907a6abc2c0SJan Kara goto put_unlocked; 9089973c98eSRoss Zwisler } 9099973c98eSRoss Zwisler 9109fc747f6SMatthew Wilcox /* Another fsync thread may have already done this entry */ 9119fc747f6SMatthew Wilcox if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 912a6abc2c0SJan Kara goto put_unlocked; 9139fc747f6SMatthew Wilcox } 9149fc747f6SMatthew Wilcox 915a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 9169fc747f6SMatthew Wilcox dax_lock_entry(xas, entry); 9179fc747f6SMatthew Wilcox 918a6abc2c0SJan Kara /* 919a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 920a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 921a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 922b93b0163SMatthew Wilcox * at the entry only under the i_pages lock and once they do that 923b93b0163SMatthew Wilcox * they will see the entry locked and wait for it to unlock. 924a6abc2c0SJan Kara */ 9259fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 9269fc747f6SMatthew Wilcox xas_unlock_irq(xas); 927a6abc2c0SJan Kara 928642261acSRoss Zwisler /* 929e4b3448bSMatthew Wilcox * If dax_writeback_mapping_range() was given a wbc->range_start 930e4b3448bSMatthew Wilcox * in the middle of a PMD, the 'index' we use needs to be 931e4b3448bSMatthew Wilcox * aligned to the start of the PMD. 9323fe0791cSDan Williams * This allows us to flush for PMD_SIZE and not have to worry about 9333fe0791cSDan Williams * partial PMD writebacks. 934642261acSRoss Zwisler */ 935a77d19f4SMatthew Wilcox pfn = dax_to_pfn(entry); 936e4b3448bSMatthew Wilcox count = 1UL << dax_entry_order(entry); 937e4b3448bSMatthew Wilcox index = xas->xa_index & ~(count - 1); 938cccbce67SDan Williams 939e4b3448bSMatthew Wilcox dax_entry_mkclean(mapping, index, pfn); 940e4b3448bSMatthew Wilcox dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE); 9414b4bb46dSJan Kara /* 9424b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 9434b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 9444b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 9454b4bb46dSJan Kara * entry lock. 9464b4bb46dSJan Kara */ 9479fc747f6SMatthew Wilcox xas_reset(xas); 9489fc747f6SMatthew Wilcox xas_lock_irq(xas); 9499fc747f6SMatthew Wilcox xas_store(xas, entry); 9509fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 951*698ab77aSVivek Goyal dax_wake_entry(xas, entry, WAKE_NEXT); 9529fc747f6SMatthew Wilcox 953e4b3448bSMatthew Wilcox trace_dax_writeback_one(mapping->host, index, count); 9549973c98eSRoss Zwisler return ret; 9559973c98eSRoss Zwisler 956a6abc2c0SJan Kara put_unlocked: 9579fc747f6SMatthew Wilcox put_unlocked_entry(xas, entry); 9589973c98eSRoss Zwisler return ret; 9599973c98eSRoss Zwisler } 9609973c98eSRoss Zwisler 9619973c98eSRoss Zwisler /* 9629973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 9639973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 9649973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 9659973c98eSRoss Zwisler */ 9667f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 9673f666c56SVivek Goyal struct dax_device *dax_dev, struct writeback_control *wbc) 9689973c98eSRoss Zwisler { 9699fc747f6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 9709973c98eSRoss Zwisler struct inode *inode = mapping->host; 9719fc747f6SMatthew Wilcox pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 9729fc747f6SMatthew Wilcox void *entry; 9739fc747f6SMatthew Wilcox int ret = 0; 9749fc747f6SMatthew Wilcox unsigned int scanned = 0; 9759973c98eSRoss Zwisler 9769973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 9779973c98eSRoss Zwisler return -EIO; 9789973c98eSRoss Zwisler 9797f6d5b52SRoss Zwisler if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) 9807f6d5b52SRoss Zwisler return 0; 9817f6d5b52SRoss Zwisler 9829fc747f6SMatthew Wilcox trace_dax_writeback_range(inode, xas.xa_index, end_index); 9839973c98eSRoss Zwisler 9849fc747f6SMatthew Wilcox tag_pages_for_writeback(mapping, xas.xa_index, end_index); 985d14a3f48SRoss Zwisler 9869fc747f6SMatthew Wilcox xas_lock_irq(&xas); 9879fc747f6SMatthew Wilcox xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 9889fc747f6SMatthew Wilcox ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 989819ec6b9SJeff Layton if (ret < 0) { 990819ec6b9SJeff Layton mapping_set_error(mapping, ret); 9919fc747f6SMatthew Wilcox break; 992d14a3f48SRoss Zwisler } 9939fc747f6SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 9949fc747f6SMatthew Wilcox continue; 9959fc747f6SMatthew Wilcox 9969fc747f6SMatthew Wilcox xas_pause(&xas); 9979fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 9989fc747f6SMatthew Wilcox cond_resched(); 9999fc747f6SMatthew Wilcox xas_lock_irq(&xas); 1000d14a3f48SRoss Zwisler } 10019fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 10029fc747f6SMatthew Wilcox trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 10039fc747f6SMatthew Wilcox return ret; 10049973c98eSRoss Zwisler } 10059973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 10069973c98eSRoss Zwisler 100731a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 1008f7ca90b1SMatthew Wilcox { 1009a3841f94SLinus Torvalds return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; 101031a6f1a6SJan Kara } 1011f7ca90b1SMatthew Wilcox 10125e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, 10135e161e40SJan Kara pfn_t *pfnp) 10145e161e40SJan Kara { 10155e161e40SJan Kara const sector_t sector = dax_iomap_sector(iomap, pos); 10165e161e40SJan Kara pgoff_t pgoff; 10175e161e40SJan Kara int id, rc; 10185e161e40SJan Kara long length; 10195e161e40SJan Kara 10205e161e40SJan Kara rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); 1021cccbce67SDan Williams if (rc) 1022cccbce67SDan Williams return rc; 1023cccbce67SDan Williams id = dax_read_lock(); 10245e161e40SJan Kara length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 102586ed913bSHuaisheng Ye NULL, pfnp); 10265e161e40SJan Kara if (length < 0) { 10275e161e40SJan Kara rc = length; 10285e161e40SJan Kara goto out; 10295e161e40SJan Kara } 10305e161e40SJan Kara rc = -EINVAL; 10315e161e40SJan Kara if (PFN_PHYS(length) < size) 10325e161e40SJan Kara goto out; 10335e161e40SJan Kara if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 10345e161e40SJan Kara goto out; 10355e161e40SJan Kara /* For larger pages we need devmap */ 10365e161e40SJan Kara if (length > 1 && !pfn_t_devmap(*pfnp)) 10375e161e40SJan Kara goto out; 10385e161e40SJan Kara rc = 0; 10395e161e40SJan Kara out: 1040cccbce67SDan Williams dax_read_unlock(id); 1041cccbce67SDan Williams return rc; 1042cccbce67SDan Williams } 1043f7ca90b1SMatthew Wilcox 10442f89dc12SJan Kara /* 104591d25ba8SRoss Zwisler * The user has performed a load from a hole in the file. Allocating a new 104691d25ba8SRoss Zwisler * page in the file would cause excessive storage usage for workloads with 104791d25ba8SRoss Zwisler * sparse files. Instead we insert a read-only mapping of the 4k zero page. 104891d25ba8SRoss Zwisler * If this page is ever written to we will re-fault and change the mapping to 104991d25ba8SRoss Zwisler * point to real DAX storage instead. 10502f89dc12SJan Kara */ 1051b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas, 1052b15cd800SMatthew Wilcox struct address_space *mapping, void **entry, 1053e30331ffSRoss Zwisler struct vm_fault *vmf) 1054e30331ffSRoss Zwisler { 1055e30331ffSRoss Zwisler struct inode *inode = mapping->host; 105691d25ba8SRoss Zwisler unsigned long vaddr = vmf->address; 1057b90ca5ccSMatthew Wilcox pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1058b90ca5ccSMatthew Wilcox vm_fault_t ret; 1059e30331ffSRoss Zwisler 1060b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 10613159f943SMatthew Wilcox DAX_ZERO_PAGE, false); 10623159f943SMatthew Wilcox 1063ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vmf->vma, vaddr, pfn); 1064e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 1065e30331ffSRoss Zwisler return ret; 1066e30331ffSRoss Zwisler } 1067e30331ffSRoss Zwisler 106881ee8e52SMatthew Wilcox (Oracle) s64 dax_iomap_zero(loff_t pos, u64 length, struct iomap *iomap) 1069679c8bd3SChristoph Hellwig { 10704f3b4f16SVivek Goyal sector_t sector = iomap_sector(iomap, pos & PAGE_MASK); 1071cccbce67SDan Williams pgoff_t pgoff; 1072cccbce67SDan Williams long rc, id; 1073cccbce67SDan Williams void *kaddr; 10740a23f9ffSVivek Goyal bool page_aligned = false; 107581ee8e52SMatthew Wilcox (Oracle) unsigned offset = offset_in_page(pos); 107681ee8e52SMatthew Wilcox (Oracle) unsigned size = min_t(u64, PAGE_SIZE - offset, length); 10770a23f9ffSVivek Goyal 10780a23f9ffSVivek Goyal if (IS_ALIGNED(sector << SECTOR_SHIFT, PAGE_SIZE) && 107981ee8e52SMatthew Wilcox (Oracle) (size == PAGE_SIZE)) 10800a23f9ffSVivek Goyal page_aligned = true; 1081cccbce67SDan Williams 10824f3b4f16SVivek Goyal rc = bdev_dax_pgoff(iomap->bdev, sector, PAGE_SIZE, &pgoff); 1083cccbce67SDan Williams if (rc) 1084cccbce67SDan Williams return rc; 1085cccbce67SDan Williams 1086cccbce67SDan Williams id = dax_read_lock(); 10870a23f9ffSVivek Goyal 10880a23f9ffSVivek Goyal if (page_aligned) 108981ee8e52SMatthew Wilcox (Oracle) rc = dax_zero_page_range(iomap->dax_dev, pgoff, 1); 10900a23f9ffSVivek Goyal else 10914f3b4f16SVivek Goyal rc = dax_direct_access(iomap->dax_dev, pgoff, 1, &kaddr, NULL); 1092cccbce67SDan Williams if (rc < 0) { 1093cccbce67SDan Williams dax_read_unlock(id); 1094cccbce67SDan Williams return rc; 1095cccbce67SDan Williams } 10960a23f9ffSVivek Goyal 10970a23f9ffSVivek Goyal if (!page_aligned) { 109881f55870SDan Williams memset(kaddr + offset, 0, size); 10994f3b4f16SVivek Goyal dax_flush(iomap->dax_dev, kaddr + offset, size); 11004b0228faSVishal Verma } 11010a23f9ffSVivek Goyal dax_read_unlock(id); 110281ee8e52SMatthew Wilcox (Oracle) return size; 1103679c8bd3SChristoph Hellwig } 1104679c8bd3SChristoph Hellwig 1105a254e568SChristoph Hellwig static loff_t 110611c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 1107c039b997SGoldwyn Rodrigues struct iomap *iomap, struct iomap *srcmap) 1108a254e568SChristoph Hellwig { 1109cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1110cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1111a254e568SChristoph Hellwig struct iov_iter *iter = data; 1112a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1113a254e568SChristoph Hellwig ssize_t ret = 0; 1114a77d4786SDan Williams size_t xfer; 1115cccbce67SDan Williams int id; 1116a254e568SChristoph Hellwig 1117a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1118a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1119a254e568SChristoph Hellwig if (pos >= end) 1120a254e568SChristoph Hellwig return 0; 1121a254e568SChristoph Hellwig 1122a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1123a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1124a254e568SChristoph Hellwig } 1125a254e568SChristoph Hellwig 1126a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1127a254e568SChristoph Hellwig return -EIO; 1128a254e568SChristoph Hellwig 1129e3fce68cSJan Kara /* 1130e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1131e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1132e3fce68cSJan Kara * written by write(2) is visible in mmap. 1133e3fce68cSJan Kara */ 1134cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1135e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1136e3fce68cSJan Kara pos >> PAGE_SHIFT, 1137e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1138e3fce68cSJan Kara } 1139e3fce68cSJan Kara 1140cccbce67SDan Williams id = dax_read_lock(); 1141a254e568SChristoph Hellwig while (pos < end) { 1142a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1143cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1144cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1145a254e568SChristoph Hellwig ssize_t map_len; 1146cccbce67SDan Williams pgoff_t pgoff; 1147cccbce67SDan Williams void *kaddr; 1148a254e568SChristoph Hellwig 1149d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1150d1908f52SMichal Hocko ret = -EINTR; 1151d1908f52SMichal Hocko break; 1152d1908f52SMichal Hocko } 1153d1908f52SMichal Hocko 1154cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1155cccbce67SDan Williams if (ret) 1156cccbce67SDan Williams break; 1157cccbce67SDan Williams 1158cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 115986ed913bSHuaisheng Ye &kaddr, NULL); 1160a254e568SChristoph Hellwig if (map_len < 0) { 1161a254e568SChristoph Hellwig ret = map_len; 1162a254e568SChristoph Hellwig break; 1163a254e568SChristoph Hellwig } 1164a254e568SChristoph Hellwig 1165cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1166cccbce67SDan Williams kaddr += offset; 1167a254e568SChristoph Hellwig map_len -= offset; 1168a254e568SChristoph Hellwig if (map_len > end - pos) 1169a254e568SChristoph Hellwig map_len = end - pos; 1170a254e568SChristoph Hellwig 1171a2e050f5SRoss Zwisler /* 1172a2e050f5SRoss Zwisler * The userspace address for the memory copy has already been 1173a2e050f5SRoss Zwisler * validated via access_ok() in either vfs_read() or 1174a2e050f5SRoss Zwisler * vfs_write(), depending on which operation we are doing. 1175a2e050f5SRoss Zwisler */ 1176a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1177a77d4786SDan Williams xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1178fec53774SDan Williams map_len, iter); 1179a254e568SChristoph Hellwig else 1180a77d4786SDan Williams xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1181b3a9a0c3SDan Williams map_len, iter); 1182a254e568SChristoph Hellwig 1183a77d4786SDan Williams pos += xfer; 1184a77d4786SDan Williams length -= xfer; 1185a77d4786SDan Williams done += xfer; 1186a77d4786SDan Williams 1187a77d4786SDan Williams if (xfer == 0) 1188a77d4786SDan Williams ret = -EFAULT; 1189a77d4786SDan Williams if (xfer < map_len) 1190a77d4786SDan Williams break; 1191a254e568SChristoph Hellwig } 1192cccbce67SDan Williams dax_read_unlock(id); 1193a254e568SChristoph Hellwig 1194a254e568SChristoph Hellwig return done ? done : ret; 1195a254e568SChristoph Hellwig } 1196a254e568SChristoph Hellwig 1197a254e568SChristoph Hellwig /** 119811c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1199a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1200a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1201a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1202a254e568SChristoph Hellwig * 1203a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1204a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1205a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1206a254e568SChristoph Hellwig */ 1207a254e568SChristoph Hellwig ssize_t 120811c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 12098ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1210a254e568SChristoph Hellwig { 1211a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1212a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1213a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1214a254e568SChristoph Hellwig unsigned flags = 0; 1215a254e568SChristoph Hellwig 1216168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 12179ffbe8acSNikolay Borisov lockdep_assert_held_write(&inode->i_rwsem); 1218a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1219168316dbSChristoph Hellwig } else { 1220168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1221168316dbSChristoph Hellwig } 1222a254e568SChristoph Hellwig 122396222d53SJeff Moyer if (iocb->ki_flags & IOCB_NOWAIT) 122496222d53SJeff Moyer flags |= IOMAP_NOWAIT; 122596222d53SJeff Moyer 1226a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1227a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 122811c59c92SRoss Zwisler iter, dax_iomap_actor); 1229a254e568SChristoph Hellwig if (ret <= 0) 1230a254e568SChristoph Hellwig break; 1231a254e568SChristoph Hellwig pos += ret; 1232a254e568SChristoph Hellwig done += ret; 1233a254e568SChristoph Hellwig } 1234a254e568SChristoph Hellwig 1235a254e568SChristoph Hellwig iocb->ki_pos += done; 1236a254e568SChristoph Hellwig return done ? done : ret; 1237a254e568SChristoph Hellwig } 123811c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1239a7d73fe6SChristoph Hellwig 1240ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error) 12419f141d6eSJan Kara { 12429f141d6eSJan Kara if (error == 0) 12439f141d6eSJan Kara return VM_FAULT_NOPAGE; 1244c9aed74eSSouptick Joarder return vmf_error(error); 12459f141d6eSJan Kara } 12469f141d6eSJan Kara 1247aaa422c4SDan Williams /* 1248aaa422c4SDan Williams * MAP_SYNC on a dax mapping guarantees dirty metadata is 1249aaa422c4SDan Williams * flushed on write-faults (non-cow), but not read-faults. 1250aaa422c4SDan Williams */ 1251aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags, 1252aaa422c4SDan Williams struct vm_area_struct *vma, struct iomap *iomap) 1253aaa422c4SDan Williams { 1254aaa422c4SDan Williams return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) 1255aaa422c4SDan Williams && (iomap->flags & IOMAP_F_DIRTY); 1256aaa422c4SDan Williams } 1257aaa422c4SDan Williams 1258ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1259c0b24625SJan Kara int *iomap_errp, const struct iomap_ops *ops) 1260a7d73fe6SChristoph Hellwig { 1261a0987ad5SJan Kara struct vm_area_struct *vma = vmf->vma; 1262a0987ad5SJan Kara struct address_space *mapping = vma->vm_file->f_mapping; 1263b15cd800SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1264a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 12651a29d85eSJan Kara unsigned long vaddr = vmf->address; 1266a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1267c039b997SGoldwyn Rodrigues struct iomap iomap = { .type = IOMAP_HOLE }; 1268c039b997SGoldwyn Rodrigues struct iomap srcmap = { .type = IOMAP_HOLE }; 12699484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1270a7d73fe6SChristoph Hellwig int error, major = 0; 1271d2c43ef1SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 1272caa51d26SJan Kara bool sync; 1273ab77dab4SSouptick Joarder vm_fault_t ret = 0; 1274a7d73fe6SChristoph Hellwig void *entry; 12751b5a1cb2SJan Kara pfn_t pfn; 1276a7d73fe6SChristoph Hellwig 1277ab77dab4SSouptick Joarder trace_dax_pte_fault(inode, vmf, ret); 1278a7d73fe6SChristoph Hellwig /* 1279a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1280a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1281a7d73fe6SChristoph Hellwig * a reliable test. 1282a7d73fe6SChristoph Hellwig */ 1283a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1284ab77dab4SSouptick Joarder ret = VM_FAULT_SIGBUS; 1285a9c42b33SRoss Zwisler goto out; 1286a9c42b33SRoss Zwisler } 1287a7d73fe6SChristoph Hellwig 1288d2c43ef1SJan Kara if (write && !vmf->cow_page) 1289a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1290a7d73fe6SChristoph Hellwig 1291b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, 0); 1292b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1293b15cd800SMatthew Wilcox ret = xa_to_internal(entry); 129413e451fdSJan Kara goto out; 129513e451fdSJan Kara } 129613e451fdSJan Kara 1297a7d73fe6SChristoph Hellwig /* 1298e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1299e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1300e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1301e2093926SRoss Zwisler * retried. 1302e2093926SRoss Zwisler */ 1303e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1304ab77dab4SSouptick Joarder ret = VM_FAULT_NOPAGE; 1305e2093926SRoss Zwisler goto unlock_entry; 1306e2093926SRoss Zwisler } 1307e2093926SRoss Zwisler 1308e2093926SRoss Zwisler /* 1309a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1310a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1311a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1312a7d73fe6SChristoph Hellwig */ 1313c039b997SGoldwyn Rodrigues error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap, &srcmap); 1314c0b24625SJan Kara if (iomap_errp) 1315c0b24625SJan Kara *iomap_errp = error; 1316a9c42b33SRoss Zwisler if (error) { 1317ab77dab4SSouptick Joarder ret = dax_fault_return(error); 131813e451fdSJan Kara goto unlock_entry; 1319a9c42b33SRoss Zwisler } 1320a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 132113e451fdSJan Kara error = -EIO; /* fs corruption? */ 132213e451fdSJan Kara goto error_finish_iomap; 1323a7d73fe6SChristoph Hellwig } 1324a7d73fe6SChristoph Hellwig 1325a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 132631a6f1a6SJan Kara sector_t sector = dax_iomap_sector(&iomap, pos); 132731a6f1a6SJan Kara 1328a7d73fe6SChristoph Hellwig switch (iomap.type) { 1329a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1330a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1331a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1332a7d73fe6SChristoph Hellwig break; 1333a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1334c7fe193fSIra Weiny error = copy_cow_page_dax(iomap.bdev, iomap.dax_dev, 1335c7fe193fSIra Weiny sector, vmf->cow_page, vaddr); 1336a7d73fe6SChristoph Hellwig break; 1337a7d73fe6SChristoph Hellwig default: 1338a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1339a7d73fe6SChristoph Hellwig error = -EIO; 1340a7d73fe6SChristoph Hellwig break; 1341a7d73fe6SChristoph Hellwig } 1342a7d73fe6SChristoph Hellwig 1343a7d73fe6SChristoph Hellwig if (error) 134413e451fdSJan Kara goto error_finish_iomap; 1345b1aa812bSJan Kara 1346b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1347ab77dab4SSouptick Joarder ret = finish_fault(vmf); 1348ab77dab4SSouptick Joarder if (!ret) 1349ab77dab4SSouptick Joarder ret = VM_FAULT_DONE_COW; 135013e451fdSJan Kara goto finish_iomap; 1351a7d73fe6SChristoph Hellwig } 1352a7d73fe6SChristoph Hellwig 1353aaa422c4SDan Williams sync = dax_fault_is_synchronous(flags, vma, &iomap); 1354caa51d26SJan Kara 1355a7d73fe6SChristoph Hellwig switch (iomap.type) { 1356a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1357a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1358a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 1359a0987ad5SJan Kara count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 1360a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1361a7d73fe6SChristoph Hellwig } 13621b5a1cb2SJan Kara error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); 13631b5a1cb2SJan Kara if (error < 0) 13641b5a1cb2SJan Kara goto error_finish_iomap; 13651b5a1cb2SJan Kara 1366b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 1367caa51d26SJan Kara 0, write && !sync); 13681b5a1cb2SJan Kara 1369caa51d26SJan Kara /* 1370caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1371caa51d26SJan Kara * we can insert PTE into page tables only after that happens. 1372caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1373caa51d26SJan Kara * insert it after fsync is done. 1374caa51d26SJan Kara */ 1375caa51d26SJan Kara if (sync) { 1376caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) { 1377caa51d26SJan Kara error = -EIO; 1378caa51d26SJan Kara goto error_finish_iomap; 1379caa51d26SJan Kara } 1380caa51d26SJan Kara *pfnp = pfn; 1381ab77dab4SSouptick Joarder ret = VM_FAULT_NEEDDSYNC | major; 1382caa51d26SJan Kara goto finish_iomap; 1383caa51d26SJan Kara } 13841b5a1cb2SJan Kara trace_dax_insert_mapping(inode, vmf, entry); 13851b5a1cb2SJan Kara if (write) 1386ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn); 13871b5a1cb2SJan Kara else 1388ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vma, vaddr, pfn); 13891b5a1cb2SJan Kara 1390ab77dab4SSouptick Joarder goto finish_iomap; 1391a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1392a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1393d2c43ef1SJan Kara if (!write) { 1394b15cd800SMatthew Wilcox ret = dax_load_hole(&xas, mapping, &entry, vmf); 139513e451fdSJan Kara goto finish_iomap; 13961550290bSRoss Zwisler } 1397df561f66SGustavo A. R. Silva fallthrough; 1398a7d73fe6SChristoph Hellwig default: 1399a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1400a7d73fe6SChristoph Hellwig error = -EIO; 1401a7d73fe6SChristoph Hellwig break; 1402a7d73fe6SChristoph Hellwig } 1403a7d73fe6SChristoph Hellwig 140413e451fdSJan Kara error_finish_iomap: 1405ab77dab4SSouptick Joarder ret = dax_fault_return(error); 14069f141d6eSJan Kara finish_iomap: 14079f141d6eSJan Kara if (ops->iomap_end) { 14089f141d6eSJan Kara int copied = PAGE_SIZE; 14099f141d6eSJan Kara 1410ab77dab4SSouptick Joarder if (ret & VM_FAULT_ERROR) 14119f141d6eSJan Kara copied = 0; 14129f141d6eSJan Kara /* 14139f141d6eSJan Kara * The fault is done by now and there's no way back (other 14149f141d6eSJan Kara * thread may be already happily using PTE we have installed). 14159f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 14169f141d6eSJan Kara * with it. 14179f141d6eSJan Kara */ 14189f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 14191550290bSRoss Zwisler } 142013e451fdSJan Kara unlock_entry: 1421b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1422a9c42b33SRoss Zwisler out: 1423ab77dab4SSouptick Joarder trace_dax_pte_fault_done(inode, vmf, ret); 1424ab77dab4SSouptick Joarder return ret | major; 1425a7d73fe6SChristoph Hellwig } 1426642261acSRoss Zwisler 1427642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1428b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1429b15cd800SMatthew Wilcox struct iomap *iomap, void **entry) 1430642261acSRoss Zwisler { 1431f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1432f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 143311cf9d86SAneesh Kumar K.V struct vm_area_struct *vma = vmf->vma; 1434653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 143511cf9d86SAneesh Kumar K.V pgtable_t pgtable = NULL; 1436642261acSRoss Zwisler struct page *zero_page; 1437642261acSRoss Zwisler spinlock_t *ptl; 1438642261acSRoss Zwisler pmd_t pmd_entry; 14393fe0791cSDan Williams pfn_t pfn; 1440642261acSRoss Zwisler 1441f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1442642261acSRoss Zwisler 1443642261acSRoss Zwisler if (unlikely(!zero_page)) 1444653b2ea3SRoss Zwisler goto fallback; 1445642261acSRoss Zwisler 14463fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1447b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14483159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1449642261acSRoss Zwisler 145011cf9d86SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) { 145111cf9d86SAneesh Kumar K.V pgtable = pte_alloc_one(vma->vm_mm); 145211cf9d86SAneesh Kumar K.V if (!pgtable) 145311cf9d86SAneesh Kumar K.V return VM_FAULT_OOM; 145411cf9d86SAneesh Kumar K.V } 145511cf9d86SAneesh Kumar K.V 1456f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1457f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1458642261acSRoss Zwisler spin_unlock(ptl); 1459653b2ea3SRoss Zwisler goto fallback; 1460642261acSRoss Zwisler } 1461642261acSRoss Zwisler 146211cf9d86SAneesh Kumar K.V if (pgtable) { 146311cf9d86SAneesh Kumar K.V pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 146411cf9d86SAneesh Kumar K.V mm_inc_nr_ptes(vma->vm_mm); 146511cf9d86SAneesh Kumar K.V } 1466f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1467642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1468f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1469642261acSRoss Zwisler spin_unlock(ptl); 1470b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1471642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1472653b2ea3SRoss Zwisler 1473653b2ea3SRoss Zwisler fallback: 147411cf9d86SAneesh Kumar K.V if (pgtable) 147511cf9d86SAneesh Kumar K.V pte_free(vma->vm_mm, pgtable); 1476b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1477642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1478642261acSRoss Zwisler } 1479642261acSRoss Zwisler 1480ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1481a2d58167SDave Jiang const struct iomap_ops *ops) 1482642261acSRoss Zwisler { 1483f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1484642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1485b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1486d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1487d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1488caa51d26SJan Kara bool sync; 14899484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1490642261acSRoss Zwisler struct inode *inode = mapping->host; 1491ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1492c039b997SGoldwyn Rodrigues struct iomap iomap = { .type = IOMAP_HOLE }; 1493c039b997SGoldwyn Rodrigues struct iomap srcmap = { .type = IOMAP_HOLE }; 1494b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1495642261acSRoss Zwisler void *entry; 1496642261acSRoss Zwisler loff_t pos; 1497642261acSRoss Zwisler int error; 1498302a5e31SJan Kara pfn_t pfn; 1499642261acSRoss Zwisler 1500282a8e03SRoss Zwisler /* 1501282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1502282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1503282a8e03SRoss Zwisler * this is a reliable test. 1504282a8e03SRoss Zwisler */ 1505957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1506282a8e03SRoss Zwisler 1507f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1508282a8e03SRoss Zwisler 1509fffa281bSRoss Zwisler /* 1510fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1511fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1512fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1513a77d19f4SMatthew Wilcox * range in the page cache. 1514fffa281bSRoss Zwisler */ 1515fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1516fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1517fffa281bSRoss Zwisler goto fallback; 1518fffa281bSRoss Zwisler 1519642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1520642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1521642261acSRoss Zwisler goto fallback; 1522642261acSRoss Zwisler 1523642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1524642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1525642261acSRoss Zwisler goto fallback; 1526642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1527642261acSRoss Zwisler goto fallback; 1528642261acSRoss Zwisler 1529b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1530282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1531282a8e03SRoss Zwisler goto out; 1532282a8e03SRoss Zwisler } 1533642261acSRoss Zwisler 1534642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1535b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1536642261acSRoss Zwisler goto fallback; 1537642261acSRoss Zwisler 1538642261acSRoss Zwisler /* 1539b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1540b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1541b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1542b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 15439f141d6eSJan Kara */ 154423c84eb7SMatthew Wilcox (Oracle) entry = grab_mapping_entry(&xas, mapping, PMD_ORDER); 1545b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1546b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1547876f2946SRoss Zwisler goto fallback; 1548b15cd800SMatthew Wilcox } 1549876f2946SRoss Zwisler 1550876f2946SRoss Zwisler /* 1551e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1552e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1553e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1554e2093926SRoss Zwisler * retried. 1555e2093926SRoss Zwisler */ 1556e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1557e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1558e2093926SRoss Zwisler result = 0; 1559e2093926SRoss Zwisler goto unlock_entry; 1560e2093926SRoss Zwisler } 1561e2093926SRoss Zwisler 1562e2093926SRoss Zwisler /* 1563876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1564876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1565876f2946SRoss Zwisler * to look up our filesystem block. 1566876f2946SRoss Zwisler */ 1567b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1568c039b997SGoldwyn Rodrigues error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap, 1569c039b997SGoldwyn Rodrigues &srcmap); 1570876f2946SRoss Zwisler if (error) 1571876f2946SRoss Zwisler goto unlock_entry; 1572876f2946SRoss Zwisler 1573876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15749f141d6eSJan Kara goto finish_iomap; 15759f141d6eSJan Kara 1576aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1577caa51d26SJan Kara 1578642261acSRoss Zwisler switch (iomap.type) { 1579642261acSRoss Zwisler case IOMAP_MAPPED: 1580302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1581302a5e31SJan Kara if (error < 0) 1582302a5e31SJan Kara goto finish_iomap; 1583302a5e31SJan Kara 1584b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15853159f943SMatthew Wilcox DAX_PMD, write && !sync); 1586302a5e31SJan Kara 1587caa51d26SJan Kara /* 1588caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1589caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1590caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1591caa51d26SJan Kara * insert it after fsync is done. 1592caa51d26SJan Kara */ 1593caa51d26SJan Kara if (sync) { 1594caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1595caa51d26SJan Kara goto finish_iomap; 1596caa51d26SJan Kara *pfnp = pfn; 1597caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1598caa51d26SJan Kara goto finish_iomap; 1599caa51d26SJan Kara } 1600caa51d26SJan Kara 1601302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1602fce86ff5SDan Williams result = vmf_insert_pfn_pmd(vmf, pfn, write); 1603642261acSRoss Zwisler break; 1604642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1605642261acSRoss Zwisler case IOMAP_HOLE: 1606642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1607876f2946SRoss Zwisler break; 1608b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1609642261acSRoss Zwisler break; 1610642261acSRoss Zwisler default: 1611642261acSRoss Zwisler WARN_ON_ONCE(1); 1612642261acSRoss Zwisler break; 1613642261acSRoss Zwisler } 1614642261acSRoss Zwisler 16159f141d6eSJan Kara finish_iomap: 16169f141d6eSJan Kara if (ops->iomap_end) { 16179f141d6eSJan Kara int copied = PMD_SIZE; 16189f141d6eSJan Kara 16199f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 16209f141d6eSJan Kara copied = 0; 16219f141d6eSJan Kara /* 16229f141d6eSJan Kara * The fault is done by now and there's no way back (other 16239f141d6eSJan Kara * thread may be already happily using PMD we have installed). 16249f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 16259f141d6eSJan Kara * with it. 16269f141d6eSJan Kara */ 16279f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 16289f141d6eSJan Kara &iomap); 16299f141d6eSJan Kara } 1630876f2946SRoss Zwisler unlock_entry: 1631b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1632642261acSRoss Zwisler fallback: 1633642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1634d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1635642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1636642261acSRoss Zwisler } 1637282a8e03SRoss Zwisler out: 1638f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1639642261acSRoss Zwisler return result; 1640642261acSRoss Zwisler } 1641a2d58167SDave Jiang #else 1642ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 164301cddfe9SArnd Bergmann const struct iomap_ops *ops) 1644a2d58167SDave Jiang { 1645a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1646a2d58167SDave Jiang } 1647642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1648a2d58167SDave Jiang 1649a2d58167SDave Jiang /** 1650a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1651a2d58167SDave Jiang * @vmf: The description of the fault 1652cec04e8cSJan Kara * @pe_size: Size of the page to fault in 16539a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1654c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1655cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1656a2d58167SDave Jiang * 1657a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1658a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1659a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1660a2d58167SDave Jiang * successfully. 1661a2d58167SDave Jiang */ 1662ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1663c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1664a2d58167SDave Jiang { 1665c791ace1SDave Jiang switch (pe_size) { 1666c791ace1SDave Jiang case PE_SIZE_PTE: 1667c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1668c791ace1SDave Jiang case PE_SIZE_PMD: 16699a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1670a2d58167SDave Jiang default: 1671a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1672a2d58167SDave Jiang } 1673a2d58167SDave Jiang } 1674a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 167571eab6dfSJan Kara 1676a77d19f4SMatthew Wilcox /* 167771eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 167871eab6dfSJan Kara * @vmf: The description of the fault 167971eab6dfSJan Kara * @pfn: PFN to insert 1680cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 168171eab6dfSJan Kara * 1682a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1683a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 168471eab6dfSJan Kara */ 1685cfc93c6cSMatthew Wilcox static vm_fault_t 1686cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 168771eab6dfSJan Kara { 168871eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1689cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1690cfc93c6cSMatthew Wilcox void *entry; 1691ab77dab4SSouptick Joarder vm_fault_t ret; 169271eab6dfSJan Kara 1693cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 169423c84eb7SMatthew Wilcox (Oracle) entry = get_unlocked_entry(&xas, order); 169571eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 169623c84eb7SMatthew Wilcox (Oracle) if (!entry || dax_is_conflict(entry) || 169723c84eb7SMatthew Wilcox (Oracle) (order == 0 && !dax_is_pte_entry(entry))) { 1698cfc93c6cSMatthew Wilcox put_unlocked_entry(&xas, entry); 1699cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 170071eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 170171eab6dfSJan Kara VM_FAULT_NOPAGE); 170271eab6dfSJan Kara return VM_FAULT_NOPAGE; 170371eab6dfSJan Kara } 1704cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1705cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1706cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1707cfc93c6cSMatthew Wilcox if (order == 0) 1708ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 170971eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1710cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1711fce86ff5SDan Williams ret = vmf_insert_pfn_pmd(vmf, pfn, FAULT_FLAG_WRITE); 171271eab6dfSJan Kara #endif 1713cfc93c6cSMatthew Wilcox else 1714ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1715cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1716ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1717ab77dab4SSouptick Joarder return ret; 171871eab6dfSJan Kara } 171971eab6dfSJan Kara 172071eab6dfSJan Kara /** 172171eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 172271eab6dfSJan Kara * @vmf: The description of the fault 172371eab6dfSJan Kara * @pe_size: Size of entry to be inserted 172471eab6dfSJan Kara * @pfn: PFN to insert 172571eab6dfSJan Kara * 172671eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 172771eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 172871eab6dfSJan Kara * table entry. 172971eab6dfSJan Kara */ 1730ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1731ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 173271eab6dfSJan Kara { 173371eab6dfSJan Kara int err; 173471eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1735cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1736cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 173771eab6dfSJan Kara 173871eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 173971eab6dfSJan Kara if (err) 174071eab6dfSJan Kara return VM_FAULT_SIGBUS; 1741cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 174271eab6dfSJan Kara } 174371eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1744