blob: 8873e4b0bc988107d8450eeb0c7a9aa8f33490fb [file] [log] [blame]
Matthew Wilcoxd475c632015-02-16 15:58:56 -08001/*
2 * fs/dax.c - Direct Access filesystem code
3 * Copyright (c) 2013-2014 Intel Corporation
4 * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
5 * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 */
16
17#include <linux/atomic.h>
18#include <linux/blkdev.h>
19#include <linux/buffer_head.h>
Ross Zwislerd77e92e2015-09-09 10:29:40 -060020#include <linux/dax.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080021#include <linux/fs.h>
22#include <linux/genhd.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080023#include <linux/highmem.h>
24#include <linux/memcontrol.h>
25#include <linux/mm.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080026#include <linux/mutex.h>
Ross Zwisler9973c982016-01-22 15:10:47 -080027#include <linux/pagevec.h>
Matthew Wilcox289c6ae2015-02-16 15:58:59 -080028#include <linux/sched.h>
Ingo Molnarf361bf42017-02-03 23:47:37 +010029#include <linux/sched/signal.h>
Matthew Wilcoxd475c632015-02-16 15:58:56 -080030#include <linux/uio.h>
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -080031#include <linux/vmstat.h>
Dan Williams34c0fd52016-01-15 16:56:14 -080032#include <linux/pfn_t.h>
Dan Williams0e749e52016-01-15 16:55:53 -080033#include <linux/sizes.h>
Jan Kara4b4bb462016-12-14 15:07:53 -080034#include <linux/mmu_notifier.h>
Christoph Hellwiga254e562016-09-19 11:24:49 +100035#include <linux/iomap.h>
36#include "internal.h"
Matthew Wilcoxd475c632015-02-16 15:58:56 -080037
Ross Zwisler282a8e02017-02-22 15:39:50 -080038#define CREATE_TRACE_POINTS
39#include <trace/events/fs_dax.h>
40
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -040041static inline unsigned int pe_order(enum page_entry_size pe_size)
42{
43 if (pe_size == PE_SIZE_PTE)
44 return PAGE_SHIFT - PAGE_SHIFT;
45 if (pe_size == PE_SIZE_PMD)
46 return PMD_SHIFT - PAGE_SHIFT;
47 if (pe_size == PE_SIZE_PUD)
48 return PUD_SHIFT - PAGE_SHIFT;
49 return ~0;
50}
51
Jan Karaac401cc2016-05-12 18:29:18 +020052/* We choose 4096 entries - same as per-zone page wait tables */
53#define DAX_WAIT_TABLE_BITS 12
54#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
55
Ross Zwisler917f3452017-09-06 16:18:58 -070056/* The 'colour' (ie low bits) within a PMD of a page offset. */
57#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
Matthew Wilcox977fbdc2018-01-31 16:17:36 -080058#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
Ross Zwisler917f3452017-09-06 16:18:58 -070059
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -040060/* The order of a PMD entry */
61#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT)
62
Ross Zwislerce95ab0f2016-11-08 11:31:44 +110063static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020064
65static int __init init_dax_wait_table(void)
66{
67 int i;
68
69 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
70 init_waitqueue_head(wait_table + i);
71 return 0;
72}
73fs_initcall(init_dax_wait_table);
74
Ross Zwisler527b19d2017-09-06 16:18:51 -070075/*
Matthew Wilcox3159f942017-11-03 13:30:42 -040076 * DAX pagecache entries use XArray value entries so they can't be mistaken
77 * for pages. We use one bit for locking, one bit for the entry size (PMD)
78 * and two more to tell us if the entry is a zero page or an empty entry that
79 * is just used for locking. In total four special bits.
Ross Zwisler527b19d2017-09-06 16:18:51 -070080 *
81 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
82 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
83 * block allocation.
84 */
Matthew Wilcox3159f942017-11-03 13:30:42 -040085#define DAX_SHIFT (4)
86#define DAX_LOCKED (1UL << 0)
87#define DAX_PMD (1UL << 1)
88#define DAX_ZERO_PAGE (1UL << 2)
89#define DAX_EMPTY (1UL << 3)
Ross Zwisler527b19d2017-09-06 16:18:51 -070090
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040091static unsigned long dax_to_pfn(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -070092{
Matthew Wilcox3159f942017-11-03 13:30:42 -040093 return xa_to_value(entry) >> DAX_SHIFT;
Ross Zwisler527b19d2017-09-06 16:18:51 -070094}
95
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040096static void *dax_make_locked(unsigned long pfn, unsigned long flags)
Ross Zwisler527b19d2017-09-06 16:18:51 -070097{
Matthew Wilcox3159f942017-11-03 13:30:42 -040098 return xa_mk_value(flags | ((unsigned long)pfn << DAX_SHIFT) |
99 DAX_LOCKED);
Ross Zwisler527b19d2017-09-06 16:18:51 -0700100}
101
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400102static bool dax_is_locked(void *entry)
103{
104 return xa_to_value(entry) & DAX_LOCKED;
105}
106
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400107static unsigned int dax_entry_order(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -0700108{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400109 if (xa_to_value(entry) & DAX_PMD)
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400110 return PMD_ORDER;
Ross Zwisler527b19d2017-09-06 16:18:51 -0700111 return 0;
112}
113
Ross Zwisler642261a2016-11-08 11:34:45 +1100114static int dax_is_pmd_entry(void *entry)
115{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400116 return xa_to_value(entry) & DAX_PMD;
Ross Zwisler642261a2016-11-08 11:34:45 +1100117}
118
119static int dax_is_pte_entry(void *entry)
120{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400121 return !(xa_to_value(entry) & DAX_PMD);
Ross Zwisler642261a2016-11-08 11:34:45 +1100122}
123
124static int dax_is_zero_entry(void *entry)
125{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400126 return xa_to_value(entry) & DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100127}
128
129static int dax_is_empty_entry(void *entry)
130{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400131 return xa_to_value(entry) & DAX_EMPTY;
Ross Zwisler642261a2016-11-08 11:34:45 +1100132}
133
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800134/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400135 * DAX page cache entry locking
Jan Karaac401cc2016-05-12 18:29:18 +0200136 */
137struct exceptional_entry_key {
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400138 struct xarray *xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100139 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200140};
141
142struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200143 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200144 struct exceptional_entry_key key;
145};
146
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400147static wait_queue_head_t *dax_entry_waitqueue(struct xarray *xa,
Ross Zwisler63e95b52016-11-08 11:32:20 +1100148 pgoff_t index, void *entry, struct exceptional_entry_key *key)
149{
150 unsigned long hash;
151
152 /*
153 * If 'entry' is a PMD, align the 'index' that we use for the wait
154 * queue to the start of that PMD. This ensures that all offsets in
155 * the range covered by the PMD map to the same bit lock.
156 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100157 if (dax_is_pmd_entry(entry))
Ross Zwisler917f3452017-09-06 16:18:58 -0700158 index &= ~PG_PMD_COLOUR;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100159
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400160 key->xa = xa;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100161 key->entry_start = index;
162
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400163 hash = hash_long((unsigned long)xa ^ index, DAX_WAIT_TABLE_BITS);
Ross Zwisler63e95b52016-11-08 11:32:20 +1100164 return wait_table + hash;
165}
166
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400167static int wake_exceptional_entry_func(wait_queue_entry_t *wait,
168 unsigned int mode, int sync, void *keyp)
Jan Karaac401cc2016-05-12 18:29:18 +0200169{
170 struct exceptional_entry_key *key = keyp;
171 struct wait_exceptional_entry_queue *ewait =
172 container_of(wait, struct wait_exceptional_entry_queue, wait);
173
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400174 if (key->xa != ewait->key.xa ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100175 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200176 return 0;
177 return autoremove_wake_function(wait, mode, sync, NULL);
178}
179
180/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700181 * @entry may no longer be the entry at the index in the mapping.
182 * The important information it's conveying is whether the entry at
183 * this index used to be a PMD entry.
Ross Zwislere30331f2017-09-06 16:18:39 -0700184 */
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400185static void dax_wake_mapping_entry_waiter(struct xarray *xa,
Ross Zwislere30331f2017-09-06 16:18:39 -0700186 pgoff_t index, void *entry, bool wake_all)
187{
188 struct exceptional_entry_key key;
189 wait_queue_head_t *wq;
190
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400191 wq = dax_entry_waitqueue(xa, index, entry, &key);
Ross Zwislere30331f2017-09-06 16:18:39 -0700192
193 /*
194 * Checking for locked entry and prepare_to_wait_exclusive() happens
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700195 * under the i_pages lock, ditto for entry handling in our callers.
Ross Zwislere30331f2017-09-06 16:18:39 -0700196 * So at this point all tasks that could have seen our entry locked
197 * must be in the waitqueue and the following check will see them.
198 */
199 if (waitqueue_active(wq))
200 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
201}
202
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -0400203static void dax_wake_entry(struct xa_state *xas, void *entry, bool wake_all)
204{
205 return dax_wake_mapping_entry_waiter(xas->xa, xas->xa_index, entry,
206 wake_all);
207}
208
209/*
210 * Look up entry in page cache, wait for it to become unlocked if it
211 * is a DAX entry and return it. The caller must subsequently call
212 * put_unlocked_entry() if it did not lock the entry or dax_unlock_entry()
213 * if it did.
214 *
215 * Must be called with the i_pages lock held.
216 */
217static void *get_unlocked_entry(struct xa_state *xas)
218{
219 void *entry;
220 struct wait_exceptional_entry_queue ewait;
221 wait_queue_head_t *wq;
222
223 init_wait(&ewait.wait);
224 ewait.wait.func = wake_exceptional_entry_func;
225
226 for (;;) {
227 entry = xas_load(xas);
228 if (!entry || xa_is_internal(entry) ||
229 WARN_ON_ONCE(!xa_is_value(entry)) ||
230 !dax_is_locked(entry))
231 return entry;
232
233 wq = dax_entry_waitqueue(xas->xa, xas->xa_index, entry,
234 &ewait.key);
235 prepare_to_wait_exclusive(wq, &ewait.wait,
236 TASK_UNINTERRUPTIBLE);
237 xas_unlock_irq(xas);
238 xas_reset(xas);
239 schedule();
240 finish_wait(wq, &ewait.wait);
241 xas_lock_irq(xas);
242 }
243}
244
245static void put_unlocked_entry(struct xa_state *xas, void *entry)
246{
247 /* If we were the only waiter woken, wake the next one */
248 if (entry)
249 dax_wake_entry(xas, entry, false);
250}
251
252/*
253 * We used the xa_state to get the entry, but then we locked the entry and
254 * dropped the xa_lock, so we know the xa_state is stale and must be reset
255 * before use.
256 */
257static void dax_unlock_entry(struct xa_state *xas, void *entry)
258{
259 void *old;
260
261 xas_reset(xas);
262 xas_lock_irq(xas);
263 old = xas_store(xas, entry);
264 xas_unlock_irq(xas);
265 BUG_ON(!dax_is_locked(old));
266 dax_wake_entry(xas, entry, false);
267}
268
269/*
270 * Return: The entry stored at this location before it was locked.
271 */
272static void *dax_lock_entry(struct xa_state *xas, void *entry)
273{
274 unsigned long v = xa_to_value(entry);
275 return xas_store(xas, xa_mk_value(v | DAX_LOCKED));
276}
277
Ross Zwislere30331f2017-09-06 16:18:39 -0700278/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700279 * Check whether the given slot is locked. Must be called with the i_pages
280 * lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200281 */
282static inline int slot_locked(struct address_space *mapping, void **slot)
283{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400284 unsigned long entry = xa_to_value(
285 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
286 return entry & DAX_LOCKED;
Jan Karaac401cc2016-05-12 18:29:18 +0200287}
288
289/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700290 * Mark the given slot as locked. Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200291 */
292static inline void *lock_slot(struct address_space *mapping, void **slot)
293{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400294 unsigned long v = xa_to_value(
295 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
296 void *entry = xa_mk_value(v | DAX_LOCKED);
297 radix_tree_replace_slot(&mapping->i_pages, slot, entry);
298 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200299}
300
301/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700302 * Mark the given slot as unlocked. Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200303 */
304static inline void *unlock_slot(struct address_space *mapping, void **slot)
305{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400306 unsigned long v = xa_to_value(
307 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
308 void *entry = xa_mk_value(v & ~DAX_LOCKED);
309 radix_tree_replace_slot(&mapping->i_pages, slot, entry);
310 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200311}
312
313/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400314 * Lookup entry in page cache, wait for it to become unlocked if it is
Matthew Wilcox3159f942017-11-03 13:30:42 -0400315 * a DAX entry and return it. The caller must call
Jan Karaac401cc2016-05-12 18:29:18 +0200316 * put_unlocked_mapping_entry() when he decided not to lock the entry or
317 * put_locked_mapping_entry() when he locked the entry and now wants to
318 * unlock it.
319 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700320 * Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200321 */
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700322static void *__get_unlocked_mapping_entry(struct address_space *mapping,
323 pgoff_t index, void ***slotp, bool (*wait_fn)(void))
Jan Karaac401cc2016-05-12 18:29:18 +0200324{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100325 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200326 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100327 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200328
329 init_wait(&ewait.wait);
330 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200331
332 for (;;) {
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700333 bool revalidate;
334
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700335 entry = __radix_tree_lookup(&mapping->i_pages, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200336 &slot);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700337 if (!entry ||
Matthew Wilcox3159f942017-11-03 13:30:42 -0400338 WARN_ON_ONCE(!xa_is_value(entry)) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200339 !slot_locked(mapping, slot)) {
340 if (slotp)
341 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100342 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200343 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100344
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400345 wq = dax_entry_waitqueue(&mapping->i_pages, index, entry,
346 &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200347 prepare_to_wait_exclusive(wq, &ewait.wait,
348 TASK_UNINTERRUPTIBLE);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700349 xa_unlock_irq(&mapping->i_pages);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700350 revalidate = wait_fn();
Jan Karaac401cc2016-05-12 18:29:18 +0200351 finish_wait(wq, &ewait.wait);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700352 xa_lock_irq(&mapping->i_pages);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700353 if (revalidate)
354 return ERR_PTR(-EAGAIN);
Jan Karaac401cc2016-05-12 18:29:18 +0200355 }
356}
357
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700358static bool entry_wait(void)
359{
360 schedule();
361 /*
362 * Never return an ERR_PTR() from
363 * __get_unlocked_mapping_entry(), just keep looping.
364 */
365 return false;
366}
367
368static void *get_unlocked_mapping_entry(struct address_space *mapping,
369 pgoff_t index, void ***slotp)
370{
371 return __get_unlocked_mapping_entry(mapping, index, slotp, entry_wait);
372}
373
374static void unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
Jan Karab1aa8122016-12-14 15:07:24 -0800375{
376 void *entry, **slot;
377
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700378 xa_lock_irq(&mapping->i_pages);
379 entry = __radix_tree_lookup(&mapping->i_pages, index, NULL, &slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400380 if (WARN_ON_ONCE(!entry || !xa_is_value(entry) ||
Jan Karab1aa8122016-12-14 15:07:24 -0800381 !slot_locked(mapping, slot))) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700382 xa_unlock_irq(&mapping->i_pages);
Jan Karab1aa8122016-12-14 15:07:24 -0800383 return;
384 }
385 unlock_slot(mapping, slot);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700386 xa_unlock_irq(&mapping->i_pages);
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400387 dax_wake_mapping_entry_waiter(&mapping->i_pages, index, entry, false);
Jan Karab1aa8122016-12-14 15:07:24 -0800388}
389
Jan Karaac401cc2016-05-12 18:29:18 +0200390static void put_locked_mapping_entry(struct address_space *mapping,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700391 pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200392{
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700393 unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200394}
395
396/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400397 * Called when we are done with page cache entry we looked up via
Jan Karaac401cc2016-05-12 18:29:18 +0200398 * get_unlocked_mapping_entry() and which we didn't lock in the end.
399 */
400static void put_unlocked_mapping_entry(struct address_space *mapping,
401 pgoff_t index, void *entry)
402{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700403 if (!entry)
Jan Karaac401cc2016-05-12 18:29:18 +0200404 return;
405
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400406 /* We have to wake up next waiter for the page cache entry lock */
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400407 dax_wake_mapping_entry_waiter(&mapping->i_pages, index, entry, false);
Ross Zwisler422476c2016-11-08 11:33:44 +1100408}
409
Dan Williamsd2c997c2017-12-22 22:02:48 -0800410static unsigned long dax_entry_size(void *entry)
411{
412 if (dax_is_zero_entry(entry))
413 return 0;
414 else if (dax_is_empty_entry(entry))
415 return 0;
416 else if (dax_is_pmd_entry(entry))
417 return PMD_SIZE;
418 else
419 return PAGE_SIZE;
420}
421
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400422static unsigned long dax_end_pfn(void *entry)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800423{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400424 return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800425}
426
427/*
428 * Iterate through all mapped pfns represented by an entry, i.e. skip
429 * 'empty' and 'zero' entries.
430 */
431#define for_each_mapped_pfn(entry, pfn) \
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400432 for (pfn = dax_to_pfn(entry); \
433 pfn < dax_end_pfn(entry); pfn++)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800434
Dan Williams73449da2018-07-13 21:49:50 -0700435/*
436 * TODO: for reflink+dax we need a way to associate a single page with
437 * multiple address_space instances at different linear_page_index()
438 * offsets.
439 */
440static void dax_associate_entry(void *entry, struct address_space *mapping,
441 struct vm_area_struct *vma, unsigned long address)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800442{
Dan Williams73449da2018-07-13 21:49:50 -0700443 unsigned long size = dax_entry_size(entry), pfn, index;
444 int i = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800445
446 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
447 return;
448
Dan Williams73449da2018-07-13 21:49:50 -0700449 index = linear_page_index(vma, address & ~(size - 1));
Dan Williamsd2c997c2017-12-22 22:02:48 -0800450 for_each_mapped_pfn(entry, pfn) {
451 struct page *page = pfn_to_page(pfn);
452
453 WARN_ON_ONCE(page->mapping);
454 page->mapping = mapping;
Dan Williams73449da2018-07-13 21:49:50 -0700455 page->index = index + i++;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800456 }
457}
458
459static void dax_disassociate_entry(void *entry, struct address_space *mapping,
460 bool trunc)
461{
462 unsigned long pfn;
463
464 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
465 return;
466
467 for_each_mapped_pfn(entry, pfn) {
468 struct page *page = pfn_to_page(pfn);
469
470 WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
471 WARN_ON_ONCE(page->mapping && page->mapping != mapping);
472 page->mapping = NULL;
Dan Williams73449da2018-07-13 21:49:50 -0700473 page->index = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800474 }
475}
476
Dan Williams5fac7402018-03-09 17:44:31 -0800477static struct page *dax_busy_page(void *entry)
478{
479 unsigned long pfn;
480
481 for_each_mapped_pfn(entry, pfn) {
482 struct page *page = pfn_to_page(pfn);
483
484 if (page_ref_count(page) > 1)
485 return page;
486 }
487 return NULL;
488}
489
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700490static bool entry_wait_revalidate(void)
491{
492 rcu_read_unlock();
493 schedule();
494 rcu_read_lock();
495
496 /*
497 * Tell __get_unlocked_mapping_entry() to take a break, we need
498 * to revalidate page->mapping after dropping locks
499 */
500 return true;
501}
502
503bool dax_lock_mapping_entry(struct page *page)
504{
505 pgoff_t index;
506 struct inode *inode;
507 bool did_lock = false;
508 void *entry = NULL, **slot;
509 struct address_space *mapping;
510
511 rcu_read_lock();
512 for (;;) {
513 mapping = READ_ONCE(page->mapping);
514
515 if (!dax_mapping(mapping))
516 break;
517
518 /*
519 * In the device-dax case there's no need to lock, a
520 * struct dev_pagemap pin is sufficient to keep the
521 * inode alive, and we assume we have dev_pagemap pin
522 * otherwise we would not have a valid pfn_to_page()
523 * translation.
524 */
525 inode = mapping->host;
526 if (S_ISCHR(inode->i_mode)) {
527 did_lock = true;
528 break;
529 }
530
531 xa_lock_irq(&mapping->i_pages);
532 if (mapping != page->mapping) {
533 xa_unlock_irq(&mapping->i_pages);
534 continue;
535 }
536 index = page->index;
537
538 entry = __get_unlocked_mapping_entry(mapping, index, &slot,
539 entry_wait_revalidate);
540 if (!entry) {
541 xa_unlock_irq(&mapping->i_pages);
542 break;
543 } else if (IS_ERR(entry)) {
544 WARN_ON_ONCE(PTR_ERR(entry) != -EAGAIN);
545 continue;
546 }
547 lock_slot(mapping, slot);
548 did_lock = true;
549 xa_unlock_irq(&mapping->i_pages);
550 break;
551 }
552 rcu_read_unlock();
553
554 return did_lock;
555}
556
557void dax_unlock_mapping_entry(struct page *page)
558{
559 struct address_space *mapping = page->mapping;
560 struct inode *inode = mapping->host;
561
562 if (S_ISCHR(inode->i_mode))
563 return;
564
565 unlock_mapping_entry(mapping, page->index);
566}
567
Jan Karaac401cc2016-05-12 18:29:18 +0200568/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400569 * Find page cache entry at given index. If it is a DAX entry, return it
570 * with the entry locked. If the page cache doesn't contain an entry at
571 * that index, add a locked empty entry.
Jan Karaac401cc2016-05-12 18:29:18 +0200572 *
Matthew Wilcox3159f942017-11-03 13:30:42 -0400573 * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
Ross Zwisler642261a2016-11-08 11:34:45 +1100574 * either return that locked entry or will return an error. This error will
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700575 * happen if there are any 4k entries within the 2MiB range that we are
576 * requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100577 *
578 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
579 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
580 * insertion will fail if it finds any 4k entries already in the tree, and a
581 * 4k insertion will cause an existing 2MiB entry to be unmapped and
582 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
583 * well as 2MiB empty entries.
584 *
585 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
586 * real storage backing them. We will leave these real 2MiB DAX entries in
587 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
588 *
Jan Karaac401cc2016-05-12 18:29:18 +0200589 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
590 * persistent memory the benefit is doubtful. We can add that later if we can
591 * show it helps.
592 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100593static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
594 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200595{
Ross Zwisler642261a2016-11-08 11:34:45 +1100596 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100597 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200598
599restart:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700600 xa_lock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100601 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100602
Matthew Wilcox3159f942017-11-03 13:30:42 -0400603 if (WARN_ON_ONCE(entry && !xa_is_value(entry))) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700604 entry = ERR_PTR(-EIO);
605 goto out_unlock;
606 }
607
Ross Zwisler642261a2016-11-08 11:34:45 +1100608 if (entry) {
Matthew Wilcox3159f942017-11-03 13:30:42 -0400609 if (size_flag & DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700610 if (dax_is_pte_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100611 put_unlocked_mapping_entry(mapping, index,
612 entry);
613 entry = ERR_PTR(-EEXIST);
614 goto out_unlock;
615 }
616 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700617 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100618 (dax_is_zero_entry(entry) ||
619 dax_is_empty_entry(entry))) {
620 pmd_downgrade = true;
621 }
622 }
623 }
624
Jan Karaac401cc2016-05-12 18:29:18 +0200625 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100626 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200627 int err;
628
Ross Zwisler642261a2016-11-08 11:34:45 +1100629 if (pmd_downgrade) {
630 /*
631 * Make sure 'entry' remains valid while we drop
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700632 * the i_pages lock.
Ross Zwisler642261a2016-11-08 11:34:45 +1100633 */
634 entry = lock_slot(mapping, slot);
635 }
636
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700637 xa_unlock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100638 /*
639 * Besides huge zero pages the only other thing that gets
640 * downgraded are empty entries which don't need to be
641 * unmapped.
642 */
643 if (pmd_downgrade && dax_is_zero_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800644 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
645 PG_PMD_NR, false);
Ross Zwisler642261a2016-11-08 11:34:45 +1100646
Jan Kara0cb80b42016-12-12 21:34:12 -0500647 err = radix_tree_preload(
648 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
649 if (err) {
650 if (pmd_downgrade)
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700651 put_locked_mapping_entry(mapping, index);
Jan Kara0cb80b42016-12-12 21:34:12 -0500652 return ERR_PTR(err);
653 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700654 xa_lock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100655
Ross Zwislere11f8b72017-04-07 16:04:57 -0700656 if (!entry) {
657 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700658 * We needed to drop the i_pages lock while calling
Ross Zwislere11f8b72017-04-07 16:04:57 -0700659 * radix_tree_preload() and we didn't have an entry to
660 * lock. See if another thread inserted an entry at
661 * our index during this time.
662 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700663 entry = __radix_tree_lookup(&mapping->i_pages, index,
Ross Zwislere11f8b72017-04-07 16:04:57 -0700664 NULL, &slot);
665 if (entry) {
666 radix_tree_preload_end();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700667 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere11f8b72017-04-07 16:04:57 -0700668 goto restart;
669 }
670 }
671
Ross Zwisler642261a2016-11-08 11:34:45 +1100672 if (pmd_downgrade) {
Dan Williamsd2c997c2017-12-22 22:02:48 -0800673 dax_disassociate_entry(entry, mapping, false);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700674 radix_tree_delete(&mapping->i_pages, index);
Ross Zwisler642261a2016-11-08 11:34:45 +1100675 mapping->nrexceptional--;
Matthew Wilcoxec4907f2018-03-28 11:01:43 -0400676 dax_wake_mapping_entry_waiter(&mapping->i_pages,
677 index, entry, true);
Ross Zwisler642261a2016-11-08 11:34:45 +1100678 }
679
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400680 entry = dax_make_locked(0, size_flag | DAX_EMPTY);
Ross Zwisler642261a2016-11-08 11:34:45 +1100681
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700682 err = __radix_tree_insert(&mapping->i_pages, index,
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400683 dax_entry_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200684 radix_tree_preload_end();
685 if (err) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700686 xa_unlock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100687 /*
Ross Zwislere11f8b72017-04-07 16:04:57 -0700688 * Our insertion of a DAX entry failed, most likely
689 * because we were inserting a PMD entry and it
690 * collided with a PTE sized entry at a different
691 * index in the PMD range. We haven't inserted
692 * anything into the radix tree and have no waiters to
693 * wake.
Ross Zwisler642261a2016-11-08 11:34:45 +1100694 */
Jan Karaac401cc2016-05-12 18:29:18 +0200695 return ERR_PTR(err);
696 }
697 /* Good, we have inserted empty locked entry into the tree. */
698 mapping->nrexceptional++;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700699 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100700 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200701 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100702 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100703 out_unlock:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700704 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100705 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200706}
707
Dan Williams5fac7402018-03-09 17:44:31 -0800708/**
709 * dax_layout_busy_page - find first pinned page in @mapping
710 * @mapping: address space to scan for a page with ref count > 1
711 *
712 * DAX requires ZONE_DEVICE mapped pages. These pages are never
713 * 'onlined' to the page allocator so they are considered idle when
714 * page->count == 1. A filesystem uses this interface to determine if
715 * any page in the mapping is busy, i.e. for DMA, or other
716 * get_user_pages() usages.
717 *
718 * It is expected that the filesystem is holding locks to block the
719 * establishment of new mappings in this address_space. I.e. it expects
720 * to be able to run unmap_mapping_range() and subsequently not race
721 * mapping_mapped() becoming true.
722 */
723struct page *dax_layout_busy_page(struct address_space *mapping)
724{
725 pgoff_t indices[PAGEVEC_SIZE];
726 struct page *page = NULL;
727 struct pagevec pvec;
728 pgoff_t index, end;
729 unsigned i;
730
731 /*
732 * In the 'limited' case get_user_pages() for dax is disabled.
733 */
734 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
735 return NULL;
736
737 if (!dax_mapping(mapping) || !mapping_mapped(mapping))
738 return NULL;
739
740 pagevec_init(&pvec);
741 index = 0;
742 end = -1;
743
744 /*
745 * If we race get_user_pages_fast() here either we'll see the
746 * elevated page count in the pagevec_lookup and wait, or
747 * get_user_pages_fast() will see that the page it took a reference
748 * against is no longer mapped in the page tables and bail to the
749 * get_user_pages() slow path. The slow path is protected by
750 * pte_lock() and pmd_lock(). New references are not taken without
751 * holding those locks, and unmap_mapping_range() will not zero the
752 * pte or pmd without holding the respective lock, so we are
753 * guaranteed to either see new references or prevent new
754 * references from being established.
755 */
756 unmap_mapping_range(mapping, 0, 0, 1);
757
758 while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
759 min(end - index, (pgoff_t)PAGEVEC_SIZE),
760 indices)) {
761 for (i = 0; i < pagevec_count(&pvec); i++) {
762 struct page *pvec_ent = pvec.pages[i];
763 void *entry;
764
765 index = indices[i];
766 if (index >= end)
767 break;
768
Matthew Wilcox3159f942017-11-03 13:30:42 -0400769 if (WARN_ON_ONCE(!xa_is_value(pvec_ent)))
Dan Williams5fac7402018-03-09 17:44:31 -0800770 continue;
771
772 xa_lock_irq(&mapping->i_pages);
773 entry = get_unlocked_mapping_entry(mapping, index, NULL);
774 if (entry)
775 page = dax_busy_page(entry);
776 put_unlocked_mapping_entry(mapping, index, entry);
777 xa_unlock_irq(&mapping->i_pages);
778 if (page)
779 break;
780 }
Ross Zwislercdbf8892018-07-29 16:59:16 -0400781
782 /*
783 * We don't expect normal struct page entries to exist in our
784 * tree, but we keep these pagevec calls so that this code is
785 * consistent with the common pattern for handling pagevecs
786 * throughout the kernel.
787 */
Dan Williams5fac7402018-03-09 17:44:31 -0800788 pagevec_remove_exceptionals(&pvec);
789 pagevec_release(&pvec);
790 index++;
791
792 if (page)
793 break;
794 }
795 return page;
796}
797EXPORT_SYMBOL_GPL(dax_layout_busy_page);
798
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400799static int __dax_invalidate_entry(struct address_space *mapping,
Jan Karac6dcf522016-08-10 17:22:44 +0200800 pgoff_t index, bool trunc)
801{
802 int ret = 0;
803 void *entry;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700804 struct radix_tree_root *pages = &mapping->i_pages;
Jan Karac6dcf522016-08-10 17:22:44 +0200805
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700806 xa_lock_irq(pages);
Jan Karac6dcf522016-08-10 17:22:44 +0200807 entry = get_unlocked_mapping_entry(mapping, index, NULL);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400808 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200809 goto out;
810 if (!trunc &&
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700811 (radix_tree_tag_get(pages, index, PAGECACHE_TAG_DIRTY) ||
812 radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE)))
Jan Karac6dcf522016-08-10 17:22:44 +0200813 goto out;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800814 dax_disassociate_entry(entry, mapping, trunc);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700815 radix_tree_delete(pages, index);
Jan Karac6dcf522016-08-10 17:22:44 +0200816 mapping->nrexceptional--;
817 ret = 1;
818out:
819 put_unlocked_mapping_entry(mapping, index, entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700820 xa_unlock_irq(pages);
Jan Karac6dcf522016-08-10 17:22:44 +0200821 return ret;
822}
Jan Karaac401cc2016-05-12 18:29:18 +0200823/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400824 * Delete DAX entry at @index from @mapping. Wait for it
825 * to be unlocked before deleting it.
Jan Karaac401cc2016-05-12 18:29:18 +0200826 */
827int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
828{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400829 int ret = __dax_invalidate_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200830
Jan Karaac401cc2016-05-12 18:29:18 +0200831 /*
832 * This gets called from truncate / punch_hole path. As such, the caller
833 * must hold locks protecting against concurrent modifications of the
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400834 * page cache (usually fs-private i_mmap_sem for writing). Since the
Matthew Wilcox3159f942017-11-03 13:30:42 -0400835 * caller has seen a DAX entry for this index, we better find it
Jan Karaac401cc2016-05-12 18:29:18 +0200836 * at that index as well...
837 */
Jan Karac6dcf522016-08-10 17:22:44 +0200838 WARN_ON_ONCE(!ret);
839 return ret;
840}
Jan Karaac401cc2016-05-12 18:29:18 +0200841
Jan Karac6dcf522016-08-10 17:22:44 +0200842/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400843 * Invalidate DAX entry if it is clean.
Jan Karac6dcf522016-08-10 17:22:44 +0200844 */
845int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
846 pgoff_t index)
847{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400848 return __dax_invalidate_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200849}
850
Dan Williamscccbce62017-01-27 13:31:42 -0800851static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
852 sector_t sector, size_t size, struct page *to,
853 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800854{
Dan Williamscccbce62017-01-27 13:31:42 -0800855 void *vto, *kaddr;
856 pgoff_t pgoff;
Dan Williamscccbce62017-01-27 13:31:42 -0800857 long rc;
858 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600859
Dan Williamscccbce62017-01-27 13:31:42 -0800860 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
861 if (rc)
862 return rc;
863
864 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800865 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -0800866 if (rc < 0) {
867 dax_read_unlock(id);
868 return rc;
869 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800870 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800871 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800872 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800873 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800874 return 0;
875}
876
Ross Zwisler642261a2016-11-08 11:34:45 +1100877/*
878 * By this point grab_mapping_entry() has ensured that we have a locked entry
879 * of the appropriate size so we don't have to worry about downgrading PMDs to
880 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
881 * already in the tree, we will skip the insertion and just dirty the PMD as
882 * appropriate.
883 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400884static void *dax_insert_entry(struct address_space *mapping,
885 struct vm_fault *vmf,
886 void *entry, pfn_t pfn_t, unsigned long flags, bool dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800887{
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700888 struct radix_tree_root *pages = &mapping->i_pages;
Dan Williams3fe07912017-10-14 17:13:45 -0700889 unsigned long pfn = pfn_t_to_pfn(pfn_t);
Jan Karaac401cc2016-05-12 18:29:18 +0200890 pgoff_t index = vmf->pgoff;
Dan Williams3fe07912017-10-14 17:13:45 -0700891 void *new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800892
Jan Karaf5b7b742017-11-01 16:36:40 +0100893 if (dirty)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800894 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800895
Matthew Wilcox3159f942017-11-03 13:30:42 -0400896 if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700897 /* we are replacing a zero page with block mapping */
898 if (dax_is_pmd_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800899 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
900 PG_PMD_NR, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700901 else /* pte entry */
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800902 unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
Ross Zwisler9973c982016-01-22 15:10:47 -0800903 }
904
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700905 xa_lock_irq(pages);
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400906 new_entry = dax_make_locked(pfn, flags);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800907 if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
908 dax_disassociate_entry(entry, mapping, false);
Dan Williams73449da2018-07-13 21:49:50 -0700909 dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800910 }
Ross Zwisler642261a2016-11-08 11:34:45 +1100911
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700912 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100913 /*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400914 * Only swap our new entry into the page cache if the current
Ross Zwisler642261a2016-11-08 11:34:45 +1100915 * entry is a zero page or an empty entry. If a normal PTE or
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400916 * PMD entry is already in the cache, we leave it alone. This
Ross Zwisler642261a2016-11-08 11:34:45 +1100917 * means that if we are trying to insert a PTE and the
918 * existing entry is a PMD, we will just leave the PMD in the
919 * tree and dirty it if necessary.
920 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800921 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200922 void **slot;
923 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800924
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700925 ret = __radix_tree_lookup(pages, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200926 WARN_ON_ONCE(ret != entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700927 __radix_tree_replace(pages, node, slot,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800928 new_entry, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700929 entry = new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800930 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700931
Jan Karaf5b7b742017-11-01 16:36:40 +0100932 if (dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700933 radix_tree_tag_set(pages, index, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700934
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700935 xa_unlock_irq(pages);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700936 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800937}
938
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400939static inline
940unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
Jan Kara4b4bb462016-12-14 15:07:53 -0800941{
942 unsigned long address;
943
944 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
945 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
946 return address;
947}
948
949/* Walk all mappings of a given index of a file and writeprotect them */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400950static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
951 unsigned long pfn)
Jan Kara4b4bb462016-12-14 15:07:53 -0800952{
953 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800954 pte_t pte, *ptep = NULL;
955 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800956 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800957
958 i_mmap_lock_read(mapping);
959 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400960 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800961
962 cond_resched();
963
964 if (!(vma->vm_flags & VM_SHARED))
965 continue;
966
967 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400968
969 /*
970 * Note because we provide start/end to follow_pte_pmd it will
971 * call mmu_notifier_invalidate_range_start() on our behalf
972 * before taking any lock.
973 */
974 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800975 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800976
Jérôme Glisse0f108512017-11-15 17:34:07 -0800977 /*
978 * No need to call mmu_notifier_invalidate_range() as we are
979 * downgrading page table protection not changing it to point
980 * to a new page.
981 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200982 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800983 */
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800984 if (pmdp) {
985#ifdef CONFIG_FS_DAX_PMD
986 pmd_t pmd;
987
988 if (pfn != pmd_pfn(*pmdp))
989 goto unlock_pmd;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800990 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800991 goto unlock_pmd;
992
993 flush_cache_page(vma, address, pfn);
994 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
995 pmd = pmd_wrprotect(pmd);
996 pmd = pmd_mkclean(pmd);
997 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800998unlock_pmd:
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800999#endif
Jan H. Schönherree190ca2018-01-31 16:14:04 -08001000 spin_unlock(ptl);
Ross Zwislerf729c8c2017-01-10 16:57:24 -08001001 } else {
1002 if (pfn != pte_pfn(*ptep))
1003 goto unlock_pte;
1004 if (!pte_dirty(*ptep) && !pte_write(*ptep))
1005 goto unlock_pte;
1006
1007 flush_cache_page(vma, address, pfn);
1008 pte = ptep_clear_flush(vma, address, ptep);
1009 pte = pte_wrprotect(pte);
1010 pte = pte_mkclean(pte);
1011 set_pte_at(vma->vm_mm, address, ptep, pte);
Ross Zwislerf729c8c2017-01-10 16:57:24 -08001012unlock_pte:
1013 pte_unmap_unlock(ptep, ptl);
1014 }
Jan Kara4b4bb462016-12-14 15:07:53 -08001015
Jérôme Glissea4d1a882017-08-31 17:17:26 -04001016 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -08001017 }
1018 i_mmap_unlock_read(mapping);
1019}
1020
Dan Williams3fe07912017-10-14 17:13:45 -07001021static int dax_writeback_one(struct dax_device *dax_dev,
1022 struct address_space *mapping, pgoff_t index, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -08001023{
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001024 struct radix_tree_root *pages = &mapping->i_pages;
Dan Williams3fe07912017-10-14 17:13:45 -07001025 void *entry2, **slot;
1026 unsigned long pfn;
1027 long ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -08001028 size_t size;
Ross Zwisler9973c982016-01-22 15:10:47 -08001029
Ross Zwisler9973c982016-01-22 15:10:47 -08001030 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -08001031 * A page got tagged dirty in DAX mapping? Something is seriously
1032 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -08001033 */
Matthew Wilcox3159f942017-11-03 13:30:42 -04001034 if (WARN_ON(!xa_is_value(entry)))
Jan Karaa6abc2c2016-12-14 15:07:47 -08001035 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -08001036
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001037 xa_lock_irq(pages);
Jan Karaa6abc2c2016-12-14 15:07:47 -08001038 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
1039 /* Entry got punched out / reallocated? */
Matthew Wilcox3159f942017-11-03 13:30:42 -04001040 if (!entry2 || WARN_ON_ONCE(!xa_is_value(entry2)))
Jan Karaa6abc2c2016-12-14 15:07:47 -08001041 goto put_unlocked;
1042 /*
1043 * Entry got reallocated elsewhere? No need to writeback. We have to
Dan Williams3fe07912017-10-14 17:13:45 -07001044 * compare pfns as we must not bail out due to difference in lockbit
Jan Karaa6abc2c2016-12-14 15:07:47 -08001045 * or entry type.
1046 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001047 if (dax_to_pfn(entry2) != dax_to_pfn(entry))
Jan Karaa6abc2c2016-12-14 15:07:47 -08001048 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +11001049 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
1050 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -08001051 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -08001052 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -08001053 }
1054
Jan Karaa6abc2c2016-12-14 15:07:47 -08001055 /* Another fsync thread may have already written back this entry */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001056 if (!radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE))
Jan Karaa6abc2c2016-12-14 15:07:47 -08001057 goto put_unlocked;
1058 /* Lock the entry to serialize with page faults */
1059 entry = lock_slot(mapping, slot);
1060 /*
1061 * We can clear the tag now but we have to be careful so that concurrent
1062 * dax_writeback_one() calls for the same index cannot finish before we
1063 * actually flush the caches. This is achieved as the calls will look
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001064 * at the entry only under the i_pages lock and once they do that
1065 * they will see the entry locked and wait for it to unlock.
Jan Karaa6abc2c2016-12-14 15:07:47 -08001066 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001067 radix_tree_tag_clear(pages, index, PAGECACHE_TAG_TOWRITE);
1068 xa_unlock_irq(pages);
Jan Karaa6abc2c2016-12-14 15:07:47 -08001069
Ross Zwisler642261a2016-11-08 11:34:45 +11001070 /*
1071 * Even if dax_writeback_mapping_range() was given a wbc->range_start
1072 * in the middle of a PMD, the 'index' we are given will be aligned to
Dan Williams3fe07912017-10-14 17:13:45 -07001073 * the start index of the PMD, as will the pfn we pull from 'entry'.
1074 * This allows us to flush for PMD_SIZE and not have to worry about
1075 * partial PMD writebacks.
Ross Zwisler642261a2016-11-08 11:34:45 +11001076 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001077 pfn = dax_to_pfn(entry);
1078 size = PAGE_SIZE << dax_entry_order(entry);
Dan Williamscccbce62017-01-27 13:31:42 -08001079
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001080 dax_entry_mkclean(mapping, index, pfn);
Dan Williams3fe07912017-10-14 17:13:45 -07001081 dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
Jan Kara4b4bb462016-12-14 15:07:53 -08001082 /*
1083 * After we have flushed the cache, we can clear the dirty tag. There
1084 * cannot be new dirty data in the pfn after the flush has completed as
1085 * the pfn mappings are writeprotected and fault waits for mapping
1086 * entry lock.
1087 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001088 xa_lock_irq(pages);
1089 radix_tree_tag_clear(pages, index, PAGECACHE_TAG_DIRTY);
1090 xa_unlock_irq(pages);
Ross Zwislerf9bc3a02017-05-08 16:00:13 -07001091 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001092 put_locked_mapping_entry(mapping, index);
Ross Zwisler9973c982016-01-22 15:10:47 -08001093 return ret;
1094
Jan Karaa6abc2c2016-12-14 15:07:47 -08001095 put_unlocked:
1096 put_unlocked_mapping_entry(mapping, index, entry2);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001097 xa_unlock_irq(pages);
Ross Zwisler9973c982016-01-22 15:10:47 -08001098 return ret;
1099}
1100
1101/*
1102 * Flush the mapping to the persistent domain within the byte range of [start,
1103 * end]. This is required by data integrity operations to ensure file data is
1104 * on persistent storage prior to completion of the operation.
1105 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001106int dax_writeback_mapping_range(struct address_space *mapping,
1107 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -08001108{
1109 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001110 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -08001111 pgoff_t indices[PAGEVEC_SIZE];
Dan Williamscccbce62017-01-27 13:31:42 -08001112 struct dax_device *dax_dev;
Ross Zwisler9973c982016-01-22 15:10:47 -08001113 struct pagevec pvec;
1114 bool done = false;
1115 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -08001116
1117 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
1118 return -EIO;
1119
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001120 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
1121 return 0;
1122
Dan Williamscccbce62017-01-27 13:31:42 -08001123 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
1124 if (!dax_dev)
1125 return -EIO;
1126
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001127 start_index = wbc->range_start >> PAGE_SHIFT;
1128 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -08001129
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001130 trace_dax_writeback_range(inode, start_index, end_index);
1131
Ross Zwisler9973c982016-01-22 15:10:47 -08001132 tag_pages_for_writeback(mapping, start_index, end_index);
1133
Mel Gorman86679822017-11-15 17:37:52 -08001134 pagevec_init(&pvec);
Ross Zwisler9973c982016-01-22 15:10:47 -08001135 while (!done) {
1136 pvec.nr = find_get_entries_tag(mapping, start_index,
1137 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
1138 pvec.pages, indices);
1139
1140 if (pvec.nr == 0)
1141 break;
1142
1143 for (i = 0; i < pvec.nr; i++) {
1144 if (indices[i] > end_index) {
1145 done = true;
1146 break;
1147 }
1148
Dan Williams3fe07912017-10-14 17:13:45 -07001149 ret = dax_writeback_one(dax_dev, mapping, indices[i],
1150 pvec.pages[i]);
Jeff Layton819ec6b2017-07-06 07:02:27 -04001151 if (ret < 0) {
1152 mapping_set_error(mapping, ret);
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001153 goto out;
Jeff Layton819ec6b2017-07-06 07:02:27 -04001154 }
Ross Zwisler9973c982016-01-22 15:10:47 -08001155 }
Jan Kara1eb643d2017-06-23 15:08:46 -07001156 start_index = indices[pvec.nr - 1] + 1;
Ross Zwisler9973c982016-01-22 15:10:47 -08001157 }
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001158out:
Dan Williamscccbce62017-01-27 13:31:42 -08001159 put_dax(dax_dev);
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001160 trace_dax_writeback_range_done(inode, start_index, end_index);
1161 return (ret < 0 ? ret : 0);
Ross Zwisler9973c982016-01-22 15:10:47 -08001162}
1163EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
1164
Jan Kara31a6f1a2017-11-01 16:36:32 +01001165static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001166{
Linus Torvaldsa3841f92017-11-17 09:51:57 -08001167 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
Jan Kara31a6f1a2017-11-01 16:36:32 +01001168}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001169
Jan Kara5e161e42017-11-01 16:36:33 +01001170static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
1171 pfn_t *pfnp)
1172{
1173 const sector_t sector = dax_iomap_sector(iomap, pos);
1174 pgoff_t pgoff;
Jan Kara5e161e42017-11-01 16:36:33 +01001175 int id, rc;
1176 long length;
1177
1178 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001179 if (rc)
1180 return rc;
Dan Williamscccbce62017-01-27 13:31:42 -08001181 id = dax_read_lock();
Jan Kara5e161e42017-11-01 16:36:33 +01001182 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001183 NULL, pfnp);
Jan Kara5e161e42017-11-01 16:36:33 +01001184 if (length < 0) {
1185 rc = length;
1186 goto out;
Dan Williamscccbce62017-01-27 13:31:42 -08001187 }
Jan Kara5e161e42017-11-01 16:36:33 +01001188 rc = -EINVAL;
1189 if (PFN_PHYS(length) < size)
1190 goto out;
1191 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
1192 goto out;
1193 /* For larger pages we need devmap */
1194 if (length > 1 && !pfn_t_devmap(*pfnp))
1195 goto out;
1196 rc = 0;
1197out:
Dan Williamscccbce62017-01-27 13:31:42 -08001198 dax_read_unlock(id);
Jan Kara5e161e42017-11-01 16:36:33 +01001199 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001200}
1201
Ross Zwislere30331f2017-09-06 16:18:39 -07001202/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001203 * The user has performed a load from a hole in the file. Allocating a new
1204 * page in the file would cause excessive storage usage for workloads with
1205 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
1206 * If this page is ever written to we will re-fault and change the mapping to
1207 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -07001208 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001209static vm_fault_t dax_load_hole(struct address_space *mapping, void *entry,
Ross Zwislere30331f2017-09-06 16:18:39 -07001210 struct vm_fault *vmf)
1211{
1212 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001213 unsigned long vaddr = vmf->address;
Matthew Wilcoxb90ca5c2018-09-11 21:27:44 -07001214 pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1215 vm_fault_t ret;
Ross Zwislere30331f2017-09-06 16:18:39 -07001216
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001217 dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001218 DAX_ZERO_PAGE, false);
1219
Souptick Joarderab77dab2018-06-07 17:04:29 -07001220 ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
Ross Zwislere30331f2017-09-06 16:18:39 -07001221 trace_dax_load_hole(inode, vmf, ret);
1222 return ret;
1223}
1224
Vishal Verma4b0228f2016-04-21 15:13:46 -04001225static bool dax_range_is_aligned(struct block_device *bdev,
1226 unsigned int offset, unsigned int length)
1227{
1228 unsigned short sector_size = bdev_logical_block_size(bdev);
1229
1230 if (!IS_ALIGNED(offset, sector_size))
1231 return false;
1232 if (!IS_ALIGNED(length, sector_size))
1233 return false;
1234
1235 return true;
1236}
1237
Dan Williamscccbce62017-01-27 13:31:42 -08001238int __dax_zero_page_range(struct block_device *bdev,
1239 struct dax_device *dax_dev, sector_t sector,
1240 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001241{
Dan Williamscccbce62017-01-27 13:31:42 -08001242 if (dax_range_is_aligned(bdev, offset, size)) {
1243 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001244
1245 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -07001246 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001247 } else {
Dan Williamscccbce62017-01-27 13:31:42 -08001248 pgoff_t pgoff;
1249 long rc, id;
1250 void *kaddr;
Dan Williamscccbce62017-01-27 13:31:42 -08001251
Dan Williamse84b83b2017-05-10 19:38:13 -07001252 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001253 if (rc)
1254 return rc;
1255
1256 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001257 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -08001258 if (rc < 0) {
1259 dax_read_unlock(id);
1260 return rc;
1261 }
Dan Williams81f55872017-05-29 13:12:20 -07001262 memset(kaddr + offset, 0, size);
Mikulas Patockac3ca0152017-08-31 21:47:43 -04001263 dax_flush(dax_dev, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -08001264 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001265 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001266 return 0;
1267}
1268EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1269
Christoph Hellwiga254e562016-09-19 11:24:49 +10001270static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001271dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001272 struct iomap *iomap)
1273{
Dan Williamscccbce62017-01-27 13:31:42 -08001274 struct block_device *bdev = iomap->bdev;
1275 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001276 struct iov_iter *iter = data;
1277 loff_t end = pos + length, done = 0;
1278 ssize_t ret = 0;
Dan Williamsa77d4782018-03-16 17:36:44 -07001279 size_t xfer;
Dan Williamscccbce62017-01-27 13:31:42 -08001280 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001281
1282 if (iov_iter_rw(iter) == READ) {
1283 end = min(end, i_size_read(inode));
1284 if (pos >= end)
1285 return 0;
1286
1287 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1288 return iov_iter_zero(min(length, end - pos), iter);
1289 }
1290
1291 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1292 return -EIO;
1293
Jan Karae3fce682016-08-10 17:10:28 +02001294 /*
1295 * Write can allocate block for an area which has a hole page mapped
1296 * into page tables. We have to tear down these mappings so that data
1297 * written by write(2) is visible in mmap.
1298 */
Jan Karacd656372017-05-12 15:46:50 -07001299 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +02001300 invalidate_inode_pages2_range(inode->i_mapping,
1301 pos >> PAGE_SHIFT,
1302 (end - 1) >> PAGE_SHIFT);
1303 }
1304
Dan Williamscccbce62017-01-27 13:31:42 -08001305 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +10001306 while (pos < end) {
1307 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -08001308 const size_t size = ALIGN(length + offset, PAGE_SIZE);
1309 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001310 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -08001311 pgoff_t pgoff;
1312 void *kaddr;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001313
Michal Hockod1908f52017-02-03 13:13:26 -08001314 if (fatal_signal_pending(current)) {
1315 ret = -EINTR;
1316 break;
1317 }
1318
Dan Williamscccbce62017-01-27 13:31:42 -08001319 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1320 if (ret)
1321 break;
1322
1323 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001324 &kaddr, NULL);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001325 if (map_len < 0) {
1326 ret = map_len;
1327 break;
1328 }
1329
Dan Williamscccbce62017-01-27 13:31:42 -08001330 map_len = PFN_PHYS(map_len);
1331 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001332 map_len -= offset;
1333 if (map_len > end - pos)
1334 map_len = end - pos;
1335
Ross Zwislera2e050f2017-09-06 16:18:54 -07001336 /*
1337 * The userspace address for the memory copy has already been
1338 * validated via access_ok() in either vfs_read() or
1339 * vfs_write(), depending on which operation we are doing.
1340 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001341 if (iov_iter_rw(iter) == WRITE)
Dan Williamsa77d4782018-03-16 17:36:44 -07001342 xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
Dan Williamsfec53772017-05-29 21:56:49 -07001343 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001344 else
Dan Williamsa77d4782018-03-16 17:36:44 -07001345 xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001346 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001347
Dan Williamsa77d4782018-03-16 17:36:44 -07001348 pos += xfer;
1349 length -= xfer;
1350 done += xfer;
1351
1352 if (xfer == 0)
1353 ret = -EFAULT;
1354 if (xfer < map_len)
1355 break;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001356 }
Dan Williamscccbce62017-01-27 13:31:42 -08001357 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001358
1359 return done ? done : ret;
1360}
1361
1362/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001363 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001364 * @iocb: The control block for this I/O
1365 * @iter: The addresses to do I/O from or to
1366 * @ops: iomap ops passed from the file system
1367 *
1368 * This function performs read and write operations to directly mapped
1369 * persistent memory. The callers needs to take care of read/write exclusion
1370 * and evicting any page cache pages in the region under I/O.
1371 */
1372ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001373dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001374 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001375{
1376 struct address_space *mapping = iocb->ki_filp->f_mapping;
1377 struct inode *inode = mapping->host;
1378 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1379 unsigned flags = 0;
1380
Christoph Hellwig168316d2017-02-08 14:43:13 -05001381 if (iov_iter_rw(iter) == WRITE) {
1382 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001383 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001384 } else {
1385 lockdep_assert_held(&inode->i_rwsem);
1386 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001387
Christoph Hellwiga254e562016-09-19 11:24:49 +10001388 while (iov_iter_count(iter)) {
1389 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001390 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001391 if (ret <= 0)
1392 break;
1393 pos += ret;
1394 done += ret;
1395 }
1396
1397 iocb->ki_pos += done;
1398 return done ? done : ret;
1399}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001400EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001401
Souptick Joarderab77dab2018-06-07 17:04:29 -07001402static vm_fault_t dax_fault_return(int error)
Jan Kara9f141d62016-10-19 14:34:31 +02001403{
1404 if (error == 0)
1405 return VM_FAULT_NOPAGE;
1406 if (error == -ENOMEM)
1407 return VM_FAULT_OOM;
1408 return VM_FAULT_SIGBUS;
1409}
1410
Dan Williamsaaa422c2017-11-13 16:38:44 -08001411/*
1412 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1413 * flushed on write-faults (non-cow), but not read-faults.
1414 */
1415static bool dax_fault_is_synchronous(unsigned long flags,
1416 struct vm_area_struct *vma, struct iomap *iomap)
1417{
1418 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1419 && (iomap->flags & IOMAP_F_DIRTY);
1420}
1421
Souptick Joarderab77dab2018-06-07 17:04:29 -07001422static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
Jan Karac0b24622018-01-07 16:38:43 -05001423 int *iomap_errp, const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001424{
Jan Karaa0987ad2017-11-01 16:36:34 +01001425 struct vm_area_struct *vma = vmf->vma;
1426 struct address_space *mapping = vma->vm_file->f_mapping;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001427 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001428 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001429 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001430 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001431 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001432 int error, major = 0;
Jan Karad2c43ef2017-11-01 16:36:35 +01001433 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001434 bool sync;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001435 vm_fault_t ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001436 void *entry;
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001437 pfn_t pfn;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001438
Souptick Joarderab77dab2018-06-07 17:04:29 -07001439 trace_dax_pte_fault(inode, vmf, ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001440 /*
1441 * Check whether offset isn't beyond end of file now. Caller is supposed
1442 * to hold locks serializing us with truncate / punch hole so this is
1443 * a reliable test.
1444 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001445 if (pos >= i_size_read(inode)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001446 ret = VM_FAULT_SIGBUS;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001447 goto out;
1448 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001449
Jan Karad2c43ef2017-11-01 16:36:35 +01001450 if (write && !vmf->cow_page)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001451 flags |= IOMAP_WRITE;
1452
Jan Kara13e451f2017-05-12 15:46:57 -07001453 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1454 if (IS_ERR(entry)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001455 ret = dax_fault_return(PTR_ERR(entry));
Jan Kara13e451f2017-05-12 15:46:57 -07001456 goto out;
1457 }
1458
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001459 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001460 * It is possible, particularly with mixed reads & writes to private
1461 * mappings, that we have raced with a PMD fault that overlaps with
1462 * the PTE we need to set up. If so just return and the fault will be
1463 * retried.
1464 */
1465 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001466 ret = VM_FAULT_NOPAGE;
Ross Zwislere2093922017-06-02 14:46:37 -07001467 goto unlock_entry;
1468 }
1469
1470 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001471 * Note that we don't bother to use iomap_apply here: DAX required
1472 * the file system block size to be equal the page size, which means
1473 * that we never have to deal with more than a single extent here.
1474 */
1475 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Jan Karac0b24622018-01-07 16:38:43 -05001476 if (iomap_errp)
1477 *iomap_errp = error;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001478 if (error) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001479 ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001480 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001481 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001482 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001483 error = -EIO; /* fs corruption? */
1484 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001485 }
1486
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001487 if (vmf->cow_page) {
Jan Kara31a6f1a2017-11-01 16:36:32 +01001488 sector_t sector = dax_iomap_sector(&iomap, pos);
1489
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001490 switch (iomap.type) {
1491 case IOMAP_HOLE:
1492 case IOMAP_UNWRITTEN:
1493 clear_user_highpage(vmf->cow_page, vaddr);
1494 break;
1495 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001496 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1497 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001498 break;
1499 default:
1500 WARN_ON_ONCE(1);
1501 error = -EIO;
1502 break;
1503 }
1504
1505 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001506 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001507
1508 __SetPageUptodate(vmf->cow_page);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001509 ret = finish_fault(vmf);
1510 if (!ret)
1511 ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001512 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001513 }
1514
Dan Williamsaaa422c2017-11-13 16:38:44 -08001515 sync = dax_fault_is_synchronous(flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001516
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001517 switch (iomap.type) {
1518 case IOMAP_MAPPED:
1519 if (iomap.flags & IOMAP_F_NEW) {
1520 count_vm_event(PGMAJFAULT);
Jan Karaa0987ad2017-11-01 16:36:34 +01001521 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001522 major = VM_FAULT_MAJOR;
1523 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001524 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1525 if (error < 0)
1526 goto error_finish_iomap;
1527
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001528 entry = dax_insert_entry(mapping, vmf, entry, pfn,
Jan Karacaa51d22017-11-01 16:36:42 +01001529 0, write && !sync);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001530
Jan Karacaa51d22017-11-01 16:36:42 +01001531 /*
1532 * If we are doing synchronous page fault and inode needs fsync,
1533 * we can insert PTE into page tables only after that happens.
1534 * Skip insertion for now and return the pfn so that caller can
1535 * insert it after fsync is done.
1536 */
1537 if (sync) {
1538 if (WARN_ON_ONCE(!pfnp)) {
1539 error = -EIO;
1540 goto error_finish_iomap;
1541 }
1542 *pfnp = pfn;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001543 ret = VM_FAULT_NEEDDSYNC | major;
Jan Karacaa51d22017-11-01 16:36:42 +01001544 goto finish_iomap;
1545 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001546 trace_dax_insert_mapping(inode, vmf, entry);
1547 if (write)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001548 ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001549 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001550 ret = vmf_insert_mixed(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001551
Souptick Joarderab77dab2018-06-07 17:04:29 -07001552 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001553 case IOMAP_UNWRITTEN:
1554 case IOMAP_HOLE:
Jan Karad2c43ef2017-11-01 16:36:35 +01001555 if (!write) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001556 ret = dax_load_hole(mapping, entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001557 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001558 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001559 /*FALLTHRU*/
1560 default:
1561 WARN_ON_ONCE(1);
1562 error = -EIO;
1563 break;
1564 }
1565
Jan Kara13e451f2017-05-12 15:46:57 -07001566 error_finish_iomap:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001567 ret = dax_fault_return(error);
Jan Kara9f141d62016-10-19 14:34:31 +02001568 finish_iomap:
1569 if (ops->iomap_end) {
1570 int copied = PAGE_SIZE;
1571
Souptick Joarderab77dab2018-06-07 17:04:29 -07001572 if (ret & VM_FAULT_ERROR)
Jan Kara9f141d62016-10-19 14:34:31 +02001573 copied = 0;
1574 /*
1575 * The fault is done by now and there's no way back (other
1576 * thread may be already happily using PTE we have installed).
1577 * Just ignore error from ->iomap_end since we cannot do much
1578 * with it.
1579 */
1580 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001581 }
Jan Kara13e451f2017-05-12 15:46:57 -07001582 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001583 put_locked_mapping_entry(mapping, vmf->pgoff);
Jan Kara13e451f2017-05-12 15:46:57 -07001584 out:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001585 trace_dax_pte_fault_done(inode, vmf, ret);
1586 return ret | major;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001587}
Ross Zwisler642261a2016-11-08 11:34:45 +11001588
1589#ifdef CONFIG_FS_DAX_PMD
Souptick Joarderab77dab2018-06-07 17:04:29 -07001590static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001591 void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001592{
Dave Jiangf4200392017-02-22 15:40:06 -08001593 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1594 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001595 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001596 struct page *zero_page;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001597 void *ret = NULL;
Ross Zwisler642261a2016-11-08 11:34:45 +11001598 spinlock_t *ptl;
1599 pmd_t pmd_entry;
Dan Williams3fe07912017-10-14 17:13:45 -07001600 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001601
Dave Jiangf4200392017-02-22 15:40:06 -08001602 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001603
1604 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001605 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001606
Dan Williams3fe07912017-10-14 17:13:45 -07001607 pfn = page_to_pfn_t(zero_page);
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001608 ret = dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001609 DAX_PMD | DAX_ZERO_PAGE, false);
Ross Zwisler642261a2016-11-08 11:34:45 +11001610
Dave Jiangf4200392017-02-22 15:40:06 -08001611 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1612 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001613 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001614 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001615 }
1616
Dave Jiangf4200392017-02-22 15:40:06 -08001617 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001618 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001619 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001620 spin_unlock(ptl);
Dave Jiangf4200392017-02-22 15:40:06 -08001621 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001622 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001623
1624fallback:
Dave Jiangf4200392017-02-22 15:40:06 -08001625 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001626 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001627}
1628
Souptick Joarderab77dab2018-06-07 17:04:29 -07001629static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Dave Jianga2d58162017-02-24 14:56:59 -08001630 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001631{
Dave Jiangf4200392017-02-22 15:40:06 -08001632 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001633 struct address_space *mapping = vma->vm_file->f_mapping;
Dave Jiangd8a849e2017-02-22 15:40:03 -08001634 unsigned long pmd_addr = vmf->address & PMD_MASK;
1635 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001636 bool sync;
Jan Kara9484ab12016-11-10 10:26:50 +11001637 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001638 struct inode *inode = mapping->host;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001639 vm_fault_t result = VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001640 struct iomap iomap = { 0 };
1641 pgoff_t max_pgoff, pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001642 void *entry;
1643 loff_t pos;
1644 int error;
Jan Kara302a5e32017-11-01 16:36:37 +01001645 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001646
Ross Zwisler282a8e02017-02-22 15:39:50 -08001647 /*
1648 * Check whether offset isn't beyond end of file now. Caller is
1649 * supposed to hold locks serializing us with truncate / punch hole so
1650 * this is a reliable test.
1651 */
1652 pgoff = linear_page_index(vma, pmd_addr);
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001653 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001654
Dave Jiangf4200392017-02-22 15:40:06 -08001655 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001656
Ross Zwislerfffa2812017-08-25 15:55:36 -07001657 /*
1658 * Make sure that the faulting address's PMD offset (color) matches
1659 * the PMD offset from the start of the file. This is necessary so
1660 * that a PMD range in the page table overlaps exactly with a PMD
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001661 * range in the page cache.
Ross Zwislerfffa2812017-08-25 15:55:36 -07001662 */
1663 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1664 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1665 goto fallback;
1666
Ross Zwisler642261a2016-11-08 11:34:45 +11001667 /* Fall back to PTEs if we're going to COW */
1668 if (write && !(vma->vm_flags & VM_SHARED))
1669 goto fallback;
1670
1671 /* If the PMD would extend outside the VMA */
1672 if (pmd_addr < vma->vm_start)
1673 goto fallback;
1674 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1675 goto fallback;
1676
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001677 if (pgoff >= max_pgoff) {
Ross Zwisler282a8e02017-02-22 15:39:50 -08001678 result = VM_FAULT_SIGBUS;
1679 goto out;
1680 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001681
1682 /* If the PMD would extend beyond the file size */
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001683 if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
Ross Zwisler642261a2016-11-08 11:34:45 +11001684 goto fallback;
1685
1686 /*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001687 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1688 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1689 * is already in the tree, for instance), it will return -EEXIST and
1690 * we just fall back to 4k entries.
Jan Kara9f141d62016-10-19 14:34:31 +02001691 */
Matthew Wilcox3159f942017-11-03 13:30:42 -04001692 entry = grab_mapping_entry(mapping, pgoff, DAX_PMD);
Jan Kara9f141d62016-10-19 14:34:31 +02001693 if (IS_ERR(entry))
Ross Zwisler876f2942017-05-12 15:47:00 -07001694 goto fallback;
1695
1696 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001697 * It is possible, particularly with mixed reads & writes to private
1698 * mappings, that we have raced with a PTE fault that overlaps with
1699 * the PMD we need to set up. If so just return and the fault will be
1700 * retried.
1701 */
1702 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1703 !pmd_devmap(*vmf->pmd)) {
1704 result = 0;
1705 goto unlock_entry;
1706 }
1707
1708 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001709 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1710 * setting up a mapping, so really we're using iomap_begin() as a way
1711 * to look up our filesystem block.
1712 */
1713 pos = (loff_t)pgoff << PAGE_SHIFT;
1714 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1715 if (error)
1716 goto unlock_entry;
1717
1718 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001719 goto finish_iomap;
1720
Dan Williamsaaa422c2017-11-13 16:38:44 -08001721 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001722
Ross Zwisler642261a2016-11-08 11:34:45 +11001723 switch (iomap.type) {
1724 case IOMAP_MAPPED:
Jan Kara302a5e32017-11-01 16:36:37 +01001725 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1726 if (error < 0)
1727 goto finish_iomap;
1728
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001729 entry = dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001730 DAX_PMD, write && !sync);
Jan Kara302a5e32017-11-01 16:36:37 +01001731
Jan Karacaa51d22017-11-01 16:36:42 +01001732 /*
1733 * If we are doing synchronous page fault and inode needs fsync,
1734 * we can insert PMD into page tables only after that happens.
1735 * Skip insertion for now and return the pfn so that caller can
1736 * insert it after fsync is done.
1737 */
1738 if (sync) {
1739 if (WARN_ON_ONCE(!pfnp))
1740 goto finish_iomap;
1741 *pfnp = pfn;
1742 result = VM_FAULT_NEEDDSYNC;
1743 goto finish_iomap;
1744 }
1745
Jan Kara302a5e32017-11-01 16:36:37 +01001746 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1747 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1748 write);
Ross Zwisler642261a2016-11-08 11:34:45 +11001749 break;
1750 case IOMAP_UNWRITTEN:
1751 case IOMAP_HOLE:
1752 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001753 break;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001754 result = dax_pmd_load_hole(vmf, &iomap, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001755 break;
1756 default:
1757 WARN_ON_ONCE(1);
1758 break;
1759 }
1760
Jan Kara9f141d62016-10-19 14:34:31 +02001761 finish_iomap:
1762 if (ops->iomap_end) {
1763 int copied = PMD_SIZE;
1764
1765 if (result == VM_FAULT_FALLBACK)
1766 copied = 0;
1767 /*
1768 * The fault is done by now and there's no way back (other
1769 * thread may be already happily using PMD we have installed).
1770 * Just ignore error from ->iomap_end since we cannot do much
1771 * with it.
1772 */
1773 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1774 &iomap);
1775 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001776 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001777 put_locked_mapping_entry(mapping, pgoff);
Ross Zwisler642261a2016-11-08 11:34:45 +11001778 fallback:
1779 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001780 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001781 count_vm_event(THP_FAULT_FALLBACK);
1782 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001783out:
Dave Jiangf4200392017-02-22 15:40:06 -08001784 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001785 return result;
1786}
Dave Jianga2d58162017-02-24 14:56:59 -08001787#else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001788static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001789 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001790{
1791 return VM_FAULT_FALLBACK;
1792}
Ross Zwisler642261a2016-11-08 11:34:45 +11001793#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001794
1795/**
1796 * dax_iomap_fault - handle a page fault on a DAX file
1797 * @vmf: The description of the fault
Jan Karacec04e82017-11-01 16:36:38 +01001798 * @pe_size: Size of the page to fault in
Jan Kara9a0dd422017-11-01 16:36:39 +01001799 * @pfnp: PFN to insert for synchronous faults if fsync is required
Jan Karac0b24622018-01-07 16:38:43 -05001800 * @iomap_errp: Storage for detailed error code in case of error
Jan Karacec04e82017-11-01 16:36:38 +01001801 * @ops: Iomap ops passed from the file system
Dave Jianga2d58162017-02-24 14:56:59 -08001802 *
1803 * When a page fault occurs, filesystems may call this helper in
1804 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1805 * has done all the necessary locking for page fault to proceed
1806 * successfully.
1807 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001808vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
Jan Karac0b24622018-01-07 16:38:43 -05001809 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001810{
Dave Jiangc791ace2017-02-24 14:57:08 -08001811 switch (pe_size) {
1812 case PE_SIZE_PTE:
Jan Karac0b24622018-01-07 16:38:43 -05001813 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001814 case PE_SIZE_PMD:
Jan Kara9a0dd422017-11-01 16:36:39 +01001815 return dax_iomap_pmd_fault(vmf, pfnp, ops);
Dave Jianga2d58162017-02-24 14:56:59 -08001816 default:
1817 return VM_FAULT_FALLBACK;
1818 }
1819}
1820EXPORT_SYMBOL_GPL(dax_iomap_fault);
Jan Kara71eab6d2017-11-01 16:36:43 +01001821
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001822/*
Jan Kara71eab6d2017-11-01 16:36:43 +01001823 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1824 * @vmf: The description of the fault
Jan Kara71eab6d2017-11-01 16:36:43 +01001825 * @pfn: PFN to insert
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001826 * @order: Order of entry to insert.
Jan Kara71eab6d2017-11-01 16:36:43 +01001827 *
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001828 * This function inserts a writeable PTE or PMD entry into the page tables
1829 * for an mmaped DAX file. It also marks the page cache entry as dirty.
Jan Kara71eab6d2017-11-01 16:36:43 +01001830 */
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001831static vm_fault_t
1832dax_insert_pfn_mkwrite(struct vm_fault *vmf, pfn_t pfn, unsigned int order)
Jan Kara71eab6d2017-11-01 16:36:43 +01001833{
1834 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001835 XA_STATE_ORDER(xas, &mapping->i_pages, vmf->pgoff, order);
1836 void *entry;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001837 vm_fault_t ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001838
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001839 xas_lock_irq(&xas);
1840 entry = get_unlocked_entry(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001841 /* Did we race with someone splitting entry or so? */
1842 if (!entry ||
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001843 (order == 0 && !dax_is_pte_entry(entry)) ||
1844 (order == PMD_ORDER && (xa_is_internal(entry) ||
1845 !dax_is_pmd_entry(entry)))) {
1846 put_unlocked_entry(&xas, entry);
1847 xas_unlock_irq(&xas);
Jan Kara71eab6d2017-11-01 16:36:43 +01001848 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1849 VM_FAULT_NOPAGE);
1850 return VM_FAULT_NOPAGE;
1851 }
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001852 xas_set_mark(&xas, PAGECACHE_TAG_DIRTY);
1853 dax_lock_entry(&xas, entry);
1854 xas_unlock_irq(&xas);
1855 if (order == 0)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001856 ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
Jan Kara71eab6d2017-11-01 16:36:43 +01001857#ifdef CONFIG_FS_DAX_PMD
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001858 else if (order == PMD_ORDER)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001859 ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Jan Kara71eab6d2017-11-01 16:36:43 +01001860 pfn, true);
Jan Kara71eab6d2017-11-01 16:36:43 +01001861#endif
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001862 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001863 ret = VM_FAULT_FALLBACK;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001864 dax_unlock_entry(&xas, entry);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001865 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1866 return ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001867}
1868
1869/**
1870 * dax_finish_sync_fault - finish synchronous page fault
1871 * @vmf: The description of the fault
1872 * @pe_size: Size of entry to be inserted
1873 * @pfn: PFN to insert
1874 *
1875 * This function ensures that the file range touched by the page fault is
1876 * stored persistently on the media and handles inserting of appropriate page
1877 * table entry.
1878 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001879vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1880 enum page_entry_size pe_size, pfn_t pfn)
Jan Kara71eab6d2017-11-01 16:36:43 +01001881{
1882 int err;
1883 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001884 unsigned int order = pe_order(pe_size);
1885 size_t len = PAGE_SIZE << order;
Jan Kara71eab6d2017-11-01 16:36:43 +01001886
Jan Kara71eab6d2017-11-01 16:36:43 +01001887 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1888 if (err)
1889 return VM_FAULT_SIGBUS;
Matthew Wilcoxcfc93c62018-03-28 11:48:03 -04001890 return dax_insert_pfn_mkwrite(vmf, pfn, order);
Jan Kara71eab6d2017-11-01 16:36:43 +01001891}
1892EXPORT_SYMBOL_GPL(dax_finish_sync_fault);