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> 29a254e568SChristoph Hellwig #include "internal.h" 30d475c634SMatthew Wilcox 31282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS 32282a8e03SRoss Zwisler #include <trace/events/fs_dax.h> 33282a8e03SRoss Zwisler 34cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size) 35cfc93c6cSMatthew Wilcox { 36cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PTE) 37cfc93c6cSMatthew Wilcox return PAGE_SHIFT - PAGE_SHIFT; 38cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PMD) 39cfc93c6cSMatthew Wilcox return PMD_SHIFT - PAGE_SHIFT; 40cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PUD) 41cfc93c6cSMatthew Wilcox return PUD_SHIFT - PAGE_SHIFT; 42cfc93c6cSMatthew Wilcox return ~0; 43cfc93c6cSMatthew Wilcox } 44cfc93c6cSMatthew Wilcox 45ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */ 46ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12 47ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 48ac401cc7SJan Kara 49917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset. */ 50917f3452SRoss Zwisler #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 51977fbdcdSMatthew Wilcox #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) 52917f3452SRoss Zwisler 53cfc93c6cSMatthew Wilcox /* The order of a PMD entry */ 54cfc93c6cSMatthew Wilcox #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) 55cfc93c6cSMatthew Wilcox 56ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 57ac401cc7SJan Kara 58ac401cc7SJan Kara static int __init init_dax_wait_table(void) 59ac401cc7SJan Kara { 60ac401cc7SJan Kara int i; 61ac401cc7SJan Kara 62ac401cc7SJan Kara for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 63ac401cc7SJan Kara init_waitqueue_head(wait_table + i); 64ac401cc7SJan Kara return 0; 65ac401cc7SJan Kara } 66ac401cc7SJan Kara fs_initcall(init_dax_wait_table); 67ac401cc7SJan Kara 68527b19d0SRoss Zwisler /* 693159f943SMatthew Wilcox * DAX pagecache entries use XArray value entries so they can't be mistaken 703159f943SMatthew Wilcox * for pages. We use one bit for locking, one bit for the entry size (PMD) 713159f943SMatthew Wilcox * and two more to tell us if the entry is a zero page or an empty entry that 723159f943SMatthew Wilcox * is just used for locking. In total four special bits. 73527b19d0SRoss Zwisler * 74527b19d0SRoss Zwisler * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE 75527b19d0SRoss Zwisler * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem 76527b19d0SRoss Zwisler * block allocation. 77527b19d0SRoss Zwisler */ 783159f943SMatthew Wilcox #define DAX_SHIFT (4) 793159f943SMatthew Wilcox #define DAX_LOCKED (1UL << 0) 803159f943SMatthew Wilcox #define DAX_PMD (1UL << 1) 813159f943SMatthew Wilcox #define DAX_ZERO_PAGE (1UL << 2) 823159f943SMatthew Wilcox #define DAX_EMPTY (1UL << 3) 83527b19d0SRoss Zwisler 84a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry) 85527b19d0SRoss Zwisler { 863159f943SMatthew Wilcox return xa_to_value(entry) >> DAX_SHIFT; 87527b19d0SRoss Zwisler } 88527b19d0SRoss Zwisler 899f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags) 909f32d221SMatthew Wilcox { 919f32d221SMatthew Wilcox return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT)); 929f32d221SMatthew Wilcox } 939f32d221SMatthew Wilcox 94cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry) 95cfc93c6cSMatthew Wilcox { 96cfc93c6cSMatthew Wilcox return xa_to_value(entry) & DAX_LOCKED; 97cfc93c6cSMatthew Wilcox } 98cfc93c6cSMatthew Wilcox 99a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry) 100527b19d0SRoss Zwisler { 1013159f943SMatthew Wilcox if (xa_to_value(entry) & DAX_PMD) 102cfc93c6cSMatthew Wilcox return PMD_ORDER; 103527b19d0SRoss Zwisler return 0; 104527b19d0SRoss Zwisler } 105527b19d0SRoss Zwisler 106fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry) 107642261acSRoss Zwisler { 1083159f943SMatthew Wilcox return xa_to_value(entry) & DAX_PMD; 109642261acSRoss Zwisler } 110642261acSRoss Zwisler 111fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry) 112642261acSRoss Zwisler { 1133159f943SMatthew Wilcox return !(xa_to_value(entry) & DAX_PMD); 114642261acSRoss Zwisler } 115642261acSRoss Zwisler 116642261acSRoss Zwisler static int dax_is_zero_entry(void *entry) 117642261acSRoss Zwisler { 1183159f943SMatthew Wilcox return xa_to_value(entry) & DAX_ZERO_PAGE; 119642261acSRoss Zwisler } 120642261acSRoss Zwisler 121642261acSRoss Zwisler static int dax_is_empty_entry(void *entry) 122642261acSRoss Zwisler { 1233159f943SMatthew Wilcox return xa_to_value(entry) & DAX_EMPTY; 124642261acSRoss Zwisler } 125642261acSRoss Zwisler 126f7ca90b1SMatthew Wilcox /* 127a77d19f4SMatthew Wilcox * DAX page cache entry locking 128ac401cc7SJan Kara */ 129ac401cc7SJan Kara struct exceptional_entry_key { 130ec4907ffSMatthew Wilcox struct xarray *xa; 13163e95b5cSRoss Zwisler pgoff_t entry_start; 132ac401cc7SJan Kara }; 133ac401cc7SJan Kara 134ac401cc7SJan Kara struct wait_exceptional_entry_queue { 135ac6424b9SIngo Molnar wait_queue_entry_t wait; 136ac401cc7SJan Kara struct exceptional_entry_key key; 137ac401cc7SJan Kara }; 138ac401cc7SJan Kara 139b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas, 140b15cd800SMatthew Wilcox void *entry, struct exceptional_entry_key *key) 14163e95b5cSRoss Zwisler { 14263e95b5cSRoss Zwisler unsigned long hash; 143b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 14463e95b5cSRoss Zwisler 14563e95b5cSRoss Zwisler /* 14663e95b5cSRoss Zwisler * If 'entry' is a PMD, align the 'index' that we use for the wait 14763e95b5cSRoss Zwisler * queue to the start of that PMD. This ensures that all offsets in 14863e95b5cSRoss Zwisler * the range covered by the PMD map to the same bit lock. 14963e95b5cSRoss Zwisler */ 150642261acSRoss Zwisler if (dax_is_pmd_entry(entry)) 151917f3452SRoss Zwisler index &= ~PG_PMD_COLOUR; 152b15cd800SMatthew Wilcox key->xa = xas->xa; 15363e95b5cSRoss Zwisler key->entry_start = index; 15463e95b5cSRoss Zwisler 155b15cd800SMatthew Wilcox hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS); 15663e95b5cSRoss Zwisler return wait_table + hash; 15763e95b5cSRoss Zwisler } 15863e95b5cSRoss Zwisler 159ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait, 160ec4907ffSMatthew Wilcox unsigned int mode, int sync, void *keyp) 161ac401cc7SJan Kara { 162ac401cc7SJan Kara struct exceptional_entry_key *key = keyp; 163ac401cc7SJan Kara struct wait_exceptional_entry_queue *ewait = 164ac401cc7SJan Kara container_of(wait, struct wait_exceptional_entry_queue, wait); 165ac401cc7SJan Kara 166ec4907ffSMatthew Wilcox if (key->xa != ewait->key.xa || 16763e95b5cSRoss Zwisler key->entry_start != ewait->key.entry_start) 168ac401cc7SJan Kara return 0; 169ac401cc7SJan Kara return autoremove_wake_function(wait, mode, sync, NULL); 170ac401cc7SJan Kara } 171ac401cc7SJan Kara 172ac401cc7SJan Kara /* 173b93b0163SMatthew Wilcox * @entry may no longer be the entry at the index in the mapping. 174b93b0163SMatthew Wilcox * The important information it's conveying is whether the entry at 175b93b0163SMatthew Wilcox * this index used to be a PMD entry. 176e30331ffSRoss Zwisler */ 177b15cd800SMatthew Wilcox static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all) 178e30331ffSRoss Zwisler { 179e30331ffSRoss Zwisler struct exceptional_entry_key key; 180e30331ffSRoss Zwisler wait_queue_head_t *wq; 181e30331ffSRoss Zwisler 182b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &key); 183e30331ffSRoss Zwisler 184e30331ffSRoss Zwisler /* 185e30331ffSRoss Zwisler * Checking for locked entry and prepare_to_wait_exclusive() happens 186b93b0163SMatthew Wilcox * under the i_pages lock, ditto for entry handling in our callers. 187e30331ffSRoss Zwisler * So at this point all tasks that could have seen our entry locked 188e30331ffSRoss Zwisler * must be in the waitqueue and the following check will see them. 189e30331ffSRoss Zwisler */ 190e30331ffSRoss Zwisler if (waitqueue_active(wq)) 191e30331ffSRoss Zwisler __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); 192e30331ffSRoss Zwisler } 193e30331ffSRoss Zwisler 194cfc93c6cSMatthew Wilcox /* 195cfc93c6cSMatthew Wilcox * Look up entry in page cache, wait for it to become unlocked if it 196cfc93c6cSMatthew Wilcox * is a DAX entry and return it. The caller must subsequently call 197cfc93c6cSMatthew Wilcox * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry() 198cfc93c6cSMatthew Wilcox * if it did. 199cfc93c6cSMatthew Wilcox * 200cfc93c6cSMatthew Wilcox * Must be called with the i_pages lock held. 201cfc93c6cSMatthew Wilcox */ 202cfc93c6cSMatthew Wilcox static void *get_unlocked_entry(struct xa_state *xas) 203cfc93c6cSMatthew Wilcox { 204cfc93c6cSMatthew Wilcox void *entry; 205cfc93c6cSMatthew Wilcox struct wait_exceptional_entry_queue ewait; 206cfc93c6cSMatthew Wilcox wait_queue_head_t *wq; 207cfc93c6cSMatthew Wilcox 208cfc93c6cSMatthew Wilcox init_wait(&ewait.wait); 209cfc93c6cSMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 210cfc93c6cSMatthew Wilcox 211cfc93c6cSMatthew Wilcox for (;;) { 2120e40de03SMatthew Wilcox entry = xas_find_conflict(xas); 2130e40de03SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) || 214cfc93c6cSMatthew Wilcox !dax_is_locked(entry)) 215cfc93c6cSMatthew Wilcox return entry; 216cfc93c6cSMatthew Wilcox 217b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 218cfc93c6cSMatthew Wilcox prepare_to_wait_exclusive(wq, &ewait.wait, 219cfc93c6cSMatthew Wilcox TASK_UNINTERRUPTIBLE); 220cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 221cfc93c6cSMatthew Wilcox xas_reset(xas); 222cfc93c6cSMatthew Wilcox schedule(); 223cfc93c6cSMatthew Wilcox finish_wait(wq, &ewait.wait); 224cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 225cfc93c6cSMatthew Wilcox } 226cfc93c6cSMatthew Wilcox } 227cfc93c6cSMatthew Wilcox 22855e56f06SMatthew Wilcox /* 22955e56f06SMatthew Wilcox * The only thing keeping the address space around is the i_pages lock 23055e56f06SMatthew Wilcox * (it's cycled in clear_inode() after removing the entries from i_pages) 23155e56f06SMatthew Wilcox * After we call xas_unlock_irq(), we cannot touch xas->xa. 23255e56f06SMatthew Wilcox */ 23355e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry) 23455e56f06SMatthew Wilcox { 23555e56f06SMatthew Wilcox struct wait_exceptional_entry_queue ewait; 23655e56f06SMatthew Wilcox wait_queue_head_t *wq; 23755e56f06SMatthew Wilcox 23855e56f06SMatthew Wilcox init_wait(&ewait.wait); 23955e56f06SMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 24055e56f06SMatthew Wilcox 24155e56f06SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 242d8a70641SDan Williams /* 243d8a70641SDan Williams * Unlike get_unlocked_entry() there is no guarantee that this 244d8a70641SDan Williams * path ever successfully retrieves an unlocked entry before an 245d8a70641SDan Williams * inode dies. Perform a non-exclusive wait in case this path 246d8a70641SDan Williams * never successfully performs its own wake up. 247d8a70641SDan Williams */ 248d8a70641SDan Williams prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); 24955e56f06SMatthew Wilcox xas_unlock_irq(xas); 25055e56f06SMatthew Wilcox schedule(); 25155e56f06SMatthew Wilcox finish_wait(wq, &ewait.wait); 25255e56f06SMatthew Wilcox } 25355e56f06SMatthew Wilcox 254cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry) 255cfc93c6cSMatthew Wilcox { 256cfc93c6cSMatthew Wilcox /* If we were the only waiter woken, wake the next one */ 257cfc93c6cSMatthew Wilcox if (entry) 258cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 259cfc93c6cSMatthew Wilcox } 260cfc93c6cSMatthew Wilcox 261cfc93c6cSMatthew Wilcox /* 262cfc93c6cSMatthew Wilcox * We used the xa_state to get the entry, but then we locked the entry and 263cfc93c6cSMatthew Wilcox * dropped the xa_lock, so we know the xa_state is stale and must be reset 264cfc93c6cSMatthew Wilcox * before use. 265cfc93c6cSMatthew Wilcox */ 266cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry) 267cfc93c6cSMatthew Wilcox { 268cfc93c6cSMatthew Wilcox void *old; 269cfc93c6cSMatthew Wilcox 2707ae2ea7dSMatthew Wilcox BUG_ON(dax_is_locked(entry)); 271cfc93c6cSMatthew Wilcox xas_reset(xas); 272cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 273cfc93c6cSMatthew Wilcox old = xas_store(xas, entry); 274cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 275cfc93c6cSMatthew Wilcox BUG_ON(!dax_is_locked(old)); 276cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 277cfc93c6cSMatthew Wilcox } 278cfc93c6cSMatthew Wilcox 279cfc93c6cSMatthew Wilcox /* 280cfc93c6cSMatthew Wilcox * Return: The entry stored at this location before it was locked. 281cfc93c6cSMatthew Wilcox */ 282cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry) 283cfc93c6cSMatthew Wilcox { 284cfc93c6cSMatthew Wilcox unsigned long v = xa_to_value(entry); 285cfc93c6cSMatthew Wilcox return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 286cfc93c6cSMatthew Wilcox } 287cfc93c6cSMatthew Wilcox 288d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry) 289d2c997c0SDan Williams { 290d2c997c0SDan Williams if (dax_is_zero_entry(entry)) 291d2c997c0SDan Williams return 0; 292d2c997c0SDan Williams else if (dax_is_empty_entry(entry)) 293d2c997c0SDan Williams return 0; 294d2c997c0SDan Williams else if (dax_is_pmd_entry(entry)) 295d2c997c0SDan Williams return PMD_SIZE; 296d2c997c0SDan Williams else 297d2c997c0SDan Williams return PAGE_SIZE; 298d2c997c0SDan Williams } 299d2c997c0SDan Williams 300a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry) 301d2c997c0SDan Williams { 302a77d19f4SMatthew Wilcox return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE; 303d2c997c0SDan Williams } 304d2c997c0SDan Williams 305d2c997c0SDan Williams /* 306d2c997c0SDan Williams * Iterate through all mapped pfns represented by an entry, i.e. skip 307d2c997c0SDan Williams * 'empty' and 'zero' entries. 308d2c997c0SDan Williams */ 309d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \ 310a77d19f4SMatthew Wilcox for (pfn = dax_to_pfn(entry); \ 311a77d19f4SMatthew Wilcox pfn < dax_end_pfn(entry); pfn++) 312d2c997c0SDan Williams 31373449dafSDan Williams /* 31473449dafSDan Williams * TODO: for reflink+dax we need a way to associate a single page with 31573449dafSDan Williams * multiple address_space instances at different linear_page_index() 31673449dafSDan Williams * offsets. 31773449dafSDan Williams */ 31873449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping, 31973449dafSDan Williams struct vm_area_struct *vma, unsigned long address) 320d2c997c0SDan Williams { 32173449dafSDan Williams unsigned long size = dax_entry_size(entry), pfn, index; 32273449dafSDan Williams int i = 0; 323d2c997c0SDan Williams 324d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 325d2c997c0SDan Williams return; 326d2c997c0SDan Williams 32773449dafSDan Williams index = linear_page_index(vma, address & ~(size - 1)); 328d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 329d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 330d2c997c0SDan Williams 331d2c997c0SDan Williams WARN_ON_ONCE(page->mapping); 332d2c997c0SDan Williams page->mapping = mapping; 33373449dafSDan Williams page->index = index + i++; 334d2c997c0SDan Williams } 335d2c997c0SDan Williams } 336d2c997c0SDan Williams 337d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping, 338d2c997c0SDan Williams bool trunc) 339d2c997c0SDan Williams { 340d2c997c0SDan Williams unsigned long pfn; 341d2c997c0SDan Williams 342d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 343d2c997c0SDan Williams return; 344d2c997c0SDan Williams 345d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 346d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 347d2c997c0SDan Williams 348d2c997c0SDan Williams WARN_ON_ONCE(trunc && page_ref_count(page) > 1); 349d2c997c0SDan Williams WARN_ON_ONCE(page->mapping && page->mapping != mapping); 350d2c997c0SDan Williams page->mapping = NULL; 35173449dafSDan Williams page->index = 0; 352d2c997c0SDan Williams } 353d2c997c0SDan Williams } 354d2c997c0SDan Williams 3555fac7408SDan Williams static struct page *dax_busy_page(void *entry) 3565fac7408SDan Williams { 3575fac7408SDan Williams unsigned long pfn; 3585fac7408SDan Williams 3595fac7408SDan Williams for_each_mapped_pfn(entry, pfn) { 3605fac7408SDan Williams struct page *page = pfn_to_page(pfn); 3615fac7408SDan Williams 3625fac7408SDan Williams if (page_ref_count(page) > 1) 3635fac7408SDan Williams return page; 3645fac7408SDan Williams } 3655fac7408SDan Williams return NULL; 3665fac7408SDan Williams } 3675fac7408SDan Williams 368c5bbd451SMatthew Wilcox /* 369c5bbd451SMatthew Wilcox * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page 370c5bbd451SMatthew Wilcox * @page: The page whose entry we want to lock 371c5bbd451SMatthew Wilcox * 372c5bbd451SMatthew Wilcox * Context: Process context. 37327359fd6SMatthew Wilcox * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could 37427359fd6SMatthew Wilcox * not be locked. 375c5bbd451SMatthew Wilcox */ 37627359fd6SMatthew Wilcox dax_entry_t dax_lock_page(struct page *page) 377c2a7d2a1SDan Williams { 3789f32d221SMatthew Wilcox XA_STATE(xas, NULL, 0); 3799f32d221SMatthew Wilcox void *entry; 380c2a7d2a1SDan Williams 381c5bbd451SMatthew Wilcox /* Ensure page->mapping isn't freed while we look at it */ 382c5bbd451SMatthew Wilcox rcu_read_lock(); 383c2a7d2a1SDan Williams for (;;) { 3849f32d221SMatthew Wilcox struct address_space *mapping = READ_ONCE(page->mapping); 385c2a7d2a1SDan Williams 38627359fd6SMatthew Wilcox entry = NULL; 387c93db7bbSMatthew Wilcox if (!mapping || !dax_mapping(mapping)) 388c5bbd451SMatthew Wilcox break; 389c2a7d2a1SDan Williams 390c2a7d2a1SDan Williams /* 391c2a7d2a1SDan Williams * In the device-dax case there's no need to lock, a 392c2a7d2a1SDan Williams * struct dev_pagemap pin is sufficient to keep the 393c2a7d2a1SDan Williams * inode alive, and we assume we have dev_pagemap pin 394c2a7d2a1SDan Williams * otherwise we would not have a valid pfn_to_page() 395c2a7d2a1SDan Williams * translation. 396c2a7d2a1SDan Williams */ 39727359fd6SMatthew Wilcox entry = (void *)~0UL; 3989f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 399c5bbd451SMatthew Wilcox break; 400c2a7d2a1SDan Williams 4019f32d221SMatthew Wilcox xas.xa = &mapping->i_pages; 4029f32d221SMatthew Wilcox xas_lock_irq(&xas); 403c2a7d2a1SDan Williams if (mapping != page->mapping) { 4049f32d221SMatthew Wilcox xas_unlock_irq(&xas); 405c2a7d2a1SDan Williams continue; 406c2a7d2a1SDan Williams } 4079f32d221SMatthew Wilcox xas_set(&xas, page->index); 4089f32d221SMatthew Wilcox entry = xas_load(&xas); 4099f32d221SMatthew Wilcox if (dax_is_locked(entry)) { 410c5bbd451SMatthew Wilcox rcu_read_unlock(); 41155e56f06SMatthew Wilcox wait_entry_unlocked(&xas, entry); 412c5bbd451SMatthew Wilcox rcu_read_lock(); 413c2a7d2a1SDan Williams continue; 414c2a7d2a1SDan Williams } 4159f32d221SMatthew Wilcox dax_lock_entry(&xas, entry); 4169f32d221SMatthew Wilcox xas_unlock_irq(&xas); 417c5bbd451SMatthew Wilcox break; 4189f32d221SMatthew Wilcox } 419c5bbd451SMatthew Wilcox rcu_read_unlock(); 42027359fd6SMatthew Wilcox return (dax_entry_t)entry; 421c2a7d2a1SDan Williams } 422c2a7d2a1SDan Williams 42327359fd6SMatthew Wilcox void dax_unlock_page(struct page *page, dax_entry_t cookie) 424c2a7d2a1SDan Williams { 425c2a7d2a1SDan Williams struct address_space *mapping = page->mapping; 4269f32d221SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, page->index); 427c2a7d2a1SDan Williams 4289f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 429c2a7d2a1SDan Williams return; 430c2a7d2a1SDan Williams 43127359fd6SMatthew Wilcox dax_unlock_entry(&xas, (void *)cookie); 432c2a7d2a1SDan Williams } 433c2a7d2a1SDan Williams 434ac401cc7SJan Kara /* 435a77d19f4SMatthew Wilcox * Find page cache entry at given index. If it is a DAX entry, return it 436a77d19f4SMatthew Wilcox * with the entry locked. If the page cache doesn't contain an entry at 437a77d19f4SMatthew Wilcox * that index, add a locked empty entry. 438ac401cc7SJan Kara * 4393159f943SMatthew Wilcox * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 440b15cd800SMatthew Wilcox * either return that locked entry or will return VM_FAULT_FALLBACK. 441b15cd800SMatthew Wilcox * This will happen if there are any PTE entries within the PMD range 442b15cd800SMatthew Wilcox * that we are requesting. 443642261acSRoss Zwisler * 444b15cd800SMatthew Wilcox * We always favor PTE entries over PMD entries. There isn't a flow where we 445b15cd800SMatthew Wilcox * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 446b15cd800SMatthew Wilcox * insertion will fail if it finds any PTE entries already in the tree, and a 447b15cd800SMatthew Wilcox * PTE insertion will cause an existing PMD entry to be unmapped and 448b15cd800SMatthew Wilcox * downgraded to PTE entries. This happens for both PMD zero pages as 449b15cd800SMatthew Wilcox * well as PMD empty entries. 450642261acSRoss Zwisler * 451b15cd800SMatthew Wilcox * The exception to this downgrade path is for PMD entries that have 452b15cd800SMatthew Wilcox * real storage backing them. We will leave these real PMD entries in 453b15cd800SMatthew Wilcox * the tree, and PTE writes will simply dirty the entire PMD entry. 454642261acSRoss Zwisler * 455ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 456ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 457ac401cc7SJan Kara * show it helps. 458b15cd800SMatthew Wilcox * 459b15cd800SMatthew Wilcox * On error, this function does not return an ERR_PTR. Instead it returns 460b15cd800SMatthew Wilcox * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 461b15cd800SMatthew Wilcox * overlap with xarray value entries. 462ac401cc7SJan Kara */ 463b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas, 464b15cd800SMatthew Wilcox struct address_space *mapping, unsigned long size_flag) 465ac401cc7SJan Kara { 466b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 467b15cd800SMatthew Wilcox bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */ 468b15cd800SMatthew Wilcox void *entry; 469ac401cc7SJan Kara 470b15cd800SMatthew Wilcox retry: 471b15cd800SMatthew Wilcox xas_lock_irq(xas); 472b15cd800SMatthew Wilcox entry = get_unlocked_entry(xas); 473642261acSRoss Zwisler 474b15cd800SMatthew Wilcox if (entry) { 4750e40de03SMatthew Wilcox if (!xa_is_value(entry)) { 476b15cd800SMatthew Wilcox xas_set_err(xas, EIO); 47791d25ba8SRoss Zwisler goto out_unlock; 47891d25ba8SRoss Zwisler } 47991d25ba8SRoss Zwisler 4803159f943SMatthew Wilcox if (size_flag & DAX_PMD) { 48191d25ba8SRoss Zwisler if (dax_is_pte_entry(entry)) { 482b15cd800SMatthew Wilcox put_unlocked_entry(xas, entry); 483b15cd800SMatthew Wilcox goto fallback; 484642261acSRoss Zwisler } 485642261acSRoss Zwisler } else { /* trying to grab a PTE entry */ 48691d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 487642261acSRoss Zwisler (dax_is_zero_entry(entry) || 488642261acSRoss Zwisler dax_is_empty_entry(entry))) { 489642261acSRoss Zwisler pmd_downgrade = true; 490642261acSRoss Zwisler } 491642261acSRoss Zwisler } 492642261acSRoss Zwisler } 493642261acSRoss Zwisler 494642261acSRoss Zwisler if (pmd_downgrade) { 495642261acSRoss Zwisler /* 496642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 497b93b0163SMatthew Wilcox * the i_pages lock. 498642261acSRoss Zwisler */ 499b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 500642261acSRoss Zwisler 501642261acSRoss Zwisler /* 502642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 503642261acSRoss Zwisler * downgraded are empty entries which don't need to be 504642261acSRoss Zwisler * unmapped. 505642261acSRoss Zwisler */ 506b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 507b15cd800SMatthew Wilcox xas_unlock_irq(xas); 508b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 509b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 510977fbdcdSMatthew Wilcox PG_PMD_NR, false); 511b15cd800SMatthew Wilcox xas_reset(xas); 512b15cd800SMatthew Wilcox xas_lock_irq(xas); 513e11f8b7bSRoss Zwisler } 514e11f8b7bSRoss Zwisler 515d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 516b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 517b15cd800SMatthew Wilcox dax_wake_entry(xas, entry, true); 518642261acSRoss Zwisler mapping->nrexceptional--; 519b15cd800SMatthew Wilcox entry = NULL; 520b15cd800SMatthew Wilcox xas_set(xas, index); 521642261acSRoss Zwisler } 522642261acSRoss Zwisler 523b15cd800SMatthew Wilcox if (entry) { 524b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 525b15cd800SMatthew Wilcox } else { 526b15cd800SMatthew Wilcox entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY); 527b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 528b15cd800SMatthew Wilcox if (xas_error(xas)) 529b15cd800SMatthew Wilcox goto out_unlock; 530ac401cc7SJan Kara mapping->nrexceptional++; 531ac401cc7SJan Kara } 532b15cd800SMatthew Wilcox 533642261acSRoss Zwisler out_unlock: 534b15cd800SMatthew Wilcox xas_unlock_irq(xas); 535b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 536b15cd800SMatthew Wilcox goto retry; 537b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 538b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 539b15cd800SMatthew Wilcox if (xas_error(xas)) 540b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 541e3ad61c6SRoss Zwisler return entry; 542b15cd800SMatthew Wilcox fallback: 543b15cd800SMatthew Wilcox xas_unlock_irq(xas); 544b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 545ac401cc7SJan Kara } 546ac401cc7SJan Kara 5475fac7408SDan Williams /** 5485fac7408SDan Williams * dax_layout_busy_page - find first pinned page in @mapping 5495fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5505fac7408SDan Williams * 5515fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5525fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5535fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5545fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5555fac7408SDan Williams * get_user_pages() usages. 5565fac7408SDan Williams * 5575fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5585fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5595fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5605fac7408SDan Williams * mapping_mapped() becoming true. 5615fac7408SDan Williams */ 5625fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping) 5635fac7408SDan Williams { 564084a8990SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 565084a8990SMatthew Wilcox void *entry; 566084a8990SMatthew Wilcox unsigned int scanned = 0; 5675fac7408SDan Williams struct page *page = NULL; 5685fac7408SDan Williams 5695fac7408SDan Williams /* 5705fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 5715fac7408SDan Williams */ 5725fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 5735fac7408SDan Williams return NULL; 5745fac7408SDan Williams 5755fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 5765fac7408SDan Williams return NULL; 5775fac7408SDan Williams 5785fac7408SDan Williams /* 5795fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 580084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 5815fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 5825fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 5835fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 5845fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 5855fac7408SDan Williams * holding those locks, and unmap_mapping_range() will not zero the 5865fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 5875fac7408SDan Williams * guaranteed to either see new references or prevent new 5885fac7408SDan Williams * references from being established. 5895fac7408SDan Williams */ 5905fac7408SDan Williams unmap_mapping_range(mapping, 0, 0, 1); 5915fac7408SDan Williams 592084a8990SMatthew Wilcox xas_lock_irq(&xas); 593084a8990SMatthew Wilcox xas_for_each(&xas, entry, ULONG_MAX) { 594084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 5955fac7408SDan Williams continue; 596084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 597084a8990SMatthew Wilcox entry = get_unlocked_entry(&xas); 5985fac7408SDan Williams if (entry) 5995fac7408SDan Williams page = dax_busy_page(entry); 600084a8990SMatthew Wilcox put_unlocked_entry(&xas, entry); 6015fac7408SDan Williams if (page) 6025fac7408SDan Williams break; 603084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 604084a8990SMatthew Wilcox continue; 605cdbf8897SRoss Zwisler 606084a8990SMatthew Wilcox xas_pause(&xas); 607084a8990SMatthew Wilcox xas_unlock_irq(&xas); 608084a8990SMatthew Wilcox cond_resched(); 609084a8990SMatthew Wilcox xas_lock_irq(&xas); 6105fac7408SDan Williams } 611084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6125fac7408SDan Williams return page; 6135fac7408SDan Williams } 6145fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6155fac7408SDan Williams 616a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 617c6dcf52cSJan Kara pgoff_t index, bool trunc) 618c6dcf52cSJan Kara { 61907f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 620c6dcf52cSJan Kara int ret = 0; 621c6dcf52cSJan Kara void *entry; 622c6dcf52cSJan Kara 62307f2d89cSMatthew Wilcox xas_lock_irq(&xas); 62407f2d89cSMatthew Wilcox entry = get_unlocked_entry(&xas); 6253159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 626c6dcf52cSJan Kara goto out; 627c6dcf52cSJan Kara if (!trunc && 62807f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 62907f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 630c6dcf52cSJan Kara goto out; 631d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 63207f2d89cSMatthew Wilcox xas_store(&xas, NULL); 633c6dcf52cSJan Kara mapping->nrexceptional--; 634c6dcf52cSJan Kara ret = 1; 635c6dcf52cSJan Kara out: 63607f2d89cSMatthew Wilcox put_unlocked_entry(&xas, entry); 63707f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 638c6dcf52cSJan Kara return ret; 639c6dcf52cSJan Kara } 64007f2d89cSMatthew Wilcox 641ac401cc7SJan Kara /* 6423159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6433159f943SMatthew Wilcox * to be unlocked before deleting it. 644ac401cc7SJan Kara */ 645ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 646ac401cc7SJan Kara { 647a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 648ac401cc7SJan Kara 649ac401cc7SJan Kara /* 650ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 651ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 652a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6533159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 654ac401cc7SJan Kara * at that index as well... 655ac401cc7SJan Kara */ 656c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 657c6dcf52cSJan Kara return ret; 658ac401cc7SJan Kara } 659ac401cc7SJan Kara 660c6dcf52cSJan Kara /* 6613159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 662c6dcf52cSJan Kara */ 663c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 664c6dcf52cSJan Kara pgoff_t index) 665c6dcf52cSJan Kara { 666a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 667ac401cc7SJan Kara } 668ac401cc7SJan Kara 669cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, 670cccbce67SDan Williams sector_t sector, size_t size, struct page *to, 671cccbce67SDan Williams unsigned long vaddr) 672f7ca90b1SMatthew Wilcox { 673cccbce67SDan Williams void *vto, *kaddr; 674cccbce67SDan Williams pgoff_t pgoff; 675cccbce67SDan Williams long rc; 676cccbce67SDan Williams int id; 677e2e05394SRoss Zwisler 678cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 679cccbce67SDan Williams if (rc) 680cccbce67SDan Williams return rc; 681cccbce67SDan Williams 682cccbce67SDan Williams id = dax_read_lock(); 68386ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL); 684cccbce67SDan Williams if (rc < 0) { 685cccbce67SDan Williams dax_read_unlock(id); 686cccbce67SDan Williams return rc; 687cccbce67SDan Williams } 688f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 689cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 690f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 691cccbce67SDan Williams dax_read_unlock(id); 692f7ca90b1SMatthew Wilcox return 0; 693f7ca90b1SMatthew Wilcox } 694f7ca90b1SMatthew Wilcox 695642261acSRoss Zwisler /* 696642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 697642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 698642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 699642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 700642261acSRoss Zwisler * appropriate. 701642261acSRoss Zwisler */ 702b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 703b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 704b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 7059973c98eSRoss Zwisler { 706b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 7079973c98eSRoss Zwisler 708f5b7b748SJan Kara if (dirty) 7099973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7109973c98eSRoss Zwisler 7113159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 712b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 71391d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 71491d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 715977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 716977fbdcdSMatthew Wilcox PG_PMD_NR, false); 71791d25ba8SRoss Zwisler else /* pte entry */ 718b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 719ac401cc7SJan Kara } 7209973c98eSRoss Zwisler 721b15cd800SMatthew Wilcox xas_reset(xas); 722b15cd800SMatthew Wilcox xas_lock_irq(xas); 723d2c997c0SDan Williams if (dax_entry_size(entry) != dax_entry_size(new_entry)) { 724d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 72573449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 726d2c997c0SDan Williams } 727642261acSRoss Zwisler 72891d25ba8SRoss Zwisler if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 729642261acSRoss Zwisler /* 730a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 731642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 732a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 733642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 734642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 735642261acSRoss Zwisler * tree and dirty it if necessary. 736642261acSRoss Zwisler */ 737b15cd800SMatthew Wilcox void *old = dax_lock_entry(xas, new_entry); 738b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 739b15cd800SMatthew Wilcox DAX_LOCKED)); 74091d25ba8SRoss Zwisler entry = new_entry; 741b15cd800SMatthew Wilcox } else { 742b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 743ac401cc7SJan Kara } 74491d25ba8SRoss Zwisler 745f5b7b748SJan Kara if (dirty) 746b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 74791d25ba8SRoss Zwisler 748b15cd800SMatthew Wilcox xas_unlock_irq(xas); 74991d25ba8SRoss Zwisler return entry; 7509973c98eSRoss Zwisler } 7519973c98eSRoss Zwisler 752a77d19f4SMatthew Wilcox static inline 753a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7544b4bb46dSJan Kara { 7554b4bb46dSJan Kara unsigned long address; 7564b4bb46dSJan Kara 7574b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7584b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 7594b4bb46dSJan Kara return address; 7604b4bb46dSJan Kara } 7614b4bb46dSJan Kara 7624b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 763a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 764a77d19f4SMatthew Wilcox unsigned long pfn) 7654b4bb46dSJan Kara { 7664b4bb46dSJan Kara struct vm_area_struct *vma; 767f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 768f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 7694b4bb46dSJan Kara spinlock_t *ptl; 7704b4bb46dSJan Kara 7714b4bb46dSJan Kara i_mmap_lock_read(mapping); 7724b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 773ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 774ac46d4f3SJérôme Glisse unsigned long address; 7754b4bb46dSJan Kara 7764b4bb46dSJan Kara cond_resched(); 7774b4bb46dSJan Kara 7784b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 7794b4bb46dSJan Kara continue; 7804b4bb46dSJan Kara 7814b4bb46dSJan Kara address = pgoff_address(index, vma); 782a4d1a885SJérôme Glisse 783a4d1a885SJérôme Glisse /* 7840cefc36bSIra Weiny * Note because we provide range to follow_pte_pmd it will 785a4d1a885SJérôme Glisse * call mmu_notifier_invalidate_range_start() on our behalf 786a4d1a885SJérôme Glisse * before taking any lock. 787a4d1a885SJérôme Glisse */ 788ac46d4f3SJérôme Glisse if (follow_pte_pmd(vma->vm_mm, address, &range, 789ac46d4f3SJérôme Glisse &ptep, &pmdp, &ptl)) 7904b4bb46dSJan Kara continue; 791f729c8c9SRoss Zwisler 7920f10851eSJérôme Glisse /* 7930f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 7940f10851eSJérôme Glisse * downgrading page table protection not changing it to point 7950f10851eSJérôme Glisse * to a new page. 7960f10851eSJérôme Glisse * 797ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 7980f10851eSJérôme Glisse */ 799f729c8c9SRoss Zwisler if (pmdp) { 800f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 801f729c8c9SRoss Zwisler pmd_t pmd; 802f729c8c9SRoss Zwisler 803f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 804f729c8c9SRoss Zwisler goto unlock_pmd; 805f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 806f729c8c9SRoss Zwisler goto unlock_pmd; 807f729c8c9SRoss Zwisler 808f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 809024eee0eSAneesh Kumar K.V pmd = pmdp_invalidate(vma, address, pmdp); 810f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 811f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 812f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 813f729c8c9SRoss Zwisler unlock_pmd: 814f729c8c9SRoss Zwisler #endif 815ee190ca6SJan H. Schönherr spin_unlock(ptl); 816f729c8c9SRoss Zwisler } else { 8174b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 818f729c8c9SRoss Zwisler goto unlock_pte; 8194b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 820f729c8c9SRoss Zwisler goto unlock_pte; 8214b4bb46dSJan Kara 8224b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8234b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8244b4bb46dSJan Kara pte = pte_wrprotect(pte); 8254b4bb46dSJan Kara pte = pte_mkclean(pte); 8264b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 827f729c8c9SRoss Zwisler unlock_pte: 8284b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 829f729c8c9SRoss Zwisler } 8304b4bb46dSJan Kara 831ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 8324b4bb46dSJan Kara } 8334b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8344b4bb46dSJan Kara } 8354b4bb46dSJan Kara 8369fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8379fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8389973c98eSRoss Zwisler { 839e4b3448bSMatthew Wilcox unsigned long pfn, index, count; 8403fe0791cSDan Williams long ret = 0; 8419973c98eSRoss Zwisler 8429973c98eSRoss Zwisler /* 843a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 844a6abc2c0SJan Kara * wrong. 8459973c98eSRoss Zwisler */ 8463159f943SMatthew Wilcox if (WARN_ON(!xa_is_value(entry))) 847a6abc2c0SJan Kara return -EIO; 8489973c98eSRoss Zwisler 8499fc747f6SMatthew Wilcox if (unlikely(dax_is_locked(entry))) { 8509fc747f6SMatthew Wilcox void *old_entry = entry; 8519fc747f6SMatthew Wilcox 8529fc747f6SMatthew Wilcox entry = get_unlocked_entry(xas); 8539fc747f6SMatthew Wilcox 854a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 8559fc747f6SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 856a6abc2c0SJan Kara goto put_unlocked; 857a6abc2c0SJan Kara /* 8589fc747f6SMatthew Wilcox * Entry got reallocated elsewhere? No need to writeback. 8599fc747f6SMatthew Wilcox * We have to compare pfns as we must not bail out due to 8609fc747f6SMatthew Wilcox * difference in lockbit or entry type. 861a6abc2c0SJan Kara */ 8629fc747f6SMatthew Wilcox if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 863a6abc2c0SJan Kara goto put_unlocked; 864642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 865642261acSRoss Zwisler dax_is_zero_entry(entry))) { 8669973c98eSRoss Zwisler ret = -EIO; 867a6abc2c0SJan Kara goto put_unlocked; 8689973c98eSRoss Zwisler } 8699973c98eSRoss Zwisler 8709fc747f6SMatthew Wilcox /* Another fsync thread may have already done this entry */ 8719fc747f6SMatthew Wilcox if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 872a6abc2c0SJan Kara goto put_unlocked; 8739fc747f6SMatthew Wilcox } 8749fc747f6SMatthew Wilcox 875a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 8769fc747f6SMatthew Wilcox dax_lock_entry(xas, entry); 8779fc747f6SMatthew Wilcox 878a6abc2c0SJan Kara /* 879a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 880a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 881a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 882b93b0163SMatthew Wilcox * at the entry only under the i_pages lock and once they do that 883b93b0163SMatthew Wilcox * they will see the entry locked and wait for it to unlock. 884a6abc2c0SJan Kara */ 8859fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 8869fc747f6SMatthew Wilcox xas_unlock_irq(xas); 887a6abc2c0SJan Kara 888642261acSRoss Zwisler /* 889e4b3448bSMatthew Wilcox * If dax_writeback_mapping_range() was given a wbc->range_start 890e4b3448bSMatthew Wilcox * in the middle of a PMD, the 'index' we use needs to be 891e4b3448bSMatthew Wilcox * aligned to the start of the PMD. 8923fe0791cSDan Williams * This allows us to flush for PMD_SIZE and not have to worry about 8933fe0791cSDan Williams * partial PMD writebacks. 894642261acSRoss Zwisler */ 895a77d19f4SMatthew Wilcox pfn = dax_to_pfn(entry); 896e4b3448bSMatthew Wilcox count = 1UL << dax_entry_order(entry); 897e4b3448bSMatthew Wilcox index = xas->xa_index & ~(count - 1); 898cccbce67SDan Williams 899e4b3448bSMatthew Wilcox dax_entry_mkclean(mapping, index, pfn); 900e4b3448bSMatthew Wilcox dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE); 9014b4bb46dSJan Kara /* 9024b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 9034b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 9044b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 9054b4bb46dSJan Kara * entry lock. 9064b4bb46dSJan Kara */ 9079fc747f6SMatthew Wilcox xas_reset(xas); 9089fc747f6SMatthew Wilcox xas_lock_irq(xas); 9099fc747f6SMatthew Wilcox xas_store(xas, entry); 9109fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 9119fc747f6SMatthew Wilcox dax_wake_entry(xas, entry, false); 9129fc747f6SMatthew Wilcox 913e4b3448bSMatthew Wilcox trace_dax_writeback_one(mapping->host, index, count); 9149973c98eSRoss Zwisler return ret; 9159973c98eSRoss Zwisler 916a6abc2c0SJan Kara put_unlocked: 9179fc747f6SMatthew Wilcox put_unlocked_entry(xas, entry); 9189973c98eSRoss Zwisler return ret; 9199973c98eSRoss Zwisler } 9209973c98eSRoss Zwisler 9219973c98eSRoss Zwisler /* 9229973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 9239973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 9249973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 9259973c98eSRoss Zwisler */ 9267f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 9277f6d5b52SRoss Zwisler struct block_device *bdev, struct writeback_control *wbc) 9289973c98eSRoss Zwisler { 9299fc747f6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 9309973c98eSRoss Zwisler struct inode *inode = mapping->host; 9319fc747f6SMatthew Wilcox pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 932cccbce67SDan Williams struct dax_device *dax_dev; 9339fc747f6SMatthew Wilcox void *entry; 9349fc747f6SMatthew Wilcox int ret = 0; 9359fc747f6SMatthew Wilcox unsigned int scanned = 0; 9369973c98eSRoss Zwisler 9379973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 9389973c98eSRoss Zwisler return -EIO; 9399973c98eSRoss Zwisler 9407f6d5b52SRoss Zwisler if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) 9417f6d5b52SRoss Zwisler return 0; 9427f6d5b52SRoss Zwisler 943cccbce67SDan Williams dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 944cccbce67SDan Williams if (!dax_dev) 945cccbce67SDan Williams return -EIO; 946cccbce67SDan Williams 9479fc747f6SMatthew Wilcox trace_dax_writeback_range(inode, xas.xa_index, end_index); 9489973c98eSRoss Zwisler 9499fc747f6SMatthew Wilcox tag_pages_for_writeback(mapping, xas.xa_index, end_index); 950d14a3f48SRoss Zwisler 9519fc747f6SMatthew Wilcox xas_lock_irq(&xas); 9529fc747f6SMatthew Wilcox xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 9539fc747f6SMatthew Wilcox ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 954819ec6b9SJeff Layton if (ret < 0) { 955819ec6b9SJeff Layton mapping_set_error(mapping, ret); 9569fc747f6SMatthew Wilcox break; 957d14a3f48SRoss Zwisler } 9589fc747f6SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 9599fc747f6SMatthew Wilcox continue; 9609fc747f6SMatthew Wilcox 9619fc747f6SMatthew Wilcox xas_pause(&xas); 9629fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 9639fc747f6SMatthew Wilcox cond_resched(); 9649fc747f6SMatthew Wilcox xas_lock_irq(&xas); 965d14a3f48SRoss Zwisler } 9669fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 967cccbce67SDan Williams put_dax(dax_dev); 9689fc747f6SMatthew Wilcox trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 9699fc747f6SMatthew Wilcox return ret; 9709973c98eSRoss Zwisler } 9719973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 9729973c98eSRoss Zwisler 97331a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 974f7ca90b1SMatthew Wilcox { 975a3841f94SLinus Torvalds return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; 97631a6f1a6SJan Kara } 977f7ca90b1SMatthew Wilcox 9785e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, 9795e161e40SJan Kara pfn_t *pfnp) 9805e161e40SJan Kara { 9815e161e40SJan Kara const sector_t sector = dax_iomap_sector(iomap, pos); 9825e161e40SJan Kara pgoff_t pgoff; 9835e161e40SJan Kara int id, rc; 9845e161e40SJan Kara long length; 9855e161e40SJan Kara 9865e161e40SJan Kara rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); 987cccbce67SDan Williams if (rc) 988cccbce67SDan Williams return rc; 989cccbce67SDan Williams id = dax_read_lock(); 9905e161e40SJan Kara length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 99186ed913bSHuaisheng Ye NULL, pfnp); 9925e161e40SJan Kara if (length < 0) { 9935e161e40SJan Kara rc = length; 9945e161e40SJan Kara goto out; 9955e161e40SJan Kara } 9965e161e40SJan Kara rc = -EINVAL; 9975e161e40SJan Kara if (PFN_PHYS(length) < size) 9985e161e40SJan Kara goto out; 9995e161e40SJan Kara if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 10005e161e40SJan Kara goto out; 10015e161e40SJan Kara /* For larger pages we need devmap */ 10025e161e40SJan Kara if (length > 1 && !pfn_t_devmap(*pfnp)) 10035e161e40SJan Kara goto out; 10045e161e40SJan Kara rc = 0; 10055e161e40SJan Kara out: 1006cccbce67SDan Williams dax_read_unlock(id); 1007cccbce67SDan Williams return rc; 1008cccbce67SDan Williams } 1009f7ca90b1SMatthew Wilcox 10102f89dc12SJan Kara /* 101191d25ba8SRoss Zwisler * The user has performed a load from a hole in the file. Allocating a new 101291d25ba8SRoss Zwisler * page in the file would cause excessive storage usage for workloads with 101391d25ba8SRoss Zwisler * sparse files. Instead we insert a read-only mapping of the 4k zero page. 101491d25ba8SRoss Zwisler * If this page is ever written to we will re-fault and change the mapping to 101591d25ba8SRoss Zwisler * point to real DAX storage instead. 10162f89dc12SJan Kara */ 1017b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas, 1018b15cd800SMatthew Wilcox struct address_space *mapping, void **entry, 1019e30331ffSRoss Zwisler struct vm_fault *vmf) 1020e30331ffSRoss Zwisler { 1021e30331ffSRoss Zwisler struct inode *inode = mapping->host; 102291d25ba8SRoss Zwisler unsigned long vaddr = vmf->address; 1023b90ca5ccSMatthew Wilcox pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1024b90ca5ccSMatthew Wilcox vm_fault_t ret; 1025e30331ffSRoss Zwisler 1026b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 10273159f943SMatthew Wilcox DAX_ZERO_PAGE, false); 10283159f943SMatthew Wilcox 1029ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vmf->vma, vaddr, pfn); 1030e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 1031e30331ffSRoss Zwisler return ret; 1032e30331ffSRoss Zwisler } 1033e30331ffSRoss Zwisler 10344b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev, 10354b0228faSVishal Verma unsigned int offset, unsigned int length) 10364b0228faSVishal Verma { 10374b0228faSVishal Verma unsigned short sector_size = bdev_logical_block_size(bdev); 10384b0228faSVishal Verma 10394b0228faSVishal Verma if (!IS_ALIGNED(offset, sector_size)) 10404b0228faSVishal Verma return false; 10414b0228faSVishal Verma if (!IS_ALIGNED(length, sector_size)) 10424b0228faSVishal Verma return false; 10434b0228faSVishal Verma 10444b0228faSVishal Verma return true; 10454b0228faSVishal Verma } 10464b0228faSVishal Verma 1047cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev, 1048cccbce67SDan Williams struct dax_device *dax_dev, sector_t sector, 1049cccbce67SDan Williams unsigned int offset, unsigned int size) 1050679c8bd3SChristoph Hellwig { 1051cccbce67SDan Williams if (dax_range_is_aligned(bdev, offset, size)) { 1052cccbce67SDan Williams sector_t start_sector = sector + (offset >> 9); 10534b0228faSVishal Verma 10544b0228faSVishal Verma return blkdev_issue_zeroout(bdev, start_sector, 105553ef7d0eSLinus Torvalds size >> 9, GFP_NOFS, 0); 10564b0228faSVishal Verma } else { 1057cccbce67SDan Williams pgoff_t pgoff; 1058cccbce67SDan Williams long rc, id; 1059cccbce67SDan Williams void *kaddr; 1060cccbce67SDan Williams 1061e84b83b9SDan Williams rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 1062cccbce67SDan Williams if (rc) 1063cccbce67SDan Williams return rc; 1064cccbce67SDan Williams 1065cccbce67SDan Williams id = dax_read_lock(); 106686ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL); 1067cccbce67SDan Williams if (rc < 0) { 1068cccbce67SDan Williams dax_read_unlock(id); 1069cccbce67SDan Williams return rc; 1070cccbce67SDan Williams } 107181f55870SDan Williams memset(kaddr + offset, 0, size); 1072c3ca015fSMikulas Patocka dax_flush(dax_dev, kaddr + offset, size); 1073cccbce67SDan Williams dax_read_unlock(id); 10744b0228faSVishal Verma } 1075679c8bd3SChristoph Hellwig return 0; 1076679c8bd3SChristoph Hellwig } 1077679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range); 1078679c8bd3SChristoph Hellwig 1079a254e568SChristoph Hellwig static loff_t 108011c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 1081a254e568SChristoph Hellwig struct iomap *iomap) 1082a254e568SChristoph Hellwig { 1083cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1084cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1085a254e568SChristoph Hellwig struct iov_iter *iter = data; 1086a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1087a254e568SChristoph Hellwig ssize_t ret = 0; 1088a77d4786SDan Williams size_t xfer; 1089cccbce67SDan Williams int id; 1090a254e568SChristoph Hellwig 1091a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1092a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1093a254e568SChristoph Hellwig if (pos >= end) 1094a254e568SChristoph Hellwig return 0; 1095a254e568SChristoph Hellwig 1096a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1097a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1098a254e568SChristoph Hellwig } 1099a254e568SChristoph Hellwig 1100a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1101a254e568SChristoph Hellwig return -EIO; 1102a254e568SChristoph Hellwig 1103e3fce68cSJan Kara /* 1104e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1105e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1106e3fce68cSJan Kara * written by write(2) is visible in mmap. 1107e3fce68cSJan Kara */ 1108cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1109e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1110e3fce68cSJan Kara pos >> PAGE_SHIFT, 1111e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1112e3fce68cSJan Kara } 1113e3fce68cSJan Kara 1114cccbce67SDan Williams id = dax_read_lock(); 1115a254e568SChristoph Hellwig while (pos < end) { 1116a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1117cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1118cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1119a254e568SChristoph Hellwig ssize_t map_len; 1120cccbce67SDan Williams pgoff_t pgoff; 1121cccbce67SDan Williams void *kaddr; 1122a254e568SChristoph Hellwig 1123d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1124d1908f52SMichal Hocko ret = -EINTR; 1125d1908f52SMichal Hocko break; 1126d1908f52SMichal Hocko } 1127d1908f52SMichal Hocko 1128cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1129cccbce67SDan Williams if (ret) 1130cccbce67SDan Williams break; 1131cccbce67SDan Williams 1132cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 113386ed913bSHuaisheng Ye &kaddr, NULL); 1134a254e568SChristoph Hellwig if (map_len < 0) { 1135a254e568SChristoph Hellwig ret = map_len; 1136a254e568SChristoph Hellwig break; 1137a254e568SChristoph Hellwig } 1138a254e568SChristoph Hellwig 1139cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1140cccbce67SDan Williams kaddr += offset; 1141a254e568SChristoph Hellwig map_len -= offset; 1142a254e568SChristoph Hellwig if (map_len > end - pos) 1143a254e568SChristoph Hellwig map_len = end - pos; 1144a254e568SChristoph Hellwig 1145a2e050f5SRoss Zwisler /* 1146a2e050f5SRoss Zwisler * The userspace address for the memory copy has already been 1147a2e050f5SRoss Zwisler * validated via access_ok() in either vfs_read() or 1148a2e050f5SRoss Zwisler * vfs_write(), depending on which operation we are doing. 1149a2e050f5SRoss Zwisler */ 1150a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1151a77d4786SDan Williams xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1152fec53774SDan Williams map_len, iter); 1153a254e568SChristoph Hellwig else 1154a77d4786SDan Williams xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1155b3a9a0c3SDan Williams map_len, iter); 1156a254e568SChristoph Hellwig 1157a77d4786SDan Williams pos += xfer; 1158a77d4786SDan Williams length -= xfer; 1159a77d4786SDan Williams done += xfer; 1160a77d4786SDan Williams 1161a77d4786SDan Williams if (xfer == 0) 1162a77d4786SDan Williams ret = -EFAULT; 1163a77d4786SDan Williams if (xfer < map_len) 1164a77d4786SDan Williams break; 1165a254e568SChristoph Hellwig } 1166cccbce67SDan Williams dax_read_unlock(id); 1167a254e568SChristoph Hellwig 1168a254e568SChristoph Hellwig return done ? done : ret; 1169a254e568SChristoph Hellwig } 1170a254e568SChristoph Hellwig 1171a254e568SChristoph Hellwig /** 117211c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1173a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1174a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1175a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1176a254e568SChristoph Hellwig * 1177a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1178a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1179a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1180a254e568SChristoph Hellwig */ 1181a254e568SChristoph Hellwig ssize_t 118211c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 11838ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1184a254e568SChristoph Hellwig { 1185a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1186a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1187a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1188a254e568SChristoph Hellwig unsigned flags = 0; 1189a254e568SChristoph Hellwig 1190168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 1191168316dbSChristoph Hellwig lockdep_assert_held_exclusive(&inode->i_rwsem); 1192a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1193168316dbSChristoph Hellwig } else { 1194168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1195168316dbSChristoph Hellwig } 1196a254e568SChristoph Hellwig 1197a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1198a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 119911c59c92SRoss Zwisler iter, dax_iomap_actor); 1200a254e568SChristoph Hellwig if (ret <= 0) 1201a254e568SChristoph Hellwig break; 1202a254e568SChristoph Hellwig pos += ret; 1203a254e568SChristoph Hellwig done += ret; 1204a254e568SChristoph Hellwig } 1205a254e568SChristoph Hellwig 1206a254e568SChristoph Hellwig iocb->ki_pos += done; 1207a254e568SChristoph Hellwig return done ? done : ret; 1208a254e568SChristoph Hellwig } 120911c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1210a7d73fe6SChristoph Hellwig 1211ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error) 12129f141d6eSJan Kara { 12139f141d6eSJan Kara if (error == 0) 12149f141d6eSJan Kara return VM_FAULT_NOPAGE; 1215c9aed74eSSouptick Joarder return vmf_error(error); 12169f141d6eSJan Kara } 12179f141d6eSJan Kara 1218aaa422c4SDan Williams /* 1219aaa422c4SDan Williams * MAP_SYNC on a dax mapping guarantees dirty metadata is 1220aaa422c4SDan Williams * flushed on write-faults (non-cow), but not read-faults. 1221aaa422c4SDan Williams */ 1222aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags, 1223aaa422c4SDan Williams struct vm_area_struct *vma, struct iomap *iomap) 1224aaa422c4SDan Williams { 1225aaa422c4SDan Williams return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) 1226aaa422c4SDan Williams && (iomap->flags & IOMAP_F_DIRTY); 1227aaa422c4SDan Williams } 1228aaa422c4SDan Williams 1229ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1230c0b24625SJan Kara int *iomap_errp, const struct iomap_ops *ops) 1231a7d73fe6SChristoph Hellwig { 1232a0987ad5SJan Kara struct vm_area_struct *vma = vmf->vma; 1233a0987ad5SJan Kara struct address_space *mapping = vma->vm_file->f_mapping; 1234b15cd800SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1235a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 12361a29d85eSJan Kara unsigned long vaddr = vmf->address; 1237a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1238a7d73fe6SChristoph Hellwig struct iomap iomap = { 0 }; 12399484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1240a7d73fe6SChristoph Hellwig int error, major = 0; 1241d2c43ef1SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 1242caa51d26SJan Kara bool sync; 1243ab77dab4SSouptick Joarder vm_fault_t ret = 0; 1244a7d73fe6SChristoph Hellwig void *entry; 12451b5a1cb2SJan Kara pfn_t pfn; 1246a7d73fe6SChristoph Hellwig 1247ab77dab4SSouptick Joarder trace_dax_pte_fault(inode, vmf, ret); 1248a7d73fe6SChristoph Hellwig /* 1249a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1250a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1251a7d73fe6SChristoph Hellwig * a reliable test. 1252a7d73fe6SChristoph Hellwig */ 1253a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1254ab77dab4SSouptick Joarder ret = VM_FAULT_SIGBUS; 1255a9c42b33SRoss Zwisler goto out; 1256a9c42b33SRoss Zwisler } 1257a7d73fe6SChristoph Hellwig 1258d2c43ef1SJan Kara if (write && !vmf->cow_page) 1259a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1260a7d73fe6SChristoph Hellwig 1261b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, 0); 1262b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1263b15cd800SMatthew Wilcox ret = xa_to_internal(entry); 126413e451fdSJan Kara goto out; 126513e451fdSJan Kara } 126613e451fdSJan Kara 1267a7d73fe6SChristoph Hellwig /* 1268e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1269e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1270e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1271e2093926SRoss Zwisler * retried. 1272e2093926SRoss Zwisler */ 1273e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1274ab77dab4SSouptick Joarder ret = VM_FAULT_NOPAGE; 1275e2093926SRoss Zwisler goto unlock_entry; 1276e2093926SRoss Zwisler } 1277e2093926SRoss Zwisler 1278e2093926SRoss Zwisler /* 1279a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1280a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1281a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1282a7d73fe6SChristoph Hellwig */ 1283a7d73fe6SChristoph Hellwig error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap); 1284c0b24625SJan Kara if (iomap_errp) 1285c0b24625SJan Kara *iomap_errp = error; 1286a9c42b33SRoss Zwisler if (error) { 1287ab77dab4SSouptick Joarder ret = dax_fault_return(error); 128813e451fdSJan Kara goto unlock_entry; 1289a9c42b33SRoss Zwisler } 1290a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 129113e451fdSJan Kara error = -EIO; /* fs corruption? */ 129213e451fdSJan Kara goto error_finish_iomap; 1293a7d73fe6SChristoph Hellwig } 1294a7d73fe6SChristoph Hellwig 1295a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 129631a6f1a6SJan Kara sector_t sector = dax_iomap_sector(&iomap, pos); 129731a6f1a6SJan Kara 1298a7d73fe6SChristoph Hellwig switch (iomap.type) { 1299a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1300a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1301a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1302a7d73fe6SChristoph Hellwig break; 1303a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1304cccbce67SDan Williams error = copy_user_dax(iomap.bdev, iomap.dax_dev, 1305cccbce67SDan Williams sector, PAGE_SIZE, vmf->cow_page, vaddr); 1306a7d73fe6SChristoph Hellwig break; 1307a7d73fe6SChristoph Hellwig default: 1308a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1309a7d73fe6SChristoph Hellwig error = -EIO; 1310a7d73fe6SChristoph Hellwig break; 1311a7d73fe6SChristoph Hellwig } 1312a7d73fe6SChristoph Hellwig 1313a7d73fe6SChristoph Hellwig if (error) 131413e451fdSJan Kara goto error_finish_iomap; 1315b1aa812bSJan Kara 1316b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1317ab77dab4SSouptick Joarder ret = finish_fault(vmf); 1318ab77dab4SSouptick Joarder if (!ret) 1319ab77dab4SSouptick Joarder ret = VM_FAULT_DONE_COW; 132013e451fdSJan Kara goto finish_iomap; 1321a7d73fe6SChristoph Hellwig } 1322a7d73fe6SChristoph Hellwig 1323aaa422c4SDan Williams sync = dax_fault_is_synchronous(flags, vma, &iomap); 1324caa51d26SJan Kara 1325a7d73fe6SChristoph Hellwig switch (iomap.type) { 1326a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1327a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1328a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 1329a0987ad5SJan Kara count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 1330a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1331a7d73fe6SChristoph Hellwig } 13321b5a1cb2SJan Kara error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); 13331b5a1cb2SJan Kara if (error < 0) 13341b5a1cb2SJan Kara goto error_finish_iomap; 13351b5a1cb2SJan Kara 1336b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 1337caa51d26SJan Kara 0, write && !sync); 13381b5a1cb2SJan Kara 1339caa51d26SJan Kara /* 1340caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1341caa51d26SJan Kara * we can insert PTE into page tables only after that happens. 1342caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1343caa51d26SJan Kara * insert it after fsync is done. 1344caa51d26SJan Kara */ 1345caa51d26SJan Kara if (sync) { 1346caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) { 1347caa51d26SJan Kara error = -EIO; 1348caa51d26SJan Kara goto error_finish_iomap; 1349caa51d26SJan Kara } 1350caa51d26SJan Kara *pfnp = pfn; 1351ab77dab4SSouptick Joarder ret = VM_FAULT_NEEDDSYNC | major; 1352caa51d26SJan Kara goto finish_iomap; 1353caa51d26SJan Kara } 13541b5a1cb2SJan Kara trace_dax_insert_mapping(inode, vmf, entry); 13551b5a1cb2SJan Kara if (write) 1356ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn); 13571b5a1cb2SJan Kara else 1358ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vma, vaddr, pfn); 13591b5a1cb2SJan Kara 1360ab77dab4SSouptick Joarder goto finish_iomap; 1361a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1362a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1363d2c43ef1SJan Kara if (!write) { 1364b15cd800SMatthew Wilcox ret = dax_load_hole(&xas, mapping, &entry, vmf); 136513e451fdSJan Kara goto finish_iomap; 13661550290bSRoss Zwisler } 1367a7d73fe6SChristoph Hellwig /*FALLTHRU*/ 1368a7d73fe6SChristoph Hellwig default: 1369a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1370a7d73fe6SChristoph Hellwig error = -EIO; 1371a7d73fe6SChristoph Hellwig break; 1372a7d73fe6SChristoph Hellwig } 1373a7d73fe6SChristoph Hellwig 137413e451fdSJan Kara error_finish_iomap: 1375ab77dab4SSouptick Joarder ret = dax_fault_return(error); 13769f141d6eSJan Kara finish_iomap: 13779f141d6eSJan Kara if (ops->iomap_end) { 13789f141d6eSJan Kara int copied = PAGE_SIZE; 13799f141d6eSJan Kara 1380ab77dab4SSouptick Joarder if (ret & VM_FAULT_ERROR) 13819f141d6eSJan Kara copied = 0; 13829f141d6eSJan Kara /* 13839f141d6eSJan Kara * The fault is done by now and there's no way back (other 13849f141d6eSJan Kara * thread may be already happily using PTE we have installed). 13859f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 13869f141d6eSJan Kara * with it. 13879f141d6eSJan Kara */ 13889f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 13891550290bSRoss Zwisler } 139013e451fdSJan Kara unlock_entry: 1391b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1392a9c42b33SRoss Zwisler out: 1393ab77dab4SSouptick Joarder trace_dax_pte_fault_done(inode, vmf, ret); 1394ab77dab4SSouptick Joarder return ret | major; 1395a7d73fe6SChristoph Hellwig } 1396642261acSRoss Zwisler 1397642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1398b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1399b15cd800SMatthew Wilcox struct iomap *iomap, void **entry) 1400642261acSRoss Zwisler { 1401f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1402f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 140311cf9d86SAneesh Kumar K.V struct vm_area_struct *vma = vmf->vma; 1404653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 140511cf9d86SAneesh Kumar K.V pgtable_t pgtable = NULL; 1406642261acSRoss Zwisler struct page *zero_page; 1407642261acSRoss Zwisler spinlock_t *ptl; 1408642261acSRoss Zwisler pmd_t pmd_entry; 14093fe0791cSDan Williams pfn_t pfn; 1410642261acSRoss Zwisler 1411f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1412642261acSRoss Zwisler 1413642261acSRoss Zwisler if (unlikely(!zero_page)) 1414653b2ea3SRoss Zwisler goto fallback; 1415642261acSRoss Zwisler 14163fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1417b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14183159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1419642261acSRoss Zwisler 142011cf9d86SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) { 142111cf9d86SAneesh Kumar K.V pgtable = pte_alloc_one(vma->vm_mm); 142211cf9d86SAneesh Kumar K.V if (!pgtable) 142311cf9d86SAneesh Kumar K.V return VM_FAULT_OOM; 142411cf9d86SAneesh Kumar K.V } 142511cf9d86SAneesh Kumar K.V 1426f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1427f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1428642261acSRoss Zwisler spin_unlock(ptl); 1429653b2ea3SRoss Zwisler goto fallback; 1430642261acSRoss Zwisler } 1431642261acSRoss Zwisler 143211cf9d86SAneesh Kumar K.V if (pgtable) { 143311cf9d86SAneesh Kumar K.V pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 143411cf9d86SAneesh Kumar K.V mm_inc_nr_ptes(vma->vm_mm); 143511cf9d86SAneesh Kumar K.V } 1436f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1437642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1438f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1439642261acSRoss Zwisler spin_unlock(ptl); 1440b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1441642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1442653b2ea3SRoss Zwisler 1443653b2ea3SRoss Zwisler fallback: 144411cf9d86SAneesh Kumar K.V if (pgtable) 144511cf9d86SAneesh Kumar K.V pte_free(vma->vm_mm, pgtable); 1446b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1447642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1448642261acSRoss Zwisler } 1449642261acSRoss Zwisler 1450ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1451a2d58167SDave Jiang const struct iomap_ops *ops) 1452642261acSRoss Zwisler { 1453f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1454642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1455b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1456d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1457d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1458caa51d26SJan Kara bool sync; 14599484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1460642261acSRoss Zwisler struct inode *inode = mapping->host; 1461ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1462642261acSRoss Zwisler struct iomap iomap = { 0 }; 1463b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1464642261acSRoss Zwisler void *entry; 1465642261acSRoss Zwisler loff_t pos; 1466642261acSRoss Zwisler int error; 1467302a5e31SJan Kara pfn_t pfn; 1468642261acSRoss Zwisler 1469282a8e03SRoss Zwisler /* 1470282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1471282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1472282a8e03SRoss Zwisler * this is a reliable test. 1473282a8e03SRoss Zwisler */ 1474957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1475282a8e03SRoss Zwisler 1476f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1477282a8e03SRoss Zwisler 1478fffa281bSRoss Zwisler /* 1479fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1480fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1481fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1482a77d19f4SMatthew Wilcox * range in the page cache. 1483fffa281bSRoss Zwisler */ 1484fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1485fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1486fffa281bSRoss Zwisler goto fallback; 1487fffa281bSRoss Zwisler 1488642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1489642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1490642261acSRoss Zwisler goto fallback; 1491642261acSRoss Zwisler 1492642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1493642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1494642261acSRoss Zwisler goto fallback; 1495642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1496642261acSRoss Zwisler goto fallback; 1497642261acSRoss Zwisler 1498b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1499282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1500282a8e03SRoss Zwisler goto out; 1501282a8e03SRoss Zwisler } 1502642261acSRoss Zwisler 1503642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1504b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1505642261acSRoss Zwisler goto fallback; 1506642261acSRoss Zwisler 1507642261acSRoss Zwisler /* 1508b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1509b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1510b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1511b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 15129f141d6eSJan Kara */ 1513b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, DAX_PMD); 1514b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1515b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1516876f2946SRoss Zwisler goto fallback; 1517b15cd800SMatthew Wilcox } 1518876f2946SRoss Zwisler 1519876f2946SRoss Zwisler /* 1520e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1521e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1522e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1523e2093926SRoss Zwisler * retried. 1524e2093926SRoss Zwisler */ 1525e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1526e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1527e2093926SRoss Zwisler result = 0; 1528e2093926SRoss Zwisler goto unlock_entry; 1529e2093926SRoss Zwisler } 1530e2093926SRoss Zwisler 1531e2093926SRoss Zwisler /* 1532876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1533876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1534876f2946SRoss Zwisler * to look up our filesystem block. 1535876f2946SRoss Zwisler */ 1536b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1537876f2946SRoss Zwisler error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); 1538876f2946SRoss Zwisler if (error) 1539876f2946SRoss Zwisler goto unlock_entry; 1540876f2946SRoss Zwisler 1541876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15429f141d6eSJan Kara goto finish_iomap; 15439f141d6eSJan Kara 1544aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1545caa51d26SJan Kara 1546642261acSRoss Zwisler switch (iomap.type) { 1547642261acSRoss Zwisler case IOMAP_MAPPED: 1548302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1549302a5e31SJan Kara if (error < 0) 1550302a5e31SJan Kara goto finish_iomap; 1551302a5e31SJan Kara 1552b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15533159f943SMatthew Wilcox DAX_PMD, write && !sync); 1554302a5e31SJan Kara 1555caa51d26SJan Kara /* 1556caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1557caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1558caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1559caa51d26SJan Kara * insert it after fsync is done. 1560caa51d26SJan Kara */ 1561caa51d26SJan Kara if (sync) { 1562caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1563caa51d26SJan Kara goto finish_iomap; 1564caa51d26SJan Kara *pfnp = pfn; 1565caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1566caa51d26SJan Kara goto finish_iomap; 1567caa51d26SJan Kara } 1568caa51d26SJan Kara 1569302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1570fce86ff5SDan Williams result = vmf_insert_pfn_pmd(vmf, pfn, write); 1571642261acSRoss Zwisler break; 1572642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1573642261acSRoss Zwisler case IOMAP_HOLE: 1574642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1575876f2946SRoss Zwisler break; 1576b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1577642261acSRoss Zwisler break; 1578642261acSRoss Zwisler default: 1579642261acSRoss Zwisler WARN_ON_ONCE(1); 1580642261acSRoss Zwisler break; 1581642261acSRoss Zwisler } 1582642261acSRoss Zwisler 15839f141d6eSJan Kara finish_iomap: 15849f141d6eSJan Kara if (ops->iomap_end) { 15859f141d6eSJan Kara int copied = PMD_SIZE; 15869f141d6eSJan Kara 15879f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 15889f141d6eSJan Kara copied = 0; 15899f141d6eSJan Kara /* 15909f141d6eSJan Kara * The fault is done by now and there's no way back (other 15919f141d6eSJan Kara * thread may be already happily using PMD we have installed). 15929f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 15939f141d6eSJan Kara * with it. 15949f141d6eSJan Kara */ 15959f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 15969f141d6eSJan Kara &iomap); 15979f141d6eSJan Kara } 1598876f2946SRoss Zwisler unlock_entry: 1599b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1600642261acSRoss Zwisler fallback: 1601642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1602d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1603642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1604642261acSRoss Zwisler } 1605282a8e03SRoss Zwisler out: 1606f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1607642261acSRoss Zwisler return result; 1608642261acSRoss Zwisler } 1609a2d58167SDave Jiang #else 1610ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 161101cddfe9SArnd Bergmann const struct iomap_ops *ops) 1612a2d58167SDave Jiang { 1613a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1614a2d58167SDave Jiang } 1615642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1616a2d58167SDave Jiang 1617a2d58167SDave Jiang /** 1618a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1619a2d58167SDave Jiang * @vmf: The description of the fault 1620cec04e8cSJan Kara * @pe_size: Size of the page to fault in 16219a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1622c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1623cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1624a2d58167SDave Jiang * 1625a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1626a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1627a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1628a2d58167SDave Jiang * successfully. 1629a2d58167SDave Jiang */ 1630ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1631c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1632a2d58167SDave Jiang { 1633c791ace1SDave Jiang switch (pe_size) { 1634c791ace1SDave Jiang case PE_SIZE_PTE: 1635c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1636c791ace1SDave Jiang case PE_SIZE_PMD: 16379a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1638a2d58167SDave Jiang default: 1639a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1640a2d58167SDave Jiang } 1641a2d58167SDave Jiang } 1642a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 164371eab6dfSJan Kara 1644a77d19f4SMatthew Wilcox /* 164571eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 164671eab6dfSJan Kara * @vmf: The description of the fault 164771eab6dfSJan Kara * @pfn: PFN to insert 1648cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 164971eab6dfSJan Kara * 1650a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1651a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 165271eab6dfSJan Kara */ 1653cfc93c6cSMatthew Wilcox static vm_fault_t 1654cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 165571eab6dfSJan Kara { 165671eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1657cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1658cfc93c6cSMatthew Wilcox void *entry; 1659ab77dab4SSouptick Joarder vm_fault_t ret; 166071eab6dfSJan Kara 1661cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 1662cfc93c6cSMatthew Wilcox entry = get_unlocked_entry(&xas); 166371eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 166471eab6dfSJan Kara if (!entry || 1665cfc93c6cSMatthew Wilcox (order == 0 && !dax_is_pte_entry(entry)) || 16660e40de03SMatthew Wilcox (order == PMD_ORDER && !dax_is_pmd_entry(entry))) { 1667cfc93c6cSMatthew Wilcox put_unlocked_entry(&xas, entry); 1668cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 166971eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 167071eab6dfSJan Kara VM_FAULT_NOPAGE); 167171eab6dfSJan Kara return VM_FAULT_NOPAGE; 167271eab6dfSJan Kara } 1673cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1674cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1675cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1676cfc93c6cSMatthew Wilcox if (order == 0) 1677ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 167871eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1679cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1680fce86ff5SDan Williams ret = vmf_insert_pfn_pmd(vmf, pfn, FAULT_FLAG_WRITE); 168171eab6dfSJan Kara #endif 1682cfc93c6cSMatthew Wilcox else 1683ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1684cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1685ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1686ab77dab4SSouptick Joarder return ret; 168771eab6dfSJan Kara } 168871eab6dfSJan Kara 168971eab6dfSJan Kara /** 169071eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 169171eab6dfSJan Kara * @vmf: The description of the fault 169271eab6dfSJan Kara * @pe_size: Size of entry to be inserted 169371eab6dfSJan Kara * @pfn: PFN to insert 169471eab6dfSJan Kara * 169571eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 169671eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 169771eab6dfSJan Kara * table entry. 169871eab6dfSJan Kara */ 1699ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1700ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 170171eab6dfSJan Kara { 170271eab6dfSJan Kara int err; 170371eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1704cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1705cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 170671eab6dfSJan Kara 170771eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 170871eab6dfSJan Kara if (err) 170971eab6dfSJan Kara return VM_FAULT_SIGBUS; 1710cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 171171eab6dfSJan Kara } 171271eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1713