blob: 0eb65c34d5a6b198a57d67671d67fd5bef2bfa72 [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 Zwisler917f3452017-09-06 16:18:58 -070045/* The 'colour' (ie low bits) within a PMD of a page offset. */
46#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
Matthew Wilcox977fbdc2018-01-31 16:17:36 -080047#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
Ross Zwisler917f3452017-09-06 16:18:58 -070048
Ross Zwislerce95ab02016-11-08 11:31:44 +110049static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020050
51static int __init init_dax_wait_table(void)
52{
53 int i;
54
55 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
56 init_waitqueue_head(wait_table + i);
57 return 0;
58}
59fs_initcall(init_dax_wait_table);
60
Ross Zwisler527b19d2017-09-06 16:18:51 -070061/*
62 * We use lowest available bit in exceptional entry for locking, one bit for
63 * the entry size (PMD) and two more to tell us if the entry is a zero page or
64 * an empty entry that is just used for locking. In total four special bits.
65 *
66 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
67 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
68 * block allocation.
69 */
70#define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 4)
71#define RADIX_DAX_ENTRY_LOCK (1 << RADIX_TREE_EXCEPTIONAL_SHIFT)
72#define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
73#define RADIX_DAX_ZERO_PAGE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
74#define RADIX_DAX_EMPTY (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 3))
75
76static unsigned long dax_radix_sector(void *entry)
77{
78 return (unsigned long)entry >> RADIX_DAX_SHIFT;
79}
80
81static void *dax_radix_locked_entry(sector_t sector, unsigned long flags)
82{
83 return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags |
84 ((unsigned long)sector << RADIX_DAX_SHIFT) |
85 RADIX_DAX_ENTRY_LOCK);
86}
87
88static unsigned int dax_radix_order(void *entry)
89{
90 if ((unsigned long)entry & RADIX_DAX_PMD)
91 return PMD_SHIFT - PAGE_SHIFT;
92 return 0;
93}
94
Ross Zwisler642261a2016-11-08 11:34:45 +110095static int dax_is_pmd_entry(void *entry)
96{
97 return (unsigned long)entry & RADIX_DAX_PMD;
98}
99
100static int dax_is_pte_entry(void *entry)
101{
102 return !((unsigned long)entry & RADIX_DAX_PMD);
103}
104
105static int dax_is_zero_entry(void *entry)
106{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700107 return (unsigned long)entry & RADIX_DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100108}
109
110static int dax_is_empty_entry(void *entry)
111{
112 return (unsigned long)entry & RADIX_DAX_EMPTY;
113}
114
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800115/*
Jan Karaac401cc2016-05-12 18:29:18 +0200116 * DAX radix tree locking
117 */
118struct exceptional_entry_key {
119 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100120 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200121};
122
123struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200124 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200125 struct exceptional_entry_key key;
126};
127
Ross Zwisler63e95b52016-11-08 11:32:20 +1100128static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
129 pgoff_t index, void *entry, struct exceptional_entry_key *key)
130{
131 unsigned long hash;
132
133 /*
134 * If 'entry' is a PMD, align the 'index' that we use for the wait
135 * queue to the start of that PMD. This ensures that all offsets in
136 * the range covered by the PMD map to the same bit lock.
137 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100138 if (dax_is_pmd_entry(entry))
Ross Zwisler917f3452017-09-06 16:18:58 -0700139 index &= ~PG_PMD_COLOUR;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100140
141 key->mapping = mapping;
142 key->entry_start = index;
143
144 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
145 return wait_table + hash;
146}
147
Ingo Molnarac6424b2017-06-20 12:06:13 +0200148static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
Jan Karaac401cc2016-05-12 18:29:18 +0200149 int sync, void *keyp)
150{
151 struct exceptional_entry_key *key = keyp;
152 struct wait_exceptional_entry_queue *ewait =
153 container_of(wait, struct wait_exceptional_entry_queue, wait);
154
155 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100156 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200157 return 0;
158 return autoremove_wake_function(wait, mode, sync, NULL);
159}
160
161/*
Ross Zwislere30331f2017-09-06 16:18:39 -0700162 * We do not necessarily hold the mapping->tree_lock when we call this
163 * function so it is possible that 'entry' is no longer a valid item in the
164 * radix tree. This is okay because all we really need to do is to find the
165 * correct waitqueue where tasks might be waiting for that old 'entry' and
166 * wake them.
167 */
Ross Zwislerd01ad192017-09-06 16:18:47 -0700168static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwislere30331f2017-09-06 16:18:39 -0700169 pgoff_t index, void *entry, bool wake_all)
170{
171 struct exceptional_entry_key key;
172 wait_queue_head_t *wq;
173
174 wq = dax_entry_waitqueue(mapping, index, entry, &key);
175
176 /*
177 * Checking for locked entry and prepare_to_wait_exclusive() happens
178 * under mapping->tree_lock, ditto for entry handling in our callers.
179 * So at this point all tasks that could have seen our entry locked
180 * must be in the waitqueue and the following check will see them.
181 */
182 if (waitqueue_active(wq))
183 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
184}
185
186/*
Jan Karaac401cc2016-05-12 18:29:18 +0200187 * Check whether the given slot is locked. The function must be called with
188 * mapping->tree_lock held
189 */
190static inline int slot_locked(struct address_space *mapping, void **slot)
191{
192 unsigned long entry = (unsigned long)
193 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
194 return entry & RADIX_DAX_ENTRY_LOCK;
195}
196
197/*
198 * Mark the given slot is locked. The function must be called with
199 * mapping->tree_lock held
200 */
201static inline void *lock_slot(struct address_space *mapping, void **slot)
202{
203 unsigned long entry = (unsigned long)
204 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
205
206 entry |= RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800207 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200208 return (void *)entry;
209}
210
211/*
212 * Mark the given slot is unlocked. The function must be called with
213 * mapping->tree_lock held
214 */
215static inline void *unlock_slot(struct address_space *mapping, void **slot)
216{
217 unsigned long entry = (unsigned long)
218 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
219
220 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800221 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200222 return (void *)entry;
223}
224
225/*
226 * Lookup entry in radix tree, wait for it to become unlocked if it is
227 * exceptional entry and return it. The caller must call
228 * put_unlocked_mapping_entry() when he decided not to lock the entry or
229 * put_locked_mapping_entry() when he locked the entry and now wants to
230 * unlock it.
231 *
232 * The function must be called with mapping->tree_lock held.
233 */
234static void *get_unlocked_mapping_entry(struct address_space *mapping,
235 pgoff_t index, void ***slotp)
236{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100237 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200238 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100239 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200240
241 init_wait(&ewait.wait);
242 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200243
244 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100245 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200246 &slot);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700247 if (!entry ||
248 WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200249 !slot_locked(mapping, slot)) {
250 if (slotp)
251 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100252 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200253 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100254
255 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200256 prepare_to_wait_exclusive(wq, &ewait.wait,
257 TASK_UNINTERRUPTIBLE);
258 spin_unlock_irq(&mapping->tree_lock);
259 schedule();
260 finish_wait(wq, &ewait.wait);
261 spin_lock_irq(&mapping->tree_lock);
262 }
263}
264
Jan Karab1aa8122016-12-14 15:07:24 -0800265static void dax_unlock_mapping_entry(struct address_space *mapping,
266 pgoff_t index)
267{
268 void *entry, **slot;
269
270 spin_lock_irq(&mapping->tree_lock);
271 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
272 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
273 !slot_locked(mapping, slot))) {
274 spin_unlock_irq(&mapping->tree_lock);
275 return;
276 }
277 unlock_slot(mapping, slot);
278 spin_unlock_irq(&mapping->tree_lock);
279 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
280}
281
Jan Karaac401cc2016-05-12 18:29:18 +0200282static void put_locked_mapping_entry(struct address_space *mapping,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700283 pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200284{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700285 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200286}
287
288/*
289 * Called when we are done with radix tree entry we looked up via
290 * get_unlocked_mapping_entry() and which we didn't lock in the end.
291 */
292static void put_unlocked_mapping_entry(struct address_space *mapping,
293 pgoff_t index, void *entry)
294{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700295 if (!entry)
Jan Karaac401cc2016-05-12 18:29:18 +0200296 return;
297
298 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100299 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
300}
301
Jan Karaac401cc2016-05-12 18:29:18 +0200302/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700303 * Find radix tree entry at given index. If it points to an exceptional entry,
304 * return it with the radix tree entry locked. If the radix tree doesn't
305 * contain given index, create an empty exceptional entry for the index and
306 * return with it locked.
Jan Karaac401cc2016-05-12 18:29:18 +0200307 *
Ross Zwisler642261a2016-11-08 11:34:45 +1100308 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
309 * either return that locked entry or will return an error. This error will
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700310 * happen if there are any 4k entries within the 2MiB range that we are
311 * requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100312 *
313 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
314 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
315 * insertion will fail if it finds any 4k entries already in the tree, and a
316 * 4k insertion will cause an existing 2MiB entry to be unmapped and
317 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
318 * well as 2MiB empty entries.
319 *
320 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
321 * real storage backing them. We will leave these real 2MiB DAX entries in
322 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
323 *
Jan Karaac401cc2016-05-12 18:29:18 +0200324 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
325 * persistent memory the benefit is doubtful. We can add that later if we can
326 * show it helps.
327 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100328static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
329 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200330{
Ross Zwisler642261a2016-11-08 11:34:45 +1100331 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100332 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200333
334restart:
335 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100336 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100337
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700338 if (WARN_ON_ONCE(entry && !radix_tree_exceptional_entry(entry))) {
339 entry = ERR_PTR(-EIO);
340 goto out_unlock;
341 }
342
Ross Zwisler642261a2016-11-08 11:34:45 +1100343 if (entry) {
344 if (size_flag & RADIX_DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700345 if (dax_is_pte_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100346 put_unlocked_mapping_entry(mapping, index,
347 entry);
348 entry = ERR_PTR(-EEXIST);
349 goto out_unlock;
350 }
351 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700352 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100353 (dax_is_zero_entry(entry) ||
354 dax_is_empty_entry(entry))) {
355 pmd_downgrade = true;
356 }
357 }
358 }
359
Jan Karaac401cc2016-05-12 18:29:18 +0200360 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100361 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200362 int err;
363
Ross Zwisler642261a2016-11-08 11:34:45 +1100364 if (pmd_downgrade) {
365 /*
366 * Make sure 'entry' remains valid while we drop
367 * mapping->tree_lock.
368 */
369 entry = lock_slot(mapping, slot);
370 }
371
Jan Karaac401cc2016-05-12 18:29:18 +0200372 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100373 /*
374 * Besides huge zero pages the only other thing that gets
375 * downgraded are empty entries which don't need to be
376 * unmapped.
377 */
378 if (pmd_downgrade && dax_is_zero_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800379 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
380 PG_PMD_NR, false);
Ross Zwisler642261a2016-11-08 11:34:45 +1100381
Jan Kara0cb80b42016-12-12 21:34:12 -0500382 err = radix_tree_preload(
383 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
384 if (err) {
385 if (pmd_downgrade)
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700386 put_locked_mapping_entry(mapping, index);
Jan Kara0cb80b42016-12-12 21:34:12 -0500387 return ERR_PTR(err);
388 }
Jan Karaac401cc2016-05-12 18:29:18 +0200389 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100390
Ross Zwislere11f8b72017-04-07 16:04:57 -0700391 if (!entry) {
392 /*
393 * We needed to drop the page_tree lock while calling
394 * radix_tree_preload() and we didn't have an entry to
395 * lock. See if another thread inserted an entry at
396 * our index during this time.
397 */
398 entry = __radix_tree_lookup(&mapping->page_tree, index,
399 NULL, &slot);
400 if (entry) {
401 radix_tree_preload_end();
402 spin_unlock_irq(&mapping->tree_lock);
403 goto restart;
404 }
405 }
406
Ross Zwisler642261a2016-11-08 11:34:45 +1100407 if (pmd_downgrade) {
408 radix_tree_delete(&mapping->page_tree, index);
409 mapping->nrexceptional--;
410 dax_wake_mapping_entry_waiter(mapping, index, entry,
411 true);
412 }
413
414 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
415
416 err = __radix_tree_insert(&mapping->page_tree, index,
417 dax_radix_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200418 radix_tree_preload_end();
419 if (err) {
420 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100421 /*
Ross Zwislere11f8b72017-04-07 16:04:57 -0700422 * Our insertion of a DAX entry failed, most likely
423 * because we were inserting a PMD entry and it
424 * collided with a PTE sized entry at a different
425 * index in the PMD range. We haven't inserted
426 * anything into the radix tree and have no waiters to
427 * wake.
Ross Zwisler642261a2016-11-08 11:34:45 +1100428 */
Jan Karaac401cc2016-05-12 18:29:18 +0200429 return ERR_PTR(err);
430 }
431 /* Good, we have inserted empty locked entry into the tree. */
432 mapping->nrexceptional++;
433 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100434 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200435 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100436 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100437 out_unlock:
Jan Karaac401cc2016-05-12 18:29:18 +0200438 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100439 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200440}
441
Jan Karac6dcf522016-08-10 17:22:44 +0200442static int __dax_invalidate_mapping_entry(struct address_space *mapping,
443 pgoff_t index, bool trunc)
444{
445 int ret = 0;
446 void *entry;
447 struct radix_tree_root *page_tree = &mapping->page_tree;
448
449 spin_lock_irq(&mapping->tree_lock);
450 entry = get_unlocked_mapping_entry(mapping, index, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700451 if (!entry || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200452 goto out;
453 if (!trunc &&
454 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
455 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
456 goto out;
457 radix_tree_delete(page_tree, index);
458 mapping->nrexceptional--;
459 ret = 1;
460out:
461 put_unlocked_mapping_entry(mapping, index, entry);
462 spin_unlock_irq(&mapping->tree_lock);
463 return ret;
464}
Jan Karaac401cc2016-05-12 18:29:18 +0200465/*
466 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
467 * entry to get unlocked before deleting it.
468 */
469int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
470{
Jan Karac6dcf522016-08-10 17:22:44 +0200471 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200472
Jan Karaac401cc2016-05-12 18:29:18 +0200473 /*
474 * This gets called from truncate / punch_hole path. As such, the caller
475 * must hold locks protecting against concurrent modifications of the
476 * radix tree (usually fs-private i_mmap_sem for writing). Since the
477 * caller has seen exceptional entry for this index, we better find it
478 * at that index as well...
479 */
Jan Karac6dcf522016-08-10 17:22:44 +0200480 WARN_ON_ONCE(!ret);
481 return ret;
482}
Jan Karaac401cc2016-05-12 18:29:18 +0200483
Jan Karac6dcf522016-08-10 17:22:44 +0200484/*
Jan Karac6dcf522016-08-10 17:22:44 +0200485 * Invalidate exceptional DAX entry if it is clean.
486 */
487int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
488 pgoff_t index)
489{
490 return __dax_invalidate_mapping_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200491}
492
Dan Williamscccbce62017-01-27 13:31:42 -0800493static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
494 sector_t sector, size_t size, struct page *to,
495 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800496{
Dan Williamscccbce62017-01-27 13:31:42 -0800497 void *vto, *kaddr;
498 pgoff_t pgoff;
499 pfn_t pfn;
500 long rc;
501 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600502
Dan Williamscccbce62017-01-27 13:31:42 -0800503 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
504 if (rc)
505 return rc;
506
507 id = dax_read_lock();
508 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, &pfn);
509 if (rc < 0) {
510 dax_read_unlock(id);
511 return rc;
512 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800513 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800514 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800515 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800516 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800517 return 0;
518}
519
Ross Zwisler642261a2016-11-08 11:34:45 +1100520/*
521 * By this point grab_mapping_entry() has ensured that we have a locked entry
522 * of the appropriate size so we don't have to worry about downgrading PMDs to
523 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
524 * already in the tree, we will skip the insertion and just dirty the PMD as
525 * appropriate.
526 */
Jan Karaac401cc2016-05-12 18:29:18 +0200527static void *dax_insert_mapping_entry(struct address_space *mapping,
528 struct vm_fault *vmf,
Ross Zwisler642261a2016-11-08 11:34:45 +1100529 void *entry, sector_t sector,
Jan Karaf5b7b742017-11-01 16:36:40 +0100530 unsigned long flags, bool dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800531{
532 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200533 void *new_entry;
534 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800535
Jan Karaf5b7b742017-11-01 16:36:40 +0100536 if (dirty)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800537 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800538
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700539 if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_ZERO_PAGE)) {
540 /* we are replacing a zero page with block mapping */
541 if (dax_is_pmd_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800542 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
543 PG_PMD_NR, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700544 else /* pte entry */
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800545 unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
Ross Zwisler9973c982016-01-22 15:10:47 -0800546 }
547
Jan Karaac401cc2016-05-12 18:29:18 +0200548 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100549 new_entry = dax_radix_locked_entry(sector, flags);
550
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700551 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100552 /*
553 * Only swap our new entry into the radix tree if the current
554 * entry is a zero page or an empty entry. If a normal PTE or
555 * PMD entry is already in the tree, we leave it alone. This
556 * means that if we are trying to insert a PTE and the
557 * existing entry is a PMD, we will just leave the PMD in the
558 * tree and dirty it if necessary.
559 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800560 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200561 void **slot;
562 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800563
Johannes Weinerf7942432016-12-12 16:43:41 -0800564 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200565 WARN_ON_ONCE(ret != entry);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800566 __radix_tree_replace(page_tree, node, slot,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800567 new_entry, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700568 entry = new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800569 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700570
Jan Karaf5b7b742017-11-01 16:36:40 +0100571 if (dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800572 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700573
Ross Zwisler9973c982016-01-22 15:10:47 -0800574 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700575 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800576}
577
Jan Kara4b4bb462016-12-14 15:07:53 -0800578static inline unsigned long
579pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
580{
581 unsigned long address;
582
583 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
584 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
585 return address;
586}
587
588/* Walk all mappings of a given index of a file and writeprotect them */
589static void dax_mapping_entry_mkclean(struct address_space *mapping,
590 pgoff_t index, unsigned long pfn)
591{
592 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800593 pte_t pte, *ptep = NULL;
594 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800595 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800596
597 i_mmap_lock_read(mapping);
598 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400599 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800600
601 cond_resched();
602
603 if (!(vma->vm_flags & VM_SHARED))
604 continue;
605
606 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400607
608 /*
609 * Note because we provide start/end to follow_pte_pmd it will
610 * call mmu_notifier_invalidate_range_start() on our behalf
611 * before taking any lock.
612 */
613 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800614 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800615
Jérôme Glisse0f108512017-11-15 17:34:07 -0800616 /*
617 * No need to call mmu_notifier_invalidate_range() as we are
618 * downgrading page table protection not changing it to point
619 * to a new page.
620 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200621 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800622 */
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800623 if (pmdp) {
624#ifdef CONFIG_FS_DAX_PMD
625 pmd_t pmd;
626
627 if (pfn != pmd_pfn(*pmdp))
628 goto unlock_pmd;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800629 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800630 goto unlock_pmd;
631
632 flush_cache_page(vma, address, pfn);
633 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
634 pmd = pmd_wrprotect(pmd);
635 pmd = pmd_mkclean(pmd);
636 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800637unlock_pmd:
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800638#endif
Jan H. Schönherree190ca2018-01-31 16:14:04 -0800639 spin_unlock(ptl);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800640 } else {
641 if (pfn != pte_pfn(*ptep))
642 goto unlock_pte;
643 if (!pte_dirty(*ptep) && !pte_write(*ptep))
644 goto unlock_pte;
645
646 flush_cache_page(vma, address, pfn);
647 pte = ptep_clear_flush(vma, address, ptep);
648 pte = pte_wrprotect(pte);
649 pte = pte_mkclean(pte);
650 set_pte_at(vma->vm_mm, address, ptep, pte);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800651unlock_pte:
652 pte_unmap_unlock(ptep, ptl);
653 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800654
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400655 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800656 }
657 i_mmap_unlock_read(mapping);
658}
659
Ross Zwisler9973c982016-01-22 15:10:47 -0800660static int dax_writeback_one(struct block_device *bdev,
Dan Williamscccbce62017-01-27 13:31:42 -0800661 struct dax_device *dax_dev, struct address_space *mapping,
662 pgoff_t index, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800663{
664 struct radix_tree_root *page_tree = &mapping->page_tree;
Dan Williamscccbce62017-01-27 13:31:42 -0800665 void *entry2, **slot, *kaddr;
666 long ret = 0, id;
667 sector_t sector;
668 pgoff_t pgoff;
669 size_t size;
670 pfn_t pfn;
Ross Zwisler9973c982016-01-22 15:10:47 -0800671
Ross Zwisler9973c982016-01-22 15:10:47 -0800672 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800673 * A page got tagged dirty in DAX mapping? Something is seriously
674 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800675 */
Jan Karaa6abc2c2016-12-14 15:07:47 -0800676 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
677 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800678
Jan Karaa6abc2c2016-12-14 15:07:47 -0800679 spin_lock_irq(&mapping->tree_lock);
680 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
681 /* Entry got punched out / reallocated? */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700682 if (!entry2 || WARN_ON_ONCE(!radix_tree_exceptional_entry(entry2)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800683 goto put_unlocked;
684 /*
685 * Entry got reallocated elsewhere? No need to writeback. We have to
686 * compare sectors as we must not bail out due to difference in lockbit
687 * or entry type.
688 */
689 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
690 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +1100691 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
692 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800693 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800694 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800695 }
696
Jan Karaa6abc2c2016-12-14 15:07:47 -0800697 /* Another fsync thread may have already written back this entry */
698 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
699 goto put_unlocked;
700 /* Lock the entry to serialize with page faults */
701 entry = lock_slot(mapping, slot);
702 /*
703 * We can clear the tag now but we have to be careful so that concurrent
704 * dax_writeback_one() calls for the same index cannot finish before we
705 * actually flush the caches. This is achieved as the calls will look
706 * at the entry only under tree_lock and once they do that they will
707 * see the entry locked and wait for it to unlock.
708 */
709 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
710 spin_unlock_irq(&mapping->tree_lock);
711
Ross Zwisler642261a2016-11-08 11:34:45 +1100712 /*
713 * Even if dax_writeback_mapping_range() was given a wbc->range_start
714 * in the middle of a PMD, the 'index' we are given will be aligned to
715 * the start index of the PMD, as will the sector we pull from
716 * 'entry'. This allows us to flush for PMD_SIZE and not have to
717 * worry about partial PMD writebacks.
718 */
Dan Williamscccbce62017-01-27 13:31:42 -0800719 sector = dax_radix_sector(entry);
720 size = PAGE_SIZE << dax_radix_order(entry);
721
722 id = dax_read_lock();
723 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
724 if (ret)
725 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800726
727 /*
Dan Williamscccbce62017-01-27 13:31:42 -0800728 * dax_direct_access() may sleep, so cannot hold tree_lock over
729 * its invocation.
Ross Zwisler9973c982016-01-22 15:10:47 -0800730 */
Dan Williamscccbce62017-01-27 13:31:42 -0800731 ret = dax_direct_access(dax_dev, pgoff, size / PAGE_SIZE, &kaddr, &pfn);
732 if (ret < 0)
733 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800734
Dan Williamscccbce62017-01-27 13:31:42 -0800735 if (WARN_ON_ONCE(ret < size / PAGE_SIZE)) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800736 ret = -EIO;
Dan Williamscccbce62017-01-27 13:31:42 -0800737 goto dax_unlock;
Ross Zwisler9973c982016-01-22 15:10:47 -0800738 }
739
Dan Williamscccbce62017-01-27 13:31:42 -0800740 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(pfn));
Mikulas Patockac3ca0152017-08-31 21:47:43 -0400741 dax_flush(dax_dev, kaddr, size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800742 /*
743 * After we have flushed the cache, we can clear the dirty tag. There
744 * cannot be new dirty data in the pfn after the flush has completed as
745 * the pfn mappings are writeprotected and fault waits for mapping
746 * entry lock.
747 */
748 spin_lock_irq(&mapping->tree_lock);
749 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
750 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislerf9bc3a02017-05-08 16:00:13 -0700751 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
Dan Williamscccbce62017-01-27 13:31:42 -0800752 dax_unlock:
753 dax_read_unlock(id);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700754 put_locked_mapping_entry(mapping, index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800755 return ret;
756
Jan Karaa6abc2c2016-12-14 15:07:47 -0800757 put_unlocked:
758 put_unlocked_mapping_entry(mapping, index, entry2);
Ross Zwisler9973c982016-01-22 15:10:47 -0800759 spin_unlock_irq(&mapping->tree_lock);
760 return ret;
761}
762
763/*
764 * Flush the mapping to the persistent domain within the byte range of [start,
765 * end]. This is required by data integrity operations to ensure file data is
766 * on persistent storage prior to completion of the operation.
767 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800768int dax_writeback_mapping_range(struct address_space *mapping,
769 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800770{
771 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +1100772 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -0800773 pgoff_t indices[PAGEVEC_SIZE];
Dan Williamscccbce62017-01-27 13:31:42 -0800774 struct dax_device *dax_dev;
Ross Zwisler9973c982016-01-22 15:10:47 -0800775 struct pagevec pvec;
776 bool done = false;
777 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800778
779 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
780 return -EIO;
781
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800782 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
783 return 0;
784
Dan Williamscccbce62017-01-27 13:31:42 -0800785 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
786 if (!dax_dev)
787 return -EIO;
788
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300789 start_index = wbc->range_start >> PAGE_SHIFT;
790 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800791
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700792 trace_dax_writeback_range(inode, start_index, end_index);
793
Ross Zwisler9973c982016-01-22 15:10:47 -0800794 tag_pages_for_writeback(mapping, start_index, end_index);
795
Mel Gorman86679822017-11-15 17:37:52 -0800796 pagevec_init(&pvec);
Ross Zwisler9973c982016-01-22 15:10:47 -0800797 while (!done) {
798 pvec.nr = find_get_entries_tag(mapping, start_index,
799 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
800 pvec.pages, indices);
801
802 if (pvec.nr == 0)
803 break;
804
805 for (i = 0; i < pvec.nr; i++) {
806 if (indices[i] > end_index) {
807 done = true;
808 break;
809 }
810
Dan Williamscccbce62017-01-27 13:31:42 -0800811 ret = dax_writeback_one(bdev, dax_dev, mapping,
812 indices[i], pvec.pages[i]);
Jeff Layton819ec6b2017-07-06 07:02:27 -0400813 if (ret < 0) {
814 mapping_set_error(mapping, ret);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700815 goto out;
Jeff Layton819ec6b2017-07-06 07:02:27 -0400816 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800817 }
Jan Kara1eb643d2017-06-23 15:08:46 -0700818 start_index = indices[pvec.nr - 1] + 1;
Ross Zwisler9973c982016-01-22 15:10:47 -0800819 }
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700820out:
Dan Williamscccbce62017-01-27 13:31:42 -0800821 put_dax(dax_dev);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700822 trace_dax_writeback_range_done(inode, start_index, end_index);
823 return (ret < 0 ? ret : 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800824}
825EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
826
Jan Kara31a6f1a2017-11-01 16:36:32 +0100827static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800828{
Linus Torvaldsa3841f92017-11-17 09:51:57 -0800829 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
Jan Kara31a6f1a2017-11-01 16:36:32 +0100830}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800831
Jan Kara5e161e42017-11-01 16:36:33 +0100832static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
833 pfn_t *pfnp)
834{
835 const sector_t sector = dax_iomap_sector(iomap, pos);
836 pgoff_t pgoff;
837 void *kaddr;
838 int id, rc;
839 long length;
840
841 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800842 if (rc)
843 return rc;
Dan Williamscccbce62017-01-27 13:31:42 -0800844 id = dax_read_lock();
Jan Kara5e161e42017-11-01 16:36:33 +0100845 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
846 &kaddr, pfnp);
847 if (length < 0) {
848 rc = length;
849 goto out;
Dan Williamscccbce62017-01-27 13:31:42 -0800850 }
Jan Kara5e161e42017-11-01 16:36:33 +0100851 rc = -EINVAL;
852 if (PFN_PHYS(length) < size)
853 goto out;
854 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
855 goto out;
856 /* For larger pages we need devmap */
857 if (length > 1 && !pfn_t_devmap(*pfnp))
858 goto out;
859 rc = 0;
860out:
Dan Williamscccbce62017-01-27 13:31:42 -0800861 dax_read_unlock(id);
Jan Kara5e161e42017-11-01 16:36:33 +0100862 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800863}
864
Ross Zwislere30331f2017-09-06 16:18:39 -0700865/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700866 * The user has performed a load from a hole in the file. Allocating a new
867 * page in the file would cause excessive storage usage for workloads with
868 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
869 * If this page is ever written to we will re-fault and change the mapping to
870 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -0700871 */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700872static int dax_load_hole(struct address_space *mapping, void *entry,
Ross Zwislere30331f2017-09-06 16:18:39 -0700873 struct vm_fault *vmf)
874{
875 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700876 unsigned long vaddr = vmf->address;
877 int ret = VM_FAULT_NOPAGE;
878 struct page *zero_page;
879 void *entry2;
Ross Zwislere30331f2017-09-06 16:18:39 -0700880
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700881 zero_page = ZERO_PAGE(0);
882 if (unlikely(!zero_page)) {
Ross Zwislere30331f2017-09-06 16:18:39 -0700883 ret = VM_FAULT_OOM;
884 goto out;
885 }
886
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700887 entry2 = dax_insert_mapping_entry(mapping, vmf, entry, 0,
Jan Karaf5b7b742017-11-01 16:36:40 +0100888 RADIX_DAX_ZERO_PAGE, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700889 if (IS_ERR(entry2)) {
890 ret = VM_FAULT_SIGBUS;
891 goto out;
Ross Zwislere30331f2017-09-06 16:18:39 -0700892 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700893
894 vm_insert_mixed(vmf->vma, vaddr, page_to_pfn_t(zero_page));
Ross Zwislere30331f2017-09-06 16:18:39 -0700895out:
896 trace_dax_load_hole(inode, vmf, ret);
897 return ret;
898}
899
Vishal Verma4b0228f2016-04-21 15:13:46 -0400900static bool dax_range_is_aligned(struct block_device *bdev,
901 unsigned int offset, unsigned int length)
902{
903 unsigned short sector_size = bdev_logical_block_size(bdev);
904
905 if (!IS_ALIGNED(offset, sector_size))
906 return false;
907 if (!IS_ALIGNED(length, sector_size))
908 return false;
909
910 return true;
911}
912
Dan Williamscccbce62017-01-27 13:31:42 -0800913int __dax_zero_page_range(struct block_device *bdev,
914 struct dax_device *dax_dev, sector_t sector,
915 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200916{
Dan Williamscccbce62017-01-27 13:31:42 -0800917 if (dax_range_is_aligned(bdev, offset, size)) {
918 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400919
920 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -0700921 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400922 } else {
Dan Williamscccbce62017-01-27 13:31:42 -0800923 pgoff_t pgoff;
924 long rc, id;
925 void *kaddr;
926 pfn_t pfn;
927
Dan Williamse84b83b2017-05-10 19:38:13 -0700928 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800929 if (rc)
930 return rc;
931
932 id = dax_read_lock();
Dan Williamse84b83b2017-05-10 19:38:13 -0700933 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr,
Dan Williamscccbce62017-01-27 13:31:42 -0800934 &pfn);
935 if (rc < 0) {
936 dax_read_unlock(id);
937 return rc;
938 }
Dan Williams81f55872017-05-29 13:12:20 -0700939 memset(kaddr + offset, 0, size);
Mikulas Patockac3ca0152017-08-31 21:47:43 -0400940 dax_flush(dax_dev, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -0800941 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400942 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200943 return 0;
944}
945EXPORT_SYMBOL_GPL(__dax_zero_page_range);
946
Christoph Hellwiga254e562016-09-19 11:24:49 +1000947static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100948dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000949 struct iomap *iomap)
950{
Dan Williamscccbce62017-01-27 13:31:42 -0800951 struct block_device *bdev = iomap->bdev;
952 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000953 struct iov_iter *iter = data;
954 loff_t end = pos + length, done = 0;
955 ssize_t ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800956 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000957
958 if (iov_iter_rw(iter) == READ) {
959 end = min(end, i_size_read(inode));
960 if (pos >= end)
961 return 0;
962
963 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
964 return iov_iter_zero(min(length, end - pos), iter);
965 }
966
967 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
968 return -EIO;
969
Jan Karae3fce682016-08-10 17:10:28 +0200970 /*
971 * Write can allocate block for an area which has a hole page mapped
972 * into page tables. We have to tear down these mappings so that data
973 * written by write(2) is visible in mmap.
974 */
Jan Karacd656372017-05-12 15:46:50 -0700975 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +0200976 invalidate_inode_pages2_range(inode->i_mapping,
977 pos >> PAGE_SHIFT,
978 (end - 1) >> PAGE_SHIFT);
979 }
980
Dan Williamscccbce62017-01-27 13:31:42 -0800981 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +1000982 while (pos < end) {
983 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -0800984 const size_t size = ALIGN(length + offset, PAGE_SIZE);
985 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000986 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -0800987 pgoff_t pgoff;
988 void *kaddr;
989 pfn_t pfn;
Christoph Hellwiga254e562016-09-19 11:24:49 +1000990
Michal Hockod1908f52017-02-03 13:13:26 -0800991 if (fatal_signal_pending(current)) {
992 ret = -EINTR;
993 break;
994 }
995
Dan Williamscccbce62017-01-27 13:31:42 -0800996 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
997 if (ret)
998 break;
999
1000 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
1001 &kaddr, &pfn);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001002 if (map_len < 0) {
1003 ret = map_len;
1004 break;
1005 }
1006
Dan Williamscccbce62017-01-27 13:31:42 -08001007 map_len = PFN_PHYS(map_len);
1008 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001009 map_len -= offset;
1010 if (map_len > end - pos)
1011 map_len = end - pos;
1012
Ross Zwislera2e050f2017-09-06 16:18:54 -07001013 /*
1014 * The userspace address for the memory copy has already been
1015 * validated via access_ok() in either vfs_read() or
1016 * vfs_write(), depending on which operation we are doing.
1017 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001018 if (iov_iter_rw(iter) == WRITE)
Dan Williamsfec53772017-05-29 21:56:49 -07001019 map_len = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1020 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001021 else
Dan Williamscccbce62017-01-27 13:31:42 -08001022 map_len = copy_to_iter(kaddr, map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001023 if (map_len <= 0) {
1024 ret = map_len ? map_len : -EFAULT;
1025 break;
1026 }
1027
1028 pos += map_len;
1029 length -= map_len;
1030 done += map_len;
1031 }
Dan Williamscccbce62017-01-27 13:31:42 -08001032 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001033
1034 return done ? done : ret;
1035}
1036
1037/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001038 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001039 * @iocb: The control block for this I/O
1040 * @iter: The addresses to do I/O from or to
1041 * @ops: iomap ops passed from the file system
1042 *
1043 * This function performs read and write operations to directly mapped
1044 * persistent memory. The callers needs to take care of read/write exclusion
1045 * and evicting any page cache pages in the region under I/O.
1046 */
1047ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001048dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001049 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001050{
1051 struct address_space *mapping = iocb->ki_filp->f_mapping;
1052 struct inode *inode = mapping->host;
1053 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1054 unsigned flags = 0;
1055
Christoph Hellwig168316d2017-02-08 14:43:13 -05001056 if (iov_iter_rw(iter) == WRITE) {
1057 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001058 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001059 } else {
1060 lockdep_assert_held(&inode->i_rwsem);
1061 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001062
Christoph Hellwiga254e562016-09-19 11:24:49 +10001063 while (iov_iter_count(iter)) {
1064 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001065 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001066 if (ret <= 0)
1067 break;
1068 pos += ret;
1069 done += ret;
1070 }
1071
1072 iocb->ki_pos += done;
1073 return done ? done : ret;
1074}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001075EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001076
Jan Kara9f141d62016-10-19 14:34:31 +02001077static int dax_fault_return(int error)
1078{
1079 if (error == 0)
1080 return VM_FAULT_NOPAGE;
1081 if (error == -ENOMEM)
1082 return VM_FAULT_OOM;
1083 return VM_FAULT_SIGBUS;
1084}
1085
Dan Williamsaaa422c2017-11-13 16:38:44 -08001086/*
1087 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1088 * flushed on write-faults (non-cow), but not read-faults.
1089 */
1090static bool dax_fault_is_synchronous(unsigned long flags,
1091 struct vm_area_struct *vma, struct iomap *iomap)
1092{
1093 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1094 && (iomap->flags & IOMAP_F_DIRTY);
1095}
1096
Jan Kara9a0dd422017-11-01 16:36:39 +01001097static int dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
Jan Karac0b24622018-01-07 16:38:43 -05001098 int *iomap_errp, const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001099{
Jan Karaa0987ad2017-11-01 16:36:34 +01001100 struct vm_area_struct *vma = vmf->vma;
1101 struct address_space *mapping = vma->vm_file->f_mapping;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001102 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001103 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001104 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001105 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001106 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001107 int error, major = 0;
Jan Karad2c43ef2017-11-01 16:36:35 +01001108 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001109 bool sync;
Jan Karab1aa8122016-12-14 15:07:24 -08001110 int vmf_ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001111 void *entry;
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001112 pfn_t pfn;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001113
Ross Zwislera9c42b32017-05-08 16:00:00 -07001114 trace_dax_pte_fault(inode, vmf, vmf_ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001115 /*
1116 * Check whether offset isn't beyond end of file now. Caller is supposed
1117 * to hold locks serializing us with truncate / punch hole so this is
1118 * a reliable test.
1119 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001120 if (pos >= i_size_read(inode)) {
1121 vmf_ret = VM_FAULT_SIGBUS;
1122 goto out;
1123 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001124
Jan Karad2c43ef2017-11-01 16:36:35 +01001125 if (write && !vmf->cow_page)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001126 flags |= IOMAP_WRITE;
1127
Jan Kara13e451f2017-05-12 15:46:57 -07001128 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1129 if (IS_ERR(entry)) {
1130 vmf_ret = dax_fault_return(PTR_ERR(entry));
1131 goto out;
1132 }
1133
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001134 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001135 * It is possible, particularly with mixed reads & writes to private
1136 * mappings, that we have raced with a PMD fault that overlaps with
1137 * the PTE we need to set up. If so just return and the fault will be
1138 * retried.
1139 */
1140 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1141 vmf_ret = VM_FAULT_NOPAGE;
1142 goto unlock_entry;
1143 }
1144
1145 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001146 * Note that we don't bother to use iomap_apply here: DAX required
1147 * the file system block size to be equal the page size, which means
1148 * that we never have to deal with more than a single extent here.
1149 */
1150 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Jan Karac0b24622018-01-07 16:38:43 -05001151 if (iomap_errp)
1152 *iomap_errp = error;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001153 if (error) {
1154 vmf_ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001155 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001156 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001157 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001158 error = -EIO; /* fs corruption? */
1159 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001160 }
1161
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001162 if (vmf->cow_page) {
Jan Kara31a6f1a2017-11-01 16:36:32 +01001163 sector_t sector = dax_iomap_sector(&iomap, pos);
1164
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001165 switch (iomap.type) {
1166 case IOMAP_HOLE:
1167 case IOMAP_UNWRITTEN:
1168 clear_user_highpage(vmf->cow_page, vaddr);
1169 break;
1170 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001171 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1172 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001173 break;
1174 default:
1175 WARN_ON_ONCE(1);
1176 error = -EIO;
1177 break;
1178 }
1179
1180 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001181 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001182
1183 __SetPageUptodate(vmf->cow_page);
1184 vmf_ret = finish_fault(vmf);
1185 if (!vmf_ret)
1186 vmf_ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001187 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001188 }
1189
Dan Williamsaaa422c2017-11-13 16:38:44 -08001190 sync = dax_fault_is_synchronous(flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001191
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001192 switch (iomap.type) {
1193 case IOMAP_MAPPED:
1194 if (iomap.flags & IOMAP_F_NEW) {
1195 count_vm_event(PGMAJFAULT);
Jan Karaa0987ad2017-11-01 16:36:34 +01001196 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001197 major = VM_FAULT_MAJOR;
1198 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001199 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1200 if (error < 0)
1201 goto error_finish_iomap;
1202
1203 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1204 dax_iomap_sector(&iomap, pos),
Jan Karacaa51d22017-11-01 16:36:42 +01001205 0, write && !sync);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001206 if (IS_ERR(entry)) {
1207 error = PTR_ERR(entry);
1208 goto error_finish_iomap;
1209 }
1210
Jan Karacaa51d22017-11-01 16:36:42 +01001211 /*
1212 * If we are doing synchronous page fault and inode needs fsync,
1213 * we can insert PTE into page tables only after that happens.
1214 * Skip insertion for now and return the pfn so that caller can
1215 * insert it after fsync is done.
1216 */
1217 if (sync) {
1218 if (WARN_ON_ONCE(!pfnp)) {
1219 error = -EIO;
1220 goto error_finish_iomap;
1221 }
1222 *pfnp = pfn;
1223 vmf_ret = VM_FAULT_NEEDDSYNC | major;
1224 goto finish_iomap;
1225 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001226 trace_dax_insert_mapping(inode, vmf, entry);
1227 if (write)
1228 error = vm_insert_mixed_mkwrite(vma, vaddr, pfn);
1229 else
1230 error = vm_insert_mixed(vma, vaddr, pfn);
1231
Jan Kara9f141d62016-10-19 14:34:31 +02001232 /* -EBUSY is fine, somebody else faulted on the same PTE */
1233 if (error == -EBUSY)
1234 error = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001235 break;
1236 case IOMAP_UNWRITTEN:
1237 case IOMAP_HOLE:
Jan Karad2c43ef2017-11-01 16:36:35 +01001238 if (!write) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001239 vmf_ret = dax_load_hole(mapping, entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001240 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001241 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001242 /*FALLTHRU*/
1243 default:
1244 WARN_ON_ONCE(1);
1245 error = -EIO;
1246 break;
1247 }
1248
Jan Kara13e451f2017-05-12 15:46:57 -07001249 error_finish_iomap:
Jan Kara9f141d62016-10-19 14:34:31 +02001250 vmf_ret = dax_fault_return(error) | major;
Jan Kara9f141d62016-10-19 14:34:31 +02001251 finish_iomap:
1252 if (ops->iomap_end) {
1253 int copied = PAGE_SIZE;
1254
1255 if (vmf_ret & VM_FAULT_ERROR)
1256 copied = 0;
1257 /*
1258 * The fault is done by now and there's no way back (other
1259 * thread may be already happily using PTE we have installed).
1260 * Just ignore error from ->iomap_end since we cannot do much
1261 * with it.
1262 */
1263 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001264 }
Jan Kara13e451f2017-05-12 15:46:57 -07001265 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001266 put_locked_mapping_entry(mapping, vmf->pgoff);
Jan Kara13e451f2017-05-12 15:46:57 -07001267 out:
Ross Zwislera9c42b32017-05-08 16:00:00 -07001268 trace_dax_pte_fault_done(inode, vmf, vmf_ret);
Jan Kara9f141d62016-10-19 14:34:31 +02001269 return vmf_ret;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001270}
Ross Zwisler642261a2016-11-08 11:34:45 +11001271
1272#ifdef CONFIG_FS_DAX_PMD
Dave Jiangf4200392017-02-22 15:40:06 -08001273static int dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001274 void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001275{
Dave Jiangf4200392017-02-22 15:40:06 -08001276 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1277 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001278 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001279 struct page *zero_page;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001280 void *ret = NULL;
Ross Zwisler642261a2016-11-08 11:34:45 +11001281 spinlock_t *ptl;
1282 pmd_t pmd_entry;
Ross Zwisler642261a2016-11-08 11:34:45 +11001283
Dave Jiangf4200392017-02-22 15:40:06 -08001284 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001285
1286 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001287 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001288
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001289 ret = dax_insert_mapping_entry(mapping, vmf, entry, 0,
Jan Karaf5b7b742017-11-01 16:36:40 +01001290 RADIX_DAX_PMD | RADIX_DAX_ZERO_PAGE, false);
Ross Zwisler642261a2016-11-08 11:34:45 +11001291 if (IS_ERR(ret))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001292 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001293
Dave Jiangf4200392017-02-22 15:40:06 -08001294 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1295 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001296 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001297 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001298 }
1299
Dave Jiangf4200392017-02-22 15:40:06 -08001300 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001301 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001302 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001303 spin_unlock(ptl);
Dave Jiangf4200392017-02-22 15:40:06 -08001304 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001305 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001306
1307fallback:
Dave Jiangf4200392017-02-22 15:40:06 -08001308 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001309 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001310}
1311
Jan Kara9a0dd422017-11-01 16:36:39 +01001312static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Dave Jianga2d58162017-02-24 14:56:59 -08001313 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001314{
Dave Jiangf4200392017-02-22 15:40:06 -08001315 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001316 struct address_space *mapping = vma->vm_file->f_mapping;
Dave Jiangd8a849e2017-02-22 15:40:03 -08001317 unsigned long pmd_addr = vmf->address & PMD_MASK;
1318 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001319 bool sync;
Jan Kara9484ab12016-11-10 10:26:50 +11001320 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001321 struct inode *inode = mapping->host;
1322 int result = VM_FAULT_FALLBACK;
1323 struct iomap iomap = { 0 };
1324 pgoff_t max_pgoff, pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001325 void *entry;
1326 loff_t pos;
1327 int error;
Jan Kara302a5e32017-11-01 16:36:37 +01001328 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001329
Ross Zwisler282a8e02017-02-22 15:39:50 -08001330 /*
1331 * Check whether offset isn't beyond end of file now. Caller is
1332 * supposed to hold locks serializing us with truncate / punch hole so
1333 * this is a reliable test.
1334 */
1335 pgoff = linear_page_index(vma, pmd_addr);
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001336 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001337
Dave Jiangf4200392017-02-22 15:40:06 -08001338 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001339
Ross Zwislerfffa2812017-08-25 15:55:36 -07001340 /*
1341 * Make sure that the faulting address's PMD offset (color) matches
1342 * the PMD offset from the start of the file. This is necessary so
1343 * that a PMD range in the page table overlaps exactly with a PMD
1344 * range in the radix tree.
1345 */
1346 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1347 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1348 goto fallback;
1349
Ross Zwisler642261a2016-11-08 11:34:45 +11001350 /* Fall back to PTEs if we're going to COW */
1351 if (write && !(vma->vm_flags & VM_SHARED))
1352 goto fallback;
1353
1354 /* If the PMD would extend outside the VMA */
1355 if (pmd_addr < vma->vm_start)
1356 goto fallback;
1357 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1358 goto fallback;
1359
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001360 if (pgoff >= max_pgoff) {
Ross Zwisler282a8e02017-02-22 15:39:50 -08001361 result = VM_FAULT_SIGBUS;
1362 goto out;
1363 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001364
1365 /* If the PMD would extend beyond the file size */
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001366 if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
Ross Zwisler642261a2016-11-08 11:34:45 +11001367 goto fallback;
1368
1369 /*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001370 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1371 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1372 * is already in the tree, for instance), it will return -EEXIST and
1373 * we just fall back to 4k entries.
Jan Kara9f141d62016-10-19 14:34:31 +02001374 */
1375 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1376 if (IS_ERR(entry))
Ross Zwisler876f2942017-05-12 15:47:00 -07001377 goto fallback;
1378
1379 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001380 * It is possible, particularly with mixed reads & writes to private
1381 * mappings, that we have raced with a PTE fault that overlaps with
1382 * the PMD we need to set up. If so just return and the fault will be
1383 * retried.
1384 */
1385 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1386 !pmd_devmap(*vmf->pmd)) {
1387 result = 0;
1388 goto unlock_entry;
1389 }
1390
1391 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001392 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1393 * setting up a mapping, so really we're using iomap_begin() as a way
1394 * to look up our filesystem block.
1395 */
1396 pos = (loff_t)pgoff << PAGE_SHIFT;
1397 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1398 if (error)
1399 goto unlock_entry;
1400
1401 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001402 goto finish_iomap;
1403
Dan Williamsaaa422c2017-11-13 16:38:44 -08001404 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001405
Ross Zwisler642261a2016-11-08 11:34:45 +11001406 switch (iomap.type) {
1407 case IOMAP_MAPPED:
Jan Kara302a5e32017-11-01 16:36:37 +01001408 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1409 if (error < 0)
1410 goto finish_iomap;
1411
1412 entry = dax_insert_mapping_entry(mapping, vmf, entry,
1413 dax_iomap_sector(&iomap, pos),
Jan Karacaa51d22017-11-01 16:36:42 +01001414 RADIX_DAX_PMD, write && !sync);
Jan Kara302a5e32017-11-01 16:36:37 +01001415 if (IS_ERR(entry))
1416 goto finish_iomap;
1417
Jan Karacaa51d22017-11-01 16:36:42 +01001418 /*
1419 * If we are doing synchronous page fault and inode needs fsync,
1420 * we can insert PMD into page tables only after that happens.
1421 * Skip insertion for now and return the pfn so that caller can
1422 * insert it after fsync is done.
1423 */
1424 if (sync) {
1425 if (WARN_ON_ONCE(!pfnp))
1426 goto finish_iomap;
1427 *pfnp = pfn;
1428 result = VM_FAULT_NEEDDSYNC;
1429 goto finish_iomap;
1430 }
1431
Jan Kara302a5e32017-11-01 16:36:37 +01001432 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1433 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1434 write);
Ross Zwisler642261a2016-11-08 11:34:45 +11001435 break;
1436 case IOMAP_UNWRITTEN:
1437 case IOMAP_HOLE:
1438 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001439 break;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001440 result = dax_pmd_load_hole(vmf, &iomap, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001441 break;
1442 default:
1443 WARN_ON_ONCE(1);
1444 break;
1445 }
1446
Jan Kara9f141d62016-10-19 14:34:31 +02001447 finish_iomap:
1448 if (ops->iomap_end) {
1449 int copied = PMD_SIZE;
1450
1451 if (result == VM_FAULT_FALLBACK)
1452 copied = 0;
1453 /*
1454 * The fault is done by now and there's no way back (other
1455 * thread may be already happily using PMD we have installed).
1456 * Just ignore error from ->iomap_end since we cannot do much
1457 * with it.
1458 */
1459 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1460 &iomap);
1461 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001462 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001463 put_locked_mapping_entry(mapping, pgoff);
Ross Zwisler642261a2016-11-08 11:34:45 +11001464 fallback:
1465 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001466 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001467 count_vm_event(THP_FAULT_FALLBACK);
1468 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001469out:
Dave Jiangf4200392017-02-22 15:40:06 -08001470 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001471 return result;
1472}
Dave Jianga2d58162017-02-24 14:56:59 -08001473#else
Jan Kara9a0dd422017-11-01 16:36:39 +01001474static int dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001475 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001476{
1477 return VM_FAULT_FALLBACK;
1478}
Ross Zwisler642261a2016-11-08 11:34:45 +11001479#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001480
1481/**
1482 * dax_iomap_fault - handle a page fault on a DAX file
1483 * @vmf: The description of the fault
Jan Karacec04e82017-11-01 16:36:38 +01001484 * @pe_size: Size of the page to fault in
Jan Kara9a0dd422017-11-01 16:36:39 +01001485 * @pfnp: PFN to insert for synchronous faults if fsync is required
Jan Karac0b24622018-01-07 16:38:43 -05001486 * @iomap_errp: Storage for detailed error code in case of error
Jan Karacec04e82017-11-01 16:36:38 +01001487 * @ops: Iomap ops passed from the file system
Dave Jianga2d58162017-02-24 14:56:59 -08001488 *
1489 * When a page fault occurs, filesystems may call this helper in
1490 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1491 * has done all the necessary locking for page fault to proceed
1492 * successfully.
1493 */
Dave Jiangc791ace2017-02-24 14:57:08 -08001494int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
Jan Karac0b24622018-01-07 16:38:43 -05001495 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001496{
Dave Jiangc791ace2017-02-24 14:57:08 -08001497 switch (pe_size) {
1498 case PE_SIZE_PTE:
Jan Karac0b24622018-01-07 16:38:43 -05001499 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001500 case PE_SIZE_PMD:
Jan Kara9a0dd422017-11-01 16:36:39 +01001501 return dax_iomap_pmd_fault(vmf, pfnp, ops);
Dave Jianga2d58162017-02-24 14:56:59 -08001502 default:
1503 return VM_FAULT_FALLBACK;
1504 }
1505}
1506EXPORT_SYMBOL_GPL(dax_iomap_fault);
Jan Kara71eab6d2017-11-01 16:36:43 +01001507
1508/**
1509 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1510 * @vmf: The description of the fault
1511 * @pe_size: Size of entry to be inserted
1512 * @pfn: PFN to insert
1513 *
1514 * This function inserts writeable PTE or PMD entry into page tables for mmaped
1515 * DAX file. It takes care of marking corresponding radix tree entry as dirty
1516 * as well.
1517 */
1518static int dax_insert_pfn_mkwrite(struct vm_fault *vmf,
1519 enum page_entry_size pe_size,
1520 pfn_t pfn)
1521{
1522 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1523 void *entry, **slot;
1524 pgoff_t index = vmf->pgoff;
1525 int vmf_ret, error;
1526
1527 spin_lock_irq(&mapping->tree_lock);
1528 entry = get_unlocked_mapping_entry(mapping, index, &slot);
1529 /* Did we race with someone splitting entry or so? */
1530 if (!entry ||
1531 (pe_size == PE_SIZE_PTE && !dax_is_pte_entry(entry)) ||
1532 (pe_size == PE_SIZE_PMD && !dax_is_pmd_entry(entry))) {
1533 put_unlocked_mapping_entry(mapping, index, entry);
1534 spin_unlock_irq(&mapping->tree_lock);
1535 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1536 VM_FAULT_NOPAGE);
1537 return VM_FAULT_NOPAGE;
1538 }
1539 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
1540 entry = lock_slot(mapping, slot);
1541 spin_unlock_irq(&mapping->tree_lock);
1542 switch (pe_size) {
1543 case PE_SIZE_PTE:
1544 error = vm_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
1545 vmf_ret = dax_fault_return(error);
1546 break;
1547#ifdef CONFIG_FS_DAX_PMD
1548 case PE_SIZE_PMD:
1549 vmf_ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
1550 pfn, true);
1551 break;
1552#endif
1553 default:
1554 vmf_ret = VM_FAULT_FALLBACK;
1555 }
1556 put_locked_mapping_entry(mapping, index);
1557 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, vmf_ret);
1558 return vmf_ret;
1559}
1560
1561/**
1562 * dax_finish_sync_fault - finish synchronous page fault
1563 * @vmf: The description of the fault
1564 * @pe_size: Size of entry to be inserted
1565 * @pfn: PFN to insert
1566 *
1567 * This function ensures that the file range touched by the page fault is
1568 * stored persistently on the media and handles inserting of appropriate page
1569 * table entry.
1570 */
1571int dax_finish_sync_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1572 pfn_t pfn)
1573{
1574 int err;
1575 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1576 size_t len = 0;
1577
1578 if (pe_size == PE_SIZE_PTE)
1579 len = PAGE_SIZE;
1580 else if (pe_size == PE_SIZE_PMD)
1581 len = PMD_SIZE;
1582 else
1583 WARN_ON_ONCE(1);
1584 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1585 if (err)
1586 return VM_FAULT_SIGBUS;
1587 return dax_insert_pfn_mkwrite(vmf, pe_size, pfn);
1588}
1589EXPORT_SYMBOL_GPL(dax_finish_sync_fault);