blob: acdb996efad2942db463d63f99d128949c6c9f66 [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
Jan Karaac401cc2016-05-12 18:29:18 +020041/* We choose 4096 entries - same as per-zone page wait tables */
42#define DAX_WAIT_TABLE_BITS 12
43#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
44
Ross Zwisler917f3452017-09-06 16:18:58 -070045/* The 'colour' (ie low bits) within a PMD of a page offset. */
46#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
Matthew Wilcox977fbdc2018-01-31 16:17:36 -080047#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT)
Ross Zwisler917f3452017-09-06 16:18:58 -070048
Ross Zwislerce95ab0f2016-11-08 11:31:44 +110049static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
Jan Karaac401cc2016-05-12 18:29:18 +020050
51static int __init init_dax_wait_table(void)
52{
53 int i;
54
55 for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
56 init_waitqueue_head(wait_table + i);
57 return 0;
58}
59fs_initcall(init_dax_wait_table);
60
Ross Zwisler527b19d2017-09-06 16:18:51 -070061/*
Matthew Wilcox3159f942017-11-03 13:30:42 -040062 * DAX pagecache entries use XArray value entries so they can't be mistaken
63 * for pages. We use one bit for locking, one bit for the entry size (PMD)
64 * and two more to tell us if the entry is a zero page or an empty entry that
65 * is just used for locking. In total four special bits.
Ross Zwisler527b19d2017-09-06 16:18:51 -070066 *
67 * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
68 * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
69 * block allocation.
70 */
Matthew Wilcox3159f942017-11-03 13:30:42 -040071#define DAX_SHIFT (4)
72#define DAX_LOCKED (1UL << 0)
73#define DAX_PMD (1UL << 1)
74#define DAX_ZERO_PAGE (1UL << 2)
75#define DAX_EMPTY (1UL << 3)
Ross Zwisler527b19d2017-09-06 16:18:51 -070076
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040077static unsigned long dax_to_pfn(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -070078{
Matthew Wilcox3159f942017-11-03 13:30:42 -040079 return xa_to_value(entry) >> DAX_SHIFT;
Ross Zwisler527b19d2017-09-06 16:18:51 -070080}
81
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040082static void *dax_make_locked(unsigned long pfn, unsigned long flags)
Ross Zwisler527b19d2017-09-06 16:18:51 -070083{
Matthew Wilcox3159f942017-11-03 13:30:42 -040084 return xa_mk_value(flags | ((unsigned long)pfn << DAX_SHIFT) |
85 DAX_LOCKED);
Ross Zwisler527b19d2017-09-06 16:18:51 -070086}
87
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -040088static unsigned int dax_entry_order(void *entry)
Ross Zwisler527b19d2017-09-06 16:18:51 -070089{
Matthew Wilcox3159f942017-11-03 13:30:42 -040090 if (xa_to_value(entry) & DAX_PMD)
Ross Zwisler527b19d2017-09-06 16:18:51 -070091 return PMD_SHIFT - PAGE_SHIFT;
92 return 0;
93}
94
Ross Zwisler642261a2016-11-08 11:34:45 +110095static int dax_is_pmd_entry(void *entry)
96{
Matthew Wilcox3159f942017-11-03 13:30:42 -040097 return xa_to_value(entry) & DAX_PMD;
Ross Zwisler642261a2016-11-08 11:34:45 +110098}
99
100static int dax_is_pte_entry(void *entry)
101{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400102 return !(xa_to_value(entry) & DAX_PMD);
Ross Zwisler642261a2016-11-08 11:34:45 +1100103}
104
105static int dax_is_zero_entry(void *entry)
106{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400107 return xa_to_value(entry) & DAX_ZERO_PAGE;
Ross Zwisler642261a2016-11-08 11:34:45 +1100108}
109
110static int dax_is_empty_entry(void *entry)
111{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400112 return xa_to_value(entry) & DAX_EMPTY;
Ross Zwisler642261a2016-11-08 11:34:45 +1100113}
114
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800115/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400116 * DAX page cache entry locking
Jan Karaac401cc2016-05-12 18:29:18 +0200117 */
118struct exceptional_entry_key {
119 struct address_space *mapping;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100120 pgoff_t entry_start;
Jan Karaac401cc2016-05-12 18:29:18 +0200121};
122
123struct wait_exceptional_entry_queue {
Ingo Molnarac6424b2017-06-20 12:06:13 +0200124 wait_queue_entry_t wait;
Jan Karaac401cc2016-05-12 18:29:18 +0200125 struct exceptional_entry_key key;
126};
127
Ross Zwisler63e95b52016-11-08 11:32:20 +1100128static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
129 pgoff_t index, void *entry, struct exceptional_entry_key *key)
130{
131 unsigned long hash;
132
133 /*
134 * If 'entry' is a PMD, align the 'index' that we use for the wait
135 * queue to the start of that PMD. This ensures that all offsets in
136 * the range covered by the PMD map to the same bit lock.
137 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100138 if (dax_is_pmd_entry(entry))
Ross Zwisler917f3452017-09-06 16:18:58 -0700139 index &= ~PG_PMD_COLOUR;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100140
141 key->mapping = mapping;
142 key->entry_start = index;
143
144 hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
145 return wait_table + hash;
146}
147
Ingo Molnarac6424b2017-06-20 12:06:13 +0200148static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mode,
Jan Karaac401cc2016-05-12 18:29:18 +0200149 int sync, void *keyp)
150{
151 struct exceptional_entry_key *key = keyp;
152 struct wait_exceptional_entry_queue *ewait =
153 container_of(wait, struct wait_exceptional_entry_queue, wait);
154
155 if (key->mapping != ewait->key.mapping ||
Ross Zwisler63e95b52016-11-08 11:32:20 +1100156 key->entry_start != ewait->key.entry_start)
Jan Karaac401cc2016-05-12 18:29:18 +0200157 return 0;
158 return autoremove_wake_function(wait, mode, sync, NULL);
159}
160
161/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700162 * @entry may no longer be the entry at the index in the mapping.
163 * The important information it's conveying is whether the entry at
164 * this index used to be a PMD entry.
Ross Zwislere30331f2017-09-06 16:18:39 -0700165 */
Ross Zwislerd01ad192017-09-06 16:18:47 -0700166static void dax_wake_mapping_entry_waiter(struct address_space *mapping,
Ross Zwislere30331f2017-09-06 16:18:39 -0700167 pgoff_t index, void *entry, bool wake_all)
168{
169 struct exceptional_entry_key key;
170 wait_queue_head_t *wq;
171
172 wq = dax_entry_waitqueue(mapping, index, entry, &key);
173
174 /*
175 * Checking for locked entry and prepare_to_wait_exclusive() happens
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700176 * under the i_pages lock, ditto for entry handling in our callers.
Ross Zwislere30331f2017-09-06 16:18:39 -0700177 * So at this point all tasks that could have seen our entry locked
178 * must be in the waitqueue and the following check will see them.
179 */
180 if (waitqueue_active(wq))
181 __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
182}
183
184/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700185 * Check whether the given slot is locked. Must be called with the i_pages
186 * lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200187 */
188static inline int slot_locked(struct address_space *mapping, void **slot)
189{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400190 unsigned long entry = xa_to_value(
191 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
192 return entry & DAX_LOCKED;
Jan Karaac401cc2016-05-12 18:29:18 +0200193}
194
195/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700196 * Mark the given slot as locked. Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200197 */
198static inline void *lock_slot(struct address_space *mapping, void **slot)
199{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400200 unsigned long v = xa_to_value(
201 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
202 void *entry = xa_mk_value(v | DAX_LOCKED);
203 radix_tree_replace_slot(&mapping->i_pages, slot, entry);
204 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200205}
206
207/*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700208 * Mark the given slot as unlocked. Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200209 */
210static inline void *unlock_slot(struct address_space *mapping, void **slot)
211{
Matthew Wilcox3159f942017-11-03 13:30:42 -0400212 unsigned long v = xa_to_value(
213 radix_tree_deref_slot_protected(slot, &mapping->i_pages.xa_lock));
214 void *entry = xa_mk_value(v & ~DAX_LOCKED);
215 radix_tree_replace_slot(&mapping->i_pages, slot, entry);
216 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200217}
218
219/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400220 * Lookup entry in page cache, wait for it to become unlocked if it is
Matthew Wilcox3159f942017-11-03 13:30:42 -0400221 * a DAX entry and return it. The caller must call
Jan Karaac401cc2016-05-12 18:29:18 +0200222 * put_unlocked_mapping_entry() when he decided not to lock the entry or
223 * put_locked_mapping_entry() when he locked the entry and now wants to
224 * unlock it.
225 *
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700226 * Must be called with the i_pages lock held.
Jan Karaac401cc2016-05-12 18:29:18 +0200227 */
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700228static void *__get_unlocked_mapping_entry(struct address_space *mapping,
229 pgoff_t index, void ***slotp, bool (*wait_fn)(void))
Jan Karaac401cc2016-05-12 18:29:18 +0200230{
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100231 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200232 struct wait_exceptional_entry_queue ewait;
Ross Zwisler63e95b52016-11-08 11:32:20 +1100233 wait_queue_head_t *wq;
Jan Karaac401cc2016-05-12 18:29:18 +0200234
235 init_wait(&ewait.wait);
236 ewait.wait.func = wake_exceptional_entry_func;
Jan Karaac401cc2016-05-12 18:29:18 +0200237
238 for (;;) {
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700239 bool revalidate;
240
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700241 entry = __radix_tree_lookup(&mapping->i_pages, index, NULL,
Jan Karaac401cc2016-05-12 18:29:18 +0200242 &slot);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700243 if (!entry ||
Matthew Wilcox3159f942017-11-03 13:30:42 -0400244 WARN_ON_ONCE(!xa_is_value(entry)) ||
Jan Karaac401cc2016-05-12 18:29:18 +0200245 !slot_locked(mapping, slot)) {
246 if (slotp)
247 *slotp = slot;
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100248 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200249 }
Ross Zwisler63e95b52016-11-08 11:32:20 +1100250
251 wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
Jan Karaac401cc2016-05-12 18:29:18 +0200252 prepare_to_wait_exclusive(wq, &ewait.wait,
253 TASK_UNINTERRUPTIBLE);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700254 xa_unlock_irq(&mapping->i_pages);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700255 revalidate = wait_fn();
Jan Karaac401cc2016-05-12 18:29:18 +0200256 finish_wait(wq, &ewait.wait);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700257 xa_lock_irq(&mapping->i_pages);
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700258 if (revalidate)
259 return ERR_PTR(-EAGAIN);
Jan Karaac401cc2016-05-12 18:29:18 +0200260 }
261}
262
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700263static bool entry_wait(void)
264{
265 schedule();
266 /*
267 * Never return an ERR_PTR() from
268 * __get_unlocked_mapping_entry(), just keep looping.
269 */
270 return false;
271}
272
273static void *get_unlocked_mapping_entry(struct address_space *mapping,
274 pgoff_t index, void ***slotp)
275{
276 return __get_unlocked_mapping_entry(mapping, index, slotp, entry_wait);
277}
278
279static void unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
Jan Karab1aa8122016-12-14 15:07:24 -0800280{
281 void *entry, **slot;
282
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700283 xa_lock_irq(&mapping->i_pages);
284 entry = __radix_tree_lookup(&mapping->i_pages, index, NULL, &slot);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400285 if (WARN_ON_ONCE(!entry || !xa_is_value(entry) ||
Jan Karab1aa8122016-12-14 15:07:24 -0800286 !slot_locked(mapping, slot))) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700287 xa_unlock_irq(&mapping->i_pages);
Jan Karab1aa8122016-12-14 15:07:24 -0800288 return;
289 }
290 unlock_slot(mapping, slot);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700291 xa_unlock_irq(&mapping->i_pages);
Jan Karab1aa8122016-12-14 15:07:24 -0800292 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
293}
294
Jan Karaac401cc2016-05-12 18:29:18 +0200295static void put_locked_mapping_entry(struct address_space *mapping,
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700296 pgoff_t index)
Jan Karaac401cc2016-05-12 18:29:18 +0200297{
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700298 unlock_mapping_entry(mapping, index);
Jan Karaac401cc2016-05-12 18:29:18 +0200299}
300
301/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400302 * Called when we are done with page cache entry we looked up via
Jan Karaac401cc2016-05-12 18:29:18 +0200303 * get_unlocked_mapping_entry() and which we didn't lock in the end.
304 */
305static void put_unlocked_mapping_entry(struct address_space *mapping,
306 pgoff_t index, void *entry)
307{
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700308 if (!entry)
Jan Karaac401cc2016-05-12 18:29:18 +0200309 return;
310
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400311 /* We have to wake up next waiter for the page cache entry lock */
Ross Zwisler422476c2016-11-08 11:33:44 +1100312 dax_wake_mapping_entry_waiter(mapping, index, entry, false);
313}
314
Dan Williamsd2c997c2017-12-22 22:02:48 -0800315static unsigned long dax_entry_size(void *entry)
316{
317 if (dax_is_zero_entry(entry))
318 return 0;
319 else if (dax_is_empty_entry(entry))
320 return 0;
321 else if (dax_is_pmd_entry(entry))
322 return PMD_SIZE;
323 else
324 return PAGE_SIZE;
325}
326
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400327static unsigned long dax_end_pfn(void *entry)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800328{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400329 return dax_to_pfn(entry) + dax_entry_size(entry) / PAGE_SIZE;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800330}
331
332/*
333 * Iterate through all mapped pfns represented by an entry, i.e. skip
334 * 'empty' and 'zero' entries.
335 */
336#define for_each_mapped_pfn(entry, pfn) \
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400337 for (pfn = dax_to_pfn(entry); \
338 pfn < dax_end_pfn(entry); pfn++)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800339
Dan Williams73449da2018-07-13 21:49:50 -0700340/*
341 * TODO: for reflink+dax we need a way to associate a single page with
342 * multiple address_space instances at different linear_page_index()
343 * offsets.
344 */
345static void dax_associate_entry(void *entry, struct address_space *mapping,
346 struct vm_area_struct *vma, unsigned long address)
Dan Williamsd2c997c2017-12-22 22:02:48 -0800347{
Dan Williams73449da2018-07-13 21:49:50 -0700348 unsigned long size = dax_entry_size(entry), pfn, index;
349 int i = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800350
351 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
352 return;
353
Dan Williams73449da2018-07-13 21:49:50 -0700354 index = linear_page_index(vma, address & ~(size - 1));
Dan Williamsd2c997c2017-12-22 22:02:48 -0800355 for_each_mapped_pfn(entry, pfn) {
356 struct page *page = pfn_to_page(pfn);
357
358 WARN_ON_ONCE(page->mapping);
359 page->mapping = mapping;
Dan Williams73449da2018-07-13 21:49:50 -0700360 page->index = index + i++;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800361 }
362}
363
364static void dax_disassociate_entry(void *entry, struct address_space *mapping,
365 bool trunc)
366{
367 unsigned long pfn;
368
369 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
370 return;
371
372 for_each_mapped_pfn(entry, pfn) {
373 struct page *page = pfn_to_page(pfn);
374
375 WARN_ON_ONCE(trunc && page_ref_count(page) > 1);
376 WARN_ON_ONCE(page->mapping && page->mapping != mapping);
377 page->mapping = NULL;
Dan Williams73449da2018-07-13 21:49:50 -0700378 page->index = 0;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800379 }
380}
381
Dan Williams5fac7402018-03-09 17:44:31 -0800382static struct page *dax_busy_page(void *entry)
383{
384 unsigned long pfn;
385
386 for_each_mapped_pfn(entry, pfn) {
387 struct page *page = pfn_to_page(pfn);
388
389 if (page_ref_count(page) > 1)
390 return page;
391 }
392 return NULL;
393}
394
Dan Williamsc2a7d2a2018-07-13 21:50:16 -0700395static bool entry_wait_revalidate(void)
396{
397 rcu_read_unlock();
398 schedule();
399 rcu_read_lock();
400
401 /*
402 * Tell __get_unlocked_mapping_entry() to take a break, we need
403 * to revalidate page->mapping after dropping locks
404 */
405 return true;
406}
407
408bool dax_lock_mapping_entry(struct page *page)
409{
410 pgoff_t index;
411 struct inode *inode;
412 bool did_lock = false;
413 void *entry = NULL, **slot;
414 struct address_space *mapping;
415
416 rcu_read_lock();
417 for (;;) {
418 mapping = READ_ONCE(page->mapping);
419
420 if (!dax_mapping(mapping))
421 break;
422
423 /*
424 * In the device-dax case there's no need to lock, a
425 * struct dev_pagemap pin is sufficient to keep the
426 * inode alive, and we assume we have dev_pagemap pin
427 * otherwise we would not have a valid pfn_to_page()
428 * translation.
429 */
430 inode = mapping->host;
431 if (S_ISCHR(inode->i_mode)) {
432 did_lock = true;
433 break;
434 }
435
436 xa_lock_irq(&mapping->i_pages);
437 if (mapping != page->mapping) {
438 xa_unlock_irq(&mapping->i_pages);
439 continue;
440 }
441 index = page->index;
442
443 entry = __get_unlocked_mapping_entry(mapping, index, &slot,
444 entry_wait_revalidate);
445 if (!entry) {
446 xa_unlock_irq(&mapping->i_pages);
447 break;
448 } else if (IS_ERR(entry)) {
449 WARN_ON_ONCE(PTR_ERR(entry) != -EAGAIN);
450 continue;
451 }
452 lock_slot(mapping, slot);
453 did_lock = true;
454 xa_unlock_irq(&mapping->i_pages);
455 break;
456 }
457 rcu_read_unlock();
458
459 return did_lock;
460}
461
462void dax_unlock_mapping_entry(struct page *page)
463{
464 struct address_space *mapping = page->mapping;
465 struct inode *inode = mapping->host;
466
467 if (S_ISCHR(inode->i_mode))
468 return;
469
470 unlock_mapping_entry(mapping, page->index);
471}
472
Jan Karaac401cc2016-05-12 18:29:18 +0200473/*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400474 * Find page cache entry at given index. If it is a DAX entry, return it
475 * with the entry locked. If the page cache doesn't contain an entry at
476 * that index, add a locked empty entry.
Jan Karaac401cc2016-05-12 18:29:18 +0200477 *
Matthew Wilcox3159f942017-11-03 13:30:42 -0400478 * When requesting an entry with size DAX_PMD, grab_mapping_entry() will
Ross Zwisler642261a2016-11-08 11:34:45 +1100479 * either return that locked entry or will return an error. This error will
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700480 * happen if there are any 4k entries within the 2MiB range that we are
481 * requesting.
Ross Zwisler642261a2016-11-08 11:34:45 +1100482 *
483 * We always favor 4k entries over 2MiB entries. There isn't a flow where we
484 * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
485 * insertion will fail if it finds any 4k entries already in the tree, and a
486 * 4k insertion will cause an existing 2MiB entry to be unmapped and
487 * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
488 * well as 2MiB empty entries.
489 *
490 * The exception to this downgrade path is for 2MiB DAX PMD entries that have
491 * real storage backing them. We will leave these real 2MiB DAX entries in
492 * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
493 *
Jan Karaac401cc2016-05-12 18:29:18 +0200494 * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
495 * persistent memory the benefit is doubtful. We can add that later if we can
496 * show it helps.
497 */
Ross Zwisler642261a2016-11-08 11:34:45 +1100498static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
499 unsigned long size_flag)
Jan Karaac401cc2016-05-12 18:29:18 +0200500{
Ross Zwisler642261a2016-11-08 11:34:45 +1100501 bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100502 void *entry, **slot;
Jan Karaac401cc2016-05-12 18:29:18 +0200503
504restart:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700505 xa_lock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100506 entry = get_unlocked_mapping_entry(mapping, index, &slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100507
Matthew Wilcox3159f942017-11-03 13:30:42 -0400508 if (WARN_ON_ONCE(entry && !xa_is_value(entry))) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700509 entry = ERR_PTR(-EIO);
510 goto out_unlock;
511 }
512
Ross Zwisler642261a2016-11-08 11:34:45 +1100513 if (entry) {
Matthew Wilcox3159f942017-11-03 13:30:42 -0400514 if (size_flag & DAX_PMD) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700515 if (dax_is_pte_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100516 put_unlocked_mapping_entry(mapping, index,
517 entry);
518 entry = ERR_PTR(-EEXIST);
519 goto out_unlock;
520 }
521 } else { /* trying to grab a PTE entry */
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700522 if (dax_is_pmd_entry(entry) &&
Ross Zwisler642261a2016-11-08 11:34:45 +1100523 (dax_is_zero_entry(entry) ||
524 dax_is_empty_entry(entry))) {
525 pmd_downgrade = true;
526 }
527 }
528 }
529
Jan Karaac401cc2016-05-12 18:29:18 +0200530 /* No entry for given index? Make sure radix tree is big enough. */
Ross Zwisler642261a2016-11-08 11:34:45 +1100531 if (!entry || pmd_downgrade) {
Jan Karaac401cc2016-05-12 18:29:18 +0200532 int err;
533
Ross Zwisler642261a2016-11-08 11:34:45 +1100534 if (pmd_downgrade) {
535 /*
536 * Make sure 'entry' remains valid while we drop
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700537 * the i_pages lock.
Ross Zwisler642261a2016-11-08 11:34:45 +1100538 */
539 entry = lock_slot(mapping, slot);
540 }
541
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700542 xa_unlock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100543 /*
544 * Besides huge zero pages the only other thing that gets
545 * downgraded are empty entries which don't need to be
546 * unmapped.
547 */
548 if (pmd_downgrade && dax_is_zero_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800549 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
550 PG_PMD_NR, false);
Ross Zwisler642261a2016-11-08 11:34:45 +1100551
Jan Kara0cb80b42016-12-12 21:34:12 -0500552 err = radix_tree_preload(
553 mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
554 if (err) {
555 if (pmd_downgrade)
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700556 put_locked_mapping_entry(mapping, index);
Jan Kara0cb80b42016-12-12 21:34:12 -0500557 return ERR_PTR(err);
558 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700559 xa_lock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100560
Ross Zwislere11f8b72017-04-07 16:04:57 -0700561 if (!entry) {
562 /*
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700563 * We needed to drop the i_pages lock while calling
Ross Zwislere11f8b72017-04-07 16:04:57 -0700564 * radix_tree_preload() and we didn't have an entry to
565 * lock. See if another thread inserted an entry at
566 * our index during this time.
567 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700568 entry = __radix_tree_lookup(&mapping->i_pages, index,
Ross Zwislere11f8b72017-04-07 16:04:57 -0700569 NULL, &slot);
570 if (entry) {
571 radix_tree_preload_end();
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700572 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere11f8b72017-04-07 16:04:57 -0700573 goto restart;
574 }
575 }
576
Ross Zwisler642261a2016-11-08 11:34:45 +1100577 if (pmd_downgrade) {
Dan Williamsd2c997c2017-12-22 22:02:48 -0800578 dax_disassociate_entry(entry, mapping, false);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700579 radix_tree_delete(&mapping->i_pages, index);
Ross Zwisler642261a2016-11-08 11:34:45 +1100580 mapping->nrexceptional--;
581 dax_wake_mapping_entry_waiter(mapping, index, entry,
582 true);
583 }
584
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400585 entry = dax_make_locked(0, size_flag | DAX_EMPTY);
Ross Zwisler642261a2016-11-08 11:34:45 +1100586
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700587 err = __radix_tree_insert(&mapping->i_pages, index,
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400588 dax_entry_order(entry), entry);
Jan Karaac401cc2016-05-12 18:29:18 +0200589 radix_tree_preload_end();
590 if (err) {
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700591 xa_unlock_irq(&mapping->i_pages);
Ross Zwisler642261a2016-11-08 11:34:45 +1100592 /*
Ross Zwislere11f8b72017-04-07 16:04:57 -0700593 * Our insertion of a DAX entry failed, most likely
594 * because we were inserting a PMD entry and it
595 * collided with a PTE sized entry at a different
596 * index in the PMD range. We haven't inserted
597 * anything into the radix tree and have no waiters to
598 * wake.
Ross Zwisler642261a2016-11-08 11:34:45 +1100599 */
Jan Karaac401cc2016-05-12 18:29:18 +0200600 return ERR_PTR(err);
601 }
602 /* Good, we have inserted empty locked entry into the tree. */
603 mapping->nrexceptional++;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700604 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100605 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200606 }
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100607 entry = lock_slot(mapping, slot);
Ross Zwisler642261a2016-11-08 11:34:45 +1100608 out_unlock:
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700609 xa_unlock_irq(&mapping->i_pages);
Ross Zwislere3ad61c2016-11-08 11:32:12 +1100610 return entry;
Jan Karaac401cc2016-05-12 18:29:18 +0200611}
612
Dan Williams5fac7402018-03-09 17:44:31 -0800613/**
614 * dax_layout_busy_page - find first pinned page in @mapping
615 * @mapping: address space to scan for a page with ref count > 1
616 *
617 * DAX requires ZONE_DEVICE mapped pages. These pages are never
618 * 'onlined' to the page allocator so they are considered idle when
619 * page->count == 1. A filesystem uses this interface to determine if
620 * any page in the mapping is busy, i.e. for DMA, or other
621 * get_user_pages() usages.
622 *
623 * It is expected that the filesystem is holding locks to block the
624 * establishment of new mappings in this address_space. I.e. it expects
625 * to be able to run unmap_mapping_range() and subsequently not race
626 * mapping_mapped() becoming true.
627 */
628struct page *dax_layout_busy_page(struct address_space *mapping)
629{
630 pgoff_t indices[PAGEVEC_SIZE];
631 struct page *page = NULL;
632 struct pagevec pvec;
633 pgoff_t index, end;
634 unsigned i;
635
636 /*
637 * In the 'limited' case get_user_pages() for dax is disabled.
638 */
639 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED))
640 return NULL;
641
642 if (!dax_mapping(mapping) || !mapping_mapped(mapping))
643 return NULL;
644
645 pagevec_init(&pvec);
646 index = 0;
647 end = -1;
648
649 /*
650 * If we race get_user_pages_fast() here either we'll see the
651 * elevated page count in the pagevec_lookup and wait, or
652 * get_user_pages_fast() will see that the page it took a reference
653 * against is no longer mapped in the page tables and bail to the
654 * get_user_pages() slow path. The slow path is protected by
655 * pte_lock() and pmd_lock(). New references are not taken without
656 * holding those locks, and unmap_mapping_range() will not zero the
657 * pte or pmd without holding the respective lock, so we are
658 * guaranteed to either see new references or prevent new
659 * references from being established.
660 */
661 unmap_mapping_range(mapping, 0, 0, 1);
662
663 while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
664 min(end - index, (pgoff_t)PAGEVEC_SIZE),
665 indices)) {
666 for (i = 0; i < pagevec_count(&pvec); i++) {
667 struct page *pvec_ent = pvec.pages[i];
668 void *entry;
669
670 index = indices[i];
671 if (index >= end)
672 break;
673
Matthew Wilcox3159f942017-11-03 13:30:42 -0400674 if (WARN_ON_ONCE(!xa_is_value(pvec_ent)))
Dan Williams5fac7402018-03-09 17:44:31 -0800675 continue;
676
677 xa_lock_irq(&mapping->i_pages);
678 entry = get_unlocked_mapping_entry(mapping, index, NULL);
679 if (entry)
680 page = dax_busy_page(entry);
681 put_unlocked_mapping_entry(mapping, index, entry);
682 xa_unlock_irq(&mapping->i_pages);
683 if (page)
684 break;
685 }
Ross Zwislercdbf8892018-07-29 16:59:16 -0400686
687 /*
688 * We don't expect normal struct page entries to exist in our
689 * tree, but we keep these pagevec calls so that this code is
690 * consistent with the common pattern for handling pagevecs
691 * throughout the kernel.
692 */
Dan Williams5fac7402018-03-09 17:44:31 -0800693 pagevec_remove_exceptionals(&pvec);
694 pagevec_release(&pvec);
695 index++;
696
697 if (page)
698 break;
699 }
700 return page;
701}
702EXPORT_SYMBOL_GPL(dax_layout_busy_page);
703
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400704static int __dax_invalidate_entry(struct address_space *mapping,
Jan Karac6dcf522016-08-10 17:22:44 +0200705 pgoff_t index, bool trunc)
706{
707 int ret = 0;
708 void *entry;
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700709 struct radix_tree_root *pages = &mapping->i_pages;
Jan Karac6dcf522016-08-10 17:22:44 +0200710
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700711 xa_lock_irq(pages);
Jan Karac6dcf522016-08-10 17:22:44 +0200712 entry = get_unlocked_mapping_entry(mapping, index, NULL);
Matthew Wilcox3159f942017-11-03 13:30:42 -0400713 if (!entry || WARN_ON_ONCE(!xa_is_value(entry)))
Jan Karac6dcf522016-08-10 17:22:44 +0200714 goto out;
715 if (!trunc &&
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700716 (radix_tree_tag_get(pages, index, PAGECACHE_TAG_DIRTY) ||
717 radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE)))
Jan Karac6dcf522016-08-10 17:22:44 +0200718 goto out;
Dan Williamsd2c997c2017-12-22 22:02:48 -0800719 dax_disassociate_entry(entry, mapping, trunc);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700720 radix_tree_delete(pages, index);
Jan Karac6dcf522016-08-10 17:22:44 +0200721 mapping->nrexceptional--;
722 ret = 1;
723out:
724 put_unlocked_mapping_entry(mapping, index, entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700725 xa_unlock_irq(pages);
Jan Karac6dcf522016-08-10 17:22:44 +0200726 return ret;
727}
Jan Karaac401cc2016-05-12 18:29:18 +0200728/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400729 * Delete DAX entry at @index from @mapping. Wait for it
730 * to be unlocked before deleting it.
Jan Karaac401cc2016-05-12 18:29:18 +0200731 */
732int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
733{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400734 int ret = __dax_invalidate_entry(mapping, index, true);
Jan Karaac401cc2016-05-12 18:29:18 +0200735
Jan Karaac401cc2016-05-12 18:29:18 +0200736 /*
737 * This gets called from truncate / punch_hole path. As such, the caller
738 * must hold locks protecting against concurrent modifications of the
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400739 * page cache (usually fs-private i_mmap_sem for writing). Since the
Matthew Wilcox3159f942017-11-03 13:30:42 -0400740 * caller has seen a DAX entry for this index, we better find it
Jan Karaac401cc2016-05-12 18:29:18 +0200741 * at that index as well...
742 */
Jan Karac6dcf522016-08-10 17:22:44 +0200743 WARN_ON_ONCE(!ret);
744 return ret;
745}
Jan Karaac401cc2016-05-12 18:29:18 +0200746
Jan Karac6dcf522016-08-10 17:22:44 +0200747/*
Matthew Wilcox3159f942017-11-03 13:30:42 -0400748 * Invalidate DAX entry if it is clean.
Jan Karac6dcf522016-08-10 17:22:44 +0200749 */
750int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
751 pgoff_t index)
752{
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400753 return __dax_invalidate_entry(mapping, index, false);
Jan Karaac401cc2016-05-12 18:29:18 +0200754}
755
Dan Williamscccbce62017-01-27 13:31:42 -0800756static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
757 sector_t sector, size_t size, struct page *to,
758 unsigned long vaddr)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800759{
Dan Williamscccbce62017-01-27 13:31:42 -0800760 void *vto, *kaddr;
761 pgoff_t pgoff;
Dan Williamscccbce62017-01-27 13:31:42 -0800762 long rc;
763 int id;
Ross Zwislere2e05392015-08-18 13:55:41 -0600764
Dan Williamscccbce62017-01-27 13:31:42 -0800765 rc = bdev_dax_pgoff(bdev, sector, size, &pgoff);
766 if (rc)
767 return rc;
768
769 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +0800770 rc = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size), &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -0800771 if (rc < 0) {
772 dax_read_unlock(id);
773 return rc;
774 }
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800775 vto = kmap_atomic(to);
Dan Williamscccbce62017-01-27 13:31:42 -0800776 copy_user_page(vto, (void __force *)kaddr, vaddr, to);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800777 kunmap_atomic(vto);
Dan Williamscccbce62017-01-27 13:31:42 -0800778 dax_read_unlock(id);
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -0800779 return 0;
780}
781
Ross Zwisler642261a2016-11-08 11:34:45 +1100782/*
783 * By this point grab_mapping_entry() has ensured that we have a locked entry
784 * of the appropriate size so we don't have to worry about downgrading PMDs to
785 * PTEs. If we happen to be trying to insert a PTE and there is a PMD
786 * already in the tree, we will skip the insertion and just dirty the PMD as
787 * appropriate.
788 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400789static void *dax_insert_entry(struct address_space *mapping,
790 struct vm_fault *vmf,
791 void *entry, pfn_t pfn_t, unsigned long flags, bool dirty)
Ross Zwisler9973c982016-01-22 15:10:47 -0800792{
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700793 struct radix_tree_root *pages = &mapping->i_pages;
Dan Williams3fe07912017-10-14 17:13:45 -0700794 unsigned long pfn = pfn_t_to_pfn(pfn_t);
Jan Karaac401cc2016-05-12 18:29:18 +0200795 pgoff_t index = vmf->pgoff;
Dan Williams3fe07912017-10-14 17:13:45 -0700796 void *new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800797
Jan Karaf5b7b742017-11-01 16:36:40 +0100798 if (dirty)
Dmitry Monakhovd2b2a282016-02-05 15:36:55 -0800799 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
Ross Zwisler9973c982016-01-22 15:10:47 -0800800
Matthew Wilcox3159f942017-11-03 13:30:42 -0400801 if (dax_is_zero_entry(entry) && !(flags & DAX_ZERO_PAGE)) {
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700802 /* we are replacing a zero page with block mapping */
803 if (dax_is_pmd_entry(entry))
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800804 unmap_mapping_pages(mapping, index & ~PG_PMD_COLOUR,
805 PG_PMD_NR, false);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700806 else /* pte entry */
Matthew Wilcox977fbdc2018-01-31 16:17:36 -0800807 unmap_mapping_pages(mapping, vmf->pgoff, 1, false);
Ross Zwisler9973c982016-01-22 15:10:47 -0800808 }
809
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700810 xa_lock_irq(pages);
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400811 new_entry = dax_make_locked(pfn, flags);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800812 if (dax_entry_size(entry) != dax_entry_size(new_entry)) {
813 dax_disassociate_entry(entry, mapping, false);
Dan Williams73449da2018-07-13 21:49:50 -0700814 dax_associate_entry(new_entry, mapping, vmf->vma, vmf->address);
Dan Williamsd2c997c2017-12-22 22:02:48 -0800815 }
Ross Zwisler642261a2016-11-08 11:34:45 +1100816
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700817 if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
Ross Zwisler642261a2016-11-08 11:34:45 +1100818 /*
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400819 * Only swap our new entry into the page cache if the current
Ross Zwisler642261a2016-11-08 11:34:45 +1100820 * entry is a zero page or an empty entry. If a normal PTE or
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400821 * PMD entry is already in the cache, we leave it alone. This
Ross Zwisler642261a2016-11-08 11:34:45 +1100822 * means that if we are trying to insert a PTE and the
823 * existing entry is a PMD, we will just leave the PMD in the
824 * tree and dirty it if necessary.
825 */
Johannes Weinerf7942432016-12-12 16:43:41 -0800826 struct radix_tree_node *node;
Jan Karaac401cc2016-05-12 18:29:18 +0200827 void **slot;
828 void *ret;
Ross Zwisler9973c982016-01-22 15:10:47 -0800829
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700830 ret = __radix_tree_lookup(pages, index, &node, &slot);
Jan Karaac401cc2016-05-12 18:29:18 +0200831 WARN_ON_ONCE(ret != entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700832 __radix_tree_replace(pages, node, slot,
Mel Gormanc7df8ad2017-11-15 17:37:41 -0800833 new_entry, NULL);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700834 entry = new_entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800835 }
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700836
Jan Karaf5b7b742017-11-01 16:36:40 +0100837 if (dirty)
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700838 radix_tree_tag_set(pages, index, PAGECACHE_TAG_DIRTY);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700839
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700840 xa_unlock_irq(pages);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700841 return entry;
Ross Zwisler9973c982016-01-22 15:10:47 -0800842}
843
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400844static inline
845unsigned long pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma)
Jan Kara4b4bb462016-12-14 15:07:53 -0800846{
847 unsigned long address;
848
849 address = vma->vm_start + ((pgoff - vma->vm_pgoff) << PAGE_SHIFT);
850 VM_BUG_ON_VMA(address < vma->vm_start || address >= vma->vm_end, vma);
851 return address;
852}
853
854/* Walk all mappings of a given index of a file and writeprotect them */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400855static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,
856 unsigned long pfn)
Jan Kara4b4bb462016-12-14 15:07:53 -0800857{
858 struct vm_area_struct *vma;
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800859 pte_t pte, *ptep = NULL;
860 pmd_t *pmdp = NULL;
Jan Kara4b4bb462016-12-14 15:07:53 -0800861 spinlock_t *ptl;
Jan Kara4b4bb462016-12-14 15:07:53 -0800862
863 i_mmap_lock_read(mapping);
864 vma_interval_tree_foreach(vma, &mapping->i_mmap, index, index) {
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400865 unsigned long address, start, end;
Jan Kara4b4bb462016-12-14 15:07:53 -0800866
867 cond_resched();
868
869 if (!(vma->vm_flags & VM_SHARED))
870 continue;
871
872 address = pgoff_address(index, vma);
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400873
874 /*
875 * Note because we provide start/end to follow_pte_pmd it will
876 * call mmu_notifier_invalidate_range_start() on our behalf
877 * before taking any lock.
878 */
879 if (follow_pte_pmd(vma->vm_mm, address, &start, &end, &ptep, &pmdp, &ptl))
Jan Kara4b4bb462016-12-14 15:07:53 -0800880 continue;
Jan Kara4b4bb462016-12-14 15:07:53 -0800881
Jérôme Glisse0f108512017-11-15 17:34:07 -0800882 /*
883 * No need to call mmu_notifier_invalidate_range() as we are
884 * downgrading page table protection not changing it to point
885 * to a new page.
886 *
Mike Rapoportad56b732018-03-21 21:22:47 +0200887 * See Documentation/vm/mmu_notifier.rst
Jérôme Glisse0f108512017-11-15 17:34:07 -0800888 */
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800889 if (pmdp) {
890#ifdef CONFIG_FS_DAX_PMD
891 pmd_t pmd;
892
893 if (pfn != pmd_pfn(*pmdp))
894 goto unlock_pmd;
Linus Torvaldsf6f37322017-12-15 18:53:22 -0800895 if (!pmd_dirty(*pmdp) && !pmd_write(*pmdp))
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800896 goto unlock_pmd;
897
898 flush_cache_page(vma, address, pfn);
899 pmd = pmdp_huge_clear_flush(vma, address, pmdp);
900 pmd = pmd_wrprotect(pmd);
901 pmd = pmd_mkclean(pmd);
902 set_pmd_at(vma->vm_mm, address, pmdp, pmd);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800903unlock_pmd:
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800904#endif
Jan H. Schönherree190ca2018-01-31 16:14:04 -0800905 spin_unlock(ptl);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800906 } else {
907 if (pfn != pte_pfn(*ptep))
908 goto unlock_pte;
909 if (!pte_dirty(*ptep) && !pte_write(*ptep))
910 goto unlock_pte;
911
912 flush_cache_page(vma, address, pfn);
913 pte = ptep_clear_flush(vma, address, ptep);
914 pte = pte_wrprotect(pte);
915 pte = pte_mkclean(pte);
916 set_pte_at(vma->vm_mm, address, ptep, pte);
Ross Zwislerf729c8c2017-01-10 16:57:24 -0800917unlock_pte:
918 pte_unmap_unlock(ptep, ptl);
919 }
Jan Kara4b4bb462016-12-14 15:07:53 -0800920
Jérôme Glissea4d1a882017-08-31 17:17:26 -0400921 mmu_notifier_invalidate_range_end(vma->vm_mm, start, end);
Jan Kara4b4bb462016-12-14 15:07:53 -0800922 }
923 i_mmap_unlock_read(mapping);
924}
925
Dan Williams3fe07912017-10-14 17:13:45 -0700926static int dax_writeback_one(struct dax_device *dax_dev,
927 struct address_space *mapping, pgoff_t index, void *entry)
Ross Zwisler9973c982016-01-22 15:10:47 -0800928{
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700929 struct radix_tree_root *pages = &mapping->i_pages;
Dan Williams3fe07912017-10-14 17:13:45 -0700930 void *entry2, **slot;
931 unsigned long pfn;
932 long ret = 0;
Dan Williamscccbce62017-01-27 13:31:42 -0800933 size_t size;
Ross Zwisler9973c982016-01-22 15:10:47 -0800934
Ross Zwisler9973c982016-01-22 15:10:47 -0800935 /*
Jan Karaa6abc2c2016-12-14 15:07:47 -0800936 * A page got tagged dirty in DAX mapping? Something is seriously
937 * wrong.
Ross Zwisler9973c982016-01-22 15:10:47 -0800938 */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400939 if (WARN_ON(!xa_is_value(entry)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800940 return -EIO;
Ross Zwisler9973c982016-01-22 15:10:47 -0800941
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700942 xa_lock_irq(pages);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800943 entry2 = get_unlocked_mapping_entry(mapping, index, &slot);
944 /* Entry got punched out / reallocated? */
Matthew Wilcox3159f942017-11-03 13:30:42 -0400945 if (!entry2 || WARN_ON_ONCE(!xa_is_value(entry2)))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800946 goto put_unlocked;
947 /*
948 * Entry got reallocated elsewhere? No need to writeback. We have to
Dan Williams3fe07912017-10-14 17:13:45 -0700949 * compare pfns as we must not bail out due to difference in lockbit
Jan Karaa6abc2c2016-12-14 15:07:47 -0800950 * or entry type.
951 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400952 if (dax_to_pfn(entry2) != dax_to_pfn(entry))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800953 goto put_unlocked;
Ross Zwisler642261a2016-11-08 11:34:45 +1100954 if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
955 dax_is_zero_entry(entry))) {
Ross Zwisler9973c982016-01-22 15:10:47 -0800956 ret = -EIO;
Jan Karaa6abc2c2016-12-14 15:07:47 -0800957 goto put_unlocked;
Ross Zwisler9973c982016-01-22 15:10:47 -0800958 }
959
Jan Karaa6abc2c2016-12-14 15:07:47 -0800960 /* Another fsync thread may have already written back this entry */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700961 if (!radix_tree_tag_get(pages, index, PAGECACHE_TAG_TOWRITE))
Jan Karaa6abc2c2016-12-14 15:07:47 -0800962 goto put_unlocked;
963 /* Lock the entry to serialize with page faults */
964 entry = lock_slot(mapping, slot);
965 /*
966 * We can clear the tag now but we have to be careful so that concurrent
967 * dax_writeback_one() calls for the same index cannot finish before we
968 * actually flush the caches. This is achieved as the calls will look
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700969 * at the entry only under the i_pages lock and once they do that
970 * they will see the entry locked and wait for it to unlock.
Jan Karaa6abc2c2016-12-14 15:07:47 -0800971 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700972 radix_tree_tag_clear(pages, index, PAGECACHE_TAG_TOWRITE);
973 xa_unlock_irq(pages);
Jan Karaa6abc2c2016-12-14 15:07:47 -0800974
Ross Zwisler642261a2016-11-08 11:34:45 +1100975 /*
976 * Even if dax_writeback_mapping_range() was given a wbc->range_start
977 * in the middle of a PMD, the 'index' we are given will be aligned to
Dan Williams3fe07912017-10-14 17:13:45 -0700978 * the start index of the PMD, as will the pfn we pull from 'entry'.
979 * This allows us to flush for PMD_SIZE and not have to worry about
980 * partial PMD writebacks.
Ross Zwisler642261a2016-11-08 11:34:45 +1100981 */
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400982 pfn = dax_to_pfn(entry);
983 size = PAGE_SIZE << dax_entry_order(entry);
Dan Williamscccbce62017-01-27 13:31:42 -0800984
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -0400985 dax_entry_mkclean(mapping, index, pfn);
Dan Williams3fe07912017-10-14 17:13:45 -0700986 dax_flush(dax_dev, page_address(pfn_to_page(pfn)), size);
Jan Kara4b4bb462016-12-14 15:07:53 -0800987 /*
988 * After we have flushed the cache, we can clear the dirty tag. There
989 * cannot be new dirty data in the pfn after the flush has completed as
990 * the pfn mappings are writeprotected and fault waits for mapping
991 * entry lock.
992 */
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700993 xa_lock_irq(pages);
994 radix_tree_tag_clear(pages, index, PAGECACHE_TAG_DIRTY);
995 xa_unlock_irq(pages);
Ross Zwislerf9bc3a02017-05-08 16:00:13 -0700996 trace_dax_writeback_one(mapping->host, index, size >> PAGE_SHIFT);
Ross Zwisler91d25ba2017-09-06 16:18:43 -0700997 put_locked_mapping_entry(mapping, index);
Ross Zwisler9973c982016-01-22 15:10:47 -0800998 return ret;
999
Jan Karaa6abc2c2016-12-14 15:07:47 -08001000 put_unlocked:
1001 put_unlocked_mapping_entry(mapping, index, entry2);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001002 xa_unlock_irq(pages);
Ross Zwisler9973c982016-01-22 15:10:47 -08001003 return ret;
1004}
1005
1006/*
1007 * Flush the mapping to the persistent domain within the byte range of [start,
1008 * end]. This is required by data integrity operations to ensure file data is
1009 * on persistent storage prior to completion of the operation.
1010 */
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001011int dax_writeback_mapping_range(struct address_space *mapping,
1012 struct block_device *bdev, struct writeback_control *wbc)
Ross Zwisler9973c982016-01-22 15:10:47 -08001013{
1014 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001015 pgoff_t start_index, end_index;
Ross Zwisler9973c982016-01-22 15:10:47 -08001016 pgoff_t indices[PAGEVEC_SIZE];
Dan Williamscccbce62017-01-27 13:31:42 -08001017 struct dax_device *dax_dev;
Ross Zwisler9973c982016-01-22 15:10:47 -08001018 struct pagevec pvec;
1019 bool done = false;
1020 int i, ret = 0;
Ross Zwisler9973c982016-01-22 15:10:47 -08001021
1022 if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
1023 return -EIO;
1024
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001025 if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
1026 return 0;
1027
Dan Williamscccbce62017-01-27 13:31:42 -08001028 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
1029 if (!dax_dev)
1030 return -EIO;
1031
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 start_index = wbc->range_start >> PAGE_SHIFT;
1033 end_index = wbc->range_end >> PAGE_SHIFT;
Ross Zwisler9973c982016-01-22 15:10:47 -08001034
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001035 trace_dax_writeback_range(inode, start_index, end_index);
1036
Ross Zwisler9973c982016-01-22 15:10:47 -08001037 tag_pages_for_writeback(mapping, start_index, end_index);
1038
Mel Gorman86679822017-11-15 17:37:52 -08001039 pagevec_init(&pvec);
Ross Zwisler9973c982016-01-22 15:10:47 -08001040 while (!done) {
1041 pvec.nr = find_get_entries_tag(mapping, start_index,
1042 PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
1043 pvec.pages, indices);
1044
1045 if (pvec.nr == 0)
1046 break;
1047
1048 for (i = 0; i < pvec.nr; i++) {
1049 if (indices[i] > end_index) {
1050 done = true;
1051 break;
1052 }
1053
Dan Williams3fe07912017-10-14 17:13:45 -07001054 ret = dax_writeback_one(dax_dev, mapping, indices[i],
1055 pvec.pages[i]);
Jeff Layton819ec6b2017-07-06 07:02:27 -04001056 if (ret < 0) {
1057 mapping_set_error(mapping, ret);
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001058 goto out;
Jeff Layton819ec6b2017-07-06 07:02:27 -04001059 }
Ross Zwisler9973c982016-01-22 15:10:47 -08001060 }
Jan Kara1eb643d2017-06-23 15:08:46 -07001061 start_index = indices[pvec.nr - 1] + 1;
Ross Zwisler9973c982016-01-22 15:10:47 -08001062 }
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001063out:
Dan Williamscccbce62017-01-27 13:31:42 -08001064 put_dax(dax_dev);
Ross Zwislerd14a3f42017-05-08 16:00:10 -07001065 trace_dax_writeback_range_done(inode, start_index, end_index);
1066 return (ret < 0 ? ret : 0);
Ross Zwisler9973c982016-01-22 15:10:47 -08001067}
1068EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
1069
Jan Kara31a6f1a2017-11-01 16:36:32 +01001070static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001071{
Linus Torvaldsa3841f92017-11-17 09:51:57 -08001072 return (iomap->addr + (pos & PAGE_MASK) - iomap->offset) >> 9;
Jan Kara31a6f1a2017-11-01 16:36:32 +01001073}
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001074
Jan Kara5e161e42017-11-01 16:36:33 +01001075static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,
1076 pfn_t *pfnp)
1077{
1078 const sector_t sector = dax_iomap_sector(iomap, pos);
1079 pgoff_t pgoff;
Jan Kara5e161e42017-11-01 16:36:33 +01001080 int id, rc;
1081 long length;
1082
1083 rc = bdev_dax_pgoff(iomap->bdev, sector, size, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001084 if (rc)
1085 return rc;
Dan Williamscccbce62017-01-27 13:31:42 -08001086 id = dax_read_lock();
Jan Kara5e161e42017-11-01 16:36:33 +01001087 length = dax_direct_access(iomap->dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001088 NULL, pfnp);
Jan Kara5e161e42017-11-01 16:36:33 +01001089 if (length < 0) {
1090 rc = length;
1091 goto out;
Dan Williamscccbce62017-01-27 13:31:42 -08001092 }
Jan Kara5e161e42017-11-01 16:36:33 +01001093 rc = -EINVAL;
1094 if (PFN_PHYS(length) < size)
1095 goto out;
1096 if (pfn_t_to_pfn(*pfnp) & (PHYS_PFN(size)-1))
1097 goto out;
1098 /* For larger pages we need devmap */
1099 if (length > 1 && !pfn_t_devmap(*pfnp))
1100 goto out;
1101 rc = 0;
1102out:
Dan Williamscccbce62017-01-27 13:31:42 -08001103 dax_read_unlock(id);
Jan Kara5e161e42017-11-01 16:36:33 +01001104 return rc;
Matthew Wilcoxf7ca90b2015-02-16 15:59:02 -08001105}
1106
Ross Zwislere30331f2017-09-06 16:18:39 -07001107/*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001108 * The user has performed a load from a hole in the file. Allocating a new
1109 * page in the file would cause excessive storage usage for workloads with
1110 * sparse files. Instead we insert a read-only mapping of the 4k zero page.
1111 * If this page is ever written to we will re-fault and change the mapping to
1112 * point to real DAX storage instead.
Ross Zwislere30331f2017-09-06 16:18:39 -07001113 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001114static vm_fault_t dax_load_hole(struct address_space *mapping, void *entry,
Ross Zwislere30331f2017-09-06 16:18:39 -07001115 struct vm_fault *vmf)
1116{
1117 struct inode *inode = mapping->host;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001118 unsigned long vaddr = vmf->address;
Matthew Wilcoxb90ca5c2018-09-11 21:27:44 -07001119 pfn_t pfn = pfn_to_pfn_t(my_zero_pfn(vaddr));
1120 vm_fault_t ret;
Ross Zwislere30331f2017-09-06 16:18:39 -07001121
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001122 dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001123 DAX_ZERO_PAGE, false);
1124
Souptick Joarderab77dab2018-06-07 17:04:29 -07001125 ret = vmf_insert_mixed(vmf->vma, vaddr, pfn);
Ross Zwislere30331f2017-09-06 16:18:39 -07001126 trace_dax_load_hole(inode, vmf, ret);
1127 return ret;
1128}
1129
Vishal Verma4b0228f2016-04-21 15:13:46 -04001130static bool dax_range_is_aligned(struct block_device *bdev,
1131 unsigned int offset, unsigned int length)
1132{
1133 unsigned short sector_size = bdev_logical_block_size(bdev);
1134
1135 if (!IS_ALIGNED(offset, sector_size))
1136 return false;
1137 if (!IS_ALIGNED(length, sector_size))
1138 return false;
1139
1140 return true;
1141}
1142
Dan Williamscccbce62017-01-27 13:31:42 -08001143int __dax_zero_page_range(struct block_device *bdev,
1144 struct dax_device *dax_dev, sector_t sector,
1145 unsigned int offset, unsigned int size)
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001146{
Dan Williamscccbce62017-01-27 13:31:42 -08001147 if (dax_range_is_aligned(bdev, offset, size)) {
1148 sector_t start_sector = sector + (offset >> 9);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001149
1150 return blkdev_issue_zeroout(bdev, start_sector,
Linus Torvalds53ef7d02017-05-05 18:49:20 -07001151 size >> 9, GFP_NOFS, 0);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001152 } else {
Dan Williamscccbce62017-01-27 13:31:42 -08001153 pgoff_t pgoff;
1154 long rc, id;
1155 void *kaddr;
Dan Williamscccbce62017-01-27 13:31:42 -08001156
Dan Williamse84b83b2017-05-10 19:38:13 -07001157 rc = bdev_dax_pgoff(bdev, sector, PAGE_SIZE, &pgoff);
Dan Williamscccbce62017-01-27 13:31:42 -08001158 if (rc)
1159 return rc;
1160
1161 id = dax_read_lock();
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001162 rc = dax_direct_access(dax_dev, pgoff, 1, &kaddr, NULL);
Dan Williamscccbce62017-01-27 13:31:42 -08001163 if (rc < 0) {
1164 dax_read_unlock(id);
1165 return rc;
1166 }
Dan Williams81f55872017-05-29 13:12:20 -07001167 memset(kaddr + offset, 0, size);
Mikulas Patockac3ca0152017-08-31 21:47:43 -04001168 dax_flush(dax_dev, kaddr + offset, size);
Dan Williamscccbce62017-01-27 13:31:42 -08001169 dax_read_unlock(id);
Vishal Verma4b0228f2016-04-21 15:13:46 -04001170 }
Christoph Hellwig679c8bd2016-05-09 10:47:04 +02001171 return 0;
1172}
1173EXPORT_SYMBOL_GPL(__dax_zero_page_range);
1174
Christoph Hellwiga254e562016-09-19 11:24:49 +10001175static loff_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001176dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
Christoph Hellwiga254e562016-09-19 11:24:49 +10001177 struct iomap *iomap)
1178{
Dan Williamscccbce62017-01-27 13:31:42 -08001179 struct block_device *bdev = iomap->bdev;
1180 struct dax_device *dax_dev = iomap->dax_dev;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001181 struct iov_iter *iter = data;
1182 loff_t end = pos + length, done = 0;
1183 ssize_t ret = 0;
Dan Williamsa77d4782018-03-16 17:36:44 -07001184 size_t xfer;
Dan Williamscccbce62017-01-27 13:31:42 -08001185 int id;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001186
1187 if (iov_iter_rw(iter) == READ) {
1188 end = min(end, i_size_read(inode));
1189 if (pos >= end)
1190 return 0;
1191
1192 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1193 return iov_iter_zero(min(length, end - pos), iter);
1194 }
1195
1196 if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
1197 return -EIO;
1198
Jan Karae3fce682016-08-10 17:10:28 +02001199 /*
1200 * Write can allocate block for an area which has a hole page mapped
1201 * into page tables. We have to tear down these mappings so that data
1202 * written by write(2) is visible in mmap.
1203 */
Jan Karacd656372017-05-12 15:46:50 -07001204 if (iomap->flags & IOMAP_F_NEW) {
Jan Karae3fce682016-08-10 17:10:28 +02001205 invalidate_inode_pages2_range(inode->i_mapping,
1206 pos >> PAGE_SHIFT,
1207 (end - 1) >> PAGE_SHIFT);
1208 }
1209
Dan Williamscccbce62017-01-27 13:31:42 -08001210 id = dax_read_lock();
Christoph Hellwiga254e562016-09-19 11:24:49 +10001211 while (pos < end) {
1212 unsigned offset = pos & (PAGE_SIZE - 1);
Dan Williamscccbce62017-01-27 13:31:42 -08001213 const size_t size = ALIGN(length + offset, PAGE_SIZE);
1214 const sector_t sector = dax_iomap_sector(iomap, pos);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001215 ssize_t map_len;
Dan Williamscccbce62017-01-27 13:31:42 -08001216 pgoff_t pgoff;
1217 void *kaddr;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001218
Michal Hockod1908f52017-02-03 13:13:26 -08001219 if (fatal_signal_pending(current)) {
1220 ret = -EINTR;
1221 break;
1222 }
1223
Dan Williamscccbce62017-01-27 13:31:42 -08001224 ret = bdev_dax_pgoff(bdev, sector, size, &pgoff);
1225 if (ret)
1226 break;
1227
1228 map_len = dax_direct_access(dax_dev, pgoff, PHYS_PFN(size),
Huaisheng Ye86ed9132018-07-30 15:15:48 +08001229 &kaddr, NULL);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001230 if (map_len < 0) {
1231 ret = map_len;
1232 break;
1233 }
1234
Dan Williamscccbce62017-01-27 13:31:42 -08001235 map_len = PFN_PHYS(map_len);
1236 kaddr += offset;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001237 map_len -= offset;
1238 if (map_len > end - pos)
1239 map_len = end - pos;
1240
Ross Zwislera2e050f2017-09-06 16:18:54 -07001241 /*
1242 * The userspace address for the memory copy has already been
1243 * validated via access_ok() in either vfs_read() or
1244 * vfs_write(), depending on which operation we are doing.
1245 */
Christoph Hellwiga254e562016-09-19 11:24:49 +10001246 if (iov_iter_rw(iter) == WRITE)
Dan Williamsa77d4782018-03-16 17:36:44 -07001247 xfer = dax_copy_from_iter(dax_dev, pgoff, kaddr,
Dan Williamsfec53772017-05-29 21:56:49 -07001248 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001249 else
Dan Williamsa77d4782018-03-16 17:36:44 -07001250 xfer = dax_copy_to_iter(dax_dev, pgoff, kaddr,
Dan Williamsb3a9a0c2018-05-02 06:46:33 -07001251 map_len, iter);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001252
Dan Williamsa77d4782018-03-16 17:36:44 -07001253 pos += xfer;
1254 length -= xfer;
1255 done += xfer;
1256
1257 if (xfer == 0)
1258 ret = -EFAULT;
1259 if (xfer < map_len)
1260 break;
Christoph Hellwiga254e562016-09-19 11:24:49 +10001261 }
Dan Williamscccbce62017-01-27 13:31:42 -08001262 dax_read_unlock(id);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001263
1264 return done ? done : ret;
1265}
1266
1267/**
Ross Zwisler11c59c92016-11-08 11:32:46 +11001268 * dax_iomap_rw - Perform I/O to a DAX file
Christoph Hellwiga254e562016-09-19 11:24:49 +10001269 * @iocb: The control block for this I/O
1270 * @iter: The addresses to do I/O from or to
1271 * @ops: iomap ops passed from the file system
1272 *
1273 * This function performs read and write operations to directly mapped
1274 * persistent memory. The callers needs to take care of read/write exclusion
1275 * and evicting any page cache pages in the region under I/O.
1276 */
1277ssize_t
Ross Zwisler11c59c92016-11-08 11:32:46 +11001278dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08001279 const struct iomap_ops *ops)
Christoph Hellwiga254e562016-09-19 11:24:49 +10001280{
1281 struct address_space *mapping = iocb->ki_filp->f_mapping;
1282 struct inode *inode = mapping->host;
1283 loff_t pos = iocb->ki_pos, ret = 0, done = 0;
1284 unsigned flags = 0;
1285
Christoph Hellwig168316d2017-02-08 14:43:13 -05001286 if (iov_iter_rw(iter) == WRITE) {
1287 lockdep_assert_held_exclusive(&inode->i_rwsem);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001288 flags |= IOMAP_WRITE;
Christoph Hellwig168316d2017-02-08 14:43:13 -05001289 } else {
1290 lockdep_assert_held(&inode->i_rwsem);
1291 }
Christoph Hellwiga254e562016-09-19 11:24:49 +10001292
Christoph Hellwiga254e562016-09-19 11:24:49 +10001293 while (iov_iter_count(iter)) {
1294 ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
Ross Zwisler11c59c92016-11-08 11:32:46 +11001295 iter, dax_iomap_actor);
Christoph Hellwiga254e562016-09-19 11:24:49 +10001296 if (ret <= 0)
1297 break;
1298 pos += ret;
1299 done += ret;
1300 }
1301
1302 iocb->ki_pos += done;
1303 return done ? done : ret;
1304}
Ross Zwisler11c59c92016-11-08 11:32:46 +11001305EXPORT_SYMBOL_GPL(dax_iomap_rw);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001306
Souptick Joarderab77dab2018-06-07 17:04:29 -07001307static vm_fault_t dax_fault_return(int error)
Jan Kara9f141d62016-10-19 14:34:31 +02001308{
1309 if (error == 0)
1310 return VM_FAULT_NOPAGE;
1311 if (error == -ENOMEM)
1312 return VM_FAULT_OOM;
1313 return VM_FAULT_SIGBUS;
1314}
1315
Dan Williamsaaa422c2017-11-13 16:38:44 -08001316/*
1317 * MAP_SYNC on a dax mapping guarantees dirty metadata is
1318 * flushed on write-faults (non-cow), but not read-faults.
1319 */
1320static bool dax_fault_is_synchronous(unsigned long flags,
1321 struct vm_area_struct *vma, struct iomap *iomap)
1322{
1323 return (flags & IOMAP_WRITE) && (vma->vm_flags & VM_SYNC)
1324 && (iomap->flags & IOMAP_F_DIRTY);
1325}
1326
Souptick Joarderab77dab2018-06-07 17:04:29 -07001327static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,
Jan Karac0b24622018-01-07 16:38:43 -05001328 int *iomap_errp, const struct iomap_ops *ops)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001329{
Jan Karaa0987ad2017-11-01 16:36:34 +01001330 struct vm_area_struct *vma = vmf->vma;
1331 struct address_space *mapping = vma->vm_file->f_mapping;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001332 struct inode *inode = mapping->host;
Jan Kara1a29d852016-12-14 15:07:01 -08001333 unsigned long vaddr = vmf->address;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001334 loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001335 struct iomap iomap = { 0 };
Jan Kara9484ab12016-11-10 10:26:50 +11001336 unsigned flags = IOMAP_FAULT;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001337 int error, major = 0;
Jan Karad2c43ef2017-11-01 16:36:35 +01001338 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001339 bool sync;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001340 vm_fault_t ret = 0;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001341 void *entry;
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001342 pfn_t pfn;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001343
Souptick Joarderab77dab2018-06-07 17:04:29 -07001344 trace_dax_pte_fault(inode, vmf, ret);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001345 /*
1346 * Check whether offset isn't beyond end of file now. Caller is supposed
1347 * to hold locks serializing us with truncate / punch hole so this is
1348 * a reliable test.
1349 */
Ross Zwislera9c42b32017-05-08 16:00:00 -07001350 if (pos >= i_size_read(inode)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001351 ret = VM_FAULT_SIGBUS;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001352 goto out;
1353 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001354
Jan Karad2c43ef2017-11-01 16:36:35 +01001355 if (write && !vmf->cow_page)
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001356 flags |= IOMAP_WRITE;
1357
Jan Kara13e451f2017-05-12 15:46:57 -07001358 entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
1359 if (IS_ERR(entry)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001360 ret = dax_fault_return(PTR_ERR(entry));
Jan Kara13e451f2017-05-12 15:46:57 -07001361 goto out;
1362 }
1363
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001364 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001365 * It is possible, particularly with mixed reads & writes to private
1366 * mappings, that we have raced with a PMD fault that overlaps with
1367 * the PTE we need to set up. If so just return and the fault will be
1368 * retried.
1369 */
1370 if (pmd_trans_huge(*vmf->pmd) || pmd_devmap(*vmf->pmd)) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001371 ret = VM_FAULT_NOPAGE;
Ross Zwislere2093922017-06-02 14:46:37 -07001372 goto unlock_entry;
1373 }
1374
1375 /*
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001376 * Note that we don't bother to use iomap_apply here: DAX required
1377 * the file system block size to be equal the page size, which means
1378 * that we never have to deal with more than a single extent here.
1379 */
1380 error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
Jan Karac0b24622018-01-07 16:38:43 -05001381 if (iomap_errp)
1382 *iomap_errp = error;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001383 if (error) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001384 ret = dax_fault_return(error);
Jan Kara13e451f2017-05-12 15:46:57 -07001385 goto unlock_entry;
Ross Zwislera9c42b32017-05-08 16:00:00 -07001386 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001387 if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
Jan Kara13e451f2017-05-12 15:46:57 -07001388 error = -EIO; /* fs corruption? */
1389 goto error_finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001390 }
1391
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001392 if (vmf->cow_page) {
Jan Kara31a6f1a2017-11-01 16:36:32 +01001393 sector_t sector = dax_iomap_sector(&iomap, pos);
1394
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001395 switch (iomap.type) {
1396 case IOMAP_HOLE:
1397 case IOMAP_UNWRITTEN:
1398 clear_user_highpage(vmf->cow_page, vaddr);
1399 break;
1400 case IOMAP_MAPPED:
Dan Williamscccbce62017-01-27 13:31:42 -08001401 error = copy_user_dax(iomap.bdev, iomap.dax_dev,
1402 sector, PAGE_SIZE, vmf->cow_page, vaddr);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001403 break;
1404 default:
1405 WARN_ON_ONCE(1);
1406 error = -EIO;
1407 break;
1408 }
1409
1410 if (error)
Jan Kara13e451f2017-05-12 15:46:57 -07001411 goto error_finish_iomap;
Jan Karab1aa8122016-12-14 15:07:24 -08001412
1413 __SetPageUptodate(vmf->cow_page);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001414 ret = finish_fault(vmf);
1415 if (!ret)
1416 ret = VM_FAULT_DONE_COW;
Jan Kara13e451f2017-05-12 15:46:57 -07001417 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001418 }
1419
Dan Williamsaaa422c2017-11-13 16:38:44 -08001420 sync = dax_fault_is_synchronous(flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001421
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001422 switch (iomap.type) {
1423 case IOMAP_MAPPED:
1424 if (iomap.flags & IOMAP_F_NEW) {
1425 count_vm_event(PGMAJFAULT);
Jan Karaa0987ad2017-11-01 16:36:34 +01001426 count_memcg_event_mm(vma->vm_mm, PGMAJFAULT);
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001427 major = VM_FAULT_MAJOR;
1428 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001429 error = dax_iomap_pfn(&iomap, pos, PAGE_SIZE, &pfn);
1430 if (error < 0)
1431 goto error_finish_iomap;
1432
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001433 entry = dax_insert_entry(mapping, vmf, entry, pfn,
Jan Karacaa51d22017-11-01 16:36:42 +01001434 0, write && !sync);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001435
Jan Karacaa51d22017-11-01 16:36:42 +01001436 /*
1437 * If we are doing synchronous page fault and inode needs fsync,
1438 * we can insert PTE into page tables only after that happens.
1439 * Skip insertion for now and return the pfn so that caller can
1440 * insert it after fsync is done.
1441 */
1442 if (sync) {
1443 if (WARN_ON_ONCE(!pfnp)) {
1444 error = -EIO;
1445 goto error_finish_iomap;
1446 }
1447 *pfnp = pfn;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001448 ret = VM_FAULT_NEEDDSYNC | major;
Jan Karacaa51d22017-11-01 16:36:42 +01001449 goto finish_iomap;
1450 }
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001451 trace_dax_insert_mapping(inode, vmf, entry);
1452 if (write)
Souptick Joarderab77dab2018-06-07 17:04:29 -07001453 ret = vmf_insert_mixed_mkwrite(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001454 else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001455 ret = vmf_insert_mixed(vma, vaddr, pfn);
Jan Kara1b5a1cb2017-11-01 16:36:36 +01001456
Souptick Joarderab77dab2018-06-07 17:04:29 -07001457 goto finish_iomap;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001458 case IOMAP_UNWRITTEN:
1459 case IOMAP_HOLE:
Jan Karad2c43ef2017-11-01 16:36:35 +01001460 if (!write) {
Souptick Joarderab77dab2018-06-07 17:04:29 -07001461 ret = dax_load_hole(mapping, entry, vmf);
Jan Kara13e451f2017-05-12 15:46:57 -07001462 goto finish_iomap;
Ross Zwisler15502902016-11-08 11:33:26 +11001463 }
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001464 /*FALLTHRU*/
1465 default:
1466 WARN_ON_ONCE(1);
1467 error = -EIO;
1468 break;
1469 }
1470
Jan Kara13e451f2017-05-12 15:46:57 -07001471 error_finish_iomap:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001472 ret = dax_fault_return(error);
Jan Kara9f141d62016-10-19 14:34:31 +02001473 finish_iomap:
1474 if (ops->iomap_end) {
1475 int copied = PAGE_SIZE;
1476
Souptick Joarderab77dab2018-06-07 17:04:29 -07001477 if (ret & VM_FAULT_ERROR)
Jan Kara9f141d62016-10-19 14:34:31 +02001478 copied = 0;
1479 /*
1480 * The fault is done by now and there's no way back (other
1481 * thread may be already happily using PTE we have installed).
1482 * Just ignore error from ->iomap_end since we cannot do much
1483 * with it.
1484 */
1485 ops->iomap_end(inode, pos, PAGE_SIZE, copied, flags, &iomap);
Ross Zwisler15502902016-11-08 11:33:26 +11001486 }
Jan Kara13e451f2017-05-12 15:46:57 -07001487 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001488 put_locked_mapping_entry(mapping, vmf->pgoff);
Jan Kara13e451f2017-05-12 15:46:57 -07001489 out:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001490 trace_dax_pte_fault_done(inode, vmf, ret);
1491 return ret | major;
Christoph Hellwiga7d73fe2016-09-19 11:24:50 +10001492}
Ross Zwisler642261a2016-11-08 11:34:45 +11001493
1494#ifdef CONFIG_FS_DAX_PMD
Souptick Joarderab77dab2018-06-07 17:04:29 -07001495static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap,
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001496 void *entry)
Ross Zwisler642261a2016-11-08 11:34:45 +11001497{
Dave Jiangf4200392017-02-22 15:40:06 -08001498 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1499 unsigned long pmd_addr = vmf->address & PMD_MASK;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001500 struct inode *inode = mapping->host;
Ross Zwisler642261a2016-11-08 11:34:45 +11001501 struct page *zero_page;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001502 void *ret = NULL;
Ross Zwisler642261a2016-11-08 11:34:45 +11001503 spinlock_t *ptl;
1504 pmd_t pmd_entry;
Dan Williams3fe07912017-10-14 17:13:45 -07001505 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001506
Dave Jiangf4200392017-02-22 15:40:06 -08001507 zero_page = mm_get_huge_zero_page(vmf->vma->vm_mm);
Ross Zwisler642261a2016-11-08 11:34:45 +11001508
1509 if (unlikely(!zero_page))
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001510 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001511
Dan Williams3fe07912017-10-14 17:13:45 -07001512 pfn = page_to_pfn_t(zero_page);
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001513 ret = dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001514 DAX_PMD | DAX_ZERO_PAGE, false);
Ross Zwisler642261a2016-11-08 11:34:45 +11001515
Dave Jiangf4200392017-02-22 15:40:06 -08001516 ptl = pmd_lock(vmf->vma->vm_mm, vmf->pmd);
1517 if (!pmd_none(*(vmf->pmd))) {
Ross Zwisler642261a2016-11-08 11:34:45 +11001518 spin_unlock(ptl);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001519 goto fallback;
Ross Zwisler642261a2016-11-08 11:34:45 +11001520 }
1521
Dave Jiangf4200392017-02-22 15:40:06 -08001522 pmd_entry = mk_pmd(zero_page, vmf->vma->vm_page_prot);
Ross Zwisler642261a2016-11-08 11:34:45 +11001523 pmd_entry = pmd_mkhuge(pmd_entry);
Dave Jiangf4200392017-02-22 15:40:06 -08001524 set_pmd_at(vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001525 spin_unlock(ptl);
Dave Jiangf4200392017-02-22 15:40:06 -08001526 trace_dax_pmd_load_hole(inode, vmf, zero_page, ret);
Ross Zwisler642261a2016-11-08 11:34:45 +11001527 return VM_FAULT_NOPAGE;
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001528
1529fallback:
Dave Jiangf4200392017-02-22 15:40:06 -08001530 trace_dax_pmd_load_hole_fallback(inode, vmf, zero_page, ret);
Ross Zwisler653b2ea2017-02-22 15:39:57 -08001531 return VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001532}
1533
Souptick Joarderab77dab2018-06-07 17:04:29 -07001534static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Dave Jianga2d58162017-02-24 14:56:59 -08001535 const struct iomap_ops *ops)
Ross Zwisler642261a2016-11-08 11:34:45 +11001536{
Dave Jiangf4200392017-02-22 15:40:06 -08001537 struct vm_area_struct *vma = vmf->vma;
Ross Zwisler642261a2016-11-08 11:34:45 +11001538 struct address_space *mapping = vma->vm_file->f_mapping;
Dave Jiangd8a849e2017-02-22 15:40:03 -08001539 unsigned long pmd_addr = vmf->address & PMD_MASK;
1540 bool write = vmf->flags & FAULT_FLAG_WRITE;
Jan Karacaa51d22017-11-01 16:36:42 +01001541 bool sync;
Jan Kara9484ab12016-11-10 10:26:50 +11001542 unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
Ross Zwisler642261a2016-11-08 11:34:45 +11001543 struct inode *inode = mapping->host;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001544 vm_fault_t result = VM_FAULT_FALLBACK;
Ross Zwisler642261a2016-11-08 11:34:45 +11001545 struct iomap iomap = { 0 };
1546 pgoff_t max_pgoff, pgoff;
Ross Zwisler642261a2016-11-08 11:34:45 +11001547 void *entry;
1548 loff_t pos;
1549 int error;
Jan Kara302a5e32017-11-01 16:36:37 +01001550 pfn_t pfn;
Ross Zwisler642261a2016-11-08 11:34:45 +11001551
Ross Zwisler282a8e02017-02-22 15:39:50 -08001552 /*
1553 * Check whether offset isn't beyond end of file now. Caller is
1554 * supposed to hold locks serializing us with truncate / punch hole so
1555 * this is a reliable test.
1556 */
1557 pgoff = linear_page_index(vma, pmd_addr);
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001558 max_pgoff = DIV_ROUND_UP(i_size_read(inode), PAGE_SIZE);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001559
Dave Jiangf4200392017-02-22 15:40:06 -08001560 trace_dax_pmd_fault(inode, vmf, max_pgoff, 0);
Ross Zwisler282a8e02017-02-22 15:39:50 -08001561
Ross Zwislerfffa2812017-08-25 15:55:36 -07001562 /*
1563 * Make sure that the faulting address's PMD offset (color) matches
1564 * the PMD offset from the start of the file. This is necessary so
1565 * that a PMD range in the page table overlaps exactly with a PMD
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001566 * range in the page cache.
Ross Zwislerfffa2812017-08-25 15:55:36 -07001567 */
1568 if ((vmf->pgoff & PG_PMD_COLOUR) !=
1569 ((vmf->address >> PAGE_SHIFT) & PG_PMD_COLOUR))
1570 goto fallback;
1571
Ross Zwisler642261a2016-11-08 11:34:45 +11001572 /* Fall back to PTEs if we're going to COW */
1573 if (write && !(vma->vm_flags & VM_SHARED))
1574 goto fallback;
1575
1576 /* If the PMD would extend outside the VMA */
1577 if (pmd_addr < vma->vm_start)
1578 goto fallback;
1579 if ((pmd_addr + PMD_SIZE) > vma->vm_end)
1580 goto fallback;
1581
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001582 if (pgoff >= max_pgoff) {
Ross Zwisler282a8e02017-02-22 15:39:50 -08001583 result = VM_FAULT_SIGBUS;
1584 goto out;
1585 }
Ross Zwisler642261a2016-11-08 11:34:45 +11001586
1587 /* If the PMD would extend beyond the file size */
Jeff Moyer957ac8c2017-11-14 20:37:27 -05001588 if ((pgoff | PG_PMD_COLOUR) >= max_pgoff)
Ross Zwisler642261a2016-11-08 11:34:45 +11001589 goto fallback;
1590
1591 /*
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001592 * grab_mapping_entry() will make sure we get a 2MiB empty entry, a
1593 * 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page
1594 * is already in the tree, for instance), it will return -EEXIST and
1595 * we just fall back to 4k entries.
Jan Kara9f141d62016-10-19 14:34:31 +02001596 */
Matthew Wilcox3159f942017-11-03 13:30:42 -04001597 entry = grab_mapping_entry(mapping, pgoff, DAX_PMD);
Jan Kara9f141d62016-10-19 14:34:31 +02001598 if (IS_ERR(entry))
Ross Zwisler876f2942017-05-12 15:47:00 -07001599 goto fallback;
1600
1601 /*
Ross Zwislere2093922017-06-02 14:46:37 -07001602 * It is possible, particularly with mixed reads & writes to private
1603 * mappings, that we have raced with a PTE fault that overlaps with
1604 * the PMD we need to set up. If so just return and the fault will be
1605 * retried.
1606 */
1607 if (!pmd_none(*vmf->pmd) && !pmd_trans_huge(*vmf->pmd) &&
1608 !pmd_devmap(*vmf->pmd)) {
1609 result = 0;
1610 goto unlock_entry;
1611 }
1612
1613 /*
Ross Zwisler876f2942017-05-12 15:47:00 -07001614 * Note that we don't use iomap_apply here. We aren't doing I/O, only
1615 * setting up a mapping, so really we're using iomap_begin() as a way
1616 * to look up our filesystem block.
1617 */
1618 pos = (loff_t)pgoff << PAGE_SHIFT;
1619 error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
1620 if (error)
1621 goto unlock_entry;
1622
1623 if (iomap.offset + iomap.length < pos + PMD_SIZE)
Jan Kara9f141d62016-10-19 14:34:31 +02001624 goto finish_iomap;
1625
Dan Williamsaaa422c2017-11-13 16:38:44 -08001626 sync = dax_fault_is_synchronous(iomap_flags, vma, &iomap);
Jan Karacaa51d22017-11-01 16:36:42 +01001627
Ross Zwisler642261a2016-11-08 11:34:45 +11001628 switch (iomap.type) {
1629 case IOMAP_MAPPED:
Jan Kara302a5e32017-11-01 16:36:37 +01001630 error = dax_iomap_pfn(&iomap, pos, PMD_SIZE, &pfn);
1631 if (error < 0)
1632 goto finish_iomap;
1633
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001634 entry = dax_insert_entry(mapping, vmf, entry, pfn,
Matthew Wilcox3159f942017-11-03 13:30:42 -04001635 DAX_PMD, write && !sync);
Jan Kara302a5e32017-11-01 16:36:37 +01001636
Jan Karacaa51d22017-11-01 16:36:42 +01001637 /*
1638 * If we are doing synchronous page fault and inode needs fsync,
1639 * we can insert PMD into page tables only after that happens.
1640 * Skip insertion for now and return the pfn so that caller can
1641 * insert it after fsync is done.
1642 */
1643 if (sync) {
1644 if (WARN_ON_ONCE(!pfnp))
1645 goto finish_iomap;
1646 *pfnp = pfn;
1647 result = VM_FAULT_NEEDDSYNC;
1648 goto finish_iomap;
1649 }
1650
Jan Kara302a5e32017-11-01 16:36:37 +01001651 trace_dax_pmd_insert_mapping(inode, vmf, PMD_SIZE, pfn, entry);
1652 result = vmf_insert_pfn_pmd(vma, vmf->address, vmf->pmd, pfn,
1653 write);
Ross Zwisler642261a2016-11-08 11:34:45 +11001654 break;
1655 case IOMAP_UNWRITTEN:
1656 case IOMAP_HOLE:
1657 if (WARN_ON_ONCE(write))
Ross Zwisler876f2942017-05-12 15:47:00 -07001658 break;
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001659 result = dax_pmd_load_hole(vmf, &iomap, entry);
Ross Zwisler642261a2016-11-08 11:34:45 +11001660 break;
1661 default:
1662 WARN_ON_ONCE(1);
1663 break;
1664 }
1665
Jan Kara9f141d62016-10-19 14:34:31 +02001666 finish_iomap:
1667 if (ops->iomap_end) {
1668 int copied = PMD_SIZE;
1669
1670 if (result == VM_FAULT_FALLBACK)
1671 copied = 0;
1672 /*
1673 * The fault is done by now and there's no way back (other
1674 * thread may be already happily using PMD we have installed).
1675 * Just ignore error from ->iomap_end since we cannot do much
1676 * with it.
1677 */
1678 ops->iomap_end(inode, pos, PMD_SIZE, copied, iomap_flags,
1679 &iomap);
1680 }
Ross Zwisler876f2942017-05-12 15:47:00 -07001681 unlock_entry:
Ross Zwisler91d25ba2017-09-06 16:18:43 -07001682 put_locked_mapping_entry(mapping, pgoff);
Ross Zwisler642261a2016-11-08 11:34:45 +11001683 fallback:
1684 if (result == VM_FAULT_FALLBACK) {
Dave Jiangd8a849e2017-02-22 15:40:03 -08001685 split_huge_pmd(vma, vmf->pmd, vmf->address);
Ross Zwisler642261a2016-11-08 11:34:45 +11001686 count_vm_event(THP_FAULT_FALLBACK);
1687 }
Ross Zwisler282a8e02017-02-22 15:39:50 -08001688out:
Dave Jiangf4200392017-02-22 15:40:06 -08001689 trace_dax_pmd_fault_done(inode, vmf, max_pgoff, result);
Ross Zwisler642261a2016-11-08 11:34:45 +11001690 return result;
1691}
Dave Jianga2d58162017-02-24 14:56:59 -08001692#else
Souptick Joarderab77dab2018-06-07 17:04:29 -07001693static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,
Arnd Bergmann01cddfe2017-02-27 14:26:44 -08001694 const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001695{
1696 return VM_FAULT_FALLBACK;
1697}
Ross Zwisler642261a2016-11-08 11:34:45 +11001698#endif /* CONFIG_FS_DAX_PMD */
Dave Jianga2d58162017-02-24 14:56:59 -08001699
1700/**
1701 * dax_iomap_fault - handle a page fault on a DAX file
1702 * @vmf: The description of the fault
Jan Karacec04e82017-11-01 16:36:38 +01001703 * @pe_size: Size of the page to fault in
Jan Kara9a0dd422017-11-01 16:36:39 +01001704 * @pfnp: PFN to insert for synchronous faults if fsync is required
Jan Karac0b24622018-01-07 16:38:43 -05001705 * @iomap_errp: Storage for detailed error code in case of error
Jan Karacec04e82017-11-01 16:36:38 +01001706 * @ops: Iomap ops passed from the file system
Dave Jianga2d58162017-02-24 14:56:59 -08001707 *
1708 * When a page fault occurs, filesystems may call this helper in
1709 * their fault handler for DAX files. dax_iomap_fault() assumes the caller
1710 * has done all the necessary locking for page fault to proceed
1711 * successfully.
1712 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001713vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
Jan Karac0b24622018-01-07 16:38:43 -05001714 pfn_t *pfnp, int *iomap_errp, const struct iomap_ops *ops)
Dave Jianga2d58162017-02-24 14:56:59 -08001715{
Dave Jiangc791ace2017-02-24 14:57:08 -08001716 switch (pe_size) {
1717 case PE_SIZE_PTE:
Jan Karac0b24622018-01-07 16:38:43 -05001718 return dax_iomap_pte_fault(vmf, pfnp, iomap_errp, ops);
Dave Jiangc791ace2017-02-24 14:57:08 -08001719 case PE_SIZE_PMD:
Jan Kara9a0dd422017-11-01 16:36:39 +01001720 return dax_iomap_pmd_fault(vmf, pfnp, ops);
Dave Jianga2d58162017-02-24 14:56:59 -08001721 default:
1722 return VM_FAULT_FALLBACK;
1723 }
1724}
1725EXPORT_SYMBOL_GPL(dax_iomap_fault);
Jan Kara71eab6d2017-11-01 16:36:43 +01001726
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001727/*
Jan Kara71eab6d2017-11-01 16:36:43 +01001728 * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables
1729 * @vmf: The description of the fault
1730 * @pe_size: Size of entry to be inserted
1731 * @pfn: PFN to insert
1732 *
Matthew Wilcoxa77d19f2018-03-27 13:39:38 -04001733 * This function inserts a writeable PTE or PMD entry into the page tables
1734 * for an mmaped DAX file. It also marks the page cache entry as dirty.
Jan Kara71eab6d2017-11-01 16:36:43 +01001735 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001736static vm_fault_t dax_insert_pfn_mkwrite(struct vm_fault *vmf,
Jan Kara71eab6d2017-11-01 16:36:43 +01001737 enum page_entry_size pe_size,
1738 pfn_t pfn)
1739{
1740 struct address_space *mapping = vmf->vma->vm_file->f_mapping;
1741 void *entry, **slot;
1742 pgoff_t index = vmf->pgoff;
Souptick Joarderab77dab2018-06-07 17:04:29 -07001743 vm_fault_t ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001744
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001745 xa_lock_irq(&mapping->i_pages);
Jan Kara71eab6d2017-11-01 16:36:43 +01001746 entry = get_unlocked_mapping_entry(mapping, index, &slot);
1747 /* Did we race with someone splitting entry or so? */
1748 if (!entry ||
1749 (pe_size == PE_SIZE_PTE && !dax_is_pte_entry(entry)) ||
1750 (pe_size == PE_SIZE_PMD && !dax_is_pmd_entry(entry))) {
1751 put_unlocked_mapping_entry(mapping, index, entry);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001752 xa_unlock_irq(&mapping->i_pages);
Jan Kara71eab6d2017-11-01 16:36:43 +01001753 trace_dax_insert_pfn_mkwrite_no_entry(mapping->host, vmf,
1754 VM_FAULT_NOPAGE);
1755 return VM_FAULT_NOPAGE;
1756 }
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001757 radix_tree_tag_set(&mapping->i_pages, index, PAGECACHE_TAG_DIRTY);
Jan Kara71eab6d2017-11-01 16:36:43 +01001758 entry = lock_slot(mapping, slot);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -07001759 xa_unlock_irq(&mapping->i_pages);
Jan Kara71eab6d2017-11-01 16:36:43 +01001760 switch (pe_size) {
1761 case PE_SIZE_PTE:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001762 ret = vmf_insert_mixed_mkwrite(vmf->vma, vmf->address, pfn);
Jan Kara71eab6d2017-11-01 16:36:43 +01001763 break;
1764#ifdef CONFIG_FS_DAX_PMD
1765 case PE_SIZE_PMD:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001766 ret = vmf_insert_pfn_pmd(vmf->vma, vmf->address, vmf->pmd,
Jan Kara71eab6d2017-11-01 16:36:43 +01001767 pfn, true);
1768 break;
1769#endif
1770 default:
Souptick Joarderab77dab2018-06-07 17:04:29 -07001771 ret = VM_FAULT_FALLBACK;
Jan Kara71eab6d2017-11-01 16:36:43 +01001772 }
1773 put_locked_mapping_entry(mapping, index);
Souptick Joarderab77dab2018-06-07 17:04:29 -07001774 trace_dax_insert_pfn_mkwrite(mapping->host, vmf, ret);
1775 return ret;
Jan Kara71eab6d2017-11-01 16:36:43 +01001776}
1777
1778/**
1779 * dax_finish_sync_fault - finish synchronous page fault
1780 * @vmf: The description of the fault
1781 * @pe_size: Size of entry to be inserted
1782 * @pfn: PFN to insert
1783 *
1784 * This function ensures that the file range touched by the page fault is
1785 * stored persistently on the media and handles inserting of appropriate page
1786 * table entry.
1787 */
Souptick Joarderab77dab2018-06-07 17:04:29 -07001788vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
1789 enum page_entry_size pe_size, pfn_t pfn)
Jan Kara71eab6d2017-11-01 16:36:43 +01001790{
1791 int err;
1792 loff_t start = ((loff_t)vmf->pgoff) << PAGE_SHIFT;
1793 size_t len = 0;
1794
1795 if (pe_size == PE_SIZE_PTE)
1796 len = PAGE_SIZE;
1797 else if (pe_size == PE_SIZE_PMD)
1798 len = PMD_SIZE;
1799 else
1800 WARN_ON_ONCE(1);
1801 err = vfs_fsync_range(vmf->vma->vm_file, start, start + len - 1, 1);
1802 if (err)
1803 return VM_FAULT_SIGBUS;
1804 return dax_insert_pfn_mkwrite(vmf, pe_size, pfn);
1805}
1806EXPORT_SYMBOL_GPL(dax_finish_sync_fault);