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