xref: /openbmc/linux/fs/dax.c (revision 0e40de03)
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 
235cfc93c6cSMatthew Wilcox static void put_unlocked_entry(struct xa_state *xas, void *entry)
236cfc93c6cSMatthew Wilcox {
237cfc93c6cSMatthew Wilcox 	/* If we were the only waiter woken, wake the next one */
238cfc93c6cSMatthew Wilcox 	if (entry)
239cfc93c6cSMatthew Wilcox 		dax_wake_entry(xas, entry, false);
240cfc93c6cSMatthew Wilcox }
241cfc93c6cSMatthew Wilcox 
242cfc93c6cSMatthew Wilcox /*
243cfc93c6cSMatthew Wilcox  * We used the xa_state to get the entry, but then we locked the entry and
244cfc93c6cSMatthew Wilcox  * dropped the xa_lock, so we know the xa_state is stale and must be reset
245cfc93c6cSMatthew Wilcox  * before use.
246cfc93c6cSMatthew Wilcox  */
247cfc93c6cSMatthew Wilcox static void dax_unlock_entry(struct xa_state *xas, void *entry)
248cfc93c6cSMatthew Wilcox {
249cfc93c6cSMatthew Wilcox 	void *old;
250cfc93c6cSMatthew Wilcox 
2517ae2ea7dSMatthew Wilcox 	BUG_ON(dax_is_locked(entry));
252cfc93c6cSMatthew Wilcox 	xas_reset(xas);
253cfc93c6cSMatthew Wilcox 	xas_lock_irq(xas);
254cfc93c6cSMatthew Wilcox 	old = xas_store(xas, entry);
255cfc93c6cSMatthew Wilcox 	xas_unlock_irq(xas);
256cfc93c6cSMatthew Wilcox 	BUG_ON(!dax_is_locked(old));
257cfc93c6cSMatthew Wilcox 	dax_wake_entry(xas, entry, false);
258cfc93c6cSMatthew Wilcox }
259cfc93c6cSMatthew Wilcox 
260cfc93c6cSMatthew Wilcox /*
261cfc93c6cSMatthew Wilcox  * Return: The entry stored at this location before it was locked.
262cfc93c6cSMatthew Wilcox  */
263cfc93c6cSMatthew Wilcox static void *dax_lock_entry(struct xa_state *xas, void *entry)
264cfc93c6cSMatthew Wilcox {
265cfc93c6cSMatthew Wilcox 	unsigned long v = xa_to_value(entry);
266cfc93c6cSMatthew Wilcox 	return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
267cfc93c6cSMatthew Wilcox }
268cfc93c6cSMatthew Wilcox 
269d2c997c0SDan Williams static unsigned long dax_entry_size(void *entry)
270d2c997c0SDan Williams {
271d2c997c0SDan Williams 	if (dax_is_zero_entry(entry))
272d2c997c0SDan Williams 		return 0;
273d2c997c0SDan Williams 	else if (dax_is_empty_entry(entry))
274d2c997c0SDan Williams 		return 0;
275d2c997c0SDan Williams 	else if (dax_is_pmd_entry(entry))
276d2c997c0SDan Williams 		return PMD_SIZE;
277d2c997c0SDan Williams 	else
278d2c997c0SDan Williams 		return PAGE_SIZE;
279d2c997c0SDan Williams }
280d2c997c0SDan Williams 
281a77d19f4SMatthew Wilcox static unsigned long dax_end_pfn(void *entry)
282d2c997c0SDan Williams {
283a77d19f4SMatthew Wilcox 	return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
284d2c997c0SDan Williams }
285d2c997c0SDan Williams 
286d2c997c0SDan Williams /*
287d2c997c0SDan Williams  * Iterate through all mapped pfns represented by an entry, i.e. skip
288d2c997c0SDan Williams  * 'empty' and 'zero' entries.
289d2c997c0SDan Williams  */
290d2c997c0SDan Williams #define for_each_mapped_pfn(entry, pfn) \
291a77d19f4SMatthew Wilcox 	for (pfn = dax_to_pfn(entry); \
292a77d19f4SMatthew Wilcox 			pfn < dax_end_pfn(entry); pfn++)
293d2c997c0SDan Williams 
29473449dafSDan Williams /*
29573449dafSDan Williams  * TODO: for reflink+dax we need a way to associate a single page with
29673449dafSDan Williams  * multiple address_space instances at different linear_page_index()
29773449dafSDan Williams  * offsets.
29873449dafSDan Williams  */
29973449dafSDan Williams static void dax_associate_entry(void *entry, struct address_space *mapping,
30073449dafSDan Williams 		struct vm_area_struct *vma, unsigned long address)
301d2c997c0SDan Williams {
30273449dafSDan Williams 	unsigned long size = dax_entry_size(entry), pfn, index;
30373449dafSDan Williams 	int i = 0;
304d2c997c0SDan Williams 
305d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
306d2c997c0SDan Williams 		return;
307d2c997c0SDan Williams 
30873449dafSDan Williams 	index = linear_page_index(vma, address & ~(size - 1));
309d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
310d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
311d2c997c0SDan Williams 
312d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping);
313d2c997c0SDan Williams 		page->mapping = mapping;
31473449dafSDan Williams 		page->index = index + i++;
315d2c997c0SDan Williams 	}
316d2c997c0SDan Williams }
317d2c997c0SDan Williams 
318d2c997c0SDan Williams static void dax_disassociate_entry(void *entry, struct address_space *mapping,
319d2c997c0SDan Williams 		bool trunc)
320d2c997c0SDan Williams {
321d2c997c0SDan Williams 	unsigned long pfn;
322d2c997c0SDan Williams 
323d2c997c0SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
324d2c997c0SDan Williams 		return;
325d2c997c0SDan Williams 
326d2c997c0SDan Williams 	for_each_mapped_pfn(entry, pfn) {
327d2c997c0SDan Williams 		struct page *page = pfn_to_page(pfn);
328d2c997c0SDan Williams 
329d2c997c0SDan Williams 		WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
330d2c997c0SDan Williams 		WARN_ON_ONCE(page->mapping && page->mapping != mapping);
331d2c997c0SDan Williams 		page->mapping = NULL;
33273449dafSDan Williams 		page->index = 0;
333d2c997c0SDan Williams 	}
334d2c997c0SDan Williams }
335d2c997c0SDan Williams 
3365fac7408SDan Williams static struct page *dax_busy_page(void *entry)
3375fac7408SDan Williams {
3385fac7408SDan Williams 	unsigned long pfn;
3395fac7408SDan Williams 
3405fac7408SDan Williams 	for_each_mapped_pfn(entry, pfn) {
3415fac7408SDan Williams 		struct page *page = pfn_to_page(pfn);
3425fac7408SDan Williams 
3435fac7408SDan Williams 		if (page_ref_count(page) > 1)
3445fac7408SDan Williams 			return page;
3455fac7408SDan Williams 	}
3465fac7408SDan Williams 	return NULL;
3475fac7408SDan Williams }
3485fac7408SDan Williams 
349c5bbd451SMatthew Wilcox /*
350c5bbd451SMatthew Wilcox  * dax_lock_mapping_entry - Lock the DAX entry corresponding to a page
351c5bbd451SMatthew Wilcox  * @page: The page whose entry we want to lock
352c5bbd451SMatthew Wilcox  *
353c5bbd451SMatthew Wilcox  * Context: Process context.
354c5bbd451SMatthew Wilcox  * Return: %true if the entry was locked or does not need to be locked.
355c5bbd451SMatthew Wilcox  */
356c2a7d2a1SDan Williams bool dax_lock_mapping_entry(struct page *page)
357c2a7d2a1SDan Williams {
3589f32d221SMatthew Wilcox 	XA_STATE(xas, NULL, 0);
3599f32d221SMatthew Wilcox 	void *entry;
360c5bbd451SMatthew Wilcox 	bool locked;
361c2a7d2a1SDan Williams 
362c5bbd451SMatthew Wilcox 	/* Ensure page->mapping isn't freed while we look at it */
363c5bbd451SMatthew Wilcox 	rcu_read_lock();
364c2a7d2a1SDan Williams 	for (;;) {
3659f32d221SMatthew Wilcox 		struct address_space *mapping = READ_ONCE(page->mapping);
366c2a7d2a1SDan Williams 
367c5bbd451SMatthew Wilcox 		locked = false;
368c2a7d2a1SDan Williams 		if (!dax_mapping(mapping))
369c5bbd451SMatthew Wilcox 			break;
370c2a7d2a1SDan Williams 
371c2a7d2a1SDan Williams 		/*
372c2a7d2a1SDan Williams 		 * In the device-dax case there's no need to lock, a
373c2a7d2a1SDan Williams 		 * struct dev_pagemap pin is sufficient to keep the
374c2a7d2a1SDan Williams 		 * inode alive, and we assume we have dev_pagemap pin
375c2a7d2a1SDan Williams 		 * otherwise we would not have a valid pfn_to_page()
376c2a7d2a1SDan Williams 		 * translation.
377c2a7d2a1SDan Williams 		 */
378c5bbd451SMatthew Wilcox 		locked = true;
3799f32d221SMatthew Wilcox 		if (S_ISCHR(mapping->host->i_mode))
380c5bbd451SMatthew Wilcox 			break;
381c2a7d2a1SDan Williams 
3829f32d221SMatthew Wilcox 		xas.xa = &mapping->i_pages;
3839f32d221SMatthew Wilcox 		xas_lock_irq(&xas);
384c2a7d2a1SDan Williams 		if (mapping != page->mapping) {
3859f32d221SMatthew Wilcox 			xas_unlock_irq(&xas);
386c2a7d2a1SDan Williams 			continue;
387c2a7d2a1SDan Williams 		}
3889f32d221SMatthew Wilcox 		xas_set(&xas, page->index);
3899f32d221SMatthew Wilcox 		entry = xas_load(&xas);
3909f32d221SMatthew Wilcox 		if (dax_is_locked(entry)) {
391c5bbd451SMatthew Wilcox 			rcu_read_unlock();
3929f32d221SMatthew Wilcox 			entry = get_unlocked_entry(&xas);
3939f32d221SMatthew Wilcox 			xas_unlock_irq(&xas);
394c5bbd451SMatthew Wilcox 			rcu_read_lock();
395c2a7d2a1SDan Williams 			continue;
396c2a7d2a1SDan Williams 		}
3979f32d221SMatthew Wilcox 		dax_lock_entry(&xas, entry);
3989f32d221SMatthew Wilcox 		xas_unlock_irq(&xas);
399c5bbd451SMatthew Wilcox 		break;
4009f32d221SMatthew Wilcox 	}
401c5bbd451SMatthew Wilcox 	rcu_read_unlock();
402c5bbd451SMatthew Wilcox 	return locked;
403c2a7d2a1SDan Williams }
404c2a7d2a1SDan Williams 
405c2a7d2a1SDan Williams void dax_unlock_mapping_entry(struct page *page)
406c2a7d2a1SDan Williams {
407c2a7d2a1SDan Williams 	struct address_space *mapping = page->mapping;
4089f32d221SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, page->index);
409fda490d3SMatthew Wilcox 	void *entry;
410c2a7d2a1SDan Williams 
4119f32d221SMatthew Wilcox 	if (S_ISCHR(mapping->host->i_mode))
412c2a7d2a1SDan Williams 		return;
413c2a7d2a1SDan Williams 
414fda490d3SMatthew Wilcox 	rcu_read_lock();
415fda490d3SMatthew Wilcox 	entry = xas_load(&xas);
416fda490d3SMatthew Wilcox 	rcu_read_unlock();
417fda490d3SMatthew Wilcox 	entry = dax_make_entry(page_to_pfn_t(page), dax_is_pmd_entry(entry));
418fda490d3SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
419c2a7d2a1SDan Williams }
420c2a7d2a1SDan Williams 
421ac401cc7SJan Kara /*
422a77d19f4SMatthew Wilcox  * Find page cache entry at given index. If it is a DAX entry, return it
423a77d19f4SMatthew Wilcox  * with the entry locked. If the page cache doesn't contain an entry at
424a77d19f4SMatthew Wilcox  * that index, add a locked empty entry.
425ac401cc7SJan Kara  *
4263159f943SMatthew Wilcox  * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
427b15cd800SMatthew Wilcox  * either return that locked entry or will return VM_FAULT_FALLBACK.
428b15cd800SMatthew Wilcox  * This will happen if there are any PTE entries within the PMD range
429b15cd800SMatthew Wilcox  * that we are requesting.
430642261acSRoss Zwisler  *
431b15cd800SMatthew Wilcox  * We always favor PTE entries over PMD entries. There isn't a flow where we
432b15cd800SMatthew Wilcox  * evict PTE entries in order to 'upgrade' them to a PMD entry.  A PMD
433b15cd800SMatthew Wilcox  * insertion will fail if it finds any PTE entries already in the tree, and a
434b15cd800SMatthew Wilcox  * PTE insertion will cause an existing PMD entry to be unmapped and
435b15cd800SMatthew Wilcox  * downgraded to PTE entries.  This happens for both PMD zero pages as
436b15cd800SMatthew Wilcox  * well as PMD empty entries.
437642261acSRoss Zwisler  *
438b15cd800SMatthew Wilcox  * The exception to this downgrade path is for PMD entries that have
439b15cd800SMatthew Wilcox  * real storage backing them.  We will leave these real PMD entries in
440b15cd800SMatthew Wilcox  * the tree, and PTE writes will simply dirty the entire PMD entry.
441642261acSRoss Zwisler  *
442ac401cc7SJan Kara  * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
443ac401cc7SJan Kara  * persistent memory the benefit is doubtful. We can add that later if we can
444ac401cc7SJan Kara  * show it helps.
445b15cd800SMatthew Wilcox  *
446b15cd800SMatthew Wilcox  * On error, this function does not return an ERR_PTR.  Instead it returns
447b15cd800SMatthew Wilcox  * a VM_FAULT code, encoded as an xarray internal entry.  The ERR_PTR values
448b15cd800SMatthew Wilcox  * overlap with xarray value entries.
449ac401cc7SJan Kara  */
450b15cd800SMatthew Wilcox static void *grab_mapping_entry(struct xa_state *xas,
451b15cd800SMatthew Wilcox 		struct address_space *mapping, unsigned long size_flag)
452ac401cc7SJan Kara {
453b15cd800SMatthew Wilcox 	unsigned long index = xas->xa_index;
454b15cd800SMatthew Wilcox 	bool pmd_downgrade = false; /* splitting PMD entry into PTE entries? */
455b15cd800SMatthew Wilcox 	void *entry;
456ac401cc7SJan Kara 
457b15cd800SMatthew Wilcox retry:
458b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
459b15cd800SMatthew Wilcox 	entry = get_unlocked_entry(xas);
460642261acSRoss Zwisler 
461b15cd800SMatthew Wilcox 	if (entry) {
4620e40de03SMatthew Wilcox 		if (!xa_is_value(entry)) {
463b15cd800SMatthew Wilcox 			xas_set_err(xas, EIO);
46491d25ba8SRoss Zwisler 			goto out_unlock;
46591d25ba8SRoss Zwisler 		}
46691d25ba8SRoss Zwisler 
4673159f943SMatthew Wilcox 		if (size_flag & DAX_PMD) {
46891d25ba8SRoss Zwisler 			if (dax_is_pte_entry(entry)) {
469b15cd800SMatthew Wilcox 				put_unlocked_entry(xas, entry);
470b15cd800SMatthew Wilcox 				goto fallback;
471642261acSRoss Zwisler 			}
472642261acSRoss Zwisler 		} else { /* trying to grab a PTE entry */
47391d25ba8SRoss Zwisler 			if (dax_is_pmd_entry(entry) &&
474642261acSRoss Zwisler 			    (dax_is_zero_entry(entry) ||
475642261acSRoss Zwisler 			     dax_is_empty_entry(entry))) {
476642261acSRoss Zwisler 				pmd_downgrade = true;
477642261acSRoss Zwisler 			}
478642261acSRoss Zwisler 		}
479642261acSRoss Zwisler 	}
480642261acSRoss Zwisler 
481642261acSRoss Zwisler 	if (pmd_downgrade) {
482642261acSRoss Zwisler 		/*
483642261acSRoss Zwisler 		 * Make sure 'entry' remains valid while we drop
484b93b0163SMatthew Wilcox 		 * the i_pages lock.
485642261acSRoss Zwisler 		 */
486b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
487642261acSRoss Zwisler 
488642261acSRoss Zwisler 		/*
489642261acSRoss Zwisler 		 * Besides huge zero pages the only other thing that gets
490642261acSRoss Zwisler 		 * downgraded are empty entries which don't need to be
491642261acSRoss Zwisler 		 * unmapped.
492642261acSRoss Zwisler 		 */
493b15cd800SMatthew Wilcox 		if (dax_is_zero_entry(entry)) {
494b15cd800SMatthew Wilcox 			xas_unlock_irq(xas);
495b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping,
496b15cd800SMatthew Wilcox 					xas->xa_index & ~PG_PMD_COLOUR,
497977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
498b15cd800SMatthew Wilcox 			xas_reset(xas);
499b15cd800SMatthew Wilcox 			xas_lock_irq(xas);
500e11f8b7bSRoss Zwisler 		}
501e11f8b7bSRoss Zwisler 
502d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
503b15cd800SMatthew Wilcox 		xas_store(xas, NULL);	/* undo the PMD join */
504b15cd800SMatthew Wilcox 		dax_wake_entry(xas, entry, true);
505642261acSRoss Zwisler 		mapping->nrexceptional--;
506b15cd800SMatthew Wilcox 		entry = NULL;
507b15cd800SMatthew Wilcox 		xas_set(xas, index);
508642261acSRoss Zwisler 	}
509642261acSRoss Zwisler 
510b15cd800SMatthew Wilcox 	if (entry) {
511b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
512b15cd800SMatthew Wilcox 	} else {
513b15cd800SMatthew Wilcox 		entry = dax_make_entry(pfn_to_pfn_t(0), size_flag | DAX_EMPTY);
514b15cd800SMatthew Wilcox 		dax_lock_entry(xas, entry);
515b15cd800SMatthew Wilcox 		if (xas_error(xas))
516b15cd800SMatthew Wilcox 			goto out_unlock;
517ac401cc7SJan Kara 		mapping->nrexceptional++;
518ac401cc7SJan Kara 	}
519b15cd800SMatthew Wilcox 
520642261acSRoss Zwisler out_unlock:
521b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
522b15cd800SMatthew Wilcox 	if (xas_nomem(xas, mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM))
523b15cd800SMatthew Wilcox 		goto retry;
524b15cd800SMatthew Wilcox 	if (xas->xa_node == XA_ERROR(-ENOMEM))
525b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_OOM);
526b15cd800SMatthew Wilcox 	if (xas_error(xas))
527b15cd800SMatthew Wilcox 		return xa_mk_internal(VM_FAULT_SIGBUS);
528e3ad61c6SRoss Zwisler 	return entry;
529b15cd800SMatthew Wilcox fallback:
530b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
531b15cd800SMatthew Wilcox 	return xa_mk_internal(VM_FAULT_FALLBACK);
532ac401cc7SJan Kara }
533ac401cc7SJan Kara 
5345fac7408SDan Williams /**
5355fac7408SDan Williams  * dax_layout_busy_page - find first pinned page in @mapping
5365fac7408SDan Williams  * @mapping: address space to scan for a page with ref count > 1
5375fac7408SDan Williams  *
5385fac7408SDan Williams  * DAX requires ZONE_DEVICE mapped pages. These pages are never
5395fac7408SDan Williams  * 'onlined' to the page allocator so they are considered idle when
5405fac7408SDan Williams  * page->count == 1. A filesystem uses this interface to determine if
5415fac7408SDan Williams  * any page in the mapping is busy, i.e. for DMA, or other
5425fac7408SDan Williams  * get_user_pages() usages.
5435fac7408SDan Williams  *
5445fac7408SDan Williams  * It is expected that the filesystem is holding locks to block the
5455fac7408SDan Williams  * establishment of new mappings in this address_space. I.e. it expects
5465fac7408SDan Williams  * to be able to run unmap_mapping_range() and subsequently not race
5475fac7408SDan Williams  * mapping_mapped() becoming true.
5485fac7408SDan Williams  */
5495fac7408SDan Williams struct page *dax_layout_busy_page(struct address_space *mapping)
5505fac7408SDan Williams {
551084a8990SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, 0);
552084a8990SMatthew Wilcox 	void *entry;
553084a8990SMatthew Wilcox 	unsigned int scanned = 0;
5545fac7408SDan Williams 	struct page *page = NULL;
5555fac7408SDan Williams 
5565fac7408SDan Williams 	/*
5575fac7408SDan Williams 	 * In the 'limited' case get_user_pages() for dax is disabled.
5585fac7408SDan Williams 	 */
5595fac7408SDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
5605fac7408SDan Williams 		return NULL;
5615fac7408SDan Williams 
5625fac7408SDan Williams 	if (!dax_mapping(mapping) || !mapping_mapped(mapping))
5635fac7408SDan Williams 		return NULL;
5645fac7408SDan Williams 
5655fac7408SDan Williams 	/*
5665fac7408SDan Williams 	 * If we race get_user_pages_fast() here either we'll see the
567084a8990SMatthew Wilcox 	 * elevated page count in the iteration and wait, or
5685fac7408SDan Williams 	 * get_user_pages_fast() will see that the page it took a reference
5695fac7408SDan Williams 	 * against is no longer mapped in the page tables and bail to the
5705fac7408SDan Williams 	 * get_user_pages() slow path.  The slow path is protected by
5715fac7408SDan Williams 	 * pte_lock() and pmd_lock(). New references are not taken without
5725fac7408SDan Williams 	 * holding those locks, and unmap_mapping_range() will not zero the
5735fac7408SDan Williams 	 * pte or pmd without holding the respective lock, so we are
5745fac7408SDan Williams 	 * guaranteed to either see new references or prevent new
5755fac7408SDan Williams 	 * references from being established.
5765fac7408SDan Williams 	 */
5775fac7408SDan Williams 	unmap_mapping_range(mapping, 0, 0, 1);
5785fac7408SDan Williams 
579084a8990SMatthew Wilcox 	xas_lock_irq(&xas);
580084a8990SMatthew Wilcox 	xas_for_each(&xas, entry, ULONG_MAX) {
581084a8990SMatthew Wilcox 		if (WARN_ON_ONCE(!xa_is_value(entry)))
5825fac7408SDan Williams 			continue;
583084a8990SMatthew Wilcox 		if (unlikely(dax_is_locked(entry)))
584084a8990SMatthew Wilcox 			entry = get_unlocked_entry(&xas);
5855fac7408SDan Williams 		if (entry)
5865fac7408SDan Williams 			page = dax_busy_page(entry);
587084a8990SMatthew Wilcox 		put_unlocked_entry(&xas, entry);
5885fac7408SDan Williams 		if (page)
5895fac7408SDan Williams 			break;
590084a8990SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
591084a8990SMatthew Wilcox 			continue;
592cdbf8897SRoss Zwisler 
593084a8990SMatthew Wilcox 		xas_pause(&xas);
594084a8990SMatthew Wilcox 		xas_unlock_irq(&xas);
595084a8990SMatthew Wilcox 		cond_resched();
596084a8990SMatthew Wilcox 		xas_lock_irq(&xas);
5975fac7408SDan Williams 	}
598084a8990SMatthew Wilcox 	xas_unlock_irq(&xas);
5995fac7408SDan Williams 	return page;
6005fac7408SDan Williams }
6015fac7408SDan Williams EXPORT_SYMBOL_GPL(dax_layout_busy_page);
6025fac7408SDan Williams 
603a77d19f4SMatthew Wilcox static int __dax_invalidate_entry(struct address_space *mapping,
604c6dcf52cSJan Kara 					  pgoff_t index, bool trunc)
605c6dcf52cSJan Kara {
60607f2d89cSMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
607c6dcf52cSJan Kara 	int ret = 0;
608c6dcf52cSJan Kara 	void *entry;
609c6dcf52cSJan Kara 
61007f2d89cSMatthew Wilcox 	xas_lock_irq(&xas);
61107f2d89cSMatthew Wilcox 	entry = get_unlocked_entry(&xas);
6123159f943SMatthew Wilcox 	if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
613c6dcf52cSJan Kara 		goto out;
614c6dcf52cSJan Kara 	if (!trunc &&
61507f2d89cSMatthew Wilcox 	    (xas_get_mark(&xas, PAGECACHE_TAG_DIRTY) ||
61607f2d89cSMatthew Wilcox 	     xas_get_mark(&xas, PAGECACHE_TAG_TOWRITE)))
617c6dcf52cSJan Kara 		goto out;
618d2c997c0SDan Williams 	dax_disassociate_entry(entry, mapping, trunc);
61907f2d89cSMatthew Wilcox 	xas_store(&xas, NULL);
620c6dcf52cSJan Kara 	mapping->nrexceptional--;
621c6dcf52cSJan Kara 	ret = 1;
622c6dcf52cSJan Kara out:
62307f2d89cSMatthew Wilcox 	put_unlocked_entry(&xas, entry);
62407f2d89cSMatthew Wilcox 	xas_unlock_irq(&xas);
625c6dcf52cSJan Kara 	return ret;
626c6dcf52cSJan Kara }
62707f2d89cSMatthew Wilcox 
628ac401cc7SJan Kara /*
6293159f943SMatthew Wilcox  * Delete DAX entry at @index from @mapping.  Wait for it
6303159f943SMatthew Wilcox  * to be unlocked before deleting it.
631ac401cc7SJan Kara  */
632ac401cc7SJan Kara int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
633ac401cc7SJan Kara {
634a77d19f4SMatthew Wilcox 	int ret = __dax_invalidate_entry(mapping, index, true);
635ac401cc7SJan Kara 
636ac401cc7SJan Kara 	/*
637ac401cc7SJan Kara 	 * This gets called from truncate / punch_hole path. As such, the caller
638ac401cc7SJan Kara 	 * must hold locks protecting against concurrent modifications of the
639a77d19f4SMatthew Wilcox 	 * page cache (usually fs-private i_mmap_sem for writing). Since the
6403159f943SMatthew Wilcox 	 * caller has seen a DAX entry for this index, we better find it
641ac401cc7SJan Kara 	 * at that index as well...
642ac401cc7SJan Kara 	 */
643c6dcf52cSJan Kara 	WARN_ON_ONCE(!ret);
644c6dcf52cSJan Kara 	return ret;
645ac401cc7SJan Kara }
646ac401cc7SJan Kara 
647c6dcf52cSJan Kara /*
6483159f943SMatthew Wilcox  * Invalidate DAX entry if it is clean.
649c6dcf52cSJan Kara  */
650c6dcf52cSJan Kara int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
651c6dcf52cSJan Kara 				      pgoff_t index)
652c6dcf52cSJan Kara {
653a77d19f4SMatthew Wilcox 	return __dax_invalidate_entry(mapping, index, false);
654ac401cc7SJan Kara }
655ac401cc7SJan Kara 
656cccbce67SDan Williams static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
657cccbce67SDan Williams 		sector_t sector, size_t size, struct page *to,
658cccbce67SDan Williams 		unsigned long vaddr)
659f7ca90b1SMatthew Wilcox {
660cccbce67SDan Williams 	void *vto, *kaddr;
661cccbce67SDan Williams 	pgoff_t pgoff;
662cccbce67SDan Williams 	long rc;
663cccbce67SDan Williams 	int id;
664e2e05394SRoss Zwisler 
665cccbce67SDan Williams 	rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
666cccbce67SDan Williams 	if (rc)
667cccbce67SDan Williams 		return rc;
668cccbce67SDan Williams 
669cccbce67SDan Williams 	id = dax_read_lock();
67086ed913bSHuaisheng Ye 	rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
671cccbce67SDan Williams 	if (rc < 0) {
672cccbce67SDan Williams 		dax_read_unlock(id);
673cccbce67SDan Williams 		return rc;
674cccbce67SDan Williams 	}
675f7ca90b1SMatthew Wilcox 	vto = kmap_atomic(to);
676cccbce67SDan Williams 	copy_user_page(vto, (void __force *)kaddr, vaddr, to);
677f7ca90b1SMatthew Wilcox 	kunmap_atomic(vto);
678cccbce67SDan Williams 	dax_read_unlock(id);
679f7ca90b1SMatthew Wilcox 	return 0;
680f7ca90b1SMatthew Wilcox }
681f7ca90b1SMatthew Wilcox 
682642261acSRoss Zwisler /*
683642261acSRoss Zwisler  * By this point grab_mapping_entry() has ensured that we have a locked entry
684642261acSRoss Zwisler  * of the appropriate size so we don't have to worry about downgrading PMDs to
685642261acSRoss Zwisler  * PTEs.  If we happen to be trying to insert a PTE and there is a PMD
686642261acSRoss Zwisler  * already in the tree, we will skip the insertion and just dirty the PMD as
687642261acSRoss Zwisler  * appropriate.
688642261acSRoss Zwisler  */
689b15cd800SMatthew Wilcox static void *dax_insert_entry(struct xa_state *xas,
690b15cd800SMatthew Wilcox 		struct address_space *mapping, struct vm_fault *vmf,
691b15cd800SMatthew Wilcox 		void *entry, pfn_t pfn, unsigned long flags, bool dirty)
6929973c98eSRoss Zwisler {
693b15cd800SMatthew Wilcox 	void *new_entry = dax_make_entry(pfn, flags);
6949973c98eSRoss Zwisler 
695f5b7b748SJan Kara 	if (dirty)
6969973c98eSRoss Zwisler 		__mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
6979973c98eSRoss Zwisler 
6983159f943SMatthew Wilcox 	if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
699b15cd800SMatthew Wilcox 		unsigned long index = xas->xa_index;
70091d25ba8SRoss Zwisler 		/* we are replacing a zero page with block mapping */
70191d25ba8SRoss Zwisler 		if (dax_is_pmd_entry(entry))
702977fbdcdSMatthew Wilcox 			unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
703977fbdcdSMatthew Wilcox 					PG_PMD_NR, false);
70491d25ba8SRoss Zwisler 		else /* pte entry */
705b15cd800SMatthew Wilcox 			unmap_mapping_pages(mapping, index, 1, false);
706ac401cc7SJan Kara 	}
7079973c98eSRoss Zwisler 
708b15cd800SMatthew Wilcox 	xas_reset(xas);
709b15cd800SMatthew Wilcox 	xas_lock_irq(xas);
710d2c997c0SDan Williams 	if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
711d2c997c0SDan Williams 		dax_disassociate_entry(entry, mapping, false);
71273449dafSDan Williams 		dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
713d2c997c0SDan Williams 	}
714642261acSRoss Zwisler 
71591d25ba8SRoss Zwisler 	if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
716642261acSRoss Zwisler 		/*
717a77d19f4SMatthew Wilcox 		 * Only swap our new entry into the page cache if the current
718642261acSRoss Zwisler 		 * entry is a zero page or an empty entry.  If a normal PTE or
719a77d19f4SMatthew Wilcox 		 * PMD entry is already in the cache, we leave it alone.  This
720642261acSRoss Zwisler 		 * means that if we are trying to insert a PTE and the
721642261acSRoss Zwisler 		 * existing entry is a PMD, we will just leave the PMD in the
722642261acSRoss Zwisler 		 * tree and dirty it if necessary.
723642261acSRoss Zwisler 		 */
724b15cd800SMatthew Wilcox 		void *old = dax_lock_entry(xas, new_entry);
725b15cd800SMatthew Wilcox 		WARN_ON_ONCE(old != xa_mk_value(xa_to_value(entry) |
726b15cd800SMatthew Wilcox 					DAX_LOCKED));
72791d25ba8SRoss Zwisler 		entry = new_entry;
728b15cd800SMatthew Wilcox 	} else {
729b15cd800SMatthew Wilcox 		xas_load(xas);	/* Walk the xa_state */
730ac401cc7SJan Kara 	}
73191d25ba8SRoss Zwisler 
732f5b7b748SJan Kara 	if (dirty)
733b15cd800SMatthew Wilcox 		xas_set_mark(xas, PAGECACHE_TAG_DIRTY);
73491d25ba8SRoss Zwisler 
735b15cd800SMatthew Wilcox 	xas_unlock_irq(xas);
73691d25ba8SRoss Zwisler 	return entry;
7379973c98eSRoss Zwisler }
7389973c98eSRoss Zwisler 
739a77d19f4SMatthew Wilcox static inline
740a77d19f4SMatthew Wilcox unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
7414b4bb46dSJan Kara {
7424b4bb46dSJan Kara 	unsigned long address;
7434b4bb46dSJan Kara 
7444b4bb46dSJan Kara 	address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
7454b4bb46dSJan Kara 	VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
7464b4bb46dSJan Kara 	return address;
7474b4bb46dSJan Kara }
7484b4bb46dSJan Kara 
7494b4bb46dSJan Kara /* Walk all mappings of a given index of a file and writeprotect them */
750a77d19f4SMatthew Wilcox static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
751a77d19f4SMatthew Wilcox 		unsigned long pfn)
7524b4bb46dSJan Kara {
7534b4bb46dSJan Kara 	struct vm_area_struct *vma;
754f729c8c9SRoss Zwisler 	pte_t pte, *ptep = NULL;
755f729c8c9SRoss Zwisler 	pmd_t *pmdp = NULL;
7564b4bb46dSJan Kara 	spinlock_t *ptl;
7574b4bb46dSJan Kara 
7584b4bb46dSJan Kara 	i_mmap_lock_read(mapping);
7594b4bb46dSJan Kara 	vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
760a4d1a885SJérôme Glisse 		unsigned long address, start, end;
7614b4bb46dSJan Kara 
7624b4bb46dSJan Kara 		cond_resched();
7634b4bb46dSJan Kara 
7644b4bb46dSJan Kara 		if (!(vma->vm_flags & VM_SHARED))
7654b4bb46dSJan Kara 			continue;
7664b4bb46dSJan Kara 
7674b4bb46dSJan Kara 		address = pgoff_address(index, vma);
768a4d1a885SJérôme Glisse 
769a4d1a885SJérôme Glisse 		/*
770a4d1a885SJérôme Glisse 		 * Note because we provide start/end to follow_pte_pmd it will
771a4d1a885SJérôme Glisse 		 * call mmu_notifier_invalidate_range_start() on our behalf
772a4d1a885SJérôme Glisse 		 * before taking any lock.
773a4d1a885SJérôme Glisse 		 */
774a4d1a885SJérôme Glisse 		if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
7754b4bb46dSJan Kara 			continue;
776f729c8c9SRoss Zwisler 
7770f10851eSJérôme Glisse 		/*
7780f10851eSJérôme Glisse 		 * No need to call mmu_notifier_invalidate_range() as we are
7790f10851eSJérôme Glisse 		 * downgrading page table protection not changing it to point
7800f10851eSJérôme Glisse 		 * to a new page.
7810f10851eSJérôme Glisse 		 *
782ad56b738SMike Rapoport 		 * See Documentation/vm/mmu_notifier.rst
7830f10851eSJérôme Glisse 		 */
784f729c8c9SRoss Zwisler 		if (pmdp) {
785f729c8c9SRoss Zwisler #ifdef CONFIG_FS_DAX_PMD
786f729c8c9SRoss Zwisler 			pmd_t pmd;
787f729c8c9SRoss Zwisler 
788f729c8c9SRoss Zwisler 			if (pfn != pmd_pfn(*pmdp))
789f729c8c9SRoss Zwisler 				goto unlock_pmd;
790f6f37321SLinus Torvalds 			if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
791f729c8c9SRoss Zwisler 				goto unlock_pmd;
792f729c8c9SRoss Zwisler 
793f729c8c9SRoss Zwisler 			flush_cache_page(vma, address, pfn);
794f729c8c9SRoss Zwisler 			pmd = pmdp_huge_clear_flush(vma, address, pmdp);
795f729c8c9SRoss Zwisler 			pmd = pmd_wrprotect(pmd);
796f729c8c9SRoss Zwisler 			pmd = pmd_mkclean(pmd);
797f729c8c9SRoss Zwisler 			set_pmd_at(vma->vm_mm, address, pmdp, pmd);
798f729c8c9SRoss Zwisler unlock_pmd:
799f729c8c9SRoss Zwisler #endif
800ee190ca6SJan H. Schönherr 			spin_unlock(ptl);
801f729c8c9SRoss Zwisler 		} else {
8024b4bb46dSJan Kara 			if (pfn != pte_pfn(*ptep))
803f729c8c9SRoss Zwisler 				goto unlock_pte;
8044b4bb46dSJan Kara 			if (!pte_dirty(*ptep) && !pte_write(*ptep))
805f729c8c9SRoss Zwisler 				goto unlock_pte;
8064b4bb46dSJan Kara 
8074b4bb46dSJan Kara 			flush_cache_page(vma, address, pfn);
8084b4bb46dSJan Kara 			pte = ptep_clear_flush(vma, address, ptep);
8094b4bb46dSJan Kara 			pte = pte_wrprotect(pte);
8104b4bb46dSJan Kara 			pte = pte_mkclean(pte);
8114b4bb46dSJan Kara 			set_pte_at(vma->vm_mm, address, ptep, pte);
812f729c8c9SRoss Zwisler unlock_pte:
8134b4bb46dSJan Kara 			pte_unmap_unlock(ptep, ptl);
814f729c8c9SRoss Zwisler 		}
8154b4bb46dSJan Kara 
816a4d1a885SJérôme Glisse 		mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
8174b4bb46dSJan Kara 	}
8184b4bb46dSJan Kara 	i_mmap_unlock_read(mapping);
8194b4bb46dSJan Kara }
8204b4bb46dSJan Kara 
8219fc747f6SMatthew Wilcox static int dax_writeback_one(struct xa_state *xas, struct dax_device *dax_dev,
8229fc747f6SMatthew Wilcox 		struct address_space *mapping, void *entry)
8239973c98eSRoss Zwisler {
8243fe0791cSDan Williams 	unsigned long pfn;
8253fe0791cSDan Williams 	long ret = 0;
826cccbce67SDan Williams 	size_t size;
8279973c98eSRoss Zwisler 
8289973c98eSRoss Zwisler 	/*
829a6abc2c0SJan Kara 	 * A page got tagged dirty in DAX mapping? Something is seriously
830a6abc2c0SJan Kara 	 * wrong.
8319973c98eSRoss Zwisler 	 */
8323159f943SMatthew Wilcox 	if (WARN_ON(!xa_is_value(entry)))
833a6abc2c0SJan Kara 		return -EIO;
8349973c98eSRoss Zwisler 
8359fc747f6SMatthew Wilcox 	if (unlikely(dax_is_locked(entry))) {
8369fc747f6SMatthew Wilcox 		void *old_entry = entry;
8379fc747f6SMatthew Wilcox 
8389fc747f6SMatthew Wilcox 		entry = get_unlocked_entry(xas);
8399fc747f6SMatthew Wilcox 
840a6abc2c0SJan Kara 		/* Entry got punched out / reallocated? */
8419fc747f6SMatthew Wilcox 		if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
842a6abc2c0SJan Kara 			goto put_unlocked;
843a6abc2c0SJan Kara 		/*
8449fc747f6SMatthew Wilcox 		 * Entry got reallocated elsewhere? No need to writeback.
8459fc747f6SMatthew Wilcox 		 * We have to compare pfns as we must not bail out due to
8469fc747f6SMatthew Wilcox 		 * difference in lockbit or entry type.
847a6abc2c0SJan Kara 		 */
8489fc747f6SMatthew Wilcox 		if (dax_to_pfn(old_entry) != dax_to_pfn(entry))
849a6abc2c0SJan Kara 			goto put_unlocked;
850642261acSRoss Zwisler 		if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
851642261acSRoss Zwisler 					dax_is_zero_entry(entry))) {
8529973c98eSRoss Zwisler 			ret = -EIO;
853a6abc2c0SJan Kara 			goto put_unlocked;
8549973c98eSRoss Zwisler 		}
8559973c98eSRoss Zwisler 
8569fc747f6SMatthew Wilcox 		/* Another fsync thread may have already done this entry */
8579fc747f6SMatthew Wilcox 		if (!xas_get_mark(xas, PAGECACHE_TAG_TOWRITE))
858a6abc2c0SJan Kara 			goto put_unlocked;
8599fc747f6SMatthew Wilcox 	}
8609fc747f6SMatthew Wilcox 
861a6abc2c0SJan Kara 	/* Lock the entry to serialize with page faults */
8629fc747f6SMatthew Wilcox 	dax_lock_entry(xas, entry);
8639fc747f6SMatthew Wilcox 
864a6abc2c0SJan Kara 	/*
865a6abc2c0SJan Kara 	 * We can clear the tag now but we have to be careful so that concurrent
866a6abc2c0SJan Kara 	 * dax_writeback_one() calls for the same index cannot finish before we
867a6abc2c0SJan Kara 	 * actually flush the caches. This is achieved as the calls will look
868b93b0163SMatthew Wilcox 	 * at the entry only under the i_pages lock and once they do that
869b93b0163SMatthew Wilcox 	 * they will see the entry locked and wait for it to unlock.
870a6abc2c0SJan Kara 	 */
8719fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_TOWRITE);
8729fc747f6SMatthew Wilcox 	xas_unlock_irq(xas);
873a6abc2c0SJan Kara 
874642261acSRoss Zwisler 	/*
875642261acSRoss Zwisler 	 * Even if dax_writeback_mapping_range() was given a wbc->range_start
876642261acSRoss Zwisler 	 * in the middle of a PMD, the 'index' we are given will be aligned to
8773fe0791cSDan Williams 	 * the start index of the PMD, as will the pfn we pull from 'entry'.
8783fe0791cSDan Williams 	 * This allows us to flush for PMD_SIZE and not have to worry about
8793fe0791cSDan Williams 	 * partial PMD writebacks.
880642261acSRoss Zwisler 	 */
881a77d19f4SMatthew Wilcox 	pfn = dax_to_pfn(entry);
882a77d19f4SMatthew Wilcox 	size = PAGE_SIZE << dax_entry_order(entry);
883cccbce67SDan Williams 
8849fc747f6SMatthew Wilcox 	dax_entry_mkclean(mapping, xas->xa_index, pfn);
8853fe0791cSDan Williams 	dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
8864b4bb46dSJan Kara 	/*
8874b4bb46dSJan Kara 	 * After we have flushed the cache, we can clear the dirty tag. There
8884b4bb46dSJan Kara 	 * cannot be new dirty data in the pfn after the flush has completed as
8894b4bb46dSJan Kara 	 * the pfn mappings are writeprotected and fault waits for mapping
8904b4bb46dSJan Kara 	 * entry lock.
8914b4bb46dSJan Kara 	 */
8929fc747f6SMatthew Wilcox 	xas_reset(xas);
8939fc747f6SMatthew Wilcox 	xas_lock_irq(xas);
8949fc747f6SMatthew Wilcox 	xas_store(xas, entry);
8959fc747f6SMatthew Wilcox 	xas_clear_mark(xas, PAGECACHE_TAG_DIRTY);
8969fc747f6SMatthew Wilcox 	dax_wake_entry(xas, entry, false);
8979fc747f6SMatthew Wilcox 
8989fc747f6SMatthew Wilcox 	trace_dax_writeback_one(mapping->host, xas->xa_index,
8999fc747f6SMatthew Wilcox 			size >> PAGE_SHIFT);
9009973c98eSRoss Zwisler 	return ret;
9019973c98eSRoss Zwisler 
902a6abc2c0SJan Kara  put_unlocked:
9039fc747f6SMatthew Wilcox 	put_unlocked_entry(xas, entry);
9049973c98eSRoss Zwisler 	return ret;
9059973c98eSRoss Zwisler }
9069973c98eSRoss Zwisler 
9079973c98eSRoss Zwisler /*
9089973c98eSRoss Zwisler  * Flush the mapping to the persistent domain within the byte range of [start,
9099973c98eSRoss Zwisler  * end]. This is required by data integrity operations to ensure file data is
9109973c98eSRoss Zwisler  * on persistent storage prior to completion of the operation.
9119973c98eSRoss Zwisler  */
9127f6d5b52SRoss Zwisler int dax_writeback_mapping_range(struct address_space *mapping,
9137f6d5b52SRoss Zwisler 		struct block_device *bdev, struct writeback_control *wbc)
9149973c98eSRoss Zwisler {
9159fc747f6SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, wbc->range_start >> PAGE_SHIFT);
9169973c98eSRoss Zwisler 	struct inode *inode = mapping->host;
9179fc747f6SMatthew Wilcox 	pgoff_t end_index = wbc->range_end >> PAGE_SHIFT;
918cccbce67SDan Williams 	struct dax_device *dax_dev;
9199fc747f6SMatthew Wilcox 	void *entry;
9209fc747f6SMatthew Wilcox 	int ret = 0;
9219fc747f6SMatthew Wilcox 	unsigned int scanned = 0;
9229973c98eSRoss Zwisler 
9239973c98eSRoss Zwisler 	if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
9249973c98eSRoss Zwisler 		return -EIO;
9259973c98eSRoss Zwisler 
9267f6d5b52SRoss Zwisler 	if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
9277f6d5b52SRoss Zwisler 		return 0;
9287f6d5b52SRoss Zwisler 
929cccbce67SDan Williams 	dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
930cccbce67SDan Williams 	if (!dax_dev)
931cccbce67SDan Williams 		return -EIO;
932cccbce67SDan Williams 
9339fc747f6SMatthew Wilcox 	trace_dax_writeback_range(inode, xas.xa_index, end_index);
9349973c98eSRoss Zwisler 
9359fc747f6SMatthew Wilcox 	tag_pages_for_writeback(mapping, xas.xa_index, end_index);
936d14a3f48SRoss Zwisler 
9379fc747f6SMatthew Wilcox 	xas_lock_irq(&xas);
9389fc747f6SMatthew Wilcox 	xas_for_each_marked(&xas, entry, end_index, PAGECACHE_TAG_TOWRITE) {
9399fc747f6SMatthew Wilcox 		ret = dax_writeback_one(&xas, dax_dev, mapping, entry);
940819ec6b9SJeff Layton 		if (ret < 0) {
941819ec6b9SJeff Layton 			mapping_set_error(mapping, ret);
9429fc747f6SMatthew Wilcox 			break;
943d14a3f48SRoss Zwisler 		}
9449fc747f6SMatthew Wilcox 		if (++scanned % XA_CHECK_SCHED)
9459fc747f6SMatthew Wilcox 			continue;
9469fc747f6SMatthew Wilcox 
9479fc747f6SMatthew Wilcox 		xas_pause(&xas);
9489fc747f6SMatthew Wilcox 		xas_unlock_irq(&xas);
9499fc747f6SMatthew Wilcox 		cond_resched();
9509fc747f6SMatthew Wilcox 		xas_lock_irq(&xas);
951d14a3f48SRoss Zwisler 	}
9529fc747f6SMatthew Wilcox 	xas_unlock_irq(&xas);
953cccbce67SDan Williams 	put_dax(dax_dev);
9549fc747f6SMatthew Wilcox 	trace_dax_writeback_range_done(inode, xas.xa_index, end_index);
9559fc747f6SMatthew Wilcox 	return ret;
9569973c98eSRoss Zwisler }
9579973c98eSRoss Zwisler EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
9589973c98eSRoss Zwisler 
95931a6f1a6SJan Kara static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
960f7ca90b1SMatthew Wilcox {
961a3841f94SLinus Torvalds 	return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
96231a6f1a6SJan Kara }
963f7ca90b1SMatthew Wilcox 
9645e161e40SJan Kara static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
9655e161e40SJan Kara 			 pfn_t *pfnp)
9665e161e40SJan Kara {
9675e161e40SJan Kara 	const sector_t sector = dax_iomap_sector(iomap, pos);
9685e161e40SJan Kara 	pgoff_t pgoff;
9695e161e40SJan Kara 	int id, rc;
9705e161e40SJan Kara 	long length;
9715e161e40SJan Kara 
9725e161e40SJan Kara 	rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
973cccbce67SDan Williams 	if (rc)
974cccbce67SDan Williams 		return rc;
975cccbce67SDan Williams 	id = dax_read_lock();
9765e161e40SJan Kara 	length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
97786ed913bSHuaisheng Ye 				   NULL, pfnp);
9785e161e40SJan Kara 	if (length < 0) {
9795e161e40SJan Kara 		rc = length;
9805e161e40SJan Kara 		goto out;
9815e161e40SJan Kara 	}
9825e161e40SJan Kara 	rc = -EINVAL;
9835e161e40SJan Kara 	if (PFN_PHYS(length) < size)
9845e161e40SJan Kara 		goto out;
9855e161e40SJan Kara 	if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
9865e161e40SJan Kara 		goto out;
9875e161e40SJan Kara 	/* For larger pages we need devmap */
9885e161e40SJan Kara 	if (length > 1 && !pfn_t_devmap(*pfnp))
9895e161e40SJan Kara 		goto out;
9905e161e40SJan Kara 	rc = 0;
9915e161e40SJan Kara out:
992cccbce67SDan Williams 	dax_read_unlock(id);
993cccbce67SDan Williams 	return rc;
994cccbce67SDan Williams }
995f7ca90b1SMatthew Wilcox 
9962f89dc12SJan Kara /*
99791d25ba8SRoss Zwisler  * The user has performed a load from a hole in the file.  Allocating a new
99891d25ba8SRoss Zwisler  * page in the file would cause excessive storage usage for workloads with
99991d25ba8SRoss Zwisler  * sparse files.  Instead we insert a read-only mapping of the 4k zero page.
100091d25ba8SRoss Zwisler  * If this page is ever written to we will re-fault and change the mapping to
100191d25ba8SRoss Zwisler  * point to real DAX storage instead.
10022f89dc12SJan Kara  */
1003b15cd800SMatthew Wilcox static vm_fault_t dax_load_hole(struct xa_state *xas,
1004b15cd800SMatthew Wilcox 		struct address_space *mapping, void **entry,
1005e30331ffSRoss Zwisler 		struct vm_fault *vmf)
1006e30331ffSRoss Zwisler {
1007e30331ffSRoss Zwisler 	struct inode *inode = mapping->host;
100891d25ba8SRoss Zwisler 	unsigned long vaddr = vmf->address;
1009b90ca5ccSMatthew Wilcox 	pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1010b90ca5ccSMatthew Wilcox 	vm_fault_t ret;
1011e30331ffSRoss Zwisler 
1012b15cd800SMatthew Wilcox 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
10133159f943SMatthew Wilcox 			DAX_ZERO_PAGE, false);
10143159f943SMatthew Wilcox 
1015ab77dab4SSouptick Joarder 	ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
1016e30331ffSRoss Zwisler 	trace_dax_load_hole(inode, vmf, ret);
1017e30331ffSRoss Zwisler 	return ret;
1018e30331ffSRoss Zwisler }
1019e30331ffSRoss Zwisler 
10204b0228faSVishal Verma static bool dax_range_is_aligned(struct block_device *bdev,
10214b0228faSVishal Verma 				 unsigned int offset, unsigned int length)
10224b0228faSVishal Verma {
10234b0228faSVishal Verma 	unsigned short sector_size = bdev_logical_block_size(bdev);
10244b0228faSVishal Verma 
10254b0228faSVishal Verma 	if (!IS_ALIGNED(offset, sector_size))
10264b0228faSVishal Verma 		return false;
10274b0228faSVishal Verma 	if (!IS_ALIGNED(length, sector_size))
10284b0228faSVishal Verma 		return false;
10294b0228faSVishal Verma 
10304b0228faSVishal Verma 	return true;
10314b0228faSVishal Verma }
10324b0228faSVishal Verma 
1033cccbce67SDan Williams int __dax_zero_page_range(struct block_device *bdev,
1034cccbce67SDan Williams 		struct dax_device *dax_dev, sector_t sector,
1035cccbce67SDan Williams 		unsigned int offset, unsigned int size)
1036679c8bd3SChristoph Hellwig {
1037cccbce67SDan Williams 	if (dax_range_is_aligned(bdev, offset, size)) {
1038cccbce67SDan Williams 		sector_t start_sector = sector + (offset >> 9);
10394b0228faSVishal Verma 
10404b0228faSVishal Verma 		return blkdev_issue_zeroout(bdev, start_sector,
104153ef7d0eSLinus Torvalds 				size >> 9, GFP_NOFS, 0);
10424b0228faSVishal Verma 	} else {
1043cccbce67SDan Williams 		pgoff_t pgoff;
1044cccbce67SDan Williams 		long rc, id;
1045cccbce67SDan Williams 		void *kaddr;
1046cccbce67SDan Williams 
1047e84b83b9SDan Williams 		rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
1048cccbce67SDan Williams 		if (rc)
1049cccbce67SDan Williams 			return rc;
1050cccbce67SDan Williams 
1051cccbce67SDan Williams 		id = dax_read_lock();
105286ed913bSHuaisheng Ye 		rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
1053cccbce67SDan Williams 		if (rc < 0) {
1054cccbce67SDan Williams 			dax_read_unlock(id);
1055cccbce67SDan Williams 			return rc;
1056cccbce67SDan Williams 		}
105781f55870SDan Williams 		memset(kaddr + offset, 0, size);
1058c3ca015fSMikulas Patocka 		dax_flush(dax_dev, kaddr + offset, size);
1059cccbce67SDan Williams 		dax_read_unlock(id);
10604b0228faSVishal Verma 	}
1061679c8bd3SChristoph Hellwig 	return 0;
1062679c8bd3SChristoph Hellwig }
1063679c8bd3SChristoph Hellwig EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1064679c8bd3SChristoph Hellwig 
1065a254e568SChristoph Hellwig static loff_t
106611c59c92SRoss Zwisler dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1067a254e568SChristoph Hellwig 		struct iomap *iomap)
1068a254e568SChristoph Hellwig {
1069cccbce67SDan Williams 	struct block_device *bdev = iomap->bdev;
1070cccbce67SDan Williams 	struct dax_device *dax_dev = iomap->dax_dev;
1071a254e568SChristoph Hellwig 	struct iov_iter *iter = data;
1072a254e568SChristoph Hellwig 	loff_t end = pos + length, done = 0;
1073a254e568SChristoph Hellwig 	ssize_t ret = 0;
1074a77d4786SDan Williams 	size_t xfer;
1075cccbce67SDan Williams 	int id;
1076a254e568SChristoph Hellwig 
1077a254e568SChristoph Hellwig 	if (iov_iter_rw(iter) == READ) {
1078a254e568SChristoph Hellwig 		end = min(end, i_size_read(inode));
1079a254e568SChristoph Hellwig 		if (pos >= end)
1080a254e568SChristoph Hellwig 			return 0;
1081a254e568SChristoph Hellwig 
1082a254e568SChristoph Hellwig 		if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1083a254e568SChristoph Hellwig 			return iov_iter_zero(min(length, end - pos), iter);
1084a254e568SChristoph Hellwig 	}
1085a254e568SChristoph Hellwig 
1086a254e568SChristoph Hellwig 	if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1087a254e568SChristoph Hellwig 		return -EIO;
1088a254e568SChristoph Hellwig 
1089e3fce68cSJan Kara 	/*
1090e3fce68cSJan Kara 	 * Write can allocate block for an area which has a hole page mapped
1091e3fce68cSJan Kara 	 * into page tables. We have to tear down these mappings so that data
1092e3fce68cSJan Kara 	 * written by write(2) is visible in mmap.
1093e3fce68cSJan Kara 	 */
1094cd656375SJan Kara 	if (iomap->flags & IOMAP_F_NEW) {
1095e3fce68cSJan Kara 		invalidate_inode_pages2_range(inode->i_mapping,
1096e3fce68cSJan Kara 					      pos >> PAGE_SHIFT,
1097e3fce68cSJan Kara 					      (end - 1) >> PAGE_SHIFT);
1098e3fce68cSJan Kara 	}
1099e3fce68cSJan Kara 
1100cccbce67SDan Williams 	id = dax_read_lock();
1101a254e568SChristoph Hellwig 	while (pos < end) {
1102a254e568SChristoph Hellwig 		unsigned offset = pos & (PAGE_SIZE - 1);
1103cccbce67SDan Williams 		const size_t size = ALIGN(length + offset, PAGE_SIZE);
1104cccbce67SDan Williams 		const sector_t sector = dax_iomap_sector(iomap, pos);
1105a254e568SChristoph Hellwig 		ssize_t map_len;
1106cccbce67SDan Williams 		pgoff_t pgoff;
1107cccbce67SDan Williams 		void *kaddr;
1108a254e568SChristoph Hellwig 
1109d1908f52SMichal Hocko 		if (fatal_signal_pending(current)) {
1110d1908f52SMichal Hocko 			ret = -EINTR;
1111d1908f52SMichal Hocko 			break;
1112d1908f52SMichal Hocko 		}
1113d1908f52SMichal Hocko 
1114cccbce67SDan Williams 		ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1115cccbce67SDan Williams 		if (ret)
1116cccbce67SDan Williams 			break;
1117cccbce67SDan Williams 
1118cccbce67SDan Williams 		map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
111986ed913bSHuaisheng Ye 				&kaddr, NULL);
1120a254e568SChristoph Hellwig 		if (map_len < 0) {
1121a254e568SChristoph Hellwig 			ret = map_len;
1122a254e568SChristoph Hellwig 			break;
1123a254e568SChristoph Hellwig 		}
1124a254e568SChristoph Hellwig 
1125cccbce67SDan Williams 		map_len = PFN_PHYS(map_len);
1126cccbce67SDan Williams 		kaddr += offset;
1127a254e568SChristoph Hellwig 		map_len -= offset;
1128a254e568SChristoph Hellwig 		if (map_len > end - pos)
1129a254e568SChristoph Hellwig 			map_len = end - pos;
1130a254e568SChristoph Hellwig 
1131a2e050f5SRoss Zwisler 		/*
1132a2e050f5SRoss Zwisler 		 * The userspace address for the memory copy has already been
1133a2e050f5SRoss Zwisler 		 * validated via access_ok() in either vfs_read() or
1134a2e050f5SRoss Zwisler 		 * vfs_write(), depending on which operation we are doing.
1135a2e050f5SRoss Zwisler 		 */
1136a254e568SChristoph Hellwig 		if (iov_iter_rw(iter) == WRITE)
1137a77d4786SDan Williams 			xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
1138fec53774SDan Williams 					map_len, iter);
1139a254e568SChristoph Hellwig 		else
1140a77d4786SDan Williams 			xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
1141b3a9a0c3SDan Williams 					map_len, iter);
1142a254e568SChristoph Hellwig 
1143a77d4786SDan Williams 		pos += xfer;
1144a77d4786SDan Williams 		length -= xfer;
1145a77d4786SDan Williams 		done += xfer;
1146a77d4786SDan Williams 
1147a77d4786SDan Williams 		if (xfer == 0)
1148a77d4786SDan Williams 			ret = -EFAULT;
1149a77d4786SDan Williams 		if (xfer < map_len)
1150a77d4786SDan Williams 			break;
1151a254e568SChristoph Hellwig 	}
1152cccbce67SDan Williams 	dax_read_unlock(id);
1153a254e568SChristoph Hellwig 
1154a254e568SChristoph Hellwig 	return done ? done : ret;
1155a254e568SChristoph Hellwig }
1156a254e568SChristoph Hellwig 
1157a254e568SChristoph Hellwig /**
115811c59c92SRoss Zwisler  * dax_iomap_rw - Perform I/O to a DAX file
1159a254e568SChristoph Hellwig  * @iocb:	The control block for this I/O
1160a254e568SChristoph Hellwig  * @iter:	The addresses to do I/O from or to
1161a254e568SChristoph Hellwig  * @ops:	iomap ops passed from the file system
1162a254e568SChristoph Hellwig  *
1163a254e568SChristoph Hellwig  * This function performs read and write operations to directly mapped
1164a254e568SChristoph Hellwig  * persistent memory.  The callers needs to take care of read/write exclusion
1165a254e568SChristoph Hellwig  * and evicting any page cache pages in the region under I/O.
1166a254e568SChristoph Hellwig  */
1167a254e568SChristoph Hellwig ssize_t
116811c59c92SRoss Zwisler dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
11698ff6daa1SChristoph Hellwig 		const struct iomap_ops *ops)
1170a254e568SChristoph Hellwig {
1171a254e568SChristoph Hellwig 	struct address_space *mapping = iocb->ki_filp->f_mapping;
1172a254e568SChristoph Hellwig 	struct inode *inode = mapping->host;
1173a254e568SChristoph Hellwig 	loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1174a254e568SChristoph Hellwig 	unsigned flags = 0;
1175a254e568SChristoph Hellwig 
1176168316dbSChristoph Hellwig 	if (iov_iter_rw(iter) == WRITE) {
1177168316dbSChristoph Hellwig 		lockdep_assert_held_exclusive(&inode->i_rwsem);
1178a254e568SChristoph Hellwig 		flags |= IOMAP_WRITE;
1179168316dbSChristoph Hellwig 	} else {
1180168316dbSChristoph Hellwig 		lockdep_assert_held(&inode->i_rwsem);
1181168316dbSChristoph Hellwig 	}
1182a254e568SChristoph Hellwig 
1183a254e568SChristoph Hellwig 	while (iov_iter_count(iter)) {
1184a254e568SChristoph Hellwig 		ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
118511c59c92SRoss Zwisler 				iter, dax_iomap_actor);
1186a254e568SChristoph Hellwig 		if (ret <= 0)
1187a254e568SChristoph Hellwig 			break;
1188a254e568SChristoph Hellwig 		pos += ret;
1189a254e568SChristoph Hellwig 		done += ret;
1190a254e568SChristoph Hellwig 	}
1191a254e568SChristoph Hellwig 
1192a254e568SChristoph Hellwig 	iocb->ki_pos += done;
1193a254e568SChristoph Hellwig 	return done ? done : ret;
1194a254e568SChristoph Hellwig }
119511c59c92SRoss Zwisler EXPORT_SYMBOL_GPL(dax_iomap_rw);
1196a7d73fe6SChristoph Hellwig 
1197ab77dab4SSouptick Joarder static vm_fault_t dax_fault_return(int error)
11989f141d6eSJan Kara {
11999f141d6eSJan Kara 	if (error == 0)
12009f141d6eSJan Kara 		return VM_FAULT_NOPAGE;
12019f141d6eSJan Kara 	if (error == -ENOMEM)
12029f141d6eSJan Kara 		return VM_FAULT_OOM;
12039f141d6eSJan Kara 	return VM_FAULT_SIGBUS;
12049f141d6eSJan Kara }
12059f141d6eSJan Kara 
1206aaa422c4SDan Williams /*
1207aaa422c4SDan Williams  * MAP_SYNC on a dax mapping guarantees dirty metadata is
1208aaa422c4SDan Williams  * flushed on write-faults (non-cow), but not read-faults.
1209aaa422c4SDan Williams  */
1210aaa422c4SDan Williams static bool dax_fault_is_synchronous(unsigned long flags,
1211aaa422c4SDan Williams 		struct vm_area_struct *vma, struct iomap *iomap)
1212aaa422c4SDan Williams {
1213aaa422c4SDan Williams 	return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1214aaa422c4SDan Williams 		&& (iomap->flags & IOMAP_F_DIRTY);
1215aaa422c4SDan Williams }
1216aaa422c4SDan Williams 
1217ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
1218c0b24625SJan Kara 			       int *iomap_errp, const struct iomap_ops *ops)
1219a7d73fe6SChristoph Hellwig {
1220a0987ad5SJan Kara 	struct vm_area_struct *vma = vmf->vma;
1221a0987ad5SJan Kara 	struct address_space *mapping = vma->vm_file->f_mapping;
1222b15cd800SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, vmf->pgoff);
1223a7d73fe6SChristoph Hellwig 	struct inode *inode = mapping->host;
12241a29d85eSJan Kara 	unsigned long vaddr = vmf->address;
1225a7d73fe6SChristoph Hellwig 	loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
1226a7d73fe6SChristoph Hellwig 	struct iomap iomap = { 0 };
12279484ab1bSJan Kara 	unsigned flags = IOMAP_FAULT;
1228a7d73fe6SChristoph Hellwig 	int error, major = 0;
1229d2c43ef1SJan Kara 	bool write = vmf->flags & FAULT_FLAG_WRITE;
1230caa51d26SJan Kara 	bool sync;
1231ab77dab4SSouptick Joarder 	vm_fault_t ret = 0;
1232a7d73fe6SChristoph Hellwig 	void *entry;
12331b5a1cb2SJan Kara 	pfn_t pfn;
1234a7d73fe6SChristoph Hellwig 
1235ab77dab4SSouptick Joarder 	trace_dax_pte_fault(inode, vmf, ret);
1236a7d73fe6SChristoph Hellwig 	/*
1237a7d73fe6SChristoph Hellwig 	 * Check whether offset isn't beyond end of file now. Caller is supposed
1238a7d73fe6SChristoph Hellwig 	 * to hold locks serializing us with truncate / punch hole so this is
1239a7d73fe6SChristoph Hellwig 	 * a reliable test.
1240a7d73fe6SChristoph Hellwig 	 */
1241a9c42b33SRoss Zwisler 	if (pos >= i_size_read(inode)) {
1242ab77dab4SSouptick Joarder 		ret = VM_FAULT_SIGBUS;
1243a9c42b33SRoss Zwisler 		goto out;
1244a9c42b33SRoss Zwisler 	}
1245a7d73fe6SChristoph Hellwig 
1246d2c43ef1SJan Kara 	if (write && !vmf->cow_page)
1247a7d73fe6SChristoph Hellwig 		flags |= IOMAP_WRITE;
1248a7d73fe6SChristoph Hellwig 
1249b15cd800SMatthew Wilcox 	entry = grab_mapping_entry(&xas, mapping, 0);
1250b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1251b15cd800SMatthew Wilcox 		ret = xa_to_internal(entry);
125213e451fdSJan Kara 		goto out;
125313e451fdSJan Kara 	}
125413e451fdSJan Kara 
1255a7d73fe6SChristoph Hellwig 	/*
1256e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1257e2093926SRoss Zwisler 	 * mappings, that we have raced with a PMD fault that overlaps with
1258e2093926SRoss Zwisler 	 * the PTE we need to set up.  If so just return and the fault will be
1259e2093926SRoss Zwisler 	 * retried.
1260e2093926SRoss Zwisler 	 */
1261e2093926SRoss Zwisler 	if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
1262ab77dab4SSouptick Joarder 		ret = VM_FAULT_NOPAGE;
1263e2093926SRoss Zwisler 		goto unlock_entry;
1264e2093926SRoss Zwisler 	}
1265e2093926SRoss Zwisler 
1266e2093926SRoss Zwisler 	/*
1267a7d73fe6SChristoph Hellwig 	 * Note that we don't bother to use iomap_apply here: DAX required
1268a7d73fe6SChristoph Hellwig 	 * the file system block size to be equal the page size, which means
1269a7d73fe6SChristoph Hellwig 	 * that we never have to deal with more than a single extent here.
1270a7d73fe6SChristoph Hellwig 	 */
1271a7d73fe6SChristoph Hellwig 	error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
1272c0b24625SJan Kara 	if (iomap_errp)
1273c0b24625SJan Kara 		*iomap_errp = error;
1274a9c42b33SRoss Zwisler 	if (error) {
1275ab77dab4SSouptick Joarder 		ret = dax_fault_return(error);
127613e451fdSJan Kara 		goto unlock_entry;
1277a9c42b33SRoss Zwisler 	}
1278a7d73fe6SChristoph Hellwig 	if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
127913e451fdSJan Kara 		error = -EIO;	/* fs corruption? */
128013e451fdSJan Kara 		goto error_finish_iomap;
1281a7d73fe6SChristoph Hellwig 	}
1282a7d73fe6SChristoph Hellwig 
1283a7d73fe6SChristoph Hellwig 	if (vmf->cow_page) {
128431a6f1a6SJan Kara 		sector_t sector = dax_iomap_sector(&iomap, pos);
128531a6f1a6SJan Kara 
1286a7d73fe6SChristoph Hellwig 		switch (iomap.type) {
1287a7d73fe6SChristoph Hellwig 		case IOMAP_HOLE:
1288a7d73fe6SChristoph Hellwig 		case IOMAP_UNWRITTEN:
1289a7d73fe6SChristoph Hellwig 			clear_user_highpage(vmf->cow_page, vaddr);
1290a7d73fe6SChristoph Hellwig 			break;
1291a7d73fe6SChristoph Hellwig 		case IOMAP_MAPPED:
1292cccbce67SDan Williams 			error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1293cccbce67SDan Williams 					sector, PAGE_SIZE, vmf->cow_page, vaddr);
1294a7d73fe6SChristoph Hellwig 			break;
1295a7d73fe6SChristoph Hellwig 		default:
1296a7d73fe6SChristoph Hellwig 			WARN_ON_ONCE(1);
1297a7d73fe6SChristoph Hellwig 			error = -EIO;
1298a7d73fe6SChristoph Hellwig 			break;
1299a7d73fe6SChristoph Hellwig 		}
1300a7d73fe6SChristoph Hellwig 
1301a7d73fe6SChristoph Hellwig 		if (error)
130213e451fdSJan Kara 			goto error_finish_iomap;
1303b1aa812bSJan Kara 
1304b1aa812bSJan Kara 		__SetPageUptodate(vmf->cow_page);
1305ab77dab4SSouptick Joarder 		ret = finish_fault(vmf);
1306ab77dab4SSouptick Joarder 		if (!ret)
1307ab77dab4SSouptick Joarder 			ret = VM_FAULT_DONE_COW;
130813e451fdSJan Kara 		goto finish_iomap;
1309a7d73fe6SChristoph Hellwig 	}
1310a7d73fe6SChristoph Hellwig 
1311aaa422c4SDan Williams 	sync = dax_fault_is_synchronous(flags, vma, &iomap);
1312caa51d26SJan Kara 
1313a7d73fe6SChristoph Hellwig 	switch (iomap.type) {
1314a7d73fe6SChristoph Hellwig 	case IOMAP_MAPPED:
1315a7d73fe6SChristoph Hellwig 		if (iomap.flags & IOMAP_F_NEW) {
1316a7d73fe6SChristoph Hellwig 			count_vm_event(PGMAJFAULT);
1317a0987ad5SJan Kara 			count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
1318a7d73fe6SChristoph Hellwig 			major = VM_FAULT_MAJOR;
1319a7d73fe6SChristoph Hellwig 		}
13201b5a1cb2SJan Kara 		error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
13211b5a1cb2SJan Kara 		if (error < 0)
13221b5a1cb2SJan Kara 			goto error_finish_iomap;
13231b5a1cb2SJan Kara 
1324b15cd800SMatthew Wilcox 		entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
1325caa51d26SJan Kara 						 0, write && !sync);
13261b5a1cb2SJan Kara 
1327caa51d26SJan Kara 		/*
1328caa51d26SJan Kara 		 * If we are doing synchronous page fault and inode needs fsync,
1329caa51d26SJan Kara 		 * we can insert PTE into page tables only after that happens.
1330caa51d26SJan Kara 		 * Skip insertion for now and return the pfn so that caller can
1331caa51d26SJan Kara 		 * insert it after fsync is done.
1332caa51d26SJan Kara 		 */
1333caa51d26SJan Kara 		if (sync) {
1334caa51d26SJan Kara 			if (WARN_ON_ONCE(!pfnp)) {
1335caa51d26SJan Kara 				error = -EIO;
1336caa51d26SJan Kara 				goto error_finish_iomap;
1337caa51d26SJan Kara 			}
1338caa51d26SJan Kara 			*pfnp = pfn;
1339ab77dab4SSouptick Joarder 			ret = VM_FAULT_NEEDDSYNC | major;
1340caa51d26SJan Kara 			goto finish_iomap;
1341caa51d26SJan Kara 		}
13421b5a1cb2SJan Kara 		trace_dax_insert_mapping(inode, vmf, entry);
13431b5a1cb2SJan Kara 		if (write)
1344ab77dab4SSouptick Joarder 			ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
13451b5a1cb2SJan Kara 		else
1346ab77dab4SSouptick Joarder 			ret = vmf_insert_mixed(vma, vaddr, pfn);
13471b5a1cb2SJan Kara 
1348ab77dab4SSouptick Joarder 		goto finish_iomap;
1349a7d73fe6SChristoph Hellwig 	case IOMAP_UNWRITTEN:
1350a7d73fe6SChristoph Hellwig 	case IOMAP_HOLE:
1351d2c43ef1SJan Kara 		if (!write) {
1352b15cd800SMatthew Wilcox 			ret = dax_load_hole(&xas, mapping, &entry, vmf);
135313e451fdSJan Kara 			goto finish_iomap;
13541550290bSRoss Zwisler 		}
1355a7d73fe6SChristoph Hellwig 		/*FALLTHRU*/
1356a7d73fe6SChristoph Hellwig 	default:
1357a7d73fe6SChristoph Hellwig 		WARN_ON_ONCE(1);
1358a7d73fe6SChristoph Hellwig 		error = -EIO;
1359a7d73fe6SChristoph Hellwig 		break;
1360a7d73fe6SChristoph Hellwig 	}
1361a7d73fe6SChristoph Hellwig 
136213e451fdSJan Kara  error_finish_iomap:
1363ab77dab4SSouptick Joarder 	ret = dax_fault_return(error);
13649f141d6eSJan Kara  finish_iomap:
13659f141d6eSJan Kara 	if (ops->iomap_end) {
13669f141d6eSJan Kara 		int copied = PAGE_SIZE;
13679f141d6eSJan Kara 
1368ab77dab4SSouptick Joarder 		if (ret & VM_FAULT_ERROR)
13699f141d6eSJan Kara 			copied = 0;
13709f141d6eSJan Kara 		/*
13719f141d6eSJan Kara 		 * The fault is done by now and there's no way back (other
13729f141d6eSJan Kara 		 * thread may be already happily using PTE we have installed).
13739f141d6eSJan Kara 		 * Just ignore error from ->iomap_end since we cannot do much
13749f141d6eSJan Kara 		 * with it.
13759f141d6eSJan Kara 		 */
13769f141d6eSJan Kara 		ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
13771550290bSRoss Zwisler 	}
137813e451fdSJan Kara  unlock_entry:
1379b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1380a9c42b33SRoss Zwisler  out:
1381ab77dab4SSouptick Joarder 	trace_dax_pte_fault_done(inode, vmf, ret);
1382ab77dab4SSouptick Joarder 	return ret | major;
1383a7d73fe6SChristoph Hellwig }
1384642261acSRoss Zwisler 
1385642261acSRoss Zwisler #ifdef CONFIG_FS_DAX_PMD
1386b15cd800SMatthew Wilcox static vm_fault_t dax_pmd_load_hole(struct xa_state *xas, struct vm_fault *vmf,
1387b15cd800SMatthew Wilcox 		struct iomap *iomap, void **entry)
1388642261acSRoss Zwisler {
1389f4200391SDave Jiang 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1390f4200391SDave Jiang 	unsigned long pmd_addr = vmf->address & PMD_MASK;
1391653b2ea3SRoss Zwisler 	struct inode *inode = mapping->host;
1392642261acSRoss Zwisler 	struct page *zero_page;
1393642261acSRoss Zwisler 	spinlock_t *ptl;
1394642261acSRoss Zwisler 	pmd_t pmd_entry;
13953fe0791cSDan Williams 	pfn_t pfn;
1396642261acSRoss Zwisler 
1397f4200391SDave Jiang 	zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
1398642261acSRoss Zwisler 
1399642261acSRoss Zwisler 	if (unlikely(!zero_page))
1400653b2ea3SRoss Zwisler 		goto fallback;
1401642261acSRoss Zwisler 
14023fe0791cSDan Williams 	pfn = page_to_pfn_t(zero_page);
1403b15cd800SMatthew Wilcox 	*entry = dax_insert_entry(xas, mapping, vmf, *entry, pfn,
14043159f943SMatthew Wilcox 			DAX_PMD | DAX_ZERO_PAGE, false);
1405642261acSRoss Zwisler 
1406f4200391SDave Jiang 	ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1407f4200391SDave Jiang 	if (!pmd_none(*(vmf->pmd))) {
1408642261acSRoss Zwisler 		spin_unlock(ptl);
1409653b2ea3SRoss Zwisler 		goto fallback;
1410642261acSRoss Zwisler 	}
1411642261acSRoss Zwisler 
1412f4200391SDave Jiang 	pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
1413642261acSRoss Zwisler 	pmd_entry = pmd_mkhuge(pmd_entry);
1414f4200391SDave Jiang 	set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
1415642261acSRoss Zwisler 	spin_unlock(ptl);
1416b15cd800SMatthew Wilcox 	trace_dax_pmd_load_hole(inode, vmf, zero_page, *entry);
1417642261acSRoss Zwisler 	return VM_FAULT_NOPAGE;
1418653b2ea3SRoss Zwisler 
1419653b2ea3SRoss Zwisler fallback:
1420b15cd800SMatthew Wilcox 	trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, *entry);
1421642261acSRoss Zwisler 	return VM_FAULT_FALLBACK;
1422642261acSRoss Zwisler }
1423642261acSRoss Zwisler 
1424ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
1425a2d58167SDave Jiang 			       const struct iomap_ops *ops)
1426642261acSRoss Zwisler {
1427f4200391SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
1428642261acSRoss Zwisler 	struct address_space *mapping = vma->vm_file->f_mapping;
1429b15cd800SMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, PMD_ORDER);
1430d8a849e1SDave Jiang 	unsigned long pmd_addr = vmf->address & PMD_MASK;
1431d8a849e1SDave Jiang 	bool write = vmf->flags & FAULT_FLAG_WRITE;
1432caa51d26SJan Kara 	bool sync;
14339484ab1bSJan Kara 	unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
1434642261acSRoss Zwisler 	struct inode *inode = mapping->host;
1435ab77dab4SSouptick Joarder 	vm_fault_t result = VM_FAULT_FALLBACK;
1436642261acSRoss Zwisler 	struct iomap iomap = { 0 };
1437b15cd800SMatthew Wilcox 	pgoff_t max_pgoff;
1438642261acSRoss Zwisler 	void *entry;
1439642261acSRoss Zwisler 	loff_t pos;
1440642261acSRoss Zwisler 	int error;
1441302a5e31SJan Kara 	pfn_t pfn;
1442642261acSRoss Zwisler 
1443282a8e03SRoss Zwisler 	/*
1444282a8e03SRoss Zwisler 	 * Check whether offset isn't beyond end of file now. Caller is
1445282a8e03SRoss Zwisler 	 * supposed to hold locks serializing us with truncate / punch hole so
1446282a8e03SRoss Zwisler 	 * this is a reliable test.
1447282a8e03SRoss Zwisler 	 */
1448957ac8c4SJeff Moyer 	max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
1449282a8e03SRoss Zwisler 
1450f4200391SDave Jiang 	trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
1451282a8e03SRoss Zwisler 
1452fffa281bSRoss Zwisler 	/*
1453fffa281bSRoss Zwisler 	 * Make sure that the faulting address's PMD offset (color) matches
1454fffa281bSRoss Zwisler 	 * the PMD offset from the start of the file.  This is necessary so
1455fffa281bSRoss Zwisler 	 * that a PMD range in the page table overlaps exactly with a PMD
1456a77d19f4SMatthew Wilcox 	 * range in the page cache.
1457fffa281bSRoss Zwisler 	 */
1458fffa281bSRoss Zwisler 	if ((vmf->pgoff & PG_PMD_COLOUR) !=
1459fffa281bSRoss Zwisler 	    ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1460fffa281bSRoss Zwisler 		goto fallback;
1461fffa281bSRoss Zwisler 
1462642261acSRoss Zwisler 	/* Fall back to PTEs if we're going to COW */
1463642261acSRoss Zwisler 	if (write && !(vma->vm_flags & VM_SHARED))
1464642261acSRoss Zwisler 		goto fallback;
1465642261acSRoss Zwisler 
1466642261acSRoss Zwisler 	/* If the PMD would extend outside the VMA */
1467642261acSRoss Zwisler 	if (pmd_addr < vma->vm_start)
1468642261acSRoss Zwisler 		goto fallback;
1469642261acSRoss Zwisler 	if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1470642261acSRoss Zwisler 		goto fallback;
1471642261acSRoss Zwisler 
1472b15cd800SMatthew Wilcox 	if (xas.xa_index >= max_pgoff) {
1473282a8e03SRoss Zwisler 		result = VM_FAULT_SIGBUS;
1474282a8e03SRoss Zwisler 		goto out;
1475282a8e03SRoss Zwisler 	}
1476642261acSRoss Zwisler 
1477642261acSRoss Zwisler 	/* If the PMD would extend beyond the file size */
1478b15cd800SMatthew Wilcox 	if ((xas.xa_index | PG_PMD_COLOUR) >= max_pgoff)
1479642261acSRoss Zwisler 		goto fallback;
1480642261acSRoss Zwisler 
1481642261acSRoss Zwisler 	/*
1482b15cd800SMatthew Wilcox 	 * grab_mapping_entry() will make sure we get an empty PMD entry,
1483b15cd800SMatthew Wilcox 	 * a zero PMD entry or a DAX PMD.  If it can't (because a PTE
1484b15cd800SMatthew Wilcox 	 * entry is already in the array, for instance), it will return
1485b15cd800SMatthew Wilcox 	 * VM_FAULT_FALLBACK.
14869f141d6eSJan Kara 	 */
1487b15cd800SMatthew Wilcox 	entry = grab_mapping_entry(&xas, mapping, DAX_PMD);
1488b15cd800SMatthew Wilcox 	if (xa_is_internal(entry)) {
1489b15cd800SMatthew Wilcox 		result = xa_to_internal(entry);
1490876f2946SRoss Zwisler 		goto fallback;
1491b15cd800SMatthew Wilcox 	}
1492876f2946SRoss Zwisler 
1493876f2946SRoss Zwisler 	/*
1494e2093926SRoss Zwisler 	 * It is possible, particularly with mixed reads & writes to private
1495e2093926SRoss Zwisler 	 * mappings, that we have raced with a PTE fault that overlaps with
1496e2093926SRoss Zwisler 	 * the PMD we need to set up.  If so just return and the fault will be
1497e2093926SRoss Zwisler 	 * retried.
1498e2093926SRoss Zwisler 	 */
1499e2093926SRoss Zwisler 	if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1500e2093926SRoss Zwisler 			!pmd_devmap(*vmf->pmd)) {
1501e2093926SRoss Zwisler 		result = 0;
1502e2093926SRoss Zwisler 		goto unlock_entry;
1503e2093926SRoss Zwisler 	}
1504e2093926SRoss Zwisler 
1505e2093926SRoss Zwisler 	/*
1506876f2946SRoss Zwisler 	 * Note that we don't use iomap_apply here.  We aren't doing I/O, only
1507876f2946SRoss Zwisler 	 * setting up a mapping, so really we're using iomap_begin() as a way
1508876f2946SRoss Zwisler 	 * to look up our filesystem block.
1509876f2946SRoss Zwisler 	 */
1510b15cd800SMatthew Wilcox 	pos = (loff_t)xas.xa_index << PAGE_SHIFT;
1511876f2946SRoss Zwisler 	error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1512876f2946SRoss Zwisler 	if (error)
1513876f2946SRoss Zwisler 		goto unlock_entry;
1514876f2946SRoss Zwisler 
1515876f2946SRoss Zwisler 	if (iomap.offset + iomap.length < pos + PMD_SIZE)
15169f141d6eSJan Kara 		goto finish_iomap;
15179f141d6eSJan Kara 
1518aaa422c4SDan Williams 	sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
1519caa51d26SJan Kara 
1520642261acSRoss Zwisler 	switch (iomap.type) {
1521642261acSRoss Zwisler 	case IOMAP_MAPPED:
1522302a5e31SJan Kara 		error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1523302a5e31SJan Kara 		if (error < 0)
1524302a5e31SJan Kara 			goto finish_iomap;
1525302a5e31SJan Kara 
1526b15cd800SMatthew Wilcox 		entry = dax_insert_entry(&xas, mapping, vmf, entry, pfn,
15273159f943SMatthew Wilcox 						DAX_PMD, write && !sync);
1528302a5e31SJan Kara 
1529caa51d26SJan Kara 		/*
1530caa51d26SJan Kara 		 * If we are doing synchronous page fault and inode needs fsync,
1531caa51d26SJan Kara 		 * we can insert PMD into page tables only after that happens.
1532caa51d26SJan Kara 		 * Skip insertion for now and return the pfn so that caller can
1533caa51d26SJan Kara 		 * insert it after fsync is done.
1534caa51d26SJan Kara 		 */
1535caa51d26SJan Kara 		if (sync) {
1536caa51d26SJan Kara 			if (WARN_ON_ONCE(!pfnp))
1537caa51d26SJan Kara 				goto finish_iomap;
1538caa51d26SJan Kara 			*pfnp = pfn;
1539caa51d26SJan Kara 			result = VM_FAULT_NEEDDSYNC;
1540caa51d26SJan Kara 			goto finish_iomap;
1541caa51d26SJan Kara 		}
1542caa51d26SJan Kara 
1543302a5e31SJan Kara 		trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1544302a5e31SJan Kara 		result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1545302a5e31SJan Kara 					    write);
1546642261acSRoss Zwisler 		break;
1547642261acSRoss Zwisler 	case IOMAP_UNWRITTEN:
1548642261acSRoss Zwisler 	case IOMAP_HOLE:
1549642261acSRoss Zwisler 		if (WARN_ON_ONCE(write))
1550876f2946SRoss Zwisler 			break;
1551b15cd800SMatthew Wilcox 		result = dax_pmd_load_hole(&xas, vmf, &iomap, &entry);
1552642261acSRoss Zwisler 		break;
1553642261acSRoss Zwisler 	default:
1554642261acSRoss Zwisler 		WARN_ON_ONCE(1);
1555642261acSRoss Zwisler 		break;
1556642261acSRoss Zwisler 	}
1557642261acSRoss Zwisler 
15589f141d6eSJan Kara  finish_iomap:
15599f141d6eSJan Kara 	if (ops->iomap_end) {
15609f141d6eSJan Kara 		int copied = PMD_SIZE;
15619f141d6eSJan Kara 
15629f141d6eSJan Kara 		if (result == VM_FAULT_FALLBACK)
15639f141d6eSJan Kara 			copied = 0;
15649f141d6eSJan Kara 		/*
15659f141d6eSJan Kara 		 * The fault is done by now and there's no way back (other
15669f141d6eSJan Kara 		 * thread may be already happily using PMD we have installed).
15679f141d6eSJan Kara 		 * Just ignore error from ->iomap_end since we cannot do much
15689f141d6eSJan Kara 		 * with it.
15699f141d6eSJan Kara 		 */
15709f141d6eSJan Kara 		ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
15719f141d6eSJan Kara 				&iomap);
15729f141d6eSJan Kara 	}
1573876f2946SRoss Zwisler  unlock_entry:
1574b15cd800SMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1575642261acSRoss Zwisler  fallback:
1576642261acSRoss Zwisler 	if (result == VM_FAULT_FALLBACK) {
1577d8a849e1SDave Jiang 		split_huge_pmd(vma, vmf->pmd, vmf->address);
1578642261acSRoss Zwisler 		count_vm_event(THP_FAULT_FALLBACK);
1579642261acSRoss Zwisler 	}
1580282a8e03SRoss Zwisler out:
1581f4200391SDave Jiang 	trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
1582642261acSRoss Zwisler 	return result;
1583642261acSRoss Zwisler }
1584a2d58167SDave Jiang #else
1585ab77dab4SSouptick Joarder static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
158601cddfe9SArnd Bergmann 			       const struct iomap_ops *ops)
1587a2d58167SDave Jiang {
1588a2d58167SDave Jiang 	return VM_FAULT_FALLBACK;
1589a2d58167SDave Jiang }
1590642261acSRoss Zwisler #endif /* CONFIG_FS_DAX_PMD */
1591a2d58167SDave Jiang 
1592a2d58167SDave Jiang /**
1593a2d58167SDave Jiang  * dax_iomap_fault - handle a page fault on a DAX file
1594a2d58167SDave Jiang  * @vmf: The description of the fault
1595cec04e8cSJan Kara  * @pe_size: Size of the page to fault in
15969a0dd422SJan Kara  * @pfnp: PFN to insert for synchronous faults if fsync is required
1597c0b24625SJan Kara  * @iomap_errp: Storage for detailed error code in case of error
1598cec04e8cSJan Kara  * @ops: Iomap ops passed from the file system
1599a2d58167SDave Jiang  *
1600a2d58167SDave Jiang  * When a page fault occurs, filesystems may call this helper in
1601a2d58167SDave Jiang  * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1602a2d58167SDave Jiang  * has done all the necessary locking for page fault to proceed
1603a2d58167SDave Jiang  * successfully.
1604a2d58167SDave Jiang  */
1605ab77dab4SSouptick Joarder vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
1606c0b24625SJan Kara 		    pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
1607a2d58167SDave Jiang {
1608c791ace1SDave Jiang 	switch (pe_size) {
1609c791ace1SDave Jiang 	case PE_SIZE_PTE:
1610c0b24625SJan Kara 		return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
1611c791ace1SDave Jiang 	case PE_SIZE_PMD:
16129a0dd422SJan Kara 		return dax_iomap_pmd_fault(vmf, pfnp, ops);
1613a2d58167SDave Jiang 	default:
1614a2d58167SDave Jiang 		return VM_FAULT_FALLBACK;
1615a2d58167SDave Jiang 	}
1616a2d58167SDave Jiang }
1617a2d58167SDave Jiang EXPORT_SYMBOL_GPL(dax_iomap_fault);
161871eab6dfSJan Kara 
1619a77d19f4SMatthew Wilcox /*
162071eab6dfSJan Kara  * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
162171eab6dfSJan Kara  * @vmf: The description of the fault
162271eab6dfSJan Kara  * @pfn: PFN to insert
1623cfc93c6cSMatthew Wilcox  * @order: Order of entry to insert.
162471eab6dfSJan Kara  *
1625a77d19f4SMatthew Wilcox  * This function inserts a writeable PTE or PMD entry into the page tables
1626a77d19f4SMatthew Wilcox  * for an mmaped DAX file.  It also marks the page cache entry as dirty.
162771eab6dfSJan Kara  */
1628cfc93c6cSMatthew Wilcox static vm_fault_t
1629cfc93c6cSMatthew Wilcox dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
163071eab6dfSJan Kara {
163171eab6dfSJan Kara 	struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1632cfc93c6cSMatthew Wilcox 	XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1633cfc93c6cSMatthew Wilcox 	void *entry;
1634ab77dab4SSouptick Joarder 	vm_fault_t ret;
163571eab6dfSJan Kara 
1636cfc93c6cSMatthew Wilcox 	xas_lock_irq(&xas);
1637cfc93c6cSMatthew Wilcox 	entry = get_unlocked_entry(&xas);
163871eab6dfSJan Kara 	/* Did we race with someone splitting entry or so? */
163971eab6dfSJan Kara 	if (!entry ||
1640cfc93c6cSMatthew Wilcox 	    (order == 0 && !dax_is_pte_entry(entry)) ||
16410e40de03SMatthew Wilcox 	    (order == PMD_ORDER && !dax_is_pmd_entry(entry))) {
1642cfc93c6cSMatthew Wilcox 		put_unlocked_entry(&xas, entry);
1643cfc93c6cSMatthew Wilcox 		xas_unlock_irq(&xas);
164471eab6dfSJan Kara 		trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
164571eab6dfSJan Kara 						      VM_FAULT_NOPAGE);
164671eab6dfSJan Kara 		return VM_FAULT_NOPAGE;
164771eab6dfSJan Kara 	}
1648cfc93c6cSMatthew Wilcox 	xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1649cfc93c6cSMatthew Wilcox 	dax_lock_entry(&xas, entry);
1650cfc93c6cSMatthew Wilcox 	xas_unlock_irq(&xas);
1651cfc93c6cSMatthew Wilcox 	if (order == 0)
1652ab77dab4SSouptick Joarder 		ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
165371eab6dfSJan Kara #ifdef CONFIG_FS_DAX_PMD
1654cfc93c6cSMatthew Wilcox 	else if (order == PMD_ORDER)
1655ab77dab4SSouptick Joarder 		ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
165671eab6dfSJan Kara 			pfn, true);
165771eab6dfSJan Kara #endif
1658cfc93c6cSMatthew Wilcox 	else
1659ab77dab4SSouptick Joarder 		ret = VM_FAULT_FALLBACK;
1660cfc93c6cSMatthew Wilcox 	dax_unlock_entry(&xas, entry);
1661ab77dab4SSouptick Joarder 	trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1662ab77dab4SSouptick Joarder 	return ret;
166371eab6dfSJan Kara }
166471eab6dfSJan Kara 
166571eab6dfSJan Kara /**
166671eab6dfSJan Kara  * dax_finish_sync_fault - finish synchronous page fault
166771eab6dfSJan Kara  * @vmf: The description of the fault
166871eab6dfSJan Kara  * @pe_size: Size of entry to be inserted
166971eab6dfSJan Kara  * @pfn: PFN to insert
167071eab6dfSJan Kara  *
167171eab6dfSJan Kara  * This function ensures that the file range touched by the page fault is
167271eab6dfSJan Kara  * stored persistently on the media and handles inserting of appropriate page
167371eab6dfSJan Kara  * table entry.
167471eab6dfSJan Kara  */
1675ab77dab4SSouptick Joarder vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1676ab77dab4SSouptick Joarder 		enum page_entry_size pe_size, pfn_t pfn)
167771eab6dfSJan Kara {
167871eab6dfSJan Kara 	int err;
167971eab6dfSJan Kara 	loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1680cfc93c6cSMatthew Wilcox 	unsigned int order = pe_order(pe_size);
1681cfc93c6cSMatthew Wilcox 	size_t len = PAGE_SIZE << order;
168271eab6dfSJan Kara 
168371eab6dfSJan Kara 	err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
168471eab6dfSJan Kara 	if (err)
168571eab6dfSJan Kara 		return VM_FAULT_SIGBUS;
1686cfc93c6cSMatthew Wilcox 	return dax_insert_pfn_mkwrite(vmf, pfn, order);
168771eab6dfSJan Kara }
168871eab6dfSJan Kara EXPORT_SYMBOL_GPL(dax_finish_sync_fault);
1689