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