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 (;;) { 219cfc93c6cSMatthew Wilcox entry = xas_load(xas); 220cfc93c6cSMatthew Wilcox if (!entry || xa_is_internal(entry) || 221cfc93c6cSMatthew Wilcox WARN_ON_ONCE(!xa_is_value(entry)) || 222cfc93c6cSMatthew Wilcox !dax_is_locked(entry)) 223cfc93c6cSMatthew Wilcox return entry; 224cfc93c6cSMatthew Wilcox 225b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 226cfc93c6cSMatthew Wilcox prepare_to_wait_exclusive(wq, &ewait.wait, 227cfc93c6cSMatthew Wilcox TASK_UNINTERRUPTIBLE); 228cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 229cfc93c6cSMatthew Wilcox xas_reset(xas); 230cfc93c6cSMatthew Wilcox schedule(); 231cfc93c6cSMatthew Wilcox finish_wait(wq, &ewait.wait); 232cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 233cfc93c6cSMatthew Wilcox } 234cfc93c6cSMatthew Wilcox } 235cfc93c6cSMatthew Wilcox 236cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry) 237cfc93c6cSMatthew Wilcox { 238cfc93c6cSMatthew Wilcox /* If we were the only waiter woken, wake the next one */ 239cfc93c6cSMatthew Wilcox if (entry) 240cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 241cfc93c6cSMatthew Wilcox } 242cfc93c6cSMatthew Wilcox 243cfc93c6cSMatthew Wilcox /* 244cfc93c6cSMatthew Wilcox * We used the xa_state to get the entry, but then we locked the entry and 245cfc93c6cSMatthew Wilcox * dropped the xa_lock, so we know the xa_state is stale and must be reset 246cfc93c6cSMatthew Wilcox * before use. 247cfc93c6cSMatthew Wilcox */ 248cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry) 249cfc93c6cSMatthew Wilcox { 250cfc93c6cSMatthew Wilcox void *old; 251cfc93c6cSMatthew Wilcox 2527ae2ea7dSMatthew Wilcox BUG_ON(dax_is_locked(entry)); 253cfc93c6cSMatthew Wilcox xas_reset(xas); 254cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 255cfc93c6cSMatthew Wilcox old = xas_store(xas, entry); 256cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 257cfc93c6cSMatthew Wilcox BUG_ON(!dax_is_locked(old)); 258cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 259cfc93c6cSMatthew Wilcox } 260cfc93c6cSMatthew Wilcox 261cfc93c6cSMatthew Wilcox /* 262cfc93c6cSMatthew Wilcox * Return: The entry stored at this location before it was locked. 263cfc93c6cSMatthew Wilcox */ 264cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry) 265cfc93c6cSMatthew Wilcox { 266cfc93c6cSMatthew Wilcox unsigned long v = xa_to_value(entry); 267cfc93c6cSMatthew Wilcox return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 268cfc93c6cSMatthew Wilcox } 269cfc93c6cSMatthew Wilcox 270d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry) 271d2c997c0SDan Williams { 272d2c997c0SDan Williams if (dax_is_zero_entry(entry)) 273d2c997c0SDan Williams return 0; 274d2c997c0SDan Williams else if (dax_is_empty_entry(entry)) 275d2c997c0SDan Williams return 0; 276d2c997c0SDan Williams else if (dax_is_pmd_entry(entry)) 277d2c997c0SDan Williams return PMD_SIZE; 278d2c997c0SDan Williams else 279d2c997c0SDan Williams return PAGE_SIZE; 280d2c997c0SDan Williams } 281d2c997c0SDan Williams 282a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry) 283d2c997c0SDan Williams { 284a77d19f4SMatthew Wilcox return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE; 285d2c997c0SDan Williams } 286d2c997c0SDan Williams 287d2c997c0SDan Williams /* 288d2c997c0SDan Williams * Iterate through all mapped pfns represented by an entry, i.e. skip 289d2c997c0SDan Williams * 'empty' and 'zero' entries. 290d2c997c0SDan Williams */ 291d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \ 292a77d19f4SMatthew Wilcox for (pfn = dax_to_pfn(entry); \ 293a77d19f4SMatthew Wilcox pfn < dax_end_pfn(entry); pfn++) 294d2c997c0SDan Williams 29573449dafSDan Williams /* 29673449dafSDan Williams * TODO: for reflink+dax we need a way to associate a single page with 29773449dafSDan Williams * multiple address_space instances at different linear_page_index() 29873449dafSDan Williams * offsets. 29973449dafSDan Williams */ 30073449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping, 30173449dafSDan Williams struct vm_area_struct *vma, unsigned long address) 302d2c997c0SDan Williams { 30373449dafSDan Williams unsigned long size = dax_entry_size(entry), pfn, index; 30473449dafSDan Williams int i = 0; 305d2c997c0SDan Williams 306d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 307d2c997c0SDan Williams return; 308d2c997c0SDan Williams 30973449dafSDan Williams index = linear_page_index(vma, address & ~(size - 1)); 310d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 311d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 312d2c997c0SDan Williams 313d2c997c0SDan Williams WARN_ON_ONCE(page->mapping); 314d2c997c0SDan Williams page->mapping = mapping; 31573449dafSDan Williams page->index = index + i++; 316d2c997c0SDan Williams } 317d2c997c0SDan Williams } 318d2c997c0SDan Williams 319d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping, 320d2c997c0SDan Williams bool trunc) 321d2c997c0SDan Williams { 322d2c997c0SDan Williams unsigned long pfn; 323d2c997c0SDan Williams 324d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 325d2c997c0SDan Williams return; 326d2c997c0SDan Williams 327d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 328d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 329d2c997c0SDan Williams 330d2c997c0SDan Williams WARN_ON_ONCE(trunc && page_ref_count(page) > 1); 331d2c997c0SDan Williams WARN_ON_ONCE(page->mapping && page->mapping != mapping); 332d2c997c0SDan Williams page->mapping = NULL; 33373449dafSDan Williams page->index = 0; 334d2c997c0SDan Williams } 335d2c997c0SDan Williams } 336d2c997c0SDan Williams 3375fac7408SDan Williams static struct page *dax_busy_page(void *entry) 3385fac7408SDan Williams { 3395fac7408SDan Williams unsigned long pfn; 3405fac7408SDan Williams 3415fac7408SDan Williams for_each_mapped_pfn(entry, pfn) { 3425fac7408SDan Williams struct page *page = pfn_to_page(pfn); 3435fac7408SDan Williams 3445fac7408SDan Williams if (page_ref_count(page) > 1) 3455fac7408SDan Williams return page; 3465fac7408SDan Williams } 3475fac7408SDan Williams return NULL; 3485fac7408SDan Williams } 3495fac7408SDan Williams 350c5bbd451SMatthew Wilcox /* 351c5bbd451SMatthew Wilcox * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page 352c5bbd451SMatthew Wilcox * @page: The page whose entry we want to lock 353c5bbd451SMatthew Wilcox * 354c5bbd451SMatthew Wilcox * Context: Process context. 355c5bbd451SMatthew Wilcox * Return: %true if the entry was locked or does not need to be locked. 356c5bbd451SMatthew Wilcox */ 357c2a7d2a1SDan Williams bool dax_lock_mapping_entry(struct page *page) 358c2a7d2a1SDan Williams { 3599f32d221SMatthew Wilcox XA_STATE(xas, NULL, 0); 3609f32d221SMatthew Wilcox void *entry; 361c5bbd451SMatthew Wilcox bool locked; 362c2a7d2a1SDan Williams 363c5bbd451SMatthew Wilcox /* Ensure page->mapping isn't freed while we look at it */ 364c5bbd451SMatthew Wilcox rcu_read_lock(); 365c2a7d2a1SDan Williams for (;;) { 3669f32d221SMatthew Wilcox struct address_space *mapping = READ_ONCE(page->mapping); 367c2a7d2a1SDan Williams 368c5bbd451SMatthew Wilcox locked = false; 369c2a7d2a1SDan Williams if (!dax_mapping(mapping)) 370c5bbd451SMatthew Wilcox break; 371c2a7d2a1SDan Williams 372c2a7d2a1SDan Williams /* 373c2a7d2a1SDan Williams * In the device-dax case there's no need to lock, a 374c2a7d2a1SDan Williams * struct dev_pagemap pin is sufficient to keep the 375c2a7d2a1SDan Williams * inode alive, and we assume we have dev_pagemap pin 376c2a7d2a1SDan Williams * otherwise we would not have a valid pfn_to_page() 377c2a7d2a1SDan Williams * translation. 378c2a7d2a1SDan Williams */ 379c5bbd451SMatthew Wilcox locked = true; 3809f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 381c5bbd451SMatthew Wilcox break; 382c2a7d2a1SDan Williams 3839f32d221SMatthew Wilcox xas.xa = &mapping->i_pages; 3849f32d221SMatthew Wilcox xas_lock_irq(&xas); 385c2a7d2a1SDan Williams if (mapping != page->mapping) { 3869f32d221SMatthew Wilcox xas_unlock_irq(&xas); 387c2a7d2a1SDan Williams continue; 388c2a7d2a1SDan Williams } 3899f32d221SMatthew Wilcox xas_set(&xas, page->index); 3909f32d221SMatthew Wilcox entry = xas_load(&xas); 3919f32d221SMatthew Wilcox if (dax_is_locked(entry)) { 392c5bbd451SMatthew Wilcox rcu_read_unlock(); 3939f32d221SMatthew Wilcox entry = get_unlocked_entry(&xas); 3949f32d221SMatthew Wilcox xas_unlock_irq(&xas); 395c5bbd451SMatthew Wilcox rcu_read_lock(); 396c2a7d2a1SDan Williams continue; 397c2a7d2a1SDan Williams } 3989f32d221SMatthew Wilcox dax_lock_entry(&xas, entry); 3999f32d221SMatthew Wilcox xas_unlock_irq(&xas); 400c5bbd451SMatthew Wilcox break; 4019f32d221SMatthew Wilcox } 402c5bbd451SMatthew Wilcox rcu_read_unlock(); 403c5bbd451SMatthew Wilcox return locked; 404c2a7d2a1SDan Williams } 405c2a7d2a1SDan Williams 406c2a7d2a1SDan Williams void dax_unlock_mapping_entry(struct page *page) 407c2a7d2a1SDan Williams { 408c2a7d2a1SDan Williams struct address_space *mapping = page->mapping; 4099f32d221SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, page->index); 410fda490d3SMatthew Wilcox void *entry; 411c2a7d2a1SDan Williams 4129f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 413c2a7d2a1SDan Williams return; 414c2a7d2a1SDan Williams 415fda490d3SMatthew Wilcox rcu_read_lock(); 416fda490d3SMatthew Wilcox entry = xas_load(&xas); 417fda490d3SMatthew Wilcox rcu_read_unlock(); 418fda490d3SMatthew Wilcox entry = dax_make_entry(page_to_pfn_t(page), dax_is_pmd_entry(entry)); 419fda490d3SMatthew Wilcox dax_unlock_entry(&xas, entry); 420c2a7d2a1SDan Williams } 421c2a7d2a1SDan Williams 422ac401cc7SJan Kara /* 423a77d19f4SMatthew Wilcox * Find page cache entry at given index. If it is a DAX entry, return it 424a77d19f4SMatthew Wilcox * with the entry locked. If the page cache doesn't contain an entry at 425a77d19f4SMatthew Wilcox * that index, add a locked empty entry. 426ac401cc7SJan Kara * 4273159f943SMatthew Wilcox * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 428b15cd800SMatthew Wilcox * either return that locked entry or will return VM_FAULT_FALLBACK. 429b15cd800SMatthew Wilcox * This will happen if there are any PTE entries within the PMD range 430b15cd800SMatthew Wilcox * that we are requesting. 431642261acSRoss Zwisler * 432b15cd800SMatthew Wilcox * We always favor PTE entries over PMD entries. There isn't a flow where we 433b15cd800SMatthew Wilcox * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 434b15cd800SMatthew Wilcox * insertion will fail if it finds any PTE entries already in the tree, and a 435b15cd800SMatthew Wilcox * PTE insertion will cause an existing PMD entry to be unmapped and 436b15cd800SMatthew Wilcox * downgraded to PTE entries. This happens for both PMD zero pages as 437b15cd800SMatthew Wilcox * well as PMD empty entries. 438642261acSRoss Zwisler * 439b15cd800SMatthew Wilcox * The exception to this downgrade path is for PMD entries that have 440b15cd800SMatthew Wilcox * real storage backing them. We will leave these real PMD entries in 441b15cd800SMatthew Wilcox * the tree, and PTE writes will simply dirty the entire PMD entry. 442642261acSRoss Zwisler * 443ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 444ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 445ac401cc7SJan Kara * show it helps. 446b15cd800SMatthew Wilcox * 447b15cd800SMatthew Wilcox * On error, this function does not return an ERR_PTR. Instead it returns 448b15cd800SMatthew Wilcox * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 449b15cd800SMatthew Wilcox * overlap with xarray value entries. 450ac401cc7SJan Kara */ 451b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas, 452b15cd800SMatthew Wilcox struct address_space *mapping, unsigned long size_flag) 453ac401cc7SJan Kara { 454b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 455b15cd800SMatthew Wilcox bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */ 456b15cd800SMatthew Wilcox void *entry; 457ac401cc7SJan Kara 458b15cd800SMatthew Wilcox retry: 459b15cd800SMatthew Wilcox xas_lock_irq(xas); 460b15cd800SMatthew Wilcox entry = get_unlocked_entry(xas); 461b15cd800SMatthew Wilcox if (xa_is_internal(entry)) 462b15cd800SMatthew Wilcox goto fallback; 463642261acSRoss Zwisler 464b15cd800SMatthew Wilcox if (entry) { 465b15cd800SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) { 466b15cd800SMatthew Wilcox xas_set_err(xas, EIO); 46791d25ba8SRoss Zwisler goto out_unlock; 46891d25ba8SRoss Zwisler } 46991d25ba8SRoss Zwisler 4703159f943SMatthew Wilcox if (size_flag & DAX_PMD) { 47191d25ba8SRoss Zwisler if (dax_is_pte_entry(entry)) { 472b15cd800SMatthew Wilcox put_unlocked_entry(xas, entry); 473b15cd800SMatthew Wilcox goto fallback; 474642261acSRoss Zwisler } 475642261acSRoss Zwisler } else { /* trying to grab a PTE entry */ 47691d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 477642261acSRoss Zwisler (dax_is_zero_entry(entry) || 478642261acSRoss Zwisler dax_is_empty_entry(entry))) { 479642261acSRoss Zwisler pmd_downgrade = true; 480642261acSRoss Zwisler } 481642261acSRoss Zwisler } 482642261acSRoss Zwisler } 483642261acSRoss Zwisler 484642261acSRoss Zwisler if (pmd_downgrade) { 485642261acSRoss Zwisler /* 486642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 487b93b0163SMatthew Wilcox * the i_pages lock. 488642261acSRoss Zwisler */ 489b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 490642261acSRoss Zwisler 491642261acSRoss Zwisler /* 492642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 493642261acSRoss Zwisler * downgraded are empty entries which don't need to be 494642261acSRoss Zwisler * unmapped. 495642261acSRoss Zwisler */ 496b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 497b15cd800SMatthew Wilcox xas_unlock_irq(xas); 498b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 499b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 500977fbdcdSMatthew Wilcox PG_PMD_NR, false); 501b15cd800SMatthew Wilcox xas_reset(xas); 502b15cd800SMatthew Wilcox xas_lock_irq(xas); 503e11f8b7bSRoss Zwisler } 504e11f8b7bSRoss Zwisler 505d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 506b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 507b15cd800SMatthew Wilcox dax_wake_entry(xas, entry, true); 508642261acSRoss Zwisler mapping->nrexceptional--; 509b15cd800SMatthew Wilcox entry = NULL; 510b15cd800SMatthew Wilcox xas_set(xas, index); 511642261acSRoss Zwisler } 512642261acSRoss Zwisler 513b15cd800SMatthew Wilcox if (entry) { 514b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 515b15cd800SMatthew Wilcox } else { 516b15cd800SMatthew Wilcox entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY); 517b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 518b15cd800SMatthew Wilcox if (xas_error(xas)) 519b15cd800SMatthew Wilcox goto out_unlock; 520ac401cc7SJan Kara mapping->nrexceptional++; 521ac401cc7SJan Kara } 522b15cd800SMatthew Wilcox 523642261acSRoss Zwisler out_unlock: 524b15cd800SMatthew Wilcox xas_unlock_irq(xas); 525b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 526b15cd800SMatthew Wilcox goto retry; 527b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 528b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 529b15cd800SMatthew Wilcox if (xas_error(xas)) 530b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 531e3ad61c6SRoss Zwisler return entry; 532b15cd800SMatthew Wilcox fallback: 533b15cd800SMatthew Wilcox xas_unlock_irq(xas); 534b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 535ac401cc7SJan Kara } 536ac401cc7SJan Kara 5375fac7408SDan Williams /** 5385fac7408SDan Williams * dax_layout_busy_page - find first pinned page in @mapping 5395fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5405fac7408SDan Williams * 5415fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5425fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5435fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5445fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5455fac7408SDan Williams * get_user_pages() usages. 5465fac7408SDan Williams * 5475fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5485fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5495fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5505fac7408SDan Williams * mapping_mapped() becoming true. 5515fac7408SDan Williams */ 5525fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping) 5535fac7408SDan Williams { 554084a8990SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 555084a8990SMatthew Wilcox void *entry; 556084a8990SMatthew Wilcox unsigned int scanned = 0; 5575fac7408SDan Williams struct page *page = NULL; 5585fac7408SDan Williams 5595fac7408SDan Williams /* 5605fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 5615fac7408SDan Williams */ 5625fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 5635fac7408SDan Williams return NULL; 5645fac7408SDan Williams 5655fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 5665fac7408SDan Williams return NULL; 5675fac7408SDan Williams 5685fac7408SDan Williams /* 5695fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 570084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 5715fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 5725fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 5735fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 5745fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 5755fac7408SDan Williams * holding those locks, and unmap_mapping_range() will not zero the 5765fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 5775fac7408SDan Williams * guaranteed to either see new references or prevent new 5785fac7408SDan Williams * references from being established. 5795fac7408SDan Williams */ 5805fac7408SDan Williams unmap_mapping_range(mapping, 0, 0, 1); 5815fac7408SDan Williams 582084a8990SMatthew Wilcox xas_lock_irq(&xas); 583084a8990SMatthew Wilcox xas_for_each(&xas, entry, ULONG_MAX) { 584084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 5855fac7408SDan Williams continue; 586084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 587084a8990SMatthew Wilcox entry = get_unlocked_entry(&xas); 5885fac7408SDan Williams if (entry) 5895fac7408SDan Williams page = dax_busy_page(entry); 590084a8990SMatthew Wilcox put_unlocked_entry(&xas, entry); 5915fac7408SDan Williams if (page) 5925fac7408SDan Williams break; 593084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 594084a8990SMatthew Wilcox continue; 595cdbf8897SRoss Zwisler 596084a8990SMatthew Wilcox xas_pause(&xas); 597084a8990SMatthew Wilcox xas_unlock_irq(&xas); 598084a8990SMatthew Wilcox cond_resched(); 599084a8990SMatthew Wilcox xas_lock_irq(&xas); 6005fac7408SDan Williams } 601084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6025fac7408SDan Williams return page; 6035fac7408SDan Williams } 6045fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6055fac7408SDan Williams 606a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 607c6dcf52cSJan Kara pgoff_t index, bool trunc) 608c6dcf52cSJan Kara { 60907f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 610c6dcf52cSJan Kara int ret = 0; 611c6dcf52cSJan Kara void *entry; 612c6dcf52cSJan Kara 61307f2d89cSMatthew Wilcox xas_lock_irq(&xas); 61407f2d89cSMatthew Wilcox entry = get_unlocked_entry(&xas); 6153159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 616c6dcf52cSJan Kara goto out; 617c6dcf52cSJan Kara if (!trunc && 61807f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 61907f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 620c6dcf52cSJan Kara goto out; 621d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 62207f2d89cSMatthew Wilcox xas_store(&xas, NULL); 623c6dcf52cSJan Kara mapping->nrexceptional--; 624c6dcf52cSJan Kara ret = 1; 625c6dcf52cSJan Kara out: 62607f2d89cSMatthew Wilcox put_unlocked_entry(&xas, entry); 62707f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 628c6dcf52cSJan Kara return ret; 629c6dcf52cSJan Kara } 63007f2d89cSMatthew Wilcox 631ac401cc7SJan Kara /* 6323159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6333159f943SMatthew Wilcox * to be unlocked before deleting it. 634ac401cc7SJan Kara */ 635ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 636ac401cc7SJan Kara { 637a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 638ac401cc7SJan Kara 639ac401cc7SJan Kara /* 640ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 641ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 642a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6433159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 644ac401cc7SJan Kara * at that index as well... 645ac401cc7SJan Kara */ 646c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 647c6dcf52cSJan Kara return ret; 648ac401cc7SJan Kara } 649ac401cc7SJan Kara 650c6dcf52cSJan Kara /* 6513159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 652c6dcf52cSJan Kara */ 653c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 654c6dcf52cSJan Kara pgoff_t index) 655c6dcf52cSJan Kara { 656a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 657ac401cc7SJan Kara } 658ac401cc7SJan Kara 659cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, 660cccbce67SDan Williams sector_t sector, size_t size, struct page *to, 661cccbce67SDan Williams unsigned long vaddr) 662f7ca90b1SMatthew Wilcox { 663cccbce67SDan Williams void *vto, *kaddr; 664cccbce67SDan Williams pgoff_t pgoff; 665cccbce67SDan Williams long rc; 666cccbce67SDan Williams int id; 667e2e05394SRoss Zwisler 668cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 669cccbce67SDan Williams if (rc) 670cccbce67SDan Williams return rc; 671cccbce67SDan Williams 672cccbce67SDan Williams id = dax_read_lock(); 67386ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL); 674cccbce67SDan Williams if (rc < 0) { 675cccbce67SDan Williams dax_read_unlock(id); 676cccbce67SDan Williams return rc; 677cccbce67SDan Williams } 678f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 679cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 680f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 681cccbce67SDan Williams dax_read_unlock(id); 682f7ca90b1SMatthew Wilcox return 0; 683f7ca90b1SMatthew Wilcox } 684f7ca90b1SMatthew Wilcox 685642261acSRoss Zwisler /* 686642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 687642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 688642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 689642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 690642261acSRoss Zwisler * appropriate. 691642261acSRoss Zwisler */ 692b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 693b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 694b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 6959973c98eSRoss Zwisler { 696b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 6979973c98eSRoss Zwisler 698f5b7b748SJan Kara if (dirty) 6999973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7009973c98eSRoss Zwisler 7013159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 702b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 70391d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 70491d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 705977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 706977fbdcdSMatthew Wilcox PG_PMD_NR, false); 70791d25ba8SRoss Zwisler else /* pte entry */ 708b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 709ac401cc7SJan Kara } 7109973c98eSRoss Zwisler 711b15cd800SMatthew Wilcox xas_reset(xas); 712b15cd800SMatthew Wilcox xas_lock_irq(xas); 713d2c997c0SDan Williams if (dax_entry_size(entry) != dax_entry_size(new_entry)) { 714d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 71573449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 716d2c997c0SDan Williams } 717642261acSRoss Zwisler 71891d25ba8SRoss Zwisler if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 719642261acSRoss Zwisler /* 720a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 721642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 722a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 723642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 724642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 725642261acSRoss Zwisler * tree and dirty it if necessary. 726642261acSRoss Zwisler */ 727b15cd800SMatthew Wilcox void *old = dax_lock_entry(xas, new_entry); 728b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 729b15cd800SMatthew Wilcox DAX_LOCKED)); 73091d25ba8SRoss Zwisler entry = new_entry; 731b15cd800SMatthew Wilcox } else { 732b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 733ac401cc7SJan Kara } 73491d25ba8SRoss Zwisler 735f5b7b748SJan Kara if (dirty) 736b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 73791d25ba8SRoss Zwisler 738b15cd800SMatthew Wilcox xas_unlock_irq(xas); 73991d25ba8SRoss Zwisler return entry; 7409973c98eSRoss Zwisler } 7419973c98eSRoss Zwisler 742a77d19f4SMatthew Wilcox static inline 743a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7444b4bb46dSJan Kara { 7454b4bb46dSJan Kara unsigned long address; 7464b4bb46dSJan Kara 7474b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7484b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 7494b4bb46dSJan Kara return address; 7504b4bb46dSJan Kara } 7514b4bb46dSJan Kara 7524b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 753a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 754a77d19f4SMatthew Wilcox unsigned long pfn) 7554b4bb46dSJan Kara { 7564b4bb46dSJan Kara struct vm_area_struct *vma; 757f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 758f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 7594b4bb46dSJan Kara spinlock_t *ptl; 7604b4bb46dSJan Kara 7614b4bb46dSJan Kara i_mmap_lock_read(mapping); 7624b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 763a4d1a885SJérôme Glisse unsigned long address, start, end; 7644b4bb46dSJan Kara 7654b4bb46dSJan Kara cond_resched(); 7664b4bb46dSJan Kara 7674b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 7684b4bb46dSJan Kara continue; 7694b4bb46dSJan Kara 7704b4bb46dSJan Kara address = pgoff_address(index, vma); 771a4d1a885SJérôme Glisse 772a4d1a885SJérôme Glisse /* 773a4d1a885SJérôme Glisse * Note because we provide start/end to follow_pte_pmd it will 774a4d1a885SJérôme Glisse * call mmu_notifier_invalidate_range_start() on our behalf 775a4d1a885SJérôme Glisse * before taking any lock. 776a4d1a885SJérôme Glisse */ 777a4d1a885SJérôme Glisse if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl)) 7784b4bb46dSJan Kara continue; 779f729c8c9SRoss Zwisler 7800f10851eSJérôme Glisse /* 7810f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 7820f10851eSJérôme Glisse * downgrading page table protection not changing it to point 7830f10851eSJérôme Glisse * to a new page. 7840f10851eSJérôme Glisse * 785ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 7860f10851eSJérôme Glisse */ 787f729c8c9SRoss Zwisler if (pmdp) { 788f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 789f729c8c9SRoss Zwisler pmd_t pmd; 790f729c8c9SRoss Zwisler 791f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 792f729c8c9SRoss Zwisler goto unlock_pmd; 793f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 794f729c8c9SRoss Zwisler goto unlock_pmd; 795f729c8c9SRoss Zwisler 796f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 797f729c8c9SRoss Zwisler pmd = pmdp_huge_clear_flush(vma, address, pmdp); 798f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 799f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 800f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 801f729c8c9SRoss Zwisler unlock_pmd: 802f729c8c9SRoss Zwisler #endif 803ee190ca6SJan H. Schönherr spin_unlock(ptl); 804f729c8c9SRoss Zwisler } else { 8054b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 806f729c8c9SRoss Zwisler goto unlock_pte; 8074b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 808f729c8c9SRoss Zwisler goto unlock_pte; 8094b4bb46dSJan Kara 8104b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8114b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8124b4bb46dSJan Kara pte = pte_wrprotect(pte); 8134b4bb46dSJan Kara pte = pte_mkclean(pte); 8144b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 815f729c8c9SRoss Zwisler unlock_pte: 8164b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 817f729c8c9SRoss Zwisler } 8184b4bb46dSJan Kara 819a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(vma->vm_mm, start, end); 8204b4bb46dSJan Kara } 8214b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8224b4bb46dSJan Kara } 8234b4bb46dSJan Kara 8249fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8259fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8269973c98eSRoss Zwisler { 8273fe0791cSDan Williams unsigned long pfn; 8283fe0791cSDan Williams long ret = 0; 829cccbce67SDan Williams size_t size; 8309973c98eSRoss Zwisler 8319973c98eSRoss Zwisler /* 832a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 833a6abc2c0SJan Kara * wrong. 8349973c98eSRoss Zwisler */ 8353159f943SMatthew Wilcox if (WARN_ON(!xa_is_value(entry))) 836a6abc2c0SJan Kara return -EIO; 8379973c98eSRoss Zwisler 8389fc747f6SMatthew Wilcox if (unlikely(dax_is_locked(entry))) { 8399fc747f6SMatthew Wilcox void *old_entry = entry; 8409fc747f6SMatthew Wilcox 8419fc747f6SMatthew Wilcox entry = get_unlocked_entry(xas); 8429fc747f6SMatthew Wilcox 843a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 8449fc747f6SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 845a6abc2c0SJan Kara goto put_unlocked; 846a6abc2c0SJan Kara /* 8479fc747f6SMatthew Wilcox * Entry got reallocated elsewhere? No need to writeback. 8489fc747f6SMatthew Wilcox * We have to compare pfns as we must not bail out due to 8499fc747f6SMatthew Wilcox * difference in lockbit or entry type. 850a6abc2c0SJan Kara */ 8519fc747f6SMatthew Wilcox if (dax_to_pfn(old_entry) != dax_to_pfn(entry)) 852a6abc2c0SJan Kara goto put_unlocked; 853642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 854642261acSRoss Zwisler dax_is_zero_entry(entry))) { 8559973c98eSRoss Zwisler ret = -EIO; 856a6abc2c0SJan Kara goto put_unlocked; 8579973c98eSRoss Zwisler } 8589973c98eSRoss Zwisler 8599fc747f6SMatthew Wilcox /* Another fsync thread may have already done this entry */ 8609fc747f6SMatthew Wilcox if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE)) 861a6abc2c0SJan Kara goto put_unlocked; 8629fc747f6SMatthew Wilcox } 8639fc747f6SMatthew Wilcox 864a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 8659fc747f6SMatthew Wilcox dax_lock_entry(xas, entry); 8669fc747f6SMatthew Wilcox 867a6abc2c0SJan Kara /* 868a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 869a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 870a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 871b93b0163SMatthew Wilcox * at the entry only under the i_pages lock and once they do that 872b93b0163SMatthew Wilcox * they will see the entry locked and wait for it to unlock. 873a6abc2c0SJan Kara */ 8749fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE); 8759fc747f6SMatthew Wilcox xas_unlock_irq(xas); 876a6abc2c0SJan Kara 877642261acSRoss Zwisler /* 878642261acSRoss Zwisler * Even if dax_writeback_mapping_range() was given a wbc->range_start 879642261acSRoss Zwisler * in the middle of a PMD, the 'index' we are given will be aligned to 8803fe0791cSDan Williams * the start index of the PMD, as will the pfn we pull from 'entry'. 8813fe0791cSDan Williams * This allows us to flush for PMD_SIZE and not have to worry about 8823fe0791cSDan Williams * partial PMD writebacks. 883642261acSRoss Zwisler */ 884a77d19f4SMatthew Wilcox pfn = dax_to_pfn(entry); 885a77d19f4SMatthew Wilcox size = PAGE_SIZE << dax_entry_order(entry); 886cccbce67SDan Williams 8879fc747f6SMatthew Wilcox dax_entry_mkclean(mapping, xas->xa_index, pfn); 8883fe0791cSDan Williams dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size); 8894b4bb46dSJan Kara /* 8904b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 8914b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 8924b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 8934b4bb46dSJan Kara * entry lock. 8944b4bb46dSJan Kara */ 8959fc747f6SMatthew Wilcox xas_reset(xas); 8969fc747f6SMatthew Wilcox xas_lock_irq(xas); 8979fc747f6SMatthew Wilcox xas_store(xas, entry); 8989fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 8999fc747f6SMatthew Wilcox dax_wake_entry(xas, entry, false); 9009fc747f6SMatthew Wilcox 9019fc747f6SMatthew Wilcox trace_dax_writeback_one(mapping->host, xas->xa_index, 9029fc747f6SMatthew Wilcox size >> PAGE_SHIFT); 9039973c98eSRoss Zwisler return ret; 9049973c98eSRoss Zwisler 905a6abc2c0SJan Kara put_unlocked: 9069fc747f6SMatthew Wilcox put_unlocked_entry(xas, entry); 9079973c98eSRoss Zwisler return ret; 9089973c98eSRoss Zwisler } 9099973c98eSRoss Zwisler 9109973c98eSRoss Zwisler /* 9119973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 9129973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 9139973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 9149973c98eSRoss Zwisler */ 9157f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 9167f6d5b52SRoss Zwisler struct block_device *bdev, struct writeback_control *wbc) 9179973c98eSRoss Zwisler { 9189fc747f6SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT); 9199973c98eSRoss Zwisler struct inode *inode = mapping->host; 9209fc747f6SMatthew Wilcox pgoff_t end_index = wbc->range_end >> PAGE_SHIFT; 921cccbce67SDan Williams struct dax_device *dax_dev; 9229fc747f6SMatthew Wilcox void *entry; 9239fc747f6SMatthew Wilcox int ret = 0; 9249fc747f6SMatthew Wilcox unsigned int scanned = 0; 9259973c98eSRoss Zwisler 9269973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 9279973c98eSRoss Zwisler return -EIO; 9289973c98eSRoss Zwisler 9297f6d5b52SRoss Zwisler if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) 9307f6d5b52SRoss Zwisler return 0; 9317f6d5b52SRoss Zwisler 932cccbce67SDan Williams dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 933cccbce67SDan Williams if (!dax_dev) 934cccbce67SDan Williams return -EIO; 935cccbce67SDan Williams 9369fc747f6SMatthew Wilcox trace_dax_writeback_range(inode, xas.xa_index, end_index); 9379973c98eSRoss Zwisler 9389fc747f6SMatthew Wilcox tag_pages_for_writeback(mapping, xas.xa_index, end_index); 939d14a3f48SRoss Zwisler 9409fc747f6SMatthew Wilcox xas_lock_irq(&xas); 9419fc747f6SMatthew Wilcox xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) { 9429fc747f6SMatthew Wilcox ret = dax_writeback_one(&xas, dax_dev, mapping, entry); 943819ec6b9SJeff Layton if (ret < 0) { 944819ec6b9SJeff Layton mapping_set_error(mapping, ret); 9459fc747f6SMatthew Wilcox break; 946d14a3f48SRoss Zwisler } 9479fc747f6SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 9489fc747f6SMatthew Wilcox continue; 9499fc747f6SMatthew Wilcox 9509fc747f6SMatthew Wilcox xas_pause(&xas); 9519fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 9529fc747f6SMatthew Wilcox cond_resched(); 9539fc747f6SMatthew Wilcox xas_lock_irq(&xas); 954d14a3f48SRoss Zwisler } 9559fc747f6SMatthew Wilcox xas_unlock_irq(&xas); 956cccbce67SDan Williams put_dax(dax_dev); 9579fc747f6SMatthew Wilcox trace_dax_writeback_range_done(inode, xas.xa_index, end_index); 9589fc747f6SMatthew Wilcox return ret; 9599973c98eSRoss Zwisler } 9609973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 9619973c98eSRoss Zwisler 96231a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 963f7ca90b1SMatthew Wilcox { 964a3841f94SLinus Torvalds return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; 96531a6f1a6SJan Kara } 966f7ca90b1SMatthew Wilcox 9675e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, 9685e161e40SJan Kara pfn_t *pfnp) 9695e161e40SJan Kara { 9705e161e40SJan Kara const sector_t sector = dax_iomap_sector(iomap, pos); 9715e161e40SJan Kara pgoff_t pgoff; 9725e161e40SJan Kara int id, rc; 9735e161e40SJan Kara long length; 9745e161e40SJan Kara 9755e161e40SJan Kara rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); 976cccbce67SDan Williams if (rc) 977cccbce67SDan Williams return rc; 978cccbce67SDan Williams id = dax_read_lock(); 9795e161e40SJan Kara length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), 98086ed913bSHuaisheng Ye NULL, pfnp); 9815e161e40SJan Kara if (length < 0) { 9825e161e40SJan Kara rc = length; 9835e161e40SJan Kara goto out; 9845e161e40SJan Kara } 9855e161e40SJan Kara rc = -EINVAL; 9865e161e40SJan Kara if (PFN_PHYS(length) < size) 9875e161e40SJan Kara goto out; 9885e161e40SJan Kara if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) 9895e161e40SJan Kara goto out; 9905e161e40SJan Kara /* For larger pages we need devmap */ 9915e161e40SJan Kara if (length > 1 && !pfn_t_devmap(*pfnp)) 9925e161e40SJan Kara goto out; 9935e161e40SJan Kara rc = 0; 9945e161e40SJan Kara out: 995cccbce67SDan Williams dax_read_unlock(id); 996cccbce67SDan Williams return rc; 997cccbce67SDan Williams } 998f7ca90b1SMatthew Wilcox 9992f89dc12SJan Kara /* 100091d25ba8SRoss Zwisler * The user has performed a load from a hole in the file. Allocating a new 100191d25ba8SRoss Zwisler * page in the file would cause excessive storage usage for workloads with 100291d25ba8SRoss Zwisler * sparse files. Instead we insert a read-only mapping of the 4k zero page. 100391d25ba8SRoss Zwisler * If this page is ever written to we will re-fault and change the mapping to 100491d25ba8SRoss Zwisler * point to real DAX storage instead. 10052f89dc12SJan Kara */ 1006b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas, 1007b15cd800SMatthew Wilcox struct address_space *mapping, void **entry, 1008e30331ffSRoss Zwisler struct vm_fault *vmf) 1009e30331ffSRoss Zwisler { 1010e30331ffSRoss Zwisler struct inode *inode = mapping->host; 101191d25ba8SRoss Zwisler unsigned long vaddr = vmf->address; 1012b90ca5ccSMatthew Wilcox pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr)); 1013b90ca5ccSMatthew Wilcox vm_fault_t ret; 1014e30331ffSRoss Zwisler 1015b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 10163159f943SMatthew Wilcox DAX_ZERO_PAGE, false); 10173159f943SMatthew Wilcox 1018ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vmf->vma, vaddr, pfn); 1019e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 1020e30331ffSRoss Zwisler return ret; 1021e30331ffSRoss Zwisler } 1022e30331ffSRoss Zwisler 10234b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev, 10244b0228faSVishal Verma unsigned int offset, unsigned int length) 10254b0228faSVishal Verma { 10264b0228faSVishal Verma unsigned short sector_size = bdev_logical_block_size(bdev); 10274b0228faSVishal Verma 10284b0228faSVishal Verma if (!IS_ALIGNED(offset, sector_size)) 10294b0228faSVishal Verma return false; 10304b0228faSVishal Verma if (!IS_ALIGNED(length, sector_size)) 10314b0228faSVishal Verma return false; 10324b0228faSVishal Verma 10334b0228faSVishal Verma return true; 10344b0228faSVishal Verma } 10354b0228faSVishal Verma 1036cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev, 1037cccbce67SDan Williams struct dax_device *dax_dev, sector_t sector, 1038cccbce67SDan Williams unsigned int offset, unsigned int size) 1039679c8bd3SChristoph Hellwig { 1040cccbce67SDan Williams if (dax_range_is_aligned(bdev, offset, size)) { 1041cccbce67SDan Williams sector_t start_sector = sector + (offset >> 9); 10424b0228faSVishal Verma 10434b0228faSVishal Verma return blkdev_issue_zeroout(bdev, start_sector, 104453ef7d0eSLinus Torvalds size >> 9, GFP_NOFS, 0); 10454b0228faSVishal Verma } else { 1046cccbce67SDan Williams pgoff_t pgoff; 1047cccbce67SDan Williams long rc, id; 1048cccbce67SDan Williams void *kaddr; 1049cccbce67SDan Williams 1050e84b83b9SDan Williams rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 1051cccbce67SDan Williams if (rc) 1052cccbce67SDan Williams return rc; 1053cccbce67SDan Williams 1054cccbce67SDan Williams id = dax_read_lock(); 105586ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL); 1056cccbce67SDan Williams if (rc < 0) { 1057cccbce67SDan Williams dax_read_unlock(id); 1058cccbce67SDan Williams return rc; 1059cccbce67SDan Williams } 106081f55870SDan Williams memset(kaddr + offset, 0, size); 1061c3ca015fSMikulas Patocka dax_flush(dax_dev, kaddr + offset, size); 1062cccbce67SDan Williams dax_read_unlock(id); 10634b0228faSVishal Verma } 1064679c8bd3SChristoph Hellwig return 0; 1065679c8bd3SChristoph Hellwig } 1066679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range); 1067679c8bd3SChristoph Hellwig 1068a254e568SChristoph Hellwig static loff_t 106911c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 1070a254e568SChristoph Hellwig struct iomap *iomap) 1071a254e568SChristoph Hellwig { 1072cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1073cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1074a254e568SChristoph Hellwig struct iov_iter *iter = data; 1075a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1076a254e568SChristoph Hellwig ssize_t ret = 0; 1077a77d4786SDan Williams size_t xfer; 1078cccbce67SDan Williams int id; 1079a254e568SChristoph Hellwig 1080a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1081a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1082a254e568SChristoph Hellwig if (pos >= end) 1083a254e568SChristoph Hellwig return 0; 1084a254e568SChristoph Hellwig 1085a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1086a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1087a254e568SChristoph Hellwig } 1088a254e568SChristoph Hellwig 1089a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1090a254e568SChristoph Hellwig return -EIO; 1091a254e568SChristoph Hellwig 1092e3fce68cSJan Kara /* 1093e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1094e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1095e3fce68cSJan Kara * written by write(2) is visible in mmap. 1096e3fce68cSJan Kara */ 1097cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1098e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1099e3fce68cSJan Kara pos >> PAGE_SHIFT, 1100e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1101e3fce68cSJan Kara } 1102e3fce68cSJan Kara 1103cccbce67SDan Williams id = dax_read_lock(); 1104a254e568SChristoph Hellwig while (pos < end) { 1105a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1106cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1107cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1108a254e568SChristoph Hellwig ssize_t map_len; 1109cccbce67SDan Williams pgoff_t pgoff; 1110cccbce67SDan Williams void *kaddr; 1111a254e568SChristoph Hellwig 1112d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1113d1908f52SMichal Hocko ret = -EINTR; 1114d1908f52SMichal Hocko break; 1115d1908f52SMichal Hocko } 1116d1908f52SMichal Hocko 1117cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1118cccbce67SDan Williams if (ret) 1119cccbce67SDan Williams break; 1120cccbce67SDan Williams 1121cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 112286ed913bSHuaisheng Ye &kaddr, NULL); 1123a254e568SChristoph Hellwig if (map_len < 0) { 1124a254e568SChristoph Hellwig ret = map_len; 1125a254e568SChristoph Hellwig break; 1126a254e568SChristoph Hellwig } 1127a254e568SChristoph Hellwig 1128cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1129cccbce67SDan Williams kaddr += offset; 1130a254e568SChristoph Hellwig map_len -= offset; 1131a254e568SChristoph Hellwig if (map_len > end - pos) 1132a254e568SChristoph Hellwig map_len = end - pos; 1133a254e568SChristoph Hellwig 1134a2e050f5SRoss Zwisler /* 1135a2e050f5SRoss Zwisler * The userspace address for the memory copy has already been 1136a2e050f5SRoss Zwisler * validated via access_ok() in either vfs_read() or 1137a2e050f5SRoss Zwisler * vfs_write(), depending on which operation we are doing. 1138a2e050f5SRoss Zwisler */ 1139a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1140a77d4786SDan Williams xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1141fec53774SDan Williams map_len, iter); 1142a254e568SChristoph Hellwig else 1143a77d4786SDan Williams xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr, 1144b3a9a0c3SDan Williams map_len, iter); 1145a254e568SChristoph Hellwig 1146a77d4786SDan Williams pos += xfer; 1147a77d4786SDan Williams length -= xfer; 1148a77d4786SDan Williams done += xfer; 1149a77d4786SDan Williams 1150a77d4786SDan Williams if (xfer == 0) 1151a77d4786SDan Williams ret = -EFAULT; 1152a77d4786SDan Williams if (xfer < map_len) 1153a77d4786SDan Williams break; 1154a254e568SChristoph Hellwig } 1155cccbce67SDan Williams dax_read_unlock(id); 1156a254e568SChristoph Hellwig 1157a254e568SChristoph Hellwig return done ? done : ret; 1158a254e568SChristoph Hellwig } 1159a254e568SChristoph Hellwig 1160a254e568SChristoph Hellwig /** 116111c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1162a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1163a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1164a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1165a254e568SChristoph Hellwig * 1166a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1167a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1168a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1169a254e568SChristoph Hellwig */ 1170a254e568SChristoph Hellwig ssize_t 117111c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 11728ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1173a254e568SChristoph Hellwig { 1174a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1175a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1176a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1177a254e568SChristoph Hellwig unsigned flags = 0; 1178a254e568SChristoph Hellwig 1179168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 1180168316dbSChristoph Hellwig lockdep_assert_held_exclusive(&inode->i_rwsem); 1181a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1182168316dbSChristoph Hellwig } else { 1183168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1184168316dbSChristoph Hellwig } 1185a254e568SChristoph Hellwig 1186a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1187a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 118811c59c92SRoss Zwisler iter, dax_iomap_actor); 1189a254e568SChristoph Hellwig if (ret <= 0) 1190a254e568SChristoph Hellwig break; 1191a254e568SChristoph Hellwig pos += ret; 1192a254e568SChristoph Hellwig done += ret; 1193a254e568SChristoph Hellwig } 1194a254e568SChristoph Hellwig 1195a254e568SChristoph Hellwig iocb->ki_pos += done; 1196a254e568SChristoph Hellwig return done ? done : ret; 1197a254e568SChristoph Hellwig } 119811c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1199a7d73fe6SChristoph Hellwig 1200ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error) 12019f141d6eSJan Kara { 12029f141d6eSJan Kara if (error == 0) 12039f141d6eSJan Kara return VM_FAULT_NOPAGE; 12049f141d6eSJan Kara if (error == -ENOMEM) 12059f141d6eSJan Kara return VM_FAULT_OOM; 12069f141d6eSJan Kara return VM_FAULT_SIGBUS; 12079f141d6eSJan Kara } 12089f141d6eSJan Kara 1209aaa422c4SDan Williams /* 1210aaa422c4SDan Williams * MAP_SYNC on a dax mapping guarantees dirty metadata is 1211aaa422c4SDan Williams * flushed on write-faults (non-cow), but not read-faults. 1212aaa422c4SDan Williams */ 1213aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags, 1214aaa422c4SDan Williams struct vm_area_struct *vma, struct iomap *iomap) 1215aaa422c4SDan Williams { 1216aaa422c4SDan Williams return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) 1217aaa422c4SDan Williams && (iomap->flags & IOMAP_F_DIRTY); 1218aaa422c4SDan Williams } 1219aaa422c4SDan Williams 1220ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, 1221c0b24625SJan Kara int *iomap_errp, const struct iomap_ops *ops) 1222a7d73fe6SChristoph Hellwig { 1223a0987ad5SJan Kara struct vm_area_struct *vma = vmf->vma; 1224a0987ad5SJan Kara struct address_space *mapping = vma->vm_file->f_mapping; 1225b15cd800SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, vmf->pgoff); 1226a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 12271a29d85eSJan Kara unsigned long vaddr = vmf->address; 1228a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1229a7d73fe6SChristoph Hellwig struct iomap iomap = { 0 }; 12309484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1231a7d73fe6SChristoph Hellwig int error, major = 0; 1232d2c43ef1SJan Kara bool write = vmf->flags & FAULT_FLAG_WRITE; 1233caa51d26SJan Kara bool sync; 1234ab77dab4SSouptick Joarder vm_fault_t ret = 0; 1235a7d73fe6SChristoph Hellwig void *entry; 12361b5a1cb2SJan Kara pfn_t pfn; 1237a7d73fe6SChristoph Hellwig 1238ab77dab4SSouptick Joarder trace_dax_pte_fault(inode, vmf, ret); 1239a7d73fe6SChristoph Hellwig /* 1240a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1241a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1242a7d73fe6SChristoph Hellwig * a reliable test. 1243a7d73fe6SChristoph Hellwig */ 1244a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1245ab77dab4SSouptick Joarder ret = VM_FAULT_SIGBUS; 1246a9c42b33SRoss Zwisler goto out; 1247a9c42b33SRoss Zwisler } 1248a7d73fe6SChristoph Hellwig 1249d2c43ef1SJan Kara if (write && !vmf->cow_page) 1250a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1251a7d73fe6SChristoph Hellwig 1252b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, 0); 1253b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1254b15cd800SMatthew Wilcox ret = xa_to_internal(entry); 125513e451fdSJan Kara goto out; 125613e451fdSJan Kara } 125713e451fdSJan Kara 1258a7d73fe6SChristoph Hellwig /* 1259e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1260e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1261e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1262e2093926SRoss Zwisler * retried. 1263e2093926SRoss Zwisler */ 1264e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1265ab77dab4SSouptick Joarder ret = VM_FAULT_NOPAGE; 1266e2093926SRoss Zwisler goto unlock_entry; 1267e2093926SRoss Zwisler } 1268e2093926SRoss Zwisler 1269e2093926SRoss Zwisler /* 1270a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1271a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1272a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1273a7d73fe6SChristoph Hellwig */ 1274a7d73fe6SChristoph Hellwig error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap); 1275c0b24625SJan Kara if (iomap_errp) 1276c0b24625SJan Kara *iomap_errp = error; 1277a9c42b33SRoss Zwisler if (error) { 1278ab77dab4SSouptick Joarder ret = dax_fault_return(error); 127913e451fdSJan Kara goto unlock_entry; 1280a9c42b33SRoss Zwisler } 1281a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 128213e451fdSJan Kara error = -EIO; /* fs corruption? */ 128313e451fdSJan Kara goto error_finish_iomap; 1284a7d73fe6SChristoph Hellwig } 1285a7d73fe6SChristoph Hellwig 1286a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 128731a6f1a6SJan Kara sector_t sector = dax_iomap_sector(&iomap, pos); 128831a6f1a6SJan Kara 1289a7d73fe6SChristoph Hellwig switch (iomap.type) { 1290a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1291a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1292a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1293a7d73fe6SChristoph Hellwig break; 1294a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1295cccbce67SDan Williams error = copy_user_dax(iomap.bdev, iomap.dax_dev, 1296cccbce67SDan Williams sector, PAGE_SIZE, vmf->cow_page, vaddr); 1297a7d73fe6SChristoph Hellwig break; 1298a7d73fe6SChristoph Hellwig default: 1299a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1300a7d73fe6SChristoph Hellwig error = -EIO; 1301a7d73fe6SChristoph Hellwig break; 1302a7d73fe6SChristoph Hellwig } 1303a7d73fe6SChristoph Hellwig 1304a7d73fe6SChristoph Hellwig if (error) 130513e451fdSJan Kara goto error_finish_iomap; 1306b1aa812bSJan Kara 1307b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1308ab77dab4SSouptick Joarder ret = finish_fault(vmf); 1309ab77dab4SSouptick Joarder if (!ret) 1310ab77dab4SSouptick Joarder ret = VM_FAULT_DONE_COW; 131113e451fdSJan Kara goto finish_iomap; 1312a7d73fe6SChristoph Hellwig } 1313a7d73fe6SChristoph Hellwig 1314aaa422c4SDan Williams sync = dax_fault_is_synchronous(flags, vma, &iomap); 1315caa51d26SJan Kara 1316a7d73fe6SChristoph Hellwig switch (iomap.type) { 1317a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1318a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1319a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 1320a0987ad5SJan Kara count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); 1321a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1322a7d73fe6SChristoph Hellwig } 13231b5a1cb2SJan Kara error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); 13241b5a1cb2SJan Kara if (error < 0) 13251b5a1cb2SJan Kara goto error_finish_iomap; 13261b5a1cb2SJan Kara 1327b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 1328caa51d26SJan Kara 0, write && !sync); 13291b5a1cb2SJan Kara 1330caa51d26SJan Kara /* 1331caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1332caa51d26SJan Kara * we can insert PTE into page tables only after that happens. 1333caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1334caa51d26SJan Kara * insert it after fsync is done. 1335caa51d26SJan Kara */ 1336caa51d26SJan Kara if (sync) { 1337caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) { 1338caa51d26SJan Kara error = -EIO; 1339caa51d26SJan Kara goto error_finish_iomap; 1340caa51d26SJan Kara } 1341caa51d26SJan Kara *pfnp = pfn; 1342ab77dab4SSouptick Joarder ret = VM_FAULT_NEEDDSYNC | major; 1343caa51d26SJan Kara goto finish_iomap; 1344caa51d26SJan Kara } 13451b5a1cb2SJan Kara trace_dax_insert_mapping(inode, vmf, entry); 13461b5a1cb2SJan Kara if (write) 1347ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn); 13481b5a1cb2SJan Kara else 1349ab77dab4SSouptick Joarder ret = vmf_insert_mixed(vma, vaddr, pfn); 13501b5a1cb2SJan Kara 1351ab77dab4SSouptick Joarder goto finish_iomap; 1352a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1353a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1354d2c43ef1SJan Kara if (!write) { 1355b15cd800SMatthew Wilcox ret = dax_load_hole(&xas, mapping, &entry, vmf); 135613e451fdSJan Kara goto finish_iomap; 13571550290bSRoss Zwisler } 1358a7d73fe6SChristoph Hellwig /*FALLTHRU*/ 1359a7d73fe6SChristoph Hellwig default: 1360a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1361a7d73fe6SChristoph Hellwig error = -EIO; 1362a7d73fe6SChristoph Hellwig break; 1363a7d73fe6SChristoph Hellwig } 1364a7d73fe6SChristoph Hellwig 136513e451fdSJan Kara error_finish_iomap: 1366ab77dab4SSouptick Joarder ret = dax_fault_return(error); 13679f141d6eSJan Kara finish_iomap: 13689f141d6eSJan Kara if (ops->iomap_end) { 13699f141d6eSJan Kara int copied = PAGE_SIZE; 13709f141d6eSJan Kara 1371ab77dab4SSouptick Joarder if (ret & VM_FAULT_ERROR) 13729f141d6eSJan Kara copied = 0; 13739f141d6eSJan Kara /* 13749f141d6eSJan Kara * The fault is done by now and there's no way back (other 13759f141d6eSJan Kara * thread may be already happily using PTE we have installed). 13769f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 13779f141d6eSJan Kara * with it. 13789f141d6eSJan Kara */ 13799f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 13801550290bSRoss Zwisler } 138113e451fdSJan Kara unlock_entry: 1382b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1383a9c42b33SRoss Zwisler out: 1384ab77dab4SSouptick Joarder trace_dax_pte_fault_done(inode, vmf, ret); 1385ab77dab4SSouptick Joarder return ret | major; 1386a7d73fe6SChristoph Hellwig } 1387642261acSRoss Zwisler 1388642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1389b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf, 1390b15cd800SMatthew Wilcox struct iomap *iomap, void **entry) 1391642261acSRoss Zwisler { 1392f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1393f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1394653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 1395642261acSRoss Zwisler struct page *zero_page; 1396642261acSRoss Zwisler spinlock_t *ptl; 1397642261acSRoss Zwisler pmd_t pmd_entry; 13983fe0791cSDan Williams pfn_t pfn; 1399642261acSRoss Zwisler 1400f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1401642261acSRoss Zwisler 1402642261acSRoss Zwisler if (unlikely(!zero_page)) 1403653b2ea3SRoss Zwisler goto fallback; 1404642261acSRoss Zwisler 14053fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1406b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14073159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1408642261acSRoss Zwisler 1409f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1410f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1411642261acSRoss Zwisler spin_unlock(ptl); 1412653b2ea3SRoss Zwisler goto fallback; 1413642261acSRoss Zwisler } 1414642261acSRoss Zwisler 1415f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1416642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1417f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1418642261acSRoss Zwisler spin_unlock(ptl); 1419b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1420642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1421653b2ea3SRoss Zwisler 1422653b2ea3SRoss Zwisler fallback: 1423b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1424642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1425642261acSRoss Zwisler } 1426642261acSRoss Zwisler 1427ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1428a2d58167SDave Jiang const struct iomap_ops *ops) 1429642261acSRoss Zwisler { 1430f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1431642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1432b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1433d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1434d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1435caa51d26SJan Kara bool sync; 14369484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1437642261acSRoss Zwisler struct inode *inode = mapping->host; 1438ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1439642261acSRoss Zwisler struct iomap iomap = { 0 }; 1440b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1441642261acSRoss Zwisler void *entry; 1442642261acSRoss Zwisler loff_t pos; 1443642261acSRoss Zwisler int error; 1444302a5e31SJan Kara pfn_t pfn; 1445642261acSRoss Zwisler 1446282a8e03SRoss Zwisler /* 1447282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1448282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1449282a8e03SRoss Zwisler * this is a reliable test. 1450282a8e03SRoss Zwisler */ 1451957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1452282a8e03SRoss Zwisler 1453f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1454282a8e03SRoss Zwisler 1455fffa281bSRoss Zwisler /* 1456fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1457fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1458fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1459a77d19f4SMatthew Wilcox * range in the page cache. 1460fffa281bSRoss Zwisler */ 1461fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1462fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1463fffa281bSRoss Zwisler goto fallback; 1464fffa281bSRoss Zwisler 1465642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1466642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1467642261acSRoss Zwisler goto fallback; 1468642261acSRoss Zwisler 1469642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1470642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1471642261acSRoss Zwisler goto fallback; 1472642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1473642261acSRoss Zwisler goto fallback; 1474642261acSRoss Zwisler 1475b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1476282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1477282a8e03SRoss Zwisler goto out; 1478282a8e03SRoss Zwisler } 1479642261acSRoss Zwisler 1480642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1481b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1482642261acSRoss Zwisler goto fallback; 1483642261acSRoss Zwisler 1484642261acSRoss Zwisler /* 1485b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1486b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1487b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1488b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 14899f141d6eSJan Kara */ 1490b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, DAX_PMD); 1491b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1492b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1493876f2946SRoss Zwisler goto fallback; 1494b15cd800SMatthew Wilcox } 1495876f2946SRoss Zwisler 1496876f2946SRoss Zwisler /* 1497e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1498e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1499e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1500e2093926SRoss Zwisler * retried. 1501e2093926SRoss Zwisler */ 1502e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1503e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1504e2093926SRoss Zwisler result = 0; 1505e2093926SRoss Zwisler goto unlock_entry; 1506e2093926SRoss Zwisler } 1507e2093926SRoss Zwisler 1508e2093926SRoss Zwisler /* 1509876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1510876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1511876f2946SRoss Zwisler * to look up our filesystem block. 1512876f2946SRoss Zwisler */ 1513b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1514876f2946SRoss Zwisler error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); 1515876f2946SRoss Zwisler if (error) 1516876f2946SRoss Zwisler goto unlock_entry; 1517876f2946SRoss Zwisler 1518876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15199f141d6eSJan Kara goto finish_iomap; 15209f141d6eSJan Kara 1521aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1522caa51d26SJan Kara 1523642261acSRoss Zwisler switch (iomap.type) { 1524642261acSRoss Zwisler case IOMAP_MAPPED: 1525302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1526302a5e31SJan Kara if (error < 0) 1527302a5e31SJan Kara goto finish_iomap; 1528302a5e31SJan Kara 1529b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15303159f943SMatthew Wilcox DAX_PMD, write && !sync); 1531302a5e31SJan Kara 1532caa51d26SJan Kara /* 1533caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1534caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1535caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1536caa51d26SJan Kara * insert it after fsync is done. 1537caa51d26SJan Kara */ 1538caa51d26SJan Kara if (sync) { 1539caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1540caa51d26SJan Kara goto finish_iomap; 1541caa51d26SJan Kara *pfnp = pfn; 1542caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1543caa51d26SJan Kara goto finish_iomap; 1544caa51d26SJan Kara } 1545caa51d26SJan Kara 1546302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1547302a5e31SJan Kara result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn, 1548302a5e31SJan Kara write); 1549642261acSRoss Zwisler break; 1550642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1551642261acSRoss Zwisler case IOMAP_HOLE: 1552642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1553876f2946SRoss Zwisler break; 1554b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1555642261acSRoss Zwisler break; 1556642261acSRoss Zwisler default: 1557642261acSRoss Zwisler WARN_ON_ONCE(1); 1558642261acSRoss Zwisler break; 1559642261acSRoss Zwisler } 1560642261acSRoss Zwisler 15619f141d6eSJan Kara finish_iomap: 15629f141d6eSJan Kara if (ops->iomap_end) { 15639f141d6eSJan Kara int copied = PMD_SIZE; 15649f141d6eSJan Kara 15659f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 15669f141d6eSJan Kara copied = 0; 15679f141d6eSJan Kara /* 15689f141d6eSJan Kara * The fault is done by now and there's no way back (other 15699f141d6eSJan Kara * thread may be already happily using PMD we have installed). 15709f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 15719f141d6eSJan Kara * with it. 15729f141d6eSJan Kara */ 15739f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 15749f141d6eSJan Kara &iomap); 15759f141d6eSJan Kara } 1576876f2946SRoss Zwisler unlock_entry: 1577b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1578642261acSRoss Zwisler fallback: 1579642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1580d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1581642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1582642261acSRoss Zwisler } 1583282a8e03SRoss Zwisler out: 1584f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1585642261acSRoss Zwisler return result; 1586642261acSRoss Zwisler } 1587a2d58167SDave Jiang #else 1588ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 158901cddfe9SArnd Bergmann const struct iomap_ops *ops) 1590a2d58167SDave Jiang { 1591a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1592a2d58167SDave Jiang } 1593642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1594a2d58167SDave Jiang 1595a2d58167SDave Jiang /** 1596a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1597a2d58167SDave Jiang * @vmf: The description of the fault 1598cec04e8cSJan Kara * @pe_size: Size of the page to fault in 15999a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1600c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1601cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1602a2d58167SDave Jiang * 1603a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1604a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1605a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1606a2d58167SDave Jiang * successfully. 1607a2d58167SDave Jiang */ 1608ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1609c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1610a2d58167SDave Jiang { 1611c791ace1SDave Jiang switch (pe_size) { 1612c791ace1SDave Jiang case PE_SIZE_PTE: 1613c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1614c791ace1SDave Jiang case PE_SIZE_PMD: 16159a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1616a2d58167SDave Jiang default: 1617a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1618a2d58167SDave Jiang } 1619a2d58167SDave Jiang } 1620a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 162171eab6dfSJan Kara 1622a77d19f4SMatthew Wilcox /* 162371eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 162471eab6dfSJan Kara * @vmf: The description of the fault 162571eab6dfSJan Kara * @pfn: PFN to insert 1626cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 162771eab6dfSJan Kara * 1628a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1629a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 163071eab6dfSJan Kara */ 1631cfc93c6cSMatthew Wilcox static vm_fault_t 1632cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 163371eab6dfSJan Kara { 163471eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1635cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1636cfc93c6cSMatthew Wilcox void *entry; 1637ab77dab4SSouptick Joarder vm_fault_t ret; 163871eab6dfSJan Kara 1639cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 1640cfc93c6cSMatthew Wilcox entry = get_unlocked_entry(&xas); 164171eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 164271eab6dfSJan Kara if (!entry || 1643cfc93c6cSMatthew Wilcox (order == 0 && !dax_is_pte_entry(entry)) || 1644cfc93c6cSMatthew Wilcox (order == PMD_ORDER && (xa_is_internal(entry) || 1645cfc93c6cSMatthew Wilcox !dax_is_pmd_entry(entry)))) { 1646cfc93c6cSMatthew Wilcox put_unlocked_entry(&xas, entry); 1647cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 164871eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 164971eab6dfSJan Kara VM_FAULT_NOPAGE); 165071eab6dfSJan Kara return VM_FAULT_NOPAGE; 165171eab6dfSJan Kara } 1652cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1653cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1654cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1655cfc93c6cSMatthew Wilcox if (order == 0) 1656ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 165771eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1658cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1659ab77dab4SSouptick Joarder ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, 166071eab6dfSJan Kara pfn, true); 166171eab6dfSJan Kara #endif 1662cfc93c6cSMatthew Wilcox else 1663ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1664cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1665ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1666ab77dab4SSouptick Joarder return ret; 166771eab6dfSJan Kara } 166871eab6dfSJan Kara 166971eab6dfSJan Kara /** 167071eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 167171eab6dfSJan Kara * @vmf: The description of the fault 167271eab6dfSJan Kara * @pe_size: Size of entry to be inserted 167371eab6dfSJan Kara * @pfn: PFN to insert 167471eab6dfSJan Kara * 167571eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 167671eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 167771eab6dfSJan Kara * table entry. 167871eab6dfSJan Kara */ 1679ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1680ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 168171eab6dfSJan Kara { 168271eab6dfSJan Kara int err; 168371eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1684cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1685cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 168671eab6dfSJan Kara 168771eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 168871eab6dfSJan Kara if (err) 168971eab6dfSJan Kara return VM_FAULT_SIGBUS; 1690cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 169171eab6dfSJan Kara } 169271eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1693