blob: cf2394e2bf4bf36b845e4c52f697ba16881d7e08 [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
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400101static bool dax_is_locked(void *entry)
102{
103 return xa_to_value(entry) & DAX_LOCKED;
104}
105
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400106static unsigned int dax_entry_order(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -0700107{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400108 if (xa_to_value(entry) & DAX_PMD)
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400109 return PMD_ORDER;
Ross Zwisler527b19d2017-09-06 16:18:51 -0700110 return 0;
111}
112
Matthew Wilcoxfda490d2018-11-16 15:07:31 -0500113static unsigned long dax_is_pmd_entry(void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +1100114{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400115 return xa_to_value(entry) & DAX_PMD;
Ross Zwisler642261a2016-11-08 11:34:45 +1100116}
117
Matthew Wilcoxfda490d2018-11-16 15:07:31 -0500118static bool dax_is_pte_entry(void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +1100119{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400120 return !(xa_to_value(entry) & DAX_PMD);
Ross Zwisler642261a2016-11-08 11:34:45 +1100121}
122
123static int dax_is_zero_entry(void *entry)
124{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400125 return xa_to_value(entry) & DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100126}
127
128static int dax_is_empty_entry(void *entry)
129{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400130 return xa_to_value(entry) & DAX_EMPTY;
Ross Zwisler642261a2016-11-08 11:34:45 +1100131}
132
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800133/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400134 * DAX page cache entry locking
Jan Karaac401cc2016-05-12 18:29:18 +0200135 */
136struct exceptional_entry_key {
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400137 struct xarray *xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100138 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200139};
140
141struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200142 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200143 struct exceptional_entry_key key;
144};
145
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400146static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas,
147 void *entry, struct exceptional_entry_key *key)
Ross Zwisler63e95b52016-11-08 11:32:20 +1100148{
149 unsigned long hash;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400150 unsigned long index = xas->xa_index;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100151
152 /*
153 * If 'entry' is a PMD, align the 'index' that we use for the wait
154 * queue to the start of that PMD. This ensures that all offsets in
155 * the range covered by the PMD map to the same bit lock.
156 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100157 if (dax_is_pmd_entry(entry))
Ross Zwisler917f3452017-09-06 16:18:58 -0700158 index &= ~PG_PMD_COLOUR;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400159 key->xa = xas->xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100160 key->entry_start = index;
161
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400162 hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100163 return wait_table + hash;
164}
165
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400166static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
167 unsigned int mode, int sync, void *keyp)
Jan Karaac401cc2016-05-12 18:29:18 +0200168{
169 struct exceptional_entry_key *key = keyp;
170 struct wait_exceptional_entry_queue *ewait =
171 container_of(wait, struct wait_exceptional_entry_queue, wait);
172
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400173 if (key->xa != ewait->key.xa ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100174 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200175 return 0;
176 return autoremove_wake_function(wait, mode, sync, NULL);
177}
178
179/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700180 * @entry may no longer be the entry at the index in the mapping.
181 * The important information it's conveying is whether the entry at
182 * this index used to be a PMD entry.
Ross Zwislere30331f2017-09-06 16:18:39 -0700183 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400184static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
Ross Zwislere30331f2017-09-06 16:18:39 -0700185{
186 struct exceptional_entry_key key;
187 wait_queue_head_t *wq;
188
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400189 wq = dax_entry_waitqueue(xas, entry, &key);
Ross Zwislere30331f2017-09-06 16:18:39 -0700190
191 /*
192 * Checking for locked entry and prepare_to_wait_exclusive() happens
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700193 * under the i_pages lock, ditto for entry handling in our callers.
Ross Zwislere30331f2017-09-06 16:18:39 -0700194 * So at this point all tasks that could have seen our entry locked
195 * must be in the waitqueue and the following check will see them.
196 */
197 if (waitqueue_active(wq))
198 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
199}
200
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400201/*
202 * Look up entry in page cache, wait for it to become unlocked if it
203 * is a DAX entry and return it. The caller must subsequently call
204 * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
205 * if it did.
206 *
207 * Must be called with the i_pages lock held.
208 */
209static void *get_unlocked_entry(struct xa_state *xas)
210{
211 void *entry;
212 struct wait_exceptional_entry_queue ewait;
213 wait_queue_head_t *wq;
214
215 init_wait(&ewait.wait);
216 ewait.wait.func = wake_exceptional_entry_func;
217
218 for (;;) {
Matthew Wilcox0e40de02018-11-16 15:19:13 -0500219 entry = xas_find_conflict(xas);
220 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) ||
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400221 !dax_is_locked(entry))
222 return entry;
223
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400224 wq = dax_entry_waitqueue(xas, entry, &ewait.key);
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400225 prepare_to_wait_exclusive(wq, &ewait.wait,
226 TASK_UNINTERRUPTIBLE);
227 xas_unlock_irq(xas);
228 xas_reset(xas);
229 schedule();
230 finish_wait(wq, &ewait.wait);
231 xas_lock_irq(xas);
232 }
233}
234
235static void put_unlocked_entry(struct xa_state *xas, void *entry)
236{
237 /* If we were the only waiter woken, wake the next one */
238 if (entry)
239 dax_wake_entry(xas, entry, false);
240}
241
242/*
243 * We used the xa_state to get the entry, but then we locked the entry and
244 * dropped the xa_lock, so we know the xa_state is stale and must be reset
245 * before use.
246 */
247static void dax_unlock_entry(struct xa_state *xas, void *entry)
248{
249 void *old;
250
Matthew Wilcox7ae2ea72018-11-09 20:09:37 -0500251 BUG_ON(dax_is_locked(entry));
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400252 xas_reset(xas);
253 xas_lock_irq(xas);
254 old = xas_store(xas, entry);
255 xas_unlock_irq(xas);
256 BUG_ON(!dax_is_locked(old));
257 dax_wake_entry(xas, entry, false);
258}
259
260/*
261 * Return: The entry stored at this location before it was locked.
262 */
263static void *dax_lock_entry(struct xa_state *xas, void *entry)
264{
265 unsigned long v = xa_to_value(entry);
266 return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
267}
268
Dan Williamsd2c997c2017-12-22 22:02:48 -0800269static unsigned long dax_entry_size(void *entry)
270{
271 if (dax_is_zero_entry(entry))
272 return 0;
273 else if (dax_is_empty_entry(entry))
274 return 0;
275 else if (dax_is_pmd_entry(entry))
276 return PMD_SIZE;
277 else
278 return PAGE_SIZE;
279}
280
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400281static unsigned long dax_end_pfn(void *entry)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800282{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400283 return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800284}
285
286/*
287 * Iterate through all mapped pfns represented by an entry, i.e. skip
288 * 'empty' and 'zero' entries.
289 */
290#define for_each_mapped_pfn(entry, pfn) \
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400291 for (pfn = dax_to_pfn(entry); \
292 pfn < dax_end_pfn(entry); pfn++)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800293
Dan Williams73449da2018-07-13 21:49:50 -0700294/*
295 * TODO: for reflink+dax we need a way to associate a single page with
296 * multiple address_space instances at different linear_page_index()
297 * offsets.
298 */
299static void dax_associate_entry(void *entry, struct address_space *mapping,
300 struct vm_area_struct *vma, unsigned long address)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800301{
Dan Williams73449da2018-07-13 21:49:50 -0700302 unsigned long size = dax_entry_size(entry), pfn, index;
303 int i = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800304
305 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
306 return;
307
Dan Williams73449da2018-07-13 21:49:50 -0700308 index = linear_page_index(vma, address & ~(size - 1));
Dan Williamsd2c997c2017-12-22 22:02:48 -0800309 for_each_mapped_pfn(entry, pfn) {
310 struct page *page = pfn_to_page(pfn);
311
312 WARN_ON_ONCE(page->mapping);
313 page->mapping = mapping;
Dan Williams73449da2018-07-13 21:49:50 -0700314 page->index = index + i++;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800315 }
316}
317
318static void dax_disassociate_entry(void *entry, struct address_space *mapping,
319 bool trunc)
320{
321 unsigned long pfn;
322
323 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
324 return;
325
326 for_each_mapped_pfn(entry, pfn) {
327 struct page *page = pfn_to_page(pfn);
328
329 WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
330 WARN_ON_ONCE(page->mapping && page->mapping != mapping);
331 page->mapping = NULL;
Dan Williams73449da2018-07-13 21:49:50 -0700332 page->index = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800333 }
334}
335
Dan Williams5fac7402018-03-09 17:44:31 -0800336static struct page *dax_busy_page(void *entry)
337{
338 unsigned long pfn;
339
340 for_each_mapped_pfn(entry, pfn) {
341 struct page *page = pfn_to_page(pfn);
342
343 if (page_ref_count(page) > 1)
344 return page;
345 }
346 return NULL;
347}
348
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500349/*
350 * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page
351 * @page: The page whose entry we want to lock
352 *
353 * Context: Process context.
354 * Return: %true if the entry was locked or does not need to be locked.
355 */
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700356bool dax_lock_mapping_entry(struct page *page)
357{
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400358 XA_STATE(xas, NULL, 0);
359 void *entry;
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500360 bool locked;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700361
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500362 /* Ensure page->mapping isn't freed while we look at it */
363 rcu_read_lock();
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700364 for (;;) {
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400365 struct address_space *mapping = READ_ONCE(page->mapping);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700366
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500367 locked = false;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700368 if (!dax_mapping(mapping))
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500369 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700370
371 /*
372 * In the device-dax case there's no need to lock, a
373 * struct dev_pagemap pin is sufficient to keep the
374 * inode alive, and we assume we have dev_pagemap pin
375 * otherwise we would not have a valid pfn_to_page()
376 * translation.
377 */
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500378 locked = true;
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400379 if (S_ISCHR(mapping->host->i_mode))
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500380 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700381
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400382 xas.xa = &mapping->i_pages;
383 xas_lock_irq(&xas);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700384 if (mapping != page->mapping) {
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400385 xas_unlock_irq(&xas);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700386 continue;
387 }
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400388 xas_set(&xas, page->index);
389 entry = xas_load(&xas);
390 if (dax_is_locked(entry)) {
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500391 rcu_read_unlock();
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400392 entry = get_unlocked_entry(&xas);
Matthew Wilcox6d7cd8c2018-11-06 13:11:57 -0500393 xas_unlock_irq(&xas);
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500394 rcu_read_lock();
Matthew Wilcox6d7cd8c2018-11-06 13:11:57 -0500395 continue;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700396 }
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400397 dax_lock_entry(&xas, entry);
398 xas_unlock_irq(&xas);
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500399 break;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700400 }
Matthew Wilcoxc5bbd452018-11-16 14:37:06 -0500401 rcu_read_unlock();
402 return locked;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700403}
404
405void dax_unlock_mapping_entry(struct page *page)
406{
407 struct address_space *mapping = page->mapping;
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400408 XA_STATE(xas, &mapping->i_pages, page->index);
Matthew Wilcoxfda490d2018-11-16 15:07:31 -0500409 void *entry;
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700410
Matthew Wilcox9f32d222018-06-12 09:46:30 -0400411 if (S_ISCHR(mapping->host->i_mode))
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700412 return;
413
Matthew Wilcoxfda490d2018-11-16 15:07:31 -0500414 rcu_read_lock();
415 entry = xas_load(&xas);
416 rcu_read_unlock();
417 entry = dax_make_entry(page_to_pfn_t(page), dax_is_pmd_entry(entry));
418 dax_unlock_entry(&xas, entry);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700419}
420
Jan Karaac401cc2016-05-12 18:29:18 +0200421/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400422 * Find page cache entry at given index. If it is a DAX entry, return it
423 * with the entry locked. If the page cache doesn't contain an entry at
424 * that index, add a locked empty entry.
Jan Karaac401cc2016-05-12 18:29:18 +0200425 *
Matthew Wilcox3159f942017-11-03 13:30:42 -0400426 * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400427 * either return that locked entry or will return VM_FAULT_FALLBACK.
428 * This will happen if there are any PTE entries within the PMD range
429 * that we are requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100430 *
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400431 * We always favor PTE entries over PMD entries. There isn't a flow where we
432 * evict PTE entries in order to 'upgrade' them to a PMD entry. A PMD
433 * insertion will fail if it finds any PTE entries already in the tree, and a
434 * PTE insertion will cause an existing PMD entry to be unmapped and
435 * downgraded to PTE entries. This happens for both PMD zero pages as
436 * well as PMD empty entries.
Ross Zwisler642261a2016-11-08 11:34:45 +1100437 *
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400438 * The exception to this downgrade path is for PMD entries that have
439 * real storage backing them. We will leave these real PMD entries in
440 * the tree, and PTE writes will simply dirty the entire PMD entry.
Ross Zwisler642261a2016-11-08 11:34:45 +1100441 *
Jan Karaac401cc2016-05-12 18:29:18 +0200442 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
443 * persistent memory the benefit is doubtful. We can add that later if we can
444 * show it helps.
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400445 *
446 * On error, this function does not return an ERR_PTR. Instead it returns
447 * a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values
448 * overlap with xarray value entries.
Jan Karaac401cc2016-05-12 18:29:18 +0200449 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400450static void *grab_mapping_entry(struct xa_state *xas,
451 struct address_space *mapping, unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200452{
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400453 unsigned long index = xas->xa_index;
454 bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
455 void *entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200456
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400457retry:
458 xas_lock_irq(xas);
459 entry = get_unlocked_entry(xas);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700460
Ross Zwisler642261a2016-11-08 11:34:45 +1100461 if (entry) {
Matthew Wilcox0e40de02018-11-16 15:19:13 -0500462 if (!xa_is_value(entry)) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400463 xas_set_err(xas, EIO);
464 goto out_unlock;
465 }
466
Matthew Wilcox3159f942017-11-03 13:30:42 -0400467 if (size_flag & DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700468 if (dax_is_pte_entry(entry)) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400469 put_unlocked_entry(xas, entry);
470 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +1100471 }
472 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700473 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100474 (dax_is_zero_entry(entry) ||
475 dax_is_empty_entry(entry))) {
476 pmd_downgrade = true;
477 }
478 }
479 }
480
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400481 if (pmd_downgrade) {
482 /*
483 * Make sure 'entry' remains valid while we drop
484 * the i_pages lock.
485 */
486 dax_lock_entry(xas, entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200487
Ross Zwisler642261a2016-11-08 11:34:45 +1100488 /*
489 * Besides huge zero pages the only other thing that gets
490 * downgraded are empty entries which don't need to be
491 * unmapped.
492 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400493 if (dax_is_zero_entry(entry)) {
494 xas_unlock_irq(xas);
495 unmap_mapping_pages(mapping,
496 xas->xa_index & ~PG_PMD_COLOUR,
497 PG_PMD_NR, false);
498 xas_reset(xas);
499 xas_lock_irq(xas);
Ross Zwislere11f8b72017-04-07 16:04:57 -0700500 }
501
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400502 dax_disassociate_entry(entry, mapping, false);
503 xas_store(xas, NULL); /* undo the PMD join */
504 dax_wake_entry(xas, entry, true);
505 mapping->nrexceptional--;
506 entry = NULL;
507 xas_set(xas, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200508 }
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400509
510 if (entry) {
511 dax_lock_entry(xas, entry);
512 } else {
513 entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
514 dax_lock_entry(xas, entry);
515 if (xas_error(xas))
516 goto out_unlock;
517 mapping->nrexceptional++;
518 }
519
520out_unlock:
521 xas_unlock_irq(xas);
522 if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
523 goto retry;
524 if (xas->xa_node == XA_ERROR(-ENOMEM))
525 return xa_mk_internal(VM_FAULT_OOM);
526 if (xas_error(xas))
527 return xa_mk_internal(VM_FAULT_SIGBUS);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100528 return entry;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400529fallback:
530 xas_unlock_irq(xas);
531 return xa_mk_internal(VM_FAULT_FALLBACK);
Jan Karaac401cc2016-05-12 18:29:18 +0200532}
533
Dan Williams5fac7402018-03-09 17:44:31 -0800534/**
535 * dax_layout_busy_page - find first pinned page in @mapping
536 * @mapping: address space to scan for a page with ref count > 1
537 *
538 * DAX requires ZONE_DEVICE mapped pages. These pages are never
539 * 'onlined' to the page allocator so they are considered idle when
540 * page->count == 1. A filesystem uses this interface to determine if
541 * any page in the mapping is busy, i.e. for DMA, or other
542 * get_user_pages() usages.
543 *
544 * It is expected that the filesystem is holding locks to block the
545 * establishment of new mappings in this address_space. I.e. it expects
546 * to be able to run unmap_mapping_range() and subsequently not race
547 * mapping_mapped() becoming true.
548 */
549struct page *dax_layout_busy_page(struct address_space *mapping)
550{
Matthew Wilcox084a8992018-05-17 13:03:48 -0400551 XA_STATE(xas, &mapping->i_pages, 0);
552 void *entry;
553 unsigned int scanned = 0;
Dan Williams5fac7402018-03-09 17:44:31 -0800554 struct page *page = NULL;
Dan Williams5fac7402018-03-09 17:44:31 -0800555
556 /*
557 * In the 'limited' case get_user_pages() for dax is disabled.
558 */
559 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
560 return NULL;
561
562 if (!dax_mapping(mapping) || !mapping_mapped(mapping))
563 return NULL;
564
Dan Williams5fac7402018-03-09 17:44:31 -0800565 /*
566 * If we race get_user_pages_fast() here either we'll see the
Matthew Wilcox084a8992018-05-17 13:03:48 -0400567 * elevated page count in the iteration and wait, or
Dan Williams5fac7402018-03-09 17:44:31 -0800568 * get_user_pages_fast() will see that the page it took a reference
569 * against is no longer mapped in the page tables and bail to the
570 * get_user_pages() slow path. The slow path is protected by
571 * pte_lock() and pmd_lock(). New references are not taken without
572 * holding those locks, and unmap_mapping_range() will not zero the
573 * pte or pmd without holding the respective lock, so we are
574 * guaranteed to either see new references or prevent new
575 * references from being established.
576 */
577 unmap_mapping_range(mapping, 0, 0, 1);
578
Matthew Wilcox084a8992018-05-17 13:03:48 -0400579 xas_lock_irq(&xas);
580 xas_for_each(&xas, entry, ULONG_MAX) {
581 if (WARN_ON_ONCE(!xa_is_value(entry)))
582 continue;
583 if (unlikely(dax_is_locked(entry)))
584 entry = get_unlocked_entry(&xas);
585 if (entry)
586 page = dax_busy_page(entry);
587 put_unlocked_entry(&xas, entry);
Dan Williams5fac7402018-03-09 17:44:31 -0800588 if (page)
589 break;
Matthew Wilcox084a8992018-05-17 13:03:48 -0400590 if (++scanned % XA_CHECK_SCHED)
591 continue;
592
593 xas_pause(&xas);
594 xas_unlock_irq(&xas);
595 cond_resched();
596 xas_lock_irq(&xas);
Dan Williams5fac7402018-03-09 17:44:31 -0800597 }
Matthew Wilcox084a8992018-05-17 13:03:48 -0400598 xas_unlock_irq(&xas);
Dan Williams5fac7402018-03-09 17:44:31 -0800599 return page;
600}
601EXPORT_SYMBOL_GPL(dax_layout_busy_page);
602
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400603static int __dax_invalidate_entry(struct address_space *mapping,
Jan Karac6dcf522016-08-10 17:22:44 +0200604 pgoff_t index, bool trunc)
605{
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400606 XA_STATE(xas, &mapping->i_pages, index);
Jan Karac6dcf522016-08-10 17:22:44 +0200607 int ret = 0;
608 void *entry;
Jan Karac6dcf522016-08-10 17:22:44 +0200609
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400610 xas_lock_irq(&xas);
611 entry = get_unlocked_entry(&xas);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400612 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200613 goto out;
614 if (!trunc &&
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400615 (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
616 xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
Jan Karac6dcf522016-08-10 17:22:44 +0200617 goto out;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800618 dax_disassociate_entry(entry, mapping, trunc);
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400619 xas_store(&xas, NULL);
Jan Karac6dcf522016-08-10 17:22:44 +0200620 mapping->nrexceptional--;
621 ret = 1;
622out:
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400623 put_unlocked_entry(&xas, entry);
624 xas_unlock_irq(&xas);
Jan Karac6dcf522016-08-10 17:22:44 +0200625 return ret;
626}
Matthew Wilcox07f2d892018-03-28 15:40:41 -0400627
Jan Karaac401cc2016-05-12 18:29:18 +0200628/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400629 * Delete DAX entry at @index from @mapping. Wait for it
630 * to be unlocked before deleting it.
Jan Karaac401cc2016-05-12 18:29:18 +0200631 */
632int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
633{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400634 int ret = __dax_invalidate_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200635
Jan Karaac401cc2016-05-12 18:29:18 +0200636 /*
637 * This gets called from truncate / punch_hole path. As such, the caller
638 * must hold locks protecting against concurrent modifications of the
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400639 * page cache (usually fs-private i_mmap_sem for writing). Since the
Matthew Wilcox3159f942017-11-03 13:30:42 -0400640 * caller has seen a DAX entry for this index, we better find it
Jan Karaac401cc2016-05-12 18:29:18 +0200641 * at that index as well...
642 */
Jan Karac6dcf522016-08-10 17:22:44 +0200643 WARN_ON_ONCE(!ret);
644 return ret;
645}
Jan Karaac401cc2016-05-12 18:29:18 +0200646
Jan Karac6dcf522016-08-10 17:22:44 +0200647/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400648 * Invalidate DAX entry if it is clean.
Jan Karac6dcf522016-08-10 17:22:44 +0200649 */
650int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
651 pgoff_t index)
652{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400653 return __dax_invalidate_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200654}
655
Dan Williamscccbce62017-01-27 13:31:42 -0800656static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
657 sector_t sector, size_t size, struct page *to,
658 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800659{
Dan Williamscccbce62017-01-27 13:31:42 -0800660 void *vto, *kaddr;
661 pgoff_t pgoff;
Dan Williamscccbce62017-01-27 13:31:42 -0800662 long rc;
663 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600664
Dan Williamscccbce62017-01-27 13:31:42 -0800665 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
666 if (rc)
667 return rc;
668
669 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800670 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -0800671 if (rc < 0) {
672 dax_read_unlock(id);
673 return rc;
674 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800675 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800676 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800677 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800678 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800679 return 0;
680}
681
Ross Zwisler642261a2016-11-08 11:34:45 +1100682/*
683 * By this point grab_mapping_entry() has ensured that we have a locked entry
684 * of the appropriate size so we don't have to worry about downgrading PMDs to
685 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
686 * already in the tree, we will skip the insertion and just dirty the PMD as
687 * appropriate.
688 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400689static void *dax_insert_entry(struct xa_state *xas,
690 struct address_space *mapping, struct vm_fault *vmf,
691 void *entry, pfn_t pfn, unsigned long flags, bool dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800692{
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400693 void *new_entry = dax_make_entry(pfn, flags);
Ross Zwisler9973c982016-01-22 15:10:47 -0800694
Jan Karaf5b7b742017-11-01 16:36:40 +0100695 if (dirty)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800696 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800697
Matthew Wilcox3159f942017-11-03 13:30:42 -0400698 if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400699 unsigned long index = xas->xa_index;
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700700 /* we are replacing a zero page with block mapping */
701 if (dax_is_pmd_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800702 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400703 PG_PMD_NR, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700704 else /* pte entry */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400705 unmap_mapping_pages(mapping, index, 1, false);
Ross Zwisler9973c982016-01-22 15:10:47 -0800706 }
707
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400708 xas_reset(xas);
709 xas_lock_irq(xas);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800710 if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
711 dax_disassociate_entry(entry, mapping, false);
Dan Williams73449da2018-07-13 21:49:50 -0700712 dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800713 }
Ross Zwisler642261a2016-11-08 11:34:45 +1100714
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700715 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100716 /*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400717 * Only swap our new entry into the page cache if the current
Ross Zwisler642261a2016-11-08 11:34:45 +1100718 * entry is a zero page or an empty entry. If a normal PTE or
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400719 * PMD entry is already in the cache, we leave it alone. This
Ross Zwisler642261a2016-11-08 11:34:45 +1100720 * means that if we are trying to insert a PTE and the
721 * existing entry is a PMD, we will just leave the PMD in the
722 * tree and dirty it if necessary.
723 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400724 void *old = dax_lock_entry(xas, new_entry);
725 WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
726 DAX_LOCKED));
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700727 entry = new_entry;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400728 } else {
729 xas_load(xas); /* Walk the xa_state */
Ross Zwisler9973c982016-01-22 15:10:47 -0800730 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700731
Jan Karaf5b7b742017-11-01 16:36:40 +0100732 if (dirty)
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400733 xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700734
Matthew Wilcoxb15cd802018-03-29 22:58:27 -0400735 xas_unlock_irq(xas);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700736 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800737}
738
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400739static inline
740unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
Jan Kara4b4bb462016-12-14 15:07:53 -0800741{
742 unsigned long address;
743
744 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
745 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
746 return address;
747}
748
749/* Walk all mappings of a given index of a file and writeprotect them */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400750static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
751 unsigned long pfn)
Jan Kara4b4bb462016-12-14 15:07:53 -0800752{
753 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800754 pte_t pte, *ptep = NULL;
755 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800756 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800757
758 i_mmap_lock_read(mapping);
759 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400760 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800761
762 cond_resched();
763
764 if (!(vma->vm_flags & VM_SHARED))
765 continue;
766
767 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400768
769 /*
770 * Note because we provide start/end to follow_pte_pmd it will
771 * call mmu_notifier_invalidate_range_start() on our behalf
772 * before taking any lock.
773 */
774 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800775 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800776
Jérôme Glisse0f108512017-11-15 17:34:07 -0800777 /*
778 * No need to call mmu_notifier_invalidate_range() as we are
779 * downgrading page table protection not changing it to point
780 * to a new page.
781 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200782 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800783 */
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800784 if (pmdp) {
785#ifdef CONFIG_FS_DAX_PMD
786 pmd_t pmd;
787
788 if (pfn != pmd_pfn(*pmdp))
789 goto unlock_pmd;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800790 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800791 goto unlock_pmd;
792
793 flush_cache_page(vma, address, pfn);
794 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
795 pmd = pmd_wrprotect(pmd);
796 pmd = pmd_mkclean(pmd);
797 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800798unlock_pmd:
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800799#endif
Jan H. Schönherree190ca2018-01-31 16:14:04 -0800800 spin_unlock(ptl);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800801 } else {
802 if (pfn != pte_pfn(*ptep))
803 goto unlock_pte;
804 if (!pte_dirty(*ptep) && !pte_write(*ptep))
805 goto unlock_pte;
806
807 flush_cache_page(vma, address, pfn);
808 pte = ptep_clear_flush(vma, address, ptep);
809 pte = pte_wrprotect(pte);
810 pte = pte_mkclean(pte);
811 set_pte_at(vma->vm_mm, address, ptep, pte);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800812unlock_pte:
813 pte_unmap_unlock(ptep, ptl);
814 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800815
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400816 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800817 }
818 i_mmap_unlock_read(mapping);
819}
820
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400821static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
822 struct address_space *mapping, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800823{
Dan Williams3fe07912017-10-14 17:13:45 -0700824 unsigned long pfn;
825 long ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800826 size_t size;
Ross Zwisler9973c982016-01-22 15:10:47 -0800827
Ross Zwisler9973c982016-01-22 15:10:47 -0800828 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800829 * A page got tagged dirty in DAX mapping? Something is seriously
830 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800831 */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400832 if (WARN_ON(!xa_is_value(entry)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800833 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800834
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400835 if (unlikely(dax_is_locked(entry))) {
836 void *old_entry = entry;
837
838 entry = get_unlocked_entry(xas);
839
840 /* Entry got punched out / reallocated? */
841 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
842 goto put_unlocked;
843 /*
844 * Entry got reallocated elsewhere? No need to writeback.
845 * We have to compare pfns as we must not bail out due to
846 * difference in lockbit or entry type.
847 */
848 if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
849 goto put_unlocked;
850 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
851 dax_is_zero_entry(entry))) {
852 ret = -EIO;
853 goto put_unlocked;
854 }
855
856 /* Another fsync thread may have already done this entry */
857 if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
858 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800859 }
860
Jan Karaa6abc2c2016-12-14 15:07:47 -0800861 /* Lock the entry to serialize with page faults */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400862 dax_lock_entry(xas, entry);
863
Jan Karaa6abc2c2016-12-14 15:07:47 -0800864 /*
865 * We can clear the tag now but we have to be careful so that concurrent
866 * dax_writeback_one() calls for the same index cannot finish before we
867 * actually flush the caches. This is achieved as the calls will look
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700868 * at the entry only under the i_pages lock and once they do that
869 * they will see the entry locked and wait for it to unlock.
Jan Karaa6abc2c2016-12-14 15:07:47 -0800870 */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400871 xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
872 xas_unlock_irq(xas);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800873
Ross Zwisler642261a2016-11-08 11:34:45 +1100874 /*
875 * Even if dax_writeback_mapping_range() was given a wbc->range_start
876 * in the middle of a PMD, the 'index' we are given will be aligned to
Dan Williams3fe07912017-10-14 17:13:45 -0700877 * the start index of the PMD, as will the pfn we pull from 'entry'.
878 * This allows us to flush for PMD_SIZE and not have to worry about
879 * partial PMD writebacks.
Ross Zwisler642261a2016-11-08 11:34:45 +1100880 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400881 pfn = dax_to_pfn(entry);
882 size = PAGE_SIZE << dax_entry_order(entry);
Dan Williamscccbce62017-01-27 13:31:42 -0800883
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400884 dax_entry_mkclean(mapping, xas->xa_index, pfn);
Dan Williams3fe07912017-10-14 17:13:45 -0700885 dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800886 /*
887 * After we have flushed the cache, we can clear the dirty tag. There
888 * cannot be new dirty data in the pfn after the flush has completed as
889 * the pfn mappings are writeprotected and fault waits for mapping
890 * entry lock.
891 */
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400892 xas_reset(xas);
893 xas_lock_irq(xas);
894 xas_store(xas, entry);
895 xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
896 dax_wake_entry(xas, entry, false);
897
898 trace_dax_writeback_one(mapping->host, xas->xa_index,
899 size >> PAGE_SHIFT);
Ross Zwisler9973c982016-01-22 15:10:47 -0800900 return ret;
901
Jan Karaa6abc2c2016-12-14 15:07:47 -0800902 put_unlocked:
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400903 put_unlocked_entry(xas, entry);
Ross Zwisler9973c982016-01-22 15:10:47 -0800904 return ret;
905}
906
907/*
908 * Flush the mapping to the persistent domain within the byte range of [start,
909 * end]. This is required by data integrity operations to ensure file data is
910 * on persistent storage prior to completion of the operation.
911 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800912int dax_writeback_mapping_range(struct address_space *mapping,
913 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -0800914{
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400915 XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
Ross Zwisler9973c982016-01-22 15:10:47 -0800916 struct inode *inode = mapping->host;
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400917 pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
Dan Williamscccbce62017-01-27 13:31:42 -0800918 struct dax_device *dax_dev;
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400919 void *entry;
920 int ret = 0;
921 unsigned int scanned = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -0800922
923 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
924 return -EIO;
925
Ross Zwisler7f6d5b52016-02-26 15:19:55 -0800926 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
927 return 0;
928
Dan Williamscccbce62017-01-27 13:31:42 -0800929 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
930 if (!dax_dev)
931 return -EIO;
932
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400933 trace_dax_writeback_range(inode, xas.xa_index, end_index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800934
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400935 tag_pages_for_writeback(mapping, xas.xa_index, end_index);
Ross Zwislerd14a3f42017-05-08 16:00:10 -0700936
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400937 xas_lock_irq(&xas);
938 xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
939 ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
940 if (ret < 0) {
941 mapping_set_error(mapping, ret);
Ross Zwisler9973c982016-01-22 15:10:47 -0800942 break;
Ross Zwisler9973c982016-01-22 15:10:47 -0800943 }
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400944 if (++scanned % XA_CHECK_SCHED)
945 continue;
946
947 xas_pause(&xas);
948 xas_unlock_irq(&xas);
949 cond_resched();
950 xas_lock_irq(&xas);
Ross Zwisler9973c982016-01-22 15:10:47 -0800951 }
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400952 xas_unlock_irq(&xas);
Dan Williamscccbce62017-01-27 13:31:42 -0800953 put_dax(dax_dev);
Matthew Wilcox9fc747f62018-03-28 16:03:45 -0400954 trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
955 return ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800956}
957EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
958
Jan Kara31a6f1a2017-11-01 16:36:32 +0100959static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800960{
Linus Torvaldsa3841f92017-11-17 09:51:57 -0800961 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
Jan Kara31a6f1a2017-11-01 16:36:32 +0100962}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800963
Jan Kara5e161e42017-11-01 16:36:33 +0100964static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
965 pfn_t *pfnp)
966{
967 const sector_t sector = dax_iomap_sector(iomap, pos);
968 pgoff_t pgoff;
Jan Kara5e161e42017-11-01 16:36:33 +0100969 int id, rc;
970 long length;
971
972 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -0800973 if (rc)
974 return rc;
Dan Williamscccbce62017-01-27 13:31:42 -0800975 id = dax_read_lock();
Jan Kara5e161e42017-11-01 16:36:33 +0100976 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800977 NULL, pfnp);
Jan Kara5e161e42017-11-01 16:36:33 +0100978 if (length < 0) {
979 rc = length;
980 goto out;
Dan Williamscccbce62017-01-27 13:31:42 -0800981 }
Jan Kara5e161e42017-11-01 16:36:33 +0100982 rc = -EINVAL;
983 if (PFN_PHYS(length) < size)
984 goto out;
985 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
986 goto out;
987 /* For larger pages we need devmap */
988 if (length > 1 && !pfn_t_devmap(*pfnp))
989 goto out;
990 rc = 0;
991out:
Dan Williamscccbce62017-01-27 13:31:42 -0800992 dax_read_unlock(id);
Jan Kara5e161e42017-11-01 16:36:33 +0100993 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800994}
995
Ross Zwislere30331f2017-09-06 16:18:39 -0700996/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700997 * The user has performed a load from a hole in the file. Allocating a new
998 * page in the file would cause excessive storage usage for workloads with
999 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
1000 * If this page is ever written to we will re-fault and change the mapping to
1001 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -07001002 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001003static vm_fault_t dax_load_hole(struct xa_state *xas,
1004 struct address_space *mapping, void **entry,
1005 struct vm_fault *vmf)
Ross Zwislere30331f2017-09-06 16:18:39 -07001006{
1007 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001008 unsigned long vaddr = vmf->address;
Matthew Wilcoxb90ca5c2018-09-11 21:27:44 -07001009 pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1010 vm_fault_t ret;
Ross Zwislere30331f2017-09-06 16:18:39 -07001011
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001012 *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001013 DAX_ZERO_PAGE, false);
1014
Souptick Joarderab77dab2018-06-07 17:04:29 -07001015 ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
Ross Zwislere30331f2017-09-06 16:18:39 -07001016 trace_dax_load_hole(inode, vmf, ret);
1017 return ret;
1018}
1019
Vishal Verma4b0228f2016-04-21 15:13:46 -04001020static bool dax_range_is_aligned(struct block_device *bdev,
1021 unsigned int offset, unsigned int length)
1022{
1023 unsigned short sector_size = bdev_logical_block_size(bdev);
1024
1025 if (!IS_ALIGNED(offset, sector_size))
1026 return false;
1027 if (!IS_ALIGNED(length, sector_size))
1028 return false;
1029
1030 return true;
1031}
1032
Dan Williamscccbce62017-01-27 13:31:42 -08001033int __dax_zero_page_range(struct block_device *bdev,
1034 struct dax_device *dax_dev, sector_t sector,
1035 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001036{
Dan Williamscccbce62017-01-27 13:31:42 -08001037 if (dax_range_is_aligned(bdev, offset, size)) {
1038 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001039
1040 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -07001041 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001042 } else {
Dan Williamscccbce62017-01-27 13:31:42 -08001043 pgoff_t pgoff;
1044 long rc, id;
1045 void *kaddr;
Dan Williamscccbce62017-01-27 13:31:42 -08001046
Dan Williamse84b83b2017-05-10 19:38:13 -07001047 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001048 if (rc)
1049 return rc;
1050
1051 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001052 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -08001053 if (rc < 0) {
1054 dax_read_unlock(id);
1055 return rc;
1056 }
Dan Williams81f55872017-05-29 13:12:20 -07001057 memset(kaddr + offset, 0, size);
Mikulas Patockac3ca0152017-08-31 21:47:43 -04001058 dax_flush(dax_dev, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -08001059 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001060 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001061 return 0;
1062}
1063EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1064
Christoph Hellwiga254e562016-09-19 11:24:49 +10001065static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001066dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001067 struct iomap *iomap)
1068{
Dan Williamscccbce62017-01-27 13:31:42 -08001069 struct block_device *bdev = iomap->bdev;
1070 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001071 struct iov_iter *iter = data;
1072 loff_t end = pos + length, done = 0;
1073 ssize_t ret = 0;
Dan Williamsa77d4782018-03-16 17:36:44 -07001074 size_t xfer;
Dan Williamscccbce62017-01-27 13:31:42 -08001075 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001076
1077 if (iov_iter_rw(iter) == READ) {
1078 end = min(end, i_size_read(inode));
1079 if (pos >= end)
1080 return 0;
1081
1082 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1083 return iov_iter_zero(min(length, end - pos), iter);
1084 }
1085
1086 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1087 return -EIO;
1088
Jan Karae3fce682016-08-10 17:10:28 +02001089 /*
1090 * Write can allocate block for an area which has a hole page mapped
1091 * into page tables. We have to tear down these mappings so that data
1092 * written by write(2) is visible in mmap.
1093 */
Jan Karacd656372017-05-12 15:46:50 -07001094 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +02001095 invalidate_inode_pages2_range(inode->i_mapping,
1096 pos >> PAGE_SHIFT,
1097 (end - 1) >> PAGE_SHIFT);
1098 }
1099
Dan Williamscccbce62017-01-27 13:31:42 -08001100 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +10001101 while (pos < end) {
1102 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -08001103 const size_t size = ALIGN(length + offset, PAGE_SIZE);
1104 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001105 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -08001106 pgoff_t pgoff;
1107 void *kaddr;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001108
Michal Hockod1908f52017-02-03 13:13:26 -08001109 if (fatal_signal_pending(current)) {
1110 ret = -EINTR;
1111 break;
1112 }
1113
Dan Williamscccbce62017-01-27 13:31:42 -08001114 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1115 if (ret)
1116 break;
1117
1118 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001119 &kaddr, NULL);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001120 if (map_len < 0) {
1121 ret = map_len;
1122 break;
1123 }
1124
Dan Williamscccbce62017-01-27 13:31:42 -08001125 map_len = PFN_PHYS(map_len);
1126 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001127 map_len -= offset;
1128 if (map_len > end - pos)
1129 map_len = end - pos;
1130
Ross Zwislera2e050f2017-09-06 16:18:54 -07001131 /*
1132 * The userspace address for the memory copy has already been
1133 * validated via access_ok() in either vfs_read() or
1134 * vfs_write(), depending on which operation we are doing.
1135 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001136 if (iov_iter_rw(iter) == WRITE)
Dan Williamsa77d4782018-03-16 17:36:44 -07001137 xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
Dan Williamsfec53772017-05-29 21:56:49 -07001138 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001139 else
Dan Williamsa77d4782018-03-16 17:36:44 -07001140 xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001141 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001142
Dan Williamsa77d4782018-03-16 17:36:44 -07001143 pos += xfer;
1144 length -= xfer;
1145 done += xfer;
1146
1147 if (xfer == 0)
1148 ret = -EFAULT;
1149 if (xfer < map_len)
1150 break;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001151 }
Dan Williamscccbce62017-01-27 13:31:42 -08001152 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001153
1154 return done ? done : ret;
1155}
1156
1157/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001158 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001159 * @iocb: The control block for this I/O
1160 * @iter: The addresses to do I/O from or to
1161 * @ops: iomap ops passed from the file system
1162 *
1163 * This function performs read and write operations to directly mapped
1164 * persistent memory. The callers needs to take care of read/write exclusion
1165 * and evicting any page cache pages in the region under I/O.
1166 */
1167ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001168dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001169 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001170{
1171 struct address_space *mapping = iocb->ki_filp->f_mapping;
1172 struct inode *inode = mapping->host;
1173 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1174 unsigned flags = 0;
1175
Christoph Hellwig168316d2017-02-08 14:43:13 -05001176 if (iov_iter_rw(iter) == WRITE) {
1177 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001178 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001179 } else {
1180 lockdep_assert_held(&inode->i_rwsem);
1181 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001182
Christoph Hellwiga254e562016-09-19 11:24:49 +10001183 while (iov_iter_count(iter)) {
1184 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001185 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001186 if (ret <= 0)
1187 break;
1188 pos += ret;
1189 done += ret;
1190 }
1191
1192 iocb->ki_pos += done;
1193 return done ? done : ret;
1194}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001195EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001196
Souptick Joarderab77dab2018-06-07 17:04:29 -07001197static vm_fault_t dax_fault_return(int error)
Jan Kara9f141d62016-10-19 14:34:31 +02001198{
1199 if (error == 0)
1200 return VM_FAULT_NOPAGE;
1201 if (error == -ENOMEM)
1202 return VM_FAULT_OOM;
1203 return VM_FAULT_SIGBUS;
1204}
1205
Dan Williamsaaa422c2017-11-13 16:38:44 -08001206/*
1207 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1208 * flushed on write-faults (non-cow), but not read-faults.
1209 */
1210static bool dax_fault_is_synchronous(unsigned long flags,
1211 struct vm_area_struct *vma, struct iomap *iomap)
1212{
1213 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1214 && (iomap->flags & IOMAP_F_DIRTY);
1215}
1216
Souptick Joarderab77dab2018-06-07 17:04:29 -07001217static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
Jan Karac0b24622018-01-07 16:38:43 -05001218 int *iomap_errp, const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001219{
Jan Karaa0987ad2017-11-01 16:36:34 +01001220 struct vm_area_struct *vma = vmf->vma;
1221 struct address_space *mapping = vma->vm_file->f_mapping;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001222 XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001223 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001224 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001225 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001226 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001227 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001228 int error, major = 0;
Jan Karad2c43ef2017-11-01 16:36:35 +01001229 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001230 bool sync;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001231 vm_fault_t ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001232 void *entry;
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001233 pfn_t pfn;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001234
Souptick Joarderab77dab2018-06-07 17:04:29 -07001235 trace_dax_pte_fault(inode, vmf, ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001236 /*
1237 * Check whether offset isn't beyond end of file now. Caller is supposed
1238 * to hold locks serializing us with truncate / punch hole so this is
1239 * a reliable test.
1240 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001241 if (pos >= i_size_read(inode)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001242 ret = VM_FAULT_SIGBUS;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001243 goto out;
1244 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001245
Jan Karad2c43ef2017-11-01 16:36:35 +01001246 if (write && !vmf->cow_page)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001247 flags |= IOMAP_WRITE;
1248
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001249 entry = grab_mapping_entry(&xas, mapping, 0);
1250 if (xa_is_internal(entry)) {
1251 ret = xa_to_internal(entry);
Jan Kara13e451f2017-05-12 15:46:57 -07001252 goto out;
1253 }
1254
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001255 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001256 * It is possible, particularly with mixed reads & writes to private
1257 * mappings, that we have raced with a PMD fault that overlaps with
1258 * the PTE we need to set up. If so just return and the fault will be
1259 * retried.
1260 */
1261 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001262 ret = VM_FAULT_NOPAGE;
Ross Zwislere2093922017-06-02 14:46:37 -07001263 goto unlock_entry;
1264 }
1265
1266 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001267 * Note that we don't bother to use iomap_apply here: DAX required
1268 * the file system block size to be equal the page size, which means
1269 * that we never have to deal with more than a single extent here.
1270 */
1271 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Jan Karac0b24622018-01-07 16:38:43 -05001272 if (iomap_errp)
1273 *iomap_errp = error;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001274 if (error) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001275 ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001276 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001277 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001278 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001279 error = -EIO; /* fs corruption? */
1280 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001281 }
1282
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001283 if (vmf->cow_page) {
Jan Kara31a6f1a2017-11-01 16:36:32 +01001284 sector_t sector = dax_iomap_sector(&iomap, pos);
1285
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001286 switch (iomap.type) {
1287 case IOMAP_HOLE:
1288 case IOMAP_UNWRITTEN:
1289 clear_user_highpage(vmf->cow_page, vaddr);
1290 break;
1291 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001292 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1293 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001294 break;
1295 default:
1296 WARN_ON_ONCE(1);
1297 error = -EIO;
1298 break;
1299 }
1300
1301 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001302 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001303
1304 __SetPageUptodate(vmf->cow_page);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001305 ret = finish_fault(vmf);
1306 if (!ret)
1307 ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001308 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001309 }
1310
Dan Williamsaaa422c2017-11-13 16:38:44 -08001311 sync = dax_fault_is_synchronous(flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001312
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001313 switch (iomap.type) {
1314 case IOMAP_MAPPED:
1315 if (iomap.flags & IOMAP_F_NEW) {
1316 count_vm_event(PGMAJFAULT);
Jan Karaa0987ad2017-11-01 16:36:34 +01001317 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001318 major = VM_FAULT_MAJOR;
1319 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001320 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1321 if (error < 0)
1322 goto error_finish_iomap;
1323
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001324 entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
Jan Karacaa51d22017-11-01 16:36:42 +01001325 0, write && !sync);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001326
Jan Karacaa51d22017-11-01 16:36:42 +01001327 /*
1328 * If we are doing synchronous page fault and inode needs fsync,
1329 * we can insert PTE into page tables only after that happens.
1330 * Skip insertion for now and return the pfn so that caller can
1331 * insert it after fsync is done.
1332 */
1333 if (sync) {
1334 if (WARN_ON_ONCE(!pfnp)) {
1335 error = -EIO;
1336 goto error_finish_iomap;
1337 }
1338 *pfnp = pfn;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001339 ret = VM_FAULT_NEEDDSYNC | major;
Jan Karacaa51d22017-11-01 16:36:42 +01001340 goto finish_iomap;
1341 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001342 trace_dax_insert_mapping(inode, vmf, entry);
1343 if (write)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001344 ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001345 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001346 ret = vmf_insert_mixed(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001347
Souptick Joarderab77dab2018-06-07 17:04:29 -07001348 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001349 case IOMAP_UNWRITTEN:
1350 case IOMAP_HOLE:
Jan Karad2c43ef2017-11-01 16:36:35 +01001351 if (!write) {
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001352 ret = dax_load_hole(&xas, mapping, &entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001353 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001354 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001355 /*FALLTHRU*/
1356 default:
1357 WARN_ON_ONCE(1);
1358 error = -EIO;
1359 break;
1360 }
1361
Jan Kara13e451f2017-05-12 15:46:57 -07001362 error_finish_iomap:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001363 ret = dax_fault_return(error);
Jan Kara9f141d62016-10-19 14:34:31 +02001364 finish_iomap:
1365 if (ops->iomap_end) {
1366 int copied = PAGE_SIZE;
1367
Souptick Joarderab77dab2018-06-07 17:04:29 -07001368 if (ret & VM_FAULT_ERROR)
Jan Kara9f141d62016-10-19 14:34:31 +02001369 copied = 0;
1370 /*
1371 * The fault is done by now and there's no way back (other
1372 * thread may be already happily using PTE we have installed).
1373 * Just ignore error from ->iomap_end since we cannot do much
1374 * with it.
1375 */
1376 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001377 }
Jan Kara13e451f2017-05-12 15:46:57 -07001378 unlock_entry:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001379 dax_unlock_entry(&xas, entry);
Jan Kara13e451f2017-05-12 15:46:57 -07001380 out:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001381 trace_dax_pte_fault_done(inode, vmf, ret);
1382 return ret | major;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001383}
Ross Zwisler642261a2016-11-08 11:34:45 +11001384
1385#ifdef CONFIG_FS_DAX_PMD
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001386static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
1387 struct iomap *iomap, void **entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001388{
Dave Jiangf4200392017-02-22 15:40:06 -08001389 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1390 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001391 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001392 struct page *zero_page;
1393 spinlock_t *ptl;
1394 pmd_t pmd_entry;
Dan Williams3fe07912017-10-14 17:13:45 -07001395 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001396
Dave Jiangf4200392017-02-22 15:40:06 -08001397 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001398
1399 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001400 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001401
Dan Williams3fe07912017-10-14 17:13:45 -07001402 pfn = page_to_pfn_t(zero_page);
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001403 *entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001404 DAX_PMD | DAX_ZERO_PAGE, false);
Ross Zwisler642261a2016-11-08 11:34:45 +11001405
Dave Jiangf4200392017-02-22 15:40:06 -08001406 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1407 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001408 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001409 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001410 }
1411
Dave Jiangf4200392017-02-22 15:40:06 -08001412 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001413 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001414 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001415 spin_unlock(ptl);
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001416 trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001417 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001418
1419fallback:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001420 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001421 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001422}
1423
Souptick Joarderab77dab2018-06-07 17:04:29 -07001424static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Dave Jianga2d58162017-02-24 14:56:59 -08001425 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001426{
Dave Jiangf4200392017-02-22 15:40:06 -08001427 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001428 struct address_space *mapping = vma->vm_file->f_mapping;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001429 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
Dave Jiangd8a849e2017-02-22 15:40:03 -08001430 unsigned long pmd_addr = vmf->address & PMD_MASK;
1431 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001432 bool sync;
Jan Kara9484ab12016-11-10 10:26:50 +11001433 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001434 struct inode *inode = mapping->host;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001435 vm_fault_t result = VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001436 struct iomap iomap = { 0 };
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001437 pgoff_t max_pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001438 void *entry;
1439 loff_t pos;
1440 int error;
Jan Kara302a5e32017-11-01 16:36:37 +01001441 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001442
Ross Zwisler282a8e02017-02-22 15:39:50 -08001443 /*
1444 * Check whether offset isn't beyond end of file now. Caller is
1445 * supposed to hold locks serializing us with truncate / punch hole so
1446 * this is a reliable test.
1447 */
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001448 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001449
Dave Jiangf4200392017-02-22 15:40:06 -08001450 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001451
Ross Zwislerfffa2812017-08-25 15:55:36 -07001452 /*
1453 * Make sure that the faulting address's PMD offset (color) matches
1454 * the PMD offset from the start of the file. This is necessary so
1455 * that a PMD range in the page table overlaps exactly with a PMD
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001456 * range in the page cache.
Ross Zwislerfffa2812017-08-25 15:55:36 -07001457 */
1458 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1459 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1460 goto fallback;
1461
Ross Zwisler642261a2016-11-08 11:34:45 +11001462 /* Fall back to PTEs if we're going to COW */
1463 if (write && !(vma->vm_flags & VM_SHARED))
1464 goto fallback;
1465
1466 /* If the PMD would extend outside the VMA */
1467 if (pmd_addr < vma->vm_start)
1468 goto fallback;
1469 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1470 goto fallback;
1471
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001472 if (xas.xa_index >= max_pgoff) {
Ross Zwisler282a8e02017-02-22 15:39:50 -08001473 result = VM_FAULT_SIGBUS;
1474 goto out;
1475 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001476
1477 /* If the PMD would extend beyond the file size */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001478 if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff)
Ross Zwisler642261a2016-11-08 11:34:45 +11001479 goto fallback;
1480
1481 /*
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001482 * grab_mapping_entry() will make sure we get an empty PMD entry,
1483 * a zero PMD entry or a DAX PMD. If it can't (because a PTE
1484 * entry is already in the array, for instance), it will return
1485 * VM_FAULT_FALLBACK.
Jan Kara9f141d62016-10-19 14:34:31 +02001486 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001487 entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
1488 if (xa_is_internal(entry)) {
1489 result = xa_to_internal(entry);
Ross Zwisler876f2942017-05-12 15:47:00 -07001490 goto fallback;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001491 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001492
1493 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001494 * It is possible, particularly with mixed reads & writes to private
1495 * mappings, that we have raced with a PTE fault that overlaps with
1496 * the PMD we need to set up. If so just return and the fault will be
1497 * retried.
1498 */
1499 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1500 !pmd_devmap(*vmf->pmd)) {
1501 result = 0;
1502 goto unlock_entry;
1503 }
1504
1505 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001506 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1507 * setting up a mapping, so really we're using iomap_begin() as a way
1508 * to look up our filesystem block.
1509 */
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001510 pos = (loff_t)xas.xa_index << PAGE_SHIFT;
Ross Zwisler876f2942017-05-12 15:47:00 -07001511 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1512 if (error)
1513 goto unlock_entry;
1514
1515 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001516 goto finish_iomap;
1517
Dan Williamsaaa422c2017-11-13 16:38:44 -08001518 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001519
Ross Zwisler642261a2016-11-08 11:34:45 +11001520 switch (iomap.type) {
1521 case IOMAP_MAPPED:
Jan Kara302a5e32017-11-01 16:36:37 +01001522 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1523 if (error < 0)
1524 goto finish_iomap;
1525
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001526 entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001527 DAX_PMD, write && !sync);
Jan Kara302a5e32017-11-01 16:36:37 +01001528
Jan Karacaa51d22017-11-01 16:36:42 +01001529 /*
1530 * If we are doing synchronous page fault and inode needs fsync,
1531 * we can insert PMD into page tables only after that happens.
1532 * Skip insertion for now and return the pfn so that caller can
1533 * insert it after fsync is done.
1534 */
1535 if (sync) {
1536 if (WARN_ON_ONCE(!pfnp))
1537 goto finish_iomap;
1538 *pfnp = pfn;
1539 result = VM_FAULT_NEEDDSYNC;
1540 goto finish_iomap;
1541 }
1542
Jan Kara302a5e32017-11-01 16:36:37 +01001543 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1544 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1545 write);
Ross Zwisler642261a2016-11-08 11:34:45 +11001546 break;
1547 case IOMAP_UNWRITTEN:
1548 case IOMAP_HOLE:
1549 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001550 break;
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001551 result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001552 break;
1553 default:
1554 WARN_ON_ONCE(1);
1555 break;
1556 }
1557
Jan Kara9f141d62016-10-19 14:34:31 +02001558 finish_iomap:
1559 if (ops->iomap_end) {
1560 int copied = PMD_SIZE;
1561
1562 if (result == VM_FAULT_FALLBACK)
1563 copied = 0;
1564 /*
1565 * The fault is done by now and there's no way back (other
1566 * thread may be already happily using PMD we have installed).
1567 * Just ignore error from ->iomap_end since we cannot do much
1568 * with it.
1569 */
1570 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1571 &iomap);
1572 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001573 unlock_entry:
Matthew Wilcoxb15cd802018-03-29 22:58:27 -04001574 dax_unlock_entry(&xas, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001575 fallback:
1576 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001577 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001578 count_vm_event(THP_FAULT_FALLBACK);
1579 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001580out:
Dave Jiangf4200392017-02-22 15:40:06 -08001581 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001582 return result;
1583}
Dave Jianga2d58162017-02-24 14:56:59 -08001584#else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001585static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001586 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001587{
1588 return VM_FAULT_FALLBACK;
1589}
Ross Zwisler642261a2016-11-08 11:34:45 +11001590#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001591
1592/**
1593 * dax_iomap_fault - handle a page fault on a DAX file
1594 * @vmf: The description of the fault
Jan Karacec04e82017-11-01 16:36:38 +01001595 * @pe_size: Size of the page to fault in
Jan Kara9a0dd422017-11-01 16:36:39 +01001596 * @pfnp: PFN to insert for synchronous faults if fsync is required
Jan Karac0b24622018-01-07 16:38:43 -05001597 * @iomap_errp: Storage for detailed error code in case of error
Jan Karacec04e82017-11-01 16:36:38 +01001598 * @ops: Iomap ops passed from the file system
Dave Jianga2d58162017-02-24 14:56:59 -08001599 *
1600 * When a page fault occurs, filesystems may call this helper in
1601 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1602 * has done all the necessary locking for page fault to proceed
1603 * successfully.
1604 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001605vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
Jan Karac0b24622018-01-07 16:38:43 -05001606 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001607{
Dave Jiangc791ace2017-02-24 14:57:08 -08001608 switch (pe_size) {
1609 case PE_SIZE_PTE:
Jan Karac0b24622018-01-07 16:38:43 -05001610 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001611 case PE_SIZE_PMD:
Jan Kara9a0dd422017-11-01 16:36:39 +01001612 return dax_iomap_pmd_fault(vmf, pfnp, ops);
Dave Jianga2d58162017-02-24 14:56:59 -08001613 default:
1614 return VM_FAULT_FALLBACK;
1615 }
1616}
1617EXPORT_SYMBOL_GPL(dax_iomap_fault);
Jan Kara71eab6d2017-11-01 16:36:43 +01001618
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001619/*
Jan Kara71eab6d2017-11-01 16:36:43 +01001620 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1621 * @vmf: The description of the fault
Jan Kara71eab6d2017-11-01 16:36:43 +01001622 * @pfn: PFN to insert
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001623 * @order: Order of entry to insert.
Jan Kara71eab6d2017-11-01 16:36:43 +01001624 *
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001625 * This function inserts a writeable PTE or PMD entry into the page tables
1626 * for an mmaped DAX file. It also marks the page cache entry as dirty.
Jan Kara71eab6d2017-11-01 16:36:43 +01001627 */
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001628static vm_fault_t
1629dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
Jan Kara71eab6d2017-11-01 16:36:43 +01001630{
1631 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001632 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1633 void *entry;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001634 vm_fault_t ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001635
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001636 xas_lock_irq(&xas);
1637 entry = get_unlocked_entry(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001638 /* Did we race with someone splitting entry or so? */
1639 if (!entry ||
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001640 (order == 0 && !dax_is_pte_entry(entry)) ||
Matthew Wilcox0e40de02018-11-16 15:19:13 -05001641 (order == PMD_ORDER && !dax_is_pmd_entry(entry))) {
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001642 put_unlocked_entry(&xas, entry);
1643 xas_unlock_irq(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001644 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1645 VM_FAULT_NOPAGE);
1646 return VM_FAULT_NOPAGE;
1647 }
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001648 xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1649 dax_lock_entry(&xas, entry);
1650 xas_unlock_irq(&xas);
1651 if (order == 0)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001652 ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
Jan Kara71eab6d2017-11-01 16:36:43 +01001653#ifdef CONFIG_FS_DAX_PMD
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001654 else if (order == PMD_ORDER)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001655 ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Jan Kara71eab6d2017-11-01 16:36:43 +01001656 pfn, true);
Jan Kara71eab6d2017-11-01 16:36:43 +01001657#endif
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001658 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001659 ret = VM_FAULT_FALLBACK;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001660 dax_unlock_entry(&xas, entry);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001661 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1662 return ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001663}
1664
1665/**
1666 * dax_finish_sync_fault - finish synchronous page fault
1667 * @vmf: The description of the fault
1668 * @pe_size: Size of entry to be inserted
1669 * @pfn: PFN to insert
1670 *
1671 * This function ensures that the file range touched by the page fault is
1672 * stored persistently on the media and handles inserting of appropriate page
1673 * table entry.
1674 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001675vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1676 enum page_entry_size pe_size, pfn_t pfn)
Jan Kara71eab6d2017-11-01 16:36:43 +01001677{
1678 int err;
1679 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001680 unsigned int order = pe_order(pe_size);
1681 size_t len = PAGE_SIZE << order;
Jan Kara71eab6d2017-11-01 16:36:43 +01001682
Jan Kara71eab6d2017-11-01 16:36:43 +01001683 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1684 if (err)
1685 return VM_FAULT_SIGBUS;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001686 return dax_insert_pfn_mkwrite(vmf, pfn, order);
Jan Kara71eab6d2017-11-01 16:36:43 +01001687}
1688EXPORT_SYMBOL_GPL(dax_finish_sync_fault);