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> 3611cf9d86SAneesh Kumar K.V #include <asm/pgalloc.h> 37a254e568SChristoph Hellwig #include "internal.h" 38d475c634SMatthew Wilcox 39282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS 40282a8e03SRoss Zwisler #include <trace/events/fs_dax.h> 41282a8e03SRoss Zwisler 42cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size) 43cfc93c6cSMatthew Wilcox { 44cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PTE) 45cfc93c6cSMatthew Wilcox return PAGE_SHIFT - PAGE_SHIFT; 46cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PMD) 47cfc93c6cSMatthew Wilcox return PMD_SHIFT - PAGE_SHIFT; 48cfc93c6cSMatthew Wilcox if (pe_size == PE_SIZE_PUD) 49cfc93c6cSMatthew Wilcox return PUD_SHIFT - PAGE_SHIFT; 50cfc93c6cSMatthew Wilcox return ~0; 51cfc93c6cSMatthew Wilcox } 52cfc93c6cSMatthew Wilcox 53ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */ 54ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12 55ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 56ac401cc7SJan Kara 57917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset. */ 58917f3452SRoss Zwisler #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 59977fbdcdSMatthew Wilcox #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) 60917f3452SRoss Zwisler 61cfc93c6cSMatthew Wilcox /* The order of a PMD entry */ 62cfc93c6cSMatthew Wilcox #define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) 63cfc93c6cSMatthew Wilcox 64ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 65ac401cc7SJan Kara 66ac401cc7SJan Kara static int __init init_dax_wait_table(void) 67ac401cc7SJan Kara { 68ac401cc7SJan Kara int i; 69ac401cc7SJan Kara 70ac401cc7SJan Kara for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 71ac401cc7SJan Kara init_waitqueue_head(wait_table + i); 72ac401cc7SJan Kara return 0; 73ac401cc7SJan Kara } 74ac401cc7SJan Kara fs_initcall(init_dax_wait_table); 75ac401cc7SJan Kara 76527b19d0SRoss Zwisler /* 773159f943SMatthew Wilcox * DAX pagecache entries use XArray value entries so they can't be mistaken 783159f943SMatthew Wilcox * for pages. We use one bit for locking, one bit for the entry size (PMD) 793159f943SMatthew Wilcox * and two more to tell us if the entry is a zero page or an empty entry that 803159f943SMatthew Wilcox * is just used for locking. In total four special bits. 81527b19d0SRoss Zwisler * 82527b19d0SRoss Zwisler * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE 83527b19d0SRoss Zwisler * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem 84527b19d0SRoss Zwisler * block allocation. 85527b19d0SRoss Zwisler */ 863159f943SMatthew Wilcox #define DAX_SHIFT (4) 873159f943SMatthew Wilcox #define DAX_LOCKED (1UL << 0) 883159f943SMatthew Wilcox #define DAX_PMD (1UL << 1) 893159f943SMatthew Wilcox #define DAX_ZERO_PAGE (1UL << 2) 903159f943SMatthew Wilcox #define DAX_EMPTY (1UL << 3) 91527b19d0SRoss Zwisler 92a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry) 93527b19d0SRoss Zwisler { 943159f943SMatthew Wilcox return xa_to_value(entry) >> DAX_SHIFT; 95527b19d0SRoss Zwisler } 96527b19d0SRoss Zwisler 979f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags) 989f32d221SMatthew Wilcox { 999f32d221SMatthew Wilcox return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT)); 1009f32d221SMatthew Wilcox } 1019f32d221SMatthew Wilcox 102cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry) 103cfc93c6cSMatthew Wilcox { 104cfc93c6cSMatthew Wilcox return xa_to_value(entry) & DAX_LOCKED; 105cfc93c6cSMatthew Wilcox } 106cfc93c6cSMatthew Wilcox 107a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry) 108527b19d0SRoss Zwisler { 1093159f943SMatthew Wilcox if (xa_to_value(entry) & DAX_PMD) 110cfc93c6cSMatthew Wilcox return PMD_ORDER; 111527b19d0SRoss Zwisler return 0; 112527b19d0SRoss Zwisler } 113527b19d0SRoss Zwisler 114fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry) 115642261acSRoss Zwisler { 1163159f943SMatthew Wilcox return xa_to_value(entry) & DAX_PMD; 117642261acSRoss Zwisler } 118642261acSRoss Zwisler 119fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry) 120642261acSRoss Zwisler { 1213159f943SMatthew Wilcox return !(xa_to_value(entry) & DAX_PMD); 122642261acSRoss Zwisler } 123642261acSRoss Zwisler 124642261acSRoss Zwisler static int dax_is_zero_entry(void *entry) 125642261acSRoss Zwisler { 1263159f943SMatthew Wilcox return xa_to_value(entry) & DAX_ZERO_PAGE; 127642261acSRoss Zwisler } 128642261acSRoss Zwisler 129642261acSRoss Zwisler static int dax_is_empty_entry(void *entry) 130642261acSRoss Zwisler { 1313159f943SMatthew Wilcox return xa_to_value(entry) & DAX_EMPTY; 132642261acSRoss Zwisler } 133642261acSRoss Zwisler 134f7ca90b1SMatthew Wilcox /* 135a77d19f4SMatthew Wilcox * DAX page cache entry locking 136ac401cc7SJan Kara */ 137ac401cc7SJan Kara struct exceptional_entry_key { 138ec4907ffSMatthew Wilcox struct xarray *xa; 13963e95b5cSRoss Zwisler pgoff_t entry_start; 140ac401cc7SJan Kara }; 141ac401cc7SJan Kara 142ac401cc7SJan Kara struct wait_exceptional_entry_queue { 143ac6424b9SIngo Molnar wait_queue_entry_t wait; 144ac401cc7SJan Kara struct exceptional_entry_key key; 145ac401cc7SJan Kara }; 146ac401cc7SJan Kara 147b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas, 148b15cd800SMatthew Wilcox void *entry, struct exceptional_entry_key *key) 14963e95b5cSRoss Zwisler { 15063e95b5cSRoss Zwisler unsigned long hash; 151b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 15263e95b5cSRoss Zwisler 15363e95b5cSRoss Zwisler /* 15463e95b5cSRoss Zwisler * If 'entry' is a PMD, align the 'index' that we use for the wait 15563e95b5cSRoss Zwisler * queue to the start of that PMD. This ensures that all offsets in 15663e95b5cSRoss Zwisler * the range covered by the PMD map to the same bit lock. 15763e95b5cSRoss Zwisler */ 158642261acSRoss Zwisler if (dax_is_pmd_entry(entry)) 159917f3452SRoss Zwisler index &= ~PG_PMD_COLOUR; 160b15cd800SMatthew Wilcox key->xa = xas->xa; 16163e95b5cSRoss Zwisler key->entry_start = index; 16263e95b5cSRoss Zwisler 163b15cd800SMatthew Wilcox hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS); 16463e95b5cSRoss Zwisler return wait_table + hash; 16563e95b5cSRoss Zwisler } 16663e95b5cSRoss Zwisler 167ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait, 168ec4907ffSMatthew Wilcox unsigned int mode, int sync, void *keyp) 169ac401cc7SJan Kara { 170ac401cc7SJan Kara struct exceptional_entry_key *key = keyp; 171ac401cc7SJan Kara struct wait_exceptional_entry_queue *ewait = 172ac401cc7SJan Kara container_of(wait, struct wait_exceptional_entry_queue, wait); 173ac401cc7SJan Kara 174ec4907ffSMatthew Wilcox if (key->xa != ewait->key.xa || 17563e95b5cSRoss Zwisler key->entry_start != ewait->key.entry_start) 176ac401cc7SJan Kara return 0; 177ac401cc7SJan Kara return autoremove_wake_function(wait, mode, sync, NULL); 178ac401cc7SJan Kara } 179ac401cc7SJan Kara 180ac401cc7SJan Kara /* 181b93b0163SMatthew Wilcox * @entry may no longer be the entry at the index in the mapping. 182b93b0163SMatthew Wilcox * The important information it's conveying is whether the entry at 183b93b0163SMatthew Wilcox * this index used to be a PMD entry. 184e30331ffSRoss Zwisler */ 185b15cd800SMatthew Wilcox static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all) 186e30331ffSRoss Zwisler { 187e30331ffSRoss Zwisler struct exceptional_entry_key key; 188e30331ffSRoss Zwisler wait_queue_head_t *wq; 189e30331ffSRoss Zwisler 190b15cd800SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &key); 191e30331ffSRoss Zwisler 192e30331ffSRoss Zwisler /* 193e30331ffSRoss Zwisler * Checking for locked entry and prepare_to_wait_exclusive() happens 194b93b0163SMatthew Wilcox * under the i_pages lock, ditto for entry handling in our callers. 195e30331ffSRoss Zwisler * So at this point all tasks that could have seen our entry locked 196e30331ffSRoss Zwisler * must be in the waitqueue and the following check will see them. 197e30331ffSRoss Zwisler */ 198e30331ffSRoss Zwisler if (waitqueue_active(wq)) 199e30331ffSRoss Zwisler __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); 200e30331ffSRoss Zwisler } 201e30331ffSRoss Zwisler 202cfc93c6cSMatthew Wilcox /* 203cfc93c6cSMatthew Wilcox * Look up entry in page cache, wait for it to become unlocked if it 204cfc93c6cSMatthew Wilcox * is a DAX entry and return it. The caller must subsequently call 205cfc93c6cSMatthew Wilcox * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry() 206cfc93c6cSMatthew Wilcox * if it did. 207cfc93c6cSMatthew Wilcox * 208cfc93c6cSMatthew Wilcox * Must be called with the i_pages lock held. 209cfc93c6cSMatthew Wilcox */ 210cfc93c6cSMatthew Wilcox static void *get_unlocked_entry(struct xa_state *xas) 211cfc93c6cSMatthew Wilcox { 212cfc93c6cSMatthew Wilcox void *entry; 213cfc93c6cSMatthew Wilcox struct wait_exceptional_entry_queue ewait; 214cfc93c6cSMatthew Wilcox wait_queue_head_t *wq; 215cfc93c6cSMatthew Wilcox 216cfc93c6cSMatthew Wilcox init_wait(&ewait.wait); 217cfc93c6cSMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 218cfc93c6cSMatthew Wilcox 219cfc93c6cSMatthew Wilcox for (;;) { 2200e40de03SMatthew Wilcox entry = xas_find_conflict(xas); 2210e40de03SMatthew Wilcox if (!entry || 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 23655e56f06SMatthew Wilcox /* 23755e56f06SMatthew Wilcox * The only thing keeping the address space around is the i_pages lock 23855e56f06SMatthew Wilcox * (it's cycled in clear_inode() after removing the entries from i_pages) 23955e56f06SMatthew Wilcox * After we call xas_unlock_irq(), we cannot touch xas->xa. 24055e56f06SMatthew Wilcox */ 24155e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry) 24255e56f06SMatthew Wilcox { 24355e56f06SMatthew Wilcox struct wait_exceptional_entry_queue ewait; 24455e56f06SMatthew Wilcox wait_queue_head_t *wq; 24555e56f06SMatthew Wilcox 24655e56f06SMatthew Wilcox init_wait(&ewait.wait); 24755e56f06SMatthew Wilcox ewait.wait.func = wake_exceptional_entry_func; 24855e56f06SMatthew Wilcox 24955e56f06SMatthew Wilcox wq = dax_entry_waitqueue(xas, entry, &ewait.key); 250d8a70641SDan Williams /* 251d8a70641SDan Williams * Unlike get_unlocked_entry() there is no guarantee that this 252d8a70641SDan Williams * path ever successfully retrieves an unlocked entry before an 253d8a70641SDan Williams * inode dies. Perform a non-exclusive wait in case this path 254d8a70641SDan Williams * never successfully performs its own wake up. 255d8a70641SDan Williams */ 256d8a70641SDan Williams prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE); 25755e56f06SMatthew Wilcox xas_unlock_irq(xas); 25855e56f06SMatthew Wilcox schedule(); 25955e56f06SMatthew Wilcox finish_wait(wq, &ewait.wait); 26055e56f06SMatthew Wilcox } 26155e56f06SMatthew Wilcox 262cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry) 263cfc93c6cSMatthew Wilcox { 264cfc93c6cSMatthew Wilcox /* If we were the only waiter woken, wake the next one */ 265cfc93c6cSMatthew Wilcox if (entry) 266cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 267cfc93c6cSMatthew Wilcox } 268cfc93c6cSMatthew Wilcox 269cfc93c6cSMatthew Wilcox /* 270cfc93c6cSMatthew Wilcox * We used the xa_state to get the entry, but then we locked the entry and 271cfc93c6cSMatthew Wilcox * dropped the xa_lock, so we know the xa_state is stale and must be reset 272cfc93c6cSMatthew Wilcox * before use. 273cfc93c6cSMatthew Wilcox */ 274cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry) 275cfc93c6cSMatthew Wilcox { 276cfc93c6cSMatthew Wilcox void *old; 277cfc93c6cSMatthew Wilcox 2787ae2ea7dSMatthew Wilcox BUG_ON(dax_is_locked(entry)); 279cfc93c6cSMatthew Wilcox xas_reset(xas); 280cfc93c6cSMatthew Wilcox xas_lock_irq(xas); 281cfc93c6cSMatthew Wilcox old = xas_store(xas, entry); 282cfc93c6cSMatthew Wilcox xas_unlock_irq(xas); 283cfc93c6cSMatthew Wilcox BUG_ON(!dax_is_locked(old)); 284cfc93c6cSMatthew Wilcox dax_wake_entry(xas, entry, false); 285cfc93c6cSMatthew Wilcox } 286cfc93c6cSMatthew Wilcox 287cfc93c6cSMatthew Wilcox /* 288cfc93c6cSMatthew Wilcox * Return: The entry stored at this location before it was locked. 289cfc93c6cSMatthew Wilcox */ 290cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry) 291cfc93c6cSMatthew Wilcox { 292cfc93c6cSMatthew Wilcox unsigned long v = xa_to_value(entry); 293cfc93c6cSMatthew Wilcox return xas_store(xas, xa_mk_value(v | DAX_LOCKED)); 294cfc93c6cSMatthew Wilcox } 295cfc93c6cSMatthew Wilcox 296d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry) 297d2c997c0SDan Williams { 298d2c997c0SDan Williams if (dax_is_zero_entry(entry)) 299d2c997c0SDan Williams return 0; 300d2c997c0SDan Williams else if (dax_is_empty_entry(entry)) 301d2c997c0SDan Williams return 0; 302d2c997c0SDan Williams else if (dax_is_pmd_entry(entry)) 303d2c997c0SDan Williams return PMD_SIZE; 304d2c997c0SDan Williams else 305d2c997c0SDan Williams return PAGE_SIZE; 306d2c997c0SDan Williams } 307d2c997c0SDan Williams 308a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry) 309d2c997c0SDan Williams { 310a77d19f4SMatthew Wilcox return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE; 311d2c997c0SDan Williams } 312d2c997c0SDan Williams 313d2c997c0SDan Williams /* 314d2c997c0SDan Williams * Iterate through all mapped pfns represented by an entry, i.e. skip 315d2c997c0SDan Williams * 'empty' and 'zero' entries. 316d2c997c0SDan Williams */ 317d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \ 318a77d19f4SMatthew Wilcox for (pfn = dax_to_pfn(entry); \ 319a77d19f4SMatthew Wilcox pfn < dax_end_pfn(entry); pfn++) 320d2c997c0SDan Williams 32173449dafSDan Williams /* 32273449dafSDan Williams * TODO: for reflink+dax we need a way to associate a single page with 32373449dafSDan Williams * multiple address_space instances at different linear_page_index() 32473449dafSDan Williams * offsets. 32573449dafSDan Williams */ 32673449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping, 32773449dafSDan Williams struct vm_area_struct *vma, unsigned long address) 328d2c997c0SDan Williams { 32973449dafSDan Williams unsigned long size = dax_entry_size(entry), pfn, index; 33073449dafSDan Williams int i = 0; 331d2c997c0SDan Williams 332d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 333d2c997c0SDan Williams return; 334d2c997c0SDan Williams 33573449dafSDan Williams index = linear_page_index(vma, address & ~(size - 1)); 336d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 337d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 338d2c997c0SDan Williams 339d2c997c0SDan Williams WARN_ON_ONCE(page->mapping); 340d2c997c0SDan Williams page->mapping = mapping; 34173449dafSDan Williams page->index = index + i++; 342d2c997c0SDan Williams } 343d2c997c0SDan Williams } 344d2c997c0SDan Williams 345d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping, 346d2c997c0SDan Williams bool trunc) 347d2c997c0SDan Williams { 348d2c997c0SDan Williams unsigned long pfn; 349d2c997c0SDan Williams 350d2c997c0SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 351d2c997c0SDan Williams return; 352d2c997c0SDan Williams 353d2c997c0SDan Williams for_each_mapped_pfn(entry, pfn) { 354d2c997c0SDan Williams struct page *page = pfn_to_page(pfn); 355d2c997c0SDan Williams 356d2c997c0SDan Williams WARN_ON_ONCE(trunc && page_ref_count(page) > 1); 357d2c997c0SDan Williams WARN_ON_ONCE(page->mapping && page->mapping != mapping); 358d2c997c0SDan Williams page->mapping = NULL; 35973449dafSDan Williams page->index = 0; 360d2c997c0SDan Williams } 361d2c997c0SDan Williams } 362d2c997c0SDan Williams 3635fac7408SDan Williams static struct page *dax_busy_page(void *entry) 3645fac7408SDan Williams { 3655fac7408SDan Williams unsigned long pfn; 3665fac7408SDan Williams 3675fac7408SDan Williams for_each_mapped_pfn(entry, pfn) { 3685fac7408SDan Williams struct page *page = pfn_to_page(pfn); 3695fac7408SDan Williams 3705fac7408SDan Williams if (page_ref_count(page) > 1) 3715fac7408SDan Williams return page; 3725fac7408SDan Williams } 3735fac7408SDan Williams return NULL; 3745fac7408SDan Williams } 3755fac7408SDan Williams 376c5bbd451SMatthew Wilcox /* 377c5bbd451SMatthew Wilcox * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page 378c5bbd451SMatthew Wilcox * @page: The page whose entry we want to lock 379c5bbd451SMatthew Wilcox * 380c5bbd451SMatthew Wilcox * Context: Process context. 38127359fd6SMatthew Wilcox * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could 38227359fd6SMatthew Wilcox * not be locked. 383c5bbd451SMatthew Wilcox */ 38427359fd6SMatthew Wilcox dax_entry_t dax_lock_page(struct page *page) 385c2a7d2a1SDan Williams { 3869f32d221SMatthew Wilcox XA_STATE(xas, NULL, 0); 3879f32d221SMatthew Wilcox void *entry; 388c2a7d2a1SDan Williams 389c5bbd451SMatthew Wilcox /* Ensure page->mapping isn't freed while we look at it */ 390c5bbd451SMatthew Wilcox rcu_read_lock(); 391c2a7d2a1SDan Williams for (;;) { 3929f32d221SMatthew Wilcox struct address_space *mapping = READ_ONCE(page->mapping); 393c2a7d2a1SDan Williams 39427359fd6SMatthew Wilcox entry = NULL; 395c93db7bbSMatthew Wilcox if (!mapping || !dax_mapping(mapping)) 396c5bbd451SMatthew Wilcox break; 397c2a7d2a1SDan Williams 398c2a7d2a1SDan Williams /* 399c2a7d2a1SDan Williams * In the device-dax case there's no need to lock, a 400c2a7d2a1SDan Williams * struct dev_pagemap pin is sufficient to keep the 401c2a7d2a1SDan Williams * inode alive, and we assume we have dev_pagemap pin 402c2a7d2a1SDan Williams * otherwise we would not have a valid pfn_to_page() 403c2a7d2a1SDan Williams * translation. 404c2a7d2a1SDan Williams */ 40527359fd6SMatthew Wilcox entry = (void *)~0UL; 4069f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 407c5bbd451SMatthew Wilcox break; 408c2a7d2a1SDan Williams 4099f32d221SMatthew Wilcox xas.xa = &mapping->i_pages; 4109f32d221SMatthew Wilcox xas_lock_irq(&xas); 411c2a7d2a1SDan Williams if (mapping != page->mapping) { 4129f32d221SMatthew Wilcox xas_unlock_irq(&xas); 413c2a7d2a1SDan Williams continue; 414c2a7d2a1SDan Williams } 4159f32d221SMatthew Wilcox xas_set(&xas, page->index); 4169f32d221SMatthew Wilcox entry = xas_load(&xas); 4179f32d221SMatthew Wilcox if (dax_is_locked(entry)) { 418c5bbd451SMatthew Wilcox rcu_read_unlock(); 41955e56f06SMatthew Wilcox wait_entry_unlocked(&xas, entry); 420c5bbd451SMatthew Wilcox rcu_read_lock(); 421c2a7d2a1SDan Williams continue; 422c2a7d2a1SDan Williams } 4239f32d221SMatthew Wilcox dax_lock_entry(&xas, entry); 4249f32d221SMatthew Wilcox xas_unlock_irq(&xas); 425c5bbd451SMatthew Wilcox break; 4269f32d221SMatthew Wilcox } 427c5bbd451SMatthew Wilcox rcu_read_unlock(); 42827359fd6SMatthew Wilcox return (dax_entry_t)entry; 429c2a7d2a1SDan Williams } 430c2a7d2a1SDan Williams 43127359fd6SMatthew Wilcox void dax_unlock_page(struct page *page, dax_entry_t cookie) 432c2a7d2a1SDan Williams { 433c2a7d2a1SDan Williams struct address_space *mapping = page->mapping; 4349f32d221SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, page->index); 435c2a7d2a1SDan Williams 4369f32d221SMatthew Wilcox if (S_ISCHR(mapping->host->i_mode)) 437c2a7d2a1SDan Williams return; 438c2a7d2a1SDan Williams 43927359fd6SMatthew Wilcox dax_unlock_entry(&xas, (void *)cookie); 440c2a7d2a1SDan Williams } 441c2a7d2a1SDan Williams 442ac401cc7SJan Kara /* 443a77d19f4SMatthew Wilcox * Find page cache entry at given index. If it is a DAX entry, return it 444a77d19f4SMatthew Wilcox * with the entry locked. If the page cache doesn't contain an entry at 445a77d19f4SMatthew Wilcox * that index, add a locked empty entry. 446ac401cc7SJan Kara * 4473159f943SMatthew Wilcox * When requesting an entry with size DAX_PMD, grab_mapping_entry() will 448b15cd800SMatthew Wilcox * either return that locked entry or will return VM_FAULT_FALLBACK. 449b15cd800SMatthew Wilcox * This will happen if there are any PTE entries within the PMD range 450b15cd800SMatthew Wilcox * that we are requesting. 451642261acSRoss Zwisler * 452b15cd800SMatthew Wilcox * We always favor PTE entries over PMD entries. There isn't a flow where we 453b15cd800SMatthew Wilcox * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD 454b15cd800SMatthew Wilcox * insertion will fail if it finds any PTE entries already in the tree, and a 455b15cd800SMatthew Wilcox * PTE insertion will cause an existing PMD entry to be unmapped and 456b15cd800SMatthew Wilcox * downgraded to PTE entries. This happens for both PMD zero pages as 457b15cd800SMatthew Wilcox * well as PMD empty entries. 458642261acSRoss Zwisler * 459b15cd800SMatthew Wilcox * The exception to this downgrade path is for PMD entries that have 460b15cd800SMatthew Wilcox * real storage backing them. We will leave these real PMD entries in 461b15cd800SMatthew Wilcox * the tree, and PTE writes will simply dirty the entire PMD entry. 462642261acSRoss Zwisler * 463ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 464ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 465ac401cc7SJan Kara * show it helps. 466b15cd800SMatthew Wilcox * 467b15cd800SMatthew Wilcox * On error, this function does not return an ERR_PTR. Instead it returns 468b15cd800SMatthew Wilcox * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values 469b15cd800SMatthew Wilcox * overlap with xarray value entries. 470ac401cc7SJan Kara */ 471b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas, 472b15cd800SMatthew Wilcox struct address_space *mapping, unsigned long size_flag) 473ac401cc7SJan Kara { 474b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 475b15cd800SMatthew Wilcox bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */ 476b15cd800SMatthew Wilcox void *entry; 477ac401cc7SJan Kara 478b15cd800SMatthew Wilcox retry: 479b15cd800SMatthew Wilcox xas_lock_irq(xas); 480b15cd800SMatthew Wilcox entry = get_unlocked_entry(xas); 481642261acSRoss Zwisler 482b15cd800SMatthew Wilcox if (entry) { 4830e40de03SMatthew Wilcox if (!xa_is_value(entry)) { 484b15cd800SMatthew Wilcox xas_set_err(xas, EIO); 48591d25ba8SRoss Zwisler goto out_unlock; 48691d25ba8SRoss Zwisler } 48791d25ba8SRoss Zwisler 4883159f943SMatthew Wilcox if (size_flag & DAX_PMD) { 48991d25ba8SRoss Zwisler if (dax_is_pte_entry(entry)) { 490b15cd800SMatthew Wilcox put_unlocked_entry(xas, entry); 491b15cd800SMatthew Wilcox goto fallback; 492642261acSRoss Zwisler } 493642261acSRoss Zwisler } else { /* trying to grab a PTE entry */ 49491d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry) && 495642261acSRoss Zwisler (dax_is_zero_entry(entry) || 496642261acSRoss Zwisler dax_is_empty_entry(entry))) { 497642261acSRoss Zwisler pmd_downgrade = true; 498642261acSRoss Zwisler } 499642261acSRoss Zwisler } 500642261acSRoss Zwisler } 501642261acSRoss Zwisler 502642261acSRoss Zwisler if (pmd_downgrade) { 503642261acSRoss Zwisler /* 504642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 505b93b0163SMatthew Wilcox * the i_pages lock. 506642261acSRoss Zwisler */ 507b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 508642261acSRoss Zwisler 509642261acSRoss Zwisler /* 510642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 511642261acSRoss Zwisler * downgraded are empty entries which don't need to be 512642261acSRoss Zwisler * unmapped. 513642261acSRoss Zwisler */ 514b15cd800SMatthew Wilcox if (dax_is_zero_entry(entry)) { 515b15cd800SMatthew Wilcox xas_unlock_irq(xas); 516b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, 517b15cd800SMatthew Wilcox xas->xa_index & ~PG_PMD_COLOUR, 518977fbdcdSMatthew Wilcox PG_PMD_NR, false); 519b15cd800SMatthew Wilcox xas_reset(xas); 520b15cd800SMatthew Wilcox xas_lock_irq(xas); 521e11f8b7bSRoss Zwisler } 522e11f8b7bSRoss Zwisler 523d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 524b15cd800SMatthew Wilcox xas_store(xas, NULL); /* undo the PMD join */ 525b15cd800SMatthew Wilcox dax_wake_entry(xas, entry, true); 526642261acSRoss Zwisler mapping->nrexceptional--; 527b15cd800SMatthew Wilcox entry = NULL; 528b15cd800SMatthew Wilcox xas_set(xas, index); 529642261acSRoss Zwisler } 530642261acSRoss Zwisler 531b15cd800SMatthew Wilcox if (entry) { 532b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 533b15cd800SMatthew Wilcox } else { 534b15cd800SMatthew Wilcox entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY); 535b15cd800SMatthew Wilcox dax_lock_entry(xas, entry); 536b15cd800SMatthew Wilcox if (xas_error(xas)) 537b15cd800SMatthew Wilcox goto out_unlock; 538ac401cc7SJan Kara mapping->nrexceptional++; 539ac401cc7SJan Kara } 540b15cd800SMatthew Wilcox 541642261acSRoss Zwisler out_unlock: 542b15cd800SMatthew Wilcox xas_unlock_irq(xas); 543b15cd800SMatthew Wilcox if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM)) 544b15cd800SMatthew Wilcox goto retry; 545b15cd800SMatthew Wilcox if (xas->xa_node == XA_ERROR(-ENOMEM)) 546b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_OOM); 547b15cd800SMatthew Wilcox if (xas_error(xas)) 548b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_SIGBUS); 549e3ad61c6SRoss Zwisler return entry; 550b15cd800SMatthew Wilcox fallback: 551b15cd800SMatthew Wilcox xas_unlock_irq(xas); 552b15cd800SMatthew Wilcox return xa_mk_internal(VM_FAULT_FALLBACK); 553ac401cc7SJan Kara } 554ac401cc7SJan Kara 5555fac7408SDan Williams /** 5565fac7408SDan Williams * dax_layout_busy_page - find first pinned page in @mapping 5575fac7408SDan Williams * @mapping: address space to scan for a page with ref count > 1 5585fac7408SDan Williams * 5595fac7408SDan Williams * DAX requires ZONE_DEVICE mapped pages. These pages are never 5605fac7408SDan Williams * 'onlined' to the page allocator so they are considered idle when 5615fac7408SDan Williams * page->count == 1. A filesystem uses this interface to determine if 5625fac7408SDan Williams * any page in the mapping is busy, i.e. for DMA, or other 5635fac7408SDan Williams * get_user_pages() usages. 5645fac7408SDan Williams * 5655fac7408SDan Williams * It is expected that the filesystem is holding locks to block the 5665fac7408SDan Williams * establishment of new mappings in this address_space. I.e. it expects 5675fac7408SDan Williams * to be able to run unmap_mapping_range() and subsequently not race 5685fac7408SDan Williams * mapping_mapped() becoming true. 5695fac7408SDan Williams */ 5705fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping) 5715fac7408SDan Williams { 572084a8990SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, 0); 573084a8990SMatthew Wilcox void *entry; 574084a8990SMatthew Wilcox unsigned int scanned = 0; 5755fac7408SDan Williams struct page *page = NULL; 5765fac7408SDan Williams 5775fac7408SDan Williams /* 5785fac7408SDan Williams * In the 'limited' case get_user_pages() for dax is disabled. 5795fac7408SDan Williams */ 5805fac7408SDan Williams if (IS_ENABLED(CONFIG_FS_DAX_LIMITED)) 5815fac7408SDan Williams return NULL; 5825fac7408SDan Williams 5835fac7408SDan Williams if (!dax_mapping(mapping) || !mapping_mapped(mapping)) 5845fac7408SDan Williams return NULL; 5855fac7408SDan Williams 5865fac7408SDan Williams /* 5875fac7408SDan Williams * If we race get_user_pages_fast() here either we'll see the 588084a8990SMatthew Wilcox * elevated page count in the iteration and wait, or 5895fac7408SDan Williams * get_user_pages_fast() will see that the page it took a reference 5905fac7408SDan Williams * against is no longer mapped in the page tables and bail to the 5915fac7408SDan Williams * get_user_pages() slow path. The slow path is protected by 5925fac7408SDan Williams * pte_lock() and pmd_lock(). New references are not taken without 5935fac7408SDan Williams * holding those locks, and unmap_mapping_range() will not zero the 5945fac7408SDan Williams * pte or pmd without holding the respective lock, so we are 5955fac7408SDan Williams * guaranteed to either see new references or prevent new 5965fac7408SDan Williams * references from being established. 5975fac7408SDan Williams */ 5985fac7408SDan Williams unmap_mapping_range(mapping, 0, 0, 1); 5995fac7408SDan Williams 600084a8990SMatthew Wilcox xas_lock_irq(&xas); 601084a8990SMatthew Wilcox xas_for_each(&xas, entry, ULONG_MAX) { 602084a8990SMatthew Wilcox if (WARN_ON_ONCE(!xa_is_value(entry))) 6035fac7408SDan Williams continue; 604084a8990SMatthew Wilcox if (unlikely(dax_is_locked(entry))) 605084a8990SMatthew Wilcox entry = get_unlocked_entry(&xas); 6065fac7408SDan Williams if (entry) 6075fac7408SDan Williams page = dax_busy_page(entry); 608084a8990SMatthew Wilcox put_unlocked_entry(&xas, entry); 6095fac7408SDan Williams if (page) 6105fac7408SDan Williams break; 611084a8990SMatthew Wilcox if (++scanned % XA_CHECK_SCHED) 612084a8990SMatthew Wilcox continue; 613cdbf8897SRoss Zwisler 614084a8990SMatthew Wilcox xas_pause(&xas); 615084a8990SMatthew Wilcox xas_unlock_irq(&xas); 616084a8990SMatthew Wilcox cond_resched(); 617084a8990SMatthew Wilcox xas_lock_irq(&xas); 6185fac7408SDan Williams } 619084a8990SMatthew Wilcox xas_unlock_irq(&xas); 6205fac7408SDan Williams return page; 6215fac7408SDan Williams } 6225fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page); 6235fac7408SDan Williams 624a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping, 625c6dcf52cSJan Kara pgoff_t index, bool trunc) 626c6dcf52cSJan Kara { 62707f2d89cSMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 628c6dcf52cSJan Kara int ret = 0; 629c6dcf52cSJan Kara void *entry; 630c6dcf52cSJan Kara 63107f2d89cSMatthew Wilcox xas_lock_irq(&xas); 63207f2d89cSMatthew Wilcox entry = get_unlocked_entry(&xas); 6333159f943SMatthew Wilcox if (!entry || WARN_ON_ONCE(!xa_is_value(entry))) 634c6dcf52cSJan Kara goto out; 635c6dcf52cSJan Kara if (!trunc && 63607f2d89cSMatthew Wilcox (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) || 63707f2d89cSMatthew Wilcox xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE))) 638c6dcf52cSJan Kara goto out; 639d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, trunc); 64007f2d89cSMatthew Wilcox xas_store(&xas, NULL); 641c6dcf52cSJan Kara mapping->nrexceptional--; 642c6dcf52cSJan Kara ret = 1; 643c6dcf52cSJan Kara out: 64407f2d89cSMatthew Wilcox put_unlocked_entry(&xas, entry); 64507f2d89cSMatthew Wilcox xas_unlock_irq(&xas); 646c6dcf52cSJan Kara return ret; 647c6dcf52cSJan Kara } 64807f2d89cSMatthew Wilcox 649ac401cc7SJan Kara /* 6503159f943SMatthew Wilcox * Delete DAX entry at @index from @mapping. Wait for it 6513159f943SMatthew Wilcox * to be unlocked before deleting it. 652ac401cc7SJan Kara */ 653ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 654ac401cc7SJan Kara { 655a77d19f4SMatthew Wilcox int ret = __dax_invalidate_entry(mapping, index, true); 656ac401cc7SJan Kara 657ac401cc7SJan Kara /* 658ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 659ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 660a77d19f4SMatthew Wilcox * page cache (usually fs-private i_mmap_sem for writing). Since the 6613159f943SMatthew Wilcox * caller has seen a DAX entry for this index, we better find it 662ac401cc7SJan Kara * at that index as well... 663ac401cc7SJan Kara */ 664c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 665c6dcf52cSJan Kara return ret; 666ac401cc7SJan Kara } 667ac401cc7SJan Kara 668c6dcf52cSJan Kara /* 6693159f943SMatthew Wilcox * Invalidate DAX entry if it is clean. 670c6dcf52cSJan Kara */ 671c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 672c6dcf52cSJan Kara pgoff_t index) 673c6dcf52cSJan Kara { 674a77d19f4SMatthew Wilcox return __dax_invalidate_entry(mapping, index, false); 675ac401cc7SJan Kara } 676ac401cc7SJan Kara 677cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, 678cccbce67SDan Williams sector_t sector, size_t size, struct page *to, 679cccbce67SDan Williams unsigned long vaddr) 680f7ca90b1SMatthew Wilcox { 681cccbce67SDan Williams void *vto, *kaddr; 682cccbce67SDan Williams pgoff_t pgoff; 683cccbce67SDan Williams long rc; 684cccbce67SDan Williams int id; 685e2e05394SRoss Zwisler 686cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 687cccbce67SDan Williams if (rc) 688cccbce67SDan Williams return rc; 689cccbce67SDan Williams 690cccbce67SDan Williams id = dax_read_lock(); 69186ed913bSHuaisheng Ye rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL); 692cccbce67SDan Williams if (rc < 0) { 693cccbce67SDan Williams dax_read_unlock(id); 694cccbce67SDan Williams return rc; 695cccbce67SDan Williams } 696f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 697cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 698f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 699cccbce67SDan Williams dax_read_unlock(id); 700f7ca90b1SMatthew Wilcox return 0; 701f7ca90b1SMatthew Wilcox } 702f7ca90b1SMatthew Wilcox 703642261acSRoss Zwisler /* 704642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 705642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 706642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 707642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 708642261acSRoss Zwisler * appropriate. 709642261acSRoss Zwisler */ 710b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas, 711b15cd800SMatthew Wilcox struct address_space *mapping, struct vm_fault *vmf, 712b15cd800SMatthew Wilcox void *entry, pfn_t pfn, unsigned long flags, bool dirty) 7139973c98eSRoss Zwisler { 714b15cd800SMatthew Wilcox void *new_entry = dax_make_entry(pfn, flags); 7159973c98eSRoss Zwisler 716f5b7b748SJan Kara if (dirty) 7179973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 7189973c98eSRoss Zwisler 7193159f943SMatthew Wilcox if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) { 720b15cd800SMatthew Wilcox unsigned long index = xas->xa_index; 72191d25ba8SRoss Zwisler /* we are replacing a zero page with block mapping */ 72291d25ba8SRoss Zwisler if (dax_is_pmd_entry(entry)) 723977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, 724977fbdcdSMatthew Wilcox PG_PMD_NR, false); 72591d25ba8SRoss Zwisler else /* pte entry */ 726b15cd800SMatthew Wilcox unmap_mapping_pages(mapping, index, 1, false); 727ac401cc7SJan Kara } 7289973c98eSRoss Zwisler 729b15cd800SMatthew Wilcox xas_reset(xas); 730b15cd800SMatthew Wilcox xas_lock_irq(xas); 731d2c997c0SDan Williams if (dax_entry_size(entry) != dax_entry_size(new_entry)) { 732d2c997c0SDan Williams dax_disassociate_entry(entry, mapping, false); 73373449dafSDan Williams dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address); 734d2c997c0SDan Williams } 735642261acSRoss Zwisler 73691d25ba8SRoss Zwisler if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 737642261acSRoss Zwisler /* 738a77d19f4SMatthew Wilcox * Only swap our new entry into the page cache if the current 739642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 740a77d19f4SMatthew Wilcox * PMD entry is already in the cache, we leave it alone. This 741642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 742642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 743642261acSRoss Zwisler * tree and dirty it if necessary. 744642261acSRoss Zwisler */ 745b15cd800SMatthew Wilcox void *old = dax_lock_entry(xas, new_entry); 746b15cd800SMatthew Wilcox WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) | 747b15cd800SMatthew Wilcox DAX_LOCKED)); 74891d25ba8SRoss Zwisler entry = new_entry; 749b15cd800SMatthew Wilcox } else { 750b15cd800SMatthew Wilcox xas_load(xas); /* Walk the xa_state */ 751ac401cc7SJan Kara } 75291d25ba8SRoss Zwisler 753f5b7b748SJan Kara if (dirty) 754b15cd800SMatthew Wilcox xas_set_mark(xas, PAGECACHE_TAG_DIRTY); 75591d25ba8SRoss Zwisler 756b15cd800SMatthew Wilcox xas_unlock_irq(xas); 75791d25ba8SRoss Zwisler return entry; 7589973c98eSRoss Zwisler } 7599973c98eSRoss Zwisler 760a77d19f4SMatthew Wilcox static inline 761a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 7624b4bb46dSJan Kara { 7634b4bb46dSJan Kara unsigned long address; 7644b4bb46dSJan Kara 7654b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 7664b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 7674b4bb46dSJan Kara return address; 7684b4bb46dSJan Kara } 7694b4bb46dSJan Kara 7704b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 771a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index, 772a77d19f4SMatthew Wilcox unsigned long pfn) 7734b4bb46dSJan Kara { 7744b4bb46dSJan Kara struct vm_area_struct *vma; 775f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 776f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 7774b4bb46dSJan Kara spinlock_t *ptl; 7784b4bb46dSJan Kara 7794b4bb46dSJan Kara i_mmap_lock_read(mapping); 7804b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 781ac46d4f3SJérôme Glisse struct mmu_notifier_range range; 782ac46d4f3SJérôme Glisse unsigned long address; 7834b4bb46dSJan Kara 7844b4bb46dSJan Kara cond_resched(); 7854b4bb46dSJan Kara 7864b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 7874b4bb46dSJan Kara continue; 7884b4bb46dSJan Kara 7894b4bb46dSJan Kara address = pgoff_address(index, vma); 790a4d1a885SJérôme Glisse 791a4d1a885SJérôme Glisse /* 7920cefc36bSIra Weiny * Note because we provide range to follow_pte_pmd it will 793a4d1a885SJérôme Glisse * call mmu_notifier_invalidate_range_start() on our behalf 794a4d1a885SJérôme Glisse * before taking any lock. 795a4d1a885SJérôme Glisse */ 796ac46d4f3SJérôme Glisse if (follow_pte_pmd(vma->vm_mm, address, &range, 797ac46d4f3SJérôme Glisse &ptep, &pmdp, &ptl)) 7984b4bb46dSJan Kara continue; 799f729c8c9SRoss Zwisler 8000f10851eSJérôme Glisse /* 8010f10851eSJérôme Glisse * No need to call mmu_notifier_invalidate_range() as we are 8020f10851eSJérôme Glisse * downgrading page table protection not changing it to point 8030f10851eSJérôme Glisse * to a new page. 8040f10851eSJérôme Glisse * 805ad56b738SMike Rapoport * See Documentation/vm/mmu_notifier.rst 8060f10851eSJérôme Glisse */ 807f729c8c9SRoss Zwisler if (pmdp) { 808f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 809f729c8c9SRoss Zwisler pmd_t pmd; 810f729c8c9SRoss Zwisler 811f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 812f729c8c9SRoss Zwisler goto unlock_pmd; 813f6f37321SLinus Torvalds if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 814f729c8c9SRoss Zwisler goto unlock_pmd; 815f729c8c9SRoss Zwisler 816f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 817f729c8c9SRoss Zwisler pmd = pmdp_huge_clear_flush(vma, address, pmdp); 818f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 819f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 820f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 821f729c8c9SRoss Zwisler unlock_pmd: 822f729c8c9SRoss Zwisler #endif 823ee190ca6SJan H. Schönherr spin_unlock(ptl); 824f729c8c9SRoss Zwisler } else { 8254b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 826f729c8c9SRoss Zwisler goto unlock_pte; 8274b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 828f729c8c9SRoss Zwisler goto unlock_pte; 8294b4bb46dSJan Kara 8304b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 8314b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 8324b4bb46dSJan Kara pte = pte_wrprotect(pte); 8334b4bb46dSJan Kara pte = pte_mkclean(pte); 8344b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 835f729c8c9SRoss Zwisler unlock_pte: 8364b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 837f729c8c9SRoss Zwisler } 8384b4bb46dSJan Kara 839ac46d4f3SJérôme Glisse mmu_notifier_invalidate_range_end(&range); 8404b4bb46dSJan Kara } 8414b4bb46dSJan Kara i_mmap_unlock_read(mapping); 8424b4bb46dSJan Kara } 8434b4bb46dSJan Kara 8449fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev, 8459fc747f6SMatthew Wilcox struct address_space *mapping, void *entry) 8469973c98eSRoss Zwisler { 847e4b3448bSMatthew Wilcox unsigned long pfn, index, count; 8483fe0791cSDan Williams long ret = 0; 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 /* 897e4b3448bSMatthew Wilcox * If dax_writeback_mapping_range() was given a wbc->range_start 898e4b3448bSMatthew Wilcox * in the middle of a PMD, the 'index' we use needs to be 899e4b3448bSMatthew Wilcox * aligned to the start of the PMD. 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); 904e4b3448bSMatthew Wilcox count = 1UL << dax_entry_order(entry); 905e4b3448bSMatthew Wilcox index = xas->xa_index & ~(count - 1); 906cccbce67SDan Williams 907e4b3448bSMatthew Wilcox dax_entry_mkclean(mapping, index, pfn); 908e4b3448bSMatthew Wilcox dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE); 9094b4bb46dSJan Kara /* 9104b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 9114b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 9124b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 9134b4bb46dSJan Kara * entry lock. 9144b4bb46dSJan Kara */ 9159fc747f6SMatthew Wilcox xas_reset(xas); 9169fc747f6SMatthew Wilcox xas_lock_irq(xas); 9179fc747f6SMatthew Wilcox xas_store(xas, entry); 9189fc747f6SMatthew Wilcox xas_clear_mark(xas, PAGECACHE_TAG_DIRTY); 9199fc747f6SMatthew Wilcox dax_wake_entry(xas, entry, false); 9209fc747f6SMatthew Wilcox 921e4b3448bSMatthew Wilcox trace_dax_writeback_one(mapping->host, index, count); 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; 141111cf9d86SAneesh Kumar K.V struct vm_area_struct *vma = vmf->vma; 1412653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 141311cf9d86SAneesh Kumar K.V pgtable_t pgtable = NULL; 1414642261acSRoss Zwisler struct page *zero_page; 1415642261acSRoss Zwisler spinlock_t *ptl; 1416642261acSRoss Zwisler pmd_t pmd_entry; 14173fe0791cSDan Williams pfn_t pfn; 1418642261acSRoss Zwisler 1419f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1420642261acSRoss Zwisler 1421642261acSRoss Zwisler if (unlikely(!zero_page)) 1422653b2ea3SRoss Zwisler goto fallback; 1423642261acSRoss Zwisler 14243fe0791cSDan Williams pfn = page_to_pfn_t(zero_page); 1425b15cd800SMatthew Wilcox *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, 14263159f943SMatthew Wilcox DAX_PMD | DAX_ZERO_PAGE, false); 1427642261acSRoss Zwisler 142811cf9d86SAneesh Kumar K.V if (arch_needs_pgtable_deposit()) { 142911cf9d86SAneesh Kumar K.V pgtable = pte_alloc_one(vma->vm_mm); 143011cf9d86SAneesh Kumar K.V if (!pgtable) 143111cf9d86SAneesh Kumar K.V return VM_FAULT_OOM; 143211cf9d86SAneesh Kumar K.V } 143311cf9d86SAneesh Kumar K.V 1434f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1435f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1436642261acSRoss Zwisler spin_unlock(ptl); 1437653b2ea3SRoss Zwisler goto fallback; 1438642261acSRoss Zwisler } 1439642261acSRoss Zwisler 144011cf9d86SAneesh Kumar K.V if (pgtable) { 144111cf9d86SAneesh Kumar K.V pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable); 144211cf9d86SAneesh Kumar K.V mm_inc_nr_ptes(vma->vm_mm); 144311cf9d86SAneesh Kumar K.V } 1444f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1445642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1446f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1447642261acSRoss Zwisler spin_unlock(ptl); 1448b15cd800SMatthew Wilcox trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry); 1449642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1450653b2ea3SRoss Zwisler 1451653b2ea3SRoss Zwisler fallback: 145211cf9d86SAneesh Kumar K.V if (pgtable) 145311cf9d86SAneesh Kumar K.V pte_free(vma->vm_mm, pgtable); 1454b15cd800SMatthew Wilcox trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry); 1455642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1456642261acSRoss Zwisler } 1457642261acSRoss Zwisler 1458ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 1459a2d58167SDave Jiang const struct iomap_ops *ops) 1460642261acSRoss Zwisler { 1461f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1462642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1463b15cd800SMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER); 1464d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1465d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 1466caa51d26SJan Kara bool sync; 14679484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1468642261acSRoss Zwisler struct inode *inode = mapping->host; 1469ab77dab4SSouptick Joarder vm_fault_t result = VM_FAULT_FALLBACK; 1470642261acSRoss Zwisler struct iomap iomap = { 0 }; 1471b15cd800SMatthew Wilcox pgoff_t max_pgoff; 1472642261acSRoss Zwisler void *entry; 1473642261acSRoss Zwisler loff_t pos; 1474642261acSRoss Zwisler int error; 1475302a5e31SJan Kara pfn_t pfn; 1476642261acSRoss Zwisler 1477282a8e03SRoss Zwisler /* 1478282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1479282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1480282a8e03SRoss Zwisler * this is a reliable test. 1481282a8e03SRoss Zwisler */ 1482957ac8c4SJeff Moyer max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); 1483282a8e03SRoss Zwisler 1484f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1485282a8e03SRoss Zwisler 1486fffa281bSRoss Zwisler /* 1487fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1488fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1489fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1490a77d19f4SMatthew Wilcox * range in the page cache. 1491fffa281bSRoss Zwisler */ 1492fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1493fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1494fffa281bSRoss Zwisler goto fallback; 1495fffa281bSRoss Zwisler 1496642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1497642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1498642261acSRoss Zwisler goto fallback; 1499642261acSRoss Zwisler 1500642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1501642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1502642261acSRoss Zwisler goto fallback; 1503642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1504642261acSRoss Zwisler goto fallback; 1505642261acSRoss Zwisler 1506b15cd800SMatthew Wilcox if (xas.xa_index >= max_pgoff) { 1507282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1508282a8e03SRoss Zwisler goto out; 1509282a8e03SRoss Zwisler } 1510642261acSRoss Zwisler 1511642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1512b15cd800SMatthew Wilcox if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff) 1513642261acSRoss Zwisler goto fallback; 1514642261acSRoss Zwisler 1515642261acSRoss Zwisler /* 1516b15cd800SMatthew Wilcox * grab_mapping_entry() will make sure we get an empty PMD entry, 1517b15cd800SMatthew Wilcox * a zero PMD entry or a DAX PMD. If it can't (because a PTE 1518b15cd800SMatthew Wilcox * entry is already in the array, for instance), it will return 1519b15cd800SMatthew Wilcox * VM_FAULT_FALLBACK. 15209f141d6eSJan Kara */ 1521b15cd800SMatthew Wilcox entry = grab_mapping_entry(&xas, mapping, DAX_PMD); 1522b15cd800SMatthew Wilcox if (xa_is_internal(entry)) { 1523b15cd800SMatthew Wilcox result = xa_to_internal(entry); 1524876f2946SRoss Zwisler goto fallback; 1525b15cd800SMatthew Wilcox } 1526876f2946SRoss Zwisler 1527876f2946SRoss Zwisler /* 1528e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1529e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1530e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1531e2093926SRoss Zwisler * retried. 1532e2093926SRoss Zwisler */ 1533e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1534e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1535e2093926SRoss Zwisler result = 0; 1536e2093926SRoss Zwisler goto unlock_entry; 1537e2093926SRoss Zwisler } 1538e2093926SRoss Zwisler 1539e2093926SRoss Zwisler /* 1540876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1541876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1542876f2946SRoss Zwisler * to look up our filesystem block. 1543876f2946SRoss Zwisler */ 1544b15cd800SMatthew Wilcox pos = (loff_t)xas.xa_index << PAGE_SHIFT; 1545876f2946SRoss Zwisler error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); 1546876f2946SRoss Zwisler if (error) 1547876f2946SRoss Zwisler goto unlock_entry; 1548876f2946SRoss Zwisler 1549876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 15509f141d6eSJan Kara goto finish_iomap; 15519f141d6eSJan Kara 1552aaa422c4SDan Williams sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); 1553caa51d26SJan Kara 1554642261acSRoss Zwisler switch (iomap.type) { 1555642261acSRoss Zwisler case IOMAP_MAPPED: 1556302a5e31SJan Kara error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); 1557302a5e31SJan Kara if (error < 0) 1558302a5e31SJan Kara goto finish_iomap; 1559302a5e31SJan Kara 1560b15cd800SMatthew Wilcox entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn, 15613159f943SMatthew Wilcox DAX_PMD, write && !sync); 1562302a5e31SJan Kara 1563caa51d26SJan Kara /* 1564caa51d26SJan Kara * If we are doing synchronous page fault and inode needs fsync, 1565caa51d26SJan Kara * we can insert PMD into page tables only after that happens. 1566caa51d26SJan Kara * Skip insertion for now and return the pfn so that caller can 1567caa51d26SJan Kara * insert it after fsync is done. 1568caa51d26SJan Kara */ 1569caa51d26SJan Kara if (sync) { 1570caa51d26SJan Kara if (WARN_ON_ONCE(!pfnp)) 1571caa51d26SJan Kara goto finish_iomap; 1572caa51d26SJan Kara *pfnp = pfn; 1573caa51d26SJan Kara result = VM_FAULT_NEEDDSYNC; 1574caa51d26SJan Kara goto finish_iomap; 1575caa51d26SJan Kara } 1576caa51d26SJan Kara 1577302a5e31SJan Kara trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); 1578fce86ff5SDan Williams result = vmf_insert_pfn_pmd(vmf, pfn, write); 1579642261acSRoss Zwisler break; 1580642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1581642261acSRoss Zwisler case IOMAP_HOLE: 1582642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1583876f2946SRoss Zwisler break; 1584b15cd800SMatthew Wilcox result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry); 1585642261acSRoss Zwisler break; 1586642261acSRoss Zwisler default: 1587642261acSRoss Zwisler WARN_ON_ONCE(1); 1588642261acSRoss Zwisler break; 1589642261acSRoss Zwisler } 1590642261acSRoss Zwisler 15919f141d6eSJan Kara finish_iomap: 15929f141d6eSJan Kara if (ops->iomap_end) { 15939f141d6eSJan Kara int copied = PMD_SIZE; 15949f141d6eSJan Kara 15959f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 15969f141d6eSJan Kara copied = 0; 15979f141d6eSJan Kara /* 15989f141d6eSJan Kara * The fault is done by now and there's no way back (other 15999f141d6eSJan Kara * thread may be already happily using PMD we have installed). 16009f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 16019f141d6eSJan Kara * with it. 16029f141d6eSJan Kara */ 16039f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 16049f141d6eSJan Kara &iomap); 16059f141d6eSJan Kara } 1606876f2946SRoss Zwisler unlock_entry: 1607b15cd800SMatthew Wilcox dax_unlock_entry(&xas, entry); 1608642261acSRoss Zwisler fallback: 1609642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1610d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1611642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1612642261acSRoss Zwisler } 1613282a8e03SRoss Zwisler out: 1614f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1615642261acSRoss Zwisler return result; 1616642261acSRoss Zwisler } 1617a2d58167SDave Jiang #else 1618ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, 161901cddfe9SArnd Bergmann const struct iomap_ops *ops) 1620a2d58167SDave Jiang { 1621a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1622a2d58167SDave Jiang } 1623642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1624a2d58167SDave Jiang 1625a2d58167SDave Jiang /** 1626a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1627a2d58167SDave Jiang * @vmf: The description of the fault 1628cec04e8cSJan Kara * @pe_size: Size of the page to fault in 16299a0dd422SJan Kara * @pfnp: PFN to insert for synchronous faults if fsync is required 1630c0b24625SJan Kara * @iomap_errp: Storage for detailed error code in case of error 1631cec04e8cSJan Kara * @ops: Iomap ops passed from the file system 1632a2d58167SDave Jiang * 1633a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1634a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1635a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1636a2d58167SDave Jiang * successfully. 1637a2d58167SDave Jiang */ 1638ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1639c0b24625SJan Kara pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) 1640a2d58167SDave Jiang { 1641c791ace1SDave Jiang switch (pe_size) { 1642c791ace1SDave Jiang case PE_SIZE_PTE: 1643c0b24625SJan Kara return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); 1644c791ace1SDave Jiang case PE_SIZE_PMD: 16459a0dd422SJan Kara return dax_iomap_pmd_fault(vmf, pfnp, ops); 1646a2d58167SDave Jiang default: 1647a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1648a2d58167SDave Jiang } 1649a2d58167SDave Jiang } 1650a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 165171eab6dfSJan Kara 1652a77d19f4SMatthew Wilcox /* 165371eab6dfSJan Kara * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables 165471eab6dfSJan Kara * @vmf: The description of the fault 165571eab6dfSJan Kara * @pfn: PFN to insert 1656cfc93c6cSMatthew Wilcox * @order: Order of entry to insert. 165771eab6dfSJan Kara * 1658a77d19f4SMatthew Wilcox * This function inserts a writeable PTE or PMD entry into the page tables 1659a77d19f4SMatthew Wilcox * for an mmaped DAX file. It also marks the page cache entry as dirty. 166071eab6dfSJan Kara */ 1661cfc93c6cSMatthew Wilcox static vm_fault_t 1662cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order) 166371eab6dfSJan Kara { 166471eab6dfSJan Kara struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1665cfc93c6cSMatthew Wilcox XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order); 1666cfc93c6cSMatthew Wilcox void *entry; 1667ab77dab4SSouptick Joarder vm_fault_t ret; 166871eab6dfSJan Kara 1669cfc93c6cSMatthew Wilcox xas_lock_irq(&xas); 1670cfc93c6cSMatthew Wilcox entry = get_unlocked_entry(&xas); 167171eab6dfSJan Kara /* Did we race with someone splitting entry or so? */ 167271eab6dfSJan Kara if (!entry || 1673cfc93c6cSMatthew Wilcox (order == 0 && !dax_is_pte_entry(entry)) || 16740e40de03SMatthew Wilcox (order == PMD_ORDER && !dax_is_pmd_entry(entry))) { 1675cfc93c6cSMatthew Wilcox put_unlocked_entry(&xas, entry); 1676cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 167771eab6dfSJan Kara trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, 167871eab6dfSJan Kara VM_FAULT_NOPAGE); 167971eab6dfSJan Kara return VM_FAULT_NOPAGE; 168071eab6dfSJan Kara } 1681cfc93c6cSMatthew Wilcox xas_set_mark(&xas, PAGECACHE_TAG_DIRTY); 1682cfc93c6cSMatthew Wilcox dax_lock_entry(&xas, entry); 1683cfc93c6cSMatthew Wilcox xas_unlock_irq(&xas); 1684cfc93c6cSMatthew Wilcox if (order == 0) 1685ab77dab4SSouptick Joarder ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); 168671eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD 1687cfc93c6cSMatthew Wilcox else if (order == PMD_ORDER) 1688fce86ff5SDan Williams ret = vmf_insert_pfn_pmd(vmf, pfn, FAULT_FLAG_WRITE); 168971eab6dfSJan Kara #endif 1690cfc93c6cSMatthew Wilcox else 1691ab77dab4SSouptick Joarder ret = VM_FAULT_FALLBACK; 1692cfc93c6cSMatthew Wilcox dax_unlock_entry(&xas, entry); 1693ab77dab4SSouptick Joarder trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret); 1694ab77dab4SSouptick Joarder return ret; 169571eab6dfSJan Kara } 169671eab6dfSJan Kara 169771eab6dfSJan Kara /** 169871eab6dfSJan Kara * dax_finish_sync_fault - finish synchronous page fault 169971eab6dfSJan Kara * @vmf: The description of the fault 170071eab6dfSJan Kara * @pe_size: Size of entry to be inserted 170171eab6dfSJan Kara * @pfn: PFN to insert 170271eab6dfSJan Kara * 170371eab6dfSJan Kara * This function ensures that the file range touched by the page fault is 170471eab6dfSJan Kara * stored persistently on the media and handles inserting of appropriate page 170571eab6dfSJan Kara * table entry. 170671eab6dfSJan Kara */ 1707ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 1708ab77dab4SSouptick Joarder enum page_entry_size pe_size, pfn_t pfn) 170971eab6dfSJan Kara { 171071eab6dfSJan Kara int err; 171171eab6dfSJan Kara loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; 1712cfc93c6cSMatthew Wilcox unsigned int order = pe_order(pe_size); 1713cfc93c6cSMatthew Wilcox size_t len = PAGE_SIZE << order; 171471eab6dfSJan Kara 171571eab6dfSJan Kara err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); 171671eab6dfSJan Kara if (err) 171771eab6dfSJan Kara return VM_FAULT_SIGBUS; 1718cfc93c6cSMatthew Wilcox return dax_insert_pfn_mkwrite(vmf, pfn, order); 171971eab6dfSJan Kara } 172071eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault); 1721