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