blob: bcfedd1848603eae584bc969d75aa8f94b290248 [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>
Ross Zwisler2765cfb2015-08-18 13:55:40 -060028#include <linux/pmem.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080029#include <linux/sched.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
Jan Karaac401cc2016-05-12 18:29:18 +020038/* We choose 4096 entries - same as per-zone page wait tables */
39#define DAX_WAIT_TABLE_BITS 12
40#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
41
Ross Zwislerce95ab02016-11-08 11:31:44 +110042static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020043
44static int __init init_dax_wait_table(void)
45{
46 int i;
47
48 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
49 init_waitqueue_head(wait_table + i);
50 return 0;
51}
52fs_initcall(init_dax_wait_table);
53
Dan Williamsb2e0d162016-01-15 16:55:59 -080054static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
55{
56 struct request_queue *q = bdev->bd_queue;
57 long rc = -EIO;
58
Dan Williams7a9eb202016-06-03 18:06:47 -070059 dax->addr = ERR_PTR(-EIO);
Dan Williamsb2e0d162016-01-15 16:55:59 -080060 if (blk_queue_enter(q, true) != 0)
61 return rc;
62
63 rc = bdev_direct_access(bdev, dax);
64 if (rc < 0) {
Dan Williams7a9eb202016-06-03 18:06:47 -070065 dax->addr = ERR_PTR(rc);
Dan Williamsb2e0d162016-01-15 16:55:59 -080066 blk_queue_exit(q);
67 return rc;
68 }
69 return rc;
70}
71
72static void dax_unmap_atomic(struct block_device *bdev,
73 const struct blk_dax_ctl *dax)
74{
75 if (IS_ERR(dax->addr))
76 return;
77 blk_queue_exit(bdev->bd_queue);
78}
79
Ross Zwisler642261a2016-11-08 11:34:45 +110080static int dax_is_pmd_entry(void *entry)
81{
82 return (unsigned long)entry & RADIX_DAX_PMD;
83}
84
85static int dax_is_pte_entry(void *entry)
86{
87 return !((unsigned long)entry & RADIX_DAX_PMD);
88}
89
90static int dax_is_zero_entry(void *entry)
91{
92 return (unsigned long)entry & RADIX_DAX_HZP;
93}
94
95static int dax_is_empty_entry(void *entry)
96{
97 return (unsigned long)entry & RADIX_DAX_EMPTY;
98}
99
Dan Williamsd1a5f2b42016-01-28 20:25:31 -0800100struct page *read_dax_sector(struct block_device *bdev, sector_t n)
101{
102 struct page *page = alloc_pages(GFP_KERNEL, 0);
103 struct blk_dax_ctl dax = {
104 .size = PAGE_SIZE,
105 .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
106 };
107 long rc;
108
109 if (!page)
110 return ERR_PTR(-ENOMEM);
111
112 rc = dax_map_atomic(bdev, &dax);
113 if (rc < 0)
114 return ERR_PTR(rc);
115 memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
116 dax_unmap_atomic(bdev, &dax);
117 return page;
118}
119
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800120/*
Jan Karaac401cc2016-05-12 18:29:18 +0200121 * DAX radix tree locking
122 */
123struct exceptional_entry_key {
124 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100125 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200126};
127
128struct wait_exceptional_entry_queue {
129 wait_queue_t wait;
130 struct exceptional_entry_key key;
131};
132
Ross Zwisler63e95b52016-11-08 11:32:20 +1100133static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
134 pgoff_t index, void *entry, struct exceptional_entry_key *key)
135{
136 unsigned long hash;
137
138 /*
139 * If 'entry' is a PMD, align the 'index' that we use for the wait
140 * queue to the start of that PMD. This ensures that all offsets in
141 * the range covered by the PMD map to the same bit lock.
142 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100143 if (dax_is_pmd_entry(entry))
Ross Zwisler63e95b52016-11-08 11:32:20 +1100144 index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
145
146 key->mapping = mapping;
147 key->entry_start = index;
148
149 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
150 return wait_table + hash;
151}
152
Jan Karaac401cc2016-05-12 18:29:18 +0200153static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
154 int sync, void *keyp)
155{
156 struct exceptional_entry_key *key = keyp;
157 struct wait_exceptional_entry_queue *ewait =
158 container_of(wait, struct wait_exceptional_entry_queue, wait);
159
160 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100161 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200162 return 0;
163 return autoremove_wake_function(wait, mode, sync, NULL);
164}
165
166/*
167 * Check whether the given slot is locked. The function must be called with
168 * mapping->tree_lock held
169 */
170static inline int slot_locked(struct address_space *mapping, void **slot)
171{
172 unsigned long entry = (unsigned long)
173 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
174 return entry & RADIX_DAX_ENTRY_LOCK;
175}
176
177/*
178 * Mark the given slot is locked. The function must be called with
179 * mapping->tree_lock held
180 */
181static inline void *lock_slot(struct address_space *mapping, void **slot)
182{
183 unsigned long entry = (unsigned long)
184 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
185
186 entry |= RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800187 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200188 return (void *)entry;
189}
190
191/*
192 * Mark the given slot is unlocked. The function must be called with
193 * mapping->tree_lock held
194 */
195static inline void *unlock_slot(struct address_space *mapping, void **slot)
196{
197 unsigned long entry = (unsigned long)
198 radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
199
200 entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
Johannes Weiner6d75f362016-12-12 16:43:43 -0800201 radix_tree_replace_slot(&mapping->page_tree, slot, (void *)entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200202 return (void *)entry;
203}
204
205/*
206 * Lookup entry in radix tree, wait for it to become unlocked if it is
207 * exceptional entry and return it. The caller must call
208 * put_unlocked_mapping_entry() when he decided not to lock the entry or
209 * put_locked_mapping_entry() when he locked the entry and now wants to
210 * unlock it.
211 *
212 * The function must be called with mapping->tree_lock held.
213 */
214static void *get_unlocked_mapping_entry(struct address_space *mapping,
215 pgoff_t index, void ***slotp)
216{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100217 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200218 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100219 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200220
221 init_wait(&ewait.wait);
222 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200223
224 for (;;) {
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100225 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200226 &slot);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100227 if (!entry || !radix_tree_exceptional_entry(entry) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200228 !slot_locked(mapping, slot)) {
229 if (slotp)
230 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100231 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200232 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100233
234 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200235 prepare_to_wait_exclusive(wq, &ewait.wait,
236 TASK_UNINTERRUPTIBLE);
237 spin_unlock_irq(&mapping->tree_lock);
238 schedule();
239 finish_wait(wq, &ewait.wait);
240 spin_lock_irq(&mapping->tree_lock);
241 }
242}
243
Jan Karab1aa8122016-12-14 15:07:24 -0800244static void dax_unlock_mapping_entry(struct address_space *mapping,
245 pgoff_t index)
246{
247 void *entry, **slot;
248
249 spin_lock_irq(&mapping->tree_lock);
250 entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
251 if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
252 !slot_locked(mapping, slot))) {
253 spin_unlock_irq(&mapping->tree_lock);
254 return;
255 }
256 unlock_slot(mapping, slot);
257 spin_unlock_irq(&mapping->tree_lock);
258 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
259}
260
Jan Karaac401cc2016-05-12 18:29:18 +0200261static void put_locked_mapping_entry(struct address_space *mapping,
262 pgoff_t index, void *entry)
263{
264 if (!radix_tree_exceptional_entry(entry)) {
265 unlock_page(entry);
266 put_page(entry);
267 } else {
Jan Karabc2466e2016-05-12 18:29:19 +0200268 dax_unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200269 }
270}
271
272/*
273 * Called when we are done with radix tree entry we looked up via
274 * get_unlocked_mapping_entry() and which we didn't lock in the end.
275 */
276static void put_unlocked_mapping_entry(struct address_space *mapping,
277 pgoff_t index, void *entry)
278{
279 if (!radix_tree_exceptional_entry(entry))
280 return;
281
282 /* We have to wake up next waiter for the radix tree entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100283 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
284}
285
Jan Karaac401cc2016-05-12 18:29:18 +0200286/*
287 * Find radix tree entry at given index. If it points to a page, return with
288 * the page locked. If it points to the exceptional entry, return with the
289 * radix tree entry locked. If the radix tree doesn't contain given index,
290 * create empty exceptional entry for the index and return with it locked.
291 *
Ross Zwisler642261a2016-11-08 11:34:45 +1100292 * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
293 * either return that locked entry or will return an error. This error will
294 * happen if there are any 4k entries (either zero pages or DAX entries)
295 * within the 2MiB range that we are requesting.
296 *
297 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
298 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
299 * insertion will fail if it finds any 4k entries already in the tree, and a
300 * 4k insertion will cause an existing 2MiB entry to be unmapped and
301 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
302 * well as 2MiB empty entries.
303 *
304 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
305 * real storage backing them. We will leave these real 2MiB DAX entries in
306 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
307 *
Jan Karaac401cc2016-05-12 18:29:18 +0200308 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
309 * persistent memory the benefit is doubtful. We can add that later if we can
310 * show it helps.
311 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100312static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
313 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200314{
Ross Zwisler642261a2016-11-08 11:34:45 +1100315 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100316 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200317
318restart:
319 spin_lock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100320 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100321
322 if (entry) {
323 if (size_flag & RADIX_DAX_PMD) {
324 if (!radix_tree_exceptional_entry(entry) ||
325 dax_is_pte_entry(entry)) {
326 put_unlocked_mapping_entry(mapping, index,
327 entry);
328 entry = ERR_PTR(-EEXIST);
329 goto out_unlock;
330 }
331 } else { /* trying to grab a PTE entry */
332 if (radix_tree_exceptional_entry(entry) &&
333 dax_is_pmd_entry(entry) &&
334 (dax_is_zero_entry(entry) ||
335 dax_is_empty_entry(entry))) {
336 pmd_downgrade = true;
337 }
338 }
339 }
340
Jan Karaac401cc2016-05-12 18:29:18 +0200341 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100342 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200343 int err;
344
Ross Zwisler642261a2016-11-08 11:34:45 +1100345 if (pmd_downgrade) {
346 /*
347 * Make sure 'entry' remains valid while we drop
348 * mapping->tree_lock.
349 */
350 entry = lock_slot(mapping, slot);
351 }
352
Jan Karaac401cc2016-05-12 18:29:18 +0200353 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100354 /*
355 * Besides huge zero pages the only other thing that gets
356 * downgraded are empty entries which don't need to be
357 * unmapped.
358 */
359 if (pmd_downgrade && dax_is_zero_entry(entry))
360 unmap_mapping_range(mapping,
361 (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
362
Jan Kara0cb80b42016-12-12 21:34:12 -0500363 err = radix_tree_preload(
364 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
365 if (err) {
366 if (pmd_downgrade)
367 put_locked_mapping_entry(mapping, index, entry);
368 return ERR_PTR(err);
369 }
Jan Karaac401cc2016-05-12 18:29:18 +0200370 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100371
372 if (pmd_downgrade) {
373 radix_tree_delete(&mapping->page_tree, index);
374 mapping->nrexceptional--;
375 dax_wake_mapping_entry_waiter(mapping, index, entry,
376 true);
377 }
378
379 entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
380
381 err = __radix_tree_insert(&mapping->page_tree, index,
382 dax_radix_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200383 radix_tree_preload_end();
384 if (err) {
385 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100386 /*
387 * Someone already created the entry? This is a
388 * normal failure when inserting PMDs in a range
389 * that already contains PTEs. In that case we want
390 * to return -EEXIST immediately.
391 */
392 if (err == -EEXIST && !(size_flag & RADIX_DAX_PMD))
Jan Karaac401cc2016-05-12 18:29:18 +0200393 goto restart;
Ross Zwisler642261a2016-11-08 11:34:45 +1100394 /*
395 * Our insertion of a DAX PMD entry failed, most
396 * likely because it collided with a PTE sized entry
397 * at a different index in the PMD range. We haven't
398 * inserted anything into the radix tree and have no
399 * waiters to wake.
400 */
Jan Karaac401cc2016-05-12 18:29:18 +0200401 return ERR_PTR(err);
402 }
403 /* Good, we have inserted empty locked entry into the tree. */
404 mapping->nrexceptional++;
405 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100406 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200407 }
408 /* Normal page in radix tree? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100409 if (!radix_tree_exceptional_entry(entry)) {
410 struct page *page = entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200411
412 get_page(page);
413 spin_unlock_irq(&mapping->tree_lock);
414 lock_page(page);
415 /* Page got truncated? Retry... */
416 if (unlikely(page->mapping != mapping)) {
417 unlock_page(page);
418 put_page(page);
419 goto restart;
420 }
421 return page;
422 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100423 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100424 out_unlock:
Jan Karaac401cc2016-05-12 18:29:18 +0200425 spin_unlock_irq(&mapping->tree_lock);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100426 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200427}
428
Ross Zwisler63e95b52016-11-08 11:32:20 +1100429/*
430 * We do not necessarily hold the mapping->tree_lock when we call this
431 * function so it is possible that 'entry' is no longer a valid item in the
Ross Zwisler642261a2016-11-08 11:34:45 +1100432 * radix tree. This is okay because all we really need to do is to find the
433 * correct waitqueue where tasks might be waiting for that old 'entry' and
434 * wake them.
Ross Zwisler63e95b52016-11-08 11:32:20 +1100435 */
Jan Karaac401cc2016-05-12 18:29:18 +0200436void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwisler63e95b52016-11-08 11:32:20 +1100437 pgoff_t index, void *entry, bool wake_all)
Jan Karaac401cc2016-05-12 18:29:18 +0200438{
Ross Zwisler63e95b52016-11-08 11:32:20 +1100439 struct exceptional_entry_key key;
440 wait_queue_head_t *wq;
441
442 wq = dax_entry_waitqueue(mapping, index, entry, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200443
444 /*
445 * Checking for locked entry and prepare_to_wait_exclusive() happens
446 * under mapping->tree_lock, ditto for entry handling in our callers.
447 * So at this point all tasks that could have seen our entry locked
448 * must be in the waitqueue and the following check will see them.
449 */
Ross Zwisler63e95b52016-11-08 11:32:20 +1100450 if (waitqueue_active(wq))
Jan Karaac401cc2016-05-12 18:29:18 +0200451 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
Jan Karaac401cc2016-05-12 18:29:18 +0200452}
453
Jan Karac6dcf522016-08-10 17:22:44 +0200454static int __dax_invalidate_mapping_entry(struct address_space *mapping,
455 pgoff_t index, bool trunc)
456{
457 int ret = 0;
458 void *entry;
459 struct radix_tree_root *page_tree = &mapping->page_tree;
460
461 spin_lock_irq(&mapping->tree_lock);
462 entry = get_unlocked_mapping_entry(mapping, index, NULL);
463 if (!entry || !radix_tree_exceptional_entry(entry))
464 goto out;
465 if (!trunc &&
466 (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
467 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE)))
468 goto out;
469 radix_tree_delete(page_tree, index);
470 mapping->nrexceptional--;
471 ret = 1;
472out:
473 put_unlocked_mapping_entry(mapping, index, entry);
474 spin_unlock_irq(&mapping->tree_lock);
475 return ret;
476}
Jan Karaac401cc2016-05-12 18:29:18 +0200477/*
478 * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
479 * entry to get unlocked before deleting it.
480 */
481int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
482{
Jan Karac6dcf522016-08-10 17:22:44 +0200483 int ret = __dax_invalidate_mapping_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200484
Jan Karaac401cc2016-05-12 18:29:18 +0200485 /*
486 * This gets called from truncate / punch_hole path. As such, the caller
487 * must hold locks protecting against concurrent modifications of the
488 * radix tree (usually fs-private i_mmap_sem for writing). Since the
489 * caller has seen exceptional entry for this index, we better find it
490 * at that index as well...
491 */
Jan Karac6dcf522016-08-10 17:22:44 +0200492 WARN_ON_ONCE(!ret);
493 return ret;
494}
Jan Karaac401cc2016-05-12 18:29:18 +0200495
Jan Karac6dcf522016-08-10 17:22:44 +0200496/*
497 * Invalidate exceptional DAX entry if easily possible. This handles DAX
498 * entries for invalidate_inode_pages() so we evict the entry only if we can
499 * do so without blocking.
500 */
501int dax_invalidate_mapping_entry(struct address_space *mapping, pgoff_t index)
502{
503 int ret = 0;
504 void *entry, **slot;
505 struct radix_tree_root *page_tree = &mapping->page_tree;
506
507 spin_lock_irq(&mapping->tree_lock);
508 entry = __radix_tree_lookup(page_tree, index, NULL, &slot);
509 if (!entry || !radix_tree_exceptional_entry(entry) ||
510 slot_locked(mapping, slot))
511 goto out;
512 if (radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_DIRTY) ||
513 radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
514 goto out;
515 radix_tree_delete(page_tree, index);
516 mapping->nrexceptional--;
517 ret = 1;
518out:
519 spin_unlock_irq(&mapping->tree_lock);
520 if (ret)
521 dax_wake_mapping_entry_waiter(mapping, index, entry, true);
522 return ret;
523}
524
525/*
526 * Invalidate exceptional DAX entry if it is clean.
527 */
528int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
529 pgoff_t index)
530{
531 return __dax_invalidate_mapping_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200532}
533
534/*
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800535 * The user has performed a load from a hole in the file. Allocating
536 * a new page in the file would cause excessive storage usage for
537 * workloads with sparse files. We allocate a page cache page instead.
538 * We'll kick it out of the page cache if it's ever written to,
539 * otherwise it will simply fall out of the page cache under memory
540 * pressure without ever having been dirtied.
541 */
Jan Karaac401cc2016-05-12 18:29:18 +0200542static int dax_load_hole(struct address_space *mapping, void *entry,
543 struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800544{
Jan Karaac401cc2016-05-12 18:29:18 +0200545 struct page *page;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800546
Jan Karaac401cc2016-05-12 18:29:18 +0200547 /* Hole page already exists? Return it... */
548 if (!radix_tree_exceptional_entry(entry)) {
549 vmf->page = entry;
550 return VM_FAULT_LOCKED;
551 }
552
553 /* This will replace locked radix tree entry with a hole page */
554 page = find_or_create_page(mapping, vmf->pgoff,
555 vmf->gfp_mask | __GFP_ZERO);
Jan Karab1aa8122016-12-14 15:07:24 -0800556 if (!page)
Jan Karaac401cc2016-05-12 18:29:18 +0200557 return VM_FAULT_OOM;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800558 vmf->page = page;
559 return VM_FAULT_LOCKED;
560}
561
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000562static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
563 struct page *to, unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800564{
Dan Williamsb2e0d162016-01-15 16:55:59 -0800565 struct blk_dax_ctl dax = {
Christoph Hellwigb0d5e822016-09-19 11:24:49 +1000566 .sector = sector,
567 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800568 };
Ross Zwislere2e05392015-08-18 13:55:41 -0600569 void *vto;
570
Dan Williamsb2e0d162016-01-15 16:55:59 -0800571 if (dax_map_atomic(bdev, &dax) < 0)
572 return PTR_ERR(dax.addr);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800573 vto = kmap_atomic(to);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800574 copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800575 kunmap_atomic(vto);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800576 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800577 return 0;
578}
579
Ross Zwisler642261a2016-11-08 11:34:45 +1100580/*
581 * By this point grab_mapping_entry() has ensured that we have a locked entry
582 * of the appropriate size so we don't have to worry about downgrading PMDs to
583 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
584 * already in the tree, we will skip the insertion and just dirty the PMD as
585 * appropriate.
586 */
Jan Karaac401cc2016-05-12 18:29:18 +0200587static void *dax_insert_mapping_entry(struct address_space *mapping,
588 struct vm_fault *vmf,
Ross Zwisler642261a2016-11-08 11:34:45 +1100589 void *entry, sector_t sector,
590 unsigned long flags)
Ross Zwisler9973c982016-01-22 15:10:47 -0800591{
592 struct radix_tree_root *page_tree = &mapping->page_tree;
Jan Karaac401cc2016-05-12 18:29:18 +0200593 int error = 0;
594 bool hole_fill = false;
595 void *new_entry;
596 pgoff_t index = vmf->pgoff;
Ross Zwisler9973c982016-01-22 15:10:47 -0800597
Jan Karaac401cc2016-05-12 18:29:18 +0200598 if (vmf->flags & FAULT_FLAG_WRITE)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800599 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800600
Jan Karaac401cc2016-05-12 18:29:18 +0200601 /* Replacing hole page with block mapping? */
602 if (!radix_tree_exceptional_entry(entry)) {
603 hole_fill = true;
604 /*
605 * Unmap the page now before we remove it from page cache below.
606 * The page is locked so it cannot be faulted in again.
607 */
608 unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
609 PAGE_SIZE, 0);
610 error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
611 if (error)
612 return ERR_PTR(error);
Ross Zwisler642261a2016-11-08 11:34:45 +1100613 } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
614 /* replacing huge zero page with PMD block mapping */
615 unmap_mapping_range(mapping,
616 (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
Ross Zwisler9973c982016-01-22 15:10:47 -0800617 }
618
Jan Karaac401cc2016-05-12 18:29:18 +0200619 spin_lock_irq(&mapping->tree_lock);
Ross Zwisler642261a2016-11-08 11:34:45 +1100620 new_entry = dax_radix_locked_entry(sector, flags);
621
Jan Karaac401cc2016-05-12 18:29:18 +0200622 if (hole_fill) {
623 __delete_from_page_cache(entry, NULL);
624 /* Drop pagecache reference */
625 put_page(entry);
Ross Zwisler642261a2016-11-08 11:34:45 +1100626 error = __radix_tree_insert(page_tree, index,
627 dax_radix_order(new_entry), new_entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200628 if (error) {
629 new_entry = ERR_PTR(error);
Ross Zwisler9973c982016-01-22 15:10:47 -0800630 goto unlock;
631 }
Jan Karaac401cc2016-05-12 18:29:18 +0200632 mapping->nrexceptional++;
Ross Zwisler642261a2016-11-08 11:34:45 +1100633 } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
634 /*
635 * Only swap our new entry into the radix tree if the current
636 * entry is a zero page or an empty entry. If a normal PTE or
637 * PMD entry is already in the tree, we leave it alone. This
638 * means that if we are trying to insert a PTE and the
639 * existing entry is a PMD, we will just leave the PMD in the
640 * tree and dirty it if necessary.
641 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800642 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200643 void **slot;
644 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800645
Johannes Weinerf7942432016-12-12 16:43:41 -0800646 ret = __radix_tree_lookup(page_tree, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200647 WARN_ON_ONCE(ret != entry);
Johannes Weiner4d693d02016-12-12 16:43:49 -0800648 __radix_tree_replace(page_tree, node, slot,
649 new_entry, NULL, NULL);
Ross Zwisler9973c982016-01-22 15:10:47 -0800650 }
Jan Karaac401cc2016-05-12 18:29:18 +0200651 if (vmf->flags & FAULT_FLAG_WRITE)
Ross Zwisler9973c982016-01-22 15:10:47 -0800652 radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
653 unlock:
654 spin_unlock_irq(&mapping->tree_lock);
Jan Karaac401cc2016-05-12 18:29:18 +0200655 if (hole_fill) {
656 radix_tree_preload_end();
657 /*
658 * We don't need hole page anymore, it has been replaced with
659 * locked radix tree entry now.
660 */
661 if (mapping->a_ops->freepage)
662 mapping->a_ops->freepage(entry);
663 unlock_page(entry);
664 put_page(entry);
665 }
666 return new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800667}
668
Jan Kara4b4bb462016-12-14 15:07:53 -0800669static inline unsigned long
670pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
671{
672 unsigned long address;
673
674 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
675 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
676 return address;
677}
678
679/* Walk all mappings of a given index of a file and writeprotect them */
680static void dax_mapping_entry_mkclean(struct address_space *mapping,
681 pgoff_t index, unsigned long pfn)
682{
683 struct vm_area_struct *vma;
684 pte_t *ptep;
685 pte_t pte;
686 spinlock_t *ptl;
687 bool changed;
688
689 i_mmap_lock_read(mapping);
690 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
691 unsigned long address;
692
693 cond_resched();
694
695 if (!(vma->vm_flags & VM_SHARED))
696 continue;
697
698 address = pgoff_address(index, vma);
699 changed = false;
700 if (follow_pte(vma->vm_mm, address, &ptep, &ptl))
701 continue;
702 if (pfn != pte_pfn(*ptep))
703 goto unlock;
704 if (!pte_dirty(*ptep) && !pte_write(*ptep))
705 goto unlock;
706
707 flush_cache_page(vma, address, pfn);
708 pte = ptep_clear_flush(vma, address, ptep);
709 pte = pte_wrprotect(pte);
710 pte = pte_mkclean(pte);
711 set_pte_at(vma->vm_mm, address, ptep, pte);
712 changed = true;
713unlock:
714 pte_unmap_unlock(ptep, ptl);
715
716 if (changed)
717 mmu_notifier_invalidate_page(vma->vm_mm, address);
718 }
719 i_mmap_unlock_read(mapping);
720}
721
Ross Zwisler9973c982016-01-22 15:10:47 -0800722static int dax_writeback_one(struct block_device *bdev,
723 struct address_space *mapping, pgoff_t index, void *entry)
724{
725 struct radix_tree_root *page_tree = &mapping->page_tree;
Ross Zwisler9973c982016-01-22 15:10:47 -0800726 struct blk_dax_ctl dax;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800727 void *entry2, **slot;
Ross Zwisler9973c982016-01-22 15:10:47 -0800728 int ret = 0;
729
Ross Zwisler9973c982016-01-22 15:10:47 -0800730 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800731 * A page got tagged dirty in DAX mapping? Something is seriously
732 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800733 */
Jan Karaa6abc2c2016-12-14 15:07:47 -0800734 if (WARN_ON(!radix_tree_exceptional_entry(entry)))
735 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800736
Jan Karaa6abc2c2016-12-14 15:07:47 -0800737 spin_lock_irq(&mapping->tree_lock);
738 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
739 /* Entry got punched out / reallocated? */
740 if (!entry2 || !radix_tree_exceptional_entry(entry2))
741 goto put_unlocked;
742 /*
743 * Entry got reallocated elsewhere? No need to writeback. We have to
744 * compare sectors as we must not bail out due to difference in lockbit
745 * or entry type.
746 */
747 if (dax_radix_sector(entry2) != dax_radix_sector(entry))
748 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +1100749 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
750 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800751 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800752 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800753 }
754
Jan Karaa6abc2c2016-12-14 15:07:47 -0800755 /* Another fsync thread may have already written back this entry */
756 if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
757 goto put_unlocked;
758 /* Lock the entry to serialize with page faults */
759 entry = lock_slot(mapping, slot);
760 /*
761 * We can clear the tag now but we have to be careful so that concurrent
762 * dax_writeback_one() calls for the same index cannot finish before we
763 * actually flush the caches. This is achieved as the calls will look
764 * at the entry only under tree_lock and once they do that they will
765 * see the entry locked and wait for it to unlock.
766 */
767 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
768 spin_unlock_irq(&mapping->tree_lock);
769
Ross Zwisler642261a2016-11-08 11:34:45 +1100770 /*
771 * Even if dax_writeback_mapping_range() was given a wbc->range_start
772 * in the middle of a PMD, the 'index' we are given will be aligned to
773 * the start index of the PMD, as will the sector we pull from
774 * 'entry'. This allows us to flush for PMD_SIZE and not have to
775 * worry about partial PMD writebacks.
776 */
777 dax.sector = dax_radix_sector(entry);
778 dax.size = PAGE_SIZE << dax_radix_order(entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800779
780 /*
781 * We cannot hold tree_lock while calling dax_map_atomic() because it
782 * eventually calls cond_resched().
783 */
784 ret = dax_map_atomic(bdev, &dax);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800785 if (ret < 0) {
786 put_locked_mapping_entry(mapping, index, entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800787 return ret;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800788 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800789
790 if (WARN_ON_ONCE(ret < dax.size)) {
791 ret = -EIO;
792 goto unmap;
793 }
794
Jan Kara4b4bb462016-12-14 15:07:53 -0800795 dax_mapping_entry_mkclean(mapping, index, pfn_t_to_pfn(dax.pfn));
Ross Zwisler9973c982016-01-22 15:10:47 -0800796 wb_cache_pmem(dax.addr, dax.size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800797 /*
798 * After we have flushed the cache, we can clear the dirty tag. There
799 * cannot be new dirty data in the pfn after the flush has completed as
800 * the pfn mappings are writeprotected and fault waits for mapping
801 * entry lock.
802 */
803 spin_lock_irq(&mapping->tree_lock);
804 radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_DIRTY);
805 spin_unlock_irq(&mapping->tree_lock);
Ross Zwisler9973c982016-01-22 15:10:47 -0800806 unmap:
807 dax_unmap_atomic(bdev, &dax);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800808 put_locked_mapping_entry(mapping, index, entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800809 return ret;
810
Jan Karaa6abc2c2016-12-14 15:07:47 -0800811 put_unlocked:
812 put_unlocked_mapping_entry(mapping, index, entry2);
Ross Zwisler9973c982016-01-22 15:10:47 -0800813 spin_unlock_irq(&mapping->tree_lock);
814 return ret;
815}
816
817/*
818 * Flush the mapping to the persistent domain within the byte range of [start,
819 * end]. This is required by data integrity operations to ensure file data is
820 * on persistent storage prior to completion of the operation.
821 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800822int dax_writeback_mapping_range(struct address_space *mapping,
823 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800824{
825 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +1100826 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -0800827 pgoff_t indices[PAGEVEC_SIZE];
828 struct pagevec pvec;
829 bool done = false;
830 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800831
832 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
833 return -EIO;
834
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800835 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
836 return 0;
837
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300838 start_index = wbc->range_start >> PAGE_SHIFT;
839 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -0800840
841 tag_pages_for_writeback(mapping, start_index, end_index);
842
843 pagevec_init(&pvec, 0);
844 while (!done) {
845 pvec.nr = find_get_entries_tag(mapping, start_index,
846 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
847 pvec.pages, indices);
848
849 if (pvec.nr == 0)
850 break;
851
852 for (i = 0; i < pvec.nr; i++) {
853 if (indices[i] > end_index) {
854 done = true;
855 break;
856 }
857
858 ret = dax_writeback_one(bdev, mapping, indices[i],
859 pvec.pages[i]);
860 if (ret < 0)
861 return ret;
862 }
863 }
Ross Zwisler9973c982016-01-22 15:10:47 -0800864 return 0;
865}
866EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
867
Jan Karaac401cc2016-05-12 18:29:18 +0200868static int dax_insert_mapping(struct address_space *mapping,
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000869 struct block_device *bdev, sector_t sector, size_t size,
870 void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800871{
Jan Kara1a29d852016-12-14 15:07:01 -0800872 unsigned long vaddr = vmf->address;
Dan Williamsb2e0d162016-01-15 16:55:59 -0800873 struct blk_dax_ctl dax = {
Christoph Hellwig1aaba092016-09-19 11:24:49 +1000874 .sector = sector,
875 .size = size,
Dan Williamsb2e0d162016-01-15 16:55:59 -0800876 };
Jan Karaac401cc2016-05-12 18:29:18 +0200877 void *ret;
878 void *entry = *entryp;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800879
Jan Kara4d9a2c82016-05-12 18:29:20 +0200880 if (dax_map_atomic(bdev, &dax) < 0)
881 return PTR_ERR(dax.addr);
Dan Williamsb2e0d162016-01-15 16:55:59 -0800882 dax_unmap_atomic(bdev, &dax);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800883
Ross Zwisler642261a2016-11-08 11:34:45 +1100884 ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector, 0);
Jan Kara4d9a2c82016-05-12 18:29:20 +0200885 if (IS_ERR(ret))
886 return PTR_ERR(ret);
Jan Karaac401cc2016-05-12 18:29:18 +0200887 *entryp = ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800888
Jan Kara4d9a2c82016-05-12 18:29:20 +0200889 return vm_insert_mixed(vma, vaddr, dax.pfn);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800890}
891
Dave Chinnerce5c5d52015-06-04 09:18:18 +1000892/**
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700893 * dax_pfn_mkwrite - handle first write to DAX page
894 * @vma: The virtual memory area where the fault occurred
895 * @vmf: The description of the fault
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700896 */
897int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
898{
Ross Zwisler9973c982016-01-22 15:10:47 -0800899 struct file *file = vma->vm_file;
Jan Karaac401cc2016-05-12 18:29:18 +0200900 struct address_space *mapping = file->f_mapping;
Jan Kara2f89dc12016-12-14 15:07:50 -0800901 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200902 pgoff_t index = vmf->pgoff;
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700903
Jan Karaac401cc2016-05-12 18:29:18 +0200904 spin_lock_irq(&mapping->tree_lock);
Jan Kara2f89dc12016-12-14 15:07:50 -0800905 entry = get_unlocked_mapping_entry(mapping, index, &slot);
906 if (!entry || !radix_tree_exceptional_entry(entry)) {
907 if (entry)
908 put_unlocked_mapping_entry(mapping, index, entry);
909 spin_unlock_irq(&mapping->tree_lock);
910 return VM_FAULT_NOPAGE;
911 }
Jan Karaac401cc2016-05-12 18:29:18 +0200912 radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
Jan Kara2f89dc12016-12-14 15:07:50 -0800913 entry = lock_slot(mapping, slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200914 spin_unlock_irq(&mapping->tree_lock);
Jan Kara2f89dc12016-12-14 15:07:50 -0800915 /*
916 * If we race with somebody updating the PTE and finish_mkwrite_fault()
917 * fails, we don't care. We need to return VM_FAULT_NOPAGE and retry
918 * the fault in either case.
919 */
920 finish_mkwrite_fault(vmf);
921 put_locked_mapping_entry(mapping, index, entry);
Boaz Harrosh0e3b2102015-04-15 16:15:14 -0700922 return VM_FAULT_NOPAGE;
923}
924EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
925
Vishal Verma4b0228f2016-04-21 15:13:46 -0400926static bool dax_range_is_aligned(struct block_device *bdev,
927 unsigned int offset, unsigned int length)
928{
929 unsigned short sector_size = bdev_logical_block_size(bdev);
930
931 if (!IS_ALIGNED(offset, sector_size))
932 return false;
933 if (!IS_ALIGNED(length, sector_size))
934 return false;
935
936 return true;
937}
938
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200939int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
940 unsigned int offset, unsigned int length)
941{
942 struct blk_dax_ctl dax = {
943 .sector = sector,
944 .size = PAGE_SIZE,
945 };
946
Vishal Verma4b0228f2016-04-21 15:13:46 -0400947 if (dax_range_is_aligned(bdev, offset, length)) {
948 sector_t start_sector = dax.sector + (offset >> 9);
949
950 return blkdev_issue_zeroout(bdev, start_sector,
951 length >> 9, GFP_NOFS, true);
952 } else {
953 if (dax_map_atomic(bdev, &dax) < 0)
954 return PTR_ERR(dax.addr);
955 clear_pmem(dax.addr + offset, length);
Vishal Verma4b0228f2016-04-21 15:13:46 -0400956 dax_unmap_atomic(bdev, &dax);
957 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +0200958 return 0;
959}
960EXPORT_SYMBOL_GPL(__dax_zero_page_range);
961
Christoph Hellwiga254e562016-09-19 11:24:49 +1000962#ifdef CONFIG_FS_IOMAP
Ross Zwisler333ccc92016-11-08 11:33:09 +1100963static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
964{
965 return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
966}
967
Christoph Hellwiga254e562016-09-19 11:24:49 +1000968static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +1100969dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +1000970 struct iomap *iomap)
971{
972 struct iov_iter *iter = data;
973 loff_t end = pos + length, done = 0;
974 ssize_t ret = 0;
975
976 if (iov_iter_rw(iter) == READ) {
977 end = min(end, i_size_read(inode));
978 if (pos >= end)
979 return 0;
980
981 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
982 return iov_iter_zero(min(length, end - pos), iter);
983 }
984
985 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
986 return -EIO;
987
988 while (pos < end) {
989 unsigned offset = pos & (PAGE_SIZE - 1);
990 struct blk_dax_ctl dax = { 0 };
991 ssize_t map_len;
992
Ross Zwisler333ccc92016-11-08 11:33:09 +1100993 dax.sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +1000994 dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
995 map_len = dax_map_atomic(iomap->bdev, &dax);
996 if (map_len < 0) {
997 ret = map_len;
998 break;
999 }
1000
1001 dax.addr += offset;
1002 map_len -= offset;
1003 if (map_len > end - pos)
1004 map_len = end - pos;
1005
1006 if (iov_iter_rw(iter) == WRITE)
1007 map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
1008 else
1009 map_len = copy_to_iter(dax.addr, map_len, iter);
1010 dax_unmap_atomic(iomap->bdev, &dax);
1011 if (map_len <= 0) {
1012 ret = map_len ? map_len : -EFAULT;
1013 break;
1014 }
1015
1016 pos += map_len;
1017 length -= map_len;
1018 done += map_len;
1019 }
1020
1021 return done ? done : ret;
1022}
1023
1024/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001025 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001026 * @iocb: The control block for this I/O
1027 * @iter: The addresses to do I/O from or to
1028 * @ops: iomap ops passed from the file system
1029 *
1030 * This function performs read and write operations to directly mapped
1031 * persistent memory. The callers needs to take care of read/write exclusion
1032 * and evicting any page cache pages in the region under I/O.
1033 */
1034ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001035dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001036 struct iomap_ops *ops)
1037{
1038 struct address_space *mapping = iocb->ki_filp->f_mapping;
1039 struct inode *inode = mapping->host;
1040 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1041 unsigned flags = 0;
1042
1043 if (iov_iter_rw(iter) == WRITE)
1044 flags |= IOMAP_WRITE;
1045
1046 /*
1047 * Yes, even DAX files can have page cache attached to them: A zeroed
1048 * page is inserted into the pagecache when we have to serve a write
1049 * fault on a hole. It should never be dirtied and can simply be
1050 * dropped from the pagecache once we get real data for the page.
1051 *
1052 * XXX: This is racy against mmap, and there's nothing we can do about
1053 * it. We'll eventually need to shift this down even further so that
1054 * we can check if we allocated blocks over a hole first.
1055 */
1056 if (mapping->nrpages) {
1057 ret = invalidate_inode_pages2_range(mapping,
1058 pos >> PAGE_SHIFT,
1059 (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
1060 WARN_ON_ONCE(ret);
1061 }
1062
1063 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
1077/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001078 * dax_iomap_fault - handle a page fault on a DAX file
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001079 * @vma: The virtual memory area where the fault occurred
1080 * @vmf: The description of the fault
1081 * @ops: iomap ops passed from the file system
1082 *
1083 * When a page fault occurs, filesystems may call this helper in their fault
1084 * or mkwrite handler for DAX files. Assumes the caller has done all the
1085 * necessary locking for the page fault to proceed successfully.
1086 */
Ross Zwisler11c59c92016-11-08 11:32:46 +11001087int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001088 struct iomap_ops *ops)
1089{
1090 struct address_space *mapping = vma->vm_file->f_mapping;
1091 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001092 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001093 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1094 sector_t sector;
1095 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001096 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001097 int error, major = 0;
Jan Karab1aa8122016-12-14 15:07:24 -08001098 int vmf_ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001099 void *entry;
1100
1101 /*
1102 * Check whether offset isn't beyond end of file now. Caller is supposed
1103 * to hold locks serializing us with truncate / punch hole so this is
1104 * a reliable test.
1105 */
1106 if (pos >= i_size_read(inode))
1107 return VM_FAULT_SIGBUS;
1108
Ross Zwisler642261a2016-11-08 11:34:45 +11001109 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001110 if (IS_ERR(entry)) {
1111 error = PTR_ERR(entry);
1112 goto out;
1113 }
1114
1115 if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
1116 flags |= IOMAP_WRITE;
1117
1118 /*
1119 * Note that we don't bother to use iomap_apply here: DAX required
1120 * the file system block size to be equal the page size, which means
1121 * that we never have to deal with more than a single extent here.
1122 */
1123 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1124 if (error)
1125 goto unlock_entry;
1126 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
1127 error = -EIO; /* fs corruption? */
Ross Zwisler15502902016-11-08 11:33:26 +11001128 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001129 }
1130
Ross Zwisler333ccc92016-11-08 11:33:09 +11001131 sector = dax_iomap_sector(&iomap, pos);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001132
1133 if (vmf->cow_page) {
1134 switch (iomap.type) {
1135 case IOMAP_HOLE:
1136 case IOMAP_UNWRITTEN:
1137 clear_user_highpage(vmf->cow_page, vaddr);
1138 break;
1139 case IOMAP_MAPPED:
1140 error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
1141 vmf->cow_page, vaddr);
1142 break;
1143 default:
1144 WARN_ON_ONCE(1);
1145 error = -EIO;
1146 break;
1147 }
1148
1149 if (error)
Ross Zwisler15502902016-11-08 11:33:26 +11001150 goto finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001151
1152 __SetPageUptodate(vmf->cow_page);
1153 vmf_ret = finish_fault(vmf);
1154 if (!vmf_ret)
1155 vmf_ret = VM_FAULT_DONE_COW;
Ross Zwisler15502902016-11-08 11:33:26 +11001156 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001157 }
1158
1159 switch (iomap.type) {
1160 case IOMAP_MAPPED:
1161 if (iomap.flags & IOMAP_F_NEW) {
1162 count_vm_event(PGMAJFAULT);
1163 mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
1164 major = VM_FAULT_MAJOR;
1165 }
1166 error = dax_insert_mapping(mapping, iomap.bdev, sector,
1167 PAGE_SIZE, &entry, vma, vmf);
1168 break;
1169 case IOMAP_UNWRITTEN:
1170 case IOMAP_HOLE:
Ross Zwisler15502902016-11-08 11:33:26 +11001171 if (!(vmf->flags & FAULT_FLAG_WRITE)) {
Jan Karab1aa8122016-12-14 15:07:24 -08001172 vmf_ret = dax_load_hole(mapping, entry, vmf);
Ross Zwisler15502902016-11-08 11:33:26 +11001173 break;
1174 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001175 /*FALLTHRU*/
1176 default:
1177 WARN_ON_ONCE(1);
1178 error = -EIO;
1179 break;
1180 }
1181
Ross Zwisler15502902016-11-08 11:33:26 +11001182 finish_iomap:
1183 if (ops->iomap_end) {
Jan Karab1aa8122016-12-14 15:07:24 -08001184 if (error || (vmf_ret & VM_FAULT_ERROR)) {
Ross Zwisler15502902016-11-08 11:33:26 +11001185 /* keep previous error */
1186 ops->iomap_end(inode, pos, PAGE_SIZE, 0, flags,
1187 &iomap);
1188 } else {
1189 error = ops->iomap_end(inode, pos, PAGE_SIZE,
1190 PAGE_SIZE, flags, &iomap);
1191 }
1192 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001193 unlock_entry:
Jan Karab1aa8122016-12-14 15:07:24 -08001194 if (vmf_ret != VM_FAULT_LOCKED || error)
Ross Zwisler15502902016-11-08 11:33:26 +11001195 put_locked_mapping_entry(mapping, vmf->pgoff, entry);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001196 out:
1197 if (error == -ENOMEM)
1198 return VM_FAULT_OOM | major;
1199 /* -EBUSY is fine, somebody else faulted on the same PTE */
1200 if (error < 0 && error != -EBUSY)
1201 return VM_FAULT_SIGBUS | major;
Jan Karab1aa8122016-12-14 15:07:24 -08001202 if (vmf_ret) {
Ross Zwisler15502902016-11-08 11:33:26 +11001203 WARN_ON_ONCE(error); /* -EBUSY from ops->iomap_end? */
Jan Karab1aa8122016-12-14 15:07:24 -08001204 return vmf_ret;
Ross Zwisler15502902016-11-08 11:33:26 +11001205 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001206 return VM_FAULT_NOPAGE | major;
1207}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001208EXPORT_SYMBOL_GPL(dax_iomap_fault);
Ross Zwisler642261a2016-11-08 11:34:45 +11001209
1210#ifdef CONFIG_FS_DAX_PMD
1211/*
1212 * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
1213 * more often than one might expect in the below functions.
1214 */
1215#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
1216
1217static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
1218 struct vm_fault *vmf, unsigned long address,
1219 struct iomap *iomap, loff_t pos, bool write, void **entryp)
1220{
1221 struct address_space *mapping = vma->vm_file->f_mapping;
1222 struct block_device *bdev = iomap->bdev;
1223 struct blk_dax_ctl dax = {
1224 .sector = dax_iomap_sector(iomap, pos),
1225 .size = PMD_SIZE,
1226 };
1227 long length = dax_map_atomic(bdev, &dax);
1228 void *ret;
1229
1230 if (length < 0) /* dax_map_atomic() failed */
1231 return VM_FAULT_FALLBACK;
1232 if (length < PMD_SIZE)
1233 goto unmap_fallback;
1234 if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
1235 goto unmap_fallback;
1236 if (!pfn_t_devmap(dax.pfn))
1237 goto unmap_fallback;
1238
1239 dax_unmap_atomic(bdev, &dax);
1240
1241 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
1242 RADIX_DAX_PMD);
1243 if (IS_ERR(ret))
1244 return VM_FAULT_FALLBACK;
1245 *entryp = ret;
1246
1247 return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
1248
1249 unmap_fallback:
1250 dax_unmap_atomic(bdev, &dax);
1251 return VM_FAULT_FALLBACK;
1252}
1253
1254static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
1255 struct vm_fault *vmf, unsigned long address,
1256 struct iomap *iomap, void **entryp)
1257{
1258 struct address_space *mapping = vma->vm_file->f_mapping;
1259 unsigned long pmd_addr = address & PMD_MASK;
1260 struct page *zero_page;
1261 spinlock_t *ptl;
1262 pmd_t pmd_entry;
1263 void *ret;
1264
1265 zero_page = mm_get_huge_zero_page(vma->vm_mm);
1266
1267 if (unlikely(!zero_page))
1268 return VM_FAULT_FALLBACK;
1269
1270 ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
1271 RADIX_DAX_PMD | RADIX_DAX_HZP);
1272 if (IS_ERR(ret))
1273 return VM_FAULT_FALLBACK;
1274 *entryp = ret;
1275
1276 ptl = pmd_lock(vma->vm_mm, pmd);
1277 if (!pmd_none(*pmd)) {
1278 spin_unlock(ptl);
1279 return VM_FAULT_FALLBACK;
1280 }
1281
1282 pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
1283 pmd_entry = pmd_mkhuge(pmd_entry);
1284 set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
1285 spin_unlock(ptl);
1286 return VM_FAULT_NOPAGE;
1287}
1288
1289int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
1290 pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
1291{
1292 struct address_space *mapping = vma->vm_file->f_mapping;
1293 unsigned long pmd_addr = address & PMD_MASK;
1294 bool write = flags & FAULT_FLAG_WRITE;
Jan Kara9484ab12016-11-10 10:26:50 +11001295 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001296 struct inode *inode = mapping->host;
1297 int result = VM_FAULT_FALLBACK;
1298 struct iomap iomap = { 0 };
1299 pgoff_t max_pgoff, pgoff;
1300 struct vm_fault vmf;
1301 void *entry;
1302 loff_t pos;
1303 int error;
1304
1305 /* Fall back to PTEs if we're going to COW */
1306 if (write && !(vma->vm_flags & VM_SHARED))
1307 goto fallback;
1308
1309 /* If the PMD would extend outside the VMA */
1310 if (pmd_addr < vma->vm_start)
1311 goto fallback;
1312 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1313 goto fallback;
1314
1315 /*
1316 * Check whether offset isn't beyond end of file now. Caller is
1317 * supposed to hold locks serializing us with truncate / punch hole so
1318 * this is a reliable test.
1319 */
1320 pgoff = linear_page_index(vma, pmd_addr);
1321 max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
1322
1323 if (pgoff > max_pgoff)
1324 return VM_FAULT_SIGBUS;
1325
1326 /* If the PMD would extend beyond the file size */
1327 if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
1328 goto fallback;
1329
1330 /*
1331 * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
1332 * PMD or a HZP entry. If it can't (because a 4k page is already in
1333 * the tree, for instance), it will return -EEXIST and we just fall
1334 * back to 4k entries.
1335 */
1336 entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
1337 if (IS_ERR(entry))
1338 goto fallback;
1339
1340 /*
1341 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1342 * setting up a mapping, so really we're using iomap_begin() as a way
1343 * to look up our filesystem block.
1344 */
1345 pos = (loff_t)pgoff << PAGE_SHIFT;
1346 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1347 if (error)
1348 goto unlock_entry;
1349 if (iomap.offset + iomap.length < pos + PMD_SIZE)
1350 goto finish_iomap;
1351
1352 vmf.pgoff = pgoff;
1353 vmf.flags = flags;
1354 vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
1355
1356 switch (iomap.type) {
1357 case IOMAP_MAPPED:
1358 result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
1359 &iomap, pos, write, &entry);
1360 break;
1361 case IOMAP_UNWRITTEN:
1362 case IOMAP_HOLE:
1363 if (WARN_ON_ONCE(write))
1364 goto finish_iomap;
1365 result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
1366 &entry);
1367 break;
1368 default:
1369 WARN_ON_ONCE(1);
1370 break;
1371 }
1372
1373 finish_iomap:
1374 if (ops->iomap_end) {
1375 if (result == VM_FAULT_FALLBACK) {
1376 ops->iomap_end(inode, pos, PMD_SIZE, 0, iomap_flags,
1377 &iomap);
1378 } else {
1379 error = ops->iomap_end(inode, pos, PMD_SIZE, PMD_SIZE,
1380 iomap_flags, &iomap);
1381 if (error)
1382 result = VM_FAULT_FALLBACK;
1383 }
1384 }
1385 unlock_entry:
1386 put_locked_mapping_entry(mapping, pgoff, entry);
1387 fallback:
1388 if (result == VM_FAULT_FALLBACK) {
1389 split_huge_pmd(vma, pmd, address);
1390 count_vm_event(THP_FAULT_FALLBACK);
1391 }
1392 return result;
1393}
1394EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
1395#endif /* CONFIG_FS_DAX_PMD */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001396#endif /* CONFIG_FS_IOMAP */