1d475c634SMatthew Wilcox /* 2d475c634SMatthew Wilcox * fs/dax.c - Direct Access filesystem code 3d475c634SMatthew Wilcox * Copyright (c) 2013-2014 Intel Corporation 4d475c634SMatthew Wilcox * Author: Matthew Wilcox <matthew.r.wilcox@intel.com> 5d475c634SMatthew Wilcox * Author: Ross Zwisler <ross.zwisler@linux.intel.com> 6d475c634SMatthew Wilcox * 7d475c634SMatthew Wilcox * This program is free software; you can redistribute it and/or modify it 8d475c634SMatthew Wilcox * under the terms and conditions of the GNU General Public License, 9d475c634SMatthew Wilcox * version 2, as published by the Free Software Foundation. 10d475c634SMatthew Wilcox * 11d475c634SMatthew Wilcox * This program is distributed in the hope it will be useful, but WITHOUT 12d475c634SMatthew Wilcox * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13d475c634SMatthew Wilcox * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14d475c634SMatthew Wilcox * more details. 15d475c634SMatthew Wilcox */ 16d475c634SMatthew Wilcox 17d475c634SMatthew Wilcox #include <linux/atomic.h> 18d475c634SMatthew Wilcox #include <linux/blkdev.h> 19d475c634SMatthew Wilcox #include <linux/buffer_head.h> 20d77e92e2SRoss Zwisler #include <linux/dax.h> 21d475c634SMatthew Wilcox #include <linux/fs.h> 22d475c634SMatthew Wilcox #include <linux/genhd.h> 23f7ca90b1SMatthew Wilcox #include <linux/highmem.h> 24f7ca90b1SMatthew Wilcox #include <linux/memcontrol.h> 25f7ca90b1SMatthew Wilcox #include <linux/mm.h> 26d475c634SMatthew Wilcox #include <linux/mutex.h> 279973c98eSRoss Zwisler #include <linux/pagevec.h> 28289c6aedSMatthew Wilcox #include <linux/sched.h> 29f361bf4aSIngo Molnar #include <linux/sched/signal.h> 30d475c634SMatthew Wilcox #include <linux/uio.h> 31f7ca90b1SMatthew Wilcox #include <linux/vmstat.h> 3234c0fd54SDan Williams #include <linux/pfn_t.h> 330e749e54SDan Williams #include <linux/sizes.h> 344b4bb46dSJan Kara #include <linux/mmu_notifier.h> 35a254e568SChristoph Hellwig #include <linux/iomap.h> 36a254e568SChristoph Hellwig #include "internal.h" 37d475c634SMatthew Wilcox 38282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS 39282a8e03SRoss Zwisler #include <trace/events/fs_dax.h> 40282a8e03SRoss Zwisler 41cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size) 42cfc93c6cSMatthew Wilcox { 43cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PTE) 44cfc93c6cSMatthew Wilcox return PAGE_SHIFT - PAGE_SHIFT; 45cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PMD) 46cfc93c6cSMatthew Wilcox return PMD_SHIFT - PAGE_SHIFT; 47cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PUD) 48cfc93c6cSMatthew Wilcox return PUD_SHIFT - PAGE_SHIFT; 49cfc93c6cSMatthew Wilcox return ~0; 50cfc93c6cSMatthew Wilcox } 51cfc93c6cSMatthew Wilcox 52ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */ 53ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12 54ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 55ac401cc7SJan Kara 56917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset. */ 57917f3452SRoss Zwisler #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 58977fbdcdSMatthew Wilcox #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) 59917f3452SRoss Zwisler 60cfc93c6cSMatthew Wilcox /* The order of a PMD entry */ 61cfc93c6cSMatthew Wilcox #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) 62cfc93c6cSMatthew Wilcox 63ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 64ac401cc7SJan Kara 65ac401cc7SJan Kara static int __init init_dax_wait_table(void) 66ac401cc7SJan Kara { 67ac401cc7SJan Kara int i; 68ac401cc7SJan Kara 69ac401cc7SJan Kara for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 70ac401cc7SJan Kara init_waitqueue_head(wait_table + i); 71ac401cc7SJan Kara return 0; 72ac401cc7SJan Kara } 73ac401cc7SJan Kara fs_initcall(init_dax_wait_table); 74ac401cc7SJan Kara 75527b19d0SRoss Zwisler /* 763159f943SMatthew Wilcox * DAX pagecache entries use XArray value entries so they can't be mistaken 773159f943SMatthew Wilcox * for pages. We use one bit for locking, one bit for the entry size (PMD) 783159f943SMatthew Wilcox * and two more to tell us if the entry is a zero page or an empty entry that 793159f943SMatthew Wilcox * is just used for locking. In total four special bits. 80527b19d0SRoss Zwisler * 81527b19d0SRoss Zwisler * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE 82527b19d0SRoss Zwisler * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem 83527b19d0SRoss Zwisler * block allocation. 84527b19d0SRoss Zwisler */ 853159f943SMatthew Wilcox #define DAX_SHIFT (4) 863159f943SMatthew Wilcox #define DAX_LOCKED (1UL << 0) 873159f943SMatthew Wilcox #define DAX_PMD (1UL << 1) 883159f943SMatthew Wilcox #define DAX_ZERO_PAGE (1UL << 2) 893159f943SMatthew Wilcox #define DAX_EMPTY (1UL << 3) 90527b19d0SRoss Zwisler 91a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry) 92527b19d0SRoss Zwisler { 933159f943SMatthew Wilcox return xa_to_value(entry) >> DAX_SHIFT; 94527b19d0SRoss Zwisler } 95527b19d0SRoss Zwisler 969f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags) 979f32d221SMatthew Wilcox { 989f32d221SMatthew Wilcox return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT)); 999f32d221SMatthew Wilcox } 1009f32d221SMatthew Wilcox 101cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry) 102cfc93c6cSMatthew Wilcox { 103cfc93c6cSMatthew Wilcox return xa_to_value(entry) & DAX_LOCKED; 104cfc93c6cSMatthew Wilcox } 105cfc93c6cSMatthew Wilcox 106a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry) 107527b19d0SRoss Zwisler { 1083159f943SMatthew Wilcox if (xa_to_value(entry) & DAX_PMD) 109cfc93c6cSMatthew Wilcox return PMD_ORDER; 110527b19d0SRoss Zwisler return 0; 111527b19d0SRoss Zwisler } 112527b19d0SRoss Zwisler 113fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry) 114642261acSRoss Zwisler { 1153159f943SMatthew Wilcox return xa_to_value(entry) & DAX_PMD; 116642261acSRoss Zwisler } 117642261acSRoss Zwisler 118fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry) 119642261acSRoss Zwisler { 1203159f943SMatthew Wilcox return !(xa_to_value(entry) & DAX_PMD); 121642261acSRoss Zwisler } 122642261acSRoss Zwisler 123642261acSRoss Zwisler static int dax_is_zero_entry(void *entry) 124642261acSRoss Zwisler { 1253159f943SMatthew Wilcox return xa_to_value(entry) & DAX_ZERO_PAGE; 126642261acSRoss Zwisler } 127642261acSRoss Zwisler 128642261acSRoss Zwisler static int dax_is_empty_entry(void *entry) 129642261acSRoss Zwisler { 1303159f943SMatthew Wilcox return xa_to_value(entry) & DAX_EMPTY; 131642261acSRoss Zwisler } 132642261acSRoss Zwisler 133f7ca90b1SMatthew Wilcox /* 134a77d19f4SMatthew Wilcox * DAX page cache entry locking 135ac401cc7SJan Kara */ 136ac401cc7SJan Kara struct exceptional_entry_key { 137ec4907ffSMatthew Wilcox struct xarray *xa; 13863e95b5cSRoss Zwisler pgoff_t entry_start; 139ac401cc7SJan Kara }; 140ac401cc7SJan Kara 141ac401cc7SJan Kara struct wait_exceptional_entry_queue { 142ac6424b9SIngo Molnar wait_queue_entry_t wait; 143ac401cc7SJan Kara struct exceptional_entry_key key; 144ac401cc7SJan Kara }; 145ac401cc7SJan Kara 146b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas, 147b15cd800SMatthew Wilcox void *entry, struct exceptional_entry_key *key) 14863e95b5cSRoss Zwisler { 14963e95b5cSRoss Zwisler unsigned long hash; 150b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 15163e95b5cSRoss Zwisler 15263e95b5cSRoss Zwisler /* 15363e95b5cSRoss Zwisler * If 'entry' is a PMD, align the 'index' that we use for the wait 15463e95b5cSRoss Zwisler * queue to the start of that PMD. This ensures that all offsets in 15563e95b5cSRoss Zwisler * the range covered by the PMD map to the same bit lock. 15663e95b5cSRoss Zwisler */ 157642261acSRoss Zwisler if (dax_is_pmd_entry(entry)) 158917f3452SRoss Zwisler index &= ~PG_PMD_COLOUR; 159b15cd800SMatthew Wilcox key->xa = xas->xa; 16063e95b5cSRoss Zwisler key->entry_start = index; 16163e95b5cSRoss Zwisler 162b15cd800SMatthew Wilcox hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS); 16363e95b5cSRoss Zwisler return wait_table + hash; 16463e95b5cSRoss Zwisler } 16563e95b5cSRoss Zwisler 166ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait, 167ec4907ffSMatthew Wilcox unsigned int mode, int sync, void *keyp) 168ac401cc7SJan Kara { 169ac401cc7SJan Kara struct exceptional_entry_key *key = keyp; 170ac401cc7SJan Kara struct wait_exceptional_entry_queue *ewait = 171ac401cc7SJan Kara container_of(wait, struct wait_exceptional_entry_queue, wait); 172ac401cc7SJan Kara 173ec4907ffSMatthew Wilcox if (key->xa != ewait->key.xa || 17463e95b5cSRoss Zwisler key->entry_start != ewait->key.entry_start) 175ac401cc7SJan Kara return 0; 176ac401cc7SJan Kara return autoremove_wake_function(wait, mode, sync, NULL); 177ac401cc7SJan Kara } 178ac401cc7SJan Kara 179ac401cc7SJan Kara /* 180b93b0163SMatthew Wilcox * @entry may no longer be the entry at the index in the mapping. 181b93b0163SMatthew Wilcox * The important information it's conveying is whether the entry at 182b93b0163SMatthew Wilcox * this index used to be a PMD entry. 183e30331ffSRoss Zwisler */ 184b15cd800SMatthew Wilcox static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all) 185e30331ffSRoss Zwisler { 186e30331ffSRoss Zwisler struct exceptional_entry_key key; 187e30331ffSRoss Zwisler wait_queue_head_t *wq; 188e30331ffSRoss Zwisler 189b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &key); 190e30331ffSRoss Zwisler 191e30331ffSRoss Zwisler /* 192e30331ffSRoss Zwisler * Checking for locked entry and prepare_to_wait_exclusive() happens 193b93b0163SMatthew Wilcox * under the i_pages lock, ditto for entry handling in our callers. 194e30331ffSRoss Zwisler * So at this point all tasks that could have seen our entry locked 195e30331ffSRoss Zwisler * must be in the waitqueue and the following check will see them. 196e30331ffSRoss Zwisler */ 197e30331ffSRoss Zwisler if (waitqueue_active(wq)) 198e30331ffSRoss Zwisler __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); 199e30331ffSRoss Zwisler } 200e30331ffSRoss Zwisler 201cfc93c6cSMatthew Wilcox /* 202cfc93c6cSMatthew Wilcox * Look up entry in page cache, wait for it to become unlocked if it 203cfc93c6cSMatthew Wilcox * is a DAX entry and return it. The caller must subsequently call 204cfc93c6cSMatthew Wilcox * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry() 205cfc93c6cSMatthew Wilcox * if it did. 206cfc93c6cSMatthew Wilcox * 207cfc93c6cSMatthew Wilcox * Must be called with the i_pages lock held. 208cfc93c6cSMatthew Wilcox */ 209cfc93c6cSMatthew Wilcox static void *get_unlocked_entry(struct xa_state *xas) 210cfc93c6cSMatthew Wilcox { 211cfc93c6cSMatthew Wilcox void *entry; 212cfc93c6cSMatthew Wilcox struct wait_exceptional_entry_queue ewait; 213cfc93c6cSMatthew Wilcox wait_queue_head_t *wq; 214cfc93c6cSMatthew Wilcox 215cfc93c6cSMatthew Wilcox init_wait(&ewait.wait); 216cfc93c6cSMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 217cfc93c6cSMatthew Wilcox 218cfc93c6cSMatthew Wilcox for (;;) { 2190e40de03SMatthew Wilcox entry = xas_find_conflict(xas); 2200e40de03SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) || 221cfc93c6cSMatthew Wilcox !dax_is_locked(entry)) 222cfc93c6cSMatthew Wilcox return entry; 223cfc93c6cSMatthew Wilcox 224b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 225cfc93c6cSMatthew Wilcox prepare_to_wait_exclusive(wq, &ewait.wait, 226cfc93c6cSMatthew Wilcox TASK_UNINTERRUPTIBLE); 227cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 228cfc93c6cSMatthew Wilcox xas_reset(xas); 229cfc93c6cSMatthew Wilcox schedule(); 230cfc93c6cSMatthew Wilcox finish_wait(wq, &ewait.wait); 231cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 232cfc93c6cSMatthew Wilcox } 233cfc93c6cSMatthew Wilcox } 234cfc93c6cSMatthew Wilcox 23555e56f06SMatthew Wilcox /* 23655e56f06SMatthew Wilcox * The only thing keeping the address space around is the i_pages lock 23755e56f06SMatthew Wilcox * (it's cycled in clear_inode() after removing the entries from i_pages) 23855e56f06SMatthew Wilcox * After we call xas_unlock_irq(), we cannot touch xas->xa. 23955e56f06SMatthew Wilcox */ 24055e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry) 24155e56f06SMatthew Wilcox { 24255e56f06SMatthew Wilcox struct wait_exceptional_entry_queue ewait; 24355e56f06SMatthew Wilcox wait_queue_head_t *wq; 24455e56f06SMatthew Wilcox 24555e56f06SMatthew Wilcox init_wait(&ewait.wait); 24655e56f06SMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 24755e56f06SMatthew Wilcox 24855e56f06SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 249d8a70641SDan Williams /* 250d8a70641SDan Williams * Unlike get_unlocked_entry() there is no guarantee that this 251d8a70641SDan Williams * path ever successfully retrieves an unlocked entry before an 252d8a70641SDan Williams * inode dies. Perform a non-exclusive wait in case this path 253d8a70641SDan Williams * never successfully performs its own wake up. 254d8a70641SDan Williams */ 255d8a70641SDan Williams prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); 25655e56f06SMatthew Wilcox xas_unlock_irq(xas); 25755e56f06SMatthew Wilcox schedule(); 25855e56f06SMatthew Wilcox finish_wait(wq, &ewait.wait); 25955e56f06SMatthew Wilcox } 26055e56f06SMatthew Wilcox 261cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry) 262cfc93c6cSMatthew Wilcox { 263cfc93c6cSMatthew Wilcox /* If we were the only waiter woken, wake the next one */ 264cfc93c6cSMatthew Wilcox if (entry) 265cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 266cfc93c6cSMatthew Wilcox } 267cfc93c6cSMatthew Wilcox 268cfc93c6cSMatthew Wilcox /* 269cfc93c6cSMatthew Wilcox * We used the xa_state to get the entry, but then we locked the entry and 270cfc93c6cSMatthew Wilcox * dropped the xa_lock, so we know the xa_state is stale and must be reset 271cfc93c6cSMatthew Wilcox * before use. 272cfc93c6cSMatthew Wilcox */ 273cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry) 274cfc93c6cSMatthew Wilcox { 275cfc93c6cSMatthew Wilcox void *old; 276cfc93c6cSMatthew Wilcox 2777ae2ea7dSMatthew Wilcox BUG_ON(dax_is_locked(entry)); 278cfc93c6cSMatthew Wilcox xas_reset(xas); 279cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 280cfc93c6cSMatthew Wilcox old = xas_store(xas, entry); 281cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 282cfc93c6cSMatthew Wilcox BUG_ON(!dax_is_locked(old)); 283cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 284cfc93c6cSMatthew Wilcox } 285cfc93c6cSMatthew Wilcox 286cfc93c6cSMatthew Wilcox /* 287cfc93c6cSMatthew Wilcox * Return: The entry stored at this location before it was locked. 288cfc93c6cSMatthew Wilcox */ 289cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry) 290cfc93c6cSMatthew Wilcox { 291cfc93c6cSMatthew Wilcox unsigned long v = xa_to_value(entry); 292cfc93c6cSMatthew Wilcox return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 293cfc93c6cSMatthew Wilcox } 294cfc93c6cSMatthew Wilcox 295d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry) 296d2c997c0SDan Williams { 297d2c997c0SDan Williams if (dax_is_zero_entry(entry)) 298d2c997c0SDan Williams return 0; 299d2c997c0SDan Williams else if (dax_is_empty_entry(entry)) 300d2c997c0SDan Williams return 0; 301d2c997c0SDan Williams else if (dax_is_pmd_entry(entry)) 302d2c997c0SDan Williams return PMD_SIZE; 303d2c997c0SDan Williams else 304d2c997c0SDan Williams return PAGE_SIZE; 305d2c997c0SDan Williams } 306d2c997c0SDan Williams 307a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry) 308d2c997c0SDan Williams { 309a77d19f4SMatthew Wilcox return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE; 310d2c997c0SDan Williams } 311d2c997c0SDan Williams 312d2c997c0SDan Williams /* 313d2c997c0SDan Williams * Iterate through all mapped pfns represented by an entry, i.e. skip 314d2c997c0SDan Williams * 'empty' and 'zero' entries. 315d2c997c0SDan Williams */ 316d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \ 317a77d19f4SMatthew Wilcox for (pfn = dax_to_pfn(entry); \ 318a77d19f4SMatthew Wilcox pfn < dax_end_pfn(entry); pfn++) 319d2c997c0SDan Williams 32073449dafSDan Williams /* 32173449dafSDan Williams * TODO: for reflink+dax we need a way to associate a single page with 32273449dafSDan Williams * multiple address_space instances at different linear_page_index() 32373449dafSDan Williams * offsets. 32473449dafSDan Williams */ 32573449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping, 32673449dafSDan Williams struct vm_area_struct *vma, unsigned long address) 327d2c997c0SDan Williams { 32873449dafSDan Williams unsigned long size = dax_entry_size(entry), pfn, index; 32973449dafSDan Williams int i = 0; 330d2c997c0SDan Williams 331d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 332d2c997c0SDan Williams return; 333d2c997c0SDan Williams 33473449dafSDan Williams index = linear_page_index(vma, address & ~(size - 1)); 335d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 336d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 337d2c997c0SDan Williams 338d2c997c0SDan Williams WARN_ON_ONCE(page->mapping); 339d2c997c0SDan Williams page->mapping = mapping; 34073449dafSDan Williams page->index = index + i++; 341d2c997c0SDan Williams } 342d2c997c0SDan Williams } 343d2c997c0SDan Williams 344d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping, 345d2c997c0SDan Williams bool trunc) 346d2c997c0SDan Williams { 347d2c997c0SDan Williams unsigned long pfn; 348d2c997c0SDan Williams 349d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 350d2c997c0SDan Williams return; 351d2c997c0SDan Williams 352d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 353d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 354d2c997c0SDan Williams 355d2c997c0SDan Williams WARN_ON_ONCE(trunc && page_ref_count(page) > 1); 356d2c997c0SDan Williams WARN_ON_ONCE(page->mapping && page->mapping != mapping); 357d2c997c0SDan Williams page->mapping = NULL; 35873449dafSDan Williams page->index = 0; 359d2c997c0SDan Williams } 360d2c997c0SDan Williams } 361d2c997c0SDan Williams 3625fac7408SDan Williams static struct page *dax_busy_page(void *entry) 3635fac7408SDan Williams { 3645fac7408SDan Williams unsigned long pfn; 3655fac7408SDan Williams 3665fac7408SDan Williams for_each_mapped_pfn(entry, pfn) { 3675fac7408SDan Williams struct page *page = pfn_to_page(pfn); 3685fac7408SDan Williams 3695fac7408SDan Williams if (page_ref_count(page) > 1) 3705fac7408SDan Williams return page; 3715fac7408SDan Williams } 3725fac7408SDan Williams return NULL; 3735fac7408SDan Williams } 3745fac7408SDan Williams 375c5bbd451SMatthew Wilcox /* 376c5bbd451SMatthew Wilcox * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page 377c5bbd451SMatthew Wilcox * @page: The page whose entry we want to lock 378c5bbd451SMatthew Wilcox * 379c5bbd451SMatthew Wilcox * Context: Process context. 38027359fd6SMatthew Wilcox * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could 38127359fd6SMatthew Wilcox * not be locked. 382c5bbd451SMatthew Wilcox */ 38327359fd6SMatthew Wilcox dax_entry_t dax_lock_page(struct page *page) 384c2a7d2a1SDan Williams { 3859f32d221SMatthew Wilcox XA_STATE(xas, NULL, 0); 3869f32d221SMatthew Wilcox void *entry; 387c2a7d2a1SDan Williams 388c5bbd451SMatthew Wilcox /* Ensure page->mapping isn't freed while we look at it */ 389c5bbd451SMatthew Wilcox rcu_read_lock(); 390c2a7d2a1SDan Williams for (;;) { 3919f32d221SMatthew Wilcox struct address_space *mapping = READ_ONCE(page->mapping); 392c2a7d2a1SDan Williams 39327359fd6SMatthew Wilcox entry = NULL; 394c93db7bbSMatthew Wilcox if (!mapping || !dax_mapping(mapping)) 395c5bbd451SMatthew Wilcox break; 396c2a7d2a1SDan Williams 397c2a7d2a1SDan Williams /* 398c2a7d2a1SDan Williams * In the device-dax case there's no need to lock, a 399c2a7d2a1SDan Williams * struct dev_pagemap pin is sufficient to keep the 400c2a7d2a1SDan Williams * inode alive, and we assume we have dev_pagemap pin 401c2a7d2a1SDan Williams * otherwise we would not have a valid pfn_to_page() 402c2a7d2a1SDan Williams * translation. 403c2a7d2a1SDan Williams */ 40427359fd6SMatthew Wilcox entry = (void *)~0UL; 4059f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 406c5bbd451SMatthew Wilcox break; 407c2a7d2a1SDan Williams 4089f32d221SMatthew Wilcox xas.xa = &mapping->i_pages; 4099f32d221SMatthew Wilcox xas_lock_irq(&xas); 410c2a7d2a1SDan Williams if (mapping != page->mapping) { 4119f32d221SMatthew Wilcox xas_unlock_irq(&xas); 412c2a7d2a1SDan Williams continue; 413c2a7d2a1SDan Williams } 4149f32d221SMatthew Wilcox xas_set(&xas, page->index); 4159f32d221SMatthew Wilcox entry = xas_load(&xas); 4169f32d221SMatthew Wilcox if (dax_is_locked(entry)) { 417c5bbd451SMatthew Wilcox rcu_read_unlock(); 41855e56f06SMatthew Wilcox wait_entry_unlocked(&xas, entry); 419c5bbd451SMatthew Wilcox rcu_read_lock(); 420c2a7d2a1SDan Williams continue; 421c2a7d2a1SDan Williams } 4229f32d221SMatthew Wilcox dax_lock_entry(&xas, entry); 4239f32d221SMatthew Wilcox xas_unlock_irq(&xas); 424c5bbd451SMatthew Wilcox break; 4259f32d221SMatthew Wilcox } 426c5bbd451SMatthew Wilcox rcu_read_unlock(); 42727359fd6SMatthew Wilcox return (dax_entry_t)entry; 428c2a7d2a1SDan Williams } 429c2a7d2a1SDan Williams 43027359fd6SMatthew Wilcox void dax_unlock_page(struct page *page, dax_entry_t cookie) 431c2a7d2a1SDan Williams { 432c2a7d2a1SDan Williams struct address_space *mapping = page->mapping; 4339f32d221SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, page->index); 434c2a7d2a1SDan Williams 4359f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 436c2a7d2a1SDan Williams return; 437c2a7d2a1SDan Williams 43827359fd6SMatthew Wilcox dax_unlock_entry(&xas, (void *)cookie); 439c2a7d2a1SDan Williams } 440c2a7d2a1SDan Williams 441ac401cc7SJan Kara /* 442a77d19f4SMatthew Wilcox * Find page cache entry at given index. If it is a DAX entry, return it 443a77d19f4SMatthew Wilcox * with the entry locked. If the page cache doesn't contain an entry at 444a77d19f4SMatthew Wilcox * that index, add a locked empty entry. 445ac401cc7SJan Kara * 4463159f943SMatthew Wilcox * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 447b15cd800SMatthew Wilcox * either return that locked entry or will return VM_FAULT_FALLBACK. 448b15cd800SMatthew Wilcox * This will happen if there are any PTE entries within the PMD range 449b15cd800SMatthew Wilcox * that we are requesting. 450642261acSRoss Zwisler * 451b15cd800SMatthew Wilcox * We always favor PTE entries over PMD entries. There isn't a flow where we 452b15cd800SMatthew Wilcox * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 453b15cd800SMatthew Wilcox * insertion will fail if it finds any PTE entries already in the tree, and a 454b15cd800SMatthew Wilcox * PTE insertion will cause an existing PMD entry to be unmapped and 455b15cd800SMatthew Wilcox * downgraded to PTE entries. This happens for both PMD zero pages as 456b15cd800SMatthew Wilcox * well as PMD empty entries. 457642261acSRoss Zwisler * 458b15cd800SMatthew Wilcox * The exception to this downgrade path is for PMD entries that have 459b15cd800SMatthew Wilcox * real storage backing them. We will leave these real PMD entries in 460b15cd800SMatthew Wilcox * the tree, and PTE writes will simply dirty the entire PMD entry. 461642261acSRoss Zwisler * 462ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 463ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 464ac401cc7SJan Kara * show it helps. 465b15cd800SMatthew Wilcox * 466b15cd800SMatthew Wilcox * On error, this function does not return an ERR_PTR. Instead it returns 467b15cd800SMatthew Wilcox * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 468b15cd800SMatthew Wilcox * overlap with xarray value entries. 469ac401cc7SJan Kara */ 470b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas, 471b15cd800SMatthew Wilcox struct address_space *mapping, unsigned long size_flag) 472ac401cc7SJan Kara { 473b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 474b15cd800SMatthew Wilcox bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */ 475b15cd800SMatthew Wilcox void *entry; 476ac401cc7SJan Kara 477b15cd800SMatthew Wilcox retry: 478b15cd800SMatthew Wilcox xas_lock_irq(xas); 479b15cd800SMatthew Wilcox entry = get_unlocked_entry(xas); 480642261acSRoss Zwisler 481b15cd800SMatthew Wilcox if (entry) { 4820e40de03SMatthew Wilcox if (!xa_is_value(entry)) { 483b15cd800SMatthew Wilcox xas_set_err(xas, EIO); 48491d25ba8SRoss Zwisler goto out_unlock; 48591d25ba8SRoss Zwisler } 48691d25ba8SRoss Zwisler 4873159f943SMatthew Wilcox if (size_flag & DAX_PMD) { 48891d25ba8SRoss Zwisler if (dax_is_pte_entry(entry)) { 489b15cd800SMatthew Wilcox put_unlocked_entry(xas, entry); 490b15cd800SMatthew Wilcox goto fallback; 491642261acSRoss Zwisler } 492642261acSRoss Zwisler } else { /* trying to grab a PTE entry */ 49391d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 494642261acSRoss Zwisler (dax_is_zero_entry(entry) || 495642261acSRoss Zwisler dax_is_empty_entry(entry))) { 496642261acSRoss Zwisler pmd_downgrade = true; 497642261acSRoss Zwisler } 498642261acSRoss Zwisler } 499642261acSRoss Zwisler } 500642261acSRoss Zwisler 501642261acSRoss Zwisler if (pmd_downgrade) { 502642261acSRoss Zwisler /* 503642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 504b93b0163SMatthew Wilcox * the i_pages lock. 505642261acSRoss Zwisler */ 506b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 507642261acSRoss Zwisler 508642261acSRoss Zwisler /* 509642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 510642261acSRoss Zwisler * downgraded are empty entries which don't need to be 511642261acSRoss Zwisler * unmapped. 512642261acSRoss Zwisler */ 513b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 514b15cd800SMatthew Wilcox xas_unlock_irq(xas); 515b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 516b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 517977fbdcdSMatthew Wilcox PG_PMD_NR, false); 518b15cd800SMatthew Wilcox xas_reset(xas); 519b15cd800SMatthew Wilcox xas_lock_irq(xas); 520e11f8b7bSRoss Zwisler } 521e11f8b7bSRoss Zwisler 522d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 523b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 524b15cd800SMatthew Wilcox dax_wake_entry(xas, entry, true); 525642261acSRoss Zwisler mapping->nrexceptional--; 526b15cd800SMatthew Wilcox entry = NULL; 527b15cd800SMatthew Wilcox xas_set(xas, index); 528642261acSRoss Zwisler } 529642261acSRoss Zwisler 530b15cd800SMatthew Wilcox if (entry) { 531b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 532b15cd800SMatthew Wilcox } else { 533b15cd800SMatthew Wilcox entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY); 534b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 535b15cd800SMatthew Wilcox if (xas_error(xas)) 536b15cd800SMatthew Wilcox goto out_unlock; 537ac401cc7SJan Kara mapping->nrexceptional++; 538ac401cc7SJan Kara } 539b15cd800SMatthew Wilcox 540642261acSRoss Zwisler out_unlock: 541b15cd800SMatthew Wilcox xas_unlock_irq(xas); 542b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 543b15cd800SMatthew Wilcox goto retry; 544b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 545b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 546b15cd800SMatthew Wilcox if (xas_error(xas)) 547b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 548e3ad61c6SRoss Zwisler return entry; 549b15cd800SMatthew Wilcox fallback: 550b15cd800SMatthew Wilcox xas_unlock_irq(xas); 551b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 552ac401cc7SJan Kara } 553ac401cc7SJan Kara 5545fac7408SDan Williams /** 5555fac7408SDan Williams * dax_layout_busy_page - find first pinned page in @mapping 5565fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5575fac7408SDan Williams * 5585fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5595fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5605fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5615fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5625fac7408SDan Williams * get_user_pages() usages. 5635fac7408SDan Williams * 5645fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5655fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5665fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5675fac7408SDan Williams * mapping_mapped() becoming true. 5685fac7408SDan Williams */ 5695fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping) 5705fac7408SDan Williams { 571084a8990SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 572084a8990SMatthew Wilcox void *entry; 573084a8990SMatthew Wilcox unsigned int scanned = 0; 5745fac7408SDan Williams struct page *page = NULL; 5755fac7408SDan Williams 5765fac7408SDan Williams /* 5775fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 5785fac7408SDan Williams */ 5795fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 5805fac7408SDan Williams return NULL; 5815fac7408SDan Williams 5825fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 5835fac7408SDan Williams return NULL; 5845fac7408SDan Williams 5855fac7408SDan Williams /* 5865fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 587084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 5885fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 5895fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 5905fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 5915fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 5925fac7408SDan Williams * holding those locks, and unmap_mapping_range() will not zero the 5935fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 5945fac7408SDan Williams * guaranteed to either see new references or prevent new 5955fac7408SDan Williams * references from being established. 5965fac7408SDan Williams */ 5975fac7408SDan Williams unmap_mapping_range(mapping, 0, 0, 1); 5985fac7408SDan Williams 599084a8990SMatthew Wilcox xas_lock_irq(&xas); 600084a8990SMatthew Wilcox xas_for_each(&xas, entry, ULONG_MAX) { 601084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 6025fac7408SDan Williams continue; 603084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 604084a8990SMatthew Wilcox entry = get_unlocked_entry(&xas); 6055fac7408SDan Williams if (entry) 6065fac7408SDan Williams page = dax_busy_page(entry); 607084a8990SMatthew Wilcox put_unlocked_entry(&xas, entry); 6085fac7408SDan Williams if (page) 6095fac7408SDan Williams break; 610084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 611084a8990SMatthew Wilcox continue; 612cdbf8897SRoss Zwisler 613084a8990SMatthew Wilcox xas_pause(&xas); 614084a8990SMatthew Wilcox xas_unlock_irq(&xas); 615084a8990SMatthew Wilcox cond_resched(); 616084a8990SMatthew Wilcox xas_lock_irq(&xas); 6175fac7408SDan Williams } 618084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6195fac7408SDan Williams return page; 6205fac7408SDan Williams } 6215fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6225fac7408SDan Williams 623a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 624c6dcf52cSJan Kara pgoff_t index, bool trunc) 625c6dcf52cSJan Kara { 62607f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 627c6dcf52cSJan Kara int ret = 0; 628c6dcf52cSJan Kara void *entry; 629c6dcf52cSJan Kara 63007f2d89cSMatthew Wilcox xas_lock_irq(&xas); 63107f2d89cSMatthew Wilcox entry = get_unlocked_entry(&xas); 6323159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 633c6dcf52cSJan Kara goto out; 634c6dcf52cSJan Kara if (!trunc && 63507f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 63607f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 637c6dcf52cSJan Kara goto out; 638d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 63907f2d89cSMatthew Wilcox xas_store(&xas, NULL); 640c6dcf52cSJan Kara mapping->nrexceptional--; 641c6dcf52cSJan Kara ret = 1; 642c6dcf52cSJan Kara out: 64307f2d89cSMatthew Wilcox put_unlocked_entry(&xas, entry); 64407f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 645c6dcf52cSJan Kara return ret; 646c6dcf52cSJan Kara } 64707f2d89cSMatthew Wilcox 648ac401cc7SJan Kara /* 6493159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6503159f943SMatthew Wilcox * to be unlocked before deleting it. 651ac401cc7SJan Kara */ 652ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 653ac401cc7SJan Kara { 654a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 655ac401cc7SJan Kara 656ac401cc7SJan Kara /* 657ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 658ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 659a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6603159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 661ac401cc7SJan Kara * at that index as well... 662ac401cc7SJan Kara */ 663c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 664c6dcf52cSJan Kara return ret; 665ac401cc7SJan Kara } 666ac401cc7SJan Kara 667c6dcf52cSJan Kara /* 6683159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 669c6dcf52cSJan Kara */ 670c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 671c6dcf52cSJan Kara pgoff_t index) 672c6dcf52cSJan Kara { 673a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 674ac401cc7SJan Kara } 675ac401cc7SJan Kara 676cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, 677cccbce67SDan Williams sector_t sector, size_t size, struct page *to, 678cccbce67SDan Williams unsigned long vaddr) 679f7ca90b1SMatthew Wilcox { 680cccbce67SDan Williams void *vto, *kaddr; 681cccbce67SDan Williams pgoff_t pgoff; 682cccbce67SDan Williams long rc; 683cccbce67SDan Williams int id; 684e2e05394SRoss Zwisler 685cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 686cccbce67SDan Williams if (rc) 687cccbce67SDan Williams return rc; 688cccbce67SDan Williams 689cccbce67SDan Williams id = dax_read_lock(); 69086ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL); 691cccbce67SDan Williams if (rc < 0) { 692cccbce67SDan Williams dax_read_unlock(id); 693cccbce67SDan Williams return rc; 694cccbce67SDan Williams } 695f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 696cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 697f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 698cccbce67SDan Williams dax_read_unlock(id); 699f7ca90b1SMatthew Wilcox return 0; 700f7ca90b1SMatthew Wilcox } 701f7ca90b1SMatthew Wilcox 702642261acSRoss Zwisler /* 703642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 704642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 705642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 706642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 707642261acSRoss Zwisler * appropriate. 708642261acSRoss Zwisler */ 709b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 710b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 711b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 7129973c98eSRoss Zwisler { 713b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 7149973c98eSRoss Zwisler 715f5b7b748SJan Kara if (dirty) 7169973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7179973c98eSRoss Zwisler 7183159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 719b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 72091d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 72191d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 722977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 723977fbdcdSMatthew Wilcox PG_PMD_NR, false); 72491d25ba8SRoss Zwisler else /* pte entry */ 725b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 726ac401cc7SJan Kara } 7279973c98eSRoss Zwisler 728b15cd800SMatthew Wilcox xas_reset(xas); 729b15cd800SMatthew Wilcox xas_lock_irq(xas); 730d2c997c0SDan Williams if (dax_entry_size(entry) != dax_entry_size(new_entry)) { 731d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 73273449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 733d2c997c0SDan Williams } 734642261acSRoss Zwisler 73591d25ba8SRoss Zwisler if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 736642261acSRoss Zwisler /* 737a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 738642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 739a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 740642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 741642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 742642261acSRoss Zwisler * tree and dirty it if necessary. 743642261acSRoss Zwisler */ 744b15cd800SMatthew Wilcox void *old = dax_lock_entry(xas, new_entry); 745b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 746b15cd800SMatthew Wilcox DAX_LOCKED)); 74791d25ba8SRoss Zwisler entry = new_entry; 748b15cd800SMatthew Wilcox } else { 749b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 750ac401cc7SJan Kara } 75191d25ba8SRoss Zwisler 752f5b7b748SJan Kara if (dirty) 753b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 75491d25ba8SRoss Zwisler 755b15cd800SMatthew Wilcox xas_unlock_irq(xas); 75691d25ba8SRoss Zwisler return entry; 7579973c98eSRoss Zwisler } 7589973c98eSRoss Zwisler 759a77d19f4SMatthew Wilcox static inline 760a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7614b4bb46dSJan Kara { 7624b4bb46dSJan Kara unsigned long address; 7634b4bb46dSJan Kara 7644b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7654b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 7664b4bb46dSJan Kara return address; 7674b4bb46dSJan Kara } 7684b4bb46dSJan Kara 7694b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 770a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 771a77d19f4SMatthew Wilcox unsigned long pfn) 7724b4bb46dSJan Kara { 7734b4bb46dSJan Kara struct vm_area_struct *vma; 774f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 775f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 7764b4bb46dSJan Kara spinlock_t *ptl; 7774b4bb46dSJan Kara 7784b4bb46dSJan Kara i_mmap_lock_read(mapping); 7794b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 780ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 781ac46d4f3SJérôme Glisse unsigned long address; 7824b4bb46dSJan Kara 7834b4bb46dSJan Kara cond_resched(); 7844b4bb46dSJan Kara 7854b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 7864b4bb46dSJan Kara continue; 7874b4bb46dSJan Kara 7884b4bb46dSJan Kara address = pgoff_address(index, vma); 789a4d1a885SJérôme Glisse 790a4d1a885SJérôme Glisse /* 791a4d1a885SJérôme Glisse * Note because we provide start/end to follow_pte_pmd it will 792a4d1a885SJérôme Glisse * call mmu_notifier_invalidate_range_start() on our behalf 793a4d1a885SJérôme Glisse * before taking any lock. 794a4d1a885SJérôme Glisse */ 795ac46d4f3SJérôme Glisse if (follow_pte_pmd(vma->vm_mm, address, &range, 796ac46d4f3SJérôme Glisse &ptep, &pmdp, &ptl)) 7974b4bb46dSJan Kara continue; 798f729c8c9SRoss Zwisler 7990f10851eSJérôme Glisse /* 8000f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 8010f10851eSJérôme Glisse * downgrading page table protection not changing it to point 8020f10851eSJérôme Glisse * to a new page. 8030f10851eSJérôme Glisse * 804ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 8050f10851eSJérôme Glisse */ 806f729c8c9SRoss Zwisler if (pmdp) { 807f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 808f729c8c9SRoss Zwisler pmd_t pmd; 809f729c8c9SRoss Zwisler 810f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 811f729c8c9SRoss Zwisler goto unlock_pmd; 812f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 813f729c8c9SRoss Zwisler goto unlock_pmd; 814f729c8c9SRoss Zwisler 815f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 816f729c8c9SRoss Zwisler pmd = pmdp_huge_clear_flush(vma, address, pmdp); 817f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 818f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 819f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 820f729c8c9SRoss Zwisler unlock_pmd: 821f729c8c9SRoss Zwisler #endif 822ee190ca6SJan H. Schönherr spin_unlock(ptl); 823f729c8c9SRoss Zwisler } else { 8244b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 825f729c8c9SRoss Zwisler goto unlock_pte; 8264b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 827f729c8c9SRoss Zwisler goto unlock_pte; 8284b4bb46dSJan Kara 8294b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8304b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8314b4bb46dSJan Kara pte = pte_wrprotect(pte); 8324b4bb46dSJan Kara pte = pte_mkclean(pte); 8334b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 834f729c8c9SRoss Zwisler unlock_pte: 8354b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 836f729c8c9SRoss Zwisler } 8374b4bb46dSJan Kara 838ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 8394b4bb46dSJan Kara } 8404b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8414b4bb46dSJan Kara } 8424b4bb46dSJan Kara 8439fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8449fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8459973c98eSRoss Zwisler { 8463fe0791cSDan Williams unsigned long pfn; 8473fe0791cSDan Williams long ret = 0; 848cccbce67SDan Williams size_t size; 8499973c98eSRoss Zwisler 8509973c98eSRoss Zwisler /* 851a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 852a6abc2c0SJan Kara * wrong. 8539973c98eSRoss Zwisler */ 8543159f943SMatthew Wilcox if (WARN_ON(!xa_is_value(entry))) 855a6abc2c0SJan Kara return -EIO; 8569973c98eSRoss Zwisler 8579fc747f6SMatthew Wilcox if (unlikely(dax_is_locked(entry))) { 8589fc747f6SMatthew Wilcox void *old_entry = entry; 8599fc747f6SMatthew Wilcox 8609fc747f6SMatthew Wilcox entry = get_unlocked_entry(xas); 8619fc747f6SMatthew Wilcox 862a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 8639fc747f6SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 864a6abc2c0SJan Kara goto put_unlocked; 865a6abc2c0SJan Kara /* 8669fc747f6SMatthew Wilcox * Entry got reallocated elsewhere? No need to writeback. 8679fc747f6SMatthew Wilcox * We have to compare pfns as we must not bail out due to 8689fc747f6SMatthew Wilcox * difference in lockbit or entry type. 869a6abc2c0SJan Kara */ 8709fc747f6SMatthew Wilcox if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 871a6abc2c0SJan Kara goto put_unlocked; 872642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 873642261acSRoss Zwisler dax_is_zero_entry(entry))) { 8749973c98eSRoss Zwisler ret = -EIO; 875a6abc2c0SJan Kara goto put_unlocked; 8769973c98eSRoss Zwisler } 8779973c98eSRoss Zwisler 8789fc747f6SMatthew Wilcox /* Another fsync thread may have already done this entry */ 8799fc747f6SMatthew Wilcox if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 880a6abc2c0SJan Kara goto put_unlocked; 8819fc747f6SMatthew Wilcox } 8829fc747f6SMatthew Wilcox 883a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 8849fc747f6SMatthew Wilcox dax_lock_entry(xas, entry); 8859fc747f6SMatthew Wilcox 886a6abc2c0SJan Kara /* 887a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 888a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 889a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 890b93b0163SMatthew Wilcox * at the entry only under the i_pages lock and once they do that 891b93b0163SMatthew Wilcox * they will see the entry locked and wait for it to unlock. 892a6abc2c0SJan Kara */ 8939fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 8949fc747f6SMatthew Wilcox xas_unlock_irq(xas); 895a6abc2c0SJan Kara 896642261acSRoss Zwisler /* 897642261acSRoss Zwisler * Even if dax_writeback_mapping_range() was given a wbc->range_start 898642261acSRoss Zwisler * in the middle of a PMD, the 'index' we are given will be aligned to 8993fe0791cSDan Williams * the start index of the PMD, as will the pfn we pull from 'entry'. 9003fe0791cSDan Williams * This allows us to flush for PMD_SIZE and not have to worry about 9013fe0791cSDan Williams * partial PMD writebacks. 902642261acSRoss Zwisler */ 903a77d19f4SMatthew Wilcox pfn = dax_to_pfn(entry); 904a77d19f4SMatthew Wilcox size = PAGE_SIZE << dax_entry_order(entry); 905cccbce67SDan Williams 9069fc747f6SMatthew Wilcox dax_entry_mkclean(mapping, xas->xa_index, pfn); 9073fe0791cSDan Williams dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size); 9084b4bb46dSJan Kara /* 9094b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 9104b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 9114b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 9124b4bb46dSJan Kara * entry lock. 9134b4bb46dSJan Kara */ 9149fc747f6SMatthew Wilcox xas_reset(xas); 9159fc747f6SMatthew Wilcox xas_lock_irq(xas); 9169fc747f6SMatthew Wilcox xas_store(xas, entry); 9179fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 9189fc747f6SMatthew Wilcox dax_wake_entry(xas, entry, false); 9199fc747f6SMatthew Wilcox 9209fc747f6SMatthew Wilcox trace_dax_writeback_one(mapping->host, xas->xa_index, 9219fc747f6SMatthew Wilcox size >> PAGE_SHIFT); 9229973c98eSRoss Zwisler return ret; 9239973c98eSRoss Zwisler 924a6abc2c0SJan Kara put_unlocked: 9259fc747f6SMatthew Wilcox put_unlocked_entry(xas, entry); 9269973c98eSRoss Zwisler return ret; 9279973c98eSRoss Zwisler } 9289973c98eSRoss Zwisler 9299973c98eSRoss Zwisler /* 9309973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 9319973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 9329973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 9339973c98eSRoss Zwisler */ 9347f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 9357f6d5b52SRoss Zwisler struct block_device *bdev, struct writeback_control *wbc) 9369973c98eSRoss Zwisler { 9379fc747f6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 9389973c98eSRoss Zwisler struct inode *inode = mapping->host; 9399fc747f6SMatthew Wilcox pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 940cccbce67SDan Williams struct dax_device *dax_dev; 9419fc747f6SMatthew Wilcox void *entry; 9429fc747f6SMatthew Wilcox int ret = 0; 9439fc747f6SMatthew Wilcox unsigned int scanned = 0; 9449973c98eSRoss Zwisler 9459973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 9469973c98eSRoss Zwisler return -EIO; 9479973c98eSRoss Zwisler 9487f6d5b52SRoss Zwisler if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) 9497f6d5b52SRoss Zwisler return 0; 9507f6d5b52SRoss Zwisler 951cccbce67SDan Williams dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 952cccbce67SDan Williams if (!dax_dev) 953cccbce67SDan Williams return -EIO; 954cccbce67SDan Williams 9559fc747f6SMatthew Wilcox trace_dax_writeback_range(inode, xas.xa_index, end_index); 9569973c98eSRoss Zwisler 9579fc747f6SMatthew Wilcox tag_pages_for_writeback(mapping, xas.xa_index, end_index); 958d14a3f48SRoss Zwisler 9599fc747f6SMatthew Wilcox xas_lock_irq(&xas); 9609fc747f6SMatthew Wilcox xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 9619fc747f6SMatthew Wilcox ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 962819ec6b9SJeff Layton if (ret < 0) { 963819ec6b9SJeff Layton mapping_set_error(mapping, ret); 9649fc747f6SMatthew Wilcox break; 965d14a3f48SRoss Zwisler } 9669fc747f6SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 9679fc747f6SMatthew Wilcox continue; 9689fc747f6SMatthew Wilcox 9699fc747f6SMatthew Wilcox xas_pause(&xas); 9709fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 9719fc747f6SMatthew Wilcox cond_resched(); 9729fc747f6SMatthew Wilcox xas_lock_irq(&xas); 973d14a3f48SRoss Zwisler } 9749fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 975cccbce67SDan Williams put_dax(dax_dev); 9769fc747f6SMatthew Wilcox trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 9779fc747f6SMatthew Wilcox return ret; 9789973c98eSRoss Zwisler } 9799973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 9809973c98eSRoss Zwisler 98131a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 982f7ca90b1SMatthew Wilcox { 983a3841f94SLinus Torvalds return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; 98431a6f1a6SJan Kara } 985f7ca90b1SMatthew Wilcox 9865e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, 9875e161e40SJan Kara pfn_t *pfnp) 9885e161e40SJan Kara { 9895e161e40SJan Kara const sector_t sector = dax_iomap_sector(iomap, pos); 9905e161e40SJan Kara pgoff_t pgoff; 9915e161e40SJan Kara int id, rc; 9925e161e40SJan Kara long length; 9935e161e40SJan Kara 9945e161e40SJan Kara rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); 995cccbce67SDan Williams if (rc) 996cccbce67SDan Williams return rc; 997cccbce67SDan Williams id = dax_read_lock(); 9985e161e40SJan Kara length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 99986ed913bSHuaisheng Ye NULL, pfnp); 10005e161e40SJan Kara if (length < 0) { 10015e161e40SJan Kara rc = length; 10025e161e40SJan Kara goto out; 10035e161e40SJan Kara } 10045e161e40SJan Kara rc = -EINVAL; 10055e161e40SJan Kara if (PFN_PHYS(length) < size) 10065e161e40SJan Kara goto out; 10075e161e40SJan Kara if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 10085e161e40SJan Kara goto out; 10095e161e40SJan Kara /* For larger pages we need devmap */ 10105e161e40SJan Kara if (length > 1 && !pfn_t_devmap(*pfnp)) 10115e161e40SJan Kara goto out; 10125e161e40SJan Kara rc = 0; 10135e161e40SJan Kara out: 1014cccbce67SDan Williams dax_read_unlock(id); 1015cccbce67SDan Williams return rc; 1016cccbce67SDan Williams } 1017f7ca90b1SMatthew Wilcox 10182f89dc12SJan Kara /* 101991d25ba8SRoss Zwisler * The user has performed a load from a hole in the file. Allocating a new 102091d25ba8SRoss Zwisler * page in the file would cause excessive storage usage for workloads with 102191d25ba8SRoss Zwisler * sparse files. Instead we insert a read-only mapping of the 4k zero page. 102291d25ba8SRoss Zwisler * If this page is ever written to we will re-fault and change the mapping to 102391d25ba8SRoss Zwisler * point to real DAX storage instead. 10242f89dc12SJan Kara */ 1025b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas, 1026b15cd800SMatthew Wilcox struct address_space *mapping, void **entry, 1027e30331ffSRoss Zwisler struct vm_fault *vmf) 1028e30331ffSRoss Zwisler { 1029e30331ffSRoss Zwisler struct inode *inode = mapping->host; 103091d25ba8SRoss Zwisler unsigned long vaddr = vmf->address; 1031b90ca5ccSMatthew Wilcox pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1032b90ca5ccSMatthew Wilcox vm_fault_t ret; 1033e30331ffSRoss Zwisler 1034b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 10353159f943SMatthew Wilcox DAX_ZERO_PAGE, false); 10363159f943SMatthew Wilcox 1037ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vmf->vma, vaddr, pfn); 1038e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 1039e30331ffSRoss Zwisler return ret; 1040e30331ffSRoss Zwisler } 1041e30331ffSRoss Zwisler 10424b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev, 10434b0228faSVishal Verma unsigned int offset, unsigned int length) 10444b0228faSVishal Verma { 10454b0228faSVishal Verma unsigned short sector_size = bdev_logical_block_size(bdev); 10464b0228faSVishal Verma 10474b0228faSVishal Verma if (!IS_ALIGNED(offset, sector_size)) 10484b0228faSVishal Verma return false; 10494b0228faSVishal Verma if (!IS_ALIGNED(length, sector_size)) 10504b0228faSVishal Verma return false; 10514b0228faSVishal Verma 10524b0228faSVishal Verma return true; 10534b0228faSVishal Verma } 10544b0228faSVishal Verma 1055cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev, 1056cccbce67SDan Williams struct dax_device *dax_dev, sector_t sector, 1057cccbce67SDan Williams unsigned int offset, unsigned int size) 1058679c8bd3SChristoph Hellwig { 1059cccbce67SDan Williams if (dax_range_is_aligned(bdev, offset, size)) { 1060cccbce67SDan Williams sector_t start_sector = sector + (offset >> 9); 10614b0228faSVishal Verma 10624b0228faSVishal Verma return blkdev_issue_zeroout(bdev, start_sector, 106353ef7d0eSLinus Torvalds size >> 9, GFP_NOFS, 0); 10644b0228faSVishal Verma } else { 1065cccbce67SDan Williams pgoff_t pgoff; 1066cccbce67SDan Williams long rc, id; 1067cccbce67SDan Williams void *kaddr; 1068cccbce67SDan Williams 1069e84b83b9SDan Williams rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 1070cccbce67SDan Williams if (rc) 1071cccbce67SDan Williams return rc; 1072cccbce67SDan Williams 1073cccbce67SDan Williams id = dax_read_lock(); 107486ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL); 1075cccbce67SDan Williams if (rc < 0) { 1076cccbce67SDan Williams dax_read_unlock(id); 1077cccbce67SDan Williams return rc; 1078cccbce67SDan Williams } 107981f55870SDan Williams memset(kaddr + offset, 0, size); 1080c3ca015fSMikulas Patocka dax_flush(dax_dev, kaddr + offset, size); 1081cccbce67SDan Williams dax_read_unlock(id); 10824b0228faSVishal Verma } 1083679c8bd3SChristoph Hellwig return 0; 1084679c8bd3SChristoph Hellwig } 1085679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range); 1086679c8bd3SChristoph Hellwig 1087a254e568SChristoph Hellwig static loff_t 108811c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 1089a254e568SChristoph Hellwig struct iomap *iomap) 1090a254e568SChristoph Hellwig { 1091cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1092cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1093a254e568SChristoph Hellwig struct iov_iter *iter = data; 1094a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1095a254e568SChristoph Hellwig ssize_t ret = 0; 1096a77d4786SDan Williams size_t xfer; 1097cccbce67SDan Williams int id; 1098a254e568SChristoph Hellwig 1099a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1100a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1101a254e568SChristoph Hellwig if (pos >= end) 1102a254e568SChristoph Hellwig return 0; 1103a254e568SChristoph Hellwig 1104a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1105a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1106a254e568SChristoph Hellwig } 1107a254e568SChristoph Hellwig 1108a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1109a254e568SChristoph Hellwig return -EIO; 1110a254e568SChristoph Hellwig 1111e3fce68cSJan Kara /* 1112e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1113e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1114e3fce68cSJan Kara * written by write(2) is visible in mmap. 1115e3fce68cSJan Kara */ 1116cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1117e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1118e3fce68cSJan Kara pos >> PAGE_SHIFT, 1119e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1120e3fce68cSJan Kara } 1121e3fce68cSJan Kara 1122cccbce67SDan Williams id = dax_read_lock(); 1123a254e568SChristoph Hellwig while (pos < end) { 1124a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1125cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1126cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1127a254e568SChristoph Hellwig ssize_t map_len; 1128cccbce67SDan Williams pgoff_t pgoff; 1129cccbce67SDan Williams void *kaddr; 1130a254e568SChristoph Hellwig 1131d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1132d1908f52SMichal Hocko ret = -EINTR; 1133d1908f52SMichal Hocko break; 1134d1908f52SMichal Hocko } 1135d1908f52SMichal Hocko 1136cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1137cccbce67SDan Williams if (ret) 1138cccbce67SDan Williams break; 1139cccbce67SDan Williams 1140cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 114186ed913bSHuaisheng Ye &kaddr, NULL); 1142a254e568SChristoph Hellwig if (map_len < 0) { 1143a254e568SChristoph Hellwig ret = map_len; 1144a254e568SChristoph Hellwig break; 1145a254e568SChristoph Hellwig } 1146a254e568SChristoph Hellwig 1147cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1148cccbce67SDan Williams kaddr += offset; 1149a254e568SChristoph Hellwig map_len -= offset; 1150a254e568SChristoph Hellwig if (map_len > end - pos) 1151a254e568SChristoph Hellwig map_len = end - pos; 1152a254e568SChristoph Hellwig 1153a2e050f5SRoss Zwisler /* 1154a2e050f5SRoss Zwisler * The userspace address for the memory copy has already been 1155a2e050f5SRoss Zwisler * validated via access_ok() in either vfs_read() or 1156a2e050f5SRoss Zwisler * vfs_write(), depending on which operation we are doing. 1157a2e050f5SRoss Zwisler */ 1158a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1159a77d4786SDan Williams xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1160fec53774SDan Williams map_len, iter); 1161a254e568SChristoph Hellwig else 1162a77d4786SDan Williams xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1163b3a9a0c3SDan Williams map_len, iter); 1164a254e568SChristoph Hellwig 1165a77d4786SDan Williams pos += xfer; 1166a77d4786SDan Williams length -= xfer; 1167a77d4786SDan Williams done += xfer; 1168a77d4786SDan Williams 1169a77d4786SDan Williams if (xfer == 0) 1170a77d4786SDan Williams ret = -EFAULT; 1171a77d4786SDan Williams if (xfer < map_len) 1172a77d4786SDan Williams break; 1173a254e568SChristoph Hellwig } 1174cccbce67SDan Williams dax_read_unlock(id); 1175a254e568SChristoph Hellwig 1176a254e568SChristoph Hellwig return done ? done : ret; 1177a254e568SChristoph Hellwig } 1178a254e568SChristoph Hellwig 1179a254e568SChristoph Hellwig /** 118011c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1181a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1182a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1183a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1184a254e568SChristoph Hellwig * 1185a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1186a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1187a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1188a254e568SChristoph Hellwig */ 1189a254e568SChristoph Hellwig ssize_t 119011c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 11918ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1192a254e568SChristoph Hellwig { 1193a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1194a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1195a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1196a254e568SChristoph Hellwig unsigned flags = 0; 1197a254e568SChristoph Hellwig 1198168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 1199168316dbSChristoph Hellwig lockdep_assert_held_exclusive(&inode->i_rwsem); 1200a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1201168316dbSChristoph Hellwig } else { 1202168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1203168316dbSChristoph Hellwig } 1204a254e568SChristoph Hellwig 1205a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1206a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 120711c59c92SRoss Zwisler iter, dax_iomap_actor); 1208a254e568SChristoph Hellwig if (ret <= 0) 1209a254e568SChristoph Hellwig break; 1210a254e568SChristoph Hellwig pos += ret; 1211a254e568SChristoph Hellwig done += ret; 1212a254e568SChristoph Hellwig } 1213a254e568SChristoph Hellwig 1214a254e568SChristoph Hellwig iocb->ki_pos += done; 1215a254e568SChristoph Hellwig return done ? done : ret; 1216a254e568SChristoph Hellwig } 121711c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1218a7d73fe6SChristoph Hellwig 1219ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error) 12209f141d6eSJan Kara { 12219f141d6eSJan Kara if (error == 0) 12229f141d6eSJan Kara return VM_FAULT_NOPAGE; 1223c9aed74eSSouptick Joarder return vmf_error(error); 12249f141d6eSJan Kara } 12259f141d6eSJan Kara 1226aaa422c4SDan Williams /* 1227aaa422c4SDan Williams * MAP_SYNC on a dax mapping guarantees dirty metadata is 1228aaa422c4SDan Williams * flushed on write-faults (non-cow), but not read-faults. 1229aaa422c4SDan Williams */ 1230aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags, 1231aaa422c4SDan Williams struct vm_area_struct *vma, struct iomap *iomap) 1232aaa422c4SDan Williams { 1233aaa422c4SDan Williams return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) 1234aaa422c4SDan Williams && (iomap->flags & IOMAP_F_DIRTY); 1235aaa422c4SDan Williams } 1236aaa422c4SDan Williams 1237ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1238c0b24625SJan Kara int *iomap_errp, const struct iomap_ops *ops) 1239a7d73fe6SChristoph Hellwig { 1240a0987ad5SJan Kara struct vm_area_struct *vma = vmf->vma; 1241a0987ad5SJan Kara struct address_space *mapping = vma->vm_file->f_mapping; 1242b15cd800SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1243a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 12441a29d85eSJan Kara unsigned long vaddr = vmf->address; 1245a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1246a7d73fe6SChristoph Hellwig struct iomap iomap = { 0 }; 12479484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1248a7d73fe6SChristoph Hellwig int error, major = 0; 1249d2c43ef1SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 1250caa51d26SJan Kara bool sync; 1251ab77dab4SSouptick Joarder vm_fault_t ret = 0; 1252a7d73fe6SChristoph Hellwig void *entry; 12531b5a1cb2SJan Kara pfn_t pfn; 1254a7d73fe6SChristoph Hellwig 1255ab77dab4SSouptick Joarder trace_dax_pte_fault(inode, vmf, ret); 1256a7d73fe6SChristoph Hellwig /* 1257a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1258a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1259a7d73fe6SChristoph Hellwig * a reliable test. 1260a7d73fe6SChristoph Hellwig */ 1261a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1262ab77dab4SSouptick Joarder ret = VM_FAULT_SIGBUS; 1263a9c42b33SRoss Zwisler goto out; 1264a9c42b33SRoss Zwisler } 1265a7d73fe6SChristoph Hellwig 1266d2c43ef1SJan Kara if (write && !vmf->cow_page) 1267a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1268a7d73fe6SChristoph Hellwig 1269b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, 0); 1270b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1271b15cd800SMatthew Wilcox ret = xa_to_internal(entry); 127213e451fdSJan Kara goto out; 127313e451fdSJan Kara } 127413e451fdSJan Kara 1275a7d73fe6SChristoph Hellwig /* 1276e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1277e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1278e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1279e2093926SRoss Zwisler * retried. 1280e2093926SRoss Zwisler */ 1281e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1282ab77dab4SSouptick Joarder ret = VM_FAULT_NOPAGE; 1283e2093926SRoss Zwisler goto unlock_entry; 1284e2093926SRoss Zwisler } 1285e2093926SRoss Zwisler 1286e2093926SRoss Zwisler /* 1287a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1288a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1289a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1290a7d73fe6SChristoph Hellwig */ 1291a7d73fe6SChristoph Hellwig error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap); 1292c0b24625SJan Kara if (iomap_errp) 1293c0b24625SJan Kara *iomap_errp = error; 1294a9c42b33SRoss Zwisler if (error) { 1295ab77dab4SSouptick Joarder ret = dax_fault_return(error); 129613e451fdSJan Kara goto unlock_entry; 1297a9c42b33SRoss Zwisler } 1298a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 129913e451fdSJan Kara error = -EIO; /* fs corruption? */ 130013e451fdSJan Kara goto error_finish_iomap; 1301a7d73fe6SChristoph Hellwig } 1302a7d73fe6SChristoph Hellwig 1303a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 130431a6f1a6SJan Kara sector_t sector = dax_iomap_sector(&iomap, pos); 130531a6f1a6SJan Kara 1306a7d73fe6SChristoph Hellwig switch (iomap.type) { 1307a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1308a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1309a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1310a7d73fe6SChristoph Hellwig break; 1311a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1312cccbce67SDan Williams error = copy_user_dax(iomap.bdev, iomap.dax_dev, 1313cccbce67SDan Williams sector, PAGE_SIZE, vmf->cow_page, vaddr); 1314a7d73fe6SChristoph Hellwig break; 1315a7d73fe6SChristoph Hellwig default: 1316a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1317a7d73fe6SChristoph Hellwig error = -EIO; 1318a7d73fe6SChristoph Hellwig break; 1319a7d73fe6SChristoph Hellwig } 1320a7d73fe6SChristoph Hellwig 1321a7d73fe6SChristoph Hellwig if (error) 132213e451fdSJan Kara goto error_finish_iomap; 1323b1aa812bSJan Kara 1324b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1325ab77dab4SSouptick Joarder ret = finish_fault(vmf); 1326ab77dab4SSouptick Joarder if (!ret) 1327ab77dab4SSouptick Joarder ret = VM_FAULT_DONE_COW; 132813e451fdSJan Kara goto finish_iomap; 1329a7d73fe6SChristoph Hellwig } 1330a7d73fe6SChristoph Hellwig 1331aaa422c4SDan Williams sync = dax_fault_is_synchronous(flags, vma, &iomap); 1332caa51d26SJan Kara 1333a7d73fe6SChristoph Hellwig switch (iomap.type) { 1334a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1335a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1336a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 1337a0987ad5SJan Kara count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 1338a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1339a7d73fe6SChristoph Hellwig } 13401b5a1cb2SJan Kara error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); 13411b5a1cb2SJan Kara if (error < 0) 13421b5a1cb2SJan Kara goto error_finish_iomap; 13431b5a1cb2SJan Kara 1344b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 1345caa51d26SJan Kara 0, write && !sync); 13461b5a1cb2SJan Kara 1347caa51d26SJan Kara /* 1348caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1349caa51d26SJan Kara * we can insert PTE into page tables only after that happens. 1350caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1351caa51d26SJan Kara * insert it after fsync is done. 1352caa51d26SJan Kara */ 1353caa51d26SJan Kara if (sync) { 1354caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) { 1355caa51d26SJan Kara error = -EIO; 1356caa51d26SJan Kara goto error_finish_iomap; 1357caa51d26SJan Kara } 1358caa51d26SJan Kara *pfnp = pfn; 1359ab77dab4SSouptick Joarder ret = VM_FAULT_NEEDDSYNC | major; 1360caa51d26SJan Kara goto finish_iomap; 1361caa51d26SJan Kara } 13621b5a1cb2SJan Kara trace_dax_insert_mapping(inode, vmf, entry); 13631b5a1cb2SJan Kara if (write) 1364ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn); 13651b5a1cb2SJan Kara else 1366ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vma, vaddr, pfn); 13671b5a1cb2SJan Kara 1368ab77dab4SSouptick Joarder goto finish_iomap; 1369a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1370a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1371d2c43ef1SJan Kara if (!write) { 1372b15cd800SMatthew Wilcox ret = dax_load_hole(&xas, mapping, &entry, vmf); 137313e451fdSJan Kara goto finish_iomap; 13741550290bSRoss Zwisler } 1375a7d73fe6SChristoph Hellwig /*FALLTHRU*/ 1376a7d73fe6SChristoph Hellwig default: 1377a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1378a7d73fe6SChristoph Hellwig error = -EIO; 1379a7d73fe6SChristoph Hellwig break; 1380a7d73fe6SChristoph Hellwig } 1381a7d73fe6SChristoph Hellwig 138213e451fdSJan Kara error_finish_iomap: 1383ab77dab4SSouptick Joarder ret = dax_fault_return(error); 13849f141d6eSJan Kara finish_iomap: 13859f141d6eSJan Kara if (ops->iomap_end) { 13869f141d6eSJan Kara int copied = PAGE_SIZE; 13879f141d6eSJan Kara 1388ab77dab4SSouptick Joarder if (ret & VM_FAULT_ERROR) 13899f141d6eSJan Kara copied = 0; 13909f141d6eSJan Kara /* 13919f141d6eSJan Kara * The fault is done by now and there's no way back (other 13929f141d6eSJan Kara * thread may be already happily using PTE we have installed). 13939f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 13949f141d6eSJan Kara * with it. 13959f141d6eSJan Kara */ 13969f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 13971550290bSRoss Zwisler } 139813e451fdSJan Kara unlock_entry: 1399b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1400a9c42b33SRoss Zwisler out: 1401ab77dab4SSouptick Joarder trace_dax_pte_fault_done(inode, vmf, ret); 1402ab77dab4SSouptick Joarder return ret | major; 1403a7d73fe6SChristoph Hellwig } 1404642261acSRoss Zwisler 1405642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1406b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1407b15cd800SMatthew Wilcox struct iomap *iomap, void **entry) 1408642261acSRoss Zwisler { 1409f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1410f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1411653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 1412642261acSRoss Zwisler struct page *zero_page; 1413642261acSRoss Zwisler spinlock_t *ptl; 1414642261acSRoss Zwisler pmd_t pmd_entry; 14153fe0791cSDan Williams pfn_t pfn; 1416642261acSRoss Zwisler 1417f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1418642261acSRoss Zwisler 1419642261acSRoss Zwisler if (unlikely(!zero_page)) 1420653b2ea3SRoss Zwisler goto fallback; 1421642261acSRoss Zwisler 14223fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1423b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14243159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1425642261acSRoss Zwisler 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 1432f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1433642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1434f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1435642261acSRoss Zwisler spin_unlock(ptl); 1436b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1437642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1438653b2ea3SRoss Zwisler 1439653b2ea3SRoss Zwisler fallback: 1440b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1441642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1442642261acSRoss Zwisler } 1443642261acSRoss Zwisler 1444ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1445a2d58167SDave Jiang const struct iomap_ops *ops) 1446642261acSRoss Zwisler { 1447f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1448642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1449b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1450d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1451d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1452caa51d26SJan Kara bool sync; 14539484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1454642261acSRoss Zwisler struct inode *inode = mapping->host; 1455ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1456642261acSRoss Zwisler struct iomap iomap = { 0 }; 1457b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1458642261acSRoss Zwisler void *entry; 1459642261acSRoss Zwisler loff_t pos; 1460642261acSRoss Zwisler int error; 1461302a5e31SJan Kara pfn_t pfn; 1462642261acSRoss Zwisler 1463282a8e03SRoss Zwisler /* 1464282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1465282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1466282a8e03SRoss Zwisler * this is a reliable test. 1467282a8e03SRoss Zwisler */ 1468957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1469282a8e03SRoss Zwisler 1470f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1471282a8e03SRoss Zwisler 1472fffa281bSRoss Zwisler /* 1473fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1474fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1475fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1476a77d19f4SMatthew Wilcox * range in the page cache. 1477fffa281bSRoss Zwisler */ 1478fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1479fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1480fffa281bSRoss Zwisler goto fallback; 1481fffa281bSRoss Zwisler 1482642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1483642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1484642261acSRoss Zwisler goto fallback; 1485642261acSRoss Zwisler 1486642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1487642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1488642261acSRoss Zwisler goto fallback; 1489642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1490642261acSRoss Zwisler goto fallback; 1491642261acSRoss Zwisler 1492b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1493282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1494282a8e03SRoss Zwisler goto out; 1495282a8e03SRoss Zwisler } 1496642261acSRoss Zwisler 1497642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1498b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1499642261acSRoss Zwisler goto fallback; 1500642261acSRoss Zwisler 1501642261acSRoss Zwisler /* 1502b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1503b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1504b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1505b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 15069f141d6eSJan Kara */ 1507b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, DAX_PMD); 1508b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1509b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1510876f2946SRoss Zwisler goto fallback; 1511b15cd800SMatthew Wilcox } 1512876f2946SRoss Zwisler 1513876f2946SRoss Zwisler /* 1514e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1515e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1516e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1517e2093926SRoss Zwisler * retried. 1518e2093926SRoss Zwisler */ 1519e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1520e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1521e2093926SRoss Zwisler result = 0; 1522e2093926SRoss Zwisler goto unlock_entry; 1523e2093926SRoss Zwisler } 1524e2093926SRoss Zwisler 1525e2093926SRoss Zwisler /* 1526876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1527876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1528876f2946SRoss Zwisler * to look up our filesystem block. 1529876f2946SRoss Zwisler */ 1530b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1531876f2946SRoss Zwisler error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); 1532876f2946SRoss Zwisler if (error) 1533876f2946SRoss Zwisler goto unlock_entry; 1534876f2946SRoss Zwisler 1535876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15369f141d6eSJan Kara goto finish_iomap; 15379f141d6eSJan Kara 1538aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1539caa51d26SJan Kara 1540642261acSRoss Zwisler switch (iomap.type) { 1541642261acSRoss Zwisler case IOMAP_MAPPED: 1542302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1543302a5e31SJan Kara if (error < 0) 1544302a5e31SJan Kara goto finish_iomap; 1545302a5e31SJan Kara 1546b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15473159f943SMatthew Wilcox DAX_PMD, write && !sync); 1548302a5e31SJan Kara 1549caa51d26SJan Kara /* 1550caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1551caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1552caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1553caa51d26SJan Kara * insert it after fsync is done. 1554caa51d26SJan Kara */ 1555caa51d26SJan Kara if (sync) { 1556caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1557caa51d26SJan Kara goto finish_iomap; 1558caa51d26SJan Kara *pfnp = pfn; 1559caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1560caa51d26SJan Kara goto finish_iomap; 1561caa51d26SJan Kara } 1562caa51d26SJan Kara 1563302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1564302a5e31SJan Kara result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn, 1565302a5e31SJan Kara write); 1566642261acSRoss Zwisler break; 1567642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1568642261acSRoss Zwisler case IOMAP_HOLE: 1569642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1570876f2946SRoss Zwisler break; 1571b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1572642261acSRoss Zwisler break; 1573642261acSRoss Zwisler default: 1574642261acSRoss Zwisler WARN_ON_ONCE(1); 1575642261acSRoss Zwisler break; 1576642261acSRoss Zwisler } 1577642261acSRoss Zwisler 15789f141d6eSJan Kara finish_iomap: 15799f141d6eSJan Kara if (ops->iomap_end) { 15809f141d6eSJan Kara int copied = PMD_SIZE; 15819f141d6eSJan Kara 15829f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 15839f141d6eSJan Kara copied = 0; 15849f141d6eSJan Kara /* 15859f141d6eSJan Kara * The fault is done by now and there's no way back (other 15869f141d6eSJan Kara * thread may be already happily using PMD we have installed). 15879f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 15889f141d6eSJan Kara * with it. 15899f141d6eSJan Kara */ 15909f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 15919f141d6eSJan Kara &iomap); 15929f141d6eSJan Kara } 1593876f2946SRoss Zwisler unlock_entry: 1594b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1595642261acSRoss Zwisler fallback: 1596642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1597d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1598642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1599642261acSRoss Zwisler } 1600282a8e03SRoss Zwisler out: 1601f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1602642261acSRoss Zwisler return result; 1603642261acSRoss Zwisler } 1604a2d58167SDave Jiang #else 1605ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 160601cddfe9SArnd Bergmann const struct iomap_ops *ops) 1607a2d58167SDave Jiang { 1608a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1609a2d58167SDave Jiang } 1610642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1611a2d58167SDave Jiang 1612a2d58167SDave Jiang /** 1613a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1614a2d58167SDave Jiang * @vmf: The description of the fault 1615cec04e8cSJan Kara * @pe_size: Size of the page to fault in 16169a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1617c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1618cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1619a2d58167SDave Jiang * 1620a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1621a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1622a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1623a2d58167SDave Jiang * successfully. 1624a2d58167SDave Jiang */ 1625ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1626c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1627a2d58167SDave Jiang { 1628c791ace1SDave Jiang switch (pe_size) { 1629c791ace1SDave Jiang case PE_SIZE_PTE: 1630c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1631c791ace1SDave Jiang case PE_SIZE_PMD: 16329a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1633a2d58167SDave Jiang default: 1634a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1635a2d58167SDave Jiang } 1636a2d58167SDave Jiang } 1637a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 163871eab6dfSJan Kara 1639a77d19f4SMatthew Wilcox /* 164071eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 164171eab6dfSJan Kara * @vmf: The description of the fault 164271eab6dfSJan Kara * @pfn: PFN to insert 1643cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 164471eab6dfSJan Kara * 1645a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1646a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 164771eab6dfSJan Kara */ 1648cfc93c6cSMatthew Wilcox static vm_fault_t 1649cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 165071eab6dfSJan Kara { 165171eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1652cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1653cfc93c6cSMatthew Wilcox void *entry; 1654ab77dab4SSouptick Joarder vm_fault_t ret; 165571eab6dfSJan Kara 1656cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 1657cfc93c6cSMatthew Wilcox entry = get_unlocked_entry(&xas); 165871eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 165971eab6dfSJan Kara if (!entry || 1660cfc93c6cSMatthew Wilcox (order == 0 && !dax_is_pte_entry(entry)) || 16610e40de03SMatthew Wilcox (order == PMD_ORDER && !dax_is_pmd_entry(entry))) { 1662cfc93c6cSMatthew Wilcox put_unlocked_entry(&xas, entry); 1663cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 166471eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 166571eab6dfSJan Kara VM_FAULT_NOPAGE); 166671eab6dfSJan Kara return VM_FAULT_NOPAGE; 166771eab6dfSJan Kara } 1668cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1669cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1670cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1671cfc93c6cSMatthew Wilcox if (order == 0) 1672ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 167371eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1674cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1675ab77dab4SSouptick Joarder ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, 167671eab6dfSJan Kara pfn, true); 167771eab6dfSJan Kara #endif 1678cfc93c6cSMatthew Wilcox else 1679ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1680cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1681ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1682ab77dab4SSouptick Joarder return ret; 168371eab6dfSJan Kara } 168471eab6dfSJan Kara 168571eab6dfSJan Kara /** 168671eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 168771eab6dfSJan Kara * @vmf: The description of the fault 168871eab6dfSJan Kara * @pe_size: Size of entry to be inserted 168971eab6dfSJan Kara * @pfn: PFN to insert 169071eab6dfSJan Kara * 169171eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 169271eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 169371eab6dfSJan Kara * table entry. 169471eab6dfSJan Kara */ 1695ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1696ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 169771eab6dfSJan Kara { 169871eab6dfSJan Kara int err; 169971eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1700cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1701cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 170271eab6dfSJan Kara 170371eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 170471eab6dfSJan Kara if (err) 170571eab6dfSJan Kara return VM_FAULT_SIGBUS; 1706cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 170771eab6dfSJan Kara } 170871eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1709