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