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 41ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */ 42ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12 43ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) 44ac401cc7SJan Kara 45ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; 46ac401cc7SJan Kara 47ac401cc7SJan Kara static int __init init_dax_wait_table(void) 48ac401cc7SJan Kara { 49ac401cc7SJan Kara int i; 50ac401cc7SJan Kara 51ac401cc7SJan Kara for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) 52ac401cc7SJan Kara init_waitqueue_head(wait_table + i); 53ac401cc7SJan Kara return 0; 54ac401cc7SJan Kara } 55ac401cc7SJan Kara fs_initcall(init_dax_wait_table); 56ac401cc7SJan Kara 57642261acSRoss Zwisler static int dax_is_pmd_entry(void *entry) 58642261acSRoss Zwisler { 59642261acSRoss Zwisler return (unsigned long)entry & RADIX_DAX_PMD; 60642261acSRoss Zwisler } 61642261acSRoss Zwisler 62642261acSRoss Zwisler static int dax_is_pte_entry(void *entry) 63642261acSRoss Zwisler { 64642261acSRoss Zwisler return !((unsigned long)entry & RADIX_DAX_PMD); 65642261acSRoss Zwisler } 66642261acSRoss Zwisler 67642261acSRoss Zwisler static int dax_is_zero_entry(void *entry) 68642261acSRoss Zwisler { 69642261acSRoss Zwisler return (unsigned long)entry & RADIX_DAX_HZP; 70642261acSRoss Zwisler } 71642261acSRoss Zwisler 72642261acSRoss Zwisler static int dax_is_empty_entry(void *entry) 73642261acSRoss Zwisler { 74642261acSRoss Zwisler return (unsigned long)entry & RADIX_DAX_EMPTY; 75642261acSRoss Zwisler } 76642261acSRoss Zwisler 77f7ca90b1SMatthew Wilcox /* 78ac401cc7SJan Kara * DAX radix tree locking 79ac401cc7SJan Kara */ 80ac401cc7SJan Kara struct exceptional_entry_key { 81ac401cc7SJan Kara struct address_space *mapping; 8263e95b5cSRoss Zwisler pgoff_t entry_start; 83ac401cc7SJan Kara }; 84ac401cc7SJan Kara 85ac401cc7SJan Kara struct wait_exceptional_entry_queue { 86ac6424b9SIngo Molnar wait_queue_entry_t wait; 87ac401cc7SJan Kara struct exceptional_entry_key key; 88ac401cc7SJan Kara }; 89ac401cc7SJan Kara 9063e95b5cSRoss Zwisler static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping, 9163e95b5cSRoss Zwisler pgoff_t index, void *entry, struct exceptional_entry_key *key) 9263e95b5cSRoss Zwisler { 9363e95b5cSRoss Zwisler unsigned long hash; 9463e95b5cSRoss Zwisler 9563e95b5cSRoss Zwisler /* 9663e95b5cSRoss Zwisler * If 'entry' is a PMD, align the 'index' that we use for the wait 9763e95b5cSRoss Zwisler * queue to the start of that PMD. This ensures that all offsets in 9863e95b5cSRoss Zwisler * the range covered by the PMD map to the same bit lock. 9963e95b5cSRoss Zwisler */ 100642261acSRoss Zwisler if (dax_is_pmd_entry(entry)) 10163e95b5cSRoss Zwisler index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1); 10263e95b5cSRoss Zwisler 10363e95b5cSRoss Zwisler key->mapping = mapping; 10463e95b5cSRoss Zwisler key->entry_start = index; 10563e95b5cSRoss Zwisler 10663e95b5cSRoss Zwisler hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS); 10763e95b5cSRoss Zwisler return wait_table + hash; 10863e95b5cSRoss Zwisler } 10963e95b5cSRoss Zwisler 110ac6424b9SIngo Molnar static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode, 111ac401cc7SJan Kara int sync, void *keyp) 112ac401cc7SJan Kara { 113ac401cc7SJan Kara struct exceptional_entry_key *key = keyp; 114ac401cc7SJan Kara struct wait_exceptional_entry_queue *ewait = 115ac401cc7SJan Kara container_of(wait, struct wait_exceptional_entry_queue, wait); 116ac401cc7SJan Kara 117ac401cc7SJan Kara if (key->mapping != ewait->key.mapping || 11863e95b5cSRoss Zwisler key->entry_start != ewait->key.entry_start) 119ac401cc7SJan Kara return 0; 120ac401cc7SJan Kara return autoremove_wake_function(wait, mode, sync, NULL); 121ac401cc7SJan Kara } 122ac401cc7SJan Kara 123ac401cc7SJan Kara /* 124e30331ffSRoss Zwisler * We do not necessarily hold the mapping->tree_lock when we call this 125e30331ffSRoss Zwisler * function so it is possible that 'entry' is no longer a valid item in the 126e30331ffSRoss Zwisler * radix tree. This is okay because all we really need to do is to find the 127e30331ffSRoss Zwisler * correct waitqueue where tasks might be waiting for that old 'entry' and 128e30331ffSRoss Zwisler * wake them. 129e30331ffSRoss Zwisler */ 130e30331ffSRoss Zwisler void dax_wake_mapping_entry_waiter(struct address_space *mapping, 131e30331ffSRoss Zwisler pgoff_t index, void *entry, bool wake_all) 132e30331ffSRoss Zwisler { 133e30331ffSRoss Zwisler struct exceptional_entry_key key; 134e30331ffSRoss Zwisler wait_queue_head_t *wq; 135e30331ffSRoss Zwisler 136e30331ffSRoss Zwisler wq = dax_entry_waitqueue(mapping, index, entry, &key); 137e30331ffSRoss Zwisler 138e30331ffSRoss Zwisler /* 139e30331ffSRoss Zwisler * Checking for locked entry and prepare_to_wait_exclusive() happens 140e30331ffSRoss Zwisler * under mapping->tree_lock, ditto for entry handling in our callers. 141e30331ffSRoss Zwisler * So at this point all tasks that could have seen our entry locked 142e30331ffSRoss Zwisler * must be in the waitqueue and the following check will see them. 143e30331ffSRoss Zwisler */ 144e30331ffSRoss Zwisler if (waitqueue_active(wq)) 145e30331ffSRoss Zwisler __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); 146e30331ffSRoss Zwisler } 147e30331ffSRoss Zwisler 148e30331ffSRoss Zwisler /* 149ac401cc7SJan Kara * Check whether the given slot is locked. The function must be called with 150ac401cc7SJan Kara * mapping->tree_lock held 151ac401cc7SJan Kara */ 152ac401cc7SJan Kara static inline int slot_locked(struct address_space *mapping, void **slot) 153ac401cc7SJan Kara { 154ac401cc7SJan Kara unsigned long entry = (unsigned long) 155ac401cc7SJan Kara radix_tree_deref_slot_protected(slot, &mapping->tree_lock); 156ac401cc7SJan Kara return entry & RADIX_DAX_ENTRY_LOCK; 157ac401cc7SJan Kara } 158ac401cc7SJan Kara 159ac401cc7SJan Kara /* 160ac401cc7SJan Kara * Mark the given slot is locked. The function must be called with 161ac401cc7SJan Kara * mapping->tree_lock held 162ac401cc7SJan Kara */ 163ac401cc7SJan Kara static inline void *lock_slot(struct address_space *mapping, void **slot) 164ac401cc7SJan Kara { 165ac401cc7SJan Kara unsigned long entry = (unsigned long) 166ac401cc7SJan Kara radix_tree_deref_slot_protected(slot, &mapping->tree_lock); 167ac401cc7SJan Kara 168ac401cc7SJan Kara entry |= RADIX_DAX_ENTRY_LOCK; 1696d75f366SJohannes Weiner radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry); 170ac401cc7SJan Kara return (void *)entry; 171ac401cc7SJan Kara } 172ac401cc7SJan Kara 173ac401cc7SJan Kara /* 174ac401cc7SJan Kara * Mark the given slot is unlocked. The function must be called with 175ac401cc7SJan Kara * mapping->tree_lock held 176ac401cc7SJan Kara */ 177ac401cc7SJan Kara static inline void *unlock_slot(struct address_space *mapping, void **slot) 178ac401cc7SJan Kara { 179ac401cc7SJan Kara unsigned long entry = (unsigned long) 180ac401cc7SJan Kara radix_tree_deref_slot_protected(slot, &mapping->tree_lock); 181ac401cc7SJan Kara 182ac401cc7SJan Kara entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK; 1836d75f366SJohannes Weiner radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry); 184ac401cc7SJan Kara return (void *)entry; 185ac401cc7SJan Kara } 186ac401cc7SJan Kara 187ac401cc7SJan Kara /* 188ac401cc7SJan Kara * Lookup entry in radix tree, wait for it to become unlocked if it is 189ac401cc7SJan Kara * exceptional entry and return it. The caller must call 190ac401cc7SJan Kara * put_unlocked_mapping_entry() when he decided not to lock the entry or 191ac401cc7SJan Kara * put_locked_mapping_entry() when he locked the entry and now wants to 192ac401cc7SJan Kara * unlock it. 193ac401cc7SJan Kara * 194ac401cc7SJan Kara * The function must be called with mapping->tree_lock held. 195ac401cc7SJan Kara */ 196ac401cc7SJan Kara static void *get_unlocked_mapping_entry(struct address_space *mapping, 197ac401cc7SJan Kara pgoff_t index, void ***slotp) 198ac401cc7SJan Kara { 199e3ad61c6SRoss Zwisler void *entry, **slot; 200ac401cc7SJan Kara struct wait_exceptional_entry_queue ewait; 20163e95b5cSRoss Zwisler wait_queue_head_t *wq; 202ac401cc7SJan Kara 203ac401cc7SJan Kara init_wait(&ewait.wait); 204ac401cc7SJan Kara ewait.wait.func = wake_exceptional_entry_func; 205ac401cc7SJan Kara 206ac401cc7SJan Kara for (;;) { 207e3ad61c6SRoss Zwisler entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, 208ac401cc7SJan Kara &slot); 209e3ad61c6SRoss Zwisler if (!entry || !radix_tree_exceptional_entry(entry) || 210ac401cc7SJan Kara !slot_locked(mapping, slot)) { 211ac401cc7SJan Kara if (slotp) 212ac401cc7SJan Kara *slotp = slot; 213e3ad61c6SRoss Zwisler return entry; 214ac401cc7SJan Kara } 21563e95b5cSRoss Zwisler 21663e95b5cSRoss Zwisler wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key); 217ac401cc7SJan Kara prepare_to_wait_exclusive(wq, &ewait.wait, 218ac401cc7SJan Kara TASK_UNINTERRUPTIBLE); 219ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 220ac401cc7SJan Kara schedule(); 221ac401cc7SJan Kara finish_wait(wq, &ewait.wait); 222ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 223ac401cc7SJan Kara } 224ac401cc7SJan Kara } 225ac401cc7SJan Kara 226b1aa812bSJan Kara static void dax_unlock_mapping_entry(struct address_space *mapping, 227b1aa812bSJan Kara pgoff_t index) 228b1aa812bSJan Kara { 229b1aa812bSJan Kara void *entry, **slot; 230b1aa812bSJan Kara 231b1aa812bSJan Kara spin_lock_irq(&mapping->tree_lock); 232b1aa812bSJan Kara entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot); 233b1aa812bSJan Kara if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) || 234b1aa812bSJan Kara !slot_locked(mapping, slot))) { 235b1aa812bSJan Kara spin_unlock_irq(&mapping->tree_lock); 236b1aa812bSJan Kara return; 237b1aa812bSJan Kara } 238b1aa812bSJan Kara unlock_slot(mapping, slot); 239b1aa812bSJan Kara spin_unlock_irq(&mapping->tree_lock); 240b1aa812bSJan Kara dax_wake_mapping_entry_waiter(mapping, index, entry, false); 241b1aa812bSJan Kara } 242b1aa812bSJan Kara 243ac401cc7SJan Kara static void put_locked_mapping_entry(struct address_space *mapping, 244ac401cc7SJan Kara pgoff_t index, void *entry) 245ac401cc7SJan Kara { 246ac401cc7SJan Kara if (!radix_tree_exceptional_entry(entry)) { 247ac401cc7SJan Kara unlock_page(entry); 248ac401cc7SJan Kara put_page(entry); 249ac401cc7SJan Kara } else { 250bc2466e4SJan Kara dax_unlock_mapping_entry(mapping, index); 251ac401cc7SJan Kara } 252ac401cc7SJan Kara } 253ac401cc7SJan Kara 254ac401cc7SJan Kara /* 255ac401cc7SJan Kara * Called when we are done with radix tree entry we looked up via 256ac401cc7SJan Kara * get_unlocked_mapping_entry() and which we didn't lock in the end. 257ac401cc7SJan Kara */ 258ac401cc7SJan Kara static void put_unlocked_mapping_entry(struct address_space *mapping, 259ac401cc7SJan Kara pgoff_t index, void *entry) 260ac401cc7SJan Kara { 261ac401cc7SJan Kara if (!radix_tree_exceptional_entry(entry)) 262ac401cc7SJan Kara return; 263ac401cc7SJan Kara 264ac401cc7SJan Kara /* We have to wake up next waiter for the radix tree entry lock */ 265422476c4SRoss Zwisler dax_wake_mapping_entry_waiter(mapping, index, entry, false); 266422476c4SRoss Zwisler } 267422476c4SRoss Zwisler 268ac401cc7SJan Kara /* 269ac401cc7SJan Kara * Find radix tree entry at given index. If it points to a page, return with 270ac401cc7SJan Kara * the page locked. If it points to the exceptional entry, return with the 271ac401cc7SJan Kara * radix tree entry locked. If the radix tree doesn't contain given index, 272ac401cc7SJan Kara * create empty exceptional entry for the index and return with it locked. 273ac401cc7SJan Kara * 274642261acSRoss Zwisler * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will 275642261acSRoss Zwisler * either return that locked entry or will return an error. This error will 276642261acSRoss Zwisler * happen if there are any 4k entries (either zero pages or DAX entries) 277642261acSRoss Zwisler * within the 2MiB range that we are requesting. 278642261acSRoss Zwisler * 279642261acSRoss Zwisler * We always favor 4k entries over 2MiB entries. There isn't a flow where we 280642261acSRoss Zwisler * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB 281642261acSRoss Zwisler * insertion will fail if it finds any 4k entries already in the tree, and a 282642261acSRoss Zwisler * 4k insertion will cause an existing 2MiB entry to be unmapped and 283642261acSRoss Zwisler * downgraded to 4k entries. This happens for both 2MiB huge zero pages as 284642261acSRoss Zwisler * well as 2MiB empty entries. 285642261acSRoss Zwisler * 286642261acSRoss Zwisler * The exception to this downgrade path is for 2MiB DAX PMD entries that have 287642261acSRoss Zwisler * real storage backing them. We will leave these real 2MiB DAX entries in 288642261acSRoss Zwisler * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry. 289642261acSRoss Zwisler * 290ac401cc7SJan Kara * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For 291ac401cc7SJan Kara * persistent memory the benefit is doubtful. We can add that later if we can 292ac401cc7SJan Kara * show it helps. 293ac401cc7SJan Kara */ 294642261acSRoss Zwisler static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index, 295642261acSRoss Zwisler unsigned long size_flag) 296ac401cc7SJan Kara { 297642261acSRoss Zwisler bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */ 298e3ad61c6SRoss Zwisler void *entry, **slot; 299ac401cc7SJan Kara 300ac401cc7SJan Kara restart: 301ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 302e3ad61c6SRoss Zwisler entry = get_unlocked_mapping_entry(mapping, index, &slot); 303642261acSRoss Zwisler 304642261acSRoss Zwisler if (entry) { 305642261acSRoss Zwisler if (size_flag & RADIX_DAX_PMD) { 306642261acSRoss Zwisler if (!radix_tree_exceptional_entry(entry) || 307642261acSRoss Zwisler dax_is_pte_entry(entry)) { 308642261acSRoss Zwisler put_unlocked_mapping_entry(mapping, index, 309642261acSRoss Zwisler entry); 310642261acSRoss Zwisler entry = ERR_PTR(-EEXIST); 311642261acSRoss Zwisler goto out_unlock; 312642261acSRoss Zwisler } 313642261acSRoss Zwisler } else { /* trying to grab a PTE entry */ 314642261acSRoss Zwisler if (radix_tree_exceptional_entry(entry) && 315642261acSRoss Zwisler dax_is_pmd_entry(entry) && 316642261acSRoss Zwisler (dax_is_zero_entry(entry) || 317642261acSRoss Zwisler dax_is_empty_entry(entry))) { 318642261acSRoss Zwisler pmd_downgrade = true; 319642261acSRoss Zwisler } 320642261acSRoss Zwisler } 321642261acSRoss Zwisler } 322642261acSRoss Zwisler 323ac401cc7SJan Kara /* No entry for given index? Make sure radix tree is big enough. */ 324642261acSRoss Zwisler if (!entry || pmd_downgrade) { 325ac401cc7SJan Kara int err; 326ac401cc7SJan Kara 327642261acSRoss Zwisler if (pmd_downgrade) { 328642261acSRoss Zwisler /* 329642261acSRoss Zwisler * Make sure 'entry' remains valid while we drop 330642261acSRoss Zwisler * mapping->tree_lock. 331642261acSRoss Zwisler */ 332642261acSRoss Zwisler entry = lock_slot(mapping, slot); 333642261acSRoss Zwisler } 334642261acSRoss Zwisler 335ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 336642261acSRoss Zwisler /* 337642261acSRoss Zwisler * Besides huge zero pages the only other thing that gets 338642261acSRoss Zwisler * downgraded are empty entries which don't need to be 339642261acSRoss Zwisler * unmapped. 340642261acSRoss Zwisler */ 341642261acSRoss Zwisler if (pmd_downgrade && dax_is_zero_entry(entry)) 342642261acSRoss Zwisler unmap_mapping_range(mapping, 343642261acSRoss Zwisler (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0); 344642261acSRoss Zwisler 3450cb80b48SJan Kara err = radix_tree_preload( 3460cb80b48SJan Kara mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM); 3470cb80b48SJan Kara if (err) { 3480cb80b48SJan Kara if (pmd_downgrade) 3490cb80b48SJan Kara put_locked_mapping_entry(mapping, index, entry); 3500cb80b48SJan Kara return ERR_PTR(err); 3510cb80b48SJan Kara } 352ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 353642261acSRoss Zwisler 354e11f8b7bSRoss Zwisler if (!entry) { 355e11f8b7bSRoss Zwisler /* 356e11f8b7bSRoss Zwisler * We needed to drop the page_tree lock while calling 357e11f8b7bSRoss Zwisler * radix_tree_preload() and we didn't have an entry to 358e11f8b7bSRoss Zwisler * lock. See if another thread inserted an entry at 359e11f8b7bSRoss Zwisler * our index during this time. 360e11f8b7bSRoss Zwisler */ 361e11f8b7bSRoss Zwisler entry = __radix_tree_lookup(&mapping->page_tree, index, 362e11f8b7bSRoss Zwisler NULL, &slot); 363e11f8b7bSRoss Zwisler if (entry) { 364e11f8b7bSRoss Zwisler radix_tree_preload_end(); 365e11f8b7bSRoss Zwisler spin_unlock_irq(&mapping->tree_lock); 366e11f8b7bSRoss Zwisler goto restart; 367e11f8b7bSRoss Zwisler } 368e11f8b7bSRoss Zwisler } 369e11f8b7bSRoss Zwisler 370642261acSRoss Zwisler if (pmd_downgrade) { 371642261acSRoss Zwisler radix_tree_delete(&mapping->page_tree, index); 372642261acSRoss Zwisler mapping->nrexceptional--; 373642261acSRoss Zwisler dax_wake_mapping_entry_waiter(mapping, index, entry, 374642261acSRoss Zwisler true); 375642261acSRoss Zwisler } 376642261acSRoss Zwisler 377642261acSRoss Zwisler entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY); 378642261acSRoss Zwisler 379642261acSRoss Zwisler err = __radix_tree_insert(&mapping->page_tree, index, 380642261acSRoss Zwisler dax_radix_order(entry), entry); 381ac401cc7SJan Kara radix_tree_preload_end(); 382ac401cc7SJan Kara if (err) { 383ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 384642261acSRoss Zwisler /* 385e11f8b7bSRoss Zwisler * Our insertion of a DAX entry failed, most likely 386e11f8b7bSRoss Zwisler * because we were inserting a PMD entry and it 387e11f8b7bSRoss Zwisler * collided with a PTE sized entry at a different 388e11f8b7bSRoss Zwisler * index in the PMD range. We haven't inserted 389e11f8b7bSRoss Zwisler * anything into the radix tree and have no waiters to 390e11f8b7bSRoss Zwisler * wake. 391642261acSRoss Zwisler */ 392ac401cc7SJan Kara return ERR_PTR(err); 393ac401cc7SJan Kara } 394ac401cc7SJan Kara /* Good, we have inserted empty locked entry into the tree. */ 395ac401cc7SJan Kara mapping->nrexceptional++; 396ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 397e3ad61c6SRoss Zwisler return entry; 398ac401cc7SJan Kara } 399ac401cc7SJan Kara /* Normal page in radix tree? */ 400e3ad61c6SRoss Zwisler if (!radix_tree_exceptional_entry(entry)) { 401e3ad61c6SRoss Zwisler struct page *page = entry; 402ac401cc7SJan Kara 403ac401cc7SJan Kara get_page(page); 404ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 405ac401cc7SJan Kara lock_page(page); 406ac401cc7SJan Kara /* Page got truncated? Retry... */ 407ac401cc7SJan Kara if (unlikely(page->mapping != mapping)) { 408ac401cc7SJan Kara unlock_page(page); 409ac401cc7SJan Kara put_page(page); 410ac401cc7SJan Kara goto restart; 411ac401cc7SJan Kara } 412ac401cc7SJan Kara return page; 413ac401cc7SJan Kara } 414e3ad61c6SRoss Zwisler entry = lock_slot(mapping, slot); 415642261acSRoss Zwisler out_unlock: 416ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 417e3ad61c6SRoss Zwisler return entry; 418ac401cc7SJan Kara } 419ac401cc7SJan Kara 420c6dcf52cSJan Kara static int __dax_invalidate_mapping_entry(struct address_space *mapping, 421c6dcf52cSJan Kara pgoff_t index, bool trunc) 422c6dcf52cSJan Kara { 423c6dcf52cSJan Kara int ret = 0; 424c6dcf52cSJan Kara void *entry; 425c6dcf52cSJan Kara struct radix_tree_root *page_tree = &mapping->page_tree; 426c6dcf52cSJan Kara 427c6dcf52cSJan Kara spin_lock_irq(&mapping->tree_lock); 428c6dcf52cSJan Kara entry = get_unlocked_mapping_entry(mapping, index, NULL); 429c6dcf52cSJan Kara if (!entry || !radix_tree_exceptional_entry(entry)) 430c6dcf52cSJan Kara goto out; 431c6dcf52cSJan Kara if (!trunc && 432c6dcf52cSJan Kara (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) || 433c6dcf52cSJan Kara radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))) 434c6dcf52cSJan Kara goto out; 435c6dcf52cSJan Kara radix_tree_delete(page_tree, index); 436c6dcf52cSJan Kara mapping->nrexceptional--; 437c6dcf52cSJan Kara ret = 1; 438c6dcf52cSJan Kara out: 439c6dcf52cSJan Kara put_unlocked_mapping_entry(mapping, index, entry); 440c6dcf52cSJan Kara spin_unlock_irq(&mapping->tree_lock); 441c6dcf52cSJan Kara return ret; 442c6dcf52cSJan Kara } 443ac401cc7SJan Kara /* 444ac401cc7SJan Kara * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree 445ac401cc7SJan Kara * entry to get unlocked before deleting it. 446ac401cc7SJan Kara */ 447ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) 448ac401cc7SJan Kara { 449c6dcf52cSJan Kara int ret = __dax_invalidate_mapping_entry(mapping, index, true); 450ac401cc7SJan Kara 451ac401cc7SJan Kara /* 452ac401cc7SJan Kara * This gets called from truncate / punch_hole path. As such, the caller 453ac401cc7SJan Kara * must hold locks protecting against concurrent modifications of the 454ac401cc7SJan Kara * radix tree (usually fs-private i_mmap_sem for writing). Since the 455ac401cc7SJan Kara * caller has seen exceptional entry for this index, we better find it 456ac401cc7SJan Kara * at that index as well... 457ac401cc7SJan Kara */ 458c6dcf52cSJan Kara WARN_ON_ONCE(!ret); 459c6dcf52cSJan Kara return ret; 460ac401cc7SJan Kara } 461ac401cc7SJan Kara 462c6dcf52cSJan Kara /* 463c6dcf52cSJan Kara * Invalidate exceptional DAX entry if it is clean. 464c6dcf52cSJan Kara */ 465c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 466c6dcf52cSJan Kara pgoff_t index) 467c6dcf52cSJan Kara { 468c6dcf52cSJan Kara return __dax_invalidate_mapping_entry(mapping, index, false); 469ac401cc7SJan Kara } 470ac401cc7SJan Kara 471cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, 472cccbce67SDan Williams sector_t sector, size_t size, struct page *to, 473cccbce67SDan Williams unsigned long vaddr) 474f7ca90b1SMatthew Wilcox { 475cccbce67SDan Williams void *vto, *kaddr; 476cccbce67SDan Williams pgoff_t pgoff; 477cccbce67SDan Williams pfn_t pfn; 478cccbce67SDan Williams long rc; 479cccbce67SDan Williams int id; 480e2e05394SRoss Zwisler 481cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 482cccbce67SDan Williams if (rc) 483cccbce67SDan Williams return rc; 484cccbce67SDan Williams 485cccbce67SDan Williams id = dax_read_lock(); 486cccbce67SDan Williams rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn); 487cccbce67SDan Williams if (rc < 0) { 488cccbce67SDan Williams dax_read_unlock(id); 489cccbce67SDan Williams return rc; 490cccbce67SDan Williams } 491f7ca90b1SMatthew Wilcox vto = kmap_atomic(to); 492cccbce67SDan Williams copy_user_page(vto, (void __force *)kaddr, vaddr, to); 493f7ca90b1SMatthew Wilcox kunmap_atomic(vto); 494cccbce67SDan Williams dax_read_unlock(id); 495f7ca90b1SMatthew Wilcox return 0; 496f7ca90b1SMatthew Wilcox } 497f7ca90b1SMatthew Wilcox 498642261acSRoss Zwisler /* 499642261acSRoss Zwisler * By this point grab_mapping_entry() has ensured that we have a locked entry 500642261acSRoss Zwisler * of the appropriate size so we don't have to worry about downgrading PMDs to 501642261acSRoss Zwisler * PTEs. If we happen to be trying to insert a PTE and there is a PMD 502642261acSRoss Zwisler * already in the tree, we will skip the insertion and just dirty the PMD as 503642261acSRoss Zwisler * appropriate. 504642261acSRoss Zwisler */ 505ac401cc7SJan Kara static void *dax_insert_mapping_entry(struct address_space *mapping, 506ac401cc7SJan Kara struct vm_fault *vmf, 507642261acSRoss Zwisler void *entry, sector_t sector, 508642261acSRoss Zwisler unsigned long flags) 5099973c98eSRoss Zwisler { 5109973c98eSRoss Zwisler struct radix_tree_root *page_tree = &mapping->page_tree; 511ac401cc7SJan Kara int error = 0; 512ac401cc7SJan Kara bool hole_fill = false; 513ac401cc7SJan Kara void *new_entry; 514ac401cc7SJan Kara pgoff_t index = vmf->pgoff; 5159973c98eSRoss Zwisler 516ac401cc7SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 5179973c98eSRoss Zwisler __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); 5189973c98eSRoss Zwisler 519ac401cc7SJan Kara /* Replacing hole page with block mapping? */ 520ac401cc7SJan Kara if (!radix_tree_exceptional_entry(entry)) { 521ac401cc7SJan Kara hole_fill = true; 5229973c98eSRoss Zwisler /* 523ac401cc7SJan Kara * Unmap the page now before we remove it from page cache below. 524ac401cc7SJan Kara * The page is locked so it cannot be faulted in again. 5259973c98eSRoss Zwisler */ 526ac401cc7SJan Kara unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT, 527ac401cc7SJan Kara PAGE_SIZE, 0); 528ac401cc7SJan Kara error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM); 5299973c98eSRoss Zwisler if (error) 530ac401cc7SJan Kara return ERR_PTR(error); 531642261acSRoss Zwisler } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) { 532642261acSRoss Zwisler /* replacing huge zero page with PMD block mapping */ 533642261acSRoss Zwisler unmap_mapping_range(mapping, 534642261acSRoss Zwisler (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0); 535ac401cc7SJan Kara } 5369973c98eSRoss Zwisler 537ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 538642261acSRoss Zwisler new_entry = dax_radix_locked_entry(sector, flags); 539642261acSRoss Zwisler 540ac401cc7SJan Kara if (hole_fill) { 541ac401cc7SJan Kara __delete_from_page_cache(entry, NULL); 542ac401cc7SJan Kara /* Drop pagecache reference */ 543ac401cc7SJan Kara put_page(entry); 544642261acSRoss Zwisler error = __radix_tree_insert(page_tree, index, 545642261acSRoss Zwisler dax_radix_order(new_entry), new_entry); 546ac401cc7SJan Kara if (error) { 547ac401cc7SJan Kara new_entry = ERR_PTR(error); 548ac401cc7SJan Kara goto unlock; 549ac401cc7SJan Kara } 5509973c98eSRoss Zwisler mapping->nrexceptional++; 551642261acSRoss Zwisler } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { 552642261acSRoss Zwisler /* 553642261acSRoss Zwisler * Only swap our new entry into the radix tree if the current 554642261acSRoss Zwisler * entry is a zero page or an empty entry. If a normal PTE or 555642261acSRoss Zwisler * PMD entry is already in the tree, we leave it alone. This 556642261acSRoss Zwisler * means that if we are trying to insert a PTE and the 557642261acSRoss Zwisler * existing entry is a PMD, we will just leave the PMD in the 558642261acSRoss Zwisler * tree and dirty it if necessary. 559642261acSRoss Zwisler */ 560f7942430SJohannes Weiner struct radix_tree_node *node; 561ac401cc7SJan Kara void **slot; 562ac401cc7SJan Kara void *ret; 563ac401cc7SJan Kara 564f7942430SJohannes Weiner ret = __radix_tree_lookup(page_tree, index, &node, &slot); 565ac401cc7SJan Kara WARN_ON_ONCE(ret != entry); 5664d693d08SJohannes Weiner __radix_tree_replace(page_tree, node, slot, 5674d693d08SJohannes Weiner new_entry, NULL, NULL); 568ac401cc7SJan Kara } 569ac401cc7SJan Kara if (vmf->flags & FAULT_FLAG_WRITE) 5709973c98eSRoss Zwisler radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY); 5719973c98eSRoss Zwisler unlock: 5729973c98eSRoss Zwisler spin_unlock_irq(&mapping->tree_lock); 573ac401cc7SJan Kara if (hole_fill) { 574ac401cc7SJan Kara radix_tree_preload_end(); 575ac401cc7SJan Kara /* 576ac401cc7SJan Kara * We don't need hole page anymore, it has been replaced with 577ac401cc7SJan Kara * locked radix tree entry now. 578ac401cc7SJan Kara */ 579ac401cc7SJan Kara if (mapping->a_ops->freepage) 580ac401cc7SJan Kara mapping->a_ops->freepage(entry); 581ac401cc7SJan Kara unlock_page(entry); 582ac401cc7SJan Kara put_page(entry); 583ac401cc7SJan Kara } 584ac401cc7SJan Kara return new_entry; 5859973c98eSRoss Zwisler } 5869973c98eSRoss Zwisler 5874b4bb46dSJan Kara static inline unsigned long 5884b4bb46dSJan Kara pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) 5894b4bb46dSJan Kara { 5904b4bb46dSJan Kara unsigned long address; 5914b4bb46dSJan Kara 5924b4bb46dSJan Kara address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); 5934b4bb46dSJan Kara VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); 5944b4bb46dSJan Kara return address; 5954b4bb46dSJan Kara } 5964b4bb46dSJan Kara 5974b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */ 5984b4bb46dSJan Kara static void dax_mapping_entry_mkclean(struct address_space *mapping, 5994b4bb46dSJan Kara pgoff_t index, unsigned long pfn) 6004b4bb46dSJan Kara { 6014b4bb46dSJan Kara struct vm_area_struct *vma; 602f729c8c9SRoss Zwisler pte_t pte, *ptep = NULL; 603f729c8c9SRoss Zwisler pmd_t *pmdp = NULL; 6044b4bb46dSJan Kara spinlock_t *ptl; 6054b4bb46dSJan Kara 6064b4bb46dSJan Kara i_mmap_lock_read(mapping); 6074b4bb46dSJan Kara vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { 608a4d1a885SJérôme Glisse unsigned long address, start, end; 6094b4bb46dSJan Kara 6104b4bb46dSJan Kara cond_resched(); 6114b4bb46dSJan Kara 6124b4bb46dSJan Kara if (!(vma->vm_flags & VM_SHARED)) 6134b4bb46dSJan Kara continue; 6144b4bb46dSJan Kara 6154b4bb46dSJan Kara address = pgoff_address(index, vma); 616a4d1a885SJérôme Glisse 617a4d1a885SJérôme Glisse /* 618a4d1a885SJérôme Glisse * Note because we provide start/end to follow_pte_pmd it will 619a4d1a885SJérôme Glisse * call mmu_notifier_invalidate_range_start() on our behalf 620a4d1a885SJérôme Glisse * before taking any lock. 621a4d1a885SJérôme Glisse */ 622a4d1a885SJérôme Glisse if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl)) 6234b4bb46dSJan Kara continue; 624f729c8c9SRoss Zwisler 625f729c8c9SRoss Zwisler if (pmdp) { 626f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 627f729c8c9SRoss Zwisler pmd_t pmd; 628f729c8c9SRoss Zwisler 629f729c8c9SRoss Zwisler if (pfn != pmd_pfn(*pmdp)) 630f729c8c9SRoss Zwisler goto unlock_pmd; 631f729c8c9SRoss Zwisler if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) 632f729c8c9SRoss Zwisler goto unlock_pmd; 633f729c8c9SRoss Zwisler 634f729c8c9SRoss Zwisler flush_cache_page(vma, address, pfn); 635f729c8c9SRoss Zwisler pmd = pmdp_huge_clear_flush(vma, address, pmdp); 636f729c8c9SRoss Zwisler pmd = pmd_wrprotect(pmd); 637f729c8c9SRoss Zwisler pmd = pmd_mkclean(pmd); 638f729c8c9SRoss Zwisler set_pmd_at(vma->vm_mm, address, pmdp, pmd); 639a4d1a885SJérôme Glisse mmu_notifier_invalidate_range(vma->vm_mm, start, end); 640f729c8c9SRoss Zwisler unlock_pmd: 641f729c8c9SRoss Zwisler spin_unlock(ptl); 642f729c8c9SRoss Zwisler #endif 643f729c8c9SRoss Zwisler } else { 6444b4bb46dSJan Kara if (pfn != pte_pfn(*ptep)) 645f729c8c9SRoss Zwisler goto unlock_pte; 6464b4bb46dSJan Kara if (!pte_dirty(*ptep) && !pte_write(*ptep)) 647f729c8c9SRoss Zwisler goto unlock_pte; 6484b4bb46dSJan Kara 6494b4bb46dSJan Kara flush_cache_page(vma, address, pfn); 6504b4bb46dSJan Kara pte = ptep_clear_flush(vma, address, ptep); 6514b4bb46dSJan Kara pte = pte_wrprotect(pte); 6524b4bb46dSJan Kara pte = pte_mkclean(pte); 6534b4bb46dSJan Kara set_pte_at(vma->vm_mm, address, ptep, pte); 654a4d1a885SJérôme Glisse mmu_notifier_invalidate_range(vma->vm_mm, start, end); 655f729c8c9SRoss Zwisler unlock_pte: 6564b4bb46dSJan Kara pte_unmap_unlock(ptep, ptl); 657f729c8c9SRoss Zwisler } 6584b4bb46dSJan Kara 659a4d1a885SJérôme Glisse mmu_notifier_invalidate_range_end(vma->vm_mm, start, end); 6604b4bb46dSJan Kara } 6614b4bb46dSJan Kara i_mmap_unlock_read(mapping); 6624b4bb46dSJan Kara } 6634b4bb46dSJan Kara 6649973c98eSRoss Zwisler static int dax_writeback_one(struct block_device *bdev, 665cccbce67SDan Williams struct dax_device *dax_dev, struct address_space *mapping, 666cccbce67SDan Williams pgoff_t index, void *entry) 6679973c98eSRoss Zwisler { 6689973c98eSRoss Zwisler struct radix_tree_root *page_tree = &mapping->page_tree; 669cccbce67SDan Williams void *entry2, **slot, *kaddr; 670cccbce67SDan Williams long ret = 0, id; 671cccbce67SDan Williams sector_t sector; 672cccbce67SDan Williams pgoff_t pgoff; 673cccbce67SDan Williams size_t size; 674cccbce67SDan Williams pfn_t pfn; 6759973c98eSRoss Zwisler 6769973c98eSRoss Zwisler /* 677a6abc2c0SJan Kara * A page got tagged dirty in DAX mapping? Something is seriously 678a6abc2c0SJan Kara * wrong. 6799973c98eSRoss Zwisler */ 680a6abc2c0SJan Kara if (WARN_ON(!radix_tree_exceptional_entry(entry))) 681a6abc2c0SJan Kara return -EIO; 6829973c98eSRoss Zwisler 683a6abc2c0SJan Kara spin_lock_irq(&mapping->tree_lock); 684a6abc2c0SJan Kara entry2 = get_unlocked_mapping_entry(mapping, index, &slot); 685a6abc2c0SJan Kara /* Entry got punched out / reallocated? */ 686a6abc2c0SJan Kara if (!entry2 || !radix_tree_exceptional_entry(entry2)) 687a6abc2c0SJan Kara goto put_unlocked; 688a6abc2c0SJan Kara /* 689a6abc2c0SJan Kara * Entry got reallocated elsewhere? No need to writeback. We have to 690a6abc2c0SJan Kara * compare sectors as we must not bail out due to difference in lockbit 691a6abc2c0SJan Kara * or entry type. 692a6abc2c0SJan Kara */ 693a6abc2c0SJan Kara if (dax_radix_sector(entry2) != dax_radix_sector(entry)) 694a6abc2c0SJan Kara goto put_unlocked; 695642261acSRoss Zwisler if (WARN_ON_ONCE(dax_is_empty_entry(entry) || 696642261acSRoss Zwisler dax_is_zero_entry(entry))) { 6979973c98eSRoss Zwisler ret = -EIO; 698a6abc2c0SJan Kara goto put_unlocked; 6999973c98eSRoss Zwisler } 7009973c98eSRoss Zwisler 701a6abc2c0SJan Kara /* Another fsync thread may have already written back this entry */ 702a6abc2c0SJan Kara if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)) 703a6abc2c0SJan Kara goto put_unlocked; 704a6abc2c0SJan Kara /* Lock the entry to serialize with page faults */ 705a6abc2c0SJan Kara entry = lock_slot(mapping, slot); 706a6abc2c0SJan Kara /* 707a6abc2c0SJan Kara * We can clear the tag now but we have to be careful so that concurrent 708a6abc2c0SJan Kara * dax_writeback_one() calls for the same index cannot finish before we 709a6abc2c0SJan Kara * actually flush the caches. This is achieved as the calls will look 710a6abc2c0SJan Kara * at the entry only under tree_lock and once they do that they will 711a6abc2c0SJan Kara * see the entry locked and wait for it to unlock. 712a6abc2c0SJan Kara */ 713a6abc2c0SJan Kara radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE); 714a6abc2c0SJan Kara spin_unlock_irq(&mapping->tree_lock); 715a6abc2c0SJan Kara 716642261acSRoss Zwisler /* 717642261acSRoss Zwisler * Even if dax_writeback_mapping_range() was given a wbc->range_start 718642261acSRoss Zwisler * in the middle of a PMD, the 'index' we are given will be aligned to 719642261acSRoss Zwisler * the start index of the PMD, as will the sector we pull from 720642261acSRoss Zwisler * 'entry'. This allows us to flush for PMD_SIZE and not have to 721642261acSRoss Zwisler * worry about partial PMD writebacks. 722642261acSRoss Zwisler */ 723cccbce67SDan Williams sector = dax_radix_sector(entry); 724cccbce67SDan Williams size = PAGE_SIZE << dax_radix_order(entry); 725cccbce67SDan Williams 726cccbce67SDan Williams id = dax_read_lock(); 727cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 728cccbce67SDan Williams if (ret) 729cccbce67SDan Williams goto dax_unlock; 7309973c98eSRoss Zwisler 7319973c98eSRoss Zwisler /* 732cccbce67SDan Williams * dax_direct_access() may sleep, so cannot hold tree_lock over 733cccbce67SDan Williams * its invocation. 7349973c98eSRoss Zwisler */ 735cccbce67SDan Williams ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn); 736cccbce67SDan Williams if (ret < 0) 737cccbce67SDan Williams goto dax_unlock; 7389973c98eSRoss Zwisler 739cccbce67SDan Williams if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) { 7409973c98eSRoss Zwisler ret = -EIO; 741cccbce67SDan Williams goto dax_unlock; 7429973c98eSRoss Zwisler } 7439973c98eSRoss Zwisler 744cccbce67SDan Williams dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn)); 7456318770aSDan Williams dax_flush(dax_dev, pgoff, kaddr, size); 7464b4bb46dSJan Kara /* 7474b4bb46dSJan Kara * After we have flushed the cache, we can clear the dirty tag. There 7484b4bb46dSJan Kara * cannot be new dirty data in the pfn after the flush has completed as 7494b4bb46dSJan Kara * the pfn mappings are writeprotected and fault waits for mapping 7504b4bb46dSJan Kara * entry lock. 7514b4bb46dSJan Kara */ 7524b4bb46dSJan Kara spin_lock_irq(&mapping->tree_lock); 7534b4bb46dSJan Kara radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY); 7544b4bb46dSJan Kara spin_unlock_irq(&mapping->tree_lock); 755f9bc3a07SRoss Zwisler trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT); 756cccbce67SDan Williams dax_unlock: 757cccbce67SDan Williams dax_read_unlock(id); 758a6abc2c0SJan Kara put_locked_mapping_entry(mapping, index, entry); 7599973c98eSRoss Zwisler return ret; 7609973c98eSRoss Zwisler 761a6abc2c0SJan Kara put_unlocked: 762a6abc2c0SJan Kara put_unlocked_mapping_entry(mapping, index, entry2); 7639973c98eSRoss Zwisler spin_unlock_irq(&mapping->tree_lock); 7649973c98eSRoss Zwisler return ret; 7659973c98eSRoss Zwisler } 7669973c98eSRoss Zwisler 7679973c98eSRoss Zwisler /* 7689973c98eSRoss Zwisler * Flush the mapping to the persistent domain within the byte range of [start, 7699973c98eSRoss Zwisler * end]. This is required by data integrity operations to ensure file data is 7709973c98eSRoss Zwisler * on persistent storage prior to completion of the operation. 7719973c98eSRoss Zwisler */ 7727f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping, 7737f6d5b52SRoss Zwisler struct block_device *bdev, struct writeback_control *wbc) 7749973c98eSRoss Zwisler { 7759973c98eSRoss Zwisler struct inode *inode = mapping->host; 776642261acSRoss Zwisler pgoff_t start_index, end_index; 7779973c98eSRoss Zwisler pgoff_t indices[PAGEVEC_SIZE]; 778cccbce67SDan Williams struct dax_device *dax_dev; 7799973c98eSRoss Zwisler struct pagevec pvec; 7809973c98eSRoss Zwisler bool done = false; 7819973c98eSRoss Zwisler int i, ret = 0; 7829973c98eSRoss Zwisler 7839973c98eSRoss Zwisler if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) 7849973c98eSRoss Zwisler return -EIO; 7859973c98eSRoss Zwisler 7867f6d5b52SRoss Zwisler if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) 7877f6d5b52SRoss Zwisler return 0; 7887f6d5b52SRoss Zwisler 789cccbce67SDan Williams dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); 790cccbce67SDan Williams if (!dax_dev) 791cccbce67SDan Williams return -EIO; 792cccbce67SDan Williams 79309cbfeafSKirill A. Shutemov start_index = wbc->range_start >> PAGE_SHIFT; 79409cbfeafSKirill A. Shutemov end_index = wbc->range_end >> PAGE_SHIFT; 7959973c98eSRoss Zwisler 796d14a3f48SRoss Zwisler trace_dax_writeback_range(inode, start_index, end_index); 797d14a3f48SRoss Zwisler 7989973c98eSRoss Zwisler tag_pages_for_writeback(mapping, start_index, end_index); 7999973c98eSRoss Zwisler 8009973c98eSRoss Zwisler pagevec_init(&pvec, 0); 8019973c98eSRoss Zwisler while (!done) { 8029973c98eSRoss Zwisler pvec.nr = find_get_entries_tag(mapping, start_index, 8039973c98eSRoss Zwisler PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE, 8049973c98eSRoss Zwisler pvec.pages, indices); 8059973c98eSRoss Zwisler 8069973c98eSRoss Zwisler if (pvec.nr == 0) 8079973c98eSRoss Zwisler break; 8089973c98eSRoss Zwisler 8099973c98eSRoss Zwisler for (i = 0; i < pvec.nr; i++) { 8109973c98eSRoss Zwisler if (indices[i] > end_index) { 8119973c98eSRoss Zwisler done = true; 8129973c98eSRoss Zwisler break; 8139973c98eSRoss Zwisler } 8149973c98eSRoss Zwisler 815cccbce67SDan Williams ret = dax_writeback_one(bdev, dax_dev, mapping, 816cccbce67SDan Williams indices[i], pvec.pages[i]); 817819ec6b9SJeff Layton if (ret < 0) { 818819ec6b9SJeff Layton mapping_set_error(mapping, ret); 819d14a3f48SRoss Zwisler goto out; 820d14a3f48SRoss Zwisler } 821d14a3f48SRoss Zwisler } 8221eb643d0SJan Kara start_index = indices[pvec.nr - 1] + 1; 823d14a3f48SRoss Zwisler } 824d14a3f48SRoss Zwisler out: 825cccbce67SDan Williams put_dax(dax_dev); 826d14a3f48SRoss Zwisler trace_dax_writeback_range_done(inode, start_index, end_index); 827d14a3f48SRoss Zwisler return (ret < 0 ? ret : 0); 8289973c98eSRoss Zwisler } 8299973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); 8309973c98eSRoss Zwisler 831ac401cc7SJan Kara static int dax_insert_mapping(struct address_space *mapping, 832cccbce67SDan Williams struct block_device *bdev, struct dax_device *dax_dev, 833cccbce67SDan Williams sector_t sector, size_t size, void **entryp, 834cccbce67SDan Williams struct vm_area_struct *vma, struct vm_fault *vmf) 835f7ca90b1SMatthew Wilcox { 8361a29d85eSJan Kara unsigned long vaddr = vmf->address; 837ac401cc7SJan Kara void *entry = *entryp; 838cccbce67SDan Williams void *ret, *kaddr; 839cccbce67SDan Williams pgoff_t pgoff; 840cccbce67SDan Williams int id, rc; 841cccbce67SDan Williams pfn_t pfn; 842f7ca90b1SMatthew Wilcox 843cccbce67SDan Williams rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); 844cccbce67SDan Williams if (rc) 845cccbce67SDan Williams return rc; 846f7ca90b1SMatthew Wilcox 847cccbce67SDan Williams id = dax_read_lock(); 848cccbce67SDan Williams rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn); 849cccbce67SDan Williams if (rc < 0) { 850cccbce67SDan Williams dax_read_unlock(id); 851cccbce67SDan Williams return rc; 852cccbce67SDan Williams } 853cccbce67SDan Williams dax_read_unlock(id); 854cccbce67SDan Williams 855cccbce67SDan Williams ret = dax_insert_mapping_entry(mapping, vmf, entry, sector, 0); 8564d9a2c87SJan Kara if (IS_ERR(ret)) 8574d9a2c87SJan Kara return PTR_ERR(ret); 858ac401cc7SJan Kara *entryp = ret; 8599973c98eSRoss Zwisler 860b4440734SRoss Zwisler trace_dax_insert_mapping(mapping->host, vmf, ret); 861cccbce67SDan Williams return vm_insert_mixed(vma, vaddr, pfn); 862f7ca90b1SMatthew Wilcox } 863f7ca90b1SMatthew Wilcox 864ce5c5d55SDave Chinner /** 8650e3b210cSBoaz Harrosh * dax_pfn_mkwrite - handle first write to DAX page 8660e3b210cSBoaz Harrosh * @vmf: The description of the fault 8670e3b210cSBoaz Harrosh */ 86811bac800SDave Jiang int dax_pfn_mkwrite(struct vm_fault *vmf) 8690e3b210cSBoaz Harrosh { 87011bac800SDave Jiang struct file *file = vmf->vma->vm_file; 871ac401cc7SJan Kara struct address_space *mapping = file->f_mapping; 872c3ff68d7SRoss Zwisler struct inode *inode = mapping->host; 8732f89dc12SJan Kara void *entry, **slot; 874ac401cc7SJan Kara pgoff_t index = vmf->pgoff; 8750e3b210cSBoaz Harrosh 876ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 8772f89dc12SJan Kara entry = get_unlocked_mapping_entry(mapping, index, &slot); 8782f89dc12SJan Kara if (!entry || !radix_tree_exceptional_entry(entry)) { 8792f89dc12SJan Kara if (entry) 880ac401cc7SJan Kara put_unlocked_mapping_entry(mapping, index, entry); 881ac401cc7SJan Kara spin_unlock_irq(&mapping->tree_lock); 882c3ff68d7SRoss Zwisler trace_dax_pfn_mkwrite_no_entry(inode, vmf, VM_FAULT_NOPAGE); 8830e3b210cSBoaz Harrosh return VM_FAULT_NOPAGE; 8840e3b210cSBoaz Harrosh } 8852f89dc12SJan Kara radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY); 8862f89dc12SJan Kara entry = lock_slot(mapping, slot); 8872f89dc12SJan Kara spin_unlock_irq(&mapping->tree_lock); 8882f89dc12SJan Kara /* 8892f89dc12SJan Kara * If we race with somebody updating the PTE and finish_mkwrite_fault() 8902f89dc12SJan Kara * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry 8912f89dc12SJan Kara * the fault in either case. 8922f89dc12SJan Kara */ 8932f89dc12SJan Kara finish_mkwrite_fault(vmf); 8942f89dc12SJan Kara put_locked_mapping_entry(mapping, index, entry); 895c3ff68d7SRoss Zwisler trace_dax_pfn_mkwrite(inode, vmf, VM_FAULT_NOPAGE); 8962f89dc12SJan Kara return VM_FAULT_NOPAGE; 8972f89dc12SJan Kara } 8980e3b210cSBoaz Harrosh EXPORT_SYMBOL_GPL(dax_pfn_mkwrite); 8990e3b210cSBoaz Harrosh 900e30331ffSRoss Zwisler /* 901e30331ffSRoss Zwisler * The user has performed a load from a hole in the file. Allocating 902e30331ffSRoss Zwisler * a new page in the file would cause excessive storage usage for 903e30331ffSRoss Zwisler * workloads with sparse files. We allocate a page cache page instead. 904e30331ffSRoss Zwisler * We'll kick it out of the page cache if it's ever written to, 905e30331ffSRoss Zwisler * otherwise it will simply fall out of the page cache under memory 906e30331ffSRoss Zwisler * pressure without ever having been dirtied. 907e30331ffSRoss Zwisler */ 908e30331ffSRoss Zwisler static int dax_load_hole(struct address_space *mapping, void **entry, 909e30331ffSRoss Zwisler struct vm_fault *vmf) 910e30331ffSRoss Zwisler { 911e30331ffSRoss Zwisler struct inode *inode = mapping->host; 912e30331ffSRoss Zwisler struct page *page; 913e30331ffSRoss Zwisler int ret; 914e30331ffSRoss Zwisler 915e30331ffSRoss Zwisler /* Hole page already exists? Return it... */ 916e30331ffSRoss Zwisler if (!radix_tree_exceptional_entry(*entry)) { 917e30331ffSRoss Zwisler page = *entry; 918e30331ffSRoss Zwisler goto finish_fault; 919e30331ffSRoss Zwisler } 920e30331ffSRoss Zwisler 921e30331ffSRoss Zwisler /* This will replace locked radix tree entry with a hole page */ 922e30331ffSRoss Zwisler page = find_or_create_page(mapping, vmf->pgoff, 923e30331ffSRoss Zwisler vmf->gfp_mask | __GFP_ZERO); 924e30331ffSRoss Zwisler if (!page) { 925e30331ffSRoss Zwisler ret = VM_FAULT_OOM; 926e30331ffSRoss Zwisler goto out; 927e30331ffSRoss Zwisler } 928e30331ffSRoss Zwisler 929e30331ffSRoss Zwisler finish_fault: 930e30331ffSRoss Zwisler vmf->page = page; 931e30331ffSRoss Zwisler ret = finish_fault(vmf); 932e30331ffSRoss Zwisler vmf->page = NULL; 933e30331ffSRoss Zwisler *entry = page; 934e30331ffSRoss Zwisler if (!ret) { 935e30331ffSRoss Zwisler /* Grab reference for PTE that is now referencing the page */ 936e30331ffSRoss Zwisler get_page(page); 937e30331ffSRoss Zwisler ret = VM_FAULT_NOPAGE; 938e30331ffSRoss Zwisler } 939e30331ffSRoss Zwisler out: 940e30331ffSRoss Zwisler trace_dax_load_hole(inode, vmf, ret); 941e30331ffSRoss Zwisler return ret; 942e30331ffSRoss Zwisler } 943e30331ffSRoss Zwisler 9444b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev, 9454b0228faSVishal Verma unsigned int offset, unsigned int length) 9464b0228faSVishal Verma { 9474b0228faSVishal Verma unsigned short sector_size = bdev_logical_block_size(bdev); 9484b0228faSVishal Verma 9494b0228faSVishal Verma if (!IS_ALIGNED(offset, sector_size)) 9504b0228faSVishal Verma return false; 9514b0228faSVishal Verma if (!IS_ALIGNED(length, sector_size)) 9524b0228faSVishal Verma return false; 9534b0228faSVishal Verma 9544b0228faSVishal Verma return true; 9554b0228faSVishal Verma } 9564b0228faSVishal Verma 957cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev, 958cccbce67SDan Williams struct dax_device *dax_dev, sector_t sector, 959cccbce67SDan Williams unsigned int offset, unsigned int size) 960679c8bd3SChristoph Hellwig { 961cccbce67SDan Williams if (dax_range_is_aligned(bdev, offset, size)) { 962cccbce67SDan Williams sector_t start_sector = sector + (offset >> 9); 9634b0228faSVishal Verma 9644b0228faSVishal Verma return blkdev_issue_zeroout(bdev, start_sector, 96553ef7d0eSLinus Torvalds size >> 9, GFP_NOFS, 0); 9664b0228faSVishal Verma } else { 967cccbce67SDan Williams pgoff_t pgoff; 968cccbce67SDan Williams long rc, id; 969cccbce67SDan Williams void *kaddr; 970cccbce67SDan Williams pfn_t pfn; 971cccbce67SDan Williams 972e84b83b9SDan Williams rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); 973cccbce67SDan Williams if (rc) 974cccbce67SDan Williams return rc; 975cccbce67SDan Williams 976cccbce67SDan Williams id = dax_read_lock(); 977e84b83b9SDan Williams rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, 978cccbce67SDan Williams &pfn); 979cccbce67SDan Williams if (rc < 0) { 980cccbce67SDan Williams dax_read_unlock(id); 981cccbce67SDan Williams return rc; 982cccbce67SDan Williams } 98381f55870SDan Williams memset(kaddr + offset, 0, size); 98481f55870SDan Williams dax_flush(dax_dev, pgoff, kaddr + offset, size); 985cccbce67SDan Williams dax_read_unlock(id); 9864b0228faSVishal Verma } 987679c8bd3SChristoph Hellwig return 0; 988679c8bd3SChristoph Hellwig } 989679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range); 990679c8bd3SChristoph Hellwig 991333ccc97SRoss Zwisler static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) 992333ccc97SRoss Zwisler { 993333ccc97SRoss Zwisler return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9); 994333ccc97SRoss Zwisler } 995333ccc97SRoss Zwisler 996a254e568SChristoph Hellwig static loff_t 99711c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, 998a254e568SChristoph Hellwig struct iomap *iomap) 999a254e568SChristoph Hellwig { 1000cccbce67SDan Williams struct block_device *bdev = iomap->bdev; 1001cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1002a254e568SChristoph Hellwig struct iov_iter *iter = data; 1003a254e568SChristoph Hellwig loff_t end = pos + length, done = 0; 1004a254e568SChristoph Hellwig ssize_t ret = 0; 1005cccbce67SDan Williams int id; 1006a254e568SChristoph Hellwig 1007a254e568SChristoph Hellwig if (iov_iter_rw(iter) == READ) { 1008a254e568SChristoph Hellwig end = min(end, i_size_read(inode)); 1009a254e568SChristoph Hellwig if (pos >= end) 1010a254e568SChristoph Hellwig return 0; 1011a254e568SChristoph Hellwig 1012a254e568SChristoph Hellwig if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) 1013a254e568SChristoph Hellwig return iov_iter_zero(min(length, end - pos), iter); 1014a254e568SChristoph Hellwig } 1015a254e568SChristoph Hellwig 1016a254e568SChristoph Hellwig if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) 1017a254e568SChristoph Hellwig return -EIO; 1018a254e568SChristoph Hellwig 1019e3fce68cSJan Kara /* 1020e3fce68cSJan Kara * Write can allocate block for an area which has a hole page mapped 1021e3fce68cSJan Kara * into page tables. We have to tear down these mappings so that data 1022e3fce68cSJan Kara * written by write(2) is visible in mmap. 1023e3fce68cSJan Kara */ 1024cd656375SJan Kara if (iomap->flags & IOMAP_F_NEW) { 1025e3fce68cSJan Kara invalidate_inode_pages2_range(inode->i_mapping, 1026e3fce68cSJan Kara pos >> PAGE_SHIFT, 1027e3fce68cSJan Kara (end - 1) >> PAGE_SHIFT); 1028e3fce68cSJan Kara } 1029e3fce68cSJan Kara 1030cccbce67SDan Williams id = dax_read_lock(); 1031a254e568SChristoph Hellwig while (pos < end) { 1032a254e568SChristoph Hellwig unsigned offset = pos & (PAGE_SIZE - 1); 1033cccbce67SDan Williams const size_t size = ALIGN(length + offset, PAGE_SIZE); 1034cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1035a254e568SChristoph Hellwig ssize_t map_len; 1036cccbce67SDan Williams pgoff_t pgoff; 1037cccbce67SDan Williams void *kaddr; 1038cccbce67SDan Williams pfn_t pfn; 1039a254e568SChristoph Hellwig 1040d1908f52SMichal Hocko if (fatal_signal_pending(current)) { 1041d1908f52SMichal Hocko ret = -EINTR; 1042d1908f52SMichal Hocko break; 1043d1908f52SMichal Hocko } 1044d1908f52SMichal Hocko 1045cccbce67SDan Williams ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); 1046cccbce67SDan Williams if (ret) 1047cccbce67SDan Williams break; 1048cccbce67SDan Williams 1049cccbce67SDan Williams map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), 1050cccbce67SDan Williams &kaddr, &pfn); 1051a254e568SChristoph Hellwig if (map_len < 0) { 1052a254e568SChristoph Hellwig ret = map_len; 1053a254e568SChristoph Hellwig break; 1054a254e568SChristoph Hellwig } 1055a254e568SChristoph Hellwig 1056cccbce67SDan Williams map_len = PFN_PHYS(map_len); 1057cccbce67SDan Williams kaddr += offset; 1058a254e568SChristoph Hellwig map_len -= offset; 1059a254e568SChristoph Hellwig if (map_len > end - pos) 1060a254e568SChristoph Hellwig map_len = end - pos; 1061a254e568SChristoph Hellwig 1062a254e568SChristoph Hellwig if (iov_iter_rw(iter) == WRITE) 1063fec53774SDan Williams map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr, 1064fec53774SDan Williams map_len, iter); 1065a254e568SChristoph Hellwig else 1066cccbce67SDan Williams map_len = copy_to_iter(kaddr, map_len, iter); 1067a254e568SChristoph Hellwig if (map_len <= 0) { 1068a254e568SChristoph Hellwig ret = map_len ? map_len : -EFAULT; 1069a254e568SChristoph Hellwig break; 1070a254e568SChristoph Hellwig } 1071a254e568SChristoph Hellwig 1072a254e568SChristoph Hellwig pos += map_len; 1073a254e568SChristoph Hellwig length -= map_len; 1074a254e568SChristoph Hellwig done += map_len; 1075a254e568SChristoph Hellwig } 1076cccbce67SDan Williams dax_read_unlock(id); 1077a254e568SChristoph Hellwig 1078a254e568SChristoph Hellwig return done ? done : ret; 1079a254e568SChristoph Hellwig } 1080a254e568SChristoph Hellwig 1081a254e568SChristoph Hellwig /** 108211c59c92SRoss Zwisler * dax_iomap_rw - Perform I/O to a DAX file 1083a254e568SChristoph Hellwig * @iocb: The control block for this I/O 1084a254e568SChristoph Hellwig * @iter: The addresses to do I/O from or to 1085a254e568SChristoph Hellwig * @ops: iomap ops passed from the file system 1086a254e568SChristoph Hellwig * 1087a254e568SChristoph Hellwig * This function performs read and write operations to directly mapped 1088a254e568SChristoph Hellwig * persistent memory. The callers needs to take care of read/write exclusion 1089a254e568SChristoph Hellwig * and evicting any page cache pages in the region under I/O. 1090a254e568SChristoph Hellwig */ 1091a254e568SChristoph Hellwig ssize_t 109211c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 10938ff6daa1SChristoph Hellwig const struct iomap_ops *ops) 1094a254e568SChristoph Hellwig { 1095a254e568SChristoph Hellwig struct address_space *mapping = iocb->ki_filp->f_mapping; 1096a254e568SChristoph Hellwig struct inode *inode = mapping->host; 1097a254e568SChristoph Hellwig loff_t pos = iocb->ki_pos, ret = 0, done = 0; 1098a254e568SChristoph Hellwig unsigned flags = 0; 1099a254e568SChristoph Hellwig 1100168316dbSChristoph Hellwig if (iov_iter_rw(iter) == WRITE) { 1101168316dbSChristoph Hellwig lockdep_assert_held_exclusive(&inode->i_rwsem); 1102a254e568SChristoph Hellwig flags |= IOMAP_WRITE; 1103168316dbSChristoph Hellwig } else { 1104168316dbSChristoph Hellwig lockdep_assert_held(&inode->i_rwsem); 1105168316dbSChristoph Hellwig } 1106a254e568SChristoph Hellwig 1107a254e568SChristoph Hellwig while (iov_iter_count(iter)) { 1108a254e568SChristoph Hellwig ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, 110911c59c92SRoss Zwisler iter, dax_iomap_actor); 1110a254e568SChristoph Hellwig if (ret <= 0) 1111a254e568SChristoph Hellwig break; 1112a254e568SChristoph Hellwig pos += ret; 1113a254e568SChristoph Hellwig done += ret; 1114a254e568SChristoph Hellwig } 1115a254e568SChristoph Hellwig 1116a254e568SChristoph Hellwig iocb->ki_pos += done; 1117a254e568SChristoph Hellwig return done ? done : ret; 1118a254e568SChristoph Hellwig } 111911c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw); 1120a7d73fe6SChristoph Hellwig 11219f141d6eSJan Kara static int dax_fault_return(int error) 11229f141d6eSJan Kara { 11239f141d6eSJan Kara if (error == 0) 11249f141d6eSJan Kara return VM_FAULT_NOPAGE; 11259f141d6eSJan Kara if (error == -ENOMEM) 11269f141d6eSJan Kara return VM_FAULT_OOM; 11279f141d6eSJan Kara return VM_FAULT_SIGBUS; 11289f141d6eSJan Kara } 11299f141d6eSJan Kara 1130a2d58167SDave Jiang static int dax_iomap_pte_fault(struct vm_fault *vmf, 1131a2d58167SDave Jiang const struct iomap_ops *ops) 1132a7d73fe6SChristoph Hellwig { 113311bac800SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1134a7d73fe6SChristoph Hellwig struct inode *inode = mapping->host; 11351a29d85eSJan Kara unsigned long vaddr = vmf->address; 1136a7d73fe6SChristoph Hellwig loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; 1137a7d73fe6SChristoph Hellwig sector_t sector; 1138a7d73fe6SChristoph Hellwig struct iomap iomap = { 0 }; 11399484ab1bSJan Kara unsigned flags = IOMAP_FAULT; 1140a7d73fe6SChristoph Hellwig int error, major = 0; 1141b1aa812bSJan Kara int vmf_ret = 0; 1142a7d73fe6SChristoph Hellwig void *entry; 1143a7d73fe6SChristoph Hellwig 1144a9c42b33SRoss Zwisler trace_dax_pte_fault(inode, vmf, vmf_ret); 1145a7d73fe6SChristoph Hellwig /* 1146a7d73fe6SChristoph Hellwig * Check whether offset isn't beyond end of file now. Caller is supposed 1147a7d73fe6SChristoph Hellwig * to hold locks serializing us with truncate / punch hole so this is 1148a7d73fe6SChristoph Hellwig * a reliable test. 1149a7d73fe6SChristoph Hellwig */ 1150a9c42b33SRoss Zwisler if (pos >= i_size_read(inode)) { 1151a9c42b33SRoss Zwisler vmf_ret = VM_FAULT_SIGBUS; 1152a9c42b33SRoss Zwisler goto out; 1153a9c42b33SRoss Zwisler } 1154a7d73fe6SChristoph Hellwig 1155a7d73fe6SChristoph Hellwig if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page) 1156a7d73fe6SChristoph Hellwig flags |= IOMAP_WRITE; 1157a7d73fe6SChristoph Hellwig 115813e451fdSJan Kara entry = grab_mapping_entry(mapping, vmf->pgoff, 0); 115913e451fdSJan Kara if (IS_ERR(entry)) { 116013e451fdSJan Kara vmf_ret = dax_fault_return(PTR_ERR(entry)); 116113e451fdSJan Kara goto out; 116213e451fdSJan Kara } 116313e451fdSJan Kara 1164a7d73fe6SChristoph Hellwig /* 1165e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1166e2093926SRoss Zwisler * mappings, that we have raced with a PMD fault that overlaps with 1167e2093926SRoss Zwisler * the PTE we need to set up. If so just return and the fault will be 1168e2093926SRoss Zwisler * retried. 1169e2093926SRoss Zwisler */ 1170e2093926SRoss Zwisler if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { 1171e2093926SRoss Zwisler vmf_ret = VM_FAULT_NOPAGE; 1172e2093926SRoss Zwisler goto unlock_entry; 1173e2093926SRoss Zwisler } 1174e2093926SRoss Zwisler 1175e2093926SRoss Zwisler /* 1176a7d73fe6SChristoph Hellwig * Note that we don't bother to use iomap_apply here: DAX required 1177a7d73fe6SChristoph Hellwig * the file system block size to be equal the page size, which means 1178a7d73fe6SChristoph Hellwig * that we never have to deal with more than a single extent here. 1179a7d73fe6SChristoph Hellwig */ 1180a7d73fe6SChristoph Hellwig error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap); 1181a9c42b33SRoss Zwisler if (error) { 1182a9c42b33SRoss Zwisler vmf_ret = dax_fault_return(error); 118313e451fdSJan Kara goto unlock_entry; 1184a9c42b33SRoss Zwisler } 1185a7d73fe6SChristoph Hellwig if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { 118613e451fdSJan Kara error = -EIO; /* fs corruption? */ 118713e451fdSJan Kara goto error_finish_iomap; 1188a7d73fe6SChristoph Hellwig } 1189a7d73fe6SChristoph Hellwig 1190333ccc97SRoss Zwisler sector = dax_iomap_sector(&iomap, pos); 1191a7d73fe6SChristoph Hellwig 1192a7d73fe6SChristoph Hellwig if (vmf->cow_page) { 1193a7d73fe6SChristoph Hellwig switch (iomap.type) { 1194a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 1195a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1196a7d73fe6SChristoph Hellwig clear_user_highpage(vmf->cow_page, vaddr); 1197a7d73fe6SChristoph Hellwig break; 1198a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1199cccbce67SDan Williams error = copy_user_dax(iomap.bdev, iomap.dax_dev, 1200cccbce67SDan Williams sector, PAGE_SIZE, vmf->cow_page, vaddr); 1201a7d73fe6SChristoph Hellwig break; 1202a7d73fe6SChristoph Hellwig default: 1203a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1204a7d73fe6SChristoph Hellwig error = -EIO; 1205a7d73fe6SChristoph Hellwig break; 1206a7d73fe6SChristoph Hellwig } 1207a7d73fe6SChristoph Hellwig 1208a7d73fe6SChristoph Hellwig if (error) 120913e451fdSJan Kara goto error_finish_iomap; 1210b1aa812bSJan Kara 1211b1aa812bSJan Kara __SetPageUptodate(vmf->cow_page); 1212b1aa812bSJan Kara vmf_ret = finish_fault(vmf); 1213b1aa812bSJan Kara if (!vmf_ret) 1214b1aa812bSJan Kara vmf_ret = VM_FAULT_DONE_COW; 121513e451fdSJan Kara goto finish_iomap; 1216a7d73fe6SChristoph Hellwig } 1217a7d73fe6SChristoph Hellwig 1218a7d73fe6SChristoph Hellwig switch (iomap.type) { 1219a7d73fe6SChristoph Hellwig case IOMAP_MAPPED: 1220a7d73fe6SChristoph Hellwig if (iomap.flags & IOMAP_F_NEW) { 1221a7d73fe6SChristoph Hellwig count_vm_event(PGMAJFAULT); 12222262185cSRoman Gushchin count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT); 1223a7d73fe6SChristoph Hellwig major = VM_FAULT_MAJOR; 1224a7d73fe6SChristoph Hellwig } 1225cccbce67SDan Williams error = dax_insert_mapping(mapping, iomap.bdev, iomap.dax_dev, 1226cccbce67SDan Williams sector, PAGE_SIZE, &entry, vmf->vma, vmf); 12279f141d6eSJan Kara /* -EBUSY is fine, somebody else faulted on the same PTE */ 12289f141d6eSJan Kara if (error == -EBUSY) 12299f141d6eSJan Kara error = 0; 1230a7d73fe6SChristoph Hellwig break; 1231a7d73fe6SChristoph Hellwig case IOMAP_UNWRITTEN: 1232a7d73fe6SChristoph Hellwig case IOMAP_HOLE: 12331550290bSRoss Zwisler if (!(vmf->flags & FAULT_FLAG_WRITE)) { 1234f449b936SJan Kara vmf_ret = dax_load_hole(mapping, &entry, vmf); 123513e451fdSJan Kara goto finish_iomap; 12361550290bSRoss Zwisler } 1237a7d73fe6SChristoph Hellwig /*FALLTHRU*/ 1238a7d73fe6SChristoph Hellwig default: 1239a7d73fe6SChristoph Hellwig WARN_ON_ONCE(1); 1240a7d73fe6SChristoph Hellwig error = -EIO; 1241a7d73fe6SChristoph Hellwig break; 1242a7d73fe6SChristoph Hellwig } 1243a7d73fe6SChristoph Hellwig 124413e451fdSJan Kara error_finish_iomap: 12459f141d6eSJan Kara vmf_ret = dax_fault_return(error) | major; 12469f141d6eSJan Kara finish_iomap: 12479f141d6eSJan Kara if (ops->iomap_end) { 12489f141d6eSJan Kara int copied = PAGE_SIZE; 12499f141d6eSJan Kara 12509f141d6eSJan Kara if (vmf_ret & VM_FAULT_ERROR) 12519f141d6eSJan Kara copied = 0; 12529f141d6eSJan Kara /* 12539f141d6eSJan Kara * The fault is done by now and there's no way back (other 12549f141d6eSJan Kara * thread may be already happily using PTE we have installed). 12559f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 12569f141d6eSJan Kara * with it. 12579f141d6eSJan Kara */ 12589f141d6eSJan Kara ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); 12591550290bSRoss Zwisler } 126013e451fdSJan Kara unlock_entry: 126113e451fdSJan Kara put_locked_mapping_entry(mapping, vmf->pgoff, entry); 1262a9c42b33SRoss Zwisler out: 1263a9c42b33SRoss Zwisler trace_dax_pte_fault_done(inode, vmf, vmf_ret); 12649f141d6eSJan Kara return vmf_ret; 1265a7d73fe6SChristoph Hellwig } 1266642261acSRoss Zwisler 1267642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD 1268642261acSRoss Zwisler /* 1269642261acSRoss Zwisler * The 'colour' (ie low bits) within a PMD of a page offset. This comes up 1270642261acSRoss Zwisler * more often than one might expect in the below functions. 1271642261acSRoss Zwisler */ 1272642261acSRoss Zwisler #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) 1273642261acSRoss Zwisler 1274f4200391SDave Jiang static int dax_pmd_insert_mapping(struct vm_fault *vmf, struct iomap *iomap, 1275f4200391SDave Jiang loff_t pos, void **entryp) 1276642261acSRoss Zwisler { 1277f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1278cccbce67SDan Williams const sector_t sector = dax_iomap_sector(iomap, pos); 1279cccbce67SDan Williams struct dax_device *dax_dev = iomap->dax_dev; 1280642261acSRoss Zwisler struct block_device *bdev = iomap->bdev; 128127a7ffacSRoss Zwisler struct inode *inode = mapping->host; 1282cccbce67SDan Williams const size_t size = PMD_SIZE; 1283cccbce67SDan Williams void *ret = NULL, *kaddr; 1284cccbce67SDan Williams long length = 0; 1285cccbce67SDan Williams pgoff_t pgoff; 1286cccbce67SDan Williams pfn_t pfn; 1287cccbce67SDan Williams int id; 1288642261acSRoss Zwisler 1289cccbce67SDan Williams if (bdev_dax_pgoff(bdev, sector, size, &pgoff) != 0) 129027a7ffacSRoss Zwisler goto fallback; 1291642261acSRoss Zwisler 1292cccbce67SDan Williams id = dax_read_lock(); 1293cccbce67SDan Williams length = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn); 1294cccbce67SDan Williams if (length < 0) 1295cccbce67SDan Williams goto unlock_fallback; 1296cccbce67SDan Williams length = PFN_PHYS(length); 1297642261acSRoss Zwisler 1298cccbce67SDan Williams if (length < size) 1299cccbce67SDan Williams goto unlock_fallback; 1300cccbce67SDan Williams if (pfn_t_to_pfn(pfn) & PG_PMD_COLOUR) 1301cccbce67SDan Williams goto unlock_fallback; 1302cccbce67SDan Williams if (!pfn_t_devmap(pfn)) 1303cccbce67SDan Williams goto unlock_fallback; 1304cccbce67SDan Williams dax_read_unlock(id); 1305cccbce67SDan Williams 1306cccbce67SDan Williams ret = dax_insert_mapping_entry(mapping, vmf, *entryp, sector, 1307642261acSRoss Zwisler RADIX_DAX_PMD); 1308642261acSRoss Zwisler if (IS_ERR(ret)) 130927a7ffacSRoss Zwisler goto fallback; 1310642261acSRoss Zwisler *entryp = ret; 1311642261acSRoss Zwisler 1312cccbce67SDan Williams trace_dax_pmd_insert_mapping(inode, vmf, length, pfn, ret); 1313f4200391SDave Jiang return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, 1314cccbce67SDan Williams pfn, vmf->flags & FAULT_FLAG_WRITE); 1315642261acSRoss Zwisler 1316cccbce67SDan Williams unlock_fallback: 1317cccbce67SDan Williams dax_read_unlock(id); 131827a7ffacSRoss Zwisler fallback: 1319cccbce67SDan Williams trace_dax_pmd_insert_mapping_fallback(inode, vmf, length, pfn, ret); 1320642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1321642261acSRoss Zwisler } 1322642261acSRoss Zwisler 1323f4200391SDave Jiang static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap, 1324f4200391SDave Jiang void **entryp) 1325642261acSRoss Zwisler { 1326f4200391SDave Jiang struct address_space *mapping = vmf->vma->vm_file->f_mapping; 1327f4200391SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1328653b2ea3SRoss Zwisler struct inode *inode = mapping->host; 1329642261acSRoss Zwisler struct page *zero_page; 1330653b2ea3SRoss Zwisler void *ret = NULL; 1331642261acSRoss Zwisler spinlock_t *ptl; 1332642261acSRoss Zwisler pmd_t pmd_entry; 1333642261acSRoss Zwisler 1334f4200391SDave Jiang zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); 1335642261acSRoss Zwisler 1336642261acSRoss Zwisler if (unlikely(!zero_page)) 1337653b2ea3SRoss Zwisler goto fallback; 1338642261acSRoss Zwisler 1339642261acSRoss Zwisler ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0, 1340642261acSRoss Zwisler RADIX_DAX_PMD | RADIX_DAX_HZP); 1341642261acSRoss Zwisler if (IS_ERR(ret)) 1342653b2ea3SRoss Zwisler goto fallback; 1343642261acSRoss Zwisler *entryp = ret; 1344642261acSRoss Zwisler 1345f4200391SDave Jiang ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); 1346f4200391SDave Jiang if (!pmd_none(*(vmf->pmd))) { 1347642261acSRoss Zwisler spin_unlock(ptl); 1348653b2ea3SRoss Zwisler goto fallback; 1349642261acSRoss Zwisler } 1350642261acSRoss Zwisler 1351f4200391SDave Jiang pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); 1352642261acSRoss Zwisler pmd_entry = pmd_mkhuge(pmd_entry); 1353f4200391SDave Jiang set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); 1354642261acSRoss Zwisler spin_unlock(ptl); 1355f4200391SDave Jiang trace_dax_pmd_load_hole(inode, vmf, zero_page, ret); 1356642261acSRoss Zwisler return VM_FAULT_NOPAGE; 1357653b2ea3SRoss Zwisler 1358653b2ea3SRoss Zwisler fallback: 1359f4200391SDave Jiang trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret); 1360642261acSRoss Zwisler return VM_FAULT_FALLBACK; 1361642261acSRoss Zwisler } 1362642261acSRoss Zwisler 1363a2d58167SDave Jiang static int dax_iomap_pmd_fault(struct vm_fault *vmf, 1364a2d58167SDave Jiang const struct iomap_ops *ops) 1365642261acSRoss Zwisler { 1366f4200391SDave Jiang struct vm_area_struct *vma = vmf->vma; 1367642261acSRoss Zwisler struct address_space *mapping = vma->vm_file->f_mapping; 1368d8a849e1SDave Jiang unsigned long pmd_addr = vmf->address & PMD_MASK; 1369d8a849e1SDave Jiang bool write = vmf->flags & FAULT_FLAG_WRITE; 13709484ab1bSJan Kara unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; 1371642261acSRoss Zwisler struct inode *inode = mapping->host; 1372642261acSRoss Zwisler int result = VM_FAULT_FALLBACK; 1373642261acSRoss Zwisler struct iomap iomap = { 0 }; 1374642261acSRoss Zwisler pgoff_t max_pgoff, pgoff; 1375642261acSRoss Zwisler void *entry; 1376642261acSRoss Zwisler loff_t pos; 1377642261acSRoss Zwisler int error; 1378642261acSRoss Zwisler 1379282a8e03SRoss Zwisler /* 1380282a8e03SRoss Zwisler * Check whether offset isn't beyond end of file now. Caller is 1381282a8e03SRoss Zwisler * supposed to hold locks serializing us with truncate / punch hole so 1382282a8e03SRoss Zwisler * this is a reliable test. 1383282a8e03SRoss Zwisler */ 1384282a8e03SRoss Zwisler pgoff = linear_page_index(vma, pmd_addr); 1385282a8e03SRoss Zwisler max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT; 1386282a8e03SRoss Zwisler 1387f4200391SDave Jiang trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); 1388282a8e03SRoss Zwisler 1389fffa281bSRoss Zwisler /* 1390fffa281bSRoss Zwisler * Make sure that the faulting address's PMD offset (color) matches 1391fffa281bSRoss Zwisler * the PMD offset from the start of the file. This is necessary so 1392fffa281bSRoss Zwisler * that a PMD range in the page table overlaps exactly with a PMD 1393fffa281bSRoss Zwisler * range in the radix tree. 1394fffa281bSRoss Zwisler */ 1395fffa281bSRoss Zwisler if ((vmf->pgoff & PG_PMD_COLOUR) != 1396fffa281bSRoss Zwisler ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) 1397fffa281bSRoss Zwisler goto fallback; 1398fffa281bSRoss Zwisler 1399642261acSRoss Zwisler /* Fall back to PTEs if we're going to COW */ 1400642261acSRoss Zwisler if (write && !(vma->vm_flags & VM_SHARED)) 1401642261acSRoss Zwisler goto fallback; 1402642261acSRoss Zwisler 1403642261acSRoss Zwisler /* If the PMD would extend outside the VMA */ 1404642261acSRoss Zwisler if (pmd_addr < vma->vm_start) 1405642261acSRoss Zwisler goto fallback; 1406642261acSRoss Zwisler if ((pmd_addr + PMD_SIZE) > vma->vm_end) 1407642261acSRoss Zwisler goto fallback; 1408642261acSRoss Zwisler 1409282a8e03SRoss Zwisler if (pgoff > max_pgoff) { 1410282a8e03SRoss Zwisler result = VM_FAULT_SIGBUS; 1411282a8e03SRoss Zwisler goto out; 1412282a8e03SRoss Zwisler } 1413642261acSRoss Zwisler 1414642261acSRoss Zwisler /* If the PMD would extend beyond the file size */ 1415642261acSRoss Zwisler if ((pgoff | PG_PMD_COLOUR) > max_pgoff) 1416642261acSRoss Zwisler goto fallback; 1417642261acSRoss Zwisler 1418642261acSRoss Zwisler /* 14199f141d6eSJan Kara * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX 14209f141d6eSJan Kara * PMD or a HZP entry. If it can't (because a 4k page is already in 14219f141d6eSJan Kara * the tree, for instance), it will return -EEXIST and we just fall 14229f141d6eSJan Kara * back to 4k entries. 14239f141d6eSJan Kara */ 14249f141d6eSJan Kara entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD); 14259f141d6eSJan Kara if (IS_ERR(entry)) 1426876f2946SRoss Zwisler goto fallback; 1427876f2946SRoss Zwisler 1428876f2946SRoss Zwisler /* 1429e2093926SRoss Zwisler * It is possible, particularly with mixed reads & writes to private 1430e2093926SRoss Zwisler * mappings, that we have raced with a PTE fault that overlaps with 1431e2093926SRoss Zwisler * the PMD we need to set up. If so just return and the fault will be 1432e2093926SRoss Zwisler * retried. 1433e2093926SRoss Zwisler */ 1434e2093926SRoss Zwisler if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && 1435e2093926SRoss Zwisler !pmd_devmap(*vmf->pmd)) { 1436e2093926SRoss Zwisler result = 0; 1437e2093926SRoss Zwisler goto unlock_entry; 1438e2093926SRoss Zwisler } 1439e2093926SRoss Zwisler 1440e2093926SRoss Zwisler /* 1441876f2946SRoss Zwisler * Note that we don't use iomap_apply here. We aren't doing I/O, only 1442876f2946SRoss Zwisler * setting up a mapping, so really we're using iomap_begin() as a way 1443876f2946SRoss Zwisler * to look up our filesystem block. 1444876f2946SRoss Zwisler */ 1445876f2946SRoss Zwisler pos = (loff_t)pgoff << PAGE_SHIFT; 1446876f2946SRoss Zwisler error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); 1447876f2946SRoss Zwisler if (error) 1448876f2946SRoss Zwisler goto unlock_entry; 1449876f2946SRoss Zwisler 1450876f2946SRoss Zwisler if (iomap.offset + iomap.length < pos + PMD_SIZE) 14519f141d6eSJan Kara goto finish_iomap; 14529f141d6eSJan Kara 1453642261acSRoss Zwisler switch (iomap.type) { 1454642261acSRoss Zwisler case IOMAP_MAPPED: 1455f4200391SDave Jiang result = dax_pmd_insert_mapping(vmf, &iomap, pos, &entry); 1456642261acSRoss Zwisler break; 1457642261acSRoss Zwisler case IOMAP_UNWRITTEN: 1458642261acSRoss Zwisler case IOMAP_HOLE: 1459642261acSRoss Zwisler if (WARN_ON_ONCE(write)) 1460876f2946SRoss Zwisler break; 1461f4200391SDave Jiang result = dax_pmd_load_hole(vmf, &iomap, &entry); 1462642261acSRoss Zwisler break; 1463642261acSRoss Zwisler default: 1464642261acSRoss Zwisler WARN_ON_ONCE(1); 1465642261acSRoss Zwisler break; 1466642261acSRoss Zwisler } 1467642261acSRoss Zwisler 14689f141d6eSJan Kara finish_iomap: 14699f141d6eSJan Kara if (ops->iomap_end) { 14709f141d6eSJan Kara int copied = PMD_SIZE; 14719f141d6eSJan Kara 14729f141d6eSJan Kara if (result == VM_FAULT_FALLBACK) 14739f141d6eSJan Kara copied = 0; 14749f141d6eSJan Kara /* 14759f141d6eSJan Kara * The fault is done by now and there's no way back (other 14769f141d6eSJan Kara * thread may be already happily using PMD we have installed). 14779f141d6eSJan Kara * Just ignore error from ->iomap_end since we cannot do much 14789f141d6eSJan Kara * with it. 14799f141d6eSJan Kara */ 14809f141d6eSJan Kara ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, 14819f141d6eSJan Kara &iomap); 14829f141d6eSJan Kara } 1483876f2946SRoss Zwisler unlock_entry: 1484876f2946SRoss Zwisler put_locked_mapping_entry(mapping, pgoff, entry); 1485642261acSRoss Zwisler fallback: 1486642261acSRoss Zwisler if (result == VM_FAULT_FALLBACK) { 1487d8a849e1SDave Jiang split_huge_pmd(vma, vmf->pmd, vmf->address); 1488642261acSRoss Zwisler count_vm_event(THP_FAULT_FALLBACK); 1489642261acSRoss Zwisler } 1490282a8e03SRoss Zwisler out: 1491f4200391SDave Jiang trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); 1492642261acSRoss Zwisler return result; 1493642261acSRoss Zwisler } 1494a2d58167SDave Jiang #else 149501cddfe9SArnd Bergmann static int dax_iomap_pmd_fault(struct vm_fault *vmf, 149601cddfe9SArnd Bergmann const struct iomap_ops *ops) 1497a2d58167SDave Jiang { 1498a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1499a2d58167SDave Jiang } 1500642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */ 1501a2d58167SDave Jiang 1502a2d58167SDave Jiang /** 1503a2d58167SDave Jiang * dax_iomap_fault - handle a page fault on a DAX file 1504a2d58167SDave Jiang * @vmf: The description of the fault 1505a2d58167SDave Jiang * @ops: iomap ops passed from the file system 1506a2d58167SDave Jiang * 1507a2d58167SDave Jiang * When a page fault occurs, filesystems may call this helper in 1508a2d58167SDave Jiang * their fault handler for DAX files. dax_iomap_fault() assumes the caller 1509a2d58167SDave Jiang * has done all the necessary locking for page fault to proceed 1510a2d58167SDave Jiang * successfully. 1511a2d58167SDave Jiang */ 1512c791ace1SDave Jiang int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, 1513c791ace1SDave Jiang const struct iomap_ops *ops) 1514a2d58167SDave Jiang { 1515c791ace1SDave Jiang switch (pe_size) { 1516c791ace1SDave Jiang case PE_SIZE_PTE: 1517a2d58167SDave Jiang return dax_iomap_pte_fault(vmf, ops); 1518c791ace1SDave Jiang case PE_SIZE_PMD: 1519a2d58167SDave Jiang return dax_iomap_pmd_fault(vmf, ops); 1520a2d58167SDave Jiang default: 1521a2d58167SDave Jiang return VM_FAULT_FALLBACK; 1522a2d58167SDave Jiang } 1523a2d58167SDave Jiang } 1524a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault); 1525