blob: ce87d21b38059928df7abe4c1ba54ce68f72e30c [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
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -040041static inline unsigned int pe_order(enum page_entry_size pe_size)
42{
43 if (pe_size == PE_SIZE_PTE)
44 return PAGE_SHIFT - PAGE_SHIFT;
45 if (pe_size == PE_SIZE_PMD)
46 return PMD_SHIFT - PAGE_SHIFT;
47 if (pe_size == PE_SIZE_PUD)
48 return PUD_SHIFT - PAGE_SHIFT;
49 return ~0;
50}
51
Jan Karaac401cc2016-05-12 18:29:18 +020052/* We choose 4096 entries - same as per-zone page wait tables */
53#define DAX_WAIT_TABLE_BITS 12
54#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
55
Ross Zwisler917f3452017-09-06 16:18:58 -070056/* The 'colour' (ie low bits) within a PMD of a page offset. */
57#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
Matthew Wilcox977fbdc2018-01-31 16:17:36 -080058#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
Ross Zwisler917f3452017-09-06 16:18:58 -070059
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -040060/* The order of a PMD entry */
61#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
62
Ross Zwislerce95ab0f2016-11-08 11:31:44 +110063static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020064
65static int __init init_dax_wait_table(void)
66{
67 int i;
68
69 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
70 init_waitqueue_head(wait_table + i);
71 return 0;
72}
73fs_initcall(init_dax_wait_table);
74
Ross Zwisler527b19d2017-09-06 16:18:51 -070075/*
Matthew Wilcox3159f942017-11-03 13:30:42 -040076 * DAX pagecache entries use XArray value entries so they can't be mistaken
77 * for pages. We use one bit for locking, one bit for the entry size (PMD)
78 * and two more to tell us if the entry is a zero page or an empty entry that
79 * is just used for locking. In total four special bits.
Ross Zwisler527b19d2017-09-06 16:18:51 -070080 *
81 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
82 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
83 * block allocation.
84 */
Matthew Wilcox3159f942017-11-03 13:30:42 -040085#define DAX_SHIFT (4)
86#define DAX_LOCKED (1UL << 0)
87#define DAX_PMD (1UL << 1)
88#define DAX_ZERO_PAGE (1UL << 2)
89#define DAX_EMPTY (1UL << 3)
Ross Zwisler527b19d2017-09-06 16:18:51 -070090
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040091static unsigned long dax_to_pfn(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -070092{
Matthew Wilcox3159f942017-11-03 13:30:42 -040093 return xa_to_value(entry) >> DAX_SHIFT;
Ross Zwisler527b19d2017-09-06 16:18:51 -070094}
95
Matthew Wilcox9f32d222018-06-12 09:46:30 -040096static void *dax_make_entry(pfn_t pfn, unsigned long flags)
97{
98 return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT));
99}
100
101static void *dax_make_page_entry(struct page *page)
102{
103 pfn_t pfn = page_to_pfn_t(page);
104 return dax_make_entry(pfn, PageHead(page) ? DAX_PMD : 0);
105}
106
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400107static bool dax_is_locked(void *entry)
108{
109 return xa_to_value(entry) & DAX_LOCKED;
110}
111
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400112static unsigned int dax_entry_order(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -0700113{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400114 if (xa_to_value(entry) & DAX_PMD)
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400115 return PMD_ORDER;
Ross Zwisler527b19d2017-09-06 16:18:51 -0700116 return 0;
117}
118
Ross Zwisler642261a2016-11-08 11:34:45 +1100119static int dax_is_pmd_entry(void *entry)
120{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400121 return xa_to_value(entry) & DAX_PMD;
Ross Zwisler642261a2016-11-08 11:34:45 +1100122}
123
124static int dax_is_pte_entry(void *entry)
125{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400126 return !(xa_to_value(entry) & DAX_PMD);
Ross Zwisler642261a2016-11-08 11:34:45 +1100127}
128
129static int dax_is_zero_entry(void *entry)
130{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400131 return xa_to_value(entry) & DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100132}
133
134static int dax_is_empty_entry(void *entry)
135{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400136 return xa_to_value(entry) & DAX_EMPTY;
Ross Zwisler642261a2016-11-08 11:34:45 +1100137}
138
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800139/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400140 * DAX page cache entry locking
Jan Karaac401cc2016-05-12 18:29:18 +0200141 */
142struct exceptional_entry_key {
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400143 struct xarray *xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100144 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200145};
146
147struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200148 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200149 struct exceptional_entry_key key;
150};
151
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400152static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas,
153 void *entry, struct exceptional_entry_key *key)
Ross Zwisler63e95b52016-11-08 11:32:20 +1100154{
155 unsigned long hash;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400156 unsigned long index = xas->xa_index;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100157
158 /*
159 * If 'entry' is a PMD, align the 'index' that we use for the wait
160 * queue to the start of that PMD. This ensures that all offsets in
161 * the range covered by the PMD map to the same bit lock.
162 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100163 if (dax_is_pmd_entry(entry))
Ross Zwisler917f3452017-09-06 16:18:58 -0700164 index &= ~PG_PMD_COLOUR;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400165 key->xa = xas->xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100166 key->entry_start = index;
167
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400168 hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100169 return wait_table + hash;
170}
171
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400172static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
173 unsigned int mode, int sync, void *keyp)
Jan Karaac401cc2016-05-12 18:29:18 +0200174{
175 struct exceptional_entry_key *key = keyp;
176 struct wait_exceptional_entry_queue *ewait =
177 container_of(wait, struct wait_exceptional_entry_queue, wait);
178
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400179 if (key->xa != ewait->key.xa ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100180 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200181 return 0;
182 return autoremove_wake_function(wait, mode, sync, NULL);
183}
184
185/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700186 * @entry may no longer be the entry at the index in the mapping.
187 * The important information it's conveying is whether the entry at
188 * this index used to be a PMD entry.
Ross Zwislere30331f2017-09-06 16:18:39 -0700189 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400190static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
Ross Zwislere30331f2017-09-06 16:18:39 -0700191{
192 struct exceptional_entry_key key;
193 wait_queue_head_t *wq;
194
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400195 wq = dax_entry_waitqueue(xas, entry, &key);
Ross Zwislere30331f2017-09-06 16:18:39 -0700196
197 /*
198 * Checking for locked entry and prepare_to_wait_exclusive() happens
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700199 * under the i_pages lock, ditto for entry handling in our callers.
Ross Zwislere30331f2017-09-06 16:18:39 -0700200 * So at this point all tasks that could have seen our entry locked
201 * must be in the waitqueue and the following check will see them.
202 */
203 if (waitqueue_active(wq))
204 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
205}
206
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400207/*
208 * Look up entry in page cache, wait for it to become unlocked if it
209 * is a DAX entry and return it. The caller must subsequently call
210 * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
211 * if it did.
212 *
213 * Must be called with the i_pages lock held.
214 */
215static void *get_unlocked_entry(struct xa_state *xas)
216{
217 void *entry;
218 struct wait_exceptional_entry_queue ewait;
219 wait_queue_head_t *wq;
220
221 init_wait(&ewait.wait);
222 ewait.wait.func = wake_exceptional_entry_func;
223
224 for (;;) {
225 entry = xas_load(xas);
226 if (!entry || xa_is_internal(entry) ||
227 WARN_ON_ONCE(!xa_is_value(entry)) ||
228 !dax_is_locked(entry))
229 return entry;
230
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400231 wq = dax_entry_waitqueue(xas, entry, &ewait.key);
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400232 prepare_to_wait_exclusive(wq, &ewait.wait,
233 TASK_UNINTERRUPTIBLE);
234 xas_unlock_irq(xas);
235 xas_reset(xas);
236 schedule();
237 finish_wait(wq, &ewait.wait);
238 xas_lock_irq(xas);
239 }
240}
241
242static void put_unlocked_entry(struct xa_state *xas, void *entry)
243{
244 /* If we were the only waiter woken, wake the next one */
245 if (entry)
246 dax_wake_entry(xas, entry, false);
247}
248
249/*
250 * We used the xa_state to get the entry, but then we locked the entry and
251 * dropped the xa_lock, so we know the xa_state is stale and must be reset
252 * before use.
253 */
254static void dax_unlock_entry(struct xa_state *xas, void *entry)
255{
256 void *old;
257
Matthew Wilcox7ae2ea72018-11-09 20:09:37 -0500258 BUG_ON(dax_is_locked(entry));
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400259 xas_reset(xas);
260 xas_lock_irq(xas);
261 old = xas_store(xas, entry);
262 xas_unlock_irq(xas);
263 BUG_ON(!dax_is_locked(old));
264 dax_wake_entry(xas, entry, false);
265}
266
267/*
268 * Return: The entry stored at this location before it was locked.
269 */
270static void *dax_lock_entry(struct xa_state *xas, void *entry)
271{
272 unsigned long v = xa_to_value(entry);
273 return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
274}
275
Dan Williamsd2c997c2017-12-22 22:02:48 -0800276static unsigned long dax_entry_size(void *entry)
277{
278 if (dax_is_zero_entry(entry))
279 return 0;
280 else if (dax_is_empty_entry(entry))
281 return 0;
282 else if (dax_is_pmd_entry(entry))
283 return PMD_SIZE;
284 else
285 return PAGE_SIZE;
286}
287
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400288static unsigned long dax_end_pfn(void *entry)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800289{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400290 return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800291}
292
293/*
294 * Iterate through all mapped pfns represented by an entry, i.e. skip
295 * 'empty' and 'zero' entries.
296 */
297#define for_each_mapped_pfn(entry, pfn) \
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400298 for (pfn = dax_to_pfn(entry); \
299 pfn < dax_end_pfn(entry); pfn++)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800300
Dan Williams73449da2018-07-13 21:49:50 -0700301/*
302 * TODO: for reflink+dax we need a way to associate a single page with
303 * multiple address_space instances at different linear_page_index()
304 * offsets.
305 */
306static void dax_associate_entry(void *entry, struct address_space *mapping,
307 struct vm_area_struct *vma, unsigned long address)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800308{
Dan Williams73449da2018-07-13 21:49:50 -0700309 unsigned long size = dax_entry_size(entry), pfn, index;
310 int i = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800311
312 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
313 return;
314
Dan Williams73449da2018-07-13 21:49:50 -0700315 index = linear_page_index(vma, address & ~(size - 1));
Dan Williamsd2c997c2017-12-22 22:02:48 -0800316 for_each_mapped_pfn(entry, pfn) {
317 struct page *page = pfn_to_page(pfn);
318
319 WARN_ON_ONCE(page->mapping);
320 page->mapping = mapping;
Dan Williams73449da2018-07-13 21:49:50 -0700321 page->index = index + i++;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800322 }
323}
324
325static void dax_disassociate_entry(void *entry, struct address_space *mapping,
326 bool trunc)
327{
328 unsigned long pfn;
329
330 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
331 return;
332
333 for_each_mapped_pfn(entry, pfn) {
334 struct page *page = pfn_to_page(pfn);
335
336 WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
337 WARN_ON_ONCE(page->mapping && page->mapping != mapping);
338 page->mapping = NULL;
Dan Williams73449da2018-07-13 21:49:50 -0700339 page->index = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800340 }
341}
342
Dan Williams5fac7402018-03-09 17:44:31 -0800343static struct page *dax_busy_page(void *entry)
344{
345 unsigned long pfn;
346
347 for_each_mapped_pfn(entry, pfn) {
348 struct page *page = pfn_to_page(pfn);
349
350 if (page_ref_count(page) > 1)
351 return page;
352 }
353 return NULL;
354}
355
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500356/*
357 * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page
358 * @page: The page whose entry we want to lock
359 *
360 * Context: Process context.
361 * Return: %true if the entry was locked or does not need to be locked.
362 */
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700363bool dax_lock_mapping_entry(struct page *page)
364{
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400365 XA_STATE(xas, NULL, 0);
366 void *entry;
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500367 bool locked;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700368
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500369 /* Ensure page->mapping isn't freed while we look at it */
370 rcu_read_lock();
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700371 for (;;) {
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400372 struct address_space *mapping = READ_ONCE(page->mapping);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700373
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500374 locked = false;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700375 if (!dax_mapping(mapping))
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500376 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700377
378 /*
379 * In the device-dax case there's no need to lock, a
380 * struct dev_pagemap pin is sufficient to keep the
381 * inode alive, and we assume we have dev_pagemap pin
382 * otherwise we would not have a valid pfn_to_page()
383 * translation.
384 */
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500385 locked = true;
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400386 if (S_ISCHR(mapping->host->i_mode))
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500387 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700388
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400389 xas.xa = &mapping->i_pages;
390 xas_lock_irq(&xas);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700391 if (mapping != page->mapping) {
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400392 xas_unlock_irq(&xas);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700393 continue;
394 }
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400395 xas_set(&xas, page->index);
396 entry = xas_load(&xas);
397 if (dax_is_locked(entry)) {
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500398 rcu_read_unlock();
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400399 entry = get_unlocked_entry(&xas);
Matthew Wilcox6d7cd8c2018-11-06 13:11:57 -0500400 xas_unlock_irq(&xas);
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500401 rcu_read_lock();
Matthew Wilcox6d7cd8c2018-11-06 13:11:57 -0500402 continue;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700403 }
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400404 dax_lock_entry(&xas, entry);
405 xas_unlock_irq(&xas);
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500406 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700407 }
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500408 rcu_read_unlock();
409 return locked;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700410}
411
412void dax_unlock_mapping_entry(struct page *page)
413{
414 struct address_space *mapping = page->mapping;
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400415 XA_STATE(xas, &mapping->i_pages, page->index);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700416
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400417 if (S_ISCHR(mapping->host->i_mode))
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700418 return;
419
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400420 dax_unlock_entry(&xas, dax_make_page_entry(page));
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700421}
422
Jan Karaac401cc2016-05-12 18:29:18 +0200423/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400424 * Find page cache entry at given index. If it is a DAX entry, return it
425 * with the entry locked. If the page cache doesn't contain an entry at
426 * that index, add a locked empty entry.
Jan Karaac401cc2016-05-12 18:29:18 +0200427 *
Matthew Wilcox3159f942017-11-03 13:30:42 -0400428 * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400429 * either return that locked entry or will return VM_FAULT_FALLBACK.
430 * This will happen if there are any PTE entries within the PMD range
431 * that we are requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100432 *
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400433 * We always favor PTE entries over PMD entries. There isn't a flow where we
434 * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD
435 * insertion will fail if it finds any PTE entries already in the tree, and a
436 * PTE insertion will cause an existing PMD entry to be unmapped and
437 * downgraded to PTE entries. This happens for both PMD zero pages as
438 * well as PMD empty entries.
Ross Zwisler642261a2016-11-08 11:34:45 +1100439 *
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400440 * The exception to this downgrade path is for PMD entries that have
441 * real storage backing them. We will leave these real PMD entries in
442 * the tree, and PTE writes will simply dirty the entire PMD entry.
Ross Zwisler642261a2016-11-08 11:34:45 +1100443 *
Jan Karaac401cc2016-05-12 18:29:18 +0200444 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
445 * persistent memory the benefit is doubtful. We can add that later if we can
446 * show it helps.
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400447 *
448 * On error, this function does not return an ERR_PTR. Instead it returns
449 * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values
450 * overlap with xarray value entries.
Jan Karaac401cc2016-05-12 18:29:18 +0200451 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400452static void *grab_mapping_entry(struct xa_state *xas,
453 struct address_space *mapping, unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200454{
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400455 unsigned long index = xas->xa_index;
456 bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
457 void *entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200458
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400459retry:
460 xas_lock_irq(xas);
461 entry = get_unlocked_entry(xas);
462 if (xa_is_internal(entry))
463 goto fallback;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700464
Ross Zwisler642261a2016-11-08 11:34:45 +1100465 if (entry) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400466 if (WARN_ON_ONCE(!xa_is_value(entry))) {
467 xas_set_err(xas, EIO);
468 goto out_unlock;
469 }
470
Matthew Wilcox3159f942017-11-03 13:30:42 -0400471 if (size_flag & DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700472 if (dax_is_pte_entry(entry)) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400473 put_unlocked_entry(xas, entry);
474 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +1100475 }
476 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700477 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100478 (dax_is_zero_entry(entry) ||
479 dax_is_empty_entry(entry))) {
480 pmd_downgrade = true;
481 }
482 }
483 }
484
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400485 if (pmd_downgrade) {
486 /*
487 * Make sure 'entry' remains valid while we drop
488 * the i_pages lock.
489 */
490 dax_lock_entry(xas, entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200491
Ross Zwisler642261a2016-11-08 11:34:45 +1100492 /*
493 * Besides huge zero pages the only other thing that gets
494 * downgraded are empty entries which don't need to be
495 * unmapped.
496 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400497 if (dax_is_zero_entry(entry)) {
498 xas_unlock_irq(xas);
499 unmap_mapping_pages(mapping,
500 xas->xa_index & ~PG_PMD_COLOUR,
501 PG_PMD_NR, false);
502 xas_reset(xas);
503 xas_lock_irq(xas);
Ross Zwislere11f8b72017-04-07 16:04:57 -0700504 }
505
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400506 dax_disassociate_entry(entry, mapping, false);
507 xas_store(xas, NULL); /* undo the PMD join */
508 dax_wake_entry(xas, entry, true);
509 mapping->nrexceptional--;
510 entry = NULL;
511 xas_set(xas, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200512 }
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400513
514 if (entry) {
515 dax_lock_entry(xas, entry);
516 } else {
517 entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
518 dax_lock_entry(xas, entry);
519 if (xas_error(xas))
520 goto out_unlock;
521 mapping->nrexceptional++;
522 }
523
524out_unlock:
525 xas_unlock_irq(xas);
526 if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
527 goto retry;
528 if (xas->xa_node == XA_ERROR(-ENOMEM))
529 return xa_mk_internal(VM_FAULT_OOM);
530 if (xas_error(xas))
531 return xa_mk_internal(VM_FAULT_SIGBUS);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100532 return entry;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400533fallback:
534 xas_unlock_irq(xas);
535 return xa_mk_internal(VM_FAULT_FALLBACK);
Jan Karaac401cc2016-05-12 18:29:18 +0200536}
537
Dan Williams5fac7402018-03-09 17:44:31 -0800538/**
539 * dax_layout_busy_page - find first pinned page in @mapping
540 * @mapping: address space to scan for a page with ref count > 1
541 *
542 * DAX requires ZONE_DEVICE mapped pages. These pages are never
543 * 'onlined' to the page allocator so they are considered idle when
544 * page->count == 1. A filesystem uses this interface to determine if
545 * any page in the mapping is busy, i.e. for DMA, or other
546 * get_user_pages() usages.
547 *
548 * It is expected that the filesystem is holding locks to block the
549 * establishment of new mappings in this address_space. I.e. it expects
550 * to be able to run unmap_mapping_range() and subsequently not race
551 * mapping_mapped() becoming true.
552 */
553struct page *dax_layout_busy_page(struct address_space *mapping)
554{
Matthew Wilcox084a8992018-05-17 13:03:48 -0400555 XA_STATE(xas, &mapping->i_pages, 0);
556 void *entry;
557 unsigned int scanned = 0;
Dan Williams5fac7402018-03-09 17:44:31 -0800558 struct page *page = NULL;
Dan Williams5fac7402018-03-09 17:44:31 -0800559
560 /*
561 * In the 'limited' case get_user_pages() for dax is disabled.
562 */
563 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
564 return NULL;
565
566 if (!dax_mapping(mapping) || !mapping_mapped(mapping))
567 return NULL;
568
Dan Williams5fac7402018-03-09 17:44:31 -0800569 /*
570 * If we race get_user_pages_fast() here either we'll see the
Matthew Wilcox084a8992018-05-17 13:03:48 -0400571 * elevated page count in the iteration and wait, or
Dan Williams5fac7402018-03-09 17:44:31 -0800572 * get_user_pages_fast() will see that the page it took a reference
573 * against is no longer mapped in the page tables and bail to the
574 * get_user_pages() slow path. The slow path is protected by
575 * pte_lock() and pmd_lock(). New references are not taken without
576 * holding those locks, and unmap_mapping_range() will not zero the
577 * pte or pmd without holding the respective lock, so we are
578 * guaranteed to either see new references or prevent new
579 * references from being established.
580 */
581 unmap_mapping_range(mapping, 0, 0, 1);
582
Matthew Wilcox084a8992018-05-17 13:03:48 -0400583 xas_lock_irq(&xas);
584 xas_for_each(&xas, entry, ULONG_MAX) {
585 if (WARN_ON_ONCE(!xa_is_value(entry)))
586 continue;
587 if (unlikely(dax_is_locked(entry)))
588 entry = get_unlocked_entry(&xas);
589 if (entry)
590 page = dax_busy_page(entry);
591 put_unlocked_entry(&xas, entry);
Dan Williams5fac7402018-03-09 17:44:31 -0800592 if (page)
593 break;
Matthew Wilcox084a8992018-05-17 13:03:48 -0400594 if (++scanned % XA_CHECK_SCHED)
595 continue;
596
597 xas_pause(&xas);
598 xas_unlock_irq(&xas);
599 cond_resched();
600 xas_lock_irq(&xas);
Dan Williams5fac7402018-03-09 17:44:31 -0800601 }
Matthew Wilcox084a8992018-05-17 13:03:48 -0400602 xas_unlock_irq(&xas);
Dan Williams5fac7402018-03-09 17:44:31 -0800603 return page;
604}
605EXPORT_SYMBOL_GPL(dax_layout_busy_page);
606
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400607static int __dax_invalidate_entry(struct address_space *mapping,
Jan Karac6dcf522016-08-10 17:22:44 +0200608 pgoff_t index, bool trunc)
609{
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400610 XA_STATE(xas, &mapping->i_pages, index);
Jan Karac6dcf522016-08-10 17:22:44 +0200611 int ret = 0;
612 void *entry;
Jan Karac6dcf522016-08-10 17:22:44 +0200613
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400614 xas_lock_irq(&xas);
615 entry = get_unlocked_entry(&xas);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400616 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200617 goto out;
618 if (!trunc &&
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400619 (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
620 xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
Jan Karac6dcf522016-08-10 17:22:44 +0200621 goto out;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800622 dax_disassociate_entry(entry, mapping, trunc);
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400623 xas_store(&xas, NULL);
Jan Karac6dcf522016-08-10 17:22:44 +0200624 mapping->nrexceptional--;
625 ret = 1;
626out:
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400627 put_unlocked_entry(&xas, entry);
628 xas_unlock_irq(&xas);
Jan Karac6dcf522016-08-10 17:22:44 +0200629 return ret;
630}
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400631
Jan Karaac401cc2016-05-12 18:29:18 +0200632/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400633 * Delete DAX entry at @index from @mapping. Wait for it
634 * to be unlocked before deleting it.
Jan Karaac401cc2016-05-12 18:29:18 +0200635 */
636int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
637{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400638 int ret = __dax_invalidate_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200639
Jan Karaac401cc2016-05-12 18:29:18 +0200640 /*
641 * This gets called from truncate / punch_hole path. As such, the caller
642 * must hold locks protecting against concurrent modifications of the
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400643 * page cache (usually fs-private i_mmap_sem for writing). Since the
Matthew Wilcox3159f942017-11-03 13:30:42 -0400644 * caller has seen a DAX entry for this index, we better find it
Jan Karaac401cc2016-05-12 18:29:18 +0200645 * at that index as well...
646 */
Jan Karac6dcf522016-08-10 17:22:44 +0200647 WARN_ON_ONCE(!ret);
648 return ret;
649}
Jan Karaac401cc2016-05-12 18:29:18 +0200650
Jan Karac6dcf522016-08-10 17:22:44 +0200651/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400652 * Invalidate DAX entry if it is clean.
Jan Karac6dcf522016-08-10 17:22:44 +0200653 */
654int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
655 pgoff_t index)
656{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400657 return __dax_invalidate_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200658}
659
Dan Williamscccbce62017-01-27 13:31:42 -0800660static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
661 sector_t sector, size_t size, struct page *to,
662 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800663{
Dan Williamscccbce62017-01-27 13:31:42 -0800664 void *vto, *kaddr;
665 pgoff_t pgoff;
Dan Williamscccbce62017-01-27 13:31:42 -0800666 long rc;
667 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600668
Dan Williamscccbce62017-01-27 13:31:42 -0800669 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
670 if (rc)
671 return rc;
672
673 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800674 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -0800675 if (rc < 0) {
676 dax_read_unlock(id);
677 return rc;
678 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800679 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800680 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800681 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800682 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800683 return 0;
684}
685
Ross Zwisler642261a2016-11-08 11:34:45 +1100686/*
687 * By this point grab_mapping_entry() has ensured that we have a locked entry
688 * of the appropriate size so we don't have to worry about downgrading PMDs to
689 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
690 * already in the tree, we will skip the insertion and just dirty the PMD as
691 * appropriate.
692 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400693static void *dax_insert_entry(struct xa_state *xas,
694 struct address_space *mapping, struct vm_fault *vmf,
695 void *entry, pfn_t pfn, unsigned long flags, bool dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800696{
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400697 void *new_entry = dax_make_entry(pfn, flags);
Ross Zwisler9973c982016-01-22 15:10:47 -0800698
Jan Karaf5b7b742017-11-01 16:36:40 +0100699 if (dirty)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800700 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800701
Matthew Wilcox3159f942017-11-03 13:30:42 -0400702 if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400703 unsigned long index = xas->xa_index;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700704 /* we are replacing a zero page with block mapping */
705 if (dax_is_pmd_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800706 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400707 PG_PMD_NR, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700708 else /* pte entry */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400709 unmap_mapping_pages(mapping, index, 1, false);
Ross Zwisler9973c982016-01-22 15:10:47 -0800710 }
711
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400712 xas_reset(xas);
713 xas_lock_irq(xas);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800714 if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
715 dax_disassociate_entry(entry, mapping, false);
Dan Williams73449da2018-07-13 21:49:50 -0700716 dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800717 }
Ross Zwisler642261a2016-11-08 11:34:45 +1100718
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700719 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100720 /*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400721 * Only swap our new entry into the page cache if the current
Ross Zwisler642261a2016-11-08 11:34:45 +1100722 * entry is a zero page or an empty entry. If a normal PTE or
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400723 * PMD entry is already in the cache, we leave it alone. This
Ross Zwisler642261a2016-11-08 11:34:45 +1100724 * means that if we are trying to insert a PTE and the
725 * existing entry is a PMD, we will just leave the PMD in the
726 * tree and dirty it if necessary.
727 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400728 void *old = dax_lock_entry(xas, new_entry);
729 WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
730 DAX_LOCKED));
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700731 entry = new_entry;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400732 } else {
733 xas_load(xas); /* Walk the xa_state */
Ross Zwisler9973c982016-01-22 15:10:47 -0800734 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700735
Jan Karaf5b7b742017-11-01 16:36:40 +0100736 if (dirty)
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400737 xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700738
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400739 xas_unlock_irq(xas);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700740 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800741}
742
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400743static inline
744unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
Jan Kara4b4bb462016-12-14 15:07:53 -0800745{
746 unsigned long address;
747
748 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
749 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
750 return address;
751}
752
753/* Walk all mappings of a given index of a file and writeprotect them */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400754static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
755 unsigned long pfn)
Jan Kara4b4bb462016-12-14 15:07:53 -0800756{
757 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800758 pte_t pte, *ptep = NULL;
759 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800760 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800761
762 i_mmap_lock_read(mapping);
763 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400764 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800765
766 cond_resched();
767
768 if (!(vma->vm_flags & VM_SHARED))
769 continue;
770
771 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400772
773 /*
774 * Note because we provide start/end to follow_pte_pmd it will
775 * call mmu_notifier_invalidate_range_start() on our behalf
776 * before taking any lock.
777 */
778 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800779 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800780
Jérôme Glisse0f108512017-11-15 17:34:07 -0800781 /*
782 * No need to call mmu_notifier_invalidate_range() as we are
783 * downgrading page table protection not changing it to point
784 * to a new page.
785 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200786 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800787 */
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800788 if (pmdp) {
789#ifdef CONFIG_FS_DAX_PMD
790 pmd_t pmd;
791
792 if (pfn != pmd_pfn(*pmdp))
793 goto unlock_pmd;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800794 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800795 goto unlock_pmd;
796
797 flush_cache_page(vma, address, pfn);
798 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
799 pmd = pmd_wrprotect(pmd);
800 pmd = pmd_mkclean(pmd);
801 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800802unlock_pmd:
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800803#endif
Jan H. Schönherree190ca2018-01-31 16:14:04 -0800804 spin_unlock(ptl);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800805 } else {
806 if (pfn != pte_pfn(*ptep))
807 goto unlock_pte;
808 if (!pte_dirty(*ptep) && !pte_write(*ptep))
809 goto unlock_pte;
810
811 flush_cache_page(vma, address, pfn);
812 pte = ptep_clear_flush(vma, address, ptep);
813 pte = pte_wrprotect(pte);
814 pte = pte_mkclean(pte);
815 set_pte_at(vma->vm_mm, address, ptep, pte);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800816unlock_pte:
817 pte_unmap_unlock(ptep, ptl);
818 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800819
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400820 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800821 }
822 i_mmap_unlock_read(mapping);
823}
824
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400825static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
826 struct address_space *mapping, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800827{
Dan Williams3fe07912017-10-14 17:13:45 -0700828 unsigned long pfn;
829 long ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800830 size_t size;
Ross Zwisler9973c982016-01-22 15:10:47 -0800831
Ross Zwisler9973c982016-01-22 15:10:47 -0800832 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800833 * A page got tagged dirty in DAX mapping? Something is seriously
834 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800835 */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400836 if (WARN_ON(!xa_is_value(entry)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800837 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800838
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400839 if (unlikely(dax_is_locked(entry))) {
840 void *old_entry = entry;
841
842 entry = get_unlocked_entry(xas);
843
844 /* Entry got punched out / reallocated? */
845 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
846 goto put_unlocked;
847 /*
848 * Entry got reallocated elsewhere? No need to writeback.
849 * We have to compare pfns as we must not bail out due to
850 * difference in lockbit or entry type.
851 */
852 if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
853 goto put_unlocked;
854 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
855 dax_is_zero_entry(entry))) {
856 ret = -EIO;
857 goto put_unlocked;
858 }
859
860 /* Another fsync thread may have already done this entry */
861 if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
862 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800863 }
864
Jan Karaa6abc2c2016-12-14 15:07:47 -0800865 /* Lock the entry to serialize with page faults */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400866 dax_lock_entry(xas, entry);
867
Jan Karaa6abc2c2016-12-14 15:07:47 -0800868 /*
869 * We can clear the tag now but we have to be careful so that concurrent
870 * dax_writeback_one() calls for the same index cannot finish before we
871 * actually flush the caches. This is achieved as the calls will look
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700872 * at the entry only under the i_pages lock and once they do that
873 * they will see the entry locked and wait for it to unlock.
Jan Karaa6abc2c2016-12-14 15:07:47 -0800874 */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400875 xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
876 xas_unlock_irq(xas);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800877
Ross Zwisler642261a2016-11-08 11:34:45 +1100878 /*
879 * Even if dax_writeback_mapping_range() was given a wbc->range_start
880 * in the middle of a PMD, the 'index' we are given will be aligned to
Dan Williams3fe07912017-10-14 17:13:45 -0700881 * the start index of the PMD, as will the pfn we pull from 'entry'.
882 * This allows us to flush for PMD_SIZE and not have to worry about
883 * partial PMD writebacks.
Ross Zwisler642261a2016-11-08 11:34:45 +1100884 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400885 pfn = dax_to_pfn(entry);
886 size = PAGE_SIZE << dax_entry_order(entry);
Dan Williamscccbce62017-01-27 13:31:42 -0800887
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400888 dax_entry_mkclean(mapping, xas->xa_index, pfn);
Dan Williams3fe07912017-10-14 17:13:45 -0700889 dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800890 /*
891 * After we have flushed the cache, we can clear the dirty tag. There
892 * cannot be new dirty data in the pfn after the flush has completed as
893 * the pfn mappings are writeprotected and fault waits for mapping
894 * entry lock.
895 */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400896 xas_reset(xas);
897 xas_lock_irq(xas);
898 xas_store(xas, entry);
899 xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
900 dax_wake_entry(xas, entry, false);
901
902 trace_dax_writeback_one(mapping->host, xas->xa_index,
903 size >> PAGE_SHIFT);
Ross Zwisler9973c982016-01-22 15:10:47 -0800904 return ret;
905
Jan Karaa6abc2c2016-12-14 15:07:47 -0800906 put_unlocked:
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400907 put_unlocked_entry(xas, entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800908 return ret;
909}
910
911/*
912 * Flush the mapping to the persistent domain within the byte range of [start,
913 * end]. This is required by data integrity operations to ensure file data is
914 * on persistent storage prior to completion of the operation.
915 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800916int dax_writeback_mapping_range(struct address_space *mapping,
917 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800918{
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400919 XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
Ross Zwisler9973c982016-01-22 15:10:47 -0800920 struct inode *inode = mapping->host;
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400921 pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
Dan Williamscccbce62017-01-27 13:31:42 -0800922 struct dax_device *dax_dev;
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400923 void *entry;
924 int ret = 0;
925 unsigned int scanned = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800926
927 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
928 return -EIO;
929
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800930 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
931 return 0;
932
Dan Williamscccbce62017-01-27 13:31:42 -0800933 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
934 if (!dax_dev)
935 return -EIO;
936
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400937 trace_dax_writeback_range(inode, xas.xa_index, end_index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800938
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400939 tag_pages_for_writeback(mapping, xas.xa_index, end_index);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700940
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400941 xas_lock_irq(&xas);
942 xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
943 ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
944 if (ret < 0) {
945 mapping_set_error(mapping, ret);
Ross Zwisler9973c982016-01-22 15:10:47 -0800946 break;
Ross Zwisler9973c982016-01-22 15:10:47 -0800947 }
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400948 if (++scanned % XA_CHECK_SCHED)
949 continue;
950
951 xas_pause(&xas);
952 xas_unlock_irq(&xas);
953 cond_resched();
954 xas_lock_irq(&xas);
Ross Zwisler9973c982016-01-22 15:10:47 -0800955 }
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400956 xas_unlock_irq(&xas);
Dan Williamscccbce62017-01-27 13:31:42 -0800957 put_dax(dax_dev);
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400958 trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
959 return ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800960}
961EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
962
Jan Kara31a6f1a2017-11-01 16:36:32 +0100963static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800964{
Linus Torvaldsa3841f92017-11-17 09:51:57 -0800965 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
Jan Kara31a6f1a2017-11-01 16:36:32 +0100966}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800967
Jan Kara5e161e42017-11-01 16:36:33 +0100968static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
969 pfn_t *pfnp)
970{
971 const sector_t sector = dax_iomap_sector(iomap, pos);
972 pgoff_t pgoff;
Jan Kara5e161e42017-11-01 16:36:33 +0100973 int id, rc;
974 long length;
975
976 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800977 if (rc)
978 return rc;
Dan Williamscccbce62017-01-27 13:31:42 -0800979 id = dax_read_lock();
Jan Kara5e161e42017-11-01 16:36:33 +0100980 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800981 NULL, pfnp);
Jan Kara5e161e42017-11-01 16:36:33 +0100982 if (length < 0) {
983 rc = length;
984 goto out;
Dan Williamscccbce62017-01-27 13:31:42 -0800985 }
Jan Kara5e161e42017-11-01 16:36:33 +0100986 rc = -EINVAL;
987 if (PFN_PHYS(length) < size)
988 goto out;
989 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
990 goto out;
991 /* For larger pages we need devmap */
992 if (length > 1 && !pfn_t_devmap(*pfnp))
993 goto out;
994 rc = 0;
995out:
Dan Williamscccbce62017-01-27 13:31:42 -0800996 dax_read_unlock(id);
Jan Kara5e161e42017-11-01 16:36:33 +0100997 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800998}
999
Ross Zwislere30331f2017-09-06 16:18:39 -07001000/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001001 * The user has performed a load from a hole in the file. Allocating a new
1002 * page in the file would cause excessive storage usage for workloads with
1003 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
1004 * If this page is ever written to we will re-fault and change the mapping to
1005 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -07001006 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001007static vm_fault_t dax_load_hole(struct xa_state *xas,
1008 struct address_space *mapping, void **entry,
1009 struct vm_fault *vmf)
Ross Zwislere30331f2017-09-06 16:18:39 -07001010{
1011 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001012 unsigned long vaddr = vmf->address;
Matthew Wilcoxb90ca5c2018-09-11 21:27:44 -07001013 pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1014 vm_fault_t ret;
Ross Zwislere30331f2017-09-06 16:18:39 -07001015
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001016 *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001017 DAX_ZERO_PAGE, false);
1018
Souptick Joarderab77dab2018-06-07 17:04:29 -07001019 ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
Ross Zwislere30331f2017-09-06 16:18:39 -07001020 trace_dax_load_hole(inode, vmf, ret);
1021 return ret;
1022}
1023
Vishal Verma4b0228f2016-04-21 15:13:46 -04001024static bool dax_range_is_aligned(struct block_device *bdev,
1025 unsigned int offset, unsigned int length)
1026{
1027 unsigned short sector_size = bdev_logical_block_size(bdev);
1028
1029 if (!IS_ALIGNED(offset, sector_size))
1030 return false;
1031 if (!IS_ALIGNED(length, sector_size))
1032 return false;
1033
1034 return true;
1035}
1036
Dan Williamscccbce62017-01-27 13:31:42 -08001037int __dax_zero_page_range(struct block_device *bdev,
1038 struct dax_device *dax_dev, sector_t sector,
1039 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001040{
Dan Williamscccbce62017-01-27 13:31:42 -08001041 if (dax_range_is_aligned(bdev, offset, size)) {
1042 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001043
1044 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -07001045 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001046 } else {
Dan Williamscccbce62017-01-27 13:31:42 -08001047 pgoff_t pgoff;
1048 long rc, id;
1049 void *kaddr;
Dan Williamscccbce62017-01-27 13:31:42 -08001050
Dan Williamse84b83b2017-05-10 19:38:13 -07001051 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001052 if (rc)
1053 return rc;
1054
1055 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001056 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -08001057 if (rc < 0) {
1058 dax_read_unlock(id);
1059 return rc;
1060 }
Dan Williams81f55872017-05-29 13:12:20 -07001061 memset(kaddr + offset, 0, size);
Mikulas Patockac3ca0152017-08-31 21:47:43 -04001062 dax_flush(dax_dev, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -08001063 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001064 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001065 return 0;
1066}
1067EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1068
Christoph Hellwiga254e562016-09-19 11:24:49 +10001069static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001070dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001071 struct iomap *iomap)
1072{
Dan Williamscccbce62017-01-27 13:31:42 -08001073 struct block_device *bdev = iomap->bdev;
1074 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001075 struct iov_iter *iter = data;
1076 loff_t end = pos + length, done = 0;
1077 ssize_t ret = 0;
Dan Williamsa77d4782018-03-16 17:36:44 -07001078 size_t xfer;
Dan Williamscccbce62017-01-27 13:31:42 -08001079 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001080
1081 if (iov_iter_rw(iter) == READ) {
1082 end = min(end, i_size_read(inode));
1083 if (pos >= end)
1084 return 0;
1085
1086 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1087 return iov_iter_zero(min(length, end - pos), iter);
1088 }
1089
1090 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1091 return -EIO;
1092
Jan Karae3fce682016-08-10 17:10:28 +02001093 /*
1094 * Write can allocate block for an area which has a hole page mapped
1095 * into page tables. We have to tear down these mappings so that data
1096 * written by write(2) is visible in mmap.
1097 */
Jan Karacd656372017-05-12 15:46:50 -07001098 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +02001099 invalidate_inode_pages2_range(inode->i_mapping,
1100 pos >> PAGE_SHIFT,
1101 (end - 1) >> PAGE_SHIFT);
1102 }
1103
Dan Williamscccbce62017-01-27 13:31:42 -08001104 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +10001105 while (pos < end) {
1106 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -08001107 const size_t size = ALIGN(length + offset, PAGE_SIZE);
1108 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001109 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -08001110 pgoff_t pgoff;
1111 void *kaddr;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001112
Michal Hockod1908f52017-02-03 13:13:26 -08001113 if (fatal_signal_pending(current)) {
1114 ret = -EINTR;
1115 break;
1116 }
1117
Dan Williamscccbce62017-01-27 13:31:42 -08001118 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1119 if (ret)
1120 break;
1121
1122 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001123 &kaddr, NULL);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001124 if (map_len < 0) {
1125 ret = map_len;
1126 break;
1127 }
1128
Dan Williamscccbce62017-01-27 13:31:42 -08001129 map_len = PFN_PHYS(map_len);
1130 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001131 map_len -= offset;
1132 if (map_len > end - pos)
1133 map_len = end - pos;
1134
Ross Zwislera2e050f2017-09-06 16:18:54 -07001135 /*
1136 * The userspace address for the memory copy has already been
1137 * validated via access_ok() in either vfs_read() or
1138 * vfs_write(), depending on which operation we are doing.
1139 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001140 if (iov_iter_rw(iter) == WRITE)
Dan Williamsa77d4782018-03-16 17:36:44 -07001141 xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
Dan Williamsfec53772017-05-29 21:56:49 -07001142 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001143 else
Dan Williamsa77d4782018-03-16 17:36:44 -07001144 xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001145 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001146
Dan Williamsa77d4782018-03-16 17:36:44 -07001147 pos += xfer;
1148 length -= xfer;
1149 done += xfer;
1150
1151 if (xfer == 0)
1152 ret = -EFAULT;
1153 if (xfer < map_len)
1154 break;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001155 }
Dan Williamscccbce62017-01-27 13:31:42 -08001156 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001157
1158 return done ? done : ret;
1159}
1160
1161/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001162 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001163 * @iocb: The control block for this I/O
1164 * @iter: The addresses to do I/O from or to
1165 * @ops: iomap ops passed from the file system
1166 *
1167 * This function performs read and write operations to directly mapped
1168 * persistent memory. The callers needs to take care of read/write exclusion
1169 * and evicting any page cache pages in the region under I/O.
1170 */
1171ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001172dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001173 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001174{
1175 struct address_space *mapping = iocb->ki_filp->f_mapping;
1176 struct inode *inode = mapping->host;
1177 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1178 unsigned flags = 0;
1179
Christoph Hellwig168316d2017-02-08 14:43:13 -05001180 if (iov_iter_rw(iter) == WRITE) {
1181 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001182 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001183 } else {
1184 lockdep_assert_held(&inode->i_rwsem);
1185 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001186
Christoph Hellwiga254e562016-09-19 11:24:49 +10001187 while (iov_iter_count(iter)) {
1188 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001189 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001190 if (ret <= 0)
1191 break;
1192 pos += ret;
1193 done += ret;
1194 }
1195
1196 iocb->ki_pos += done;
1197 return done ? done : ret;
1198}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001199EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001200
Souptick Joarderab77dab2018-06-07 17:04:29 -07001201static vm_fault_t dax_fault_return(int error)
Jan Kara9f141d62016-10-19 14:34:31 +02001202{
1203 if (error == 0)
1204 return VM_FAULT_NOPAGE;
1205 if (error == -ENOMEM)
1206 return VM_FAULT_OOM;
1207 return VM_FAULT_SIGBUS;
1208}
1209
Dan Williamsaaa422c2017-11-13 16:38:44 -08001210/*
1211 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1212 * flushed on write-faults (non-cow), but not read-faults.
1213 */
1214static bool dax_fault_is_synchronous(unsigned long flags,
1215 struct vm_area_struct *vma, struct iomap *iomap)
1216{
1217 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1218 && (iomap->flags & IOMAP_F_DIRTY);
1219}
1220
Souptick Joarderab77dab2018-06-07 17:04:29 -07001221static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
Jan Karac0b24622018-01-07 16:38:43 -05001222 int *iomap_errp, const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001223{
Jan Karaa0987ad2017-11-01 16:36:34 +01001224 struct vm_area_struct *vma = vmf->vma;
1225 struct address_space *mapping = vma->vm_file->f_mapping;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001226 XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001227 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001228 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001229 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001230 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001231 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001232 int error, major = 0;
Jan Karad2c43ef2017-11-01 16:36:35 +01001233 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001234 bool sync;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001235 vm_fault_t ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001236 void *entry;
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001237 pfn_t pfn;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001238
Souptick Joarderab77dab2018-06-07 17:04:29 -07001239 trace_dax_pte_fault(inode, vmf, ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001240 /*
1241 * Check whether offset isn't beyond end of file now. Caller is supposed
1242 * to hold locks serializing us with truncate / punch hole so this is
1243 * a reliable test.
1244 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001245 if (pos >= i_size_read(inode)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001246 ret = VM_FAULT_SIGBUS;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001247 goto out;
1248 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001249
Jan Karad2c43ef2017-11-01 16:36:35 +01001250 if (write && !vmf->cow_page)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001251 flags |= IOMAP_WRITE;
1252
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001253 entry = grab_mapping_entry(&xas, mapping, 0);
1254 if (xa_is_internal(entry)) {
1255 ret = xa_to_internal(entry);
Jan Kara13e451f2017-05-12 15:46:57 -07001256 goto out;
1257 }
1258
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001259 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001260 * It is possible, particularly with mixed reads & writes to private
1261 * mappings, that we have raced with a PMD fault that overlaps with
1262 * the PTE we need to set up. If so just return and the fault will be
1263 * retried.
1264 */
1265 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001266 ret = VM_FAULT_NOPAGE;
Ross Zwislere2093922017-06-02 14:46:37 -07001267 goto unlock_entry;
1268 }
1269
1270 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001271 * Note that we don't bother to use iomap_apply here: DAX required
1272 * the file system block size to be equal the page size, which means
1273 * that we never have to deal with more than a single extent here.
1274 */
1275 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Jan Karac0b24622018-01-07 16:38:43 -05001276 if (iomap_errp)
1277 *iomap_errp = error;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001278 if (error) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001279 ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001280 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001281 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001282 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001283 error = -EIO; /* fs corruption? */
1284 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001285 }
1286
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001287 if (vmf->cow_page) {
Jan Kara31a6f1a2017-11-01 16:36:32 +01001288 sector_t sector = dax_iomap_sector(&iomap, pos);
1289
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001290 switch (iomap.type) {
1291 case IOMAP_HOLE:
1292 case IOMAP_UNWRITTEN:
1293 clear_user_highpage(vmf->cow_page, vaddr);
1294 break;
1295 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001296 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1297 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001298 break;
1299 default:
1300 WARN_ON_ONCE(1);
1301 error = -EIO;
1302 break;
1303 }
1304
1305 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001306 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001307
1308 __SetPageUptodate(vmf->cow_page);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001309 ret = finish_fault(vmf);
1310 if (!ret)
1311 ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001312 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001313 }
1314
Dan Williamsaaa422c2017-11-13 16:38:44 -08001315 sync = dax_fault_is_synchronous(flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001316
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001317 switch (iomap.type) {
1318 case IOMAP_MAPPED:
1319 if (iomap.flags & IOMAP_F_NEW) {
1320 count_vm_event(PGMAJFAULT);
Jan Karaa0987ad2017-11-01 16:36:34 +01001321 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001322 major = VM_FAULT_MAJOR;
1323 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001324 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1325 if (error < 0)
1326 goto error_finish_iomap;
1327
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001328 entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
Jan Karacaa51d22017-11-01 16:36:42 +01001329 0, write && !sync);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001330
Jan Karacaa51d22017-11-01 16:36:42 +01001331 /*
1332 * If we are doing synchronous page fault and inode needs fsync,
1333 * we can insert PTE into page tables only after that happens.
1334 * Skip insertion for now and return the pfn so that caller can
1335 * insert it after fsync is done.
1336 */
1337 if (sync) {
1338 if (WARN_ON_ONCE(!pfnp)) {
1339 error = -EIO;
1340 goto error_finish_iomap;
1341 }
1342 *pfnp = pfn;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001343 ret = VM_FAULT_NEEDDSYNC | major;
Jan Karacaa51d22017-11-01 16:36:42 +01001344 goto finish_iomap;
1345 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001346 trace_dax_insert_mapping(inode, vmf, entry);
1347 if (write)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001348 ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001349 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001350 ret = vmf_insert_mixed(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001351
Souptick Joarderab77dab2018-06-07 17:04:29 -07001352 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001353 case IOMAP_UNWRITTEN:
1354 case IOMAP_HOLE:
Jan Karad2c43ef2017-11-01 16:36:35 +01001355 if (!write) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001356 ret = dax_load_hole(&xas, mapping, &entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001357 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001358 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001359 /*FALLTHRU*/
1360 default:
1361 WARN_ON_ONCE(1);
1362 error = -EIO;
1363 break;
1364 }
1365
Jan Kara13e451f2017-05-12 15:46:57 -07001366 error_finish_iomap:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001367 ret = dax_fault_return(error);
Jan Kara9f141d62016-10-19 14:34:31 +02001368 finish_iomap:
1369 if (ops->iomap_end) {
1370 int copied = PAGE_SIZE;
1371
Souptick Joarderab77dab2018-06-07 17:04:29 -07001372 if (ret & VM_FAULT_ERROR)
Jan Kara9f141d62016-10-19 14:34:31 +02001373 copied = 0;
1374 /*
1375 * The fault is done by now and there's no way back (other
1376 * thread may be already happily using PTE we have installed).
1377 * Just ignore error from ->iomap_end since we cannot do much
1378 * with it.
1379 */
1380 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001381 }
Jan Kara13e451f2017-05-12 15:46:57 -07001382 unlock_entry:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001383 dax_unlock_entry(&xas, entry);
Jan Kara13e451f2017-05-12 15:46:57 -07001384 out:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001385 trace_dax_pte_fault_done(inode, vmf, ret);
1386 return ret | major;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001387}
Ross Zwisler642261a2016-11-08 11:34:45 +11001388
1389#ifdef CONFIG_FS_DAX_PMD
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001390static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
1391 struct iomap *iomap, void **entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001392{
Dave Jiangf4200392017-02-22 15:40:06 -08001393 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1394 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001395 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001396 struct page *zero_page;
1397 spinlock_t *ptl;
1398 pmd_t pmd_entry;
Dan Williams3fe07912017-10-14 17:13:45 -07001399 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001400
Dave Jiangf4200392017-02-22 15:40:06 -08001401 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001402
1403 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001404 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001405
Dan Williams3fe07912017-10-14 17:13:45 -07001406 pfn = page_to_pfn_t(zero_page);
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001407 *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001408 DAX_PMD | DAX_ZERO_PAGE, false);
Ross Zwisler642261a2016-11-08 11:34:45 +11001409
Dave Jiangf4200392017-02-22 15:40:06 -08001410 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1411 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001412 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001413 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001414 }
1415
Dave Jiangf4200392017-02-22 15:40:06 -08001416 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001417 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001418 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001419 spin_unlock(ptl);
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001420 trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001421 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001422
1423fallback:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001424 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001425 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001426}
1427
Souptick Joarderab77dab2018-06-07 17:04:29 -07001428static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Dave Jianga2d58162017-02-24 14:56:59 -08001429 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001430{
Dave Jiangf4200392017-02-22 15:40:06 -08001431 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001432 struct address_space *mapping = vma->vm_file->f_mapping;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001433 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
Dave Jiangd8a849e2017-02-22 15:40:03 -08001434 unsigned long pmd_addr = vmf->address & PMD_MASK;
1435 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001436 bool sync;
Jan Kara9484ab12016-11-10 10:26:50 +11001437 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001438 struct inode *inode = mapping->host;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001439 vm_fault_t result = VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001440 struct iomap iomap = { 0 };
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001441 pgoff_t max_pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001442 void *entry;
1443 loff_t pos;
1444 int error;
Jan Kara302a5e32017-11-01 16:36:37 +01001445 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001446
Ross Zwisler282a8e02017-02-22 15:39:50 -08001447 /*
1448 * Check whether offset isn't beyond end of file now. Caller is
1449 * supposed to hold locks serializing us with truncate / punch hole so
1450 * this is a reliable test.
1451 */
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001452 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001453
Dave Jiangf4200392017-02-22 15:40:06 -08001454 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001455
Ross Zwislerfffa2812017-08-25 15:55:36 -07001456 /*
1457 * Make sure that the faulting address's PMD offset (color) matches
1458 * the PMD offset from the start of the file. This is necessary so
1459 * that a PMD range in the page table overlaps exactly with a PMD
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001460 * range in the page cache.
Ross Zwislerfffa2812017-08-25 15:55:36 -07001461 */
1462 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1463 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1464 goto fallback;
1465
Ross Zwisler642261a2016-11-08 11:34:45 +11001466 /* Fall back to PTEs if we're going to COW */
1467 if (write && !(vma->vm_flags & VM_SHARED))
1468 goto fallback;
1469
1470 /* If the PMD would extend outside the VMA */
1471 if (pmd_addr < vma->vm_start)
1472 goto fallback;
1473 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1474 goto fallback;
1475
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001476 if (xas.xa_index >= max_pgoff) {
Ross Zwisler282a8e02017-02-22 15:39:50 -08001477 result = VM_FAULT_SIGBUS;
1478 goto out;
1479 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001480
1481 /* If the PMD would extend beyond the file size */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001482 if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff)
Ross Zwisler642261a2016-11-08 11:34:45 +11001483 goto fallback;
1484
1485 /*
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001486 * grab_mapping_entry() will make sure we get an empty PMD entry,
1487 * a zero PMD entry or a DAX PMD. If it can't (because a PTE
1488 * entry is already in the array, for instance), it will return
1489 * VM_FAULT_FALLBACK.
Jan Kara9f141d62016-10-19 14:34:31 +02001490 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001491 entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
1492 if (xa_is_internal(entry)) {
1493 result = xa_to_internal(entry);
Ross Zwisler876f2942017-05-12 15:47:00 -07001494 goto fallback;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001495 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001496
1497 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001498 * It is possible, particularly with mixed reads & writes to private
1499 * mappings, that we have raced with a PTE fault that overlaps with
1500 * the PMD we need to set up. If so just return and the fault will be
1501 * retried.
1502 */
1503 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1504 !pmd_devmap(*vmf->pmd)) {
1505 result = 0;
1506 goto unlock_entry;
1507 }
1508
1509 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001510 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1511 * setting up a mapping, so really we're using iomap_begin() as a way
1512 * to look up our filesystem block.
1513 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001514 pos = (loff_t)xas.xa_index << PAGE_SHIFT;
Ross Zwisler876f2942017-05-12 15:47:00 -07001515 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1516 if (error)
1517 goto unlock_entry;
1518
1519 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001520 goto finish_iomap;
1521
Dan Williamsaaa422c2017-11-13 16:38:44 -08001522 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001523
Ross Zwisler642261a2016-11-08 11:34:45 +11001524 switch (iomap.type) {
1525 case IOMAP_MAPPED:
Jan Kara302a5e32017-11-01 16:36:37 +01001526 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1527 if (error < 0)
1528 goto finish_iomap;
1529
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001530 entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001531 DAX_PMD, write && !sync);
Jan Kara302a5e32017-11-01 16:36:37 +01001532
Jan Karacaa51d22017-11-01 16:36:42 +01001533 /*
1534 * If we are doing synchronous page fault and inode needs fsync,
1535 * we can insert PMD into page tables only after that happens.
1536 * Skip insertion for now and return the pfn so that caller can
1537 * insert it after fsync is done.
1538 */
1539 if (sync) {
1540 if (WARN_ON_ONCE(!pfnp))
1541 goto finish_iomap;
1542 *pfnp = pfn;
1543 result = VM_FAULT_NEEDDSYNC;
1544 goto finish_iomap;
1545 }
1546
Jan Kara302a5e32017-11-01 16:36:37 +01001547 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1548 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1549 write);
Ross Zwisler642261a2016-11-08 11:34:45 +11001550 break;
1551 case IOMAP_UNWRITTEN:
1552 case IOMAP_HOLE:
1553 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001554 break;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001555 result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001556 break;
1557 default:
1558 WARN_ON_ONCE(1);
1559 break;
1560 }
1561
Jan Kara9f141d62016-10-19 14:34:31 +02001562 finish_iomap:
1563 if (ops->iomap_end) {
1564 int copied = PMD_SIZE;
1565
1566 if (result == VM_FAULT_FALLBACK)
1567 copied = 0;
1568 /*
1569 * The fault is done by now and there's no way back (other
1570 * thread may be already happily using PMD we have installed).
1571 * Just ignore error from ->iomap_end since we cannot do much
1572 * with it.
1573 */
1574 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1575 &iomap);
1576 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001577 unlock_entry:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001578 dax_unlock_entry(&xas, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001579 fallback:
1580 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001581 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001582 count_vm_event(THP_FAULT_FALLBACK);
1583 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001584out:
Dave Jiangf4200392017-02-22 15:40:06 -08001585 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001586 return result;
1587}
Dave Jianga2d58162017-02-24 14:56:59 -08001588#else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001589static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001590 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001591{
1592 return VM_FAULT_FALLBACK;
1593}
Ross Zwisler642261a2016-11-08 11:34:45 +11001594#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001595
1596/**
1597 * dax_iomap_fault - handle a page fault on a DAX file
1598 * @vmf: The description of the fault
Jan Karacec04e82017-11-01 16:36:38 +01001599 * @pe_size: Size of the page to fault in
Jan Kara9a0dd422017-11-01 16:36:39 +01001600 * @pfnp: PFN to insert for synchronous faults if fsync is required
Jan Karac0b24622018-01-07 16:38:43 -05001601 * @iomap_errp: Storage for detailed error code in case of error
Jan Karacec04e82017-11-01 16:36:38 +01001602 * @ops: Iomap ops passed from the file system
Dave Jianga2d58162017-02-24 14:56:59 -08001603 *
1604 * When a page fault occurs, filesystems may call this helper in
1605 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1606 * has done all the necessary locking for page fault to proceed
1607 * successfully.
1608 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001609vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
Jan Karac0b24622018-01-07 16:38:43 -05001610 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001611{
Dave Jiangc791ace2017-02-24 14:57:08 -08001612 switch (pe_size) {
1613 case PE_SIZE_PTE:
Jan Karac0b24622018-01-07 16:38:43 -05001614 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001615 case PE_SIZE_PMD:
Jan Kara9a0dd422017-11-01 16:36:39 +01001616 return dax_iomap_pmd_fault(vmf, pfnp, ops);
Dave Jianga2d58162017-02-24 14:56:59 -08001617 default:
1618 return VM_FAULT_FALLBACK;
1619 }
1620}
1621EXPORT_SYMBOL_GPL(dax_iomap_fault);
Jan Kara71eab6d2017-11-01 16:36:43 +01001622
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001623/*
Jan Kara71eab6d2017-11-01 16:36:43 +01001624 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1625 * @vmf: The description of the fault
Jan Kara71eab6d2017-11-01 16:36:43 +01001626 * @pfn: PFN to insert
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001627 * @order: Order of entry to insert.
Jan Kara71eab6d2017-11-01 16:36:43 +01001628 *
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001629 * This function inserts a writeable PTE or PMD entry into the page tables
1630 * for an mmaped DAX file. It also marks the page cache entry as dirty.
Jan Kara71eab6d2017-11-01 16:36:43 +01001631 */
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001632static vm_fault_t
1633dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
Jan Kara71eab6d2017-11-01 16:36:43 +01001634{
1635 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001636 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1637 void *entry;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001638 vm_fault_t ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001639
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001640 xas_lock_irq(&xas);
1641 entry = get_unlocked_entry(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001642 /* Did we race with someone splitting entry or so? */
1643 if (!entry ||
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001644 (order == 0 && !dax_is_pte_entry(entry)) ||
1645 (order == PMD_ORDER && (xa_is_internal(entry) ||
1646 !dax_is_pmd_entry(entry)))) {
1647 put_unlocked_entry(&xas, entry);
1648 xas_unlock_irq(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001649 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1650 VM_FAULT_NOPAGE);
1651 return VM_FAULT_NOPAGE;
1652 }
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001653 xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1654 dax_lock_entry(&xas, entry);
1655 xas_unlock_irq(&xas);
1656 if (order == 0)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001657 ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
Jan Kara71eab6d2017-11-01 16:36:43 +01001658#ifdef CONFIG_FS_DAX_PMD
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001659 else if (order == PMD_ORDER)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001660 ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Jan Kara71eab6d2017-11-01 16:36:43 +01001661 pfn, true);
Jan Kara71eab6d2017-11-01 16:36:43 +01001662#endif
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001663 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001664 ret = VM_FAULT_FALLBACK;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001665 dax_unlock_entry(&xas, entry);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001666 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1667 return ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001668}
1669
1670/**
1671 * dax_finish_sync_fault - finish synchronous page fault
1672 * @vmf: The description of the fault
1673 * @pe_size: Size of entry to be inserted
1674 * @pfn: PFN to insert
1675 *
1676 * This function ensures that the file range touched by the page fault is
1677 * stored persistently on the media and handles inserting of appropriate page
1678 * table entry.
1679 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001680vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1681 enum page_entry_size pe_size, pfn_t pfn)
Jan Kara71eab6d2017-11-01 16:36:43 +01001682{
1683 int err;
1684 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001685 unsigned int order = pe_order(pe_size);
1686 size_t len = PAGE_SIZE << order;
Jan Kara71eab6d2017-11-01 16:36:43 +01001687
Jan Kara71eab6d2017-11-01 16:36:43 +01001688 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1689 if (err)
1690 return VM_FAULT_SIGBUS;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001691 return dax_insert_pfn_mkwrite(vmf, pfn, order);
Jan Kara71eab6d2017-11-01 16:36:43 +01001692}
1693EXPORT_SYMBOL_GPL(dax_finish_sync_fault);