blob: 5b2eac3ef077db86c8e916abae92e14d084eefd1 [file] [log] [blame]
Matthew Wilcoxd475c632015-02-16 15:58:56 -08001/*
2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 */
16
17#include <linux/atomic.h>
18#include <linux/blkdev.h>
19#include <linux/buffer_head.h>
Ross Zwislerd77e92e2015-09-09 10:29:40 -060020#include <linux/dax.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080021#include <linux/fs.h>
22#include <linux/genhd.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080023#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080026#include <linux/mutex.h>
Ross Zwisler9973c982016-01-22 15:10:47 -080027#include <linux/pagevec.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080028#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010029#include <linux/sched/signal.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080030#include <linux/uio.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080031#include <linux/vmstat.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080032#include <linux/pfn_t.h>
Dan Williams0e749e52016-01-15 16:55:53 -080033#include <linux/sizes.h>
Jan Kara4b4bb462016-12-14 15:07:53 -080034#include <linux/mmu_notifier.h>
Christoph Hellwiga254e562016-09-19 11:24:49 +100035#include <linux/iomap.h>
36#include "internal.h"
Matthew Wilcoxd475c632015-02-16 15:58:56 -080037
Ross Zwisler282a8e02017-02-22 15:39:50 -080038#define CREATE_TRACE_POINTS
39#include <trace/events/fs_dax.h>
40
Jan Karaac401cc2016-05-12 18:29:18 +020041/* We choose 4096 entries - same as per-zone page wait tables */
42#define DAX_WAIT_TABLE_BITS 12
43#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
44
Ross Zwislerce95ab0f2016-11-08 11:31:44 +110045static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020046
47static int __init init_dax_wait_table(void)
48{
49 int i;
50
51 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
52 init_waitqueue_head(wait_table + i);
53 return 0;
54}
55fs_initcall(init_dax_wait_table);
56
Ross Zwisler527b19d2017-09-06 16:18:51 -070057/*
58 * We use lowest available bit in exceptional entry for locking, one bit for
59 * the entry size (PMD) and two more to tell us if the entry is a zero page or
60 * an empty entry that is just used for locking. In total four special bits.
61 *
62 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
63 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
64 * block allocation.
65 */
66#define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 4)
67#define RADIX_DAX_ENTRY_LOCK (1 << RADIX_TREE_EXCEPTIONAL_SHIFT)
68#define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
69#define RADIX_DAX_ZERO_PAGE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
70#define RADIX_DAX_EMPTY (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 3))
71
72static unsigned long dax_radix_sector(void *entry)
73{
74 return (unsigned long)entry >> RADIX_DAX_SHIFT;
75}
76
77static void *dax_radix_locked_entry(sector_t sector, unsigned long flags)
78{
79 return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags |
80 ((unsigned long)sector << RADIX_DAX_SHIFT) |
81 RADIX_DAX_ENTRY_LOCK);
82}
83
84static unsigned int dax_radix_order(void *entry)
85{
86 if ((unsigned long)entry & RADIX_DAX_PMD)
87 return PMD_SHIFT - PAGE_SHIFT;
88 return 0;
89}
90
Ross Zwisler642261a2016-11-08 11:34:45 +110091static int dax_is_pmd_entry(void *entry)
92{
93 return (unsigned long)entry & RADIX_DAX_PMD;
94}
95
96static int dax_is_pte_entry(void *entry)
97{
98 return !((unsigned long)entry & RADIX_DAX_PMD);
99}
100
101static int dax_is_zero_entry(void *entry)
102{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700103 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100104}
105
106static int dax_is_empty_entry(void *entry)
107{
108 return (unsigned long)entry & RADIX_DAX_EMPTY;
109}
110
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800111/*
Jan Karaac401cc2016-05-12 18:29:18 +0200112 * DAX radix tree locking
113 */
114struct exceptional_entry_key {
115 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100116 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200117};
118
119struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200120 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200121 struct exceptional_entry_key key;
122};
123
Ross Zwisler63e95b52016-11-08 11:32:20 +1100124static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
125 pgoff_t index, void *entry, struct exceptional_entry_key *key)
126{
127 unsigned long hash;
128
129 /*
130 * If 'entry' is a PMD, align the 'index' that we use for the wait
131 * queue to the start of that PMD. This ensures that all offsets in
132 * the range covered by the PMD map to the same bit lock.
133 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100134 if (dax_is_pmd_entry(entry))
Ross Zwisler63e95b52016-11-08 11:32:20 +1100135 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
136
137 key->mapping = mapping;
138 key->entry_start = index;
139
140 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
141 return wait_table + hash;
142}
143
Ingo Molnarac6424b2017-06-20 12:06:13 +0200144static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
Jan Karaac401cc2016-05-12 18:29:18 +0200145 int sync, void *keyp)
146{
147 struct exceptional_entry_key *key = keyp;
148 struct wait_exceptional_entry_queue *ewait =
149 container_of(wait, struct wait_exceptional_entry_queue, wait);
150
151 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100152 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200153 return 0;
154 return autoremove_wake_function(wait, mode, sync, NULL);
155}
156
157/*
Ross Zwislere30331f2017-09-06 16:18:39 -0700158 * We do not necessarily hold the mapping->tree_lock when we call this
159 * function so it is possible that 'entry' is no longer a valid item in the
160 * radix tree. This is okay because all we really need to do is to find the
161 * correct waitqueue where tasks might be waiting for that old 'entry' and
162 * wake them.
163 */
Ross Zwislerd01ad192017-09-06 16:18:47 -0700164static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwislere30331f2017-09-06 16:18:39 -0700165 pgoff_t index, void *entry, bool wake_all)
166{
167 struct exceptional_entry_key key;
168 wait_queue_head_t *wq;
169
170 wq = dax_entry_waitqueue(mapping, index, entry, &key);
171
172 /*
173 * Checking for locked entry and prepare_to_wait_exclusive() happens
174 * under mapping->tree_lock, ditto for entry handling in our callers.
175 * So at this point all tasks that could have seen our entry locked
176 * must be in the waitqueue and the following check will see them.
177 */
178 if (waitqueue_active(wq))
179 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
180}
181
182/*
Jan Karaac401cc2016-05-12 18:29:18 +0200183 * Check whether the given slot is locked. The function must be called with
184 * mapping->tree_lock held
185 */
186static inline int slot_locked(struct address_space *mapping, void **slot)
187{
188 unsigned long entry = (unsigned long)
189 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
190 return entry & RADIX_DAX_ENTRY_LOCK;
191}
192
193/*
194 * Mark the given slot is locked. The function must be called with
195 * mapping->tree_lock held
196 */
197static inline void *lock_slot(struct address_space *mapping, void **slot)
198{
199 unsigned long entry = (unsigned long)
200 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
201
202 entry |= RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800203 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200204 return (void *)entry;
205}
206
207/*
208 * Mark the given slot is unlocked. The function must be called with
209 * mapping->tree_lock held
210 */
211static inline void *unlock_slot(struct address_space *mapping, void **slot)
212{
213 unsigned long entry = (unsigned long)
214 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
215
216 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800217 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200218 return (void *)entry;
219}
220
221/*
222 * Lookup entry in radix tree, wait for it to become unlocked if it is
223 * exceptional entry and return it. The caller must call
224 * put_unlocked_mapping_entry() when he decided not to lock the entry or
225 * put_locked_mapping_entry() when he locked the entry and now wants to
226 * unlock it.
227 *
228 * The function must be called with mapping->tree_lock held.
229 */
230static void *get_unlocked_mapping_entry(struct address_space *mapping,
231 pgoff_t index, void ***slotp)
232{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100233 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200234 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100235 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200236
237 init_wait(&ewait.wait);
238 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200239
240 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100241 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200242 &slot);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700243 if (!entry ||
244 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200245 !slot_locked(mapping, slot)) {
246 if (slotp)
247 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100248 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200249 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100250
251 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200252 prepare_to_wait_exclusive(wq, &ewait.wait,
253 TASK_UNINTERRUPTIBLE);
254 spin_unlock_irq(&mapping->tree_lock);
255 schedule();
256 finish_wait(wq, &ewait.wait);
257 spin_lock_irq(&mapping->tree_lock);
258 }
259}
260
Jan Karab1aa8122016-12-14 15:07:24 -0800261static void dax_unlock_mapping_entry(struct address_space *mapping,
262 pgoff_t index)
263{
264 void *entry, **slot;
265
266 spin_lock_irq(&mapping->tree_lock);
267 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
268 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
269 !slot_locked(mapping, slot))) {
270 spin_unlock_irq(&mapping->tree_lock);
271 return;
272 }
273 unlock_slot(mapping, slot);
274 spin_unlock_irq(&mapping->tree_lock);
275 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
276}
277
Jan Karaac401cc2016-05-12 18:29:18 +0200278static void put_locked_mapping_entry(struct address_space *mapping,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700279 pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200280{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700281 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200282}
283
284/*
285 * Called when we are done with radix tree entry we looked up via
286 * get_unlocked_mapping_entry() and which we didn't lock in the end.
287 */
288static void put_unlocked_mapping_entry(struct address_space *mapping,
289 pgoff_t index, void *entry)
290{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700291 if (!entry)
Jan Karaac401cc2016-05-12 18:29:18 +0200292 return;
293
294 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100295 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
296}
297
Jan Karaac401cc2016-05-12 18:29:18 +0200298/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700299 * Find radix tree entry at given index. If it points to an exceptional entry,
300 * return it with the radix tree entry locked. If the radix tree doesn't
301 * contain given index, create an empty exceptional entry for the index and
302 * return with it locked.
Jan Karaac401cc2016-05-12 18:29:18 +0200303 *
Ross Zwisler642261a2016-11-08 11:34:45 +1100304 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
305 * either return that locked entry or will return an error. This error will
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700306 * happen if there are any 4k entries within the 2MiB range that we are
307 * requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100308 *
309 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
310 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
311 * insertion will fail if it finds any 4k entries already in the tree, and a
312 * 4k insertion will cause an existing 2MiB entry to be unmapped and
313 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
314 * well as 2MiB empty entries.
315 *
316 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
317 * real storage backing them. We will leave these real 2MiB DAX entries in
318 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
319 *
Jan Karaac401cc2016-05-12 18:29:18 +0200320 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
321 * persistent memory the benefit is doubtful. We can add that later if we can
322 * show it helps.
323 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100324static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
325 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200326{
Ross Zwisler642261a2016-11-08 11:34:45 +1100327 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100328 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200329
330restart:
331 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100332 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100333
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700334 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
335 entry = ERR_PTR(-EIO);
336 goto out_unlock;
337 }
338
Ross Zwisler642261a2016-11-08 11:34:45 +1100339 if (entry) {
340 if (size_flag & RADIX_DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700341 if (dax_is_pte_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100342 put_unlocked_mapping_entry(mapping, index,
343 entry);
344 entry = ERR_PTR(-EEXIST);
345 goto out_unlock;
346 }
347 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700348 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100349 (dax_is_zero_entry(entry) ||
350 dax_is_empty_entry(entry))) {
351 pmd_downgrade = true;
352 }
353 }
354 }
355
Jan Karaac401cc2016-05-12 18:29:18 +0200356 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100357 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200358 int err;
359
Ross Zwisler642261a2016-11-08 11:34:45 +1100360 if (pmd_downgrade) {
361 /*
362 * Make sure 'entry' remains valid while we drop
363 * mapping->tree_lock.
364 */
365 entry = lock_slot(mapping, slot);
366 }
367
Jan Karaac401cc2016-05-12 18:29:18 +0200368 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100369 /*
370 * Besides huge zero pages the only other thing that gets
371 * downgraded are empty entries which don't need to be
372 * unmapped.
373 */
374 if (pmd_downgrade && dax_is_zero_entry(entry))
375 unmap_mapping_range(mapping,
376 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
377
Jan Kara0cb80b42016-12-12 21:34:12 -0500378 err = radix_tree_preload(
379 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
380 if (err) {
381 if (pmd_downgrade)
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700382 put_locked_mapping_entry(mapping, index);
Jan Kara0cb80b42016-12-12 21:34:12 -0500383 return ERR_PTR(err);
384 }
Jan Karaac401cc2016-05-12 18:29:18 +0200385 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100386
Ross Zwislere11f8b72017-04-07 16:04:57 -0700387 if (!entry) {
388 /*
389 * We needed to drop the page_tree lock while calling
390 * radix_tree_preload() and we didn't have an entry to
391 * lock. See if another thread inserted an entry at
392 * our index during this time.
393 */
394 entry = __radix_tree_lookup(&mapping->page_tree, index,
395 NULL, &slot);
396 if (entry) {
397 radix_tree_preload_end();
398 spin_unlock_irq(&mapping->tree_lock);
399 goto restart;
400 }
401 }
402
Ross Zwisler642261a2016-11-08 11:34:45 +1100403 if (pmd_downgrade) {
404 radix_tree_delete(&mapping->page_tree, index);
405 mapping->nrexceptional--;
406 dax_wake_mapping_entry_waiter(mapping, index, entry,
407 true);
408 }
409
410 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
411
412 err = __radix_tree_insert(&mapping->page_tree, index,
413 dax_radix_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200414 radix_tree_preload_end();
415 if (err) {
416 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100417 /*
Ross Zwislere11f8b72017-04-07 16:04:57 -0700418 * Our insertion of a DAX entry failed, most likely
419 * because we were inserting a PMD entry and it
420 * collided with a PTE sized entry at a different
421 * index in the PMD range. We haven't inserted
422 * anything into the radix tree and have no waiters to
423 * wake.
Ross Zwisler642261a2016-11-08 11:34:45 +1100424 */
Jan Karaac401cc2016-05-12 18:29:18 +0200425 return ERR_PTR(err);
426 }
427 /* Good, we have inserted empty locked entry into the tree. */
428 mapping->nrexceptional++;
429 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100430 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200431 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100432 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100433 out_unlock:
Jan Karaac401cc2016-05-12 18:29:18 +0200434 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100435 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200436}
437
Jan Karac6dcf522016-08-10 17:22:44 +0200438static int __dax_invalidate_mapping_entry(struct address_space *mapping,
439 pgoff_t index, bool trunc)
440{
441 int ret = 0;
442 void *entry;
443 struct radix_tree_root *page_tree = &mapping->page_tree;
444
445 spin_lock_irq(&mapping->tree_lock);
446 entry = get_unlocked_mapping_entry(mapping, index, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700447 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200448 goto out;
449 if (!trunc &&
450 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
451 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
452 goto out;
453 radix_tree_delete(page_tree, index);
454 mapping->nrexceptional--;
455 ret = 1;
456out:
457 put_unlocked_mapping_entry(mapping, index, entry);
458 spin_unlock_irq(&mapping->tree_lock);
459 return ret;
460}
Jan Karaac401cc2016-05-12 18:29:18 +0200461/*
462 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
463 * entry to get unlocked before deleting it.
464 */
465int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
466{
Jan Karac6dcf522016-08-10 17:22:44 +0200467 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200468
Jan Karaac401cc2016-05-12 18:29:18 +0200469 /*
470 * This gets called from truncate / punch_hole path. As such, the caller
471 * must hold locks protecting against concurrent modifications of the
472 * radix tree (usually fs-private i_mmap_sem for writing). Since the
473 * caller has seen exceptional entry for this index, we better find it
474 * at that index as well...
475 */
Jan Karac6dcf522016-08-10 17:22:44 +0200476 WARN_ON_ONCE(!ret);
477 return ret;
478}
Jan Karaac401cc2016-05-12 18:29:18 +0200479
Jan Karac6dcf522016-08-10 17:22:44 +0200480/*
Jan Karac6dcf522016-08-10 17:22:44 +0200481 * Invalidate exceptional DAX entry if it is clean.
482 */
483int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
484 pgoff_t index)
485{
486 return __dax_invalidate_mapping_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200487}
488
Dan Williamscccbce62017-01-27 13:31:42 -0800489static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
490 sector_t sector, size_t size, struct page *to,
491 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800492{
Dan Williamscccbce62017-01-27 13:31:42 -0800493 void *vto, *kaddr;
494 pgoff_t pgoff;
495 pfn_t pfn;
496 long rc;
497 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600498
Dan Williamscccbce62017-01-27 13:31:42 -0800499 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
500 if (rc)
501 return rc;
502
503 id = dax_read_lock();
504 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
505 if (rc < 0) {
506 dax_read_unlock(id);
507 return rc;
508 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800509 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800510 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800511 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800512 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800513 return 0;
514}
515
Ross Zwisler642261a2016-11-08 11:34:45 +1100516/*
517 * By this point grab_mapping_entry() has ensured that we have a locked entry
518 * of the appropriate size so we don't have to worry about downgrading PMDs to
519 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
520 * already in the tree, we will skip the insertion and just dirty the PMD as
521 * appropriate.
522 */
Jan Karaac401cc2016-05-12 18:29:18 +0200523static void *dax_insert_mapping_entry(struct address_space *mapping,
524 struct vm_fault *vmf,
Ross Zwisler642261a2016-11-08 11:34:45 +1100525 void *entry, sector_t sector,
526 unsigned long flags)
Ross Zwisler9973c982016-01-22 15:10:47 -0800527{
528 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200529 void *new_entry;
530 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800531
Jan Karaac401cc2016-05-12 18:29:18 +0200532 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800533 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800534
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700535 if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
536 /* we are replacing a zero page with block mapping */
537 if (dax_is_pmd_entry(entry))
538 unmap_mapping_range(mapping,
539 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK,
540 PMD_SIZE, 0);
541 else /* pte entry */
542 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
543 PAGE_SIZE, 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800544 }
545
Jan Karaac401cc2016-05-12 18:29:18 +0200546 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100547 new_entry = dax_radix_locked_entry(sector, flags);
548
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700549 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100550 /*
551 * Only swap our new entry into the radix tree if the current
552 * entry is a zero page or an empty entry. If a normal PTE or
553 * PMD entry is already in the tree, we leave it alone. This
554 * means that if we are trying to insert a PTE and the
555 * existing entry is a PMD, we will just leave the PMD in the
556 * tree and dirty it if necessary.
557 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800558 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200559 void **slot;
560 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800561
Johannes Weinerf7942432016-12-12 16:43:41 -0800562 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200563 WARN_ON_ONCE(ret != entry);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800564 __radix_tree_replace(page_tree, node, slot,
565 new_entry, NULL, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700566 entry = new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800567 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700568
Jan Karaac401cc2016-05-12 18:29:18 +0200569 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800570 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700571
Ross Zwisler9973c982016-01-22 15:10:47 -0800572 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700573 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800574}
575
Jan Kara4b4bb462016-12-14 15:07:53 -0800576static inline unsigned long
577pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
578{
579 unsigned long address;
580
581 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
582 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
583 return address;
584}
585
586/* Walk all mappings of a given index of a file and writeprotect them */
587static void dax_mapping_entry_mkclean(struct address_space *mapping,
588 pgoff_t index, unsigned long pfn)
589{
590 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800591 pte_t pte, *ptep = NULL;
592 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800593 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800594
595 i_mmap_lock_read(mapping);
596 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400597 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800598
599 cond_resched();
600
601 if (!(vma->vm_flags & VM_SHARED))
602 continue;
603
604 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400605
606 /*
607 * Note because we provide start/end to follow_pte_pmd it will
608 * call mmu_notifier_invalidate_range_start() on our behalf
609 * before taking any lock.
610 */
611 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800612 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800613
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800614 if (pmdp) {
615#ifdef CONFIG_FS_DAX_PMD
616 pmd_t pmd;
617
618 if (pfn != pmd_pfn(*pmdp))
619 goto unlock_pmd;
620 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
621 goto unlock_pmd;
622
623 flush_cache_page(vma, address, pfn);
624 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
625 pmd = pmd_wrprotect(pmd);
626 pmd = pmd_mkclean(pmd);
627 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400628 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800629unlock_pmd:
630 spin_unlock(ptl);
631#endif
632 } else {
633 if (pfn != pte_pfn(*ptep))
634 goto unlock_pte;
635 if (!pte_dirty(*ptep) && !pte_write(*ptep))
636 goto unlock_pte;
637
638 flush_cache_page(vma, address, pfn);
639 pte = ptep_clear_flush(vma, address, ptep);
640 pte = pte_wrprotect(pte);
641 pte = pte_mkclean(pte);
642 set_pte_at(vma->vm_mm, address, ptep, pte);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400643 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800644unlock_pte:
645 pte_unmap_unlock(ptep, ptl);
646 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800647
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400648 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800649 }
650 i_mmap_unlock_read(mapping);
651}
652
Ross Zwisler9973c982016-01-22 15:10:47 -0800653static int dax_writeback_one(struct block_device *bdev,
Dan Williamscccbce62017-01-27 13:31:42 -0800654 struct dax_device *dax_dev, struct address_space *mapping,
655 pgoff_t index, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800656{
657 struct radix_tree_root *page_tree = &mapping->page_tree;
Dan Williamscccbce62017-01-27 13:31:42 -0800658 void *entry2, **slot, *kaddr;
659 long ret = 0, id;
660 sector_t sector;
661 pgoff_t pgoff;
662 size_t size;
663 pfn_t pfn;
Ross Zwisler9973c982016-01-22 15:10:47 -0800664
Ross Zwisler9973c982016-01-22 15:10:47 -0800665 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800666 * A page got tagged dirty in DAX mapping? Something is seriously
667 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800668 */
Jan Karaa6abc2c2016-12-14 15:07:47 -0800669 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
670 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800671
Jan Karaa6abc2c2016-12-14 15:07:47 -0800672 spin_lock_irq(&mapping->tree_lock);
673 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
674 /* Entry got punched out / reallocated? */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700675 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800676 goto put_unlocked;
677 /*
678 * Entry got reallocated elsewhere? No need to writeback. We have to
679 * compare sectors as we must not bail out due to difference in lockbit
680 * or entry type.
681 */
682 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
683 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +1100684 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
685 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800686 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800687 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800688 }
689
Jan Karaa6abc2c2016-12-14 15:07:47 -0800690 /* Another fsync thread may have already written back this entry */
691 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
692 goto put_unlocked;
693 /* Lock the entry to serialize with page faults */
694 entry = lock_slot(mapping, slot);
695 /*
696 * We can clear the tag now but we have to be careful so that concurrent
697 * dax_writeback_one() calls for the same index cannot finish before we
698 * actually flush the caches. This is achieved as the calls will look
699 * at the entry only under tree_lock and once they do that they will
700 * see the entry locked and wait for it to unlock.
701 */
702 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
703 spin_unlock_irq(&mapping->tree_lock);
704
Ross Zwisler642261a2016-11-08 11:34:45 +1100705 /*
706 * Even if dax_writeback_mapping_range() was given a wbc->range_start
707 * in the middle of a PMD, the 'index' we are given will be aligned to
708 * the start index of the PMD, as will the sector we pull from
709 * 'entry'. This allows us to flush for PMD_SIZE and not have to
710 * worry about partial PMD writebacks.
711 */
Dan Williamscccbce62017-01-27 13:31:42 -0800712 sector = dax_radix_sector(entry);
713 size = PAGE_SIZE << dax_radix_order(entry);
714
715 id = dax_read_lock();
716 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
717 if (ret)
718 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800719
720 /*
Dan Williamscccbce62017-01-27 13:31:42 -0800721 * dax_direct_access() may sleep, so cannot hold tree_lock over
722 * its invocation.
Ross Zwisler9973c982016-01-22 15:10:47 -0800723 */
Dan Williamscccbce62017-01-27 13:31:42 -0800724 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
725 if (ret < 0)
726 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800727
Dan Williamscccbce62017-01-27 13:31:42 -0800728 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800729 ret = -EIO;
Dan Williamscccbce62017-01-27 13:31:42 -0800730 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800731 }
732
Dan Williamscccbce62017-01-27 13:31:42 -0800733 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
Dan Williams63187702017-05-29 13:07:46 -0700734 dax_flush(dax_dev, pgoff, kaddr, size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800735 /*
736 * After we have flushed the cache, we can clear the dirty tag. There
737 * cannot be new dirty data in the pfn after the flush has completed as
738 * the pfn mappings are writeprotected and fault waits for mapping
739 * entry lock.
740 */
741 spin_lock_irq(&mapping->tree_lock);
742 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
743 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislerf9bc3a02017-05-08 16:00:13 -0700744 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
Dan Williamscccbce62017-01-27 13:31:42 -0800745 dax_unlock:
746 dax_read_unlock(id);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700747 put_locked_mapping_entry(mapping, index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800748 return ret;
749
Jan Karaa6abc2c2016-12-14 15:07:47 -0800750 put_unlocked:
751 put_unlocked_mapping_entry(mapping, index, entry2);
Ross Zwisler9973c982016-01-22 15:10:47 -0800752 spin_unlock_irq(&mapping->tree_lock);
753 return ret;
754}
755
756/*
757 * Flush the mapping to the persistent domain within the byte range of [start,
758 * end]. This is required by data integrity operations to ensure file data is
759 * on persistent storage prior to completion of the operation.
760 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800761int dax_writeback_mapping_range(struct address_space *mapping,
762 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800763{
764 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +1100765 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -0800766 pgoff_t indices[PAGEVEC_SIZE];
Dan Williamscccbce62017-01-27 13:31:42 -0800767 struct dax_device *dax_dev;
Ross Zwisler9973c982016-01-22 15:10:47 -0800768 struct pagevec pvec;
769 bool done = false;
770 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800771
772 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
773 return -EIO;
774
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800775 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
776 return 0;
777
Dan Williamscccbce62017-01-27 13:31:42 -0800778 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
779 if (!dax_dev)
780 return -EIO;
781
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300782 start_index = wbc->range_start >> PAGE_SHIFT;
783 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800784
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700785 trace_dax_writeback_range(inode, start_index, end_index);
786
Ross Zwisler9973c982016-01-22 15:10:47 -0800787 tag_pages_for_writeback(mapping, start_index, end_index);
788
789 pagevec_init(&pvec, 0);
790 while (!done) {
791 pvec.nr = find_get_entries_tag(mapping, start_index,
792 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
793 pvec.pages, indices);
794
795 if (pvec.nr == 0)
796 break;
797
798 for (i = 0; i < pvec.nr; i++) {
799 if (indices[i] > end_index) {
800 done = true;
801 break;
802 }
803
Dan Williamscccbce62017-01-27 13:31:42 -0800804 ret = dax_writeback_one(bdev, dax_dev, mapping,
805 indices[i], pvec.pages[i]);
Jeff Layton819ec6b2017-07-06 07:02:27 -0400806 if (ret < 0) {
807 mapping_set_error(mapping, ret);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700808 goto out;
Jeff Layton819ec6b2017-07-06 07:02:27 -0400809 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800810 }
Jan Kara1eb643d2017-06-23 15:08:46 -0700811 start_index = indices[pvec.nr - 1] + 1;
Ross Zwisler9973c982016-01-22 15:10:47 -0800812 }
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700813out:
Dan Williamscccbce62017-01-27 13:31:42 -0800814 put_dax(dax_dev);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700815 trace_dax_writeback_range_done(inode, start_index, end_index);
816 return (ret < 0 ? ret : 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800817}
818EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
819
Jan Karaac401cc2016-05-12 18:29:18 +0200820static int dax_insert_mapping(struct address_space *mapping,
Dan Williamscccbce62017-01-27 13:31:42 -0800821 struct block_device *bdev, struct dax_device *dax_dev,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700822 sector_t sector, size_t size, void *entry,
Dan Williamscccbce62017-01-27 13:31:42 -0800823 struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800824{
Jan Kara1a29d852016-12-14 15:07:01 -0800825 unsigned long vaddr = vmf->address;
Dan Williamscccbce62017-01-27 13:31:42 -0800826 void *ret, *kaddr;
827 pgoff_t pgoff;
828 int id, rc;
829 pfn_t pfn;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800830
Dan Williamscccbce62017-01-27 13:31:42 -0800831 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
832 if (rc)
833 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800834
Dan Williamscccbce62017-01-27 13:31:42 -0800835 id = dax_read_lock();
836 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
837 if (rc < 0) {
838 dax_read_unlock(id);
839 return rc;
840 }
841 dax_read_unlock(id);
842
843 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector, 0);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200844 if (IS_ERR(ret))
845 return PTR_ERR(ret);
Ross Zwisler9973c982016-01-22 15:10:47 -0800846
Ross Zwislerb4440732017-05-08 16:00:16 -0700847 trace_dax_insert_mapping(mapping->host, vmf, ret);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700848 if (vmf->flags & FAULT_FLAG_WRITE)
849 return vm_insert_mixed_mkwrite(vma, vaddr, pfn);
850 else
851 return vm_insert_mixed(vma, vaddr, pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800852}
853
Ross Zwislere30331f2017-09-06 16:18:39 -0700854/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700855 * The user has performed a load from a hole in the file. Allocating a new
856 * page in the file would cause excessive storage usage for workloads with
857 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
858 * If this page is ever written to we will re-fault and change the mapping to
859 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -0700860 */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700861static int dax_load_hole(struct address_space *mapping, void *entry,
Ross Zwislere30331f2017-09-06 16:18:39 -0700862 struct vm_fault *vmf)
863{
864 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700865 unsigned long vaddr = vmf->address;
866 int ret = VM_FAULT_NOPAGE;
867 struct page *zero_page;
868 void *entry2;
Ross Zwislere30331f2017-09-06 16:18:39 -0700869
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700870 zero_page = ZERO_PAGE(0);
871 if (unlikely(!zero_page)) {
Ross Zwislere30331f2017-09-06 16:18:39 -0700872 ret = VM_FAULT_OOM;
873 goto out;
874 }
875
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700876 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
877 RADIX_DAX_ZERO_PAGE);
878 if (IS_ERR(entry2)) {
879 ret = VM_FAULT_SIGBUS;
880 goto out;
Ross Zwislere30331f2017-09-06 16:18:39 -0700881 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700882
883 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
Ross Zwislere30331f2017-09-06 16:18:39 -0700884out:
885 trace_dax_load_hole(inode, vmf, ret);
886 return ret;
887}
888
Vishal Verma4b0228f2016-04-21 15:13:46 -0400889static bool dax_range_is_aligned(struct block_device *bdev,
890 unsigned int offset, unsigned int length)
891{
892 unsigned short sector_size = bdev_logical_block_size(bdev);
893
894 if (!IS_ALIGNED(offset, sector_size))
895 return false;
896 if (!IS_ALIGNED(length, sector_size))
897 return false;
898
899 return true;
900}
901
Dan Williamscccbce62017-01-27 13:31:42 -0800902int __dax_zero_page_range(struct block_device *bdev,
903 struct dax_device *dax_dev, sector_t sector,
904 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200905{
Dan Williamscccbce62017-01-27 13:31:42 -0800906 if (dax_range_is_aligned(bdev, offset, size)) {
907 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400908
909 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -0700910 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400911 } else {
Dan Williamscccbce62017-01-27 13:31:42 -0800912 pgoff_t pgoff;
913 long rc, id;
914 void *kaddr;
915 pfn_t pfn;
916
Dan Williamse84b83b2017-05-10 19:38:13 -0700917 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800918 if (rc)
919 return rc;
920
921 id = dax_read_lock();
Dan Williamse84b83b2017-05-10 19:38:13 -0700922 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
Dan Williamscccbce62017-01-27 13:31:42 -0800923 &pfn);
924 if (rc < 0) {
925 dax_read_unlock(id);
926 return rc;
927 }
Dan Williams81f55872017-05-29 13:12:20 -0700928 memset(kaddr + offset, 0, size);
929 dax_flush(dax_dev, pgoff, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -0800930 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400931 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200932 return 0;
933}
934EXPORT_SYMBOL_GPL(__dax_zero_page_range);
935
Ross Zwisler333ccc92016-11-08 11:33:09 +1100936static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
937{
938 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
939}
940
Christoph Hellwiga254e562016-09-19 11:24:49 +1000941static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100942dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000943 struct iomap *iomap)
944{
Dan Williamscccbce62017-01-27 13:31:42 -0800945 struct block_device *bdev = iomap->bdev;
946 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000947 struct iov_iter *iter = data;
948 loff_t end = pos + length, done = 0;
949 ssize_t ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800950 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000951
952 if (iov_iter_rw(iter) == READ) {
953 end = min(end, i_size_read(inode));
954 if (pos >= end)
955 return 0;
956
957 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
958 return iov_iter_zero(min(length, end - pos), iter);
959 }
960
961 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
962 return -EIO;
963
Jan Karae3fce682016-08-10 17:10:28 +0200964 /*
965 * Write can allocate block for an area which has a hole page mapped
966 * into page tables. We have to tear down these mappings so that data
967 * written by write(2) is visible in mmap.
968 */
Jan Karacd656372017-05-12 15:46:50 -0700969 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +0200970 invalidate_inode_pages2_range(inode->i_mapping,
971 pos >> PAGE_SHIFT,
972 (end - 1) >> PAGE_SHIFT);
973 }
974
Dan Williamscccbce62017-01-27 13:31:42 -0800975 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +1000976 while (pos < end) {
977 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -0800978 const size_t size = ALIGN(length + offset, PAGE_SIZE);
979 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000980 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -0800981 pgoff_t pgoff;
982 void *kaddr;
983 pfn_t pfn;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000984
Michal Hockod1908f52017-02-03 13:13:26 -0800985 if (fatal_signal_pending(current)) {
986 ret = -EINTR;
987 break;
988 }
989
Dan Williamscccbce62017-01-27 13:31:42 -0800990 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
991 if (ret)
992 break;
993
994 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
995 &kaddr, &pfn);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000996 if (map_len < 0) {
997 ret = map_len;
998 break;
999 }
1000
Dan Williamscccbce62017-01-27 13:31:42 -08001001 map_len = PFN_PHYS(map_len);
1002 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001003 map_len -= offset;
1004 if (map_len > end - pos)
1005 map_len = end - pos;
1006
Ross Zwislera2e050f2017-09-06 16:18:54 -07001007 /*
1008 * The userspace address for the memory copy has already been
1009 * validated via access_ok() in either vfs_read() or
1010 * vfs_write(), depending on which operation we are doing.
1011 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001012 if (iov_iter_rw(iter) == WRITE)
Dan Williamsfec53772017-05-29 21:56:49 -07001013 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1014 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001015 else
Dan Williamscccbce62017-01-27 13:31:42 -08001016 map_len = copy_to_iter(kaddr, map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001017 if (map_len <= 0) {
1018 ret = map_len ? map_len : -EFAULT;
1019 break;
1020 }
1021
1022 pos += map_len;
1023 length -= map_len;
1024 done += map_len;
1025 }
Dan Williamscccbce62017-01-27 13:31:42 -08001026 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001027
1028 return done ? done : ret;
1029}
1030
1031/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001032 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001033 * @iocb: The control block for this I/O
1034 * @iter: The addresses to do I/O from or to
1035 * @ops: iomap ops passed from the file system
1036 *
1037 * This function performs read and write operations to directly mapped
1038 * persistent memory. The callers needs to take care of read/write exclusion
1039 * and evicting any page cache pages in the region under I/O.
1040 */
1041ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001042dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001043 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001044{
1045 struct address_space *mapping = iocb->ki_filp->f_mapping;
1046 struct inode *inode = mapping->host;
1047 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1048 unsigned flags = 0;
1049
Christoph Hellwig168316d2017-02-08 14:43:13 -05001050 if (iov_iter_rw(iter) == WRITE) {
1051 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001052 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001053 } else {
1054 lockdep_assert_held(&inode->i_rwsem);
1055 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001056
Christoph Hellwiga254e562016-09-19 11:24:49 +10001057 while (iov_iter_count(iter)) {
1058 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001059 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001060 if (ret <= 0)
1061 break;
1062 pos += ret;
1063 done += ret;
1064 }
1065
1066 iocb->ki_pos += done;
1067 return done ? done : ret;
1068}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001069EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001070
Jan Kara9f141d62016-10-19 14:34:31 +02001071static int dax_fault_return(int error)
1072{
1073 if (error == 0)
1074 return VM_FAULT_NOPAGE;
1075 if (error == -ENOMEM)
1076 return VM_FAULT_OOM;
1077 return VM_FAULT_SIGBUS;
1078}
1079
Dave Jianga2d58162017-02-24 14:56:59 -08001080static int dax_iomap_pte_fault(struct vm_fault *vmf,
1081 const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001082{
Dave Jiang11bac802017-02-24 14:56:41 -08001083 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001084 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001085 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001086 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1087 sector_t sector;
1088 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001089 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001090 int error, major = 0;
Jan Karab1aa8122016-12-14 15:07:24 -08001091 int vmf_ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001092 void *entry;
1093
Ross Zwislera9c42b32017-05-08 16:00:00 -07001094 trace_dax_pte_fault(inode, vmf, vmf_ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001095 /*
1096 * Check whether offset isn't beyond end of file now. Caller is supposed
1097 * to hold locks serializing us with truncate / punch hole so this is
1098 * a reliable test.
1099 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001100 if (pos >= i_size_read(inode)) {
1101 vmf_ret = VM_FAULT_SIGBUS;
1102 goto out;
1103 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001104
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001105 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1106 flags |= IOMAP_WRITE;
1107
Jan Kara13e451f2017-05-12 15:46:57 -07001108 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1109 if (IS_ERR(entry)) {
1110 vmf_ret = dax_fault_return(PTR_ERR(entry));
1111 goto out;
1112 }
1113
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001114 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001115 * It is possible, particularly with mixed reads & writes to private
1116 * mappings, that we have raced with a PMD fault that overlaps with
1117 * the PTE we need to set up. If so just return and the fault will be
1118 * retried.
1119 */
1120 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1121 vmf_ret = VM_FAULT_NOPAGE;
1122 goto unlock_entry;
1123 }
1124
1125 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001126 * Note that we don't bother to use iomap_apply here: DAX required
1127 * the file system block size to be equal the page size, which means
1128 * that we never have to deal with more than a single extent here.
1129 */
1130 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Ross Zwislera9c42b32017-05-08 16:00:00 -07001131 if (error) {
1132 vmf_ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001133 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001134 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001135 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001136 error = -EIO; /* fs corruption? */
1137 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001138 }
1139
Ross Zwisler333ccc92016-11-08 11:33:09 +11001140 sector = dax_iomap_sector(&iomap, pos);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001141
1142 if (vmf->cow_page) {
1143 switch (iomap.type) {
1144 case IOMAP_HOLE:
1145 case IOMAP_UNWRITTEN:
1146 clear_user_highpage(vmf->cow_page, vaddr);
1147 break;
1148 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001149 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1150 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001151 break;
1152 default:
1153 WARN_ON_ONCE(1);
1154 error = -EIO;
1155 break;
1156 }
1157
1158 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001159 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001160
1161 __SetPageUptodate(vmf->cow_page);
1162 vmf_ret = finish_fault(vmf);
1163 if (!vmf_ret)
1164 vmf_ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001165 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001166 }
1167
1168 switch (iomap.type) {
1169 case IOMAP_MAPPED:
1170 if (iomap.flags & IOMAP_F_NEW) {
1171 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001172 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001173 major = VM_FAULT_MAJOR;
1174 }
Dan Williamscccbce62017-01-27 13:31:42 -08001175 error = dax_insert_mapping(mapping, iomap.bdev, iomap.dax_dev,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001176 sector, PAGE_SIZE, entry, vmf->vma, vmf);
Jan Kara9f141d62016-10-19 14:34:31 +02001177 /* -EBUSY is fine, somebody else faulted on the same PTE */
1178 if (error == -EBUSY)
1179 error = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001180 break;
1181 case IOMAP_UNWRITTEN:
1182 case IOMAP_HOLE:
Ross Zwisler15502902016-11-08 11:33:26 +11001183 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001184 vmf_ret = dax_load_hole(mapping, entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001185 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001186 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001187 /*FALLTHRU*/
1188 default:
1189 WARN_ON_ONCE(1);
1190 error = -EIO;
1191 break;
1192 }
1193
Jan Kara13e451f2017-05-12 15:46:57 -07001194 error_finish_iomap:
Jan Kara9f141d62016-10-19 14:34:31 +02001195 vmf_ret = dax_fault_return(error) | major;
Jan Kara9f141d62016-10-19 14:34:31 +02001196 finish_iomap:
1197 if (ops->iomap_end) {
1198 int copied = PAGE_SIZE;
1199
1200 if (vmf_ret & VM_FAULT_ERROR)
1201 copied = 0;
1202 /*
1203 * The fault is done by now and there's no way back (other
1204 * thread may be already happily using PTE we have installed).
1205 * Just ignore error from ->iomap_end since we cannot do much
1206 * with it.
1207 */
1208 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001209 }
Jan Kara13e451f2017-05-12 15:46:57 -07001210 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001211 put_locked_mapping_entry(mapping, vmf->pgoff);
Jan Kara13e451f2017-05-12 15:46:57 -07001212 out:
Ross Zwislera9c42b32017-05-08 16:00:00 -07001213 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
Jan Kara9f141d62016-10-19 14:34:31 +02001214 return vmf_ret;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001215}
Ross Zwisler642261a2016-11-08 11:34:45 +11001216
1217#ifdef CONFIG_FS_DAX_PMD
1218/*
1219 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1220 * more often than one might expect in the below functions.
1221 */
1222#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1223
Dave Jiangf4200392017-02-22 15:40:06 -08001224static int dax_pmd_insert_mapping(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001225 loff_t pos, void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001226{
Dave Jiangf4200392017-02-22 15:40:06 -08001227 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Dan Williamscccbce62017-01-27 13:31:42 -08001228 const sector_t sector = dax_iomap_sector(iomap, pos);
1229 struct dax_device *dax_dev = iomap->dax_dev;
Ross Zwisler642261a2016-11-08 11:34:45 +11001230 struct block_device *bdev = iomap->bdev;
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001231 struct inode *inode = mapping->host;
Dan Williamscccbce62017-01-27 13:31:42 -08001232 const size_t size = PMD_SIZE;
1233 void *ret = NULL, *kaddr;
1234 long length = 0;
1235 pgoff_t pgoff;
1236 pfn_t pfn;
1237 int id;
Ross Zwisler642261a2016-11-08 11:34:45 +11001238
Dan Williamscccbce62017-01-27 13:31:42 -08001239 if (bdev_dax_pgoff(bdev, sector, size, &pgoff) != 0)
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001240 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001241
Dan Williamscccbce62017-01-27 13:31:42 -08001242 id = dax_read_lock();
1243 length = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
1244 if (length < 0)
1245 goto unlock_fallback;
1246 length = PFN_PHYS(length);
Ross Zwisler642261a2016-11-08 11:34:45 +11001247
Dan Williamscccbce62017-01-27 13:31:42 -08001248 if (length < size)
1249 goto unlock_fallback;
1250 if (pfn_t_to_pfn(pfn) & PG_PMD_COLOUR)
1251 goto unlock_fallback;
1252 if (!pfn_t_devmap(pfn))
1253 goto unlock_fallback;
1254 dax_read_unlock(id);
1255
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001256 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector,
Ross Zwisler642261a2016-11-08 11:34:45 +11001257 RADIX_DAX_PMD);
1258 if (IS_ERR(ret))
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001259 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001260
Dan Williamscccbce62017-01-27 13:31:42 -08001261 trace_dax_pmd_insert_mapping(inode, vmf, length, pfn, ret);
Dave Jiangf4200392017-02-22 15:40:06 -08001262 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Dan Williamscccbce62017-01-27 13:31:42 -08001263 pfn, vmf->flags & FAULT_FLAG_WRITE);
Ross Zwisler642261a2016-11-08 11:34:45 +11001264
Dan Williamscccbce62017-01-27 13:31:42 -08001265unlock_fallback:
1266 dax_read_unlock(id);
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001267fallback:
Dan Williamscccbce62017-01-27 13:31:42 -08001268 trace_dax_pmd_insert_mapping_fallback(inode, vmf, length, pfn, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001269 return VM_FAULT_FALLBACK;
1270}
1271
Dave Jiangf4200392017-02-22 15:40:06 -08001272static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001273 void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001274{
Dave Jiangf4200392017-02-22 15:40:06 -08001275 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1276 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001277 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001278 struct page *zero_page;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001279 void *ret = NULL;
Ross Zwisler642261a2016-11-08 11:34:45 +11001280 spinlock_t *ptl;
1281 pmd_t pmd_entry;
Ross Zwisler642261a2016-11-08 11:34:45 +11001282
Dave Jiangf4200392017-02-22 15:40:06 -08001283 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001284
1285 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001286 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001287
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001288 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
1289 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE);
Ross Zwisler642261a2016-11-08 11:34:45 +11001290 if (IS_ERR(ret))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001291 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001292
Dave Jiangf4200392017-02-22 15:40:06 -08001293 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1294 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001295 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001296 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001297 }
1298
Dave Jiangf4200392017-02-22 15:40:06 -08001299 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001300 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001301 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001302 spin_unlock(ptl);
Dave Jiangf4200392017-02-22 15:40:06 -08001303 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001304 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001305
1306fallback:
Dave Jiangf4200392017-02-22 15:40:06 -08001307 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001308 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001309}
1310
Dave Jianga2d58162017-02-24 14:56:59 -08001311static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1312 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001313{
Dave Jiangf4200392017-02-22 15:40:06 -08001314 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001315 struct address_space *mapping = vma->vm_file->f_mapping;
Dave Jiangd8a849e2017-02-22 15:40:03 -08001316 unsigned long pmd_addr = vmf->address & PMD_MASK;
1317 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Kara9484ab12016-11-10 10:26:50 +11001318 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001319 struct inode *inode = mapping->host;
1320 int result = VM_FAULT_FALLBACK;
1321 struct iomap iomap = { 0 };
1322 pgoff_t max_pgoff, pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001323 void *entry;
1324 loff_t pos;
1325 int error;
1326
Ross Zwisler282a8e02017-02-22 15:39:50 -08001327 /*
1328 * Check whether offset isn't beyond end of file now. Caller is
1329 * supposed to hold locks serializing us with truncate / punch hole so
1330 * this is a reliable test.
1331 */
1332 pgoff = linear_page_index(vma, pmd_addr);
1333 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1334
Dave Jiangf4200392017-02-22 15:40:06 -08001335 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001336
Ross Zwislerfffa2812017-08-25 15:55:36 -07001337 /*
1338 * Make sure that the faulting address's PMD offset (color) matches
1339 * the PMD offset from the start of the file. This is necessary so
1340 * that a PMD range in the page table overlaps exactly with a PMD
1341 * range in the radix tree.
1342 */
1343 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1344 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1345 goto fallback;
1346
Ross Zwisler642261a2016-11-08 11:34:45 +11001347 /* Fall back to PTEs if we're going to COW */
1348 if (write && !(vma->vm_flags & VM_SHARED))
1349 goto fallback;
1350
1351 /* If the PMD would extend outside the VMA */
1352 if (pmd_addr < vma->vm_start)
1353 goto fallback;
1354 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1355 goto fallback;
1356
Ross Zwisler282a8e02017-02-22 15:39:50 -08001357 if (pgoff > max_pgoff) {
1358 result = VM_FAULT_SIGBUS;
1359 goto out;
1360 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001361
1362 /* If the PMD would extend beyond the file size */
1363 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1364 goto fallback;
1365
1366 /*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001367 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1368 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1369 * is already in the tree, for instance), it will return -EEXIST and
1370 * we just fall back to 4k entries.
Jan Kara9f141d62016-10-19 14:34:31 +02001371 */
1372 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1373 if (IS_ERR(entry))
Ross Zwisler876f2942017-05-12 15:47:00 -07001374 goto fallback;
1375
1376 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001377 * It is possible, particularly with mixed reads & writes to private
1378 * mappings, that we have raced with a PTE fault that overlaps with
1379 * the PMD we need to set up. If so just return and the fault will be
1380 * retried.
1381 */
1382 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1383 !pmd_devmap(*vmf->pmd)) {
1384 result = 0;
1385 goto unlock_entry;
1386 }
1387
1388 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001389 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1390 * setting up a mapping, so really we're using iomap_begin() as a way
1391 * to look up our filesystem block.
1392 */
1393 pos = (loff_t)pgoff << PAGE_SHIFT;
1394 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1395 if (error)
1396 goto unlock_entry;
1397
1398 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001399 goto finish_iomap;
1400
Ross Zwisler642261a2016-11-08 11:34:45 +11001401 switch (iomap.type) {
1402 case IOMAP_MAPPED:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001403 result = dax_pmd_insert_mapping(vmf, &iomap, pos, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001404 break;
1405 case IOMAP_UNWRITTEN:
1406 case IOMAP_HOLE:
1407 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001408 break;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001409 result = dax_pmd_load_hole(vmf, &iomap, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001410 break;
1411 default:
1412 WARN_ON_ONCE(1);
1413 break;
1414 }
1415
Jan Kara9f141d62016-10-19 14:34:31 +02001416 finish_iomap:
1417 if (ops->iomap_end) {
1418 int copied = PMD_SIZE;
1419
1420 if (result == VM_FAULT_FALLBACK)
1421 copied = 0;
1422 /*
1423 * The fault is done by now and there's no way back (other
1424 * thread may be already happily using PMD we have installed).
1425 * Just ignore error from ->iomap_end since we cannot do much
1426 * with it.
1427 */
1428 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1429 &iomap);
1430 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001431 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001432 put_locked_mapping_entry(mapping, pgoff);
Ross Zwisler642261a2016-11-08 11:34:45 +11001433 fallback:
1434 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001435 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001436 count_vm_event(THP_FAULT_FALLBACK);
1437 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001438out:
Dave Jiangf4200392017-02-22 15:40:06 -08001439 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001440 return result;
1441}
Dave Jianga2d58162017-02-24 14:56:59 -08001442#else
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001443static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1444 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001445{
1446 return VM_FAULT_FALLBACK;
1447}
Ross Zwisler642261a2016-11-08 11:34:45 +11001448#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001449
1450/**
1451 * dax_iomap_fault - handle a page fault on a DAX file
1452 * @vmf: The description of the fault
1453 * @ops: iomap ops passed from the file system
1454 *
1455 * When a page fault occurs, filesystems may call this helper in
1456 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1457 * has done all the necessary locking for page fault to proceed
1458 * successfully.
1459 */
Dave Jiangc791ace2017-02-24 14:57:08 -08001460int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1461 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001462{
Dave Jiangc791ace2017-02-24 14:57:08 -08001463 switch (pe_size) {
1464 case PE_SIZE_PTE:
Dave Jianga2d58162017-02-24 14:56:59 -08001465 return dax_iomap_pte_fault(vmf, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001466 case PE_SIZE_PMD:
Dave Jianga2d58162017-02-24 14:56:59 -08001467 return dax_iomap_pmd_fault(vmf, ops);
1468 default:
1469 return VM_FAULT_FALLBACK;
1470 }
1471}
1472EXPORT_SYMBOL_GPL(dax_iomap_fault);