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