Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * fs/dax.c - Direct Access filesystem code |
| 3 | * Copyright (c) 2013-2014 Intel Corporation |
| 4 | * Author: Matthew Wilcox <matthew.r.wilcox@intel.com> |
| 5 | * Author: Ross Zwisler <ross.zwisler@linux.intel.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/atomic.h> |
| 18 | #include <linux/blkdev.h> |
| 19 | #include <linux/buffer_head.h> |
Ross Zwisler | d77e92e | 2015-09-09 10:29:40 -0600 | [diff] [blame] | 20 | #include <linux/dax.h> |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 21 | #include <linux/fs.h> |
| 22 | #include <linux/genhd.h> |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 23 | #include <linux/highmem.h> |
| 24 | #include <linux/memcontrol.h> |
| 25 | #include <linux/mm.h> |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 26 | #include <linux/mutex.h> |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 27 | #include <linux/pagevec.h> |
Matthew Wilcox | 289c6ae | 2015-02-16 15:58:59 -0800 | [diff] [blame] | 28 | #include <linux/sched.h> |
Ingo Molnar | f361bf4 | 2017-02-03 23:47:37 +0100 | [diff] [blame] | 29 | #include <linux/sched/signal.h> |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 30 | #include <linux/uio.h> |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 31 | #include <linux/vmstat.h> |
Dan Williams | 34c0fd5 | 2016-01-15 16:56:14 -0800 | [diff] [blame] | 32 | #include <linux/pfn_t.h> |
Dan Williams | 0e749e5 | 2016-01-15 16:55:53 -0800 | [diff] [blame] | 33 | #include <linux/sizes.h> |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 34 | #include <linux/mmu_notifier.h> |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 35 | #include <linux/iomap.h> |
| 36 | #include "internal.h" |
Matthew Wilcox | d475c63 | 2015-02-16 15:58:56 -0800 | [diff] [blame] | 37 | |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 38 | #define CREATE_TRACE_POINTS |
| 39 | #include <trace/events/fs_dax.h> |
| 40 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 41 | /* We choose 4096 entries - same as per-zone page wait tables */ |
| 42 | #define DAX_WAIT_TABLE_BITS 12 |
| 43 | #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS) |
| 44 | |
Ross Zwisler | 917f345 | 2017-09-06 16:18:58 -0700 | [diff] [blame] | 45 | /* The 'colour' (ie low bits) within a PMD of a page offset. */ |
| 46 | #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1) |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 47 | #define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) |
Ross Zwisler | 917f345 | 2017-09-06 16:18:58 -0700 | [diff] [blame] | 48 | |
Ross Zwisler | ce95ab0 | 2016-11-08 11:31:44 +1100 | [diff] [blame] | 49 | static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES]; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 50 | |
| 51 | static int __init init_dax_wait_table(void) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++) |
| 56 | init_waitqueue_head(wait_table + i); |
| 57 | return 0; |
| 58 | } |
| 59 | fs_initcall(init_dax_wait_table); |
| 60 | |
Ross Zwisler | 527b19d | 2017-09-06 16:18:51 -0700 | [diff] [blame] | 61 | /* |
| 62 | * We use lowest available bit in exceptional entry for locking, one bit for |
| 63 | * the entry size (PMD) and two more to tell us if the entry is a zero page or |
| 64 | * an empty entry that is just used for locking. In total four special bits. |
| 65 | * |
| 66 | * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE |
| 67 | * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem |
| 68 | * block allocation. |
| 69 | */ |
| 70 | #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 4) |
| 71 | #define RADIX_DAX_ENTRY_LOCK (1 << RADIX_TREE_EXCEPTIONAL_SHIFT) |
| 72 | #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1)) |
| 73 | #define RADIX_DAX_ZERO_PAGE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2)) |
| 74 | #define RADIX_DAX_EMPTY (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 3)) |
| 75 | |
| 76 | static unsigned long dax_radix_sector(void *entry) |
| 77 | { |
| 78 | return (unsigned long)entry >> RADIX_DAX_SHIFT; |
| 79 | } |
| 80 | |
| 81 | static void *dax_radix_locked_entry(sector_t sector, unsigned long flags) |
| 82 | { |
| 83 | return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags | |
| 84 | ((unsigned long)sector << RADIX_DAX_SHIFT) | |
| 85 | RADIX_DAX_ENTRY_LOCK); |
| 86 | } |
| 87 | |
| 88 | static unsigned int dax_radix_order(void *entry) |
| 89 | { |
| 90 | if ((unsigned long)entry & RADIX_DAX_PMD) |
| 91 | return PMD_SHIFT - PAGE_SHIFT; |
| 92 | return 0; |
| 93 | } |
| 94 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 95 | static int dax_is_pmd_entry(void *entry) |
| 96 | { |
| 97 | return (unsigned long)entry & RADIX_DAX_PMD; |
| 98 | } |
| 99 | |
| 100 | static int dax_is_pte_entry(void *entry) |
| 101 | { |
| 102 | return !((unsigned long)entry & RADIX_DAX_PMD); |
| 103 | } |
| 104 | |
| 105 | static int dax_is_zero_entry(void *entry) |
| 106 | { |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 107 | return (unsigned long)entry & RADIX_DAX_ZERO_PAGE; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static int dax_is_empty_entry(void *entry) |
| 111 | { |
| 112 | return (unsigned long)entry & RADIX_DAX_EMPTY; |
| 113 | } |
| 114 | |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 115 | /* |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 116 | * DAX radix tree locking |
| 117 | */ |
| 118 | struct exceptional_entry_key { |
| 119 | struct address_space *mapping; |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 120 | pgoff_t entry_start; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | struct wait_exceptional_entry_queue { |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 124 | wait_queue_entry_t wait; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 125 | struct exceptional_entry_key key; |
| 126 | }; |
| 127 | |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 128 | static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping, |
| 129 | pgoff_t index, void *entry, struct exceptional_entry_key *key) |
| 130 | { |
| 131 | unsigned long hash; |
| 132 | |
| 133 | /* |
| 134 | * If 'entry' is a PMD, align the 'index' that we use for the wait |
| 135 | * queue to the start of that PMD. This ensures that all offsets in |
| 136 | * the range covered by the PMD map to the same bit lock. |
| 137 | */ |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 138 | if (dax_is_pmd_entry(entry)) |
Ross Zwisler | 917f345 | 2017-09-06 16:18:58 -0700 | [diff] [blame] | 139 | index &= ~PG_PMD_COLOUR; |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 140 | |
| 141 | key->mapping = mapping; |
| 142 | key->entry_start = index; |
| 143 | |
| 144 | hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS); |
| 145 | return wait_table + hash; |
| 146 | } |
| 147 | |
Ingo Molnar | ac6424b | 2017-06-20 12:06:13 +0200 | [diff] [blame] | 148 | static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode, |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 149 | int sync, void *keyp) |
| 150 | { |
| 151 | struct exceptional_entry_key *key = keyp; |
| 152 | struct wait_exceptional_entry_queue *ewait = |
| 153 | container_of(wait, struct wait_exceptional_entry_queue, wait); |
| 154 | |
| 155 | if (key->mapping != ewait->key.mapping || |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 156 | key->entry_start != ewait->key.entry_start) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 157 | return 0; |
| 158 | return autoremove_wake_function(wait, mode, sync, NULL); |
| 159 | } |
| 160 | |
| 161 | /* |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 162 | * We do not necessarily hold the mapping->tree_lock when we call this |
| 163 | * function so it is possible that 'entry' is no longer a valid item in the |
| 164 | * radix tree. This is okay because all we really need to do is to find the |
| 165 | * correct waitqueue where tasks might be waiting for that old 'entry' and |
| 166 | * wake them. |
| 167 | */ |
Ross Zwisler | d01ad19 | 2017-09-06 16:18:47 -0700 | [diff] [blame] | 168 | static void dax_wake_mapping_entry_waiter(struct address_space *mapping, |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 169 | pgoff_t index, void *entry, bool wake_all) |
| 170 | { |
| 171 | struct exceptional_entry_key key; |
| 172 | wait_queue_head_t *wq; |
| 173 | |
| 174 | wq = dax_entry_waitqueue(mapping, index, entry, &key); |
| 175 | |
| 176 | /* |
| 177 | * Checking for locked entry and prepare_to_wait_exclusive() happens |
| 178 | * under mapping->tree_lock, ditto for entry handling in our callers. |
| 179 | * So at this point all tasks that could have seen our entry locked |
| 180 | * must be in the waitqueue and the following check will see them. |
| 181 | */ |
| 182 | if (waitqueue_active(wq)) |
| 183 | __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key); |
| 184 | } |
| 185 | |
| 186 | /* |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 187 | * Check whether the given slot is locked. The function must be called with |
| 188 | * mapping->tree_lock held |
| 189 | */ |
| 190 | static inline int slot_locked(struct address_space *mapping, void **slot) |
| 191 | { |
| 192 | unsigned long entry = (unsigned long) |
| 193 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 194 | return entry & RADIX_DAX_ENTRY_LOCK; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Mark the given slot is locked. The function must be called with |
| 199 | * mapping->tree_lock held |
| 200 | */ |
| 201 | static inline void *lock_slot(struct address_space *mapping, void **slot) |
| 202 | { |
| 203 | unsigned long entry = (unsigned long) |
| 204 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 205 | |
| 206 | entry |= RADIX_DAX_ENTRY_LOCK; |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 207 | radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 208 | return (void *)entry; |
| 209 | } |
| 210 | |
| 211 | /* |
| 212 | * Mark the given slot is unlocked. The function must be called with |
| 213 | * mapping->tree_lock held |
| 214 | */ |
| 215 | static inline void *unlock_slot(struct address_space *mapping, void **slot) |
| 216 | { |
| 217 | unsigned long entry = (unsigned long) |
| 218 | radix_tree_deref_slot_protected(slot, &mapping->tree_lock); |
| 219 | |
| 220 | entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK; |
Johannes Weiner | 6d75f36 | 2016-12-12 16:43:43 -0800 | [diff] [blame] | 221 | radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 222 | return (void *)entry; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * Lookup entry in radix tree, wait for it to become unlocked if it is |
| 227 | * exceptional entry and return it. The caller must call |
| 228 | * put_unlocked_mapping_entry() when he decided not to lock the entry or |
| 229 | * put_locked_mapping_entry() when he locked the entry and now wants to |
| 230 | * unlock it. |
| 231 | * |
| 232 | * The function must be called with mapping->tree_lock held. |
| 233 | */ |
| 234 | static void *get_unlocked_mapping_entry(struct address_space *mapping, |
| 235 | pgoff_t index, void ***slotp) |
| 236 | { |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 237 | void *entry, **slot; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 238 | struct wait_exceptional_entry_queue ewait; |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 239 | wait_queue_head_t *wq; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 240 | |
| 241 | init_wait(&ewait.wait); |
| 242 | ewait.wait.func = wake_exceptional_entry_func; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 243 | |
| 244 | for (;;) { |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 245 | entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 246 | &slot); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 247 | if (!entry || |
| 248 | WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) || |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 249 | !slot_locked(mapping, slot)) { |
| 250 | if (slotp) |
| 251 | *slotp = slot; |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 252 | return entry; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 253 | } |
Ross Zwisler | 63e95b5 | 2016-11-08 11:32:20 +1100 | [diff] [blame] | 254 | |
| 255 | wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 256 | prepare_to_wait_exclusive(wq, &ewait.wait, |
| 257 | TASK_UNINTERRUPTIBLE); |
| 258 | spin_unlock_irq(&mapping->tree_lock); |
| 259 | schedule(); |
| 260 | finish_wait(wq, &ewait.wait); |
| 261 | spin_lock_irq(&mapping->tree_lock); |
| 262 | } |
| 263 | } |
| 264 | |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 265 | static void dax_unlock_mapping_entry(struct address_space *mapping, |
| 266 | pgoff_t index) |
| 267 | { |
| 268 | void *entry, **slot; |
| 269 | |
| 270 | spin_lock_irq(&mapping->tree_lock); |
| 271 | entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot); |
| 272 | if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) || |
| 273 | !slot_locked(mapping, slot))) { |
| 274 | spin_unlock_irq(&mapping->tree_lock); |
| 275 | return; |
| 276 | } |
| 277 | unlock_slot(mapping, slot); |
| 278 | spin_unlock_irq(&mapping->tree_lock); |
| 279 | dax_wake_mapping_entry_waiter(mapping, index, entry, false); |
| 280 | } |
| 281 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 282 | static void put_locked_mapping_entry(struct address_space *mapping, |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 283 | pgoff_t index) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 284 | { |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 285 | dax_unlock_mapping_entry(mapping, index); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Called when we are done with radix tree entry we looked up via |
| 290 | * get_unlocked_mapping_entry() and which we didn't lock in the end. |
| 291 | */ |
| 292 | static void put_unlocked_mapping_entry(struct address_space *mapping, |
| 293 | pgoff_t index, void *entry) |
| 294 | { |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 295 | if (!entry) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 296 | return; |
| 297 | |
| 298 | /* We have to wake up next waiter for the radix tree entry lock */ |
Ross Zwisler | 422476c | 2016-11-08 11:33:44 +1100 | [diff] [blame] | 299 | dax_wake_mapping_entry_waiter(mapping, index, entry, false); |
| 300 | } |
| 301 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 302 | /* |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 303 | * Find radix tree entry at given index. If it points to an exceptional entry, |
| 304 | * return it with the radix tree entry locked. If the radix tree doesn't |
| 305 | * contain given index, create an empty exceptional entry for the index and |
| 306 | * return with it locked. |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 307 | * |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 308 | * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will |
| 309 | * either return that locked entry or will return an error. This error will |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 310 | * happen if there are any 4k entries within the 2MiB range that we are |
| 311 | * requesting. |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 312 | * |
| 313 | * We always favor 4k entries over 2MiB entries. There isn't a flow where we |
| 314 | * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB |
| 315 | * insertion will fail if it finds any 4k entries already in the tree, and a |
| 316 | * 4k insertion will cause an existing 2MiB entry to be unmapped and |
| 317 | * downgraded to 4k entries. This happens for both 2MiB huge zero pages as |
| 318 | * well as 2MiB empty entries. |
| 319 | * |
| 320 | * The exception to this downgrade path is for 2MiB DAX PMD entries that have |
| 321 | * real storage backing them. We will leave these real 2MiB DAX entries in |
| 322 | * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry. |
| 323 | * |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 324 | * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For |
| 325 | * persistent memory the benefit is doubtful. We can add that later if we can |
| 326 | * show it helps. |
| 327 | */ |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 328 | static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index, |
| 329 | unsigned long size_flag) |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 330 | { |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 331 | bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */ |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 332 | void *entry, **slot; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 333 | |
| 334 | restart: |
| 335 | spin_lock_irq(&mapping->tree_lock); |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 336 | entry = get_unlocked_mapping_entry(mapping, index, &slot); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 337 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 338 | if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) { |
| 339 | entry = ERR_PTR(-EIO); |
| 340 | goto out_unlock; |
| 341 | } |
| 342 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 343 | if (entry) { |
| 344 | if (size_flag & RADIX_DAX_PMD) { |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 345 | if (dax_is_pte_entry(entry)) { |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 346 | put_unlocked_mapping_entry(mapping, index, |
| 347 | entry); |
| 348 | entry = ERR_PTR(-EEXIST); |
| 349 | goto out_unlock; |
| 350 | } |
| 351 | } else { /* trying to grab a PTE entry */ |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 352 | if (dax_is_pmd_entry(entry) && |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 353 | (dax_is_zero_entry(entry) || |
| 354 | dax_is_empty_entry(entry))) { |
| 355 | pmd_downgrade = true; |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 360 | /* No entry for given index? Make sure radix tree is big enough. */ |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 361 | if (!entry || pmd_downgrade) { |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 362 | int err; |
| 363 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 364 | if (pmd_downgrade) { |
| 365 | /* |
| 366 | * Make sure 'entry' remains valid while we drop |
| 367 | * mapping->tree_lock. |
| 368 | */ |
| 369 | entry = lock_slot(mapping, slot); |
| 370 | } |
| 371 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 372 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 373 | /* |
| 374 | * Besides huge zero pages the only other thing that gets |
| 375 | * downgraded are empty entries which don't need to be |
| 376 | * unmapped. |
| 377 | */ |
| 378 | if (pmd_downgrade && dax_is_zero_entry(entry)) |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 379 | unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, |
| 380 | PG_PMD_NR, false); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 381 | |
Jan Kara | 0cb80b4 | 2016-12-12 21:34:12 -0500 | [diff] [blame] | 382 | err = radix_tree_preload( |
| 383 | mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM); |
| 384 | if (err) { |
| 385 | if (pmd_downgrade) |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 386 | put_locked_mapping_entry(mapping, index); |
Jan Kara | 0cb80b4 | 2016-12-12 21:34:12 -0500 | [diff] [blame] | 387 | return ERR_PTR(err); |
| 388 | } |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 389 | spin_lock_irq(&mapping->tree_lock); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 390 | |
Ross Zwisler | e11f8b7 | 2017-04-07 16:04:57 -0700 | [diff] [blame] | 391 | if (!entry) { |
| 392 | /* |
| 393 | * We needed to drop the page_tree lock while calling |
| 394 | * radix_tree_preload() and we didn't have an entry to |
| 395 | * lock. See if another thread inserted an entry at |
| 396 | * our index during this time. |
| 397 | */ |
| 398 | entry = __radix_tree_lookup(&mapping->page_tree, index, |
| 399 | NULL, &slot); |
| 400 | if (entry) { |
| 401 | radix_tree_preload_end(); |
| 402 | spin_unlock_irq(&mapping->tree_lock); |
| 403 | goto restart; |
| 404 | } |
| 405 | } |
| 406 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 407 | if (pmd_downgrade) { |
| 408 | radix_tree_delete(&mapping->page_tree, index); |
| 409 | mapping->nrexceptional--; |
| 410 | dax_wake_mapping_entry_waiter(mapping, index, entry, |
| 411 | true); |
| 412 | } |
| 413 | |
| 414 | entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY); |
| 415 | |
| 416 | err = __radix_tree_insert(&mapping->page_tree, index, |
| 417 | dax_radix_order(entry), entry); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 418 | radix_tree_preload_end(); |
| 419 | if (err) { |
| 420 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 421 | /* |
Ross Zwisler | e11f8b7 | 2017-04-07 16:04:57 -0700 | [diff] [blame] | 422 | * Our insertion of a DAX entry failed, most likely |
| 423 | * because we were inserting a PMD entry and it |
| 424 | * collided with a PTE sized entry at a different |
| 425 | * index in the PMD range. We haven't inserted |
| 426 | * anything into the radix tree and have no waiters to |
| 427 | * wake. |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 428 | */ |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 429 | return ERR_PTR(err); |
| 430 | } |
| 431 | /* Good, we have inserted empty locked entry into the tree. */ |
| 432 | mapping->nrexceptional++; |
| 433 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 434 | return entry; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 435 | } |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 436 | entry = lock_slot(mapping, slot); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 437 | out_unlock: |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 438 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | e3ad61c | 2016-11-08 11:32:12 +1100 | [diff] [blame] | 439 | return entry; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 442 | static int __dax_invalidate_mapping_entry(struct address_space *mapping, |
| 443 | pgoff_t index, bool trunc) |
| 444 | { |
| 445 | int ret = 0; |
| 446 | void *entry; |
| 447 | struct radix_tree_root *page_tree = &mapping->page_tree; |
| 448 | |
| 449 | spin_lock_irq(&mapping->tree_lock); |
| 450 | entry = get_unlocked_mapping_entry(mapping, index, NULL); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 451 | if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry))) |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 452 | goto out; |
| 453 | if (!trunc && |
| 454 | (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) || |
| 455 | radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))) |
| 456 | goto out; |
| 457 | radix_tree_delete(page_tree, index); |
| 458 | mapping->nrexceptional--; |
| 459 | ret = 1; |
| 460 | out: |
| 461 | put_unlocked_mapping_entry(mapping, index, entry); |
| 462 | spin_unlock_irq(&mapping->tree_lock); |
| 463 | return ret; |
| 464 | } |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 465 | /* |
| 466 | * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree |
| 467 | * entry to get unlocked before deleting it. |
| 468 | */ |
| 469 | int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index) |
| 470 | { |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 471 | int ret = __dax_invalidate_mapping_entry(mapping, index, true); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 472 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 473 | /* |
| 474 | * This gets called from truncate / punch_hole path. As such, the caller |
| 475 | * must hold locks protecting against concurrent modifications of the |
| 476 | * radix tree (usually fs-private i_mmap_sem for writing). Since the |
| 477 | * caller has seen exceptional entry for this index, we better find it |
| 478 | * at that index as well... |
| 479 | */ |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 480 | WARN_ON_ONCE(!ret); |
| 481 | return ret; |
| 482 | } |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 483 | |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 484 | /* |
Jan Kara | c6dcf52 | 2016-08-10 17:22:44 +0200 | [diff] [blame] | 485 | * Invalidate exceptional DAX entry if it is clean. |
| 486 | */ |
| 487 | int dax_invalidate_mapping_entry_sync(struct address_space *mapping, |
| 488 | pgoff_t index) |
| 489 | { |
| 490 | return __dax_invalidate_mapping_entry(mapping, index, false); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 491 | } |
| 492 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 493 | static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev, |
| 494 | sector_t sector, size_t size, struct page *to, |
| 495 | unsigned long vaddr) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 496 | { |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 497 | void *vto, *kaddr; |
| 498 | pgoff_t pgoff; |
| 499 | pfn_t pfn; |
| 500 | long rc; |
| 501 | int id; |
Ross Zwisler | e2e0539 | 2015-08-18 13:55:41 -0600 | [diff] [blame] | 502 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 503 | rc = bdev_dax_pgoff(bdev, sector, size, &pgoff); |
| 504 | if (rc) |
| 505 | return rc; |
| 506 | |
| 507 | id = dax_read_lock(); |
| 508 | rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn); |
| 509 | if (rc < 0) { |
| 510 | dax_read_unlock(id); |
| 511 | return rc; |
| 512 | } |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 513 | vto = kmap_atomic(to); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 514 | copy_user_page(vto, (void __force *)kaddr, vaddr, to); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 515 | kunmap_atomic(vto); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 516 | dax_read_unlock(id); |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 520 | /* |
| 521 | * By this point grab_mapping_entry() has ensured that we have a locked entry |
| 522 | * of the appropriate size so we don't have to worry about downgrading PMDs to |
| 523 | * PTEs. If we happen to be trying to insert a PTE and there is a PMD |
| 524 | * already in the tree, we will skip the insertion and just dirty the PMD as |
| 525 | * appropriate. |
| 526 | */ |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 527 | static void *dax_insert_mapping_entry(struct address_space *mapping, |
| 528 | struct vm_fault *vmf, |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 529 | void *entry, sector_t sector, |
Jan Kara | f5b7b74 | 2017-11-01 16:36:40 +0100 | [diff] [blame] | 530 | unsigned long flags, bool dirty) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 531 | { |
| 532 | struct radix_tree_root *page_tree = &mapping->page_tree; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 533 | void *new_entry; |
| 534 | pgoff_t index = vmf->pgoff; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 535 | |
Jan Kara | f5b7b74 | 2017-11-01 16:36:40 +0100 | [diff] [blame] | 536 | if (dirty) |
Dmitry Monakhov | d2b2a28 | 2016-02-05 15:36:55 -0800 | [diff] [blame] | 537 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 538 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 539 | if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) { |
| 540 | /* we are replacing a zero page with block mapping */ |
| 541 | if (dax_is_pmd_entry(entry)) |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 542 | unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR, |
| 543 | PG_PMD_NR, false); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 544 | else /* pte entry */ |
Matthew Wilcox | 977fbdc | 2018-01-31 16:17:36 -0800 | [diff] [blame] | 545 | unmap_mapping_pages(mapping, vmf->pgoff, 1, false); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 546 | } |
| 547 | |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 548 | spin_lock_irq(&mapping->tree_lock); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 549 | new_entry = dax_radix_locked_entry(sector, flags); |
| 550 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 551 | if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) { |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 552 | /* |
| 553 | * Only swap our new entry into the radix tree if the current |
| 554 | * entry is a zero page or an empty entry. If a normal PTE or |
| 555 | * PMD entry is already in the tree, we leave it alone. This |
| 556 | * means that if we are trying to insert a PTE and the |
| 557 | * existing entry is a PMD, we will just leave the PMD in the |
| 558 | * tree and dirty it if necessary. |
| 559 | */ |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 560 | struct radix_tree_node *node; |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 561 | void **slot; |
| 562 | void *ret; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 563 | |
Johannes Weiner | f794243 | 2016-12-12 16:43:41 -0800 | [diff] [blame] | 564 | ret = __radix_tree_lookup(page_tree, index, &node, &slot); |
Jan Kara | ac401cc | 2016-05-12 18:29:18 +0200 | [diff] [blame] | 565 | WARN_ON_ONCE(ret != entry); |
Johannes Weiner | 4d693d0 | 2016-12-12 16:43:49 -0800 | [diff] [blame] | 566 | __radix_tree_replace(page_tree, node, slot, |
Mel Gorman | c7df8ad | 2017-11-15 17:37:41 -0800 | [diff] [blame] | 567 | new_entry, NULL); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 568 | entry = new_entry; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 569 | } |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 570 | |
Jan Kara | f5b7b74 | 2017-11-01 16:36:40 +0100 | [diff] [blame] | 571 | if (dirty) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 572 | radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 573 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 574 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 575 | return entry; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 578 | static inline unsigned long |
| 579 | pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) |
| 580 | { |
| 581 | unsigned long address; |
| 582 | |
| 583 | address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT); |
| 584 | VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma); |
| 585 | return address; |
| 586 | } |
| 587 | |
| 588 | /* Walk all mappings of a given index of a file and writeprotect them */ |
| 589 | static void dax_mapping_entry_mkclean(struct address_space *mapping, |
| 590 | pgoff_t index, unsigned long pfn) |
| 591 | { |
| 592 | struct vm_area_struct *vma; |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 593 | pte_t pte, *ptep = NULL; |
| 594 | pmd_t *pmdp = NULL; |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 595 | spinlock_t *ptl; |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 596 | |
| 597 | i_mmap_lock_read(mapping); |
| 598 | vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) { |
Jérôme Glisse | a4d1a88 | 2017-08-31 17:17:26 -0400 | [diff] [blame] | 599 | unsigned long address, start, end; |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 600 | |
| 601 | cond_resched(); |
| 602 | |
| 603 | if (!(vma->vm_flags & VM_SHARED)) |
| 604 | continue; |
| 605 | |
| 606 | address = pgoff_address(index, vma); |
Jérôme Glisse | a4d1a88 | 2017-08-31 17:17:26 -0400 | [diff] [blame] | 607 | |
| 608 | /* |
| 609 | * Note because we provide start/end to follow_pte_pmd it will |
| 610 | * call mmu_notifier_invalidate_range_start() on our behalf |
| 611 | * before taking any lock. |
| 612 | */ |
| 613 | if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl)) |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 614 | continue; |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 615 | |
Jérôme Glisse | 0f10851 | 2017-11-15 17:34:07 -0800 | [diff] [blame] | 616 | /* |
| 617 | * No need to call mmu_notifier_invalidate_range() as we are |
| 618 | * downgrading page table protection not changing it to point |
| 619 | * to a new page. |
| 620 | * |
| 621 | * See Documentation/vm/mmu_notifier.txt |
| 622 | */ |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 623 | if (pmdp) { |
| 624 | #ifdef CONFIG_FS_DAX_PMD |
| 625 | pmd_t pmd; |
| 626 | |
| 627 | if (pfn != pmd_pfn(*pmdp)) |
| 628 | goto unlock_pmd; |
Linus Torvalds | f6f3732 | 2017-12-15 18:53:22 -0800 | [diff] [blame] | 629 | if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp)) |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 630 | goto unlock_pmd; |
| 631 | |
| 632 | flush_cache_page(vma, address, pfn); |
| 633 | pmd = pmdp_huge_clear_flush(vma, address, pmdp); |
| 634 | pmd = pmd_wrprotect(pmd); |
| 635 | pmd = pmd_mkclean(pmd); |
| 636 | set_pmd_at(vma->vm_mm, address, pmdp, pmd); |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 637 | unlock_pmd: |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 638 | #endif |
Jan H. Schönherr | ee190ca | 2018-01-31 16:14:04 -0800 | [diff] [blame] | 639 | spin_unlock(ptl); |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 640 | } else { |
| 641 | if (pfn != pte_pfn(*ptep)) |
| 642 | goto unlock_pte; |
| 643 | if (!pte_dirty(*ptep) && !pte_write(*ptep)) |
| 644 | goto unlock_pte; |
| 645 | |
| 646 | flush_cache_page(vma, address, pfn); |
| 647 | pte = ptep_clear_flush(vma, address, ptep); |
| 648 | pte = pte_wrprotect(pte); |
| 649 | pte = pte_mkclean(pte); |
| 650 | set_pte_at(vma->vm_mm, address, ptep, pte); |
Ross Zwisler | f729c8c | 2017-01-10 16:57:24 -0800 | [diff] [blame] | 651 | unlock_pte: |
| 652 | pte_unmap_unlock(ptep, ptl); |
| 653 | } |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 654 | |
Jérôme Glisse | a4d1a88 | 2017-08-31 17:17:26 -0400 | [diff] [blame] | 655 | mmu_notifier_invalidate_range_end(vma->vm_mm, start, end); |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 656 | } |
| 657 | i_mmap_unlock_read(mapping); |
| 658 | } |
| 659 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 660 | static int dax_writeback_one(struct block_device *bdev, |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 661 | struct dax_device *dax_dev, struct address_space *mapping, |
| 662 | pgoff_t index, void *entry) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 663 | { |
| 664 | struct radix_tree_root *page_tree = &mapping->page_tree; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 665 | void *entry2, **slot, *kaddr; |
| 666 | long ret = 0, id; |
| 667 | sector_t sector; |
| 668 | pgoff_t pgoff; |
| 669 | size_t size; |
| 670 | pfn_t pfn; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 671 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 672 | /* |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 673 | * A page got tagged dirty in DAX mapping? Something is seriously |
| 674 | * wrong. |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 675 | */ |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 676 | if (WARN_ON(!radix_tree_exceptional_entry(entry))) |
| 677 | return -EIO; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 678 | |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 679 | spin_lock_irq(&mapping->tree_lock); |
| 680 | entry2 = get_unlocked_mapping_entry(mapping, index, &slot); |
| 681 | /* Entry got punched out / reallocated? */ |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 682 | if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2))) |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 683 | goto put_unlocked; |
| 684 | /* |
| 685 | * Entry got reallocated elsewhere? No need to writeback. We have to |
| 686 | * compare sectors as we must not bail out due to difference in lockbit |
| 687 | * or entry type. |
| 688 | */ |
| 689 | if (dax_radix_sector(entry2) != dax_radix_sector(entry)) |
| 690 | goto put_unlocked; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 691 | if (WARN_ON_ONCE(dax_is_empty_entry(entry) || |
| 692 | dax_is_zero_entry(entry))) { |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 693 | ret = -EIO; |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 694 | goto put_unlocked; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 695 | } |
| 696 | |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 697 | /* Another fsync thread may have already written back this entry */ |
| 698 | if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)) |
| 699 | goto put_unlocked; |
| 700 | /* Lock the entry to serialize with page faults */ |
| 701 | entry = lock_slot(mapping, slot); |
| 702 | /* |
| 703 | * We can clear the tag now but we have to be careful so that concurrent |
| 704 | * dax_writeback_one() calls for the same index cannot finish before we |
| 705 | * actually flush the caches. This is achieved as the calls will look |
| 706 | * at the entry only under tree_lock and once they do that they will |
| 707 | * see the entry locked and wait for it to unlock. |
| 708 | */ |
| 709 | radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE); |
| 710 | spin_unlock_irq(&mapping->tree_lock); |
| 711 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 712 | /* |
| 713 | * Even if dax_writeback_mapping_range() was given a wbc->range_start |
| 714 | * in the middle of a PMD, the 'index' we are given will be aligned to |
| 715 | * the start index of the PMD, as will the sector we pull from |
| 716 | * 'entry'. This allows us to flush for PMD_SIZE and not have to |
| 717 | * worry about partial PMD writebacks. |
| 718 | */ |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 719 | sector = dax_radix_sector(entry); |
| 720 | size = PAGE_SIZE << dax_radix_order(entry); |
| 721 | |
| 722 | id = dax_read_lock(); |
| 723 | ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); |
| 724 | if (ret) |
| 725 | goto dax_unlock; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 726 | |
| 727 | /* |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 728 | * dax_direct_access() may sleep, so cannot hold tree_lock over |
| 729 | * its invocation. |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 730 | */ |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 731 | ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn); |
| 732 | if (ret < 0) |
| 733 | goto dax_unlock; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 734 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 735 | if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) { |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 736 | ret = -EIO; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 737 | goto dax_unlock; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 740 | dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn)); |
Mikulas Patocka | c3ca015 | 2017-08-31 21:47:43 -0400 | [diff] [blame] | 741 | dax_flush(dax_dev, kaddr, size); |
Jan Kara | 4b4bb46 | 2016-12-14 15:07:53 -0800 | [diff] [blame] | 742 | /* |
| 743 | * After we have flushed the cache, we can clear the dirty tag. There |
| 744 | * cannot be new dirty data in the pfn after the flush has completed as |
| 745 | * the pfn mappings are writeprotected and fault waits for mapping |
| 746 | * entry lock. |
| 747 | */ |
| 748 | spin_lock_irq(&mapping->tree_lock); |
| 749 | radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY); |
| 750 | spin_unlock_irq(&mapping->tree_lock); |
Ross Zwisler | f9bc3a0 | 2017-05-08 16:00:13 -0700 | [diff] [blame] | 751 | trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 752 | dax_unlock: |
| 753 | dax_read_unlock(id); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 754 | put_locked_mapping_entry(mapping, index); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 755 | return ret; |
| 756 | |
Jan Kara | a6abc2c | 2016-12-14 15:07:47 -0800 | [diff] [blame] | 757 | put_unlocked: |
| 758 | put_unlocked_mapping_entry(mapping, index, entry2); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 759 | spin_unlock_irq(&mapping->tree_lock); |
| 760 | return ret; |
| 761 | } |
| 762 | |
| 763 | /* |
| 764 | * Flush the mapping to the persistent domain within the byte range of [start, |
| 765 | * end]. This is required by data integrity operations to ensure file data is |
| 766 | * on persistent storage prior to completion of the operation. |
| 767 | */ |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 768 | int dax_writeback_mapping_range(struct address_space *mapping, |
| 769 | struct block_device *bdev, struct writeback_control *wbc) |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 770 | { |
| 771 | struct inode *inode = mapping->host; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 772 | pgoff_t start_index, end_index; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 773 | pgoff_t indices[PAGEVEC_SIZE]; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 774 | struct dax_device *dax_dev; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 775 | struct pagevec pvec; |
| 776 | bool done = false; |
| 777 | int i, ret = 0; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 778 | |
| 779 | if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT)) |
| 780 | return -EIO; |
| 781 | |
Ross Zwisler | 7f6d5b5 | 2016-02-26 15:19:55 -0800 | [diff] [blame] | 782 | if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL) |
| 783 | return 0; |
| 784 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 785 | dax_dev = dax_get_by_host(bdev->bd_disk->disk_name); |
| 786 | if (!dax_dev) |
| 787 | return -EIO; |
| 788 | |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 789 | start_index = wbc->range_start >> PAGE_SHIFT; |
| 790 | end_index = wbc->range_end >> PAGE_SHIFT; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 791 | |
Ross Zwisler | d14a3f4 | 2017-05-08 16:00:10 -0700 | [diff] [blame] | 792 | trace_dax_writeback_range(inode, start_index, end_index); |
| 793 | |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 794 | tag_pages_for_writeback(mapping, start_index, end_index); |
| 795 | |
Mel Gorman | 8667982 | 2017-11-15 17:37:52 -0800 | [diff] [blame] | 796 | pagevec_init(&pvec); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 797 | while (!done) { |
| 798 | pvec.nr = find_get_entries_tag(mapping, start_index, |
| 799 | PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE, |
| 800 | pvec.pages, indices); |
| 801 | |
| 802 | if (pvec.nr == 0) |
| 803 | break; |
| 804 | |
| 805 | for (i = 0; i < pvec.nr; i++) { |
| 806 | if (indices[i] > end_index) { |
| 807 | done = true; |
| 808 | break; |
| 809 | } |
| 810 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 811 | ret = dax_writeback_one(bdev, dax_dev, mapping, |
| 812 | indices[i], pvec.pages[i]); |
Jeff Layton | 819ec6b | 2017-07-06 07:02:27 -0400 | [diff] [blame] | 813 | if (ret < 0) { |
| 814 | mapping_set_error(mapping, ret); |
Ross Zwisler | d14a3f4 | 2017-05-08 16:00:10 -0700 | [diff] [blame] | 815 | goto out; |
Jeff Layton | 819ec6b | 2017-07-06 07:02:27 -0400 | [diff] [blame] | 816 | } |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 817 | } |
Jan Kara | 1eb643d | 2017-06-23 15:08:46 -0700 | [diff] [blame] | 818 | start_index = indices[pvec.nr - 1] + 1; |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 819 | } |
Ross Zwisler | d14a3f4 | 2017-05-08 16:00:10 -0700 | [diff] [blame] | 820 | out: |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 821 | put_dax(dax_dev); |
Ross Zwisler | d14a3f4 | 2017-05-08 16:00:10 -0700 | [diff] [blame] | 822 | trace_dax_writeback_range_done(inode, start_index, end_index); |
| 823 | return (ret < 0 ? ret : 0); |
Ross Zwisler | 9973c98 | 2016-01-22 15:10:47 -0800 | [diff] [blame] | 824 | } |
| 825 | EXPORT_SYMBOL_GPL(dax_writeback_mapping_range); |
| 826 | |
Jan Kara | 31a6f1a | 2017-11-01 16:36:32 +0100 | [diff] [blame] | 827 | static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos) |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 828 | { |
Linus Torvalds | a3841f9 | 2017-11-17 09:51:57 -0800 | [diff] [blame] | 829 | return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9; |
Jan Kara | 31a6f1a | 2017-11-01 16:36:32 +0100 | [diff] [blame] | 830 | } |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 831 | |
Jan Kara | 5e161e4 | 2017-11-01 16:36:33 +0100 | [diff] [blame] | 832 | static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size, |
| 833 | pfn_t *pfnp) |
| 834 | { |
| 835 | const sector_t sector = dax_iomap_sector(iomap, pos); |
| 836 | pgoff_t pgoff; |
| 837 | void *kaddr; |
| 838 | int id, rc; |
| 839 | long length; |
| 840 | |
| 841 | rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 842 | if (rc) |
| 843 | return rc; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 844 | id = dax_read_lock(); |
Jan Kara | 5e161e4 | 2017-11-01 16:36:33 +0100 | [diff] [blame] | 845 | length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size), |
| 846 | &kaddr, pfnp); |
| 847 | if (length < 0) { |
| 848 | rc = length; |
| 849 | goto out; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 850 | } |
Jan Kara | 5e161e4 | 2017-11-01 16:36:33 +0100 | [diff] [blame] | 851 | rc = -EINVAL; |
| 852 | if (PFN_PHYS(length) < size) |
| 853 | goto out; |
| 854 | if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1)) |
| 855 | goto out; |
| 856 | /* For larger pages we need devmap */ |
| 857 | if (length > 1 && !pfn_t_devmap(*pfnp)) |
| 858 | goto out; |
| 859 | rc = 0; |
| 860 | out: |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 861 | dax_read_unlock(id); |
Jan Kara | 5e161e4 | 2017-11-01 16:36:33 +0100 | [diff] [blame] | 862 | return rc; |
Matthew Wilcox | f7ca90b | 2015-02-16 15:59:02 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 865 | /* |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 866 | * The user has performed a load from a hole in the file. Allocating a new |
| 867 | * page in the file would cause excessive storage usage for workloads with |
| 868 | * sparse files. Instead we insert a read-only mapping of the 4k zero page. |
| 869 | * If this page is ever written to we will re-fault and change the mapping to |
| 870 | * point to real DAX storage instead. |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 871 | */ |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 872 | static int dax_load_hole(struct address_space *mapping, void *entry, |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 873 | struct vm_fault *vmf) |
| 874 | { |
| 875 | struct inode *inode = mapping->host; |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 876 | unsigned long vaddr = vmf->address; |
| 877 | int ret = VM_FAULT_NOPAGE; |
| 878 | struct page *zero_page; |
| 879 | void *entry2; |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 880 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 881 | zero_page = ZERO_PAGE(0); |
| 882 | if (unlikely(!zero_page)) { |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 883 | ret = VM_FAULT_OOM; |
| 884 | goto out; |
| 885 | } |
| 886 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 887 | entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0, |
Jan Kara | f5b7b74 | 2017-11-01 16:36:40 +0100 | [diff] [blame] | 888 | RADIX_DAX_ZERO_PAGE, false); |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 889 | if (IS_ERR(entry2)) { |
| 890 | ret = VM_FAULT_SIGBUS; |
| 891 | goto out; |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 892 | } |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 893 | |
| 894 | vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page)); |
Ross Zwisler | e30331f | 2017-09-06 16:18:39 -0700 | [diff] [blame] | 895 | out: |
| 896 | trace_dax_load_hole(inode, vmf, ret); |
| 897 | return ret; |
| 898 | } |
| 899 | |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 900 | static bool dax_range_is_aligned(struct block_device *bdev, |
| 901 | unsigned int offset, unsigned int length) |
| 902 | { |
| 903 | unsigned short sector_size = bdev_logical_block_size(bdev); |
| 904 | |
| 905 | if (!IS_ALIGNED(offset, sector_size)) |
| 906 | return false; |
| 907 | if (!IS_ALIGNED(length, sector_size)) |
| 908 | return false; |
| 909 | |
| 910 | return true; |
| 911 | } |
| 912 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 913 | int __dax_zero_page_range(struct block_device *bdev, |
| 914 | struct dax_device *dax_dev, sector_t sector, |
| 915 | unsigned int offset, unsigned int size) |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 916 | { |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 917 | if (dax_range_is_aligned(bdev, offset, size)) { |
| 918 | sector_t start_sector = sector + (offset >> 9); |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 919 | |
| 920 | return blkdev_issue_zeroout(bdev, start_sector, |
Linus Torvalds | 53ef7d0 | 2017-05-05 18:49:20 -0700 | [diff] [blame] | 921 | size >> 9, GFP_NOFS, 0); |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 922 | } else { |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 923 | pgoff_t pgoff; |
| 924 | long rc, id; |
| 925 | void *kaddr; |
| 926 | pfn_t pfn; |
| 927 | |
Dan Williams | e84b83b | 2017-05-10 19:38:13 -0700 | [diff] [blame] | 928 | rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 929 | if (rc) |
| 930 | return rc; |
| 931 | |
| 932 | id = dax_read_lock(); |
Dan Williams | e84b83b | 2017-05-10 19:38:13 -0700 | [diff] [blame] | 933 | rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 934 | &pfn); |
| 935 | if (rc < 0) { |
| 936 | dax_read_unlock(id); |
| 937 | return rc; |
| 938 | } |
Dan Williams | 81f5587 | 2017-05-29 13:12:20 -0700 | [diff] [blame] | 939 | memset(kaddr + offset, 0, size); |
Mikulas Patocka | c3ca015 | 2017-08-31 21:47:43 -0400 | [diff] [blame] | 940 | dax_flush(dax_dev, kaddr + offset, size); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 941 | dax_read_unlock(id); |
Vishal Verma | 4b0228f | 2016-04-21 15:13:46 -0400 | [diff] [blame] | 942 | } |
Christoph Hellwig | 679c8bd | 2016-05-09 10:47:04 +0200 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | EXPORT_SYMBOL_GPL(__dax_zero_page_range); |
| 946 | |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 947 | static loff_t |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 948 | dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data, |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 949 | struct iomap *iomap) |
| 950 | { |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 951 | struct block_device *bdev = iomap->bdev; |
| 952 | struct dax_device *dax_dev = iomap->dax_dev; |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 953 | struct iov_iter *iter = data; |
| 954 | loff_t end = pos + length, done = 0; |
| 955 | ssize_t ret = 0; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 956 | int id; |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 957 | |
| 958 | if (iov_iter_rw(iter) == READ) { |
| 959 | end = min(end, i_size_read(inode)); |
| 960 | if (pos >= end) |
| 961 | return 0; |
| 962 | |
| 963 | if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN) |
| 964 | return iov_iter_zero(min(length, end - pos), iter); |
| 965 | } |
| 966 | |
| 967 | if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED)) |
| 968 | return -EIO; |
| 969 | |
Jan Kara | e3fce68 | 2016-08-10 17:10:28 +0200 | [diff] [blame] | 970 | /* |
| 971 | * Write can allocate block for an area which has a hole page mapped |
| 972 | * into page tables. We have to tear down these mappings so that data |
| 973 | * written by write(2) is visible in mmap. |
| 974 | */ |
Jan Kara | cd65637 | 2017-05-12 15:46:50 -0700 | [diff] [blame] | 975 | if (iomap->flags & IOMAP_F_NEW) { |
Jan Kara | e3fce68 | 2016-08-10 17:10:28 +0200 | [diff] [blame] | 976 | invalidate_inode_pages2_range(inode->i_mapping, |
| 977 | pos >> PAGE_SHIFT, |
| 978 | (end - 1) >> PAGE_SHIFT); |
| 979 | } |
| 980 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 981 | id = dax_read_lock(); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 982 | while (pos < end) { |
| 983 | unsigned offset = pos & (PAGE_SIZE - 1); |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 984 | const size_t size = ALIGN(length + offset, PAGE_SIZE); |
| 985 | const sector_t sector = dax_iomap_sector(iomap, pos); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 986 | ssize_t map_len; |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 987 | pgoff_t pgoff; |
| 988 | void *kaddr; |
| 989 | pfn_t pfn; |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 990 | |
Michal Hocko | d1908f5 | 2017-02-03 13:13:26 -0800 | [diff] [blame] | 991 | if (fatal_signal_pending(current)) { |
| 992 | ret = -EINTR; |
| 993 | break; |
| 994 | } |
| 995 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 996 | ret = bdev_dax_pgoff(bdev, sector, size, &pgoff); |
| 997 | if (ret) |
| 998 | break; |
| 999 | |
| 1000 | map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), |
| 1001 | &kaddr, &pfn); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1002 | if (map_len < 0) { |
| 1003 | ret = map_len; |
| 1004 | break; |
| 1005 | } |
| 1006 | |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 1007 | map_len = PFN_PHYS(map_len); |
| 1008 | kaddr += offset; |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1009 | map_len -= offset; |
| 1010 | if (map_len > end - pos) |
| 1011 | map_len = end - pos; |
| 1012 | |
Ross Zwisler | a2e050f | 2017-09-06 16:18:54 -0700 | [diff] [blame] | 1013 | /* |
| 1014 | * The userspace address for the memory copy has already been |
| 1015 | * validated via access_ok() in either vfs_read() or |
| 1016 | * vfs_write(), depending on which operation we are doing. |
| 1017 | */ |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1018 | if (iov_iter_rw(iter) == WRITE) |
Dan Williams | fec5377 | 2017-05-29 21:56:49 -0700 | [diff] [blame] | 1019 | map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr, |
| 1020 | map_len, iter); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1021 | else |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 1022 | map_len = copy_to_iter(kaddr, map_len, iter); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1023 | if (map_len <= 0) { |
| 1024 | ret = map_len ? map_len : -EFAULT; |
| 1025 | break; |
| 1026 | } |
| 1027 | |
| 1028 | pos += map_len; |
| 1029 | length -= map_len; |
| 1030 | done += map_len; |
| 1031 | } |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 1032 | dax_read_unlock(id); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1033 | |
| 1034 | return done ? done : ret; |
| 1035 | } |
| 1036 | |
| 1037 | /** |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 1038 | * dax_iomap_rw - Perform I/O to a DAX file |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1039 | * @iocb: The control block for this I/O |
| 1040 | * @iter: The addresses to do I/O from or to |
| 1041 | * @ops: iomap ops passed from the file system |
| 1042 | * |
| 1043 | * This function performs read and write operations to directly mapped |
| 1044 | * persistent memory. The callers needs to take care of read/write exclusion |
| 1045 | * and evicting any page cache pages in the region under I/O. |
| 1046 | */ |
| 1047 | ssize_t |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 1048 | dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, |
Christoph Hellwig | 8ff6daa | 2017-01-27 23:20:26 -0800 | [diff] [blame] | 1049 | const struct iomap_ops *ops) |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1050 | { |
| 1051 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
| 1052 | struct inode *inode = mapping->host; |
| 1053 | loff_t pos = iocb->ki_pos, ret = 0, done = 0; |
| 1054 | unsigned flags = 0; |
| 1055 | |
Christoph Hellwig | 168316d | 2017-02-08 14:43:13 -0500 | [diff] [blame] | 1056 | if (iov_iter_rw(iter) == WRITE) { |
| 1057 | lockdep_assert_held_exclusive(&inode->i_rwsem); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1058 | flags |= IOMAP_WRITE; |
Christoph Hellwig | 168316d | 2017-02-08 14:43:13 -0500 | [diff] [blame] | 1059 | } else { |
| 1060 | lockdep_assert_held(&inode->i_rwsem); |
| 1061 | } |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1062 | |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1063 | while (iov_iter_count(iter)) { |
| 1064 | ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops, |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 1065 | iter, dax_iomap_actor); |
Christoph Hellwig | a254e56 | 2016-09-19 11:24:49 +1000 | [diff] [blame] | 1066 | if (ret <= 0) |
| 1067 | break; |
| 1068 | pos += ret; |
| 1069 | done += ret; |
| 1070 | } |
| 1071 | |
| 1072 | iocb->ki_pos += done; |
| 1073 | return done ? done : ret; |
| 1074 | } |
Ross Zwisler | 11c59c9 | 2016-11-08 11:32:46 +1100 | [diff] [blame] | 1075 | EXPORT_SYMBOL_GPL(dax_iomap_rw); |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1076 | |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1077 | static int dax_fault_return(int error) |
| 1078 | { |
| 1079 | if (error == 0) |
| 1080 | return VM_FAULT_NOPAGE; |
| 1081 | if (error == -ENOMEM) |
| 1082 | return VM_FAULT_OOM; |
| 1083 | return VM_FAULT_SIGBUS; |
| 1084 | } |
| 1085 | |
Dan Williams | aaa422c | 2017-11-13 16:38:44 -0800 | [diff] [blame] | 1086 | /* |
| 1087 | * MAP_SYNC on a dax mapping guarantees dirty metadata is |
| 1088 | * flushed on write-faults (non-cow), but not read-faults. |
| 1089 | */ |
| 1090 | static bool dax_fault_is_synchronous(unsigned long flags, |
| 1091 | struct vm_area_struct *vma, struct iomap *iomap) |
| 1092 | { |
| 1093 | return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC) |
| 1094 | && (iomap->flags & IOMAP_F_DIRTY); |
| 1095 | } |
| 1096 | |
Jan Kara | 9a0dd42 | 2017-11-01 16:36:39 +0100 | [diff] [blame] | 1097 | static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, |
Jan Kara | c0b2462 | 2018-01-07 16:38:43 -0500 | [diff] [blame] | 1098 | int *iomap_errp, const struct iomap_ops *ops) |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1099 | { |
Jan Kara | a0987ad | 2017-11-01 16:36:34 +0100 | [diff] [blame] | 1100 | struct vm_area_struct *vma = vmf->vma; |
| 1101 | struct address_space *mapping = vma->vm_file->f_mapping; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1102 | struct inode *inode = mapping->host; |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 1103 | unsigned long vaddr = vmf->address; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1104 | loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1105 | struct iomap iomap = { 0 }; |
Jan Kara | 9484ab1 | 2016-11-10 10:26:50 +1100 | [diff] [blame] | 1106 | unsigned flags = IOMAP_FAULT; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1107 | int error, major = 0; |
Jan Kara | d2c43ef | 2017-11-01 16:36:35 +0100 | [diff] [blame] | 1108 | bool write = vmf->flags & FAULT_FLAG_WRITE; |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1109 | bool sync; |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 1110 | int vmf_ret = 0; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1111 | void *entry; |
Jan Kara | 1b5a1cb | 2017-11-01 16:36:36 +0100 | [diff] [blame] | 1112 | pfn_t pfn; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1113 | |
Ross Zwisler | a9c42b3 | 2017-05-08 16:00:00 -0700 | [diff] [blame] | 1114 | trace_dax_pte_fault(inode, vmf, vmf_ret); |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1115 | /* |
| 1116 | * Check whether offset isn't beyond end of file now. Caller is supposed |
| 1117 | * to hold locks serializing us with truncate / punch hole so this is |
| 1118 | * a reliable test. |
| 1119 | */ |
Ross Zwisler | a9c42b3 | 2017-05-08 16:00:00 -0700 | [diff] [blame] | 1120 | if (pos >= i_size_read(inode)) { |
| 1121 | vmf_ret = VM_FAULT_SIGBUS; |
| 1122 | goto out; |
| 1123 | } |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1124 | |
Jan Kara | d2c43ef | 2017-11-01 16:36:35 +0100 | [diff] [blame] | 1125 | if (write && !vmf->cow_page) |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1126 | flags |= IOMAP_WRITE; |
| 1127 | |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1128 | entry = grab_mapping_entry(mapping, vmf->pgoff, 0); |
| 1129 | if (IS_ERR(entry)) { |
| 1130 | vmf_ret = dax_fault_return(PTR_ERR(entry)); |
| 1131 | goto out; |
| 1132 | } |
| 1133 | |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1134 | /* |
Ross Zwisler | e209392 | 2017-06-02 14:46:37 -0700 | [diff] [blame] | 1135 | * It is possible, particularly with mixed reads & writes to private |
| 1136 | * mappings, that we have raced with a PMD fault that overlaps with |
| 1137 | * the PTE we need to set up. If so just return and the fault will be |
| 1138 | * retried. |
| 1139 | */ |
| 1140 | if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) { |
| 1141 | vmf_ret = VM_FAULT_NOPAGE; |
| 1142 | goto unlock_entry; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1146 | * Note that we don't bother to use iomap_apply here: DAX required |
| 1147 | * the file system block size to be equal the page size, which means |
| 1148 | * that we never have to deal with more than a single extent here. |
| 1149 | */ |
| 1150 | error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap); |
Jan Kara | c0b2462 | 2018-01-07 16:38:43 -0500 | [diff] [blame] | 1151 | if (iomap_errp) |
| 1152 | *iomap_errp = error; |
Ross Zwisler | a9c42b3 | 2017-05-08 16:00:00 -0700 | [diff] [blame] | 1153 | if (error) { |
| 1154 | vmf_ret = dax_fault_return(error); |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1155 | goto unlock_entry; |
Ross Zwisler | a9c42b3 | 2017-05-08 16:00:00 -0700 | [diff] [blame] | 1156 | } |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1157 | if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) { |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1158 | error = -EIO; /* fs corruption? */ |
| 1159 | goto error_finish_iomap; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1160 | } |
| 1161 | |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1162 | if (vmf->cow_page) { |
Jan Kara | 31a6f1a | 2017-11-01 16:36:32 +0100 | [diff] [blame] | 1163 | sector_t sector = dax_iomap_sector(&iomap, pos); |
| 1164 | |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1165 | switch (iomap.type) { |
| 1166 | case IOMAP_HOLE: |
| 1167 | case IOMAP_UNWRITTEN: |
| 1168 | clear_user_highpage(vmf->cow_page, vaddr); |
| 1169 | break; |
| 1170 | case IOMAP_MAPPED: |
Dan Williams | cccbce6 | 2017-01-27 13:31:42 -0800 | [diff] [blame] | 1171 | error = copy_user_dax(iomap.bdev, iomap.dax_dev, |
| 1172 | sector, PAGE_SIZE, vmf->cow_page, vaddr); |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1173 | break; |
| 1174 | default: |
| 1175 | WARN_ON_ONCE(1); |
| 1176 | error = -EIO; |
| 1177 | break; |
| 1178 | } |
| 1179 | |
| 1180 | if (error) |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1181 | goto error_finish_iomap; |
Jan Kara | b1aa812 | 2016-12-14 15:07:24 -0800 | [diff] [blame] | 1182 | |
| 1183 | __SetPageUptodate(vmf->cow_page); |
| 1184 | vmf_ret = finish_fault(vmf); |
| 1185 | if (!vmf_ret) |
| 1186 | vmf_ret = VM_FAULT_DONE_COW; |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1187 | goto finish_iomap; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1188 | } |
| 1189 | |
Dan Williams | aaa422c | 2017-11-13 16:38:44 -0800 | [diff] [blame] | 1190 | sync = dax_fault_is_synchronous(flags, vma, &iomap); |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1191 | |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1192 | switch (iomap.type) { |
| 1193 | case IOMAP_MAPPED: |
| 1194 | if (iomap.flags & IOMAP_F_NEW) { |
| 1195 | count_vm_event(PGMAJFAULT); |
Jan Kara | a0987ad | 2017-11-01 16:36:34 +0100 | [diff] [blame] | 1196 | count_memcg_event_mm(vma->vm_mm, PGMAJFAULT); |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1197 | major = VM_FAULT_MAJOR; |
| 1198 | } |
Jan Kara | 1b5a1cb | 2017-11-01 16:36:36 +0100 | [diff] [blame] | 1199 | error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn); |
| 1200 | if (error < 0) |
| 1201 | goto error_finish_iomap; |
| 1202 | |
| 1203 | entry = dax_insert_mapping_entry(mapping, vmf, entry, |
| 1204 | dax_iomap_sector(&iomap, pos), |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1205 | 0, write && !sync); |
Jan Kara | 1b5a1cb | 2017-11-01 16:36:36 +0100 | [diff] [blame] | 1206 | if (IS_ERR(entry)) { |
| 1207 | error = PTR_ERR(entry); |
| 1208 | goto error_finish_iomap; |
| 1209 | } |
| 1210 | |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1211 | /* |
| 1212 | * If we are doing synchronous page fault and inode needs fsync, |
| 1213 | * we can insert PTE into page tables only after that happens. |
| 1214 | * Skip insertion for now and return the pfn so that caller can |
| 1215 | * insert it after fsync is done. |
| 1216 | */ |
| 1217 | if (sync) { |
| 1218 | if (WARN_ON_ONCE(!pfnp)) { |
| 1219 | error = -EIO; |
| 1220 | goto error_finish_iomap; |
| 1221 | } |
| 1222 | *pfnp = pfn; |
| 1223 | vmf_ret = VM_FAULT_NEEDDSYNC | major; |
| 1224 | goto finish_iomap; |
| 1225 | } |
Jan Kara | 1b5a1cb | 2017-11-01 16:36:36 +0100 | [diff] [blame] | 1226 | trace_dax_insert_mapping(inode, vmf, entry); |
| 1227 | if (write) |
| 1228 | error = vm_insert_mixed_mkwrite(vma, vaddr, pfn); |
| 1229 | else |
| 1230 | error = vm_insert_mixed(vma, vaddr, pfn); |
| 1231 | |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1232 | /* -EBUSY is fine, somebody else faulted on the same PTE */ |
| 1233 | if (error == -EBUSY) |
| 1234 | error = 0; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1235 | break; |
| 1236 | case IOMAP_UNWRITTEN: |
| 1237 | case IOMAP_HOLE: |
Jan Kara | d2c43ef | 2017-11-01 16:36:35 +0100 | [diff] [blame] | 1238 | if (!write) { |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1239 | vmf_ret = dax_load_hole(mapping, entry, vmf); |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1240 | goto finish_iomap; |
Ross Zwisler | 1550290 | 2016-11-08 11:33:26 +1100 | [diff] [blame] | 1241 | } |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1242 | /*FALLTHRU*/ |
| 1243 | default: |
| 1244 | WARN_ON_ONCE(1); |
| 1245 | error = -EIO; |
| 1246 | break; |
| 1247 | } |
| 1248 | |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1249 | error_finish_iomap: |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1250 | vmf_ret = dax_fault_return(error) | major; |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1251 | finish_iomap: |
| 1252 | if (ops->iomap_end) { |
| 1253 | int copied = PAGE_SIZE; |
| 1254 | |
| 1255 | if (vmf_ret & VM_FAULT_ERROR) |
| 1256 | copied = 0; |
| 1257 | /* |
| 1258 | * The fault is done by now and there's no way back (other |
| 1259 | * thread may be already happily using PTE we have installed). |
| 1260 | * Just ignore error from ->iomap_end since we cannot do much |
| 1261 | * with it. |
| 1262 | */ |
| 1263 | ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap); |
Ross Zwisler | 1550290 | 2016-11-08 11:33:26 +1100 | [diff] [blame] | 1264 | } |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1265 | unlock_entry: |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1266 | put_locked_mapping_entry(mapping, vmf->pgoff); |
Jan Kara | 13e451f | 2017-05-12 15:46:57 -0700 | [diff] [blame] | 1267 | out: |
Ross Zwisler | a9c42b3 | 2017-05-08 16:00:00 -0700 | [diff] [blame] | 1268 | trace_dax_pte_fault_done(inode, vmf, vmf_ret); |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1269 | return vmf_ret; |
Christoph Hellwig | a7d73fe | 2016-09-19 11:24:50 +1000 | [diff] [blame] | 1270 | } |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1271 | |
| 1272 | #ifdef CONFIG_FS_DAX_PMD |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1273 | static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap, |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1274 | void *entry) |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1275 | { |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1276 | struct address_space *mapping = vmf->vma->vm_file->f_mapping; |
| 1277 | unsigned long pmd_addr = vmf->address & PMD_MASK; |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1278 | struct inode *inode = mapping->host; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1279 | struct page *zero_page; |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1280 | void *ret = NULL; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1281 | spinlock_t *ptl; |
| 1282 | pmd_t pmd_entry; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1283 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1284 | zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1285 | |
| 1286 | if (unlikely(!zero_page)) |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1287 | goto fallback; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1288 | |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1289 | ret = dax_insert_mapping_entry(mapping, vmf, entry, 0, |
Jan Kara | f5b7b74 | 2017-11-01 16:36:40 +0100 | [diff] [blame] | 1290 | RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1291 | if (IS_ERR(ret)) |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1292 | goto fallback; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1293 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1294 | ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd); |
| 1295 | if (!pmd_none(*(vmf->pmd))) { |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1296 | spin_unlock(ptl); |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1297 | goto fallback; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1298 | } |
| 1299 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1300 | pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1301 | pmd_entry = pmd_mkhuge(pmd_entry); |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1302 | set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1303 | spin_unlock(ptl); |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1304 | trace_dax_pmd_load_hole(inode, vmf, zero_page, ret); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1305 | return VM_FAULT_NOPAGE; |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1306 | |
| 1307 | fallback: |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1308 | trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret); |
Ross Zwisler | 653b2ea | 2017-02-22 15:39:57 -0800 | [diff] [blame] | 1309 | return VM_FAULT_FALLBACK; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1310 | } |
| 1311 | |
Jan Kara | 9a0dd42 | 2017-11-01 16:36:39 +0100 | [diff] [blame] | 1312 | static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1313 | const struct iomap_ops *ops) |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1314 | { |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1315 | struct vm_area_struct *vma = vmf->vma; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1316 | struct address_space *mapping = vma->vm_file->f_mapping; |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 1317 | unsigned long pmd_addr = vmf->address & PMD_MASK; |
| 1318 | bool write = vmf->flags & FAULT_FLAG_WRITE; |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1319 | bool sync; |
Jan Kara | 9484ab1 | 2016-11-10 10:26:50 +1100 | [diff] [blame] | 1320 | unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1321 | struct inode *inode = mapping->host; |
| 1322 | int result = VM_FAULT_FALLBACK; |
| 1323 | struct iomap iomap = { 0 }; |
| 1324 | pgoff_t max_pgoff, pgoff; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1325 | void *entry; |
| 1326 | loff_t pos; |
| 1327 | int error; |
Jan Kara | 302a5e3 | 2017-11-01 16:36:37 +0100 | [diff] [blame] | 1328 | pfn_t pfn; |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1329 | |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 1330 | /* |
| 1331 | * Check whether offset isn't beyond end of file now. Caller is |
| 1332 | * supposed to hold locks serializing us with truncate / punch hole so |
| 1333 | * this is a reliable test. |
| 1334 | */ |
| 1335 | pgoff = linear_page_index(vma, pmd_addr); |
Jeff Moyer | 957ac8c | 2017-11-14 20:37:27 -0500 | [diff] [blame] | 1336 | max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE); |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 1337 | |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1338 | trace_dax_pmd_fault(inode, vmf, max_pgoff, 0); |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 1339 | |
Ross Zwisler | fffa281 | 2017-08-25 15:55:36 -0700 | [diff] [blame] | 1340 | /* |
| 1341 | * Make sure that the faulting address's PMD offset (color) matches |
| 1342 | * the PMD offset from the start of the file. This is necessary so |
| 1343 | * that a PMD range in the page table overlaps exactly with a PMD |
| 1344 | * range in the radix tree. |
| 1345 | */ |
| 1346 | if ((vmf->pgoff & PG_PMD_COLOUR) != |
| 1347 | ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR)) |
| 1348 | goto fallback; |
| 1349 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1350 | /* Fall back to PTEs if we're going to COW */ |
| 1351 | if (write && !(vma->vm_flags & VM_SHARED)) |
| 1352 | goto fallback; |
| 1353 | |
| 1354 | /* If the PMD would extend outside the VMA */ |
| 1355 | if (pmd_addr < vma->vm_start) |
| 1356 | goto fallback; |
| 1357 | if ((pmd_addr + PMD_SIZE) > vma->vm_end) |
| 1358 | goto fallback; |
| 1359 | |
Jeff Moyer | 957ac8c | 2017-11-14 20:37:27 -0500 | [diff] [blame] | 1360 | if (pgoff >= max_pgoff) { |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 1361 | result = VM_FAULT_SIGBUS; |
| 1362 | goto out; |
| 1363 | } |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1364 | |
| 1365 | /* If the PMD would extend beyond the file size */ |
Jeff Moyer | 957ac8c | 2017-11-14 20:37:27 -0500 | [diff] [blame] | 1366 | if ((pgoff | PG_PMD_COLOUR) >= max_pgoff) |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1367 | goto fallback; |
| 1368 | |
| 1369 | /* |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1370 | * grab_mapping_entry() will make sure we get a 2MiB empty entry, a |
| 1371 | * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page |
| 1372 | * is already in the tree, for instance), it will return -EEXIST and |
| 1373 | * we just fall back to 4k entries. |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1374 | */ |
| 1375 | entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD); |
| 1376 | if (IS_ERR(entry)) |
Ross Zwisler | 876f294 | 2017-05-12 15:47:00 -0700 | [diff] [blame] | 1377 | goto fallback; |
| 1378 | |
| 1379 | /* |
Ross Zwisler | e209392 | 2017-06-02 14:46:37 -0700 | [diff] [blame] | 1380 | * It is possible, particularly with mixed reads & writes to private |
| 1381 | * mappings, that we have raced with a PTE fault that overlaps with |
| 1382 | * the PMD we need to set up. If so just return and the fault will be |
| 1383 | * retried. |
| 1384 | */ |
| 1385 | if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) && |
| 1386 | !pmd_devmap(*vmf->pmd)) { |
| 1387 | result = 0; |
| 1388 | goto unlock_entry; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
Ross Zwisler | 876f294 | 2017-05-12 15:47:00 -0700 | [diff] [blame] | 1392 | * Note that we don't use iomap_apply here. We aren't doing I/O, only |
| 1393 | * setting up a mapping, so really we're using iomap_begin() as a way |
| 1394 | * to look up our filesystem block. |
| 1395 | */ |
| 1396 | pos = (loff_t)pgoff << PAGE_SHIFT; |
| 1397 | error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap); |
| 1398 | if (error) |
| 1399 | goto unlock_entry; |
| 1400 | |
| 1401 | if (iomap.offset + iomap.length < pos + PMD_SIZE) |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1402 | goto finish_iomap; |
| 1403 | |
Dan Williams | aaa422c | 2017-11-13 16:38:44 -0800 | [diff] [blame] | 1404 | sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap); |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1405 | |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1406 | switch (iomap.type) { |
| 1407 | case IOMAP_MAPPED: |
Jan Kara | 302a5e3 | 2017-11-01 16:36:37 +0100 | [diff] [blame] | 1408 | error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn); |
| 1409 | if (error < 0) |
| 1410 | goto finish_iomap; |
| 1411 | |
| 1412 | entry = dax_insert_mapping_entry(mapping, vmf, entry, |
| 1413 | dax_iomap_sector(&iomap, pos), |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1414 | RADIX_DAX_PMD, write && !sync); |
Jan Kara | 302a5e3 | 2017-11-01 16:36:37 +0100 | [diff] [blame] | 1415 | if (IS_ERR(entry)) |
| 1416 | goto finish_iomap; |
| 1417 | |
Jan Kara | caa51d2 | 2017-11-01 16:36:42 +0100 | [diff] [blame] | 1418 | /* |
| 1419 | * If we are doing synchronous page fault and inode needs fsync, |
| 1420 | * we can insert PMD into page tables only after that happens. |
| 1421 | * Skip insertion for now and return the pfn so that caller can |
| 1422 | * insert it after fsync is done. |
| 1423 | */ |
| 1424 | if (sync) { |
| 1425 | if (WARN_ON_ONCE(!pfnp)) |
| 1426 | goto finish_iomap; |
| 1427 | *pfnp = pfn; |
| 1428 | result = VM_FAULT_NEEDDSYNC; |
| 1429 | goto finish_iomap; |
| 1430 | } |
| 1431 | |
Jan Kara | 302a5e3 | 2017-11-01 16:36:37 +0100 | [diff] [blame] | 1432 | trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry); |
| 1433 | result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn, |
| 1434 | write); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1435 | break; |
| 1436 | case IOMAP_UNWRITTEN: |
| 1437 | case IOMAP_HOLE: |
| 1438 | if (WARN_ON_ONCE(write)) |
Ross Zwisler | 876f294 | 2017-05-12 15:47:00 -0700 | [diff] [blame] | 1439 | break; |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1440 | result = dax_pmd_load_hole(vmf, &iomap, entry); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1441 | break; |
| 1442 | default: |
| 1443 | WARN_ON_ONCE(1); |
| 1444 | break; |
| 1445 | } |
| 1446 | |
Jan Kara | 9f141d6 | 2016-10-19 14:34:31 +0200 | [diff] [blame] | 1447 | finish_iomap: |
| 1448 | if (ops->iomap_end) { |
| 1449 | int copied = PMD_SIZE; |
| 1450 | |
| 1451 | if (result == VM_FAULT_FALLBACK) |
| 1452 | copied = 0; |
| 1453 | /* |
| 1454 | * The fault is done by now and there's no way back (other |
| 1455 | * thread may be already happily using PMD we have installed). |
| 1456 | * Just ignore error from ->iomap_end since we cannot do much |
| 1457 | * with it. |
| 1458 | */ |
| 1459 | ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags, |
| 1460 | &iomap); |
| 1461 | } |
Ross Zwisler | 876f294 | 2017-05-12 15:47:00 -0700 | [diff] [blame] | 1462 | unlock_entry: |
Ross Zwisler | 91d25ba | 2017-09-06 16:18:43 -0700 | [diff] [blame] | 1463 | put_locked_mapping_entry(mapping, pgoff); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1464 | fallback: |
| 1465 | if (result == VM_FAULT_FALLBACK) { |
Dave Jiang | d8a849e | 2017-02-22 15:40:03 -0800 | [diff] [blame] | 1466 | split_huge_pmd(vma, vmf->pmd, vmf->address); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1467 | count_vm_event(THP_FAULT_FALLBACK); |
| 1468 | } |
Ross Zwisler | 282a8e0 | 2017-02-22 15:39:50 -0800 | [diff] [blame] | 1469 | out: |
Dave Jiang | f420039 | 2017-02-22 15:40:06 -0800 | [diff] [blame] | 1470 | trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result); |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1471 | return result; |
| 1472 | } |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1473 | #else |
Jan Kara | 9a0dd42 | 2017-11-01 16:36:39 +0100 | [diff] [blame] | 1474 | static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, |
Arnd Bergmann | 01cddfe | 2017-02-27 14:26:44 -0800 | [diff] [blame] | 1475 | const struct iomap_ops *ops) |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1476 | { |
| 1477 | return VM_FAULT_FALLBACK; |
| 1478 | } |
Ross Zwisler | 642261a | 2016-11-08 11:34:45 +1100 | [diff] [blame] | 1479 | #endif /* CONFIG_FS_DAX_PMD */ |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1480 | |
| 1481 | /** |
| 1482 | * dax_iomap_fault - handle a page fault on a DAX file |
| 1483 | * @vmf: The description of the fault |
Jan Kara | cec04e8 | 2017-11-01 16:36:38 +0100 | [diff] [blame] | 1484 | * @pe_size: Size of the page to fault in |
Jan Kara | 9a0dd42 | 2017-11-01 16:36:39 +0100 | [diff] [blame] | 1485 | * @pfnp: PFN to insert for synchronous faults if fsync is required |
Jan Kara | c0b2462 | 2018-01-07 16:38:43 -0500 | [diff] [blame] | 1486 | * @iomap_errp: Storage for detailed error code in case of error |
Jan Kara | cec04e8 | 2017-11-01 16:36:38 +0100 | [diff] [blame] | 1487 | * @ops: Iomap ops passed from the file system |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1488 | * |
| 1489 | * When a page fault occurs, filesystems may call this helper in |
| 1490 | * their fault handler for DAX files. dax_iomap_fault() assumes the caller |
| 1491 | * has done all the necessary locking for page fault to proceed |
| 1492 | * successfully. |
| 1493 | */ |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 1494 | int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, |
Jan Kara | c0b2462 | 2018-01-07 16:38:43 -0500 | [diff] [blame] | 1495 | pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops) |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1496 | { |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 1497 | switch (pe_size) { |
| 1498 | case PE_SIZE_PTE: |
Jan Kara | c0b2462 | 2018-01-07 16:38:43 -0500 | [diff] [blame] | 1499 | return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops); |
Dave Jiang | c791ace | 2017-02-24 14:57:08 -0800 | [diff] [blame] | 1500 | case PE_SIZE_PMD: |
Jan Kara | 9a0dd42 | 2017-11-01 16:36:39 +0100 | [diff] [blame] | 1501 | return dax_iomap_pmd_fault(vmf, pfnp, ops); |
Dave Jiang | a2d5816 | 2017-02-24 14:56:59 -0800 | [diff] [blame] | 1502 | default: |
| 1503 | return VM_FAULT_FALLBACK; |
| 1504 | } |
| 1505 | } |
| 1506 | EXPORT_SYMBOL_GPL(dax_iomap_fault); |
Jan Kara | 71eab6d | 2017-11-01 16:36:43 +0100 | [diff] [blame] | 1507 | |
| 1508 | /** |
| 1509 | * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables |
| 1510 | * @vmf: The description of the fault |
| 1511 | * @pe_size: Size of entry to be inserted |
| 1512 | * @pfn: PFN to insert |
| 1513 | * |
| 1514 | * This function inserts writeable PTE or PMD entry into page tables for mmaped |
| 1515 | * DAX file. It takes care of marking corresponding radix tree entry as dirty |
| 1516 | * as well. |
| 1517 | */ |
| 1518 | static int dax_insert_pfn_mkwrite(struct vm_fault *vmf, |
| 1519 | enum page_entry_size pe_size, |
| 1520 | pfn_t pfn) |
| 1521 | { |
| 1522 | struct address_space *mapping = vmf->vma->vm_file->f_mapping; |
| 1523 | void *entry, **slot; |
| 1524 | pgoff_t index = vmf->pgoff; |
| 1525 | int vmf_ret, error; |
| 1526 | |
| 1527 | spin_lock_irq(&mapping->tree_lock); |
| 1528 | entry = get_unlocked_mapping_entry(mapping, index, &slot); |
| 1529 | /* Did we race with someone splitting entry or so? */ |
| 1530 | if (!entry || |
| 1531 | (pe_size == PE_SIZE_PTE && !dax_is_pte_entry(entry)) || |
| 1532 | (pe_size == PE_SIZE_PMD && !dax_is_pmd_entry(entry))) { |
| 1533 | put_unlocked_mapping_entry(mapping, index, entry); |
| 1534 | spin_unlock_irq(&mapping->tree_lock); |
| 1535 | trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf, |
| 1536 | VM_FAULT_NOPAGE); |
| 1537 | return VM_FAULT_NOPAGE; |
| 1538 | } |
| 1539 | radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY); |
| 1540 | entry = lock_slot(mapping, slot); |
| 1541 | spin_unlock_irq(&mapping->tree_lock); |
| 1542 | switch (pe_size) { |
| 1543 | case PE_SIZE_PTE: |
| 1544 | error = vm_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn); |
| 1545 | vmf_ret = dax_fault_return(error); |
| 1546 | break; |
| 1547 | #ifdef CONFIG_FS_DAX_PMD |
| 1548 | case PE_SIZE_PMD: |
| 1549 | vmf_ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd, |
| 1550 | pfn, true); |
| 1551 | break; |
| 1552 | #endif |
| 1553 | default: |
| 1554 | vmf_ret = VM_FAULT_FALLBACK; |
| 1555 | } |
| 1556 | put_locked_mapping_entry(mapping, index); |
| 1557 | trace_dax_insert_pfn_mkwrite(mapping->host, vmf, vmf_ret); |
| 1558 | return vmf_ret; |
| 1559 | } |
| 1560 | |
| 1561 | /** |
| 1562 | * dax_finish_sync_fault - finish synchronous page fault |
| 1563 | * @vmf: The description of the fault |
| 1564 | * @pe_size: Size of entry to be inserted |
| 1565 | * @pfn: PFN to insert |
| 1566 | * |
| 1567 | * This function ensures that the file range touched by the page fault is |
| 1568 | * stored persistently on the media and handles inserting of appropriate page |
| 1569 | * table entry. |
| 1570 | */ |
| 1571 | int dax_finish_sync_fault(struct vm_fault *vmf, enum page_entry_size pe_size, |
| 1572 | pfn_t pfn) |
| 1573 | { |
| 1574 | int err; |
| 1575 | loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT; |
| 1576 | size_t len = 0; |
| 1577 | |
| 1578 | if (pe_size == PE_SIZE_PTE) |
| 1579 | len = PAGE_SIZE; |
| 1580 | else if (pe_size == PE_SIZE_PMD) |
| 1581 | len = PMD_SIZE; |
| 1582 | else |
| 1583 | WARN_ON_ONCE(1); |
| 1584 | err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1); |
| 1585 | if (err) |
| 1586 | return VM_FAULT_SIGBUS; |
| 1587 | return dax_insert_pfn_mkwrite(vmf, pe_size, pfn); |
| 1588 | } |
| 1589 | EXPORT_SYMBOL_GPL(dax_finish_sync_fault); |