xref: /openbmc/linux/fs/dax.c (revision 06083a09)
12025cf9eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d475c634SMatthew Wilcox /*
3d475c634SMatthew Wilcox  * fs/dax.c - Direct Access filesystem code
4d475c634SMatthew Wilcox  * Copyright (c) 2013-2014 Intel Corporation
5d475c634SMatthew Wilcox  * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
6d475c634SMatthew Wilcox  * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
7d475c634SMatthew Wilcox  */
8d475c634SMatthew Wilcox 
9d475c634SMatthew Wilcox #include <linux/atomic.h>
10d475c634SMatthew Wilcox #include <linux/blkdev.h>
11d475c634SMatthew Wilcox #include <linux/buffer_head.h>
12d77e92e2SRoss Zwisler #include <linux/dax.h>
13d475c634SMatthew Wilcox #include <linux/fs.h>
14f7ca90b1SMatthew Wilcox #include <linux/highmem.h>
15f7ca90b1SMatthew Wilcox #include <linux/memcontrol.h>
16f7ca90b1SMatthew Wilcox #include <linux/mm.h>
17d475c634SMatthew Wilcox #include <linux/mutex.h>
189973c98eSRoss Zwisler #include <linux/pagevec.h>
19289c6aedSMatthew Wilcox #include <linux/sched.h>
20f361bf4aSIngo Molnar #include <linux/sched/signal.h>
21d475c634SMatthew Wilcox #include <linux/uio.h>
22f7ca90b1SMatthew Wilcox #include <linux/vmstat.h>
2334c0fd54SDan Williams #include <linux/pfn_t.h>
240e749e54SDan Williams #include <linux/sizes.h>
254b4bb46dSJan Kara #include <linux/mmu_notifier.h>
26a254e568SChristoph Hellwig #include <linux/iomap.h>
27*06083a09SMuchun Song #include <linux/rmap.h>
2811cf9d86SAneesh Kumar K.V #include <asm/pgalloc.h>
29d475c634SMatthew Wilcox 
30282a8e03SRoss Zwisler #define CREATE_TRACE_POINTS
31282a8e03SRoss Zwisler #include <trace/events/fs_dax.h>
32282a8e03SRoss Zwisler 
33cfc93c6cSMatthew Wilcox static inline unsigned int pe_order(enum page_entry_size pe_size)
34cfc93c6cSMatthew Wilcox {
35cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PTE)
36cfc93c6cSMatthew Wilcox 		return PAGE_SHIFT - PAGE_SHIFT;
37cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PMD)
38cfc93c6cSMatthew Wilcox 		return PMD_SHIFT - PAGE_SHIFT;
39cfc93c6cSMatthew Wilcox 	if (pe_size == PE_SIZE_PUD)
40cfc93c6cSMatthew Wilcox 		return PUD_SHIFT - PAGE_SHIFT;
41cfc93c6cSMatthew Wilcox 	return ~0;
42cfc93c6cSMatthew Wilcox }
43cfc93c6cSMatthew Wilcox 
44ac401cc7SJan Kara /* We choose 4096 entries - same as per-zone page wait tables */
45ac401cc7SJan Kara #define DAX_WAIT_TABLE_BITS 12
46ac401cc7SJan Kara #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
47ac401cc7SJan Kara 
48917f3452SRoss Zwisler /* The 'colour' (ie low bits) within a PMD of a page offset.  */
49917f3452SRoss Zwisler #define PG_PMD_COLOUR	((PMD_SIZE >> PAGE_SHIFT) - 1)
50977fbdcdSMatthew Wilcox #define PG_PMD_NR	(PMD_SIZE >> PAGE_SHIFT)
51917f3452SRoss Zwisler 
52cfc93c6cSMatthew Wilcox /* The order of a PMD entry */
53cfc93c6cSMatthew Wilcox #define PMD_ORDER	(PMD_SHIFT - PAGE_SHIFT)
54cfc93c6cSMatthew Wilcox 
55ce95ab0fSRoss Zwisler static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
56ac401cc7SJan Kara 
57ac401cc7SJan Kara static int __init init_dax_wait_table(void)
58ac401cc7SJan Kara {
59ac401cc7SJan Kara 	int i;
60ac401cc7SJan Kara 
61ac401cc7SJan Kara 	for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
62ac401cc7SJan Kara 		init_waitqueue_head(wait_table + i);
63ac401cc7SJan Kara 	return 0;
64ac401cc7SJan Kara }
65ac401cc7SJan Kara fs_initcall(init_dax_wait_table);
66ac401cc7SJan Kara 
67527b19d0SRoss Zwisler /*
683159f943SMatthew Wilcox  * DAX pagecache entries use XArray value entries so they can't be mistaken
693159f943SMatthew Wilcox  * for pages.  We use one bit for locking, one bit for the entry size (PMD)
703159f943SMatthew Wilcox  * and two more to tell us if the entry is a zero page or an empty entry that
713159f943SMatthew Wilcox  * is just used for locking.  In total four special bits.
72527b19d0SRoss Zwisler  *
73527b19d0SRoss Zwisler  * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
74527b19d0SRoss Zwisler  * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
75527b19d0SRoss Zwisler  * block allocation.
76527b19d0SRoss Zwisler  */
773159f943SMatthew Wilcox #define DAX_SHIFT	(4)
783159f943SMatthew Wilcox #define DAX_LOCKED	(1UL << 0)
793159f943SMatthew Wilcox #define DAX_PMD		(1UL << 1)
803159f943SMatthew Wilcox #define DAX_ZERO_PAGE	(1UL << 2)
813159f943SMatthew Wilcox #define DAX_EMPTY	(1UL << 3)
82527b19d0SRoss Zwisler 
83a77d19f4SMatthew Wilcox static unsigned long dax_to_pfn(void *entry)
84527b19d0SRoss Zwisler {
853159f943SMatthew Wilcox 	return xa_to_value(entry) >> DAX_SHIFT;
86527b19d0SRoss Zwisler }
87527b19d0SRoss Zwisler 
889f32d221SMatthew Wilcox static void *dax_make_entry(pfn_t pfn, unsigned long flags)
899f32d221SMatthew Wilcox {
909f32d221SMatthew Wilcox 	return xa_mk_value(flags | (pfn_t_to_pfn(pfn) << DAX_SHIFT));
919f32d221SMatthew Wilcox }
929f32d221SMatthew Wilcox 
93cfc93c6cSMatthew Wilcox static bool dax_is_locked(void *entry)
94cfc93c6cSMatthew Wilcox {
95cfc93c6cSMatthew Wilcox 	return xa_to_value(entry) & DAX_LOCKED;
96cfc93c6cSMatthew Wilcox }
97cfc93c6cSMatthew Wilcox 
98a77d19f4SMatthew Wilcox static unsigned int dax_entry_order(void *entry)
99527b19d0SRoss Zwisler {
1003159f943SMatthew Wilcox 	if (xa_to_value(entry) & DAX_PMD)
101cfc93c6cSMatthew Wilcox 		return PMD_ORDER;
102527b19d0SRoss Zwisler 	return 0;
103527b19d0SRoss Zwisler }
104527b19d0SRoss Zwisler 
105fda490d3SMatthew Wilcox static unsigned long dax_is_pmd_entry(void *entry)
106642261acSRoss Zwisler {
1073159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_PMD;
108642261acSRoss Zwisler }
109642261acSRoss Zwisler 
110fda490d3SMatthew Wilcox static bool dax_is_pte_entry(void *entry)
111642261acSRoss Zwisler {
1123159f943SMatthew Wilcox 	return !(xa_to_value(entry) & DAX_PMD);
113642261acSRoss Zwisler }
114642261acSRoss Zwisler 
115642261acSRoss Zwisler static int dax_is_zero_entry(void *entry)
116642261acSRoss Zwisler {
1173159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_ZERO_PAGE;
118642261acSRoss Zwisler }
119642261acSRoss Zwisler 
120642261acSRoss Zwisler static int dax_is_empty_entry(void *entry)
121642261acSRoss Zwisler {
1223159f943SMatthew Wilcox 	return xa_to_value(entry) & DAX_EMPTY;
123642261acSRoss Zwisler }
124642261acSRoss Zwisler 
125f7ca90b1SMatthew Wilcox /*
12623c84eb7SMatthew Wilcox (Oracle)  * true if the entry that was found is of a smaller order than the entry
12723c84eb7SMatthew Wilcox (Oracle)  * we were looking for
12823c84eb7SMatthew Wilcox (Oracle)  */
12923c84eb7SMatthew Wilcox (Oracle) static bool dax_is_conflict(void *entry)
13023c84eb7SMatthew Wilcox (Oracle) {
13123c84eb7SMatthew Wilcox (Oracle) 	return entry == XA_RETRY_ENTRY;
13223c84eb7SMatthew Wilcox (Oracle) }
13323c84eb7SMatthew Wilcox (Oracle) 
13423c84eb7SMatthew Wilcox (Oracle) /*
135a77d19f4SMatthew Wilcox  * DAX page cache entry locking
136ac401cc7SJan Kara  */
137ac401cc7SJan Kara struct exceptional_entry_key {
138ec4907ffSMatthew Wilcox 	struct xarray *xa;
13963e95b5cSRoss Zwisler 	pgoff_t entry_start;
140ac401cc7SJan Kara };
141ac401cc7SJan Kara 
142ac401cc7SJan Kara struct wait_exceptional_entry_queue {
143ac6424b9SIngo Molnar 	wait_queue_entry_t wait;
144ac401cc7SJan Kara 	struct exceptional_entry_key key;
145ac401cc7SJan Kara };
146ac401cc7SJan Kara 
147698ab77aSVivek Goyal /**
148698ab77aSVivek Goyal  * enum dax_wake_mode: waitqueue wakeup behaviour
149698ab77aSVivek Goyal  * @WAKE_ALL: wake all waiters in the waitqueue
150698ab77aSVivek Goyal  * @WAKE_NEXT: wake only the first waiter in the waitqueue
151698ab77aSVivek Goyal  */
152698ab77aSVivek Goyal enum dax_wake_mode {
153698ab77aSVivek Goyal 	WAKE_ALL,
154698ab77aSVivek Goyal 	WAKE_NEXT,
155698ab77aSVivek Goyal };
156698ab77aSVivek Goyal 
157b15cd800SMatthew Wilcox static wait_queue_head_t *dax_entry_waitqueue(struct xa_state *xas,
158b15cd800SMatthew Wilcox 		void *entry, struct exceptional_entry_key *key)
15963e95b5cSRoss Zwisler {
16063e95b5cSRoss Zwisler 	unsigned long hash;
161b15cd800SMatthew Wilcox 	unsigned long index = xas->xa_index;
16263e95b5cSRoss Zwisler 
16363e95b5cSRoss Zwisler 	/*
16463e95b5cSRoss Zwisler 	 * If 'entry' is a PMD, align the 'index' that we use for the wait
16563e95b5cSRoss Zwisler 	 * queue to the start of that PMD.  This ensures that all offsets in
16663e95b5cSRoss Zwisler 	 * the range covered by the PMD map to the same bit lock.
16763e95b5cSRoss Zwisler 	 */
168642261acSRoss Zwisler 	if (dax_is_pmd_entry(entry))
169917f3452SRoss Zwisler 		index &= ~PG_PMD_COLOUR;
170b15cd800SMatthew Wilcox 	key->xa = xas->xa;
17163e95b5cSRoss Zwisler 	key->entry_start = index;
17263e95b5cSRoss Zwisler 
173b15cd800SMatthew Wilcox 	hash = hash_long((unsigned long)xas->xa ^ index, DAX_WAIT_TABLE_BITS);
17463e95b5cSRoss Zwisler 	return wait_table + hash;
17563e95b5cSRoss Zwisler }
17663e95b5cSRoss Zwisler 
177ec4907ffSMatthew Wilcox static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
178ec4907ffSMatthew Wilcox 		unsigned int mode, int sync, void *keyp)
179ac401cc7SJan Kara {
180ac401cc7SJan Kara 	struct exceptional_entry_key *key = keyp;
181ac401cc7SJan Kara 	struct wait_exceptional_entry_queue *ewait =
182ac401cc7SJan Kara 		container_of(wait, struct wait_exceptional_entry_queue, wait);
183ac401cc7SJan Kara 
184ec4907ffSMatthew Wilcox 	if (key->xa != ewait->key.xa ||
18563e95b5cSRoss Zwisler 	    key->entry_start != ewait->key.entry_start)
186ac401cc7SJan Kara 		return 0;
187ac401cc7SJan Kara 	return autoremove_wake_function(wait, mode, sync, NULL);
188ac401cc7SJan Kara }
189ac401cc7SJan Kara 
190ac401cc7SJan Kara /*
191b93b0163SMatthew Wilcox  * @entry may no longer be the entry at the index in the mapping.
192b93b0163SMatthew Wilcox  * The important information it's conveying is whether the entry at
193b93b0163SMatthew Wilcox  * this index used to be a PMD entry.
194e30331ffSRoss Zwisler  */
195698ab77aSVivek Goyal static void dax_wake_entry(struct xa_state *xas, void *entry,
196698ab77aSVivek Goyal 			   enum dax_wake_mode mode)
197e30331ffSRoss Zwisler {
198e30331ffSRoss Zwisler 	struct exceptional_entry_key key;
199e30331ffSRoss Zwisler 	wait_queue_head_t *wq;
200e30331ffSRoss Zwisler 
201b15cd800SMatthew Wilcox 	wq = dax_entry_waitqueue(xas, entry, &key);
202e30331ffSRoss Zwisler 
203e30331ffSRoss Zwisler 	/*
204e30331ffSRoss Zwisler 	 * Checking for locked entry and prepare_to_wait_exclusive() happens
205b93b0163SMatthew Wilcox 	 * under the i_pages lock, ditto for entry handling in our callers.
206e30331ffSRoss Zwisler 	 * So at this point all tasks that could have seen our entry locked
207e30331ffSRoss Zwisler 	 * must be in the waitqueue and the following check will see them.
208e30331ffSRoss Zwisler 	 */
209e30331ffSRoss Zwisler 	if (waitqueue_active(wq))
210698ab77aSVivek Goyal 		__wake_up(wq, TASK_NORMAL, mode == WAKE_ALL ? 0 : 1, &key);
211e30331ffSRoss Zwisler }
212e30331ffSRoss Zwisler 
213cfc93c6cSMatthew Wilcox /*
214cfc93c6cSMatthew Wilcox  * Look up entry in page cache, wait for it to become unlocked if it
215cfc93c6cSMatthew Wilcox  * is a DAX entry and return it.  The caller must subsequently call
216cfc93c6cSMatthew Wilcox  * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
21723c84eb7SMatthew Wilcox (Oracle)  * if it did.  The entry returned may have a larger order than @order.
21823c84eb7SMatthew Wilcox (Oracle)  * If @order is larger than the order of the entry found in i_pages, this
21923c84eb7SMatthew Wilcox (Oracle)  * function returns a dax_is_conflict entry.
220cfc93c6cSMatthew Wilcox  *
221cfc93c6cSMatthew Wilcox  * Must be called with the i_pages lock held.
222cfc93c6cSMatthew Wilcox  */
22323c84eb7SMatthew Wilcox (Oracle) static void *get_unlocked_entry(struct xa_state *xas, unsigned int order)
224cfc93c6cSMatthew Wilcox {
225cfc93c6cSMatthew Wilcox 	void *entry;
226cfc93c6cSMatthew Wilcox 	struct wait_exceptional_entry_queue ewait;
227cfc93c6cSMatthew Wilcox 	wait_queue_head_t *wq;
228cfc93c6cSMatthew Wilcox 
229cfc93c6cSMatthew Wilcox 	init_wait(&ewait.wait);
230cfc93c6cSMatthew Wilcox 	ewait.wait.func = wake_exceptional_entry_func;
231cfc93c6cSMatthew Wilcox 
232cfc93c6cSMatthew Wilcox 	for (;;) {
2330e40de03SMatthew Wilcox 		entry = xas_find_conflict(xas);
2346370740eSDan Williams 		if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
2356370740eSDan Williams 			return entry;
23623c84eb7SMatthew Wilcox (Oracle) 		if (dax_entry_order(entry) < order)
23723c84eb7SMatthew Wilcox (Oracle) 			return XA_RETRY_ENTRY;
2386370740eSDan Williams 		if (!dax_is_locked(entry))
239cfc93c6cSMatthew Wilcox 			return entry;
240cfc93c6cSMatthew Wilcox 
241b15cd800SMatthew Wilcox 		wq = dax_entry_waitqueue(xas, entry, &ewait.key);
242cfc93c6cSMatthew Wilcox 		prepare_to_wait_exclusive(wq, &ewait.wait,
243cfc93c6cSMatthew Wilcox 					  TASK_UNINTERRUPTIBLE);
244cfc93c6cSMatthew Wilcox 		xas_unlock_irq(xas);
245cfc93c6cSMatthew Wilcox 		xas_reset(xas);
246cfc93c6cSMatthew Wilcox 		schedule();
247cfc93c6cSMatthew Wilcox 		finish_wait(wq, &ewait.wait);
248cfc93c6cSMatthew Wilcox 		xas_lock_irq(xas);
249cfc93c6cSMatthew Wilcox 	}
250cfc93c6cSMatthew Wilcox }
251cfc93c6cSMatthew Wilcox 
25255e56f06SMatthew Wilcox /*
25355e56f06SMatthew Wilcox  * The only thing keeping the address space around is the i_pages lock
25455e56f06SMatthew Wilcox  * (it's cycled in clear_inode() after removing the entries from i_pages)
25555e56f06SMatthew Wilcox  * After we call xas_unlock_irq(), we cannot touch xas->xa.
25655e56f06SMatthew Wilcox  */
25755e56f06SMatthew Wilcox static void wait_entry_unlocked(struct xa_state *xas, void *entry)
25855e56f06SMatthew Wilcox {
25955e56f06SMatthew Wilcox 	struct wait_exceptional_entry_queue ewait;
26055e56f06SMatthew Wilcox 	wait_queue_head_t *wq;
26155e56f06SMatthew Wilcox 
26255e56f06SMatthew Wilcox 	init_wait(&ewait.wait);
26355e56f06SMatthew Wilcox 	ewait.wait.func = wake_exceptional_entry_func;
26455e56f06SMatthew Wilcox 
26555e56f06SMatthew Wilcox 	wq = dax_entry_waitqueue(xas, entry, &ewait.key);
266d8a70641SDan Williams 	/*
267d8a70641SDan Williams 	 * Unlike get_unlocked_entry() there is no guarantee that this
268d8a70641SDan Williams 	 * path ever successfully retrieves an unlocked entry before an
269d8a70641SDan Williams 	 * inode dies. Perform a non-exclusive wait in case this path
270d8a70641SDan Williams 	 * never successfully performs its own wake up.
271d8a70641SDan Williams 	 */
272d8a70641SDan Williams 	prepare_to_wait(wq, &ewait.wait, TASK_UNINTERRUPTIBLE);
27355e56f06SMatthew Wilcox 	xas_unlock_irq(xas);
27455e56f06SMatthew Wilcox 	schedule();
27555e56f06SMatthew Wilcox 	finish_wait(wq, &ewait.wait);
27655e56f06SMatthew Wilcox }
27755e56f06SMatthew Wilcox 
2784c3d043dSVivek Goyal static void put_unlocked_entry(struct xa_state *xas, void *entry,
2794c3d043dSVivek Goyal 			       enum dax_wake_mode mode)
280cfc93c6cSMatthew Wilcox {
28161c30c98SJan Kara 	if (entry && !dax_is_conflict(entry))
2824c3d043dSVivek Goyal 		dax_wake_entry(xas, entry, mode);
283cfc93c6cSMatthew Wilcox }
284cfc93c6cSMatthew Wilcox 
285cfc93c6cSMatthew Wilcox /*
286cfc93c6cSMatthew Wilcox  * We used the xa_state to get the entry, but then we locked the entry and
287cfc93c6cSMatthew Wilcox  * dropped the xa_lock, so we know the xa_state is stale and must be reset
288cfc93c6cSMatthew Wilcox  * before use.
289cfc93c6cSMatthew Wilcox  */
290cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry)
291cfc93c6cSMatthew Wilcox {
292cfc93c6cSMatthew Wilcox 	void *old;
293cfc93c6cSMatthew Wilcox 
2947ae2ea7dSMatthew Wilcox 	BUG_ON(dax_is_locked(entry));
295cfc93c6cSMatthew Wilcox 	xas_reset(xas);
296cfc93c6cSMatthew Wilcox 	xas_lock_irq(xas);
297cfc93c6cSMatthew Wilcox 	old = xas_store(xas, entry);
298cfc93c6cSMatthew Wilcox 	xas_unlock_irq(xas);
299cfc93c6cSMatthew Wilcox 	BUG_ON(!dax_is_locked(old));
300698ab77aSVivek Goyal 	dax_wake_entry(xas, entry, WAKE_NEXT);
301cfc93c6cSMatthew Wilcox }
302cfc93c6cSMatthew Wilcox 
303cfc93c6cSMatthew Wilcox /*
304cfc93c6cSMatthew Wilcox  * Return: The entry stored at this location before it was locked.
305cfc93c6cSMatthew Wilcox  */
306cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry)
307cfc93c6cSMatthew Wilcox {
308cfc93c6cSMatthew Wilcox 	unsigned long v = xa_to_value(entry);
309cfc93c6cSMatthew Wilcox 	return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
310cfc93c6cSMatthew Wilcox }
311cfc93c6cSMatthew Wilcox 
312d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry)
313d2c997c0SDan Williams {
314d2c997c0SDan Williams 	if (dax_is_zero_entry(entry))
315d2c997c0SDan Williams 		return 0;
316d2c997c0SDan Williams 	else if (dax_is_empty_entry(entry))
317d2c997c0SDan Williams 		return 0;
318d2c997c0SDan Williams 	else if (dax_is_pmd_entry(entry))
319d2c997c0SDan Williams 		return PMD_SIZE;
320d2c997c0SDan Williams 	else
321d2c997c0SDan Williams 		return PAGE_SIZE;
322d2c997c0SDan Williams }
323d2c997c0SDan Williams 
324a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry)
325d2c997c0SDan Williams {
326a77d19f4SMatthew Wilcox 	return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
327d2c997c0SDan Williams }
328d2c997c0SDan Williams 
329d2c997c0SDan Williams /*
330d2c997c0SDan Williams  * Iterate through all mapped pfns represented by an entry, i.e. skip
331d2c997c0SDan Williams  * 'empty' and 'zero' entries.
332d2c997c0SDan Williams  */
333d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \
334a77d19f4SMatthew Wilcox 	for (pfn = dax_to_pfn(entry); \
335a77d19f4SMatthew Wilcox 			pfn < dax_end_pfn(entry); pfn++)
336d2c997c0SDan Williams 
33773449dafSDan Williams /*
33873449dafSDan Williams  * TODO: for reflink+dax we need a way to associate a single page with
33973449dafSDan Williams  * multiple address_space instances at different linear_page_index()
34073449dafSDan Williams  * offsets.
34173449dafSDan Williams  */
34273449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping,
34373449dafSDan Williams 		struct vm_area_struct *vma, unsigned long address)
344d2c997c0SDan Williams {
34573449dafSDan Williams 	unsigned long size = dax_entry_size(entry), pfn, index;
34673449dafSDan Williams 	int i = 0;
347d2c997c0SDan Williams 
348d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
349d2c997c0SDan Williams 		return;
350d2c997c0SDan Williams 
35173449dafSDan Williams 	index = linear_page_index(vma, address & ~(size - 1));
352d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
353d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
354d2c997c0SDan Williams 
355d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping);
356d2c997c0SDan Williams 		page->mapping = mapping;
35773449dafSDan Williams 		page->index = index + i++;
358d2c997c0SDan Williams 	}
359d2c997c0SDan Williams }
360d2c997c0SDan Williams 
361d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping,
362d2c997c0SDan Williams 		bool trunc)
363d2c997c0SDan Williams {
364d2c997c0SDan Williams 	unsigned long pfn;
365d2c997c0SDan Williams 
366d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
367d2c997c0SDan Williams 		return;
368d2c997c0SDan Williams 
369d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
370d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
371d2c997c0SDan Williams 
372d2c997c0SDan Williams 		WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
373d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping && page->mapping != mapping);
374d2c997c0SDan Williams 		page->mapping = NULL;
37573449dafSDan Williams 		page->index = 0;
376d2c997c0SDan Williams 	}
377d2c997c0SDan Williams }
378d2c997c0SDan Williams 
3795fac7408SDan Williams static struct page *dax_busy_page(void *entry)
3805fac7408SDan Williams {
3815fac7408SDan Williams 	unsigned long pfn;
3825fac7408SDan Williams 
3835fac7408SDan Williams 	for_each_mapped_pfn(entry, pfn) {
3845fac7408SDan Williams 		struct page *page = pfn_to_page(pfn);
3855fac7408SDan Williams 
3865fac7408SDan Williams 		if (page_ref_count(page) > 1)
3875fac7408SDan Williams 			return page;
3885fac7408SDan Williams 	}
3895fac7408SDan Williams 	return NULL;
3905fac7408SDan Williams }
3915fac7408SDan Williams 
392c5bbd451SMatthew Wilcox /*
393c2e8021aSShiyang Ruan  * dax_lock_page - Lock the DAX entry corresponding to a page
394c5bbd451SMatthew Wilcox  * @page: The page whose entry we want to lock
395c5bbd451SMatthew Wilcox  *
396c5bbd451SMatthew Wilcox  * Context: Process context.
39727359fd6SMatthew Wilcox  * Return: A cookie to pass to dax_unlock_page() or 0 if the entry could
39827359fd6SMatthew Wilcox  * not be locked.
399c5bbd451SMatthew Wilcox  */
40027359fd6SMatthew Wilcox dax_entry_t dax_lock_page(struct page *page)
401c2a7d2a1SDan Williams {
4029f32d221SMatthew Wilcox 	XA_STATE(xas, NULL, 0);
4039f32d221SMatthew Wilcox 	void *entry;
404c2a7d2a1SDan Williams 
405c5bbd451SMatthew Wilcox 	/* Ensure page->mapping isn't freed while we look at it */
406c5bbd451SMatthew Wilcox 	rcu_read_lock();
407c2a7d2a1SDan Williams 	for (;;) {
4089f32d221SMatthew Wilcox 		struct address_space *mapping = READ_ONCE(page->mapping);
409c2a7d2a1SDan Williams 
41027359fd6SMatthew Wilcox 		entry = NULL;
411c93db7bbSMatthew Wilcox 		if (!mapping || !dax_mapping(mapping))
412c5bbd451SMatthew Wilcox 			break;
413c2a7d2a1SDan Williams 
414c2a7d2a1SDan Williams 		/*
415c2a7d2a1SDan Williams 		 * In the device-dax case there's no need to lock, a
416c2a7d2a1SDan Williams 		 * struct dev_pagemap pin is sufficient to keep the
417c2a7d2a1SDan Williams 		 * inode alive, and we assume we have dev_pagemap pin
418c2a7d2a1SDan Williams 		 * otherwise we would not have a valid pfn_to_page()
419c2a7d2a1SDan Williams 		 * translation.
420c2a7d2a1SDan Williams 		 */
42127359fd6SMatthew Wilcox 		entry = (void *)~0UL;
4229f32d221SMatthew Wilcox 		if (S_ISCHR(mapping->host->i_mode))
423c5bbd451SMatthew Wilcox 			break;
424c2a7d2a1SDan Williams 
4259f32d221SMatthew Wilcox 		xas.xa = &mapping->i_pages;
4269f32d221SMatthew Wilcox 		xas_lock_irq(&xas);
427c2a7d2a1SDan Williams 		if (mapping != page->mapping) {
4289f32d221SMatthew Wilcox 			xas_unlock_irq(&xas);
429c2a7d2a1SDan Williams 			continue;
430c2a7d2a1SDan Williams 		}
4319f32d221SMatthew Wilcox 		xas_set(&xas, page->index);
4329f32d221SMatthew Wilcox 		entry = xas_load(&xas);
4339f32d221SMatthew Wilcox 		if (dax_is_locked(entry)) {
434c5bbd451SMatthew Wilcox 			rcu_read_unlock();
43555e56f06SMatthew Wilcox 			wait_entry_unlocked(&xas, entry);
436c5bbd451SMatthew Wilcox 			rcu_read_lock();
437c2a7d2a1SDan Williams 			continue;
438c2a7d2a1SDan Williams 		}
4399f32d221SMatthew Wilcox 		dax_lock_entry(&xas, entry);
4409f32d221SMatthew Wilcox 		xas_unlock_irq(&xas);
441c5bbd451SMatthew Wilcox 		break;
4429f32d221SMatthew Wilcox 	}
443c5bbd451SMatthew Wilcox 	rcu_read_unlock();
44427359fd6SMatthew Wilcox 	return (dax_entry_t)entry;
445c2a7d2a1SDan Williams }
446c2a7d2a1SDan Williams 
44727359fd6SMatthew Wilcox void dax_unlock_page(struct page *page, dax_entry_t cookie)
448c2a7d2a1SDan Williams {
449c2a7d2a1SDan Williams 	struct address_space *mapping = page->mapping;
4509f32d221SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, page->index);
451c2a7d2a1SDan Williams 
4529f32d221SMatthew Wilcox 	if (S_ISCHR(mapping->host->i_mode))
453c2a7d2a1SDan Williams 		return;
454c2a7d2a1SDan Williams 
45527359fd6SMatthew Wilcox 	dax_unlock_entry(&xas, (void *)cookie);
456c2a7d2a1SDan Williams }
457c2a7d2a1SDan Williams 
458ac401cc7SJan Kara /*
459a77d19f4SMatthew Wilcox  * Find page cache entry at given index. If it is a DAX entry, return it
460a77d19f4SMatthew Wilcox  * with the entry locked. If the page cache doesn't contain an entry at
461a77d19f4SMatthew Wilcox  * that index, add a locked empty entry.
462ac401cc7SJan Kara  *
4633159f943SMatthew Wilcox  * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
464b15cd800SMatthew Wilcox  * either return that locked entry or will return VM_FAULT_FALLBACK.
465b15cd800SMatthew Wilcox  * This will happen if there are any PTE entries within the PMD range
466b15cd800SMatthew Wilcox  * that we are requesting.
467642261acSRoss Zwisler  *
468b15cd800SMatthew Wilcox  * We always favor PTE entries over PMD entries. There isn't a flow where we
469b15cd800SMatthew Wilcox  * evict PTE entries in order to 'upgrade' them to a PMD entry.  A PMD
470b15cd800SMatthew Wilcox  * insertion will fail if it finds any PTE entries already in the tree, and a
471b15cd800SMatthew Wilcox  * PTE insertion will cause an existing PMD entry to be unmapped and
472b15cd800SMatthew Wilcox  * downgraded to PTE entries.  This happens for both PMD zero pages as
473b15cd800SMatthew Wilcox  * well as PMD empty entries.
474642261acSRoss Zwisler  *
475b15cd800SMatthew Wilcox  * The exception to this downgrade path is for PMD entries that have
476b15cd800SMatthew Wilcox  * real storage backing them.  We will leave these real PMD entries in
477b15cd800SMatthew Wilcox  * the tree, and PTE writes will simply dirty the entire PMD entry.
478642261acSRoss Zwisler  *
479ac401cc7SJan Kara  * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
480ac401cc7SJan Kara  * persistent memory the benefit is doubtful. We can add that later if we can
481ac401cc7SJan Kara  * show it helps.
482b15cd800SMatthew Wilcox  *
483b15cd800SMatthew Wilcox  * On error, this function does not return an ERR_PTR.  Instead it returns
484b15cd800SMatthew Wilcox  * a VM_FAULT code, encoded as an xarray internal entry.  The ERR_PTR values
485b15cd800SMatthew Wilcox  * overlap with xarray value entries.
486ac401cc7SJan Kara  */
487b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas,
48823c84eb7SMatthew Wilcox (Oracle) 		struct address_space *mapping, unsigned int order)
489ac401cc7SJan Kara {
490b15cd800SMatthew Wilcox 	unsigned long index = xas->xa_index;
4911a14e377SJan Kara 	bool pmd_downgrade;	/* splitting PMD entry into PTE entries? */
492b15cd800SMatthew Wilcox 	void *entry;
493ac401cc7SJan Kara 
494b15cd800SMatthew Wilcox retry:
4951a14e377SJan Kara 	pmd_downgrade = false;
496b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
49723c84eb7SMatthew Wilcox (Oracle) 	entry = get_unlocked_entry(xas, order);
498642261acSRoss Zwisler 
499b15cd800SMatthew Wilcox 	if (entry) {
50023c84eb7SMatthew Wilcox (Oracle) 		if (dax_is_conflict(entry))
50123c84eb7SMatthew Wilcox (Oracle) 			goto fallback;
5020e40de03SMatthew Wilcox 		if (!xa_is_value(entry)) {
50349688e65SHao Li 			xas_set_err(xas, -EIO);
50491d25ba8SRoss Zwisler 			goto out_unlock;
50591d25ba8SRoss Zwisler 		}
50691d25ba8SRoss Zwisler 
50723c84eb7SMatthew Wilcox (Oracle) 		if (order == 0) {
50891d25ba8SRoss Zwisler 			if (dax_is_pmd_entry(entry) &&
509642261acSRoss Zwisler 			    (dax_is_zero_entry(entry) ||
510642261acSRoss Zwisler 			     dax_is_empty_entry(entry))) {
511642261acSRoss Zwisler 				pmd_downgrade = true;
512642261acSRoss Zwisler 			}
513642261acSRoss Zwisler 		}
514642261acSRoss Zwisler 	}
515642261acSRoss Zwisler 
516642261acSRoss Zwisler 	if (pmd_downgrade) {
517642261acSRoss Zwisler 		/*
518642261acSRoss Zwisler 		 * Make sure 'entry' remains valid while we drop
519b93b0163SMatthew Wilcox 		 * the i_pages lock.
520642261acSRoss Zwisler 		 */
521b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
522642261acSRoss Zwisler 
523642261acSRoss Zwisler 		/*
524642261acSRoss Zwisler 		 * Besides huge zero pages the only other thing that gets
525642261acSRoss Zwisler 		 * downgraded are empty entries which don't need to be
526642261acSRoss Zwisler 		 * unmapped.
527642261acSRoss Zwisler 		 */
528b15cd800SMatthew Wilcox 		if (dax_is_zero_entry(entry)) {
529b15cd800SMatthew Wilcox 			xas_unlock_irq(xas);
530b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping,
531b15cd800SMatthew Wilcox 					xas->xa_index & ~PG_PMD_COLOUR,
532977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
533b15cd800SMatthew Wilcox 			xas_reset(xas);
534b15cd800SMatthew Wilcox 			xas_lock_irq(xas);
535e11f8b7bSRoss Zwisler 		}
536e11f8b7bSRoss Zwisler 
537d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
538b15cd800SMatthew Wilcox 		xas_store(xas, NULL);	/* undo the PMD join */
539698ab77aSVivek Goyal 		dax_wake_entry(xas, entry, WAKE_ALL);
5407f0e07fbSMatthew Wilcox (Oracle) 		mapping->nrpages -= PG_PMD_NR;
541b15cd800SMatthew Wilcox 		entry = NULL;
542b15cd800SMatthew Wilcox 		xas_set(xas, index);
543642261acSRoss Zwisler 	}
544642261acSRoss Zwisler 
545b15cd800SMatthew Wilcox 	if (entry) {
546b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
547b15cd800SMatthew Wilcox 	} else {
54823c84eb7SMatthew Wilcox (Oracle) 		unsigned long flags = DAX_EMPTY;
54923c84eb7SMatthew Wilcox (Oracle) 
55023c84eb7SMatthew Wilcox (Oracle) 		if (order > 0)
55123c84eb7SMatthew Wilcox (Oracle) 			flags |= DAX_PMD;
55223c84eb7SMatthew Wilcox (Oracle) 		entry = dax_make_entry(pfn_to_pfn_t(0), flags);
553b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
554b15cd800SMatthew Wilcox 		if (xas_error(xas))
555b15cd800SMatthew Wilcox 			goto out_unlock;
5567f0e07fbSMatthew Wilcox (Oracle) 		mapping->nrpages += 1UL << order;
557ac401cc7SJan Kara 	}
558b15cd800SMatthew Wilcox 
559642261acSRoss Zwisler out_unlock:
560b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
561b15cd800SMatthew Wilcox 	if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
562b15cd800SMatthew Wilcox 		goto retry;
563b15cd800SMatthew Wilcox 	if (xas->xa_node == XA_ERROR(-ENOMEM))
564b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_OOM);
565b15cd800SMatthew Wilcox 	if (xas_error(xas))
566b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_SIGBUS);
567e3ad61c6SRoss Zwisler 	return entry;
568b15cd800SMatthew Wilcox fallback:
569b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
570b15cd800SMatthew Wilcox 	return xa_mk_internal(VM_FAULT_FALLBACK);
571ac401cc7SJan Kara }
572ac401cc7SJan Kara 
5735fac7408SDan Williams /**
5746bbdd563SVivek Goyal  * dax_layout_busy_page_range - find first pinned page in @mapping
5755fac7408SDan Williams  * @mapping: address space to scan for a page with ref count > 1
5766bbdd563SVivek Goyal  * @start: Starting offset. Page containing 'start' is included.
5776bbdd563SVivek Goyal  * @end: End offset. Page containing 'end' is included. If 'end' is LLONG_MAX,
5786bbdd563SVivek Goyal  *       pages from 'start' till the end of file are included.
5795fac7408SDan Williams  *
5805fac7408SDan Williams  * DAX requires ZONE_DEVICE mapped pages. These pages are never
5815fac7408SDan Williams  * 'onlined' to the page allocator so they are considered idle when
5825fac7408SDan Williams  * page->count == 1. A filesystem uses this interface to determine if
5835fac7408SDan Williams  * any page in the mapping is busy, i.e. for DMA, or other
5845fac7408SDan Williams  * get_user_pages() usages.
5855fac7408SDan Williams  *
5865fac7408SDan Williams  * It is expected that the filesystem is holding locks to block the
5875fac7408SDan Williams  * establishment of new mappings in this address_space. I.e. it expects
5885fac7408SDan Williams  * to be able to run unmap_mapping_range() and subsequently not race
5895fac7408SDan Williams  * mapping_mapped() becoming true.
5905fac7408SDan Williams  */
5916bbdd563SVivek Goyal struct page *dax_layout_busy_page_range(struct address_space *mapping,
5926bbdd563SVivek Goyal 					loff_t start, loff_t end)
5935fac7408SDan Williams {
594084a8990SMatthew Wilcox 	void *entry;
595084a8990SMatthew Wilcox 	unsigned int scanned = 0;
5965fac7408SDan Williams 	struct page *page = NULL;
5976bbdd563SVivek Goyal 	pgoff_t start_idx = start >> PAGE_SHIFT;
5986bbdd563SVivek Goyal 	pgoff_t end_idx;
5996bbdd563SVivek Goyal 	XA_STATE(xas, &mapping->i_pages, start_idx);
6005fac7408SDan Williams 
6015fac7408SDan Williams 	/*
6025fac7408SDan Williams 	 * In the 'limited' case get_user_pages() for dax is disabled.
6035fac7408SDan Williams 	 */
6045fac7408SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
6055fac7408SDan Williams 		return NULL;
6065fac7408SDan Williams 
6075fac7408SDan Williams 	if (!dax_mapping(mapping) || !mapping_mapped(mapping))
6085fac7408SDan Williams 		return NULL;
6095fac7408SDan Williams 
6106bbdd563SVivek Goyal 	/* If end == LLONG_MAX, all pages from start to till end of file */
6116bbdd563SVivek Goyal 	if (end == LLONG_MAX)
6126bbdd563SVivek Goyal 		end_idx = ULONG_MAX;
6136bbdd563SVivek Goyal 	else
6146bbdd563SVivek Goyal 		end_idx = end >> PAGE_SHIFT;
6155fac7408SDan Williams 	/*
6165fac7408SDan Williams 	 * If we race get_user_pages_fast() here either we'll see the
617084a8990SMatthew Wilcox 	 * elevated page count in the iteration and wait, or
6185fac7408SDan Williams 	 * get_user_pages_fast() will see that the page it took a reference
6195fac7408SDan Williams 	 * against is no longer mapped in the page tables and bail to the
6205fac7408SDan Williams 	 * get_user_pages() slow path.  The slow path is protected by
6215fac7408SDan Williams 	 * pte_lock() and pmd_lock(). New references are not taken without
6226bbdd563SVivek Goyal 	 * holding those locks, and unmap_mapping_pages() will not zero the
6235fac7408SDan Williams 	 * pte or pmd without holding the respective lock, so we are
6245fac7408SDan Williams 	 * guaranteed to either see new references or prevent new
6255fac7408SDan Williams 	 * references from being established.
6265fac7408SDan Williams 	 */
6276bbdd563SVivek Goyal 	unmap_mapping_pages(mapping, start_idx, end_idx - start_idx + 1, 0);
6285fac7408SDan Williams 
629084a8990SMatthew Wilcox 	xas_lock_irq(&xas);
6306bbdd563SVivek Goyal 	xas_for_each(&xas, entry, end_idx) {
631084a8990SMatthew Wilcox 		if (WARN_ON_ONCE(!xa_is_value(entry)))
6325fac7408SDan Williams 			continue;
633084a8990SMatthew Wilcox 		if (unlikely(dax_is_locked(entry)))
63423c84eb7SMatthew Wilcox (Oracle) 			entry = get_unlocked_entry(&xas, 0);
6355fac7408SDan Williams 		if (entry)
6365fac7408SDan Williams 			page = dax_busy_page(entry);
6374c3d043dSVivek Goyal 		put_unlocked_entry(&xas, entry, WAKE_NEXT);
6385fac7408SDan Williams 		if (page)
6395fac7408SDan Williams 			break;
640084a8990SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
641084a8990SMatthew Wilcox 			continue;
642cdbf8897SRoss Zwisler 
643084a8990SMatthew Wilcox 		xas_pause(&xas);
644084a8990SMatthew Wilcox 		xas_unlock_irq(&xas);
645084a8990SMatthew Wilcox 		cond_resched();
646084a8990SMatthew Wilcox 		xas_lock_irq(&xas);
6475fac7408SDan Williams 	}
648084a8990SMatthew Wilcox 	xas_unlock_irq(&xas);
6495fac7408SDan Williams 	return page;
6505fac7408SDan Williams }
6516bbdd563SVivek Goyal EXPORT_SYMBOL_GPL(dax_layout_busy_page_range);
6526bbdd563SVivek Goyal 
6536bbdd563SVivek Goyal struct page *dax_layout_busy_page(struct address_space *mapping)
6546bbdd563SVivek Goyal {
6556bbdd563SVivek Goyal 	return dax_layout_busy_page_range(mapping, 0, LLONG_MAX);
6566bbdd563SVivek Goyal }
6575fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page);
6585fac7408SDan Williams 
659a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping,
660c6dcf52cSJan Kara 					  pgoff_t index, bool trunc)
661c6dcf52cSJan Kara {
66207f2d89cSMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
663c6dcf52cSJan Kara 	int ret = 0;
664c6dcf52cSJan Kara 	void *entry;
665c6dcf52cSJan Kara 
66607f2d89cSMatthew Wilcox 	xas_lock_irq(&xas);
66723c84eb7SMatthew Wilcox (Oracle) 	entry = get_unlocked_entry(&xas, 0);
6683159f943SMatthew Wilcox 	if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
669c6dcf52cSJan Kara 		goto out;
670c6dcf52cSJan Kara 	if (!trunc &&
67107f2d89cSMatthew Wilcox 	    (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
67207f2d89cSMatthew Wilcox 	     xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
673c6dcf52cSJan Kara 		goto out;
674d2c997c0SDan Williams 	dax_disassociate_entry(entry, mapping, trunc);
67507f2d89cSMatthew Wilcox 	xas_store(&xas, NULL);
6767f0e07fbSMatthew Wilcox (Oracle) 	mapping->nrpages -= 1UL << dax_entry_order(entry);
677c6dcf52cSJan Kara 	ret = 1;
678c6dcf52cSJan Kara out:
67923738832SVivek Goyal 	put_unlocked_entry(&xas, entry, WAKE_ALL);
68007f2d89cSMatthew Wilcox 	xas_unlock_irq(&xas);
681c6dcf52cSJan Kara 	return ret;
682c6dcf52cSJan Kara }
68307f2d89cSMatthew Wilcox 
684ac401cc7SJan Kara /*
6853159f943SMatthew Wilcox  * Delete DAX entry at @index from @mapping.  Wait for it
6863159f943SMatthew Wilcox  * to be unlocked before deleting it.
687ac401cc7SJan Kara  */
688ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
689ac401cc7SJan Kara {
690a77d19f4SMatthew Wilcox 	int ret = __dax_invalidate_entry(mapping, index, true);
691ac401cc7SJan Kara 
692ac401cc7SJan Kara 	/*
693ac401cc7SJan Kara 	 * This gets called from truncate / punch_hole path. As such, the caller
694ac401cc7SJan Kara 	 * must hold locks protecting against concurrent modifications of the
695a77d19f4SMatthew Wilcox 	 * page cache (usually fs-private i_mmap_sem for writing). Since the
6963159f943SMatthew Wilcox 	 * caller has seen a DAX entry for this index, we better find it
697ac401cc7SJan Kara 	 * at that index as well...
698ac401cc7SJan Kara 	 */
699c6dcf52cSJan Kara 	WARN_ON_ONCE(!ret);
700c6dcf52cSJan Kara 	return ret;
701ac401cc7SJan Kara }
702ac401cc7SJan Kara 
703c6dcf52cSJan Kara /*
7043159f943SMatthew Wilcox  * Invalidate DAX entry if it is clean.
705c6dcf52cSJan Kara  */
706c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
707c6dcf52cSJan Kara 				      pgoff_t index)
708c6dcf52cSJan Kara {
709a77d19f4SMatthew Wilcox 	return __dax_invalidate_entry(mapping, index, false);
710ac401cc7SJan Kara }
711ac401cc7SJan Kara 
71260696eb2SChristoph Hellwig static pgoff_t dax_iomap_pgoff(const struct iomap *iomap, loff_t pos)
713f7ca90b1SMatthew Wilcox {
714de205114SChristoph Hellwig 	return PHYS_PFN(iomap->addr + (pos & PAGE_MASK) - iomap->offset);
715429f8de7SChristoph Hellwig }
716429f8de7SChristoph Hellwig 
717429f8de7SChristoph Hellwig static int copy_cow_page_dax(struct vm_fault *vmf, const struct iomap_iter *iter)
718429f8de7SChristoph Hellwig {
71960696eb2SChristoph Hellwig 	pgoff_t pgoff = dax_iomap_pgoff(&iter->iomap, iter->pos);
720cccbce67SDan Williams 	void *vto, *kaddr;
721cccbce67SDan Williams 	long rc;
722cccbce67SDan Williams 	int id;
723e2e05394SRoss Zwisler 
724cccbce67SDan Williams 	id = dax_read_lock();
725429f8de7SChristoph Hellwig 	rc = dax_direct_access(iter->iomap.dax_dev, pgoff, 1, &kaddr, NULL);
726cccbce67SDan Williams 	if (rc < 0) {
727cccbce67SDan Williams 		dax_read_unlock(id);
728cccbce67SDan Williams 		return rc;
729cccbce67SDan Williams 	}
730429f8de7SChristoph Hellwig 	vto = kmap_atomic(vmf->cow_page);
731429f8de7SChristoph Hellwig 	copy_user_page(vto, kaddr, vmf->address, vmf->cow_page);
732f7ca90b1SMatthew Wilcox 	kunmap_atomic(vto);
733cccbce67SDan Williams 	dax_read_unlock(id);
734f7ca90b1SMatthew Wilcox 	return 0;
735f7ca90b1SMatthew Wilcox }
736f7ca90b1SMatthew Wilcox 
737642261acSRoss Zwisler /*
738642261acSRoss Zwisler  * By this point grab_mapping_entry() has ensured that we have a locked entry
739642261acSRoss Zwisler  * of the appropriate size so we don't have to worry about downgrading PMDs to
740642261acSRoss Zwisler  * PTEs.  If we happen to be trying to insert a PTE and there is a PMD
741642261acSRoss Zwisler  * already in the tree, we will skip the insertion and just dirty the PMD as
742642261acSRoss Zwisler  * appropriate.
743642261acSRoss Zwisler  */
744b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas,
745b15cd800SMatthew Wilcox 		struct address_space *mapping, struct vm_fault *vmf,
746b15cd800SMatthew Wilcox 		void *entry, pfn_t pfn, unsigned long flags, bool dirty)
7479973c98eSRoss Zwisler {
748b15cd800SMatthew Wilcox 	void *new_entry = dax_make_entry(pfn, flags);
7499973c98eSRoss Zwisler 
750f5b7b748SJan Kara 	if (dirty)
7519973c98eSRoss Zwisler 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
7529973c98eSRoss Zwisler 
7533159f943SMatthew Wilcox 	if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
754b15cd800SMatthew Wilcox 		unsigned long index = xas->xa_index;
75591d25ba8SRoss Zwisler 		/* we are replacing a zero page with block mapping */
75691d25ba8SRoss Zwisler 		if (dax_is_pmd_entry(entry))
757977fbdcdSMatthew Wilcox 			unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
758977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
75991d25ba8SRoss Zwisler 		else /* pte entry */
760b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping, index, 1, false);
761ac401cc7SJan Kara 	}
7629973c98eSRoss Zwisler 
763b15cd800SMatthew Wilcox 	xas_reset(xas);
764b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
7651571c029SJan Kara 	if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
7661571c029SJan Kara 		void *old;
7671571c029SJan Kara 
768d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
76973449dafSDan Williams 		dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
770642261acSRoss Zwisler 		/*
771a77d19f4SMatthew Wilcox 		 * Only swap our new entry into the page cache if the current
772642261acSRoss Zwisler 		 * entry is a zero page or an empty entry.  If a normal PTE or
773a77d19f4SMatthew Wilcox 		 * PMD entry is already in the cache, we leave it alone.  This
774642261acSRoss Zwisler 		 * means that if we are trying to insert a PTE and the
775642261acSRoss Zwisler 		 * existing entry is a PMD, we will just leave the PMD in the
776642261acSRoss Zwisler 		 * tree and dirty it if necessary.
777642261acSRoss Zwisler 		 */
7781571c029SJan Kara 		old = dax_lock_entry(xas, new_entry);
779b15cd800SMatthew Wilcox 		WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
780b15cd800SMatthew Wilcox 					DAX_LOCKED));
78191d25ba8SRoss Zwisler 		entry = new_entry;
782b15cd800SMatthew Wilcox 	} else {
783b15cd800SMatthew Wilcox 		xas_load(xas);	/* Walk the xa_state */
784ac401cc7SJan Kara 	}
78591d25ba8SRoss Zwisler 
786f5b7b748SJan Kara 	if (dirty)
787b15cd800SMatthew Wilcox 		xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
78891d25ba8SRoss Zwisler 
789b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
79091d25ba8SRoss Zwisler 	return entry;
7919973c98eSRoss Zwisler }
7929973c98eSRoss Zwisler 
7939fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
7949fc747f6SMatthew Wilcox 		struct address_space *mapping, void *entry)
7959973c98eSRoss Zwisler {
796*06083a09SMuchun Song 	unsigned long pfn, index, count, end;
7973fe0791cSDan Williams 	long ret = 0;
798*06083a09SMuchun Song 	struct vm_area_struct *vma;
7999973c98eSRoss Zwisler 
8009973c98eSRoss Zwisler 	/*
801a6abc2c0SJan Kara 	 * A page got tagged dirty in DAX mapping? Something is seriously
802a6abc2c0SJan Kara 	 * wrong.
8039973c98eSRoss Zwisler 	 */
8043159f943SMatthew Wilcox 	if (WARN_ON(!xa_is_value(entry)))
805a6abc2c0SJan Kara 		return -EIO;
8069973c98eSRoss Zwisler 
8079fc747f6SMatthew Wilcox 	if (unlikely(dax_is_locked(entry))) {
8089fc747f6SMatthew Wilcox 		void *old_entry = entry;
8099fc747f6SMatthew Wilcox 
81023c84eb7SMatthew Wilcox (Oracle) 		entry = get_unlocked_entry(xas, 0);
8119fc747f6SMatthew Wilcox 
812a6abc2c0SJan Kara 		/* Entry got punched out / reallocated? */
8139fc747f6SMatthew Wilcox 		if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
814a6abc2c0SJan Kara 			goto put_unlocked;
815a6abc2c0SJan Kara 		/*
8169fc747f6SMatthew Wilcox 		 * Entry got reallocated elsewhere? No need to writeback.
8179fc747f6SMatthew Wilcox 		 * We have to compare pfns as we must not bail out due to
8189fc747f6SMatthew Wilcox 		 * difference in lockbit or entry type.
819a6abc2c0SJan Kara 		 */
8209fc747f6SMatthew Wilcox 		if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
821a6abc2c0SJan Kara 			goto put_unlocked;
822642261acSRoss Zwisler 		if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
823642261acSRoss Zwisler 					dax_is_zero_entry(entry))) {
8249973c98eSRoss Zwisler 			ret = -EIO;
825a6abc2c0SJan Kara 			goto put_unlocked;
8269973c98eSRoss Zwisler 		}
8279973c98eSRoss Zwisler 
8289fc747f6SMatthew Wilcox 		/* Another fsync thread may have already done this entry */
8299fc747f6SMatthew Wilcox 		if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
830a6abc2c0SJan Kara 			goto put_unlocked;
8319fc747f6SMatthew Wilcox 	}
8329fc747f6SMatthew Wilcox 
833a6abc2c0SJan Kara 	/* Lock the entry to serialize with page faults */
8349fc747f6SMatthew Wilcox 	dax_lock_entry(xas, entry);
8359fc747f6SMatthew Wilcox 
836a6abc2c0SJan Kara 	/*
837a6abc2c0SJan Kara 	 * We can clear the tag now but we have to be careful so that concurrent
838a6abc2c0SJan Kara 	 * dax_writeback_one() calls for the same index cannot finish before we
839a6abc2c0SJan Kara 	 * actually flush the caches. This is achieved as the calls will look
840b93b0163SMatthew Wilcox 	 * at the entry only under the i_pages lock and once they do that
841b93b0163SMatthew Wilcox 	 * they will see the entry locked and wait for it to unlock.
842a6abc2c0SJan Kara 	 */
8439fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
8449fc747f6SMatthew Wilcox 	xas_unlock_irq(xas);
845a6abc2c0SJan Kara 
846642261acSRoss Zwisler 	/*
847e4b3448bSMatthew Wilcox 	 * If dax_writeback_mapping_range() was given a wbc->range_start
848e4b3448bSMatthew Wilcox 	 * in the middle of a PMD, the 'index' we use needs to be
849e4b3448bSMatthew Wilcox 	 * aligned to the start of the PMD.
8503fe0791cSDan Williams 	 * This allows us to flush for PMD_SIZE and not have to worry about
8513fe0791cSDan Williams 	 * partial PMD writebacks.
852642261acSRoss Zwisler 	 */
853a77d19f4SMatthew Wilcox 	pfn = dax_to_pfn(entry);
854e4b3448bSMatthew Wilcox 	count = 1UL << dax_entry_order(entry);
855e4b3448bSMatthew Wilcox 	index = xas->xa_index & ~(count - 1);
856*06083a09SMuchun Song 	end = index + count - 1;
857cccbce67SDan Williams 
858*06083a09SMuchun Song 	/* Walk all mappings of a given index of a file and writeprotect them */
859*06083a09SMuchun Song 	i_mmap_lock_read(mapping);
860*06083a09SMuchun Song 	vma_interval_tree_foreach(vma, &mapping->i_mmap, index, end) {
861*06083a09SMuchun Song 		pfn_mkclean_range(pfn, count, index, vma);
862*06083a09SMuchun Song 		cond_resched();
863*06083a09SMuchun Song 	}
864*06083a09SMuchun Song 	i_mmap_unlock_read(mapping);
865*06083a09SMuchun Song 
866e4b3448bSMatthew Wilcox 	dax_flush(dax_dev, page_address(pfn_to_page(pfn)), count * PAGE_SIZE);
8674b4bb46dSJan Kara 	/*
8684b4bb46dSJan Kara 	 * After we have flushed the cache, we can clear the dirty tag. There
8694b4bb46dSJan Kara 	 * cannot be new dirty data in the pfn after the flush has completed as
8704b4bb46dSJan Kara 	 * the pfn mappings are writeprotected and fault waits for mapping
8714b4bb46dSJan Kara 	 * entry lock.
8724b4bb46dSJan Kara 	 */
8739fc747f6SMatthew Wilcox 	xas_reset(xas);
8749fc747f6SMatthew Wilcox 	xas_lock_irq(xas);
8759fc747f6SMatthew Wilcox 	xas_store(xas, entry);
8769fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
877698ab77aSVivek Goyal 	dax_wake_entry(xas, entry, WAKE_NEXT);
8789fc747f6SMatthew Wilcox 
879e4b3448bSMatthew Wilcox 	trace_dax_writeback_one(mapping->host, index, count);
8809973c98eSRoss Zwisler 	return ret;
8819973c98eSRoss Zwisler 
882a6abc2c0SJan Kara  put_unlocked:
8834c3d043dSVivek Goyal 	put_unlocked_entry(xas, entry, WAKE_NEXT);
8849973c98eSRoss Zwisler 	return ret;
8859973c98eSRoss Zwisler }
8869973c98eSRoss Zwisler 
8879973c98eSRoss Zwisler /*
8889973c98eSRoss Zwisler  * Flush the mapping to the persistent domain within the byte range of [start,
8899973c98eSRoss Zwisler  * end]. This is required by data integrity operations to ensure file data is
8909973c98eSRoss Zwisler  * on persistent storage prior to completion of the operation.
8919973c98eSRoss Zwisler  */
8927f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping,
8933f666c56SVivek Goyal 		struct dax_device *dax_dev, struct writeback_control *wbc)
8949973c98eSRoss Zwisler {
8959fc747f6SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
8969973c98eSRoss Zwisler 	struct inode *inode = mapping->host;
8979fc747f6SMatthew Wilcox 	pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
8989fc747f6SMatthew Wilcox 	void *entry;
8999fc747f6SMatthew Wilcox 	int ret = 0;
9009fc747f6SMatthew Wilcox 	unsigned int scanned = 0;
9019973c98eSRoss Zwisler 
9029973c98eSRoss Zwisler 	if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
9039973c98eSRoss Zwisler 		return -EIO;
9049973c98eSRoss Zwisler 
9057716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping) || wbc->sync_mode != WB_SYNC_ALL)
9067f6d5b52SRoss Zwisler 		return 0;
9077f6d5b52SRoss Zwisler 
9089fc747f6SMatthew Wilcox 	trace_dax_writeback_range(inode, xas.xa_index, end_index);
9099973c98eSRoss Zwisler 
9109fc747f6SMatthew Wilcox 	tag_pages_for_writeback(mapping, xas.xa_index, end_index);
911d14a3f48SRoss Zwisler 
9129fc747f6SMatthew Wilcox 	xas_lock_irq(&xas);
9139fc747f6SMatthew Wilcox 	xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
9149fc747f6SMatthew Wilcox 		ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
915819ec6b9SJeff Layton 		if (ret < 0) {
916819ec6b9SJeff Layton 			mapping_set_error(mapping, ret);
9179fc747f6SMatthew Wilcox 			break;
918d14a3f48SRoss Zwisler 		}
9199fc747f6SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
9209fc747f6SMatthew Wilcox 			continue;
9219fc747f6SMatthew Wilcox 
9229fc747f6SMatthew Wilcox 		xas_pause(&xas);
9239fc747f6SMatthew Wilcox 		xas_unlock_irq(&xas);
9249fc747f6SMatthew Wilcox 		cond_resched();
9259fc747f6SMatthew Wilcox 		xas_lock_irq(&xas);
926d14a3f48SRoss Zwisler 	}
9279fc747f6SMatthew Wilcox 	xas_unlock_irq(&xas);
9289fc747f6SMatthew Wilcox 	trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
9299fc747f6SMatthew Wilcox 	return ret;
9309973c98eSRoss Zwisler }
9319973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
9329973c98eSRoss Zwisler 
93365dd814aSChristoph Hellwig static int dax_iomap_pfn(const struct iomap *iomap, loff_t pos, size_t size,
9345e161e40SJan Kara 			 pfn_t *pfnp)
9355e161e40SJan Kara {
93660696eb2SChristoph Hellwig 	pgoff_t pgoff = dax_iomap_pgoff(iomap, pos);
9375e161e40SJan Kara 	int id, rc;
9385e161e40SJan Kara 	long length;
9395e161e40SJan Kara 
940cccbce67SDan Williams 	id = dax_read_lock();
9415e161e40SJan Kara 	length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
94286ed913bSHuaisheng Ye 				   NULL, pfnp);
9435e161e40SJan Kara 	if (length < 0) {
9445e161e40SJan Kara 		rc = length;
9455e161e40SJan Kara 		goto out;
9465e161e40SJan Kara 	}
9475e161e40SJan Kara 	rc = -EINVAL;
9485e161e40SJan Kara 	if (PFN_PHYS(length) < size)
9495e161e40SJan Kara 		goto out;
9505e161e40SJan Kara 	if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
9515e161e40SJan Kara 		goto out;
9525e161e40SJan Kara 	/* For larger pages we need devmap */
9535e161e40SJan Kara 	if (length > 1 && !pfn_t_devmap(*pfnp))
9545e161e40SJan Kara 		goto out;
9555e161e40SJan Kara 	rc = 0;
9565e161e40SJan Kara out:
957cccbce67SDan Williams 	dax_read_unlock(id);
958cccbce67SDan Williams 	return rc;
959cccbce67SDan Williams }
960f7ca90b1SMatthew Wilcox 
9612f89dc12SJan Kara /*
96291d25ba8SRoss Zwisler  * The user has performed a load from a hole in the file.  Allocating a new
96391d25ba8SRoss Zwisler  * page in the file would cause excessive storage usage for workloads with
96491d25ba8SRoss Zwisler  * sparse files.  Instead we insert a read-only mapping of the 4k zero page.
96591d25ba8SRoss Zwisler  * If this page is ever written to we will re-fault and change the mapping to
96691d25ba8SRoss Zwisler  * point to real DAX storage instead.
9672f89dc12SJan Kara  */
968b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas,
969b15cd800SMatthew Wilcox 		struct address_space *mapping, void **entry,
970e30331ffSRoss Zwisler 		struct vm_fault *vmf)
971e30331ffSRoss Zwisler {
972e30331ffSRoss Zwisler 	struct inode *inode = mapping->host;
97391d25ba8SRoss Zwisler 	unsigned long vaddr = vmf->address;
974b90ca5ccSMatthew Wilcox 	pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
975b90ca5ccSMatthew Wilcox 	vm_fault_t ret;
976e30331ffSRoss Zwisler 
977b15cd800SMatthew Wilcox 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
9783159f943SMatthew Wilcox 			DAX_ZERO_PAGE, false);
9793159f943SMatthew Wilcox 
980ab77dab4SSouptick Joarder 	ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
981e30331ffSRoss Zwisler 	trace_dax_load_hole(inode, vmf, ret);
982e30331ffSRoss Zwisler 	return ret;
983e30331ffSRoss Zwisler }
984e30331ffSRoss Zwisler 
985c2436190SShiyang Ruan #ifdef CONFIG_FS_DAX_PMD
986c2436190SShiyang Ruan static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
98765dd814aSChristoph Hellwig 		const struct iomap *iomap, void **entry)
988c2436190SShiyang Ruan {
989c2436190SShiyang Ruan 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
990c2436190SShiyang Ruan 	unsigned long pmd_addr = vmf->address & PMD_MASK;
991c2436190SShiyang Ruan 	struct vm_area_struct *vma = vmf->vma;
992c2436190SShiyang Ruan 	struct inode *inode = mapping->host;
993c2436190SShiyang Ruan 	pgtable_t pgtable = NULL;
994c2436190SShiyang Ruan 	struct page *zero_page;
995c2436190SShiyang Ruan 	spinlock_t *ptl;
996c2436190SShiyang Ruan 	pmd_t pmd_entry;
997c2436190SShiyang Ruan 	pfn_t pfn;
998c2436190SShiyang Ruan 
999c2436190SShiyang Ruan 	zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
1000c2436190SShiyang Ruan 
1001c2436190SShiyang Ruan 	if (unlikely(!zero_page))
1002c2436190SShiyang Ruan 		goto fallback;
1003c2436190SShiyang Ruan 
1004c2436190SShiyang Ruan 	pfn = page_to_pfn_t(zero_page);
1005c2436190SShiyang Ruan 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
1006c2436190SShiyang Ruan 			DAX_PMD | DAX_ZERO_PAGE, false);
1007c2436190SShiyang Ruan 
1008c2436190SShiyang Ruan 	if (arch_needs_pgtable_deposit()) {
1009c2436190SShiyang Ruan 		pgtable = pte_alloc_one(vma->vm_mm);
1010c2436190SShiyang Ruan 		if (!pgtable)
1011c2436190SShiyang Ruan 			return VM_FAULT_OOM;
1012c2436190SShiyang Ruan 	}
1013c2436190SShiyang Ruan 
1014c2436190SShiyang Ruan 	ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1015c2436190SShiyang Ruan 	if (!pmd_none(*(vmf->pmd))) {
1016c2436190SShiyang Ruan 		spin_unlock(ptl);
1017c2436190SShiyang Ruan 		goto fallback;
1018c2436190SShiyang Ruan 	}
1019c2436190SShiyang Ruan 
1020c2436190SShiyang Ruan 	if (pgtable) {
1021c2436190SShiyang Ruan 		pgtable_trans_huge_deposit(vma->vm_mm, vmf->pmd, pgtable);
1022c2436190SShiyang Ruan 		mm_inc_nr_ptes(vma->vm_mm);
1023c2436190SShiyang Ruan 	}
1024c2436190SShiyang Ruan 	pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
1025c2436190SShiyang Ruan 	pmd_entry = pmd_mkhuge(pmd_entry);
1026c2436190SShiyang Ruan 	set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
1027c2436190SShiyang Ruan 	spin_unlock(ptl);
1028c2436190SShiyang Ruan 	trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
1029c2436190SShiyang Ruan 	return VM_FAULT_NOPAGE;
1030c2436190SShiyang Ruan 
1031c2436190SShiyang Ruan fallback:
1032c2436190SShiyang Ruan 	if (pgtable)
1033c2436190SShiyang Ruan 		pte_free(vma->vm_mm, pgtable);
1034c2436190SShiyang Ruan 	trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
1035c2436190SShiyang Ruan 	return VM_FAULT_FALLBACK;
1036c2436190SShiyang Ruan }
1037c2436190SShiyang Ruan #else
1038c2436190SShiyang Ruan static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
103965dd814aSChristoph Hellwig 		const struct iomap *iomap, void **entry)
1040c2436190SShiyang Ruan {
1041c2436190SShiyang Ruan 	return VM_FAULT_FALLBACK;
1042c2436190SShiyang Ruan }
1043c2436190SShiyang Ruan #endif /* CONFIG_FS_DAX_PMD */
1044c2436190SShiyang Ruan 
1045e5c71954SChristoph Hellwig static int dax_memzero(struct dax_device *dax_dev, pgoff_t pgoff,
1046e5c71954SChristoph Hellwig 		unsigned int offset, size_t size)
1047e5c71954SChristoph Hellwig {
1048e5c71954SChristoph Hellwig 	void *kaddr;
1049e5c71954SChristoph Hellwig 	long ret;
1050e5c71954SChristoph Hellwig 
1051e5c71954SChristoph Hellwig 	ret = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
1052e5c71954SChristoph Hellwig 	if (ret > 0) {
1053e5c71954SChristoph Hellwig 		memset(kaddr + offset, 0, size);
1054e5c71954SChristoph Hellwig 		dax_flush(dax_dev, kaddr + offset, size);
1055e5c71954SChristoph Hellwig 	}
1056e5c71954SChristoph Hellwig 	return ret;
1057e5c71954SChristoph Hellwig }
1058e5c71954SChristoph Hellwig 
1059c6f40468SChristoph Hellwig static s64 dax_zero_iter(struct iomap_iter *iter, bool *did_zero)
1060679c8bd3SChristoph Hellwig {
1061c6f40468SChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
1062c6f40468SChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
1063c6f40468SChristoph Hellwig 	loff_t pos = iter->pos;
1064c6f40468SChristoph Hellwig 	u64 length = iomap_length(iter);
1065c6f40468SChristoph Hellwig 	s64 written = 0;
1066c6f40468SChristoph Hellwig 
1067c6f40468SChristoph Hellwig 	/* already zeroed?  we're done. */
1068c6f40468SChristoph Hellwig 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
1069c6f40468SChristoph Hellwig 		return length;
1070c6f40468SChristoph Hellwig 
1071c6f40468SChristoph Hellwig 	do {
107281ee8e52SMatthew Wilcox (Oracle) 		unsigned offset = offset_in_page(pos);
107381ee8e52SMatthew Wilcox (Oracle) 		unsigned size = min_t(u64, PAGE_SIZE - offset, length);
1074c6f40468SChristoph Hellwig 		pgoff_t pgoff = dax_iomap_pgoff(iomap, pos);
1075c6f40468SChristoph Hellwig 		long rc;
1076c6f40468SChristoph Hellwig 		int id;
10770a23f9ffSVivek Goyal 
1078cccbce67SDan Williams 		id = dax_read_lock();
1079e5c71954SChristoph Hellwig 		if (IS_ALIGNED(pos, PAGE_SIZE) && size == PAGE_SIZE)
108081ee8e52SMatthew Wilcox (Oracle) 			rc = dax_zero_page_range(iomap->dax_dev, pgoff, 1);
10810a23f9ffSVivek Goyal 		else
1082e5c71954SChristoph Hellwig 			rc = dax_memzero(iomap->dax_dev, pgoff, offset, size);
1083cccbce67SDan Williams 		dax_read_unlock(id);
10840a23f9ffSVivek Goyal 
1085e5c71954SChristoph Hellwig 		if (rc < 0)
1086e5c71954SChristoph Hellwig 			return rc;
1087c6f40468SChristoph Hellwig 		pos += size;
1088c6f40468SChristoph Hellwig 		length -= size;
1089c6f40468SChristoph Hellwig 		written += size;
1090c6f40468SChristoph Hellwig 		if (did_zero)
1091c6f40468SChristoph Hellwig 			*did_zero = true;
1092c6f40468SChristoph Hellwig 	} while (length > 0);
1093c6f40468SChristoph Hellwig 
1094c6f40468SChristoph Hellwig 	return written;
1095679c8bd3SChristoph Hellwig }
1096679c8bd3SChristoph Hellwig 
1097c6f40468SChristoph Hellwig int dax_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1098c6f40468SChristoph Hellwig 		const struct iomap_ops *ops)
1099c6f40468SChristoph Hellwig {
1100c6f40468SChristoph Hellwig 	struct iomap_iter iter = {
1101c6f40468SChristoph Hellwig 		.inode		= inode,
1102c6f40468SChristoph Hellwig 		.pos		= pos,
1103c6f40468SChristoph Hellwig 		.len		= len,
1104952da063SChristoph Hellwig 		.flags		= IOMAP_DAX | IOMAP_ZERO,
1105c6f40468SChristoph Hellwig 	};
1106c6f40468SChristoph Hellwig 	int ret;
1107c6f40468SChristoph Hellwig 
1108c6f40468SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
1109c6f40468SChristoph Hellwig 		iter.processed = dax_zero_iter(&iter, did_zero);
1110c6f40468SChristoph Hellwig 	return ret;
1111c6f40468SChristoph Hellwig }
1112c6f40468SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_zero_range);
1113c6f40468SChristoph Hellwig 
1114c6f40468SChristoph Hellwig int dax_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1115c6f40468SChristoph Hellwig 		const struct iomap_ops *ops)
1116c6f40468SChristoph Hellwig {
1117c6f40468SChristoph Hellwig 	unsigned int blocksize = i_blocksize(inode);
1118c6f40468SChristoph Hellwig 	unsigned int off = pos & (blocksize - 1);
1119c6f40468SChristoph Hellwig 
1120c6f40468SChristoph Hellwig 	/* Block boundary? Nothing to do */
1121c6f40468SChristoph Hellwig 	if (!off)
1122c6f40468SChristoph Hellwig 		return 0;
1123c6f40468SChristoph Hellwig 	return dax_zero_range(inode, pos, blocksize - off, did_zero, ops);
1124c6f40468SChristoph Hellwig }
1125c6f40468SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_truncate_page);
1126c6f40468SChristoph Hellwig 
1127ca289e0bSChristoph Hellwig static loff_t dax_iomap_iter(const struct iomap_iter *iomi,
1128ca289e0bSChristoph Hellwig 		struct iov_iter *iter)
1129a254e568SChristoph Hellwig {
1130ca289e0bSChristoph Hellwig 	const struct iomap *iomap = &iomi->iomap;
1131ca289e0bSChristoph Hellwig 	loff_t length = iomap_length(iomi);
1132ca289e0bSChristoph Hellwig 	loff_t pos = iomi->pos;
1133cccbce67SDan Williams 	struct dax_device *dax_dev = iomap->dax_dev;
1134a254e568SChristoph Hellwig 	loff_t end = pos + length, done = 0;
1135a254e568SChristoph Hellwig 	ssize_t ret = 0;
1136a77d4786SDan Williams 	size_t xfer;
1137cccbce67SDan Williams 	int id;
1138a254e568SChristoph Hellwig 
1139a254e568SChristoph Hellwig 	if (iov_iter_rw(iter) == READ) {
1140ca289e0bSChristoph Hellwig 		end = min(end, i_size_read(iomi->inode));
1141a254e568SChristoph Hellwig 		if (pos >= end)
1142a254e568SChristoph Hellwig 			return 0;
1143a254e568SChristoph Hellwig 
1144a254e568SChristoph Hellwig 		if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1145a254e568SChristoph Hellwig 			return iov_iter_zero(min(length, end - pos), iter);
1146a254e568SChristoph Hellwig 	}
1147a254e568SChristoph Hellwig 
1148a254e568SChristoph Hellwig 	if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1149a254e568SChristoph Hellwig 		return -EIO;
1150a254e568SChristoph Hellwig 
1151e3fce68cSJan Kara 	/*
1152e3fce68cSJan Kara 	 * Write can allocate block for an area which has a hole page mapped
1153e3fce68cSJan Kara 	 * into page tables. We have to tear down these mappings so that data
1154e3fce68cSJan Kara 	 * written by write(2) is visible in mmap.
1155e3fce68cSJan Kara 	 */
1156cd656375SJan Kara 	if (iomap->flags & IOMAP_F_NEW) {
1157ca289e0bSChristoph Hellwig 		invalidate_inode_pages2_range(iomi->inode->i_mapping,
1158e3fce68cSJan Kara 					      pos >> PAGE_SHIFT,
1159e3fce68cSJan Kara 					      (end - 1) >> PAGE_SHIFT);
1160e3fce68cSJan Kara 	}
1161e3fce68cSJan Kara 
1162cccbce67SDan Williams 	id = dax_read_lock();
1163a254e568SChristoph Hellwig 	while (pos < end) {
1164a254e568SChristoph Hellwig 		unsigned offset = pos & (PAGE_SIZE - 1);
1165cccbce67SDan Williams 		const size_t size = ALIGN(length + offset, PAGE_SIZE);
116660696eb2SChristoph Hellwig 		pgoff_t pgoff = dax_iomap_pgoff(iomap, pos);
1167a254e568SChristoph Hellwig 		ssize_t map_len;
1168cccbce67SDan Williams 		void *kaddr;
1169a254e568SChristoph Hellwig 
1170d1908f52SMichal Hocko 		if (fatal_signal_pending(current)) {
1171d1908f52SMichal Hocko 			ret = -EINTR;
1172d1908f52SMichal Hocko 			break;
1173d1908f52SMichal Hocko 		}
1174d1908f52SMichal Hocko 
1175cccbce67SDan Williams 		map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
117686ed913bSHuaisheng Ye 				&kaddr, NULL);
1177a254e568SChristoph Hellwig 		if (map_len < 0) {
1178a254e568SChristoph Hellwig 			ret = map_len;
1179a254e568SChristoph Hellwig 			break;
1180a254e568SChristoph Hellwig 		}
1181a254e568SChristoph Hellwig 
1182cccbce67SDan Williams 		map_len = PFN_PHYS(map_len);
1183cccbce67SDan Williams 		kaddr += offset;
1184a254e568SChristoph Hellwig 		map_len -= offset;
1185a254e568SChristoph Hellwig 		if (map_len > end - pos)
1186a254e568SChristoph Hellwig 			map_len = end - pos;
1187a254e568SChristoph Hellwig 
1188a254e568SChristoph Hellwig 		if (iov_iter_rw(iter) == WRITE)
1189a77d4786SDan Williams 			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1190fec53774SDan Williams 					map_len, iter);
1191a254e568SChristoph Hellwig 		else
1192a77d4786SDan Williams 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
1193b3a9a0c3SDan Williams 					map_len, iter);
1194a254e568SChristoph Hellwig 
1195a77d4786SDan Williams 		pos += xfer;
1196a77d4786SDan Williams 		length -= xfer;
1197a77d4786SDan Williams 		done += xfer;
1198a77d4786SDan Williams 
1199a77d4786SDan Williams 		if (xfer == 0)
1200a77d4786SDan Williams 			ret = -EFAULT;
1201a77d4786SDan Williams 		if (xfer < map_len)
1202a77d4786SDan Williams 			break;
1203a254e568SChristoph Hellwig 	}
1204cccbce67SDan Williams 	dax_read_unlock(id);
1205a254e568SChristoph Hellwig 
1206a254e568SChristoph Hellwig 	return done ? done : ret;
1207a254e568SChristoph Hellwig }
1208a254e568SChristoph Hellwig 
1209a254e568SChristoph Hellwig /**
121011c59c92SRoss Zwisler  * dax_iomap_rw - Perform I/O to a DAX file
1211a254e568SChristoph Hellwig  * @iocb:	The control block for this I/O
1212a254e568SChristoph Hellwig  * @iter:	The addresses to do I/O from or to
1213a254e568SChristoph Hellwig  * @ops:	iomap ops passed from the file system
1214a254e568SChristoph Hellwig  *
1215a254e568SChristoph Hellwig  * This function performs read and write operations to directly mapped
1216a254e568SChristoph Hellwig  * persistent memory.  The callers needs to take care of read/write exclusion
1217a254e568SChristoph Hellwig  * and evicting any page cache pages in the region under I/O.
1218a254e568SChristoph Hellwig  */
1219a254e568SChristoph Hellwig ssize_t
122011c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
12218ff6daa1SChristoph Hellwig 		const struct iomap_ops *ops)
1222a254e568SChristoph Hellwig {
1223ca289e0bSChristoph Hellwig 	struct iomap_iter iomi = {
1224ca289e0bSChristoph Hellwig 		.inode		= iocb->ki_filp->f_mapping->host,
1225ca289e0bSChristoph Hellwig 		.pos		= iocb->ki_pos,
1226ca289e0bSChristoph Hellwig 		.len		= iov_iter_count(iter),
1227952da063SChristoph Hellwig 		.flags		= IOMAP_DAX,
1228ca289e0bSChristoph Hellwig 	};
1229ca289e0bSChristoph Hellwig 	loff_t done = 0;
1230ca289e0bSChristoph Hellwig 	int ret;
1231a254e568SChristoph Hellwig 
1232168316dbSChristoph Hellwig 	if (iov_iter_rw(iter) == WRITE) {
1233ca289e0bSChristoph Hellwig 		lockdep_assert_held_write(&iomi.inode->i_rwsem);
1234ca289e0bSChristoph Hellwig 		iomi.flags |= IOMAP_WRITE;
1235168316dbSChristoph Hellwig 	} else {
1236ca289e0bSChristoph Hellwig 		lockdep_assert_held(&iomi.inode->i_rwsem);
1237168316dbSChristoph Hellwig 	}
1238a254e568SChristoph Hellwig 
123996222d53SJeff Moyer 	if (iocb->ki_flags & IOCB_NOWAIT)
1240ca289e0bSChristoph Hellwig 		iomi.flags |= IOMAP_NOWAIT;
124196222d53SJeff Moyer 
1242ca289e0bSChristoph Hellwig 	while ((ret = iomap_iter(&iomi, ops)) > 0)
1243ca289e0bSChristoph Hellwig 		iomi.processed = dax_iomap_iter(&iomi, iter);
1244a254e568SChristoph Hellwig 
1245ca289e0bSChristoph Hellwig 	done = iomi.pos - iocb->ki_pos;
1246ca289e0bSChristoph Hellwig 	iocb->ki_pos = iomi.pos;
1247a254e568SChristoph Hellwig 	return done ? done : ret;
1248a254e568SChristoph Hellwig }
124911c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw);
1250a7d73fe6SChristoph Hellwig 
1251ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error)
12529f141d6eSJan Kara {
12539f141d6eSJan Kara 	if (error == 0)
12549f141d6eSJan Kara 		return VM_FAULT_NOPAGE;
1255c9aed74eSSouptick Joarder 	return vmf_error(error);
12569f141d6eSJan Kara }
12579f141d6eSJan Kara 
1258aaa422c4SDan Williams /*
1259aaa422c4SDan Williams  * MAP_SYNC on a dax mapping guarantees dirty metadata is
1260aaa422c4SDan Williams  * flushed on write-faults (non-cow), but not read-faults.
1261aaa422c4SDan Williams  */
1262aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags,
126365dd814aSChristoph Hellwig 		struct vm_area_struct *vma, const struct iomap *iomap)
1264aaa422c4SDan Williams {
1265aaa422c4SDan Williams 	return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1266aaa422c4SDan Williams 		&& (iomap->flags & IOMAP_F_DIRTY);
1267aaa422c4SDan Williams }
1268aaa422c4SDan Williams 
126955f81639SShiyang Ruan /*
127055f81639SShiyang Ruan  * When handling a synchronous page fault and the inode need a fsync, we can
127155f81639SShiyang Ruan  * insert the PTE/PMD into page tables only after that fsync happened. Skip
127255f81639SShiyang Ruan  * insertion for now and return the pfn so that caller can insert it after the
127355f81639SShiyang Ruan  * fsync is done.
127455f81639SShiyang Ruan  */
127555f81639SShiyang Ruan static vm_fault_t dax_fault_synchronous_pfnp(pfn_t *pfnp, pfn_t pfn)
127655f81639SShiyang Ruan {
127755f81639SShiyang Ruan 	if (WARN_ON_ONCE(!pfnp))
127855f81639SShiyang Ruan 		return VM_FAULT_SIGBUS;
127955f81639SShiyang Ruan 	*pfnp = pfn;
128055f81639SShiyang Ruan 	return VM_FAULT_NEEDDSYNC;
128155f81639SShiyang Ruan }
128255f81639SShiyang Ruan 
128365dd814aSChristoph Hellwig static vm_fault_t dax_fault_cow_page(struct vm_fault *vmf,
128465dd814aSChristoph Hellwig 		const struct iomap_iter *iter)
128555f81639SShiyang Ruan {
128655f81639SShiyang Ruan 	vm_fault_t ret;
128755f81639SShiyang Ruan 	int error = 0;
128855f81639SShiyang Ruan 
128965dd814aSChristoph Hellwig 	switch (iter->iomap.type) {
129055f81639SShiyang Ruan 	case IOMAP_HOLE:
129155f81639SShiyang Ruan 	case IOMAP_UNWRITTEN:
1292429f8de7SChristoph Hellwig 		clear_user_highpage(vmf->cow_page, vmf->address);
129355f81639SShiyang Ruan 		break;
129455f81639SShiyang Ruan 	case IOMAP_MAPPED:
1295429f8de7SChristoph Hellwig 		error = copy_cow_page_dax(vmf, iter);
129655f81639SShiyang Ruan 		break;
129755f81639SShiyang Ruan 	default:
129855f81639SShiyang Ruan 		WARN_ON_ONCE(1);
129955f81639SShiyang Ruan 		error = -EIO;
130055f81639SShiyang Ruan 		break;
130155f81639SShiyang Ruan 	}
130255f81639SShiyang Ruan 
130355f81639SShiyang Ruan 	if (error)
130455f81639SShiyang Ruan 		return dax_fault_return(error);
130555f81639SShiyang Ruan 
130655f81639SShiyang Ruan 	__SetPageUptodate(vmf->cow_page);
130755f81639SShiyang Ruan 	ret = finish_fault(vmf);
130855f81639SShiyang Ruan 	if (!ret)
130955f81639SShiyang Ruan 		return VM_FAULT_DONE_COW;
131055f81639SShiyang Ruan 	return ret;
131155f81639SShiyang Ruan }
131255f81639SShiyang Ruan 
1313c2436190SShiyang Ruan /**
131465dd814aSChristoph Hellwig  * dax_fault_iter - Common actor to handle pfn insertion in PTE/PMD fault.
1315c2436190SShiyang Ruan  * @vmf:	vm fault instance
131665dd814aSChristoph Hellwig  * @iter:	iomap iter
1317c2436190SShiyang Ruan  * @pfnp:	pfn to be returned
1318c2436190SShiyang Ruan  * @xas:	the dax mapping tree of a file
1319c2436190SShiyang Ruan  * @entry:	an unlocked dax entry to be inserted
1320c2436190SShiyang Ruan  * @pmd:	distinguish whether it is a pmd fault
1321c2436190SShiyang Ruan  */
132265dd814aSChristoph Hellwig static vm_fault_t dax_fault_iter(struct vm_fault *vmf,
132365dd814aSChristoph Hellwig 		const struct iomap_iter *iter, pfn_t *pfnp,
132465dd814aSChristoph Hellwig 		struct xa_state *xas, void **entry, bool pmd)
1325c2436190SShiyang Ruan {
1326c2436190SShiyang Ruan 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
132765dd814aSChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
1328c2436190SShiyang Ruan 	size_t size = pmd ? PMD_SIZE : PAGE_SIZE;
1329c2436190SShiyang Ruan 	loff_t pos = (loff_t)xas->xa_index << PAGE_SHIFT;
1330c2436190SShiyang Ruan 	bool write = vmf->flags & FAULT_FLAG_WRITE;
133165dd814aSChristoph Hellwig 	bool sync = dax_fault_is_synchronous(iter->flags, vmf->vma, iomap);
1332c2436190SShiyang Ruan 	unsigned long entry_flags = pmd ? DAX_PMD : 0;
1333c2436190SShiyang Ruan 	int err = 0;
1334c2436190SShiyang Ruan 	pfn_t pfn;
1335c2436190SShiyang Ruan 
133665dd814aSChristoph Hellwig 	if (!pmd && vmf->cow_page)
133765dd814aSChristoph Hellwig 		return dax_fault_cow_page(vmf, iter);
133865dd814aSChristoph Hellwig 
1339c2436190SShiyang Ruan 	/* if we are reading UNWRITTEN and HOLE, return a hole. */
1340c2436190SShiyang Ruan 	if (!write &&
1341c2436190SShiyang Ruan 	    (iomap->type == IOMAP_UNWRITTEN || iomap->type == IOMAP_HOLE)) {
1342c2436190SShiyang Ruan 		if (!pmd)
1343c2436190SShiyang Ruan 			return dax_load_hole(xas, mapping, entry, vmf);
1344c2436190SShiyang Ruan 		return dax_pmd_load_hole(xas, vmf, iomap, entry);
1345c2436190SShiyang Ruan 	}
1346c2436190SShiyang Ruan 
1347c2436190SShiyang Ruan 	if (iomap->type != IOMAP_MAPPED) {
1348c2436190SShiyang Ruan 		WARN_ON_ONCE(1);
1349c2436190SShiyang Ruan 		return pmd ? VM_FAULT_FALLBACK : VM_FAULT_SIGBUS;
1350c2436190SShiyang Ruan 	}
1351c2436190SShiyang Ruan 
135265dd814aSChristoph Hellwig 	err = dax_iomap_pfn(&iter->iomap, pos, size, &pfn);
1353c2436190SShiyang Ruan 	if (err)
1354c2436190SShiyang Ruan 		return pmd ? VM_FAULT_FALLBACK : dax_fault_return(err);
1355c2436190SShiyang Ruan 
1356c2436190SShiyang Ruan 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn, entry_flags,
1357c2436190SShiyang Ruan 				  write && !sync);
1358c2436190SShiyang Ruan 
1359c2436190SShiyang Ruan 	if (sync)
1360c2436190SShiyang Ruan 		return dax_fault_synchronous_pfnp(pfnp, pfn);
1361c2436190SShiyang Ruan 
1362c2436190SShiyang Ruan 	/* insert PMD pfn */
1363c2436190SShiyang Ruan 	if (pmd)
1364c2436190SShiyang Ruan 		return vmf_insert_pfn_pmd(vmf, pfn, write);
1365c2436190SShiyang Ruan 
1366c2436190SShiyang Ruan 	/* insert PTE pfn */
1367c2436190SShiyang Ruan 	if (write)
1368c2436190SShiyang Ruan 		return vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
1369c2436190SShiyang Ruan 	return vmf_insert_mixed(vmf->vma, vmf->address, pfn);
1370c2436190SShiyang Ruan }
1371c2436190SShiyang Ruan 
1372ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
1373c0b24625SJan Kara 			       int *iomap_errp, const struct iomap_ops *ops)
1374a7d73fe6SChristoph Hellwig {
137565dd814aSChristoph Hellwig 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1376b15cd800SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
137765dd814aSChristoph Hellwig 	struct iomap_iter iter = {
137865dd814aSChristoph Hellwig 		.inode		= mapping->host,
137965dd814aSChristoph Hellwig 		.pos		= (loff_t)vmf->pgoff << PAGE_SHIFT,
138065dd814aSChristoph Hellwig 		.len		= PAGE_SIZE,
1381952da063SChristoph Hellwig 		.flags		= IOMAP_DAX | IOMAP_FAULT,
138265dd814aSChristoph Hellwig 	};
1383ab77dab4SSouptick Joarder 	vm_fault_t ret = 0;
1384a7d73fe6SChristoph Hellwig 	void *entry;
138565dd814aSChristoph Hellwig 	int error;
1386a7d73fe6SChristoph Hellwig 
138765dd814aSChristoph Hellwig 	trace_dax_pte_fault(iter.inode, vmf, ret);
1388a7d73fe6SChristoph Hellwig 	/*
1389a7d73fe6SChristoph Hellwig 	 * Check whether offset isn't beyond end of file now. Caller is supposed
1390a7d73fe6SChristoph Hellwig 	 * to hold locks serializing us with truncate / punch hole so this is
1391a7d73fe6SChristoph Hellwig 	 * a reliable test.
1392a7d73fe6SChristoph Hellwig 	 */
139365dd814aSChristoph Hellwig 	if (iter.pos >= i_size_read(iter.inode)) {
1394ab77dab4SSouptick Joarder 		ret = VM_FAULT_SIGBUS;
1395a9c42b33SRoss Zwisler 		goto out;
1396a9c42b33SRoss Zwisler 	}
1397a7d73fe6SChristoph Hellwig 
139865dd814aSChristoph Hellwig 	if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
139965dd814aSChristoph Hellwig 		iter.flags |= IOMAP_WRITE;
1400a7d73fe6SChristoph Hellwig 
1401b15cd800SMatthew Wilcox 	entry = grab_mapping_entry(&xas, mapping, 0);
1402b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1403b15cd800SMatthew Wilcox 		ret = xa_to_internal(entry);
140413e451fdSJan Kara 		goto out;
140513e451fdSJan Kara 	}
140613e451fdSJan Kara 
1407a7d73fe6SChristoph Hellwig 	/*
1408e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1409e2093926SRoss Zwisler 	 * mappings, that we have raced with a PMD fault that overlaps with
1410e2093926SRoss Zwisler 	 * the PTE we need to set up.  If so just return and the fault will be
1411e2093926SRoss Zwisler 	 * retried.
1412e2093926SRoss Zwisler 	 */
1413e2093926SRoss Zwisler 	if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1414ab77dab4SSouptick Joarder 		ret = VM_FAULT_NOPAGE;
1415e2093926SRoss Zwisler 		goto unlock_entry;
1416e2093926SRoss Zwisler 	}
1417e2093926SRoss Zwisler 
141865dd814aSChristoph Hellwig 	while ((error = iomap_iter(&iter, ops)) > 0) {
141965dd814aSChristoph Hellwig 		if (WARN_ON_ONCE(iomap_length(&iter) < PAGE_SIZE)) {
142065dd814aSChristoph Hellwig 			iter.processed = -EIO;	/* fs corruption? */
142165dd814aSChristoph Hellwig 			continue;
142265dd814aSChristoph Hellwig 		}
142365dd814aSChristoph Hellwig 
142465dd814aSChristoph Hellwig 		ret = dax_fault_iter(vmf, &iter, pfnp, &xas, &entry, false);
142565dd814aSChristoph Hellwig 		if (ret != VM_FAULT_SIGBUS &&
142665dd814aSChristoph Hellwig 		    (iter.iomap.flags & IOMAP_F_NEW)) {
142765dd814aSChristoph Hellwig 			count_vm_event(PGMAJFAULT);
142865dd814aSChristoph Hellwig 			count_memcg_event_mm(vmf->vma->vm_mm, PGMAJFAULT);
142965dd814aSChristoph Hellwig 			ret |= VM_FAULT_MAJOR;
143065dd814aSChristoph Hellwig 		}
143165dd814aSChristoph Hellwig 
143265dd814aSChristoph Hellwig 		if (!(ret & VM_FAULT_ERROR))
143365dd814aSChristoph Hellwig 			iter.processed = PAGE_SIZE;
143465dd814aSChristoph Hellwig 	}
143565dd814aSChristoph Hellwig 
1436c0b24625SJan Kara 	if (iomap_errp)
1437c0b24625SJan Kara 		*iomap_errp = error;
143865dd814aSChristoph Hellwig 	if (!ret && error)
1439ab77dab4SSouptick Joarder 		ret = dax_fault_return(error);
1440a7d73fe6SChristoph Hellwig 
144113e451fdSJan Kara unlock_entry:
1442b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1443a9c42b33SRoss Zwisler out:
144465dd814aSChristoph Hellwig 	trace_dax_pte_fault_done(iter.inode, vmf, ret);
144565dd814aSChristoph Hellwig 	return ret;
1446a7d73fe6SChristoph Hellwig }
1447642261acSRoss Zwisler 
1448642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD
144955f81639SShiyang Ruan static bool dax_fault_check_fallback(struct vm_fault *vmf, struct xa_state *xas,
145055f81639SShiyang Ruan 		pgoff_t max_pgoff)
1451642261acSRoss Zwisler {
1452d8a849e1SDave Jiang 	unsigned long pmd_addr = vmf->address & PMD_MASK;
1453d8a849e1SDave Jiang 	bool write = vmf->flags & FAULT_FLAG_WRITE;
1454282a8e03SRoss Zwisler 
1455fffa281bSRoss Zwisler 	/*
1456fffa281bSRoss Zwisler 	 * Make sure that the faulting address's PMD offset (color) matches
1457fffa281bSRoss Zwisler 	 * the PMD offset from the start of the file.  This is necessary so
1458fffa281bSRoss Zwisler 	 * that a PMD range in the page table overlaps exactly with a PMD
1459a77d19f4SMatthew Wilcox 	 * range in the page cache.
1460fffa281bSRoss Zwisler 	 */
1461fffa281bSRoss Zwisler 	if ((vmf->pgoff & PG_PMD_COLOUR) !=
1462fffa281bSRoss Zwisler 	    ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
146355f81639SShiyang Ruan 		return true;
1464fffa281bSRoss Zwisler 
1465642261acSRoss Zwisler 	/* Fall back to PTEs if we're going to COW */
146655f81639SShiyang Ruan 	if (write && !(vmf->vma->vm_flags & VM_SHARED))
146755f81639SShiyang Ruan 		return true;
1468642261acSRoss Zwisler 
1469642261acSRoss Zwisler 	/* If the PMD would extend outside the VMA */
147055f81639SShiyang Ruan 	if (pmd_addr < vmf->vma->vm_start)
147155f81639SShiyang Ruan 		return true;
147255f81639SShiyang Ruan 	if ((pmd_addr + PMD_SIZE) > vmf->vma->vm_end)
147355f81639SShiyang Ruan 		return true;
147455f81639SShiyang Ruan 
147555f81639SShiyang Ruan 	/* If the PMD would extend beyond the file size */
147655f81639SShiyang Ruan 	if ((xas->xa_index | PG_PMD_COLOUR) >= max_pgoff)
147755f81639SShiyang Ruan 		return true;
147855f81639SShiyang Ruan 
147955f81639SShiyang Ruan 	return false;
148055f81639SShiyang Ruan }
148155f81639SShiyang Ruan 
1482642261acSRoss Zwisler static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
1483642261acSRoss Zwisler 			       const struct iomap_ops *ops)
1484642261acSRoss Zwisler {
148565dd814aSChristoph Hellwig 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1486642261acSRoss Zwisler 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
148765dd814aSChristoph Hellwig 	struct iomap_iter iter = {
148865dd814aSChristoph Hellwig 		.inode		= mapping->host,
148965dd814aSChristoph Hellwig 		.len		= PMD_SIZE,
1490952da063SChristoph Hellwig 		.flags		= IOMAP_DAX | IOMAP_FAULT,
149165dd814aSChristoph Hellwig 	};
1492c2436190SShiyang Ruan 	vm_fault_t ret = VM_FAULT_FALLBACK;
1493642261acSRoss Zwisler 	pgoff_t max_pgoff;
1494642261acSRoss Zwisler 	void *entry;
1495642261acSRoss Zwisler 	int error;
1496642261acSRoss Zwisler 
149765dd814aSChristoph Hellwig 	if (vmf->flags & FAULT_FLAG_WRITE)
149865dd814aSChristoph Hellwig 		iter.flags |= IOMAP_WRITE;
149965dd814aSChristoph Hellwig 
1500642261acSRoss Zwisler 	/*
1501642261acSRoss Zwisler 	 * Check whether offset isn't beyond end of file now. Caller is
1502642261acSRoss Zwisler 	 * supposed to hold locks serializing us with truncate / punch hole so
1503642261acSRoss Zwisler 	 * this is a reliable test.
1504642261acSRoss Zwisler 	 */
150565dd814aSChristoph Hellwig 	max_pgoff = DIV_ROUND_UP(i_size_read(iter.inode), PAGE_SIZE);
1506642261acSRoss Zwisler 
150765dd814aSChristoph Hellwig 	trace_dax_pmd_fault(iter.inode, vmf, max_pgoff, 0);
1508642261acSRoss Zwisler 
1509b15cd800SMatthew Wilcox 	if (xas.xa_index >= max_pgoff) {
1510c2436190SShiyang Ruan 		ret = VM_FAULT_SIGBUS;
1511282a8e03SRoss Zwisler 		goto out;
1512282a8e03SRoss Zwisler 	}
1513642261acSRoss Zwisler 
151455f81639SShiyang Ruan 	if (dax_fault_check_fallback(vmf, &xas, max_pgoff))
1515642261acSRoss Zwisler 		goto fallback;
1516642261acSRoss Zwisler 
1517642261acSRoss Zwisler 	/*
1518b15cd800SMatthew Wilcox 	 * grab_mapping_entry() will make sure we get an empty PMD entry,
1519b15cd800SMatthew Wilcox 	 * a zero PMD entry or a DAX PMD.  If it can't (because a PTE
1520b15cd800SMatthew Wilcox 	 * entry is already in the array, for instance), it will return
1521b15cd800SMatthew Wilcox 	 * VM_FAULT_FALLBACK.
15229f141d6eSJan Kara 	 */
152323c84eb7SMatthew Wilcox (Oracle) 	entry = grab_mapping_entry(&xas, mapping, PMD_ORDER);
1524b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1525c2436190SShiyang Ruan 		ret = xa_to_internal(entry);
1526876f2946SRoss Zwisler 		goto fallback;
1527b15cd800SMatthew Wilcox 	}
1528876f2946SRoss Zwisler 
1529876f2946SRoss Zwisler 	/*
1530e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1531e2093926SRoss Zwisler 	 * mappings, that we have raced with a PTE fault that overlaps with
1532e2093926SRoss Zwisler 	 * the PMD we need to set up.  If so just return and the fault will be
1533e2093926SRoss Zwisler 	 * retried.
1534e2093926SRoss Zwisler 	 */
1535e2093926SRoss Zwisler 	if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1536e2093926SRoss Zwisler 			!pmd_devmap(*vmf->pmd)) {
1537c2436190SShiyang Ruan 		ret = 0;
1538e2093926SRoss Zwisler 		goto unlock_entry;
1539e2093926SRoss Zwisler 	}
1540e2093926SRoss Zwisler 
154165dd814aSChristoph Hellwig 	iter.pos = (loff_t)xas.xa_index << PAGE_SHIFT;
154265dd814aSChristoph Hellwig 	while ((error = iomap_iter(&iter, ops)) > 0) {
154365dd814aSChristoph Hellwig 		if (iomap_length(&iter) < PMD_SIZE)
154465dd814aSChristoph Hellwig 			continue; /* actually breaks out of the loop */
1545876f2946SRoss Zwisler 
154665dd814aSChristoph Hellwig 		ret = dax_fault_iter(vmf, &iter, pfnp, &xas, &entry, true);
154765dd814aSChristoph Hellwig 		if (ret != VM_FAULT_FALLBACK)
154865dd814aSChristoph Hellwig 			iter.processed = PMD_SIZE;
1549caa51d26SJan Kara 	}
1550caa51d26SJan Kara 
1551876f2946SRoss Zwisler unlock_entry:
1552b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1553642261acSRoss Zwisler fallback:
1554c2436190SShiyang Ruan 	if (ret == VM_FAULT_FALLBACK) {
155565dd814aSChristoph Hellwig 		split_huge_pmd(vmf->vma, vmf->pmd, vmf->address);
1556642261acSRoss Zwisler 		count_vm_event(THP_FAULT_FALLBACK);
1557642261acSRoss Zwisler 	}
1558282a8e03SRoss Zwisler out:
155965dd814aSChristoph Hellwig 	trace_dax_pmd_fault_done(iter.inode, vmf, max_pgoff, ret);
1560c2436190SShiyang Ruan 	return ret;
1561642261acSRoss Zwisler }
1562a2d58167SDave Jiang #else
1563ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
156401cddfe9SArnd Bergmann 			       const struct iomap_ops *ops)
1565a2d58167SDave Jiang {
1566a2d58167SDave Jiang 	return VM_FAULT_FALLBACK;
1567a2d58167SDave Jiang }
1568642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */
1569a2d58167SDave Jiang 
1570a2d58167SDave Jiang /**
1571a2d58167SDave Jiang  * dax_iomap_fault - handle a page fault on a DAX file
1572a2d58167SDave Jiang  * @vmf: The description of the fault
1573cec04e8cSJan Kara  * @pe_size: Size of the page to fault in
15749a0dd422SJan Kara  * @pfnp: PFN to insert for synchronous faults if fsync is required
1575c0b24625SJan Kara  * @iomap_errp: Storage for detailed error code in case of error
1576cec04e8cSJan Kara  * @ops: Iomap ops passed from the file system
1577a2d58167SDave Jiang  *
1578a2d58167SDave Jiang  * When a page fault occurs, filesystems may call this helper in
1579a2d58167SDave Jiang  * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1580a2d58167SDave Jiang  * has done all the necessary locking for page fault to proceed
1581a2d58167SDave Jiang  * successfully.
1582a2d58167SDave Jiang  */
1583ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1584c0b24625SJan Kara 		    pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
1585a2d58167SDave Jiang {
1586c791ace1SDave Jiang 	switch (pe_size) {
1587c791ace1SDave Jiang 	case PE_SIZE_PTE:
1588c0b24625SJan Kara 		return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
1589c791ace1SDave Jiang 	case PE_SIZE_PMD:
15909a0dd422SJan Kara 		return dax_iomap_pmd_fault(vmf, pfnp, ops);
1591a2d58167SDave Jiang 	default:
1592a2d58167SDave Jiang 		return VM_FAULT_FALLBACK;
1593a2d58167SDave Jiang 	}
1594a2d58167SDave Jiang }
1595a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault);
159671eab6dfSJan Kara 
1597a77d19f4SMatthew Wilcox /*
159871eab6dfSJan Kara  * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
159971eab6dfSJan Kara  * @vmf: The description of the fault
160071eab6dfSJan Kara  * @pfn: PFN to insert
1601cfc93c6cSMatthew Wilcox  * @order: Order of entry to insert.
160271eab6dfSJan Kara  *
1603a77d19f4SMatthew Wilcox  * This function inserts a writeable PTE or PMD entry into the page tables
1604a77d19f4SMatthew Wilcox  * for an mmaped DAX file.  It also marks the page cache entry as dirty.
160571eab6dfSJan Kara  */
1606cfc93c6cSMatthew Wilcox static vm_fault_t
1607cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
160871eab6dfSJan Kara {
160971eab6dfSJan Kara 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1610cfc93c6cSMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1611cfc93c6cSMatthew Wilcox 	void *entry;
1612ab77dab4SSouptick Joarder 	vm_fault_t ret;
161371eab6dfSJan Kara 
1614cfc93c6cSMatthew Wilcox 	xas_lock_irq(&xas);
161523c84eb7SMatthew Wilcox (Oracle) 	entry = get_unlocked_entry(&xas, order);
161671eab6dfSJan Kara 	/* Did we race with someone splitting entry or so? */
161723c84eb7SMatthew Wilcox (Oracle) 	if (!entry || dax_is_conflict(entry) ||
161823c84eb7SMatthew Wilcox (Oracle) 	    (order == 0 && !dax_is_pte_entry(entry))) {
16194c3d043dSVivek Goyal 		put_unlocked_entry(&xas, entry, WAKE_NEXT);
1620cfc93c6cSMatthew Wilcox 		xas_unlock_irq(&xas);
162171eab6dfSJan Kara 		trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
162271eab6dfSJan Kara 						      VM_FAULT_NOPAGE);
162371eab6dfSJan Kara 		return VM_FAULT_NOPAGE;
162471eab6dfSJan Kara 	}
1625cfc93c6cSMatthew Wilcox 	xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1626cfc93c6cSMatthew Wilcox 	dax_lock_entry(&xas, entry);
1627cfc93c6cSMatthew Wilcox 	xas_unlock_irq(&xas);
1628cfc93c6cSMatthew Wilcox 	if (order == 0)
1629ab77dab4SSouptick Joarder 		ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
163071eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD
1631cfc93c6cSMatthew Wilcox 	else if (order == PMD_ORDER)
1632fce86ff5SDan Williams 		ret = vmf_insert_pfn_pmd(vmf, pfn, FAULT_FLAG_WRITE);
163371eab6dfSJan Kara #endif
1634cfc93c6cSMatthew Wilcox 	else
1635ab77dab4SSouptick Joarder 		ret = VM_FAULT_FALLBACK;
1636cfc93c6cSMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1637ab77dab4SSouptick Joarder 	trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1638ab77dab4SSouptick Joarder 	return ret;
163971eab6dfSJan Kara }
164071eab6dfSJan Kara 
164171eab6dfSJan Kara /**
164271eab6dfSJan Kara  * dax_finish_sync_fault - finish synchronous page fault
164371eab6dfSJan Kara  * @vmf: The description of the fault
164471eab6dfSJan Kara  * @pe_size: Size of entry to be inserted
164571eab6dfSJan Kara  * @pfn: PFN to insert
164671eab6dfSJan Kara  *
164771eab6dfSJan Kara  * This function ensures that the file range touched by the page fault is
164871eab6dfSJan Kara  * stored persistently on the media and handles inserting of appropriate page
164971eab6dfSJan Kara  * table entry.
165071eab6dfSJan Kara  */
1651ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1652ab77dab4SSouptick Joarder 		enum page_entry_size pe_size, pfn_t pfn)
165371eab6dfSJan Kara {
165471eab6dfSJan Kara 	int err;
165571eab6dfSJan Kara 	loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1656cfc93c6cSMatthew Wilcox 	unsigned int order = pe_order(pe_size);
1657cfc93c6cSMatthew Wilcox 	size_t len = PAGE_SIZE << order;
165871eab6dfSJan Kara 
165971eab6dfSJan Kara 	err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
166071eab6dfSJan Kara 	if (err)
166171eab6dfSJan Kara 		return VM_FAULT_SIGBUS;
1662cfc93c6cSMatthew Wilcox 	return dax_insert_pfn_mkwrite(vmf, pfn, order);
166371eab6dfSJan Kara }
166471eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault);
1665