xref: /openbmc/linux/fs/dax.c (revision 55e56f06)
1d475c634SMatthew Wilcox /*
2d475c634SMatthew Wilcox  * fs/dax.c - Direct Access filesystem code
3d475c634SMatthew Wilcox  * Copyright (c) 2013-2014 Intel Corporation
4d475c634SMatthew Wilcox  * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5d475c634SMatthew Wilcox  * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
6d475c634SMatthew Wilcox  *
7d475c634SMatthew Wilcox  * This program is free software; you can redistribute it and/or modify it
8d475c634SMatthew Wilcox  * under the terms and conditions of the GNU General Public License,
9d475c634SMatthew Wilcox  * version 2, as published by the Free Software Foundation.
10d475c634SMatthew Wilcox  *
11d475c634SMatthew Wilcox  * This program is distributed in the hope it will be useful, but WITHOUT
12d475c634SMatthew Wilcox  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13d475c634SMatthew Wilcox  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14d475c634SMatthew Wilcox  * more details.
15d475c634SMatthew Wilcox  */
16d475c634SMatthew Wilcox 
17d475c634SMatthew Wilcox #include <linux/atomic.h>
18d475c634SMatthew Wilcox #include <linux/blkdev.h>
19d475c634SMatthew Wilcox #include <linux/buffer_head.h>
20d77e92e2SRoss Zwisler #include <linux/dax.h>
21d475c634SMatthew Wilcox #include <linux/fs.h>
22d475c634SMatthew Wilcox #include <linux/genhd.h>
23f7ca90b1SMatthew Wilcox #include <linux/highmem.h>
24f7ca90b1SMatthew Wilcox #include <linux/memcontrol.h>
25f7ca90b1SMatthew Wilcox #include <linux/mm.h>
26d475c634SMatthew Wilcox #include <linux/mutex.h>
279973c98eSRoss Zwisler #include <linux/pagevec.h>
28289c6aedSMatthew Wilcox #include <linux/sched.h>
29f361bf4aSIngo Molnar #include <linux/sched/signal.h>
30d475c634SMatthew Wilcox #include <linux/uio.h>
31f7ca90b1SMatthew Wilcox #include <linux/vmstat.h>
3234c0fd54SDan Williams #include <linux/pfn_t.h>
330e749e54SDan Williams #include <linux/sizes.h>
344b4bb46dSJan Kara #include <linux/mmu_notifier.h>
35a254e568SChristoph Hellwig #include <linux/iomap.h>
36a254e568SChristoph Hellwig #include "internal.h"
37d475c634SMatthew Wilcox 
38282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS
39282a8e03SRoss Zwisler #include <trace/events/fs_dax.h>
40282a8e03SRoss Zwisler 
41cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size)
42cfc93c6cSMatthew Wilcox {
43cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PTE)
44cfc93c6cSMatthew Wilcox 		return PAGE_SHIFT - PAGE_SHIFT;
45cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PMD)
46cfc93c6cSMatthew Wilcox 		return PMD_SHIFT - PAGE_SHIFT;
47cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PUD)
48cfc93c6cSMatthew Wilcox 		return PUD_SHIFT - PAGE_SHIFT;
49cfc93c6cSMatthew Wilcox 	return ~0;
50cfc93c6cSMatthew Wilcox }
51cfc93c6cSMatthew Wilcox 
52ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */
53ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12
54ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
55ac401cc7SJan Kara 
56917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset.  */
57917f3452SRoss Zwisler #define PG_PMD_COLOUR	((PMD_SIZE >> PAGE_SHIFT) - 1)
58977fbdcdSMatthew Wilcox #define PG_PMD_NR	(PMD_SIZE >> PAGE_SHIFT)
59917f3452SRoss Zwisler 
60cfc93c6cSMatthew Wilcox /* The order of a PMD entry */
61cfc93c6cSMatthew Wilcox #define PMD_ORDER	(PMD_SHIFT - PAGE_SHIFT)
62cfc93c6cSMatthew Wilcox 
63ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
64ac401cc7SJan Kara 
65ac401cc7SJan Kara static int __init init_dax_wait_table(void)
66ac401cc7SJan Kara {
67ac401cc7SJan Kara 	int i;
68ac401cc7SJan Kara 
69ac401cc7SJan Kara 	for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
70ac401cc7SJan Kara 		init_waitqueue_head(wait_table + i);
71ac401cc7SJan Kara 	return 0;
72ac401cc7SJan Kara }
73ac401cc7SJan Kara fs_initcall(init_dax_wait_table);
74ac401cc7SJan Kara 
75527b19d0SRoss Zwisler /*
763159f943SMatthew Wilcox  * DAX pagecache entries use XArray value entries so they can't be mistaken
773159f943SMatthew Wilcox  * for pages.  We use one bit for locking, one bit for the entry size (PMD)
783159f943SMatthew Wilcox  * and two more to tell us if the entry is a zero page or an empty entry that
793159f943SMatthew Wilcox  * is just used for locking.  In total four special bits.
80527b19d0SRoss Zwisler  *
81527b19d0SRoss Zwisler  * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
82527b19d0SRoss Zwisler  * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
83527b19d0SRoss Zwisler  * block allocation.
84527b19d0SRoss Zwisler  */
853159f943SMatthew Wilcox #define DAX_SHIFT	(4)
863159f943SMatthew Wilcox #define DAX_LOCKED	(1UL << 0)
873159f943SMatthew Wilcox #define DAX_PMD		(1UL << 1)
883159f943SMatthew Wilcox #define DAX_ZERO_PAGE	(1UL << 2)
893159f943SMatthew Wilcox #define DAX_EMPTY	(1UL << 3)
90527b19d0SRoss Zwisler 
91a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry)
92527b19d0SRoss Zwisler {
933159f943SMatthew Wilcox 	return xa_to_value(entry) >> DAX_SHIFT;
94527b19d0SRoss Zwisler }
95527b19d0SRoss Zwisler 
969f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags)
979f32d221SMatthew Wilcox {
989f32d221SMatthew Wilcox 	return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT));
999f32d221SMatthew Wilcox }
1009f32d221SMatthew Wilcox 
101cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry)
102cfc93c6cSMatthew Wilcox {
103cfc93c6cSMatthew Wilcox 	return xa_to_value(entry) & DAX_LOCKED;
104cfc93c6cSMatthew Wilcox }
105cfc93c6cSMatthew Wilcox 
106a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry)
107527b19d0SRoss Zwisler {
1083159f943SMatthew Wilcox 	if (xa_to_value(entry) & DAX_PMD)
109cfc93c6cSMatthew Wilcox 		return PMD_ORDER;
110527b19d0SRoss Zwisler 	return 0;
111527b19d0SRoss Zwisler }
112527b19d0SRoss Zwisler 
113fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry)
114642261acSRoss Zwisler {
1153159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_PMD;
116642261acSRoss Zwisler }
117642261acSRoss Zwisler 
118fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry)
119642261acSRoss Zwisler {
1203159f943SMatthew Wilcox 	return !(xa_to_value(entry) & DAX_PMD);
121642261acSRoss Zwisler }
122642261acSRoss Zwisler 
123642261acSRoss Zwisler static int dax_is_zero_entry(void *entry)
124642261acSRoss Zwisler {
1253159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_ZERO_PAGE;
126642261acSRoss Zwisler }
127642261acSRoss Zwisler 
128642261acSRoss Zwisler static int dax_is_empty_entry(void *entry)
129642261acSRoss Zwisler {
1303159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_EMPTY;
131642261acSRoss Zwisler }
132642261acSRoss Zwisler 
133f7ca90b1SMatthew Wilcox /*
134a77d19f4SMatthew Wilcox  * DAX page cache entry locking
135ac401cc7SJan Kara  */
136ac401cc7SJan Kara struct exceptional_entry_key {
137ec4907ffSMatthew Wilcox 	struct xarray *xa;
13863e95b5cSRoss Zwisler 	pgoff_t entry_start;
139ac401cc7SJan Kara };
140ac401cc7SJan Kara 
141ac401cc7SJan Kara struct wait_exceptional_entry_queue {
142ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
143ac401cc7SJan Kara 	struct exceptional_entry_key key;
144ac401cc7SJan Kara };
145ac401cc7SJan Kara 
146b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas,
147b15cd800SMatthew Wilcox 		void *entry, struct exceptional_entry_key *key)
14863e95b5cSRoss Zwisler {
14963e95b5cSRoss Zwisler 	unsigned long hash;
150b15cd800SMatthew Wilcox 	unsigned long index = xas->xa_index;
15163e95b5cSRoss Zwisler 
15263e95b5cSRoss Zwisler 	/*
15363e95b5cSRoss Zwisler 	 * If 'entry' is a PMD, align the 'index' that we use for the wait
15463e95b5cSRoss Zwisler 	 * queue to the start of that PMD.  This ensures that all offsets in
15563e95b5cSRoss Zwisler 	 * the range covered by the PMD map to the same bit lock.
15663e95b5cSRoss Zwisler 	 */
157642261acSRoss Zwisler 	if (dax_is_pmd_entry(entry))
158917f3452SRoss Zwisler 		index &= ~PG_PMD_COLOUR;
159b15cd800SMatthew Wilcox 	key->xa = xas->xa;
16063e95b5cSRoss Zwisler 	key->entry_start = index;
16163e95b5cSRoss Zwisler 
162b15cd800SMatthew Wilcox 	hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS);
16363e95b5cSRoss Zwisler 	return wait_table + hash;
16463e95b5cSRoss Zwisler }
16563e95b5cSRoss Zwisler 
166ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
167ec4907ffSMatthew Wilcox 		unsigned int mode, int sync, void *keyp)
168ac401cc7SJan Kara {
169ac401cc7SJan Kara 	struct exceptional_entry_key *key = keyp;
170ac401cc7SJan Kara 	struct wait_exceptional_entry_queue *ewait =
171ac401cc7SJan Kara 		container_of(wait, struct wait_exceptional_entry_queue, wait);
172ac401cc7SJan Kara 
173ec4907ffSMatthew Wilcox 	if (key->xa != ewait->key.xa ||
17463e95b5cSRoss Zwisler 	    key->entry_start != ewait->key.entry_start)
175ac401cc7SJan Kara 		return 0;
176ac401cc7SJan Kara 	return autoremove_wake_function(wait, mode, sync, NULL);
177ac401cc7SJan Kara }
178ac401cc7SJan Kara 
179ac401cc7SJan Kara /*
180b93b0163SMatthew Wilcox  * @entry may no longer be the entry at the index in the mapping.
181b93b0163SMatthew Wilcox  * The important information it's conveying is whether the entry at
182b93b0163SMatthew Wilcox  * this index used to be a PMD entry.
183e30331ffSRoss Zwisler  */
184b15cd800SMatthew Wilcox static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
185e30331ffSRoss Zwisler {
186e30331ffSRoss Zwisler 	struct exceptional_entry_key key;
187e30331ffSRoss Zwisler 	wait_queue_head_t *wq;
188e30331ffSRoss Zwisler 
189b15cd800SMatthew Wilcox 	wq = dax_entry_waitqueue(xas, entry, &key);
190e30331ffSRoss Zwisler 
191e30331ffSRoss Zwisler 	/*
192e30331ffSRoss Zwisler 	 * Checking for locked entry and prepare_to_wait_exclusive() happens
193b93b0163SMatthew Wilcox 	 * under the i_pages lock, ditto for entry handling in our callers.
194e30331ffSRoss Zwisler 	 * So at this point all tasks that could have seen our entry locked
195e30331ffSRoss Zwisler 	 * must be in the waitqueue and the following check will see them.
196e30331ffSRoss Zwisler 	 */
197e30331ffSRoss Zwisler 	if (waitqueue_active(wq))
198e30331ffSRoss Zwisler 		__wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
199e30331ffSRoss Zwisler }
200e30331ffSRoss Zwisler 
201cfc93c6cSMatthew Wilcox /*
202cfc93c6cSMatthew Wilcox  * Look up entry in page cache, wait for it to become unlocked if it
203cfc93c6cSMatthew Wilcox  * is a DAX entry and return it.  The caller must subsequently call
204cfc93c6cSMatthew Wilcox  * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
205cfc93c6cSMatthew Wilcox  * if it did.
206cfc93c6cSMatthew Wilcox  *
207cfc93c6cSMatthew Wilcox  * Must be called with the i_pages lock held.
208cfc93c6cSMatthew Wilcox  */
209cfc93c6cSMatthew Wilcox static void *get_unlocked_entry(struct xa_state *xas)
210cfc93c6cSMatthew Wilcox {
211cfc93c6cSMatthew Wilcox 	void *entry;
212cfc93c6cSMatthew Wilcox 	struct wait_exceptional_entry_queue ewait;
213cfc93c6cSMatthew Wilcox 	wait_queue_head_t *wq;
214cfc93c6cSMatthew Wilcox 
215cfc93c6cSMatthew Wilcox 	init_wait(&ewait.wait);
216cfc93c6cSMatthew Wilcox 	ewait.wait.func = wake_exceptional_entry_func;
217cfc93c6cSMatthew Wilcox 
218cfc93c6cSMatthew Wilcox 	for (;;) {
2190e40de03SMatthew Wilcox 		entry = xas_find_conflict(xas);
2200e40de03SMatthew Wilcox 		if (!entry || WARN_ON_ONCE(!xa_is_value(entry)) ||
221cfc93c6cSMatthew Wilcox 				!dax_is_locked(entry))
222cfc93c6cSMatthew Wilcox 			return entry;
223cfc93c6cSMatthew Wilcox 
224b15cd800SMatthew Wilcox 		wq = dax_entry_waitqueue(xas, entry, &ewait.key);
225cfc93c6cSMatthew Wilcox 		prepare_to_wait_exclusive(wq, &ewait.wait,
226cfc93c6cSMatthew Wilcox 					  TASK_UNINTERRUPTIBLE);
227cfc93c6cSMatthew Wilcox 		xas_unlock_irq(xas);
228cfc93c6cSMatthew Wilcox 		xas_reset(xas);
229cfc93c6cSMatthew Wilcox 		schedule();
230cfc93c6cSMatthew Wilcox 		finish_wait(wq, &ewait.wait);
231cfc93c6cSMatthew Wilcox 		xas_lock_irq(xas);
232cfc93c6cSMatthew Wilcox 	}
233cfc93c6cSMatthew Wilcox }
234cfc93c6cSMatthew Wilcox 
23555e56f06SMatthew Wilcox /*
23655e56f06SMatthew Wilcox  * The only thing keeping the address space around is the i_pages lock
23755e56f06SMatthew Wilcox  * (it's cycled in clear_inode() after removing the entries from i_pages)
23855e56f06SMatthew Wilcox  * After we call xas_unlock_irq(), we cannot touch xas->xa.
23955e56f06SMatthew Wilcox  */
24055e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry)
24155e56f06SMatthew Wilcox {
24255e56f06SMatthew Wilcox 	struct wait_exceptional_entry_queue ewait;
24355e56f06SMatthew Wilcox 	wait_queue_head_t *wq;
24455e56f06SMatthew Wilcox 
24555e56f06SMatthew Wilcox 	init_wait(&ewait.wait);
24655e56f06SMatthew Wilcox 	ewait.wait.func = wake_exceptional_entry_func;
24755e56f06SMatthew Wilcox 
24855e56f06SMatthew Wilcox 	wq = dax_entry_waitqueue(xas, entry, &ewait.key);
24955e56f06SMatthew Wilcox 	prepare_to_wait_exclusive(wq, &ewait.wait, TASK_UNINTERRUPTIBLE);
25055e56f06SMatthew Wilcox 	xas_unlock_irq(xas);
25155e56f06SMatthew Wilcox 	schedule();
25255e56f06SMatthew Wilcox 	finish_wait(wq, &ewait.wait);
25355e56f06SMatthew Wilcox 
25455e56f06SMatthew Wilcox 	/*
25555e56f06SMatthew Wilcox 	 * Entry lock waits are exclusive. Wake up the next waiter since
25655e56f06SMatthew Wilcox 	 * we aren't sure we will acquire the entry lock and thus wake
25755e56f06SMatthew Wilcox 	 * the next waiter up on unlock.
25855e56f06SMatthew Wilcox 	 */
25955e56f06SMatthew Wilcox 	if (waitqueue_active(wq))
26055e56f06SMatthew Wilcox 		__wake_up(wq, TASK_NORMAL, 1, &ewait.key);
26155e56f06SMatthew Wilcox }
26255e56f06SMatthew Wilcox 
263cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry)
264cfc93c6cSMatthew Wilcox {
265cfc93c6cSMatthew Wilcox 	/* If we were the only waiter woken, wake the next one */
266cfc93c6cSMatthew Wilcox 	if (entry)
267cfc93c6cSMatthew Wilcox 		dax_wake_entry(xas, entry, false);
268cfc93c6cSMatthew Wilcox }
269cfc93c6cSMatthew Wilcox 
270cfc93c6cSMatthew Wilcox /*
271cfc93c6cSMatthew Wilcox  * We used the xa_state to get the entry, but then we locked the entry and
272cfc93c6cSMatthew Wilcox  * dropped the xa_lock, so we know the xa_state is stale and must be reset
273cfc93c6cSMatthew Wilcox  * before use.
274cfc93c6cSMatthew Wilcox  */
275cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry)
276cfc93c6cSMatthew Wilcox {
277cfc93c6cSMatthew Wilcox 	void *old;
278cfc93c6cSMatthew Wilcox 
2797ae2ea7dSMatthew Wilcox 	BUG_ON(dax_is_locked(entry));
280cfc93c6cSMatthew Wilcox 	xas_reset(xas);
281cfc93c6cSMatthew Wilcox 	xas_lock_irq(xas);
282cfc93c6cSMatthew Wilcox 	old = xas_store(xas, entry);
283cfc93c6cSMatthew Wilcox 	xas_unlock_irq(xas);
284cfc93c6cSMatthew Wilcox 	BUG_ON(!dax_is_locked(old));
285cfc93c6cSMatthew Wilcox 	dax_wake_entry(xas, entry, false);
286cfc93c6cSMatthew Wilcox }
287cfc93c6cSMatthew Wilcox 
288cfc93c6cSMatthew Wilcox /*
289cfc93c6cSMatthew Wilcox  * Return: The entry stored at this location before it was locked.
290cfc93c6cSMatthew Wilcox  */
291cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry)
292cfc93c6cSMatthew Wilcox {
293cfc93c6cSMatthew Wilcox 	unsigned long v = xa_to_value(entry);
294cfc93c6cSMatthew Wilcox 	return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
295cfc93c6cSMatthew Wilcox }
296cfc93c6cSMatthew Wilcox 
297d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry)
298d2c997c0SDan Williams {
299d2c997c0SDan Williams 	if (dax_is_zero_entry(entry))
300d2c997c0SDan Williams 		return 0;
301d2c997c0SDan Williams 	else if (dax_is_empty_entry(entry))
302d2c997c0SDan Williams 		return 0;
303d2c997c0SDan Williams 	else if (dax_is_pmd_entry(entry))
304d2c997c0SDan Williams 		return PMD_SIZE;
305d2c997c0SDan Williams 	else
306d2c997c0SDan Williams 		return PAGE_SIZE;
307d2c997c0SDan Williams }
308d2c997c0SDan Williams 
309a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry)
310d2c997c0SDan Williams {
311a77d19f4SMatthew Wilcox 	return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
312d2c997c0SDan Williams }
313d2c997c0SDan Williams 
314d2c997c0SDan Williams /*
315d2c997c0SDan Williams  * Iterate through all mapped pfns represented by an entry, i.e. skip
316d2c997c0SDan Williams  * 'empty' and 'zero' entries.
317d2c997c0SDan Williams  */
318d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \
319a77d19f4SMatthew Wilcox 	for (pfn = dax_to_pfn(entry); \
320a77d19f4SMatthew Wilcox 			pfn < dax_end_pfn(entry); pfn++)
321d2c997c0SDan Williams 
32273449dafSDan Williams /*
32373449dafSDan Williams  * TODO: for reflink+dax we need a way to associate a single page with
32473449dafSDan Williams  * multiple address_space instances at different linear_page_index()
32573449dafSDan Williams  * offsets.
32673449dafSDan Williams  */
32773449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping,
32873449dafSDan Williams 		struct vm_area_struct *vma, unsigned long address)
329d2c997c0SDan Williams {
33073449dafSDan Williams 	unsigned long size = dax_entry_size(entry), pfn, index;
33173449dafSDan Williams 	int i = 0;
332d2c997c0SDan Williams 
333d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
334d2c997c0SDan Williams 		return;
335d2c997c0SDan Williams 
33673449dafSDan Williams 	index = linear_page_index(vma, address & ~(size - 1));
337d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
338d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
339d2c997c0SDan Williams 
340d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping);
341d2c997c0SDan Williams 		page->mapping = mapping;
34273449dafSDan Williams 		page->index = index + i++;
343d2c997c0SDan Williams 	}
344d2c997c0SDan Williams }
345d2c997c0SDan Williams 
346d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping,
347d2c997c0SDan Williams 		bool trunc)
348d2c997c0SDan Williams {
349d2c997c0SDan Williams 	unsigned long pfn;
350d2c997c0SDan Williams 
351d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
352d2c997c0SDan Williams 		return;
353d2c997c0SDan Williams 
354d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
355d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
356d2c997c0SDan Williams 
357d2c997c0SDan Williams 		WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
358d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping && page->mapping != mapping);
359d2c997c0SDan Williams 		page->mapping = NULL;
36073449dafSDan Williams 		page->index = 0;
361d2c997c0SDan Williams 	}
362d2c997c0SDan Williams }
363d2c997c0SDan Williams 
3645fac7408SDan Williams static struct page *dax_busy_page(void *entry)
3655fac7408SDan Williams {
3665fac7408SDan Williams 	unsigned long pfn;
3675fac7408SDan Williams 
3685fac7408SDan Williams 	for_each_mapped_pfn(entry, pfn) {
3695fac7408SDan Williams 		struct page *page = pfn_to_page(pfn);
3705fac7408SDan Williams 
3715fac7408SDan Williams 		if (page_ref_count(page) > 1)
3725fac7408SDan Williams 			return page;
3735fac7408SDan Williams 	}
3745fac7408SDan Williams 	return NULL;
3755fac7408SDan Williams }
3765fac7408SDan Williams 
377c5bbd451SMatthew Wilcox /*
378c5bbd451SMatthew Wilcox  * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page
379c5bbd451SMatthew Wilcox  * @page: The page whose entry we want to lock
380c5bbd451SMatthew Wilcox  *
381c5bbd451SMatthew Wilcox  * Context: Process context.
382c5bbd451SMatthew Wilcox  * Return: %true if the entry was locked or does not need to be locked.
383c5bbd451SMatthew Wilcox  */
384c2a7d2a1SDan Williams bool dax_lock_mapping_entry(struct page *page)
385c2a7d2a1SDan Williams {
3869f32d221SMatthew Wilcox 	XA_STATE(xas, NULL, 0);
3879f32d221SMatthew Wilcox 	void *entry;
388c5bbd451SMatthew Wilcox 	bool locked;
389c2a7d2a1SDan Williams 
390c5bbd451SMatthew Wilcox 	/* Ensure page->mapping isn't freed while we look at it */
391c5bbd451SMatthew Wilcox 	rcu_read_lock();
392c2a7d2a1SDan Williams 	for (;;) {
3939f32d221SMatthew Wilcox 		struct address_space *mapping = READ_ONCE(page->mapping);
394c2a7d2a1SDan Williams 
395c5bbd451SMatthew Wilcox 		locked = false;
396c93db7bbSMatthew Wilcox 		if (!mapping || !dax_mapping(mapping))
397c5bbd451SMatthew Wilcox 			break;
398c2a7d2a1SDan Williams 
399c2a7d2a1SDan Williams 		/*
400c2a7d2a1SDan Williams 		 * In the device-dax case there's no need to lock, a
401c2a7d2a1SDan Williams 		 * struct dev_pagemap pin is sufficient to keep the
402c2a7d2a1SDan Williams 		 * inode alive, and we assume we have dev_pagemap pin
403c2a7d2a1SDan Williams 		 * otherwise we would not have a valid pfn_to_page()
404c2a7d2a1SDan Williams 		 * translation.
405c2a7d2a1SDan Williams 		 */
406c5bbd451SMatthew Wilcox 		locked = true;
4079f32d221SMatthew Wilcox 		if (S_ISCHR(mapping->host->i_mode))
408c5bbd451SMatthew Wilcox 			break;
409c2a7d2a1SDan Williams 
4109f32d221SMatthew Wilcox 		xas.xa = &mapping->i_pages;
4119f32d221SMatthew Wilcox 		xas_lock_irq(&xas);
412c2a7d2a1SDan Williams 		if (mapping != page->mapping) {
4139f32d221SMatthew Wilcox 			xas_unlock_irq(&xas);
414c2a7d2a1SDan Williams 			continue;
415c2a7d2a1SDan Williams 		}
4169f32d221SMatthew Wilcox 		xas_set(&xas, page->index);
4179f32d221SMatthew Wilcox 		entry = xas_load(&xas);
4189f32d221SMatthew Wilcox 		if (dax_is_locked(entry)) {
419c5bbd451SMatthew Wilcox 			rcu_read_unlock();
42055e56f06SMatthew Wilcox 			wait_entry_unlocked(&xas, entry);
421c5bbd451SMatthew Wilcox 			rcu_read_lock();
422c2a7d2a1SDan Williams 			continue;
423c2a7d2a1SDan Williams 		}
4249f32d221SMatthew Wilcox 		dax_lock_entry(&xas, entry);
4259f32d221SMatthew Wilcox 		xas_unlock_irq(&xas);
426c5bbd451SMatthew Wilcox 		break;
4279f32d221SMatthew Wilcox 	}
428c5bbd451SMatthew Wilcox 	rcu_read_unlock();
429c5bbd451SMatthew Wilcox 	return locked;
430c2a7d2a1SDan Williams }
431c2a7d2a1SDan Williams 
432c2a7d2a1SDan Williams void dax_unlock_mapping_entry(struct page *page)
433c2a7d2a1SDan Williams {
434c2a7d2a1SDan Williams 	struct address_space *mapping = page->mapping;
4359f32d221SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, page->index);
436fda490d3SMatthew Wilcox 	void *entry;
437c2a7d2a1SDan Williams 
4389f32d221SMatthew Wilcox 	if (S_ISCHR(mapping->host->i_mode))
439c2a7d2a1SDan Williams 		return;
440c2a7d2a1SDan Williams 
441fda490d3SMatthew Wilcox 	rcu_read_lock();
442fda490d3SMatthew Wilcox 	entry = xas_load(&xas);
443fda490d3SMatthew Wilcox 	rcu_read_unlock();
444fda490d3SMatthew Wilcox 	entry = dax_make_entry(page_to_pfn_t(page), dax_is_pmd_entry(entry));
445fda490d3SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
446c2a7d2a1SDan Williams }
447c2a7d2a1SDan Williams 
448ac401cc7SJan Kara /*
449a77d19f4SMatthew Wilcox  * Find page cache entry at given index. If it is a DAX entry, return it
450a77d19f4SMatthew Wilcox  * with the entry locked. If the page cache doesn't contain an entry at
451a77d19f4SMatthew Wilcox  * that index, add a locked empty entry.
452ac401cc7SJan Kara  *
4533159f943SMatthew Wilcox  * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
454b15cd800SMatthew Wilcox  * either return that locked entry or will return VM_FAULT_FALLBACK.
455b15cd800SMatthew Wilcox  * This will happen if there are any PTE entries within the PMD range
456b15cd800SMatthew Wilcox  * that we are requesting.
457642261acSRoss Zwisler  *
458b15cd800SMatthew Wilcox  * We always favor PTE entries over PMD entries. There isn't a flow where we
459b15cd800SMatthew Wilcox  * evict PTE entries in order to 'upgrade' them to a PMD entry.  A PMD
460b15cd800SMatthew Wilcox  * insertion will fail if it finds any PTE entries already in the tree, and a
461b15cd800SMatthew Wilcox  * PTE insertion will cause an existing PMD entry to be unmapped and
462b15cd800SMatthew Wilcox  * downgraded to PTE entries.  This happens for both PMD zero pages as
463b15cd800SMatthew Wilcox  * well as PMD empty entries.
464642261acSRoss Zwisler  *
465b15cd800SMatthew Wilcox  * The exception to this downgrade path is for PMD entries that have
466b15cd800SMatthew Wilcox  * real storage backing them.  We will leave these real PMD entries in
467b15cd800SMatthew Wilcox  * the tree, and PTE writes will simply dirty the entire PMD entry.
468642261acSRoss Zwisler  *
469ac401cc7SJan Kara  * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
470ac401cc7SJan Kara  * persistent memory the benefit is doubtful. We can add that later if we can
471ac401cc7SJan Kara  * show it helps.
472b15cd800SMatthew Wilcox  *
473b15cd800SMatthew Wilcox  * On error, this function does not return an ERR_PTR.  Instead it returns
474b15cd800SMatthew Wilcox  * a VM_FAULT code, encoded as an xarray internal entry.  The ERR_PTR values
475b15cd800SMatthew Wilcox  * overlap with xarray value entries.
476ac401cc7SJan Kara  */
477b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas,
478b15cd800SMatthew Wilcox 		struct address_space *mapping, unsigned long size_flag)
479ac401cc7SJan Kara {
480b15cd800SMatthew Wilcox 	unsigned long index = xas->xa_index;
481b15cd800SMatthew Wilcox 	bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
482b15cd800SMatthew Wilcox 	void *entry;
483ac401cc7SJan Kara 
484b15cd800SMatthew Wilcox retry:
485b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
486b15cd800SMatthew Wilcox 	entry = get_unlocked_entry(xas);
487642261acSRoss Zwisler 
488b15cd800SMatthew Wilcox 	if (entry) {
4890e40de03SMatthew Wilcox 		if (!xa_is_value(entry)) {
490b15cd800SMatthew Wilcox 			xas_set_err(xas, EIO);
49191d25ba8SRoss Zwisler 			goto out_unlock;
49291d25ba8SRoss Zwisler 		}
49391d25ba8SRoss Zwisler 
4943159f943SMatthew Wilcox 		if (size_flag & DAX_PMD) {
49591d25ba8SRoss Zwisler 			if (dax_is_pte_entry(entry)) {
496b15cd800SMatthew Wilcox 				put_unlocked_entry(xas, entry);
497b15cd800SMatthew Wilcox 				goto fallback;
498642261acSRoss Zwisler 			}
499642261acSRoss Zwisler 		} else { /* trying to grab a PTE entry */
50091d25ba8SRoss Zwisler 			if (dax_is_pmd_entry(entry) &&
501642261acSRoss Zwisler 			    (dax_is_zero_entry(entry) ||
502642261acSRoss Zwisler 			     dax_is_empty_entry(entry))) {
503642261acSRoss Zwisler 				pmd_downgrade = true;
504642261acSRoss Zwisler 			}
505642261acSRoss Zwisler 		}
506642261acSRoss Zwisler 	}
507642261acSRoss Zwisler 
508642261acSRoss Zwisler 	if (pmd_downgrade) {
509642261acSRoss Zwisler 		/*
510642261acSRoss Zwisler 		 * Make sure 'entry' remains valid while we drop
511b93b0163SMatthew Wilcox 		 * the i_pages lock.
512642261acSRoss Zwisler 		 */
513b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
514642261acSRoss Zwisler 
515642261acSRoss Zwisler 		/*
516642261acSRoss Zwisler 		 * Besides huge zero pages the only other thing that gets
517642261acSRoss Zwisler 		 * downgraded are empty entries which don't need to be
518642261acSRoss Zwisler 		 * unmapped.
519642261acSRoss Zwisler 		 */
520b15cd800SMatthew Wilcox 		if (dax_is_zero_entry(entry)) {
521b15cd800SMatthew Wilcox 			xas_unlock_irq(xas);
522b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping,
523b15cd800SMatthew Wilcox 					xas->xa_index & ~PG_PMD_COLOUR,
524977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
525b15cd800SMatthew Wilcox 			xas_reset(xas);
526b15cd800SMatthew Wilcox 			xas_lock_irq(xas);
527e11f8b7bSRoss Zwisler 		}
528e11f8b7bSRoss Zwisler 
529d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
530b15cd800SMatthew Wilcox 		xas_store(xas, NULL);	/* undo the PMD join */
531b15cd800SMatthew Wilcox 		dax_wake_entry(xas, entry, true);
532642261acSRoss Zwisler 		mapping->nrexceptional--;
533b15cd800SMatthew Wilcox 		entry = NULL;
534b15cd800SMatthew Wilcox 		xas_set(xas, index);
535642261acSRoss Zwisler 	}
536642261acSRoss Zwisler 
537b15cd800SMatthew Wilcox 	if (entry) {
538b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
539b15cd800SMatthew Wilcox 	} else {
540b15cd800SMatthew Wilcox 		entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
541b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
542b15cd800SMatthew Wilcox 		if (xas_error(xas))
543b15cd800SMatthew Wilcox 			goto out_unlock;
544ac401cc7SJan Kara 		mapping->nrexceptional++;
545ac401cc7SJan Kara 	}
546b15cd800SMatthew Wilcox 
547642261acSRoss Zwisler out_unlock:
548b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
549b15cd800SMatthew Wilcox 	if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
550b15cd800SMatthew Wilcox 		goto retry;
551b15cd800SMatthew Wilcox 	if (xas->xa_node == XA_ERROR(-ENOMEM))
552b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_OOM);
553b15cd800SMatthew Wilcox 	if (xas_error(xas))
554b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_SIGBUS);
555e3ad61c6SRoss Zwisler 	return entry;
556b15cd800SMatthew Wilcox fallback:
557b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
558b15cd800SMatthew Wilcox 	return xa_mk_internal(VM_FAULT_FALLBACK);
559ac401cc7SJan Kara }
560ac401cc7SJan Kara 
5615fac7408SDan Williams /**
5625fac7408SDan Williams  * dax_layout_busy_page - find first pinned page in @mapping
5635fac7408SDan Williams  * @mapping: address space to scan for a page with ref count > 1
5645fac7408SDan Williams  *
5655fac7408SDan Williams  * DAX requires ZONE_DEVICE mapped pages. These pages are never
5665fac7408SDan Williams  * 'onlined' to the page allocator so they are considered idle when
5675fac7408SDan Williams  * page->count == 1. A filesystem uses this interface to determine if
5685fac7408SDan Williams  * any page in the mapping is busy, i.e. for DMA, or other
5695fac7408SDan Williams  * get_user_pages() usages.
5705fac7408SDan Williams  *
5715fac7408SDan Williams  * It is expected that the filesystem is holding locks to block the
5725fac7408SDan Williams  * establishment of new mappings in this address_space. I.e. it expects
5735fac7408SDan Williams  * to be able to run unmap_mapping_range() and subsequently not race
5745fac7408SDan Williams  * mapping_mapped() becoming true.
5755fac7408SDan Williams  */
5765fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping)
5775fac7408SDan Williams {
578084a8990SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, 0);
579084a8990SMatthew Wilcox 	void *entry;
580084a8990SMatthew Wilcox 	unsigned int scanned = 0;
5815fac7408SDan Williams 	struct page *page = NULL;
5825fac7408SDan Williams 
5835fac7408SDan Williams 	/*
5845fac7408SDan Williams 	 * In the 'limited' case get_user_pages() for dax is disabled.
5855fac7408SDan Williams 	 */
5865fac7408SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
5875fac7408SDan Williams 		return NULL;
5885fac7408SDan Williams 
5895fac7408SDan Williams 	if (!dax_mapping(mapping) || !mapping_mapped(mapping))
5905fac7408SDan Williams 		return NULL;
5915fac7408SDan Williams 
5925fac7408SDan Williams 	/*
5935fac7408SDan Williams 	 * If we race get_user_pages_fast() here either we'll see the
594084a8990SMatthew Wilcox 	 * elevated page count in the iteration and wait, or
5955fac7408SDan Williams 	 * get_user_pages_fast() will see that the page it took a reference
5965fac7408SDan Williams 	 * against is no longer mapped in the page tables and bail to the
5975fac7408SDan Williams 	 * get_user_pages() slow path.  The slow path is protected by
5985fac7408SDan Williams 	 * pte_lock() and pmd_lock(). New references are not taken without
5995fac7408SDan Williams 	 * holding those locks, and unmap_mapping_range() will not zero the
6005fac7408SDan Williams 	 * pte or pmd without holding the respective lock, so we are
6015fac7408SDan Williams 	 * guaranteed to either see new references or prevent new
6025fac7408SDan Williams 	 * references from being established.
6035fac7408SDan Williams 	 */
6045fac7408SDan Williams 	unmap_mapping_range(mapping, 0, 0, 1);
6055fac7408SDan Williams 
606084a8990SMatthew Wilcox 	xas_lock_irq(&xas);
607084a8990SMatthew Wilcox 	xas_for_each(&xas, entry, ULONG_MAX) {
608084a8990SMatthew Wilcox 		if (WARN_ON_ONCE(!xa_is_value(entry)))
6095fac7408SDan Williams 			continue;
610084a8990SMatthew Wilcox 		if (unlikely(dax_is_locked(entry)))
611084a8990SMatthew Wilcox 			entry = get_unlocked_entry(&xas);
6125fac7408SDan Williams 		if (entry)
6135fac7408SDan Williams 			page = dax_busy_page(entry);
614084a8990SMatthew Wilcox 		put_unlocked_entry(&xas, entry);
6155fac7408SDan Williams 		if (page)
6165fac7408SDan Williams 			break;
617084a8990SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
618084a8990SMatthew Wilcox 			continue;
619cdbf8897SRoss Zwisler 
620084a8990SMatthew Wilcox 		xas_pause(&xas);
621084a8990SMatthew Wilcox 		xas_unlock_irq(&xas);
622084a8990SMatthew Wilcox 		cond_resched();
623084a8990SMatthew Wilcox 		xas_lock_irq(&xas);
6245fac7408SDan Williams 	}
625084a8990SMatthew Wilcox 	xas_unlock_irq(&xas);
6265fac7408SDan Williams 	return page;
6275fac7408SDan Williams }
6285fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page);
6295fac7408SDan Williams 
630a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping,
631c6dcf52cSJan Kara 					  pgoff_t index, bool trunc)
632c6dcf52cSJan Kara {
63307f2d89cSMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
634c6dcf52cSJan Kara 	int ret = 0;
635c6dcf52cSJan Kara 	void *entry;
636c6dcf52cSJan Kara 
63707f2d89cSMatthew Wilcox 	xas_lock_irq(&xas);
63807f2d89cSMatthew Wilcox 	entry = get_unlocked_entry(&xas);
6393159f943SMatthew Wilcox 	if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
640c6dcf52cSJan Kara 		goto out;
641c6dcf52cSJan Kara 	if (!trunc &&
64207f2d89cSMatthew Wilcox 	    (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
64307f2d89cSMatthew Wilcox 	     xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
644c6dcf52cSJan Kara 		goto out;
645d2c997c0SDan Williams 	dax_disassociate_entry(entry, mapping, trunc);
64607f2d89cSMatthew Wilcox 	xas_store(&xas, NULL);
647c6dcf52cSJan Kara 	mapping->nrexceptional--;
648c6dcf52cSJan Kara 	ret = 1;
649c6dcf52cSJan Kara out:
65007f2d89cSMatthew Wilcox 	put_unlocked_entry(&xas, entry);
65107f2d89cSMatthew Wilcox 	xas_unlock_irq(&xas);
652c6dcf52cSJan Kara 	return ret;
653c6dcf52cSJan Kara }
65407f2d89cSMatthew Wilcox 
655ac401cc7SJan Kara /*
6563159f943SMatthew Wilcox  * Delete DAX entry at @index from @mapping.  Wait for it
6573159f943SMatthew Wilcox  * to be unlocked before deleting it.
658ac401cc7SJan Kara  */
659ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
660ac401cc7SJan Kara {
661a77d19f4SMatthew Wilcox 	int ret = __dax_invalidate_entry(mapping, index, true);
662ac401cc7SJan Kara 
663ac401cc7SJan Kara 	/*
664ac401cc7SJan Kara 	 * This gets called from truncate / punch_hole path. As such, the caller
665ac401cc7SJan Kara 	 * must hold locks protecting against concurrent modifications of the
666a77d19f4SMatthew Wilcox 	 * page cache (usually fs-private i_mmap_sem for writing). Since the
6673159f943SMatthew Wilcox 	 * caller has seen a DAX entry for this index, we better find it
668ac401cc7SJan Kara 	 * at that index as well...
669ac401cc7SJan Kara 	 */
670c6dcf52cSJan Kara 	WARN_ON_ONCE(!ret);
671c6dcf52cSJan Kara 	return ret;
672ac401cc7SJan Kara }
673ac401cc7SJan Kara 
674c6dcf52cSJan Kara /*
6753159f943SMatthew Wilcox  * Invalidate DAX entry if it is clean.
676c6dcf52cSJan Kara  */
677c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
678c6dcf52cSJan Kara 				      pgoff_t index)
679c6dcf52cSJan Kara {
680a77d19f4SMatthew Wilcox 	return __dax_invalidate_entry(mapping, index, false);
681ac401cc7SJan Kara }
682ac401cc7SJan Kara 
683cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
684cccbce67SDan Williams 		sector_t sector, size_t size, struct page *to,
685cccbce67SDan Williams 		unsigned long vaddr)
686f7ca90b1SMatthew Wilcox {
687cccbce67SDan Williams 	void *vto, *kaddr;
688cccbce67SDan Williams 	pgoff_t pgoff;
689cccbce67SDan Williams 	long rc;
690cccbce67SDan Williams 	int id;
691e2e05394SRoss Zwisler 
692cccbce67SDan Williams 	rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
693cccbce67SDan Williams 	if (rc)
694cccbce67SDan Williams 		return rc;
695cccbce67SDan Williams 
696cccbce67SDan Williams 	id = dax_read_lock();
69786ed913bSHuaisheng Ye 	rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
698cccbce67SDan Williams 	if (rc < 0) {
699cccbce67SDan Williams 		dax_read_unlock(id);
700cccbce67SDan Williams 		return rc;
701cccbce67SDan Williams 	}
702f7ca90b1SMatthew Wilcox 	vto = kmap_atomic(to);
703cccbce67SDan Williams 	copy_user_page(vto, (void __force *)kaddr, vaddr, to);
704f7ca90b1SMatthew Wilcox 	kunmap_atomic(vto);
705cccbce67SDan Williams 	dax_read_unlock(id);
706f7ca90b1SMatthew Wilcox 	return 0;
707f7ca90b1SMatthew Wilcox }
708f7ca90b1SMatthew Wilcox 
709642261acSRoss Zwisler /*
710642261acSRoss Zwisler  * By this point grab_mapping_entry() has ensured that we have a locked entry
711642261acSRoss Zwisler  * of the appropriate size so we don't have to worry about downgrading PMDs to
712642261acSRoss Zwisler  * PTEs.  If we happen to be trying to insert a PTE and there is a PMD
713642261acSRoss Zwisler  * already in the tree, we will skip the insertion and just dirty the PMD as
714642261acSRoss Zwisler  * appropriate.
715642261acSRoss Zwisler  */
716b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas,
717b15cd800SMatthew Wilcox 		struct address_space *mapping, struct vm_fault *vmf,
718b15cd800SMatthew Wilcox 		void *entry, pfn_t pfn, unsigned long flags, bool dirty)
7199973c98eSRoss Zwisler {
720b15cd800SMatthew Wilcox 	void *new_entry = dax_make_entry(pfn, flags);
7219973c98eSRoss Zwisler 
722f5b7b748SJan Kara 	if (dirty)
7239973c98eSRoss Zwisler 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
7249973c98eSRoss Zwisler 
7253159f943SMatthew Wilcox 	if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
726b15cd800SMatthew Wilcox 		unsigned long index = xas->xa_index;
72791d25ba8SRoss Zwisler 		/* we are replacing a zero page with block mapping */
72891d25ba8SRoss Zwisler 		if (dax_is_pmd_entry(entry))
729977fbdcdSMatthew Wilcox 			unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
730977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
73191d25ba8SRoss Zwisler 		else /* pte entry */
732b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping, index, 1, false);
733ac401cc7SJan Kara 	}
7349973c98eSRoss Zwisler 
735b15cd800SMatthew Wilcox 	xas_reset(xas);
736b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
737d2c997c0SDan Williams 	if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
738d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
73973449dafSDan Williams 		dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
740d2c997c0SDan Williams 	}
741642261acSRoss Zwisler 
74291d25ba8SRoss Zwisler 	if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
743642261acSRoss Zwisler 		/*
744a77d19f4SMatthew Wilcox 		 * Only swap our new entry into the page cache if the current
745642261acSRoss Zwisler 		 * entry is a zero page or an empty entry.  If a normal PTE or
746a77d19f4SMatthew Wilcox 		 * PMD entry is already in the cache, we leave it alone.  This
747642261acSRoss Zwisler 		 * means that if we are trying to insert a PTE and the
748642261acSRoss Zwisler 		 * existing entry is a PMD, we will just leave the PMD in the
749642261acSRoss Zwisler 		 * tree and dirty it if necessary.
750642261acSRoss Zwisler 		 */
751b15cd800SMatthew Wilcox 		void *old = dax_lock_entry(xas, new_entry);
752b15cd800SMatthew Wilcox 		WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
753b15cd800SMatthew Wilcox 					DAX_LOCKED));
75491d25ba8SRoss Zwisler 		entry = new_entry;
755b15cd800SMatthew Wilcox 	} else {
756b15cd800SMatthew Wilcox 		xas_load(xas);	/* Walk the xa_state */
757ac401cc7SJan Kara 	}
75891d25ba8SRoss Zwisler 
759f5b7b748SJan Kara 	if (dirty)
760b15cd800SMatthew Wilcox 		xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
76191d25ba8SRoss Zwisler 
762b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
76391d25ba8SRoss Zwisler 	return entry;
7649973c98eSRoss Zwisler }
7659973c98eSRoss Zwisler 
766a77d19f4SMatthew Wilcox static inline
767a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
7684b4bb46dSJan Kara {
7694b4bb46dSJan Kara 	unsigned long address;
7704b4bb46dSJan Kara 
7714b4bb46dSJan Kara 	address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
7724b4bb46dSJan Kara 	VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
7734b4bb46dSJan Kara 	return address;
7744b4bb46dSJan Kara }
7754b4bb46dSJan Kara 
7764b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */
777a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
778a77d19f4SMatthew Wilcox 		unsigned long pfn)
7794b4bb46dSJan Kara {
7804b4bb46dSJan Kara 	struct vm_area_struct *vma;
781f729c8c9SRoss Zwisler 	pte_t pte, *ptep = NULL;
782f729c8c9SRoss Zwisler 	pmd_t *pmdp = NULL;
7834b4bb46dSJan Kara 	spinlock_t *ptl;
7844b4bb46dSJan Kara 
7854b4bb46dSJan Kara 	i_mmap_lock_read(mapping);
7864b4bb46dSJan Kara 	vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
787a4d1a885SJérôme Glisse 		unsigned long address, start, end;
7884b4bb46dSJan Kara 
7894b4bb46dSJan Kara 		cond_resched();
7904b4bb46dSJan Kara 
7914b4bb46dSJan Kara 		if (!(vma->vm_flags & VM_SHARED))
7924b4bb46dSJan Kara 			continue;
7934b4bb46dSJan Kara 
7944b4bb46dSJan Kara 		address = pgoff_address(index, vma);
795a4d1a885SJérôme Glisse 
796a4d1a885SJérôme Glisse 		/*
797a4d1a885SJérôme Glisse 		 * Note because we provide start/end to follow_pte_pmd it will
798a4d1a885SJérôme Glisse 		 * call mmu_notifier_invalidate_range_start() on our behalf
799a4d1a885SJérôme Glisse 		 * before taking any lock.
800a4d1a885SJérôme Glisse 		 */
801a4d1a885SJérôme Glisse 		if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
8024b4bb46dSJan Kara 			continue;
803f729c8c9SRoss Zwisler 
8040f10851eSJérôme Glisse 		/*
8050f10851eSJérôme Glisse 		 * No need to call mmu_notifier_invalidate_range() as we are
8060f10851eSJérôme Glisse 		 * downgrading page table protection not changing it to point
8070f10851eSJérôme Glisse 		 * to a new page.
8080f10851eSJérôme Glisse 		 *
809ad56b738SMike Rapoport 		 * See Documentation/vm/mmu_notifier.rst
8100f10851eSJérôme Glisse 		 */
811f729c8c9SRoss Zwisler 		if (pmdp) {
812f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD
813f729c8c9SRoss Zwisler 			pmd_t pmd;
814f729c8c9SRoss Zwisler 
815f729c8c9SRoss Zwisler 			if (pfn != pmd_pfn(*pmdp))
816f729c8c9SRoss Zwisler 				goto unlock_pmd;
817f6f37321SLinus Torvalds 			if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
818f729c8c9SRoss Zwisler 				goto unlock_pmd;
819f729c8c9SRoss Zwisler 
820f729c8c9SRoss Zwisler 			flush_cache_page(vma, address, pfn);
821f729c8c9SRoss Zwisler 			pmd = pmdp_huge_clear_flush(vma, address, pmdp);
822f729c8c9SRoss Zwisler 			pmd = pmd_wrprotect(pmd);
823f729c8c9SRoss Zwisler 			pmd = pmd_mkclean(pmd);
824f729c8c9SRoss Zwisler 			set_pmd_at(vma->vm_mm, address, pmdp, pmd);
825f729c8c9SRoss Zwisler unlock_pmd:
826f729c8c9SRoss Zwisler #endif
827ee190ca6SJan H. Schönherr 			spin_unlock(ptl);
828f729c8c9SRoss Zwisler 		} else {
8294b4bb46dSJan Kara 			if (pfn != pte_pfn(*ptep))
830f729c8c9SRoss Zwisler 				goto unlock_pte;
8314b4bb46dSJan Kara 			if (!pte_dirty(*ptep) && !pte_write(*ptep))
832f729c8c9SRoss Zwisler 				goto unlock_pte;
8334b4bb46dSJan Kara 
8344b4bb46dSJan Kara 			flush_cache_page(vma, address, pfn);
8354b4bb46dSJan Kara 			pte = ptep_clear_flush(vma, address, ptep);
8364b4bb46dSJan Kara 			pte = pte_wrprotect(pte);
8374b4bb46dSJan Kara 			pte = pte_mkclean(pte);
8384b4bb46dSJan Kara 			set_pte_at(vma->vm_mm, address, ptep, pte);
839f729c8c9SRoss Zwisler unlock_pte:
8404b4bb46dSJan Kara 			pte_unmap_unlock(ptep, ptl);
841f729c8c9SRoss Zwisler 		}
8424b4bb46dSJan Kara 
843a4d1a885SJérôme Glisse 		mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
8444b4bb46dSJan Kara 	}
8454b4bb46dSJan Kara 	i_mmap_unlock_read(mapping);
8464b4bb46dSJan Kara }
8474b4bb46dSJan Kara 
8489fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
8499fc747f6SMatthew Wilcox 		struct address_space *mapping, void *entry)
8509973c98eSRoss Zwisler {
8513fe0791cSDan Williams 	unsigned long pfn;
8523fe0791cSDan Williams 	long ret = 0;
853cccbce67SDan Williams 	size_t size;
8549973c98eSRoss Zwisler 
8559973c98eSRoss Zwisler 	/*
856a6abc2c0SJan Kara 	 * A page got tagged dirty in DAX mapping? Something is seriously
857a6abc2c0SJan Kara 	 * wrong.
8589973c98eSRoss Zwisler 	 */
8593159f943SMatthew Wilcox 	if (WARN_ON(!xa_is_value(entry)))
860a6abc2c0SJan Kara 		return -EIO;
8619973c98eSRoss Zwisler 
8629fc747f6SMatthew Wilcox 	if (unlikely(dax_is_locked(entry))) {
8639fc747f6SMatthew Wilcox 		void *old_entry = entry;
8649fc747f6SMatthew Wilcox 
8659fc747f6SMatthew Wilcox 		entry = get_unlocked_entry(xas);
8669fc747f6SMatthew Wilcox 
867a6abc2c0SJan Kara 		/* Entry got punched out / reallocated? */
8689fc747f6SMatthew Wilcox 		if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
869a6abc2c0SJan Kara 			goto put_unlocked;
870a6abc2c0SJan Kara 		/*
8719fc747f6SMatthew Wilcox 		 * Entry got reallocated elsewhere? No need to writeback.
8729fc747f6SMatthew Wilcox 		 * We have to compare pfns as we must not bail out due to
8739fc747f6SMatthew Wilcox 		 * difference in lockbit or entry type.
874a6abc2c0SJan Kara 		 */
8759fc747f6SMatthew Wilcox 		if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
876a6abc2c0SJan Kara 			goto put_unlocked;
877642261acSRoss Zwisler 		if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
878642261acSRoss Zwisler 					dax_is_zero_entry(entry))) {
8799973c98eSRoss Zwisler 			ret = -EIO;
880a6abc2c0SJan Kara 			goto put_unlocked;
8819973c98eSRoss Zwisler 		}
8829973c98eSRoss Zwisler 
8839fc747f6SMatthew Wilcox 		/* Another fsync thread may have already done this entry */
8849fc747f6SMatthew Wilcox 		if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
885a6abc2c0SJan Kara 			goto put_unlocked;
8869fc747f6SMatthew Wilcox 	}
8879fc747f6SMatthew Wilcox 
888a6abc2c0SJan Kara 	/* Lock the entry to serialize with page faults */
8899fc747f6SMatthew Wilcox 	dax_lock_entry(xas, entry);
8909fc747f6SMatthew Wilcox 
891a6abc2c0SJan Kara 	/*
892a6abc2c0SJan Kara 	 * We can clear the tag now but we have to be careful so that concurrent
893a6abc2c0SJan Kara 	 * dax_writeback_one() calls for the same index cannot finish before we
894a6abc2c0SJan Kara 	 * actually flush the caches. This is achieved as the calls will look
895b93b0163SMatthew Wilcox 	 * at the entry only under the i_pages lock and once they do that
896b93b0163SMatthew Wilcox 	 * they will see the entry locked and wait for it to unlock.
897a6abc2c0SJan Kara 	 */
8989fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
8999fc747f6SMatthew Wilcox 	xas_unlock_irq(xas);
900a6abc2c0SJan Kara 
901642261acSRoss Zwisler 	/*
902642261acSRoss Zwisler 	 * Even if dax_writeback_mapping_range() was given a wbc->range_start
903642261acSRoss Zwisler 	 * in the middle of a PMD, the 'index' we are given will be aligned to
9043fe0791cSDan Williams 	 * the start index of the PMD, as will the pfn we pull from 'entry'.
9053fe0791cSDan Williams 	 * This allows us to flush for PMD_SIZE and not have to worry about
9063fe0791cSDan Williams 	 * partial PMD writebacks.
907642261acSRoss Zwisler 	 */
908a77d19f4SMatthew Wilcox 	pfn = dax_to_pfn(entry);
909a77d19f4SMatthew Wilcox 	size = PAGE_SIZE << dax_entry_order(entry);
910cccbce67SDan Williams 
9119fc747f6SMatthew Wilcox 	dax_entry_mkclean(mapping, xas->xa_index, pfn);
9123fe0791cSDan Williams 	dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
9134b4bb46dSJan Kara 	/*
9144b4bb46dSJan Kara 	 * After we have flushed the cache, we can clear the dirty tag. There
9154b4bb46dSJan Kara 	 * cannot be new dirty data in the pfn after the flush has completed as
9164b4bb46dSJan Kara 	 * the pfn mappings are writeprotected and fault waits for mapping
9174b4bb46dSJan Kara 	 * entry lock.
9184b4bb46dSJan Kara 	 */
9199fc747f6SMatthew Wilcox 	xas_reset(xas);
9209fc747f6SMatthew Wilcox 	xas_lock_irq(xas);
9219fc747f6SMatthew Wilcox 	xas_store(xas, entry);
9229fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
9239fc747f6SMatthew Wilcox 	dax_wake_entry(xas, entry, false);
9249fc747f6SMatthew Wilcox 
9259fc747f6SMatthew Wilcox 	trace_dax_writeback_one(mapping->host, xas->xa_index,
9269fc747f6SMatthew Wilcox 			size >> PAGE_SHIFT);
9279973c98eSRoss Zwisler 	return ret;
9289973c98eSRoss Zwisler 
929a6abc2c0SJan Kara  put_unlocked:
9309fc747f6SMatthew Wilcox 	put_unlocked_entry(xas, entry);
9319973c98eSRoss Zwisler 	return ret;
9329973c98eSRoss Zwisler }
9339973c98eSRoss Zwisler 
9349973c98eSRoss Zwisler /*
9359973c98eSRoss Zwisler  * Flush the mapping to the persistent domain within the byte range of [start,
9369973c98eSRoss Zwisler  * end]. This is required by data integrity operations to ensure file data is
9379973c98eSRoss Zwisler  * on persistent storage prior to completion of the operation.
9389973c98eSRoss Zwisler  */
9397f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping,
9407f6d5b52SRoss Zwisler 		struct block_device *bdev, struct writeback_control *wbc)
9419973c98eSRoss Zwisler {
9429fc747f6SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
9439973c98eSRoss Zwisler 	struct inode *inode = mapping->host;
9449fc747f6SMatthew Wilcox 	pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
945cccbce67SDan Williams 	struct dax_device *dax_dev;
9469fc747f6SMatthew Wilcox 	void *entry;
9479fc747f6SMatthew Wilcox 	int ret = 0;
9489fc747f6SMatthew Wilcox 	unsigned int scanned = 0;
9499973c98eSRoss Zwisler 
9509973c98eSRoss Zwisler 	if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
9519973c98eSRoss Zwisler 		return -EIO;
9529973c98eSRoss Zwisler 
9537f6d5b52SRoss Zwisler 	if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
9547f6d5b52SRoss Zwisler 		return 0;
9557f6d5b52SRoss Zwisler 
956cccbce67SDan Williams 	dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
957cccbce67SDan Williams 	if (!dax_dev)
958cccbce67SDan Williams 		return -EIO;
959cccbce67SDan Williams 
9609fc747f6SMatthew Wilcox 	trace_dax_writeback_range(inode, xas.xa_index, end_index);
9619973c98eSRoss Zwisler 
9629fc747f6SMatthew Wilcox 	tag_pages_for_writeback(mapping, xas.xa_index, end_index);
963d14a3f48SRoss Zwisler 
9649fc747f6SMatthew Wilcox 	xas_lock_irq(&xas);
9659fc747f6SMatthew Wilcox 	xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
9669fc747f6SMatthew Wilcox 		ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
967819ec6b9SJeff Layton 		if (ret < 0) {
968819ec6b9SJeff Layton 			mapping_set_error(mapping, ret);
9699fc747f6SMatthew Wilcox 			break;
970d14a3f48SRoss Zwisler 		}
9719fc747f6SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
9729fc747f6SMatthew Wilcox 			continue;
9739fc747f6SMatthew Wilcox 
9749fc747f6SMatthew Wilcox 		xas_pause(&xas);
9759fc747f6SMatthew Wilcox 		xas_unlock_irq(&xas);
9769fc747f6SMatthew Wilcox 		cond_resched();
9779fc747f6SMatthew Wilcox 		xas_lock_irq(&xas);
978d14a3f48SRoss Zwisler 	}
9799fc747f6SMatthew Wilcox 	xas_unlock_irq(&xas);
980cccbce67SDan Williams 	put_dax(dax_dev);
9819fc747f6SMatthew Wilcox 	trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
9829fc747f6SMatthew Wilcox 	return ret;
9839973c98eSRoss Zwisler }
9849973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
9859973c98eSRoss Zwisler 
98631a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
987f7ca90b1SMatthew Wilcox {
988a3841f94SLinus Torvalds 	return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
98931a6f1a6SJan Kara }
990f7ca90b1SMatthew Wilcox 
9915e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
9925e161e40SJan Kara 			 pfn_t *pfnp)
9935e161e40SJan Kara {
9945e161e40SJan Kara 	const sector_t sector = dax_iomap_sector(iomap, pos);
9955e161e40SJan Kara 	pgoff_t pgoff;
9965e161e40SJan Kara 	int id, rc;
9975e161e40SJan Kara 	long length;
9985e161e40SJan Kara 
9995e161e40SJan Kara 	rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
1000cccbce67SDan Williams 	if (rc)
1001cccbce67SDan Williams 		return rc;
1002cccbce67SDan Williams 	id = dax_read_lock();
10035e161e40SJan Kara 	length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
100486ed913bSHuaisheng Ye 				   NULL, pfnp);
10055e161e40SJan Kara 	if (length < 0) {
10065e161e40SJan Kara 		rc = length;
10075e161e40SJan Kara 		goto out;
10085e161e40SJan Kara 	}
10095e161e40SJan Kara 	rc = -EINVAL;
10105e161e40SJan Kara 	if (PFN_PHYS(length) < size)
10115e161e40SJan Kara 		goto out;
10125e161e40SJan Kara 	if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
10135e161e40SJan Kara 		goto out;
10145e161e40SJan Kara 	/* For larger pages we need devmap */
10155e161e40SJan Kara 	if (length > 1 && !pfn_t_devmap(*pfnp))
10165e161e40SJan Kara 		goto out;
10175e161e40SJan Kara 	rc = 0;
10185e161e40SJan Kara out:
1019cccbce67SDan Williams 	dax_read_unlock(id);
1020cccbce67SDan Williams 	return rc;
1021cccbce67SDan Williams }
1022f7ca90b1SMatthew Wilcox 
10232f89dc12SJan Kara /*
102491d25ba8SRoss Zwisler  * The user has performed a load from a hole in the file.  Allocating a new
102591d25ba8SRoss Zwisler  * page in the file would cause excessive storage usage for workloads with
102691d25ba8SRoss Zwisler  * sparse files.  Instead we insert a read-only mapping of the 4k zero page.
102791d25ba8SRoss Zwisler  * If this page is ever written to we will re-fault and change the mapping to
102891d25ba8SRoss Zwisler  * point to real DAX storage instead.
10292f89dc12SJan Kara  */
1030b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas,
1031b15cd800SMatthew Wilcox 		struct address_space *mapping, void **entry,
1032e30331ffSRoss Zwisler 		struct vm_fault *vmf)
1033e30331ffSRoss Zwisler {
1034e30331ffSRoss Zwisler 	struct inode *inode = mapping->host;
103591d25ba8SRoss Zwisler 	unsigned long vaddr = vmf->address;
1036b90ca5ccSMatthew Wilcox 	pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1037b90ca5ccSMatthew Wilcox 	vm_fault_t ret;
1038e30331ffSRoss Zwisler 
1039b15cd800SMatthew Wilcox 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
10403159f943SMatthew Wilcox 			DAX_ZERO_PAGE, false);
10413159f943SMatthew Wilcox 
1042ab77dab4SSouptick Joarder 	ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
1043e30331ffSRoss Zwisler 	trace_dax_load_hole(inode, vmf, ret);
1044e30331ffSRoss Zwisler 	return ret;
1045e30331ffSRoss Zwisler }
1046e30331ffSRoss Zwisler 
10474b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev,
10484b0228faSVishal Verma 				 unsigned int offset, unsigned int length)
10494b0228faSVishal Verma {
10504b0228faSVishal Verma 	unsigned short sector_size = bdev_logical_block_size(bdev);
10514b0228faSVishal Verma 
10524b0228faSVishal Verma 	if (!IS_ALIGNED(offset, sector_size))
10534b0228faSVishal Verma 		return false;
10544b0228faSVishal Verma 	if (!IS_ALIGNED(length, sector_size))
10554b0228faSVishal Verma 		return false;
10564b0228faSVishal Verma 
10574b0228faSVishal Verma 	return true;
10584b0228faSVishal Verma }
10594b0228faSVishal Verma 
1060cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev,
1061cccbce67SDan Williams 		struct dax_device *dax_dev, sector_t sector,
1062cccbce67SDan Williams 		unsigned int offset, unsigned int size)
1063679c8bd3SChristoph Hellwig {
1064cccbce67SDan Williams 	if (dax_range_is_aligned(bdev, offset, size)) {
1065cccbce67SDan Williams 		sector_t start_sector = sector + (offset >> 9);
10664b0228faSVishal Verma 
10674b0228faSVishal Verma 		return blkdev_issue_zeroout(bdev, start_sector,
106853ef7d0eSLinus Torvalds 				size >> 9, GFP_NOFS, 0);
10694b0228faSVishal Verma 	} else {
1070cccbce67SDan Williams 		pgoff_t pgoff;
1071cccbce67SDan Williams 		long rc, id;
1072cccbce67SDan Williams 		void *kaddr;
1073cccbce67SDan Williams 
1074e84b83b9SDan Williams 		rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
1075cccbce67SDan Williams 		if (rc)
1076cccbce67SDan Williams 			return rc;
1077cccbce67SDan Williams 
1078cccbce67SDan Williams 		id = dax_read_lock();
107986ed913bSHuaisheng Ye 		rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
1080cccbce67SDan Williams 		if (rc < 0) {
1081cccbce67SDan Williams 			dax_read_unlock(id);
1082cccbce67SDan Williams 			return rc;
1083cccbce67SDan Williams 		}
108481f55870SDan Williams 		memset(kaddr + offset, 0, size);
1085c3ca015fSMikulas Patocka 		dax_flush(dax_dev, kaddr + offset, size);
1086cccbce67SDan Williams 		dax_read_unlock(id);
10874b0228faSVishal Verma 	}
1088679c8bd3SChristoph Hellwig 	return 0;
1089679c8bd3SChristoph Hellwig }
1090679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1091679c8bd3SChristoph Hellwig 
1092a254e568SChristoph Hellwig static loff_t
109311c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1094a254e568SChristoph Hellwig 		struct iomap *iomap)
1095a254e568SChristoph Hellwig {
1096cccbce67SDan Williams 	struct block_device *bdev = iomap->bdev;
1097cccbce67SDan Williams 	struct dax_device *dax_dev = iomap->dax_dev;
1098a254e568SChristoph Hellwig 	struct iov_iter *iter = data;
1099a254e568SChristoph Hellwig 	loff_t end = pos + length, done = 0;
1100a254e568SChristoph Hellwig 	ssize_t ret = 0;
1101a77d4786SDan Williams 	size_t xfer;
1102cccbce67SDan Williams 	int id;
1103a254e568SChristoph Hellwig 
1104a254e568SChristoph Hellwig 	if (iov_iter_rw(iter) == READ) {
1105a254e568SChristoph Hellwig 		end = min(end, i_size_read(inode));
1106a254e568SChristoph Hellwig 		if (pos >= end)
1107a254e568SChristoph Hellwig 			return 0;
1108a254e568SChristoph Hellwig 
1109a254e568SChristoph Hellwig 		if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1110a254e568SChristoph Hellwig 			return iov_iter_zero(min(length, end - pos), iter);
1111a254e568SChristoph Hellwig 	}
1112a254e568SChristoph Hellwig 
1113a254e568SChristoph Hellwig 	if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1114a254e568SChristoph Hellwig 		return -EIO;
1115a254e568SChristoph Hellwig 
1116e3fce68cSJan Kara 	/*
1117e3fce68cSJan Kara 	 * Write can allocate block for an area which has a hole page mapped
1118e3fce68cSJan Kara 	 * into page tables. We have to tear down these mappings so that data
1119e3fce68cSJan Kara 	 * written by write(2) is visible in mmap.
1120e3fce68cSJan Kara 	 */
1121cd656375SJan Kara 	if (iomap->flags & IOMAP_F_NEW) {
1122e3fce68cSJan Kara 		invalidate_inode_pages2_range(inode->i_mapping,
1123e3fce68cSJan Kara 					      pos >> PAGE_SHIFT,
1124e3fce68cSJan Kara 					      (end - 1) >> PAGE_SHIFT);
1125e3fce68cSJan Kara 	}
1126e3fce68cSJan Kara 
1127cccbce67SDan Williams 	id = dax_read_lock();
1128a254e568SChristoph Hellwig 	while (pos < end) {
1129a254e568SChristoph Hellwig 		unsigned offset = pos & (PAGE_SIZE - 1);
1130cccbce67SDan Williams 		const size_t size = ALIGN(length + offset, PAGE_SIZE);
1131cccbce67SDan Williams 		const sector_t sector = dax_iomap_sector(iomap, pos);
1132a254e568SChristoph Hellwig 		ssize_t map_len;
1133cccbce67SDan Williams 		pgoff_t pgoff;
1134cccbce67SDan Williams 		void *kaddr;
1135a254e568SChristoph Hellwig 
1136d1908f52SMichal Hocko 		if (fatal_signal_pending(current)) {
1137d1908f52SMichal Hocko 			ret = -EINTR;
1138d1908f52SMichal Hocko 			break;
1139d1908f52SMichal Hocko 		}
1140d1908f52SMichal Hocko 
1141cccbce67SDan Williams 		ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1142cccbce67SDan Williams 		if (ret)
1143cccbce67SDan Williams 			break;
1144cccbce67SDan Williams 
1145cccbce67SDan Williams 		map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
114686ed913bSHuaisheng Ye 				&kaddr, NULL);
1147a254e568SChristoph Hellwig 		if (map_len < 0) {
1148a254e568SChristoph Hellwig 			ret = map_len;
1149a254e568SChristoph Hellwig 			break;
1150a254e568SChristoph Hellwig 		}
1151a254e568SChristoph Hellwig 
1152cccbce67SDan Williams 		map_len = PFN_PHYS(map_len);
1153cccbce67SDan Williams 		kaddr += offset;
1154a254e568SChristoph Hellwig 		map_len -= offset;
1155a254e568SChristoph Hellwig 		if (map_len > end - pos)
1156a254e568SChristoph Hellwig 			map_len = end - pos;
1157a254e568SChristoph Hellwig 
1158a2e050f5SRoss Zwisler 		/*
1159a2e050f5SRoss Zwisler 		 * The userspace address for the memory copy has already been
1160a2e050f5SRoss Zwisler 		 * validated via access_ok() in either vfs_read() or
1161a2e050f5SRoss Zwisler 		 * vfs_write(), depending on which operation we are doing.
1162a2e050f5SRoss Zwisler 		 */
1163a254e568SChristoph Hellwig 		if (iov_iter_rw(iter) == WRITE)
1164a77d4786SDan Williams 			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1165fec53774SDan Williams 					map_len, iter);
1166a254e568SChristoph Hellwig 		else
1167a77d4786SDan Williams 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
1168b3a9a0c3SDan Williams 					map_len, iter);
1169a254e568SChristoph Hellwig 
1170a77d4786SDan Williams 		pos += xfer;
1171a77d4786SDan Williams 		length -= xfer;
1172a77d4786SDan Williams 		done += xfer;
1173a77d4786SDan Williams 
1174a77d4786SDan Williams 		if (xfer == 0)
1175a77d4786SDan Williams 			ret = -EFAULT;
1176a77d4786SDan Williams 		if (xfer < map_len)
1177a77d4786SDan Williams 			break;
1178a254e568SChristoph Hellwig 	}
1179cccbce67SDan Williams 	dax_read_unlock(id);
1180a254e568SChristoph Hellwig 
1181a254e568SChristoph Hellwig 	return done ? done : ret;
1182a254e568SChristoph Hellwig }
1183a254e568SChristoph Hellwig 
1184a254e568SChristoph Hellwig /**
118511c59c92SRoss Zwisler  * dax_iomap_rw - Perform I/O to a DAX file
1186a254e568SChristoph Hellwig  * @iocb:	The control block for this I/O
1187a254e568SChristoph Hellwig  * @iter:	The addresses to do I/O from or to
1188a254e568SChristoph Hellwig  * @ops:	iomap ops passed from the file system
1189a254e568SChristoph Hellwig  *
1190a254e568SChristoph Hellwig  * This function performs read and write operations to directly mapped
1191a254e568SChristoph Hellwig  * persistent memory.  The callers needs to take care of read/write exclusion
1192a254e568SChristoph Hellwig  * and evicting any page cache pages in the region under I/O.
1193a254e568SChristoph Hellwig  */
1194a254e568SChristoph Hellwig ssize_t
119511c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
11968ff6daa1SChristoph Hellwig 		const struct iomap_ops *ops)
1197a254e568SChristoph Hellwig {
1198a254e568SChristoph Hellwig 	struct address_space *mapping = iocb->ki_filp->f_mapping;
1199a254e568SChristoph Hellwig 	struct inode *inode = mapping->host;
1200a254e568SChristoph Hellwig 	loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1201a254e568SChristoph Hellwig 	unsigned flags = 0;
1202a254e568SChristoph Hellwig 
1203168316dbSChristoph Hellwig 	if (iov_iter_rw(iter) == WRITE) {
1204168316dbSChristoph Hellwig 		lockdep_assert_held_exclusive(&inode->i_rwsem);
1205a254e568SChristoph Hellwig 		flags |= IOMAP_WRITE;
1206168316dbSChristoph Hellwig 	} else {
1207168316dbSChristoph Hellwig 		lockdep_assert_held(&inode->i_rwsem);
1208168316dbSChristoph Hellwig 	}
1209a254e568SChristoph Hellwig 
1210a254e568SChristoph Hellwig 	while (iov_iter_count(iter)) {
1211a254e568SChristoph Hellwig 		ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
121211c59c92SRoss Zwisler 				iter, dax_iomap_actor);
1213a254e568SChristoph Hellwig 		if (ret <= 0)
1214a254e568SChristoph Hellwig 			break;
1215a254e568SChristoph Hellwig 		pos += ret;
1216a254e568SChristoph Hellwig 		done += ret;
1217a254e568SChristoph Hellwig 	}
1218a254e568SChristoph Hellwig 
1219a254e568SChristoph Hellwig 	iocb->ki_pos += done;
1220a254e568SChristoph Hellwig 	return done ? done : ret;
1221a254e568SChristoph Hellwig }
122211c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw);
1223a7d73fe6SChristoph Hellwig 
1224ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error)
12259f141d6eSJan Kara {
12269f141d6eSJan Kara 	if (error == 0)
12279f141d6eSJan Kara 		return VM_FAULT_NOPAGE;
12289f141d6eSJan Kara 	if (error == -ENOMEM)
12299f141d6eSJan Kara 		return VM_FAULT_OOM;
12309f141d6eSJan Kara 	return VM_FAULT_SIGBUS;
12319f141d6eSJan Kara }
12329f141d6eSJan Kara 
1233aaa422c4SDan Williams /*
1234aaa422c4SDan Williams  * MAP_SYNC on a dax mapping guarantees dirty metadata is
1235aaa422c4SDan Williams  * flushed on write-faults (non-cow), but not read-faults.
1236aaa422c4SDan Williams  */
1237aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags,
1238aaa422c4SDan Williams 		struct vm_area_struct *vma, struct iomap *iomap)
1239aaa422c4SDan Williams {
1240aaa422c4SDan Williams 	return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1241aaa422c4SDan Williams 		&& (iomap->flags & IOMAP_F_DIRTY);
1242aaa422c4SDan Williams }
1243aaa422c4SDan Williams 
1244ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
1245c0b24625SJan Kara 			       int *iomap_errp, const struct iomap_ops *ops)
1246a7d73fe6SChristoph Hellwig {
1247a0987ad5SJan Kara 	struct vm_area_struct *vma = vmf->vma;
1248a0987ad5SJan Kara 	struct address_space *mapping = vma->vm_file->f_mapping;
1249b15cd800SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
1250a7d73fe6SChristoph Hellwig 	struct inode *inode = mapping->host;
12511a29d85eSJan Kara 	unsigned long vaddr = vmf->address;
1252a7d73fe6SChristoph Hellwig 	loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1253a7d73fe6SChristoph Hellwig 	struct iomap iomap = { 0 };
12549484ab1bSJan Kara 	unsigned flags = IOMAP_FAULT;
1255a7d73fe6SChristoph Hellwig 	int error, major = 0;
1256d2c43ef1SJan Kara 	bool write = vmf->flags & FAULT_FLAG_WRITE;
1257caa51d26SJan Kara 	bool sync;
1258ab77dab4SSouptick Joarder 	vm_fault_t ret = 0;
1259a7d73fe6SChristoph Hellwig 	void *entry;
12601b5a1cb2SJan Kara 	pfn_t pfn;
1261a7d73fe6SChristoph Hellwig 
1262ab77dab4SSouptick Joarder 	trace_dax_pte_fault(inode, vmf, ret);
1263a7d73fe6SChristoph Hellwig 	/*
1264a7d73fe6SChristoph Hellwig 	 * Check whether offset isn't beyond end of file now. Caller is supposed
1265a7d73fe6SChristoph Hellwig 	 * to hold locks serializing us with truncate / punch hole so this is
1266a7d73fe6SChristoph Hellwig 	 * a reliable test.
1267a7d73fe6SChristoph Hellwig 	 */
1268a9c42b33SRoss Zwisler 	if (pos >= i_size_read(inode)) {
1269ab77dab4SSouptick Joarder 		ret = VM_FAULT_SIGBUS;
1270a9c42b33SRoss Zwisler 		goto out;
1271a9c42b33SRoss Zwisler 	}
1272a7d73fe6SChristoph Hellwig 
1273d2c43ef1SJan Kara 	if (write && !vmf->cow_page)
1274a7d73fe6SChristoph Hellwig 		flags |= IOMAP_WRITE;
1275a7d73fe6SChristoph Hellwig 
1276b15cd800SMatthew Wilcox 	entry = grab_mapping_entry(&xas, mapping, 0);
1277b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1278b15cd800SMatthew Wilcox 		ret = xa_to_internal(entry);
127913e451fdSJan Kara 		goto out;
128013e451fdSJan Kara 	}
128113e451fdSJan Kara 
1282a7d73fe6SChristoph Hellwig 	/*
1283e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1284e2093926SRoss Zwisler 	 * mappings, that we have raced with a PMD fault that overlaps with
1285e2093926SRoss Zwisler 	 * the PTE we need to set up.  If so just return and the fault will be
1286e2093926SRoss Zwisler 	 * retried.
1287e2093926SRoss Zwisler 	 */
1288e2093926SRoss Zwisler 	if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1289ab77dab4SSouptick Joarder 		ret = VM_FAULT_NOPAGE;
1290e2093926SRoss Zwisler 		goto unlock_entry;
1291e2093926SRoss Zwisler 	}
1292e2093926SRoss Zwisler 
1293e2093926SRoss Zwisler 	/*
1294a7d73fe6SChristoph Hellwig 	 * Note that we don't bother to use iomap_apply here: DAX required
1295a7d73fe6SChristoph Hellwig 	 * the file system block size to be equal the page size, which means
1296a7d73fe6SChristoph Hellwig 	 * that we never have to deal with more than a single extent here.
1297a7d73fe6SChristoph Hellwig 	 */
1298a7d73fe6SChristoph Hellwig 	error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1299c0b24625SJan Kara 	if (iomap_errp)
1300c0b24625SJan Kara 		*iomap_errp = error;
1301a9c42b33SRoss Zwisler 	if (error) {
1302ab77dab4SSouptick Joarder 		ret = dax_fault_return(error);
130313e451fdSJan Kara 		goto unlock_entry;
1304a9c42b33SRoss Zwisler 	}
1305a7d73fe6SChristoph Hellwig 	if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
130613e451fdSJan Kara 		error = -EIO;	/* fs corruption? */
130713e451fdSJan Kara 		goto error_finish_iomap;
1308a7d73fe6SChristoph Hellwig 	}
1309a7d73fe6SChristoph Hellwig 
1310a7d73fe6SChristoph Hellwig 	if (vmf->cow_page) {
131131a6f1a6SJan Kara 		sector_t sector = dax_iomap_sector(&iomap, pos);
131231a6f1a6SJan Kara 
1313a7d73fe6SChristoph Hellwig 		switch (iomap.type) {
1314a7d73fe6SChristoph Hellwig 		case IOMAP_HOLE:
1315a7d73fe6SChristoph Hellwig 		case IOMAP_UNWRITTEN:
1316a7d73fe6SChristoph Hellwig 			clear_user_highpage(vmf->cow_page, vaddr);
1317a7d73fe6SChristoph Hellwig 			break;
1318a7d73fe6SChristoph Hellwig 		case IOMAP_MAPPED:
1319cccbce67SDan Williams 			error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1320cccbce67SDan Williams 					sector, PAGE_SIZE, vmf->cow_page, vaddr);
1321a7d73fe6SChristoph Hellwig 			break;
1322a7d73fe6SChristoph Hellwig 		default:
1323a7d73fe6SChristoph Hellwig 			WARN_ON_ONCE(1);
1324a7d73fe6SChristoph Hellwig 			error = -EIO;
1325a7d73fe6SChristoph Hellwig 			break;
1326a7d73fe6SChristoph Hellwig 		}
1327a7d73fe6SChristoph Hellwig 
1328a7d73fe6SChristoph Hellwig 		if (error)
132913e451fdSJan Kara 			goto error_finish_iomap;
1330b1aa812bSJan Kara 
1331b1aa812bSJan Kara 		__SetPageUptodate(vmf->cow_page);
1332ab77dab4SSouptick Joarder 		ret = finish_fault(vmf);
1333ab77dab4SSouptick Joarder 		if (!ret)
1334ab77dab4SSouptick Joarder 			ret = VM_FAULT_DONE_COW;
133513e451fdSJan Kara 		goto finish_iomap;
1336a7d73fe6SChristoph Hellwig 	}
1337a7d73fe6SChristoph Hellwig 
1338aaa422c4SDan Williams 	sync = dax_fault_is_synchronous(flags, vma, &iomap);
1339caa51d26SJan Kara 
1340a7d73fe6SChristoph Hellwig 	switch (iomap.type) {
1341a7d73fe6SChristoph Hellwig 	case IOMAP_MAPPED:
1342a7d73fe6SChristoph Hellwig 		if (iomap.flags & IOMAP_F_NEW) {
1343a7d73fe6SChristoph Hellwig 			count_vm_event(PGMAJFAULT);
1344a0987ad5SJan Kara 			count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
1345a7d73fe6SChristoph Hellwig 			major = VM_FAULT_MAJOR;
1346a7d73fe6SChristoph Hellwig 		}
13471b5a1cb2SJan Kara 		error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
13481b5a1cb2SJan Kara 		if (error < 0)
13491b5a1cb2SJan Kara 			goto error_finish_iomap;
13501b5a1cb2SJan Kara 
1351b15cd800SMatthew Wilcox 		entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
1352caa51d26SJan Kara 						 0, write && !sync);
13531b5a1cb2SJan Kara 
1354caa51d26SJan Kara 		/*
1355caa51d26SJan Kara 		 * If we are doing synchronous page fault and inode needs fsync,
1356caa51d26SJan Kara 		 * we can insert PTE into page tables only after that happens.
1357caa51d26SJan Kara 		 * Skip insertion for now and return the pfn so that caller can
1358caa51d26SJan Kara 		 * insert it after fsync is done.
1359caa51d26SJan Kara 		 */
1360caa51d26SJan Kara 		if (sync) {
1361caa51d26SJan Kara 			if (WARN_ON_ONCE(!pfnp)) {
1362caa51d26SJan Kara 				error = -EIO;
1363caa51d26SJan Kara 				goto error_finish_iomap;
1364caa51d26SJan Kara 			}
1365caa51d26SJan Kara 			*pfnp = pfn;
1366ab77dab4SSouptick Joarder 			ret = VM_FAULT_NEEDDSYNC | major;
1367caa51d26SJan Kara 			goto finish_iomap;
1368caa51d26SJan Kara 		}
13691b5a1cb2SJan Kara 		trace_dax_insert_mapping(inode, vmf, entry);
13701b5a1cb2SJan Kara 		if (write)
1371ab77dab4SSouptick Joarder 			ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
13721b5a1cb2SJan Kara 		else
1373ab77dab4SSouptick Joarder 			ret = vmf_insert_mixed(vma, vaddr, pfn);
13741b5a1cb2SJan Kara 
1375ab77dab4SSouptick Joarder 		goto finish_iomap;
1376a7d73fe6SChristoph Hellwig 	case IOMAP_UNWRITTEN:
1377a7d73fe6SChristoph Hellwig 	case IOMAP_HOLE:
1378d2c43ef1SJan Kara 		if (!write) {
1379b15cd800SMatthew Wilcox 			ret = dax_load_hole(&xas, mapping, &entry, vmf);
138013e451fdSJan Kara 			goto finish_iomap;
13811550290bSRoss Zwisler 		}
1382a7d73fe6SChristoph Hellwig 		/*FALLTHRU*/
1383a7d73fe6SChristoph Hellwig 	default:
1384a7d73fe6SChristoph Hellwig 		WARN_ON_ONCE(1);
1385a7d73fe6SChristoph Hellwig 		error = -EIO;
1386a7d73fe6SChristoph Hellwig 		break;
1387a7d73fe6SChristoph Hellwig 	}
1388a7d73fe6SChristoph Hellwig 
138913e451fdSJan Kara  error_finish_iomap:
1390ab77dab4SSouptick Joarder 	ret = dax_fault_return(error);
13919f141d6eSJan Kara  finish_iomap:
13929f141d6eSJan Kara 	if (ops->iomap_end) {
13939f141d6eSJan Kara 		int copied = PAGE_SIZE;
13949f141d6eSJan Kara 
1395ab77dab4SSouptick Joarder 		if (ret & VM_FAULT_ERROR)
13969f141d6eSJan Kara 			copied = 0;
13979f141d6eSJan Kara 		/*
13989f141d6eSJan Kara 		 * The fault is done by now and there's no way back (other
13999f141d6eSJan Kara 		 * thread may be already happily using PTE we have installed).
14009f141d6eSJan Kara 		 * Just ignore error from ->iomap_end since we cannot do much
14019f141d6eSJan Kara 		 * with it.
14029f141d6eSJan Kara 		 */
14039f141d6eSJan Kara 		ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
14041550290bSRoss Zwisler 	}
140513e451fdSJan Kara  unlock_entry:
1406b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1407a9c42b33SRoss Zwisler  out:
1408ab77dab4SSouptick Joarder 	trace_dax_pte_fault_done(inode, vmf, ret);
1409ab77dab4SSouptick Joarder 	return ret | major;
1410a7d73fe6SChristoph Hellwig }
1411642261acSRoss Zwisler 
1412642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD
1413b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
1414b15cd800SMatthew Wilcox 		struct iomap *iomap, void **entry)
1415642261acSRoss Zwisler {
1416f4200391SDave Jiang 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1417f4200391SDave Jiang 	unsigned long pmd_addr = vmf->address & PMD_MASK;
1418653b2ea3SRoss Zwisler 	struct inode *inode = mapping->host;
1419642261acSRoss Zwisler 	struct page *zero_page;
1420642261acSRoss Zwisler 	spinlock_t *ptl;
1421642261acSRoss Zwisler 	pmd_t pmd_entry;
14223fe0791cSDan Williams 	pfn_t pfn;
1423642261acSRoss Zwisler 
1424f4200391SDave Jiang 	zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
1425642261acSRoss Zwisler 
1426642261acSRoss Zwisler 	if (unlikely(!zero_page))
1427653b2ea3SRoss Zwisler 		goto fallback;
1428642261acSRoss Zwisler 
14293fe0791cSDan Williams 	pfn = page_to_pfn_t(zero_page);
1430b15cd800SMatthew Wilcox 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
14313159f943SMatthew Wilcox 			DAX_PMD | DAX_ZERO_PAGE, false);
1432642261acSRoss Zwisler 
1433f4200391SDave Jiang 	ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1434f4200391SDave Jiang 	if (!pmd_none(*(vmf->pmd))) {
1435642261acSRoss Zwisler 		spin_unlock(ptl);
1436653b2ea3SRoss Zwisler 		goto fallback;
1437642261acSRoss Zwisler 	}
1438642261acSRoss Zwisler 
1439f4200391SDave Jiang 	pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
1440642261acSRoss Zwisler 	pmd_entry = pmd_mkhuge(pmd_entry);
1441f4200391SDave Jiang 	set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
1442642261acSRoss Zwisler 	spin_unlock(ptl);
1443b15cd800SMatthew Wilcox 	trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
1444642261acSRoss Zwisler 	return VM_FAULT_NOPAGE;
1445653b2ea3SRoss Zwisler 
1446653b2ea3SRoss Zwisler fallback:
1447b15cd800SMatthew Wilcox 	trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
1448642261acSRoss Zwisler 	return VM_FAULT_FALLBACK;
1449642261acSRoss Zwisler }
1450642261acSRoss Zwisler 
1451ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
1452a2d58167SDave Jiang 			       const struct iomap_ops *ops)
1453642261acSRoss Zwisler {
1454f4200391SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
1455642261acSRoss Zwisler 	struct address_space *mapping = vma->vm_file->f_mapping;
1456b15cd800SMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
1457d8a849e1SDave Jiang 	unsigned long pmd_addr = vmf->address & PMD_MASK;
1458d8a849e1SDave Jiang 	bool write = vmf->flags & FAULT_FLAG_WRITE;
1459caa51d26SJan Kara 	bool sync;
14609484ab1bSJan Kara 	unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
1461642261acSRoss Zwisler 	struct inode *inode = mapping->host;
1462ab77dab4SSouptick Joarder 	vm_fault_t result = VM_FAULT_FALLBACK;
1463642261acSRoss Zwisler 	struct iomap iomap = { 0 };
1464b15cd800SMatthew Wilcox 	pgoff_t max_pgoff;
1465642261acSRoss Zwisler 	void *entry;
1466642261acSRoss Zwisler 	loff_t pos;
1467642261acSRoss Zwisler 	int error;
1468302a5e31SJan Kara 	pfn_t pfn;
1469642261acSRoss Zwisler 
1470282a8e03SRoss Zwisler 	/*
1471282a8e03SRoss Zwisler 	 * Check whether offset isn't beyond end of file now. Caller is
1472282a8e03SRoss Zwisler 	 * supposed to hold locks serializing us with truncate / punch hole so
1473282a8e03SRoss Zwisler 	 * this is a reliable test.
1474282a8e03SRoss Zwisler 	 */
1475957ac8c4SJeff Moyer 	max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1476282a8e03SRoss Zwisler 
1477f4200391SDave Jiang 	trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
1478282a8e03SRoss Zwisler 
1479fffa281bSRoss Zwisler 	/*
1480fffa281bSRoss Zwisler 	 * Make sure that the faulting address's PMD offset (color) matches
1481fffa281bSRoss Zwisler 	 * the PMD offset from the start of the file.  This is necessary so
1482fffa281bSRoss Zwisler 	 * that a PMD range in the page table overlaps exactly with a PMD
1483a77d19f4SMatthew Wilcox 	 * range in the page cache.
1484fffa281bSRoss Zwisler 	 */
1485fffa281bSRoss Zwisler 	if ((vmf->pgoff & PG_PMD_COLOUR) !=
1486fffa281bSRoss Zwisler 	    ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1487fffa281bSRoss Zwisler 		goto fallback;
1488fffa281bSRoss Zwisler 
1489642261acSRoss Zwisler 	/* Fall back to PTEs if we're going to COW */
1490642261acSRoss Zwisler 	if (write && !(vma->vm_flags & VM_SHARED))
1491642261acSRoss Zwisler 		goto fallback;
1492642261acSRoss Zwisler 
1493642261acSRoss Zwisler 	/* If the PMD would extend outside the VMA */
1494642261acSRoss Zwisler 	if (pmd_addr < vma->vm_start)
1495642261acSRoss Zwisler 		goto fallback;
1496642261acSRoss Zwisler 	if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1497642261acSRoss Zwisler 		goto fallback;
1498642261acSRoss Zwisler 
1499b15cd800SMatthew Wilcox 	if (xas.xa_index >= max_pgoff) {
1500282a8e03SRoss Zwisler 		result = VM_FAULT_SIGBUS;
1501282a8e03SRoss Zwisler 		goto out;
1502282a8e03SRoss Zwisler 	}
1503642261acSRoss Zwisler 
1504642261acSRoss Zwisler 	/* If the PMD would extend beyond the file size */
1505b15cd800SMatthew Wilcox 	if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff)
1506642261acSRoss Zwisler 		goto fallback;
1507642261acSRoss Zwisler 
1508642261acSRoss Zwisler 	/*
1509b15cd800SMatthew Wilcox 	 * grab_mapping_entry() will make sure we get an empty PMD entry,
1510b15cd800SMatthew Wilcox 	 * a zero PMD entry or a DAX PMD.  If it can't (because a PTE
1511b15cd800SMatthew Wilcox 	 * entry is already in the array, for instance), it will return
1512b15cd800SMatthew Wilcox 	 * VM_FAULT_FALLBACK.
15139f141d6eSJan Kara 	 */
1514b15cd800SMatthew Wilcox 	entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
1515b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1516b15cd800SMatthew Wilcox 		result = xa_to_internal(entry);
1517876f2946SRoss Zwisler 		goto fallback;
1518b15cd800SMatthew Wilcox 	}
1519876f2946SRoss Zwisler 
1520876f2946SRoss Zwisler 	/*
1521e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1522e2093926SRoss Zwisler 	 * mappings, that we have raced with a PTE fault that overlaps with
1523e2093926SRoss Zwisler 	 * the PMD we need to set up.  If so just return and the fault will be
1524e2093926SRoss Zwisler 	 * retried.
1525e2093926SRoss Zwisler 	 */
1526e2093926SRoss Zwisler 	if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1527e2093926SRoss Zwisler 			!pmd_devmap(*vmf->pmd)) {
1528e2093926SRoss Zwisler 		result = 0;
1529e2093926SRoss Zwisler 		goto unlock_entry;
1530e2093926SRoss Zwisler 	}
1531e2093926SRoss Zwisler 
1532e2093926SRoss Zwisler 	/*
1533876f2946SRoss Zwisler 	 * Note that we don't use iomap_apply here.  We aren't doing I/O, only
1534876f2946SRoss Zwisler 	 * setting up a mapping, so really we're using iomap_begin() as a way
1535876f2946SRoss Zwisler 	 * to look up our filesystem block.
1536876f2946SRoss Zwisler 	 */
1537b15cd800SMatthew Wilcox 	pos = (loff_t)xas.xa_index << PAGE_SHIFT;
1538876f2946SRoss Zwisler 	error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1539876f2946SRoss Zwisler 	if (error)
1540876f2946SRoss Zwisler 		goto unlock_entry;
1541876f2946SRoss Zwisler 
1542876f2946SRoss Zwisler 	if (iomap.offset + iomap.length < pos + PMD_SIZE)
15439f141d6eSJan Kara 		goto finish_iomap;
15449f141d6eSJan Kara 
1545aaa422c4SDan Williams 	sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
1546caa51d26SJan Kara 
1547642261acSRoss Zwisler 	switch (iomap.type) {
1548642261acSRoss Zwisler 	case IOMAP_MAPPED:
1549302a5e31SJan Kara 		error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1550302a5e31SJan Kara 		if (error < 0)
1551302a5e31SJan Kara 			goto finish_iomap;
1552302a5e31SJan Kara 
1553b15cd800SMatthew Wilcox 		entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
15543159f943SMatthew Wilcox 						DAX_PMD, write && !sync);
1555302a5e31SJan Kara 
1556caa51d26SJan Kara 		/*
1557caa51d26SJan Kara 		 * If we are doing synchronous page fault and inode needs fsync,
1558caa51d26SJan Kara 		 * we can insert PMD into page tables only after that happens.
1559caa51d26SJan Kara 		 * Skip insertion for now and return the pfn so that caller can
1560caa51d26SJan Kara 		 * insert it after fsync is done.
1561caa51d26SJan Kara 		 */
1562caa51d26SJan Kara 		if (sync) {
1563caa51d26SJan Kara 			if (WARN_ON_ONCE(!pfnp))
1564caa51d26SJan Kara 				goto finish_iomap;
1565caa51d26SJan Kara 			*pfnp = pfn;
1566caa51d26SJan Kara 			result = VM_FAULT_NEEDDSYNC;
1567caa51d26SJan Kara 			goto finish_iomap;
1568caa51d26SJan Kara 		}
1569caa51d26SJan Kara 
1570302a5e31SJan Kara 		trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1571302a5e31SJan Kara 		result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1572302a5e31SJan Kara 					    write);
1573642261acSRoss Zwisler 		break;
1574642261acSRoss Zwisler 	case IOMAP_UNWRITTEN:
1575642261acSRoss Zwisler 	case IOMAP_HOLE:
1576642261acSRoss Zwisler 		if (WARN_ON_ONCE(write))
1577876f2946SRoss Zwisler 			break;
1578b15cd800SMatthew Wilcox 		result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry);
1579642261acSRoss Zwisler 		break;
1580642261acSRoss Zwisler 	default:
1581642261acSRoss Zwisler 		WARN_ON_ONCE(1);
1582642261acSRoss Zwisler 		break;
1583642261acSRoss Zwisler 	}
1584642261acSRoss Zwisler 
15859f141d6eSJan Kara  finish_iomap:
15869f141d6eSJan Kara 	if (ops->iomap_end) {
15879f141d6eSJan Kara 		int copied = PMD_SIZE;
15889f141d6eSJan Kara 
15899f141d6eSJan Kara 		if (result == VM_FAULT_FALLBACK)
15909f141d6eSJan Kara 			copied = 0;
15919f141d6eSJan Kara 		/*
15929f141d6eSJan Kara 		 * The fault is done by now and there's no way back (other
15939f141d6eSJan Kara 		 * thread may be already happily using PMD we have installed).
15949f141d6eSJan Kara 		 * Just ignore error from ->iomap_end since we cannot do much
15959f141d6eSJan Kara 		 * with it.
15969f141d6eSJan Kara 		 */
15979f141d6eSJan Kara 		ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
15989f141d6eSJan Kara 				&iomap);
15999f141d6eSJan Kara 	}
1600876f2946SRoss Zwisler  unlock_entry:
1601b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1602642261acSRoss Zwisler  fallback:
1603642261acSRoss Zwisler 	if (result == VM_FAULT_FALLBACK) {
1604d8a849e1SDave Jiang 		split_huge_pmd(vma, vmf->pmd, vmf->address);
1605642261acSRoss Zwisler 		count_vm_event(THP_FAULT_FALLBACK);
1606642261acSRoss Zwisler 	}
1607282a8e03SRoss Zwisler out:
1608f4200391SDave Jiang 	trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
1609642261acSRoss Zwisler 	return result;
1610642261acSRoss Zwisler }
1611a2d58167SDave Jiang #else
1612ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
161301cddfe9SArnd Bergmann 			       const struct iomap_ops *ops)
1614a2d58167SDave Jiang {
1615a2d58167SDave Jiang 	return VM_FAULT_FALLBACK;
1616a2d58167SDave Jiang }
1617642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */
1618a2d58167SDave Jiang 
1619a2d58167SDave Jiang /**
1620a2d58167SDave Jiang  * dax_iomap_fault - handle a page fault on a DAX file
1621a2d58167SDave Jiang  * @vmf: The description of the fault
1622cec04e8cSJan Kara  * @pe_size: Size of the page to fault in
16239a0dd422SJan Kara  * @pfnp: PFN to insert for synchronous faults if fsync is required
1624c0b24625SJan Kara  * @iomap_errp: Storage for detailed error code in case of error
1625cec04e8cSJan Kara  * @ops: Iomap ops passed from the file system
1626a2d58167SDave Jiang  *
1627a2d58167SDave Jiang  * When a page fault occurs, filesystems may call this helper in
1628a2d58167SDave Jiang  * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1629a2d58167SDave Jiang  * has done all the necessary locking for page fault to proceed
1630a2d58167SDave Jiang  * successfully.
1631a2d58167SDave Jiang  */
1632ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1633c0b24625SJan Kara 		    pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
1634a2d58167SDave Jiang {
1635c791ace1SDave Jiang 	switch (pe_size) {
1636c791ace1SDave Jiang 	case PE_SIZE_PTE:
1637c0b24625SJan Kara 		return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
1638c791ace1SDave Jiang 	case PE_SIZE_PMD:
16399a0dd422SJan Kara 		return dax_iomap_pmd_fault(vmf, pfnp, ops);
1640a2d58167SDave Jiang 	default:
1641a2d58167SDave Jiang 		return VM_FAULT_FALLBACK;
1642a2d58167SDave Jiang 	}
1643a2d58167SDave Jiang }
1644a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault);
164571eab6dfSJan Kara 
1646a77d19f4SMatthew Wilcox /*
164771eab6dfSJan Kara  * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
164871eab6dfSJan Kara  * @vmf: The description of the fault
164971eab6dfSJan Kara  * @pfn: PFN to insert
1650cfc93c6cSMatthew Wilcox  * @order: Order of entry to insert.
165171eab6dfSJan Kara  *
1652a77d19f4SMatthew Wilcox  * This function inserts a writeable PTE or PMD entry into the page tables
1653a77d19f4SMatthew Wilcox  * for an mmaped DAX file.  It also marks the page cache entry as dirty.
165471eab6dfSJan Kara  */
1655cfc93c6cSMatthew Wilcox static vm_fault_t
1656cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
165771eab6dfSJan Kara {
165871eab6dfSJan Kara 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1659cfc93c6cSMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1660cfc93c6cSMatthew Wilcox 	void *entry;
1661ab77dab4SSouptick Joarder 	vm_fault_t ret;
166271eab6dfSJan Kara 
1663cfc93c6cSMatthew Wilcox 	xas_lock_irq(&xas);
1664cfc93c6cSMatthew Wilcox 	entry = get_unlocked_entry(&xas);
166571eab6dfSJan Kara 	/* Did we race with someone splitting entry or so? */
166671eab6dfSJan Kara 	if (!entry ||
1667cfc93c6cSMatthew Wilcox 	    (order == 0 && !dax_is_pte_entry(entry)) ||
16680e40de03SMatthew Wilcox 	    (order == PMD_ORDER && !dax_is_pmd_entry(entry))) {
1669cfc93c6cSMatthew Wilcox 		put_unlocked_entry(&xas, entry);
1670cfc93c6cSMatthew Wilcox 		xas_unlock_irq(&xas);
167171eab6dfSJan Kara 		trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
167271eab6dfSJan Kara 						      VM_FAULT_NOPAGE);
167371eab6dfSJan Kara 		return VM_FAULT_NOPAGE;
167471eab6dfSJan Kara 	}
1675cfc93c6cSMatthew Wilcox 	xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1676cfc93c6cSMatthew Wilcox 	dax_lock_entry(&xas, entry);
1677cfc93c6cSMatthew Wilcox 	xas_unlock_irq(&xas);
1678cfc93c6cSMatthew Wilcox 	if (order == 0)
1679ab77dab4SSouptick Joarder 		ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
168071eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD
1681cfc93c6cSMatthew Wilcox 	else if (order == PMD_ORDER)
1682ab77dab4SSouptick Joarder 		ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
168371eab6dfSJan Kara 			pfn, true);
168471eab6dfSJan Kara #endif
1685cfc93c6cSMatthew Wilcox 	else
1686ab77dab4SSouptick Joarder 		ret = VM_FAULT_FALLBACK;
1687cfc93c6cSMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1688ab77dab4SSouptick Joarder 	trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1689ab77dab4SSouptick Joarder 	return ret;
169071eab6dfSJan Kara }
169171eab6dfSJan Kara 
169271eab6dfSJan Kara /**
169371eab6dfSJan Kara  * dax_finish_sync_fault - finish synchronous page fault
169471eab6dfSJan Kara  * @vmf: The description of the fault
169571eab6dfSJan Kara  * @pe_size: Size of entry to be inserted
169671eab6dfSJan Kara  * @pfn: PFN to insert
169771eab6dfSJan Kara  *
169871eab6dfSJan Kara  * This function ensures that the file range touched by the page fault is
169971eab6dfSJan Kara  * stored persistently on the media and handles inserting of appropriate page
170071eab6dfSJan Kara  * table entry.
170171eab6dfSJan Kara  */
1702ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1703ab77dab4SSouptick Joarder 		enum page_entry_size pe_size, pfn_t pfn)
170471eab6dfSJan Kara {
170571eab6dfSJan Kara 	int err;
170671eab6dfSJan Kara 	loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1707cfc93c6cSMatthew Wilcox 	unsigned int order = pe_order(pe_size);
1708cfc93c6cSMatthew Wilcox 	size_t len = PAGE_SIZE << order;
170971eab6dfSJan Kara 
171071eab6dfSJan Kara 	err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
171171eab6dfSJan Kara 	if (err)
171271eab6dfSJan Kara 		return VM_FAULT_SIGBUS;
1713cfc93c6cSMatthew Wilcox 	return dax_insert_pfn_mkwrite(vmf, pfn, order);
171471eab6dfSJan Kara }
171571eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault);
1716