blob: ab67ae30ccbf69581ae635700e9e0dd625bb5d05 [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 Zwisler642261a2016-11-08 11:34:45 +110057static int dax_is_pmd_entry(void *entry)
58{
59 return (unsigned long)entry & RADIX_DAX_PMD;
60}
61
62static int dax_is_pte_entry(void *entry)
63{
64 return !((unsigned long)entry & RADIX_DAX_PMD);
65}
66
67static int dax_is_zero_entry(void *entry)
68{
Ross Zwisler91d25ba2017-09-06 16:18:43 -070069 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +110070}
71
72static int dax_is_empty_entry(void *entry)
73{
74 return (unsigned long)entry & RADIX_DAX_EMPTY;
75}
76
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080077/*
Jan Karaac401cc2016-05-12 18:29:18 +020078 * DAX radix tree locking
79 */
80struct exceptional_entry_key {
81 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +110082 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +020083};
84
85struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +020086 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +020087 struct exceptional_entry_key key;
88};
89
Ross Zwisler63e95b52016-11-08 11:32:20 +110090static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
91 pgoff_t index, void *entry, struct exceptional_entry_key *key)
92{
93 unsigned long hash;
94
95 /*
96 * If 'entry' is a PMD, align the 'index' that we use for the wait
97 * queue to the start of that PMD. This ensures that all offsets in
98 * the range covered by the PMD map to the same bit lock.
99 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100100 if (dax_is_pmd_entry(entry))
Ross Zwisler63e95b52016-11-08 11:32:20 +1100101 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
102
103 key->mapping = mapping;
104 key->entry_start = index;
105
106 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
107 return wait_table + hash;
108}
109
Ingo Molnarac6424b2017-06-20 12:06:13 +0200110static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
Jan Karaac401cc2016-05-12 18:29:18 +0200111 int sync, void *keyp)
112{
113 struct exceptional_entry_key *key = keyp;
114 struct wait_exceptional_entry_queue *ewait =
115 container_of(wait, struct wait_exceptional_entry_queue, wait);
116
117 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100118 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200119 return 0;
120 return autoremove_wake_function(wait, mode, sync, NULL);
121}
122
123/*
Ross Zwislere30331f2017-09-06 16:18:39 -0700124 * We do not necessarily hold the mapping->tree_lock when we call this
125 * function so it is possible that 'entry' is no longer a valid item in the
126 * radix tree. This is okay because all we really need to do is to find the
127 * correct waitqueue where tasks might be waiting for that old 'entry' and
128 * wake them.
129 */
130void dax_wake_mapping_entry_waiter(struct address_space *mapping,
131 pgoff_t index, void *entry, bool wake_all)
132{
133 struct exceptional_entry_key key;
134 wait_queue_head_t *wq;
135
136 wq = dax_entry_waitqueue(mapping, index, entry, &key);
137
138 /*
139 * Checking for locked entry and prepare_to_wait_exclusive() happens
140 * under mapping->tree_lock, ditto for entry handling in our callers.
141 * So at this point all tasks that could have seen our entry locked
142 * must be in the waitqueue and the following check will see them.
143 */
144 if (waitqueue_active(wq))
145 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
146}
147
148/*
Jan Karaac401cc2016-05-12 18:29:18 +0200149 * Check whether the given slot is locked. The function must be called with
150 * mapping->tree_lock held
151 */
152static inline int slot_locked(struct address_space *mapping, void **slot)
153{
154 unsigned long entry = (unsigned long)
155 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
156 return entry & RADIX_DAX_ENTRY_LOCK;
157}
158
159/*
160 * Mark the given slot is locked. The function must be called with
161 * mapping->tree_lock held
162 */
163static inline void *lock_slot(struct address_space *mapping, void **slot)
164{
165 unsigned long entry = (unsigned long)
166 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
167
168 entry |= RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800169 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200170 return (void *)entry;
171}
172
173/*
174 * Mark the given slot is unlocked. The function must be called with
175 * mapping->tree_lock held
176 */
177static inline void *unlock_slot(struct address_space *mapping, void **slot)
178{
179 unsigned long entry = (unsigned long)
180 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
181
182 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800183 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200184 return (void *)entry;
185}
186
187/*
188 * Lookup entry in radix tree, wait for it to become unlocked if it is
189 * exceptional entry and return it. The caller must call
190 * put_unlocked_mapping_entry() when he decided not to lock the entry or
191 * put_locked_mapping_entry() when he locked the entry and now wants to
192 * unlock it.
193 *
194 * The function must be called with mapping->tree_lock held.
195 */
196static void *get_unlocked_mapping_entry(struct address_space *mapping,
197 pgoff_t index, void ***slotp)
198{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100199 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200200 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100201 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200202
203 init_wait(&ewait.wait);
204 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200205
206 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100207 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200208 &slot);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700209 if (!entry ||
210 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200211 !slot_locked(mapping, slot)) {
212 if (slotp)
213 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100214 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200215 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100216
217 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200218 prepare_to_wait_exclusive(wq, &ewait.wait,
219 TASK_UNINTERRUPTIBLE);
220 spin_unlock_irq(&mapping->tree_lock);
221 schedule();
222 finish_wait(wq, &ewait.wait);
223 spin_lock_irq(&mapping->tree_lock);
224 }
225}
226
Jan Karab1aa8122016-12-14 15:07:24 -0800227static void dax_unlock_mapping_entry(struct address_space *mapping,
228 pgoff_t index)
229{
230 void *entry, **slot;
231
232 spin_lock_irq(&mapping->tree_lock);
233 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
234 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
235 !slot_locked(mapping, slot))) {
236 spin_unlock_irq(&mapping->tree_lock);
237 return;
238 }
239 unlock_slot(mapping, slot);
240 spin_unlock_irq(&mapping->tree_lock);
241 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
242}
243
Jan Karaac401cc2016-05-12 18:29:18 +0200244static void put_locked_mapping_entry(struct address_space *mapping,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700245 pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200246{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700247 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200248}
249
250/*
251 * Called when we are done with radix tree entry we looked up via
252 * get_unlocked_mapping_entry() and which we didn't lock in the end.
253 */
254static void put_unlocked_mapping_entry(struct address_space *mapping,
255 pgoff_t index, void *entry)
256{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700257 if (!entry)
Jan Karaac401cc2016-05-12 18:29:18 +0200258 return;
259
260 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100261 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
262}
263
Jan Karaac401cc2016-05-12 18:29:18 +0200264/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700265 * Find radix tree entry at given index. If it points to an exceptional entry,
266 * return it with the radix tree entry locked. If the radix tree doesn't
267 * contain given index, create an empty exceptional entry for the index and
268 * return with it locked.
Jan Karaac401cc2016-05-12 18:29:18 +0200269 *
Ross Zwisler642261a2016-11-08 11:34:45 +1100270 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
271 * either return that locked entry or will return an error. This error will
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700272 * happen if there are any 4k entries within the 2MiB range that we are
273 * requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100274 *
275 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
276 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
277 * insertion will fail if it finds any 4k entries already in the tree, and a
278 * 4k insertion will cause an existing 2MiB entry to be unmapped and
279 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
280 * well as 2MiB empty entries.
281 *
282 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
283 * real storage backing them. We will leave these real 2MiB DAX entries in
284 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
285 *
Jan Karaac401cc2016-05-12 18:29:18 +0200286 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
287 * persistent memory the benefit is doubtful. We can add that later if we can
288 * show it helps.
289 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100290static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
291 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200292{
Ross Zwisler642261a2016-11-08 11:34:45 +1100293 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100294 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200295
296restart:
297 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100298 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100299
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700300 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
301 entry = ERR_PTR(-EIO);
302 goto out_unlock;
303 }
304
Ross Zwisler642261a2016-11-08 11:34:45 +1100305 if (entry) {
306 if (size_flag & RADIX_DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700307 if (dax_is_pte_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100308 put_unlocked_mapping_entry(mapping, index,
309 entry);
310 entry = ERR_PTR(-EEXIST);
311 goto out_unlock;
312 }
313 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700314 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100315 (dax_is_zero_entry(entry) ||
316 dax_is_empty_entry(entry))) {
317 pmd_downgrade = true;
318 }
319 }
320 }
321
Jan Karaac401cc2016-05-12 18:29:18 +0200322 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100323 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200324 int err;
325
Ross Zwisler642261a2016-11-08 11:34:45 +1100326 if (pmd_downgrade) {
327 /*
328 * Make sure 'entry' remains valid while we drop
329 * mapping->tree_lock.
330 */
331 entry = lock_slot(mapping, slot);
332 }
333
Jan Karaac401cc2016-05-12 18:29:18 +0200334 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100335 /*
336 * Besides huge zero pages the only other thing that gets
337 * downgraded are empty entries which don't need to be
338 * unmapped.
339 */
340 if (pmd_downgrade && dax_is_zero_entry(entry))
341 unmap_mapping_range(mapping,
342 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
343
Jan Kara0cb80b42016-12-12 21:34:12 -0500344 err = radix_tree_preload(
345 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
346 if (err) {
347 if (pmd_downgrade)
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700348 put_locked_mapping_entry(mapping, index);
Jan Kara0cb80b42016-12-12 21:34:12 -0500349 return ERR_PTR(err);
350 }
Jan Karaac401cc2016-05-12 18:29:18 +0200351 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100352
Ross Zwislere11f8b72017-04-07 16:04:57 -0700353 if (!entry) {
354 /*
355 * We needed to drop the page_tree lock while calling
356 * radix_tree_preload() and we didn't have an entry to
357 * lock. See if another thread inserted an entry at
358 * our index during this time.
359 */
360 entry = __radix_tree_lookup(&mapping->page_tree, index,
361 NULL, &slot);
362 if (entry) {
363 radix_tree_preload_end();
364 spin_unlock_irq(&mapping->tree_lock);
365 goto restart;
366 }
367 }
368
Ross Zwisler642261a2016-11-08 11:34:45 +1100369 if (pmd_downgrade) {
370 radix_tree_delete(&mapping->page_tree, index);
371 mapping->nrexceptional--;
372 dax_wake_mapping_entry_waiter(mapping, index, entry,
373 true);
374 }
375
376 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
377
378 err = __radix_tree_insert(&mapping->page_tree, index,
379 dax_radix_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200380 radix_tree_preload_end();
381 if (err) {
382 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100383 /*
Ross Zwislere11f8b72017-04-07 16:04:57 -0700384 * Our insertion of a DAX entry failed, most likely
385 * because we were inserting a PMD entry and it
386 * collided with a PTE sized entry at a different
387 * index in the PMD range. We haven't inserted
388 * anything into the radix tree and have no waiters to
389 * wake.
Ross Zwisler642261a2016-11-08 11:34:45 +1100390 */
Jan Karaac401cc2016-05-12 18:29:18 +0200391 return ERR_PTR(err);
392 }
393 /* Good, we have inserted empty locked entry into the tree. */
394 mapping->nrexceptional++;
395 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100396 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200397 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100398 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100399 out_unlock:
Jan Karaac401cc2016-05-12 18:29:18 +0200400 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100401 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200402}
403
Jan Karac6dcf522016-08-10 17:22:44 +0200404static int __dax_invalidate_mapping_entry(struct address_space *mapping,
405 pgoff_t index, bool trunc)
406{
407 int ret = 0;
408 void *entry;
409 struct radix_tree_root *page_tree = &mapping->page_tree;
410
411 spin_lock_irq(&mapping->tree_lock);
412 entry = get_unlocked_mapping_entry(mapping, index, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700413 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200414 goto out;
415 if (!trunc &&
416 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
417 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
418 goto out;
419 radix_tree_delete(page_tree, index);
420 mapping->nrexceptional--;
421 ret = 1;
422out:
423 put_unlocked_mapping_entry(mapping, index, entry);
424 spin_unlock_irq(&mapping->tree_lock);
425 return ret;
426}
Jan Karaac401cc2016-05-12 18:29:18 +0200427/*
428 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
429 * entry to get unlocked before deleting it.
430 */
431int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
432{
Jan Karac6dcf522016-08-10 17:22:44 +0200433 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200434
Jan Karaac401cc2016-05-12 18:29:18 +0200435 /*
436 * This gets called from truncate / punch_hole path. As such, the caller
437 * must hold locks protecting against concurrent modifications of the
438 * radix tree (usually fs-private i_mmap_sem for writing). Since the
439 * caller has seen exceptional entry for this index, we better find it
440 * at that index as well...
441 */
Jan Karac6dcf522016-08-10 17:22:44 +0200442 WARN_ON_ONCE(!ret);
443 return ret;
444}
Jan Karaac401cc2016-05-12 18:29:18 +0200445
Jan Karac6dcf522016-08-10 17:22:44 +0200446/*
Jan Karac6dcf522016-08-10 17:22:44 +0200447 * Invalidate exceptional DAX entry if it is clean.
448 */
449int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
450 pgoff_t index)
451{
452 return __dax_invalidate_mapping_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200453}
454
Dan Williamscccbce62017-01-27 13:31:42 -0800455static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
456 sector_t sector, size_t size, struct page *to,
457 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800458{
Dan Williamscccbce62017-01-27 13:31:42 -0800459 void *vto, *kaddr;
460 pgoff_t pgoff;
461 pfn_t pfn;
462 long rc;
463 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600464
Dan Williamscccbce62017-01-27 13:31:42 -0800465 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
466 if (rc)
467 return rc;
468
469 id = dax_read_lock();
470 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
471 if (rc < 0) {
472 dax_read_unlock(id);
473 return rc;
474 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800475 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800476 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800477 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800478 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800479 return 0;
480}
481
Ross Zwisler642261a2016-11-08 11:34:45 +1100482/*
483 * By this point grab_mapping_entry() has ensured that we have a locked entry
484 * of the appropriate size so we don't have to worry about downgrading PMDs to
485 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
486 * already in the tree, we will skip the insertion and just dirty the PMD as
487 * appropriate.
488 */
Jan Karaac401cc2016-05-12 18:29:18 +0200489static void *dax_insert_mapping_entry(struct address_space *mapping,
490 struct vm_fault *vmf,
Ross Zwisler642261a2016-11-08 11:34:45 +1100491 void *entry, sector_t sector,
492 unsigned long flags)
Ross Zwisler9973c982016-01-22 15:10:47 -0800493{
494 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200495 void *new_entry;
496 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800497
Jan Karaac401cc2016-05-12 18:29:18 +0200498 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800499 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800500
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700501 if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
502 /* we are replacing a zero page with block mapping */
503 if (dax_is_pmd_entry(entry))
504 unmap_mapping_range(mapping,
505 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK,
506 PMD_SIZE, 0);
507 else /* pte entry */
508 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
509 PAGE_SIZE, 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800510 }
511
Jan Karaac401cc2016-05-12 18:29:18 +0200512 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100513 new_entry = dax_radix_locked_entry(sector, flags);
514
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700515 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100516 /*
517 * Only swap our new entry into the radix tree if the current
518 * entry is a zero page or an empty entry. If a normal PTE or
519 * PMD entry is already in the tree, we leave it alone. This
520 * means that if we are trying to insert a PTE and the
521 * existing entry is a PMD, we will just leave the PMD in the
522 * tree and dirty it if necessary.
523 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800524 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200525 void **slot;
526 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800527
Johannes Weinerf7942432016-12-12 16:43:41 -0800528 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200529 WARN_ON_ONCE(ret != entry);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800530 __radix_tree_replace(page_tree, node, slot,
531 new_entry, NULL, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700532 entry = new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800533 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700534
Jan Karaac401cc2016-05-12 18:29:18 +0200535 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800536 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700537
Ross Zwisler9973c982016-01-22 15:10:47 -0800538 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700539 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800540}
541
Jan Kara4b4bb462016-12-14 15:07:53 -0800542static inline unsigned long
543pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
544{
545 unsigned long address;
546
547 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
548 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
549 return address;
550}
551
552/* Walk all mappings of a given index of a file and writeprotect them */
553static void dax_mapping_entry_mkclean(struct address_space *mapping,
554 pgoff_t index, unsigned long pfn)
555{
556 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800557 pte_t pte, *ptep = NULL;
558 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800559 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800560
561 i_mmap_lock_read(mapping);
562 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400563 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800564
565 cond_resched();
566
567 if (!(vma->vm_flags & VM_SHARED))
568 continue;
569
570 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400571
572 /*
573 * Note because we provide start/end to follow_pte_pmd it will
574 * call mmu_notifier_invalidate_range_start() on our behalf
575 * before taking any lock.
576 */
577 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800578 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800579
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800580 if (pmdp) {
581#ifdef CONFIG_FS_DAX_PMD
582 pmd_t pmd;
583
584 if (pfn != pmd_pfn(*pmdp))
585 goto unlock_pmd;
586 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
587 goto unlock_pmd;
588
589 flush_cache_page(vma, address, pfn);
590 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
591 pmd = pmd_wrprotect(pmd);
592 pmd = pmd_mkclean(pmd);
593 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400594 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800595unlock_pmd:
596 spin_unlock(ptl);
597#endif
598 } else {
599 if (pfn != pte_pfn(*ptep))
600 goto unlock_pte;
601 if (!pte_dirty(*ptep) && !pte_write(*ptep))
602 goto unlock_pte;
603
604 flush_cache_page(vma, address, pfn);
605 pte = ptep_clear_flush(vma, address, ptep);
606 pte = pte_wrprotect(pte);
607 pte = pte_mkclean(pte);
608 set_pte_at(vma->vm_mm, address, ptep, pte);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400609 mmu_notifier_invalidate_range(vma->vm_mm, start, end);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800610unlock_pte:
611 pte_unmap_unlock(ptep, ptl);
612 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800613
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400614 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800615 }
616 i_mmap_unlock_read(mapping);
617}
618
Ross Zwisler9973c982016-01-22 15:10:47 -0800619static int dax_writeback_one(struct block_device *bdev,
Dan Williamscccbce62017-01-27 13:31:42 -0800620 struct dax_device *dax_dev, struct address_space *mapping,
621 pgoff_t index, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800622{
623 struct radix_tree_root *page_tree = &mapping->page_tree;
Dan Williamscccbce62017-01-27 13:31:42 -0800624 void *entry2, **slot, *kaddr;
625 long ret = 0, id;
626 sector_t sector;
627 pgoff_t pgoff;
628 size_t size;
629 pfn_t pfn;
Ross Zwisler9973c982016-01-22 15:10:47 -0800630
Ross Zwisler9973c982016-01-22 15:10:47 -0800631 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800632 * A page got tagged dirty in DAX mapping? Something is seriously
633 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800634 */
Jan Karaa6abc2c2016-12-14 15:07:47 -0800635 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
636 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800637
Jan Karaa6abc2c2016-12-14 15:07:47 -0800638 spin_lock_irq(&mapping->tree_lock);
639 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
640 /* Entry got punched out / reallocated? */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700641 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800642 goto put_unlocked;
643 /*
644 * Entry got reallocated elsewhere? No need to writeback. We have to
645 * compare sectors as we must not bail out due to difference in lockbit
646 * or entry type.
647 */
648 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
649 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +1100650 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
651 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800652 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800653 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800654 }
655
Jan Karaa6abc2c2016-12-14 15:07:47 -0800656 /* Another fsync thread may have already written back this entry */
657 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
658 goto put_unlocked;
659 /* Lock the entry to serialize with page faults */
660 entry = lock_slot(mapping, slot);
661 /*
662 * We can clear the tag now but we have to be careful so that concurrent
663 * dax_writeback_one() calls for the same index cannot finish before we
664 * actually flush the caches. This is achieved as the calls will look
665 * at the entry only under tree_lock and once they do that they will
666 * see the entry locked and wait for it to unlock.
667 */
668 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
669 spin_unlock_irq(&mapping->tree_lock);
670
Ross Zwisler642261a2016-11-08 11:34:45 +1100671 /*
672 * Even if dax_writeback_mapping_range() was given a wbc->range_start
673 * in the middle of a PMD, the 'index' we are given will be aligned to
674 * the start index of the PMD, as will the sector we pull from
675 * 'entry'. This allows us to flush for PMD_SIZE and not have to
676 * worry about partial PMD writebacks.
677 */
Dan Williamscccbce62017-01-27 13:31:42 -0800678 sector = dax_radix_sector(entry);
679 size = PAGE_SIZE << dax_radix_order(entry);
680
681 id = dax_read_lock();
682 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
683 if (ret)
684 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800685
686 /*
Dan Williamscccbce62017-01-27 13:31:42 -0800687 * dax_direct_access() may sleep, so cannot hold tree_lock over
688 * its invocation.
Ross Zwisler9973c982016-01-22 15:10:47 -0800689 */
Dan Williamscccbce62017-01-27 13:31:42 -0800690 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
691 if (ret < 0)
692 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800693
Dan Williamscccbce62017-01-27 13:31:42 -0800694 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800695 ret = -EIO;
Dan Williamscccbce62017-01-27 13:31:42 -0800696 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800697 }
698
Dan Williamscccbce62017-01-27 13:31:42 -0800699 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
Dan Williams63187702017-05-29 13:07:46 -0700700 dax_flush(dax_dev, pgoff, kaddr, size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800701 /*
702 * After we have flushed the cache, we can clear the dirty tag. There
703 * cannot be new dirty data in the pfn after the flush has completed as
704 * the pfn mappings are writeprotected and fault waits for mapping
705 * entry lock.
706 */
707 spin_lock_irq(&mapping->tree_lock);
708 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
709 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislerf9bc3a02017-05-08 16:00:13 -0700710 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
Dan Williamscccbce62017-01-27 13:31:42 -0800711 dax_unlock:
712 dax_read_unlock(id);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700713 put_locked_mapping_entry(mapping, index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800714 return ret;
715
Jan Karaa6abc2c2016-12-14 15:07:47 -0800716 put_unlocked:
717 put_unlocked_mapping_entry(mapping, index, entry2);
Ross Zwisler9973c982016-01-22 15:10:47 -0800718 spin_unlock_irq(&mapping->tree_lock);
719 return ret;
720}
721
722/*
723 * Flush the mapping to the persistent domain within the byte range of [start,
724 * end]. This is required by data integrity operations to ensure file data is
725 * on persistent storage prior to completion of the operation.
726 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800727int dax_writeback_mapping_range(struct address_space *mapping,
728 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800729{
730 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +1100731 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -0800732 pgoff_t indices[PAGEVEC_SIZE];
Dan Williamscccbce62017-01-27 13:31:42 -0800733 struct dax_device *dax_dev;
Ross Zwisler9973c982016-01-22 15:10:47 -0800734 struct pagevec pvec;
735 bool done = false;
736 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800737
738 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
739 return -EIO;
740
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800741 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
742 return 0;
743
Dan Williamscccbce62017-01-27 13:31:42 -0800744 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
745 if (!dax_dev)
746 return -EIO;
747
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300748 start_index = wbc->range_start >> PAGE_SHIFT;
749 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800750
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700751 trace_dax_writeback_range(inode, start_index, end_index);
752
Ross Zwisler9973c982016-01-22 15:10:47 -0800753 tag_pages_for_writeback(mapping, start_index, end_index);
754
755 pagevec_init(&pvec, 0);
756 while (!done) {
757 pvec.nr = find_get_entries_tag(mapping, start_index,
758 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
759 pvec.pages, indices);
760
761 if (pvec.nr == 0)
762 break;
763
764 for (i = 0; i < pvec.nr; i++) {
765 if (indices[i] > end_index) {
766 done = true;
767 break;
768 }
769
Dan Williamscccbce62017-01-27 13:31:42 -0800770 ret = dax_writeback_one(bdev, dax_dev, mapping,
771 indices[i], pvec.pages[i]);
Jeff Layton819ec6b2017-07-06 07:02:27 -0400772 if (ret < 0) {
773 mapping_set_error(mapping, ret);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700774 goto out;
Jeff Layton819ec6b2017-07-06 07:02:27 -0400775 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800776 }
Jan Kara1eb643d2017-06-23 15:08:46 -0700777 start_index = indices[pvec.nr - 1] + 1;
Ross Zwisler9973c982016-01-22 15:10:47 -0800778 }
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700779out:
Dan Williamscccbce62017-01-27 13:31:42 -0800780 put_dax(dax_dev);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700781 trace_dax_writeback_range_done(inode, start_index, end_index);
782 return (ret < 0 ? ret : 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800783}
784EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
785
Jan Karaac401cc2016-05-12 18:29:18 +0200786static int dax_insert_mapping(struct address_space *mapping,
Dan Williamscccbce62017-01-27 13:31:42 -0800787 struct block_device *bdev, struct dax_device *dax_dev,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700788 sector_t sector, size_t size, void *entry,
Dan Williamscccbce62017-01-27 13:31:42 -0800789 struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800790{
Jan Kara1a29d852016-12-14 15:07:01 -0800791 unsigned long vaddr = vmf->address;
Dan Williamscccbce62017-01-27 13:31:42 -0800792 void *ret, *kaddr;
793 pgoff_t pgoff;
794 int id, rc;
795 pfn_t pfn;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800796
Dan Williamscccbce62017-01-27 13:31:42 -0800797 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
798 if (rc)
799 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800800
Dan Williamscccbce62017-01-27 13:31:42 -0800801 id = dax_read_lock();
802 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
803 if (rc < 0) {
804 dax_read_unlock(id);
805 return rc;
806 }
807 dax_read_unlock(id);
808
809 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector, 0);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200810 if (IS_ERR(ret))
811 return PTR_ERR(ret);
Ross Zwisler9973c982016-01-22 15:10:47 -0800812
Ross Zwislerb4440732017-05-08 16:00:16 -0700813 trace_dax_insert_mapping(mapping->host, vmf, ret);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700814 if (vmf->flags & FAULT_FLAG_WRITE)
815 return vm_insert_mixed_mkwrite(vma, vaddr, pfn);
816 else
817 return vm_insert_mixed(vma, vaddr, pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800818}
819
Ross Zwislere30331f2017-09-06 16:18:39 -0700820/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700821 * The user has performed a load from a hole in the file. Allocating a new
822 * page in the file would cause excessive storage usage for workloads with
823 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
824 * If this page is ever written to we will re-fault and change the mapping to
825 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -0700826 */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700827static int dax_load_hole(struct address_space *mapping, void *entry,
Ross Zwislere30331f2017-09-06 16:18:39 -0700828 struct vm_fault *vmf)
829{
830 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700831 unsigned long vaddr = vmf->address;
832 int ret = VM_FAULT_NOPAGE;
833 struct page *zero_page;
834 void *entry2;
Ross Zwislere30331f2017-09-06 16:18:39 -0700835
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700836 zero_page = ZERO_PAGE(0);
837 if (unlikely(!zero_page)) {
Ross Zwislere30331f2017-09-06 16:18:39 -0700838 ret = VM_FAULT_OOM;
839 goto out;
840 }
841
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700842 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
843 RADIX_DAX_ZERO_PAGE);
844 if (IS_ERR(entry2)) {
845 ret = VM_FAULT_SIGBUS;
846 goto out;
Ross Zwislere30331f2017-09-06 16:18:39 -0700847 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700848
849 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
Ross Zwislere30331f2017-09-06 16:18:39 -0700850out:
851 trace_dax_load_hole(inode, vmf, ret);
852 return ret;
853}
854
Vishal Verma4b0228f2016-04-21 15:13:46 -0400855static bool dax_range_is_aligned(struct block_device *bdev,
856 unsigned int offset, unsigned int length)
857{
858 unsigned short sector_size = bdev_logical_block_size(bdev);
859
860 if (!IS_ALIGNED(offset, sector_size))
861 return false;
862 if (!IS_ALIGNED(length, sector_size))
863 return false;
864
865 return true;
866}
867
Dan Williamscccbce62017-01-27 13:31:42 -0800868int __dax_zero_page_range(struct block_device *bdev,
869 struct dax_device *dax_dev, sector_t sector,
870 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200871{
Dan Williamscccbce62017-01-27 13:31:42 -0800872 if (dax_range_is_aligned(bdev, offset, size)) {
873 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400874
875 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -0700876 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400877 } else {
Dan Williamscccbce62017-01-27 13:31:42 -0800878 pgoff_t pgoff;
879 long rc, id;
880 void *kaddr;
881 pfn_t pfn;
882
Dan Williamse84b83b2017-05-10 19:38:13 -0700883 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800884 if (rc)
885 return rc;
886
887 id = dax_read_lock();
Dan Williamse84b83b2017-05-10 19:38:13 -0700888 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
Dan Williamscccbce62017-01-27 13:31:42 -0800889 &pfn);
890 if (rc < 0) {
891 dax_read_unlock(id);
892 return rc;
893 }
Dan Williams81f55872017-05-29 13:12:20 -0700894 memset(kaddr + offset, 0, size);
895 dax_flush(dax_dev, pgoff, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -0800896 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400897 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200898 return 0;
899}
900EXPORT_SYMBOL_GPL(__dax_zero_page_range);
901
Ross Zwisler333ccc92016-11-08 11:33:09 +1100902static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
903{
904 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
905}
906
Christoph Hellwiga254e562016-09-19 11:24:49 +1000907static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100908dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000909 struct iomap *iomap)
910{
Dan Williamscccbce62017-01-27 13:31:42 -0800911 struct block_device *bdev = iomap->bdev;
912 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000913 struct iov_iter *iter = data;
914 loff_t end = pos + length, done = 0;
915 ssize_t ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800916 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000917
918 if (iov_iter_rw(iter) == READ) {
919 end = min(end, i_size_read(inode));
920 if (pos >= end)
921 return 0;
922
923 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
924 return iov_iter_zero(min(length, end - pos), iter);
925 }
926
927 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
928 return -EIO;
929
Jan Karae3fce682016-08-10 17:10:28 +0200930 /*
931 * Write can allocate block for an area which has a hole page mapped
932 * into page tables. We have to tear down these mappings so that data
933 * written by write(2) is visible in mmap.
934 */
Jan Karacd656372017-05-12 15:46:50 -0700935 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +0200936 invalidate_inode_pages2_range(inode->i_mapping,
937 pos >> PAGE_SHIFT,
938 (end - 1) >> PAGE_SHIFT);
939 }
940
Dan Williamscccbce62017-01-27 13:31:42 -0800941 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +1000942 while (pos < end) {
943 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -0800944 const size_t size = ALIGN(length + offset, PAGE_SIZE);
945 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000946 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -0800947 pgoff_t pgoff;
948 void *kaddr;
949 pfn_t pfn;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000950
Michal Hockod1908f52017-02-03 13:13:26 -0800951 if (fatal_signal_pending(current)) {
952 ret = -EINTR;
953 break;
954 }
955
Dan Williamscccbce62017-01-27 13:31:42 -0800956 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
957 if (ret)
958 break;
959
960 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
961 &kaddr, &pfn);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000962 if (map_len < 0) {
963 ret = map_len;
964 break;
965 }
966
Dan Williamscccbce62017-01-27 13:31:42 -0800967 map_len = PFN_PHYS(map_len);
968 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000969 map_len -= offset;
970 if (map_len > end - pos)
971 map_len = end - pos;
972
973 if (iov_iter_rw(iter) == WRITE)
Dan Williamsfec53772017-05-29 21:56:49 -0700974 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
975 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000976 else
Dan Williamscccbce62017-01-27 13:31:42 -0800977 map_len = copy_to_iter(kaddr, map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000978 if (map_len <= 0) {
979 ret = map_len ? map_len : -EFAULT;
980 break;
981 }
982
983 pos += map_len;
984 length -= map_len;
985 done += map_len;
986 }
Dan Williamscccbce62017-01-27 13:31:42 -0800987 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000988
989 return done ? done : ret;
990}
991
992/**
Ross Zwisler11c59c92016-11-08 11:32:46 +1100993 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +1000994 * @iocb: The control block for this I/O
995 * @iter: The addresses to do I/O from or to
996 * @ops: iomap ops passed from the file system
997 *
998 * This function performs read and write operations to directly mapped
999 * persistent memory. The callers needs to take care of read/write exclusion
1000 * and evicting any page cache pages in the region under I/O.
1001 */
1002ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001003dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001004 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001005{
1006 struct address_space *mapping = iocb->ki_filp->f_mapping;
1007 struct inode *inode = mapping->host;
1008 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1009 unsigned flags = 0;
1010
Christoph Hellwig168316d2017-02-08 14:43:13 -05001011 if (iov_iter_rw(iter) == WRITE) {
1012 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001013 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001014 } else {
1015 lockdep_assert_held(&inode->i_rwsem);
1016 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001017
Christoph Hellwiga254e562016-09-19 11:24:49 +10001018 while (iov_iter_count(iter)) {
1019 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001020 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001021 if (ret <= 0)
1022 break;
1023 pos += ret;
1024 done += ret;
1025 }
1026
1027 iocb->ki_pos += done;
1028 return done ? done : ret;
1029}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001030EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001031
Jan Kara9f141d62016-10-19 14:34:31 +02001032static int dax_fault_return(int error)
1033{
1034 if (error == 0)
1035 return VM_FAULT_NOPAGE;
1036 if (error == -ENOMEM)
1037 return VM_FAULT_OOM;
1038 return VM_FAULT_SIGBUS;
1039}
1040
Dave Jianga2d58162017-02-24 14:56:59 -08001041static int dax_iomap_pte_fault(struct vm_fault *vmf,
1042 const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001043{
Dave Jiang11bac802017-02-24 14:56:41 -08001044 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001045 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001046 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001047 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1048 sector_t sector;
1049 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001050 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001051 int error, major = 0;
Jan Karab1aa8122016-12-14 15:07:24 -08001052 int vmf_ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001053 void *entry;
1054
Ross Zwislera9c42b32017-05-08 16:00:00 -07001055 trace_dax_pte_fault(inode, vmf, vmf_ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001056 /*
1057 * Check whether offset isn't beyond end of file now. Caller is supposed
1058 * to hold locks serializing us with truncate / punch hole so this is
1059 * a reliable test.
1060 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001061 if (pos >= i_size_read(inode)) {
1062 vmf_ret = VM_FAULT_SIGBUS;
1063 goto out;
1064 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001065
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001066 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1067 flags |= IOMAP_WRITE;
1068
Jan Kara13e451f2017-05-12 15:46:57 -07001069 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1070 if (IS_ERR(entry)) {
1071 vmf_ret = dax_fault_return(PTR_ERR(entry));
1072 goto out;
1073 }
1074
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001075 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001076 * It is possible, particularly with mixed reads & writes to private
1077 * mappings, that we have raced with a PMD fault that overlaps with
1078 * the PTE we need to set up. If so just return and the fault will be
1079 * retried.
1080 */
1081 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1082 vmf_ret = VM_FAULT_NOPAGE;
1083 goto unlock_entry;
1084 }
1085
1086 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001087 * Note that we don't bother to use iomap_apply here: DAX required
1088 * the file system block size to be equal the page size, which means
1089 * that we never have to deal with more than a single extent here.
1090 */
1091 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Ross Zwislera9c42b32017-05-08 16:00:00 -07001092 if (error) {
1093 vmf_ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001094 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001095 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001096 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001097 error = -EIO; /* fs corruption? */
1098 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001099 }
1100
Ross Zwisler333ccc92016-11-08 11:33:09 +11001101 sector = dax_iomap_sector(&iomap, pos);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001102
1103 if (vmf->cow_page) {
1104 switch (iomap.type) {
1105 case IOMAP_HOLE:
1106 case IOMAP_UNWRITTEN:
1107 clear_user_highpage(vmf->cow_page, vaddr);
1108 break;
1109 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001110 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1111 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001112 break;
1113 default:
1114 WARN_ON_ONCE(1);
1115 error = -EIO;
1116 break;
1117 }
1118
1119 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001120 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001121
1122 __SetPageUptodate(vmf->cow_page);
1123 vmf_ret = finish_fault(vmf);
1124 if (!vmf_ret)
1125 vmf_ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001126 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001127 }
1128
1129 switch (iomap.type) {
1130 case IOMAP_MAPPED:
1131 if (iomap.flags & IOMAP_F_NEW) {
1132 count_vm_event(PGMAJFAULT);
Roman Gushchin22621852017-07-06 15:40:25 -07001133 count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001134 major = VM_FAULT_MAJOR;
1135 }
Dan Williamscccbce62017-01-27 13:31:42 -08001136 error = dax_insert_mapping(mapping, iomap.bdev, iomap.dax_dev,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001137 sector, PAGE_SIZE, entry, vmf->vma, vmf);
Jan Kara9f141d62016-10-19 14:34:31 +02001138 /* -EBUSY is fine, somebody else faulted on the same PTE */
1139 if (error == -EBUSY)
1140 error = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001141 break;
1142 case IOMAP_UNWRITTEN:
1143 case IOMAP_HOLE:
Ross Zwisler15502902016-11-08 11:33:26 +11001144 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001145 vmf_ret = dax_load_hole(mapping, entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001146 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001147 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001148 /*FALLTHRU*/
1149 default:
1150 WARN_ON_ONCE(1);
1151 error = -EIO;
1152 break;
1153 }
1154
Jan Kara13e451f2017-05-12 15:46:57 -07001155 error_finish_iomap:
Jan Kara9f141d62016-10-19 14:34:31 +02001156 vmf_ret = dax_fault_return(error) | major;
Jan Kara9f141d62016-10-19 14:34:31 +02001157 finish_iomap:
1158 if (ops->iomap_end) {
1159 int copied = PAGE_SIZE;
1160
1161 if (vmf_ret & VM_FAULT_ERROR)
1162 copied = 0;
1163 /*
1164 * The fault is done by now and there's no way back (other
1165 * thread may be already happily using PTE we have installed).
1166 * Just ignore error from ->iomap_end since we cannot do much
1167 * with it.
1168 */
1169 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001170 }
Jan Kara13e451f2017-05-12 15:46:57 -07001171 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001172 put_locked_mapping_entry(mapping, vmf->pgoff);
Jan Kara13e451f2017-05-12 15:46:57 -07001173 out:
Ross Zwislera9c42b32017-05-08 16:00:00 -07001174 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
Jan Kara9f141d62016-10-19 14:34:31 +02001175 return vmf_ret;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001176}
Ross Zwisler642261a2016-11-08 11:34:45 +11001177
1178#ifdef CONFIG_FS_DAX_PMD
1179/*
1180 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1181 * more often than one might expect in the below functions.
1182 */
1183#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1184
Dave Jiangf4200392017-02-22 15:40:06 -08001185static int dax_pmd_insert_mapping(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001186 loff_t pos, void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001187{
Dave Jiangf4200392017-02-22 15:40:06 -08001188 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Dan Williamscccbce62017-01-27 13:31:42 -08001189 const sector_t sector = dax_iomap_sector(iomap, pos);
1190 struct dax_device *dax_dev = iomap->dax_dev;
Ross Zwisler642261a2016-11-08 11:34:45 +11001191 struct block_device *bdev = iomap->bdev;
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001192 struct inode *inode = mapping->host;
Dan Williamscccbce62017-01-27 13:31:42 -08001193 const size_t size = PMD_SIZE;
1194 void *ret = NULL, *kaddr;
1195 long length = 0;
1196 pgoff_t pgoff;
1197 pfn_t pfn;
1198 int id;
Ross Zwisler642261a2016-11-08 11:34:45 +11001199
Dan Williamscccbce62017-01-27 13:31:42 -08001200 if (bdev_dax_pgoff(bdev, sector, size, &pgoff) != 0)
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001201 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001202
Dan Williamscccbce62017-01-27 13:31:42 -08001203 id = dax_read_lock();
1204 length = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
1205 if (length < 0)
1206 goto unlock_fallback;
1207 length = PFN_PHYS(length);
Ross Zwisler642261a2016-11-08 11:34:45 +11001208
Dan Williamscccbce62017-01-27 13:31:42 -08001209 if (length < size)
1210 goto unlock_fallback;
1211 if (pfn_t_to_pfn(pfn) & PG_PMD_COLOUR)
1212 goto unlock_fallback;
1213 if (!pfn_t_devmap(pfn))
1214 goto unlock_fallback;
1215 dax_read_unlock(id);
1216
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001217 ret = dax_insert_mapping_entry(mapping, vmf, entry, sector,
Ross Zwisler642261a2016-11-08 11:34:45 +11001218 RADIX_DAX_PMD);
1219 if (IS_ERR(ret))
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001220 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001221
Dan Williamscccbce62017-01-27 13:31:42 -08001222 trace_dax_pmd_insert_mapping(inode, vmf, length, pfn, ret);
Dave Jiangf4200392017-02-22 15:40:06 -08001223 return vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Dan Williamscccbce62017-01-27 13:31:42 -08001224 pfn, vmf->flags & FAULT_FLAG_WRITE);
Ross Zwisler642261a2016-11-08 11:34:45 +11001225
Dan Williamscccbce62017-01-27 13:31:42 -08001226unlock_fallback:
1227 dax_read_unlock(id);
Ross Zwisler27a7ffa2017-02-22 15:40:00 -08001228fallback:
Dan Williamscccbce62017-01-27 13:31:42 -08001229 trace_dax_pmd_insert_mapping_fallback(inode, vmf, length, pfn, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001230 return VM_FAULT_FALLBACK;
1231}
1232
Dave Jiangf4200392017-02-22 15:40:06 -08001233static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001234 void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001235{
Dave Jiangf4200392017-02-22 15:40:06 -08001236 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1237 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001238 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001239 struct page *zero_page;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001240 void *ret = NULL;
Ross Zwisler642261a2016-11-08 11:34:45 +11001241 spinlock_t *ptl;
1242 pmd_t pmd_entry;
Ross Zwisler642261a2016-11-08 11:34:45 +11001243
Dave Jiangf4200392017-02-22 15:40:06 -08001244 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001245
1246 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001247 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001248
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001249 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
1250 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE);
Ross Zwisler642261a2016-11-08 11:34:45 +11001251 if (IS_ERR(ret))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001252 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001253
Dave Jiangf4200392017-02-22 15:40:06 -08001254 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1255 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001256 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001257 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001258 }
1259
Dave Jiangf4200392017-02-22 15:40:06 -08001260 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001261 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001262 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001263 spin_unlock(ptl);
Dave Jiangf4200392017-02-22 15:40:06 -08001264 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001265 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001266
1267fallback:
Dave Jiangf4200392017-02-22 15:40:06 -08001268 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001269 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001270}
1271
Dave Jianga2d58162017-02-24 14:56:59 -08001272static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1273 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001274{
Dave Jiangf4200392017-02-22 15:40:06 -08001275 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001276 struct address_space *mapping = vma->vm_file->f_mapping;
Dave Jiangd8a849e2017-02-22 15:40:03 -08001277 unsigned long pmd_addr = vmf->address & PMD_MASK;
1278 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Kara9484ab12016-11-10 10:26:50 +11001279 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001280 struct inode *inode = mapping->host;
1281 int result = VM_FAULT_FALLBACK;
1282 struct iomap iomap = { 0 };
1283 pgoff_t max_pgoff, pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001284 void *entry;
1285 loff_t pos;
1286 int error;
1287
Ross Zwisler282a8e02017-02-22 15:39:50 -08001288 /*
1289 * Check whether offset isn't beyond end of file now. Caller is
1290 * supposed to hold locks serializing us with truncate / punch hole so
1291 * this is a reliable test.
1292 */
1293 pgoff = linear_page_index(vma, pmd_addr);
1294 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1295
Dave Jiangf4200392017-02-22 15:40:06 -08001296 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001297
Ross Zwislerfffa2812017-08-25 15:55:36 -07001298 /*
1299 * Make sure that the faulting address's PMD offset (color) matches
1300 * the PMD offset from the start of the file. This is necessary so
1301 * that a PMD range in the page table overlaps exactly with a PMD
1302 * range in the radix tree.
1303 */
1304 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1305 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1306 goto fallback;
1307
Ross Zwisler642261a2016-11-08 11:34:45 +11001308 /* Fall back to PTEs if we're going to COW */
1309 if (write && !(vma->vm_flags & VM_SHARED))
1310 goto fallback;
1311
1312 /* If the PMD would extend outside the VMA */
1313 if (pmd_addr < vma->vm_start)
1314 goto fallback;
1315 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1316 goto fallback;
1317
Ross Zwisler282a8e02017-02-22 15:39:50 -08001318 if (pgoff > max_pgoff) {
1319 result = VM_FAULT_SIGBUS;
1320 goto out;
1321 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001322
1323 /* If the PMD would extend beyond the file size */
1324 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1325 goto fallback;
1326
1327 /*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001328 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1329 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1330 * is already in the tree, for instance), it will return -EEXIST and
1331 * we just fall back to 4k entries.
Jan Kara9f141d62016-10-19 14:34:31 +02001332 */
1333 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1334 if (IS_ERR(entry))
Ross Zwisler876f2942017-05-12 15:47:00 -07001335 goto fallback;
1336
1337 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001338 * It is possible, particularly with mixed reads & writes to private
1339 * mappings, that we have raced with a PTE fault that overlaps with
1340 * the PMD we need to set up. If so just return and the fault will be
1341 * retried.
1342 */
1343 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1344 !pmd_devmap(*vmf->pmd)) {
1345 result = 0;
1346 goto unlock_entry;
1347 }
1348
1349 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001350 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1351 * setting up a mapping, so really we're using iomap_begin() as a way
1352 * to look up our filesystem block.
1353 */
1354 pos = (loff_t)pgoff << PAGE_SHIFT;
1355 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1356 if (error)
1357 goto unlock_entry;
1358
1359 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001360 goto finish_iomap;
1361
Ross Zwisler642261a2016-11-08 11:34:45 +11001362 switch (iomap.type) {
1363 case IOMAP_MAPPED:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001364 result = dax_pmd_insert_mapping(vmf, &iomap, pos, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001365 break;
1366 case IOMAP_UNWRITTEN:
1367 case IOMAP_HOLE:
1368 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001369 break;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001370 result = dax_pmd_load_hole(vmf, &iomap, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001371 break;
1372 default:
1373 WARN_ON_ONCE(1);
1374 break;
1375 }
1376
Jan Kara9f141d62016-10-19 14:34:31 +02001377 finish_iomap:
1378 if (ops->iomap_end) {
1379 int copied = PMD_SIZE;
1380
1381 if (result == VM_FAULT_FALLBACK)
1382 copied = 0;
1383 /*
1384 * The fault is done by now and there's no way back (other
1385 * thread may be already happily using PMD we have installed).
1386 * Just ignore error from ->iomap_end since we cannot do much
1387 * with it.
1388 */
1389 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1390 &iomap);
1391 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001392 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001393 put_locked_mapping_entry(mapping, pgoff);
Ross Zwisler642261a2016-11-08 11:34:45 +11001394 fallback:
1395 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001396 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001397 count_vm_event(THP_FAULT_FALLBACK);
1398 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001399out:
Dave Jiangf4200392017-02-22 15:40:06 -08001400 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001401 return result;
1402}
Dave Jianga2d58162017-02-24 14:56:59 -08001403#else
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001404static int dax_iomap_pmd_fault(struct vm_fault *vmf,
1405 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001406{
1407 return VM_FAULT_FALLBACK;
1408}
Ross Zwisler642261a2016-11-08 11:34:45 +11001409#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001410
1411/**
1412 * dax_iomap_fault - handle a page fault on a DAX file
1413 * @vmf: The description of the fault
1414 * @ops: iomap ops passed from the file system
1415 *
1416 * When a page fault occurs, filesystems may call this helper in
1417 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1418 * has done all the necessary locking for page fault to proceed
1419 * successfully.
1420 */
Dave Jiangc791ace2017-02-24 14:57:08 -08001421int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1422 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001423{
Dave Jiangc791ace2017-02-24 14:57:08 -08001424 switch (pe_size) {
1425 case PE_SIZE_PTE:
Dave Jianga2d58162017-02-24 14:56:59 -08001426 return dax_iomap_pte_fault(vmf, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001427 case PE_SIZE_PMD:
Dave Jianga2d58162017-02-24 14:56:59 -08001428 return dax_iomap_pmd_fault(vmf, ops);
1429 default:
1430 return VM_FAULT_FALLBACK;
1431 }
1432}
1433EXPORT_SYMBOL_GPL(dax_iomap_fault);